Skip to content

Commit f16fb1e

Browse files
Russell KingRussell King
authored andcommitted
[ARM] Add stacktrace support and make oprofile use it
Add support for stacktrace. Use the new stacktrace code with oprofile instead of it's version; there's no point having multiple versions of stacktracing in the kernel. Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
1 parent ed519de commit f16fb1e

5 files changed

Lines changed: 109 additions & 54 deletions

File tree

‎arch/arm/Kconfig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ config GENERIC_HARDIRQS
6767
bool
6868
default y
6969

70+
config STACKTRACE_SUPPORT
71+
bool
72+
default y
73+
74+
config LOCKDEP_SUPPORT
75+
bool
76+
default y
77+
7078
config TRACE_IRQFLAGS_SUPPORT
7179
bool
7280
default y

‎arch/arm/kernel/Makefile‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ AFLAGS_head.o := -DTEXT_OFFSET=$(TEXT_OFFSET)
77
# Object file lists.
88

99
obj-y := compat.o entry-armv.o entry-common.o irq.o \
10-
process.o ptrace.o semaphore.o setup.o signal.o sys_arm.o \
11-
time.o traps.o
10+
process.o ptrace.o semaphore.o setup.o signal.o \
11+
sys_arm.o stacktrace.o time.o traps.o
1212

1313
obj-$(CONFIG_ISA_DMA_API) += dma.o
1414
obj-$(CONFIG_ARCH_ACORN) += ecard.o

‎arch/arm/kernel/stacktrace.c‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <linux/sched.h>
2+
#include <linux/stacktrace.h>
3+
4+
#include "stacktrace.h"
5+
6+
int walk_stackframe(unsigned long fp, unsigned long low, unsigned long high,
7+
int (*fn)(struct stackframe *, void *), void *data)
8+
{
9+
struct stackframe *frame;
10+
11+
do {
12+
/*
13+
* Check current frame pointer is within bounds
14+
*/
15+
if ((fp - 12) < low || fp + 4 >= high)
16+
break;
17+
18+
frame = (struct stackframe *)(fp - 12);
19+
20+
if (fn(frame, data))
21+
break;
22+
23+
/*
24+
* Update the low bound - the next frame must always
25+
* be at a higher address than the current frame.
26+
*/
27+
low = fp + 4;
28+
fp = frame->fp;
29+
} while (fp);
30+
31+
return 0;
32+
}
33+
34+
#ifdef CONFIG_STACKTRACE
35+
struct stack_trace_data {
36+
struct stack_trace *trace;
37+
unsigned int skip;
38+
};
39+
40+
static int save_trace(struct stackframe *frame, void *d)
41+
{
42+
struct stack_trace_data *data = d;
43+
struct stack_trace *trace = data->trace;
44+
45+
if (data->skip) {
46+
data->skip--;
47+
return 0;
48+
}
49+
50+
trace->entries[trace->nr_entries++] = frame->lr;
51+
52+
return trace->nr_entries >= trace->max_entries;
53+
}
54+
55+
void save_stack_trace(struct stack_trace *trace, struct task_struct *task)
56+
{
57+
struct stack_trace_data data;
58+
unsigned long fp, base;
59+
60+
data.trace = trace;
61+
data.skip = trace->skip;
62+
63+
if (task) {
64+
base = (unsigned long)task_stack_page(task);
65+
fp = 0; /* FIXME */
66+
} else {
67+
base = (unsigned long)task_stack_page(current);
68+
asm("mov %0, fp" : "=r" (fp));
69+
}
70+
71+
walk_stackframe(fp, base, base + THREAD_SIZE, save_trace, &data);
72+
}
73+
#endif

‎arch/arm/kernel/stacktrace.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
struct stackframe {
2+
unsigned long fp;
3+
unsigned long sp;
4+
unsigned long lr;
5+
unsigned long pc;
6+
};
7+
8+
int walk_stackframe(unsigned long fp, unsigned long low, unsigned long high,
9+
int (*fn)(struct stackframe *, void *), void *data);

‎arch/arm/oprofile/backtrace.c‎

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@
1919
#include <asm/ptrace.h>
2020
#include <asm/uaccess.h>
2121

22+
#include "../kernel/stacktrace.h"
23+
24+
static int report_trace(struct stackframe *frame, void *d)
25+
{
26+
unsigned int *depth = d;
27+
28+
if (*depth) {
29+
oprofile_add_trace(frame->lr);
30+
(*depth)--;
31+
}
32+
33+
return *depth == 0;
34+
}
2235

2336
/*
2437
* The registers we're interested in are at the end of the variable
@@ -32,21 +45,6 @@ struct frame_tail {
3245
unsigned long lr;
3346
} __attribute__((packed));
3447

35-
36-
#ifdef CONFIG_FRAME_POINTER
37-
static struct frame_tail* kernel_backtrace(struct frame_tail *tail)
38-
{
39-
oprofile_add_trace(tail->lr);
40-
41-
/* frame pointers should strictly progress back up the stack
42-
* (towards higher addresses) */
43-
if (tail >= tail->fp)
44-
return NULL;
45-
46-
return tail->fp-1;
47-
}
48-
#endif
49-
5048
static struct frame_tail* user_backtrace(struct frame_tail *tail)
5149
{
5250
struct frame_tail buftail[2];
@@ -67,47 +65,14 @@ static struct frame_tail* user_backtrace(struct frame_tail *tail)
6765
return buftail[0].fp-1;
6866
}
6967

70-
/*
71-
* | | /\ Higher addresses
72-
* | |
73-
* --------------- stack base (address of current_thread_info)
74-
* | thread info |
75-
* . .
76-
* | stack |
77-
* --------------- saved regs->ARM_fp value if valid (frame_tail address)
78-
* . .
79-
* --------------- struct pt_regs stored on stack (struct pt_regs *)
80-
* | |
81-
* . .
82-
* | |
83-
* --------------- %esp
84-
* | |
85-
* | | \/ Lower addresses
86-
*
87-
* Thus, &pt_regs <-> stack base restricts the valid(ish) fp values
88-
*/
89-
static int valid_kernel_stack(struct frame_tail *tail, struct pt_regs *regs)
90-
{
91-
unsigned long tailaddr = (unsigned long)tail;
92-
unsigned long stack = (unsigned long)regs;
93-
unsigned long stack_base = (stack & ~(THREAD_SIZE - 1)) + THREAD_SIZE;
94-
95-
return (tailaddr > stack) && (tailaddr < stack_base);
96-
}
97-
9868
void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
9969
{
100-
struct frame_tail *tail;
101-
102-
tail = ((struct frame_tail *) regs->ARM_fp) - 1;
70+
struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1;
10371

10472
if (!user_mode(regs)) {
105-
106-
#ifdef CONFIG_FRAME_POINTER
107-
while (depth-- && tail && valid_kernel_stack(tail, regs)) {
108-
tail = kernel_backtrace(tail);
109-
}
110-
#endif
73+
unsigned long base = ((unsigned long)regs) & ~(THREAD_SIZE - 1);
74+
walk_stackframe(regs->ARM_fp, base, base + THREAD_SIZE,
75+
report_trace, &depth);
11176
return;
11277
}
11378

0 commit comments

Comments
 (0)