corda/src/heapdump.cpp

114 lines
2.2 KiB
C++
Raw Normal View History

/* Copyright (c) 2008, 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 "machine.h"
#include "heapwalk.h"
using namespace vm;
namespace {
enum {
Root,
2008-10-23 00:05:34 +00:00
Size,
ClassName,
Push,
2008-10-23 00:05:34 +00:00
Pop
};
void
write1(FILE* out, uint8_t v)
{
size_t n UNUSED = fwrite(&v, 1, 1, c->out);
}
void
write4(FILE* out, uint32_t v)
{
uint8_t b[] = { v >> 24, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF };
size_t n UNUSED = fwrite(b, 4, 1, out);
}
void
writeString(FILE* out, int8_t* p, unsigned size)
{
write4(c, size);
size_t n UNUSED = fwrite(p, size, 1, out);
}
unsigned
objectSize(Thread* t, object o)
{
return extendedSize(t, o, baseSize(t, o, objectClass(t, o)));
}
} // namespace
namespace vm {
void
dumpHeap(Thread* t, FILE* out)
{
2008-11-23 23:58:01 +00:00
class Visitor: public HeapVisitor {
public:
2008-11-23 23:58:01 +00:00
Visitor(Thread* t, FILE* out): t(t), out(out), nextNumber(1) { }
virtual void root() {
write1(out, Root);
}
virtual unsigned visitNew(object p) {
if (p) {
unsigned number = nextNumber++;
write4(out, number);
write1(out, Size);
write4(out, objectSize(t, p));
if (objectClass(t, p) == arrayBody(t, t->m->types, Machine::ClassType))
{
object name = className(t, p);
if (name) {
write1(out, ClassName);
writeString(out, &byteArrayBody(t, name, 0),
byteArrayLength(t, name) - 1);
}
}
return number;
2008-10-23 00:05:34 +00:00
} else {
return 0;
2008-10-23 00:05:34 +00:00
}
}
virtual void visitOld(object, unsigned number) {
write4(out, number);
}
2008-12-03 02:38:32 +00:00
virtual void push(object, unsigned, unsigned) {
write1(out, Push);
}
virtual void pop() {
write1(out, Pop);
}
Thread* t;
FILE* out;
unsigned nextNumber;
2008-11-23 23:58:01 +00:00
} visitor(t, out);
2008-11-23 23:58:01 +00:00
HeapWalker* w = makeHeapWalker(t, &visitor);
w->visitAllRoots();
w->dispose();
}
} // namespace vm