Monday, May 4, 2009

Compiling and using GDB for arm-linux

Some days ago I had to compile gdb manually in order to debug an arm-linux app. It's quite trivial but it's so useful that I thought it would be a nice idea to post the instructions here. Below there are some explanations about debugging remotely with KDevelop.

This was done with gdb-6.8, you can grab it here. It is assumed that arm-linux tools are available (PATH correctly set).

Compiling the GDB client

Decompress gdb-6.8 and compile it by issuing:



tar xvf gdb-6.8.tar.gz
cd gdb-6.8
./configure --build=x86 --host=x86 --target=arm-linux
make

After compiling just copy gdb/gdb to arm-linux-gdb where the arm-linux toolchain binaries reside (this is purely for organization and proper naming). You can now remove the gdb-6.8 directory.

NOTE: if your host computer architecture isn't intel-based just replace x86 by the correct value for your platform.

Compiling the GDB server (ARM)

Decompress gdb-6.8 and compile it by issuing:



tar xvf gdb-6.8.tar.gz
cd gdb-6.8
./configure --host=arm-linux
make

After compiling just copy gdb/gdbserver/gdbserver to your arm-linux filesystem so that you can execute it from a remote shell (gdbserver will be an arm-elf binary). You can now remove the gdb-6.8 directory.

Testing connections

First the server should be started in the  arm processor by issuing something like:



gdbserver host:1234 _executable_

Where _executable_ is the application that is going to be debugged and host:1234 tells gdbserver to listen for connections to port 1234.

To run gdb just type this in a PC shell:



arm-linux-gdb --se=_executable_

After that you'll get the gdb prompt. To connect to the target type:



target remote target_ip:1234

To resume execution type 'continue'. You can get an overview on gdb usage here.

Debugging with KDevelop

KDevelop can be used to watch variables and debug the remote target (place breakpoints, stop execution, etc). Just go to Project -> Project Options and make sure you have something like this:



monitor-debug.gdb is a file that contains the following



target remote target_ip:1234

After this you will be ready to remotely debug any arm-linux application.

_

16 comments:

  1. Hi,

    Thanks for the post. Trying to achieve the same, but when I get to the gdb prompt on the host and use "target remote target_ip:1234" to try to connect to the target I get:

    target_ip: unknown host
    target_ip:1234: No such file or directory.

    Any thoughts? (Also FYI I had to configure with --disable-werror to make as I was getting some warnings treated as errors).

    Thanks!
    Scott

    ReplyDelete
  2. Sorry for my foolishness... of course target_ip is replaced with the ip address...

    -Scott

    ReplyDelete
  3. Hi Scott. I usually edit /etc/hosts so I don't have to type the full ip address. Just add a line like: 192.168.1.123 myboard

    Then 'myboard' will resolve to 192.168.1.123, so you can use target remote myboard:1234

    ReplyDelete
  4. Hi, I also tried to compile gdb6.3 but I got an error like:

    ada-lex.c missing and flex not available.
    make[1]: *** [ada-lex.c] Error 1
    make[1]: Leaving directory `/home/mario/Documents/gdb6.3/gdb-6.3/gdb'
    make: *** [all-gdb] Error 2

    I am using ubuntu 9.10. I installed already the flex pacckage...

    Regards!
    Mario Huizar

    ReplyDelete
  5. It is fixed.. I only need to run the ./configure again. :D

    Regards!
    Mario Huizar

    ReplyDelete
  6. Hi..
    I am able to run gdb on host and able to connect to the gdbserver running on the target. but when I give target remote IP:port it says.. "GDB will be unable to find dynamic linker break point function. and track explicitly loaded dynamic code." and I'm not able to set the break point and run also.. please help..

    ReplyDelete
  7. According to what I read on the net (googling), that warning is printed when gdb cannot find the symbols for some shared libraries. However, that should not prevent you from debugging your program. Make sure you are compiling and linking with -g (gcc).

    ReplyDelete
  8. Hi,
    I tried to compile gdb to mini2440 and it keeps giving me an error:

    [code]
    checking zlib.h presence... yes
    checking for zlib.h... yes
    checking for library containing dlgetmodinfo... no
    checking for iconv... yes
    checking for iconv declaration... install-shextern size_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);
    checking for library containing waddstr... no
    configure: WARNING: no enhanced curses library found; disabling TUI
    checking for library containing tgetent... no
    configure: error: no termcap library found
    make[1]: *** [configure-gdb] Error 1
    make[1]: Leaving directory `/cross_compile/gdb-7.2/gdb-7.2'
    make: *** [all] Error 2
    user@VMWARE:/cross_compile/gdb-7.2/gdb-7.2$
    [/code]

    I tried to compile termcap library before, as it says here http://cygwin.com/ml/crossgcc/2007-02/msg00016.html
    but the compiler doesn't care about it. I was wondering if is it ncurses a dependency too, because it looks for an enhanced curses library, with the function tgetent(), but only gives a warning.

    Is there any dependencies to compile before?

    Thanks in advance,
    Sergio

    ReplyDelete
  9. Did you try compiling and 'installing' libncurses? That should fix it

    ReplyDelete
  10. Hi,
    I'm sorry for the delay, I've been too busy.
    Finally I could compile it, I had to compiled libncurses before, but it was too tricky :) I had to be changing the content of the Makefile, and then I got.

    Thanks a lot for sharing this,
    Sergio

    ReplyDelete
  11. When I followed your advise to build gdb, I get a error msg "error: ignoring return value of ‘getcwd’, declared with attribute warn_unused_resul"
    Is there any bug???
    Thank for your post!

    ReplyDelete
  12. Hi, if you get that when compiling gdb my guess is that you should try to find where that error is coming from and at least assign the return value of getcwd() to something, at least to bypass the error and try again. This could be happening because of different versions of your libc and gdb.

    ReplyDelete
  13. Just add --disable-werror to your build line, and that error will go away.

    ReplyDelete
  14. Sorry, to your configure line

    ReplyDelete
  15. Hi Everyone,

    I am new for Linux. I had download gdb-7.5 from gnu resourses. I am building gdb in cygwin environment and I got an error as shown below.

    configure: WARNING: no enhanced curses library found; disabling TUI
    checking for library containing tgetent... no
    configure: error: no termcap library found
    make[1]: *** [configure-gdb] Error 1
    make[1]: Leaving directory `/xyz/..../xyz'
    make: *** [all] Error 2
    [xyz/..../xyz]$

    I had also referred others thread and I already installed the ncurses-5.9 but when I was going to make that file then i got the error message like

    collect2: ld returned 1 exit status
    make[1]: *** [demo.exe] Error 1
    make[1]: Leaving directory xyz/.../xyz/ncurses-5.9/c++'
    make: *** [all] Error 2

    I also tried to download termcap library and it was successfually buil but when I was going to build gdb-7.5 then it shows me same error as described above....


    So if anyone has idea how to solve this problem then it would be very kind...

    Thanks in advance...

    ReplyDelete
  16. you are a life saver, thankyou

    ReplyDelete