YouTip LogoYouTip

Linux Comm Gdb

[![Image 1: Linux Command Manual](#) Linux Command Manual](#) * * * GDB (GNU Debugger) is the most commonly used program debugging tool on Linux systems. It helps developers: * Track program execution flow * Set breakpoints to pause program execution * View and modify variable values * Analyze program crash reasons * Examine function call stacks GDB supports multiple programming languages, including C, C++, Objective-C, Fortran, Ada, etc., making it an indispensable debugging tool for Linux developers. * * * ## Install gdb On most Linux distributions, gdb can be easily installed via the package manager: ## Example # Ubuntu/Debian sudo apt-get install gdb # CentOS/RHEL sudo yum install gdb # Fedora sudo dnf install gdb # Arch Linux sudo pacman -S gdb After installation, verify the installation was successful: gdb --version * * * ## Compile a Debuggable Program To debug a program with gdb, you need to add the `-g` option during compilation to generate debug information: gcc -g program.c -o program The `-g` option embeds source code information into the executable, allowing gdb to correlate machine instructions with the source code. * * * ## Basic gdb Commands ### Start and Exit gdb ## Example # Start gdb and load a program gdb ./program # Start gdb and attach to a running process gdb-p PID # Exit gdb (gdb) quit # or shorthand (gdb) q ### Run Program ## Example # Run the program (gdb) run # or shorthand (gdb) r # Run with arguments (gdb) run arg1 arg2 ### Breakpoint Management ## Example # Set breakpoint at specified line (gdb)break 10 # or shorthand (gdb) b 10 # Set breakpoint at function entry (gdb)break main (gdb)break function_name # View all breakpoints (gdb) info breakpoints # Delete breakpoint (gdb) delete 1# Delete breakpoint number 1 (gdb) delete # Delete all breakpoints ### Program Execution Control ## Example # Continue execution until next breakpoint (gdb)continue # or shorthand (gdb) c # Step execution (step into function) (gdb) step # or shorthand (gdb) s # Step execution (step over function) (gdb) next # or shorthand (gdb) n # Execute until current function returns (gdb) finish ### View Code ## Example # View code near current line (gdb) list # or shorthand (gdb) l # View code near specified line (gdb) list 15 # View code of specified function (gdb) list main ### Inspect Variables and Memory ## Example # Print variable value (gdb) print variable_name # or shorthand (gdb) p variable_name # Modify variable value (gdb) print variable_name = new_value # View variable type (gdb) ptype variable_name # View memory contents (gdb) x/10xw &variable # View 10 words in hexadecimal ### Call Stack Analysis ## Example # View call stack (gdb) backtrace # or shorthand (gdb) bt # Switch to specified stack frame (gdb) frame 2 # or shorthand (gdb) f 2 * * * ## Advanced Debugging Techniques ### Conditional Breakpoints ## Example # Trigger breakpoint when i equals 5 (gdb)break 10 if i == 5 ### Watchpoints ## Example # Pause when variable is modified (gdb)watch variable_name # Pause when variable is read (gdb) rwatch variable_name # Pause when variable is read or modified (gdb) awatch variable_name ### Multi-threaded Debugging ## Example # View all threads (gdb) info threads # Switch to specified thread (gdb) thread 2 # Only allow current thread to execute (gdb)set scheduler-locking on ### Debug Core Dump Files ## Example # Load core dump file gdb ./program core # View call stack at crash (gdb) bt * * * ## gdb Graphical Interface gdb also supports graphical interface mode, which can be started with the `-tui` option: gdb -tui ./program Or switch during use: ## Example (gdb) layout src # Show source code window (gdb) layout asm # Show assembly window (gdb) layout regs # Show register window (gdb) layout split# Show both source and assembly * * * ## Useful gdb Configuration You can add common configurations to the `~/.gdbinit` file: ## Example # Display pretty printing set print pretty on # Set history size set history save on set history size 1000 # Custom command define printarray set$i = 0 while$i< $arg0 printf"array[%d] = %dn", $i, $arg1[$i] set$i = $i + 1 end end * * * ## Common Problem Solving ### Cannot See Source Code During Debugging 1. Make sure the `-g` option was used during compilation 2. Check if the source code is in the same location as during compilation 3. Use the `directory` command to add source code path: (gdb) directory /path/to/source ### Debugging Optimized Code Optimization may change the order of code execution, making debugging difficult. It is recommended to disable optimization during debugging: gcc -O0 -g program.c -o program * * * ## Summary gdb is a powerful debugging tool on Linux. Mastering it can significantly improve debugging efficiency. This article introduced the basic usage and common commands of gdb. Readers are advised to consolidate this knowledge through practical debugging exercises. As you gain experience, you will find that gdb can help you solve various complex debugging problems. * * Linux Command Manual](#)
← Linux Comm LddLinux Comm Resize2Fs β†’