Skip to content

Commit d79128f

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

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
@@ -77,6 +77,7 @@ static VALUE sym_object, sym_wall, sym_cpu, sym_custom, sym_name, sym_file, sym_
7777
static VALUE sym_samples, sym_total_samples, sym_missed_samples, sym_edges, sym_lines;
7878
static VALUE sym_version, sym_mode, sym_interval, sym_raw, sym_metadata, sym_frames, sym_ignore_gc, sym_out;
7979
static VALUE sym_aggregate, sym_raw_timestamp_deltas, sym_state, sym_marking, sym_sweeping;
80+
static VALUE str_top_label;
8081
static VALUE sym_gc_samples, objtracer;
8182
static VALUE gc_hook;
8283
static VALUE rb_mStackProf;
@@ -224,6 +225,17 @@ frame_lines_i(st_data_t key, st_data_t val, st_data_t arg)
224225
return ST_CONTINUE;
225226
}
226227

228+
static VALUE
229+
coerce_frame_name(VALUE name, VALUE line) {
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, RSTRING_PTR(line));
234+
return new_name;
235+
}
236+
return name;
237+
}
238+
227239
static int
228240
frame_i(st_data_t key, st_data_t val, st_data_t arg)
229241
{
@@ -242,13 +254,15 @@ frame_i(st_data_t key, st_data_t val, st_data_t arg)
242254
line = INT2FIX(0);
243255
} else {
244256
name = rb_profile_frame_full_label(frame);
245-
246257
file = rb_profile_frame_absolute_path(frame);
247258
if (NIL_P(file))
248259
file = rb_profile_frame_path(frame);
249260
line = rb_profile_frame_first_lineno(frame);
250261
}
251262

263+
264+
name = coerce_frame_name(name, file);
265+
252266
rb_hash_aset(details, sym_name, name);
253267
rb_hash_aset(details, sym_file, file);
254268
if (line != INT2FIX(0)) {
@@ -741,6 +755,8 @@ Init_stackprof(void)
741755
S(sweeping);
742756
#undef S
743757

758+
str_top_label = rb_str_new_cstr("<top (required)>");
759+
744760
/* Need to run this to warm the symbol table before we call this during GC */
745761
rb_gc_latest_gc_info(sym_state);
746762

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)