]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/arm-ds5-gator.git/blob - driver/gator_backtrace.c
gator-driver: Handle task struct correctly
[android-sdk/arm-ds5-gator.git] / driver / gator_backtrace.c
1 /**
2  * Copyright (C) ARM Limited 2010-2013. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  */
10 /*
11  * EABI backtrace stores {fp,lr} on the stack.
12  */
13 struct stack_frame_eabi {
14         union {
15                 struct {
16                         unsigned long fp;
17                         // May be the fp in the case of a leaf function or clang
18                         unsigned long lr;
19                         // If lr is really the fp, lr2 is the corresponding lr
20                         unsigned long lr2;
21                 };
22                 // Used to read 32 bit fp/lr from a 64 bit kernel
23                 struct {
24                         u32 fp_32;
25                         // same as lr above
26                         u32 lr_32;
27                         // same as lr2 above
28                         u32 lr2_32;
29                 };
30         };
31 };
33 static void arm_backtrace_eabi(int cpu, struct pt_regs *const regs, unsigned int depth)
34 {
35 #if defined(__arm__) || defined(__aarch64__)
36         struct stack_frame_eabi *curr;
37         struct stack_frame_eabi bufcurr;
38 #if defined(__arm__)
39         const bool is_compat = false;
40         unsigned long fp = regs->ARM_fp;
41         unsigned long sp = regs->ARM_sp;
42         unsigned long lr = regs->ARM_lr;
43         const int gcc_frame_offset = sizeof(unsigned long);
44 #else
45         // Is userspace aarch32 (32 bit)
46         const bool is_compat = compat_user_mode(regs);
47         unsigned long fp = (is_compat ? regs->regs[11] : regs->regs[29]);
48         unsigned long sp = (is_compat ? regs->compat_sp : regs->sp);
49         unsigned long lr = (is_compat ? regs->compat_lr : regs->regs[30]);
50         const int gcc_frame_offset = (is_compat ? sizeof(u32) : 0);
51 #endif
52         // clang frame offset is always zero
53         int is_user_mode = user_mode(regs);
55         // pc (current function) has already been added
57         if (!is_user_mode) {
58                 return;
59         }
61         // Add the lr (parent function)
62         // entry preamble may not have executed
63         gator_add_trace(cpu, lr);
65         // check fp is valid
66         if (fp == 0 || fp < sp) {
67                 return;
68         }
70         // Get the current stack frame
71         curr = (struct stack_frame_eabi *)(fp - gcc_frame_offset);
72         if ((unsigned long)curr & 3) {
73                 return;
74         }
76         while (depth-- && curr) {
77                 if (!access_ok(VERIFY_READ, curr, sizeof(struct stack_frame_eabi)) ||
78                                 __copy_from_user_inatomic(&bufcurr, curr, sizeof(struct stack_frame_eabi))) {
79                         return;
80                 }
82                 fp = (is_compat ? bufcurr.fp_32 : bufcurr.fp);
83                 lr = (is_compat ? bufcurr.lr_32 : bufcurr.lr);
85 #define calc_next(reg) ((reg) - gcc_frame_offset)
86                 // Returns true if reg is a valid fp
87 #define validate_next(reg, curr) \
88                 ((reg) != 0 && (calc_next(reg) & 3) == 0 && (unsigned long)(curr) < calc_next(reg))
90                 // Try lr from the stack as the fp because gcc leaf functions do not push lr
91                 // If gcc_frame_offset is non-zero, the lr will also be the clang fp
92                 // This assumes code is at a lower address than the stack
93                 if (validate_next(lr, curr)) {
94                         fp = lr;
95                         lr = (is_compat ? bufcurr.lr2_32 : bufcurr.lr2);
96                 }
98                 gator_add_trace(cpu, lr);
100                 if (!validate_next(fp, curr)) {
101                         return;
102                 }
104                 // Move to the next stack frame
105                 curr = (struct stack_frame_eabi *)calc_next(fp);
106         }
107 #endif
110 #if defined(__arm__) || defined(__aarch64__)
111 static int report_trace(struct stackframe *frame, void *d)
113         unsigned int *depth = d, cookie = NO_COOKIE;
114         unsigned long addr = frame->pc;
116         if (*depth) {
117 #if defined(MODULE)
118                 unsigned int cpu = get_physical_cpu();
119                 struct module *mod = __module_address(addr);
120                 if (mod) {
121                         cookie = get_cookie(cpu, current, mod->name, false);
122                         addr = addr - (unsigned long)mod->module_core;
123                 }
124 #endif
125                 marshal_backtrace(addr & ~1, cookie);
126                 (*depth)--;
127         }
129         return *depth == 0;
131 #endif
133 // Uncomment the following line to enable kernel stack unwinding within gator, note it can also be defined from the Makefile
134 // #define GATOR_KERNEL_STACK_UNWINDING
136 #if (defined(__arm__) || defined(__aarch64__)) && !defined(GATOR_KERNEL_STACK_UNWINDING)
137 // Disabled by default
138 MODULE_PARM_DESC(kernel_stack_unwinding, "Allow kernel stack unwinding.");
139 bool kernel_stack_unwinding = 0;
140 module_param(kernel_stack_unwinding, bool, 0644);
141 #endif
143 static void kernel_backtrace(int cpu, struct pt_regs *const regs)
145 #if defined(__arm__) || defined(__aarch64__)
146 #ifdef GATOR_KERNEL_STACK_UNWINDING
147         int depth = gator_backtrace_depth;
148 #else
149         int depth = (kernel_stack_unwinding ? gator_backtrace_depth : 1);
150 #endif
151         struct stackframe frame;
152         if (depth == 0)
153                 depth = 1;
154 #if defined(__arm__)
155         frame.fp = regs->ARM_fp;
156         frame.sp = regs->ARM_sp;
157         frame.lr = regs->ARM_lr;
158         frame.pc = regs->ARM_pc;
159 #else
160         frame.fp = regs->regs[29];
161         frame.sp = regs->sp;
162         frame.pc = regs->pc;
163 #endif
164         walk_stackframe(&frame, report_trace, &depth);
165 #else
166         marshal_backtrace(PC_REG & ~1, NO_COOKIE);
167 #endif