vscode launch&attach及常用插件使用必备指南

安装插件

 better c++是必须的。

 c/c++必备插件参见:https://blog.csdn.net/weixin_44834554/article/details/129797480

java出身习惯于eclispe快捷键,可以安装eclipse keymap

 

自动同步到远程

  安装sftp自动同步,https://blog.csdn.net/zht2002/article/details/130349227

调试配置

.vscode下创建文件launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "attach lightdb process",
            "type": "cppdbg",
            "request": "attach",
            "name": "attach",
            "program": "/home/zjh/stage/lightdb-x/bin/lightdb",
            "processId": "${command:pickProcess}",
            "cwd": "${workspaceFolder}"
        },
        {
            "name": "start lightdb master",
            "type": "cppdbg",
            "request": "launch",
            "program": "/home/zjh/stage/lightdb-x/bin/lightdb",
            "args": ["-D","stage/lightdb-x/test_incre"],
            "stopAtEntry": false,
            "cwd": "/home/zjh",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description":  "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo helloworld",
            "type": "shell",
            "command": "echo aaa",
            "problemMatcher": [],
            "group": "test"
        },
        {
            "type": "shell",
            "label": "gcc default",
            "command": "gcc",
            "args": [
                "-I${workspaceFolder}/src/include",
                "${file}",
                "-g", // -g 生成调试信息,不然 无法使用断点
                "-o",
                
                "${fileDirname}/${fileBasenameNoExtension}.out"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        },
        {
            "type": "shell",
            "label": "gcc",
            "command": "gcc",
            "args": [
                "-I${workspaceFolder}/src/include",
                "${file}",
                "-g", // -g 生成调试信息,不然 无法使用断点
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        },
        {
            "type": "shell",
            "label": "g++",
            "command": "g++",
            "args": [
                "-I${workspaceFolder}/src/include",
                "${file}",
                "-g", // -g 生成调试信息,不然 无法使用断点
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ]
}

上述已经同时包含launch和attach。

基于make(推荐)

launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Lauch", // 启动配置的下拉菜单中显示的名称
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/main", // 将要进行调试的程序的路径, workspaceFolder指当前工作目录(即vscode打开的目录:hello),main指的是makefile编译后目标码(可执行程序)的名字
            "args": [], // 程序启动的参数
            "stopAtEntry": false, // 设置true时,程序将暂停在程序入口处, 即main()的第一个{位置
            "cwd": "${workspaceFolder}", // 调试时的工作目录
            "environment": [],
            "externalConsole": false, // 调试时,是否显示控制台串口
            "MIMode": "gdb", // 调试命令
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build_debug", // 使用哪个任务进行编译,需要指定tasks.json中的一个,这里选择用build_debug任务进行编译
            "miDebuggerPath": "/usr/bin/gdb" // 调试命令的路径
        }
    ]
}

task.json

{
    "tasks": [
        {
            "label": "build_debug", // 任务名称,调试时可以指定不用任务进行处理
            "type": "shell", // [shell, process], 定义任务作为作为进程运行还是在shell中作为命令运行; (测试没看出啥区别...)
            "command": "make", // 要执行的命令,可以是外部程序或者是shell命令。这里使用make编译命令
            "problemMatcher": [ // 要使用的问题匹配程序。可以是一个字符串或一个问题匹配程序定义,也可以是一个字符串数组和多个问题匹配程序。
                "$gcc"
            ],
            "group": { // 定义此任务属于的执行组。它支持 "build" 以将其添加到生成组,也支持 "test" 以将其添加到测试组。
                "kind": "build",
                "isDefault": true
            },
            "presentation": { // 配置用于显示任务输出并读取其输入的面板
                "echo": true, // 控制是否将执行的命令显示到面板中。默认值为“true”。
                "reveal": "always", // 控制运行任务的终端是否显示。可按选项 "revealProblems" 进行替代。默认设置为“始终”。
                "focus": false, // 控制面板是否获取焦点。默认值为“false”。如果设置为“true”,面板也会显示。
                "panel": "shared", // 控制是否在任务间共享面板。同一个任务使用相同面板还是每次运行时新创建一个面板。
                "showReuseMessage": true, // 控制是否显示“终端将被任务重用,按任意键关闭”提示
                "clear": false // 运行前清屏
            }
        },
        {
            "label": "build_release",
            "type": "shell",
            "command": "make",
            "args": ["CFLAGS = -O2"], // 编译参数, 替换makefile中让CFLAGS字段
            "dependsOn":["build_clean"], // 指定依赖让task, 即会先执行build_clean,然后再执行build_release
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "build_clean",
            "type": "shell",
            "command": "make",
            "args": ["clean"], // 相当于执行 make clean命令
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
    ],
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "dedicated",  //任务间不共享面板, 同一个任务共享相同面板.
        "showReuseMessage": false, //控制是否显示“终端将被任务重用, 按任意键关闭”提示.
        "clear": false
    },  //如果这是你最后一部分内容,就把这个逗号去掉。
    "version": "2.0.0"
}

 

 解决方法:配置的类型“cppdbg”不受支持  没安装C/C++拓展或者禁用了这个拓展,导致配置的类型“cppdbg”不受支持。安装C/C++拓展或者启用拓展就好了。

 

vscode error: Please specify the "MIDebuggerPath" option

解决办法:终端安装gdb -> sudo apt install gdb

 

 

vscode远程开发-调试模式下attach远程进程提示管理员权限失败的问题(之前都不提示,好像是装了minikube之后就有问题了,提示gdb必须root权限)

/usr/bin/env /bin/sh /tmp/Microsoft-MIEngine-Cmd-nkcnhtb0.4ih
Superuser access is required to attach to a process. Attaching as superuser can potentially harm your computer. Do you want to continue? [y/N]

参见https://blog.csdn.net/comhaqs/article/details/125312187,centos的话,用如下文件即可。

[root@lightdb-dev neo4j-community-5.15.0]# cat /usr/share/polkit-1/actions/org.freedesktop.pkexec.gdb.policy 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
 "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<policyconfig>
 
  <action id="org.freedesktop.pkexec.gdb-settings">
    <icon_name>gdb-settings</icon_name>
    <defaults>
      <allow_any>yes</allow_any>
      <allow_inactive>yes</allow_inactive>
      <allow_active>yes</allow_active>
    </defaults>
    <annotate key="org.freedesktop.policykit.exec.path">/usr/bin/gdb</annotate>
    <annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
  </action>
 
</policyconfig>

 

posted @ 2023-04-08 23:14  zhjh256  阅读(421)  评论(0编辑  收藏  举报