2016-05-09 14 views
3

私はCバイナリファイル(ソースコードが利用可能)と事前定義された作業負荷を持っています。私は、すべての関数呼び出しとそのそれぞれのパラメータ、および戻り値を呼び出し順に記録したい。例えば、以下のコードで:各C関数呼び出しのすべてのパラメータ値を記録します

int myfunc(int value){ 
    return value*2; 
} 

int main(void){ 
    int i; 
    i = myfunc(10); 
    i = myfunc(12); 
    i = myfunc(20); 
    return 0; 
} 

それは、このようなログファイルになりshoud:

int main() 
int myfunc(10) 
myfunc return 20 
int myfunc(12) 
myfunc return 24 
int myfunc(21) 
myfunc return 42 
main return 0 

を私はすでにそれを行うにはインテルPINを使用しようとした、そしてそれは非常にうまく機能ただし、変数がポインタ、配列、構造体またはtypedefの場合は例外です。私はすべての変数が必要です。

私はソースコードを持っており、デバッグオプションを使用してコンパイルできます。

アイデア?

P.S.私の実際の作業負荷は90種類の機能から約5000回の呼び出しを持つため、手動でデバッグすることは不可能です。

編集:

(gdb) help command 
Set commands to be executed when a breakpoint is hit. 
Give breakpoint number as argument after "commands". 
With no argument, the targeted breakpoint is the last one set. 
The commands themselves follow starting on the next line. 
Type a line containing "end" to indicate the end of them. 
Give "silent" as the first line to make the breakpoint silent; 
then no output is printed when it is hit, except what the commands print. 

チェックアウト私のアプローチ:OSは、あなたが実行時にブレークポイント「イベント」を使用して関数呼び出しとその戻り値を追跡するためgdbを使用することができますアーチのLinuxの64ビット

+0

[Systemtap](https://sourceware.org/systemtap/)の1つのオプションがあります。あなたのOSは指定されていませんが、SystemtapはLinux用です。 – kaylum

+0

Tks @kaylum、Systemtapを試して、動作すればここに投稿します。 – William

答えて

5

ある

ソースコード:

int myfunc(int n) 
{ 
    int val = n * 2; 

    return val; 
} 

int main(void) 
{ 
    int i; 

    i = myfunc(10); 
    i = myfunc(12); 
    i = myfunc(20); 

    return 0; 
} 

1)コンパイルよ

$ gcc -g program.c -o program 

2)を実行しgdb-gオプションを使用して、デバッグモードでウルプログラムgdbシェルで

$ gdb ./program 

3)は、あなたが追跡したい各機能および/またはラインにブレークポイントを設定することができます。

(gdb) break myfunc 
Breakpoint 1 at 0x4011d6: file program.c, line 3. 

(gdb) break program.c:5 
Breakpoint 2 at 0x4011de: file program.c, line 5. 

4)silentコマンドを使用してサイレントブレークポイントのイベントを設定します。

(gdb) command 1 
Type commands for breakpoint(s) 1, one per line. 
End with a line saying just "end". 
>silent 
>printf "calling myfunc(%d)\n", n 
>cont 
>end 

(gdb) command 2 
Type commands for breakpoint(s) 2, one per line. 
End with a line saying just "end". 
>silent 
>printf "myfunc return %d\n", val 
>cont 
>end 

5)プログラムを実行します。

(gdb) run 
Starting program: /home/Administrator/program 
[New Thread 192.0xe00] 
[New Thread 192.0x55c] 
calling myfunc(10) 
myfunc return 20 
calling myfunc(12) 
myfunc return 24 
calling myfunc(20) 
myfunc return 40 
[Inferior 1 (process 192) exited normally] 

OBS:gdbが呼び出されたときに.gdbinitというスクリプトファイルを読み込みます。この ファイルには、gdb の起動中に自動的に実行するコマンドが含まれており、デバッグ操作を自動化するために使用できます。

希望すると助かります!

+0

ありがとうラコバス。私は@kalyumの提案を最初に試してみます。私は侵入が巨大であるため、デバッグは避けています。必須ではありませんが、侵入の少ないものを使うことができればいいです。しかし、私のphdスポンサーは、2つの新しいオプションがあることを知りたいと思うでしょう。 – William

+0

申し訳ありません@ウィリアム、しかし私は 'gdb'よりも邪魔にならない方法は知らない。 – Lacobus

+0

Btw、 'strace'というツールをチェックしましたか?プロセスとLinuxカーネル間の相互作用を監視するために使用されます。システムコール、シグナル配信、プロセス状態の変更などが含まれます。あなたのPhDのための良い話題かもしれません。 – Lacobus

関連する問題