Skip to content

Commit

Permalink
add LLDB support
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This commit renames some files - you will need to adjust your
import paths in .gdbinit or .lldbinit (see readme for updated)
  • Loading branch information
pgosar committed Apr 10, 2023
1 parent 9a7cef1 commit 88d6d29
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 172 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# ChatGDB
Harness the power of ChatGPT inside the GDB debugger!
Harness the power of ChatGPT inside the GDB/LLDB debugger!


ChatGDB is a tool designed to superpower your debugging experience with GDB, a debugger for compiled languages. Use it to accelerate your debugging workflow by leveraging the power of ChatGPT to assist you while using GDB!
ChatGDB is a tool designed to superpower your debugging experience with GDB or LLDB, debuggers for compiled languages. Use it to accelerate your debugging workflow by leveraging the power of ChatGPT to assist you while using GDB/LLDB!

It allows you to explain in natural language what you want to do, and then automatically execute the relevant command. Optionally, you can ask ChatGPT to explain the command it just ran or even pass in any question for it to answer. Focus on what's important - figuring out that nasty bug instead of chasing down GDB commands at the tip of your tongue.
It allows you to explain in natural language what you want to do, and then automatically execute the relevant command. Optionally, you can ask ChatGPT to explain the command it just ran or even pass in any question for it to answer. Focus on what's important - figuring out that nasty bug instead of chasing down GDB/LLDB commands at the tip of your tongue.

![Image](https://lh5.googleusercontent.com/xZMLwWWxavqYjC3fyCIZJ0m-s-f-XEoiOeWGbxRrw3dWoukUoWzJJ4iiBkVO2Vtiyr4K6o0WkTs7B40TapeBPIYwgVRVhDXGVjB4tFYoKH3_nK847nYXl3pISB6dEP6Wp_o0uPlfJOjCrLspm0_VNw)

Expand Down Expand Up @@ -48,11 +48,15 @@ To update ChatGDB, run the following


### Usage
I first recommend editing your ```$HOME/.gdbinit``` to source the main script automatically on startup. Run the following command:
For GDB usage, I first recommend editing your ```$HOME/.gdbinit``` to source the main script automatically on startup. Run the following command:

```echo "source $(python -m site --user-site)/chatgdb/core.py" > $HOME/.gdbinit```
```echo "source $(python -m site --user-site)/chatgdb/gdb.py" > $HOME/.gdbinit```

While inside GDB the command chat appended by your query, for example ```chat list all breakpoints that I created```.
The same applies for LLDB. Edit your ```$HOME/.lldbinit``` and run the following command:

```echo "command script import $(python -m site --user-site)/chatgdb/lldb.py" > $HOME/.lldbinit```

While inside your debugger, you can run the command chat appended by your query, for example ```chat list all breakpoints that I created```.
There is also a command called ```explain``` that you can use with no arguments to explain the previously run command,
and optionally, with a query to just ask GPT a question. For example, running ```explain``` directly after running
```break 7``` would prompt the tool to explain how breakpoints work. Running ```explain how input formatting works in gdb```
Expand Down
165 changes: 0 additions & 165 deletions chatgdb/core.py

This file was deleted.

71 changes: 71 additions & 0 deletions chatgdb/gdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import gdb
from chatgdb import utils

prev_command = ""
COMMAND_PROMPT = "Give me a SINGLE GDB command with no explanation. Do NOT \
write any English above or below the command. Only give me the command as \
text. Here is my question: "
EXPLANATION_PROMPT = "Give me an explanation for this GDB command: "


class GDBCommand(gdb.Command):
"""Custom GDB command - chat
The chat command is used to generate GDB commands based on plain English
input.
"""

def __init__(self):
"""Initializes custom GDB command"""
super(GDBCommand, self).__init__("chat", gdb.COMMAND_DATA)

# creates api request on command invocation
def invoke(self, arg, from_tty):
"""Invokes custom GDB command and sends API request
Params:
arg (str): argument passed to command
from_tty (bool): whether command was invoked from TTY
"""
global prev_command
# handling if user is asking for help on how to use the commands
if arg == "help":
utils.chat_help()
return

prev_command, command = utils.chat_helper(arg, COMMAND_PROMPT)
gdb.execute(command)


class ExplainCommand(gdb.Command):
"""Custom GDB command - explain
The explain command is used to generate explanations for either the
previous command or a user query
"""
def __init__(self):
"""Initializes custom GDB command"""
super(ExplainCommand, self).__init__("explain", gdb.COMMAND_DATA)

# creates api request on command invocation
def invoke(self, arg, from_tty):
"""Invokes custom GDB command and sends API request
Params:
arg (str): argument passed to commands
from_tty (bool): whether command was invoked from from_tty
"""
utils.explain_helper(prev_command, arg, EXPLANATION_PROMPT)


GDBCommand()
ExplainCommand()


def main():
print("ChatGDB loaded successfully. Type 'chat help' for information "
"on how to run the commands.")


if __name__ == "__main__":
main()
42 changes: 42 additions & 0 deletions chatgdb/lldb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import lldb
from chatgdb import utils


def __lldb_init_module(debugger, internal_dict):
"""This function handles the initialization of the custom commands"""
# lldb doesn't trigger python's main function so we print the help here
print("ChatLLDB loaded successfully. Type 'chat help' for information "
"on how to run the commands.")
debugger.HandleCommand('command script add -f lldb.chat chat')
debugger.HandleCommand('command script add -f lldb.explain explain')


prev_command = ""
COMMAND_PROMPT = "Give me a SINGLE LLDB command with no explanation. Do NOT \
give me a GDB command. DO NOT write any English above or below the command. \
Only give me the command as text. Here is my question: "
EXPLANATION_PROMPT = "Give me an explanation for this LLDB command: "


def chat(debugger, command, result, internal_dict):
"""Custom LLDB command - chat
The chat command is used to generate GDB commands based on plain English
input.
"""
global prev_command
# handle when user types 'chat help'
if command == "help":
utils.chat_help()
return
prev_command, command = utils.chat_helper(command, prompt=COMMAND_PROMPT)
debugger.HandleCommand(command)


def explain(debugger, command, result, internal_dict):
"""Custom LLDB command - explain
The explain command is used to generate explanations for either the
previous command or a user query
"""
utils.explain_helper(prev_command, command, prompt=EXPLANATION_PROMPT)
Loading

0 comments on commit 88d6d29

Please sign in to comment.