diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-10-09 03:21:57 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-10-09 03:21:57 +0000 |
commit | 97fffcadcb45c26c988d893c992072163d9f03b3 (patch) | |
tree | 2913a6c395408ff994caec282a83f39f85d597e6 | |
parent | e20ac9442dbb615530775551619687518c37459b (diff) |
ext/objspace/objspace_dump.c: print addresses consistently
The format addresses are printed in are different if you use
`ObjectSpace.dump_all(output: :stdout)` vs.
`ObjectSpace.dump_all(output: :string)` (or `ObjectSpace.dump`) due to
differences in the underlying `vfprintf` implementation.
Use `"%#"PRIxVALUE` to format `VALUE`.
Co-authored-by: Ashe Connor <[email protected]>
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64974 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ext/objspace/objspace_dump.c | 14 | ||||
-rw-r--r-- | test/objspace/test_objspace.rb | 56 |
2 files changed, 63 insertions, 7 deletions
diff --git a/ext/objspace/objspace_dump.c b/ext/objspace/objspace_dump.c index b249e3f5fb..602cbadef5 100644 --- a/ext/objspace/objspace_dump.c +++ b/ext/objspace/objspace_dump.c @@ -172,9 +172,9 @@ reachable_object_i(VALUE ref, void *data) return; if (dc->cur_obj_references == 0) - dump_append(dc, ", \"references\":[\"%p\"", (void *)ref); + dump_append(dc, ", \"references\":[\"%#"PRIxVALUE"\"", ref); else - dump_append(dc, ", \"%p\"", (void *)ref); + dump_append(dc, ", \"%#"PRIxVALUE"\"", ref); dc->cur_obj_references++; } @@ -235,10 +235,10 @@ dump_object(VALUE obj, struct dump_config *dc) if (dc->cur_obj == dc->string) return; - dump_append(dc, "{\"address\":\"%p\", \"type\":\"%s\"", (void *)obj, obj_type(obj)); + dump_append(dc, "{\"address\":\"%#"PRIxVALUE"\", \"type\":\"%s\"", obj, obj_type(obj)); if (dc->cur_obj_klass) - dump_append(dc, ", \"class\":\"%p\"", (void *)dc->cur_obj_klass); + dump_append(dc, ", \"class\":\"%#"PRIxVALUE"\"", dc->cur_obj_klass); if (rb_obj_frozen_p(obj)) dump_append(dc, ", \"frozen\":true"); @@ -274,7 +274,7 @@ dump_object(VALUE obj, struct dump_config *dc) case T_HASH: dump_append(dc, ", \"size\":%"PRIuSIZE, (size_t)RHASH_SIZE(obj)); if (FL_TEST(obj, HASH_PROC_DEFAULT)) - dump_append(dc, ", \"default\":\"%p\"", (void *)RHASH_IFNONE(obj)); + dump_append(dc, ", \"default\":\"%#"PRIxVALUE"\"", RHASH_IFNONE(obj)); break; case T_ARRAY: @@ -363,9 +363,9 @@ root_obj_i(const char *category, VALUE obj, void *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); + dump_append(dc, "{\"type\":\"ROOT\", \"root\":\"%s\", \"references\":[\"%#"PRIxVALUE"\"", category, obj); else - dump_append(dc, ", \"%p\"", (void *)obj); + dump_append(dc, ", \"%#"PRIxVALUE"\"", obj); dc->root_category = category; dc->roots++; diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb index 0e1ad327b9..947170203a 100644 --- a/test/objspace/test_objspace.rb +++ b/test/objspace/test_objspace.rb @@ -318,6 +318,62 @@ class TestObjSpace < Test::Unit::TestCase end end + def test_dump_addresses_match_dump_all_addresses + assert_in_out_err(%w[-robjspace], "#{<<-"begin;"}\n#{<<-'end;'}") do |output, error| + begin; + def dump_my_heap_please + obj = Object.new + puts ObjectSpace.dump(obj) + ObjectSpace.dump_all(output: $stdout) + end + + dump_my_heap_please + end; + needle = JSON.parse(output.first) + addr = needle['address'] + found = output.drop(1).find { |l| JSON.parse(l)['address'] == addr } + assert found, "object #{addr} should be findable in full heap dump" + end + end + + def test_dump_class_addresses_match_dump_all_addresses + assert_in_out_err(%w[-robjspace], "#{<<-"begin;"}\n#{<<-'end;'}") do |output, error| + begin; + def dump_my_heap_please + obj = Object.new + puts ObjectSpace.dump(obj) + ObjectSpace.dump_all(output: $stdout) + end + + dump_my_heap_please + end; + needle = JSON.parse(output.first) + addr = needle['class'] + found = output.drop(1).find { |l| JSON.parse(l)['address'] == addr } + assert found, "object #{addr} should be findable in full heap dump" + end + end + + def test_dump_reference_addresses_match_dump_all_addresses + assert_in_out_err(%w[-robjspace], "#{<<-"begin;"}\n#{<<-'end;'}") do |output, error| + begin; + def dump_my_heap_please + obj = Object.new + obj2 = Object.new + obj2.instance_variable_set(:@ref, obj) + puts ObjectSpace.dump(obj) + ObjectSpace.dump_all(output: $stdout) + end + + dump_my_heap_please + end; + needle = JSON.parse(output.first) + addr = needle['address'] + found = output.drop(1).find { |l| (JSON.parse(l)['references'] || []).include? addr } + assert found, "object #{addr} should be findable in full heap dump" + end + end + def test_dump_all entry = /"bytesize":11, "value":"TEST STRING", "encoding":"UTF-8", "file":"-", "line":4, "method":"dump_my_heap_please", "generation":/ |