2013-11-08 12:06:55 -05:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
objspace_dump.c - Heap dumping ObjectSpace extender for MRI.
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Sat Oct 11 10:11:00 2013
|
|
|
|
|
|
|
|
NOTE: This extension library is not expected to exist except C Ruby.
|
|
|
|
|
|
|
|
All the files in this distribution are covered under the Ruby's
|
|
|
|
license (see the file COPYING).
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#include "ruby/ruby.h"
|
|
|
|
#include "ruby/debug.h"
|
|
|
|
#include "ruby/encoding.h"
|
|
|
|
#include "ruby/io.h"
|
|
|
|
#include "gc.h"
|
|
|
|
#include "node.h"
|
|
|
|
#include "vm_core.h"
|
|
|
|
#include "objspace.h"
|
2013-12-09 21:26:09 -05:00
|
|
|
#include "internal.h"
|
2013-11-08 12:06:55 -05:00
|
|
|
|
|
|
|
static VALUE sym_output, sym_stdout, sym_string, sym_file;
|
|
|
|
|
|
|
|
struct dump_config {
|
|
|
|
VALUE type;
|
|
|
|
FILE *stream;
|
|
|
|
VALUE string;
|
|
|
|
int roots;
|
|
|
|
const char *root_category;
|
|
|
|
VALUE cur_obj;
|
|
|
|
VALUE cur_obj_klass;
|
|
|
|
size_t cur_obj_references;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
dump_append(struct dump_config *dc, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list vl;
|
|
|
|
va_start(vl, format);
|
|
|
|
|
|
|
|
if (dc->stream) {
|
|
|
|
vfprintf(dc->stream, format, vl);
|
|
|
|
}
|
|
|
|
else if (dc->string)
|
|
|
|
rb_str_vcatf(dc->string, format, vl);
|
|
|
|
|
|
|
|
va_end(vl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dump_append_string_value(struct dump_config *dc, VALUE obj)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char c, *value;
|
|
|
|
|
|
|
|
dump_append(dc, "\"");
|
|
|
|
for (i = 0, value = RSTRING_PTR(obj); i < RSTRING_LEN(obj); i++) {
|
|
|
|
switch ((c = value[i])) {
|
2013-11-09 01:16:42 -05:00
|
|
|
case '\\':
|
|
|
|
case '"':
|
|
|
|
dump_append(dc, "\\%c", c);
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
dump_append(dc, "\\u0000");
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
dump_append(dc, "\\b");
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
dump_append(dc, "\\t");
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
dump_append(dc, "\\f");
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
dump_append(dc, "\\n");
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
dump_append(dc, "\\r");
|
|
|
|
break;
|
|
|
|
default:
|
2013-11-26 00:27:22 -05:00
|
|
|
if (c <= 0x1f)
|
|
|
|
dump_append(dc, "\\u%04d", c);
|
|
|
|
else
|
|
|
|
dump_append(dc, "%c", c);
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dump_append(dc, "\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char *
|
|
|
|
obj_type(VALUE obj)
|
|
|
|
{
|
|
|
|
switch (BUILTIN_TYPE(obj)) {
|
|
|
|
#define CASE_TYPE(type) case T_##type: return #type; break
|
|
|
|
CASE_TYPE(NONE);
|
|
|
|
CASE_TYPE(NIL);
|
|
|
|
CASE_TYPE(OBJECT);
|
|
|
|
CASE_TYPE(CLASS);
|
|
|
|
CASE_TYPE(ICLASS);
|
|
|
|
CASE_TYPE(MODULE);
|
|
|
|
CASE_TYPE(FLOAT);
|
|
|
|
CASE_TYPE(STRING);
|
|
|
|
CASE_TYPE(REGEXP);
|
|
|
|
CASE_TYPE(ARRAY);
|
|
|
|
CASE_TYPE(HASH);
|
|
|
|
CASE_TYPE(STRUCT);
|
|
|
|
CASE_TYPE(BIGNUM);
|
|
|
|
CASE_TYPE(FILE);
|
|
|
|
CASE_TYPE(FIXNUM);
|
|
|
|
CASE_TYPE(TRUE);
|
|
|
|
CASE_TYPE(FALSE);
|
|
|
|
CASE_TYPE(DATA);
|
|
|
|
CASE_TYPE(MATCH);
|
|
|
|
CASE_TYPE(SYMBOL);
|
|
|
|
CASE_TYPE(RATIONAL);
|
|
|
|
CASE_TYPE(COMPLEX);
|
|
|
|
CASE_TYPE(UNDEF);
|
|
|
|
CASE_TYPE(NODE);
|
|
|
|
CASE_TYPE(ZOMBIE);
|
|
|
|
#undef CASE_TYPE
|
|
|
|
}
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
reachable_object_i(VALUE ref, void *data)
|
|
|
|
{
|
|
|
|
struct dump_config *dc = (struct dump_config *)data;
|
|
|
|
|
|
|
|
if (dc->cur_obj_klass == ref)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (dc->cur_obj_references == 0)
|
|
|
|
dump_append(dc, ", \"references\":[\"%p\"", (void *)ref);
|
|
|
|
else
|
|
|
|
dump_append(dc, ", \"%p\"", (void *)ref);
|
|
|
|
|
|
|
|
dc->cur_obj_references++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dump_object(VALUE obj, struct dump_config *dc)
|
|
|
|
{
|
|
|
|
size_t memsize;
|
|
|
|
struct allocation_info *ainfo;
|
|
|
|
rb_io_t *fptr;
|
2013-12-09 21:26:09 -05:00
|
|
|
ID flags[RB_OBJ_GC_FLAGS_MAX];
|
|
|
|
size_t n, i;
|
2013-11-08 12:06:55 -05:00
|
|
|
|
|
|
|
dc->cur_obj = obj;
|
|
|
|
dc->cur_obj_references = 0;
|
|
|
|
dc->cur_obj_klass = BUILTIN_TYPE(obj) == T_NODE ? 0 : RBASIC_CLASS(obj);
|
|
|
|
|
|
|
|
if (dc->cur_obj == dc->string)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dump_append(dc, "{\"address\":\"%p\", \"type\":\"%s\"", (void *)obj, obj_type(obj));
|
|
|
|
|
|
|
|
if (dc->cur_obj_klass)
|
|
|
|
dump_append(dc, ", \"class\":\"%p\"", (void *)dc->cur_obj_klass);
|
|
|
|
if (rb_obj_frozen_p(obj))
|
|
|
|
dump_append(dc, ", \"frozen\":true");
|
|
|
|
|
|
|
|
switch (BUILTIN_TYPE(obj)) {
|
2013-11-09 01:16:42 -05:00
|
|
|
case T_NODE:
|
|
|
|
dump_append(dc, ", \"node_type\":\"%s\"", ruby_node_name(nd_type(obj)));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_STRING:
|
|
|
|
if (STR_EMBED_P(obj))
|
|
|
|
dump_append(dc, ", \"embedded\":true");
|
|
|
|
if (is_broken_string(obj))
|
|
|
|
dump_append(dc, ", \"broken\":true");
|
2013-12-09 21:26:09 -05:00
|
|
|
if (FL_TEST(obj, RSTRING_FSTR))
|
|
|
|
dump_append(dc, ", \"fstring\":true");
|
2013-11-09 01:16:42 -05:00
|
|
|
if (STR_SHARED_P(obj))
|
|
|
|
dump_append(dc, ", \"shared\":true");
|
|
|
|
else {
|
|
|
|
dump_append(dc, ", \"bytesize\":%ld", RSTRING_LEN(obj));
|
2014-02-05 00:41:04 -05:00
|
|
|
if (!STR_EMBED_P(obj) && !STR_SHARED_P(obj) && (long)rb_str_capacity(obj) != RSTRING_LEN(obj))
|
2013-11-09 01:16:42 -05:00
|
|
|
dump_append(dc, ", \"capacity\":%ld", rb_str_capacity(obj));
|
|
|
|
|
|
|
|
if (is_ascii_string(obj)) {
|
|
|
|
dump_append(dc, ", \"value\":");
|
|
|
|
dump_append_string_value(dc, obj);
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
2013-11-09 01:16:42 -05:00
|
|
|
}
|
2013-11-08 12:06:55 -05:00
|
|
|
|
2013-11-09 01:16:42 -05:00
|
|
|
if (!ENCODING_IS_ASCII8BIT(obj))
|
|
|
|
dump_append(dc, ", \"encoding\":\"%s\"", rb_enc_name(rb_enc_from_index(ENCODING_GET(obj))));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_HASH:
|
|
|
|
dump_append(dc, ", \"size\":%ld", RHASH_SIZE(obj));
|
|
|
|
if (FL_TEST(obj, HASH_PROC_DEFAULT))
|
|
|
|
dump_append(dc, ", \"default\":\"%p\"", (void *)RHASH_IFNONE(obj));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_ARRAY:
|
|
|
|
dump_append(dc, ", \"length\":%ld", RARRAY_LEN(obj));
|
|
|
|
if (RARRAY_LEN(obj) > 0 && FL_TEST(obj, ELTS_SHARED))
|
|
|
|
dump_append(dc, ", \"shared\":true");
|
|
|
|
if (RARRAY_LEN(obj) > 0 && FL_TEST(obj, RARRAY_EMBED_FLAG))
|
|
|
|
dump_append(dc, ", \"embedded\":true");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_CLASS:
|
|
|
|
case T_MODULE:
|
|
|
|
if (dc->cur_obj_klass)
|
|
|
|
dump_append(dc, ", \"name\":\"%s\"", rb_class2name(obj));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_DATA:
|
|
|
|
if (RTYPEDDATA_P(obj))
|
|
|
|
dump_append(dc, ", \"struct\":\"%s\"", RTYPEDDATA_TYPE(obj)->wrap_struct_name);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
|
|
|
dump_append(dc, ", \"value\":\"%g\"", RFLOAT_VALUE(obj));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_OBJECT:
|
|
|
|
dump_append(dc, ", \"ivars\":%ld", ROBJECT_NUMIV(obj));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FILE:
|
|
|
|
fptr = RFILE(obj)->fptr;
|
2014-03-10 17:55:51 -04:00
|
|
|
if (fptr)
|
|
|
|
dump_append(dc, ", \"fd\":%d", fptr->fd);
|
2013-11-09 01:16:42 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_ZOMBIE:
|
|
|
|
dump_append(dc, "}\n");
|
|
|
|
return;
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_objspace_reachable_objects_from(obj, reachable_object_i, dc);
|
|
|
|
if (dc->cur_obj_references > 0)
|
|
|
|
dump_append(dc, "]");
|
|
|
|
|
|
|
|
if ((ainfo = objspace_lookup_allocation_info(obj))) {
|
|
|
|
dump_append(dc, ", \"file\":\"%s\", \"line\":%lu", ainfo->path, ainfo->line);
|
2014-08-02 21:56:16 -04:00
|
|
|
if (RTEST(ainfo->mid)) {
|
|
|
|
VALUE m = rb_sym2str(ainfo->mid);
|
|
|
|
dump_append(dc, ", \"method\":\"%s\"", RSTRING_PTR(m));
|
|
|
|
}
|
2013-11-23 19:22:19 -05:00
|
|
|
dump_append(dc, ", \"generation\":%"PRIuSIZE, ainfo->generation);
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((memsize = rb_obj_memsize_of(obj)) > 0)
|
2013-11-23 19:22:19 -05:00
|
|
|
dump_append(dc, ", \"memsize\":%"PRIuSIZE, memsize);
|
2013-11-08 12:06:55 -05:00
|
|
|
|
2013-12-09 21:26:09 -05:00
|
|
|
if ((n = rb_obj_gc_flags(obj, flags, sizeof(flags))) > 0) {
|
|
|
|
dump_append(dc, ", \"flags\":{");
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
dump_append(dc, "\"%s\":true", rb_id2name(flags[i]));
|
|
|
|
if (i != n-1) dump_append(dc, ", ");
|
|
|
|
}
|
|
|
|
dump_append(dc, "}");
|
|
|
|
}
|
|
|
|
|
2013-11-08 12:06:55 -05:00
|
|
|
dump_append(dc, "}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
heap_i(void *vstart, void *vend, size_t stride, void *data)
|
|
|
|
{
|
|
|
|
VALUE v = (VALUE)vstart;
|
|
|
|
for (; v != (VALUE)vend; v += stride) {
|
|
|
|
if (RBASIC(v)->flags)
|
|
|
|
dump_object(v, data);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
root_obj_i(const char *category, VALUE obj, void *data)
|
|
|
|
{
|
|
|
|
struct dump_config *dc = (struct dump_config *)data;
|
|
|
|
|
|
|
|
if (dc->root_category != NULL && category != dc->root_category)
|
|
|
|
dump_append(dc, "]}\n");
|
|
|
|
if (dc->root_category == NULL || category != dc->root_category)
|
|
|
|
dump_append(dc, "{\"type\":\"ROOT\", \"root\":\"%s\", \"references\":[\"%p\"", category, (void *)obj);
|
|
|
|
else
|
|
|
|
dump_append(dc, ", \"%p\"", (void *)obj);
|
|
|
|
|
|
|
|
dc->root_category = category;
|
|
|
|
dc->roots++;
|
|
|
|
}
|
|
|
|
|
2013-11-09 01:16:53 -05:00
|
|
|
static VALUE
|
2013-11-14 11:06:09 -05:00
|
|
|
dump_output(struct dump_config *dc, VALUE opts, VALUE output, const char *filename)
|
2013-11-09 01:16:53 -05:00
|
|
|
{
|
2013-11-14 11:06:09 -05:00
|
|
|
VALUE tmp;
|
|
|
|
|
2013-11-09 01:16:53 -05:00
|
|
|
if (RTEST(opts))
|
|
|
|
output = rb_hash_aref(opts, sym_output);
|
|
|
|
|
|
|
|
if (output == sym_stdout) {
|
|
|
|
dc->stream = stdout;
|
|
|
|
dc->string = Qnil;
|
|
|
|
}
|
|
|
|
else if (output == sym_file) {
|
2013-11-14 11:06:09 -05:00
|
|
|
rb_io_t *fptr;
|
|
|
|
rb_require("tempfile");
|
|
|
|
tmp = rb_assoc_new(rb_str_new_cstr(filename), rb_str_new_cstr(".json"));
|
|
|
|
tmp = rb_funcallv(rb_path2class("Tempfile"), rb_intern("create"), 1, &tmp);
|
|
|
|
io:
|
|
|
|
dc->string = rb_io_get_write_io(tmp);
|
|
|
|
rb_io_flush(dc->string);
|
|
|
|
GetOpenFile(dc->string, fptr);
|
|
|
|
dc->stream = rb_io_stdio_file(fptr);
|
2013-11-09 01:16:53 -05:00
|
|
|
}
|
|
|
|
else if (output == sym_string) {
|
|
|
|
dc->string = rb_str_new_cstr("");
|
|
|
|
}
|
2013-11-14 11:06:09 -05:00
|
|
|
else if (!NIL_P(tmp = rb_io_check_io(output))) {
|
|
|
|
output = sym_file;
|
|
|
|
goto io;
|
|
|
|
}
|
2013-11-09 01:16:53 -05:00
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "wrong output option: %"PRIsVALUE, output);
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dump_result(struct dump_config *dc, VALUE output)
|
|
|
|
{
|
|
|
|
if (output == sym_string) {
|
|
|
|
return dc->string;
|
|
|
|
}
|
|
|
|
else if (output == sym_file) {
|
2013-11-14 11:06:09 -05:00
|
|
|
rb_io_flush(dc->string);
|
2013-11-09 01:16:53 -05:00
|
|
|
return dc->string;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-08 12:06:55 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ObjectSpace.dump(obj[, output: :string]) # => "{ ... }"
|
2013-11-25 16:46:00 -05:00
|
|
|
* ObjectSpace.dump(obj, output: :file) # => #<File:/tmp/rubyobj20131125-88733-1xkfmpv.json>
|
|
|
|
* ObjectSpace.dump(obj, output: :stdout) # => nil
|
2013-11-08 12:06:55 -05:00
|
|
|
*
|
|
|
|
* Dump the contents of a ruby object as JSON.
|
|
|
|
*
|
|
|
|
* This method is only expected to work with C Ruby.
|
|
|
|
* This is an experimental method and is subject to change.
|
|
|
|
* In particular, the function signature and output format are
|
|
|
|
* not guaranteed to be compatible in future versions of ruby.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
objspace_dump(int argc, VALUE *argv, VALUE os)
|
|
|
|
{
|
2013-11-14 11:06:09 -05:00
|
|
|
static const char filename[] = "rubyobj";
|
2013-11-08 12:06:55 -05:00
|
|
|
VALUE obj = Qnil, opts = Qnil, output;
|
|
|
|
struct dump_config dc = {0,};
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "1:", &obj, &opts);
|
|
|
|
|
2013-11-09 01:16:53 -05:00
|
|
|
output = dump_output(&dc, opts, sym_string, filename);
|
2013-11-08 12:06:55 -05:00
|
|
|
|
|
|
|
dump_object(obj, &dc);
|
|
|
|
|
2013-11-09 01:16:53 -05:00
|
|
|
return dump_result(&dc, output);
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-11-25 16:46:00 -05:00
|
|
|
* ObjectSpace.dump_all([output: :file]) # => #<File:/tmp/rubyheap20131125-88469-laoj3v.json>
|
2013-11-08 12:06:55 -05:00
|
|
|
* ObjectSpace.dump_all(output: :stdout) # => nil
|
|
|
|
* ObjectSpace.dump_all(output: :string) # => "{...}\n{...}\n..."
|
2013-11-26 00:27:22 -05:00
|
|
|
* ObjectSpace.dump_all(output:
|
|
|
|
* File.open('heap.json','w')) # => #<File:heap.json>
|
2013-11-08 12:06:55 -05:00
|
|
|
*
|
|
|
|
* Dump the contents of the ruby heap as JSON.
|
|
|
|
*
|
|
|
|
* This method is only expected to work with C Ruby.
|
|
|
|
* This is an experimental method and is subject to change.
|
|
|
|
* In particular, the function signature and output format are
|
|
|
|
* not guaranteed to be compatible in future versions of ruby.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
objspace_dump_all(int argc, VALUE *argv, VALUE os)
|
|
|
|
{
|
2013-11-14 11:06:09 -05:00
|
|
|
static const char filename[] = "rubyheap";
|
2013-11-08 12:06:55 -05:00
|
|
|
VALUE opts = Qnil, output;
|
|
|
|
struct dump_config dc = {0,};
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "0:", &opts);
|
|
|
|
|
2013-11-09 01:16:53 -05:00
|
|
|
output = dump_output(&dc, opts, sym_file, filename);
|
2013-11-08 12:06:55 -05:00
|
|
|
|
|
|
|
/* dump roots */
|
|
|
|
rb_objspace_reachable_objects_from_root(root_obj_i, &dc);
|
|
|
|
if (dc.roots) dump_append(&dc, "]}\n");
|
|
|
|
|
|
|
|
/* dump all objects */
|
|
|
|
rb_objspace_each_objects(heap_i, &dc);
|
|
|
|
|
2013-11-09 01:16:53 -05:00
|
|
|
return dump_result(&dc, output);
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_objspace_dump(VALUE rb_mObjSpace)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
rb_mObjSpace = rb_define_module("ObjectSpace"); /* let rdoc know */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rb_define_module_function(rb_mObjSpace, "dump", objspace_dump, -1);
|
|
|
|
rb_define_module_function(rb_mObjSpace, "dump_all", objspace_dump_all, -1);
|
|
|
|
|
|
|
|
sym_output = ID2SYM(rb_intern("output"));
|
|
|
|
sym_stdout = ID2SYM(rb_intern("stdout"));
|
|
|
|
sym_string = ID2SYM(rb_intern("string"));
|
|
|
|
sym_file = ID2SYM(rb_intern("file"));
|
2013-12-09 21:26:09 -05:00
|
|
|
|
|
|
|
/* force create static IDs */
|
|
|
|
rb_obj_gc_flags(rb_mObjSpace, 0, 0);
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|