Linux & Command Line ADVANCED Linux ⏱️ 4 min read 📅 Updated: 14 Sep 2026

Advanced: Tracing Process Memory Accesses and Call Stacks using ptrace and GDB

This advanced guide teaches system engineers how to use the Linux <code>ptrace</code> system call, orchestrated via GDB, to intercept, analyze, and trace specific memory reads/writes and reconstruct detailed call stacks for running processes.

Gurveer Singh
Gurveer Singh ✓ Verified Specialist
Founder & Chief Architect · hSECURITIES Labs
⚡ FAST-TRACK PROTOCOL

Direct Diagnostic & Remediation Summary

  • Prerequisite Check: Ensure you have debugging tools installed: sudo apt update && sudo apt install gdb build-essential.
  • Basic Tracing Setup: Run the target application under GDB, then use the watch/rwatch commands on memory addresses or breakpoints to halt execution upon access.
🛠️ Tools & Prerequisites: A Linux environment (Ubuntu/Debian recommended), Root or elevated user privileges (for attaching to processes), Debugging tools installed (GDB, build-essential). Knowledge of Assembly language and process architecture is required.
🔍 Root Cause & Diagnostic Overview: When a program exhibits unexpected behavior—such as memory corruption, unauthorized data leakage, or non-deterministic crashes—standard logging often fails to pinpoint the exact line of code responsible. The ptrace system call allows one process (the tracer) to observe and control the execution of another process (the tracee). GDB provides a high-level interface to manage this tracing, enabling inspection at the CPU instruction level, monitoring memory reads/writes, and capturing the full context (registers, stack frames) when an event occurs.
STEP 1

Preparation: Compiling Debug Symbols & Setting Up the Target

Before tracing, the target application must be compiled with debugging symbols (-g) to allow GDB to map addresses back to source code lines. You also need a known process ID (PID) or the executable path.

If you are testing a custom binary (e.g., vulnerable_app), compile it first:

gcc -g vulnerable_app.c -o vulnerable_app

To trace an already running process, obtain its PID using the ps aux | grep [process_name] command.

STEP 2

Attaching GDB and Initial Breakpoints

Use GDB to load the executable. For local testing, run it directly under GDB. For live analysis, use the attach command.

gdb ./vulnerable_app

Once in the GDB prompt, set a breakpoint at the suspected function entry point or address:

break vulnerable_function_name

If tracing an external process (PID 12345):

gdb -p 12345

After attaching, run the program until it hits your breakpoint:

continue
STEP 3

Monitoring Memory Accesses (Watchpoints)

To trace memory reads or writes, use watch or rwatch. These commands instruct the kernel to generate a signal when access occurs, halting GDB execution.

  • Write Watchpoint (Hardware Breakpoint): Set this on the memory address suspected of being corrupted or written to unexpectedly. watch *0xDEADBEEF
    (This halts on any write to that specific memory location.)
  • Read Watchpoint: Use rwatch if you need to know the exact moment data is read from a sensitive register or memory area. rwatch *0xCAFEDEAD
    (This halts upon any read attempt.)

When a watchpoint triggers, GDB will stop and report which instruction caused the access.

STEP 4

Call Stack Reconstruction (Backtraces)

When execution halts due to a breakpoint or watchpoint, use bt (backtrace) to see the sequence of function calls leading up to the halt point. This reconstructs the call stack.

bt full

The full flag attempts to provide local variable information for each frame, which is invaluable for debugging complex state machines. You can then use frame N followed by info locals to inspect variables specific to that call level.

TROUBLESHOOTING

Common Pitfalls & Advanced Diagnostics

  • Permission Denied (EPERM): If GDB cannot attach or set breakpoints, the target process might be running with elevated privileges that restrict debugging access. You may need to run your entire diagnostic session as root (sudo gdb ...).
  • Watchpoint Overload: Hardware watchpoints are limited by CPU architecture (often 4 per core). If you need more, you must switch to software breakpoints break *address, which is slower but uses more stack space.
  • Kernel Interaction: For advanced kernel tracing, consider using ftrace or eBPF tools instead of pure GDB/ptrace, as these offer deeper visibility into system calls.
  • Signal Handling: If the program ignores SIGTRAP (the signal used by breakpoints), you may need to examine its signal handler registration using info signals within GDB.
FINAL STEP

Verification, Results & Long-Term Stability Validation

The fix is validated by successfully reproducing the failure condition (e.g., memory corruption) while using GDB to halt execution exactly at the offending instruction. The goal is not just to stop the crash, but to identify why it crashed.

  • Memory Check: After identifying the write, use x/s &memory_address to dump the corrupted data and compare it against expected values.
  • Stack Trace Validation: Confirm that the backtrace (bt full) points logically to the module or function responsible for setting the bad pointer value.
  • Prevention Strategy: Implement static analysis tools (like Valgrind memcheck) during development, as these are safer and faster than runtime tracing for general memory leak detection.
Resolution Complete: By mastering GDB's integration with ptrace, you have gained the ability to perform surgical, low-level diagnostics on process execution flow and memory state. If symptoms persist, re-evaluate whether the bug is application logic (fixable by code) or a race condition/OS interaction (requiring kernel debugging tools like eBPF).
COMMAND LINE / TERMINAL
gcc -g vulnerable_app.c -o vulnerable_app
gdb ./vulnerable_app
break vulnerable_function_name
continue
watch *0xDEADBEEF
rwatch *0xCAFEDEAD
bt full
exit

Frequently Asked Questions

What is the primary difference between 'watch' and 'rwatch' in GDB?

<code>watch</code> monitors for any write operation to a memory location, while <code>rwatch</code> specifically monitors for read operations from that location. Both halt execution upon detection.

Can I trace a process written in C++ using this method?

Yes, provided the compiler generated debugging symbols (-g) and GDB supports the necessary runtime library structures for that specific C++ version.

Is ptrace inherently insecure?

<code>ptrace</code> is powerful and can be misused. Modern OSes implement strict security boundaries (like seccomp filters) to mitigate unauthorized process inspection, which is why elevated privileges are often required.

Engineering Assistance

Need help implementing this solution?

Our cybersecurity and infrastructure engineers can audit, harden, or configure your systems directly.

Request Technical Assistance →
Gurveer Singh
Gurveer Singh ✓ Verified Author
Founder & CEO at hSECURITIES | Cybersecurity Educator | Security Researcher | Ethical Hacking Enthusiast | Technology Mentor
Meet the Founder →

Gurveer Singh Saggu is the Founder and CEO of hSECURITIES, a cybersecurity and technology education platform focused on practical learning, digital security awareness, hardware diagnostics, Linux administration, and real-world cybersecurity training.

With a strong passion for technology, ethical hacking, and security education, he has spent years helping students, professionals, and organizations understand modern cyber threats and develop practical technical skills. Through hSECURITIES, he creates hands-on learning resources, technical guides, security awareness content, and community-driven initiatives that make cybersecurity more accessible.

His mission is to bridge the gap between theoretical knowledge and practical implementation by providing step-by-step learning experiences, technical troubleshooting resources, and security-focused educational platforms.

Community Discussion & Confirmations (0)

Technical questions, hardware findings, and verified fix reports.

No reader questions posted yet. Have you tried this solution or experienced an edge case? Post below.

Ask a Question or Share Solution