Mostrando postagens com marcador debug. Mostrar todas as postagens
Mostrando postagens com marcador debug. Mostrar todas as postagens

domingo, julho 18, 2010

Error installing ruby-debug

Last week I tried to install ruby-debug on my Ubuntu 9.10 and I got an error as you can see below:

andreyq@desenv-ubuntu-9:~$ sudo gem install ruby-debug
[sudo] password for andreyq:
Building native extensions.  This could take a while...
ERROR:  Error installing ruby-debug:
ERROR: Failed to build gem native extension.

/usr/bin/ruby1.8 extconf.rb
extconf.rb:1:in `require': no such file to load -- mkmf (LoadError)
from extconf.rb:1

Gem files will remain installed in /var/lib/gems/1.8/gems/linecache-0.43 for inspection.
Results logged to /var/lib/gems/1.8/gems/linecache-0.43/ext/gem_make.out
andreyq@desenv-ubuntu-9:~$

After some research I discovered that I needed to have
ruby1.8-dev
package installed in order to satisfy some dependencies.

I did it
sudo apt-get install ruby1.8-dev
and then I tried again to install ruby-debug on my system:

andreyq@desenv-ubuntu-9:~$ sudo gem install ruby-debug
Building native extensions.  This could take a while...
Building native extensions.  This could take a while...
Successfully installed linecache-0.43
Successfully installed ruby-debug-base-0.10.3
Successfully installed ruby-debug-0.10.3
3 gems installed
Installing ri documentation for linecache-0.43...
Installing ri documentation for ruby-debug-base-0.10.3...
Installing ri documentation for ruby-debug-0.10.3...
Installing RDoc documentation for linecache-0.43...
Installing RDoc documentation for ruby-debug-base-0.10.3...
Installing RDoc documentation for ruby-debug-0.10.3...
andreyq@desenv-ubuntu-9:~$

Great! It worked!

What is weird is that I thought that the
ruby1.8-dev
package would be required only for changes on ruby itself.

Erro na instalação do ruby-debug

Na semana passada eu tentei instalar o ruby-debug no meu Ubuntu 9.10 e obtive o erro mostrado abaixo:

andreyq@desenv-ubuntu-9:~$ sudo gem install ruby-debug
[sudo] password for andreyq:
Building native extensions.  This could take a while...
ERROR:  Error installing ruby-debug:
ERROR: Failed to build gem native extension.

/usr/bin/ruby1.8 extconf.rb
extconf.rb:1:in `require': no such file to load -- mkmf (LoadError)
from extconf.rb:1

Gem files will remain installed in /var/lib/gems/1.8/gems/linecache-0.43 for inspection.
Results logged to /var/lib/gems/1.8/gems/linecache-0.43/ext/gem_make.out
andreyq@desenv-ubuntu-9:~$

Depois de algumas pesquisas descobri que precisava ter o pacote
ruby1.8-dev
instalado para satisfazer algumas dependências.

Fiz a instalação do mesmo:

sudo apt-get install ruby1.8-dev

e tentei novamente instalar o ruby-debug:

andreyq@desenv-ubuntu-9:~$ sudo gem install ruby-debug
Building native extensions.  This could take a while...
Building native extensions.  This could take a while...
Successfully installed linecache-0.43
Successfully installed ruby-debug-base-0.10.3
Successfully installed ruby-debug-0.10.3
3 gems installed
Installing ri documentation for linecache-0.43...
Installing ri documentation for ruby-debug-base-0.10.3...
Installing ri documentation for ruby-debug-0.10.3...
Installing RDoc documentation for linecache-0.43...
Installing RDoc documentation for ruby-debug-base-0.10.3...
Installing RDoc documentation for ruby-debug-0.10.3...
andreyq@desenv-ubuntu-9:~$

Ótimo! Desta vez obtive sucesso.

O engraçado é que eu pensava que o pacote ruby1.8-dev só seria necessário se eu fosse alterar o próprio ruby.

segunda-feira, março 17, 2008

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 debugging information.
#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;
}

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 <ENTER>
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 debugging 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 collection 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.

sábado, março 08, 2008

How to load debug symbols with GDB

My friend Wanderley asked me if it is possible to tell GDB to load debuging symbols from some file and use it to help debuging a program that doesn't have them.

Yes. It is.

There are two solutions to this question.

I going to explain the first solution in this post. The other solution I will explain in the next post.

You can load debuging information from an debug enabled version of executable file.

In order to better explain the first solution, I will setup my sample environment as follows:
  • 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.
#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;
}
Listing 1 - sample program source code

I have two versions of the program: with and without debuging information.

1 - You compile your program with debug information. In our sample:
gcc -Wall -g -release release.c <ENTER>

2 - You make a copy of your program. In our sample:
cp release release.full <ENTER>

3 - You strip off debuging information:
strip -s release <ENTER>

As you can see on Figure 1, we have two programs. released.full has debuging symbols but release doesn't have them.

Figure 1

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

5 - Move file released.full to ~/local/symbols/
mv release.full ~/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.

Note: if the program was already running you could get its PID then attach GDB to it.

Figure 2 shows us two windows. The first one shows that our executable file has no debug information. In the other window we can see that release is not yet loaded.

Figure 2 - executable file named release is loaded by GDB but it is not yet running.

9 - Let GDB to load symbols from executable file named release.full. This binary version of our program has all symbols that we need to debug.

Please notice that GDB will not replace the release executable by release.full version of our program. It will just import symbols from release.full into release debugging session.

But GDB needs to know in advance where it must put the symbols it will load. How can you determine the correct memory address?

It is quite simple. You issue command maint inside GDB:
maint info sections

Then you look for .text section. The address that is in the first column is what you want. In our sample, it is 0x08048320. See figure 3.

Figure 3 - looking for .text section address

10 - The next step is to instruct GDB to load debug symbols into .text section. To achieve it you do this:
add-symbol-file ~/local/symbols/release.full <.text section address>

In our sample it means to type:
add-symbol-file ~/local/symbols/release.full 0x08048320

From now on you can debug your program as usual.

Figure 4 shows us that debugging symbols where imported successfully and that now the list command (abbreviated as l) shows us the program source code.

Figure 4 - now our GDB session has debuging symbols

As you can see in figure 5, I set a break point at line 17 and I ran the program that stopped there. Then I printed i variable.

In the other terminal I issued ps command. It was done just to show you that the only program running was release executable. There is no instance of release.full program.

Figure 5 - debugging session.

I hope this post will make your life easier. In the next time I will teach you another way to import debugging symbols.