linux vim debugger,Vim 调试:termdebug 入门
簡介
termdebug 是從 Vim 8.1 開始內(nèi)置的調(diào)試插件,僅支持 GDB。
本教程僅在 Linux 下(Ubuntu 16.04)測試通過。
安裝
將 Vim 升級至 8.1 或以上版本。
GDB 需升級至 7.12 或以上版本。
啟動
默認情況下需手動加載 termdebug 插件:
:packadd termdebug
假設(shè)我們有一個簡單的 helloworld.cpp 文件:
#include
using namespace std;
int main() {
cout << "hello world" << endl;
int in;
cin >> in;
cout << "you input " << in << endl;
return 0;
}
我們將其編譯為二進制文件 helloworld:
g++ -g helloworld.cpp -o helloworld
現(xiàn)在,我們在 Vim 中啟動 termdebug 來調(diào)試這個程序:
:Termdebug helloworld
這時 termdebug 會為我們開三個窗口。
其中, GDB 窗口提供 GDB 原生操作;程序窗口供被調(diào)試程序使用,提供 IO 功能;源碼窗口提供源碼交互。
在 GUI 版本的 Vim (如 gvim)中,源碼窗口還提供交互按鈕:
我們可以通過 按鍵切換不同窗口。
調(diào)試程序
我們既可以在 GDB 窗口中調(diào)試,也可以在源碼窗口中調(diào)試。
GDB 調(diào)試常用指令:
- file bin 加載名為 bin 的二進制文件
- CTRL-C 中斷程序
- run/r 運行
- next/n 執(zhí)行當前行,停在下一行 (step over)
- step/s 執(zhí)行當前行,進入下一層函數(shù) (step in)
- finish 執(zhí)行直至離開當前函數(shù)
- where 顯示棧
- continue/c 繼續(xù)執(zhí)行
- break/b N 在第 N 行加斷點
- break/b f 在函數(shù) f 處加斷點
- delete 刪除所有斷點
更多 GDB 使用方法請參考官方文檔: https://sourceware.org/gdb/current/onlinedocs/gdb/
在源碼窗口中的調(diào)試指令:
:Run [args] 運行程序,可帶參數(shù) [args],或沿用上一次運行的參數(shù)
:Arguments {args} 設(shè)置下一次運行所用參數(shù)
:Break 在當前行加斷點
:Clear 刪除當前行的一個斷點
:Step = gdb "step"
:Over = gdb "next"
:Finish = gdb "finish"
:Continue = gdb "continue"
:Stop 中斷程序
如果覺得手動輸入調(diào)試指令太麻煩,可以在個人的 .vimrc 文件中自定義 keymap 來執(zhí)行這些命令,如用 來添加斷點:
nnoremap :Break
現(xiàn)在我們來演示一下調(diào)試上面的已經(jīng)加載好的 helloworld 程序:
先移動至 GDB 窗口,輸入 b main 以在 main 函數(shù)入口處添加斷點;
在 GDB 窗口輸入 r,程序開始運行,并停在 main 函數(shù)入口;
在 GDB 窗口輸入 n,程序停在 cout << "hello world" << endl; 這一行;
移動至源碼窗口,輸入 :Continue,程序繼續(xù)運行,并在 cin >> in 處等待用戶輸入;
移動至程序窗口(IO窗口),輸入數(shù)字 3 并回車,可以看到程序輸出 you input 3,并運行直至結(jié)束;
移動至 GDB 窗口,輸入 q,退出調(diào)試。
獲取幫助文檔
在 Vim 窗口中輸入 :h terminal-debug 閱讀詳細的幫助文檔。
總結(jié)
以上是生活随笔為你收集整理的linux vim debugger,Vim 调试:termdebug 入门的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 毫秒转分钟和秒_PDF如何转换
- 下一篇: 计算机组装与维护预实验报告,计算机组装与