llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.92k stars 11.52k forks source link

Support for disassembly in lldb-vscode #59264

Open eloparco opened 1 year ago

eloparco commented 1 year ago

Hello, I was reading the DAP specification and it seems to me that lldb-vscode doesn't implement DisassembleRequest yet.

What would be the steps needed to add support for it? And what's the easiest way to debug it? Trying to get some insights, I'd be happy to contribute to add this feature. Thanks

llvmbot commented 1 year ago

@llvm/issue-subscribers-lldb

eloparco commented 1 year ago

I made some progress. Now I'm able to get the Open Disassembly View options in the menu when I right-click. When I select it, a disassembly window appears (with some junk data for now).

To debug the application that I want to disassemble, I'm using this lldb-vscode configuration:

{
    "name": "Launch with lldb-vscode",
    "type": "lldb-vscode",
    "request": "launch",
    "program": "${workspaceFolder}/my_hello_world_executable",
    "args": [],
    "stopOnEntry": false,
    "cwd": "${fileDirname}",
    "env": [],
}

To debug lldb-vscode itself, I'm using the CodeLLDB extension:

 {
     "name": "Attach lldb-vscode",
     "type": "lldb",
     "request": "attach",
     "program": "/path/to/lldb-vscode",
}

I'll create a PR if I manage to get something decent. Any help or suggestion is much appreciated.

eloparco commented 1 year ago

When I run the lldb-vscode debugger config, I'd like to execute some lldb commands, for example to get the stack trace. How can I do that? That's what I get if I try:

bt
expression failed to parse:
error: <user expression 3>:1:1: use of undeclared identifier 'bt'
bt
^

I can't even attach another lldb process to it since lldb only supports one process per target.

jimingham commented 1 year ago

On Dec 1, 2022, at 3:06 AM, Enrico Loparco @.***> wrote:

When I run the lldb-vscode debugger config, I'd like to execute some lldb commands, for example to get the stack trace. How can I do that? That's what I get if I try:

bt expression failed to parse: error: <user expression 3>:1:1: use of undeclared identifier 'bt' bt ^

I have no idea where you are entering this, but apparently it's somewhere that's expecting an expression, and you are giving it an lldb command instead. You want to enter this into the debugger console in VSCode assuming such a thing exists.

I can't even attach another lldb process to it since lldb only supports one process per target.

It wouldn't really make sense to support debugging multiple processes at a time through one target, since the target holds the executable file being debugged, the breakpoints for that debug session, etc. If a target had multiple processes they would all have to share the same executable through the target, and all your breakpoints, watchpoints, etc.

Instead, lldb supports multiple targets. You can call "target create" as many times as you want, and then "target select" to switch among them. That's how the lldb driver program supports multiprocess debugging. You can also create multiple SBDebuggers if you want to keep the debug sessions entirely separate (that's how Xcode does it). I have no idea how VSCode handles this, but it certainly could.

Jim

— Reply to this email directly, view it on GitHub https://github.com/llvm/llvm-project/issues/59264#issuecomment-1333593094, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADUPVW4HWI4U4MHTKGLWIEDWLCBB5ANCNFSM6AAAAAASPNXOCQ. You are receiving this because you are on a team that was mentioned.

eloparco commented 1 year ago

I have no idea where you are entering this, but apparently it's somewhere that's expecting an expression, and you are giving it an lldb command instead. You want to enter this into the debugger console in VSCode assuming such a thing exists.

I'm entering it into the DEBUG CONSOLE in VSCode. Same place where I type it for other extensions (e.g. bt for CodeLLDB, -exec bt for cppdbg).

I just want to have control of the debugger to get stack traces and disassemble, so that I can test the changes that I'm making in lldb-vscode.cpp.

What's the equivalent of

    "name": "Launch with lldb-vscode",
    "type": "lldb-vscode",
    "request": "launch",
    "program": "${workspaceFolder}/my_hello_world_executable",
    "args": [],
    "stopOnEntry": false,
    "cwd": "${fileDirname}",
    "env": [],
}

from command line? Maybe if I try with that I can get the info that I want. Thanks

jimingham commented 1 year ago

$ lldb (lldb) break set -f Foo.c -l 10 (lldb) run

But the lldb driver and lldb-vscode are separate programs that both load the lldb shared library, so you won't get into lldb-vscode.cpp that way. There are a bunch of tests in lldb/test/API/tools/lldb-vscode. When you eventually get this working you're going to have to write some tests for it in the same style as these other tests. So it might be easier to just start writing your test and use that to poke at your code as you develop it.

Otherwise, you need to get the attention of someone who actually uses VSCode with the lldb extensions, which is not me...

I find it really hard to believe it doesn't have a debugger command console, but the error you showed was the error you get from running the text you typed in the expr command. Somebody was doing expr bt for you... So whatever that is it wasn't the lldb debugger command console.

Sorry I can't be of more help...

Jim

On Dec 1, 2022, at 5:33 PM, Enrico Loparco @.***> wrote:

I have no idea where you are entering this, but apparently it's somewhere that's expecting an expression, and you are giving it an lldb command instead. You want to enter this into the debugger console in VSCode assuming such a thing exists.

I'm entering it into the DEBUG CONSOLE in VSCode. Same place where I type it for other extensions (e.g. bt for CodeLLDB, -exec bt for cppdbg).

I just want to have control of the debugger to get stack traces and disassemble, so that I can test the changes that I'm making in lldb-vscode.cpp.

What's the equivalent of

"name": "Launch with lldb-vscode",
"type": "lldb-vscode",
"request": "launch",
"program": "${workspaceFolder}/my_hello_world_executable",
"args": [],
"stopOnEntry": false,
"cwd": "${fileDirname}",
"env": [],

} from command line? Maybe if I try with that I can get the info that I want. Thanks

— Reply to this email directly, view it on GitHub https://github.com/llvm/llvm-project/issues/59264#issuecomment-1334654406, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADUPVW5DITQCJFJMY6XU4MLWLFGX7ANCNFSM6AAAAAASPNXOCQ. You are receiving this because you are on a team that was mentioned.

eloparco commented 1 year ago

Thanks. Any API that I can use to retrieve the start of the current stack frame from lldb-vscode.cpp? VS Code usually asks for negative offsets from the current instruction and if the offset is large I don't want to risk reading outside the current stack frame.

In general, I'd like to know how I can get the low and high PC for the current stack frame (to avoid overstepping the boundaries when I try to get the instructions used in that stack frame).

eloparco commented 1 year ago

I ended up caching the current stack frame after each StackTraceRequest. In that way I can get the address range (low and high pc) from the cached stack frame when handling DisassembleRequests.

eloparco commented 1 year ago

It took me some time, but I requested a review https://reviews.llvm.org/D140358.

This is how it looks like: image