add debugging code
[cor.git] / arch / sh / oprofile / backtrace.c
blobf1205f92631df3b9289fe935b63d5a0d9667666e
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * SH specific backtracing code for oprofile
5 * Copyright 2007 STMicroelectronics Ltd.
7 * Author: Dave Peverley <dpeverley@mpc-data.co.uk>
9 * Based on ARM oprofile backtrace code by Richard Purdie and in turn, i386
10 * oprofile backtrace code by John Levon, David Smith
12 #include <linux/oprofile.h>
13 #include <linux/sched.h>
14 #include <linux/kallsyms.h>
15 #include <linux/mm.h>
16 #include <asm/unwinder.h>
17 #include <asm/ptrace.h>
18 #include <linux/uaccess.h>
19 #include <asm/sections.h>
20 #include <asm/stacktrace.h>
22 static int backtrace_stack(void *data, char *name)
24 /* Yes, we want all stacks */
25 return 0;
28 static void backtrace_address(void *data, unsigned long addr, int reliable)
30 unsigned int *depth = data;
32 if ((*depth)--)
33 oprofile_add_trace(addr);
36 static struct stacktrace_ops backtrace_ops = {
37 .stack = backtrace_stack,
38 .address = backtrace_address,
41 /* Limit to stop backtracing too far. */
42 static int backtrace_limit = 20;
44 static unsigned long *
45 user_backtrace(unsigned long *stackaddr, struct pt_regs *regs)
47 unsigned long buf_stack;
49 /* Also check accessibility of address */
50 if (!access_ok(stackaddr, sizeof(unsigned long)))
51 return NULL;
53 if (__copy_from_user_inatomic(&buf_stack, stackaddr, sizeof(unsigned long)))
54 return NULL;
56 /* Quick paranoia check */
57 if (buf_stack & 3)
58 return NULL;
60 oprofile_add_trace(buf_stack);
62 stackaddr++;
64 return stackaddr;
67 void sh_backtrace(struct pt_regs * const regs, unsigned int depth)
69 unsigned long *stackaddr;
72 * Paranoia - clip max depth as we could get lost in the weeds.
74 if (depth > backtrace_limit)
75 depth = backtrace_limit;
77 stackaddr = (unsigned long *)kernel_stack_pointer(regs);
78 if (!user_mode(regs)) {
79 if (depth)
80 unwind_stack(NULL, regs, stackaddr,
81 &backtrace_ops, &depth);
82 return;
85 while (depth-- && (stackaddr != NULL))
86 stackaddr = user_backtrace(stackaddr, regs);