Erlang堆栈跟踪中包含多少个条目?

leg*_*cia 3 erlang stack-trace

当查看来自Erlang程序中错误的堆栈跟踪时,在我看来,有时候我看不到整个图片,某些条目已被删除。堆栈跟踪中包含多少个条目是否有限制?

leg*_*cia 5

首先,如果将堆栈跟踪打印为Erlang术语,请注意,io:format如果列表足够深,则可能会截断该列表。如果发生这种情况,您将...在输出中看到。

其次,如果似乎堆栈跟踪的中间缺少条目,则可能是由于尾调用优化。如果一个函数做的最后一件事是调用另一个函数,那么将消除调用函数的堆栈框架,并且在堆栈跟踪中将不可见。

除此之外,堆栈跟踪中仅包括最后(最内部)八个堆栈帧。例如,给定一个发出错误信号深度为十二帧的程序(请参见下面的清单),您将获得以下输出:

1> foo:one().
** exception error: this_is_twelve
     in function  foo:twelve/0 (foo.erl, line 49)
     in call from foo:eleven/0 (foo.erl, line 45)
     in call from foo:ten/0 (foo.erl, line 41)
     in call from foo:nine/0 (foo.erl, line 37)
     in call from foo:eight/0 (foo.erl, line 33)
     in call from foo:seven/0 (foo.erl, line 29)
     in call from foo:six/0 (foo.erl, line 25)
     in call from foo:five/0 (foo.erl, line 21)
Run Code Online (Sandbox Code Playgroud)

请注意,堆栈跟踪中不存在最外层的四个条目,仅剩下八个最内层的条目。

八只是默认值。您可以通过erlang:system_flag/2使用进行更改backtrace_depth。(它返回旧值。)

2> erlang:system_flag(backtrace_depth, 10).
8
3> foo:one().                              
** exception error: this_is_twelve
     in function  foo:twelve/0 (foo.erl, line 49) 
     in call from foo:eleven/0 (foo.erl, line 45)
     in call from foo:ten/0 (foo.erl, line 41)
     in call from foo:nine/0 (foo.erl, line 37)
     in call from foo:eight/0 (foo.erl, line 33)
     in call from foo:seven/0 (foo.erl, line 29)
     in call from foo:six/0 (foo.erl, line 25)
     in call from foo:five/0 (foo.erl, line 21)
     in call from foo:four/0 (foo.erl, line 17)
     in call from foo:three/0 (foo.erl, line 13)
Run Code Online (Sandbox Code Playgroud)

这是foo.erl。的x原子是有防止成为尾调用,这将意味着调用者的堆栈帧将从栈跟踪被去除的函数调用。

-module(foo).
-compile(export_all).

one() ->
   two(),
   x.

two() ->
   three(),
   x.

three() ->
   four(),
   x.

four() ->
   five(),
   x.

five() ->
   six(),
   x.

six() ->
   seven(),
   x.

seven() ->
   eight(),
   x.

eight() ->
   nine(),
   x.

nine() ->
   ten(),
   x.

ten() ->
   eleven(),
   x.

eleven() ->
   twelve(),
   x.

twelve() ->
   error(this_is_twelve),
   x.
Run Code Online (Sandbox Code Playgroud)