Skip to content

Commit

Permalink
【增加】主栈检查功能。
Browse files Browse the repository at this point in the history
Signed-off-by: armink <[email protected]>
  • Loading branch information
armink committed Jul 16, 2019
1 parent 8bbd425 commit 9b1158b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016-2018 Armink ([email protected])
Copyright (c) 2016-2019 Armink ([email protected])

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CmBacktrace: ARM Cortex-M 系列 MCU 错误追踪库

[![GitHub release](https://img.shields.io/github/release/armink/CmBacktrace.svg)](https://github.com/armink/CmBacktrace/releases/latest) [![GitHub commits](https://img.shields.io/github/commits-since/armink/CmBacktrace/1.2.0.svg)](https://github.com/armink/CmBacktrace/compare/1.0.0...master) [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/armink/CmBacktrace/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/armink/CmBacktrace.svg)](https://github.com/armink/CmBacktrace/releases/latest) [![GitHub commits](https://img.shields.io/github/commits-since/armink/CmBacktrace/1.3.0.svg)](https://github.com/armink/CmBacktrace/compare/1.0.0...master) [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/armink/CmBacktrace/master/LICENSE)

## 0、CmBacktrace 是什么

Expand Down Expand Up @@ -176,6 +176,44 @@ void cm_backtrace_fault(uint32_t fault_handler_lr, uint32_t fault_handler_sp)
- 1、注释/删除其他文件中定义的 `HardFault_Handler` 函数,仅保留 cmb_fault.s 中的;
- 2、将 cmb_fault.s 移除工程,手动添加 `cm_backtrace_fault` 函数至现有的故障处理函数,但需要注意的是,务必 **保证该函数数入参的准备性** ,否则可能会导致故障诊断功能及堆栈打印功能无法正常运行。所以如果是新手,不推荐第二种解决方法。

#### 2.5.4 初始化时提示无法获取主栈(main stack)信息

`cmd_def.h` 中有定义默认的主栈配置,大致如下:

```c

#if defined(__CC_ARM)
/* C stack block name, default is STACK */
#ifndef CMB_CSTACK_BLOCK_NAME
#define CMB_CSTACK_BLOCK_NAME STACK
#endif
...
#elif defined(__ICCARM__)
/* C stack block name, default is 'CSTACK' */
#ifndef CMB_CSTACK_BLOCK_NAME
#define CMB_CSTACK_BLOCK_NAME "CSTACK"
#endif
...
#elif defined(__GNUC__)
/* C stack block start address, defined on linker script file, default is _sstack */
#ifndef CMB_CSTACK_BLOCK_START
#define CMB_CSTACK_BLOCK_START _sstack
#endif
/* C stack block end address, defined on linker script file, default is _estack */
#ifndef CMB_CSTACK_BLOCK_END
#define CMB_CSTACK_BLOCK_END _estack
#endif
...
#else
```

比如在 Keil-MDK 编译器下会默认选择 `STACK` 作为主栈 block 的名称,但在一些特殊平台下,项目的主栈 block 名称可能不叫 `STACK`,导致 CmBacktrace 无法获取到正确的主栈信息,所以在初始化时会有如上的错误提示信息。

解决这个问题一般有两个思路

- 1、在 `cmb_cfg.h` 中重新定义主栈的信息,此时 CmBacktrace 会优先使用 `cmb_cfg.h` 中的配置信息;
- 2、修改项目配置,如果是 Keil-MDK ,则在启动文件的开头位置,将主栈的名称修改为默认的 `STACK` ,其他编译器一般很少出现该问题。

### 2.6 许可

采用 MIT 开源协议,细节请阅读项目中的 LICENSE 文件内容。
10 changes: 9 additions & 1 deletion cm_backtrace/cm_backtrace.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of the CmBacktrace Library.
*
* Copyright (c) 2016-2017, Armink, <[email protected]>
* Copyright (c) 2016-2019, Armink, <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -62,6 +62,7 @@
#endif

enum {
PRINT_MAIN_STACK_CFG_ERROR,
PRINT_FIRMWARE_INFO,
PRINT_ASSERT_ON_THREAD,
PRINT_ASSERT_ON_HANDLER,
Expand Down Expand Up @@ -103,6 +104,7 @@ enum {

static const char * const print_info[] = {
#if (CMB_PRINT_LANGUAGE == CMB_PRINT_LANGUAGE_ENGLISH)
[PRINT_MAIN_STACK_CFG_ERROR] = "ERROR: Unable to get the main stack information, please check the configuration of the main stack",
[PRINT_FIRMWARE_INFO] = "Firmware name: %s, hardware version: %s, software version: %s",
[PRINT_ASSERT_ON_THREAD] = "Assert on thread %s",
[PRINT_ASSERT_ON_HANDLER] = "Assert on interrupt or bare metal(no OS) environment",
Expand Down Expand Up @@ -141,6 +143,7 @@ static const char * const print_info[] = {
[PRINT_MMAR] = "The memory management fault occurred address is %08x",
[PRINT_BFAR] = "The bus fault occurred address is %08x",
#elif (CMB_PRINT_LANGUAGE == CMB_PRINT_LANGUAGE_CHINESE)
[PRINT_MAIN_STACK_CFG_ERROR] = "错误:无法获取主栈信息,请检查主栈的相关配置",
[PRINT_FIRMWARE_INFO] = "固件名称:%s,硬件版本号:%s,软件版本号:%s",
[PRINT_ASSERT_ON_THREAD] = "在线程(%s)中发生断言",
[PRINT_ASSERT_ON_HANDLER] = "在中断或裸机环境下发生断言",
Expand Down Expand Up @@ -229,6 +232,11 @@ void cm_backtrace_init(const char *firmware_name, const char *hardware_ver, cons
#error "not supported compiler"
#endif

if (main_stack_size == 0) {
cmb_println(print_info[PRINT_MAIN_STACK_CFG_ERROR]);
return;
}

init_ok = true;
}

Expand Down
4 changes: 2 additions & 2 deletions cm_backtrace/cmb_def.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of the CmBacktrace Library.
*
* Copyright (c) 2016-2018, Armink, <[email protected]>
* Copyright (c) 2016-2019, Armink, <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -34,7 +34,7 @@
#include <stdlib.h>

/* library software version number */
#define CMB_SW_VERSION "1.2.1"
#define CMB_SW_VERSION "1.3.0"

#define CMB_CPU_ARM_CORTEX_M0 0
#define CMB_CPU_ARM_CORTEX_M3 1
Expand Down

0 comments on commit 9b1158b

Please sign in to comment.