Search Google

Monday, October 29, 2007

Something about mtrace()

quote from http://ace.acadiau.ca/math/ACMMaC/howtos/mtrace.html

Finding Memory Leaks Using mtrace

When testing code on small samples, small problems such as memory leaks are often undetectable. However, once the program is run on a larger scale, existing memory leaks become larger problems; they often will cause a program to crash or to swap extensively (slowing performance drastically), and also affect any other programs running on the same system by reducing available memory.

To help avoid, or more often fix, such memory leaks, there are many tools available. One within the GNU platform is called mtrace.

Steps to Profiling a Program

1. Add a call to mtrace() at the very start of your main function, and include mcheck.h in the file containing the main program.
2. Compile the program with debug options set (i.e. the '-g' switch)
3. export a variable called MALLOC_TRACE which contains a filename where the trace data should be stored. Example: export MALLOC_TRACE=mtrace.out
4. Run the program, once
5. Run 'mtrace my_program mtrace.out' to view the output

1. Add the mtrace() call

mtrace works by adding hooks into calls which allocate and deallocate memory, logging these calls, and reporting on any mis-matches between the recorded allocations and deallocations. In order to begin recording information, these hooks have to be turned on. Calling the mtrace() function does this. In order to work properly, you must do this at THE VERY FIRST ACTION of the program. As well, since mtrace() is declared in mcheck.h, you'll have to include that as well.

For example, let's look at a simple program: #include <stdio.h> #include <stdlib.h> #include <mcheck.h> int main() { char *string; mtrace(); string = malloc(100 * sizeof(char)); return 0; }

Notice declarations are before the call to mtrace(), but any executed code comes after. The best place to call mtrace() is the very first executing line of code in main().

2. Compile program with debugging options

Although not strictly necessary, in order to get best use of mtrace, you should compile with debugging options set. This consists of adding '-g' to your compiling and linking commands.

So, if you were to compile a program composed of a single file, let's say hello.c, you would compile it with a command line resembling: gcc -g -o mtrace_test mtrace_test.c

However, if you were to compile a program consisting of multiple files, or where you have to do separate compilation and linking, you would have to supply -g on all compilation and linking commands. For example, given the files mtrace_test.c and file2.c, the commands would resemble the following: gcc -g -c mtrace_test.c gcc -g -c file2.c gcc -g -o mtrace_test mtrace_test.o file2.o

Often in larger software, you would modify the flags variables (CFLAGS, CXXFLAGS, FFLAGS, LD_OPTS and/or LDFLAGS) in your Makefile to add these, or you would set and export these as environment variables before running configure. An example of this last approach: export CFLAGS='-g' export LDFLAGS='-g' ./configure make

3. export MALLOC_TRACE

mtrace() saves its data to whatever file is named in the environment variable MALLOC_TRACE. So, in order to have the data saved, you must set this before running your program.

For bash, you could set the variable to 'mtrace.out' by running the following: export MALLOC_TRACE="mtrace.out"

For C shell, it would be: setenv MALLOC_TRACE mtrace.out

4. Run The Program, Once

Now you just need to run your program as usual. While it runs, it will save all the necessary data to the file specified in the MALLOC_TRACE environment variable.

I recommend running the program in such a way as to cover as much of the program's functionality as possible. If there are any problems, you want to try to make sure you trigger them.

5. View The Data

You should now have a file containing the trace data. In the case of the examples above, it would be mtrace.out. To view the data in a readable format, run the mtrace command, giving the program name and trace file name as arguments. Using the above examples, with our program name being mtrace_test and trace file name being mtrace.out, our command would be: mtrace mtrace_test mtrace.out

Assuming the C code at the beginning was the code in mtrace_test.c, the following output would be produced: Memory not freed: ----------------- Address Size Caller 0x0000000000501460 0x64 at /array/home/dcurrie/test/mtrace/mtrace_test.c:11

The address column is mostly meaningless. Feel free to ignore that - it can vary from run to run. More importantly, the entries in the "Memory not freed" report contains the file name and line number the allocation call has been traced to, and also contains the number of bytes allocated (0x64 is hexadecimal for 100)

So, the information tells us that data allocated on line 11 of mtrace_test.c is never deallocated, and so we should take corrective action to deallocate that particular memory.

Wrap-Up

Although in very small programs like what is used in the example, a quick visual inspection works well, when you expand to large programs, using tools like mtrace to diagnose for you where memory is allocated but not deallocated is a wonderful tool to save both your time and sanity. It also allows you to know quantitatively that no memory leaks occur in your code.

Even more importantly, when you plan to package up libraries or sets of functions to be used by other people, you will want to make certain no leaks exist, and such tools are a vital part of checking the quality of the code you release to other people.

Disclaimer

Acadia University and the author claim no responsibility for anything resulting from the use or misuse of information presented here. Read and use at your own risk.

Sunday, October 28, 2007

取得電腦中已經安裝的套件清單

ls /var/lib/dpkg/info/ | cut -f1 -d. | uniq

以上是適用於ubuntu的路徑。

Saturday, October 27, 2007

open .svg files in Linux

The quote from http://www.mepislovers.org/forums/showthread.php?t=9972:

"Gimp can open SVG if gimp-svg is installed."

That's all

我老了嗎?

這兩天在網咖收完信之後完全沒有玩game的慾望,最多只是看著其他人玩著SF Online回想當年跟一群朋友玩CS,SC的情景。但願是因為當場沒有其他有人在的關係。。。
BTW,今天是我的生日,休假就當作是國防部給我的生日禮物吧 :-D

轉行做hacker

為了認識hacking的一些常識(*),我特別趁本週休假到書局逛逛,很幸運的發現一本不錯的書,書名是駭客攻防實戰詳解,不同於一般市面的書只有介紹如何使用現成的駭客軟體(太low了!),這本書還有一些理論面的說明,雖然沒有很深入但是對於才要入門的我卻是不錯的工具書。
有趣的是,一開始看的時候並沒有發現什麼異狀,直到看到幾張screenshots之後才赫然發現這本書的作者是內地同胞,由電子工業出版社發行,松崗不過是把他引進並加以繁體化而已。
主要在探討Windows下的cases,兩位作者果然非常"愛國"地避談廣為大陸機關使用的Linux系統,我想手法應該大同小異,不過漏洞就要自己額外花時間去找了。此外,書中所談到的許多工具軟體本身都將大陸的網域排除在外,愛國心由此可見一般!

* 包含從資訊蒐集到種木馬、利用漏洞、隱藏蹤跡、清理善後、防火牆技術等入門資訊

Sunday, October 21, 2007

Overcome javascript:void(0);

以前看到javascript:void(0);的連結只知道是死路一條而無法將影音檔下載存到電腦中。今天則發現其實還是有辦法破解這個javascript成功下載影音檔案!
首先參考這篇安裝MediaPlayerConnectivity plugin(我將xine設定為external player),之後點擊播放影音檔的連結,這時候MediaPlayerConnectivity plugin會開啟xine播放檔案,然後將xine的playlist打開,在play list的最下面就可以看到該檔案的實際位址。
接下來可以使用任何下載軟體將檔案抓下來存在電腦中 :-)
當然,如果遇到streaming type的檔案,還是需要搭配mmsclient使用!

xine playlist中的資訊

New found utilities

mimms:mmsclient的套件名稱 (在Ubuntu上是這麼叫它),用來下載MS格式的線上影音檔案。
foto-mosaik:一個在windows上執行的軟體,用來製作Truman show海報那樣子的圖(用很多小圖拼湊成一張大圖),目前正在尋找Linux下的替代軟體。
==>找到了!
metapixel:在Linux下製作mosaic pictures/images的軟體,這個網站有使用教學。

An example of mosaic pictures/images

Saturday, October 20, 2007

MediaPlayerConnectivity

MediaPlayerConnectivity是給firefox用的plugin,其用途是補強firefox多媒體方面的表現。
例如,原始的firefox並不支援播放wmx格式的檔案,偏偏又有很多網站使用這種由MS制定的音樂格式,安裝MediaPlayerConnectivity之後就可以透過外部播放器(mplayer, totem, xine等。。。)播放firefox不支援的影音檔案。

今天會找到這個plugin完全是因為要聽漢聲電台上的軍歌(用javascript害我抓不下來@@)!
下星期督導長就要把我跟另一個菜鳥抓去考試,條文的部份都背完了,軍歌則要趁這兩天放假趕快聽一聽背起來~
可是到現在我還在聽線上音樂,望著需要1K~1K2字的作文題目"決定快樂,從心出發"發呆,真是沒想到放假還要寫回家功課,下一梯新兵什麼時候才要來幫我們做這些事情阿!!!

有點失望

轉眼間撥交到新單位已經一個星期,新單位還算輕鬆,跟很多單位比起來應該算是天堂(每天晚上還有時間打桌球。。。)!
只不過這個星期中聽到許多學長(義務役+志願役)分析著本中隊的未來,聽著聽著便覺得有點失望。
原本以為到這個單位可以經手一些有趣的業務,"順便"學到一點網路安全的實務經驗,但是因為上頭的政策改變,導致接下來的義務役通通都被編到"整備"組中,美其名是當種子教官教志願役官兵寫程式之類的工作,但是其實大部分的時間會花在打雜上面。
這樣子整個奇蒙子就不是很好,好像趁我們在服役的時候完全被吸乾而沒有得到任何新的東西,當然可以趁有時間的時候自修,但是如果上班時間就是做相關的事情似乎會比較理想。

其實從許多大大小小的事情都可以瞧的出些端倪,例如昨天長官來視導的時候督導長就叫我&宣文(跟我一起進去的菜鳥)到外面待命,說好聽是我們呆在裡面也無聊,但是我覺得是避免讓我們接觸到機敏資料~
這時候自我安慰機制再度開啟:其實上面會這樣想也是有道理啦,把國家安全放在役期僅一年的義務役身上太不可靠了,而且這樣的話退伍的時候也不用被管制出境(但換個角度想,海陸,空特那些人怎麼辦?!)。

看來我就認命的在接下來的十一個月乖乖打雜會過得比較快樂,雖然有人帶會學的比較快,不過反正到時候站安官的時候也有一堆時間可以自己看書自修,有道是天下無難事只怕有心人!

Wednesday, October 10, 2007

clever auto complete

今天意外發現一套Linux上開發的3D引擎(Cube & Cube2),由於該引擎所表現出來的效果十分優良讓我興起把他抓下來研究的念頭,抓完之後在解壓縮的時候輸入檔名之際忽然發現以往按下Tab就會出現的auto complete居然沒有發揮作用!
仔細檢查之後發現該壓縮檔的格式為bz2,而我下的指令參數為給gz用的tar xzf,改為tar xjf之後auto complete果然恢復作用。
沒想到現在連auto complete都會幫忙判別檔案類型,看來我們離星際旅行又近了一步 :-P

Monday, October 08, 2007

不幸中的大幸

不幸:
1. 放假兩天被颱風吃掉
2. 昨天才裝好nvidia driver的$0 PC正式退役(http://www.ubuntu.org.tw/modules/newbb/viewtopic.php?topic_id=6321&forum=1&post_id=28872#forumpost28872)。

大幸:
昨天媽媽問我NZ passport什麼時候過期,要我到時候提前去換新護照,於是我上了department of internal affairs網站看更換護照的注意事項,看著看著便看到第一次申請護照必須準備certificate of citizenship,心中忽然一驚:"我把那張該死的紙丟到哪了?"
左找右找找遍了房間與地下室就是找不到,雖然護照過期只需要拿舊護照換新護照,但是總覺得那張證明還是該好好收著,於是便帶著安撫的語氣告訴自己一定是放在石牌,因為回台灣之後根本沒去碰它,所以忘了帶回三芝~
今天帶著忐忑的心情前往石牌偵查(請鄰兵以火力掩護我前去偵查@@),幸好它乖乖的躺在書桌的抽屜中,我也終於放下心中的大石頭 :-)

就是這張該死的紙


===== Update @ 2007-10-10 =====
昨天抱著姑且一試的心情做了memory test,經過一整天的測試之後發現memory的pass rate只有1%!於是採隨機抽樣的方式拔掉了一條256M的記憶體(共三條),結果居然就可以開機,原來還好好的記憶忽然就這樣掛掉,整個就是冏~
BTW,拔記憶體的同時順便把MX200換成前陣子從爸爸舊電腦拔下來的GeForce4 Ti4200,跑glxgears的時候performance增加十倍(從三百多提高到三千多)。

Sunday, October 07, 2007

video card driver v.s. cpu load

原本想說我又不會拿$0 PC玩遊戲因此而沒有在重裝xubuntu的時候安裝nvidia driver,於是在接下來的時日中常常覺得cpu load常常飆到很高的"啪數"造成系統lag,尤其是在看網頁的時候(eg. nvidia的官方網頁)。
一開始的時候還會安慰自己說這台電腦的配備本來就不好(1.6GHz + 768M ram + GeForce2 MX200),lag也是應該的,但是今天實在是再也無法忍受,於是抱著姑且一試的心情到nvidia官網把舊的driver抓下來安裝(上一篇的安裝kernel source其實就是為了裝nvidia driver)。
沒想到driver裝完之後整個系統還頗順暢,雖然還不到玩game的程度(不要太苛求MX200~),但是至少大幅改善原先光是看網頁就會lag的問題!

這件事情告訴我們,有driver就要裝。。。

install kernel-source in ubuntu

在ubuntu與debian的套件名稱有點出入,因此安裝kernel source的時候無法直接將debian那套搬過來用,google之後得知必須安裝套件linux-sourcelinux-headers(*)。
意外中發現ubuntu的apt-get越來越人性化,當我輸入apt-get install linux-headers的時候他會顯示所有有效的版本讓我選擇,當然,正確的版本還是要自行透過uname取得。

myprompt:$ sudo apt-get install linux-headers
讀取套件清單中... 完成
了解套件依存關係中
Reading state information... 完成
虛擬套件『linux-headers』的提供者是:
linux-headers-2.6.20-16-server-bigiron 2.6.20-16.32
linux-headers-2.6.20-16-server 2.6.20-16.32
linux-headers-2.6.20-16-lowlatency 2.6.20-16.32
linux-headers-2.6.20-16-generic 2.6.20-16.32
linux-headers-2.6.20-16-386 2.6.20-16.32
linux-headers-2.6.20-16 2.6.20-16.32
linux-headers-2.6.20-15-server-bigiron 2.6.20-15.27
linux-headers-2.6.20-15-server 2.6.20-15.27
linux-headers-2.6.20-15-lowlatency 2.6.20-15.27
linux-headers-2.6.20-15-generic 2.6.20-15.27
linux-headers-2.6.20-15-386 2.6.20-15.27
linux-headers-2.6.20-15 2.6.20-15.27
請您明確地選擇一個來進行安裝。


* http://tech.givemethe.net/node/9

Friday, October 05, 2007

終於盼到結訓假了

上次探親假收假之後便時時刻刻盼望結訓假的到來,這下終於盼到了!
這九天之中"密集"操課六天因為接著有三天的鑑測,說到鑑測鑑測成績就要老王賣瓜一下,本連的成績應該是全旅第三名(因為打靶成績是第三名,而這向成績的比重最重),終於喝到連長從外面泡沫紅茶店買來的飲料。
最後就是今天的抽籤,本人的運氣還不錯,小抽的時候就一舉中籤得以在10/12向國防部參謀本部資電作戰指揮部本部報到,聽說是在機房裡面做事,這麼一來應該等同在冷氣房裡上班了。
開始放結訓假的時候有種時空錯亂的感覺,整個心情放鬆的狀態讓我以為我已經退伍了~~"
心中的喜悅形於色,喜悅的不是脫離班長以上的長官,而是終於能夠離開那個鬼地方&一群無法控制自我行為能力+疑似智商低落的人。
唯一美中不足的地方是每次休假都遇到颱風@@