STM32 HardFault_Handler调试总结

最近自己写了个简单的内存管理,想到能更好的管理内存,不巧在使用时进入到了HardFault_Handler了。根据之前的经验是使用jlink或者stlink进行硬件仿真,看看是在哪一行出了问题。一般情况下是无效地址使用,或着堆栈溢出问题。不过这些在调试时都要查手册中的寄存器描述,还是有些头疼的。我百度了下,广大网友还是有不少办法的。

博文1:https://blog.csdn.net/electrocrazy/article/details/78173558
博文2:https://www.cnblogs.com/dylancao/p/11142205.html
更有个百度文章(https://wenku.baidu.com/view/a4a7499afad6195f312ba6d2.html),我觉得这个写的有问题,它说在HardFault_Handler时直接使用bx LR,就可以在使用仿真器调试时,跳转到出问题的地方。但是当跳转到HardFault_Handler时,LR寄存器已经被复写了。

上面博文1和博文2,描述的都非常不错。其实原理大家都和明白,当出现异常时,M4内核会自动的把xPSR,PC,LR,R12,R3,R2,R1,R0这8个寄存器压入栈中。在HardFault_Handler异常开始的地方获取的堆栈指针,进而就可以拿到发生异常时的相关寄存器,其中最重要的就是LR和PC指针了。现在有下面2个问题

  • 1.如果区分数据压入的是MSP还是PSP.?
    由于在进入的异常函数时,LR寄存器会被内核写入特定值,用来表示之前数据压入到那个栈了。如下面描述的当LR寄存器的值为0xFFFFFFF1时,这个一般都是在handler,放生在中断嵌套场景。当为0xFFFF FFF9时说明主程序是在线程模式下运行,并且使用了MSP.当为0xFFFF FFFD时说明是在使用PSP时线程模式下运行,然后进入到异常中的。
    STM32 HardFault_Handler调试总结_第1张图片
  • 如何定位问题
    根据堆栈中解析出来的PC指针,就可以使用addr2line命令解析出来是在哪一行出问题了。如果你移植cmBackTrace皆可以打印出backTrace信息,可以直观的看到PC指针,而且还可以知道是什么问题导致进入到HardFault_Handler,非常好用省去了自己查阅手册的麻烦。
  • 问题分析
    移植fatfs时,运行软件进入到HardFault_Handler。一开始使用stlink硬件仿真可以查找到是在哪里出问题了。但是出来什么异常,为此移植了cmBackTrace,才发现是内存对齐问题。使用了我自己的内存管理出问题了,如果使用系统默认的malloc()方法是没有问题。
    我定义的内存头块如下所示:可以看到头部是2字节对齐,而stm32一般都是4字节对齐,所以把他修改成4字节对齐就可以了。
typedef struct
{
    u16 len      ;     //max block size is 32k
    u16 pre_len  ;     //max block size is 32k
    u16 is_used  ;      //if memory block was used set to 1.
} __attribute__((aligned (2))) mem_hdr_t;

下面是我格式化的log,看起来有点像Android的log。就这样吧。

//由于打开了dumpstack功能,所以打印了堆栈中的数据,
25Q128FV  [I] W25QXX_Init:32 w25q128 probe success!, id:0xef13
main       [I] main:64 start:0x7d0c,end:0x7d0c
main       [I] main:67 hello world
diskio:    [I] disk_initialize:39 sd init
diskio:    [I] disk_initialize:50 res:3
main       [E] main:71 sd filesystem mount failed!error:FR_NOT_READY
diskio:    [I] disk_initialize:43 flash init
W25Q128FV  [I] W25QXX_Init:32 w25q128 probe success!, id:0xef13
diskio:    [I] disk_initialize:50 res:0
diskio:    [I] disk_read:94 flash read:buf:0x20009586,sector:0,count:1
BaceTrace  [D] cm_backtrace_fault:643 
BaceTrace  [D] cm_backtrace_fault:644 main_stack_start:0x2001f9ff,stack_size:1536
BaceTrace  [D] cm_backtrace_firmware_info:250 Firmware name: CmBacktrace, hardware version: V1.0.0, software version: V0.1.0
BaceTrace  [D] cm_backtrace_fault:663 Fault on interrupt or bare metal(no OS) environment
BaceTrace  [D] dump_stack:327 ===== Thread stack information =====
BaceTrace  [D] dump_stack:329   addr: 0x2001fd58    data: 0x08003789
BaceTrace  [D] dump_stack:329   addr: 0x2001fd5c    data: 0x08007aa4
BaceTrace  [D] dump_stack:329   addr: 0x2001fd60    data: 0x00000045
BaceTrace  [D] dump_stack:329   addr: 0x2001fd64    data: 0x00000000
BaceTrace  [D] dump_stack:329   addr: 0x2001fd68    data: 0x2000159c
BaceTrace  [D] dump_stack:329   addr: 0x2001fd6c    data: 0x00000001
BaceTrace  [D] dump_stack:329   addr: 0x2001fd70    data: 0x08007bdc
BaceTrace  [D] dump_stack:329   addr: 0x2001fd74    data: 0x08007a5c
BaceTrace  [D] dump_stack:329   addr: 0x2001fd78    data: 0x08007ac8
BaceTrace  [D] dump_stack:329   addr: 0x2001fd7c    data: 0x2000159c
BaceTrace  [D] dump_stack:329   addr: 0x2001fd80    data: 0x08004609
BaceTrace  [D] dump_stack:329   addr: 0x2001fd84    data: 0x08007aca
BaceTrace  [D] dump_stack:329   addr: 0x2001fd88    data: 0x20009556
BaceTrace  [D] dump_stack:329   addr: 0x2001fd8c    data: 0x20000004
BaceTrace  [D] dump_stack:329   addr: 0x2001fd90    data: 0x08007aca
BaceTrace  [D] dump_stack:329   addr: 0x2001fd94    data: 0x20000004
BaceTrace  [D] dump_stack:329   addr: 0x2001fd98    data: 0x080055c7
BaceTrace  [D] dump_stack:329   addr: 0x2001fd9c    data: 0x08007aa4
BaceTrace  [D] dump_stack:329   addr: 0x2001fda0    data: 0x08007928
BaceTrace  [D] dump_stack:329   addr: 0x2001fda4    data: 0x00007d0c
BaceTrace  [D] dump_stack:329   addr: 0x2001fda8    data: 0x0aef6dc5
BaceTrace  [D] dump_stack:329   addr: 0x2001fdac    data: 0x00000000
BaceTrace  [D] dump_stack:329   addr: 0x2001fdb0    data: 0x00000000
BaceTrace  [D] dump_stack:329   addr: 0x2001fdb4    data: 0x00000000
BaceTrace  [D] dump_stack:329   addr: 0x2001fdb8    data: 0x00130001
BaceTrace  [D] dump_stack:329   addr: 0x2001fdbc    data: 0x00000007
BaceTrace  [D] dump_stack:329   addr: 0x2001fdc0    data: 0x00000007
BaceTrace  [D] dump_stack:329   addr: 0x2001fdc4    data: 0x00000002
BaceTrace  [D] dump_stack:329   addr: 0x2001fdc8    data: 0x00000002
BaceTrace  [D] dump_stack:329   addr: 0x2001fdcc    data: 0x00000028
BaceTrace  [D] dump_stack:329   addr: 0x2001fdd0    data: 0x00000008
BaceTrace  [D] dump_stack:329   addr: 0x2001fdd4    data: 0x20009590
BaceTrace  [D] dump_stack:329   addr: 0x2001fdd8    data: 0x00000000
BaceTrace  [D] dump_stack:329   addr: 0x2001fddc    data: 0x776d7261
BaceTrace  [D] dump_stack:329   addr: 0x2001fde0    data: 0x46646e69
BaceTrace  [D] dump_stack:329   addr: 0x2001fde4    data: 0x5f345a0e
BaceTrace  [D] dump_stack:329   addr: 0x2001fde8    data: 0x630461d7
BaceTrace  [D] dump_stack:329   addr: 0x2001fdec    data: 0x317cc2a7
BaceTrace  [D] dump_stack:329   addr: 0x2001fdf0    data: 0xdfcceb98
BaceTrace  [D] dump_stack:329   addr: 0x2001fdf4    data: 0x5803c7e8
BaceTrace  [D] dump_stack:329   addr: 0x2001fdf8    data: 0xfc685714
BaceTrace  [D] dump_stack:329   addr: 0x2001fdfc    data: 0x701415bf
BaceTrace  [D] dump_stack:329   addr: 0x2001fe00    data: 0x7f542a7c
BaceTrace  [D] dump_stack:329   addr: 0x2001fe04    data: 0x6cca30d7
BaceTrace  [D] dump_stack:329   addr: 0x2001fe08    data: 0x06303e29
BaceTrace  [D] dump_stack:329   addr: 0x2001fe0c    data: 0x95b14e1c
BaceTrace  [D] dump_stack:329   addr: 0x2001fe10    data: 0xd6af671b
BaceTrace  [D] dump_stack:329   addr: 0x2001fe14    data: 0xd75c1fca
BaceTrace  [D] dump_stack:329   addr: 0x2001fe18    data: 0xbe026da9
BaceTrace  [D] dump_stack:329   addr: 0x2001fe1c    data: 0xf1ffc211
BaceTrace  [D] dump_stack:329   addr: 0x2001fe20    data: 0xd453d100
BaceTrace  [D] dump_stack:329   addr: 0x2001fe24    data: 0x375579e6
BaceTrace  [D] dump_stack:329   addr: 0x2001fe28    data: 0x811eefbf
BaceTrace  [D] dump_stack:329   addr: 0x2001fe2c    data: 0xc1aa5bb8
BaceTrace  [D] dump_stack:329   addr: 0x2001fe30    data: 0x354c6503
BaceTrace  [D] dump_stack:329   addr: 0x2001fe34    data: 0x8a2bd76b
BaceTrace  [D] dump_stack:329   addr: 0x2001fe38    data: 0x6a39d39b
BaceTrace  [D] dump_stack:329   addr: 0x2001fe3c    data: 0x83dc240d
BaceTrace  [D] dump_stack:329   addr: 0x2001fe40    data: 0x17d5283d
BaceTrace  [D] dump_stack:329   addr: 0x2001fe44    data: 0x4a032bf8
BaceTrace  [D] dump_stack:329   addr: 0x2001fe48    data: 0x40348045
BaceTrace  [D] dump_stack:329   addr: 0x2001fe4c    data: 0x9c4d362d
BaceTrace  [D] dump_stack:329   addr: 0x2001fe50    data: 0xe2f8b712
BaceTrace  [D] dump_stack:329   addr: 0x2001fe54    data: 0x247917ff
BaceTrace  [D] dump_stack:329   addr: 0x2001fe58    data: 0x35211349
BaceTrace  [D] dump_stack:329   addr: 0x2001fe5c    data: 0x983cd95e
BaceTrace  [D] dump_stack:329   addr: 0x2001fe60    data: 0xc83ddb0c
BaceTrace  [D] dump_stack:329   addr: 0x2001fe64    data: 0x7d64b8fa
BaceTrace  [D] dump_stack:329   addr: 0x2001fe68    data: 0xf59ebe7a
BaceTrace  [D] dump_stack:329   addr: 0x2001fe6c    data: 0x77c2407c
BaceTrace  [D] dump_stack:329   addr: 0x2001fe70    data: 0xff689e5b
BaceTrace  [D] dump_stack:329   addr: 0x2001fe74    data: 0x38022334
BaceTrace  [D] dump_stack:329   addr: 0x2001fe78    data: 0xd163d154
BaceTrace  [D] dump_stack:329   addr: 0x2001fe7c    data: 0x792996d4
BaceTrace  [D] dump_stack:329   addr: 0x2001fe80    data: 0xc3c31040
BaceTrace  [D] dump_stack:329   addr: 0x2001fe84    data: 0x5c1e6b09
BaceTrace  [D] dump_stack:329   addr: 0x2001fe88    data: 0xaf91442b
BaceTrace  [D] dump_stack:329   addr: 0x2001fe8c    data: 0xd7e3bf18
BaceTrace  [D] dump_stack:329   addr: 0x2001fe90    data: 0x5e78efa1
BaceTrace  [D] dump_stack:329   addr: 0x2001fe94    data: 0x6a4516a4
BaceTrace  [D] dump_stack:329   addr: 0x2001fe98    data: 0x97c5b71d
BaceTrace  [D] dump_stack:329   addr: 0x2001fe9c    data: 0x52cb236a
BaceTrace  [D] dump_stack:329   addr: 0x2001fea0    data: 0x37332b58
BaceTrace  [D] dump_stack:329   addr: 0x2001fea4    data: 0x76695742
BaceTrace  [D] dump_stack:329   addr: 0x2001fea8    data: 0xc1fb7cb8
BaceTrace  [D] dump_stack:329   addr: 0x2001feac    data: 0xd4a94989
BaceTrace  [D] dump_stack:329   addr: 0x2001feb0    data: 0xf92d448f
BaceTrace  [D] dump_stack:329   addr: 0x2001feb4    data: 0xa3b1879d
BaceTrace  [D] dump_stack:329   addr: 0x2001feb8    data: 0x7c0f3d25
BaceTrace  [D] dump_stack:329   addr: 0x2001febc    data: 0x60792b41
BaceTrace  [D] dump_stack:329   addr: 0x2001fec0    data: 0x17e8643b
BaceTrace  [D] dump_stack:329   addr: 0x2001fec4    data: 0x68bec0a3
BaceTrace  [D] dump_stack:329   addr: 0x2001fec8    data: 0x46812ca7
BaceTrace  [D] dump_stack:329   addr: 0x2001fecc    data: 0xb75d4502
BaceTrace  [D] dump_stack:329   addr: 0x2001fed0    data: 0x1f0d5b15
BaceTrace  [D] dump_stack:329   addr: 0x2001fed4    data: 0x6fcaf859
BaceTrace  [D] dump_stack:329   addr: 0x2001fed8    data: 0xc5bc62bf
BaceTrace  [D] dump_stack:329   addr: 0x2001fedc    data: 0x17fb3b07
BaceTrace  [D] dump_stack:329   addr: 0x2001fee0    data: 0x6fbb6ade
BaceTrace  [D] dump_stack:329   addr: 0x2001fee4    data: 0x24707e45
BaceTrace  [D] dump_stack:329   addr: 0x2001fee8    data: 0x595bec4f
BaceTrace  [D] dump_stack:329   addr: 0x2001feec    data: 0x04238885
BaceTrace  [D] dump_stack:329   addr: 0x2001fef0    data: 0x62f56fe8
BaceTrace  [D] dump_stack:329   addr: 0x2001fef4    data: 0x0165e580
BaceTrace  [D] dump_stack:329   addr: 0x2001fef8    data: 0x12670561
BaceTrace  [D] dump_stack:329   addr: 0x2001fefc    data: 0xafebba53
BaceTrace  [D] dump_stack:329   addr: 0x2001ff00    data: 0x1595d798
BaceTrace  [D] dump_stack:329   addr: 0x2001ff04    data: 0xa9f71a38
BaceTrace  [D] dump_stack:329   addr: 0x2001ff08    data: 0xedbdc401
BaceTrace  [D] dump_stack:329   addr: 0x2001ff0c    data: 0x9a5a5bf6
BaceTrace  [D] dump_stack:329   addr: 0x2001ff10    data: 0x42c91ca5
BaceTrace  [D] dump_stack:329   addr: 0x2001ff14    data: 0xe63c402a
BaceTrace  [D] dump_stack:329   addr: 0x2001ff18    data: 0x59c17a22
BaceTrace  [D] dump_stack:329   addr: 0x2001ff1c    data: 0x177c4aac
BaceTrace  [D] dump_stack:329   addr: 0x2001ff20    data: 0x60bcb03f
BaceTrace  [D] dump_stack:329   addr: 0x2001ff24    data: 0x0e8c7354
BaceTrace  [D] dump_stack:329   addr: 0x2001ff28    data: 0xcae32a02
BaceTrace  [D] dump_stack:329   addr: 0x2001ff2c    data: 0xceee9030
BaceTrace  [D] dump_stack:329   addr: 0x2001ff30    data: 0xfc6f3b6d
BaceTrace  [D] dump_stack:329   addr: 0x2001ff34    data: 0x1699a95f
BaceTrace  [D] dump_stack:329   addr: 0x2001ff38    data: 0x43965554
BaceTrace  [D] dump_stack:329   addr: 0x2001ff3c    data: 0xa0c720dd
BaceTrace  [D] dump_stack:329   addr: 0x2001ff40    data: 0xc74a17a4
BaceTrace  [D] dump_stack:329   addr: 0x2001ff44    data: 0x8d0220ce
BaceTrace  [D] dump_stack:329   addr: 0x2001ff48    data: 0x2f737429
BaceTrace  [D] dump_stack:329   addr: 0x2001ff4c    data: 0x8f511282
BaceTrace  [D] dump_stack:329   addr: 0x2001ff50    data: 0x7b72ba44
BaceTrace  [D] dump_stack:329   addr: 0x2001ff54    data: 0xe1eb90ab
BaceTrace  [D] dump_stack:329   addr: 0x2001ff58    data: 0x5ea7d1d6
BaceTrace  [D] dump_stack:329   addr: 0x2001ff5c    data: 0x01e3a5e4
BaceTrace  [D] dump_stack:329   addr: 0x2001ff60    data: 0x4dcef4de
BaceTrace  [D] dump_stack:329   addr: 0x2001ff64    data: 0x60b8ba82
BaceTrace  [D] dump_stack:329   addr: 0x2001ff68    data: 0xe4272419
BaceTrace  [D] dump_stack:329   addr: 0x2001ff6c    data: 0xb3c4907f
BaceTrace  [D] dump_stack:329   addr: 0x2001ff70    data: 0xa7327b38
BaceTrace  [D] dump_stack:329   addr: 0x2001ff74    data: 0xb75a380c
BaceTrace  [D] dump_stack:329   addr: 0x2001ff78    data: 0xf5817311
BaceTrace  [D] dump_stack:329   addr: 0x2001ff7c    data: 0x1198e717
BaceTrace  [D] dump_stack:329   addr: 0x2001ff80    data: 0xf1d20367
BaceTrace  [D] dump_stack:329   addr: 0x2001ff84    data: 0xbd4a3965
BaceTrace  [D] dump_stack:329   addr: 0x2001ff88    data: 0x6d38ffe6
BaceTrace  [D] dump_stack:329   addr: 0x2001ff8c    data: 0x9bbfd7ce
BaceTrace  [D] dump_stack:329   addr: 0x2001ff90    data: 0xd3622681
BaceTrace  [D] dump_stack:329   addr: 0x2001ff94    data: 0xa96e7637
BaceTrace  [D] dump_stack:329   addr: 0x2001ff98    data: 0xee82836d
BaceTrace  [D] dump_stack:329   addr: 0x2001ff9c    data: 0x709bfa70
BaceTrace  [D] dump_stack:329   addr: 0x2001ffa0    data: 0x879b7e68
BaceTrace  [D] dump_stack:329   addr: 0x2001ffa4    data: 0xff57a32c
BaceTrace  [D] dump_stack:329   addr: 0x2001ffa8    data: 0xed46b307
BaceTrace  [D] dump_stack:329   addr: 0x2001ffac    data: 0x504d2efd
BaceTrace  [D] dump_stack:329   addr: 0x2001ffb0    data: 0x5682630d
BaceTrace  [D] dump_stack:329   addr: 0x2001ffb4    data: 0xb709c156
BaceTrace  [D] dump_stack:329   addr: 0x2001ffb8    data: 0x5880fd8a
BaceTrace  [D] dump_stack:329   addr: 0x2001ffbc    data: 0x05d2a9bf
BaceTrace  [D] dump_stack:329   addr: 0x2001ffc0    data: 0xa3752921
BaceTrace  [D] dump_stack:329   addr: 0x2001ffc4    data: 0x2b4ec587
BaceTrace  [D] dump_stack:329   addr: 0x2001ffc8    data: 0x4a45e7ab
BaceTrace  [D] dump_stack:329   addr: 0x2001ffcc    data: 0x494ba114
BaceTrace  [D] dump_stack:329   addr: 0x2001ffd0    data: 0xce79dd36
BaceTrace  [D] dump_stack:329   addr: 0x2001ffd4    data: 0x02846ca2
BaceTrace  [D] dump_stack:329   addr: 0x2001ffd8    data: 0x9bcc14cb
BaceTrace  [D] dump_stack:329   addr: 0x2001ffdc    data: 0x00000000
BaceTrace  [D] dump_stack:329   addr: 0x2001ffe0    data: 0x00000000
BaceTrace  [D] dump_stack:329   addr: 0x2001ffe4    data: 0x00000000
BaceTrace  [D] dump_stack:329   addr: 0x2001ffe8    data: 0x00000000
BaceTrace  [D] dump_stack:329   addr: 0x2001ffec    data: 0x00000000
BaceTrace  [D] dump_stack:329   addr: 0x2001fff0    data: 0x00000000
BaceTrace  [D] dump_stack:329   addr: 0x2001fff4    data: 0x00000000
BaceTrace  [D] dump_stack:329   addr: 0x2001fff8    data: 0x0800595f
BaceTrace  [D] dump_stack:329   addr: 0x2001fffc    data: 0x661106a5
BaceTrace  [D] dump_stack:331 ====================================
BaceTrace  [D] cm_backtrace_fault:685 =================== Registers information ====================
BaceTrace  [D] cm_backtrace_fault:696   R0 : 0x00000001  R1 : 0x000000fb  R2 : 0x00000007  R3 : 0x00000007
BaceTrace  [D] cm_backtrace_fault:700   R12: 0x000000fd  LR : 0x00000001  PC : 0x08003852  PSR: 0x81000200
BaceTrace  [D] cm_backtrace_fault:704 ==============================================================
BaceTrace  [D] fault_diagnosis:582 Usage fault is caused by indicates that an unaligned access fault has taken place
BaceTrace  [D] print_call_stack:445 Show more call stack info by run: addr2line -e CmBacktrace.elf -a -f .*s

根据PC : 0x08003852可以使用addr2line.exe进行解析,解析出来后可以看到是在ff.c的2295行。

E:\learn\Learning\my-Stm32\software\EXP23-FATFS (master)
λ .\tools\addr2line.exe -f -e .\Project\Debug\Project.elf 0x08003852
find_volume
E:/learn/Learning/my-Stm32/software/EXP23-FATFS/Project/Debug/../ExtLib/FatFs/src/ff.c:2295

你可能感兴趣的:(单片机物联网)