⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gdb-2-modules.php.html

📁 使用gdb调试linux下程序的详细说明
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<tr><td valign="top"><input value="4" name="voteID" type="radio"></td><td width="100%"><font class="content">更好看的模板。</font></td></tr><tr><td valign="top"><input value="5" name="voteID" type="radio"></td><td width="100%"><font class="content">其他。</font></td></tr></tbody></table><br></font><center><font class="content"><font class="content"><input value="投票" type="submit"></font><br><br><font class="content"><a href="http://www.linuxts.com/modules.php?name=Surveys&amp;op=results&amp;pollID=3&amp;mode=&amp;order=&amp;thold="><b>结果</b></a><br><a href="http://www.linuxts.com/modules.php?name=Surveys"><b>投票一览</b></a><br><br>投票: <b>143</b> <br> 评论: <b>0</b></font></font></center></form></td></tr></tbody></table></td></tr></tbody></table><br><table bgcolor="#000000" width="150" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td><table cellpadding="3" cellspacing="1" border="0" width="100%"><tbody><tr><td bgcolor="#ffffff"><font class="content">最新下载</font></td></tr><tr><td bgcolor="#ffffff"><font class="content"><strong><big>·</big></strong>&nbsp;1: <a href="http://www.linuxts.com/modules.php?name=Downloads&amp;d_op=viewdownloaddetails&amp;lid=82&amp;title=fedora1_cd3">fedora1 cd3</a><br><strong><big>·</big></strong>&nbsp;2: <a href="http://www.linuxts.com/modules.php?name=Downloads&amp;d_op=viewdownloaddetails&amp;lid=81&amp;title=fedora1_cd2">fedora1 cd2</a><br><strong><big>·</big></strong>&nbsp;3: <a href="http://www.linuxts.com/modules.php?name=Downloads&amp;d_op=viewdownloaddetails&amp;lid=80&amp;title=fedora1_cd1">fedora1 cd1</a><br><strong><big>·</big></strong>&nbsp;4: <a href="http://www.linuxts.com/modules.php?name=Downloads&amp;d_op=viewdownloaddetails&amp;lid=79&amp;title=AntiVir%20for%20Linux%20Server">AntiVir for Linux Server</a><br><strong><big>·</big></strong>&nbsp;5: <a href="http://www.linuxts.com/modules.php?name=Downloads&amp;d_op=viewdownloaddetails&amp;lid=76&amp;title=tsmail-qmail-2.0-rh8">tsmail-qmail-2.0-rh8</a><br></font></td></tr></tbody></table></td></tr></tbody></table><br></td><td><img alt="" border="0" height="1" width="15" src="GDB-2-modules.php_files/pixel.gif"></td><td width="100%"><table bgcolor="#cccccc" cellpadding="0" cellspacing="1" border="0" width="100%"><tbody><tr><td><table bgcolor="#ffffff" cellpadding="8" cellspacing="1" border="0" width="100%"><tbody><tr><td><font class="option"><b>用GDB调试程序(2)</b></font><br><br><font class="content">(77 阅读) &nbsp;&nbsp;<a href="http://www.linuxts.com/modules.php?name=Sections&amp;op=printpage&amp;artid=222"><img height="11" width="15" alt="以易于打印的格式显示" border="0" src="GDB-2-modules.php_files/print.gif"></a></font><br><br><br><br><p><font face="Courier New"><strong>GDB的命令概貌<br>———————</strong></font></p><p><font face="Courier New">启动gdb后,就你被带入gdb的调试环境中,就可以使用gdb的命令开始调试程序了,gdb的命令可以使用help命令来查看,如下所示:</font></p><p><font face="Courier New">&nbsp;&nbsp;&nbsp; /home/hchen&gt; gdb<br>&nbsp;&nbsp;&nbsp; GNU gdb 5.1.1<br>&nbsp;&nbsp;&nbsp; Copyright 2002 Free Software Foundation, Inc.<br>&nbsp;&nbsp;&nbsp; GDB is free software, covered by the GNU General Public License, and you are<br>&nbsp;&nbsp;&nbsp; welcome to change it and/or distribute copies of it under certain conditions.<br>&nbsp;&nbsp;&nbsp; Type "show copying" to see the conditions.<br>&nbsp;&nbsp;&nbsp; There is absolutely no warranty for GDB.&nbsp; Type "show warranty" for details.<br>&nbsp;&nbsp;&nbsp; This GDB was configured as "i386-suse-linux".<br>&nbsp;&nbsp;&nbsp; (gdb) help<br>&nbsp;&nbsp;&nbsp; List of classes of commands:</font></p><p><font face="Courier New">&nbsp;&nbsp;&nbsp; aliases -- Aliases of other commands<br>&nbsp;&nbsp;&nbsp; breakpoints -- Making program stop at certain points<br>&nbsp;&nbsp;&nbsp; data -- Examining data<br>&nbsp;&nbsp;&nbsp; files -- Specifying and examining files<br>&nbsp;&nbsp;&nbsp; internals -- Maintenance commands<br>&nbsp;&nbsp;&nbsp; obscure -- Obscure features<br>&nbsp;&nbsp;&nbsp; running -- Running the program<br>&nbsp;&nbsp;&nbsp; stack -- Examining the stack<br>&nbsp;&nbsp;&nbsp; status -- Status inquiries<br>&nbsp;&nbsp;&nbsp; support -- Support facilities<br>&nbsp;&nbsp;&nbsp; tracepoints -- Tracing of program execution without stopping the program<br>&nbsp;&nbsp;&nbsp; user-defined -- User-defined commands</font></p><p><font face="Courier New">&nbsp;&nbsp;&nbsp; Type "help" followed by a class name for a list of commands in that class.<br>&nbsp;&nbsp;&nbsp; Type "help" followed by command name for full documentation.<br>&nbsp;&nbsp;&nbsp; Command name abbreviations are allowed if unambiguous.<br>&nbsp;&nbsp;&nbsp; (gdb)</font></p><p><font face="Courier New">gdb的命令很多,gdb把之分成许多个种类。help命令只是例出gdb的命令种类,如果要看种类中的命令,可以使用help <class>命令,如:help breakpoints,查看设置断点的所有命令。也可以直接help <command>来查看命令的帮助。</command></class></font></p><p><br><font face="Courier New">gdb中,输入命令时,可以不用打全命令,只用打命令的前几个字符就可以了,当然,命令的前几个字符应该要标志着一个唯一的命令,在Linux下,你可以敲击两次TAB键来补齐命令的全称,如果有重复的,那么gdb会把其例出来。<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 示例一:在进入函数func时,设置一个断点。可以敲入break func,或是直接就是b func<br>&nbsp;&nbsp;&nbsp; (gdb) b func<br>&nbsp;&nbsp;&nbsp; Breakpoint 1 at 0x8048458: file hello.c, line 10.<br>&nbsp;<br>&nbsp;&nbsp;&nbsp; 示例二:敲入b按两次TAB键,你会看到所有b打头的命令:<br>&nbsp;&nbsp;&nbsp; (gdb) b<br>&nbsp;&nbsp;&nbsp; backtrace&nbsp; break&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bt<br>&nbsp;&nbsp;&nbsp; (gdb)</font></p><p><font face="Courier New">&nbsp;&nbsp;&nbsp; 示例三:只记得函数的前缀,可以这样:<br>&nbsp;&nbsp;&nbsp; (gdb) b make_ &lt;按TAB键&gt;<br>&nbsp;&nbsp;&nbsp; (再按下一次TAB键,你会看到:)<br>&nbsp;&nbsp;&nbsp; make_a_section_from_file&nbsp;&nbsp;&nbsp;&nbsp; make_environ<br>&nbsp;&nbsp;&nbsp; make_abs_section&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; make_function_type<br>&nbsp;&nbsp;&nbsp; make_blockvector&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; make_pointer_type<br>&nbsp;&nbsp;&nbsp;make_cleanup&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;make_reference_type<br>&nbsp;&nbsp;&nbsp;make_command&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;make_symbol_completion_list<br>&nbsp;&nbsp;&nbsp; (gdb) b make_<br>&nbsp;&nbsp;&nbsp; GDB把所有make开头的函数全部例出来给你查看。</font></p><p><font face="Courier New">&nbsp;&nbsp;&nbsp; 示例四:调试C++的程序时,有可以函数名一样。如:<br>&nbsp;&nbsp;&nbsp; (gdb) b 'bubble( M-? <br>&nbsp;&nbsp;&nbsp; bubble(double,double)&nbsp;&nbsp;&nbsp; bubble(int,int)<br>&nbsp;&nbsp;&nbsp; (gdb) b 'bubble(<br>&nbsp;&nbsp;&nbsp; 你可以查看到C++中的所有的重载函数及参数。(注:M-?和“按两次TAB键”是一个意思)</font></p><p><font face="Courier New">要退出gdb时,只用发quit或命令简称q就行了。</font></p><p><font face="Courier New"></font>&nbsp;</p><p><font face="Courier New"><strong>GDB中运行UNIX的shell程序<br>————————————</strong></font></p><p><font face="Courier New">在gdb环境中,你可以执行UNIX的shell的命令,使用gdb的shell命令来完成:</font></p><p><font face="Courier New">&nbsp;&nbsp;&nbsp; shell <command string=""><br>&nbsp;&nbsp;&nbsp; 调用UNIX的shell来执行<command string="">,环境变量SHELL中定义的UNIX的shell将会被用来执行<command string="">,如果SHELL没有定义,那就使用UNIX的标准shell:/bin/sh。(在Windows中使用Command.com或cmd.exe)</command></command></command></font></p><p><font face="Courier New">还有一个gdb命令是make:<br>&nbsp;&nbsp;&nbsp; make <make-args><br>&nbsp;&nbsp;&nbsp; 可以在gdb中执行make命令来重新build自己的程序。这个命令等价于“shell make <make-args>”。 </make-args></make-args></font></p><p><font face="Courier New"></font>&nbsp;</p><p><br><font face="Courier New"><strong>在GDB中运行程序<br>————————</strong></font></p><p><font face="Courier New">当以gdb <program>方式启动gdb后,gdb会在PATH路径和当前目录中搜索<program>的源文件。如要确认gdb是否读到源文件,可使用l或list命令,看看gdb是否能列出源代码。</program></program></font></p><p><font face="Courier New">在gdb中,运行程序使用r或是run命令。程序的运行,你有可能需要设置下面四方面的事。</font></p><p><font face="Courier New">1、程序运行参数。<br>&nbsp;&nbsp;&nbsp; set args 可指定运行时参数。(如:set args 10 20 30 40 50)<br>&nbsp;&nbsp;&nbsp; show args 命令可以查看设置好的运行参数。</font></p><p><font face="Courier New">2、运行环境。<br>&nbsp;&nbsp;&nbsp; path </font></p><dir><font face="Courier New">可设定程序的运行路径。<br>&nbsp;&nbsp;&nbsp; show paths 查看程序的运行路径。<br>&nbsp;&nbsp;&nbsp; set environment varname [=value] 设置环境变量。如:set env USER=hchen<br>&nbsp;&nbsp;&nbsp; show environment [varname] 查看环境变量。</font><p></p><p><font face="Courier New">3、工作目录。<br>&nbsp;&nbsp;&nbsp; cd </font></p><dir><font face="Courier New">相当于shell的cd命令。<br>&nbsp;&nbsp;&nbsp; pwd 显示当前的所在目录。</font><p></p><p><font face="Courier New">4、程序的输入输出。<br>&nbsp;&nbsp;&nbsp; info terminal 显示你程序用到的终端的模式。<br>&nbsp;&nbsp;&nbsp; 使用重定向控制程序输出。如:run &gt; outfile<br>&nbsp;&nbsp;&nbsp; tty命令可以指写输入输出的终端设备。如:tty /dev/ttyb</font></p><p><br><font face="Courier New"><strong>调试已运行的程序<br>————————</strong></font></p><p><font face="Courier New">两种方法:<br>1、在UNIX下用ps查看正在运行的程序的PID(进程ID),然后用gdb <program>PID格式挂接正在运行的程序。<br>2、先用gdb <program>关联上源代码,并进行gdb,在gdb中用attach命令来挂接进程的PID。并用detach来取消挂接的进程。</program></program></font></p><p><font face="Courier New"></font>&nbsp;</p><p><font face="Courier New"><strong>暂停 / 恢复程序运行<br>—————————</strong></font></p><p><font face="Courier New">调试程序中,暂停程序运行是必须的,GDB可以方便地暂停程序的运行。你可以设置程序的在哪行停住,在什么条件下停住,在收到什么信号时停往等等。以便于你查看运行时的变量,以及运行时的流程。</font></p><p><font face="Courier New">当进程被gdb停住时,你可以使用info program 来查看程序的是否在运行,进程号,被暂停的原因。</font></p><p><font face="Courier New">在gdb中,我们可以有以下几种暂停方式:断点(BreakPoint)、观察点(WatchPoint)、捕捉点(CatchPoint)、信号(Signals)、线程停止(Thread Stops)。如果要恢复程序运行,可以使用c或是continue命令。</font></p><p><br><font face="Courier New"><strong>一、设置断点(BreakPoint)</strong><br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 我们用break命令来设置断点。正面有几点设置断点的方法:<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; break <function><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在进入指定函数时停住。C++中可以使用class::function或function(type,type)格式来指定函数名。</function></font></p><p><font face="Courier New">&nbsp;&nbsp;&nbsp; break <linenum><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在指定行号停住。</linenum></font></p><p><font face="Courier New">&nbsp;&nbsp;&nbsp; break +offset <br>&nbsp;&nbsp;&nbsp; break -offset <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在当前行号的前面或后面的offset行停住。offiset为自然数。</font></p><p><font face="Courier New">&nbsp;&nbsp;&nbsp; break filename:linenum <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在源文件filename的linenum行处停住。</font></p><p><font face="Courier New">&nbsp;&nbsp;&nbsp; break filename:function <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在源文件filename的function函数的入口处停住。</font></p><p><font face="Courier New">&nbsp;&nbsp;&nbsp; break *address<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在程序运行的内存地址处停住。</font></p><p><font face="Courier New">&nbsp;&nbsp;&nbsp; break <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break命令没有参数时,表示在下一条指令处停住。</font></p><p><font face="Courier New">&nbsp;&nbsp;&nbsp; break ... if <condition><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...可以是上述的参数,condition表示条件,在条件成立时停住。比如在循环境体中,可以设置break if i=100,表示当i为100时停住程序。</condition></font></p><p><font face="Courier New">&nbsp;&nbsp;&nbsp; 查看断点时,可使用info命令,如下所示:(注:n表示断点号)<br>&nbsp;&nbsp;&nbsp; info breakpoints [n] <br>&nbsp;&nbsp;&nbsp; info break [n] <br>&nbsp;&nbsp;&nbsp; </font></p><p><font face="Courier New"><strong>二、设置观察点(WatchPoint)</strong><br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 观察点一般来观察某个表达式(变量也是一种表达式)的值是否有变化了,如果有变化,马上停住程序。我们有下面的几种方法来设置观察点:<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; watch <expr><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 为表达式(变量)expr设置一个观察点。一量表达式值有变化时,马上停住程序。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; rwatch <expr><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当表达式(变量)expr被读时,停住程序。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; awatch <expr><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当表达式(变量)的值被读或被写时,停住程序。<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; info watchpoints<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 列出当前所设置了的所有观察点。</expr></expr></expr></font></p><p><br><font face="Courier New"><strong>三、设置捕捉点(CatchPoint)</strong></font></p><p><font face="Courier New">&nbsp;&nbsp;&nbsp; 你可设置捕捉点来补捉程序运行时的一些事件。如:载入共享库(动态链接库)或是C++的异常。设置捕捉点的格式为:<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; catch <event><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当event发生时,停住程序。event可以是下面的内容:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1、throw 一个C++抛出的异常。(throw为关键字)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2、catch 一个C++捕捉到的异常。(catch为关键字)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3、exec 调用系统调用exec时。(exec为关键字,目前此功能只在HP-UX下有用)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4、fork 调用系统调用fork时。(fork为关键字,目前此功能只在HP-UX下有用)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5、vfork 调用系统调用vfork时。(vfork为关键字,目前此功能只在HP-UX下有用)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6、load 或 load <libname>载入共享库(动态链接库)时。(load为关键字,目前此功能只在HP-UX下有用)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7、unload 或 unload <libname>卸载共享库(动态链接库)时。(unload为关键字,目前此功能只在HP-UX下有用)</libname></libname></event></font></p><p><font face="Courier New">&nbsp;&nbsp;&nbsp; tcatch <event><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 只设置一次捕捉点,当程序停住以后,应点被自动删除。</event></font></p></dir></dir></td></tr><tr><td align="center"> &nbsp;&nbsp; <br><br>[ <a href="http://www.linuxts.com/modules.php?name=Sections&amp;op=listarticles&amp;secid=2">返回到本类 </a> | <a href="http://www.linuxts.com/modules.php?name=Sections">知识中心目录</a> ]</td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table><table align="center" width="100%"><tbody><tr align="center"><td colspan="3" width="100%"><font class="footmsg">Copyright &#169; 2002-2003 by <a href="http://www.linuxts.com/"><font class="footmsg_l">linuxts技术研究站点</font></a>. All Rights Reserved. <a href="mailto:linuxts@linuxaid.com.cn">如有意见请与站长联系</a><br>Page Generation: 0.999 秒<br></font> </td></tr></tbody></table></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -