Skip to content

How to use vscode for developing

Ahoy, the Fate Weaver edited this page Aug 10, 2023 · 12 revisions

如何使用vscode进行开发和调试

vscode插件

参考插件:

image

以下设置基于这些插件。

c/c++, cmake设置

参考.vscode/setting.json

{
    "cmake.cmakePath": "${workspaceFolder}/deps/3rd/usr/local/oceanbase/devtools/bin/cmake",
    "cmake.parallelJobs": 6,
    "cmake.generator": "Unix Makefiles",
    "cmake.buildDirectory": "${workspaceFolder}/build_debug",
    "cmake.configureArgs": [
        "-DCMAKE_BUILD_TYPE=Debug",
        "-DOB_USE_LLD=ON",
        "-DOB_BUILD_UNITTEST=ON"
    ],
    "C_Cpp.default.compilerPath": "${workspaceFolder}/deps/3rd/usr/local/oceanbase/devtools/bin/clang++",
    "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
    "cmake.debugConfig": {
        "MIMode": "gdb",
        "miDebuggerPath": "/usr/bin/gdb",
      }
}

Debug设置

参考.vscode/launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        // debug using vscode cmake tools.
        // choose debug target underneath.
        // debug target will be set automatically.
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            // Resolved by CMake Tools:
            "program": "${command:cmake.launchTargetPath}",
            "args": [],
            "stopAtEntry": false,
            // working dirctory resolved by CMake Tools.
            "cwd": "${command:cmake.getLaunchTargetDirectory}",
            // configure source file map manually to walk around debug files
            // not found bug in vscode.
            // Here shows an example for debugging source files in sql/parser
            "sourceFileMap":{
                "./build_debug/unittest/sql/parser/./unittest/sql/parser": {
                    "editorPath": "${workspaceFolder}/unittest/sql/parser"
                },
                "./build_debug/src/sql/parser/./src/sql/parser": {
                    "editorPath": "${workspaceFolder}/src/sql/parser"
                },
            },
            "environment": [
                {
                    // add the directory where our target was built to the PATHs
                    // it gets resolved by CMake Tools:
                    "name": "PATH",
                    "value": "${env:PATH}:${command:cmake.getLaunchTargetDirectory}"
                },
            ],
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
        // traditional debug configuration: set target manually.
        {
            "name": "(gdb) manual Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build_debug/unittest/sql/parser/test_parser",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }

    ]
}

跟踪observer进程

使用attach request

{
    "version": "0.2.0",
    "configurations": [
    
        {
            "name": "(gdb) Attach",
            "type": "cppdbg",
            "request": "attach",
            "program": "/root/.obd/repository/oceanbase-ce/4.1.0.1/oceanbase-ce/bin/observer",
            "processId": "${input:FindPID}",
            "MIMode": "gdb",
            "miDebuggerPath": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "sourceFileMap": {
                "./build_debug/src/observer/./src/observer/omt": {
                    "editorPath": "${workspaceFolder}/src/observer/omt"
                },
                "./build_debug/src/sql/parser/./src/sql/parser": {
                    "editorPath": "${workspaceFolder}/src/sql/parser",
                    "useForBreakpoints": true
                },
                "./build_debug/src/sql/./src/sql": {
                    "editorPath": "${workspaceFolder}/src/sql",
                    "useForBreakpoints": true
                },
                "./build_debug/src/sql/engine/expr/./src/sql/engine/expr": {
                    "editorPath": "${workspaceFolder}/src/sql/engine/expr",
                    "useForBreakpoints": true
                },
                "./build_debug/src/storage/./src/storage": {
                    "editorPath": "${workspaceFolder}/src/storage",
                    "useForBreakpoints": true
                },
                "./build_debug/src/observer/./src/observer/mysql": {
                    "editorPath": "${workspaceFolder}/src/observer/mysql",
                    "useForBreakpoints": true
                },
                "./build_debug/deps/oblib/src/rpc/./deps/oblib/src/rpc/": {
                    "editorPath": "${workspaceFolder}/deps/oblib/src/rpc/",
                    "useForBreakpoints": true
                },
                "./build_debug/src/observer/./deps/oblib/src/lib/allocator/": {
                    "editorPath": "${workspaceFolder}/deps/oblib/src/lib/allocator",
                    "useForBreakpoints": true
                },
                "./build_debug/deps/oblib/src/lib/./deps/oblib/src/lib/": {
                    "editorPath": "${workspaceFolder}/deps/oblib/src/lib",
                    "useForBreakpoints": true
                },
                "./build_debug/src/storage/./src/sql/engine/basic": {
                    "editorPath": "${workspaceFolder}/src/sql/engine/basic",
                    "useForBreakpoints": true
                }
            }
        }
    ],
    "inputs": [
        {
            "id": "FindPID",
            "type": "command",
            "command": "shellCommand.execute",
            "args": {
                "command": "ps -aux | grep /bin/observer | grep -v grep | awk '{print $2}'",
                "description": "Select your observer PID",
                "useFirstResult": true,
            }
        }
    ]
}

Ad-hoc Debug例程

如果想通过写Debug历程验证一些东西可以使用此方法:

  1. 在某个源码文件夹添加新的源码文件 image
  2. 在对应文件夹下Cmake文件中添加编译目标 image

Cmake Tools的UI使用

image

  • 上方为功能区;
  • 下方为调试入口,右边选择target.

参考文档

Cmake Tools