📄 用gdb调试程序.htm
字号:
</tr>
<tr>
<td class="left_tdbg2"></td>
</tr>
<tr>
<td class="left_title"><a href="/Article/ShowElite.asp"><img src="/skin/artkiss/zxtja.gif" width="173" height="34" border="0"></a></td>
</tr>
<tr>
<td class="left_tdbg1"><script language='javascript' src='/Article/JS/Article_Elite2.js'></script></td>
</tr>
<tr>
<td class="left_tdbg2"></td>
</tr>
<tr>
<td class="left_title"><img src="/skin/artkiss/xgwz.gif" width="173" height="34"></td>
</tr>
<tr>
<td class="left_tdbg1"><li><a href='/Article/Class1/Class3/200403/79.html'title='文章标题:Debug 使用简介
作 者:不详
更新时间:2004-3-25 9:05:05'>Debug 使用简介</a></li><br></td>
</tr>
<tr>
<td class="left_tdbg2"></td>
</tr>
</table>
<!--网页中部左栏代码结束-->
</td>
<td valign="top">
<!--网页中部右栏文章内容代码开始-->
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="main_title_575" style="word-break:break-all;Width:fixed">
<tr>
<td width="3%" valign="middle"><img src="/skin/artkiss/homedha3.gif" width="55" height="34"></td>
<td width="66%">用GDB调试程序</td>
<td align="right"> <font color='#009900'></font></td>
</tr>
</table> <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="main_tdbg_575" style="word-break:break-all;Width:fixed">
<tr align="center" valign="middle">
<td height="30" colspan="2" class="main_ArticleTitle" style="word-break:break-all;Width:fixed">用GDB调试程序</td>
</tr>
<tr align="center" valign="middle">
<td height="20" colspan="2" class="main_ArticleSubheading" style="word-break:break-all;Width:fixed"></td>
</tr>
<tr align="center">
<td height="26" colspan="2" class="left_tdbgall" style="border-top:1px #D8D8D8 solid;border-bottom:1px #D8D8D8 solid;">作者:陈皓 文章来源:纯C电子杂志 2005年1月号(总第3期) 点击数:<script language='javascript' src='/Article/GetHits.asp?ArticleID=418'></script> 更新时间:2005-3-2</td>
</tr>
<tr>
<td height="300" colspan="2" valign="top" id="fontzoom" style="word-break:break-all;Width:fixed"> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p><FONT face="Times New Roman" size=3> </FONT></o:p></SPAN></P>
<P class=a style="MARGIN: 0cm 0cm 0pt"><FONT size=6><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Times New Roman'">用</SPAN><SPAN lang=EN-US><FONT face="Times New Roman">GDB</FONT></SPAN><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: 'Times New Roman'">调试程序</SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.25pt; TEXT-ALIGN: right; punctuation-trim: leading" align=right><FONT size=3><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">作者</SPAN></B><FONT face="Times New Roman"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.0pt">:</SPAN></B><SPAN lang=EN-US> </SPAN></FONT><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">陈皓</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.0pt"><FONT face="Times New Roman">(</FONT><A href="mailto:haoel@hotmail.com"><FONT face="Times New Roman">haoel@hotmail.com</FONT></A><FONT face="Times New Roman">)<o:p></o:p></FONT></SPAN></B></FONT></P>
<H1 style="MARGIN: 17pt 0cm 16.5pt 17pt; tab-stops: list 9.0pt"><FONT face="Times New Roman"><SPAN lang=EN-US style="mso-fareast-font-family: 'Times New Roman'"><SPAN style="mso-list: Ignore"><FONT size=5>1、</FONT><SPAN style="FONT: 7pt 'Times New Roman'"> </SPAN></SPAN></SPAN><SPAN lang=EN-US><FONT size=5>GDB</FONT></SPAN></FONT><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><FONT size=5>概述</FONT></SPAN></H1>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT size=3><SPAN lang=EN-US><FONT face="Times New Roman">GDB</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是</SPAN><SPAN lang=EN-US><FONT face="Times New Roman">GNU</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">开源组织发布的一个强大的</SPAN><SPAN lang=EN-US><FONT face="Times New Roman">UNIX</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">下的程序调试工具。或许,各位比较喜欢那种图形界面方式的,像</SPAN><SPAN lang=EN-US><FONT face="Times New Roman">VC</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</SPAN><SPAN lang=EN-US><FONT face="Times New Roman">BCB</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">等</SPAN><SPAN lang=EN-US><FONT face="Times New Roman">IDE</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的调试,但如果你是在</SPAN><SPAN lang=EN-US><FONT face="Times New Roman">UNIX</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">平台下做软件,你会发现</SPAN><SPAN lang=EN-US><FONT face="Times New Roman">GDB</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这个调试工具有比</SPAN><SPAN lang=EN-US><FONT face="Times New Roman">VC</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、</SPAN><SPAN lang=EN-US><FONT face="Times New Roman">BCB</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的图形化调试器更强大的功能。所谓“寸有所长,尺有所短”就是这个道理。</SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=3><SPAN lang=EN-US><SPAN style="mso-tab-count: 1"><FONT face="Times New Roman"> </FONT></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">一般来说,</SPAN><SPAN lang=EN-US><FONT face="Times New Roman">GDB</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">主要帮忙你完成下面四个方面的功能:</SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=3><SPAN lang=EN-US><FONT face="Times New Roman"><SPAN style="mso-spacerun: yes"> </SPAN>1</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。</SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=3><SPAN lang=EN-US><FONT face="Times New Roman"><SPAN style="mso-spacerun: yes"> </SPAN>2</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式)</SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=3><SPAN lang=EN-US><FONT face="Times New Roman"><SPAN style="mso-spacerun: yes"> </SPAN>3</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、当程序被停住时,可以检查此时你的程序中所发生的事。</SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=3><SPAN lang=EN-US><FONT face="Times New Roman"><SPAN style="mso-spacerun: yes"> </SPAN>4</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">、动态的改变你程序的执行环境。</SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT size=3><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">从上面看来,</SPAN><SPAN lang=EN-US><FONT face="Times New Roman">GDB</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和一般的调试工具没有什么两样,基本上也是完成这些功能,不过在细节上,你会发现</SPAN><SPAN lang=EN-US><FONT face="Times New Roman">GDB</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这个调试工具的强大,大家可能比较习惯了图形化的调试工具,但有时候,命令行的调试工具却有着图形化工具所不能完成的功能。让我们一一看来。</SPAN></FONT></P>
<H1 style="MARGIN: 17pt 0cm 16.5pt 17pt"><SPAN lang=EN-US style="mso-fareast-font-family: 'Times New Roman'"><SPAN style="mso-list: Ignore"><FONT face="Times New Roman"><FONT size=5>2、</FONT><SPAN style="FONT: 7pt 'Times New Roman'"> </SPAN></FONT></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><FONT size=5>一个调试示例</FONT></SPAN></H1>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=3><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">源程序:</SPAN><SPAN lang=EN-US><FONT face="Times New Roman">tst.c</FONT></SPAN></FONT></P>
<P class=020 style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><FONT style="BACKGROUND-COLOR: #cccccc"><FONT face="Courier New"><SPAN style="mso-spacerun: yes"> </SPAN>1 #include <stdio.h></FONT></FONT></SPAN></P>
<P class=020 style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><FONT style="BACKGROUND-COLOR: #cccccc"><FONT face="Courier New"><SPAN style="mso-spacerun: yes"> </SPAN>2</FONT></FONT></SPAN></P>
<P class=020 style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><FONT style="BACKGROUND-COLOR: #cccccc"><FONT face="Courier New"><SPAN style="mso-spacerun: yes"> </SPAN>3 int func(int n)</FONT></FONT></SPAN></P>
<P class=020 style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><FONT style="BACKGROUND-COLOR: #cccccc"><FONT face="Courier New"><SPAN style="mso-spacerun: yes"> </SPAN>4 {</FONT></FONT></SPAN></P>
<P class=020 style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><FONT style="BACKGROUND-COLOR: #cccccc"><FONT face="Courier New"><SPAN style="mso-spacerun: yes"> </SPAN>5<SPAN style="mso-spacerun: yes"> </SPAN>int sum=0,i;</FONT></FONT></SPAN></P>
<P class=020 style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><FONT style="BACKGROUND-COLOR: #cccccc"><FONT face="Courier New"><SPAN style="mso-spacerun: yes"> </SPAN>6<SPAN style="mso-spacerun: yes"> </SPAN>for(i=0; i<n; i++)</FONT></FONT></SPAN></P>
<P class=020 style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><FONT style="BACKGROUND-COLOR: #cccccc"><FONT face="Courier New"><SPAN style="mso-spacerun: yes"> </SPAN>7<SPAN style="mso-spacerun: yes"> </SPAN>{</FONT></FONT></SPAN></P>
<P class=020 style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><FONT style="BACKGROUND-COLOR: #cccccc"><FONT face="Courier New"><SPAN style="mso-spacerun: yes"> </SPAN>8<SPAN style="mso-spacerun: yes"> </SPAN>sum+=i;</FONT></FONT></SPAN></P>
<P class=020 style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><FONT style="BACKGROUND-COLOR: #cccccc"><FONT face="Courier New"><SPAN style="mso-spacerun: yes"> </SPAN>9<SPAN style="mso-spacerun: yes"> </SPAN>}</FONT></FONT></SPAN></P>
<P class=020 style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><FONT style="BACKGROUND-COLOR: #cccccc"><FONT face="Courier New"><SPAN style="mso-spacerun: yes"> </SPAN>10<SPAN style="mso-spacerun: yes"> </SPAN>return sum;</FONT></FONT></SPAN></P>
<P class=020 style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><FONT style="BACKGROUND-COLOR: #cccccc"><FONT face="Courier New"><SPAN style="mso-spacerun: yes"> </SPAN>11 }</FONT></FONT></SPAN></P>
<P class=020 style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><FONT style="BACKGROUND-COLOR: #cccccc"><FONT face="Courier New"><SPAN style="mso-spacerun: yes"> </SPAN>12</FONT></FONT></SPAN></P>
<P class=020 style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><FONT style="BACKGROUND-COLOR: #cccccc"><FONT face="Courier New"><SPAN style="mso-spacerun: yes"> </SPAN>13</FONT></FONT></SPAN></P>
<P class=020 style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><FONT style="BACKGROUND-COLOR: #cccccc"><FONT face="Courier New"><SPAN style="mso-spacerun: yes"> </SPAN>14 main()</FONT></FONT></SPAN></P>
<P class=020 style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><FONT style="BACKGROUND-COLOR: #cccccc"><FONT face="Courier New"><SPAN style="mso-spacerun: yes"> </SPAN>15 {</FONT></FONT></SPAN></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -