您的位置:首页 > 其它

GDB 7.0 中文手册 —— 1. GDB简介和一个简单的使用例子

2007-03-22 18:01 1091 查看
GDB最新的手册,每天翻译一些。

虽水平有限,望造福大家

转贴请注明出处:
http://blog.csdn.net/benson_linux/archive/2007/03/22/1537834.aspx
 

英文原文:
http://sourceware.org/gdb/current/onlinedocs/gdb_toc.html
 

 

GDB 概要

 

通过使用调试器,如GDB,我们能够观察一个程序的执行过程,或者这个程序执行过程中的崩溃时都干了什么。当然我们还可以用调试器来阅读程序。

 

GDB主要通过4中方式帮助你查找Bug:

启动我们要调试的程序,并显示可能影响程序行为的任何因素。

在特定条件下,中断被调试程序的运行。

程序运行中断时,检查当前的运行状态。

改变进程空间中的数据,动态控制被调试程序的执行逻辑。

GDB可以调试C和C++语言编写的程序。更多信息,参考语言支持

C/C++



 

Support for Modula-2 is partial. For information on Modula-2, see Modula-2.

Debugging Pascal programs which use sets, subranges, file variables, or nested functions does not currently work. GDB does not support entering expressions, printing values, or similar features using Pascal syntax.

GDB can be used to debug programs written in Fortran, although it may be necessary to refer to some variables with a trailing underscore.

GDB can be used to debug programs written in Objective-C, using either the Apple/NeXT or the GNU Objective-C runtime.

    Free Software

          Freely redistributable software

    Contributors to GDB

 

 

 

自由软件

 

GDB是受GNU General Public License(GPL)保护的自由软件。人们可以自由拷贝通过GPL版权授权的软件,并且当他获得这个软件的拷贝时,同时也获取了修改这份拷贝的权利(这就意味着他有权利从作者处获取源代码),并且有权利发布改进的拷贝。典型的软件公司(盈利为目的)使用版权限制人们使用软件的自由;而自由软件基金会使用GPL保障了人们使用,修改和发布软件的自由。

 

 

自由软件需要文档

 

当今,自由软件社区最大的短板不是丰富的软件,而是缺乏好的、自由的文档能够随软件一起发行,许多重量级软件并没有随包发行自由的参考手册和介绍文字。如今有太多的重量级自由软件包并没有随包发行自由的手册和教程,对于推广软件的使用来说,这是主要不足。

 

什么样的文档不是自由文档呢?例如 Perl ,大家通常不能自由的使用教程手册。为什么呢?文档的作者发布的时候明确指出,不能复制,不能修改,不提供源文件。

 

这样的事情屡见不鲜,我们经常听到GNU软件的使用者热切的说,为了给社区做贡献,我正在写一份手册,签了出版合同后,发现一切都完了,这份手册已不再自由。

 

自由文档,就像自由软件一样,是一种自由精神,不是利益。不是说为纸质印刷品标价出售,就违背自由精神(事实上自由软件基金会也这个干),关键在于发行者限制了文档的使用。自由文档允许获得源代码,允许拷贝和修改,非自由文档不这样。

 

和自由软件的标准一样,不管通过任何媒介,自由文档允许随软件包多次再发布,包括正式的商业发布。

 

允许修改文档的技术内容也很重要。当修改了软件,添加或者修改了特性时,他们如果细心的话,同样也会修改文档,以新的软件便提供准确和清晰的文档。自由软件社区不会选择非自由的文档。

 

某些限制条款则比较合理,比如要求保护原作者的版权声明,发行说明或作者清单;要求修改版的文档,说明这是修改版;甚至可以要求某些与技术主题无关的章节不能被删除或修改。这类限制之所以被社区接受,因为它们没有影响文档的技术自由分享目的。

文档必须允许修改技术相关的章节,并允许通过任何形式和任何渠道发布。否则,社区认为这不是我们需要的自由文档。

 

请把这件事情告诉更多的人,自由社区一直缺乏自由发行的文档。如果我们不停的呼吁,自由软件需要自由文档和自由手册,也许下一个打算写手册的人就会发现只有自由文档才能帮助自由社区。

 

好吧,假如你正在写一份文档,就像我一样。请一定要坚持以GNU自由文档或其他自由文档授权发布。注意这只取决于你,而不是出版商。出版商肯定不会主动提出使用某种自由授权,这需要你的坚持,斩钉截铁的把要求提出来。实在不行,希望你能坚持换一家出版商:)在你不能判断一份授权是否自由,请联系我licensing@gnu.org



 

为自由文档做贡献的方式多种多样,多购买一些自由文档印刷的出版物,尤其是出版商花钱请人编制或改良的出版物。也就是说尽量不要购买非自由的出版物,以鼓励出版商出版自由出版物,买之前先看看发行说明,坚持自己的理念。有可能的话,看看出版历史,给那些作者金钱上的资助。

 

http://www.fsf.org/doc/other-free-books.html

有一份自由软件基金会出版书目。

 

 

 

向GDB做贡献

 

Richard Stallman 是GDB和其他GNU软件的创始人(大师级人物)。很多人参与了GDB的开发,本节列出主要参与人员和他们的贡献。自由软件的优势之一就是谁都可以参与开发,遗憾的是在这我们无法一一列出,不过在GDB发行版中的ChangLog文件中几乎列出了全部参与人。

 

随着时间的推移,2.0版本以前的ChangLog已经丢失了。

 

呼吁:热烈欢迎大家添加本节内容。如果你或你的朋友、敌人,本应但没有出现在下表中,我们希望能加上这些名字。
 

(下边都是贡献者的人名和事情,暂不翻译,目前我还不想认识这些高手——!)

So that they may not regard their many labors as thankless, we particularly thank those who shepherded gdb through major releases: Andrew Cagney (releases 6.3, 6.2, 6.1, 6.0, 5.3, 5.2, 5.1 and 5.0); Jim Blandy (release 4.18); Jason Molenda (release 4.17); Stan Shebs (release 4.14); Fred Fish (releases 4.16, 4.15, 4.13, 4.12, 4.11, 4.10, and 4.9); Stu Grossman and John Gilmore (releases 4.8, 4.7, 4.6, 4.5, and 4.4); John Gilmore (releases 4.3, 4.2, 4.1, 4.0, and 3.9); Jim Kingdon (releases 3.5, 3.4, and 3.3); and Randy Smith (releases 3.2, 3.1, and 3.0).

Richard Stallman, assisted at various times by Peter TerMaat, Chris Hanson, and Richard Mlynarik, handled releases through 2.8.

Michael Tiemann is the author of most of the gnu C++ support in gdb, with significant additional contributions from Per Bothner and Daniel Berlin. James Clark wrote the gnu C++ demangler. Early work on C++ was by Peter TerMaat (who also did much general update work leading to release 3.0).

gdb uses the BFD subroutine library to examine multiple object-file formats; BFD was a joint project of David V. Henkel-Wallace, Rich Pixley, Steve Chamberlain, and John Gilmore.

David Johnson wrote the original COFF support; Pace Willison did the original support for encapsulated COFF.

Brent Benson of Harris Computer Systems contributed DWARF 2 support.

Adam de Boor and Bradley Davis contributed the ISI Optimum V support. Per Bothner, Noboyuki Hikichi, and Alessandro Forin contributed MIPS support. Jean-Daniel Fekete contributed Sun 386i support. Chris Hanson improved the HP9000 support. Noboyuki Hikichi and Tomoyuki Hasei contributed Sony/News OS 3 support. David Johnson contributed Encore Umax support. Jyrki Kuoppala contributed Altos 3068 support. Jeff Law contributed HP PA and SOM support. Keith Packard contributed NS32K support. Doug Rabson contributed Acorn Risc Machine support. Bob Rusk contributed Harris Nighthawk CX-UX support. Chris Smith contributed Convex support (and Fortran debugging). Jonathan Stone contributed Pyramid support. Michael Tiemann contributed SPARC support. Tim Tucker contributed support for the Gould NP1 and Gould Powernode. Pace Willison contributed Intel 386 support. Jay Vosburgh contributed Symmetry support. Marko Mlinar contributed OpenRISC 1000 support.

Andreas Schwab contributed M68K gnu/Linux support.

Rich Schaefer and Peter Schauer helped with support of SunOS shared libraries.

Jay Fenlason and Roland McGrath ensured that gdb and GAS agree about several machine instruction sets.

Patrick Duval, Ted Goldstein, Vikram Koka and Glenn Engel helped develop remote debugging. Intel Corporation, Wind River Systems, AMD, and ARM contributed remote debugging modules for the i960, VxWorks, A29K UDI, and RDI targets, respectively.

Brian Fox is the author of the readline libraries providing command-line editing and command history.

Andrew Beers of SUNY Buffalo wrote the language-switching code, the Modula-2 support, and contributed the Languages chapter of this manual.

Fred Fish wrote most of the support for Unix System Vr4. He also enhanced the command-completion support to cover C++ overloaded symbols.

Hitachi America (now Renesas America), Ltd. sponsored the support for H8/300, H8/500, and Super-H processors.

NEC sponsored the support for the v850, Vr4xxx, and Vr5xxx processors.

Mitsubishi (now Renesas) sponsored the support for D10V, D30V, and M32R/D processors.

Toshiba sponsored the support for the TX39 Mips processor.

Matsushita sponsored the support for the MN10200 and MN10300 processors.

Fujitsu sponsored the support for SPARClite and FR30 processors.

Kung Hsu, Jeff Law, and Rick Sladkey added support for hardware watchpoints.

Michael Snyder added support for tracepoints.

Stu Grossman wrote gdbserver.

Jim Kingdon, Peter Schauer, Ian Taylor, and Stu Grossman made nearly innumerable bug fixes and cleanups throughout gdb.

The following people at the Hewlett-Packard Company contributed support for the PA-RISC 2.0 architecture, HP-UX 10.20, 10.30, and 11.0 (narrow mode), HP's implementation of kernel threads, HP's aC++ compiler, and the Text User Interface (nee Terminal User Interface): Ben Krepp, Richard Title, John Bishop, Susan Macchia, Kathy Mann, Satish Pai, India Paul, Steve Rehrauer, and Elena Zannoni. Kim Haase provided HP-specific information in this manual.

DJ Delorie ported gdb to MS-DOS, for the DJGPP project. Robert Hoehne made significant contributions to the DJGPP port.

Cygnus Solutions has sponsored gdb maintenance and much of its development since 1991. Cygnus engineers who have worked on gdb fulltime include Mark Alexander, Jim Blandy, Per Bothner, Kevin Buettner, Edith Epstein, Chris Faylor, Fred Fish, Martin Hunt, Jim Ingham, John Gilmore, Stu Grossman, Kung Hsu, Jim Kingdon, John Metzler, Fernando Nasser, Geoffrey Noer, Dawn Perchik, Rich Pixley, Zdenek Radouch, Keith Seitz, Stan Shebs, David Taylor, and Elena Zannoni. In addition, Dave Brolley, Ian Carmichael, Steve Chamberlain, Nick Clifton, JT Conklin, Stan Cox, DJ Delorie, Ulrich Drepper, Frank Eigler, Doug Evans, Sean Fagan, David Henkel-Wallace, Richard Henderson, Jeff Holcomb, Jeff Law, Jim Lemke, Tom Lord, Bob Manson, Michael Meissner, Jason Merrill, Catherine Moore, Drew Moseley, Ken Raeburn, Gavin Romig-Koch, Rob Savoye, Jamie Smith, Mike Stump, Ian Taylor, Angela Thomas, Michael Tiemann, Tom Tromey, Ron Unrau, Jim Wilson, and David Zuhn have made contributions both large and small.

Andrew Cagney, Fernando Nasser, and Elena Zannoni, while working for Cygnus Solutions, implemented the original gdb/mi interface.

Jim Blandy added support for preprocessor macros, while working for Red Hat.

Andrew Cagney designed gdb's architecture vector. Many people including Andrew Cagney, Stephane Carrez, Randolph Chung, Nick Duffek, Richard Henderson, Mark Kettenis, Grace Sainsbury, Kei Sakamoto, Yoshinori Sato, Michael Snyder, Andreas Schwab, Jason Thorpe, Corinna Vinschen, Ulrich Weigand, and Elena Zannoni, helped with the migration of old architectures to this new framework.

Andrew Cagney completely re-designed and re-implemented gdb's unwinder framework, this consisting of a fresh new design featuring frame IDs, independent frame sniffers, and the sentinel frame. Mark Kettenis implemented the dwarf 2 unwinder, Jeff Johnston the libunwind unwinder, and Andrew Cagney the dummy, sentinel, tramp, and trad unwinders. The architecture-specific changes, each involving a complete rewrite of the architecture's frame code, were carried out by Jim Blandy, Joel Brobecker, Kevin Buettner, Andrew Cagney, Stephane Carrez, Randolph Chung, Orjan Friberg, Richard Henderson, Daniel Jacobowitz, Jeff Johnston, Mark Kettenis, Theodore A. Roth, Kei Sakamoto, Yoshinori Sato, Michael Snyder, Corinna Vinschen, and Ulrich Weigand.

Christian Zankel, Ross Morley, Bob Wilson, and Maxim Grigoriev from Tensilica, Inc. contributed support for Xtensa processors. Others who have worked on the Xtensa port of gdb in the past include Steve Tjiang, John Newlin, and Scott Foehner.

Michael Eager and staff of Xilinx, Inc., contributed support for the Xilinx MicroBlaze architecture.

 

 

 

1 一个简单的GDB例子

 

GDB手册很长,闲暇时可以慢慢看。不过几个简单的命令就可以让大家上手GDB。本节就来说说这几个命令。

 

GNU m4(一个通用宏处理器)早期的一个版本很明显的存在一个bug:有些时候,当我们修改默认的字符串引用符号,用于在一个宏定义内捕获宏定义的命令将不能正常工作。在下边这个简单的m4会话中,我们定义宏foo展开为 0 0 0 0;然后我们用m4内建defn来定义同样的宏bar。然后,当我们改变起字符串引用起始符号为<QUOTE>,结束符号为<UNQUOTE>,同样的程序会运行失败。

$ cd gnu/m4
$ ./m4
define(foo,0000)

foo
0000
define(bar,defn(`foo'))

bar
0000
changequote(<QUOTE>,<UNQUOTE>)

define(baz,defn(<QUOTE>foo<UNQUOTE>))
baz
Ctrl-d
m4: End of input: 0: fatal error: EOF in string

让我们用GDB看看都发生了什么。
$ gdb m4

gdb is free software and you are welcome to distribute copies
of it under certain conditions; type "show copying" to see
the conditions.
There is absolutely no warranty for gdb; type "show warranty"
for details.

gdb 7.0.50.20091114, Copyright 1999 Free Software Foundation, Inc...
(gdb)

GDB的启动速度很快,因为启动时GDB只读取必要的索引信息,其他信息在运行中按需读取。现在,我们先告诉GDB缩短显示信息宽度,以适合本手册使用。
(gdb) set width 70

我们需要查看m4内建函数changequote如何工作。通过阅读源码,我们找到相关子函数m4_changequote,所以我们通过GDB命令break(缩写是b)在此处设置断点。
 
(gdb) break m4_changequote
Breakpoint 1 at 0x62f4: file builtin.c, line 879.

然后使用run命令在GDB的监控环境中运行m4;直到m4_changequote函数之前,m4跟往常一样执行:
(gdb) run
Starting program: /work/Editorial/gdb/gnu/m4/m4
define(foo,0000)

foo
0000

为了出发断点,我们调用changequote。然后GDB挂起m4进程的运行,显示当前断点处的上下文信息。
changequote(<QUOTE>,<UNQUOTE>)

Breakpoint 1, m4_changequote (argc=3, argv=0x33c70)
at builtin.c:879
879         if (bad_argc(TOKEN_DATA_TEXT(argv[0]),argc,1,3))

现在使用命令n(next)单步执行当前函数的下一条语句。
(gdb) n
882         set_quotes((argc >= 2) ? TOKEN_DATA_TEXT(argv[1])/
: nil,

set_quotes看着想是我们要找的地方。那就通过命令s(step)单步跟踪进去。注意不是用next单步跟踪,因为step命令会进入任何调用函数中。进入set_quotes函数:
(gdb) s
set_quotes (lq=0x34c78 "<QUOTE>", rq=0x34c88 "<UNQUOTE>")
at input.c:530
530         if (lquote != def_lquote)

此时屏幕打印栈概要信息,包括m4当前运行到哪个函数和对应的参数。这只是当前的栈,通过backtrace命令(缩写是bt)我们可以查看全部的栈概要信息:backtrace显示当前所有的栈记录概要。
(gdb) bt
#0  set_quotes (lq=0x34c78 "<QUOTE>", rq=0x34c88 "<UNQUOTE>")
at input.c:530
#1  0x6344 in m4_changequote (argc=3, argv=0x33c70)
at builtin.c:882
#2  0x8174 in expand_macro (sym=0x33320) at macro.c:242
#3  0x7a88 in expand_token (obs=0x0, t=209696, td=0xf7fffa30)
at macro.c:71
#4  0x79dc in expand_input () at macro.c:40
#5  0x2930 in main (argc=0, argv=0xf7fffb20) at m4.c:195

继续单步跟踪,看看发生了什么。注意,单步跟踪的前两次我们用’s‘;后两次用’n‘,以避免进入xstrdup系统函数。
(gdb) s
0x3b5c  532         if (rquote != def_rquote)
(gdb) s
0x3b80  535         lquote = (lq == nil || *lq == '/0') ?  /
def_lquote : xstrdup(lq);
(gdb) n
536         rquote = (rq == nil || *rq == '/0') ? def_rquote/
: xstrdup(rq);
(gdb) n
538         len_lquote = strlen(rquote);

最后一行看起来有点奇怪啊。我们先检查一下lquote和rquote的值,看看是不是我们传入的新值。通过命令p(print)查看:
(gdb) p lquote
$1 = 0x35d40 "<QUOTE>"
(gdb) p rquote
$2 = 0x35d50 "<UNQUOTE>"

lquote和rquote确实是新的左右引号字符串。为了看看此时的代码上下文,我们用l(list)命令打印当前代码行前后10行的代码。
(gdb) l
533             xfree(rquote);
534
535         lquote = (lq == nil || *lq == '/0') ? def_lquote/
: xstrdup (lq);
536         rquote = (rq == nil || *rq == '/0') ? def_rquote/
: xstrdup (rq);
537
538         len_lquote = strlen(rquote);
539         len_rquote = strlen(lquote);
540     }
541
542     void

继续运行后两行代码,完成len_quote和len_rquote的赋值,然后检查他们的值。
(gdb) n
539         len_rquote = strlen(lquote);
(gdb) n
540     }
(gdb) p len_lquote
$3 = 9
(gdb) p len_rquote
$4 = 7

从len_lquote和len_rquote变量名的含义看,明显是错了。那么我们尝试用正确的值重新赋值,因为p命令能打印任何表达式的值,包括带有函数调用的赋值表达式,所以我们用p命令。
(gdb) p len_lquote=strlen(lquote)
$5 = 7
(gdb) p len_rquote=strlen(rquote)
$6 = 9

这样能纠正m4内建函数defn的错误么?我们用命令c(continue)让m4继续运行(不再单步跟踪),然后试着输入刚才引起错误的m4代码:
(gdb) c
Continuing.

define(baz,defn(<QUOTE>foo<UNQUOTE>))

baz
0000

成功了!~~新的字符串引用符号工作正常。问题看来就出在长度变量的拼写错误上。输入EOF让m4退出:
Ctrl-d
Program exited normally. 

GDB环境里打印“Program exited normally."这说明m4正常结束了。我们输入GDB命令quit退出gdb完成调试。
(gdb) quit

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐