Forlinx OK-MX9352-C Development Board Linux 6.1.33 System GDB Debugging Tutorial

1. Using GDB

GDB, short for The GNU Project Debugger, is a comprehensive debugging tool for Linux. It supports debugging techniques such as breakpoints, single-stepping, variable printing, variable observation, register inspection, and stack inspection. In software development within the Linux environment, GDB serves as the primary debugging tool for C and C++ programs.

The OK-MX9352-C's default Linux 5.15.52 kernel supports gdbserver, and the provided development environment also supports GDB by default.

1.1 Compiling Applications in the Development Environment

Set Environment Variables:

forlinx@ubuntu:~$:. /opt/fsl-imx-xwayland/6.1-mickledore/environment-setup-armv8a-poky
-linux

Compile the Application with Debugging Symbols: Ensure the -g option is included for debugging symbols.

forlinx@ubuntu:~$ $CC -g 485-test.c -o 485_gdbtest

Copy the Application to the Development Board: Transfer 485_gdbtest to the root directory (/) of the development board.

Forlinx OK-MX9352-C Development Board Linux 6.1.33 System GDB Debugging Tutorial

1.2 Setting Up the Development Board IP and Port

root@ok-mx93:~# ifconfig eth0 172.16.0.177

Forlinx OK-MX9352-C Development Board Linux 6.1.33 System GDB Debugging Tutorial

root@ok-mx93:/# chmod 777 485_gdbtest
root@ok-mx93:/# gdbserver 172.16.0.177:2345 485_gdbtest
Process /485_gdbtest created; pid = 740
Listening on port 2345

1.3 Configuring Virtual Machine IP in the Same Subnet and Ensuring Connectivity

1.4 Debugging

Forlinx OK-MX9352-C Development Board Linux 6.1.33 System GDB Debugging Tutorial

Now, debug according to the needs using the following common command:

Here are a few commonly used commands:

  • (1) l: List all source code.
  • (2) break main: Set a breakpoint at main.
    break test_debug.c:11: Set a breakpoint at line 11 in test_debug.c.
  • (3) c: Continue execution until the next breakpoint.
  • (4) step: Single-step execution (enter functions).
  • (5) next: Single-step execution (skip over functions).
  • (6) print a: Print the value of variable a.
  • (7) quit: Exit GDB (this will also terminate gdbserver on the development board).

2. Offline Debugging of Multithreaded Applications

2.1 Compiling Applications in the Development Environment

Set Environment Variables: Ensure the cross-compiler is available by setting the appropriate environment variables

forlinx@ubuntu:~$ . /opt/fsl-imx-xwayland/6.1-mickledore/environment-setup-armv8a-poky
-linux

Compile the Application with Debugging Symbols: Ensure the -g option is included for debugging symbols.

forlinx@ubuntu:~# $CC -g test1.c -o test1

2.2 Compiling the Program and Running It in the Background on the Development Board

root@ok-mx93:~# ./test1 > test.log &
[1] 780
root@ok-mx93:~# ps
    PID TTY          TIME CMD
    237 ttyLP0   00:00:00 login
    503 ttyLP0   00:00:00 sh
    780 ttyLP0   00:00:00 test1
    783 ttyLP0   00:00:00 ps

Forlinx OK-MX9352-C Development Board Linux 6.1.33 System GDB Debugging Tutorial

2.3 Using pstree -p id to View the Relationship Between Main Thread and Child Threads

root@ok-mx93:~# pstree -p 780
test1(780)-+-{test1}(781)
           `-{test1}(782)

Forlinx OK-MX9352-C Development Board Linux 6.1.33 System GDB Debugging Tutorial

2.4 Debugging with gdb test1 id and Inspecting the Call Stack with bt

(1) View main thread information

root@ok-mx93:~# gdb test1 780
GNU gdb (GDB) 11.2
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
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 "aarch64-poky-linux".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
.
Find the GDB manual and other documentation resources online at:
    .
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test1...
Attaching to program: /home/root/test1, process 780
[New LWP 781]
[New LWP 782]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/libthread_db.so.1".
__futex_abstimed_wait_common64 (private=, cancel=true, abstime=0x0, op=265, expected=781, futex_word=0xffffbdf4f1f0) at futex-internal.c:57
57      futex-internal.c: No such file or directory.
(gdb) bt
#0  __futex_abstimed_wait_common64 (private=, cancel=true, abstime=0x0, op=265, expected=781, futex_word=0xffffbdf4f1f0) at futex-internal.c:57
#1  __futex_abstimed_wait_common (cancel=true, private=, abstime=0x0, clockid=, expected=781, futex_word=0xffffbdf4f1f0) at futex-internal.c:87
#2  __GI___futex_abstimed_wait_cancelable64 (futex_word=0xffffbdf4f1f0, expected=781, clockid=, abstime=0x0, private=) at futex-internal.c:139
#3  0x0000ffffbdfd1b78 in __pthread_clockjoin_ex (threadid=281473868689696, thread_return=thread_return@entry=0x0, clockid=clockid@entry=0, abstime=abstime@entry=0x0, block=block@entry=true)
    at pthread_join_common.c:102
#4  0x0000ffffbdfd1a54 in ___pthread_join (threadid=, thread_return=thread_return@entry=0x0) at pthread_join.c:24
#5  0x0000aaaab07309e8 in main () at test1.c:41

(2) View child thread information

root@ok-mx93:~# gdb test1 781
GNU gdb (GDB) 11.2
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
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 "aarch64-poky-linux".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
.
Find the GDB manual and other documentation resources online at:
    .
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test1...
Attaching to program: /home/root/test1, process 781
Reading symbols from /lib/libpthread.so.0...
Reading symbols from /lib/.debug/libpthread.so.0...
Reading symbols from /lib/libc.so.6...
Reading symbols from /lib/.debug/libc.so.6...
Reading symbols from /lib/ld-linux-aarch64.so.1...
Reading symbols from /lib/.debug/ld-linux-aarch64.so.1...
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/libthread_db.so.1".
0x0000ffffbe004790 in __GI___clock_nanosleep (clock_id=, clock_id@entry=0, flags=flags@entry=0, req=req@entry=0xffffbdf4e810, rem=rem@entry=0xffffbdf4e810) at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:48
48      ../sysdeps/unix/sysv/linux/clock_nanosleep.c: No such file or directory.
(gdb) bt
#0  0x0000ffffbe004790 in __GI___clock_nanosleep (clock_id=, clock_id@entry=0, flags=flags@entry=0, req=req@entry=0xffffbdf4e810, rem=rem@entry=0xffffbdf4e810)
    at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:48
#1  0x0000ffffbe0096e0 in __GI___nanosleep (req=req@entry=0xffffbdf4e810, rem=rem@entry=0xffffbdf4e810) at ../sysdeps/unix/sysv/linux/nanosleep.c:25
#2  0x0000ffffbe0095b8 in __sleep (seconds=0, seconds@entry=1) at ../sysdeps/posix/sleep.c:55
#3  0x0000aaaab0730b98 in pthread_run1 (arg=) at test1.c:14
#4  0x0000ffffbdfd02a8 in start_thread (arg=0xfffffd77f6d7) at pthread_create.c:442
#5  0x0000ffffbe037ddc in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:79
root@ok-mx93:~# gdb test1 782
GNU gdb (GDB) 11.2
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
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 "aarch64-poky-linux".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
.
Find the GDB manual and other documentation resources online at:
    .
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test1...
Attaching to program: /home/root/test1, process 782
Reading symbols from /lib/libpthread.so.0...
Reading symbols from /lib/.debug/libpthread.so.0...
Reading symbols from /lib/libc.so.6...
Reading symbols from /lib/.debug/libc.so.6...
Reading symbols from /lib/ld-linux-aarch64.so.1...
Reading symbols from /lib/.debug/ld-linux-aarch64.so.1...
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/libthread_db.so.1".
0x0000ffffbe004790 in __GI___clock_nanosleep (clock_id=, clock_id@entry=0, flags=flags@entry=0, req=req@entry=0xffffbd73e810, rem=rem@entry=0xffffbd73e810) at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:48
48      ../sysdeps/unix/sysv/linux/clock_nanosleep.c: No such file or directory.
(gdb) bt
#0  0x0000ffffbe004790 in __GI___clock_nanosleep (clock_id=, clock_id@entry=0, flags=flags@entry=0, req=req@entry=0xffffbd73e810, rem=rem@entry=0xffffbd73e810)
    at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:48
#1  0x0000ffffbe0096e0 in __GI___nanosleep (req=req@entry=0xffffbd73e810, rem=rem@entry=0xffffbd73e810) at ../sysdeps/unix/sysv/linux/nanosleep.c:25
#2  0x0000ffffbe0095b8 in __sleep (seconds=0, seconds@entry=1) at ../sysdeps/posix/sleep.c:55
#3  0x0000aaaab0730bd8 in pthread_run2 (arg=) at test1.c:25
#4  0x0000ffffbdfd02a8 in start_thread (arg=0xfffffd77f6d7) at pthread_create.c:442
#5  0x0000ffffbe037ddc in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:79



Dear friends, we have created an exclusive embedded technical exchange group on Facebook, where our experts share the latest technological trends and practical skills. Join us and grow together!