Skip to content

Conversation

@hturner
Copy link
Member

@hturner hturner commented Jul 30, 2025

Opening this PR just to document the changes on this test branch, which demo a prototype LLDB setup.

You can try it out by building and running locally as described in the docs for local setup, but switching to the test/lldb branch before launching vscode.

Then:

  1. Compile allow_ptrace.c
    gcc -shared -fPIC -o allow_ptrace.so allow_ptrace.c
    
  2. Make the launch_r script executable
    chmod +x /workspaces/r-dev-env/scripts/launch_r.sh
    
    At this stage you should be able to open an R terminal, which will use the pre-built version of R. Running Sys.getenv("LD_PRELOAD") should show `"/workspaces/r-dev-env/scripts/allow_ptrace.so"). Quit R.
  3. Build R from source as usual (optionally setting CFLAGS="-g -O0" before the configure step).
  4. Manually switch from the inbuilt version of R to the one you just built by editing the launch.sh script:
    # exec /workspaces/r-dev-env/build/r-devel/bin/R "$@"
    exec /usr/bin/R "$@"
    
    i.e. uncomment the one you want, comment the one you don't want
  5. Open a new R terminal. It should use the new version of R and show the same value for Sys.getenv("LD_PRELOAD") .
  6. Start debugging with LLDB:
    • Run Sys.getpid() to get the process ID of R.
    • From the Run and Debug panel run "(lldb) Attach to R", input the pid to attach to the R process.
    • Set a breakpoint on a C file in the local copy of the sources, e.g. $TOP_SRCDIR/src/nmath/rgamma.c
    • Run code using that function e.g. rgamma(5,1) and the debugger will start.

Tested on Ubuntu 24.04 with fresh checkout from devel branch:

lldb_on_r-devel stepping_over_rgamma

Note that debugger will not work for the pre-built version of R, as it was installed from a binary and we don't have access to the source files!

lldb_on_prebuilt_r

To get this working nicely we would want to do steps 1 and 2 as part of the devcontainer setup. Step 4 should be done by the which_r script - not by simply editing comments, but dynamically switching the path to the R executable, in a similar way to how we currently switch the path in the vscode settings:

updated_settings_data=$(cat "$settings_file_path" | jq --arg subdir "$selected_version" '."r.rterm.linux"=$subdir | ."r.rpath.linux"=$subdir')
echo "$updated_settings_data" > "$settings_file_path"

After running the debugger we get a new button in the status bar labelled "(lldb) Attach to R (r-dev-env)" that you can use to select and start the debug configuration. This is okay, but it hides the "R: (not attached)" button. So either people need to hide some more things in the Status bar, or open R via the command palette. We would probably need to say something about this in the docs.

@hturner hturner changed the base branch from main to devel July 30, 2025 18:05

# Remove git directory if it exists
rm -rf .git
# rm -rf .git
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I commented out this line and added a .gitignore file to make it easier to share my changes here. I think we still want to delete the .git in the container to avoid confusing users.

@hturner
Copy link
Member Author

hturner commented Aug 11, 2025

Closing this, as we are getting closer on #267 now

@hturner hturner closed this Aug 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants