Skip to content

Instantly share code, notes, and snippets.

@tednaleid
Last active September 6, 2021 02:25
Show Gist options
  • Save tednaleid/47dcce6e1bc178b63180953cd654651c to your computer and use it in GitHub Desktop.
Save tednaleid/47dcce6e1bc178b63180953cd654651c to your computer and use it in GitHub Desktop.
debugging pony with vscode + lldb

Debugging Pony with VSCode and lldb

The pony language reference has a debugging section that got me pointed in the right direction for getting VSCode set up and integrated with lldb.

There were a few steps that were glossed over in the instructions, but I eventually got everything working, including:

  • breakpoints (including conditional breakpoints)
  • current variable values (including evaluated Strings not pointers)
  • watch expressions
  • step debugging
  • lldb console interaction

MacOS VSCode + lldb Setup and Configuration

There's a good lldb cheatsheet on the pony website.

Install VSCode

Setup VSCode instructions

Install lldb

Using brew:

brew install llvm
sudo /usr/sbin/DevToolsSecurity --enable

Download the pony-lldb type summary extensions

These type summary extensions configure lldb to print out formatted results for your expressions and variables. Instead of having to manually evaluate data types like String * in lldb like this (assuming pony code let my_var = "world"):

p (char *)(my_var->_ptr)
(char *) $1 = 0x0000000100018940 "world"

They'll automatically show up in the IDE as the resolved value of "b'world'".

As of 2021-08-13, there's a bug in the main repo of pony-lldb where it doesn't work with python3.

Till that is merged, I've got a fork with the fix that can be cloned locally.

Later config assumes this is cloned into a sibling directory of your project repo.

git clone [email protected]:tednaleid/pony-lldb.git

Tell VSCode to allow breakpoints anywhere

In the VSCode settings, under Features->Debug, check "Allow breakpoints in any file."

This will be in your global settings.json as:

    "debug.allowBreakpointsEverywhere": true,

Install and configure the "CodeLLDB" VSCode Extension

In the "Extensions" view, you should be able to search for CodeLLDB and install it.

Under Settings->Extensions->LLDB make the following changes:

  • uncheck "Lldb: Derefernece Pointers" (settings.json will show: "lldb.dereferencePointers": false,)
    • this gets the UI to show the nicer versions of values from pony-lldb above
  • Under "Lldb > Launch: Init Commands" click "Edit in settings.json" and to add the command to add lldb extensions:
    "lldb.launch.initCommands": [
        "command script import ${workspaceRoot}/../pony-lldb/pony_lldb.py"
    ],

This instructs lldb to load the pony-lldb type summary extensions before running your app with lldb.

Create your new project (or select an existing project)

Here's a sample file you can use, scratch.pony:

actor Main
  new create(env: Env) =>
    let target = "world"
    let buf = recover String end
    let one: U64 = 1

    buf.append("hello ")
    buf.append(target)

    env.out.print(consume buf)

Make a "task" that can compile a debug version of your app

create a ponyc_debug task in the menu: Terminal->Configure Tasks...

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "ponyc_debug",
            "type": "shell",
            "command": "ponyc --debug"
        }
    ]
}

Create a Run and Debug LLDB Launch Target for your app

This will call the ponyc_debug task above to create a debug version of your app, then will launch lldb.

If your app has arguments to pass in, you can also add those.

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "launch debug",
            "program": "${workspaceRoot}/scratch",
            "args": [],
            "cwd": "${workspaceRoot}",
            "preLaunchTask": "ponyc_debug",
        },
    ]
}

This is the equivalent to doing this on the command line:

ponyc --debug && lldb ./scratch

Debug Your App!

You should now be able to set breakpoints, watch statements, etc, in your app.

Then you can run the "launch debug" configuration under View->Run and it should stop at your breakpoints.

You can also run any native lldb commands you want in the Debug Console pane at the bottom of your screen.

The Pony LLDB Cheatsheet has a bunch of good instructions for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment