歡迎您光臨本站 註冊首頁

mplayer的GetTimer函數有bug嗎?

←手機掃碼閱讀     火星人 @ 2014-03-12 , reply:0
  作者:Linuxeden管理團隊成員c-aries
用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"

gdb mplayer bug


[火星人 ] mplayer的GetTimer函數有bug嗎?已經有374次圍觀

http://coctec.com/docs/program/show-post-71586.html