summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAidan MacDonald <amachronic@protonmail.com>2022-05-16 14:33:26 +0100
committerAidan MacDonald <amachronic@protonmail.com>2022-05-22 07:16:11 -0400
commit981e9728390b401404c36241e2ce6bd4cfcb723d (patch)
treeb68a7eaf7bf53dcd0dea8b29324c1e303bbb48f7
parentcade488b089667f1252220d6b613c6795f960852 (diff)
downloadrockbox-981e9728390b401404c36241e2ce6bd4cfcb723d.tar.gz
rockbox-981e9728390b401404c36241e2ce6bd4cfcb723d.zip
mips: add native backtrace implementation
Should make debugging crashes on native MIPS targets far easier. This is by no means a 100% complete or robust implementation but it seems to handle the vast majority of functions. Change-Id: Id5f430270e02b5092b79026b6876675c784aa649
-rw-r--r--firmware/export/backtrace.h3
-rw-r--r--firmware/export/system.h3
-rw-r--r--firmware/panic.c10
-rw-r--r--lib/mipsunwinder/SOURCES2
-rw-r--r--lib/mipsunwinder/backtrace-mips32.c236
-rw-r--r--lib/mipsunwinder/backtrace-mipsunwinder.h65
-rw-r--r--lib/mipsunwinder/init_context_32.S12
-rw-r--r--lib/mipsunwinder/mipsunwinder.make23
-rw-r--r--tools/root.make6
9 files changed, 357 insertions, 3 deletions
diff --git a/firmware/export/backtrace.h b/firmware/export/backtrace.h
index 283e293b2a..fb007ab004 100644
--- a/firmware/export/backtrace.h
+++ b/firmware/export/backtrace.h
@@ -25,6 +25,9 @@
25#ifdef BACKTRACE_UNWARMINDER 25#ifdef BACKTRACE_UNWARMINDER
26#include "backtrace-unwarminder.h" 26#include "backtrace-unwarminder.h"
27#endif 27#endif
28#ifdef BACKTRACE_MIPSUNWINDER
29#include "backtrace-mipsunwinder.h"
30#endif
28 31
29/* Print a backtrace using lcd_* functions, starting at the given line and updating 32/* Print a backtrace using lcd_* functions, starting at the given line and updating
30 * the line number. On targets that support it (typically native targets), the 33 * the line number. On targets that support it (typically native targets), the
diff --git a/firmware/export/system.h b/firmware/export/system.h
index 9558be559a..def3122205 100644
--- a/firmware/export/system.h
+++ b/firmware/export/system.h
@@ -258,7 +258,8 @@ static inline void cpu_boost_unlock(void)
258#endif 258#endif
259 259
260/* Define this if target has support for generating backtraces */ 260/* Define this if target has support for generating backtraces */
261#ifdef CPU_ARM 261#if defined(CPU_ARM) || \
262 (defined(CPU_MIPS) && (CONFIG_PLATFORM & PLATFORM_NATIVE))
262 #define HAVE_RB_BACKTRACE 263 #define HAVE_RB_BACKTRACE
263#endif 264#endif
264 265
diff --git a/firmware/panic.c b/firmware/panic.c
index fcfa8b2bb8..586ecb6e0a 100644
--- a/firmware/panic.c
+++ b/firmware/panic.c
@@ -32,9 +32,9 @@
32#include "system.h" 32#include "system.h"
33#include "logf.h" 33#include "logf.h"
34 34
35#if defined(CPU_ARM) 35#ifdef HAVE_RB_BACKTRACE
36#include "gcc_extensions.h" 36#include "gcc_extensions.h"
37#include <backtrace.h> 37#include "backtrace.h"
38#endif 38#endif
39 39
40static char panic_buf[128]; 40static char panic_buf[128];
@@ -65,6 +65,12 @@ void panicf_f( const char *fmt, ...)
65 ); 65 );
66 66
67 int pc = (int)__builtin_return_address(0); 67 int pc = (int)__builtin_return_address(0);
68#elif defined(BACKTRACE_MIPSUNWINDER)
69void panicf( const char *fmt, ... )
70{
71 /* NOTE: these are obtained by the backtrace lib */
72 const int pc = 0;
73 const int sp = 0;
68#else 74#else
69void panicf( const char *fmt, ...) 75void panicf( const char *fmt, ...)
70{ 76{
diff --git a/lib/mipsunwinder/SOURCES b/lib/mipsunwinder/SOURCES
new file mode 100644
index 0000000000..32fa57d157
--- /dev/null
+++ b/lib/mipsunwinder/SOURCES
@@ -0,0 +1,2 @@
1backtrace-mips32.c
2init_context_32.S
diff --git a/lib/mipsunwinder/backtrace-mips32.c b/lib/mipsunwinder/backtrace-mips32.c
new file mode 100644
index 0000000000..c5ab41e628
--- /dev/null
+++ b/lib/mipsunwinder/backtrace-mips32.c
@@ -0,0 +1,236 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * MIPS32 backtrace implementation
11 * Copyright (C) 2022 Aidan MacDonald
12 *
13 * References:
14 * https://yosefk.com/blog/getting-the-call-stack-without-a-frame-pointer.html
15 * https://elinux.org/images/6/68/ELC2008_-_Back-tracing_in_MIPS-based_Linux_Systems.pdf
16 * System V ABI MIPS Supplement, 3rd edition
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version 2
21 * of the License, or (at your option) any later version.
22 *
23 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
24 * KIND, either express or implied.
25 *
26 ****************************************************************************/
27
28#include "backtrace.h"
29#include "lcd.h"
30#include <stdint.h>
31#include <stdbool.h>
32#include <stdalign.h>
33#include <stdarg.h>
34#include <stdio.h>
35
36#define MIN_ADDR 0x80000000ul
37#define MAX_ADDR (MIN_ADDR + (MEMORYSIZE * 1024 * 1024) - 1)
38
39static bool read_check(const void* addr, size_t alignment)
40{
41 if(addr < (const void*)MIN_ADDR ||
42 addr > (const void*)MAX_ADDR)
43 return false;
44
45 if((uintptr_t)addr & (alignment - 1))
46 return false;
47
48 return true;
49}
50
51static bool read32(const void* addr, uint32_t* val)
52{
53 if(!read_check(addr, alignof(uint32_t)))
54 return false;
55
56 *val = *(const uint32_t*)addr;
57 return true;
58}
59
60#if 0
61static bool read16(const void* addr, uint16_t* val)
62{
63 if(!read_check(addr, alignof(uint16_t)))
64 return false;
65
66 *val = *(const uint16_t*)addr;
67 return true;
68}
69
70static bool read8(const void* addr, uint8_t* val)
71{
72 if(!read_check(addr, alignof(uint8_t)))
73 return false;
74
75 *val = *(const uint8_t*)addr;
76 return true;
77}
78#endif
79
80static long extract_s16(uint32_t val)
81{
82#if 1
83 /* not ISO C, but gets GCC to emit 'seh' which is more compact */
84 return (int32_t)(val << 16) >> 16;
85#else
86 val &= 0xffff;
87
88 if(val > 0x7fff)
89 return (long)val - 0x10000l;
90 else
91 return val;
92#endif
93}
94
95/* TODO - cases not handled by the backtrace algorithm
96 *
97 * 1. functions that save the frame pointer will not be handled correctly
98 * (need to implement the algorithm specified by the System V ABI).
99 *
100 * 2. GCC can generate functions with a "false" stack pointer decrement,
101 * for some examples see read_bmp_fd and walk_path. those functions
102 * seem to be more difficult to deal with and the SysV algorithm will
103 * also get confused by them, but they are not common.
104 */
105int mips_bt_step(struct mips_bt_context* ctx)
106{
107 /* go backward and look for the stack pointer decrement */
108 uint32_t* pc = ctx->pc;
109 uint32_t insn;
110 long sp_off;
111
112 while(true) {
113 if(!read32(pc, &insn))
114 return 0;
115
116 /* addiu sp, sp, sp_off */
117 if((insn >> 16) == 0x27bd) {
118 sp_off = extract_s16(insn);
119 if(sp_off < 0)
120 break;
121 }
122
123 /* jr ra */
124 if(insn == 0x03e00008) {
125 /* end if this is not a leaf or we lack register info */
126 if(ctx->depth > 0 || !(ctx->valid & (1 << MIPSBT_RA))) {
127 mips_bt_debug(ctx, "unexpected leaf function");
128 return 0;
129 }
130
131 /* this is a leaf function - ra contains the return address
132 * and sp is unchanged */
133 ctx->pc = (void*)ctx->reg[MIPSBT_RA] - 8;
134 ctx->depth++;
135 return 1;
136 }
137
138 --pc;
139 }
140
141 mips_bt_debug(ctx, "found sp_off=%ld at %p", sp_off, pc);
142
143 /* now go forward and find the saved return address */
144 while((void*)pc < ctx->pc) {
145 if(!read32(pc, &insn))
146 return 0;
147
148 /* sw ra, ra_off(sp) */
149 if((insn >> 16) == 0xafbf) {
150 long ra_off = extract_s16(insn);
151 uint32_t save_ra;
152
153 /* load the saved return address */
154 mips_bt_debug(ctx, "load ra from %p+%ld", ctx->sp, ra_off);
155 if(!read32(ctx->sp + ra_off, &save_ra))
156 return 0;
157
158 if(save_ra == 0) {
159 mips_bt_debug("hit root");
160 return 0;
161 }
162
163 /* update bt context */
164 ctx->pc = (void*)save_ra - 8;
165 ctx->sp -= sp_off;
166
167 /* update saved register info */
168 ctx->reg[MIPSBT_RA] = save_ra;
169 ctx->valid |= (1 << MIPSBT_RA);
170
171 ctx->depth++;
172 return 1;
173 }
174
175 ++pc;
176 }
177
178 /* sometimes an exception occurs before ra is saved - in this case
179 * the ra register should contain the caller PC, but we still need
180 * to adjust the stack frame. */
181 if(ctx->depth == 0 && (ctx->valid & (1 << MIPSBT_RA))) {
182 ctx->pc = (void*)ctx->reg[MIPSBT_RA] - 8;
183 ctx->sp -= sp_off;
184
185 ctx->depth++;
186 return 1;
187 }
188
189 mips_bt_debug(ctx, "ra not found");
190 return 0;
191}
192
193#ifdef MIPSUNWINDER_DEBUG
194static void rb_backtrace_debugf(void* arg, const char* fmt, ...)
195{
196 static char buf[64];
197 va_list ap;
198 va_start(ap, fmt);
199 vsnprintf(buf, sizeof(buf), fmt, ap);
200 va_end(ap);
201
202 unsigned int* line = arg;
203 lcd_putsf(4, (*line)++, "%s", buf);
204 lcd_update();
205}
206#endif
207
208void rb_backtrace_ctx(void* arg, unsigned* line)
209{
210 struct mips_bt_context* ctx = arg;
211#ifdef MIPSUNWINDER_DEBUG
212 ctx->debugf = rb_backtrace_debugf;
213 ctx->debug_arg = line;
214#endif
215
216 do {
217 lcd_putsf(0, (*line)++, "%02d pc:%08lx sp:%08lx",
218 ctx->depth, (unsigned long)ctx->pc, (unsigned long)ctx->sp);
219 lcd_update();
220 } while(mips_bt_step(ctx));
221
222 lcd_puts(0, (*line)++, "bt end");
223 lcd_update();
224}
225
226void rb_backtrace(int pcAddr, int spAddr, unsigned* line)
227{
228 (void)pcAddr;
229 (void)spAddr;
230
231 struct mips_bt_context ctx;
232 mips_bt_start(&ctx);
233 mips_bt_step(&ctx); /* step over this function */
234
235 rb_backtrace_ctx(&ctx, line);
236}
diff --git a/lib/mipsunwinder/backtrace-mipsunwinder.h b/lib/mipsunwinder/backtrace-mipsunwinder.h
new file mode 100644
index 0000000000..4d6288b5fe
--- /dev/null
+++ b/lib/mipsunwinder/backtrace-mipsunwinder.h
@@ -0,0 +1,65 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2022 Aidan MacDonald
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#ifndef BACKTRACE_MIPSUNWINDER_H
23#define BACKTRACE_MIPSUNWINDER_H
24
25/*#define MIPSUNWINDER_DEBUG*/
26
27#include <stdint.h>
28
29enum {
30 MIPSBT_RA,
31 MIPSBT_NREG,
32};
33
34struct mips_bt_context {
35 void* pc;
36 void* sp;
37 int depth;
38 uint32_t valid;
39 uint32_t reg[MIPSBT_NREG];
40#ifdef MIPSUNWINDER_DEBUG
41 void(*debugf)(void*, const char*, ...);
42 void* debug_arg;
43#endif
44};
45
46int mips_bt_step(struct mips_bt_context* ctx);
47void mips_bt_start(struct mips_bt_context* ctx);
48
49#ifdef MIPSUNWINDER_DEBUG
50# define mips_bt_debug(ctx, ...) \
51 do { struct mips_bt_context* __ctx = ctx; \
52 if(__ctx->debugf) \
53 __ctx->debugf(__ctx->debug_arg, __VA_ARGS__);\
54 } while(0)
55#else
56# define mips_bt_debug(...) do { } while(0)
57#endif
58
59/* NOTE: ignores pcAddr and spAddr, backtrace starts from caller */
60void rb_backtrace(int pcAddr, int spAddr, unsigned* line);
61
62/* given struct mips_bt_context argument, print stack traceback */
63void rb_backtrace_ctx(void* arg, unsigned* line);
64
65#endif /* BACKTRACE_MIPSUNWINDER_H */
diff --git a/lib/mipsunwinder/init_context_32.S b/lib/mipsunwinder/init_context_32.S
new file mode 100644
index 0000000000..a943d13dc3
--- /dev/null
+++ b/lib/mipsunwinder/init_context_32.S
@@ -0,0 +1,12 @@
1#include "mips.h"
2
3 .text
4 .global mips_bt_start
5
6mips_bt_start:
7 addiu v0, ra, -8
8 sw v0, 0(a0) /* ctx->pc = ra - 8 */
9 sw sp, 4(a0) /* ctx->sp = sp */
10 sw zero, 8(a0) /* ctx->depth = 0 */
11 sw zero, 12(a0) /* ctx->valid = 0 */
12 jr ra
diff --git a/lib/mipsunwinder/mipsunwinder.make b/lib/mipsunwinder/mipsunwinder.make
new file mode 100644
index 0000000000..ddd1ce078f
--- /dev/null
+++ b/lib/mipsunwinder/mipsunwinder.make
@@ -0,0 +1,23 @@
1# __________ __ ___.
2# Open \______ \ ____ ____ | | _\_ |__ _______ ___
3# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
4# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
5# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
6# \/ \/ \/ \/ \/
7#
8
9MIPSUNWINDERLIB_DIR = $(ROOTDIR)/lib/mipsunwinder
10MIPSUNWINDERLIB_SRC = $(call preprocess, $(MIPSUNWINDERLIB_DIR)/SOURCES)
11MIPSUNWINDERLIB_OBJ := $(call c2obj, $(MIPSUNWINDERLIB_SRC))
12
13OTHER_SRC += $(MIPSUNWINDERLIB_SRC)
14
15MIPSUNWINDERLIB = $(BUILDDIR)/lib/libmipsunwinder.a
16CORE_LIBS += $(MIPSUNWINDERLIB)
17
18INCLUDES += -I$(MIPSUNWINDERLIB_DIR)
19DEFINES += -DBACKTRACE_MIPSUNWINDER
20
21$(MIPSUNWINDERLIB): $(MIPSUNWINDERLIB_OBJ)
22 $(SILENT)$(shell rm -f $@)
23 $(call PRINTS,AR $(@F))$(AR) rcs $@ $^ >/dev/null
diff --git a/tools/root.make b/tools/root.make
index 2a83a32292..03c4d48986 100644
--- a/tools/root.make
+++ b/tools/root.make
@@ -75,6 +75,12 @@ ifeq (,$(findstring checkwps,$(APP_TYPE)))
75 include $(ROOTDIR)/lib/unwarminder/unwarminder.make 75 include $(ROOTDIR)/lib/unwarminder/unwarminder.make
76 endif 76 endif
77 endif 77 endif
78 ifeq (arch_mips,$(ARCH))
79 # mips unwinder is only usable on native ports
80 ifeq (,$(APP_TYPE))
81 include $(ROOTDIR)/lib/mipsunwinder/mipsunwinder.make
82 endif
83 endif
78 ifeq (,$(findstring bootloader,$(APPSDIR))) 84 ifeq (,$(findstring bootloader,$(APPSDIR)))
79 include $(ROOTDIR)/lib/skin_parser/skin_parser.make 85 include $(ROOTDIR)/lib/skin_parser/skin_parser.make
80 include $(ROOTDIR)/lib/tlsf/libtlsf.make 86 include $(ROOTDIR)/lib/tlsf/libtlsf.make