PX4 Firmware
PX4 Autopilot Software http://px4.io
hardfault_log.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2015 PX4 Development Team. All rights reserved.
4  * Author: @author David Sidrane <david_s5@nscdg.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * 3. Neither the name PX4 nor the names of its contributors may be
17  * used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  ****************************************************************************/
34 #pragma once
35 /****************************************************************************
36  * Included Files
37  ****************************************************************************/
38 #include <px4_platform_common/px4_config.h>
39 #include <systemlib/px4_macros.h>
40 
41 /****************************************************************************
42  * Pre-processor Definitions
43  ****************************************************************************/
44 #define HARDFAULT_REBOOT_FILENO 0
45 #define HARDFAULT_REBOOT_PATH BBSRAM_PATH "" STRINGIFY(HARDFAULT_REBOOT_FILENO)
46 #define HARDFAULT_ULOG_FILENO 3
47 #define HARDFAULT_ULOG_PATH BBSRAM_PATH "" STRINGIFY(HARDFAULT_ULOG_FILENO)
48 #define HARDFAULT_FILENO 4
49 #define HARDFAULT_PATH BBSRAM_PATH "" STRINGIFY(HARDFAULT_FILENO)
50 
51 #define HARDFAULT_MAX_ULOG_FILE_LEN 64 /* must be large enough to store the full path to the log file */
52 
53 #define BBSRAM_SIZE_FN0 (sizeof(int))
54 #define BBSRAM_SIZE_FN1 384 /* greater then 2.5 times the size of vehicle_status_s */
55 #define BBSRAM_SIZE_FN2 384 /* greater then 2.5 times the size of vehicle_status_s */
56 #define BBSRAM_SIZE_FN3 HARDFAULT_MAX_ULOG_FILE_LEN
57 #define BBSRAM_SIZE_FN4 -1
58 
59 /* The following guides in the amount of the user and interrupt stack
60  * data we can save. The amount of storage left will dictate the actual
61  * number of entries of the user stack data saved. If it is too big
62  * It will be truncated by the call to stm32_bbsram_savepanic
63  */
64 #define BBSRAM_HEADER_SIZE 20 /* This is an assumption */
65 #define BBSRAM_USED ((5*BBSRAM_HEADER_SIZE)+(BBSRAM_SIZE_FN0+BBSRAM_SIZE_FN1+BBSRAM_SIZE_FN2+BBSRAM_SIZE_FN3))
66 #define BBSRAM_REAMINING (PX4_BBSRAM_SIZE-BBSRAM_USED)
67 #if CONFIG_ARCH_INTERRUPTSTACK <= 3
68 # define BBSRAM_NUMBER_STACKS 1
69 #else
70 # define BBSRAM_NUMBER_STACKS 2
71 #endif
72 #define BBSRAM_FIXED_ELEMENTS_SIZE (sizeof(info_s))
73 #define BBSRAM_LEFTOVER (BBSRAM_REAMINING-BBSRAM_FIXED_ELEMENTS_SIZE)
74 
75 #define CONFIG_ISTACK_SIZE (BBSRAM_LEFTOVER/BBSRAM_NUMBER_STACKS/sizeof(stack_word_t))
76 #define CONFIG_USTACK_SIZE (BBSRAM_LEFTOVER/BBSRAM_NUMBER_STACKS/sizeof(stack_word_t))
77 
78 /* The path to the Battery Backed up SRAM */
79 #define BBSRAM_PATH "/fs/bbr"
80 /* The sizes of the files to create (-1) use rest of BBSRAM memory */
81 #define BSRAM_FILE_SIZES { \
82  BBSRAM_SIZE_FN0, /* For Time stamp only */ \
83  BBSRAM_SIZE_FN1, /* For Current Flight Parameters Copy A */ \
84  BBSRAM_SIZE_FN2, /* For Current Flight Parameters Copy B */ \
85  BBSRAM_SIZE_FN3, /* For the latest ULog file path */ \
86  BBSRAM_SIZE_FN4, /* For the Panic Log use rest of space */ \
87  0 /* End of table marker */ \
88  }
89 
90 /* For Assert keep this much of the file name*/
91 #define MAX_FILE_PATH_LENGTH 40
92 
93 
94 /* Fixed size strings
95  * To change a format add the number of chars not represented by the format
96  * Specifier to the xxxx_NUM definei.e %Y is YYYY so add 2 and %s is -2
97  * Also xxxxTIME_FMT need to match in size. See CCASERT in hardfault_log.c
98  */
99 #define LOG_PATH_BASE "/fs/microsd/"
100 #define LOG_PATH_BASE_LEN ((arraySize(LOG_PATH_BASE))-1)
101 
102 #define LOG_NAME_FMT "fault_%s.log"
103 #define LOG_NAME_NUM ( -2 )
104 #define LOG_NAME_LEN ((arraySize(LOG_NAME_FMT)-1) + LOG_NAME_NUM)
105 
106 #define TIME_FMT "%Y_%m_%d_%H_%M_%S"
107 #define TIME_FMT_NUM (2+ 0+ 0+ 0+ 0+ 0)
108 #define TIME_FMT_LEN (((arraySize(TIME_FMT)-1) + TIME_FMT_NUM))
109 
110 #define LOG_PATH_LEN ((LOG_PATH_BASE_LEN + LOG_NAME_LEN + TIME_FMT_LEN))
111 
112 #define HEADER_TIME_FMT "%Y-%m-%d-%H:%M:%S"
113 #define HEADER_TIME_FMT_NUM (2+ 0+ 0+ 0+ 0+ 0)
114 #define HEADER_TIME_FMT_LEN (((arraySize(HEADER_TIME_FMT)-1) + HEADER_TIME_FMT_NUM))
115 
116 /* Select which format to use. On a terminal the details are at the bottom
117  * and in a file they are at the top
118  */
119 #define HARDFAULT_DISPLAY_FORMAT 1
120 #define HARDFAULT_FILE_FORMAT 0
121 
122 /****************************************************************************
123  * Public Types
124  ****************************************************************************/
125 
126 /* Used for stack frame storage */
127 typedef uint32_t stack_word_t;
128 
129 /* Stack related data */
130 
131 typedef struct {
132  uint32_t sp;
133  uint32_t top;
134  uint32_t size;
135 
136 } _stack_s;
137 
138 typedef struct {
140 #if CONFIG_ARCH_INTERRUPTSTACK > 3
141  _stack_s interrupt;
142 #endif
143 
144 } stack_t;
145 
146 /* Not Used for reference only */
147 
148 typedef struct {
149  uint32_t r0;
150  uint32_t r1;
151  uint32_t r2;
152  uint32_t r3;
153  uint32_t r4;
154  uint32_t r5;
155  uint32_t r6;
156  uint32_t r7;
157  uint32_t r8;
158  uint32_t r9;
159  uint32_t r10;
160  uint32_t r11;
161  uint32_t r12;
162  uint32_t sp;
163  uint32_t lr;
164  uint32_t pc;
165  uint32_t xpsr;
166  uint32_t d0;
167  uint32_t d1;
168  uint32_t d2;
169  uint32_t d3;
170  uint32_t d4;
171  uint32_t d5;
172  uint32_t d6;
173  uint32_t d7;
174  uint32_t d8;
175  uint32_t d9;
176  uint32_t d10;
177  uint32_t d11;
178  uint32_t d12;
179  uint32_t d13;
180  uint32_t d14;
181  uint32_t d15;
182  uint32_t fpscr;
183  uint32_t sp_main;
184  uint32_t sp_process;
185  uint32_t apsr;
186  uint32_t ipsr;
187  uint32_t epsr;
188  uint32_t primask;
189  uint32_t basepri;
190  uint32_t faultmask;
191  uint32_t control;
192  uint32_t s0;
193  uint32_t s1;
194  uint32_t s2;
195  uint32_t s3;
196  uint32_t s4;
197  uint32_t s5;
198  uint32_t s6;
199  uint32_t s7;
200  uint32_t s8;
201  uint32_t s9;
202  uint32_t s10;
203  uint32_t s11;
204  uint32_t s12;
205  uint32_t s13;
206  uint32_t s14;
207  uint32_t s15;
208  uint32_t s16;
209  uint32_t s17;
210  uint32_t s18;
211  uint32_t s19;
212  uint32_t s20;
213  uint32_t s21;
214  uint32_t s22;
215  uint32_t s23;
216  uint32_t s24;
217  uint32_t s25;
218  uint32_t s26;
219  uint32_t s27;
220  uint32_t s28;
221  uint32_t s29;
222  uint32_t s30;
223  uint32_t s31;
224 } proc_regs_s;
225 
226 
227 /* Flags to identify what is in the dump */
228 typedef enum {
229  eRegsPresent = 0x01,
234 } fault_flags_t;
235 
236 typedef struct {
237  fault_flags_t flags; /* What is in the dump */
238  uintptr_t current_regs; /* Used to validate the dump */
239  int lineno; /* __LINE__ to up_assert */
240  int pid; /* Process ID */
241  uint32_t regs[XCPTCONTEXT_REGS]; /* Interrupt register save
242  * area */
243  stack_t stacks; /* Stack info */
244 #if CONFIG_TASK_NAME_SIZE > 0
245  char name[CONFIG_TASK_NAME_SIZE + 1]; /* Task name (with NULL
246  * terminator) */
247 #endif
248  char filename[MAX_FILE_PATH_LENGTH]; /* the Last of chars in
249  * __FILE__ to up_assert */
250 } info_s;
251 
252 typedef struct {
253  info_s info; /* The info */
254 #if CONFIG_ARCH_INTERRUPTSTACK > 3 /* The amount of stack data is compile time
255  * sized backed on what is left after the
256  * other BBSRAM files are defined
257  * The order is such that only the
258  * ustack should be truncated
259  */
261 #endif
263 } fullcontext_s;
264 
266 /****************************************************************************
267  * Name: hardfault_check_status
268  *
269  * Description:
270  * Check the status of the BBSRAM hard fault file which can be in
271  * one of two states Armed, Valid or Broken.
272  *
273  * Armed - The file in the armed state is not accessible in the fs.
274  * The act of unlinking it is what arms it.
275  *
276  * Valid - The file contains data from a hard fault
277  *
278  * Inputs:
279  * - caller: A label to display in syslog output
280  *
281  * Returned Value:
282  * -ENOENT Armed - The file is in the armed state
283  * OK Valid - The file contains data from a fault that has not
284  * been committed to disk (@see write_hardfault()).
285  * - Any < 0 Broken - Should not happen
286  *
287  ****************************************************************************/
288 int hardfault_check_status(char *caller) weak_function;
289 
290 /****************************************************************************
291  * Name: hardfault_write
292  *
293  * Description:
294  * Will parse and write a human readable output of the data
295  * in the BBSRAM file. Once
296  *
297  *
298  * Inputs:
299  * - caller: A label to display in syslog output
300  * - fd: An FD to write the data to
301  * - format: Select which format to use.
302  *
303  * HARDFAULT_DISPLAY_FORMAT On the console the details are
304  * at the bottom
305  * HARDFAULT_FILE_FORMAT In a file details are at the top
306  * of the log file
307  *
308  * - rearm: If true will move the file to the Armed state, if
309  * false the file is not armed and can be read again
310  *
311  * Returned Value:
312  *
313  * OK or errno
314  *
315  *
316  ****************************************************************************/
317 int hardfault_write(char *caller, int fd, int format, bool rearm) weak_function;
318 
319 /****************************************************************************
320  * Name: hardfault_rearm
321  *
322  * Description:
323  * Will move the file to the Armed state
324  *
325  *
326  * Inputs:
327  * - caller: A label to display in syslog output
328  *
329  * Returned Value:
330  *
331  * OK or errno
332  *
333  *
334  ****************************************************************************/
335 int hardfault_rearm(char *caller) weak_function;
336 
337 /****************************************************************************
338  * Name: hardfault_increment_reboot
339  *
340  * Description:
341  * Will increment the reboot counter. The reboot counter will help
342  * detect reboot loops.
343  *
344  *
345  * Inputs:
346  * - caller: A label to display in syslog output
347  * - reset : when set will reset the reboot counter to 0.
348  *
349  * Returned Value:
350  *
351  * The current value of the reboot counter (after increment/reset) or errno < 0.
352  *
353  *
354  ****************************************************************************/
355 int hardfault_increment_reboot(char *caller, bool reset) weak_function;
356 
uint32_t s9
uint32_t s2
uint32_t s21
uint32_t s22
uint32_t s20
#define __END_DECLS
Definition: visibility.h:59
uint32_t s31
uint32_t r6
uint32_t s7
uint32_t xpsr
int hardfault_rearm(char *caller) weak_function
uint32_t s13
uint32_t d8
uint32_t s14
uint32_t s27
uint32_t d13
uint32_t d9
uint32_t s15
uint32_t d4
uint32_t d11
uint32_t r9
uint32_t s10
A set of useful macros for enhanced runtime and compile time error detection and warning suppression...
uint32_t epsr
int reset(enum LPS22HB_BUS busid)
Reset the driver.
uint32_t lr
uint32_t r3
fault_flags_t flags
uint32_t s3
uint32_t top
#define CONFIG_ISTACK_SIZE
Definition: hardfault_log.h:75
int hardfault_write(char *caller, int fd, int format, bool rearm) weak_function
uint32_t s30
uint32_t s28
__BEGIN_DECLS int hardfault_check_status(char *caller) weak_function
uint32_t d10
uint32_t s11
uint32_t s4
uint32_t faultmask
uint32_t s24
uint32_t s17
uint32_t s26
uint32_t r10
uintptr_t current_regs
uint32_t s8
uint32_t s18
uint32_t fpscr
uint32_t apsr
#define __BEGIN_DECLS
Definition: visibility.h:58
fault_flags_t
#define MAX_FILE_PATH_LENGTH
Definition: hardfault_log.h:91
uint32_t s0
uint32_t sp_process
int lineno
stack_t stacks
uint32_t d6
uint32_t d14
uint32_t s25
uint32_t s23
uint32_t ipsr
uint32_t sp
int fd
Definition: dataman.cpp:146
uint32_t r4
uint32_t r1
const char * name
Definition: tests_main.c:58
uint32_t r12
uint32_t sp
uint32_t d3
uint32_t r2
uint32_t s1
uint32_t s19
uint32_t r0
uint32_t d5
uint32_t sp_main
uint32_t d1
uint32_t stack_word_t
uint32_t size
uint32_t primask
uint32_t s16
uint32_t d7
uint32_t pc
uint32_t control
uint32_t r5
uint32_t r8
uint32_t d15
uint32_t d2
uint32_t r11
_stack_s user
uint32_t d12
uint32_t s5
uint32_t s6
uint32_t basepri
uint32_t d0
uint32_t s12
int hardfault_increment_reboot(char *caller, bool reset) weak_function
#define CONFIG_USTACK_SIZE
Definition: hardfault_log.h:76
uint32_t r7
uint32_t s29