/********************************************************************* * Author:samson * date:01/30/2015 * Test PL Atform: * 3.13.0-24-generic * GNU bash, 4.3.11 (1)-release * *********************************** ********************************/
Many times, the release version of the program in the compile time is not added-g this option, then if you want to debug a program, what should be done?
When you add the-G option, you can set the breakpoint by line number, function name, etc., but without the symbol table, how to set the breakpoint of the program and debug it?
This will use the disassembly and then the address of the breakpoint set to debug, the situation can be see the following example of the process:
The TEST.C code is as follows:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int m = 0, n = 9;
int k = M+n;
printf ("K is%d\n", K);
m = k + N;
printf ("M is%d\n", m);
n = m-n;
printf ("N is%d\n", N);
return 0;
}compile with a compile command line without the-G parameter:
[email protected]:~$ gcc test.cto debug a program using GDB:
[Email protected]:~$ gdb a.out
GNU gdb (GDB) 7.5.91.20130417-cvs-ubuntu
Copyright (C) Free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or later This was free software:you was 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 is configured as "I686-linux-gnu".
For bugs reporting instructions, please see:
Reading symbols from A.out ... (No debugging symbols found) ... done.
(GDB) L
No symbol table is read. Please use the "file" command.
(GDB) Disassemble main
Dump of assembler code for function main:
0x0804841c <+0>: Push%EBP
0X0804841D <+1>: mov%esp,%ebp
0x0804841f <+3>: and $0xfffffff0,%esp
0x08048422 <+6>: Sub $0x20,%esp
0x08048425 <+9>: Movl $0x0,0x14 (%ESP)
0x0804842d <+17>: Movl $0x9,0x18 (%ESP)
0x08048435 <+25>: mov 0x18 (%ESP),%eax
0x08048439 <+29>: mov 0x14 (%ESP),%edx
0x0804843d <+33>: Add%edx,%eax
0x0804843f <+35>: mov%eax,0x1c (%ESP)
0x08048443 <+39>: mov 0x1c (%ESP),%eax
0x08048447 <+43>: mov%eax,0x4 (%ESP)
0x0804844b <+47>: Movl $0x8048540, (%ESP)
0x08048452 <+54>: Call 0x80482f0 <[email protected]>
0x08048457 <+59>: mov 0x18 (%ESP),%eax
0x0804845b <+63>: mov 0x1c (%ESP),%edx
0x0804845f <+67>: Add%edx,%eax
0x08048461 <+69>: mov%eax,0x14 (%ESP)
0x08048465 <+73>: mov 0x14 (%ESP),%eax
0x08048469 <+77>: mov%eax,0x4 (%ESP)
0X0804846D <+81>: Movl $0x8048549, (%ESP)
0x08048474 <+88>: Call 0x80482f0 <[email protected]>
0x08048479 <+93>: mov 0x18 (%ESP),%eax
0x0804847d <+97>: mov 0x14 (%ESP),%edx
0x08048481 <+101>: mov%edx,%ecx
0x08048483 <+103>: Sub%eax,%ecx
0x08048485 <+105>: mov%ecx,%eax
0x08048487 <+107>: mov%eax,0x18 (%ESP)
0x0804848b <+111>: mov 0x18 (%ESP),%eax
0x0804848f <+115>: mov%eax,0x4 (%ESP)
0x08048493 <+119>: Movl $0x8048552, (%ESP)
0x0804849a <+126>: Call 0x80482f0 <[email protected]>
0x0804849f <+131>: mov $0x0,%eax
0X080484A4 <+136>: Leave
0x080484a5 <+137>: ret
End of assembler dump.
(GDB) B *0x08048452
Breakpoint 1 at 0x8048452
(GDB) B *0x08048474
Breakpoint 2 at 0x8048474
(GDB) B *0x0804849a
Breakpoint 3 at 0x804849a
(GDB) Info b
Num Type Disp Enb Address What
1 breakpoint Keep y 0x08048452 <main+54>
2 Breakpoint Keep y 0x08048474 <main+88>
3 Breakpoint Keep y 0x0804849a <main+126>
(GDB) R
Starting Program:a.out
Breakpoint 1, 0x08048452 in Main ()
(GDB) C
Continuing.
K is 9
Breakpoint 2, 0x08048474 in Main ()
(GDB) C
Continuing.
M is 18
Breakpoint 3, 0x0804849a in Main ()
(GDB) C
Continuing.
N is 9
[Inferior 1 (process 19933) exited normally]
As can be seen from the above steps, disassemble main is used to disassemble the main function and then use B *address to set the breakpoint at the address of the three printf addresses.
How GDB debugs a program without a symbol table (compiled without the-G option)