Rechercher : dans
Par :

Débogger avec gdb

Dernière réponse le 30 mai 2008 à 22:48:55 moimeme, le 30 mai 2008 à 20:37:01 
 Signaler ce message aux modérateurs

Bonjour,
je n'arrive pas a comprendre comment utiliser le debogeur gdb...
j'ai regardé sur internet, je suis tombé sur cette page http://www.linux-kheops.com/doc/man/manfr/man-html-0.9/man1/­gdb.1.html, mais je n'arrive pas à trouver mes erreurs.

J'ai essayé un programme qui marche, je lui ai rajouté exprès une erreur qui fait un segmentation fault...mais impossible de trouver cette erreur avec gdb...
pourriez vous m'expliquer comment faire ??

voici mon programme:
"include<stdio.h>
int main (void)
{
int i;
scanf("%d",i); // il manque le &, c'est ce qui fait le segmentation fault
printf("vous avez ecrit %d",i);
return0;
}


Comment trouver l'erreur avec le déboggeur ?? (quand je met run , il dit "failed to read a valid object file image from memory"...)

merci

Configuration: Windows Vista
Internet Explorer 7.0

Meilleures réponses pour « débogger avec gdb » dans :
[Langage C] C/C++ Erreur de segmentation VoirQu'est ce qu'une erreur de segmentation Vous êtes en train de développer une application sous Linux en C/C++. Tout va bien, ça compile, les oiseaux chantent. Donc vous lancez votre application pour la tester. Et vous obtenez l'un de ces deux...

1

jisisv, le 30 mai 2008 à 21:17:46

As tu compilé ton source avec l'option -g pour les informations de débugages ? (-g)

[johand@horus]~/src/c/gdb $cat sigseg.c
#include<stdio.h>
int main (void)
{
int i;
scanf("%d", i); // il manque le &, c'est ce qui fait le segmentation fault
printf("vous avez ecrit %d", i);
return 0;
}

[johand@horus]~/src/c/gdb $gcc -g -o sigseg sigseg.c
[johand@horus]~/src/c/gdb $gdb ./sigseg
GNU gdb 6.7.1-debian
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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 "i486-linux-gnu"...
Using host libthread_db library "/lib/i686/cmov/libthread_db.so.1".
(gdb) run
Starting program: /home/johand/src/c/gdb/sigseg
123456

Program received signal SIGSEGV, Segmentation fault.
0xb7dfaebb in _IO_vfscanf () from /lib/i686/cmov/libc.so.6
(gdb) help stack
Examining the stack.
The stack is made up of stack frames.  Gdb assigns numbers to stack frames
counting from zero for the innermost (currently executing) frame.

At any time gdb identifies one frame as the "selected" frame.
Variable lookups are done with respect to the selected frame.
When the program being debugged stops, gdb selects the innermost frame.
The commands below can be used to select other frames by number or address.

List of commands:

backtrace -- Print backtrace of all stack frames
bt -- Print backtrace of all stack frames
down -- Select and print stack frame called by this one
frame -- Select and print a stack frame
return -- Make selected stack frame return to its caller
select-frame -- Select a stack frame without printing anything
up -- Select and print stack frame that called this one

Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.

(gdb) bt
#0  0xb7dfaebb in _IO_vfscanf () from /lib/i686/cmov/libc.so.6
#1  0xb7e04b9b in scanf () from /lib/i686/cmov/libc.so.6
#2  0x080483c8 in main () at sigseg.c:5
(gdb) l
1       #include<stdio.h>
2       int main (void)
3       {
4       int i;
5       scanf("%d", i); // il manque le &, c'est ce qui fait le segmentation fault
6       printf("vous avez ecrit %d", i);
7       return 0;
8       }
9
(gdb)



'btt' pour examiner le stack....
Je ne suis pas spécialiste gdb , mais en utisant l'aide en lgne et ses neurones, on peut prendre la main sur l'outil.
Gates gave you the windows.
GNU gave us the whole house.(Alexandrin)

Répondre à jisisv

2

pibarze, le 30 mai 2008 à 21:35:21

Dans gdb :
b main -> pour poser un breakpoint sur la fonction "main"
run -> pour démarrer le programme. L'exécution s'arrêtera au début de la fonction "main"
n -> pour avancer d'une ligne
Taper une touche et entrée puis avec un peu de chance "une segmentation fault" dont tu connaitras la provenance.

Répondre à pibarze

3

 kilian, le 30 mai 2008 à 22:48:55

Une petite astuce:
http://www.commentcamarche.net/faq/sujet 4791 langage c c c erreur de segmentation

La commande bt est aussi très utile, elle permet de faire un backtrace: c'est à dire voir les appels de fonctions empilées jusqu'à l'aboutissement de l'erreur.

Répondre à kilian