Skip to content

Expose helpers for C/C++/Rust debuggers - #769

Merged
lionel- merged 24 commits into
mainfrom
feature/c-debug
Apr 10, 2025
Merged

lionel- merged 24 commits into
mainfrom
feature/c-debug

Conversation

@lionel-

@lionel- lionel- commented Apr 9, 2025 •

Copy link
Copy Markdown
Contributor

Closes posit-dev/positron#7174
Progress towards posit-dev/positron#7171

This adds 4 helpers to the global C namespace. The first 3 are part of #7174 and are meant to be directly called by users in the Watch Pane or the Debug Console:

const char* ark_print(SEXP x);
const char* ark_inspect(SEXP x);
const char* ark_trace_back();

Here is how it looks when called from the Debug Console:

Screenshot 2025-04-08 at 12 16 31

Note you'll need to set this in LLDB otherwise newlines and unicode characters are escaped:

settings set escape-non-printables false

And here is how it looks in the Watch Pane (by default you need to opt into the complex expression evaluator with \nat:

yay

(This view helped me fix a bug in rlang!)

Worth noting you can also call these from the Rust debugger (e.g. when debugging Ark itself) if you can get your hands on a SEXP address.

The last one is meant to be called by formatters for the Variable Pane (posit-dev/positron#7171):

const char* ark_display_value(SEXP x);

It is called by the Python formatter implemented in posit-dev/positron#7171 (comment). Here is how it looks in the Variables Pane:

Screenshot 2025-04-09 at 12 18 27

Visibility of symbols in the debugger

Getting these Ark helpers callable from the debugger was surprisingly hard. Including a function with "C" linkage in the binary is not sufficient to be able to call it reliably from the LLDB interpreter. Even though extern "C" symbols are visible with nm, and even though you can set breakpoints in them, they are not necessarily considered to be in scope by the LLDB interpreter. I've tried #[no_mangle] but that didn't help. It seems the only way to make the function globally available is to export it from an actual C file.

So these functions are implemented in debug.c and a new directive in build.rs gets the compiler to build this new C file. The C file is bare bones and directly calls into Rust implementations.

I also had to trick the Rust compiler into thinking that these functions are needed by the program, otherwise they are treated as dead code and omitted from the Ark binary, even in debug builds. We do this by storing a pointer to a function we'd like to preserve in a global variable and marking this variable with #[used] (you can only use that marker on variables, not functions).

Redirecting output to LLDB

Ideally the output of functions like Rf_PrintValue would be redirected to stdout but this is made difficult because by default stdout and stderr are captured and redirected to IOPub again. This allows the output of programs run via system() to be sent to the frontend instead of the Ark's process stdout which is invisible to the user.

One way around that is to start Ark with the --no-capture-streams argument, then you're able to call Rf_PrintValue() and see the output in the console. However it is non obvious to newcomers that this argument exists and even for those who know about it this adds distracting friction to the dev workflow. Also it's easy to forget to unset that argument after debugging which may introduce bugs in regular usage of Ark as C-level output is no longer sent over IOPub.

To avoid having to set --no-capture-streams I tried to take a copy of the original file descriptors of stdout/stderr to divert output there. Unfortunately this doesn't seem to work when stopped in lldb, probably because it does some redirection of its own.

The next best thing I could find is to return the output as a C string. If you set this LLDB setting the C string is printed with special characters correctly formatted (newlines in particular):

settings set escape-non-printables false

The redirection works by flipping a global variable that instructs RMain::write_console() to push its input to a buffer instead of sending it over to IOPub. The capture_console_output() helper is in charge of setting and restoring this variable.

I took some care to ensure consistency of the state in a debugging context where weird stuff can happen:

  • Atomic boolean check in case write_console() is being run when the debugger pauses.

  • Panics are detected and delayed to allow some time to restore the state.

  • R longjumps are also caught.

However I did not make capture_console_output() reentrant. If we need to do that we could turn the output buffer into a stack, but that could get complicated so hopefully will not be needed (I can't think of a case where this would be needed).

@DavisVaughan DavisVaughan left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I can't seem to get the Debug Variables pane to recognize the python script, but I was able to call ark_print() and friends from the debug command prompt.

Maybe you can walk me through the Debug Variables pane bit tomorrow, and we can document the process in an ark help document, and then it can eventually make it into C/C++ help docs for Positron?

Comment thread crates/ark/src/debug.c
@@ -0,0 +1,27 @@
typedef struct SEXPREC *SEXP;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment about what's going on here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is defining SEXP as an opaque struct pointer type.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It just struck me as odd that it seems to imply that you already have access to SEXPREC but not SEXP, but I'm probably misunderstanding it

Comment thread crates/ark/src/debug.rs Resolved
Comment thread crates/ark/src/debug.rs
Comment on lines +44 to +48
/// Uses lobstr's `sxp()` function because libr can't find `R_inspect()`.
/// It's an `attribute_hidden` function but since the symbol is visible
/// on macOS (and you can call it in the debugger) I would have expected
/// libr to be able to find it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You could go through the R level .Internal(inspect(x)) right?

Are we worried about what could happen in lobstr isn't installed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oh good idea

If lobstr is not installed the R-level error is reported through the return value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

One big issue with R-level evaluation is that it might mess with namedness and refcounts. I wish lobstr would expose a C callable for this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I opened r-lib/lobstr#77

Let's keep the lobstr backend as we will probably switch to a direct C call.

Comment thread crates/ark/src/debug.rs Resolved
Comment thread crates/ark/src/debug.rs Resolved
Comment thread crates/libr/src/r.rs Resolved
Comment thread crates/ark/src/debug.rs Outdated
// https://github.com/r-lib/rlang/issues/1059
unsafe {
let fun =
libr::R_GetCCallable(c"rlang".as_ptr(), c"rlang_print_backtrace".as_ptr()).unwrap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What happens if rlang isn't loaded yet?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This will return the error to the debugger saying the function was not registered. We can't unconditionally force rlang to load here as we don't know if it would be safe to do so at the point where the debugger is paused.

Comment thread crates/harp/Cargo.toml Resolved
Comment thread crates/ark/src/debug.rs Outdated
/// Run closure and capture its console output.
///
/// Useful for debugging. For instance you can use this to call code from the
/// lldb interpreter. Output from stdout and stderr is returned instead of being

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we have any hope of this working with gdb / on windows?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes it should work fine

Comment thread crates/ark/src/interface.rs Resolved
@lionel-
lionel- merged commit 4074f51 into main Apr 10, 2025
@lionel-
lionel- deleted the feature/c-debug branch April 10, 2025 12:40
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 10, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Helpers provided by Ark for the C/C++ debugger

2 participants