close

之前jserv寫了篇很有趣的文章(Who Call Me ?),利用一些小技巧去取caller和callee 之間的information。不過每個trace 的地方都還要手動塞code,如果要在往上看再去看stack frame,我對x86實在是很不熟。


今天在寫profiling的東東想到是不是可以用instrumentation(註一)的方式來得到同樣的資訊,把whocallme 的call 塞到每個function裡。原本以為-pg(gprof)可以做得到,因為gprof本來就是利用偷塞一些code來統計資訊,但gcc似乎不允許你塞自已的function。


那有其它的方式嗎?果然gcc support -finstrument-functions這個選項,會在你所有function 前後各塞入一個function,讓user可以蒐集或輸出一些資訊



void __cyg_profile_func_enter (void *this_fn, void *call_site);
void __cyg_profile_func_exit   (void *this_fn, void *call_site);



使用方式呢?這種技巧jserv 去年已經介紹過了,請看這裡。那現在就是把他們組合起來啦,底下是patch


--- ../whocallme/whocallme.c    2008-07-30 13:57:30.000000000 +0800
+++ ../whocallme2/whocallme.c   2008-08-20 17:59:23.000000000 +0800
@@ -60,7 +60,7 @@
{
    int i;
    for (i = 0; i < table_count; i++) {
-       if (addr > fun_table[i].addr) {
+       if (addr >= fun_table[i].addr) {
            if (addr < fun_table[i + 1].addr)
                return fun_table[i].name;
        }
@@ -134,3 +134,12 @@
    qsort(fun_table, table_count, sizeof(FUN_TABLE), compare_function);
    return 0;
}
+
+#define DUMP(func, call) \
+        printf("%s: func = %s, called by = %s\n", __FUNCTION__, func, call)
+
+void __attribute__((__no_instrument_function__))__cyg_profile_func_enter(void *this_func, void *call_site)
+{
+        DUMP(find_function_by_addr((unsigned long)this_func),
+        find_function_by_addr((unsigned long)call_site));
+}


 


在編譯你自已的程式時加入 -finstrument-functions(編譯whocallme.c 的時候不用,不然每個function 都要再補上no_instrucment_function的 attribute


,然後輸出就會長成這樣,


__cyg_profile_func_enter: func = test, called by = main
__cyg_profile_func_enter: func = test_a, called by = main
__cyg_profile_func_enter: func = test_b, called by = test_a
__cyg_profile_func_enter: func = test, called by = test_b
__cyg_profile_func_enter: func = test_c, called by = test_a
__cyg_profile_func_enter: func = test_b, called by = main
__cyg_profile_func_enter: func = test, called by = test_b
__cyg_profile_func_enter: func = test_c, called by = main


註一:instrumentation 要翻成啥?,"中山山"好像不錯

arrow
arrow
    全站熱搜

    cmchao 發表在 痞客邦 留言(0) 人氣()