Chinaunix首页 | 论坛 | 博客
  • 博客访问: 366552
  • 博文数量: 83
  • 博客积分: 5322
  • 博客等级: 中校
  • 技术积分: 1057
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-11 11:27
个人简介

爱生活,爱阅读

文章分类

全部博文(83)

文章存档

2015年(1)

2013年(1)

2012年(80)

2011年(1)

分类: LINUX

2012-09-08 00:03:21

Using GNU's GDB Debugger

By Peter Jay Salzman

Previous: 

Next: 



什么是调试器

A symbolic debugger is an application that runs your program, just like you can, when you type the name of your program. The difference is, a debugger can step through your source code, line by line, executing each line only when you want it to. You can even step through your program machine instruction by machine instruction (try that with printf())! At any point, you can inspect and even change the value of any variable at run-time. If your program crashes, a symbolic debugger tells you where and why the program crashed so you can deduce what went wrong. You can go through the program and see what source code lines get executed and in what order.

符号调试器(symbolic debugger)就是一个运行你的程序的软件,就像你能够通过输入你的程序名一样来运行它。不同之处在于,调试器可以单步遍历(step through)你的源代码,只有当你希望它执行某一行的时候,它才会执行该行。你甚至可以逐条机器指令逐条指令的执行(尝试使用printf()做到这一点)!在任何地让,你可以在运行时(run-time)检查甚至修改任何变量的值。如果你的程序崩溃(crash)了,那么,符号调试器会告诉你程序在哪里以及为什么会崩溃,进而推断出现了什么错误。你可以检查(go through)程序并查看执行了哪一行代码,以及以什么样的顺序执行的。

Do you have an infinite loop? No problem! Use a debugger to step through the loop and see why your conditional fails to do what you had expected. Did the program crash on a variable access? No problem! The debugger will tell you all sorts of information about the variable you tried to access and the value you assigned (or perhaps didn't assign) to it. Is there a line in your code which isn't executing? No problem! Use the debugger to see what gets executed, in what order, and why a particular line isn't getting reached! Other than a compiler, the debugger is the most useful tool a programmer can use.

你有一个死循环(infinite loop)?没问题!使用调试器单步遍历(step through)该循环,然后查看为什么你的条件没有按照你期望的执行。你的程序是否在访问一个变量时后崩溃了?没问题!调试器将告诉你所有的,你希望访问的该变量的各类信息以及你给予该变量的赋值(或者你可能没有赋值)。是否你的代码中的某行没有执行?没有问题!使用调试器查看什么执行了,以什么顺序执行的,以及为什么一个特殊的行没有被执行到!除了编译器,调试器是程序员可以使用的最有用的一个工具。

为什么不适用printf()?

Most people use the printf() debugging method. This is called adding "trace code" to your program. Simply put, they sprinkle their code with printf() to view the value of variables at certain strategic points and also to examine the order of execution of lines of source code.

大多说的人使用printf()的调试方法。这被称作为你程序添加“跟踪代码”(traced code)。通过简单的放置,人们使用printf()来查看在一个特定重要点(certain strategic point)上变量的值,以及检查源代码各行的执行顺序。

There are a few reasons why this may not be the best way of doing things:

为什么printf()可能不是最好的调试方式,有以下几种原因:

1、Sometimes you need a lot of printf()'s, and it can get tedious putting them in and taking them out. Inserting and deleting superfluous code all the time is really distracting. It draws attention away from what you're doing. It's like trying to implement a linked list while someone is talking to you about last night's Futurama episode.

有时你需要大量的printf(),将其放置在代码中以及从代码中取出非常的乏味。在任何时候,插入与删除冗余代码都很让人分心。它从你正在专注的事情上分散了注意力。就像你正在实现一个链表,而此时有人向你讲述昨晚“未来世界”的情节一样。

2A symbolic debugger can do an awful lot that printf() can't. You can do just about anything you can think of, including changing the value of variables at run-time, halt the program temporarily, list source code, print the datatype of a variable or struct that you don't recognize, jump to an arbitrary line of code, and much, much more.

一个符号调试器可以做很多printf()无法完成的事情。你可以做任何你想到的事情,包括改变运行时变量的值,临时终止程序,列出源代码,列出你不认识的变量或者结构的数据类型,进入任意一行代码,等等很多。

2、You can use a symbolic debugger on a running process; you don't even have to kill the process! Try that with printf()!

 你可以在一个运行的进程中使用符号调试器;你甚至不需要杀死该进程!使用printf()试一试吧!

3、You can use a symbolic debugger on a process that has already crashed and died without having to re-run the program. You'll see the state the program was in at the time of death and can inspect all the variables.

你可以在一个已经崩溃的进程上使用符号调试器而不需要重新运行至。你将看到程序崩溃时的程序状态,以及查看所有的变量值。

5A knowledge of GDB will increase your knowledge of programs, processes, memory and your language of choice.

    了解GDB会加深你对程序,进程,内存以及选择的编程语言的理解。

You'll be able to find and fix your bugs faster using a symbolic debugger like GDB. However, this isn't to say that printf() has no use in debugging. Sometimes it's the best way to go. However, for real code, a debugger can almost always get the job done orders of magnitude faster and easier. And using a debugger is always more elegant, and if you don't care about elegance, you should quit programming on Linux and start using Visual C++.

使用像GDB这样的符号调试器,你可以更加快速地来发现与解决缺陷。然而,这并不是说printf()在调试中没有用处。有时,它是最好的调试方式。然而,对于真实的代码,调试器在几乎总是能够快速地解决问题。并且使用一个调试器总是更加的简洁,如果你不在意简洁,那么你应该退出Linux编程,并开始使用Visual C++

什么是GDB?

In the previous section I told you what a symbolic debugger is. There are actually MANY symbolic debuggers, and in the next section I'll mention some of them. However, this tutorial is about one particular debugger which I use, called GDB.

在前面的部分,我告诉了你什么是符号调试器。通常由很多符号调试器,在下一部分,我将提及一些。然而,该教程是关于一个我使用的特殊的调试器,叫做GDB

GDB is a debugger which is part of the  GNU operating system. Its original author is Richard M. Stallman (affectionately known as "", one of the finest heroes of the free software movement), and has a long and impressive list of , including some interesting corporate sponsorship for support under various architectures. It's a wonderful piece of software and outclasses nearly every other debugger I've seen, including commercial ones.

GDB 是一种调试器,它是自由软件基金的GNU操作系统的一部分。它的原作者是Richard M.Stallman(众所周知的”RMS”,自由软件最好的英雄),以及很多令人印象深刻的贡献者,包括一些为了支持不同架构、有趣的赞助团体。它是一个极好的软件,几乎超越了所有我见过的调试器,包括一些商业版本的调试器。

GDB can be used to debug C, C++, Objective-C, Fortran, Java and Assembly programs. There's partial support for Modula-2 and Pascal. It'll run on any architecture you can think of that supports Unix, so learning GDB on your home PC will give you the power to debug code anywhere Unix can run!

GDB可以用于调试C,C++,Objective-CFortranJava 与汇编程序。部分支持Modula-2Pascal。它能够运行在任何你可以想到的支持Unix系统的架构。所以,在你的家用计算机上学习GDB将是你拥有调试任何Unix可以运行的代码。

Way back when, dbx was the canonical debugger people used on Unix systems. With the advent of GNU being the standard by which all Unix systems are measured, GDB became the canonical debugger of the debugging world. As a result, even commercial debuggers have a tendency to be command compatible (or even idea compatible) with GDB, so learning GDB will enable you to use a whole slew of other debuggers. In short, if you learn GDB, you will be able to debug anything almost anywhere with any debugger in the Unix world.

很早之前,dbx是一个在Unix系统上使用的权威调试器。随着GNU成为所有Unix系统的标准开始,GDB成为调试世界的权威调试器。于是,甚至商业的调试器也有这样一个趋势:与GDB命令兼容,所以学习GDB将使得你可以使用其他调试器。简而言之,如果你学习了GDB,你将能够在Unix世界中使用任何调试器来调试任何程序。

GDB's homepage is located at  and as of Nov 2006, is up to version 6.4.

GDB的主页在: ,到201111月,最新版本为6.4

GDB is  software (meaning that not only is GDB free software, but all publicly released derivatives and enhancements people make to GDB must also be free) and is licensed under the GNU 

.是非盈利版权软件(这意味着,不仅仅GDB是自由软件,所有公共发行的衍生版本以及GDB的增强版本均是自由的),其认证是基于GNUGPL

Other Symbolic Debuggers

其它符号调试器

This section documents other debuggers, both actively developed and long gone. I give a short history when the information is available. Any additions (history, debuggers not listed here, other front ends, screenshots), please .

这一部分介绍其它调试器,包括正在活跃开发的以及已经消失的。下面给出一个简短的调试器的历史。增加任何内容,

调试器

l  The first debugger that I know of was called dbx, and like GDB, was command line driven. The text UI of GDB was written to resemble dbx, although the two debuggers are not command line compatible. Other symbolic debuggers were written so that their UI resembled dbx (or GDB) as well. For this reason, you'll find many command line debuggers to be quite similar. If you learn to use GDB, you'll largely be able to navigate through most other debuggers.

我知道的第一个调试器叫做dbx,和GDB一样,也是基于命令行的。GDB的文本界面(UI,用户接口)与dbx相似,尽管这两种调试器的命令并不兼容。其它符号调试器也与dxb(或者GDB)的界面相似。正是基于此类原因,你将会法相许多命令行调试器都很相似。如果你学习是有GDB,你将很大程度上能能够驾驭其它大多数调试器。

l   is another debugger originally developed by Mark Russell but is now updated by Rod Armstrong. It also comes with its own . Ups includes a C interpreter which allows you to add fragments of code simply by editing them into the source window (the source file itself is not modified). Perversely, this lets you add debugging printf() calls without recompiling, relinking or even restarting the target program. ups supports C, C++ and limited FORTRAN debugging on SunOS, Solaris, Linux and FreeBSD. Screenshots: oldnew.

Ups是另一种调试器,它最初由Mark Russell开发,但现在由Rod Armstrong 更新。该调试器有其自身的特征(own theme song)。Ups包括一个C 解释器,它允许你通过编辑源代码窗口(source window)(而不改变源文件内容)而增加代码段。这让你可以添加printf()调用而无需重新编译,重新链接,或者重新启动目标程序。Ups支持在SunOS Solaris LinuxFreeBSD上进行C\C++和有限的Fortran调试。屏幕截图为:旧图新图

l  The  sells an excellent high-quality GUI debugger named . pgdbg specializes in debugging all kinds of parallel code on many different kinds of clusters (distributed memory, SMP servers, etc). While pgdb is a very high-powered debugger, it's also expensive. Screenshot.

波特兰小组销售一款优秀的、高质量的图形界面调试器:pgdbgPgdbg擅长于在多种不同的集群(分布式内存,多核处理器等等)上,进行各种并行代码的调试。而pgdb是一款非常高效的调试器,同时也非常昂贵。屏幕截图。

前端

l  xwpe. Screenshot.

Xwpe。屏幕截图。

l  The most popular GDB front end is DDD, the  which uses the Motif widget set. DDD has some nice features: it can give you graphical representations of linked lists, ADT's and trees. In addition, DDD is a front end to the Python, Java and Perl debuggers as well. I personally don't use DDD much, but I still appreciate it. DDD used to be quite buggy. Over the years it has stopped crashing regularly(!) on me, but as of March 2003, still crashes on a blue moon. In addition, the pop-up command tool definitely has "issues" with window managers that have multiple screens, like Enlightenment.

最著名的GDB前端是DDD,一个使用了Motif 控件集(Motif widget set)的数据显示调试器。DDD有一些友好的特征(nice feature):它将链表,ADT与树以图形化方式显示。另外,DDD也是PythonJavaPerl调试器的前端。我个人并不使用太多DDD,但我仍然很欣赏它。之前的DDD有很多缺陷。经过了这么多年,它已经不再经常崩溃。但是在20033月,仍然以蓝屏的方式崩溃了。另外,其弹出式命令工具(pop-up command tool)明显与Windows的多屏幕管理器相似,像启蒙。

l  tgdb is a Tcl/Tk front end for GDB first written in 1994 by a company named HighTec EDV-Systeme GmbH, in Germany. It was shareware (asking price was $30). Development and support seems to have ended many years ago. It shouldn't be confused with "trivial gdb" which is also called tgdb. Does anyone have a screenshot?

Tgdb是一种GDBTCL\TK前端,它最初由德国的HighTec EDV-Systeme GmbH公司于1994年开发。它是一个共享软件(价格为30美元)。这种前端的开发与支持在很多年之前好像已经结束。不应该把它与“trivial gdb”混淆,因为后者也叫做tgdb。有人有屏幕截图么?

l  xdbx is a front end to dbx that was created by Po Cheung of Microelectronics and Computer Technology Corporation (MCC) in March 10, 1989. It uses the old X Athena widget set (libxaw). It has its own license which is open source but not copyleft. Development died a long, long time ago. Screenshot.

Xdbx是由MCC Po Cheung1989310日创建的dbx的前端。它采用了老的X Athena 控件集(X Athena widget set)(libxaw)。它由他自己的版权许可证:开放源代码是盈利。很久很久之前已经停止开发。屏幕截图。

l  xxgdb is a front end to GDB that was created in December 1990 by Pierre Willard. It has its own license which is open source but not copyleft. It's built from the source code for xdbx; basically, xxgdb is xdbx adapted to GDB instead of dbx. xxgdb uses the old X Athena widget set (libxaw). It currently doesn't run on any system that uses unix98 posix TTYs. Development died in 2002. It most likely doesn't work with current versions of GDB. Screenshot.

Xxgdb是由Pierre Willard 199012月创建的GDB前端。它由自己的版权许可证:开放源代码当盈利。它从xdbx源代码构建;从根本上说,xxgdb是取代了dbx来匹配GDB的。Xxgdb使用老的X Athena 控件集(X Athena widget set)(libxaw)。现在,它在任何使用unix98 posix终端的系统上不再运行。于2002年中止开发。它与当前版本的GDB看起来最为无关。屏幕截图。

l   is a Motif based front end for GDB written by  back in January 3 1992. mxgdb is based on xxgdb: Jim ported xxgdb from the Athena widget set to the Motif widget set (in turn, xxgdb was a GDB port of xdbx). It's licensed under the GNU GPL and was last maintained (I think) by . It most likely doesn't work with current GDB versions. Does anyone have a screenshot?

是由Jim Tsillas 1992年元月3日开发的基于MotifGDB前端。Mxgdb基于xxgdbJim将采用了Athena控件集(Athena widget set)的xxgdb转换为采用Motif控件集(Motif widget set)(【】)。它遵守GNU GPL许可证,最后由Robert Stockmann维护(我认为)。它似乎与当前版本的GDB不兼容。任何人拥有其屏幕截图吗?

 

阅读(1056) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~