Vim + Gdb 程序的完美集合

[ GDB ]php

gdb中查看源代碼執行路徑 html

tui就是 terminal UI的意思python

gdb -tui 代碼窗口相關命令: linux

info win 顯示窗口的大小 web

layout next 切換到下一個佈局模式 shell

layout prev 切換到上一個佈局模式 express

layout src 只顯示源代碼vim

layout asm 只顯示彙編代碼 windows

layout split 顯示源代碼和彙編代碼bash

layout regs 增長寄存器內容顯示

focus cmd/src/asm/regs/next/prev 切換當前窗口

refresh 刷新全部窗口

tui reg next 顯示下一組寄存器

tui reg system 顯示系統寄存器

update 更新源代碼窗口和當前執行點

winheight name +/- line 調整name窗口的高度

tabset nchar 設置tab爲nchar個字符

gdb在執行中,會自動跟蹤代碼

 

GDB

調用gdb編譯須要在cc後面加 -g參數再加-o;

[root@redhat home]#gdb 調試文件:啓動gdb

(gdb) l :(字母l)從第一行開始列出源碼

(gdb) break n :在第n行處設置斷點

(gdb) break func:在函數func()的入口處設置斷點

(gdb) info break: 查看斷點信息

(gdb) r:運行程序

(gdb) n:單步執行

(gdb) c:繼續運行

(gdb) p 變量 :打印變量的值

(gdb) bt:查看函數堆棧

(gdb) finish:退出函數

(gdb) shell 命令行:執行shell命令行

(gdb) set args 參數:指定運行時的參數

(gdb) show args:查看設置好的參數

(gdb) show paths:查看程序運行路徑;

           set environment varname [=value] 設置環境變量。如:set env USER=hchen;

            show environment [varname] 查看環境變量;

(gdb) cd 至關於shell的cd;

(gdb)pwd :顯示當前所在目錄

(gdb)info program: 來查看程序的是否在運行,進程號,被暫停的緣由

(gdb)clear 行號n:清除第n行的斷點

(gdb)delete 斷點號n:刪除第n個斷點

(gdb)disable 斷點號n:暫停第n個斷點

(gdb)enable 斷點號n:開啓第n個斷點

(gdb)step:單步調試若是有函數調用,則進入函數;與命令n不一樣,n是不進入調用的函數的

 

  • list :簡記爲 l ,其做用就是列出程序的源代碼,默認每次顯示10行。
  • list 行號:將顯示當前文件以「行號」爲中心的先後10行代碼,如:list 12
  • list 函數名:將顯示「函數名」所在函數的源代碼,如:list main
  • list :不帶參數,將接着上一次 list 命令的,輸出下邊的內容。
注意 :若是運行list 命令獲得相似以下的打印,那是由於在編譯程序時沒有加入 -g 選項:
(gdb) list
1       ../sysdeps/i386/elf/start.S: No such file or directory.
        in ../sysdeps/i386/elf/start.S

 

  • run:簡記爲 r ,其做用是運行程序,當遇到斷點後,程序會在斷點處中止運行,等待用戶輸入下一步的命令。
  • 回車:重複上一條命令。
  • set args:設置運行程序時的命令行參數,如:set args 33 55
  • show args:顯示命令行參數
  • continue:簡訊爲 c ,其做用是繼續運行被斷點中斷的程序。
  • break:爲程序設置斷點。
  • break 行號:在當前文件的「行號」處設置斷點,如:break  33
  • break 函數名:在用戶定義的函數「函數名」處設置斷點,如:break cb_button
  • info breakpoints:顯示當前程序的斷點設置狀況
  • disable breakpoints Num:關閉斷點「Num」,使其無效,其中「Num」爲 info breakpoints 中顯示的對應值
  • enable breakpoints Num:打開斷點「Num」,使其從新生效
  • step:簡記爲 s ,單步跟蹤程序,當遇到函數調用時,則進入此函數體(通常只進入用戶自定義函數)。
  • next:簡記爲 n,單步跟蹤程序,當遇到函數調用時,也不進入此函數體;此命令同 step 的主要區別是,step 遇到用戶自定義的函數,將步進到函數中去運行,而 next 則直接調用函數,不會進入到函數體內。
  • until:當你厭倦了在一個循環體內單步跟蹤時,這個命令能夠運行程序直到退出循環體。
  • finish: 運行程序,直到當前函數完成返回,並打印函數返回時的堆棧地址和返回值及參數值等信息。
  • stepi或nexti:單步跟蹤一些機器指令。
  • print 表達式:簡記爲 p ,其中「表達式」能夠是任何當前正在被測試程序的有效表達式,好比當前正在調試C語言的程序,那麼「表達式」能夠是任何C語言的有效表達式,包括數字,變量甚至是函數調用。
  • print a:將顯示整數 a 的值
  • print ++a:將把 a 中的值加1,並顯示出來
  • print name:將顯示字符串 name 的值
  • print gdb_test(22):將以整數22做爲參數調用 gdb_test() 函數
  • print gdb_test(a):將以變量 a 做爲參數調用 gdb_test() 函數
  • bt:顯示當前程序的函數調用堆棧。
  • display 表達式:在單步運行時將很是有用,使用display命令設置一個表達式後,它將在每次單步進行指令後,緊接着輸出被設置的表達式及值。如: display a
  • watch 表達式:設置一個監視點,一旦被監視的「表達式」的值改變,gdb將強行終止正在被調試的程序。如: watch a
  • kill:將強行終止當前正在調試的程序
  • help 命令:help 命令將顯示「命令」的經常使用幫助信息
  • call 函數(參數):調用「函數」,並傳遞「參數」,如:call  gdb_test(55)

 

  • layout:用於分割窗口,能夠一邊查看代碼,一邊測試:
  • layout src:顯示源代碼窗口
  • layout asm:顯示反彙編窗口
  • layout regs:顯示源代碼/反彙編和CPU寄存器窗口
  • layout split:顯示源代碼和反彙編窗口
  • Ctrl + L:刷新窗口
  • quit:簡記爲 q ,退出gdb

固然,gdb的功能遠不止這些,包括多進程/多線程/信號/遠程調試等功能在這裏均沒有說起,有須要的讀者能夠參考其它信息。

 

 

 

#sudo apt-cache search vim

exuberant-ctags - build tag file indexes of source code definitions
linuxdoc-tools - convert LinuxDoc SGML source into other formats
vim - Vi IMproved - enhanced vi editor
vim-common - Vi IMproved - Common files
vim-dbg - Vi IMproved - enhanced vi editor (debugging symbols)
vim-doc - Vi IMproved - HTML documentation
vim-gnome - Vi IMproved - enhanced vi editor - with GNOME2 GUI
vim-gui-common - Vi IMproved - Common GUI files
vim-runtime - Vi IMproved - Runtime files
vim-tiny - Vi IMproved - enhanced vi editor - compact version
transcode-utils - Transcode utility programs
apvlv - PDF viewer with Vim-like behaviour
bicyclerepair - A refactoring tool for python
bleachbit - delete unnecessary files from the system
cernlib-base - CERNLIB data analysis suite - common files
colordiff - tool to colorize 'diff' output
cream - VIM macros that make the VIM easier to use for beginners
dmtcp - Checkpoint/Restart functionality for Linux processes
dmtcp-dbg - Debug package for dmtcp
editmoin - edit MoinMoin wiki pages with your favourite editor
elvis-tiny - Tiny vi compatible editor for the base system
fim - a scriptable frame buffer and ascii art image viewer
get-flash-videos - Video downloader for various Flash-based video hosting sites
global - Source code search and browse tools
glogg - A smart interactive log explorer using Qt4
gramadoir - Irish language grammar checker (integration scripts)
jvim-canna - Japanized VIM (Canna version)
jvim-doc - Documentation for jvim (Japanized VIM)
libdmtcpaware-dev - DMTCP programming interface -- developer package
libdmtcpaware1 - DMTCP programming interface
liblatex-table-perl - Perl extension for the automatic generation of LaTeX tables
libtext-findindent-perl - module to heuristically determine indentation style
libtext-vimcolor-perl - syntax color text in HTML or XML using Vim
libvi-quickfix-perl - Perl support for vim's QuickFix mode
netrik - text mode WWW browser with vi like keybindings
notmuch-vim - thread-based email index, search and tagging (vim interface)
nvi - 4.4BSD re-implementation of vi
nvi-doc - 4.4BSD re-implementation of vi - documentation files
nvi - 4.4BSD re-implementation of vi
nvi-doc - 4.4BSD re-implementation of vi - documentation files
ocaml-tools - tools for OCaml developers
openshot - Create and edit videos and movies
pms - Practical Music Search, an MPD client
ranger - File manager with an ncurses frontend written in Python
shorlfilter - Text filter to shorten long URLs using online redirection database
sisu - documents - structuring, publishing in multiple formats and search
tmux - terminal multiplexer
txt2regex - A Regular Expression "wizard", all written with bash2 builtins
uzbl - Lightweight Webkit browser following the UNIX philosophy
vim-addon-manager - manager of addons for the Vim editor
vim-athena - Vi IMproved - enhanced vi editor - with Athena GUI
vim-gtk - Vi IMproved - enhanced vi editor - with GTK2 GUI
vim-latexsuite - view, edit and compile LaTeX documents from within Vim
vim-lesstif - Vi IMproved - enhanced vi editor (transitional package)
vim-nox - Vi IMproved - enhanced vi editor
vim-rails - plugins for vim to allow easier editing of Rails Applications
vim-scripts - plugins for vim, adding bells and whistles
vim-syntax-go - Syntax files to highlight Go in the Vim editor
vim-syntax-gtk - Syntax files to highlight GTK+ keywords in vim
vim-vimoutliner - script for building an outline editor on top of Vim
vimhelp-de - Vi IMproved - Documentation files (German translation)
zathura - PDF viewer with a minimalistic interface
kdesdk-scripts - scripts and data files for development
vim-puppet - syntax highlighting for puppet manifests in vim

搜索發現apt源沒有什麼vim gdb整合的軟件,意外發現有一些有用的包,能夠根據我的需求安裝.

sudo apt-get install colordiff

google以後,發現能夠經過安裝vimgdb實線vim和gdb的整合

 

[ Useful Link ]

Vim How-to

http://users.skynet.be/antoine.mechelynck/vim/index.htm

Vim Org - Downloading Vim

http://www.vim.org/download.php

vimgdb : Emacs like gdb interface to cterm vim

http://www.vim.org/scripts/script.php?script_id=3039

This script provides an emacs like GDB interface to vim. Woks in cygwin and unix terminals (no need for +clientserver).

Start the program like this
vimgdb <gdb-params> <exe-file> <exe-params>
This will open vim with two windows. Type the gdb commands in bottom window. The top window will show the current file line and breakpoints highlighted.

You can also jump to top window and use the following keys
<F6> run
<F5> continue
<F7> step into
<F8> step over
<F9> toggle break point
Ctr-p Watch the variable under Cursor/Visual selection
watch the output/variables in bottom window.
Known Bugs:
Don't use the quit gdb command (that will hang the vim), instead quit the vim.

install details

copy the vimgdb, vimgdb_msg to any directory in the system path
copy the vimgdb.vim to ~/.vim/misc directory
Needs perl & gdb

 

clewn gdb 

Clewn implements full gdb support in the vim editor: breakpoints, watch variables, gdb command completion, assembly windows, etc.

This may be done with clewn or vimGdb. There is also a third way with pyclewn, a python program with more features than clewn and vimGdb, and a tight integration in Vim. See the pyclewn web site for a table listing the differences between all three programs.

Clewn is a program controlling vim through the netBeans socket interface, it runs concurrently with vim and talks to vim. Clewn can only be used with gvim, the graphical implementation of vim, as vim on a terminal does not support netBeans. VimGdb is a vim patch implemented as a vim optional feature.

Both alternatives use the same base source code to interface with gdb. Clewn, as a standalone process, needs its own terminal. This is not the case with vimGdb, but a drawback is that a different patch must be applied to each new Vim version.

They both share the same features set, except clewn supports some features that vimGdb does not have:

  • display of gdb expression values in a balloon
  • gdb `run' commands do input and output on the clewn terminal, while vimgdb users must use the gdb 'tty' or 'attach' commands to control the debuged program input/output

mainpage: http://clewn.sourceforge.net/

userguide: http://clewn.sourceforge.net/doc.html

http://blog.csdn.net/dduwayne/article/details/6595674

 

CGDB

http://cgdb.sourceforge.net/

CGDB is a curses (terminal-based) interface to the GNU Debugger (GDB). Its goal is to be lightweight and responsive; not encumbered with unnecessary features.

The primary feature of CGDB is the constant presence of a source display (screenshots), updated as the program executes, to help keep you focused while debugging. The interface is inspired by the classic Unix text editor, vi. Those familiar with vi (or vim) should feel right at home using CGDB.

Comparison of clewn, vimGdb and pyclewn

The following table lists the differences between clewn, vimGdb and pyclewn.

  vimGdb clewn pyclewn
platform unix unix

all unix platforms supported by python - requires GNU gdb from macports on Mac Os X

Windows (Python 3 only)

language C C Python 2.4 and above Python 3
vim mode vim in a terminal, gvim gvim vim in a terminal (vim 7.3 or above required), gvim
vim interface a vim patch a standalone program connected to gvim with a netbeans socket

a standalone program connected to vim with a netbeans socket

pyclewn may be started from within vim

vim version a different patch for each vim version vim 6.3 and above vim 7.0 and above
debuggers gdb gdb gdb, pdb
gdb features  
  • watched variables
  • project file
  • tight integration with vim
  • gdb/mi interface
  • asynchronous gdb commands
  • watched variables
  • project file
pdb features    
  • interrupt the debuggee
  • attach to a running python process
  • the threadstack command
 

[ Blog ]

gdb簡單入門教程

http://os.chinaunix.net/a2005/1211/967/000000967327.shtml

http://fanqiang.chinaunix.net/program/other/2006-07-14/4834.shtml

http://bzhang.iteye.com/blog/376214

 

gdb中彙編調試

http://blog.sina.com.cn/s/blog_535413540100a82y.html

 

vim gdb ddd xxgdb精彩的程序調試 

http://easwy.com/blog/archives/advanced-vim-skills-vim-gdb-vimgdb/

 

[ Q&A ]

Q
上網搜安裝教程,安裝vim7.2 + vimgdb補丁,那麼系統自帶的vim7.3怎麼辦?

A
能夠安裝到其它目錄就行了,好比/opt/home/xxx目錄裏面,而後編輯.bashrcalias vi=安裝路徑/vim.

vim-dbg Vimgdb插件不要緊,Vimgdb能夠去 vim 網站下載
Link http://www.vim.org/scripts/script.php?script_id=3039

集成gdbvim插件有兩個,一個只須要編譯一個庫,另外一個須要先打補丁後編譯,樓主問的是須要先打補丁後編譯的那個。

在vim裏用gdb調試的兩種方式 

gdb是很好的程序調試器,而vim是強大的文本編輯器。若是二者能緊密的結合在一塊兒,將會給調試程序帶來極大的便利。事實上這件事早有人幫助完成了,下面簡單介紹兩種方式--gdbvim插件方式和pyclewn方式。
gdbvim插件方式
該方式比較簡單,它只須要下載一個vim插件和一個可執行的perl文件便可以正常使用。其原理就是封裝gdb,把gdb執行的動做引發的代碼記號發給vim的服務端端口,從而讓源代碼在vim中顯示,並跟蹤位置。這就要求vim支持命令服務器(+clientserver)和信號標記(+signs)功能,若是vim不支持,請從新編譯安裝vim或者直接安裝發行版提供的vim巨型版。


小提示:
使用以下方法檢測vim是否支持clientserver和signs,前面有+號表示支持,-號表示不支持。
複製內容到剪貼板
代碼:

www@linuxany.com:/tmp$ vim –version |grep -o ‘+clientserver\|+signs’
到vim官方的插件庫中去下載gdbvim插件,下載的文件中包括兩個文件gdbvim和gdbvim.vim,gdbvim就是封裝gdb的一個perl程序,將其放到你係統的執行路徑下($PATH中的任意目錄),若是不放也能夠,只是要你在執行時寫明路徑。gdbvim.vim是vim對應的插件,將其放到你vim的插件目錄下(linux裏用戶本身的插件目錄是~/.vim/plugin,能夠參看vim的幫助:h plugin)。
上述完成以後,就能夠正常使用了gdbvim了。最簡單的使用就是直接執行gdbvim,gdbvim會自動檢測有沒有vim運行做爲命令服務器, 若是沒有檢測到命令服務器的存在,它會以gvim的形式建立一個vim的命令服務器。接下來你就能正常使用它了。若是你不想gdbvim自動去啓動gvim,你能夠選擇以下方案:
# 讓vim以命令服務器方式啓動,服務器名爲test。
複製內容到剪貼板
代碼:

www@linuxany.com:/tmp$ vim –servername test
#查看當前vim的命令服務器的列表。
複製內容到剪貼板
代碼:

www@linuxany.com:/tmp$ vim –serverlist
#上面的列表中的名字都是大寫的,因此下面用了大寫的TEST,這個緣由還不太清楚,反正以–serverlist出來的列表爲準。
複製內容到剪貼板
代碼:

www@linuxany.com:/tmp$ gdbvim –server=TEST
到此,gdbvim就能夠正常使用了。
 
 
pyclewn方式
該方式的要求比較多一點,首先要求 python環境(python2.4+),其次要求vim在7.0+,有支持netbeans_intg和autocmd。只要前面的兩個需求知足了,安裝pyclew是很容易的事。下面是安裝步驟,能夠參考pyclewn官方的安裝說明。
複製內容到剪貼板
代碼:

www@linuxany.com:~/source/soft_source$ tar xf pyclewn-1.1.tar.gz
www@linuxany.com:~/source/soft_source$ cd pyclewn-1.1/
www@linuxany.com:~/source/soft_source/pyclewn-1.1$ sudo python setup.py install –force
#若是不想安裝到全局的,能夠選擇只安裝給當前用戶
複製內容到剪貼板
代碼:

www@linuxany.com:~/source/soft_source/pyclewn-1.1$ vimdir=$HOME/.vim python setup.py install –force –home=$HOME
完成後,直接運行pyclewn,默認爲打開一個gvim窗口,在這個窗口裏要先打開一個文件(:e /path/to/source.c),而後在vim的命令行模式下執行pyclew封裝事後的gdb命令(均在gdb的指令前加上了大C的前綴,默認爲這個大C,這個也能夠從新設置的,見pyclew -h)。固然了pyclew對這些命令默認做爲了鍵映射,能夠用:Cmapkeys打開它,這樣就可使用快捷鍵來替換輸入:Cnext等之類的命令。到此pyclewn就正常使用了。
相關文章
相關標籤/搜索