A10 VP - Debugging Linux Applications with the GNU Debugger
Debugging Linux Applications with the GNU Debugger

Prerequisites

Install Host Packages

This section assumes Ubuntu is used on the host. Other operating systems require different commands.

For this installation, the gdb debugger and gcc cross compiler packages are required. If Ubuntu is used as the host, the installation commands are:

sudo apt-get install gcc-arm-linux-gnueabihf
sudo apt-get install gdb-multiarch

Install Target Packages

The target requires that only the gdbserver package is installed.

1. Boot the virtual platform as shown in the "Boot Using Pre-Built Linux Kernel" section.

2. Install the gdbserver package by running the following command:

opkg update
opkg install gdbserver

Create and Cross-Compile Application on Host

1. Go to your home folder on the host and create a file named factorial.c that contains the following source code:

#include 

int factorial(int n) {
  if (n == 0)
    return 1;
  return n * factorial (n - 1);
}

int main () {
  int i;
  int n;
  for (i = 0; i < 10; ++i) {
    n = factorial (i);
    printf ("factorial(%d) = %d\n", i, n);
  }
  return 0;
}

2. Cross-compile the factorial.c file by typing the following command:

arm-linux-gnueabihf-gcc factorial.c -ggdb -o factorial.out

Copy the Application to the Target

To move the application to the target, run the commands listed below on the target. Be sure to replace host_user, host_name and host_path with the actual values for your host Linux PC:

cd ~
scp host_user@host_name:host_path/factorial.out .
scp host_user@host_name:host_path/factorial.c .

Start the gdb Server on the Target

Start the gdb server on the target by typing the following commands:

cd ~
gdbserver :8080 ./factorial.out

Debug Using the gdb Client on the Host

1. Run the gdb client on the host:

gdb-multiarch ./factorial.out
Process ./factorial.out created; pid = 229
Listening on port 8080

2. Connect gdb to the target.

Note: This example uses localhost:3624 instead of 192.168.0.9:8080. Please refer to the user guide "Network Connectivity" section accessible at https://www.altera.com/products/soc/tools_and_software.html#virtualplatform for more details.

(gdb) target remote localhost:3624
Remote debugging using localhost:3624
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initializers
and track explicitly loaded dynamic code.
0x76fcfb00 in ?? ()

3. Use the following gdb sample commands to debug the code:
  • b main: Set a breakpoint at the main function
  • c: Continue until the breakpoint is hit
  • s: Step one instruction
  • b 14: Insert a breakpoint at line 14
  • c typed multiple times: Run through iterations of the loop
  • l: List code

The host console looks similar to the view below:

<name>@ubuntu-12:~$ gdb-multiarch ./factorial.out
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1)7.4-2012.04
Copyright (C) 2012 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 "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/<name>/factorial.out...done.
(gdb) target remote localhost:3624
Remote debugging using localhost:3624
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initializers
and track explicitly loaded dynamic code.
0x76fcfb00 in ?? ()
(gdb) b main
Cannot access memory at address 0x0
Breakpoint 1 at 0x83ce: file factorial.c, line 12.
(gdb) c
Continuing.
warning: Could not load shared library symbols for 2 libraries, e.g.
/lib/libc.so.6.
Use the "info sharedlibrary" command to see the complete listing.
Do you need "set solib-search-path" or "set sysroot"?
Breakpoint 1, main () at factorial.c:12
12 for (i = 0; i < 10; ++i) {
(gdb) l
7 }
8
9 int main () {
10 int i;
11 int n;
12 for (i = 0; i < 10; ++i) {
13 n = factorial (i);
14 printf ("factorial(%d) = %d\n", i, n);
15 }
16 return 0;
(gdb) b 14
Breakpoint 2 at 0x83de: file factorial.c, line 14.
(gdb) c
Continuing.
Breakpoint 2, main () at factorial.c:14
14 printf ("factorial(%d) = %d\n", i, n);
(gdb) s
Cannot access memory at address 0x0
Breakpoint 2, main () at factorial.c:14
14 printf ("factorial(%d) = %d\n", i, n);
(gdb) c
Continuing.
Breakpoint 2, main () at factorial.c:14
14 printf ("factorial(%d) = %d\n", i, n);
(gdb) c
Continuing.
Breakpoint 2, main () at factorial.c:14
14 printf ("factorial(%d) = %d\n", i, n);
(gdb) c
Continuing. Breakpoint 2, main () at factorial.c:14
14 printf ("factorial(%d) = %d\n", i, n);
(gdb) c
Continuing.
Breakpoint 2, main () at factorial.c:14
14 printf ("factorial(%d) = %d\n", i, n);
(gdb) c

The target console looks similar to the view below:

root@host:~# gdbserver :8080 ./factorial.out
Process ./factorial.out created; pid = 229
Listening on port 8080
Remote debugging from host 192.168.0.1
factorial(0) = 1
factorial(1) = 1
factorial(2) = 2
factorial(3) = 6
factorial(4) = 24
factorial(5) = 120
factorial(6) = 720
factorial(7) = 5040
factorial(8) = 40320
factorial(9) = 362880
Child exited with status 0

© 1999-2024 RocketBoards.org by the contributing authors. All material on this collaboration platform is the property of the contributing authors.

Privacy Policy - Terms Of Use

This website is using cookies. More info. That's Fine