2 * Stack trace management functions
4 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
6 #include <linux/sched.h>
7 #include <linux/stacktrace.h>
8 #include <linux/module.h>
9 #include <linux/uaccess.h>
10 #include <asm/stacktrace.h>
12 static void save_stack_warning(void *data
, char *msg
)
17 save_stack_warning_symbol(void *data
, char *msg
, unsigned long symbol
)
21 static int save_stack_stack(void *data
, char *name
)
26 static void save_stack_address(void *data
, unsigned long addr
, int reliable
)
28 struct stack_trace
*trace
= data
;
31 if (trace
->skip
> 0) {
35 if (trace
->nr_entries
< trace
->max_entries
)
36 trace
->entries
[trace
->nr_entries
++] = addr
;
40 save_stack_address_nosched(void *data
, unsigned long addr
, int reliable
)
42 struct stack_trace
*trace
= (struct stack_trace
*)data
;
45 if (in_sched_functions(addr
))
47 if (trace
->skip
> 0) {
51 if (trace
->nr_entries
< trace
->max_entries
)
52 trace
->entries
[trace
->nr_entries
++] = addr
;
55 static const struct stacktrace_ops save_stack_ops
= {
56 .warning
= save_stack_warning
,
57 .warning_symbol
= save_stack_warning_symbol
,
58 .stack
= save_stack_stack
,
59 .address
= save_stack_address
,
62 static const struct stacktrace_ops save_stack_ops_nosched
= {
63 .warning
= save_stack_warning
,
64 .warning_symbol
= save_stack_warning_symbol
,
65 .stack
= save_stack_stack
,
66 .address
= save_stack_address_nosched
,
70 * Save stack-backtrace addresses into a stack_trace buffer.
72 void save_stack_trace(struct stack_trace
*trace
)
74 dump_trace(current
, NULL
, NULL
, 0, &save_stack_ops
, trace
);
75 if (trace
->nr_entries
< trace
->max_entries
)
76 trace
->entries
[trace
->nr_entries
++] = ULONG_MAX
;
78 EXPORT_SYMBOL_GPL(save_stack_trace
);
80 void save_stack_trace_tsk(struct task_struct
*tsk
, struct stack_trace
*trace
)
82 dump_trace(tsk
, NULL
, NULL
, 0, &save_stack_ops_nosched
, trace
);
83 if (trace
->nr_entries
< trace
->max_entries
)
84 trace
->entries
[trace
->nr_entries
++] = ULONG_MAX
;
86 EXPORT_SYMBOL_GPL(save_stack_trace_tsk
);
88 /* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
91 const void __user
*next_fp
;
92 unsigned long ret_addr
;
95 static int copy_stack_frame(const void __user
*fp
, struct stack_frame
*frame
)
99 if (!access_ok(VERIFY_READ
, fp
, sizeof(*frame
)))
104 if (__copy_from_user_inatomic(frame
, fp
, sizeof(*frame
)))
111 static inline void __save_stack_trace_user(struct stack_trace
*trace
)
113 const struct pt_regs
*regs
= task_pt_regs(current
);
114 const void __user
*fp
= (const void __user
*)regs
->bp
;
116 if (trace
->nr_entries
< trace
->max_entries
)
117 trace
->entries
[trace
->nr_entries
++] = regs
->ip
;
119 while (trace
->nr_entries
< trace
->max_entries
) {
120 struct stack_frame frame
;
122 frame
.next_fp
= NULL
;
124 if (!copy_stack_frame(fp
, &frame
))
126 if ((unsigned long)fp
< regs
->sp
)
128 if (frame
.ret_addr
) {
129 trace
->entries
[trace
->nr_entries
++] =
132 if (fp
== frame
.next_fp
)
138 void save_stack_trace_user(struct stack_trace
*trace
)
141 * Trace user stack if we are not a kernel thread
144 __save_stack_trace_user(trace
);
146 if (trace
->nr_entries
< trace
->max_entries
)
147 trace
->entries
[trace
->nr_entries
++] = ULONG_MAX
;