Ubuntu 建立 gdb cross-complier 開發環境

一、系統配置

  • gdb run on PC (test on Ubuntu LTS 16.04 64 bit )
  • gdbserver(cross complier) run on embede system(PEL3KS)
  • arm-app(cross complier) run on ebede system(PEL3KS)

二、Download GDB Source Code

  1. gdb - https://ftp.gnu.org/gnu/gdb/gdb-7.9.tar.gz
  2. termcap - https://ftp.gnu.org/gnu/termcap/termcap-1.3.1.tar.gz
  3. expat - https://sourceforge.net/projects/expat/
  4. sudo apt install textinfo (注意:此處不是用apt-get)

三、Prepare

//這一步可以放在 ~/.profile最尾端,以方便登入後呼叫 arm-linux-gnueabihf-XXX(cross complier)
$ export PATH=$PATH:/opt/toolchains/ti335x/i686-arago-linux/usr/bin
$ cd ~/Desktop
//解壓縮 gdb
$ tar zxvf gdb-7.9.tar.gz
//解壓縮 termcap
$ tar zxvf termcap-1.3.1.tar.gz
//解壓縮expat
$ bzip2 -d expat-2.2.6.tar.bz2
$ tar xvf expat-2.2.6.tar

四、Build

Build gdb -> build gdbserver -> build arm-app,過程中會遇到一些問題,所以會安裝 termcap、expat、textinfo

1.gdb

$ cd gdb-7.9
$ ./configure --target=arm-linux-gnueabihf --program-prefix=arm-linux-gnueabihf- --prefix=/home/yuyan/Desktop/gdb-7.9-build

// tips: -j2 表示指定cpu數量,可以加快編譯
$ make -j2
$ make install

2.gdbserver

$ cd ~/Desktop/gdb-7.9/gdb/gdbserver

//CC=為指定編譯器,CFLAGS式如果編譯器不是灌在系統裡的話,記得要指定不然make 基本上會失敗。
$ ./configure --target=arm-linux-gnueabihf --host=arm-linux-gnueabihf --prefix=/home/yuyan/Desktop/gdb-7.9-build CC=arm-linux-gnueabihf-gcc CFLAGS="-I /opt/toolchains/ti335x/cortexa8t2hf-vfp-neon-linux-gnueabi/usr/include/"

3.arm-app

$ mkdir arm-app
$ cd arm-app
$ vim main.c

//main.c
#include <stdio.h>
int main(int argc,char *argv[])
{
    printf("Hello arm-app\n");
    return 0;
}

$ vim Makefile

// Makefile
CXX=arm-linux-gnueabihf-gcc
CXXFLAGS:=-g -I /opt/toolchains/ti335x/cortexa8t2hf-vfp-neon-linux-gnueabi/usr/include/
BIN=main.out
LDLIBS=
CFLAGS=-g

SRC=$(wildcard *.c)
OBJ=$(SRC:%.c=%.o)

all: $(OBJ)
        $(CXX) -o $(BIN) $^
%.o: %.c
        $(CXX) -c $< $(CXXFLAGS)

.PHONY:clean
clean:
        rm -rf *.o
        rm $(BIN)

$ make
//將.out 傳至embede system,本文是用nfs 的方式。
$ cp main.out /var/nfsshare

五、疑難排解(編譯或程式執行時)

1. gdb make (makeinfo: command not found )

http://stackoverflow.com/questions/338317/what-is-makeinfo-and-how-do-i-get-it

提示錯誤:/home/gdb-7.9/missing: line 81: makeinfo: command 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:
         
         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:
         
安裝庫:sudo apt-get install textinfo即可

2. gdb make (error: no termcap library found)

configure: error: no termcap library found
make[1]: *** [configure-gdb] Error 1
make: *** [all] Error 2

//https://ftp.gnu.org/gnu/termcap/
 $cd ./termcap-1.3.1
 $./configure --target=arm-none-linux-gnueabi --prefix=/tmp/termcap
 $make
 $sudo make install
 $sudo cp /tmp/termcap/include/termcap.h /usr/include
 $sudo cp /tmp/termcap/lib/libtermcap.a /usr/lib
 $sudo rm -rf /tmp/termcap

3. gdbserver: linux-x86-low.cerror: sys/reg.h: No such file or directory

  1. 找到config.h裡HAVE_SYS_REG_H 將其註釋掉.
  2. 重新make(記得先make clean)

4. gdb run Remote ‘g’ packet reply is too long

//修改gdb/remote.c文件中的static void
process_g_packet (struct regcache *regcache)
$ vim gdb/remote.c
//由
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->pnum == -1)
         continue;
    if (rsa->regs->offset >= rsa->sizeof_g_packet)
         rsa->regs->in_g_packet = 0;
    else
         rsa->regs->in_g_packet = 1;
    }
}

5. gdb run (一執行就 Segment fault)

如果gdb調試時經常崩潰不能運行gdb指令,且gdb加載調試程序時出現如下警告:
"warning: Can not parse XML target description; XML support was disabled at compile time"

原因:gdb編譯時缺少XML的解析庫expat, 這個庫對gdb是可選項,但是我編譯gdb7.9的時候如果沒有這個庫卻不行,所以編譯的時候加入了這個庫。方法如下:

1 下載expat,跟解壓縮
http://sourceforge.net/projects/expat/

2 配置和編譯expat
    $ cd expat-2.2.6
    $ ./configure
    $ make && make install

3.rebuild and re-complier gdb (記得先用 make distclean)

六、Use gdb 、gdbserver debug arm-app

embede system:(啟動 gdbserver)

$ cd /mnt/yuyan_nfs
$./arm-linux-gnueabihf-gdbserver :10000 ./main.out

PC:(使用 gdb 調適 arm-app)

$ cd /var/nfsshare
$ ls
arm-linux-gnueabihf-gdb  arm-linux-gnueabihf-gdbserver  main.out

$ ./arm-linux-gnueabihf-gdb arm-app.out
GNU gdb (GDB) 7.9
Copyright (C) 2015 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-unknown-linux-gnu --target=arm-linux-gnueabihf".
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 main.out...done.
(gdb) target remote 172.16.27.130:10000
Remote debugging using 172.16.27.130:10000
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initializers
and track explicitly loaded dynamic code.
0xb6fe0c80 in ?? ()
(gdb) b main
Breakpoint 1 at 0x8430: file main.c, line 12.
(gdb) bt
#0  0xb6fe0c80 in ?? ()
#1  0x00000000 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
(gdb) set solib-search-path /opt/toolchains/ti335x/cortexa8t2hf-vfp-neon-linux-gnueabi/lib
Reading symbols from /opt/toolchains/ti335x/cortexa8t2hf-vfp-neon-linux-gnueabi/lib/ld-2.15.so...(no debugging symbols found)...done.
(gdb) bt
#0  0xb6fe0c80 in ?? () from /opt/toolchains/ti335x/cortexa8t2hf-vfp-neon-linux-gnueabi/lib/ld-2.15.so
#1  0x00000000 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
(gdb) r
The "remote" target does not support "run".  Try "help target" or "continue".
(gdb) continue
Continuing.

Breakpoint 1, main () at main.c:12
12        printf("Hello World!\n");
(gdb) bt
#0  main () at main.c:12
(gdb) continue
Continuing.
[Inferior 1 (process 835) exited normally]
(gdb) q

  目錄