Description
Advanced Debugging in C
Jason Zixu Zhou
McGill Sep-20 2024
Overview
• Concurrency in C Programs
• Common Concurrency IssuesTools for Debugging
• ThreadSanitizer (TSan)
• GDB
• Valgrind (Helgrind)
• Examples
• Race Conditions
• Deadlocks
• Atomic Operations
What is Concurrency?
• Concurrency: Multiple tasks make progress, but not necessarily at the same time. They are interleaved. • Example: A single-core CPU switches between multiple tasks.
• Parallelism: Multiple tasks run simultaneously on multiple cores or processors. • Example: A multi-core CPU running multiple threads at the same time.
• Concurrency is about dealing with lots of tasks at once.
• Parallelism is about doing lots of tasks at once.
• Not equivalent to parallelism (Concurrency ≠ Parallelism)
Common Concurrency Issues
• Race Conditions
• When multiple threads access shared data concurrently without proper synchronization
• Deadlocks
• When two or more threads are waiting for each other to release resources
• Atomic Violations
• Non-atomic operations interrupted by another thread
Tools for Debugging Concurrency
GDB: Used for interactive debugging.
Good for step-by-step execution and conditional breakpoints.
Valgrind: Used for detecting memory management issues.
Ideal for finding memory leaks and memory corruption.
Helgrind: Used for detecting concurrency issues in multithreaded applications.
Focuses on race conditions and deadlocks.
When to Use Which Tool?
• Memory Management Issues: Choose Valgrind.
• Concurrency/Synchronization Issues: Choose Helgrind.
• Interactive Step-by-Step Debugging: Choose GDB.
• Complex Problems: Combine tools for comprehensive analysis.
Build with Makefile
Using Valgrind – Memory Leak Example
• valgrind –leak-check=yes ./leak_memory
Detecting Race Conditions with ThreadSanitizer
GDB – Debugging Segmentation Faults
Using Helgrind – Race Condition Example
• valgrind –tool=helgrind ./race_condition
Exercise: Dead lock
Reviews
There are no reviews yet.