printf
"-uri si sa depistam linia unde "crapa". Aplicand aceasta metoda o sa gasim ultima
linie care produce output, astfel putem depista ultima linie care se executa si produce acest eveniment!gdb
(The GNU Debugger) gasim aceasta linie mai rapid.
test.c:
#include <stdio.h>
|
gcc -o test test.c
), rulam programul (./test
), iar dupa citirea lui n
, obtinem:
Dati n: 2
Segmentation fault
Pentru a depista problema:
bash
ca si interpretor de comenzi, utilizind: ulimit -c unlimited
gcc -g -o test test.c
./test
Dati n: 2
Segmentation fault (core dumped)
Observam ca spre deosebire de prima rulare sistemul a produs un fisier core:
ls -1 core*
produce:core.30881
30881
fiind pid-ul progamului nostru.rm core.*
).
gdb test core.30881
gdb nume_executabil nume_fisier_core
)
GNU gdb Fedora (6.8-32.fc10)
Copyright (C) 2008 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 "i386-redhat-linux-gnu"...
(no debugging symbols found)
warning: Can't read pathname for load map: Input/output error.
Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
(no debugging symbols found)
Core was generated by `./test'.
Program terminated with signal 11, Segmentation fault.
[New process 30881]
#0 0x00c021d0 in _IO_vfscanf_internal () from /lib/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.9-3.i686
(gdb)
backtrace
(sau prescurtarea bt
) pentru a vizualiza stiva de executie:
(gdb) bt
#0 0x00c021d0 in _IO_vfscanf_internal () from /lib/libc.so.6
#1 0x00c0d438 in scanf () from /lib/libc.so.6
#2 0x08048424 in main () at test.c:5
2
.frame nr_frame
(gdb) frame 2
#2 0x08048424 in main () at test.c:5
5 scanf("%d",n);
(gdb)
5
s-a produs citirea variabilei n
, in acest caz este evident: n
nu a fost citit corespunzator.print nume_variabila
:
(gdb) print n
$1 = 12175056
(gdb)
gdb
folosim comanda: quit
sau q
(gdb) q
Folosind aceasta metoda putem depista relativ usor unde s-a produs evenimentul "Segmentation fault".
Pentru mai multe informatii despre gdb
folositi man gdb
sau tutorialul de aici.