您的位置:首页 > Web前端

gdb来debug caffe代码

2016-12-17 14:20 351 查看
[ GDB ]
gdb中查看源代码执行路径
使用gdb来debug caffe代码基本分为以下几步:

**********************
step1: gdb
./build/tools/caffe
step2: b
154
step3: set args train --solver=/home/wenyangming/caffe/examples/cifar10/cifar10_quick_solver.prototxt 
b
n
其中可以通过使用layout来check达到conquegdb的功能
************************

tui就是 terminal UI的意思

gdb -tui 代码窗口相关命令:

info win 显示窗口的大小

layout next 切换到下一个布局模式

layout prev 切换到上一个布局模式

layout src 只显示源代码

layout asm 只显示汇编代码

layout split 显示源代码和汇编代码

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 clewnvimGdb and
pyclewn.

 vimGdbclewnpyclewn
platformunixunixall unix platforms supported by python - requires GNU gdb from macports on Mac Os X
Windows (Python 3 only)
languageCCPython 2.4 and above Python 3
vim modevim in a terminal, gvimgvimvim in a terminal (vim 7.3 or above required), gvim
vim interfacea vim patcha standalone program connected to gvim with a netbeans socketa standalone program connected to vim with a netbeans socket
pyclewn may be started from within vim
vim versiona different patch for each vim versionvim 6.3 and abovevim 7.0 and above
debuggersgdbgdbgdb, 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目录里面,然后编辑.bashrc,alias
vi=安装路径/vim.

vim-dbg 和Vimgdb插件没关系,Vimgdb可以去 vim 网站下载
Link: http://www.vim.org/scripts/script.php?script_id=3039

集成gdb的vim插件有两个,一个只需要编译一个库,另一个需要先打补丁后编译,楼主问的是需要先打补丁后编译的那个。
在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就正常使用了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: