See this tool, instructions in this patch.
The tool can generate from your LLVM IR file another LLVM IR file with added debugging info referring to your original LLVM IR file. You then can use that new LLVM IR file for compilation. And during the debugging you will be dealing with the items (stack trace, lines of code) of your original LLVM IR file.
If this reply helps then please up-vote for this reply such that the others know how to debug LLVM IR files too, and build more functionality on top of it. At the moment looks like this is the only option available.
Details.
If you need to deal with bare LLVM IR files, e.g. manually-written
(as opposed to those generated by the compilers from the files in other languages, e.g. C, where the generated LLVM IR files have the in-line debugging info referring to the lines of code and symbols in that C file),
then you can see the (crash) stack trace (of LLVM IR functions), set breakpoints on your LLVM IR functions and hit those breakpoints with the debugger, probably step through your LLVM IR code (but I'm not sure about seeing the values of variables, function parameters, etc.)
by using the tool mentioned above.
Before applying the tool you better replace in your original LLVM IR file all the occurrences of define internal
with define
(remove internal
for function definitions). After this the stack trace shown by the debugger will be more informative.
Debugging
The patch mentioned above provides the details about debugging in Mac OS. For Win and Linux/WSL see below.
In Windows
We used the Visual Studio Code with the extension CodeLLDB (v1.7.0). More details about debugging in VSCode.
The debugger configuration file example "{Repo Root}/.vscode/launch.json":
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "DebuggingLLVMIR",
"type": "lldb",
"request": "launch",
"cwd": "${workspaceFolder}\\some\\path",
"program": "${workspaceFolder}\\some\\path\\to\\your_exe.exe",
"args": ["your_cmd_line_arg"],
"environment": [
{ "name": "BUILD_CONFIGURATION", "value": "Debug" }, // Unrelated example of an env var.
{ "name": "PATH", "value": "../../SomeDir/bin/Debug/bin;../../../SomeDir/Native/build"} // Optional.
],
"console":"integratedTerminal"
},
]
}
In Linux/WSL (Ubuntu 20.04)
Two options here.
Command-line LLDB
lldb build/your_exe your_cmd_line_arg # Launch the debugging session with LLDB debugger.
(lldb) r # Run.
<Ctrl+c> # Break.
(lldb) bt # Stack Trace.
(lldb) f 1 # Select the stack trace frame 1.
(lldb) f 5 # 5.
(lldb) q # Quit the debugger.
Visual Studio Code with the extension CodeLLDB (v1.7.0)
Make sure that you can run ./build/your_exe your_cmd_line_arg
in command line. For that you will likely need to adjust the LD_LIBRARY_PATH
env var.
export LD_LIBRARY_PATH=<absolute path to>/your/build:$LD_LIBRARY_PATH
# Make sure the var is set OK
env | grep LD_LIB
# Make sure all the dynamic lib paths are resolved correctly
ldd ./build/your_exe
# Make sure the executable runs
./build/your_exe your_cmd_line_arg
# Kill the run
<Ctrl+c>
Now start the VSCode.
The debuger configuration file example "{Repo Root}/.vscode/launch.json:":
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "DebuggingLLVMIR",
"program": "${workspaceFolder}/path/to/your/build/your_exe",
"args": ["your_cmd_line_arg"],
"cwd": "${workspaceFolder}/path/to/your",
//"environment": [
// { "name": "LD_LIBRARY_PATH", "value": "/mnt/c/path/to/your/build" },
// { "name": "PATH", "value": "/mnt/c/path/to/your/build"}
//]
}
]
}
lli
and look how it interprets every instrucion. You'd have to do some hacking to set a breakpoint, though. – Adela