Skip to content

Commit 66362c6

Browse files
committed
Replace <top> label by the required file path
1 parent ae980e1 commit 66362c6

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

ext/stackprof/stackprof.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,20 @@ frame_lines_i(st_data_t key, st_data_t val, st_data_t arg)
224224
return ST_CONTINUE;
225225
}
226226

227+
static VALUE
228+
coerce_frame_name(VALUE name, VALUE line)
229+
{
230+
char *start_pointer = strstr(RSTRING_PTR(name), "<top (required)>\0");
231+
if (start_pointer) {
232+
VALUE new_name = rb_str_new(RSTRING_PTR(name), start_pointer - RSTRING_PTR(name));
233+
rb_str_cat_cstr(new_name, "<require:");
234+
rb_str_cat_cstr(new_name, RSTRING_PTR(line));
235+
rb_str_cat_cstr(new_name, ">");
236+
return new_name;
237+
}
238+
return name;
239+
}
240+
227241
static int
228242
frame_i(st_data_t key, st_data_t val, st_data_t arg)
229243
{
@@ -242,13 +256,15 @@ frame_i(st_data_t key, st_data_t val, st_data_t arg)
242256
line = INT2FIX(0);
243257
} else {
244258
name = rb_profile_frame_full_label(frame);
245-
246259
file = rb_profile_frame_absolute_path(frame);
247260
if (NIL_P(file))
248261
file = rb_profile_frame_path(frame);
249262
line = rb_profile_frame_first_lineno(frame);
250263
}
251264

265+
266+
name = coerce_frame_name(name, file);
267+
252268
rb_hash_aset(details, sym_name, name);
253269
rb_hash_aset(details, sym_file, file);
254270
if (line != INT2FIX(0)) {

test/test_stackprof.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,18 @@ def test_gc
194194
assert_operator profile[:missed_samples], :<=, 25
195195
end
196196

197+
def test_top_required
198+
tmpfile = Tempfile.new(%w(stackprof-script .rb))
199+
tmpfile.write("10.times { sleep 0.1 }\n")
200+
tmpfile.flush
201+
path = File.realpath(tmpfile.path)
202+
ret = StackProf.run(interval: 10) do
203+
require path
204+
end
205+
frame_names = ret[:frames].values.select { |f| f[:file] == path }.map { |f| f[:name] }
206+
assert_equal ["block in #{path}", path], frame_names
207+
end
208+
197209
def test_out
198210
tmpfile = Tempfile.new('stackprof-out')
199211
ret = StackProf.run(mode: :custom, out: tmpfile) do

0 commit comments

Comments
 (0)