Skip to content

Commit

Permalink
add rb_print_memory_objects() debug function
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent Sansonetti committed May 20, 2011
1 parent cbb0951 commit d5a774d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
7 changes: 7 additions & 0 deletions HACKING.rdoc
Expand Up @@ -166,3 +166,10 @@ The following environment variables might help you debug easy bugs.

* To symbolize a Ruby address (named as ??) in the backtrace:
(gdb) p (void)rb_symbolicate(<address>)

* To print the addresses of all memory objects of a given minimum size (useful
when debugging memory leaks):
(gdb) p (void)rb_print_memory_objects(<size>)

* To print the GC references of a given memory object:
(gdb) info gc-references <address>
34 changes: 33 additions & 1 deletion gc.c
Expand Up @@ -561,7 +561,7 @@ rb_objc_yield_classes(VALUE of)

static void
rb_objc_recorder(task_t task, void *context, unsigned type_mask,
vm_range_t *ranges, unsigned range_count)
vm_range_t *ranges, unsigned range_count)
{
struct rb_objc_recorder_context *ctx;
vm_range_t *r, *end;
Expand Down Expand Up @@ -1057,3 +1057,35 @@ rb_gc_register_address(VALUE *slot)
{
rb_global_variable(slot);
}

static void
print_memory_object(task_t task, void *context, unsigned type_mask,
vm_range_t *ranges, unsigned range_count)
{
const size_t min_size = *(size_t *)context;
for (vm_range_t *r = ranges, *end = ranges + range_count; r < end; r++) {
const size_t size = auto_zone_size(__auto_zone, (void *)r->address);
if (size >= min_size) {
printf("address %p size %ld layout type ",
(void *)r->address, size);
switch (auto_zone_get_layout_type(__auto_zone,
(void *)r->address)) {
case AUTO_OBJECT:
printf("object (class %s)\n",
class_getName(object_getClass((void *)r->address)));
break;
default:
printf("memory\n");
break;
}
}
}
}

void
rb_print_memory_objects(size_t min_size)
{
(((malloc_zone_t *)__auto_zone)->introspect->enumerator)(mach_task_self(),
(void *)&min_size, MALLOC_PTR_IN_USE_RANGE_TYPE,
(vm_address_t)__auto_zone, NULL, print_memory_object);
}

0 comments on commit d5a774d

Please sign in to comment.