编译,使用的编译器:arm-2009q3
编译gdb
tar -zxvf gdb-7.11.1.tar.gz
cd gdb-7.11.1/
./configure --target=arm-none-linux-gnueabi --prefix=/work/system/gdb_install/
make
make install
或者
make install prefix=/work/system/gdb_install/把arm-none-linux-gnueabi-gdb复制到/bin目录
编译gdbserver
cd gdb/gdbserver/
./configure --host=arm-none-linux-gnueabi
make CFLAGS="-static"
复制gdbserver到开发板上
编译要调试的应用,编译时加上-g选项
arm-none-linux-gnueabi-gcc -g -o test_debug test_debug.c
#include <stdio.h>
void C(int *p)
{
*p = 0x12;
}
void B(int *p)
{
C(p);
}
void A(int *p)
{
B(p);
}
void A2(int *p)
{
C(p);
}
int main(int argc, char **argv)
{
int a;
int *p = NULL;
A2(&a); // A2 > C
printf("a = 0x%x\n", a);
A(p); // A > B > C
return 0;
}
在开发板上:
gdbserver 10.168.10.121:2345 ./test_debug
在PC上
/bin/arm-none-linux-gnueabi-gdb
输入:target remote 10.168.10.121:2345
然后: 使用gdb命令来控制程序
出现这个Remote 'g' packet reply is too long:
要修改gdb/remote.c文件中的static void
process_g_packet (struct regcache *regcache)函数:
process_g_packet (struct regcache *regcache)
{
struct gdbarch *gdbarch = get_regcache_arch (regcache);
struct remote_state *rs = get_remote_state ();
struct remote_arch_state *rsa = get_remote_arch_state ();
int i, buf_len;
char *p;
char *regs;
buf_len = strlen (rs->buf);
/* Further sanity checks, with knowledge of the architecture. */
//if (buf_len > 2 * rsa->sizeof_g_packet)
//error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
if (buf_len > 2 * rsa->sizeof_g_packet)
{
rsa->sizeof_g_packet = buf_len;
for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
{
if (rsa->regs[i].pnum == -1)
continue;
if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
rsa->regs[i].in_g_packet = 0;
else
rsa->regs[i].in_g_packet = 1;
}
}
....
}
调试过程:
/bin/arm-none-linux-gnueabi-gdb ./test_debug
/bin/arm-none-linux-gnueabi-gdb: Symbol `acs_map' has different size in shared object, consider re-linking
GNU gdb (GDB) 7.11.1
Copyright (C) 2016 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=x86_64-pc-linux-gnu --target=arm-none-linux-gnueabi".
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"...
Reading symbols from ./test_debug...done.
(gdb) target remote 10.168.10.121:2345
Remote debugging using 10.168.10.121:2345
warning: Can not parse XML target description; XML support was disabled at compile time
Reading /lib/ld-linux.so.3 from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /lib/ld-linux.so.3 from remote target...
Reading symbols from target:/lib/ld-linux.so.3...(no debugging symbols found)...done.
0x400007b0 in _start () from target:/lib/ld-linux.so.3
(gdb) l
15 void A(int *p)
16 {
17 B(p);
18 }
19
20 void A2(int *p)
21 {
22 C(p);
23 }
24
(gdb) b main
Breakpoint 1 at 0x84c8: file test_debug.c, line 29.
(gdb) step
Single stepping until exit from function _start,
which has no line number information.
Reading /lib/libgcc_s.so.1 from remote target...
Reading /lib/libc.so.6 from remote target...
Breakpoint 1, main (argc=1, argv=0xbefffe74) at test_debug.c:29
29 int *p = NULL;
(gdb) b test_debug.c:31
Breakpoint 2 at 0x84d0: file test_debug.c, line 31.
(gdb) step
Breakpoint 2, main (argc=1, argv=0xbefffe74) at test_debug.c:31
31 A2(&a); // A2 > C
(gdb) step
A2 (p=0xbefffd10) at test_debug.c:22
22 C(p);
(gdb) step
C (p=0xbefffd10) at test_debug.c:6
6 *p = 0x12;
(gdb) print a
No symbol "a" in current context.
(gdb) print p
$1 = (int *) 0xbefffd10
(gdb) step
7 }
(gdb) step
A2 (p=0xbefffd10) at test_debug.c:23
23 }
(gdb) step
main (argc=1, argv=0xbefffe74) at test_debug.c:32
32 printf("a = 0x%x\n", a);
(gdb) print a
$2 = 18
(gdb) next
34 A(p); // A > B > C
(gdb) step
A (p=0x0) at test_debug.c:17
17 B(p);
(gdb) step
B (p=0x0) at test_debug.c:12
12 C(p);
(gdb) step
C (p=0x0) at test_debug.c:6
6 *p = 0x12;
(gdb) print p
$3 = (int *) 0x0
(gdb) step
Program received signal SIGSEGV, Segmentation fault.
0x00008444 in C (p=0x0) at test_debug.c:6
6 *p = 0x12;
(gdb) step
Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
使用core dump 文件调式
将生成的core文件复制到pc端
/bin/arm-none-linux-gnueabi-gdb ./test_debug ./core