ChatDBG
by Emery Berger
ChatDBG is an experimental debugger for Python and native C/C++ code that integrates large language models into a standard debugger (pdb, lldb, and gdb) to help debug your code. With ChatDBG, you can ask your debugger "why" your program failed, and it will provide a suggested fix.
As far as we are aware, ChatDBG is the first debugger to automatically perform root cause analysis and to provide suggested fixes. This is an alpha release; we greatly welcome feedback and suggestions!
Installation
NOTE: To use ChatDBG, you must first set up an OpenAI API key. If you
already have an API key, you can set it as an environment variable
called OPENAI_API_KEY. If you do not have one yet,
you can get a key here: https://platform.openai.com/account/api-keys
Install ChatDBG using pip (you need to do this whether you are debugging Python, C, or C++ code):
If you are using ChatDBG to debug Python programs, you are done. If you want to use ChatDBG to debug native code with gdb or lldb, follow the installation instructions below.
Installing as an lldb extension
Install ChatDBG into the lldb debugger by running the following command:
Linux
Mac
This will install ChatDBG as an LLVM extension.
Installing as a gdb extension
Install ChatDBG into the gdb debugger by running the following command:
This will install ChatDBG as a GDB extension.
Usage
Debugging Python
To use ChatDBG to debug Python programs, simply run your Python script with the -m flag:
or just
ChatDBG is an extension of the standard Python debugger pdb. Like
pdb, when your script encounters an uncaught exception, ChatDBG will
enter post mortem debugging mode.
Unlike other debuggers, you can then use the why command to ask
ChatDBG why your program failed and get a suggested fix.
Debugging native code (lldb / gdb)
To use ChatDBG with lldb or gdb, just run native code (compiled with -g for debugging symbols) with your choice of debugger; when it crashes, ask why. This also works for post mortem debugging (when you load a core with the -c option).
Examples
(ChatDBG lldb) run
Process 85494 launched: '/Users/emery/git/ChatDBG/test/a.out' (arm64)
TEST 1
TEST -422761288
TEST 0
TEST 0
TEST 0
TEST 0
TEST 0
TEST 0
Process 85494 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x100056200)
frame #0: 0x0000000100002f64 a.out`foo(n=8, b=1) at test.cpp:7:22
4 int x[] = { 1, 2, 3, 4, 5 };
5
6 void foo(int n, float b) {
-> 7 cout << "TEST " << x[n * 10000] << endl;
8 }
9
10 int main()
Target 0: (a.out) stopped.
Ask why to have ChatDBG provide a helpful explanation why this program failed, and suggest a fix:
(ChatDBG lldb) why
The root cause of this error is accessing an index of the array `x`
that is out of bounds. In `foo()`, the index is calculated as `n *
10000`, which can be much larger than the size of the array `x` (which
is only 5 elements). In the given trace, the program is trying to
access the memory address `0x100056200`, which is outside of the range
of allocated memory for the array `x`.
To fix this error, we need to ensure that the index is within the
bounds of the array. One way to do this is to check the value of `n`
before calculating the index, and ensure that it is less than the size
of the array divided by the size of the element. For example, we can
modify `foo()` as follows:
```
void foo(int n, float b) {
if (n < 0 || n >= sizeof(x)/sizeof(x[0])) {
cout << "ERROR: Invalid index" << endl;
return;
}
cout << "TEST " << x[n] << endl;
}
```
This code checks if `n` is within the valid range, and prints an error
message if it is not. If `n` is within the range, the function prints
the value of the element at index `n` of `x`. With this modification,
the program will avoid accessing memory outside the bounds of the
array, and will print the expected output for valid indices.
:
, 9, in <>
, 4, in
:
or
>
->
Ask why to have ChatDBG provide a helpful explanation why this program failed, and suggest a fix:
is is
in . 0 99,
0, .
, ,
is
:
= 0
continue
+= 1
return