vscode linux debug skills & vscode settings.json & launch.json

  1. vscode 在虛擬機中運行需要禁用 3D 加速。或者在 .bashrc 中添加 alias code="code --disable-gpu"
  2. settings.json 常用配置:
{
    "git.enabled": false,  // 關掉vscode git插件

    // Controls if quick suggestions should show up while typing
    "editor.quickSuggestions": {
        "other": true,
        "comments": false,
        "strings": true
    },

    "files.exclude": {  // 去除某些目錄
        "*build": true,
        "*.tar.gz": true,
        "*.zip": true
    },
    "search.exclude": {  // 去除某些目錄中的搜索
        "*build": true,
        "*.tar.gz": true,
        "*.zip": true
    },

    // Controls if suggestions should be accepted on 'Enter' - in addition to 'Tab'. Helps to avoid ambiguity between inserting new lines or accepting suggestions. The value 'smart' means only accept a suggestion with Enter when it makes a textual change
    "editor.acceptSuggestionOnEnter": "on",

    // Controls the delay in ms after which quick suggestions will show up.
    "editor.quickSuggestionsDelay": 10,

    // Controls if suggestions should automatically show up when typing trigger characters
    "editor.suggestOnTriggerCharacters": true,

    // Controls if pressing tab inserts the best suggestion and if tab cycles through other suggestions
    "editor.tabCompletion": "on",

    // Controls whether sorting favours words that appear close to the cursor
    "editor.suggest.localityBonus": true,

    // Controls how suggestions are pre-selected when showing the suggest list
    "editor.suggestSelection": "recentlyUsed",

    // Enable word based suggestions
    "editor.wordBasedSuggestions": true,

    "editor.parameterHints.enabled": true,
    "workbench.tree.indent": 20,  // Explorer 中目錄樹的寬度
    "editor.mouseWheelScrollSensitivity": 2,
    "editor.smoothScrolling": false,
    
    "editor.trimAutoWhitespace": true,
    "files.trimTrailingWhitespace": true,  // 保存時自動刪除行尾的空格
    "editor.renderWhitespace": "all",  // 將空格顯示爲 "."
    "C_Cpp.clang_format_style": "Google",  // c++ format格式
    
    "C_Cpp.errorSquiggles": "Disabled",
    "C_Cpp.default.cppStandard": "c++11",
    "C_Cpp.loggingLevel": "Debug",
}
  1. 常用 debug 配置文件:
{
    "name": "(gdb)  Debug1",
    "type": "cppdbg",
    "request": "launch",
    "program": "/home/key/ws/hello.out",
    "args": ["param1", "param2"],
    "stopAtEntry": true,
    "cwd": "/home/key/ws/",
    "environment": [], //, {"name":"alloc_dealloc_mismatch", "value":"0"}],
    "externalConsole": false,
    "MIMode": "gdb",
    "setupCommands": [
        {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing", // -gdb-set follow-fork-mode child",
            "ignoreFailures": true
        },
        {
            "description": "Additional .so lib",
            "text": "set env LD_LIBRARY_PATH=/usr/local/mysql/connector-c++-8.0/lib64/",
            "ignoreFailures": true
        },
        {"text": "skip -gfi /usr/include/c++/*/*/*"},  // gdb not step into stl
        {"text": "skip -gfi /usr/include/c++/*/*"},
        {"text": "skip -gfi /usr/include/c++/*"}
    ],
    // "logging": { "engineLogging": true },
    "sourceFileMap": {
        "/opt/in/docker1":"/home/key/ws1"  // option
    }
    "logging": { "engineLogging": true }  // 輸出信息到Debug Console
},
{
    "name": "(gdb) Docker Launch1",
    "type": "cppdbg",
    "request": "launch",
    "program": "/opt/hello.out",
    "cwd": "/root",
    "args": [],
    "stopAtEntry": true,
    "environment": [],
    "externalConsole": false,
    "pipeTransport": {
        "debuggerPath": "/usr/bin/gdb",
        "pipeProgram": "docker",
        "pipeArgs": ["exec", "-i", "dockerName", "bash", "-c"],  // bahs -c: read string as cmd
        "pipeCwd": "${workspaceRoot}"
    },
    // "sourceFileMap": {
    //     "/root/src":"/home/user/ws/src"  // option, 需要從docker中複製一份src到 /home/user/ws 中。
    // },
	"MIMode": "gdb",
	"setupCommands": [{
	    "description": "Enable pretty-printing for gdb",
	    "text": "-enable-pretty-printing",
	    "ignoreFailures": true
	},
	{
	    "description": "Additional libs for gdb",
	    "text": "set env LD_LIBRARY_PATH=/usr/local/mysql/connector-c++-8.0/lib64/"
	}],
	"logging": { "engineLogging": true }
}, 

注意,如果希望vscode debug 運行在docker中的程序,則需要在這樣啓動docker: docker run --privileged xxx (提升所有權限)或者 docker run --security-opt="seccomp=unconfined" --cap-add=SYS_PTRACE xxx (disable address space randomization + enable PTRACE).

  1. vscode Debug 查看函數返回值: step into 函數,然後再 vscode DEBUG CONSOLE 中輸入 "-exec finish"
  2. vscode Debug子進程:需要提前在子進程中打上斷點, 並且 break before fork && "-exec -gdb-set follow-fork-mode child"
  3. vscode Debug 查看某個地址的內存值: -exec x/4ub 0x555556e9ba10 或者 -exec x/4xb

VSCode快捷鍵:

https://code.visualstudio.com/shortcuts/keyboard-shortcuts-linux.pdf

按住 shift + 鼠標點選  (塊選,多行選擇)
Alt + Shift + 上下     (上下多光標)
Alt + Shift + 左右     (左右單詞—— word 選擇)
Alt + Shift + i		   (在選擇行的行尾插入光標)
Alt + Shift + 鼠標拖動  (多光標快選)
Alt + 鼠標點擊		(單個插入光標)
Ctrl + U					(undo 多光標操作)
Ctrl + Shift + L 		(選中所有當前所選的字符串)

Ctrl + 左右		(左右移動一個單詞——word)
Ctrl + L			(單行選擇——配合Ctrl+C Ctrl+V實現複製一行)
Ctrl + P			(打開文件)

以上配合 Ctrl K + Ctrl F (format當前所選),以及 Ctrl + Shift + P
Trim Trailing Whitespace
... to be continue...
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章