Repository navigation
Expose helpers for C/C++/Rust debuggers - #769
Conversation
There was a problem hiding this comment.
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?
| @@ -0,0 +1,27 @@ | |||
| typedef struct SEXPREC *SEXP; | |||
There was a problem hiding this comment.
Comment about what's going on here?
There was a problem hiding this comment.
This is defining SEXP as an opaque struct pointer type.
There was a problem hiding this comment.
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
| /// 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. |
There was a problem hiding this comment.
You could go through the R level .Internal(inspect(x)) right?
Are we worried about what could happen in lobstr isn't installed?
There was a problem hiding this comment.
oh good idea
If lobstr is not installed the R-level error is reported through the return value.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I opened r-lib/lobstr#77
Let's keep the lobstr backend as we will probably switch to a direct C call.
| // 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(); |
There was a problem hiding this comment.
What happens if rlang isn't loaded yet?
There was a problem hiding this comment.
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.
| /// 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 |
There was a problem hiding this comment.
Do we have any hope of this working with gdb / on windows?
There was a problem hiding this comment.
yes it should work fine
6a75a9a to
a2f9955
Compare
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:
Here is how it looks when called from the Debug Console:
Note you'll need to set this in LLDB otherwise newlines and unicode characters are escaped:
And here is how it looks in the Watch Pane (by default you need to opt into the complex expression evaluator with
\nat:(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
SEXPaddress.The last one is meant to be called by formatters for the Variable Pane (posit-dev/positron#7171):
It is called by the Python formatter implemented in posit-dev/positron#7171 (comment). Here is how it looks in the Variables Pane:
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 thoughextern "C"symbols are visible withnm, 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.cand a new directive inbuild.rsgets 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_PrintValuewould 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 viasystem()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-streamsargument, then you're able to callRf_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-streamsI 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):
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. Thecapture_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).