用gdb读mplayer代码的过程中,发现一行诡异的代码。
程序作者是否喝高了?
--- 准备篇
$ cat url http://www.mplayerhq.hu/MPlayer/releases/MPlayer-1.0rc1.tar.bz2 http://www.mplayerhq.hu/MPlayer/releases/MPlayer-1.0rc2.tar.bz2 http://www.mplayerhq.hu/MPlayer/releases/codecs/essential-20071007.tar.bz2 http://www.mplayerhq.hu/MPlayer/releases/codecs/all-20071007.tar.bz2 http://www.mplayerhq.hu/MPlayer/releases/fonts/font-arial-iso-8859-7.tar.bz2 http://www.mplayerhq.hu/MPlayer/skins/Blue-1.7.tar.bz2 http://www.mplayerhq.hu/MPlayer/patches/asmrules_fix_20061231.diff http://www.mplayerhq.hu/MPlayer/patches/cddb_fix_20070605.diff http://www.linuxfromscratch.org/patches/blfs/6.3/MPlayer-1.0rc1-ext_ffmpeg-1.patch $ wget -c -i url #下载url文件列表中的文件 $ tar xvf MPlayer-1.0rc2.tar.bz2 $ cd MPlayer-1.0rc2 $ grep -n -R -i "cflags" * | grep -i "O[0-9]" configure:2326: CFLAGS="-W -Wall -O2 $_march $_mcpu $_pipe $_debug $_profile" configure:2330: CFLAGS="-O2 $_march $_mcpu $_pipe -fomit-frame-pointer -wd167 -wd556 -wd144" configure:2332: CFLAGS="-O2 $_march $_mcpu $_pipe" configure:2334: CFLAGS="-Wall -Wno-switch -Wpointer-arith -Wredundant-decls -O4 $_march $_mcpu $_pipe -ffast-math -fomit-frame-pointer" drivers/Makefile:5:CFLAGS = -O2 -D__KERNEL__ -DMODULE -Wall -I$(KERNEL_INC) \ vidix/kernelhelper/Makefile:2:CFLAGS = -O2 -D__KERNEL__ -DMODULE -I$(KERNEL_INC) \ $ |
mplayer编译时进行了O2和O4的优化,使用gdb调试时无法打印出某些变量的值(因为代码被优化了)
所以使用gdb调试前,最好将源代码的编译优化选项去掉:)
按照提示,手工将以上grep命令输出中的-O2和-O4去掉,如:
configure:2326: CFLAGS="-W -Wall -O2 $_march $_mcpu $_pipe $_debug $_profile"
打开当前目录下的configure文件,到第2326行,改为
CFLAGS="-W -Wall $_march $_mcpu $_pipe $_debug $_profile"
配置并编译
$ ./configure --prefix=/usr --enable-debug --codecsdir=/usr/lib/codecs/ --enable-fbdev --disable-dvdnav --disable-dvdread --disable-dvdread-internal --enable-mencoder --confdir=/etc/mplayer $ make |
注意加了 --enable-debug 选项
make期间会发现几次报错,程序自动中断编译,如下:
make[1]: *** [i386/dsputil_mmx.o] Error 1
make[1]: Leaving directory `/home/c-aries/source/mplayer/MPlayer-1.0rc2/libavcodec'
make: *** [libavcodec/libavcodec.a] 错误 2
估计是因为去掉了O2这些编译优化后出错,解决方法:
根据make的输出往回找,找到编译 dsputil_mmx.c 文件的命令
cc -I../libswscale -I../libavcodec -DHAVE_AV_CONFIG_H -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_ISOC9X_SOURCE -I.. -I.. -I../libavutil -Wdisabled-optimization -Wno-pointer-sign -Wdeclaration-after-statement -I. -I.. -I../libavutil -W -Wall -march=native -mtune=native -pipe -g -D_REENTRANT -DHAVE_CONFIG_H -I/usr/include/directfb -I/usr/include/ -I/usr/include/SDL -D_REENTRANT -I/usr/include/kde/artsc -pthread -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/freetype2 -I/usr/include -c -o i386/dsputil_mmx.o i386/dsputil_mmx.c
加上在configure文件中去掉的 -O2 选项,到 -Wall 后面
$ cd libavcodec/ $ cc -I../libswscale -I../libavcodec -DHAVE_AV_CONFIG_H -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_ISOC9X_SOURCE -I.. -I.. -I../libavutil -Wdisabled-optimization -Wno-pointer-sign -Wdeclaration-after-statement -I. -I.. -I../libavutil -W -Wall -O2 -march=native -mtune=native -pipe -g -D_REENTRANT -DHAVE_CONFIG_H -I/usr/include/directfb -I/usr/include/ -I/usr/include/SDL -D_REENTRANT -I/usr/include/kde/artsc -pthread -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/freetype2 -I/usr/include -c -o i386/dsputil_mmx.o i386/dsputil_mmx.c $ cd .. $ make |
接着继续编译
$ pwd /home/c-aries/source/mplayer/MPlayer-1.0rc2 $ ls mplayer mplayer $ |
最后生成mplayer在当前目录就成功了
--- 调试篇
$ gdb ./mplayer (gdb) start -vo fbdev ~/video/拥抱春天.flv (gdb) b GetTimer Breakpoint 2 at 0x86bd320: file timer-lx.c, line 35. (gdb) c Continuing. Breakpoint 2, GetTimer () at timer-lx.c:35 35 gettimeofday(&tv,NULL); (gdb) bt #0 GetTimer () at timer-lx.c:35 #1 0x086bd398 in GetRelativeTime () at timer-lx.c:54 #2 0x086bd3d7 in InitTimer () at timer-lx.c:63 #3 0x080a3523 in main (argc=4, argv=0xbff03334) at mplayer.c:2298 (gdb) l 35 30 31 // Returns current time in microseconds 32 unsigned int GetTimer(void){ 33 struct timeval tv; 34 // float s; 35 gettimeofday(&tv,NULL); 36 // s=tv.tv_usec;s*=0.000001;s+=tv.tv_sec; 37 return (tv.tv_sec*1000000+tv.tv_usec); 38 } 39 (gdb) n 37 return (tv.tv_sec*1000000+tv.tv_usec); (gdb) p tv.tv_sec $1 = 1271052273 (gdb) p tv.tv_sec * 1000000 $2 = -348578240 #想想都知道溢出得很严重... (gdb) n 38 } (gdb) GetRelativeTime () at timer-lx.c:56 56 r=t-RelativeTime; (gdb) l 51 // Returns time spent between now and last call in seconds 52 float GetRelativeTime(void){ 53 unsigned int t,r; 54 t=GetTimer(); 55 // t*=16;printf("time=%ud\n",t); 56 r=t-RelativeTime; 57 RelativeTime=t; 58 return (float)r * 0.000001F; 59 } 60 (gdb) p t $3 = 3947348404 (gdb) f #下一步,将运行第56行的代码,其中RelativeTime为上一次调用GetTimer的时间记录,t为此次调用GetTimer的时间记录 #0 GetRelativeTime () at timer-lx.c:56 56 r=t-RelativeTime; (gdb) |
问题: GetTimer函数有bug吗?
t和RelativeTime都是无符号32位整型
当此次调用GetTimer时,第37行的 tv.tv_sec*1000000+tv.tv_usec 发生溢出并赋值给变量t
而上一次的时间记录RelativeTime未发生溢出,导致t的值小于RelativeTime
则 r=t-RelativeTime 计算结果能真实反映两次时间记录的间隔吗?
--- 证明篇
(gdb) printf "%u\n", -1 4294967295 #取无符号数最大数值 (gdb) printf "%u\n", 4294 * 1000000 + 967295 # (1) 当前系统时间为4294.967295秒 4294967295 (gdb) printf "%u\n", 4295 * 1000000 + 967295 # (2) 当前系统时间为4295.967295秒 999999 (gdb) printf "%u\n", 999999 - 4294967295 1000000 # 计算结果为: (2)和(1)相差1秒 (gdb) printf "%u\n", 4294 / 60 71 # 4294秒约为71分钟 (gdb) printf "%u\n", (4294 * 2 + 1) * 1000000 + 967295 # (3) 当前系统时间为8589.967295秒 32703 # 约72分钟后 (gdb) printf "%u\n", 32703 - 4294967295 32704 # 计算结果为: (3)和(1)相差0.032704秒,而事实上已经过了约72分钟 (gdb) |
结论:
mplayer GetTimer()函数出错的周期为约72分钟,远远满足视频帧的播放间隔
所以按实际使用情况来说,该函数没有bug
Q.E.D.
--- 后记*体会
使用gdb读代码可以轻松了解代码的大概框架和流程,比静态阅读代码愉悦多了
难怪 Stallman 大叔演讲,教我们学编程时,说,"不要害怕使用debugger"
时间:2010-04-12 15:38
来源:Linuxeden
作者:c-aries
原文链接