2 * Stack trace management functions
4 * Copyright (C) 2006-2009 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
6 #include <linux/sched.h>
7 #include <linux/stacktrace.h>
8 #include <linux/export.h>
9 #include <linux/uaccess.h>
10 #include <asm/stacktrace.h>
11 #include <asm/unwind.h>
13 static int save_stack_address(struct stack_trace *trace, unsigned long addr,
16 if (nosched && in_sched_functions(addr))
19 if (trace->skip > 0) {
24 if (trace->nr_entries >= trace->max_entries)
27 trace->entries[trace->nr_entries++] = addr;
31 static void __save_stack_trace(struct stack_trace *trace,
32 struct task_struct *task, struct pt_regs *regs,
35 struct unwind_state state;
39 save_stack_address(trace, regs->ip, nosched);
41 for (unwind_start(&state, task, regs, NULL); !unwind_done(&state);
42 unwind_next_frame(&state)) {
43 addr = unwind_get_return_address(&state);
44 if (!addr || save_stack_address(trace, addr, nosched))
48 if (trace->nr_entries < trace->max_entries)
49 trace->entries[trace->nr_entries++] = ULONG_MAX;
53 * Save stack-backtrace addresses into a stack_trace buffer.
55 void save_stack_trace(struct stack_trace *trace)
57 __save_stack_trace(trace, current, NULL, false);
59 EXPORT_SYMBOL_GPL(save_stack_trace);
61 void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
63 __save_stack_trace(trace, current, regs, false);
66 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
68 if (!try_get_task_stack(tsk))
71 __save_stack_trace(trace, tsk, NULL, true);
75 EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
77 /* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
79 struct stack_frame_user {
80 const void __user *next_fp;
81 unsigned long ret_addr;
85 copy_stack_frame(const void __user *fp, struct stack_frame_user *frame)
89 if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
94 if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
101 static inline void __save_stack_trace_user(struct stack_trace *trace)
103 const struct pt_regs *regs = task_pt_regs(current);
104 const void __user *fp = (const void __user *)regs->bp;
106 if (trace->nr_entries < trace->max_entries)
107 trace->entries[trace->nr_entries++] = regs->ip;
109 while (trace->nr_entries < trace->max_entries) {
110 struct stack_frame_user frame;
112 frame.next_fp = NULL;
114 if (!copy_stack_frame(fp, &frame))
116 if ((unsigned long)fp < regs->sp)
118 if (frame.ret_addr) {
119 trace->entries[trace->nr_entries++] =
122 if (fp == frame.next_fp)
128 void save_stack_trace_user(struct stack_trace *trace)
131 * Trace user stack if we are not a kernel thread
134 __save_stack_trace_user(trace);
136 if (trace->nr_entries < trace->max_entries)
137 trace->entries[trace->nr_entries++] = ULONG_MAX;