GDB: Practical Commands and Functionalities

The GNU Debugger (GDB) is an essential tool for developers aiming to understand and fix issues in their code. With its extensive range of commands and functionalities, GDB allows for detailed inspection and control of program execution. This guide provides practical examples of GDB usage, helping developers harness its power for efficient debugging and problem-solving.

GDB Inspect Variable

GDB (GNU Debugger) is an essential tool for debugging applications. One of its primary features is the ability to inspect variables at runtime. This feature allows developers to monitor the values of variables as their program executes, making it easier to track down bugs and understand program behavior.

Example:

(gdb) print variable_name

This command displays the current value of variable_name.

GDB List

Listing source code and functions is straightforward with GDB. The list command enables developers to view specific sections of their source code directly in the debugger.

Example:

(gdb) list 10,20

This command lists lines 10 through 20 of the source code.

GDB Show Variable

The show variable command in GDB is useful for displaying the current value of a variable. This command can be used to get detailed information about the state of a variable at a particular point in execution.

Example:

(gdb) show variable my_variable

This command shows the value of my_variable.

GDB Print Variable

Printing variables in GDB provides a way to see the value stored in a variable at runtime. This is crucial for debugging and ensuring the program behaves as expected.

Example:

(gdb) print my_var

This command prints the value of my_var.

GDB Breakpoint List

Managing breakpoints is a critical aspect of debugging. The breakpoint list command in GDB helps users view all the breakpoints set during a debugging session.

Example:

(gdb) info breakpoints

This command lists all breakpoints.

GDB Show Variables

The show variables command allows developers to see a list of all variables in the current scope along with their values. This is helpful for getting a quick overview of the program's state.

Example:

(gdb) info variables

This command shows all variables.

GDB Print Variables

Similar to print variable, the print variables command is used to print the values of multiple variables at once, which can be helpful in understanding the broader state of your application.

Example:

(gdb) info locals

This command prints all local variables.

GDB List Variables

Listing all the variables within the current scope can help developers understand what data is available and its current state. GDB provides commands to list these variables efficiently.

Example:

(gdb) info locals

This command lists all local variables.

GDB Set Variable

Setting a variable's value during a debugging session can be useful for testing how changes affect program behavior. The set variable command in GDB facilitates this process.

Example:

(gdb) set variable my_var = 10

This command sets my_var to 10.

GDB Print

The print command is versatile and can be used to print expressions, variables, or the result of function calls. This is crucial for monitoring and debugging.

Example:

(gdb) print 5 + 3

This command prints the result of the expression 5 + 3.

GDB Set Var

Short for set variable, the set var command allows you to change the value of a variable during a debugging session, aiding in testing different scenarios.

Example:

(gdb) set var my_var = 42

This command sets my_var to 42.

GDB Watch

The watch command in GDB allows you to monitor a variable or an expression and break execution whenever the value changes, which is extremely useful for tracking down issues related to variable modifications.

Example:

(gdb) watch my_var

This command sets a watchpoint on my_var.

GDB Print Binary

Printing a variable in binary format can be useful for low-level debugging, especially when dealing with hardware or protocol-specific issues.

Example:

(gdb) print /t my_var

This command prints my_var in binary format.

GDB Watch Variable

Watching a variable involves setting a watchpoint that breaks execution whenever the variable's value changes. This helps track down where and when a variable is modified.

Example:

(gdb) watch my_var

This command sets a watchpoint on my_var.

Watch GDB

Watching variables or expressions in GDB helps identify unexpected changes and the code responsible for them. This is a powerful feature for real-time monitoring during debugging sessions.

Example:

(gdb) watch my_var

This command watches the my_var variable.

GDB Print Memory

Printing memory contents directly can provide insights into how data is stored and manipulated in your application. This is particularly useful for debugging memory-related issues.

Example:

(gdb) x/10x &my_var

This command prints 10 hexadecimal values starting at the address of my_var.

GDB Print Current Line

To print the source code of the current line being executed, GDB provides a straightforward command. This is useful for quick reference without switching context.

Example:

(gdb) list

This command lists the current source code line.

GDB Show Stack

The show stack command allows you to view the current call stack, which is critical for understanding the flow of execution and diagnosing issues related to function calls and recursion.

Example:

(gdb) backtrace

This command shows the call stack.

GDB Show Current Line

To display the source code of the current execution line, GDB provides the show current line command, helping you stay oriented within your code during debugging.

Example:

(gdb) list

This command shows the current line in the source code.

GDB List Breakpoints

Listing all breakpoints set during a debugging session helps manage and control the debugging process more effectively.

Example:

(gdb) info breakpoints

This command lists all breakpoints.

Remove Breakpoint GDB

Removing breakpoints is crucial for managing your debugging session. GDB makes it simple to remove breakpoints that are no longer needed.

Example:

(gdb) delete 1

This command removes breakpoint number 1.

GDB Thread List

Managing and debugging multi-threaded applications requires knowing which threads are active. GDB provides commands to list and manage threads.

Example:

(gdb) info threads

This command lists all threads.

How to Remove Breakpoint in GDB

Removing breakpoints that are no longer required helps streamline the debugging process. GDB provides clear commands to manage this.

Example:

(gdb) delete 2

This command removes breakpoint number 2.

List Breakpoints GDB

To see all breakpoints that have been set, GDB provides a command to list them, which is helpful for managing your debugging session.

Example:

(gdb) info breakpoints

This command lists all breakpoints.

GDB Pass Arguments

Passing arguments to your program during a debugging session allows you to test how different inputs affect program behavior.

Example:

(gdb) set args arg1 arg2

This command sets the program arguments to arg1 and arg2.

GDB List Threads

Listing all threads in a multi-threaded program helps you understand and debug concurrent execution.

Example:

(gdb) info threads

This command lists all threads.

GDB Show Memory

Viewing the contents of memory at specific addresses can provide insights into how data is being managed by your program.

Example:

(gdb) x/10x 0x7fffffffe000

This command shows 10 hexadecimal values starting at address 0x7fffffffe000.

Print Local Variables GDB

Printing local variables helps in understanding the current state of your program's execution within a specific function or scope.

Example:

(gdb) info locals

This command prints all local variables.

Print Memory GDB

GDB allows you to examine memory at specified addresses, which is useful for debugging low-level issues and understanding data storage.

Example:

(gdb) x/10x &my_var

This command prints 10 hexadecimal values starting at the address of my_var.

GDB Print Local Variables

Viewing local variables in GDB gives insight into the function's current state and helps in debugging issues specific to that scope.

Example:

(gdb) info locals

This command prints all local variables.

GDB Print Stack

Printing the call stack helps understand the sequence of function calls that led to the current point of execution.

Example:

(gdb) backtrace

This command prints the call stack.

Clear Breakpoint GDB

Clearing breakpoints when they are no longer needed helps streamline your debugging session.

Example:

(gdb) clear 3

This command clears breakpoint number 3.

Remove Breakpoints GDB

Managing breakpoints by removing those no longer necessary helps maintain an efficient debugging process.

Example:

(gdb) delete 3

This command removes breakpoint number 3.

GDB Print Hex

Printing values in hexadecimal format is useful for debugging applications dealing with binary data or low-level programming

.

Example:

(gdb) print /x my_var

This command prints my_var in hexadecimal.

GDB Display

The display command in GDB continuously shows the value of an expression or variable whenever the program stops, which helps in real-time monitoring.

Example:

(gdb) display my_var

This command continuously displays the value of my_var.

GDB P

The p command is a shorthand for print in GDB, allowing quick value inspections.

Example:

(gdb) p my_var

This command prints the value of my_var.

GDB List Symbols

Listing all symbols helps understand the scope of variables, functions, and other entities in your program.

Example:

(gdb) info functions

This command lists all functions.

GDB Show Source

Showing the source code in GDB helps you see the context of the current execution point.

Example:

(gdb) list

This command shows the current source code.

GDB Show Breakpoints

Viewing all breakpoints in GDB helps manage and navigate your debugging session.

Example:

(gdb) info breakpoints

This command shows all breakpoints.

Print Stack GDB

Understanding the call stack is crucial for debugging issues related to function calls and execution flow.

Example:

(gdb) backtrace

This command prints the call stack.

GDB View Stack

Viewing the stack helps trace function calls and understand the sequence leading to the current execution point.

Example:

(gdb) backtrace

This command shows the call stack.

GDB Display Memory

Examining memory contents helps in understanding data storage and debugging memory-related issues.

Example:

(gdb) x/10x &my_var

This command displays 10 hexadecimal values starting from the address of my_var.

List Breakpoints in GDB

Listing breakpoints helps manage and navigate through breakpoints set during a debugging session.

Example:

(gdb) info breakpoints

This command lists all breakpoints.

How to Remove Breakpoints in GDB

Removing unnecessary breakpoints helps streamline the debugging process.

Example:

(gdb) delete 4

This command removes breakpoint number 4.

GDB Disable Breakpoint

Disabling breakpoints allows you to temporarily stop them from pausing execution without removing them.

Example:

(gdb) disable 2

This command disables breakpoint number 2.

GDB Print in Hex

Printing variables in hexadecimal format is useful for debugging applications dealing with binary data.

Example:

(gdb) print /x my_var

This command prints my_var in hexadecimal.

Print in Hex GDB

Debugging binary data often requires viewing values in hexadecimal format.

Example:

(gdb) print /x my_var

This command prints my_var in hexadecimal.

List Threads GDB

Listing all threads in a multi-threaded program helps understand and debug concurrent execution.

Example:

(gdb) info threads

This command lists all threads.

GDB Print Register

Viewing register contents is essential for low-level debugging and understanding processor state.

Example:

(gdb) info registers

This command prints the contents of CPU registers.

Print Register GDB

Printing register values helps in debugging low-level operations and understanding the state of the processor.

Example:

(gdb) info registers

This command prints the contents of all registers.

GDB Peda

GDB Peda (Python Exploit Development Assistance for GDB) is an enhanced version of GDB with additional features for exploit development and debugging.

Example:

(gdb) peda

This command activates Peda within GDB.

GDB Display Registers

Displaying registers helps in low-level debugging by showing the current state of the processor.

Example:

(gdb) info registers

This command displays the contents of all registers.

GDB Args

Setting program arguments for a debugging session is necessary for testing different input scenarios.

Example:

(gdb) set args arg1 arg2

This command sets the program arguments to arg1 and arg2.

GDB Run with Args

Running your program with specific arguments helps test different input scenarios during debugging.

Example:

(gdb) set args arg1 arg2

This command sets and runs the program with arg1 and arg2.

GDB Show Registers

Viewing the contents of CPU registers is essential for understanding the low-level state of your program during debugging.

Example:

(gdb) info registers

This command shows all register values.

Run GDB with Args

Setting program arguments before running it in GDB helps test various input conditions.

Example:

(gdb) set args arg1 arg2

This command sets the program arguments to arg1 and arg2.

GDB Print String

Printing a string variable's value helps in debugging issues related to string handling.

Example:

(gdb) print my_string

This command prints the value of my_string.

GDB Dump Registers

Dumping register contents helps capture the processor's state, which is crucial for debugging.

Example:

(gdb) info registers

This command dumps all register values.


GDB Call Stack

Viewing the call stack helps understand the sequence of function calls leading to the current execution point.

Example:

(gdb) backtrace

This command shows the call stack.


GDB Breakpoint Condition

Setting conditional breakpoints helps pause execution only when specific conditions are met, making debugging more efficient.

Example:

(gdb) break main if x > 10

This command sets a breakpoint at main that triggers if x is greater than 10.


GDB Dump Memory

Dumping memory contents helps capture and analyze the state of data stored in memory.

Example:

(gdb) dump memory dump.bin 0x600000 0x601000

This command dumps memory from address 0x600000 to 0x601000 into a file named dump.bin.


GDB Breakpoint at Line

Setting breakpoints at specific lines in the source code allows for precise control over execution flow during debugging.

Example:

(gdb) break 42

This command sets a breakpoint at line 42.


GDB Show Code

Displaying the current source code helps you stay oriented within your code during a debugging session.

Example:

(gdb) list

This command shows the current source code.


GDB Print Registers

Viewing the contents of registers helps in low-level debugging and understanding the processor state.

Example:

(gdb) info registers

This command prints all register values.


GDB with Arguments

Running your program with arguments helps test different input scenarios during debugging.

Example:

(gdb) set args arg1 arg2

This command sets the program arguments to arg1 and arg2.


How to Set Breakpoint in GDB

Setting breakpoints allows you to pause execution at specific points, making it easier to debug and inspect program state.

Example:

(gdb) break main

This command sets a breakpoint at the main function.


Print Hex GDB

Viewing variable values in hexadecimal is useful for debugging low-level data.

Example:

(gdb) print /x my_var

This command prints my_var in hexadecimal.


GDB Conditional Breakpoint

Setting breakpoints with conditions allows for more precise control over when execution pauses, aiding in efficient debugging.

Example:

(gdb) break main if x == 10

This command sets a breakpoint at main that triggers only if x is equal to 10.


X GDB

The x command examines memory at specified addresses, which is crucial for low-level debugging.

Example:

(gdb) x/10x 0x7fffffffe000

This command examines 10 hexadecimal values starting at address 0x7fffffffe000.


GDB Examine Memory

Examining memory directly helps in understanding how data is stored and manipulated, which is crucial for debugging.

Example:

(gdb) x/10x 0x7fffffffe000

This command examines 10 hexadecimal values starting at address 0x7fffffffe000.


GDB X

The x command is used to examine memory contents, providing insights into how data is being managed.

Example:

(gdb) x/10x 0x7fffffffe000

This command examines 10 hexadecimal values starting from address 0x7fffffffe000.


How to Step into a Function in GDB

Stepping into functions allows you to debug line-by-line within function calls, gaining a deeper understanding of the program flow.

Example:

(gdb) step

This command steps into the next function call.


GDB Set Breakpoint

Setting breakpoints at specific code points allows

you to pause execution and inspect program state.

Example:

(gdb) break 25

This command sets a breakpoint at line 25.


Conditional Breakpoint GDB

Setting breakpoints with conditions allows you to control execution flow more precisely, pausing only when specific conditions are met.

Example:

(gdb) break 25 if x > 5

This command sets a breakpoint at line 25 that triggers only if x is greater than 5.


GDB Arguments

Passing arguments to your program in GDB helps test different scenarios during debugging.

Example:

(gdb) set args arg1 arg2

This command sets the program arguments to arg1 and arg2.


GDB Ignore Signal

Ignoring specific signals during debugging helps maintain control over program execution without unnecessary interruptions.

Example:

(gdb) handle SIGPIPE nostop noprint

This command tells GDB to ignore SIGPIPE.


X Command in GDB

The x command is used for examining memory, providing insights into data storage and manipulation.

Example:

(gdb) x/10x 0x7fffffffe000

This command examines 10 hexadecimal values starting at address 0x7fffffffe000.


GDB Backtrace All Threads

Viewing the call stack for all threads helps debug multi-threaded applications by understanding the execution flow in each thread.

Example:

(gdb) thread apply all backtrace

This command prints the call stack for all threads.


GDB Watchpoints

Watchpoints in GDB monitor specific variables or memory locations, pausing execution whenever their values change, which is useful for tracking data modifications.

Example:

(gdb) watch my_var

This command sets a watchpoint on my_var.

Conclusion

Mastering GDB is crucial for any developer looking to efficiently debug and understand their programs. By leveraging the various commands and techniques outlined in this guide, you can gain deeper insights into your code, identify and fix bugs more effectively, and enhance the overall quality of your software. Whether you are inspecting variables, managing breakpoints, examining memory, or analyzing the call stack, GDB provides the tools necessary for comprehensive debugging and troubleshooting. Embrace these practical examples to unlock the full potential of GDB and streamline your development process.

@freecoder
@freecoder

With 15+ years in low-level development, I'm passionate about crafting clean, maintainable code.
I believe in readable coding, rigorous testing, and concise solutions.

Articles: 29