How to use gdb to debug linux app on hifive unleashed?

Hi, I am trying to use gdb to debug linux application program on hifive unleashed. With openocd cfg file and gdb in the freedom-u-sdk, I can attach to hifive unleashed, and I can see 5 thread. But how do I debug some application program?
For example, I built a simple application program, hello.c, compile it with -g, then I start openocd and gdb, below is my operation in gdb:
(gdb) target remote :3333
Remote debugging using :3333
warning: Target-supplied registers are not supported by the current architecture
warning: No executable has been specified and target does not support
determining executable automatically. Try using the “file” command.
0x0000000080000314 in ?? ()
(gdb) info threads
Id Target Id Frame

  • 1 Thread 1 (Name: Hart 0, RV64) 0x0000000080000314 in ?? ()
    2 Thread 2 (Name: Hart 1, RV64) 0x000000e00094adfc in ?? ()
    3 Thread 3 (Name: Hart 2, RV64) 0x00000000800028ae in ?? ()
    4 Thread 4 (Name: Hart 3, RV64) 0x000000e00096a61c in ?? ()
    5 Thread 5 (Name: Hart 4, RV64) 0x000000e000971294 in ?? ()
    (gdb) thread 2
    [Switching to thread 2 (Thread 2)]
    #0 0x000000e00094adfc in ?? ()
    (gdb) info threads
    Id Target Id Frame
    1 Thread 1 (Name: Hart 0, RV64) 0x0000000080000314 in ?? ()
  • 2 Thread 2 (Name: Hart 1, RV64) 0x000000e00094adfc in ?? ()
    3 Thread 3 (Name: Hart 2, RV64) 0x00000000800028ae in ?? ()
    4 Thread 4 (Name: Hart 3, RV64) 0x000000e00096a61c in ?? ()
    5 Thread 5 (Name: Hart 4, RV64) 0x000000e000971294 in ?? ()
    (gdb) file ~/tmp/test_hello/hello_hifive_u
    A program is being debugged already.
    Are you sure you want to change the file? (y or n) y
    Reading symbols from ~/tmp/test_hello/hello_hifive_u…done.
    (gdb) thread 2 apply load
    [Switching to thread 2 (Thread 2)]
    #0 0x000000e00094adfc in ?? ()
    (gdb) l
    1 #include <stdio.h>
    2
    3 void main() {
    4 printf(“hello from hifive unleashed\n”);
    5 }
    (gdb) thread 2 apply continue
    [Switching to thread 2 (Thread 2)]
    #0 0x000000e00094adfc in ?? ()

but I cannot see any output in the serial port, so can anybody show me how could I debug app with gdb on hifive unleashed?

You probably want to run gdb locally on the HiFive-U, rather than using remote gdb over JTAG. There are several good introductory tutorials on the Internet for this.
Here are two of them:

https://www.cs.cmu.edu/~gilpin/tutorial/
https://cs.baylor.edu/~donahoo/tools/gdb/tutorial.html

There is riscv linux native support in gdb now. You need the FSF GDB development sources, as this support was added after the last gdb release. You need a 4.20 linux kernel for all of the gdb support, but 4.19 has everything except the FP register support and should also work OK if you don’t need FP support. Then as Paul mentioned, you want to build gdb natively on the HiFive Unleashed and use it like on any other linux system.

Using openocd to debug is only going to work for bootloaders and kernels and other bare metal programs.

1 Like

thanks for your reply, I will have a try.

Thanks a lot, your reply is very clear, I will try it.