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)
Answer from ikh on Stack Overflow
๐ŸŒ
OnlineGDB
onlinegdb.com
GDB online Debugger | Compiler - Code, Compile, Run, Debug online C, C++
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS Code, Compile, Run and Debug online from anywhere in world.
Online C
OnlineGDB is online IDE with c compiler. Quick and easy way to compile c program online. It supports gcc compiler for c.
Online C++
OnlineGDB is online IDE with C++ compiler. Quick and easy way to compiler c++ program online. It supports g++ compiler for c++.
Java
OnlineGDB is online IDE with java compiler. Quick and easy way to run java program online.
Python
OnlineGDB is online IDE with python compiler. Quick and easy way to compile python program online. It supports python3.
Discussions

debugging - How to debug gdb with itself - Stack Overflow
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... More on stackoverflow.com
๐ŸŒ stackoverflow.com
A quick intro to gdb.
This Red Hat Tutorial learns you the finer points. More on reddit.com
๐ŸŒ r/C_Programming
4
40
May 28, 2023
debugging - How to step-into, step-over and step-out with GDB? - Unix & Linux Stack Exchange
I typed help while I was in the GDB but didn't find anything about step-into, step-over and step-out. I put a breakpoint in an Assembly program in _start (break _start). Afterwards I typed next and... More on unix.stackexchange.com
๐ŸŒ unix.stackexchange.com
July 24, 2016
Is GDB used in the industry?
I have never worked on firmware where we didnโ€™t take the time to set up a debugger on it. More on reddit.com
๐ŸŒ r/embedded
37
45
May 30, 2024
๐ŸŒ
Kauffman77
kauffman77.github.io โ€บ tutorials โ€บ gdb.html
Quick Guide to gdb: The GNU Debugger
April 4, 2025 - Failure: Double debugger burger, order up! * Score: 0 / 50 pts * [Inferior 1 (process 27727) exited normally] # gdb indicates program ended (gdb) break phase01 # set a breakpoint to stop at function phase01() Breakpoint 1 at 0x55555555551a: file puzzlebox.c, line 220.
Top answer
1 of 3
8

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)
2 of 3
4

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.

Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ a quick intro to gdb.
r/C_Programming on Reddit: A quick intro to gdb.
May 28, 2023 -

GDB Quick Guide

I found out I didn't need cgdb by the way, it was better to just gdb -tui on Debian. Less modem noise in the form of uninterpreted escape sequences too.

So it paid up to do gdb -help.

The guide doesn't mention watch or watch -l but it points to Debugging with GDB where you can find everything.

It is a good quick start, with a couple of examples.

๐ŸŒ
GNU Project
sourceware.org โ€บ gdb
GDB: The GNU Project Debugger
GDB, the GNU Project debugger, allows you to see what is going on `inside' another program while it executes -- or what another program was doing at the moment it crashed.
๐ŸŒ
Florida State University
cs.fsu.edu โ€บ ~myers โ€บ cop3330 โ€บ debug โ€บ debugger.html
Debugging with GDB
Once you have compiled an executable file that includes a debugging symbol table, you debug it by opening it in gdb.
๐ŸŒ
GNU
gnu.org โ€บ software โ€บ gdb โ€บ gdb.html
GNU Debugger (GDB)
GDB, the GNU Project debugger, allows you to see what is going on `inside' another program while it executes -- or what another program was doing at the moment it crashed.
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ GNU_Debugger
GNU Debugger - Wikipedia
1 month ago - The GNU Debugger (GDB) is a portable debugger that runs on many Unix-like systems and works for many programming languages, including Ada, Assembly, C, C++, D, Fortran, Haskell, Go, Objective-C, OpenCL C, Modula-2, Pascal, Rust, and partially others.
๐ŸŒ
Reddit
reddit.com โ€บ r/embedded โ€บ is gdb used in the industry?
r/embedded on Reddit: Is GDB used in the industry?
May 30, 2024 -

During my internship, we only used a logic analyzer and CLI for debugging. Is the GDB used in the industry for large-scale firmware projects? if so, how often?

Top answer
1 of 19
114
I have never worked on firmware where we didnโ€™t take the time to set up a debugger on it.
2 of 19
41
Yes, extensively. If you're firing up an embedded debugger, chances are more likely than not that it is at its core either GDB, or something using GDB type interfaces. However, chances are at least as likely as not that what you're looking at is a GUI front end interface to GDB (though knowing how to use it directly is important). Typically in the context of a flash-based MCU what you are dealing with is actually a chain of: hardware debug capabilities built into the target processor - breakpoints, single step, memory access (occasionally this is a software monitor instead) debug interface hardware - JTAG extensions, SWD, BDM, sometimes just a serial or even network connection Host software speaking the GDB server protocol (or occasionally that is the monitor routine on the target itself talking via serial or network - or sometimes this is software on the debug probe for example Blackmagic) GDB itself GUI front end for GDB - either its own program or part of an IDE such as Eclipse Not all debugging of course uses a breakpoint type debugger - it gets complicated anytime code is interacting with other things that have their own expectations of timing - including an RTOS kernel not setup for careful coordination with a debugger. (You can typically get away with setting a breakpoint at a spot of interest and then looking around effectively post-mortem, but being able to continue operation from there may not be workable where those expectations have been broken) That's why you see logic analyzers, serial printf type things, etc also used. With experience you'll anticipate the right tool for your need - a need which can vary as you address different issues in the same project. Not having access to breakpoint debugging is a limitation. But often it really is not the best first approach.
๐ŸŒ
Reddit
reddit.com โ€บ r/embedded โ€บ usage of gdb over command line
r/embedded on Reddit: Usage of GDB over command line
August 31, 2022 -

I have recently joined a company as an embedded SW engineer and almost everyone is using GDB over command line for debugging.

I have been debugging only using built-in graphical debuggers within the IDE. So this is something completely new for me and I can't really appreciate advantage of the command line debugging.

Is it worth getting familiar with it? Will I appreciate it once I know the commands and the workflow? I work mainly with C, Assembly, C++ and Python (for automatic testing only).

Is the command line GDB standard for other companies as well? We are a semiconductor company btw.

Top answer
1 of 5
11
Very use case and preference specific. Have you asked the engineers in your company why they're using cli gdb? Maybe there's something within the company that makes using cli gdb preferred over any IDE or maybe it's just along the lines of "it's what I've learned one time" or "not having to get used to all different IDE debugging interfaces". I do use cli gdb (tui mode specifically) and most of my cases of me starting the debugger is a test failure which are ran in cli so going from to gdb --tui --args is extremely easy. I've looked around for frontends that can be easily started in cli but they're either very slow (starting up or running) or not as cli invoke friendly (i.e., having to go into a configuration file to augment the program with args to be debugged before being able to start). Again, use case and preference specific so you do you if you want to IDE debug all the way. Good luck!
2 of 5
8
You might not always have an IDE available, or might not want to install a full IDE just to debug/update some legacy code that has been in production for 30 years. I am moving to using GDB for anything that isn't already supported by my Lauterbach setup. It really minimizes the amount of bloat I have to install and set up on every new machine I use. As someone else mentioned, -tui is a gamechanger. I am not sure how long I'd stick with CL debugging without it. If you are working with Cortex devices, you could try VS Code with the Cortex-Debug plugin. I believe that it can utilize SVD files to give you core and peripheral definitions.
๐ŸŒ
Baylor
cs.baylor.edu โ€บ ~donahoo โ€บ tools โ€บ gdb โ€บ tutorial.html
How to Debug Using GDB
The distinction doesn't matter here since there are no functions. You may use the shortest, unambigious spelling of a GDB command to save some typing. Here we use n and s instead of next and step, respectively. If the command is simply a repeat of the previous command, you can just hit return, ...