How do I use the MinGW gdb debugger to debug a C++ program in Windows?
Asked Answered
C

2

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.

Canister answered 12/1, 2011 at 17:30 Comment(0)
R
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.

Risorgimento answered 12/1, 2011 at 17:40 Comment(10)
A few more commands you should be familiar with: run, continue, next, list and help. When all else fails, try using help.Schaefer
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?Worst
@Worst 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.Risorgimento
@Worst 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?Risorgimento
I asked related question here. 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?Worst
@Worst 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: superuser.com/q/358434/40712 Personally, I open the binary in the 32-bit and 64-bit builds of Dependency Walker (one of the answers discusses this technique).Risorgimento
gcc -m32 helloworld.c gave errors like this. The comment there 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?Worst
@Worst 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.Risorgimento
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...Worst
@Worst "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.Risorgimento
O
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

Orient answered 12/1, 2011 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.