VS code 调试配置 Debugging

One of the key features of Visual Studio Code is its great debugging support. VS Code's built-in debugger helps accelerate your edit, compile, and debug loop.

VS code 最关键的一个特性就是能够支持调试功能。

VS code 调试配置 Debugging

Debugger extensions#

VS Code has built-in debugging support for the Node.js runtime and can debug JavaScript, TypeScript, or any other language that gets transpiled to JavaScript.

For debugging other languages and runtimes (including PHPRubyGoC#PythonC++PowerShell and many others), look for Debuggers extensions in the VS Code Marketplace or select Install Additional Debuggers in the top-level Run menu.

Below are several popular extensions which include debugging support:

通过可拓展的插件,VS几乎支持所有的编程语言的调试。


VS Code官方Nj的调试例子:

Start debugging#

The following documentation is based on the built-in Node.js debugger, but most of the concepts and features are applicable to other debuggers as well.

It is helpful to first create a sample Node.js application before reading about debugging. You can follow the Node.js walkthrough to install Node.js and create a simple "Hello World" JavaScript application (app.js). Once you have a simple application set up, this page will take you through VS Code debugging features.

调试的启动就是用[RUN]命令即可,但是,需要做一些配置的工作:

Run view#

To bring up the Run view, select the Run icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D.

 

VS code 调试配置 Debugging

VS code 调试配置 Debugging

The Run view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.

If running and debugging is not yet configured (no launch.json has been created), VS Code shows the Run start view.

从官网的说明看,配置的工作主要是launch.json这个json

Launch configurations#

To run or debug a simple app in VS Code, select Run and Debug on the Debug start view or press F5 and VS Code will try to run your currently active file.

However, for most debugging scenarios, creating a launch configuration file is beneficial because it allows you to configure and save debugging setup details. VS Code keeps debugging configuration information in a launch.json file located in a .vscode folder in your workspace (project root folder) or in your user settings or workspace settings.

To create a launch.json file, click the create a launch.json file link in the Run start view.

直接运行F5人【RUN】是可以进行调试你现在的文件,这个我做了试验,可以进行,但是,最好是做一个配置的文件这样可以记录你的环境情况。

此外,从官网看,VS的工作空间,就是你的项目文件的所在根目录:

然后,新建的launch.json 需要放在一个叫[.vscde]的文件夹下面。

VS code 调试配置 Debugging

 看了很多博客,说要自己做一个目录和文件,其实,如果你是第一次,初恋,那么系统会自己帮你生成一个文件夹和文件。

VS code 调试配置 Debugging

而且,会依据你的环境,自动帮你搞一个你要的配置环境。

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": ["<node_internals>/**"],
      "program": "${workspaceFolder}\\app.js"
    }
  ]
}

 Note that the attributes available in launch configurations vary from debugger to debugger. You can use IntelliSense suggestions (Ctrl+Space) to find out which attributes exist for a specific debugger. Hover help is also available for all attributes.

 当然每个调试的装载的配置是不同的,VS会自动给出对每一个特定的调试器属性的定义的内涵。

【只需要鼠标停留在上面】

下图是C++自动生成的配置文件,和NJ有不同 

VS code 调试配置 Debugging

Launch versus attach configurations#

In VS Code, there are two core debugging modes, Launch and Attach, which handle two different workflows and segments of developers. Depending on your workflow, it can be confusing to know what type of configuration is appropriate for your project.

If you come from a browser Developer Tools background, you might not be used to "launching from your tool," since your browser instance is already open. When you open DevTools, you are simply attaching DevTools to your open browser tab. On the other hand, if you come from a server or desktop background, it's quite normal to have your editor launch your process for you, and your editor automatically attaches its debugger to the newly launched process.

The best way to explain the difference between launch and attach is to think of a launch configuration as a recipe for how to start your app in debug mode before VS Code attaches to it, while an attach configuration is a recipe for how to connect VS Code's debugger to an app or process that's already running.

VS Code debuggers typically support launching a program in debug mode or attaching to an already running program in debug mode. Depending on the request (attach or launch), different attributes are required, and VS Code's launch.json validation and suggestions should help with that.

 VS支持两种配置的方式,分别是桌面开发的Lauch和浏览器开发的attach两种模式。

Add a new configuration#

To add a new configuration to an existing launch.json, use one of the following techniques:

  • Use IntelliSense if your cursor is located inside the configurations array.
  • Press the Add Configuration button to invoke snippet IntelliSense at the start of the array.
  • Choose Add Configuration option in the Run menu.

 在原有的配置下,可以再增加配置:

VS code 调试配置 Debugging


【作者案】最常用的配置增加是增加头文件:举例如下:

按「F1」启动指令输入框,输入 C/C++,选择第一项 Edit Configuration: 

VS code 调试配置 Debugging

会自动生成C/C++的配置文件

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.18362.0",
            "compilerPath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\bin\\Hostx64\\x64\\cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-msvc-x64"
        }
    ],
    "version": 4
}

我们先不增加头文件按【RUN】进行测试,

VS code 调试配置 Debugging

点击错误的标识叉号,错误会展开:

VS code 调试配置 Debugging

点击灯泡,他会提示你怎么做,

【笔者案】我选择了编辑,库文件编辑,这时候,会打开C++的进一步的配置,

 

如上图会报错,找不到头文件:VS code 调试配置 Debugging

对配置文件进行修改,给出include的文件的路径后找不到文件的报错去除。

VS code 调试配置 Debugging

 这个配置最终会给写到jason的文件里面。

配置的各个参数的列表,官网里面给了。可参照给出的参考连接。这些可以理解为配置的某种写法。其他断点的功能也没有什么新意。也许制定函数和数据断点是比较方便,但是这个功能其他的调试器也有。

Vs CODE支持C/S的架构,能够同时进行服务器端和客户端的调试,这个应该很有用。


参考:

Debugging in Visual Studio CodeVS code 调试配置 Debugginghttps://code.visualstudio.com/docs/editor/debugging#_launch-configurations

2 个步骤为 VSCode 配置工程头文件路径! - 登龙 - 博客园 (cnblogs.com)

上一篇:Linux中set -x 与 set +x命令的使用


下一篇:使用SpaCy分词