Liu 的个人资料唯有仰望是真实的照片日志列表更多 工具 帮助

日志


2006/11/18

腹肌培养项目启动

为了将最近日渐堆积的小肚子消下去,恢复之前比较好点的体型,从今天开始要找个地方做仰卧起坐,将腹肌培养项目优先级提高。

2006/11/14

用GDB调试程序(zz)

发信人: lwolf (被CH了...), 信区: Linux
标  题: 用GDB调试程序(好文,zz)
发信站: 北大未名站 (2006年11月08日17:15:54 星期三) , 站内信件

用GDB调试程序

说明:从CSDN的网站上找到的GDB使用说明。
原文标题:用GDB调试程序
作者:haoel
关键字:gdb 调试 c c++ gun
网址链接:出处

这篇文章非常好,所以转载了下来,作为收藏。

用GDB调试程序

GDB概述
————

GDB 是GNU开源组织发布的一个强大的UNIX下的程序调试工具。或许,各位比较喜欢那种
图形界面方式的,像VC、BCB等IDE的调试,但如果你是在 UNIX平台下做软件,你会发现
GDB这个调试工具有比VC、BCB的图形化调试器更强大的功能。所谓“寸有所长,尺有所短
”就是这个道理。

一般来说,GDB主要帮忙你完成下面四个方面的功能:

1、启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。
2、可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式)
3、当程序被停住时,可以检查此时你的程序中所发生的事。
4、动态的改变你程序的执行环境。

从上面看来,GDB和一般的调试工具没有什么两样,基本上也是完成这些功能,不过在细
节上,你会发现GDB这个调试工具的强大,大家可能比较习惯了图形化的调试工具,但有
时候,命令行的调试工具却有着图形化工具所不能完成的功能。让我们一一看来。

一个调试示例
——————

源程序:tst.c

1 #include <stdio.h>
2
3 int func(int n)
4 {
5 int sum=0,i;
6 for(i=0; i<n; i++)
7 {
8 sum+=i;
9 }
10 return sum;
11 }
12
13
14 main()
15 {
16 int i;
17 long result = 0;
18 for(i=1; i<=100; i++)
19 {
20 result += i;
21 }
22
23 printf("result[1-100] = %d \n", result );
24 printf("result[1-250] = %d \n", func(250) );
25 }

编译生成执行文件:(Linux下)
hchen/test> cc -g tst.c -o tst

使用GDB调试:

hchen/test> gdb tst <---------- 启动GDB
GNU gdb 5.1.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or 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.
This GDB was configured as "i386-suse-linux"...
(gdb) l <-------------------- l命令相当于list,从第一行开始例出原码。
1 #include <stdio.h>
2
3 int func(int n)
4 {
5 int sum=0,i;
6 for(i=0; i<n; i++)
7 {
8 sum+=i;
9 }
10 return sum;
(gdb) <-------------------- 直接回车表示,重复上一次命令
11 }
12
13
14 main()
15 {
16 int i;
17 long result = 0;
18 for(i=1; i<=100; i++)
19 {
20 result += i;
(gdb) break 16 <-------------------- 设置断点,在源程序第16行处。
Breakpoint 1 at 0x8048496: file tst.c, line 16.
(gdb) break func <-------------------- 设置断点,在函数func()入口处。
Breakpoint 2 at 0x8048456: file tst.c, line 5.
(gdb) info break <-------------------- 查看断点信息。
Num Type Disp Enb Address What
1 breakpoint keep y 0x08048496 in main at tst.c:16
2 breakpoint keep y 0x08048456 in func at tst.c:5
(gdb) r <--------------------- 运行程序,run命令简写
Starting program: /home/hchen/test/tst

Breakpoint 1, main () at tst.c:17 <---------- 在断点处停住。
17 long result = 0;
(gdb) n <--------------------- 单条语句执行,next命令简写。
18 for(i=1; i<=100; i++)
(gdb) n
20 result += i;
(gdb) n
18 for(i=1; i<=100; i++)
(gdb) n
20 result += i;
(gdb) c <--------------------- 继续运行程序,continue命令简写。
Continuing.
result[1-100] = 5050 <----------程序输出。

Breakpoint 2, func (n=250) at tst.c:5
5 int sum=0,i;
(gdb) n
6 for(i=1; i<=n; i++)
(gdb) p i <--------------------- 打印变量i的值,print命令简写。
$1 = 134513808
(gdb) n
8 sum+=i;
(gdb) n
6 for(i=1; i<=n; i++)
(gdb) p sum
$2 = 1
(gdb) n
8 sum+=i;
(gdb) p i
$3 = 2
(gdb) n
6 for(i=1; i<=n; i++)
(gdb) p sum
$4 = 3
(gdb) bt <--------------------- 查看函数堆栈。
#0 func (n=250) at tst.c:5
#1 0x080484e4 in main () at tst.c:24
#2 0x400409ed in __libc_start_main () from /lib/libc.so.6
(gdb) finish <--------------------- 退出函数。
Run till exit from #0 func (n=250) at tst.c:5
0x080484e4 in main () at tst.c:24
24 printf("result[1-250] = %d \n", func(250) );
Value returned is $6 = 31375
(gdb) c <--------------------- 继续运行。
Continuing.
result[1-250] = 31375 <----------程序输出。

Program exited with code 027. <--------程序退出,调试结束。
(gdb) q <--------------------- 退出gdb。
hchen/test>

好了,有了以上的感性认识,还是让我们来系统地认识一下gdb吧。

使用GDB
————

一般来说GDB主要调试的是C/C++的程序。要调试C/C++的程序,首先在编译时,我们必须
要把调试信息加到可执行文件中。使用编译器(cc/gcc/g++)的 -g 参数可以做到这一点
。如:

> cc -g hello.c -o hello
> g++ -g hello.cpp -o hello

如果没有-g,你将看不见程序的函数名、变量名,所代替的全是运行时的内存地址。当你
用-g把调试信息加入之后,并成功编译目标代码以后,让我们来看看如何用gdb来调试他
。

启动GDB的方法有以下几种:

1、gdb <program>
program也就是你的执行文件,一般在当然目录下。

2、gdb <program> core
用gdb同时调试一个运行程序和core文件,core是程序非法执行后core dump后产生的文件
。

3、gdb <program> <PID>
如果你的程序是一个服务程序,那么你可以指定这个服务程序运行时的进程ID。gdb会自
动attach上去,并调试他。program应该在PATH环境变量中搜索得到。



GDB启动时,可以加上一些GDB的启动开关,详细的开关可以用gdb -help查看。我在下面
只例举一些比较常用的参数:

-symbols <file>
-s <file>
从指定文件中读取符号表。

-se file
从指定文件中读取符号表信息,并把他用在可执行文件中。

-core <file>
-c <file>
调试时core dump的core文件。

-directory <directory>
-d <directory>
加入一个源文件的搜索路径。默认搜索路径是环境变量中PATH所定义的路径。

GDB的命令概貌
———————

启动gdb后,就你被带入gdb的调试环境中,就可以使用gdb的命令开始调试程序了,gdb的
命令可以使用help命令来查看,如下所示:

/home/hchen> gdb
GNU gdb 5.1.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or 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.
This GDB was configured as "i386-suse-linux".
(gdb) help
List of classes of commands:

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands

Type "help" followed by a class name for a list of commands in that class.
Type "help" followed by command name for full documentation.
Command name abbreviations are allowed if unambiguous.
(gdb)

gdb 的命令很多,gdb把之分成许多个种类。help命令只是例出gdb的命令种类,如果要看
种类中的命令,可以使用help <class> 命令,如:help breakpoints,查看设置断点的
所有命令。也可以直接help <command>来查看命令的帮助。


gdb中,输入命令时,可以不用打全命令,只用打命令的前几个字符就可以了,当然,命
令的前几个字符应该要标志着一个唯一的命令,在Linux下,你可以敲击两次TAB键来补齐
命令的全称,如果有重复的,那么gdb会把其例出来。

示例一:在进入函数func时,设置一个断点。可以敲入break func,或是直接就是b fun
c
(gdb) b func
Breakpoint 1 at 0x8048458: file hello.c, line 10.

示例二:敲入b按两次TAB键,你会看到所有b打头的命令:
(gdb) b
backtrace break bt
(gdb)

示例三:只记得函数的前缀,可以这样:
(gdb) b make_ <按TAB键>
(再按下一次TAB键,你会看到:)
make_a_section_from_file make_environ
make_abs_section make_function_type
make_blockvector make_pointer_type
make_cleanup make_reference_type
make_command make_symbol_completion_list
(gdb) b make_
GDB把所有make开头的函数全部例出来给你查看。

示例四:调试C++的程序时,有可以函数名一样。如:
(gdb) b 'bubble( M-?
bubble(double,double) bubble(int,int)
(gdb) b 'bubble(
你可以查看到C++中的所有的重载函数及参数。(注:M-?和“按两次TAB键”是一个意思
)

要退出gdb时,只用发quit或命令简称q就行了。

GDB中运行UNIX的shell程序
————————————

在gdb环境中,你可以执行UNIX的shell的命令,使用gdb的shell命令来完成:

shell <command string>
调用UNIX的shell来执行<command string>,环境变量SHELL中定义的UNIX的shell将会被
用来执行<command string>,如果SHELL没有定义,那就使用UNIX的标准shell:/bin/sh
。(在Windows中使用Command.com或 cmd.exe)

还有一个gdb命令是make:
make <make-args>
可以在gdb中执行make命令来重新build自己的程序。这个命令等价于“shell make <mak
e-args>”。

在GDB中运行程序
————————

当以gdb <program>方式启动gdb后,gdb会在PATH路径和当前目录中搜索<program>的源文
件。如要确认gdb是否读到源文件,可使用l或list命令,看看gdb是否能列出源代码。

在gdb中,运行程序使用r或是run命令。程序的运行,你有可能需要设置下面四方面的事
。

1、程序运行参数。
set args 可指定运行时参数。(如:set args 10 20 30 40 50)
show args 命令可以查看设置好的运行参数。

2、运行环境。
path <dir> 可设定程序的运行路径。
show paths 查看程序的运行路径。
set environment varname [=value] 设置环境变量。如:set env USER=hchen
show environment [varname] 查看环境变量。

3、工作目录。
cd <dir> 相当于shell的cd命令。
pwd 显示当前的所在目录。

4、程序的输入输出。
info terminal 显示你程序用到的终端的模式。
使用重定向控制程序输出。如:run > outfile
tty命令可以指写输入输出的终端设备。如:tty /dev/ttyb


调试已运行的程序
————————

两种方法:
1、在UNIX下用ps查看正在运行的程序的PID(进程ID),然后用gdb <program> PID格式
挂接正在运行的程序。
2、先用gdb <program>关联上源代码,并进行gdb,在gdb中用attach命令来挂接进程的P
ID。并用detach来取消挂接的进程。

暂停 / 恢复程序运行
—————————

调试程序中,暂停程序运行是必须的,GDB可以方便地暂停程序的运行。你可以设置程序
的在哪行停住,在什么条件下停住,在收到什么信号时停往等等。以便于你查看运行时的
变量,以及运行时的流程。

当进程被gdb停住时,你可以使用info program 来查看程序的是否在运行,进程号,被暂
停的原因。

在gdb中,我们可以有以下几种暂停方式:断点(BreakPoint)、观察点(WatchPoint)
、捕捉点(CatchPoint)、信号(Signals)、线程停止(Thread Stops)。如果要恢复
程序运行,可以使用c或是continue命令。

一、设置断点(BreakPoint)

我们用break命令来设置断点。正面有几点设置断点的方法:

break <function>
在进入指定函数时停住。C++中可以使用class::function或function(type,type)格式来
指定函数名。

break <linenum>
在指定行号停住。

break +offset
break -offset
在当前行号的前面或后面的offset行停住。offiset为自然数。

break filename:linenum
在源文件filename的linenum行处停住。

break filename:function
在源文件filename的function函数的入口处停住。

break *address
在程序运行的内存地址处停住。

break
break命令没有参数时,表示在下一条指令处停住。

break ... if <condition>
...可以是上述的参数,condition表示条件,在条件成立时停住。比如在循环境体中,可
以设置break if i=100,表示当i为100时停住程序。

查看断点时,可使用info命令,如下所示:(注:n表示断点号)
info breakpoints [n]
info break [n]

二、设置观察点(WatchPoint)

观察点一般来观察某个表达式(变量也是一种表达式)的值是否有变化了,如果有变化,
马上停住程序。我们有下面的几种方法来设置观察点:

watch <expr>
为表达式(变量)expr设置一个观察点。一量表达式值有变化时,马上停住程序。

rwatch <expr>
当表达式(变量)expr被读时,停住程序。

awatch <expr>
当表达式(变量)的值被读或被写时,停住程序。

info watchpoints
列出当前所设置了的所有观察点。

三、设置捕捉点(CatchPoint)

你可设置捕捉点来补捉程序运行时的一些事件。如:载入共享库(动态链接库)或是C++
的异常。设置捕捉点的格式为:

catch <event>
当event发生时,停住程序。event可以是下面的内容:
1、throw 一个C++抛出的异常。(throw为关键字)
2、catch 一个C++捕捉到的异常。(catch为关键字)
3、exec 调用系统调用exec时。(exec为关键字,目前此功能只在HP-UX下有用)
4、fork 调用系统调用fork时。(fork为关键字,目前此功能只在HP-UX下有用)
5、vfork 调用系统调用vfork时。(vfork为关键字,目前此功能只在HP-UX下有用)
6、load 或 load <libname> 载入共享库(动态链接库)时。(load为关键字,目前此功
能只在HP-UX下有用)
7、unload 或 unload <libname> 卸载共享库(动态链接库)时。(unload为关键字,目
前此功能只在HP-UX下有用)

tcatch <event>
只设置一次捕捉点,当程序停住以后,应点被自动删除。

四、维护停止点

上面说了如何设置程序的停止点,GDB中的停止点也就是上述的三类。在GDB中,如果你觉
得已定义好的停止点没有用了,你可以使用delete、clear、disable、enable这几个命令
来进行维护。

clear
清除所有的已定义的停止点。

clear <function>
clear <filename:function>
清除所有设置在函数上的停止点。

clear <linenum>
clear <filename:linenum>
清除所有设置在指定行上的停止点。

delete [breakpoints] [range...]
删除指定的断点,breakpoints为断点号。如果不指定断点号,则表示删除所有的断点。
range 表示断点号的范围(如:3-7)。其简写命令为d。

比删除更好的一种方法是disable停止点,disable了的停止点,GDB不会删除,当你还需
要时,enable即可,就好像回收站一样。

disable [breakpoints] [range...]
disable所指定的停止点,breakpoints为停止点号。如果什么都不指定,表示disable所
有的停止点。简写命令是dis.

enable [breakpoints] [range...]
enable所指定的停止点,breakpoints为停止点号。

enable [breakpoints] once range...
enable所指定的停止点一次,当程序停止后,该停止点马上被GDB自动disable。

enable [breakpoints] delete range...
enable所指定的停止点一次,当程序停止后,该停止点马上被GDB自动删除。

五、停止条件维护

前面在说到设置断点时,我们提到过可以设置一个条件,当条件成立时,程序自动停止,
这是一个非常强大的功能,这里,我想专门说说这个条件的相关维护命令。一般来说,为
断点设置一个条件,我们使用 if关键词,后面跟其断点条件。并且,条件设置好后,我
们可以用condition命令来修改断点的条件。(只有break和watch命令支持if, catch目
前暂不支持if)

condition <bnum> <expression>
修改断点号为bnum的停止条件为expression。

condition <bnum>
清除断点号为bnum的停止条件。


还有一个比较特殊的维护命令ignore,你可以指定程序运行时,忽略停止条件几次。

ignore <bnum> <count>
表示忽略断点号为bnum的停止条件count次。

六、为停止点设定运行命令

我们可以使用GDB提供的command命令来设置停止点的运行命令。也就是说,当运行的程序
在被停止住时,我们可以让其自动运行一些别的命令,这很有利行自动化调试。对基于G
DB的自动化调试是一个强大的支持。


commands [bnum]
... command-list ...
end

为断点号bnum指写一个命令列表。当程序被该断点停住时,gdb会依次运行命令列表中的
命令。

例如:

break foo if x>0
commands
printf "x is %d\n",x
continue
end
断点设置在函数foo中,断点条件是x>0,如果程序被断住后,也就是,一旦x的值在foo函
数中大于0,GDB会自动打印出x的值,并继续运行程序。

如果你要清除断点上的命令序列,那么只要简单的执行一下commands命令,并直接在打个
end就行了。

七、断点菜单

在 C++中,可能会重复出现同一个名字的函数若干次(函数重载),在这种情况下,bre
ak <function>不能告诉GDB要停在哪个函数的入口。当然,你可以使用break <function
(type)>也就是把函数的参数类型告诉GDB,以指定一个函数。否则的话,GDB会给你列出
一个断点菜单供你选择你所需要的断点。你只要输入你菜单列表中的编号就可以了。如:

(gdb) b String::after
[0] cancel
[1] all
[2] file:String.cc; line number:867
[3] file:String.cc; line number:860
[4] file:String.cc; line number:875
[5] file:String.cc; line number:853
[6] file:String.cc; line number:846
[7] file:String.cc; line number:735
> 2 4 6
Breakpoint 1 at 0xb26c: file String.cc, line 867.
Breakpoint 2 at 0xb344: file String.cc, line 875.
Breakpoint 3 at 0xafcc: file String.cc, line 846.
Multiple breakpoints were set.
Use the "delete" command to delete unwanted
breakpoints.
(gdb)

可见,GDB列出了所有after的重载函数,你可以选一下列表编号就行了。0表示放弃设置
断点,1表示所有函数都设置断点。

八、恢复程序运行和单步调试

当程序被停住了,你可以用continue命令恢复程序的运行直到程序结束,或下一个断点到
来。也可以使用step或next命令单步跟踪程序。

continue [ignore-count]
c [ignore-count]
fg [ignore-count]
恢复程序运行,直到程序结束,或是下一个断点到来。ignore-count表示忽略其后的断点
次数。continue,c,fg三个命令都是一样的意思。


step <count>
单步跟踪,如果有函数调用,他会进入该函数。进入函数的前提是,此函数被编译有deb
ug信息。很像VC等工具中的step in。后面可以加count也可以不加,不加表示一条条地执
行,加表示执行后面的count条指令,然后再停住。

next <count>
同样单步跟踪,如果有函数调用,他不会进入该函数。很像VC等工具中的step over。后
面可以加count也可以不加,不加表示一条条地执行,加表示执行后面的count条指令,然
后再停住。

set step-mode
set step-mode on
打开step-mode模式,于是,在进行单步跟踪时,程序不会因为没有debug信息而不停住。
这个参数有很利于查看机器码。

set step-mod off
关闭step-mode模式。

finish
运行程序,直到当前函数完成返回。并打印函数返回时的堆栈地址和返回值及参数值等信
息。

until 或 u
当你厌倦了在一个循环体内单步跟踪时,这个命令可以运行程序直到退出循环体。

stepi 或 si
nexti 或 ni
单步跟踪一条机器指令!一条程序代码有可能由数条机器指令完成,stepi和nexti可以单
步执行机器指令。与之一样有相同功能的命令是 “display/i $pc” ,当运行完这个命
令后,单步跟踪会在打出程序代码的同时打出机器指令(也就是汇编代码)

九、信号(Signals)

信号是一种软中断,是一种处理异步事件的方法。一般来说,操作系统都支持许多信号。
尤其是UNIX,比较重要应用程序一般都会处理信号。UNIX定义了许多信号,比如SIGINT表
示中断字符信号,也就是Ctrl+C的信号,SIGBUS表示硬件故障的信号;SIGCHLD表示子进
程状态改变信号; SIGKILL表示终止程序运行的信号,等等。信号量编程是UNIX下非常重
要的一种技术。

GDB有能力在你调试程序的时候处理任何一种信号,你可以告诉GDB需要处理哪一种信号。
你可以要求GDB收到你所指定的信号时,马上停住正在运行的程序,以供你进行调试。你
可以用GDB的handle命令来完成这一功能。

handle <signal> <keywords...>
在GDB 中定义一个信号处理。信号<signal>可以以SIG开头或不以SIG开头,可以用定义一
个要处理信号的范围(如:SIGIO- SIGKILL,表示处理从SIGIO信号到SIGKILL的信号,其
中包括SIGIO,SIGIOT,SIGKILL三个信号),也可以使用关键字 all来标明要处理所有的
信号。一旦被调试的程序接收到信号,运行程序马上会被GDB停住,以供调试。其<keywo
rds>可以是以下几种关键字的一个或多个。

nostop
当被调试的程序收到信号时,GDB不会停住程序的运行,但会打出消息告诉你收到这种信
号。
stop
当被调试的程序收到信号时,GDB会停住你的程序。
print
当被调试的程序收到信号时,GDB会显示出一条信息。
noprint
当被调试的程序收到信号时,GDB不会告诉你收到信号的信息。
pass
noignore
当被调试的程序收到信号时,GDB不处理信号。这表示,GDB会把这个信号交给被调试程序
会处理。
nopass
ignore
当被调试的程序收到信号时,GDB不会让被调试程序来处理这个信号。


info signals
info handle
查看有哪些信号在被GDB检测中。

十、线程(Thread Stops)

如果你程序是多线程的话,你可以定义你的断点是否在所有的线程上,或是在某个特定的
线程。GDB很容易帮你完成这一工作。

break <linespec> thread <threadno>
break <linespec> thread <threadno> if ...
linespec 指定了断点设置在的源程序的行号。threadno指定了线程的ID,注意,这个ID
是GDB分配的,你可以通过“info threads”命令来查看正在运行程序中的线程信息。如
果你不指定thread <threadno>则表示你的断点设在所有线程上面。你还可以为某线程指
定断点条件。如:

(gdb) break frik.c:13 thread 28 if bartab > lim

当你的程序被GDB停住时,所有的运行线程都会被停住。这方便你你查看运行程序的总体
情况。而在你恢复程序运行时,所有的线程也会被恢复运行。那怕是主进程在被单步调试
时。

查看栈信息
—————

当程序被停住了,你需要做的第一件事就是查看程序是在哪里停住的。当你的程序调用了
一个函数,函数的地址,函数参数,函数内的局部变量都会被压入“栈”(Stack)中。
你可以用GDB命令来查看当前的栈中的信息。

下面是一些查看函数调用栈信息的GDB命令:

backtrace
bt
打印当前的函数调用栈的所有信息。如:

(gdb) bt
#0 func (n=250) at tst.c:6
#1 0x08048524 in main (argc=1, argv=0xbffff674) at tst.c:30
#2 0x400409ed in __libc_start_main () from /lib/libc.so.6

从上可以看出函数的调用栈信息:__libc_start_main --> main() --> func()


backtrace <n>
bt <n>
n是一个正整数,表示只打印栈顶上n层的栈信息。

backtrace <-n>
bt <-n>
-n表一个负整数,表示只打印栈底下n层的栈信息。

如果你要查看某一层的信息,你需要在切换当前的栈,一般来说,程序停止时,最顶层的
栈就是当前栈,如果你要查看栈下面层的详细信息,首先要做的是切换当前栈。

frame <n>
f <n>
n是一个从0开始的整数,是栈中的层编号。比如:frame 0,表示栈顶,frame 1,表示栈
的第二层。

up <n>
表示向栈的上面移动n层,可以不打n,表示向上移动一层。

down <n>
表示向栈的下面移动n层,可以不打n,表示向下移动一层。


上面的命令,都会打印出移动到的栈层的信息。如果你不想让其打出信息。你可以使用这
三个命令:

select-frame <n> 对应于 frame 命令。
up-silently <n> 对应于 up 命令。
down-silently <n> 对应于 down 命令。


查看当前栈层的信息,你可以用以下GDB命令:

frame 或 f
会打印出这些信息:栈的层编号,当前的函数名,函数参数值,函数所在文件及行号,函
数执行到的语句。

info frame
info f
这个命令会打印出更为详细的当前栈层的信息,只不过,大多数都是运行时的内内地址。
比如:函数地址,调用函数的地址,被调用函数的地址,目前的函数是由什么样的程序语
言写成的、函数参数地址及值、局部变量的地址等等。如:
(gdb) info f
Stack level 0, frame at 0xbffff5d4:
eip = 0x804845d in func (tst.c:6); saved eip 0x8048524
called by frame at 0xbffff60c
source language c.
Arglist at 0xbffff5d4, args: n=250
Locals at 0xbffff5d4, Previous frame's sp is 0x0
Saved registers:
ebp at 0xbffff5d4, eip at 0xbffff5d8

info args
打印出当前函数的参数名及其值。

info locals
打印出当前函数中所有局部变量及其值。

info catch
打印出当前的函数中的异常处理信息。


查看源程序
—————

一、显示源代码

GDB 可以打印出所调试程序的源代码,当然,在程序编译时一定要加上-g的参数,把源程
序信息编译到执行文件中。不然就看不到源程序了。当程序停下来以后, GDB会报告程序
停在了那个文件的第几行上。你可以用list命令来打印程序的源代码。还是来看一看查看
源代码的GDB命令吧。

list <linenum>
显示程序第linenum行的周围的源程序。

list <function>
显示函数名为function的函数的源程序。

list
显示当前行后面的源程序。

list -
显示当前行前面的源程序。

一般是打印当前行的上5行和下5行,如果显示函数是是上2行下8行,默认是10行,当然,
你也可以定制显示的范围,使用下面命令可以设置一次显示源程序的行数。

set listsize <count>
设置一次显示源代码的行数。

show listsize
查看当前listsize的设置。


list命令还有下面的用法:

list <first>, <last>
显示从first行到last行之间的源代码。

list , <last>
显示从当前行到last行之间的源代码。

list +
往后显示源代码。


一般来说在list后面可以跟以下这们的参数:

<linenum> 行号。
<+offset> 当前行号的正偏移量。
<-offset> 当前行号的负偏移量。
<filename:linenum> 哪个文件的哪一行。
<function> 函数名。
<filename:function> 哪个文件中的哪个函数。
<*address> 程序运行时的语句在内存中的地址。


二、搜索源代码

不仅如此,GDB还提供了源代码搜索的命令:

forward-search <regexp>
search <regexp>
向前面搜索。

reverse-search <regexp>
全部搜索。

其中,<regexp>就是正则表达式,也主一个字符串的匹配模式,关于正则表达式,我就不
在这里讲了,还请各位查看相关资料。


三、指定源文件的路径

某些时候,用-g编译过后的执行程序中只是包括了源文件的名字,没有路径名。GDB提供
了可以让你指定源文件的路径的命令,以便GDB进行搜索。

directory <dirname ... >
dir <dirname ... >
加一个源文件路径到当前路径的前面。如果你要指定多个路径,UNIX下你可以使用“:”
,Windows下你可以使用“;”。
directory
清除所有的自定义的源文件搜索路径信息。

show directories
显示定义了的源文件搜索路径。


四、源代码的内存

你可以使用info line命令来查看源代码在内存中的地址。info line后面可以跟“行号”
,“函数名”,“文件名:行号”,“文件名:函数名”,这个命令会打印出所指定的源码
在运行时的内存地址,如:

(gdb) info line tst.c:func
Line 5 of "tst.c" starts at address 0x8048456 <func+6> and ends at 0x804845d 
<func+13>.

还有一个命令(disassemble)你可以查看源程序的当前执行时的机器码,这个命令会把
目前内存中的指令dump出来。如下面的示例表示查看函数func的汇编代码。

(gdb) disassemble func
Dump of assembler code for function func:
0x8048450 <func>: push %ebp
0x8048451 <func+1>: mov %esp,%ebp
0x8048453 <func+3>: sub $0x18,%esp
0x8048456 <func+6>: movl $0x0,0xfffffffc(%ebp)
0x804845d <func+13>: movl $0x1,0xfffffff8(%ebp)
0x8048464 <func+20>: mov 0xfffffff8(%ebp),%eax
0x8048467 <func+23>: cmp 0x8(%ebp),%eax
0x804846a <func+26>: jle 0x8048470 <func+32>
0x804846c <func+28>: jmp 0x8048480 <func+48>
0x804846e <func+30>: mov %esi,%esi
0x8048470 <func+32>: mov 0xfffffff8(%ebp),%eax
0x8048473 <func+35>: add %eax,0xfffffffc(%ebp)
0x8048476 <func+38>: incl 0xfffffff8(%ebp)
0x8048479 <func+41>: jmp 0x8048464 <func+20>
0x804847b <func+43>: nop
0x804847c <func+44>: lea 0x0(%esi,1),%esi
0x8048480 <func+48>: mov 0xfffffffc(%ebp),%edx
0x8048483 <func+51>: mov %edx,%eax
0x8048485 <func+53>: jmp 0x8048487 <func+55>
0x8048487 <func+55>: mov %ebp,%esp
0x8048489 <func+57>: pop %ebp
0x804848a <func+58>: ret
End of assembler dump.

查看运行时数据
———————

在你调试程序时,当程序被停住时,你可以使用print命令(简写命令为p),或是同义命
令inspect来查看当前程序的运行数据。print命令的格式是:

print <expr>
print /<f> <expr>
<expr>是表达式,是你所调试的程序的语言的表达式(GDB可以调试多种编程语言),<f
>是输出的格式,比如,如果要把表达式按16进制的格式输出,那么就是/x。


一、表达式

print和许多GDB的命令一样,可以接受一个表达式,GDB会根据当前的程序运行的数据来
计算这个表达式,既然是表达式,那么就可以是当前程序运行中的const常量、变量、函
数等内容。可惜的是GDB不能使用你在程序中所定义的宏。

表达式的语法应该是当前所调试的语言的语法,由于C/C++是一种大众型的语言,所以,
本文中的例子都是关于C/C++的。(而关于用GDB调试其它语言的章节,我将在后面介绍)

在表达式中,有几种GDB所支持的操作符,它们可以用在任何一种语言中。

@
是一个和数组有关的操作符,在后面会有更详细的说明。

::
指定一个在文件或是一个函数中的变量。

{<type>} <addr>
表示一个指向内存地址<addr>的类型为type的一个对象。


二、程序变量

在GDB中,你可以随时查看以下三种变量的值:
1、全局变量(所有文件可见的)
2、静态全局变量(当前文件可见的)
3、局部变量(当前Scope可见的)

如果你的局部变量和全局变量发生冲突(也就是重名),一般情况下是局部变量会隐藏全
局变量,也就是说,如果一个全局变量和一个函数中的局部变量同名时,如果当前停止点
在函数中,用print显示出的变量的值会是函数中的局部变量的值。如果此时你想查看全
局变量的值时,你可以使用“::”操作符:

file::variable
function::variable
可以通过这种形式指定你所想查看的变量,是哪个文件中的或是哪个函数中的。例如,查
看文件f2.c中的全局变量x的值:

gdb) p 'f2.c'::x

当然,“::”操作符会和C++中的发生冲突,GDB能自动识别“::” 是否C++的操作符,所
以你不必担心在调试C++程序时会出现异常。

另外,需要注意的是,如果你的程序编译时开启了优化选项,那么在用GDB调试被优化过
的程序时,可能会发生某些变量不能访问,或是取值错误码的情况。这个是很正常的,因
为优化程序会删改你的程序,整理你程序的语句顺序,剔除一些无意义的变量等,所以在
GDB调试这种程序时,运行时的指令和你所编写指令就有不一样,也就会出现你所想象不
到的结果。对付这种情况时,需要在编译程序时关闭编译优化。一般来说,几乎所有的编
译器都支持编译优化的开关,例如,GNU 的C/C++编译器GCC,你可以使用“-gstabs”选
项来解决这个问题。关于编译器的参数,还请查看编译器的使用说明文档。


三、数组

有时候,你需要查看一段连续的内存空间的值。比如数组的一段,或是动态分配的数据的
大小。你可以使用GDB的“@”操作符,“@”的左边是第一个内存的地址的值,“@”的右
边则你你想查看内存的长度。例如,你的程序中有这样的语句:

int *array = (int *) malloc (len * sizeof (int));

于是,在GDB调试过程中,你可以以如下命令显示出这个动态数组的取值:

p *array@len

@的左边是数组的首地址的值,也就是变量array所指向的内容,右边则是数据的长度,其
保存在变量len中,其输出结果,大约是下面这个样子的:

(gdb) p *array@len
$1 = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38,
 40}

如果是静态数组的话,可以直接用print数组名,就可以显示数组中所有数据的内容了。


四、输出格式

一般来说,GDB会根据变量的类型输出变量的值。但你也可以自定义GDB的输出的格式。例
如,你想输出一个整数的十六进制,或是二进制来查看这个整型变量的中的位的情况。要
做到这样,你可以使用GDB的数据显示格式:

x 按十六进制格式显示变量。
d 按十进制格式显示变量。
u 按十六进制格式显示无符号整型。
o 按八进制格式显示变量。
t 按二进制格式显示变量。
a 按十六进制格式显示变量。
c 按字符格式显示变量。
f 按浮点数格式显示变量。

(gdb) p i
$21 = 101

(gdb) p/a i
$22 = 0x65

(gdb) p/c i
$23 = 101 'e'

(gdb) p/f i
$24 = 1.41531145e-43

(gdb) p/x i
$25 = 0x65

(gdb) p/t i
$26 = 1100101


五、查看内存

你可以使用examine命令(简写是x)来查看内存地址中的值。x命令的语法如下所示:

x/<n/f/u> <addr>

n、f、u是可选的参数。

n 是一个正整数,表示显示内存的长度,也就是说从当前地址向后显示几个地址的内容。
f 表示显示的格式,参见上面。如果地址所指的是字符串,那么格式可以是s,如果地十
是指令地址,那么格式可以是i。
u 表示从当前地址往后请求的字节数,如果不指定的话,GDB默认是4个bytes。u参数可以
用下面的字符来代替,b表示单字节,h表示双字节,w表示四字节,g表示八字节。当我们
指定了字节长度后,GDB会从指内存定的内存地址开始,读写指定字节,并把其当作一个
值取出来。

<addr>表示一个内存地址。

n/f/u三个参数可以一起使用。例如:

命令:x/3uh 0x54320 表示,从内存地址0x54320读取内容,h表示以双字节为一个单位,
3表示三个单位,u表示按十六进制显示。


六、自动显示

你可以设置一些自动显示的变量,当程序停住时,或是在你单步跟踪时,这些变量会自动
显示。相关的GDB命令是display。

display <expr>
display/<fmt> <expr>
display/<fmt> <addr>

expr是一个表达式,fmt表示显示的格式,addr表示内存地址,当你用display设定好了一
个或多个表达式后,只要你的程序被停下来,GDB会自动显示你所设置的这些表达式的值
。

格式i和s同样被display支持,一个非常有用的命令是:

display/i $pc

$pc是GDB的环境变量,表示着指令的地址,/i则表示输出格式为机器指令码,也就是汇编
。于是当程序停下后,就会出现源代码和机器指令码相对应的情形,这是一个很有意思的
功能。

下面是一些和display相关的GDB命令:

undisplay <dnums...>
delete display <dnums...>
删除自动显示,dnums意为所设置好了的自动显式的编号。如果要同时删除几个,编号可
以用空格分隔,如果要删除一个范围内的编号,可以用减号表示(如:2-5)

disable display <dnums...>
enable display <dnums...>
disable和enalbe不删除自动显示的设置,而只是让其失效和恢复。

info display
查看display设置的自动显示的信息。GDB会打出一张表格,向你报告当然调试中设置了多
少个自动显示设置,其中包括,设置的编号,表达式,是否enable。

七、设置显示选项

GDB中关于显示的选项比较多,这里我只例举大多数常用的选项。

set print address
set print address on
打开地址输出,当程序显示函数信息时,GDB会显出函数的参数地址。系统默认为打开的
,如:

(gdb) f
#0 set_quotes (lq=0x34c78 "<<", rq=0x34c88 ">>")
at input.c:530
530 if (lquote != def_lquote)


set print address off
关闭函数的参数地址显示,如:

(gdb) set print addr off
(gdb) f
#0 set_quotes (lq="<<", rq=">>") at input.c:530
530 if (lquote != def_lquote)

show print address
查看当前地址显示选项是否打开。

set print array
set print array on
打开数组显示,打开后当数组显示时,每个元素占一行,如果不打开的话,每个元素则以
逗号分隔。这个选项默认是关闭的。与之相关的两个命令如下,我就不再多说了。

set print array off
show print array

set print elements <number-of-elements>
这个选项主要是设置数组的,如果你的数组太大了,那么就可以指定一个<number-of-el
ements>来指定数据显示的最大长度,当到达这个长度时,GDB就不再往下显示了。如果设
置为0,则表示不限制。

show print elements
查看print elements的选项信息。

set print null-stop <on/off>
如果打开了这个选项,那么当显示字符串时,遇到结束符则停止显示。这个选项默认为o
ff。

set print pretty on
如果打开printf pretty这个选项,那么当GDB显示结构体时会比较漂亮。如:

$1 = {
next = 0x0,
flags = {
sweet = 1,
sour = 1
},
meat = 0x54 "Pork"
}

set print pretty off
关闭printf pretty这个选项,GDB显示结构体时会如下显示:

$1 = {next = 0x0, flags = {sweet = 1, sour = 1}, meat = 0x54 "Pork"}

show print pretty
查看GDB是如何显示结构体的。


set print sevenbit-strings <on/off>
设置字符显示,是否按“\nnn”的格式显示,如果打开,则字符串或字符数据按\nnn显示
,如“\065”。

show print sevenbit-strings
查看字符显示开关是否打开。

set print union <on/off>
设置显示结构体时,是否显式其内的联合体数据。例如有以下数据结构:

typedef enum {Tree, Bug} Species;
typedef enum {Big_tree, Acorn, Seedling} Tree_forms;
typedef enum {Caterpillar, Cocoon, Butterfly}
Bug_forms;

struct thing {
Species it;
union {
Tree_forms tree;
Bug_forms bug;
} form;
};

struct thing foo = {Tree, {Acorn}};

当打开这个开关时,执行 p foo 命令后,会如下显示:
$1 = {it = Tree, form = {tree = Acorn, bug = Cocoon}}

当关闭这个开关时,执行 p foo 命令后,会如下显示:
$1 = {it = Tree, form = {...}}

show print union
查看联合体数据的显示方式

set print object <on/off>
在C++中,如果一个对象指针指向其派生类,如果打开这个选项,GDB会自动按照虚方法调
用的规则显示输出,如果关闭这个选项的话,GDB就不管虚函数表了。这个选项默认是of
f。

show print object
查看对象选项的设置。

set print static-members <on/off>
这个选项表示,当显示一个C++对象中的内容是,是否显示其中的静态数据成员。默认是
on。

show print static-members
查看静态数据成员选项设置。

set print vtbl <on/off>
当此选项打开时,GDB将用比较规整的格式来显示虚函数表时。其默认是关闭的。

show print vtbl
查看虚函数显示格式的选项。


八、历史记录

当你用GDB的print查看程序运行时的数据时,你每一个print都会被GDB记录下来。GDB会
以$1, $2, $3 .....这样的方式为你每一个print命令编上号。于是,你可以使用这个编
号访问以前的表达式,如$1。这个功能所带来的好处是,如果你先前输入了一个比较长的
表达式,如果你还想查看这个表达式的值,你可以使用历史记录来访问,省去了重复输入
。


九、GDB环境变量

你可以在GDB的调试环境中定义自己的变量,用来保存一些调试程序中的运行数据。要定
义一个GDB的变量很简单只需。使用GDB的set命令。GDB的环境变量和UNIX一样,也是以$
起头。如:

set $foo = *object_ptr

使用环境变量时,GDB会在你第一次使用时创建这个变量,而在以后的使用中,则直接对
其賦值。环境变量没有类型,你可以给环境变量定义任一的类型。包括结构体和数组。

show convenience
该命令查看当前所设置的所有的环境变量。

这是一个比较强大的功能,环境变量和程序变量的交互使用,将使得程序调试更为灵活便
捷。例如:

set $i = 0
print bar[$i++]->contents

于是,当你就不必,print bar[0]->contents, print bar[1]->contents地输入命令了。
输入这样的命令后,只用敲回车,重复执行上一条语句,环境变量会自动累加,从而完成
逐个输出的功能。


十、查看寄存器

要查看寄存器的值,很简单,可以使用如下命令:

info registers
查看寄存器的情况。(除了浮点寄存器)

info all-registers
查看所有寄存器的情况。(包括浮点寄存器)

info registers <regname ...>
查看所指定的寄存器的情况。

寄存器中放置了程序运行时的数据,比如程序当前运行的指令地址(ip),程序的当前堆
栈地址(sp)等等。你同样可以使用print命令来访问寄存器的情况,只需要在寄存器名
字前加一个$符号就可以了。如:p $eip。

改变程序的执行
———————

一旦使用GDB挂上被调试程序,当程序运行起来后,你可以根据自己的调试思路来动态地
在GDB中更改当前被调试程序的运行线路或是其变量的值,这个强大的功能能够让你更好
的调试你的程序,比如,你可以在程序的一次运行中走遍程序的所有分支。


一、修改变量值

修改被调试程序运行时的变量值,在GDB中很容易实现,使用GDB的print命令即可完成。
如:

(gdb) print x=4

x=4这个表达式是C/C++的语法,意为把变量x的值修改为4,如果你当前调试的语言是Pas
cal,那么你可以使用Pascal的语法:x:=4。

在某些时候,很有可能你的变量和GDB中的参数冲突,如:

(gdb) whatis width
type = double
(gdb) p width
$4 = 13
(gdb) set width=47
Invalid syntax in expression.

因为,set width是GDB的命令,所以,出现了“Invalid syntax in expression”的设置
错误,此时,你可以使用set var命令来告诉GDB,width不是你GDB的参数,而是程序的变
量名,如:

(gdb) set var width=47

另外,还可能有些情况,GDB并不报告这种错误,所以保险起见,在你改变程序变量取值
时,最好都使用set var格式的GDB命令。


二、跳转执行

一般来说,被调试程序会按照程序代码的运行顺序依次执行。GDB提供了乱序执行的功能
,也就是说,GDB可以修改程序的执行顺序,可以让程序执行随意跳跃。这个功能可以由
GDB的jump命令来完:

jump <linespec>
指定下一条语句的运行点。<linespce>可以是文件的行号,可以是file:line格式,可以
是+num这种偏移量格式。表式着下一条运行语句从哪里开始。

jump <address>
这里的<address>是代码行的内存地址。

注意,jump命令不会改变当前的程序栈中的内容,所以,当你从一个函数跳到另一个函数
时,当函数运行完返回时进行弹栈操作时必然会发生错误,可能结果还是非常奇怪的,甚
至于产生程序Core Dump。所以最好是同一个函数中进行跳转。

熟悉汇编的人都知道,程序运行时,有一个寄存器用于保存当前代码所在的内存地址。所
以,jump命令也就是改变了这个寄存器中的值。于是,你可以使用“set $pc”来更改跳
转执行的地址。如:

set $pc = 0x485


三、产生信号量

使用singal命令,可以产生一个信号量给被调试的程序。如:中断信号Ctrl+C。这非常方
便于程序的调试,可以在程序运行的任意位置设置断点,并在该断点用GDB产生一个信号
量,这种精确地在某处产生信号非常有利程序的调试。

语法是:signal <singal>,UNIX的系统信号量通常从1到15。所以<singal>取值也在这个
范围。

single命令和shell的kill命令不同,系统的kill命令发信号给被调试程序时,是由GDB截
获的,而single命令所发出一信号则是直接发给被调试程序的。


四、强制函数返回

如果你的调试断点在某个函数中,并还有语句没有执行完。你可以使用return命令强制函
数忽略还没有执行的语句并返回。

return
return <expression>
使用return命令取消当前函数的执行,并立即返回,如果指定了<expression>,那么该表
达式的值会被认作函数的返回值。


五、强制调用函数

call <expr>
表达式中可以一是函数,以此达到强制调用函数的目的。并显示函数的返回值,如果函数
返回值是void,那么就不显示。

另一个相似的命令也可以完成这一功能——print,print后面可以跟表达式,所以也可以
用他来调用函数,print和call的不同是,如果函数返回void,call则不显示,print则显
示函数返回值,并把该值存入历史数据中。



在不同语言中使用GDB
——————————

GDB 支持下列语言:C, C++, Fortran, PASCAL, Java, Chill, assembly, 和 Modula-2
。一般说来,GDB会根据你所调试的程序来确定当然的调试语言,比如:发现文件名后缀
为“.c”的,GDB会认为是C程序。文件名后缀为 “.C, .cc, .cp, .cpp, .cxx, .c++”
的,GDB会认为是C++程序。而后缀是“.f, .F”的,GDB会认为是Fortran程序,还有,后
缀为如果是“.s, .S”的会认为是汇编语言。

也就是说,GDB会根据你所调试的程序的语言,来设置自己的语言环境,并让GDB的命令跟
着语言环境的改变而改变。比如一些GDB命令需要用到表达式或变量时,这些表达式或变
量的语法,完全是根据当前的语言环境而改变的。例如C/C++中对指针的语法是*p,而在
Modula-2中则是p^。并且,如果你当前的程序是由几种不同语言一同编译成的,那到在调
试过程中,GDB也能根据不同的语言自动地切换语言环境。这种跟着语言环境而改变的功
能,真是体贴开发人员的一种设计。


下面是几个相关于GDB语言环境的命令:

show language
查看当前的语言环境。如果GDB不能识为你所调试的编程语言,那么,C语言被认为是默认
的环境。

info frame
查看当前函数的程序语言。

info source
查看当前文件的程序语言。

如果GDB没有检测出当前的程序语言,那么你也可以手动设置当前的程序语言。使用set 
language命令即可做到。

当set language命令后什么也不跟的话,你可以查看GDB所支持的语言种类:

(gdb) set language
The currently understood settings are:

local or auto Automatic setting based on source file
c Use the C language
c++ Use the C++ language
asm Use the Asm language
chill Use the Chill language
fortran Use the Fortran language
java Use the Java language
modula-2 Use the Modula-2 language
pascal Use the Pascal language
scheme Use the Scheme language

于是你可以在set language后跟上被列出来的程序语言名,来设置当前的语言环境。



后记
——

GDB 是一个强大的命令行调试工具。大家知道命令行的强大就是在于,其可以形成执行序
列,形成脚本。UNIX下的软件全是命令行的,这给程序开发提代供了极大的便利,命令行
软件的优势在于,它们可以非常容易的集成在一起,使用几个简单的已有工具的命令,就
可以做出一个非常强大的功能。

于是 UNIX下的软件比Windows下的软件更能有机地结合,各自发挥各自的长处,组合成更
为强劲的功能。而Windows下的图形软件基本上是各自为营,互相不能调用,很不利于各
种软件的相互集成。在这里并不是要和Windows做个什么比较,所谓“寸有所长,尺有所
短”,图形化工具还是有不如命令行的地方。(看到这句话时,希望各位千万再也不要认
为我就是“鄙视图形界面”,和我抬杠了 )

我是根据版本为5.1.1的GDB所写的这篇文章,所以可能有些功能已被修改,或是又有更为
强劲的功能。而且,我写得非常仓促,写得比较简略,并且,其中我已经看到有许多错别
字了(我用五笔,所以错字让你看不懂),所以,我在这里对我文中的差错表示万分的歉
意。

文中所罗列的GDB的功能时,我只是罗列了一些带用的GDB的命令和使用方法,其实,我这
里只讲述的功能大约只占GDB所有功能的60%吧,详细的文档,还是请查看GDB的帮助和使
用手册吧,或许,过段时间,如果我有空,我再写一篇GDB的高级使用。

我个人非常喜欢GDB的自动调试的功能,这个功能真的很强大,试想,我在UNIX下写个脚
本,让脚本自动编译我的程序,被自动调试,并把结果报告出来,调试成功,自动check
in源码库。一个命令,编译带着调试带着checkin,多爽啊。只是GDB对自动化调试目前支
持还不是很成熟,只能实现半自动化,真心期望着GDB的自动化调试功能的成熟。

如果各位对GDB或是别的技术问题有兴趣的话,欢迎和我讨论交流。本人目前主要在UNIX
下做产品软件的开发,所以,对UNIX下的软件开发比较熟悉,当然,不单单是技术,对软
件工程实施,软件设计,系统分析,项目管理我也略有心得。欢迎大家找我交流,(QQ是
:753640,MSN是: haoel@hotmail.com)

--
First is insight, next is technique.

※ 来源:·北大未名站 bbs.pku.edu.cn·[FROM: 162.105.172.153]

世界末日

想笑来伪装掉下的眼泪
点点头承认自己会怕黑
我只求能借一点时间来陪
你却连同情都不给
想哭来试探自己麻痹了没
全世界好像只有我疲惫
无所谓反正难过就敷衍走一回
但愿绝望和无奈远走高飞
天灰灰会不会让我忘了你是谁
夜越黑梦违背难追难回味
我的世界将被摧毁也许事与愿违
累不累睡不睡单影无人相依偎
夜越黑梦违背有谁来安慰
我的世界将被摧毁或许颓废也是另一种美

想哭来试探自己麻痹了没
全世界好像只有我疲惫
无所无所谓反正难过就敷衍走一回
但愿绝望和无奈远走高飞
天灰灰会不会让我忘了你是谁
夜越黑梦违背难追难回味
我的世界将被摧毁
也许事与愿违
累不累睡不睡单影无人相依偎

2006/11/11

她走在美的光彩中(Byran)

她走在美的光彩中,
像皎洁的夜晚繁星满天.
明与暗最美妙的,色泽,
在她的容颜和秋波中闪现:
融成一片淡淡的莹辉,
那柔光在白昼无从看见.
多一道暗影,少一缕光芒,
都会损伤这难言的优美;
美波动在她乌黑的秀发上,
又轻柔地映照她的面庞;
愉悦的思想在这里颂扬,
心灵寓所的纯洁、高尚.
啊,在脸颊上,在眉宇间,
如此温和、平静,又脉脉含情;
那迷人的微笑,那容颜的光彩,
都在说明一个善良的生命:
她的灵魂安于世间的一切,
她的心充溢着纯真的爱情!

2006/11/10

Just For Fun: Quicksort in 3 lines(zz)

http://aspn.activestate.com/ASPN/Cookbook/Python/R...

Title: Just For Fun: Quicksort in 3 Lines
Submitter: Nathan Gray (other recipes)
Last Updated: 2001/08/07
Version no: 1.3
Category: Searching

Description:

OK, 4 lines if you count wrapped lines. :^) This is a rather naive implementation of quicksort that illustrates the expressive power of list comprehensions. DO NOT USE THIS IN REAL CODE!
NOTE: Due to an annoyance in the ASPN submission filters you must manually remove the space after the '\' character in the third line if you intend to use the code. Otherwise you will get a syntax error.

def qsort(L):
    if len(L) <= 1: return L
    return qsort( [ lt for lt in L[1:] if lt < L[0] ] )  +  \ 
              [ L[0] ]  +  qsort( [ ge for ge in L[1:] if ge >= L[0] ] )


# IMHO this is almost as nice as the Haskell version from www.haskell.org:
# qsort [] = [] 
# qsort (x:xs) = qsort elts_lt_x ++ [x] ++ qsort elts_greq_x
#                 where 
#                   elts_lt_x = [y | y <- xs, y < x] 
#                   elts_greq_x = [y | y <- xs, y >= x]

# And here's a test function:
def qs_test(length):
    import random
    joe = range(length)
    random.shuffle(joe)
    qsJoe = qsort(joe)
    for i in range(len(qsJoe)):
        assert qsJoe[i] == i, 'qsort is broken!'

Discussion:

I cooked up this function after finding the wonderful Haskell quicksort at http://www.haskell.org/aboutHaskell.html (which I've reproduced above). After marvelling at the elegance of this code for a while I realized that list comprehensions made the same thing possible in Python!
Both implementations pivot on the first element of the list and thus have worst-case performance for the very common case of sorting an already-sorted list. You would never want to do this in production code! Since this is just a recreational excercise, though, it doesn't really matter. :-)
Since list comprehensions were introduced in Python 2.0 this code will not work on any earlier version.

 

Refinements, Raymond Hettinger, 2002/02/28
Here is a slightly less naive version. Pivot selection is random to make worst case performance less likely. Pivots are counted to handle degenerate cases with many equal elements. Filter is used for rapid partitioning.

def qs(ds):
    if len(ds) <= 1: return ds
    pivot = random.choice(ds)
    return qs(filter(lambda x: x < pivot, ds)) +
           [pivot]*ds.count(pivot) +
           qs(filter(lambda x: x > pivot, ds))

Add comment

Just For Fun: Quicksort in 1 Line, Christophe Delord, 2002/08/25
Of course this must not be used in real code too!

q=lambda x:(lambda o=lambda s:[i for i in x if cmp(i,x[0])==s]:len(x)>1 and q(o(-1))+o(0)+q(o(1)) or x)()
This lambda expression is just a rewriting of this function:
def q(x):
	if len(x)>1:
		lt = [i for i in x if cmp(i,x[0]) == -1 ]
		eq = [i for i in x if cmp(i,x[0]) == 0 ]
		gt = [i for i in x if cmp(i,x[0]) == 1 ]
		return q(lt) + q(eq) + q(gt)
	else:
		return x

Add comment

Bug in quicksort long form..., Jeremy Zucker, 2003/01/15

def q(x):
	if len(x)>1:
		lt = [i for i in x if cmp(i,x[0]) == -1 ]
		eq = [i for i in x if cmp(i,x[0]) == 0 ]
		gt = [i for i in x if cmp(i,x[0]) == 1 ]
		return q(lt) + q(eq) + q(gt)
	else:                  ^^^^^   
		return x
Running q(eq) will infinitely recurse. Use eq instead of q(eq)

2006/11/7

love poem


还没享受够故都的秋,寒冷的冬天就在嗖嗖两天风中毫不留情的来了。
那天回家在勺园到办公楼的路上听到前面的小朋友说,在燕园这个园子
里,冬天脱光的特别多,夏天尤其是毕业生分手的特别的多,呵呵。
在公司拼命的赶进度,又是眼瞅着光光节的到来,今天开始每天贴诗吧。

The Way You Make Me Feel

You make me feel special,
You make me feel new,
You make me feel loved,
With everything you do.

You hold me close when I am sad.
You wipe the tears from my face.
Every time we are together,
It seems like the perfect place.

My eyes light up when you enter a room.
I smile when we are together.
No matter how bad things are,
You always make them better.

I love the way you kiss me,
The way you hold me tight.
I love the way you touch me,
I could be with you all night.

I love the way you can make me laugh
For absolutely no reason at all.
I love how no matter what I do,
You will be there to catch me when I fall.

I just want you to know,
That even though we sometimes fight,
I will always love you!
No matter what, day or night.

- Amanda Standridge -

555...

那天去剪头发,给我剪的大哥说,给你个小建议,你头顶的头发稍有些稀了,注意稍微修饰一下。
555, 惨啊,还什么都没那啥呢,怎么能......
不知道是否睡的太少有影响,早睡早睡,嗯嗯,以后要每天提前3个小时睡,提前半小时起.

2006/11/6

Hash Crash Course(zz)

Perl.com
Published on Perl.com http://www.perl.com/pub/a/2006/11/02/all-about-hashes.html
See this if you're having trouble printing code examples

Hash Crash Course
By Simon Cozens
November 02, 2006

When I teach about hashes, I do what most Perl tutors and tutorials do: I introduce the hash as a "dictionary": a mapping between one thing and another. The classic example, for instance, is to have a set of English words mapped to French words:

    %french = (
        apple  => "pomme",
        pear   => "poivre",
        orange => "Leon Brocard"
    );

Yet the more I look at my code--and more often, the more I look at how to tidy up other people's code--I realize that this is perhaps the least common use of a hash. Much more often, I use hashes in particular idioms which have very little in common with this concept of a mapping. It's interesting to consider the ways that programmers actually use hashes in Perl code.

Counting

Many of the uses of hashes are to "answer questions about lists." When you have an array or list of values and you need to ask about its properties, you will often find yourself using a hash. Start simply by counting the number of particular elements in a list. Here's the naïve approach:

    my $count = 0;
    for (@list) {
        $count++ if $_ eq "apple";
    }

You can smarten this up with the use of the grep function:

    $count = grep $_ eq "apple", @list;

... but when you need the number of pears in the list, then you have to do the same again:

    $count_apples = grep $_ eq "apple", @list;
    $count_pears  = grep $_ eq "pear",  @list;

Now there are two passes over the list, and the situation isn't going to get any prettier from here. What you want is basically a histogram of the data, and you can get that with a hash:

    my %histogram;
    $histogram{$_}++ for @list;

This hash associates each individual item with its count, and it only traverses the list once.

In a recent case, I was looking at a list of tags associated with various photographs. To lay out the data for display, it was useful to know how many different tags there are in the list. I could get that simply from the number of keys in the histogram:

    $unique = keys %histogram;

I could also delete the duplicates and end up with a list of the unique tags:

    @unique = keys %histogram;

Finally, I could show the five most popular tags from the list:

    @popular = 
        (sort { $histogram{$b} <=> $histogram{$b} } @unique)[0..4];

This sorts the list of unique tags based on their popularity in the original list, and pulls out the top five.

Uniqueness

Another idiom to get the unique elements from a list is a variation on the "counting things" idiom; but this time you don't care how many of each item there is, just that there's at least one. This allows you to say:

    for (@list) { $unique{$_} = 1 }
    @unique = keys %unique;

which reduces to:

    %unique = map { $_ => 1 } @list;
    @unique = keys %unique;

You can take this a stage further with a slightly non-standard idiom to do the whole thing in one operation:

    @unique = keys %{{ map { $_ => 1 } @list }};

keys requires a hash, so this funny-looking line creates an anonymous hash ({ map ... @list }) and then turns it into a real hash (%{ $hash_ref }) to feed it to keys.

The advantage of this trick is that you can use a variation of it to work with lists which include objects as well as ordinary scalars. Here's an example of the problem:

    my @tags;
    push @tags, Memories::Tag->retrieve_random for 1..10;

This should get ten random Memories::Tag objects. However, some of them might be the same, and, assuming that you want to see the unique ones:

    %unique = map { $_ => 1 } @tags;
    @unique = keys %unique;

Unfortunately, if you then try to do anything with the tags, it all goes horribly wrong:

    print $unique[0]->name;

    # Can't locate object method "test" via package "Memories::Tag(0x1801380)"

What's happened is that hash keys can only be strings; once you put the object into a hash as the key, then it's not an object any more. Perl turns it into a string. There's no (easy) way to get the strings back into an object. What you need is some kind of data structure to map these broken strings into the original objects. Thankfully, there is one in the form of the hash itself! So the code becomes:

    %unique = map { $_ => $_ } @tags;
    @unique = values %unique;

First, map the object--which Perl will smash into a string--with another copy of the object, which will retain its object-ness. Then instead of getting the strings out of the hash, which are useless to us, you get the objects.

Of course, this doesn't quite work, because even if retrieve_random retrieves the same tag name twice, it will return two different objects with the same data. (That's true unless the underlying database abstraction layer uses caching, which is another good use for a hash.)

The solution is to make the list unique based on a property of the tag, such as its name. This time, the code is:

    %unique = map { $_->name => $_ } @tags;
    @unique = values %unique;

Okay, now you have a list of objects which you've retrieved randomly, but which have unique names. The only problem is that you retrieved ten of them to start with, and then whittled the list down to get rid of duplicates. What if you actually wanted 10?

The solution is to keep track of ones that you've already seen, so that you don't create any duplicates in the first place. The question "Have I seen this before?" is easy to answer using another hash-based idiom:

    my @tags;
    my %seen;
    while (@tags < 10) {
        my $candidate = Memories::Tag->retrieve_random;
        next if $seen{$candidate->name}++;
        push @tags, $candidate;
    }

Once again you're collecting unique values by putting them into a hash. Suppose that the first candidate tag that comes along is japan. At this point the hash is empty, and so $seen{japan} is zero. Because it's zero, the code continues through the loop with next, but still increments the hash value. Now $seen{japan} stands at 1, and the tag goes into the list. If japan comes past again, $seen{japan} will be positive so the next code will activate, and the tag will not go into the list again. (Don't try this if you have fewer than 10 distinct tags, of course!)

 

Caching

A special case of "have I seen this before?" comes when you want to create a cache: if I have seen this before, what was the answer I saw last time? Here's how to do that.

I mentioned that the underlying database abstraction layer in the tags example might cache look-ups and return the same object if you requested the same tag multiple times:

    my %cache;

    sub retrieve {
        my ($self, $id) = @_;
        return $cache{$id} if exists $cache{$id};
        return $cache{$id} = $self->_hard_retrieve($id);
    }

This checks to see if it has seen the $id before; if so have, return the value from the hash. If not, work out what the value should be, then store it in the hash for next time, then return the whole lot.

Of course, for heavy-duty applications like a database abstraction layer, you need to do a little more work, such as pruning the cache to make sure it doesn't get out of date or get so full of data that it eats all your memory. The Cache::Cache suite of modules from CPAN takes care of all of this for you, and the core Memoize module adds this kind of caching to a function without you specifically having to write the cache-getting and -setting code.

Searching

Finally, you can use hashes for searching.

If you've done any computer science, you'll probably know about a couple of common searching algorithms. (Even if you haven't, they're so common that you've probably reinvented them without knowing it.) One is linear search, where you start at the beginning of a list and work your way toward the end, looking for the target item:

    my $index;
    for $index (0..@chambers) {
        last if $chambers[$index] == $bullet;
    }
    print "Found at index $index" if $index < @chambers;

Another is binary search, where you start in the middle of a sorted list, and work out whether you should go higher or lower than the current element, like players in some demented version of Card Sharks. This is basically the way to look up names in a phone book--it's faster, but a bit more complicated to implement:

    my @names = qw(Able Baker Charlie Dog ...);
    my $index;

    my $target = "Roger";

    sub search {
        my ($lower, $upper) = (0, $#names);
        while ($lower <= $upper) {
            my $index = ($lower + $upper) / 2;
            if ($names[$index] lt $target) {
                $lower = $index + 1;
            } elsif ($names[$index] gt $target) {
                $upper = $index--1;
            } else { return $index }
        }
        # Not found!
    }

This starts in the middle, at "Mike". "Go higher!" shouts the crowd, so it goes half-way between there and the end, to "Tare". Then it needs to go lower, so looks half-way between "Mike" and "Tare" and gets to "Peter". Now it needs to go higher again, between "Peter" and "Tare", and finds "Roger".

That takes four comparisons, which is not bad. Can you do it any better? How about... oh, none?

    my %search = map { $names[$_] => $_ } 0..$#names;
    print $names{"Roger"};

Now I'm cheating somewhat here because setting up the hash requires iterating over the whole array, but once you've done that, the searches are basically free.

Of course, if you need to look up an array index by its contents, then maybe you're doing something wrong in the first place, and should have used a hash to start with. Consider, for instance, in a configuration file:

    force-v3-sigs
    escape-from-lines
    lock-once
    load-extension rndlinux
    keyserver wwwkeys.eu.pgp.net
    keyserver the.earth.li

(Yes, that's from GnuPG.) Each line has a key and optionally a value, but you can have multiple values for each key. At the end of the day, you might want to look through and say "tell me all the keyservers"--but not just the keyservers, you want to read the whole config in and be able to say that about any key. That's a search problem, and a tricky one. Here's one solution:

    while (<>) {
        chomp;
        my ($key, $value) = split /\s+/, $_, 2;
        push @config, [$key, $value];
    }

Now you have to say:

    @keyservers = map { $_->[1] } grep { $_->[0] eq "keyserver" } @config;

This is actually a linear search in disguise. (grep does a linear search for you.)

With hashes, the issue is much simpler:

    while (<>) {
        chomp;
        my ($key, $value) = split /\s+/, $_, 2;
        push @{$config{$key}}, $value;
    }

This treats every key as a separate array reference, and pushes values into that array. Now to retrieve the list of keyservers, just look up the array inside the hash:

    @keyservers = @{$config{"keyservers"}};

This process is very remniscient of the final pattern--using a hash as a portable symbol table.

Dispatch Tables

Instead of having the configuration reader create a bunch of arrays--@keyservers, @load_extension and so on--I created a hash which held the arrays so as to look them up indirectly but more efficiently. In effect, instead of using the Perl symbol table, you can use a hash as a portable symbol table.

Suppose you have a script that does several related things: it manages your to-do list by adding, editing, listing, and deleting to-do items:

    % todo add "Email Samuel about photos"
    Todo item 129 created
    % todo done 129
    Item 129 marked as done

You might expect the script to look like:

    my $command = shift @ARGV;
    if    ($command eq "add")  { add(@ARGV)  }
    elsif ($command eq "list") { list(@ARGV) }
    elsif ($command eq "done") { done(@ARGV) }
    elsif ($command eq "edit") { edit(@ARGV) }
    ...
    else { die "Unknown command: $command" }

That is quite tedious; you need to edit the program in several places every time you add a new command. You could use symbolic references--that is, tell Perl to call a function named $command:

    my $command = shift @ARGV;
    sub AUTOLOAD { die "Unknown command: $AUTOLOAD" }
    no strict 'refs';
    &{$command}(@ARGV);

But that's somewhat crazy. It allows the user to get at any subroutine in the main package, which you may not want, and to keep any error checking you have to assume that any undefined subroutine call comes from the command line.

The middle way is to copy the commands into a hash, mapped to a function reference:

    %commands = (
        add  => \&add,
        list => \&list,
        edit => \&edit,
        done => \&done,
    );
    my $command = shift @ARGV;
    if (!exists $commands{$command}) { die "Unknown command: $command" }
    $commands{$command}->(@ARGV);

This keeps strict happy, it's safe in the way it restricts what subroutines users can call, and it allows for error checking that doesn't mess everything else up. Mark Jason Dominus' Higher Order Perl shows how you can define commands at runtime if you use dispatch tables, something you can't do if you hard-code your dispatch.

Conclusion

I've explored some of the most common hash-based patterns: using hashes for counting, uniqueness, searching, and dispatch--rather a lot more than just mapping from one thing to another. Of course, that is what a hash does at one level, but the uses of such a data structure are a lot more diverse than just that.

That's how you improve your Perl programming--you take elements of the language which ostensibly do one thing, and you find that they're great for more complicated uses as well. Maybe after these ideas you'll be able to find a few more hash idioms of your own!

Perl.com Compilation Copyright © 1998-2006 O'Reilly Media, Inc.

2006/11/4

Nine Things Developers Want More Than Money(zz)

Nine Things Developers Want More Than Money

Many of the developers I know have been programming since they were in junior high. Whether it was building text-based games on an Apple IIe or creating a high school football roster app in Visual Basic, it's something they did for the challenge, for the love of learning new things and, oh yes, for the chicks. Ladies love a man who can speak BASIC to his Apple.

Code

College graduates face a sad reality when they leave the protective womb of a university and have to get their first real job. Many of my friends found jobs paying around $25k out of school, and were amazed that the starting engineering and computer science salaries were nearly double that. But the majority of the engineers in my class didn't become engineers for the money; we did it because it touched on a deep inner yearning to tinker and impress their friends. And did I mention the chicks?

Money is a motivating factor for most of us, but assuming comparable pay, what is it that makes some companies attract and retain developers while others churn through them like toilet paper?
Hygiene and Motivation
In the 1950s a researcher named Frederick Herzberg studied 200 engineers and accountants in the US. He asked them a few simple questions and came up with what is one of the most widely-accepted theories on job satisfaction called Two Factor Theory.

His theory breaks job satisfaction into two factors:

  • hygiene factors such as working conditions, quality of supervision, salary, safety, and company policies
  • motivation factors such as achievement, recognition, responsibility, the work itself, personal growth, and advancement

Hygiene factors are necessary to ensure employees don't become dissatisfied, but they don't contribute to higher levels of motivation. Motivation factors are what create motivation and job satisfaction by fulfilling a person's need for meaning and personal growth.

Think of a large financial company like Countrywide or IndyMac. Although I've never worked for either, the stories I've heard indicate the hygiene factors are well taken care of: working conditions are good, supervision is reasonable, salaries are decent, they have good benefits, etc...

However, the motivation factors are, shall we say, incognito. As Herzberg noticed, this scenario leads to employees viewing the job as little more than a paycheck, which is probably all right for companies like Countrywide and IndyMac.

Football Take the flip side: a tiny startup in a dingy office with no windows, crappy benefits, little supervision (because the CEO's on the road making sales), and no company policies (because the CEO's on the road making sales). But the constant rush of learning, being responsible for the company's success or failure (almost single-handedly at times), and believing in the company's future growth makes this job much more desirable for many developers.
One of my early programming jobs was for a web consulting startup during the dot-com boom. There were 7 of us (we grew to 17 during the height of the boom) shooting each other with water pistols, throwing Nerf footballs around the office, and cranking out insane amounts of caffeine-driven code. We learned a new language every project and were always on the cutting edge.
I remember thinking that a company across town could have offered me a $15,000 dollar raise and I wouldn't have taken it. The motivation factors were overpowering.
On the flip side, the benefits were terrible, the office was a series of tiny cubicles, gray from years of neglect - Smurf-blue network cables hung from the ceiling, and supervision was...well...non-existent. And although hygiene factors were lacking, developers flocked to work for this company and only one left while I was there. She was interested in a more stable work environment and better benefits, and went to work for a large financial institution much like IndyMac.

Rob's Criteria for Keeping Your Developers Happy
If you want to collect a paycheck for 25 years and retire with a gold watch and a pension then go for companies that have the hygiene factors nailed. Stroll in at 8, head for the door at 4:59, and count the years until you're kicking up your feet on a beach bar in Costa Rica.

Big Building But if you're reading this, odds are that you aren't the kind of person who never thinks about code after 5:01; you're more likely to have a collection of DVDs that come up in an Amazon search for "Silicon Valley." You're probably one of those people who needs motivation factors or you go crazy with restlessness, and when the motivation factors are in place you'll work ridiculous hours for low pay just because it's so damn fun.
I talked to a dozen colleagues and pored over my own experiences to arrive at this list of nine software development motivation factors - Rob's Criteria for Keeping Your Developers Happy.
There's only one rule when determining your score: your vote doesn't count unless you're a developer. If you're not in the trenches writing code then forward this article to someone who does and ask for their opinion. In addition to keeping management from making an unfair assessment, my greater hope is that this inspires conversation and forces management and developers to talk about these issues so we can get them out in the open.
Without further ado, here they are:
1. Being Set Up to Succeed
It's a sad reality, but most software projects are set up to fail. Every developer has their horror stories; the "anti-patterns" of software project management.

I've seen an architect given documentation for a legacy system that he pored over for week while designing a new interface for the product. After the design was complete he found out that the documentation was three years old and didn't reflect several major changes the system.

I've spent hours preparing a detailed technical estimate only to be told that the real deadline, already set by product development, gives me half the time I need.
Realistic deadlines are a huge part of being set up to succeed. Developers want to build software that not only works, but is maintainable; something they can take pride in. This is not in-line with product development's goals, which are for developers to build software that works, and nothing more.
The first thing to go when time is tight is quality and maintainability. Being forced to build crap is one of the worst things you can do to a craftsman. Delivering a project on-time but knowing it's a piece of crap feels a heck of a lot like failure to someone who takes pride in what they build.
Wheat
It's critical to have buy-in to do things the right way, and not just the quick way. As one developer I talked to put it "Quality is as important as feature count and budget."
Schedule is not the only way a project can be set up to fail, but it is the most common. Others include: being forced to use cheap tools (be it software or hardware), working with a partner who doesn't deliver, bad project management (see #2, below), changing scope, and unspoken expectations, among others.
2. Having Excellent Management
Excellent management, both for projects and people, is a must-have motivation factor. This means no micro-managing, the encouragement of independent thinking, knowing what it takes to build quality software, quick decision making, and a willingness to take a bullet for the team when product development tries to shorten the schedule
These are the traits of an amazing software manager; the traits of a manager whose team would bathe in boiling oil to defend her, and work all-nighters to prove her right. When a manager takes bullets for the team, good developers tend to return the favor and then some. It creates an almost cult-ish loyalty, and the results are not only motivated developers, but insanely good software.

3. Learning New Things
Behavioral research indicates we're happiest when we're learning new skills or challenging old ones. A recent article cites a study by two University of Columbia researchers suggesting that workers would be happy to forgo as much as a 20% raise if it meant a job with more variety or one that required more skill. This research suggests that we are willing to be paid less for work that's interesting, fun, and teaches us new skills.

This is why companies using Ruby can find experienced programmers willing to work for less than their typical salaries. The learning factor is huge when it comes to negotiating compensation.

Every developer I know loves playing with flashy new technologies. It was Perl and HTML in the mid-90s, ASP, PHP and Java in the late-90s, ASP.NET and XML a few years ago, and today it's AJAX and Ruby (and in some circles ASP.NET 2.0). Give someone a chance to use these toys and they'll not only be able to impress their friends, but fulfill that piece inside of them that needs to learn.

Keep a developer learning and they'll be happy working in a windowless basement eating stale food pushed through a slot in the door. And they'll never ask for a raise.

4. Exercising Creativity and Solving the Right Kind of Problems
Developers love a challenge. Without them we get bored, our minds wander, we balance our checkbook, check our email, hit Digg and Slashdot, read a few blogs, hit the water cooler, and see if any of our friends are online so we can once and for all settle the debate surrounding your uncle, the IDisposable interface, and that piece of toast shaped like the Virgin Mary.

Droplets

I've watched developers on multiple occasions stay up until sunrise to solve a technical problem without being asked and without extra pay. The best developers are addicted to problem solving. Just drop a Sudoku in the middle of a group and watch them attack it.

Faced with the right type of challenge many developers will not stop until it's fixed, especially if it requires a particularly creative solution. Faced with the wrong type of challenge and they're back on instant messenger describing the toast.

The right type of challenge is a technical challenge that teaches a new skill, preferably one everyone's talking about. One example could be: "Consume these five RSS feeds, aggregate the data, and display the headlines on a web page...and figure out how to use AJAX to make it cool."

The wrong types of challenges are things like: "Fix that other guy's code. You know, the one we didn't fire because we were afraid he might cause problems. Well, he wrote a really crappy system and now we need to fix it and make it high-quality and maintainable. Oh, and you have until tomorrow."

If your business doesn't provide challenging work to developers, figure out how you can start. If there is no chance you'll ever be able to provide challenging work, find developers who are into hygiene factors, because developers who need motivation factors won't stay long.
5. Having a Voice
Developers are in the trenches, and they're the first ones to know when a system or process is not working. One developer I spoke with told me:
"[I want] someone to listen to my problems and actually take them seriously. I've worked at a few places where more RAM, more hard disk space, or faster/dual CPUs were simply not a priority for the company, but it was incredibly aggravating to the point of impeding my work. At one place I worked, every time I wanted to compile the software I had to clear all my temporary files because I needed more disk space. Talk about asinine. Being forced to work using outdated technology is really frustrating."
Speaker

When a developer speaks, someone should listen. When several developers are saying the same thing, someone should listen and act...quickly.

6. Being Recognized for Hard Work
As engineers we love building things that impress ourselves and our friends. At least the ones who realize how hard it is to write a Perl compiler. From scratch. In FORTRAN. On a Vic 20.

Building something great is fun, but it's much more fun when someone's there to pat you on the back, throw you a party, sing your praises, or buy you a steak dinner. Most developers enjoy hearing praise and receiving recognition for hard work, but even the ones who don't need it are somehow soured when they don't receive it (or worse yet, someone else receives recognition for your work).

Recognition is one of Herzberg's core motivation factors and it applies to software developers as much as the engineers originally interviewed.

7. Building Something that Matters
Even though we're not medics in Bosnia or food carriers in Sudan, most people want to feel like we're somehow doing our part to make the world a better place, both technologically and socially. Some of us might think we do it just for the sake of technology, but in the back of our minds we see ourselves as part of a grand scheme.

For instance, a friend of mine works for a financial company and cherishes every time they launch a product that helps the under-served financial community.

An Albertsons inventory system developer enjoys coming to work every day because his work ensures, via complex supply and demand algorithms, that the baby cereals are always available on the shelves.

Building something that matters makes an L.A. Times software engineer ecstatic that the trucks are now saving over 30% of their mileage and fuel costs due to his shortest path finding software implementation for newspaper delivery.

On the other hand, writing an interface to a buggy API that'll be used a total of 15 times in the next year doesn't seem like it matters much.

Copying and pasting an entire application and changing a bunch of labels isn't as exciting as it might sound.

And hacking in a few more case statements in a ridiculously complex stored procedure in order to service yet another customer without creating a proper data structure somehow doesn't seem to fulfill that part of us that wants to build something that matters.

Library

8. Building Software without an Act of Congress
I was a contractor for three years starting in 2001, and during that time I built a ton of web applications. Since much of my development was off-site I became accustomed to writing software really quickly once we knew what to build. Another developer and I built insane amounts of software over the course of two years.

When I got my next full-time job it felt like I was dragging 50-pound weights. For every page I wanted to build I had to call a meeting with six people. Any change to the database required three approvals. It was nuts, and applications took 5x longer to build. Talk about frustrating.

The authority to make project decisions without calling a meeting is huge.

9. Having Few Legacy Constraints
No one likes developing against buggy interfaces, crappy code, and poorly-designed data models. Too many legacy constraints kill creativity, require an act of congress to modify, and generally sucks the fun out of building software (see several of the previous points for why this is bad).

If you have gobs of legacy liability, try to figure out a way to minimize its impact on future development. If you can't, look for people who value hygiene factors, because motivation factor developers are not going to maintain the same poor-quality applications for very long.

Determining Your Score
Let's face it, the bar has been set pretty low when it comes to motivating developers. How many companies can you think of that would score even as high as a 3?

Since this test hasn't been administered to companies across the globe there's no basis for comparison, but that's where you come in. I'd like to do an informal survey so we can get an idea of how things are in the real world. Please post your company's score in the comments (you don't have to post the company name).

Most large companies I can think of would be lucky to score a 1. Google would probably score an 8 or a 9.

Wrap Up
If you're a manager, when was the last time you asked your developers about these issues? If you're a developer, when was the last time you respectfully raised one of these issues, providing examples and a possible solution?

If the answer is "a long time ago" then you have some work to do. Send this article to a few of your colleagues and start discussing how to enact change.