Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to dump T_NONE objects and add page numbers #33

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 37 additions & 2 deletions ext/objspace/objspace_dump.c
Expand Up @@ -21,6 +21,7 @@
#include "objspace.h"

static VALUE sym_output, sym_stdout, sym_string, sym_file;
static VALUE sym_include_pages, sym_include_none;

struct dump_config {
VALUE type;
Expand All @@ -31,6 +32,9 @@ struct dump_config {
VALUE cur_obj;
VALUE cur_obj_klass;
size_t cur_obj_references;
int include_pages;
int include_none;
int pages_seen;
};

PRINTF_ARGS(static void dump_append(struct dump_config *, const char *, ...), 2, 3);
Expand Down Expand Up @@ -190,6 +194,18 @@ dump_append_string_content(struct dump_config *dc, VALUE obj)
}
}

static void
dump_empty(VALUE obj, struct dump_config *dc)
{
dump_append(dc, "{\"address\":\"%p\", ", (void *)obj);
dump_append(dc, "\"type\":\"NONE\"");

if (dc->include_pages)
dump_append(dc, ", \"page_number\":%d", dc->pages_seen);
dump_append(dc, "}\n");
return;
}

static void
dump_object(VALUE obj, struct dump_config *dc)
{
Expand All @@ -215,6 +231,8 @@ dump_object(VALUE obj, struct dump_config *dc)

if (dc->cur_obj_klass)
dump_append(dc, ", \"class\":\"%p\"", (void *)dc->cur_obj_klass);
if (dc->include_pages)
dump_append(dc, ", \"page_number\":%d", dc->pages_seen);
if (rb_obj_frozen_p(obj))
dump_append(dc, ", \"frozen\":true");

Expand Down Expand Up @@ -318,11 +336,15 @@ dump_object(VALUE obj, struct dump_config *dc)
static int
heap_i(void *vstart, void *vend, size_t stride, void *data)
{
struct dump_config *dc = (struct dump_config *)data;
VALUE v = (VALUE)vstart;
for (; v != (VALUE)vend; v += stride) {
if (RBASIC(v)->flags)
dump_object(v, data);
dump_object(v, dc);
else if (dc->include_none && T_NONE == BUILTIN_TYPE(v))
dump_empty(v, dc);
}
dc->pages_seen++;
return 0;
}

Expand All @@ -347,9 +369,20 @@ dump_output(struct dump_config *dc, VALUE opts, VALUE output, const char *filena
{
VALUE tmp;

if (RTEST(opts))
dc->pages_seen = 0;
dc->include_pages = 0;
dc->include_none = 0;

if (RTEST(opts)) {
output = rb_hash_aref(opts, sym_output);

if (Qtrue == rb_hash_lookup2(opts, sym_include_pages, Qfalse))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are reverse comparisons standard on MRI? I don't recall seeing many of these around.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think I was just thinking in reverse. I'll switch it. ;)

dc->include_pages = 1;

if (Qtrue == rb_hash_lookup2(opts, sym_include_none, Qfalse))
dc->include_none = 1;
}

if (output == sym_stdout) {
dc->stream = stdout;
dc->string = Qnil;
Expand Down Expand Up @@ -474,6 +507,8 @@ Init_objspace_dump(VALUE rb_mObjSpace)
sym_stdout = ID2SYM(rb_intern("stdout"));
sym_string = ID2SYM(rb_intern("string"));
sym_file = ID2SYM(rb_intern("file"));
sym_include_pages = ID2SYM(rb_intern("include_pages"));
sym_include_none = ID2SYM(rb_intern("include_none"));

/* force create static IDs */
rb_obj_gc_flags(rb_mObjSpace, 0, 0);
Expand Down