Skip to content

venkateshchandru/gdbdocument

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 

Repository files navigation

GCC and GDB Command Reference

This repository provides a comprehensive reference for using GCC (GNU Compiler Collection) and GDB (GNU Debugger) for compiling and debugging C programs.


Table of Contents


GCC (GNU Compiler Collection)

Basic Commands

  1. Compile a C program:
    gcc main.c -o program
    
  2. Enable debugging information:
    gcc -g main.c -o program
    
  3. Enable warnings during compilation:
    gcc -Wall main.c -o program
    
  4. Treat warnings as errors:
    gcc -Werror main.c -o program
    

GDB (GNU Debugger)

Basic Commands

  1. Start GDB with a Text User Interface (TUI):
    gdb --tui
    
  2. View source code:
    list
    
  3. View the type of a variable:
    whatis <variable>
    
  4. View members of a struct variable:
    ptype <variable>
    
  5. Dereference a pointer:
    * <pointer_name>
    
  6. Set a temporary breakpoint:
    tbreak
    
  7. List all breakpoints:
    info breakpoints
    
  8. Delete a breakpoint:
    delete <breakpoint_num>
    
  9. Redirect GDB output:
    tty <path>
    
  10. Set a conditional breakpoint:
    b <file_name>:<line_number> if <condition>
    
  11. Jump directly to execution:
    advance <function_name or filename:line_num>
    
  12. Execute until a specific line:
    until <function_name or filename:line_num>
    
  13. Watch a variable for changes:
    watch <variable_name>
    
  14. Enable/disable a breakpoint:
    enable <breakpoint_num>
    disable <breakpoint_num>
    
  15. Save breakpoints to a file:
    save breakpoints <file_name.txt>
    
  16. Load breakpoints from a file:
    source <file_name.txt>
    
  17. Display changes in a variable:
    display <variable_name>
    
  18. View list of displayed variables:
    info display
    
  19. Remove a variable from display list:
    undisplay <display_num>
    

Program Stack Commands

  1. Backtrace to see the call stack:
    bt
    
  2. Finish the current stack frame:
    finish
    
  3. Navigate up/down in the call stack:
    up
    down
    
    

Debugging Issues

  1. Segmentation fault: Occurs when a program accesses memory it does not own.

  2. Stack overflow: Happens when there is excessive memory usage in the call stack.

GDB Help

Get help for a specific command: ```bash help

Debugging Techniques

  1. Delta debugging: Use multiple breakpoints to isolate errors.

  2. Calling functions within GDB:

    call <function_name(arg...)>
    
  3. Attaching GDB to a running process:

    #Get process ID:
    ps aux | grep <name>
    
    #Attach to a process:
    gdb -p <process_id>
    
    #Detach from a process:
    detach
    
  4. Handling core dumps:

    #Set unlimited core dump size:
     sudo ulimit -c unlimited
    
    #Check core dump size:
     ulimit -c
    
    #Install necessary packages
     sudo apt install systemd-coredump
    
    #View core dump files
     coredumpctl dump
     coredumpctl gdb
    

Breakpoints and Commands

  1. Set commands to execute automatically at breakpoints:
    commands <breakpoint_num>
    print <variable>
    bt
    end
    

GDB Scripts

  1. Create a script for GDB commands:

    File name: .gdbinit Add commands inside the file.

Advanced Commands

  1. Set a variable value:

    set var <variable_name> = <value>
    
  2. Reverse Debugging:

    target record-full
    reverse-next
    
  3. Debugging multithreaded programs:

    1. View Threads:
      info threads
      
    2. Switch to a specific thread:
      thread <thread_num>

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published