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

问题描述 投票:3回答:1

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

erlang stack-trace
1个回答
4
投票

首先,如果堆栈跟踪打印为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)

请注意,堆栈跟踪中不存在最外面的四个条目,并且仅保留最内层的八个条目。

八是默认值。您可以通过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)

这是foo.erlx原子用于防止函数调用成为尾调用,这意味着调用者的堆栈帧将从堆栈跟踪中删除。

-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.
© www.soinside.com 2019 - 2024. All rights reserved.