Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion ext/stackprof/stackprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -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), "<top (required)>\0");
Copy link
Owner

Choose a reason for hiding this comment

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

Wouldn't some sort of strncmp be better here? Does the string match exactly, or is it sometimes contained within?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It either match exactly or is trailing:

  • <top (required)>
  • block in <top (required)>

if (start_pointer) {
VALUE new_name = rb_str_new(RSTRING_PTR(name), start_pointer - RSTRING_PTR(name));
rb_str_cat_cstr(new_name, "<require:");
rb_str_cat_cstr(new_name, RSTRING_PTR(line));
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)
{
Expand All @@ -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)) {
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 <require:#{path}>", "<require:#{path}>"], frame_names
end

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