GDB是什么
GDB 全称“GNU symbolic debugger”,从名称上不难看出,它诞生于 GNU 计划(同时诞生的还有 GCC、Emacs 等),是 Linux 下常用的程序调试器。发展至今,GDB 已经迭代了诸多个版本,当下的 GDB 支持调试多种编程语言编写的程序,包括 C、C++、Go、Objective-C、OpenCL、Ada 等。实际场景中,GDB 更常用来调试 C 和 C++ 程序。
创建安装目录
cd ~
mkdir arm-linux
cd ~/arm-linux
mkdir -p gdb/src
mkdir -p gdb/build
export SRCDIR=~/arm-linux/gdb/src
export BUILDDIR=~/arm-linux/gdb/build
export TARGET=arm-linux-gnueabi
export PREFIX=/usr/local/arm/4.8.2
export TARGET_PREFIX=$PREFIX/$TARGET
export PATH=$PATH:$PREFIX/bin
GDB源码下载
cd ~/arm-linux/gdb/src
wget http://ftp.gnu.org/gnu/gdb/gdb-8.3.1.tar.gz
tar -xvzf gdb-8.3.1.tar.gz
编译
cd ~/arm-linux/gdb/build
~/arm-linux/gdb/src/gdb-8.3.1/configure --prefix=/usr/local/arm/4.8.2--target=arm-linux-gnueabi
make
常见错误1
...
/bin/bash ./libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I../../../src/gdb-7.6.1/bfd -I. -I../../../src/gdb-7.6.1/bfd -I../../../src/gdb-7.6.1/bfd/../include -DHAVE_bfd_elf32_littlearm_vec -DHAVE_bfd_elf32_bigarm_vec -DHAVE_bfd_elf32_little_generic_vec -DHAVE_bfd_elf32_big_generic_vec -DBINDIR='"/usr/local/arm/4.8.2/bin"' -W -Wall -Wstrict-prototypes -Wmissing-prototypes -Wshadow -Werror -g -O2 -MT opncls.lo -MD -MP -MF .deps/opncls.Tpo -c -o opncls.lo ../../../src/gdb-7.6.1/bfd/opncls.c
libtool: compile: gcc -DHAVE_CONFIG_H -I. -I../../../src/gdb-7.6.1/bfd -I. -I../../../src/gdb-7.6.1/bfd -I../../../src/gdb-7.6.1/bfd/../include -DHAVE_bfd_elf32_littlearm_vec -DHAVE_bfd_elf32_bigarm_vec -DHAVE_bfd_elf32_little_generic_vec -DHAVE_bfd_elf32_big_generic_vec -DBINDIR=\"/usr/local/arm/4.8.2/bin\" -W -Wall -Wstrict-prototypes -Wmissing-prototypes -Wshadow -Werror -g -O2 -MT opncls.lo -MD -MP -MF .deps/opncls.Tpo -c ../../../src/gdb-7.6.1/bfd/opncls.c -o opncls.o
In file included from ../../../src/gdb-7.6.1/bfd/opncls.c:26:0:
../../../src/gdb-7.6.1/bfd/opncls.c: In function ‘bfd_fopen’:
./bfd.h:529:65: error: right-hand operand of comma expression has no effect [-Werror=unused-value]
#define bfd_set_cacheable(abfd,bool) (((abfd)->cacheable = bool), TRUE)
^
../../../src/gdb-7.6.1/bfd/opncls.c:263:5: note: in expansion of macro ‘bfd_set_cacheable’
bfd_set_cacheable (nbfd, TRUE);
^
cc1: all warnings being treated as errors
Makefile:1607: recipe for target 'opncls.lo' failed
make[4]: *** [opncls.lo] Error 1
make[4]: Leaving directory '/home/weisf/arm-linux/build/gdb/bfd'
Makefile:1649: recipe for target 'all-recursive' failed
make[3]: *** [all-recursive] Error 1
make[3]: Leaving directory '/home/weisf/arm-linux/build/gdb/bfd'
Makefile:1135: recipe for target 'all' failed
make[2]: *** [all] Error 2
make[2]: Leaving directory '/home/weisf/arm-linux/build/gdb/bfd'
Makefile:2579: recipe for target 'all-bfd' failed
make[1]: *** [all-bfd] Error 2
make[1]: Leaving directory '/home/weisf/arm-linux/build/gdb'
Makefile:828: recipe for target 'all' failed
make: *** [all] Error 2
原因:
比较新的编译器如gcc4.5 以上的编译器,对 #define bfd_set_cacheable(abfd,bool) (((abfd)->cacheable = bool), TRUE) 这样的语法不支持
解决方案:
解决方法是加上忽略告警的选项 --disable-werror:
make clean
~/arm-linux/gdb/src/gdb-8.3.1/configure --prefix=/usr/local/arm/4.8.2 --target=arm-linux-gnueabi --disable-werror
make
常见错误2
make[3]: Entering directory '/home/vv/arm-linux/gdb/build/gdb'
make[4]: Entering directory '/home/vv/arm-linux/gdb/build/gdb/doc'
/home/vv/arm-linux/gdb/src/gdb-9.1/missing makeinfo --split-size=5000000 --split-size=5000000 -I /home/vv/arm-linux/gdb/src/gdb-9.1/gdb/doc/../../readline/readline/doc -I /home/vv/arm-linux/gdb/src/gdb-9.1/gdb/doc/../mi -I /home/vv/arm-linux/gdb/src/gdb-9.1/gdb/doc \
-o gdb.info /home/vv/arm-linux/gdb/src/gdb-9.1/gdb/doc/gdb.texinfo
/home/vv/arm-linux/gdb/src/gdb-9.1/missing: 81: /home/vv/arm-linux/gdb/src/gdb-9.1/missing: makeinfo: not found
WARNING: 'makeinfo' is missing on your system.
You should only need it if you modified a '.texi' file, or
any other file indirectly affecting the aspect of the manual.
You might want to install the Texinfo package:
<http://www.gnu.org/software/texinfo/>
The spurious makeinfo call might also be the consequence of
using a buggy 'make' (AIX, DU, IRIX), in which case you might
want to install GNU make:
<http://www.gnu.org/software/make/>
Makefile:490: recipe for target 'gdb.info' failed
make[4]: *** [gdb.info] Error 127
make[4]: Leaving directory '/home/vv/arm-linux/gdb/build/gdb/doc'
Makefile:2006: recipe for target 'subdir_do' failed
make[3]: *** [subdir_do] Error 1
make[3]: Leaving directory '/home/vv/arm-linux/gdb/build/gdb'
Makefile:1651: recipe for target 'all' failed
make[2]: *** [all] Error 2
make[2]: Leaving directory '/home/vv/arm-linux/gdb/build/gdb'
Makefile:9563: recipe for target 'all-gdb' failed
make[1]: *** [all-gdb] Error 2
make[1]: Leaving directory '/home/vv/arm-linux/gdb/build'
Makefile:851: recipe for target 'all' failed
make: *** [all] Error 2
解决方案:
sudo apt install texinfo
常见错误3
Configuring in ./gdb
configure: loading cache ./config.cache
configure: error: `MAKEINFO' has changed since the previous run:
configure: former value: `/home/vv/arm-linux/gdb/src/gdb-9.1/missing makeinfo --split-size=5000000'
configure: current value: `makeinfo --split-size=5000000'
configure: error: in `/home/vv/arm-linux/gdb/build/gdb':
configure: error: changes in the environment can compromise the build
configure: error: run `make distclean' and/or `rm ./config.cache' and start over
Makefile:9533: recipe for target 'configure-gdb' failed
make[1]: *** [configure-gdb] Error 1
make[1]: Leaving directory '/home/vv/arm-linux/gdb/build'
Makefile:851: recipe for target 'all' failed
make: *** [all] Error 2