Skip to content

Support running native debuggers in ocamltest - #13199

Merged
shindere merged 7 commits into
ocaml:trunkfrom
tmcgilchrist:tsmc-ocaml-test-lldb
Sep 24, 2024
Merged

shindere merged 7 commits into
ocaml:trunkfrom
tmcgilchrist:tsmc-ocaml-test-lldb

Conversation

@tmcgilchrist

Copy link
Copy Markdown
Contributor

This change introduces the ability to run native debuggers (LLDB and GDB) as part of the ocaml testsuite.

GBD and LLDB both provide Python APIs for interacting with the debugger. This makes it possible to write Python scripted tests, e.g. setting breakpoints, inspecting stack frames and looking up symbol information. In particular LLDB has a test suite for the Python API https://github.com/llvm/llvm-project/tree/main/lldb/test/API with many useful examples of what is possible.

Of course you can still provide CLI arguments to the debugger, with some extra work to sanitize the output
to remove machine information (hex-addresses, process ids, file paths etc). For that I've re-used the existing awk dependency which should be sufficient for the Unix platforms that will be initially supported.

Having support in ocamltest for LLDB and GDB will help ensure that any future changes don't break the debugging experience and more tests can be written in Python to exercise OCaml debugging functionality.

6d101c0 is a small refactor to make the existing bytecode debugger tests use debugger_script as the name of the commands file to run against the debugger. To run a debugger test you would have:
debugger_script to point to the commands and choose which debugger to run (gdb|lldb|ocamldebug).

This change should be taken as a whole with the commits squashed before merging.

@MisterDA MisterDA 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.

Minor comments.

  1. Is it necessary to introduce the dev_null variable? I don't ever recall hearing about an Unix where it's not /dev/null (POSIX defines /dev/null). The Path to /dev/null description is redundant, maybe Path to the null device is a better wording? and would also be portable to Windows where the null device is simply called NUL. Note that we also have Filename.null in the standard library.
  2. You could use if ! command -v <the_command> &> /dev/null; then in the scripts instead of which.
  3. You could add a new line at the end of all new files, some of them are missing them. It's generally a bit nicer to interact with them.

@shindere

shindere commented May 27, 2024 via email

Copy link
Copy Markdown
Contributor

@tmcgilchrist
tmcgilchrist marked this pull request as draft May 30, 2024 00:15
@tmcgilchrist

Copy link
Copy Markdown
Contributor Author

Moving to Draft status, I have discovered issues with CI permissions and repeatability of generated symbols, that need fixing before this is mergable.

@shindere

shindere commented May 30, 2024 via email

Copy link
Copy Markdown
Contributor

@tmcgilchrist
tmcgilchrist force-pushed the tsmc-ocaml-test-lldb branch 3 times, most recently from 8f9c188 to b004ef2 Compare July 4, 2024 22:44
@tmcgilchrist

Copy link
Copy Markdown
Contributor Author

This change introduces the ability to run GDB or LLDB (what I'm calling native debuggers) to ocamltest. The main changes are in the first four commits.

8f9c188 provides a hopefully compelling example of what can be done with native debugger support. The fixes in #13271 #13261 #13241 are all checked with this test and provide future scope for validating more debugging features work. @gasche @kayceesrk

At the moment the test is implemented with tools we already use in the build scripts (sed and shell) with all the drawbacks of cross platform shell scripting. In future these tests could rely on LLDB/GDB python support to implement tests.

@tmcgilchrist
tmcgilchrist marked this pull request as ready for review July 4, 2024 22:56
@@ -0,0 +1,15 @@
break *(&caml_start_program+0)

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.

Curious detail here with gdb when setting a breakpoint using just br caml_start_program GDB was setting the actual breakpoint at some offset from that symbol in the assembly. On AMD64 it was directly on that symbol but for ARM64 it is at caml_start_program+4 and for RISC-V it ends up at caml_start_program+40. So I have used an offset here. If you remove it then the GDB session on RISC-V is incorrect as we re-enter via caml_callback_asm and jump_to_caml label which incorrectly hits that breakpoint.

@shindere

Copy link
Copy Markdown
Contributor

Many thanks for the work!

At this stage I have three observations:

  1. When running the test, could you please print which debugger is run?

  2. I notice that in the code you emphase that the debuggers are for native
    code and I was wondering why that matters, actually?

  3. YOu have added allthose debuggers to files whosename is prefixed with
    "ocaml_" but this is not really where they should be added, as those files
    are for tests and actions that are specific to OCaml, which is probably
    notthe case for these debuggers so I'd suggset to add all this to the
    "builtin_" files and to create them when they don't exist. Also, I think in
    the names of the commands and files the "run prefix does not relally
    looknecessary to me?

@gasche gasche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I just had a look, this looks okay to me, but I don't understand your intent regarding how to check that gdb or lldb are available. What I see is that you started with a boolean action lldb, but commented it out, for gdb there is a shell script to check availability (no action) and for lldb I don't see any availability-checking code. (The tests refer to a lldb_test.py file but I cannot find it in the PR itself, how does that work?)

Comment thread ocamltest/main.ml
let make = try Sys.getenv "MAKE" with Not_found -> "make" in
let initial_environment = Environments.from_bindings
[
Builtin_variables.dev_null, "/dev/null";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On Windows my understanding is that /dev/null is not available but NUL works as a null sink. (You only use this variable in tests that are disabled on windows, but it is available more widely so maybe we should try to get it right.)

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.

Honestly, my focus is on improving native Unix debugger and I have no plans to work on debugging for Windows in the near future. Plus I don't have access to an environment to test Windows changes right or really know what I'm doing on Windows in general.

IF there is a simple change to get Windows support here I can apply it. Otherwise I would ask to leave Windows support as a future task.

Comment thread ocamltest/ocaml_actions.ml Resolved
Comment thread ocamltest/ocaml_actions.ml Resolved
@tmcgilchrist
tmcgilchrist force-pushed the tsmc-ocaml-test-lldb branch from b004ef2 to f31cd9a Compare August 8, 2024 07:36
@tmcgilchrist

Copy link
Copy Markdown
Contributor Author

@shindere answers inline below.

  1. When running the test, could you please print which debugger is run?

It already does this, it will print out "Debugging program %s with %s" based on the program and debugger used.

  1. I notice that in the code you emphase that the debuggers are for native
    code and I was wondering why that matters, actually?

I used the distinction of native vs bytecode based on the compiler backends. ocamldebug was already covered by tests in tools-debugger along with the other tools distributed with the compiler. Since lldb/gdb aren't distributed with the compiler and are for debugging native executables I've put them into a separate directory and set of tests. Other arrangements of directory / test naming are possible, keeping the information about OS, debugger and architecture obvious is useful when running the tests.

  1. YOu have added allthose debuggers to files whosename is prefixed with
    "ocaml_" but this is not really where they should be added, as those files
    are for tests and actions that are specific to OCaml, which is probably
    notthe case for these debuggers so I'd suggset to add all this to the
    "builtin_" files and to create them when they don't exist. Also, I think in
    the names of the commands and files the "run prefix does not relally
    looknecessary to me?

41ab0f0 moves all the debugger related code into separate modules. Using builtin_ didn't work because ocamldebug had a dependency on certain ocaml_action paths. This organisation seems better and allowed me to simplify the code for debugger_actions.ml.

@shindere

Copy link
Copy Markdown
Contributor

Many thanks for the refactoring work, @tmcgilchrist.

Studying the code, I think there is room for further simplification
and re-architecturing the code, but I propose to leave that for another PR
since it's not so easy to describe and also because a lot of time and energy
has already been spent on this PR which I think can safely be
merged in its current state, once the Changes entry has been updated.

Using Python script API to validate setting break points and
backtraces, which allows common behaviour across platforms.

Also check lldb and gdb versions across platforms, older versions of
XCode on AMD64 are problematic with OCaml name mangled symbols.
@shindere
shindere merged commit 67c5d4b into ocaml:trunk Sep 24, 2024
@Octachron

Copy link
Copy Markdown
Member

The new tests have been failing on amd64+ubuntu and arm64 on the Inria CI due to mismatched line numbers and mangling.

@gasche

gasche commented Sep 25, 2024

Copy link
Copy Markdown
Member

It's not clear from your report how to fix the output sanitizer, (putting myself in the shoes of someone willing to fix this, theoretically) could you include examples of the output on these CI machines?

@ghost

ghost commented Sep 25, 2024

Copy link
Copy Markdown

I have a pending fix to tweak the .c line numbers as this is also hitting one of my PRs; however I can't fix the lldb oracles because the lldb tests also depend on a particular python module (embedded_interpreter) which I can't seem to find ATM.

@ghost

ghost commented Sep 25, 2024

Copy link
Copy Markdown

See #13477.

@tmcgilchrist

Copy link
Copy Markdown
Contributor Author

There's a few more fixes required to the python / sed to make this more general on top of #13477. While I work on that please consider disabling the tests if they are causing problems.

@Octachron if you could include linux distro, gdb and lldb version numbers along with the failing tests that would help. I've mainly tested my work on recent Ubnutu 22.04/24.04 distros

@Octachron

Copy link
Copy Markdown
Member

@tmcgilchrist , this information is available on the inria CI at https://ci.inria.fr/ocaml (on which you have access rights as far as I can see).

@tmcgilchrist

Copy link
Copy Markdown
Contributor Author

Thank you @Octachron I do have access and have found the right logs.

@shindere

shindere commented Sep 26, 2024 via email

Copy link
Copy Markdown
Contributor

@Octachron Octachron mentioned this pull request Sep 26, 2024
@shindere

Copy link
Copy Markdown
Contributor

At some point we will need to make sure the GitHub runners do have gdb and
lldb and all the Python support code installed so that the test are actually
run rather than skipped.

Is that something you could easily help with, @MisterDA?

@tmcgilchrist

Copy link
Copy Markdown
Contributor Author

@shindere I'll include installing gdb and lldb for Linux in my PR that I'm working on.

tmcgilchrist added a commit to tmcgilchrist/ocaml that referenced this pull request Sep 30, 2024
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.

5 participants