📄 gdb调试精粹及使用实例.htm
字号:
在程序中设置一个断点<BR>cd 改变当前工作目录<BR>clear 删除刚才停止处的断点<BR>commands
命中断点时,列出将要执行的命令<BR>continue 从断点开始继续执行<BR>delete
删除一个断点或监测点;也可与其他命令一起使用<BR>display 程序停止时显示变量和表达时<BR>down
下移栈帧,使得另一个函数成为当前函数<BR>frame 选择下一条continue命令的帧<BR>info 显示与该程序有关的各种信息<BR>jump
在源程序中的另一点开始运行<BR>kill 异常终止在gdb 控制下运行的程序<BR>list 列出相应于正在执行的程序的原文件内容<BR>next
执行下一个源程序行,从而执行其整体中的一个函数<BR>print 显示变量或表达式的值<BR>pwd 显示当前工作目录<BR>pype
显示一个数据结构(如一个结构或C++类)的内容<BR>quit 退出gdb<BR>reverse-search 在源文件中反向搜索正规表达式<BR>run
执行该程序<BR>search 在源文件中搜索正规表达式<BR>set variable 给变量赋值<BR>signal
将一个信号发送到正在运行的进程<BR>step 执行下一个源程序行,必要时进入下一个函数<BR>undisplay
display命令的反命令,不要显示表达式<BR>until 结束当前循环<BR>up 上移栈帧,使另一函数成为当前函数<BR>watch
在程序中设置一个监测点(即数据断点)<BR>whatis
显示变量或函数类型<BR>****************************************************<BR>GNU的调试器称为gdb,该程序是一个交互式工具,工作在字符模式。在
X Window 系统中,有一个gdb的前端图形工具,称为xxgdb。gdb 是功能强大的调试程序,可完成如下的调试任务:<BR>* 设置断点;<BR>*
监视程序变量的值;<BR>* 程序的单步执行;<BR>* 修改变量的值。<BR>在可以使用 gdb 调试程序之前,必须使用 -g 选项编译源文件。可在
makefile 中如下定义 CFLAGS 变量:<BR>CFLAGS = -g<BR>运行 gdb 调试程序时通常使用如下的命令:<BR>gdb
progname</P>
<P>在 gdb 提示符处键入help,将列出命令的分类,主要的分类有:<BR>* aliases:命令别名<BR>*
breakpoints:断点定义;<BR>* data:数据查看;<BR>* files:指定并查看文件;<BR>* internals:维护命令;<BR>*
running:程序执行;<BR>* stack:调用栈查看;<BR>* statu:状态查看;<BR>* tracepoints:跟踪程序执行。<BR>键入
help 后跟命令的分类名,可获得该类命令的详细清单。</P>
<P>gdb 的常用命令<BR>命令 解释<BR>break NUM 在指定的行上设置断点。<BR>bt
显示所有的调用栈帧。该命令可用来显示函数的调用顺序。<BR>clear 删除设置在特定源文件、特定行上的断点。其用法为clear
FILENAME:NUM<BR>continue 继续执行正在调试的程序。该命令用在程序由于处理信号或断点而 导致停止运行时。<BR>display EXPR
每次程序停止后显示表达式的值。表达式由程序定义的变量组成。<BR>file FILE 装载指定的可执行文件进行调试。<BR>help NAME
显示指定命令的帮助信息。<BR>info break 显示当前断点清单,包括到达断点处的次数等。<BR>info files
显示被调试文件的详细信息。<BR>info func 显示所有的函数名称。<BR>info local 显示当函数中的局部变量信息。<BR>info prog
显示被调试程序的执行状态。<BR>info var 显示所有的全局和静态变量名称。<BR>kill 终止正被调试的程序。<BR>list
显示源代码段。<BR>make 在不退出 gdb 的情况下运行 make 工具。<BR>next
在不单步执行进入其他函数的情况下,向前执行一行源代码。<BR>print EXPR 显示表达式 EXPR 的值。</P>
<P>******gdb 使用范例************************<BR>—————–<BR>清单 一个有错误的 C 源程序
bugging.c<BR>代码:</P>
<P>—————–<BR>1 #i nclude<BR>2<BR>3 static char buff [256];<BR>4 static char*
string;<BR>5 int main ()<BR>6 {<BR>7 printf (”Please input a string:
“);<BR>8 gets (string);<BR>9 printf (”\nYour string is: %s\n”,
string);<BR>10 }</P>
<P>—————–<BR>上面这个程序非常简单,其目的是接受用户的输入,然后将用户的输入打印出来。该程序使用了一个未经过初始化的字符串地址
string,因此,编译并运行之后,将出现 Segment Fault 错误:<BR>$ gcc -o bugging -g bugging.c<BR>$
./bugging<BR>Please input a string: asfd<BR>Segmentation fault (core
dumped)<BR>为了查找该程序中出现的问题,我们利用 gdb,并按如下的步骤进行:<BR>1.运行 gdb bugging 命令,装入 bugging
可执行文件;<BR>2.执行装入的 bugging 命令 run;<BR>3.使用 where 命令查看程序出错的地方;<BR>4.利用 list 命令查看调用
gets 函数附近的代码;<BR>5.唯一能够导致 gets 函数出错的因素就是变量 string。用print命令查看 string 的值;<BR>6.在
gdb 中,我们可以直接修改变量的值,只要将 string 取一个合法的指针值就可以了,为此,我们在第8行处设置断点 break
8;<BR>7.程序重新运行到第 8行处停止,这时,我们可以用 set variable 命令修改 string
的取值;<BR>8.然后继续运行,将看到正确的程序运行结果。<BR>(http://www.fanqiang.com)</P></DIV>
<P class=postendmeta>
<UL>
<LI>如果您从本文中得到帮助或对本站感兴趣,请<A href="http://feed.freshbug.com/">订阅本站全文
Feed</A>(参考<A href="http://www.freshbug.com/rsshelp">订阅帮助</A>)。
<LI>转载原创文章请注明,转载自 : <STRONG>freshbug's Blog</STRONG>[<A
href="http://www.freshbug.com/">http://www.freshbug.com/</A>]
<LI>本文永久链接 : <A class=permalink title=GDB调试精粹及使用实例
href="http://www.freshbug.com/archives/65"
rel=bookmark>http://www.freshbug.com/archives/65</A> </LI></UL>
<P></P></DIV><!-- You can start editing here. -->
<DIV class="commentbar clearfix">
<DIV class=fL>本文相关评论 - 1条评论都没有呢 </DIV>
<DIV class=fR><A href="http://www.freshbug.com/archives/65/feed">本文评论的<ABBR
title="Really Simple Syndication">RSS</ABBR></A> | <A
href="http://www.freshbug.com/archives/65/trackback">Trackback <ACRONYM
title='\"Uniform' Identifier\? Resource>URI</ACRONYM></A> </DIV></DIV>
<DIV class=commhead><SPAN class=comm-func><A title=发表您的评论
onclick="return _$ff()"
href="http://www.freshbug.com/archives/65#addcomment">立刻发表评论 »</A></SPAN> </DIV><!--googleAD-->
<DIV class=commentlist>
<SCRIPT type=text/javascript><!--google_ad_client = "pub-3782903016783837";/* 468x60, 创建于 08-2-21 */google_ad_slot = "9641013038";google_ad_width = 468;google_ad_height = 60;//--></SCRIPT>
<SCRIPT src="GDB调试精粹及使用实例.files/show_ads.js" type=text/javascript></SCRIPT>
</DIV>
<H2></H2>
<DIV class=commentlist>
<P>目前还没有评论.</P></DIV>
<SCRIPT> $(".title .collapseicon").bind("click",function(e){ collapseThread("div-com"+$(this).attr("id")); e.stopPropagation(); }); $(".commentlist .title").bind("click",function(e){$(this).children(".collapseicon").click();e.stopPropagation();}).mouseover(function(){$(this).css("background","#eee")}).mouseout(function(){$(this).css("background","transparent")}); $(".commentlist a").attr("target","_blank");</SCRIPT>
<DIV class="comment clearfix" id=addcomment><A id=addcommentanchor
name=addcommentanchor></A>
<FORM id=commentform action=http://www.freshbug.com/wp-comments-post.php
method=post>
<DIV class=add>
<DIV id=reroot style="DISPLAY: none"><A onclick="reRoot(); return false;"
href="http://www.freshbug.com/archives/65#">评论原文</A> </DIV>
<DIV class="clearfix formr"><LABEL>昵称(Name)* </LABEL>
<DIV><INPUT class=textarea id=author tabIndex=1 size=28 name=author>
</DIV></DIV>
<DIV class="clearfix formr"><LABEL>邮箱(E-Mail)* </LABEL>
<DIV><INPUT id=email tabIndex=2 size=28 name=email> </DIV></DIV>
<DIV class="clearfix formr"><LABEL>网站(Website) </LABEL>
<DIV><INPUT id=url tabIndex=3 size=28 name=url> </DIV></DIV><TEXTAREA id=comment tabIndex=4 name=comment rows=10 cols=60></TEXTAREA>
<DIV><INPUT type=hidden value=65 name=comment_post_ID> <INPUT type=hidden
value=/archives/65 name=redirect_to> <INPUT class=p id=addcommentbutton onclick="if(typeof(onAddComment) == 'function') { onAddComment(); } else { alert('ERROR:\nIt looks like the website administrator hasn\'t activated the Brians Threaded Comments plugin from the plugin page'); };" tabIndex=5 type=button value="立即发表 (Submit!)" name=addcommentbutton>
</DIV></DIV></FORM></DIV>
<DIV class="nav clearfix">
<DIV class=prev-p>« <A
href="http://www.freshbug.com/archives/64">Google声称对今日大盘下跌负责</A></DIV>
<DIV class=next-p><A href="http://www.freshbug.com/archives/66">ACE
5.6在VS2005下的安装步骤</A> »</DIV></DIV></DIV><!-- /content-freshbug--></DIV><!-- /content -->
<DIV id=sidebar>
<UL><BR>
<LI class=widget>
<H2>搜索</H2><!-- SiteSearch Google -->
<FORM action=http://www.freshbug.com/search method=get target=_top>
<TABLE border=0>
<TBODY>
<TR>
<TD vAlign=top noWrap align=left height=32><!--</td>--><!--<td nowrap="nowrap">--><INPUT type=hidden
value=www.freshbug.com name=domains></INPUT> <LABEL
style="DISPLAY: none" for=sbi>输入您的搜索字词</LABEL> <INPUT class=g-g id=sbi
maxLength=255 size=18 name=q></INPUT> <LABEL style="DISPLAY: none"
for=sbb>提交搜索表单</LABEL> <INPUT class=btns id=sbb type=submit value="Search Now!" name=sa></INPUT>
</TD></TR>
<TR><!--<td> </td>-->
<TD noWrap>
<TABLE>
<TBODY>
<TR>
<TD><INPUT class=n-b id=ss0 type=radio value=""
name=sitesearch></INPUT> <LABEL title=搜索网络 for=ss0><FONT
color=#000000 size=-1>Web</FONT></LABEL></TD>
<TD><INPUT class=n-b id=ss1 type=radio CHECKED
value=www.freshbug.com name=sitesearch></INPUT> <LABEL
title="搜索 www.freshbug.com" for=ss1><FONT color=#000000
size=-1>freshbug.com</FONT></LABEL></TD></TR></TBODY></TABLE><INPUT
type=hidden value=pub-3782903016783837 name=client></INPUT> <INPUT
type=hidden value=1 name=forid></INPUT> <INPUT type=hidden
value=7031562895 name=channel></INPUT> <INPUT type=hidden value=UTF-8
name=ie></INPUT> <INPUT type=hidden value=UTF-8 name=oe></INPUT> <INPUT
type=hidden
value=GALT:#006600;GL:1;DIV:#d4d0c8;VLC:CC3300;AH:center;BGC:f9fbea;LBGC:f9fbea;ALC:006600;LC:006600;T:000000;GFNT:e5c000;GIMP:e5c000;LH:16;LW:16;L:http://www.freshbug.com/logo.gif;S:http://www.freshbug.com;FORID:11
name=cof></INPUT> <INPUT type=hidden value=zh-CN name=hl></INPUT>
</TD></TR></TBODY></TABLE></FORM><!-- SiteSearch Google --><!--<li class="pagenav"><h2>页面</h2><ul><li class="page_item page-item-2"><a href="http://www.freshbug.com/about" title="freshbug是谁啊?">freshbug是谁啊?</a></li><li class="page_item page-item-43"><a href="http://www.freshbug.com/rsshelp" title="像投递报纸一样投到您的怀里">像投递报纸一样投到您的怀里</a></li><li class="page_item page-item-35"><a href="http://www.freshbug.com/miansilu" title="勉思录">勉思录</a></li><li class="page_item page-item-37"><a href="http://www.freshbug.com/wordofshadow" title="宁财神青春的影子">宁财神青春的影子</a></li></ul></li>-->
<LI class=pagenav>
<H2>年轮</H2>
<UL>
<LI class="page_item page-item-2"><A title=freshbug是谁啊?
href="http://www.freshbug.com/about">freshbug是谁啊?</A>
<LI class="page_item page-item-43"><A title=像投递报纸一样投到您的怀里
href="http://www.freshbug.com/rsshelp">像投递报纸一样投到您的怀里</A>
<LI class="page_item page-item-35"><A title=勉思录
href="http://www.freshbug.com/miansilu">勉思录</A>
<LI class="page_item page-item-37"><A title=宁财神青春的影子
href="http://www.freshbug.com/wordofshadow">宁财神青春的影子</A> </LI></UL>
<LI class=widget>
<H2>最新撰写</H2>
<UL>
<LI><A title="12月 11, 2008" href="http://www.freshbug.com/archives/76"
rel=bookmark>我要的日子</A>
<LI><A title="10月 15, 2008" href="http://www.freshbug.com/archives/73"
rel=bookmark>经济大萧条时代来了,你准备好了吗?</A>
<LI><A title="9月 26, 2008" href="http://www.freshbug.com/archives/72"
rel=bookmark>《格斗之王合辑 大蛇传说》10月21日北美预定发售</A>
<LI><A title="9月 8, 2008" href="http://www.freshbug.com/archives/71"
rel=bookmark>很多时候 只是态度问题 马云终于还是对百度说了不</A>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -