用gdb调试Cortex-M系列芯片

准备工作

本文假设读者

  1. 已经熟悉arm gcc工具链
  2. 已经在win中安装好mingw或者arm-none-eabi-gcc工具
  3. 具有合适的代码工程和编译脚本,且编译输出elf文件时,已添加-g选项来生成调试信息
  4. 安装jlink调试工具和对应驱动
  5. 有对应的硬件电路

如果其中任何一项不满足,请自行google相关文档,完成基础准备工作

  1. 开始 -> SEGGER -> Jlink V4.90e -> Jlink GDB Server
  2. Jlink会显示GDB config配置选项,如下图配置
    GDB Server配置选项
  3. 点击OK,启动GDB server,在J-Link一栏会显示Connected字符,表示成功连接硬件,如下图所示
    GDB Server配置完成

启动GDB

  1. 打开CMD,输入arm-none-eabi-gdb,打开后,如下图所示;如果没有显示对应信息,属于没有配置环境变量的path,请添加gcc
    tools的路径进去
  2. 输入file Demo.elf加载调试文件,这里以Demo.elf为例,实际你可以替换为自己的elf文件
  3. 输入target remote localhost:2331,连接gdb server,连接成功后,会在Jlink GDB server中显示对应的状态,如下所示
    GDB Server连接成功
  4. 输入monitor reset来复位MCU,从而让MCU处于确定的状态
  5. 输入load往MCU中加载调试文件,也就是常见的烧录过程
  6. 输入break main设置main断点,让MCU执行到main中停止

下面是使用Power shell的交互过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
PS C:\> arm-none-eabi-gdb
GNU gdb (GNU Tools for ARM Embedded Processors) 7.8.0.20150604-cvs
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=i686-w64-mingw32 --target=arm-none-eabi".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) file Demo.elf
Reading symbols from Demo.elf...done.
(gdb) target remote localhost:2331
Remote debugging using localhost:2331
0x00000000 in ?? ()
(gdb) monitor reset
Resetting target
(gdb) load
Loading section .text, size 0x434 lma 0x8000000
Loading section .data, size 0x14 lma 0x8000434
Start address 0x80003ec, load size 1096
Transfer rate: 59 KB/sec, 548 bytes/write.
(gdb) break main
Breakpoint 1 at 0x8000112: file src\App\main.c, line 27.
(gdb) c
Continuing.

Breakpoint 1, main () at src\App\main.c:27
27 int i = 0;
(gdb) l
22 /* Includes ------------------------------------------------------------------*/
23 #include "stm32f10x.h"
24 #include <stdio.h>
25 int main(void)
26 {
27 int i = 0;
28
29
30 for(int m = 0; m < 100; m++)
31 {
(gdb) l
32 i = m + i;
33 }
34
35 return 0;
36 }
(gdb) s
30 for(int m = 0; m < 100; m++)
(gdb) s
32 i = m + i;
(gdb) s
30 for(int m = 0; m < 100; m++)
(gdb) s
32 i = m + i;
(gdb) p m
$1 = 1
(gdb) break 35
Breakpoint 2 at 0x8000130: file src\App\main.c, line 35.
(gdb) c
Continuing.

Breakpoint 2, main () at src\App\main.c:35
35 return 0;
(gdb) p i
$2 = 4950
(gdb) q
A debugging session is active.

Inferior 1 [Remote target] will be killed.

Quit anyway? (y or n) y

参考资料:

  1. gcc manual
  2. gdb manual
  3. jlink user manual, charapter 3.3 J-link GDB Server
  4. scons user guide
  5. scons manual