How to use the command gdb (with examples)

How to use the command gdb (with examples)

GDB (GNU Debugger) is a powerful command-line tool used for debugging programs written in various languages. It allows developers to analyze and troubleshoot their programs by examining their execution at different stages.

Use case 1: Debug an executable

Code:

gdb <executable>

Motivation: Debugging an executable is a common use case for GDB. When you encounter issues or errors during the execution of a program, GDB helps in identifying the problem by providing insights into the program’s behavior and allowing you to interactively debug the code.

Explanation:

  • <executable>: The name or path of the executable you want to debug.

Example output:

GNU gdb (GDB) 8.2.1
...
Reading symbols from executable...done.
(gdb)

Use case 2: Attach a process to gdb

Code:

gdb -p <procID>

Motivation: Sometimes, you may want to debug a running process without restarting it. Attaching a process to GDB allows you to inspect its state, variables, and stack frames at a given point in time, helping you understand the program’s behavior and diagnose issues.

Explanation:

  • <procID>: The process ID of the running process you want to attach to GDB.

Example output:

GNU gdb (GDB) 8.2.1
...
Attaching to process <procID>
...
(gdb) 

Use case 3: Debug with a core file

Code:

gdb -c <core> <executable>

Motivation: When a program crashes, it can generate a core dump file that contains a snapshot of the program’s memory at the time of the crash. By using GDB with a core file, you can analyze the state of the crashed program, identify the cause of the crash, and debug it effectively.

Explanation:

  • <core>: The path to the core file generated by the crashed program.
  • <executable>: The name or path of the executable associated with the core file.

Example output:

GNU gdb (GDB) 8.2.1
...
[New process <procID>]
Core was generated by `<executable>`.
Program terminated with signal <signal>, <reason>.
...
(gdb) 

Use case 4: Execute given GDB commands upon start

Code:

gdb -ex "commands" <executable>

Motivation: In certain scenarios, it can be helpful to automate certain GDB commands to be executed upon starting the debugger. This can save time by automating repetitive tasks or setting breakpoints automatically.

Explanation:

  • "commands": The GDB commands enclosed in double quotes that you want to execute automatically upon starting GDB.
  • <executable>: The name or path of the executable you want to debug.

Example output:

GNU gdb (GDB) 8.2.1
...
Reading symbols from executable...done.
(gdb) commands
Type commands for when GDB starts and doesn't stop until it receives an interrupt.
...
end
(gdb) 

Use case 5: Start gdb and pass arguments to the executable

Code:

gdb --args <executable> <argument1> <argument2>

Motivation: Passing command-line arguments to the executable while debugging can help simulate specific scenarios or test different inputs without modifying the source code.

Explanation:

  • <executable>: The name or path of the executable you want to debug.
  • <argument1>, <argument2>: The command-line arguments to be passed to the executable.

Example output:

GNU gdb (GDB) 8.2.1
...
Reading symbols from executable...done.
(gdb) 

Conclusion:

GDB is a versatile tool that enables developers to debug programs effectively. With its various use cases, it allows users to investigate issues, analyze running processes, debug crashes, automate tasks, and test program behavior with ease. Understanding each use case can greatly enhance the debugging process and help developers identify and resolve problems efficiently.

Related Posts

.NET Run Command (with examples)

.NET Run Command (with examples)

The dotnet run command is used to compile and launch a .

Read More
How to securely overwrite the free space and inodes of a disk using the sfill command (with examples)

How to securely overwrite the free space and inodes of a disk using the sfill command (with examples)

The sfill command is a powerful tool that allows users to securely overwrite the free space and inodes of a disk, ensuring that previously deleted data is irrecoverable.

Read More
Using cabal (with examples)

Using cabal (with examples)

1: Searching and listing packages from Hackage To search and list packages from the Hackage package repository, you can use the cabal list command followed by a search string.

Read More