Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug gdb with itself

Tags:

debugging

gdb

I have gdb installed on my machine. Today I have compiled another version of gdb that is running fine. Now I want to debug this new gdb using my older gdb. Please guide me in this regard. How can I know that how gdb reads symbols from the provided executable, how it inserts break points, handles function calls and other things.

Thanks.

like image 261
sbunny Avatar asked Aug 22 '14 11:08

sbunny


People also ask

How do I set a breakpoint in GDB?

Setting breakpoints A breakpoint is like a stop sign in your code -- whenever gdb gets to a breakpoint it halts execution of your program and allows you to examine it. To set breakpoints, type "break [filename]:[linenumber]".

How do I invoke GDB?

Invoking GDB. Invoke GDB by running the program gdb . Once started, GDB reads commands from the terminal until you tell it to exit. You can also run gdb with a variety of arguments and options, to specify more of your debugging environment at the outset.

What is batch mode in GDB?

Batch mode refers to batch processing, which means automated processing, without human intervention. According to GDB documentation : Batch mode disables pagination, sets unlimited terminal width and height see Screen Size, and acts as if set confirm off were in effect (see Messages/Warnings).


2 Answers

Think easily; when you want to debug some program, you probably compile it with -g or -ggdb and run gdb, don't you?

  1. Download gdb source.

  2. Compile it with -ggdb

    ./configure --prefix=<where-to-install>
    make CFLAGS="-ggdb" CXXFLAGS="-ggdb"
    make install
    
  3. Debug it!

    gdb <where-to-install>/bin/gdb
    

I've never tried it (and never thought it), but it may work. (And it looks very interesting; I'm about to try it!)


Um, I've just tested it in cygwin, and figure out the problem that the debugger gdb's output and the debuggee gdb's output are mixed; I solved it by using gdbserver to debug.

# On terminal 1..
$ gdbserver localhost:1234 gdb-gdb/prefix/bin/gdb
Process gdb-gdb/prefix/bin/gdb created; pid = 972
Listening on port 1234
Remote debugging from host 127.0.0.1
GNU gdb (GDB) 7.7.1
Copyright (C) 2014 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 "i686-pc-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) q

Child exited with status 0
GDBserver exiting

and

# On terminal 2..
$ gdb gdb-gdb/prefix/bin/gdb
GNU gdb (GDB) 7.8
Copyright (C) 2014 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 "i686-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from gdb-gdb/prefix/bin/gdb...done.
(gdb) target remote localhost:1234
Remote debugging using localhost:1234
0x7c93120f in ntdll!DbgBreakPoint ()
   from /cygdrive/c/WINDOWS/system32/ntdll.dll
(gdb) c
Continuing.
[Inferior 1 (Remote target) exited normally]
(gdb)
like image 142
ikh Avatar answered Sep 28 '22 08:09

ikh


Once the first gdb starts running after taking the new gdb as an input file it will become paused after showing the info message. At this point you can put a break point on the function of new gdb which you want to execute.

e.g break insert_breakpoints // the function used to insert break points.

Now execute: run This will start the execution of the new loaded gdb. Use file command to provide any executable HelloWorld.c comiled with -g option (for building debugging symbols) to the new gdb.

Now insert break point any where in the HelloWorld executable i.e break main

This break command will call the insert_breakpoints function of gdb used for the insertion of breakpoints at which we have previously placed a break point.

Now you can use backtrace or other commands for examining the function calls and other stuff like that.

Hope that will solve your problem.

like image 39
saqlain raza Avatar answered Sep 28 '22 07:09

saqlain raza