Process Debugging
Process debugging is the activity of observing and controlling
a running process to locate programming errors, inspect program
state, and understand execution behavior.
Linux provides debugging support through facilities such as
the ptrace() system call and debuggers like GDB.
Learning Objectives
- Understand process debugging fundamentals.
- Learn process states during debugging.
- Study the
ptrace()system call. - Understand debugger attachment.
- Learn the general debugging workflow.
- Prepare for advanced debugging tools.
1. What is Process Debugging?
Process debugging is the process of examining a running or stopped process to understand its execution, inspect memory, analyze registers, and identify software defects. A debugger can pause execution, inspect program state, and resume execution after analysis.
2. Why Process Debugging is Important
| Reason | Benefit |
|---|---|
| Crash Analysis | Identify the cause of abnormal termination. |
| Logic Verification | Verify program execution flow. |
| Memory Inspection | Examine variables and process memory. |
| Register Inspection | Observe CPU register contents. |
| Bug Isolation | Locate the source of software defects. |
3. Process States During Debugging
| State | Description |
|---|---|
| Running | The process is actively executing instructions. |
| Stopped | The debugger temporarily pauses execution. |
| Waiting | The process is waiting for an event or resource. |
| Terminated | The process has completed execution. |
4. ptrace() System Call
The ptrace() system call enables one process
(the debugger) to observe and control another process
(the tracee).
Operations include stopping execution, examining registers,
reading memory, writing memory, and continuing execution.
5. ptrace() Workflow
6. Attaching to a Running Process
A debugger may attach to an already running process. After attachment, the debugger can pause execution, inspect the process state, and resume execution without restarting the application.
7. General Debugging Workflow
8. Key Debugging Operations
| Operation | Purpose |
|---|---|
| Pause | Temporarily stop execution. |
| Continue | Resume execution. |
| Step | Execute one instruction or source line. |
| Inspect | View variables, registers, or memory. |
| Modify | Change variable or memory contents. |
9. Interview Questions
- What is process debugging?
- Why is process debugging necessary?
- What process states are commonly encountered during debugging?
- What is the purpose of
ptrace()? - Which process controls the tracee?
- Can a debugger attach to an already running process?
- What operations can a debugger perform?
- Why are registers inspected during debugging?
- What is the difference between pausing and terminating a process?
- Describe the general process debugging workflow.
10. GDB Process Debugging
GNU Debugger (GDB) is the standard debugger used on Linux for examining process execution. It allows developers to pause execution, inspect variables, analyze memory, examine registers, and control program flow.
11. Breakpoints
A breakpoint instructs the debugger to stop program execution when a specified source line or function is reached. Breakpoints allow examination of program state before execution continues.
12. Watchpoints
A watchpoint stops program execution whenever the value of a specified variable changes. Watchpoints are useful when tracking unexpected modifications to variables.
13. Register Inspection
Registers store the processor's current execution state. Inspecting register values helps determine the exact processor state when execution is paused.
14. Memory Inspection
GDB can examine memory at any valid process address. Memory inspection is useful when analyzing pointers, arrays, structures, and dynamically allocated objects.
15. Stack Frames
Each function call creates a stack frame containing local variables, parameters, and return information. GDB allows navigation through stack frames to inspect execution history.
16. Practical POSIX Example
A breakpoint placed before the printf() statement
allows inspection of the variable value before it
is printed.
17. Breakpoint Workflow
18. Linux Debugging Commands
| Command | Description |
|---|---|
gdb ./program |
Start debugging a program. |
break main |
Create a breakpoint at the main function. |
watch variable |
Create a watchpoint. |
info registers |
Display CPU registers. |
x/16x address |
Examine sixteen hexadecimal memory values. |
19. Interview Questions
- What is the purpose of a breakpoint?
- How does a watchpoint differ from a breakpoint?
- Why are CPU registers inspected during debugging?
- What information is stored in a stack frame?
- How can memory be examined in GDB?
- What is the purpose of the
backtracecommand? - When should watchpoints be preferred?
- How do breakpoints assist in debugging?
- What information does
info registersprovide? - Why are stack frames useful during crash analysis?
20. strace
strace is a Linux diagnostic utility that traces
system calls made by a process.
It displays the system calls executed, their arguments,
return values, and error codes, making it useful for analyzing
interactions between an application and the operating system.
21. Running strace
This command starts the program and displays each system call performed during execution.
22. ltrace
ltrace traces calls made to dynamically linked
library functions.
Unlike strace, which traces kernel system calls,
ltrace focuses on library function invocations.
23. Running ltrace
This command displays library function calls performed by the application during execution.
24. Debugging Child Processes
Applications may create child processes using
fork().
GDB provides options that allow developers to choose whether
the debugger follows the parent process or the newly created
child process after a fork() operation.
25. Following Child Processes
These commands instruct GDB to continue debugging the child process and optionally keep both parent and child under debugger control.
26. Core Dump Analysis
Core dump analysis allows developers to examine the state of a process after abnormal termination. The executable file and the generated core dump are loaded into GDB to inspect the call stack, registers, variables, and memory contents.
27. Practical POSIX Example
When debugging applications that create child processes, the debugger can be configured to follow either process depending on the debugging objective.
28. Linux Analysis Commands
| Command | Description |
|---|---|
strace ./program |
Trace system calls. |
strace -p PID |
Attach to a running process and trace system calls. |
ltrace ./program |
Trace dynamically linked library calls. |
gdb ./program core |
Analyze a core dump. |
file core |
Display information about a core dump file. |
29. Advanced Interview Questions
- What is the purpose of
strace? - How does
ltracediffer fromstrace? - When should
stracebe preferred over GDB? - What information does a core dump contain?
- How can GDB debug child processes created using
fork()? - What does
set follow-fork-mode childdo? - What is the purpose of
detach-on-fork? - Can
straceattach to an already running process? - Why is core dump analysis useful after application crashes?
- Which tool traces library function calls instead of system calls?
30. Process Debugging Best Practices
- Compile applications with debugging symbols using
-g. - Enable compiler warning options.
- Reproduce the problem consistently before investigation.
- Use breakpoints instead of excessive logging where appropriate.
- Inspect variables before modifying program execution.
- Analyze stack frames after crashes.
- Collect and preserve core dump files.
- Verify the fix by repeating the original test case.
31. Common Process Debugging Mistakes
| Mistake | Consequence |
|---|---|
| Debugging optimized binaries only | Difficult variable inspection. |
| Ignoring compiler warnings | Potential defects remain unresolved. |
| Skipping stack analysis | Crash origin may be overlooked. |
| Changing code before identifying the root cause | Original defect becomes harder to reproduce. |
| Not preserving core dumps | Post-mortem debugging becomes impossible. |
| Stopping investigation after fixing one symptom | Underlying defects may still exist. |
32. Process Debugging Limitations
| Limitation | Description |
|---|---|
| Timing-sensitive defects | Some problems occur only under specific execution timing. |
| Optimized code | Compiler optimizations may change variable visibility. |
| External dependencies | Failures involving devices or networks may be difficult to reproduce. |
| Multi-process applications | Simultaneous debugging of several cooperating processes increases complexity. |
| Production environments | Attaching a debugger may temporarily interrupt application execution. |
33. Process Debugging Workflow Summary
34. Module Summary
This module introduced the principles of process debugging on
UNIX/Linux systems.
Topics included process debugging fundamentals, process states,
the ptrace() system call, debugger attachment,
GDB breakpoints and watchpoints, register inspection, memory
inspection, stack frames, strace,
ltrace, child process debugging, core dump
analysis, debugging best practices, and common debugging
limitations.
Concept Mastery Assessment — Module 63
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.