llvm-capstone/lldb/tools/lldb-vscode
Justin Bogner dcb6d212fd Reapply "[Option] Add "Visibility" field and clone the OptTable APIs to use it"
This reverts commit 4e3b89483a, with
fixes for places I'd missed updating in lld and lldb. I've also
renamed OptionVisibility::Default to "DefaultVis" to avoid ambiguity
since the undecorated name has to be available anywhere Options.inc is
included.

Original message follows:

This splits OptTable's "Flags" field into "Flags" and "Visibility",
updates the places where we instantiate Option tables, and adds
variants of the OptTable APIs that use Visibility mask instead of
Include/Exclude flags.

We need to do this to clean up a bunch of complexity in the clang
driver's option handling - there's a whole slew of flags like
CoreOption, NoDriverOption, and FlangOnlyOption there today to try to
handle all of the permutations of flags that the various drivers need,
but it really doesn't scale well, as can be seen by things like the
somewhat recently introduced CLDXCOption.

Instead, we'll provide an additive model for visibility that's
separate from the other flags. For things like "HelpHidden", which is
used as a "subtractive" modifier for option visibility, we leave that
in "Flags" and handle it as a special case.

Note that we don't actually update the users of the Include/Exclude
APIs here or change the flags that exist in clang at all - that will
come in a follow up that refactors clang's Options.td to use the
increased flexibility this change allows.

Differential Revision: https://reviews.llvm.org/D157149
2023-08-15 01:16:58 -07:00
..
syntaxes
BreakpointBase.cpp Revert "[lldb] Remove redundant .c_str() and .get() calls" 2022-12-19 13:52:10 +05:00
BreakpointBase.h Add formatting support for VSCode logpoints message 2022-11-01 08:43:30 -07:00
CMakeLists.txt
ExceptionBreakpoint.cpp
ExceptionBreakpoint.h
FifoFiles.cpp [lldb] Use std::optional instead of llvm::Optional (NFC) 2023-01-07 14:18:35 -08:00
FifoFiles.h Remove dependency from raw_ostream on <chrono> 2022-01-21 15:17:39 +01:00
FunctionBreakpoint.cpp Support logpoints in lldb-vscode 2022-06-20 16:22:12 -07:00
FunctionBreakpoint.h
IOStream.cpp [LLDB] Applying clang-tidy modernize-use-equals-default over LLDB 2022-03-31 13:21:49 -07:00
IOStream.h
JSONUtils.cpp Display PC instead of <unknown> for stack trace in vscode 2023-08-04 11:07:27 -07:00
JSONUtils.h [lldb-vscode] Adding support for the "disassemble" request. 2023-07-31 12:44:23 -04:00
lldb-vscode-Info.plist.in
lldb-vscode.cpp Reapply "[Option] Add "Visibility" field and clone the OptTable APIs to use it" 2023-08-15 01:16:58 -07:00
LLDBUtils.cpp Fix lldb-vscode frame id integer overflow 2023-07-26 16:12:41 -07:00
LLDBUtils.h
Options.td [lldb-vsocde] Cleaning up the usage of the Separate helper in Options.td. 2023-07-26 15:27:17 -04:00
OutputRedirector.cpp [lldb-vscode] Implement stderr/stdout on win32 and redirect lldb log to VSCode 2022-04-05 11:54:03 -07:00
OutputRedirector.h
package.json Revert "Add runToBinaryEntry option for lldb-vscode" 2022-11-23 13:26:11 +01:00
ProgressEvent.cpp [lldb] Use std::optional instead of llvm::Optional (NFC) 2023-01-07 14:18:35 -08:00
ProgressEvent.h [lldb] Use std::optional instead of llvm::Optional (NFC) 2023-01-07 14:18:35 -08:00
README.md [lldb-vscode] Creating a new flag for adjusting the behavior of evaluation repl expressions to allow users to more easily invoke lldb commands. 2023-07-19 21:04:12 +00:00
RunInTerminal.cpp JSON: llvm::Optional => std::optional 2022-12-16 07:56:52 +00:00
RunInTerminal.h
SourceBreakpoint.cpp [lldb-vscode] Adding support for column break points. 2023-07-06 16:33:22 -04:00
SourceBreakpoint.h
VSCode.cpp [lldb][lldb-vscode] Pretty print JSON to log files 2023-08-03 15:09:12 +00:00
VSCode.h [lldb-vscode] Adding support for the "disassemble" request. 2023-07-31 12:44:23 -04:00
VSCodeForward.h [lldb-vscode] Adding support for the "disassemble" request. 2023-07-31 12:44:23 -04:00

Table of Contents

Introduction

The lldb-vscode tool creates a command line tool that implements the Visual Studio Code Debug API. It can be installed as an extension for the Visual Studio Code and Nuclide IDE. The protocol is easy to run remotely and also can allow other tools and IDEs to get a full featured debugger with a well defined protocol.

Installation for Visual Studio Code

Installing the plug-in involves creating a directory in the ~/.vscode/extensions folder and copying the package.json file that is in the same directory as this documentation into it, and copying to symlinking a lldb-vscode binary into the bin directory inside the plug-in directory.

If you want to make a stand alone plug-in that you can send to others on unix systems:

$ mkdir -p ~/.vscode/extensions/llvm-org.lldb-vscode-0.1.0/bin
$ cp package.json ~/.vscode/extensions/llvm-org.lldb-vscode-0.1.0
$ cd ~/.vscode/extensions/llvm-org.lldb-vscode-0.1.0/bin
$ cp /path/to/a/built/lldb-vscode .
$ cp /path/to/a/built/liblldb.so .

It is important to note that the directory ~/.vscode/extensions works for users logged in locally to the machine. If you are remoting into the box using Visual Studio Code's Remote plugins (SSH, WSL, Docker) it will look for extensions on ~/.vscode-server/extensions only and you will not see your just installed lldb-vscode plug-in. If you want this plugin to be visible to remoting users, you will need to either repeat the process above for the ~/.vscode-server folder or create a symbolic link from it to ~/.vscode/extensions:

$ cd ~/.vscode-server/extensions
$ ln -s ~/.vscode/extensions/llvm-org.lldb-vscode-0.1.0  llvm-org.lldb-vscode-0.1.0

If you want to make a stand alone plug-in that you can send to others on macOS systems:

$ mkdir -p ~/.vscode/extensions/llvm-org.lldb-vscode-0.1.0/bin
$ cp package.json ~/.vscode/extensions/llvm-org.lldb-vscode-0.1.0
$ cd ~/.vscode/extensions/llvm-org.lldb-vscode-0.1.0/bin
$ cp /path/to/a/built/lldb-vscode .
$ rsync -av /path/to/a/built/LLDB.framework LLDB.framework

You might need to create additional directories for the liblldb.so or LLDB.framework inside or next to the bin folder depending on how the rpath is set in your lldb-vscode binary. By default the Debug builds of LLDB usually includes the current executable directory in the rpath, so these steps should work for most people.

To create a plug-in that symlinks into your lldb-vscode in your build directory:

$ mkdir -p ~/.vscode/extensions/llvm-org.lldb-vscode-0.1.0/bin
$ cp package.json ~/.vscode/extensions/llvm-org.lldb-vscode-0.1.0
$ cd ~/.vscode/extensions/llvm-org.lldb-vscode-0.1.0/bin
$ ln -s /path/to/a/built/lldb-vscode

This is handy if you want to debug and develope the lldb-vscode executable when adding features or fixing bugs.

Configurations

Launching to attaching require you to create a launch configuration. This file defines arguments that get passed to lldb-vscode and the configuration settings control how the launch or attach happens.

Launch Configuration Settings

When you launch a program with Visual Studio Code you will need to create a launch.json file that defines how your program will be run. The JSON configuration file can contain the following lldb-vscode specific launch key/value pairs:

parameter type req
name string Y A configuration name that will be displayed in the IDE.
type string Y Must be "lldb-vscode".
request string Y Must be "launch".
program string Y Path to the executable to launch.
args [string] An array of command line argument strings to be passed to the program being launched.
cwd string The program working directory.
env dictionary Environment variables to set when launching the program. The format of each environment variable string is "VAR=VALUE" for environment variables with values or just "VAR" for environment variables with no values.
stopOnEntry boolean Whether to stop program immediately after launching.
initCommands [string] LLDB commands executed upon debugger startup prior to creating the LLDB target. Commands and command output will be sent to the debugger console when they are executed.
preRunCommands [string] LLDB commands executed just before launching after the LLDB target has been created. Commands and command output will be sent to the debugger console when they are executed.
stopCommands [string] LLDB commands executed just after each stop. Commands and command output will be sent to the debugger console when they are executed.
launchCommands [string] LLDB commands executed to launch the program. Commands and command output will be sent to the debugger console when they are executed.
exitCommands [string] LLDB commands executed when the program exits. Commands and command output will be sent to the debugger console when they are executed.
terminateCommands [string] LLDB commands executed when the debugging session ends. Commands and command output will be sent to the debugger console when they are executed.
sourceMap [string[2]] Specify an array of path re-mappings. Each element in the array must be a two element array containing a source and destination pathname.
debuggerRoot string Specify a working directory to use when launching lldb-vscode. If the debug information in your executable contains relative paths, this option can be used so that lldb-vscode can find source files and object files that have relative paths.

Attaching Settings

When attaching to a process using LLDB you can attach in a few ways

  1. Attach to an existing process using the process ID
  2. Attach to an existing process by name
  3. Attach by name by waiting for the next instance of a process to launch

The JSON configuration file can contain the following lldb-vscode specific launch key/value pairs:

parameter type req
name string Y A configuration name that will be displayed in the IDE.
type string Y Must be "lldb-vscode".
request string Y Must be "attach".
program string Path to the executable to attach to. This value is optional but can help to resolve breakpoints prior the attaching to the program.
pid number The process id of the process you wish to attach to. If pid is omitted, the debugger will attempt to attach to the program by finding a process whose file name matches the file name from porgram. Setting this value to ${command:pickMyProcess} will allow interactive process selection in the IDE.
stopOnEntry boolean Whether to stop program immediately after launching.
waitFor boolean Wait for the process to launch.
initCommands [string] LLDB commands executed upon debugger startup prior to creating the LLDB target. Commands and command output will be sent to the debugger console when they are executed.
preRunCommands [string] LLDB commands executed just before launching after the LLDB target has been created. Commands and command output will be sent to the debugger console when they are executed.
stopCommands [string] LLDB commands executed just after each stop. Commands and command output will be sent to the debugger console when they are executed.
exitCommands [string] LLDB commands executed when the program exits. Commands and command output will be sent to the debugger console when they are executed.
terminateCommands [string] LLDB commands executed when the debugging session ends. Commands and command output will be sent to the debugger console when they are executed.
attachCommands [string] LLDB commands that will be executed after preRunCommands which take place of the code that normally does the attach. The commands can create a new target and attach or launch it however desired. This allows custom launch and attach configurations. Core files can use target create --core /path/to/core to attach to core files.

Example configurations

Launching

This will launch /tmp/a.out with arguments one, two, and three and adds FOO=1 and bar to the environment:

{
  "type": "lldb-vscode",
  "request": "launch",
  "name": "Debug",
  "program": "/tmp/a.out",
  "args": [ "one", "two", "three" ],
  "env": [ "FOO=1", "BAR" ],
}

Attach using PID

This will attach to a process a.out whose process ID is 123:

{
  "type": "lldb-vscode",
  "request": "attach",
  "name": "Attach to PID",
  "program": "/tmp/a.out",
  "pid": 123
}

Attach by Name

This will attach to an existing process whose base name matches a.out. All we have to do is leave the pid value out of the above configuration:

{
  "name": "Attach to Name",
  "type": "lldb-vscode",
  "request": "attach",
  "program": "/tmp/a.out",
}

If you want to ignore any existing a.out processes and wait for the next instance to be launched you can add the "waitFor" key value pair:

{
  "name": "Attach to Name (wait)",
  "type": "lldb-vscode",
  "request": "attach",
  "program": "/tmp/a.out",
  "waitFor": true
}

This will work as long as the architecture, vendor and OS supports waiting for processes. Currently MacOS is the only platform that supports this.

Loading a Core File

This loads the coredump file /cores/123.core associated with the program /tmp/a.out:

{
  "name": "Load coredump",
  "type": "lldb-vscode",
  "request": "attach",
  "coreFile": "/cores/123.core",
  "program": "/tmp/a.out"
}

Custom debugger commands

The lldb-vscode tool includes additional custom commands to support the Debug Adapter Protocol features.

startDebugging

Using the command lldb-vscode startDebugging it is possible to trigger a reverse request to the client requesting a child debug session with the specified configuration. For example, this can be used to attached to forked or spawned processes. For more information see Reverse Requests StartDebugging.

The custom command has the following format:

lldb-vscode startDebugging <launch|attach> <configuration>

This will launch a server and then request a child debug session for a client.

{
  "program": "server",
  "postRunCommand": [
    "lldb-vscode startDebugging launch '{\"program\":\"client\"}'"
  ]
}

repl-mode

Inspect or adjust the behavior of lldb-vscode repl evaluation requests. The supported modes are variable, command and auto.

  • variable - Variable mode expressions are evaluated in the context of the current frame. Use a ``` prefix on the command to run an lldb command.
  • command - Command mode expressions are evaluated as lldb commands, as a result, values printed by lldb are always stringified representations of the expression output.
  • auto - Auto mode will attempt to infer if the expression represents an lldb command or a variable expression. A heuristic is used to infer if the input represents a variable or a command. Use a ``` prefix to ensure an expression is evaluated as a command.

The initial repl-mode can be configured with the cli flag --repl-mode=<mode> and may also be adjusted at runtime using the lldb command lldb-vscode repl-mode <mode>.