Erlangでdbg
モジュールは、このための便利な機能を提供します。ここでは、あなたのコードを想定し、これらの機能をトレースしたいどのようにモジュールa
にされてい:
セットアップ:あなたは、モジュール内の任意の関数を呼び出すたび
1> c(a). % load a.erl
{ok,a}
2> dbg:tracer(). % start the default trace message receiver
{ok,<0.71.0>}
3> dbg:p(all, c). % setup call tracing on all processes
{ok,[{matched,[email protected],34}]}
4> dbg:tpl(a, '_', '_', []). % trace all functions in a
{ok,[{matched,[email protected],5}]}
は今、その名前と引数は、シェルで印刷されます。
5> a:sum(lists:seq(1, 10)).
(<0.64.0>) call a:sum([1,2,3,4,5,6,7,8,9,10])
(<0.64.0>) call a:sum([2,3,4,5,6,7,8,9,10])
(<0.64.0>) call a:sum([3,4,5,6,7,8,9,10])
(<0.64.0>) call a:sum([4,5,6,7,8,9,10])
(<0.64.0>) call a:sum([5,6,7,8,9,10])
(<0.64.0>) call a:sum([6,7,8,9,10])
(<0.64.0>) call a:sum([7,8,9,10])
(<0.64.0>) call a:sum("\b\t\n")
(<0.64.0>) call a:sum("\t\n")
(<0.64.0>) call a:sum("\n")
(<0.64.0>) call a:sum([])
55
6> a:tail_sum(lists:seq(1, 10)).
(<0.64.0>) call a:tail_sum([1,2,3,4,5,6,7,8,9,10])
(<0.64.0>) call a:tail_sum([1,2,3,4,5,6,7,8,9,10],0)
(<0.64.0>) call a:tail_sum([2,3,4,5,6,7,8,9,10],1)
(<0.64.0>) call a:tail_sum([3,4,5,6,7,8,9,10],3)
(<0.64.0>) call a:tail_sum([4,5,6,7,8,9,10],6)
(<0.64.0>) call a:tail_sum([5,6,7,8,9,10],10)
(<0.64.0>) call a:tail_sum([6,7,8,9,10],15)
(<0.64.0>) call a:tail_sum([7,8,9,10],21)
(<0.64.0>) call a:tail_sum("\b\t\n",28)
(<0.64.0>) call a:tail_sum("\t\n",36)
(<0.64.0>) call a:tail_sum("\n",45)
(<0.64.0>) call a:tail_sum([],55)
55
またreturn_trace()
を返すマッチの仕様を使用して戻り値をログに記録するトレーサーを求めることができます。
7> dbg:tpl(a, '_', '_', [{'_',[],[{return_trace}]}]).
(<0.64.0>) call a:module_info()
{ok,[{matched,[email protected],5},{saved,1}]}
8> a:sum(lists:seq(1, 10)).
(<0.64.0>) call a:sum([1,2,3,4,5,6,7,8,9,10])
(<0.64.0>) call a:sum([2,3,4,5,6,7,8,9,10])
(<0.64.0>) call a:sum([3,4,5,6,7,8,9,10])
(<0.64.0>) call a:sum([4,5,6,7,8,9,10])
(<0.64.0>) call a:sum([5,6,7,8,9,10])
(<0.64.0>) call a:sum([6,7,8,9,10])
(<0.64.0>) call a:sum([7,8,9,10])
(<0.64.0>) call a:sum("\b\t\n")
(<0.64.0>) call a:sum("\t\n")
(<0.64.0>) call a:sum("\n")
(<0.64.0>) call a:sum([])
(<0.64.0>) returned from a:sum/1 -> 0
(<0.64.0>) returned from a:sum/1 -> 10
(<0.64.0>) returned from a:sum/1 -> 19
(<0.64.0>) returned from a:sum/1 -> 27
(<0.64.0>) returned from a:sum/1 -> 34
(<0.64.0>) returned from a:sum/1 -> 40
(<0.64.0>) returned from a:sum/1 -> 45
(<0.64.0>) returned from a:sum/1 -> 49
(<0.64.0>) returned from a:sum/1 -> 52
(<0.64.0>) returned from a:sum/1 -> 54
(<0.64.0>) returned from a:sum/1 -> 55
55
9> a:tail_sum(lists:seq(1, 10)).
(<0.64.0>) call a:tail_sum([1,2,3,4,5,6,7,8,9,10])
(<0.64.0>) call a:tail_sum([1,2,3,4,5,6,7,8,9,10],0)
(<0.64.0>) call a:tail_sum([2,3,4,5,6,7,8,9,10],1)
(<0.64.0>) call a:tail_sum([3,4,5,6,7,8,9,10],3)
(<0.64.0>) call a:tail_sum([4,5,6,7,8,9,10],6)
(<0.64.0>) call a:tail_sum([5,6,7,8,9,10],10)
(<0.64.0>) call a:tail_sum([6,7,8,9,10],15)
(<0.64.0>) call a:tail_sum([7,8,9,10],21)
(<0.64.0>) call a:tail_sum("\b\t\n",28)
(<0.64.0>) call a:tail_sum("\t\n",36)
(<0.64.0>) call a:tail_sum("\n",45)
(<0.64.0>) call a:tail_sum([],55)
(<0.64.0>) returned from a:tail_sum/2 -> 55
(<0.64.0>) returned from a:tail_sum/2 -> 55
(<0.64.0>) returned from a:tail_sum/2 -> 55
(<0.64.0>) returned from a:tail_sum/2 -> 55
(<0.64.0>) returned from a:tail_sum/2 -> 55
(<0.64.0>) returned from a:tail_sum/2 -> 55
(<0.64.0>) returned from a:tail_sum/2 -> 55
(<0.64.0>) returned from a:tail_sum/2 -> 55
(<0.64.0>) returned from a:tail_sum/2 -> 55
(<0.64.0>) returned from a:tail_sum/2 -> 55
(<0.64.0>) returned from a:tail_sum/2 -> 55
(<0.64.0>) returned from a:tail_sum/1 -> 55
55
完全に働いた:これは明らかに
sum
を段階tail_sum
とは対照的に値を構築する方法を示しています!ありがとう –私は 'return_trace'を含むように私の答えを編集しました。これはさらに便利なはずです。トレースは、Erlang VMの私のお気に入りの機能の1つです! – Dogbert