blob: d640a86198b19f89e6ff7302ee063ef2d523f987 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * @file backtrace.c
3 *
4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
6 *
7 * @author John Levon
8 * @author David Smith
9 */
10
11#include <linux/oprofile.h>
12#include <linux/sched.h>
13#include <linux/mm.h>
14#include <asm/ptrace.h>
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070015#include <asm/uaccess.h>
Jan Blunck574a6042007-10-19 20:35:03 +020016#include <asm/stacktrace.h>
17
18static void backtrace_warning_symbol(void *data, char *msg,
19 unsigned long symbol)
20{
21 /* Ignore warnings */
22}
23
24static void backtrace_warning(void *data, char *msg)
25{
26 /* Ignore warnings */
27}
28
29static int backtrace_stack(void *data, char *name)
30{
31 /* Yes, we want all stacks */
32 return 0;
33}
34
Arjan van de Venbc850d62008-01-30 13:33:07 +010035static void backtrace_address(void *data, unsigned long addr, int reliable)
Jan Blunck574a6042007-10-19 20:35:03 +020036{
37 unsigned int *depth = data;
38
39 if ((*depth)--)
40 oprofile_add_trace(addr);
41}
42
43static struct stacktrace_ops backtrace_ops = {
Frederic Weisbecker61c19172009-12-17 05:40:33 +010044 .warning = backtrace_warning,
45 .warning_symbol = backtrace_warning_symbol,
46 .stack = backtrace_stack,
47 .address = backtrace_address,
48 .walk_stack = print_context_stack,
Jan Blunck574a6042007-10-19 20:35:03 +020049};
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Jiri Olsa40c6b3c2010-09-29 10:46:46 -040051static struct stack_frame *dump_user_backtrace(struct stack_frame *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Jiri Olsa40c6b3c2010-09-29 10:46:46 -040053 struct stack_frame bufhead[2];
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070054
Jiri Olsa40c6b3c2010-09-29 10:46:46 -040055 /* Also check accessibility of one struct stack_frame beyond */
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070056 if (!access_ok(VERIFY_READ, head, sizeof(bufhead)))
57 return NULL;
58 if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead)))
59 return NULL;
60
Jiri Olsa40c6b3c2010-09-29 10:46:46 -040061 oprofile_add_trace(bufhead[0].return_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 /* frame pointers should strictly progress back up the stack
64 * (towards higher addresses) */
Jiri Olsa40c6b3c2010-09-29 10:46:46 -040065 if (head >= bufhead[0].next_frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return NULL;
67
Jiri Olsa40c6b3c2010-09-29 10:46:46 -040068 return bufhead[0].next_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071void
72x86_backtrace(struct pt_regs * const regs, unsigned int depth)
73{
Jiri Olsa40c6b3c2010-09-29 10:46:46 -040074 struct stack_frame *head = (struct stack_frame *)frame_pointer(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Vincent Hanquezfa1e1bd2005-06-23 00:08:44 -070076 if (!user_mode_vm(regs)) {
Masami Hiramatsu7b6c6c72009-05-11 17:03:00 -040077 unsigned long stack = kernel_stack_pointer(regs);
Jan Blunck574a6042007-10-19 20:35:03 +020078 if (depth)
Arjan van de Ven5bc27dc2008-01-30 13:33:07 +010079 dump_trace(NULL, regs, (unsigned long *)stack, 0,
Jan Blunck574a6042007-10-19 20:35:03 +020080 &backtrace_ops, &depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return;
82 }
83
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070084 while (depth-- && head)
Gerald Britton30379442006-02-14 10:19:04 -050085 head = dump_user_backtrace(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}