Remote Debugging with GDB Print

 

Step through the following procedure in order to start remote debugging of Linux userspace applications with gdbserver:

  • Download the Yocto toolchain from the Emcraft website.
  • Install the toolchain to your development host. The toolchain can be installed to an arbitrary directory:
  • $ sh ./fsl-imx-fb-glibc-x86_64-meta-toolchain-qt5-aarch64-toolchain-4.9.51 -mx8-beta.sh

  • Activate the cross-build environment:
  • $ source /opt/fsl-imx-fb/4.9.51-mx8-beta/environment-setup-aarch64-poky- linux $ unset CCACHE_PATH

  • Make sure your target board has TCP/IP configured in Linux:
  • ~ # ifconfig eth0 Link encap:Ethernet HWaddr 3c:fb:96:77:88:a0 inet addr:172.17.11.163 Bcast:172.17.255.255 Mask:255.255.0.0 inet6 addr: fe80::3efb:96ff:fe77:88a0/64 Scope:Link UP BROADCAST RUNNING MULTICAST DYNAMIC MTU:1500 Metric:1 RX packets:8407 errors:0 dropped:27 overruns:0 frame:0 TX packets:551 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:831648 (812.1 KiB) TX bytes:247626 (241.8 KiB) ...

  • It is recommended to rebuild your application (see attached for example) with special GCC options. Use the following options:
  • [sasha@liman app]$ $CC -o app app.c -O0 -g

  • Deploy your application binary (app) to the target. For example, you can scp it:
  • [sasha@liman app]$ scp app  root​@172.17.11.163:/tmp/ app 100% 10KB 10.4KB/s 00:00

  • Run gdbserver on the target:
  • ~ # gdbserver :1234 /tmp/app Process /tmp/app created; pid = 3366 Listening on port 1234

  • Run GDB on the development host:
  • [sasha@liman app]$ aarch64-poky-linux-gdb ./app 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-pokysdk-linux --target= aarch64-poky-linux". 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 ./app...done. (gdb)

  • Specify the sysroot directory containing copies of the target libraries:
  • (gdb) set sysroot /opt/fsl-imx-wayland/4.9.51-mx8-ga/sysroots/\ aarch64-poky-linux/ (gdb)

  • Establish a connection to the target:
  • (gdb) target remote 172.17.11.163:1234 Remote debugging using 172.17.11.163:1234 Reading symbols from /opt/fsl-imx-wayland/4.9.51-mx8-ga/sysroots/aarch64 -poky-linux/lib/ld-linux-aarch64.so.1...Reading symbols from /opt/fsl-imx- wayland/4.9.51-mx8-ga/sysroots/aarch64-poky-linux/lib/.debug/ld-2.24.so ...done. done. 0x0000ffffb7fd3c40 in _start () from /opt/fsl-imx-wayland/4.9.51-mx8-ga/ sysroots/aarch64-poky-linux/lib/ld-linux-aarch64.so.1 (gdb)

  • Debug your application using the standard GDB commands. Here is a sample debug session:
  • (gdb) b main Breakpoint 1 at 0x4005d0: file app.c, line 16. (gdb) c Continuing. Breakpoint 1, main () at app.c:16 16 a = 10; (gdb) n 18 b = 20; (gdb) p a $1 = 10 (gdb) n 20 ret = func(a,b); (gdb) p b $2 = 20 (gdb) s func (a=10, b=20) at app.c:5 5 int c = (a+b)/2; (gdb) n 7 printf("%s: (%d + %d)/2 = %d\n", __func__, a, b, c); (gdb) p c $3 = 15 (gdb) bt #0 func (a=10, b=20) at app.c:7 #1 0x00000000004005ec in main () at app.c:20 (gdb) n 9 return c; (gdb) n 10 } (gdb) fini Run till exit from #0 func (a=10, b=20) at app.c:10 0x00000000004005ec in main () at app.c:20 20 ret = func(a,b); Value returned is $4 = 15 (gdb)