Skip to content

Commit

Permalink
Replace <top> label by the required file path
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Jul 7, 2020
1 parent ae980e1 commit 89a5f8f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
16 changes: 15 additions & 1 deletion ext/stackprof/stackprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ 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), "<top (required)>\0");
if (start_pointer) {
VALUE new_name = rb_str_new(RSTRING_PTR(name), start_pointer - RSTRING_PTR(name));
rb_str_cat_cstr(new_name, RSTRING_PTR(line));
return new_name;
}
return name;
}

static int
frame_i(st_data_t key, st_data_t val, st_data_t arg)
{
Expand All @@ -242,13 +254,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)) {
Expand Down
12 changes: 12 additions & 0 deletions test/test_stackprof.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 #{path}", path], frame_names
end

def test_out
tmpfile = Tempfile.new('stackprof-out')
ret = StackProf.run(mode: :custom, out: tmpfile) do
Expand Down

0 comments on commit 89a5f8f

Please sign in to comment.