Completed: / exercises

Using IDEs Debug mode

Code Blocks

When you debug a program using codeblocks and the program crashes you can check "Debug" → "Debugging windows" → "Call stack" and check in which line your sofware crashes
Please, use breakpoints.

GDB online Debugger

When you are debugging, and your program crashes it will provide information on which line it crashes and why.
Please, use breakpoints

Using gdb from command line

If you are using Windows you have different options to install gcc and gdb

Compiling Code for GDB

To use gdb to debug a program, you need to compile your code with debugging information. You can do this by adding the -g flag when you compile your code with gcc:
gcc -g -o my_program my_program.c

Basic GDB Usage

  1. Start GDB:
Start gdb with your compiled program as an argument:
   gdb my_program
  1. Set Breakpoints:
You can set breakpoints at specific lines of your code to pause execution:
   break [filename:]linenumber
For example, to set a breakpoint at line 10, you would type:
   break 10
  1. Run the Program:
Start your program within gdb:
   run [program arguments]
  1. Inspecting Variables:
When your program hits a breakpoint or crashes, you can inspect variable values:
print variable_name
  1. Stepping Through the Program:
- Next: Move to the next line of code in the current function (doesn’t step into functions):
next
- Step: Move to the next line of code, stepping into functions if they are called:
step
  1. Viewing the Call Stack:
When your program is paused, you can view the call stack using:
backtrace
  1. Exiting GDB:
You can exit gdb anytime using:
quit

Additional Commands:

- Continue: Continue program execution until the next breakpoint:
  continue
- Clear: Remove breakpoints:
  clear linenumber
- List: Show the source code around the current line:
  list
- Info Breakpoints: View all set breakpoints:
  info breakpoints
- Help: Get help on gdb commands:
  help [command_name]

Example Session:

$ gdb my_program
(gdb) break 10
(gdb) run arg1 arg2
(gdb) print my_variable
(gdb) next
(gdb) quit

Tips:

- Be sure to explore gdb's help command to find more features and functionalities.
- Check the manual pages (man gdb) or the official GDB documentationfor more detailed information
?