Skip to content

Commit 2ec220e

Browse files
Ken ChenAlexey Dobriyan
Ken Chen
authored and
Alexey Dobriyan
committed
proc: add /proc/*/stack
/proc/*/stack adds the ability to query a task's stack trace. It is more useful than /proc/*/wchan as it provides full stack trace instead of single depth. Example output: $ cat /proc/self/stack [<c010a271>] save_stack_trace_tsk+0x17/0x35 [<c01827b4>] proc_pid_stack+0x4a/0x76 [<c018312d>] proc_single_show+0x4a/0x5e [<c016bdec>] seq_read+0xf3/0x29f [<c015a004>] vfs_read+0x6d/0x91 [<c015a0c1>] sys_read+0x3b/0x60 [<c0102eda>] syscall_call+0x7/0xb [<ffffffff>] 0xffffffff [add save_stack_trace_tsk() on mips, ACK Ralf --adobriyan] Signed-off-by: Ken Chen <kenchen@google.com> Signed-off-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
1 parent 631f9c1 commit 2ec220e

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

Documentation/filesystems/proc.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ Table 1-1: Process specific entries in /proc
140140
statm Process memory status information
141141
status Process status in human readable form
142142
wchan If CONFIG_KALLSYMS is set, a pre-decoded wchan
143+
stack Report full stack trace, enable via CONFIG_STACKTRACE
143144
smaps Extension based on maps, the rss size for each mapped file
144145
..............................................................................
145146

arch/mips/kernel/stacktrace.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ static void save_raw_context_stack(struct stack_trace *trace,
3232
}
3333
}
3434

35-
static void save_context_stack(struct stack_trace *trace, struct pt_regs *regs)
35+
static void save_context_stack(struct stack_trace *trace,
36+
struct task_struct *tsk, struct pt_regs *regs)
3637
{
3738
unsigned long sp = regs->regs[29];
3839
#ifdef CONFIG_KALLSYMS
@@ -41,7 +42,7 @@ static void save_context_stack(struct stack_trace *trace, struct pt_regs *regs)
4142

4243
if (raw_show_trace || !__kernel_text_address(pc)) {
4344
unsigned long stack_page =
44-
(unsigned long)task_stack_page(current);
45+
(unsigned long)task_stack_page(tsk);
4546
if (stack_page && sp >= stack_page &&
4647
sp <= stack_page + THREAD_SIZE - 32)
4748
save_raw_context_stack(trace, sp);
@@ -54,7 +55,7 @@ static void save_context_stack(struct stack_trace *trace, struct pt_regs *regs)
5455
trace->entries[trace->nr_entries++] = pc;
5556
if (trace->nr_entries >= trace->max_entries)
5657
break;
57-
pc = unwind_stack(current, &sp, pc, &ra);
58+
pc = unwind_stack(tsk, &sp, pc, &ra);
5859
} while (pc);
5960
#else
6061
save_raw_context_stack(trace, sp);
@@ -65,13 +66,24 @@ static void save_context_stack(struct stack_trace *trace, struct pt_regs *regs)
6566
* Save stack-backtrace addresses into a stack_trace buffer.
6667
*/
6768
void save_stack_trace(struct stack_trace *trace)
69+
{
70+
save_stack_trace_tsk(current, trace);
71+
}
72+
EXPORT_SYMBOL_GPL(save_stack_trace);
73+
74+
void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
6875
{
6976
struct pt_regs dummyregs;
7077
struct pt_regs *regs = &dummyregs;
7178

7279
WARN_ON(trace->nr_entries || !trace->max_entries);
7380

74-
prepare_frametrace(regs);
75-
save_context_stack(trace, regs);
81+
if (tsk != current) {
82+
regs->regs[29] = tsk->thread.reg29;
83+
regs->regs[31] = 0;
84+
regs->cp0_epc = tsk->thread.reg31;
85+
} else
86+
prepare_frametrace(regs);
87+
save_context_stack(trace, tsk, regs);
7688
}
77-
EXPORT_SYMBOL_GPL(save_stack_trace);
89+
EXPORT_SYMBOL_GPL(save_stack_trace_tsk);

fs/proc/base.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include <linux/mm.h>
6666
#include <linux/rcupdate.h>
6767
#include <linux/kallsyms.h>
68+
#include <linux/stacktrace.h>
6869
#include <linux/resource.h>
6970
#include <linux/module.h>
7071
#include <linux/mount.h>
@@ -337,6 +338,37 @@ static int proc_pid_wchan(struct task_struct *task, char *buffer)
337338
}
338339
#endif /* CONFIG_KALLSYMS */
339340

341+
#ifdef CONFIG_STACKTRACE
342+
343+
#define MAX_STACK_TRACE_DEPTH 64
344+
345+
static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
346+
struct pid *pid, struct task_struct *task)
347+
{
348+
struct stack_trace trace;
349+
unsigned long *entries;
350+
int i;
351+
352+
entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
353+
if (!entries)
354+
return -ENOMEM;
355+
356+
trace.nr_entries = 0;
357+
trace.max_entries = MAX_STACK_TRACE_DEPTH;
358+
trace.entries = entries;
359+
trace.skip = 0;
360+
save_stack_trace_tsk(task, &trace);
361+
362+
for (i = 0; i < trace.nr_entries; i++) {
363+
seq_printf(m, "[<%p>] %pS\n",
364+
(void *)entries[i], (void *)entries[i]);
365+
}
366+
kfree(entries);
367+
368+
return 0;
369+
}
370+
#endif
371+
340372
#ifdef CONFIG_SCHEDSTATS
341373
/*
342374
* Provides /proc/PID/schedstat
@@ -2500,6 +2532,9 @@ static const struct pid_entry tgid_base_stuff[] = {
25002532
#ifdef CONFIG_KALLSYMS
25012533
INF("wchan", S_IRUGO, proc_pid_wchan),
25022534
#endif
2535+
#ifdef CONFIG_STACKTRACE
2536+
ONE("stack", S_IRUSR, proc_pid_stack),
2537+
#endif
25032538
#ifdef CONFIG_SCHEDSTATS
25042539
INF("schedstat", S_IRUGO, proc_pid_schedstat),
25052540
#endif
@@ -2835,6 +2870,9 @@ static const struct pid_entry tid_base_stuff[] = {
28352870
#ifdef CONFIG_KALLSYMS
28362871
INF("wchan", S_IRUGO, proc_pid_wchan),
28372872
#endif
2873+
#ifdef CONFIG_STACKTRACE
2874+
ONE("stack", S_IRUSR, proc_pid_stack),
2875+
#endif
28382876
#ifdef CONFIG_SCHEDSTATS
28392877
INF("schedstat", S_IRUGO, proc_pid_schedstat),
28402878
#endif

0 commit comments

Comments
 (0)