Brief guide on how to use OnlineGDB debugger

If you are new to programming world, then its possible that you may not have heard of debugger. Well, in this article I will explain OnlineGDB debugger usage in brief.

  • What is Debugger?
    In general, debugger is utility that runs target program in controlled environment where you can control execution of program and see the state of program when program is paused.
    GDB is such debugger, which is used to debug C/C++ programs.
    OnlineGDB provides an interface to use GDB in cloud environment from your browser.
  • How can I control execution of program?
    We can tell debugger when to pause a program by setting breakpoints.
    To set a breakpoint, click on blank area seen on left side of line number in editor. When you click it, it should display red circle; which means breakpoint is set on that line number. Here is image of how it looks like.

Clicking here will set breakpoint on given line number

You can set breakpoint via GDB console as well, more details you can find on this article.

Once you set breakpoint, when you start program in debug mode, it will pause execution when program reaches the line where breakpoint is set.

Breakpoint on line number 12 is hit.Program paused here.

Now we can use stepping commands to execute program line by line.

stepping commands

1. continue – Resume program execution until next breakpoint is reached
2. step into – Execute program line by line stepping into function
3. step over – Execute program line by line but don’t go inside function call
4. step out – Resume program execution until current function is finished

  • What can I see when program is paused?
    You can see call stack and values of local variables of current stack shows you chain of function calls. As seen in below image, “main” function is calling function “factorial” and “factorial” is calling function “multiply”.
    call stack window

Local variables window shows you values of local variables of current function. As shown in image below, value of “fact” is 1, “i” is 1.
local variables window

Here is single image depicting usage of OnlineGDB debugger.
online gdb debugger usage

Now we have control and eye on program execution, so we can inspect program execution flow. Breakpoints, stepping commands and variables displays are basic feature of debugger. In future, I will post more features helpful for debugging.

Leave a Reply

Your email address will not be published. Required fields are marked *