From the free course The Missing Semester of Your CS Education on Lykke
Debugging and Profiling
Key concepts: strace · Interactive Debuggers (GDB/LLDB) · Record-Replay Debugging (rr) · Memory Sanitizers
Techniques for finding bugs and performance bottlenecks using system-level tools.
Debugging and Profiling
Overview
Debugging is more than just print statements. This section covers tools that allow you to inspect a program's interaction with the hardware and the OS.
Key Concepts
- strace: A utility to monitor system calls—how a program asks the OS to read files, open network sockets, or allocate memory.
- Interactive Debuggers: Tools like GDB allow you to pause execution, inspect variables, and step through code line-by-line.
- Record-Replay (rr): Advanced debugging that allows you to record a failure and replay it deterministically as many times as needed.
- Machine Introspection: Using tools like
htop,dool, andjournalctlto monitor system resources and logs in real-time.
Why This Matters
Sophisticated bugs require sophisticated tools. Learning to trace system calls or use memory sanitizers can help you solve 'impossible' bugs in minutes.
Flashcards
- What is the primary purpose of the 'strace' utility?: It monitors system calls (syscalls) made by a program to the OS kernel, such as opening files or network sockets.
- How does Record-Replay Debugging (rr) differ from traditional debugging?: It records a program's execution to allow deterministic replay, ensuring that non-deterministic bugs (like race conditions) happen the same way every time.
- What are Memory Sanitizers (e.g., ASan) used for?: They are compiler-based tools that detect memory errors like buffer overflows, use-after-free, and memory leaks at runtime.
- Name two common interactive debuggers and their main features.: GDB and LLDB; they allow developers to set breakpoints, step through code line-by-line, and inspect variable states during execution.
- What is 'Machine Introspection' in the context of debugging?: Using system-level tools like htop, dool, or journalctl to monitor resource usage and logs to understand how the environment affects the program.
Study Guide
Debugging and Profiling Study Guide
Debugging is the process of identifying and resolving bugs within software. While simple issues can be solved with print statements, complex systems require tools that provide visibility into memory, system calls, and execution flow. Profiling complements this by measuring the space or time complexity of a program to optimize performance.
Effective debugging involves moving from high-level system observation down to low-level instruction stepping. By using the right tool for the right layer—whether it is the OS interface, the memory heap, or the source code—developers can isolate root causes in highly non-deterministic environments.
- strace: Essential for diagnosing 'Permission Denied' or 'File Not Found' errors by showing exactly which system calls fail.
- Interactive Debuggers (GDB/LLDB): Best for deep dives into logic errors where you need to pause time and check variable values.
- Record-Replay (rr): Solves the 'it works on my machine' problem by capturing a failure once and allowing infinite, identical re-runs.
- Memory Sanitizers: Automated tools that catch memory safety violations that might not cause an immediate crash but lead to security vulnerabilities.
- System Monitoring: Tools like
htopandjournalctlprovide the context of the environment, such as CPU spikes or kernel logs.
Infographic
Source Materials
Study The Missing Semester of Your CS Education on Lykke
This course page is free to open: signed out, you can read its first two sections, and signing in, on any plan, opens the rest. The free Lykke Chrome extension syncs your own Canvas courses, so ChatGPT, Claude and Muse can answer from your classes and deadlines. It only reads Canvas.
Add the free extensionSign in to open every sectionAll sections of The Missing Semester of Your CS Education · Browse more course pages