vscode 在debug模式给被调试程序传递环境变量

 

 

https://blog.csdn.net/jinxiaonian11/article/details/127965187

 

cppdbg `miDebuggerServerAddress` parameter generates warning · Issue #9195 · microsoft/vscode-cpptools (github.com)

"environment": [
        {
            "name": "PATH",
            "value": "${config:esp_toolchain_paths}:${env:PATH}"
        }
    ],
    "useExtendedRemote": true,
    "postRemoteConnectCommands": [ { "description": "Target Remote Device on Port 3333", "text": "target extended-remote :3333", "ignoreFailures": false }, { "description": "Respect Hardware Limitations (as prescribed by Espressif)", "text": "set remote hardware-watchpoint-limit 2", "ignoreFailures": false }, { "description": "Hard Reset and Immediately Halt", "text": "monitor reset halt", "ignoreFailures": false }, { "description": "Flush Internal Register Cache", "text": "flushregs", "ignoreFailures": false }, { "description": "Set Temporary Hardware Assisted Breakpoint at `call_start_cpu0`", "text": "thbreak call_start_cpu0", "ignoreFailures": false }, { "description": "Set Temporary Hardware Assisted Breakpoint at `app_main`", "text": "thbreak app_main", "ignoreFailures": false }, { "description": "Shutdown GDB Server on GDB Detach", "text": "monitor [target current] configure -event gdb-detach { shutdown }", "ignoreFailures": false }, ],

 

  "environment": [
    {
      "name": "LD_LIBRARY_PATH",
      "value": "/path_to_libImportantLib/"
    },
]

 

动态库so路径配置   (vscode-cpptools有bug暂时需要手动执行set solib-search-path /home/testuser/libtest)

In Launch.json set
"additionalSOLibSearchPath": "/path_to_libImportantLib/" 


"additionalSOLibSearchPath": "/path/to/symbols;/another/path/to/symbols"
"symbolSearchPath": "C:\\path\\to\\symbols;C:\\another\\path\\to\\symbols"


 

GDB命令

info sharedlibrary

set solib-search-path /home/testuser/libtest

 

 

 

在VS CODE的.vscode文件夹中launch.json,添加以下命令

        "postRemoteConnectCommands": [
            {
                "text": "source ${workspaceFolder}/.gdbinit"
            }
        ],

 

 

The Remote Debug configuration | CLion Documentation (jetbrains.com)

 

 

Debug shared libraries

To debug shared libraries, add the following commands in ~/.gdbinit or .lldbinit on the local machine. For more information about accessing and editing the script, refer to Using .gdbinit/.lldbinit configuration files.

  • set solib-search-paths for GDB

    However, by default, this command is executed on the debugger startup before attaching to the remote target (see the corresponding issue). As a workaround for this, you can use GDB hooks:

    define target hookpost-remote
    set solib-search-path /path/to/my.so:/path/to/sysroot:/path/to/vendorlibs
    break main  # if you also need the debug sessions to pause at the beginning
    end
     

    This way, GDB will execute set solib-search-path specified in the hook every time the remote target is connected.

  • settings set target.exec-search-paths /path/to/libs for LLDB

 

 

 

Additional symbols

If there are additional directories where the debugger can find symbol files (for example, .pdb files for the Visual Studio Windows Debugger), they can be specified by adding the additionalSOLibSearchPath (for GDB or LLDB) or symbolSearchPath (for the Visual Studio Windows Debugger).

For example:

    "additionalSOLibSearchPath": "/path/to/symbols;/another/path/to/symbols"

or

    "symbolSearchPath": "C:\\path\\to\\symbols;C:\\another\\path\\to\\symbols"

Locate source files

The source file location can be changed if the source files are not located in the compilation location. This is done by simple replacement pairs added in the sourceFileMap section. The first match in this list will be used.

For example:

"sourceFileMap": {
    "/build/gcc-4.8-fNUjSI/gcc-4.8-4.8.4/build/i686-linux-gnu/libstdc++-v3/include/i686-linux-gnu": "/usr/include/i686-linux-gnu/c++/4.8",
    "/build/gcc-4.8-fNUjSI/gcc-4.8-4.8.4/build/i686-linux-gnu/libstdc++-v3/include": "/usr/include/c++/4.8"
}
 

 

C/C++

{
  "name": "C++ Launch",
  "type": "cppdbg",
  "request": "launch",
  "program": "${workspaceFolder}/a.out",
  "args": ["arg1", "arg2"],
  "environment": [{ "name": "config", "value": "Debug" }],
  "cwd": "${workspaceFolder}"
}

 

https://code.visualstudio.com/docs/cpp/launch-json-reference#_environment

 

Python

  "env": {
          "ENV_TEST":"1",
          "ENV1":"12",
    }

https://code.visualstudio.com/docs/python/debugging#_env

 

使用.env文件方式

https://code.visualstudio.com/docs/python/environments#_environment-variables

By default, the Python extension looks for and loads a file named .env in the current workspace folder, then applies those definitions. The file is identified by the default entry "python.envFile": "${workspaceFolder}/.env" in your user settings (see General Python settings). You can change the python.envFile setting at any time to use a different definitions file.

 

 

Use of the PYTHONPATH variable

The PYTHONPATH environment variable specifies additional locations where the Python interpreter should look for modules. In VS Code, PYTHONPATH can be set through the terminal settings (terminal.integrated.env.*) and/or within an .env file.

When the terminal settings are used, PYTHONPATH affects any tools that are run within the terminal by a user, as well as any action the extension performs for a user that is routed through the terminal such as debugging. However, in this case when the extension is performing an action that isn't routed through the terminal, such as the use of a linter or formatter, then this setting won't have an effect on module look-up.

When PYTHONPATH is set using an .env file, it will affect anything the extension does on your behalf and actions performed by the debugger, but it will not affect tools run in the terminal.

If needed, you can set PYTHONPATH using both methods.

An example of when to use PYTHONPATH would be if you have source code in a src folder and tests in a tests folder. When running tests, however, those tests can't normally access modules in src unless you hard-code relative paths.

To solve this problem, you could add the path to src to PYTHONPATH by creating an .env file within your VS Code workspace.

PYTHONPATH=src

Then set python.envFile in your settings.json file to point to the .env file you just created. For example, if the .env file was in your workspace root, your settings.json would be set as shown:

"python.envFile": "${workspaceFolder}/.env"

The value of PYTHONPATH can contain multiple locations separated by os.pathsep: a semicolon (;) on Windows and a colon (:) on Linux/macOS. Invalid paths are ignored. If you find that your value for PYTHONPATH isn't working as expected, make sure that you're using the correct separator between locations for the operating system. For example, using a colon to separate locations on Windows, or using a semicolon to separate locations on Linux/macOS results in an invalid value for PYTHONPATH, which is ignored.

 

The Remote Debug configuration

Last modified: 16 April 2024

The Remote Debug configuration allows you to debug remotely under gdbserver or lldb-server. Use this configuration if you already have the executable with debug information and don't need CLion to build the project for you. This configuration is independent of a particular build system or project format.

With this configuration, you can remotely debug applications that were built by any build system. The only requirement is for the debug symbols to be present on the local machine.

Target platforms

 
 

There are no restrictions on target environment in case it supports gdbserver.

Your program can run remotely on any OS including Linux-based embedded like Raspbian OS (refer to Raspberry Pi OS Guide), on a cloud platform, or inside a Docker container. You can connect to any GDB stub that complies with the remote gdbserver protocol: for example, Qemu to debug OS kernels or OpenOCD to debug flashed firmware.

CLion's bundled GDB, which is used as a client debugger by default, is built with multiarch support, which makes it suitable for remote cross-platform debug in various Linux/Windows/macOS and embedded cases. Find the full list of the supported targets below.

Remote targets supported by the bundled GDB

 
 

General steps of the workflow

Find below a brief description of the steps to take for remote GDB/LLDB debug. More details for each step are given in the next chapters.

  1. Prepare a binary with debug information. If required, use a cross-platform toolchain.

    In the case of remote LLDB, the debugger does not download any debug symbols or system libraries automatically, so they should be present on the local machine. For cross-platform debug from macOS to Linux or from Linux to macOS, use cross-compilation via musl (or the alternatives), which will be shipped with the required libraries.

  2. Make sure to place the binary on the remote machine and symbol file on the local machine.

    Usually, the debug executable itself works well as a symbol file, or this can be a separate file as well.

     

    As Remote Debug configuration does not synchronize your files, you will need to keep track of binaries and symbol files and synchronize them manually upon any change.

  3. In CLion, create a Remote Debug configuration. The settings you specify are crucial for the debugger to be able to stop on breakpoints during a remote session, so we recommend you double-check the configuration set up.

  4. Launch the program under gdbserver/lldb-server on the remote machine.

  5. Back in CLion, start debugging the configuration you created on step 3.

Create a Remote Debug configuration

  1. Go to Run | Edit Configurations, click , and select Remote Debug from the list of templates.

  2. Remote Run/Debug Configuration

    Specify the following settings:

    • Debugger

      Select the client debugger: bundled GDB / bundled LLDB, one of the toolchain GDB debuggers, or a custom GDB binary.

    • 'target remote' args ('process connect' url for LLDB)

      In this field, provide the connection to the remote system.

      For GDB, this is IP address followed by a port number (like in our example) or connection details in another format.

      For LLDB, use the connect://host:port notation, for example, connect://127.0.0.1:1234.

    • Symbol file

      This is the local machine path to the file with debug symbols, which can be a non-stripped copy of the executable running on target or an ELF file containing only the debug info.

       

      If you are debugging a shared object library, avoid specifying the .so file as a symbol file. With .so as a symbol file, the debugger resolves the source location to two places: one to the actual address in memory, and the other to a file address within the .so file, which is not a valid memory address. As a result, stepping and other debugging actions do not work correctly.

       
      GDB
      LLDB
       

      Recent versions of GDB clients can transfer symbols from gdbserver automatically, so leaving this field empty may also work well if the executable running on target is a non-stripped binary.

    • Path mappings

      Use this pane to provide the paths on the target machine (the Remote column) to be mapped to local paths on host (the Local column).

    • Sysroot

      Sysroot is used by GDB/LLDB client to access the copies of target libraries with debug symbols on your local system (in order to set breakpoints and find source lines in the libraries code).

       
      GDB
      LLDB
       

      Most of the recent versions of GDB can download the files automatically, so it's not required that you specify this field. However, note that automatic download may slow down the debugging process significantly.

      If you choose to manually copy the libraries into a non-default local directory and want GDB to use it, set its path in this field.

Launch your program remotely under gdbserver/lldb-server

  1. To launch your application on the target, you can use the remote terminal or invoke the CLion's built-in SSH terminal (run Tools | Start SSH session and provide the credentials).

     
    GDB
    LLDB
     

    Run gdbserver with two arguments:

    • Either a device name (for a serial line connection) or a TCP hostname with the port number (:1234 in the example below).

    • The path and filename of the executable to be debugged, followed by the program input arguments, if any.

    remote ssh terminal
  2. Gdbserver/lldb-server then suspends the program at the entry point and waits for the client debugger to connect.

Start a remote debug session

  1. in CLion, place breakpoint in your code, then start a debug  session for the newly created Remote Debug configuration.

  2. CLion’s debugger will connect to the running remote process. The terminal will show the Remote debugging from host.. message, and you can also check the debugger console for the Debugger connected to.. message.

  3. Now you can inspect your code as if it was running locally (for example, step through and examine variables as usual):

    debug via gds/gdbserver

     

    Normally, your program should stop at breakpoints set from the IDE. In case it does not, double-check that symbol file and path mappings in the Run/Debug configuration settings are specified correctly.

Debug shared libraries

To debug shared libraries, add the following commands in ~/.gdbinit or .lldbinit on the local machine. For more information about accessing and editing the script, refer to Using .gdbinit/.lldbinit configuration files.

  • set solib-search-paths for GDB

    However, by default, this command is executed on the debugger startup before attaching to the remote target (see the corresponding issue). As a workaround for this, you can use GDB hooks:

    define target hookpost-remote
    set solib-search-path /path/to/my.so:/path/to/sysroot:/path/to/vendorlibs
    break main  # if you also need the debug sessions to pause at the beginning
    end
     

    This way, GDB will execute set solib-search-path specified in the hook every time the remote target is connected.

  • settings set target.exec-search-paths /path/to/libs for LLDB

posted @ 2022-12-09 17:20  sinferwu  阅读(233)  评论(0编辑  收藏  举报