Skip to content
William Cazorla edited this page Feb 26, 2022 · 7 revisions

debug-tools

Getting aquainted with gdb

Main gdb commands

  • r : run or relaunch execution.

Navigation

  • n : step over the next line
  • s : step into the next line, dive into a function call.
  • c : continue execution until next breakpoint or regular interruption.

Breakpoints

  • b : set a breakpoint to the line you are on.
  • b [func_name] : set a breakpoint to that function first line.
  • b [source_file_name:line_number] : set a breakpoint to that source file line.

Visualisation

  • p [variable] : prints a variable once. The C syntax fully applies so you can cast and dereference those, as well as try some dereferencing syntax. Great to fix, understand and test your pointers on pointers !
  • disp [variable] : keeps printing a variable throughout a function exec, even when you come back to this function.
  • bt : Backtrace, shows and numbers the stack frames. Allowing you to navigate in or out function calls / down or up the stack. Makes fixing segfaults a breeze !
  • f [stack number] : allows you to dive into any stack frame / function call.

How to use gdb to debug child process forks

  • Launch gdb with your program : gdb ./minishell
  • set follow-fork-mode child
  • Set a breakpoint in a function inside the child : b child_proc
  • Open the text user interface : tui e
    • Tui display may get scrambled when your program outputs on stdout, don't panic and just refresh your display by scrolling inside the code preview window a bit.
  • Run r
  • When gdb meets a fork its sends a SIGTRAP signal, continue to resume execution : c