Mastering Debugging: Leveraging Debuggers for Effective Programming
Debugging is an essential skill for programmers, and using a debugger enhances this process significantly. Unlike primitive methods like inserting print statements, a debugger allows you to monitor and control program execution in real time. With the ability to execute code line by line, set breakpoints, and inspect or modify variable contents dynamically, debuggers offer deeper insights and greater control over your code. This guide explores various debugging tools like jGRASP, VC++, and GDB, showcasing how to set breakpoints, step through code, and utilize valuable commands to streamline debugging tasks.
Mastering Debugging: Leveraging Debuggers for Effective Programming
E N D
Presentation Transcript
Using a debugger • A primitive way of debugging is to insert print statements.
Using a debugger • A primitive way of debugging is to insert print statements. • That’s OK but a debugger is much more powerful. • It’s a program that watches and controls another program as it runs!
Using a debugger • A primitive way of debugging is to insert print statements. • That’s OK but a debugger is much more powerful. • It’s a program that watches and controls another program as it runs! • It allows us to execute our code one line at a time. • It allows us to set breakpoints (stop points) in our code. • We can even examine and change the contents of variables as our program runs!
Start the debugger (jGRASP) • Build -> Debug • The program then runs and stops at our first breakpoint.
step over step in step out jGRASP variables (r-click to change value) next line to be executed end debugging
Debugging with gdb (Unix/Linux) • Direct compiler to include debugging information with –g (ex. g++ -g test.cpp) • Run the debugger and indicate program to debug (ex. gdb ./a.out)
Useful gdb commands • b main set breakpoint in func main • b 10 set breakpoint at line #10 • cont continue • del break delete all breakpoints • l (ell) list source code lines • l <func> (ell) list source code lines for <func> • n next (like step over) • p <var> print contents of variable • quit end execution • run start running program • s step (like step into) • set <var>=value change var