From 57b3db3dd516e2d17c1a13a9b22673dbf66804ea Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Mon, 6 Jul 2020 23:29:08 +0200 Subject: [PATCH] Replace label by the required file path --- ext/stackprof/stackprof.c | 18 +++++++++++++++++- test/test_stackprof.rb | 12 ++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/ext/stackprof/stackprof.c b/ext/stackprof/stackprof.c index a65531b9..825e8fc5 100644 --- a/ext/stackprof/stackprof.c +++ b/ext/stackprof/stackprof.c @@ -224,6 +224,20 @@ frame_lines_i(st_data_t key, st_data_t val, st_data_t arg) return ST_CONTINUE; } +static VALUE +coerce_frame_name(VALUE name, VALUE line) +{ + char *start_pointer = strstr(RSTRING_PTR(name), "\0"); + if (start_pointer) { + VALUE new_name = rb_str_new(RSTRING_PTR(name), start_pointer - RSTRING_PTR(name)); + rb_str_cat_cstr(new_name, ""); + return new_name; + } + return name; +} + static int frame_i(st_data_t key, st_data_t val, st_data_t arg) { @@ -242,13 +256,15 @@ frame_i(st_data_t key, st_data_t val, st_data_t arg) line = INT2FIX(0); } else { name = rb_profile_frame_full_label(frame); - file = rb_profile_frame_absolute_path(frame); if (NIL_P(file)) file = rb_profile_frame_path(frame); line = rb_profile_frame_first_lineno(frame); } + + name = coerce_frame_name(name, file); + rb_hash_aset(details, sym_name, name); rb_hash_aset(details, sym_file, file); if (line != INT2FIX(0)) { diff --git a/test/test_stackprof.rb b/test/test_stackprof.rb index c30e2afd..e643fe49 100644 --- a/test/test_stackprof.rb +++ b/test/test_stackprof.rb @@ -194,6 +194,18 @@ def test_gc assert_operator profile[:missed_samples], :<=, 25 end + def test_top_required + tmpfile = Tempfile.new(%w(stackprof-script .rb)) + tmpfile.write("10.times { sleep 0.1 }\n") + tmpfile.flush + path = File.realpath(tmpfile.path) + ret = StackProf.run(interval: 10) do + require path + end + frame_names = ret[:frames].values.select { |f| f[:file] == path }.map { |f| f[:name] } + assert_equal ["block in ", ""], frame_names + end + def test_out tmpfile = Tempfile.new('stackprof-out') ret = StackProf.run(mode: :custom, out: tmpfile) do