proxygen
`gdb` scripts

This directory contains a collection of gdb scripts that we have found helpful. These scripts use the gdb extension Python API.

How to run the scripts

To run the scripts, fire up gdb and load a script with source -v. Example:

1 {lang=bash}
2 $ gdb -p 123456
3 (gdb) source -v ./folly/experimental/gdb/deadlock.py
4 Type "deadlock" to detect deadlocks.
5 # At this point, any new commands defined in `deadlock.py` are available.
6 (gdb) deadlock
7 Found deadlock!
8 ...

What does each script do?

deadlock.py - Detect deadlocks

Consider the following program that always deadlocks:

1 {lang=cpp}
2 void deadlock3() {
3  std::mutex m1, m2, m3;
4  folly::Baton<> b1, b2, b3;
5 
6  auto t1 = std::thread([&m1, &m2, &b1, &b2] {
7  std::lock_guard<std::mutex> g1(m1);
8  b1.post();
9  b2.wait();
10  std::lock_guard<std::mutex> g2(m2);
11  });
12 
13  auto t2 = std::thread([&m3, &m2, &b3, &b2] {
14  std::lock_guard<std::mutex> g2(m2);
15  b2.post();
16  b3.wait();
17  std::lock_guard<std::mutex> g3(m3);
18  });
19 
20  auto t3 = std::thread([&m3, &m1, &b3, &b1] {
21  std::lock_guard<std::mutex> g3(m3);
22  b3.post();
23  b1.wait();
24  std::lock_guard<std::mutex> g1(m1);
25  });
26 
27  t1.join();
28  t2.join();
29  t3.join();
30 }

The deadlock.py script introduces a new deadlock command that can help us identify the threads and mutexes involved with the deadlock.

1 {lang=bash}
2 $ gdb -p 2174496
3 (gdb) source -v ./folly/experimental/gdb/deadlock.py
4 Type "deadlock" to detect deadlocks.
5 (gdb) deadlock
6 Found deadlock!
7 Thread 2 (LWP 2174497) is waiting on mutex (0x00007ffcff42a4c0) held by Thread 3 (LWP 2174498)
8 Thread 3 (LWP 2174498) is waiting on mutex (0x00007ffcff42a4f0) held by Thread 4 (LWP 2174499)
9 Thread 4 (LWP 2174499) is waiting on mutex (0x00007ffcff42a490) held by Thread 2 (LWP 2174497)

NOTE: This script only works on Linux and requires debug symbols to be installed for the pthread library.