- Notifications
You must be signed in to change notification settings - Fork54
ChatDBG - AI-assisted debugging. Uses AI to answer 'why'
License
plasma-umass/ChatDBG
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
byEmery Berger,Stephen Freund,Kyla Levin,Nicolas van Kempen (ordered alphabetically)
ChatDBG is an AI-based debugging assistant for C/C++/Python/Rust code that integrates large language models into a standard debugger (pdb
,lldb
,gdb
, andwindbg
) to help debug your code. With ChatDBG, you can engage in a dialog with your debugger, asking open-ended questions about your program, likewhy is x null?
. ChatDBG willtake the wheel and steer the debugger to answer your queries. ChatDBG can provide error diagnoses and suggest fixes.
As far as we are aware, ChatDBG is thefirst debugger to automatically perform root cause analysis and to provide suggested fixes.
Watch ChatDBG in action!
LLDB ontest-overflow.cpp | GDB ontest-overflow.cpp | Pdb onbootstrap.py |
---|---|---|
For technical details and a complete evaluation, see our arXiv paper,ChatDBG: An AI-Powered Debugging Assistant (PDF).
Note
ChatDBG forpdb
,lldb
, andgdb
are feature-complete; we are currently backporting features for these debuggers into the other debuggers.
Important
ChatDBG currently needs to be connected to anOpenAI account.Your account will need to have a positive balance for this to work (check your balance). If you have never purchased credits, you will need to purchase at least $1 in credits (if your API account was created before August 13, 2023) or $0.50 (if you have a newer API account) in order to have access to GPT-4, which ChatDBG uses.Get a key here.
Once you have an API key, set it as an environment variable calledOPENAI_API_KEY
.
export OPENAI_API_KEY=<your-api-key>
Install ChatDBG usingpip
(you need to do this whether you are debugging Python, C, or C++ code):
python3 -m pip install chatdbg
If you are using ChatDBG to debug Python programs, you are done. If you want to use ChatDBG to debug native code withgdb
orlldb
, follow the installation instructions below.
lldb installation instructions
Install ChatDBG into thelldb
debugger by running the following command:
python3 -m pip install ChatDBGpython3 -c'import chatdbg; print(f"command script import {chatdbg.__path__[0]}/chatdbg_lldb.py")'>>~/.lldbinit
If you encounter an error, you may be using an older version of LLVM. Update to the latest version as follows:
sudo apt install -y lsb-release wget software-properties-common gnupgcurl -sSf https://apt.llvm.org/llvm.sh | sudo bash -s -- 18 all# LLDB now available as `lldb-18`.
xcrun python3 -m pip install ChatDBGxcrun python3 -c'import chatdbg; print(f"command script import {chatdbg.__path__[0]}/chatdbg_lldb.py")'>>~/.lldbinit
This will install ChatDBG as an LLVM extension.
gdb installation instructions
Install ChatDBG into thegdb
debugger by running the following command:
python3 -m pip install ChatDBGpython3 -c'import chatdbg; print(f"source {chatdbg.__path__[0]}/chatdbg_gdb.py")'>>~/.gdbinit
This will install ChatDBG as a GDB extension.
WinDBG installation instructions
- Install WinDBG: Follow instructionshere if
WinDBG
is not installed already. - Install
vcpkg
: Follow instructionshere ifvcpkg
is not installed already. - Install Debugging Tools for Windows: Install the Windows SDKhere and check the box
Debugging Tools for Windows
. - Navigate to the
src\chatdbg
directory:cd src\chatdbg
- Install needed dependencies: Run
vcpkg install
- Build the chatdbg.dll extension: Run
mkdir build & cd build & cmake .. & cmake --build . & cd ..
Using ChatDBG:
- Load into WinDBGX:
- Run
windbgx your_executable_here.exe
- Click the menu items
View
->Command browser
- Type
.load debug\chatdbg.dll
- Run
- After running code and hitting an exception / signal:
- Type
!why
in Command browser
- Type
To use ChatDBG to debug Python programs, simply run your Python script as follows:
chatdbg -ccontinue yourscript.py
ChatDBG is an extension of the standard Python debuggerpdb
. Likepdb
, when your script encounters an uncaught exception, ChatDBG willenter post mortem debugging mode.
Unlike other debuggers, you can then use thewhy
command to askChatDBG why your program failed and get a suggested fix. After the LLM responds,you may issue additional debugging commands or continue the conversation by enteringany other text.
To use ChatDBG as the default debugger for IPython or inside Jupyter Notebooks,create a IPython profile and then add the necessary exensions on startup. (Modifythese lines as necessary if you already have a customized profile file.)
ipython profile createecho"c.InteractiveShellApp.extensions = ['chatdbg.chatdbg_pdb', 'ipyflow']">>~/.ipython/profile_default/ipython_config.py
On the command line, you can then run:
ipython --pdb yourscript.py
Inside Jupyter, run your notebook with theipyflow kernel and include this line magic at the top of the file.
%pdb
To use ChatDBG withlldb
orgdb
, just run native code (compiled with-g
for debugging symbols) with your choice of debugger; when it crashes, askwhy
. This also works for post mortem debugging (when you load a core with the-c
option).
The native debuggers work slightly differently than Pdb. After the debugger responds to your question, you will enter into ChatDBG's command loop, as indicated by the(ChatDBG chatting)
prompt. You may continue issuing debugging commands and you may send additional messages to the LLM by starting those messages with "chat". When you are done, typequit
to return to the debugger's main command loop.
Debugging Rust programs
To use ChatDBG with Rust, you need to do two steps: modify yourCargo.toml
file and add one line to your source program.
- Add this to your
Cargo.toml
file:
[dependencies]chatdbg ="0.6.2"[profile.dev]panic ="abort"[profile.release]panic ="abort"
- In your program, apply the
#[chatdbg::main]
attribute to yourmain
function:
#[chatdbg::main]fn main(){
Now you can debug your Rust code withgdb
orlldb
.
ChatDBG example inlldb
(ChatDBG lldb) runProcess85494 launched:'/Users/emery/git/ChatDBG/test/a.out' (arm64)TEST1TEST -422761288TEST0TEST0TEST0TEST0TEST0TEST0Process85494 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:224 int x[] = {1,2,3,4,5 };56 void foo(int n,float b) {->7 cout <<"TEST" << x[n *10000] << endl;8 }910 int main()Target0: (a.out) stopped.
Askwhy
to have ChatDBG provide a helpful explanation why this program failed, and suggest a fix:
(ChatDBG lldb) whyThe 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` (whichis only5 elements). In the given trace, theprogram is trying toaccess the memoryaddress `0x100056200`, which is outside of the rangeof allocated memory for the array `x`.To fix this error, we need to ensure that the index is within thebounds of the array. One way to do this is tocheck the value of `n`before calculating the index, and ensure that it is less than the sizeof the array divided by the size of the element. For example, we canmodify `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 errormessage if it is not. If `n` is within the range, the function printsthe value of the element at index `n` of `x`. With this modification,theprogram will avoid accessing memory outside the bounds of thearray, and willprint the expected output for valid indices.
ChatDBG example in Python (pdb)
Traceback (mostrecentcalllast):File"yourscript.py",line9,in<module>print(tryme(100))File"yourscript.py",line4,intrymeifx/i>2:ZeroDivisionError:divisionbyzeroUncaughtexception.EnteringpostmortemdebuggingRunning'cont'or'step'willrestarttheprogram>yourscript.py(4)tryme()->ifx/i>2:
Askwhy
to have ChatDBG provide a helpful explanation why this program failed, and suggest a fix:
(ChatDBGPdb)whyTherootcauseoftheerroristhatthecodeisattemptingtodividebyzerointheline"if x / i > 2".Asirangesfrom0to99,itwilleventuallyreachthevalueof0,causingaZeroDivisionError.Apossiblefixforthiswouldbetoaddacheckforibeingequaltozerobeforeperformingthedivision.Thiscouldbedonebyaddinganadditionalconditionalstatement,suchas"if i == 0: continue",toskipovertheiterationwheniiszero.Theupdatedcodewouldlooklikethis:deftryme(x):count=0foriinrange(100):ifi==0:continueifx/i>2:count+=1returncountif__name__=='__main__':print(tryme(100))
About
ChatDBG - AI-assisted debugging. Uses AI to answer 'why'