紀錄一下在 linux 上不同層級的 tracing tool,xtrace 系列為主..
- ltrace:system library function trace, ex : printf
- ftrace:kernel function trace, ex : ext4_xxx,適合透過 filter 來 trace kernel module。
- strace:system call function trace, ex : open,可以用來追蹤像 apache & PHP 的一些錯誤發生的函式或效能的瓶頸。
- ptrace:process trace
- fenris:application tracer, its “GUI”, and an interactive debugger / GUI
- dtrace:solaris 的 debug 工具。
- systemtap:類似 dtrace。
- utrace:Utrace 是 Linux 中最新的 trace 和 debug 基礎構架
based on gdb, user level trace
- etrace:Run-time function call tree with gcc
- call-graphs-using-the-gnu-project-debugger : 這個 script 寫的很好,可以無腦套用。
- trace-and-profile-function-calls-with-gcc
systemtap function trace example
由於 utrace 是 redhat 在維護,所以這個 user space trace 是 systemtap + utrace 最好是用新一點的 fedora 才能跑,我用自己 patch utrace 的 ubuntu 10.4 跑不起來,應該是 systemtap 版本的問題。
1 2 3 4 | //kernel space $ stap -e 'probe kernel.function("*").call { log(probefunc()) }' //user space 要 trace 的程式編譯時需要 gcc -g $ stap -e 'probe process("main").function("*").call { log(probefunc()) }' -c . /main |
參考 ftrace 格式用 systemtap 寫 process function trace..
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | function timestamp:long() { return gettimeofday_us() - trace[depth] } function trace(entry_p) { printf ( "(%d) %d us |%s %s%s\n" , cpu(), timestamp(), thread_indent(entry_p*2), (entry_p>0 ? probefunc() : "" ), (entry_p>0 ? "() {" : "}" ) ) } global trace global depth probe process(@1). function ( "*" ).call { depth++ trace[depth] = gettimeofday_us() trace(1) } probe process(@1). function ( "*" ). return { trace(-1) delete trace[depth] depth-- } # example output $ stap -c . /main pftrace.stp main (2) 1 us | 0 main(9017): main() { (2) 0 us | 12 main(9017): foo() { (2) 7 us | 18 main(9017): } (2) 1 us | 25 main(9017): bar() { (2) 0 us | 34 main(9017): barone() { (2) 6 us | 40 main(9017): } (2) 0 us | 46 main(9017): bartwo() { (2) 6 us | 51 main(9017): } (2) 30 us | 54 main(9017): } (2) 59 us | 57 main(9017): } |
參考資料:
- http://www.kgdb.info/
- http://stackoverflow.com/questions/311840/tool-to-trace-local-function-calls-in-linux
- http://lcamtuf.coredump.cx/fenris/debug-tools.html
- Spying on Apache and PHP with Strace
- ibm utrace
- utrace patch
- 在Ubuntu10.10上升級內核到2.6.36使用systemtap