How GDB debugs a program without a symbol table (compiled without the-G option)

Source: Internet
Author: User

/*********************************************************************
* Author:samson
* date:01/30/2015
* Test Platform:
* 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.c
To 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.

GDB Debug Program Usage http://www.linuxidc.com/Linux/2013-06/86044.htm

Gdb+gdbserver non-source debugging Android dynamic link library tips http://www.linuxidc.com/Linux/2013-06/85936.htm

Use HELLO-GL2 to establish NDK-GDB environment (source code and non-source debugging environment) http://www.linuxidc.com/Linux/2013-06/85935.htm

Use GDB to debug printf source code on Ubuntu http://www.linuxidc.com/Linux/2013-03/80346.htm

Debug loadable modules with GDB under Linux http://www.linuxidc.com/Linux/2013-01/77969.htm

Use GDB breakpoint Go program under Ubuntu http://www.linuxidc.com/Linux/2012-06/62941.htm

Use the GDB command-line debugger to debug a C + + program http://www.linuxidc.com/Linux/2014-11/109845.htm

detailed description of GDB : please click here
GDB 's : please click here

This article permanently updates the link address : http://www.linuxidc.com/Linux/2015-02/113324.htm

How GDB debugs a program without a symbol table (compiled without the-G option)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.