34

I have looked for documentation on this and found nothing. I have MinGW installed and it works great. I just don't know how to use the debugger.

Given some simple code, say in a file called "mycode.cpp":

int main()
{
    int temp = 0;

    for (int i = 0; i < 5; ++i)
        temp += i;

    return 0;
}

...how would I debug this. What are the commands that I use to debug code with MinGW and GDB in windows? Can I step through the code via the command line like in Visual Studio? If so what commands do I use to do that?

Are there any tutorials for using GDB out there? I couldn't find any, but if anyone could direct me to one that would be great too. I'm tired of writing tons of std::cout statements to debug complex code.

Mike Webb
  • 8,855
  • 18
  • 78
  • 111

2 Answers2

51

The first step is to compile your program with -g to include debugging information within the executable:

g++ -g -o myprog.exe mycode.cpp

Then the program can be loaded into gdb:

gdb myprog.exe

A few commands to get you started:

  • break main will cause the debugger to break when main is called. You can also break on lines of code with break FILENAME:LINENO. For example, break mycode.cpp:4 breaks execution whenever the program reaches line 4 of mycode.cpp.
  • start starts the program. In your case, you need to set breakpoints before starting the program because it exits quickly.

At a breakpoint:

  • print VARNAME. That's how you print values of variables, whether local, static, or global. For example, at the for loop, you can type print temp to print out the value of the temp variable.
  • step This is equivalent to "step into".
  • next or adv +1 Advance to the next line (like "step over"). You can also advance to a specific line of a specific file with, for example, adv mycode.cpp:8.
  • bt Print a backtrace. This is a stack trace, essentially.
  • continue Exactly like a "continue" operation of a visual debugger. It causes the program execution to continue until the next break point or the program exits.

The best thing to read is the GDB users' manual.

Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
  • 2
    A few more commands you should be familiar with: `run`, `continue`, `next`, `list` and `help`. When all else fails, try using `help`. – Thomas Matthews Jan 12 '11 at 17:44
  • 1
    tried `g++ -g helloworld.c`, it generated only a.exe. Is it supposed to generate some more files to help debugging with gdb. Running `gdb a.exe` gives message:`not in executable format: File format not recognized` and starts `(gdb)` command prompt. Running `(gdb) break main` gives `No symbol table is loaded. Use the "file" command.`. Running `(gdb) start` gives the same `No symbol table is loaded. Use the "file" command.`. What wrong I am doing? – Mahesha999 Mar 10 '17 at 11:27
  • @Mahesha999 No other files are generated; the debugging information is stored within the executable. Can you try running `a.exe` directly? "not in executable format: File format not recognized" implies that `a.exe` is not an executable. – Daniel Trebbien Mar 11 '17 at 12:23
  • @Mahesha999 What target is listed in the "This GDB was configured as ..." message when you start `gdb` (if you don't see such a message, what is the output of the `show configuration` command)? Maybe `a.exe` is 64-bit and `gdb` is configured for 32-bit, or vice versa? – Daniel Trebbien Mar 11 '17 at 23:16
  • I asked related question [here](http://stackoverflow.com/questions/42717962/setting-up-vscode-for-c-c-debugging-on-window-7-with-gcc-g-and-gdb). I am using gdb, gcc, g++ from mingw distribution. **(1)** gdb is configured as "i686-pc-mingw32" **(2)** gcc.exe (x86_64-win32-seh-rev201506, mingwpy build) 4.9.2 **(3)** g++.exe (x86_64-win32-seh-rev201506, mingwpy build) 4.9.2. Also a.exe is generated from the same gcc. How do I know whether a.exe is 64bit or 32 bit? – Mahesha999 Mar 12 '17 at 19:12
  • 1
    @Mahesha999 Thank you for these details. My guess is that because your compiler is 64-bit, it is generating 64-bit binaries (unless you somehow have a cross-compiler). You can try compiling your program using the `-m32` GCC compiler option. As for checking whether an exe is 32-bit or 64-bit, there are a lot of methods listed here: https://superuser.com/q/358434/40712 Personally, I open the binary in the 32-bit and 64-bit builds of [Dependency Walker](http://dependencywalker.com) ([one of the answers](https://superuser.com/a/1088914/40712) discusses this technique). – Daniel Trebbien Mar 12 '17 at 21:51
  • `gcc -m32 helloworld.c` gave errors like [this](http://stackoverflow.com/questions/16304804). The [comment there](http://stackoverflow.com/a/18420336/1317018) explains it with `-m32` option. It asks to add `i686-w64-mingw32`/`x86_64-w64-mingw32` flags while compiling. `gcc -x86_64-w64-mingw32 helloworld.c` gives `language not recognized` error, `gcc -i686-w64-mingw32 helloworld.c` gives ` unrecognized command line option`, Am I missing some environment path variable? – Mahesha999 Mar 16 '17 at 08:39
  • @Mahesha999 If your compiler supported building 32-bit binaries in addition to 64-bit binaries, then `-m32` would be all that you need. However, as one of the answers to the question which you linked to says, the MinGW-w64 compilers appear to be single-target (that is, they only support outputting 64-bit binaries). To resolve the problem, you should either use a gdb targeting x86_64 or use gcc/g++ targeting i686 or earlier. – Daniel Trebbien Mar 20 '17 at 21:32
  • Should I install the desired gdb/gcc/g++ explicitly? I feel I have pretty much exhausted with the efforts to fix this thing. (Anyways billion thanks for your replies) I wish there is some article explaining everything about mingw, gcc, g++ that will help me do setup in one go instead of tweaking things one by one to check what works. I know there are many articles explaining what is what but I need something concise that will leave me with precise understanding so that I can get the setup done in single try. Or may be I simply lack experience to assimilate the things... – Mahesha999 Mar 21 '17 at 10:38
  • @Mahesha999 "Should I install the desired gdb/gcc/g++ explicitly?" That's what I would do. Just use matching gdb and gcc/g++; i.e. either both are 32-bit or both are 64-bit. – Daniel Trebbien Mar 21 '17 at 11:50
6

There are a few gdb guis for windows in this question windows version of the GDB frontend DDD

Although DDD hasn't been ported

Community
  • 1
  • 1
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263