Skip to content

Commit 1fd8df2

Browse files
mhiramathitachirostedt
authored andcommitted
tracing/kprobes: Fix kprobe-tracer to support stack trace
Fix to support kernel stack trace correctly on kprobe-tracer. Since the execution path of kprobe-based dynamic events is different from other tracepoint-based events, normal ftrace_trace_stack() doesn't work correctly. To fix that, this introduces ftrace_trace_stack_regs() which traces stack via pt_regs instead of current stack register. e.g. # echo p schedule+4 > /sys/kernel/debug/tracing/kprobe_events # echo 1 > /sys/kernel/debug/tracing/options/stacktrace # echo 1 > /sys/kernel/debug/tracing/events/kprobes/enable # head -n 20 /sys/kernel/debug/tracing/trace bash-2968 [000] 10297.050245: p_schedule_4: (schedule+0x4/0x4ca) bash-2968 [000] 10297.050247: <stack trace> => schedule_timeout => n_tty_read => tty_read => vfs_read => sys_read => system_call_fastpath kworker/0:1-2940 [000] 10297.050265: p_schedule_4: (schedule+0x4/0x4ca) kworker/0:1-2940 [000] 10297.050266: <stack trace> => worker_thread => kthread => kernel_thread_helper sshd-1132 [000] 10297.050365: p_schedule_4: (schedule+0x4/0x4ca) sshd-1132 [000] 10297.050365: <stack trace> => sysret_careful Note: Even with this fix, the first entry will be skipped if the probe is put on the function entry area before the frame pointer is set up (usually, that is 4 bytes (push %bp; mov %sp %bp) on x86), because stack unwinder depends on the frame pointer. Signed-off-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: yrl.pp-manager.tt@hitachi.com Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Namhyung Kim <namhyung@gmail.com> Link: http://lkml.kernel.org/r/20110608070934.17777.17116.stgit@fedora15 Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent c624d33 commit 1fd8df2

4 files changed

Lines changed: 46 additions & 7 deletions

File tree

‎include/linux/ftrace_event.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
129129
void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
130130
struct ring_buffer_event *event,
131131
unsigned long flags, int pc);
132+
void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer *buffer,
133+
struct ring_buffer_event *event,
134+
unsigned long flags, int pc,
135+
struct pt_regs *regs);
132136
void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
133137
struct ring_buffer_event *event);
134138

‎kernel/trace/trace.c‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,18 @@ void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
11931193
}
11941194
EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
11951195

1196+
void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1197+
struct ring_buffer_event *event,
1198+
unsigned long flags, int pc,
1199+
struct pt_regs *regs)
1200+
{
1201+
ring_buffer_unlock_commit(buffer, event);
1202+
1203+
ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1204+
ftrace_trace_userstack(buffer, flags, pc);
1205+
}
1206+
EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit_regs);
1207+
11961208
void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
11971209
struct ring_buffer_event *event)
11981210
{
@@ -1238,7 +1250,7 @@ ftrace(struct trace_array *tr, struct trace_array_cpu *data,
12381250
#ifdef CONFIG_STACKTRACE
12391251
static void __ftrace_trace_stack(struct ring_buffer *buffer,
12401252
unsigned long flags,
1241-
int skip, int pc)
1253+
int skip, int pc, struct pt_regs *regs)
12421254
{
12431255
struct ftrace_event_call *call = &event_kernel_stack;
12441256
struct ring_buffer_event *event;
@@ -1257,24 +1269,36 @@ static void __ftrace_trace_stack(struct ring_buffer *buffer,
12571269
trace.skip = skip;
12581270
trace.entries = entry->caller;
12591271

1260-
save_stack_trace(&trace);
1272+
if (regs)
1273+
save_stack_trace_regs(regs, &trace);
1274+
else
1275+
save_stack_trace(&trace);
12611276
if (!filter_check_discard(call, entry, buffer, event))
12621277
ring_buffer_unlock_commit(buffer, event);
12631278
}
12641279

1280+
void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1281+
int skip, int pc, struct pt_regs *regs)
1282+
{
1283+
if (!(trace_flags & TRACE_ITER_STACKTRACE))
1284+
return;
1285+
1286+
__ftrace_trace_stack(buffer, flags, skip, pc, regs);
1287+
}
1288+
12651289
void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
12661290
int skip, int pc)
12671291
{
12681292
if (!(trace_flags & TRACE_ITER_STACKTRACE))
12691293
return;
12701294

1271-
__ftrace_trace_stack(buffer, flags, skip, pc);
1295+
__ftrace_trace_stack(buffer, flags, skip, pc, NULL);
12721296
}
12731297

12741298
void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
12751299
int pc)
12761300
{
1277-
__ftrace_trace_stack(tr->buffer, flags, skip, pc);
1301+
__ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
12781302
}
12791303

12801304
/**
@@ -1290,7 +1314,7 @@ void trace_dump_stack(void)
12901314
local_save_flags(flags);
12911315

12921316
/* skipping 3 traces, seems to get us at the caller of this function */
1293-
__ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
1317+
__ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
12941318
}
12951319

12961320
static DEFINE_PER_CPU(int, user_stack_count);

‎kernel/trace/trace.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ void update_max_tr_single(struct trace_array *tr,
389389
void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
390390
int skip, int pc);
391391

392+
void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
393+
int skip, int pc, struct pt_regs *regs);
394+
392395
void ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags,
393396
int pc);
394397

@@ -400,6 +403,12 @@ static inline void ftrace_trace_stack(struct ring_buffer *buffer,
400403
{
401404
}
402405

406+
static inline void ftrace_trace_stack_regs(struct ring_buffer *buffer,
407+
unsigned long flags, int skip,
408+
int pc, struct pt_regs *regs)
409+
{
410+
}
411+
403412
static inline void ftrace_trace_userstack(struct ring_buffer *buffer,
404413
unsigned long flags, int pc)
405414
{

‎kernel/trace/trace_kprobe.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,8 @@ static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
13971397
store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
13981398

13991399
if (!filter_current_check_discard(buffer, call, entry, event))
1400-
trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
1400+
trace_nowake_buffer_unlock_commit_regs(buffer, event,
1401+
irq_flags, pc, regs);
14011402
}
14021403

14031404
/* Kretprobe handler */
@@ -1429,7 +1430,8 @@ static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
14291430
store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
14301431

14311432
if (!filter_current_check_discard(buffer, call, entry, event))
1432-
trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
1433+
trace_nowake_buffer_unlock_commit_regs(buffer, event,
1434+
irq_flags, pc, regs);
14331435
}
14341436

14351437
/* Event entry printers */

0 commit comments

Comments
 (0)