corda/src/heapdump.cpp

116 lines
2.4 KiB
C++
Raw Normal View History

2015-03-13 12:52:59 -06:00
/* Copyright (c) 2008-2015, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
#include "avian/machine.h"
#include "avian/heapwalk.h"
using namespace vm;
namespace {
namespace local {
2014-07-11 09:50:18 -06:00
enum { Root, Size, ClassName, Push, Pop };
void write1(FILE* out, uint8_t v)
{
size_t n UNUSED = fwrite(&v, 1, 1, out);
}
2014-07-11 09:50:18 -06:00
void write4(FILE* out, uint32_t v)
{
2014-07-11 09:50:18 -06:00
uint8_t b[] = {static_cast<uint8_t>(v >> 24),
static_cast<uint8_t>((v >> 16) & 0xFF),
static_cast<uint8_t>((v >> 8) & 0xFF),
static_cast<uint8_t>(v & 0xFF)};
2012-06-28 10:30:49 -06:00
size_t n UNUSED = fwrite(b, 4, 1, out);
}
2014-07-11 09:50:18 -06:00
void writeString(FILE* out, int8_t* p, unsigned size)
{
write4(out, size);
size_t n UNUSED = fwrite(p, size, 1, out);
}
2014-07-11 09:50:18 -06:00
unsigned objectSize(Thread* t, object o)
{
return extendedSize(t, o, baseSize(t, o, objectClass(t, o)));
}
2014-07-11 09:50:18 -06:00
} // namespace local
2014-07-11 09:50:18 -06:00
} // namespace
namespace vm {
2014-07-11 09:50:18 -06:00
void dumpHeap(Thread* t, FILE* out)
{
2014-07-11 09:50:18 -06:00
class Visitor : public HeapVisitor {
public:
2014-07-11 09:50:18 -06:00
Visitor(Thread* t, FILE* out) : t(t), out(out), nextNumber(1)
{
}
2014-07-11 09:50:18 -06:00
virtual void root()
{
write1(out, local::Root);
}
2014-07-11 09:50:18 -06:00
virtual unsigned visitNew(object p)
{
if (p) {
unsigned number = nextNumber++;
local::write4(out, number);
local::write1(out, local::Size);
local::write4(out, local::objectSize(t, p));
2014-05-28 22:17:25 -06:00
if (objectClass(t, p) == type(t, GcClass::Type)) {
2014-07-16 14:18:42 -06:00
GcByteArray* name = static_cast<GcClass*>(p)->name();
if (name) {
local::write1(out, local::ClassName);
2014-07-16 14:18:42 -06:00
local::writeString(out, name->body().begin(), name->length() - 1);
}
}
return number;
2008-10-22 18:05:34 -06:00
} else {
return 0;
2008-10-22 18:05:34 -06:00
}
}
2014-07-11 09:50:18 -06:00
virtual void visitOld(object, unsigned number)
{
local::write4(out, number);
}
2014-07-11 09:50:18 -06:00
virtual void push(object, unsigned, unsigned)
{
local::write1(out, local::Push);
}
2014-07-11 09:50:18 -06:00
virtual void pop()
{
local::write1(out, local::Pop);
}
Thread* t;
FILE* out;
unsigned nextNumber;
2008-11-23 16:58:01 -07:00
} visitor(t, out);
2008-11-23 16:58:01 -07:00
HeapWalker* w = makeHeapWalker(t, &visitor);
w->visitAllRoots();
w->dispose();
}
2014-07-11 09:50:18 -06:00
} // namespace vm