Raspberry Pi: debugging with gdb, command line

20170325

This post is about how to debug Raspberry Pi software with gdb, command line.

Environment

  • Ubuntu 16.04 LTS
  • arm-linux-gnueabihf (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
  • Raspberry Pi 3 Model B

Prepare/compile helloworld

See here.

Install gdb-multiarch on host

$ sudo apt-get install gdb-multiarch

Install gdbserver on Raspberry Pi

$ sudo apt-get install gdbserver

Debug

# On target, 192.168.0.131 - debug host's ip address
$ gdbserver --multi 192.168.0.131:2345
Listening on port 2345

# On host, 192.168.0.140 - target ip address
$ gdb-multiarch
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
...
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) target extended-remote 192.168.0.140:2345
Remote debugging using 192.168.0.140:2345
(gdb) set remote exec-file /home/pi/path/helloworld
(gdb) file /home/uname/path/helloworld
Reading symbols from /home/uname/path/helloworld...done.
(gdb) start
Temporary breakpoint 1 at 0x105f4: file src/helloworld.cpp, line 12.
Starting program: /home/uname/path/helloworld 
...
Temporary breakpoint 1, main () at src/helloworld.cpp:12
12    int main() {
(gdb) next
13        std::cout << "Hello World!" << std::endl;
(gdb) next
12    int main() {
(gdb) next
13        std::cout << "Hello World!" << std::endl;
(gdb) next
12    int main() {
(gdb) next
13        std::cout << "Hello World!" << std::endl;
(gdb) next
15    }

# On target
Remote debugging from host 192.168.0.131
Process /home/pi/path/helloworld created; pid = 2267
Hello World!