Skip to content

04. Debugging

Ashish Bhattarai edited this page Apr 29, 2021 · 6 revisions

Sample Project

In the following demostration, the Michi project that comes in the C-Programming-Guide. You can use git to clone the project, or you can download the project from the link. You can either open Michi.sln using visual studio or build using CLANG (build.bat file generates PDB automatically if you have CLANG compiler in your path)

Start Debugging and Stepping

*Note: The pictures how alternative way of doing the same thing using GUI

  1. Press [F5] to run the program, then exit the program. Start Debugging
  2. Press [F10] or [F11] to start debugging the program Debug Stepping
  3. Press [F10] to step over, and [F11] to step into each source code line
  4. Notice the difference between Step Over and Step Into (try step over and into in a line with a function call)

Breakpoints

  • Breakpoints are the points that you set in your program on which the debugger breaks (stops) the program.
  • To set a breakpoint, goto the line you want to set a breakpoint and press [F9], to unset the breakpoint press [F9] again.
  • You can also click on the column to the right of line number to set or unset the breakpoints. When you set a breakpoint, a red circle will be shown as an indicator Breakpoint
  • After you set the breakpoint, press [F5] to run the program, now you can step into and step over again as before
  • Pressing [Shift+F10] lets you step out of function (it executes the function and gets you out of function). This is handy when you don't want to step over the rest of the lines in the current function.
  • To put a breakpoint on a function, you can directly press [Ctrl+B] and type the name of the funtion, or you can go in the Menu:Debug->New Breakpoint->Function Breakpoint
  • To view all the breakpoints, press [Ctrl+Alt+B], or in the Menu:Debug->Windows->Breakpoints
  • To remove all the breakpoints from the project, press [Ctrl+Shift+F9], or in the Menu:Debug->Delete All Breakpoints
  • You can set or unset the breakpoints in the while you are in mid debuging session

Watch Window

  • Once you are in debugging session, you can get access to the Watch window:Debug->Windows->Watch
  • You can give name of variables in the watch windows and video it's values
  • Combine this with stepping in and over, you get to see the values in each step of the program
  • You can also have constant expression results in the watch window (i.e. using it as calculator)
  • You can right click on the watch windows and select hexa-decimal display which is useful sometimes
  • You can put pointer variable with number separate by comma (ptr, 12) and the watch window will show the number of consecutive address values Watch Window

Callstack

  • Callstack is the windows that shows you the stack of function calls or stack frames. You can access this while debugging.
  • Press [Ctrl+Alt+C] to view callstack windows or in Menu:Debug->Windows->Callstack
  • Callstack is useful to check the previous function call and their values, to get some understanding into the callstack window, step into some functions and view the callstack
  • To check the values of previous stack frame, you can double click the function frame in callstack windows, and type the variable names in Watch window
  • Combining callstack with watch window is a very handy and powerful debugging tool, once you get familiar with it, you'll be using it all the time for debugging. Callstack

Memory

  • Memory window let's you view memory of the program
  • You can type the address directly or use address of operator on the variable to view the value in the memory
  • To view Memory window: Debug->Windows->Memory Memory

Conditional Breakpoints

  • Conditional breakpoints are the breakpoints with condition. Basically, it lets you break on certain line if some condition satisfies.
  • In order to set conditional breakpoints, click the red breakpoint indicator, and click on Conditions.... Alternatively, you can press Alt+F9, C.
  • Give the condition and click on Close button
  • Now you can start the debugger Conditional Breakpoint

Data Breakpoints

  • Data breakpoints are breakpoints set on data unlike setting the breakpoints on code, well that's obvious by the name.
  • The breakpoint is triggered when the value of the provided address is changed.
  • To set breakpoint (note that you can only set data breakpoint while debugging session is going on), first set a normal breakpoint and then Debug->New Breakpoint->Data Breakpoint
  • Now give the address of the variable (it should be visible in the scope that the program is halted)
  • Fress [F5] to continue execution, the problem will be halted automatically when the given address is changed Data Breakpoint Setting Data Breakpoint

Disassembly

  • Disassemly lets you see the assembly code generated by the compiler. This is especially useful when you are trying to check what the optimized assembly code is generated.
  • To view disassembly, set a breakpoint in the place where you want to see the disassembly and run the debugger, after the breakpoint is hit, right click and then click on Go to Disassembly. Disassembly

Reference for in-depth learning

https://docs.microsoft.com/en-us/visualstudio/debugger/navigating-through-code-with-the-debugger?view=vs-2019

Clone this wiki locally