Skip to content

venkateshchandru/gdbdocument

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 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):

    shortcut in GDB terminal: ctrl+x+a

    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>
      
      

GDB UPDATED COMMANDS

  1. Finish executing the current function:
    finish
    
  2. Prints the value in hexadecimal:
     print/x n
    
  3. Directly examing memory: the x command:
     x &i
    
  4. Read watchpoint for a variable:
     rwatch <varaiable>
    
  5. Read and write watchpoint for a variable:
     awatch <varaiable>
    
  6. Set logging into file:
     set logging on
    

Note GDB saves all output from this point in a text file called gdb.txt that resides in the directory in which you are running GDB

Core Dump

A “core dump” is a snapshot of memory at the instant the program crashed, typically saved in a file called “core”.

The core contains the memory contents of the process at the point of the seg-fault including its code segment, data segment, stack segment and heap segment

1. Uses of Core Dump Files

--> Core dumps allow a user to save a crash for later or off-site analysis, or comparison with other crashes.

--> For embedded computers, it may be impractical to support debugging on the computer itself, so analysis of a dump may take place on a different computer.

2. Resource Limits

Every process has various limits associated with it.

  ```bash
   $ man setrlimit

Bash offers a built-in ulimit through which we can edit these.

Core-file max-size (ulimit -c/RLIMIT_CORE) controls the maximum core file size that can be generated when the process snaps.

  ```bash
   $ ulimit -c unlimited

3. Where is my core?

The core is dumped in the current working directory of the process.

4. What do do with a core file

Core files can be examined with gdb, the GNU debugger.

It can read the crash informations, and display (among other things) the backtrace that leads to the crash.

  ```bash
   $ gdb /path/to/binary /path/to/core/file

After gdb finished to read the input and shows its prompt, execute:

  ```bash
   (gdb) backtrace

or

  ```bash
   (gdb) bt

GDB will then output the backtrace.

Dumping core from outside the program

One possibility is with gdb, if available. This will let the program running:

  ```bash
   (gdb) attach <pid>
   (gdb) generate-core-file <optional-filename>
   (gdb) detach

Another possibility is to signal the process. This will terminate it, assuming the signal is not caught by a custom signal handler:

  ```bash
   kill -s SIGABRT <pid>

Dumping core from within the program

  ```bash
   {
   /*
    * Alternative:
    *   char *p = NULL; *p = 0;
    */
     abort();
     }

System call trace

Strace is a debugging tool that monitors the system calls made by a program and the signals it receives

Basic command

  ```bash
   strace <program/command>

Basic command with statistics of each system call

  ```bash
   strace -c <program/command>

Tracing Only specific system calls

  ```bash
   strace -e trace='<system_call>' <program/command>

Attach to a Process

  ```bash
   strace -p <PID>

Library call tracing

ltrace is a command-line utility used to trace library calls made by a program. It shows calls to functions in shared libraries, such as libc, along with their arguments and return values. It is particularly useful for debugging issues related to dynamic linking or library usage.

  ```bash
   ltrace <program>

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published