Prusa MINI Firmware overview
unwinder.h
Go to the documentation of this file.
1 /***************************************************************************
2  * ARM Stack Unwinder, Michael.McTernan.2001@cs.bris.ac.uk
3  * Updated, adapted and several bug fixes on 2018 by Eduardo José Tagle
4  *
5  * This program is PUBLIC DOMAIN.
6  * This means that there is no copyright and anyone is able to take a copy
7  * for free and use it as they wish, with or without modifications, and in
8  * any context, commerically or otherwise. The only limitation is that I
9  * don't guarantee that the software is fit for any purpose or accept any
10  * liablity for it's use or misuse - this software is without warranty.
11  **************************************************************************/
12 /** \file
13  * Interface to the ARM stack unwinding module.
14  **************************************************************************/
15 
16 #pragma once
17 
18 #include <stdint.h>
19 
20 /** \def UNW_DEBUG
21  * If this define is set, additional information will be produced while
22  * unwinding the stack to allow debug of the unwind module itself.
23  */
24 /* #define UNW_DEBUG 1 */
25 
26 /***************************************************************************
27  * Type Definitions
28  **************************************************************************/
29 
30 /** Possible results for UnwindStart to return.
31  */
32 typedef enum {
33  /** Unwinding was successful and complete. */
34  UNWIND_SUCCESS = 0,
35 
36  /** Not an error: More frames are available. */
38 
39  /** Unsupported DWARF unwind personality. */
41 
42  /** Refused to perform unwind. */
43  UNWIND_REFUSED = -2,
44 
45  /** Reached an invalid SP. */
46  UNWIND_INVALID_SP = -3,
47 
48  /** Reached an invalid PC */
49  UNWIND_INVALID_PC = -4,
50 
51  /** Unsupported DWARF instruction */
53 
54  /** More than UNW_MAX_INSTR_COUNT instructions were interpreted. */
55  UNWIND_EXHAUSTED = -6,
56 
57  /** Unwinding stopped because the reporting func returned false. */
58  UNWIND_TRUNCATED = -7,
59 
60  /** Read data was found to be inconsistent. */
62 
63  /** Unsupported instruction or data found. */
64  UNWIND_UNSUPPORTED = -9,
65 
66  /** General failure. */
67  UNWIND_FAILURE = -10,
68 
69  /** Illegal instruction. */
71 
72  /** Unwinding hit the reset vector. */
73  UNWIND_RESET = -12,
74 
75  /** Failed read for an instruction word. */
76  UNWIND_IREAD_W_FAIL = -13,
77 
78  /** Failed read for an instruction half-word. */
79  UNWIND_IREAD_H_FAIL = -14,
80 
81  /** Failed read for an instruction byte. */
82  UNWIND_IREAD_B_FAIL = -15,
83 
84  /** Failed read for a data word. */
85  UNWIND_DREAD_W_FAIL = -16,
86 
87  /** Failed read for a data half-word. */
88  UNWIND_DREAD_H_FAIL = -17,
89 
90  /** Failed read for a data byte. */
91  UNWIND_DREAD_B_FAIL = -18,
92 
93  /** Failed write for a data word. */
95 
96 } UnwResult;
97 
98 /** A backtrace report */
99 typedef struct {
100  uint32_t function; /** Starts address of function */
101  const char *name; /** Function name, or null if not available */
102  uint32_t address; /** PC on that function */
103 } UnwReport;
104 
105 /** Type for function pointer for result callback.
106  * The function is passed two parameters, the first is a void * pointer,
107  * and the second is the return address of the function. The bottom bit
108  * of the passed address indicates the execution mode; if it is set,
109  * the execution mode at the return address is Thumb, otherwise it is
110  * ARM.
111  *
112  * The return value of this function determines whether unwinding should
113  * continue or not. If true is returned, unwinding will continue and the
114  * report function maybe called again in future. If false is returned,
115  * unwinding will stop with UnwindStart() returning UNWIND_TRUNCATED.
116  */
117 typedef bool (*UnwindReportFunc)(void* data, const UnwReport* bte);
118 
119 /** Structure that holds memory callback function pointers.
120  */
121 typedef struct {
122 
123  /** Report an unwind result. */
124  UnwindReportFunc report;
125 
126  /** Read a 32 bit word from memory.
127  * The memory address to be read is passed as \a address, and
128  * \a *val is expected to be populated with the read value.
129  * If the address cannot or should not be read, false can be
130  * returned to indicate that unwinding should stop. If true
131  * is returned, \a *val is assumed to be valid and unwinding
132  * will continue.
133  */
134  bool (*readW)(const uint32_t address, uint32_t *val);
135 
136  /** Read a 16 bit half-word from memory.
137  * This function has the same usage as for readW, but is expected
138  * to read only a 16 bit value.
139  */
140  bool (*readH)(const uint32_t address, uint16_t *val);
141 
142  /** Read a byte from memory.
143  * This function has the same usage as for readW, but is expected
144  * to read only an 8 bit value.
145  */
146  bool (*readB)(const uint32_t address, uint8_t *val);
147 
148  #ifdef UNW_DEBUG
149  /** Print a formatted line for debug. */
150  void (*printf)(const char *format, ...);
151  #endif
153 
154 /* A frame */
155 typedef struct {
156  uint32_t fp;
157  uint32_t sp;
158  uint32_t lr;
159  uint32_t pc;
160 } UnwindFrame;
161 
162 /** Start unwinding the current stack.
163  * This will unwind the stack starting at the PC value supplied to in the
164  * link register (i.e. not a normal register) and the stack pointer value
165  * supplied.
166  *
167  * -If the program was compiled with -funwind-tables , it will use them to
168  * perform the traceback. Otherwise, brute force will be employed
169  * -If the program was compiled with -mpoke-function-name, then you will
170  * get function names in the traceback. Otherwise, you will not.
171  */
172 UnwResult UnwindStart(UnwindFrame* frame, const UnwindCallbacks *cb, void *data);
unwarm.h
UnwInitState
void UnwInitState(UnwState *const state, const UnwindCallbacks *cb, void *rptData, uint32_t pcValue, uint32_t spValue)
UNWIND_FAILURE
Definition: unwinder.h:66
UnwState
Definition: unwarm.h:87
UNWIND_DWRITE_W_FAIL
Definition: unwinder.h:93
UnwStartThumb
UnwResult UnwStartThumb(UnwState *const state)
UNWIND_IREAD_W_FAIL
Definition: unwinder.h:75
UnwReport
Definition: unwinder.h:97
UnwTabEntry
Definition: unwarmbytab.h:26
UNWIND_EXHAUSTED
Definition: unwinder.h:54
UNWIND_UNSUPPORTED_DWARF_INSTR
Definition: unwinder.h:51
UNWIND_INVALID_SP
Definition: unwinder.h:45
data
uint8_t data[8]
Definition: masstorage.h:49
UNWIND_MORE_AVAILABLE
Definition: unwinder.h:36
state
static volatile fsensor_t state
Definition: filament_sensor.c:23
UnwindReportFunc
bool(* UnwindReportFunc)(void *data, const UnwReport *bte)
Definition: unwinder.h:115
UnwindFrame
Definition: unwinder.h:153
UNWIND_INCONSISTENT
Definition: unwinder.h:60
UNWIND_UNSUPPORTED
Definition: unwinder.h:63
UNWIND_REFUSED
Definition: unwinder.h:42
UNWIND_DREAD_W_FAIL
Definition: unwinder.h:84
UNWIND_IREAD_H_FAIL
Definition: unwinder.h:78
unwinder.h
UNWIND_UNSUPPORTED_DWARF_PERSONALITY
Definition: unwinder.h:39
void
void
Definition: png.h:1083
UNWIND_DREAD_H_FAIL
Definition: unwinder.h:87
uint8_t
const uint8_t[]
Definition: 404_html.c:3
UNWIND_RESET
Definition: unwinder.h:72
address
UsbDeviceAddress address
Definition: address.h:202
unwarmbytab.h
UnwStartArm
UnwResult UnwStartArm(UnwState *const state)
UNWIND_IREAD_B_FAIL
Definition: unwinder.h:81
UNWIND_INVALID_PC
Definition: unwinder.h:48
UnwindFrame::pc
uint32_t pc
Definition: unwinder.h:157
UNWIND_DREAD_B_FAIL
Definition: unwinder.h:90
UNWIND_SUCCESS
Definition: unwinder.h:33
UnwindCallbacks
Definition: unwinder.h:119
UNWIND_TRUNCATED
Definition: unwinder.h:57
UnwindByTableStart
UnwResult UnwindByTableStart(UnwindFrame *frame, const UnwindCallbacks *cb, void *data)
UnwindFrame::sp
uint32_t sp
Definition: unwinder.h:155
UnwindStart
UnwResult UnwindStart(UnwindFrame *frame, const UnwindCallbacks *cb, void *data)
UnwResult
UnwResult
Definition: unwinder.h:30
UNWIND_ILLEGAL_INSTR
Definition: unwinder.h:69