Mac使用vimspector調試c++

vimspector安裝

vim配置文件加入一下內容

Plug 'puremourning/vimspector'
let g:vimspector_enable_mappings = 'HUMAN'

:PlugInstall安裝插件即可。

上面的HUMAN是插件配置的一套快捷命令。具體如下

Key Function API
F5 When debugging, continue. Otherwise start debugging. vimspector#Continue()
F3 Stop debugging. vimspector#Stop()
F4 Restart debugging with the same configuration. vimspector#Restart()
F6 Pause debugee. vimspector#Pause()
F9 Toggle line breakpoint on the current line. vimspector#ToggleBreakpoint()
<leader>F9 Toggle conditional line breakpoint on the current line. vimspector#ToggleBreakpoint( { trigger expr, hit count expr } )
F8 Add a function breakpoint for the expression under cursor vimspector#AddFunctionBreakpoint('<cexpr>')
F10 Step Over vimspector#StepOver()
F11 Step Into vimspector#StepInto()
F12 Step out of current function scope vimspector#StepOut()

vimspector配置

Language Status Switch Adapter Dependencies
C, C++, etc. Tested --all or --enable-c vscode-cpptools mono-core
Python Tested --all or --enable-python debugpy Python 2.7 or Python 3
Go Tested --enable-go vscode-go Go, [Delve][]
TCL Supported --all or --enable-tcl tclpro TCL 8.5
Bourne Shell Supported --all or --enable-bash vscode-bash-debug Bash v??
Node.js Supported --force-enable-node vscode-node-debug2 6<Node<12, Npm
Javascript Supported --force-enable-chrome debugger-for-chrome Chrome
Java Supported --force-enable-java vscode-java-debug Compatible LSP plugin (see later)
C# (dotnet core) Experimental --force-enable-csharp netcoredbg DotNet core
C# (mono) Experimental --force-enable-csharp vscode-mono-debug Mono
Python.legacy Legacy --force-enable-python.legacy vscode-python Node 10, Python 2.7 or Python3

在vimspector目錄運行 以上按照你本所需調試語言的命令即可。這裏我只需調試C++代碼。在我本地~/.vim/plugged/vimspector/下執行

./install.py --enable-c

這裏會去下載一個cpptools-osx.vsix文件 可以掛vpn運行安裝。或者下好放到對應的文件加即可。

~/.vim/plugged/vimspector/gadgets/macos下添加.gadgets.json文件

{
	"adapters": {
		"vscode-cpptools": {
			"attach": {
				"pidProperty": "processId",
				"pidSelect": "ask"
			},
		"command": [
			"${gadgetDir}/vscode-cpptools/debugAdapters/OpenDebugAD7"
		],
		"name": "cppdbg"
		}
	}
}

新建文件夾~/.vim/plugged/vimspector/gadgets/macos/.gadgets.d 添加lldb-vscode.json文件

{
	"adapters": {
		"lldb-vscode": {
			"variables": {
				"LLVM": {
					"shell": "brew --prefix llvm"
				}
			},
		"attach": {
			"pidProperty": "pid",
			"pidSelect": "ask"
		},
		"command": [
			"${LLVM}/bin/lldb-vscode"
		],
		"env": {
			"LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY":
			"YES"
		},
		"name":"lldb"
		}
	}
}

項目配置

在項目下添加.vimspector.json

{
  "configurations": {
    "cpp:launch": {
      "adapter": "vscode-cpptools",
      "configuration": {
        "name": "cpp",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/a.out",
        "args": ["*${ProgramArgs}"],
        "cwd": "${workspaceRoot}",
        "environment": [],
        "externalConsole": false,
        "stopAtEntry": true,
        "MIMode": "lldb",
        "logging": {
          "engineLogging": true
        }
      }
    }
  }
}

其中program是本地的項目編譯後的文件。這的mac下需要使用clang進行編譯,使用gcc無法調試。

編輯a.cpp文件

#include<stdio.h>
int main(){
	int a = 1;
	int b = 2;
	int c = a + b;
	printf("%d", c);
	return 0;
}

vim打開a.cpp文件 F9添加斷點 F5進入調試。結果如下圖所示

F11逐步調試即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章