17 Mar 2008 marcioandreyoliveira   » (Journeyer)

How to load debug symbols with GDB - Another Approach

I wrote in the post How to load debug symbols with GDB one way do debug a stripped version of an application with full access to debug symbols.

In this post I will explain another way you can do it.

We are going to load debuging information from a file that was generated by our executable just before it was stripped off..

I am going to use the same source and environment settings that I used in my earlier post:
  • released.c: source code of the program we wish to debug (listing 1).
  • ~/estudo/: Source code of our program will be put here.
  • ~/local/bin: The stripped off version of binary program will stay here.
  • ~/local/symbols: In this place are all files that contain debuging information.

<center><tbody></tbody>

#include <stdio.h>
#include <stdlib.h>


int division(int a, int b);

int m;

int main(void)
{
int i;
int j;

printf("vou setar i\n");
i = 10;

printf("vou setar j\n");
j = 1;

printf ("i = %d, j = %d\n", i, j);
m = division(i, j);

printf("m = %d / %d = %d\n", i, j, m);

return 0;

int division(int a, int b)
{
return a / b;
}


</center>
Listing 1 - sample program source code

Let's start:

1 - Create your program with debug information. In our sample:
gcc -g -o release release.c <ENTER>

2 - Now we will generate a file that will contain only debug informations. You generate this file by issuing this command:
objcopy --only-keep-debug <executable_file> <symbol_file> <object_file> <symbol_file> <ENTER></symbol_file></object_file></symbol_file></executable_file>
In our sample:
objcopy --only-keep-debug release release.sym <ENTER>

The choice of .sym as an extension for the debug info file is totally arbitrary. You can use whatever you wish to.

3 - You remove debugging information:
strip -s release <ENTER>

4 - Move file release to ~/local/bin/:
mv release ~/local/bin <ENTER>

5 - Move file released.sym to ~/local/symbols/
mv release.sym ~/local/symbols <ENTER>

6 - Go to directory ~/local/bin
cd ~/local/bin <ENTER>

7 - Run GDB:
gdb ./release <ENTER>

8 - Try list command to see that release executable file doesn't have symbols in it.

Figure 1 shows us what I said.

Figure 1 - executable file named release is loaded by GDB.

9 - Let GDB to load symbols from symbols file named release.sym. This file has all symbols that we need to debug. You achieve this by issuing the following command:
add-symbol-file ~/local/symbols/release.sym <ENTER>

From now on you can debug your program as usual.

Figure 2 shows us that debuging symbols where imported successfully and that now the list command shows us the program source code.

Figure 2 - now our GDB session has debuging symbols

As you can see in figure 3, I set a break point at line 17 and I ran the program that stopped there. Then I printed i variable. It is possible just because symbols were loaded.

Figure 3 - debugging session.

You can see that this method to load debug symbols is easier than the previous one presented in the post How to load debug symbols with GDB.

My friend Jumpi complained that it is not the unix way to debug applications that just don't have debug information by loading a separated file. We have core files to help us to debug the application in our host.

I agree with him that it is unusual to debug this way, but it may be useful.

The idea behind this method (and behind the previous one) is similar to that used by MS with .pdb files.

It will allow you to save disk space on your host because you can keep only stripped versions of your applications and libraries in it. It is especially important to embedded systems.

At the same time it let you have a colection of symbol files stored at some place (say, a DVD disc or another server). When (and if) you need to do a debug session

I hope this post will make your life easier.

Syndicated 2008-03-17 14:49:00 from Marcio Andrey Oliveira

Latest blog entries     Older blog entries

New Advogato Features

FOAF updates: Trust rankings are now exported, making the data available to other users and websites. An external FOAF URI has been added, allowing users to link to an additional FOAF file.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!