From e0fe23461cc94a80e02a9542ab2182f62badd5de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Thu, 31 Mar 2011 07:31:14 +0200 Subject: Don't build stacktrace until erlang:get_stacktrace() is called Currently, dbg_istk:exception_stacktrace/2 does not do a very good job imitating BEAM's stacktrace. The reason is that it need to be relatively fast since a simulated stacktrace is constructed not only when an exception oocurs, but also before every call to non-interpreted code. To prepare for a future more thorough (and slower) stacktrace construction, refactor the building of the stacktrace so that it only is done when erlang:get_stacktrace/0 is called. --- lib/debugger/src/dbg_istk.erl | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'lib/debugger/src/dbg_istk.erl') diff --git a/lib/debugger/src/dbg_istk.erl b/lib/debugger/src/dbg_istk.erl index 2e7b114fab..2c4c2a3518 100644 --- a/lib/debugger/src/dbg_istk.erl +++ b/lib/debugger/src/dbg_istk.erl @@ -19,7 +19,7 @@ -module(dbg_istk). -export([init/0,to_external/0,from_external/1, push/2,pop/0,pop/1,stack_level/0, - exception_stacktrace/2, + delayed_stacktrace/0,delayed_stacktrace/1, bindings/1,stack_frame/2,backtrace/2, in_use_p/2]). @@ -106,25 +106,33 @@ stack_level() -> stack_level([]) -> 1; stack_level([#e{level=Le}|_]) -> Le. -%% exception_stacktrace(HowMuch, #ieval{}) -> Stacktrace -%% HowMuch = complete | no_current -%% Stacktrace = [{M,F,Args|Arity} | {Fun,Args}] -%% Convert internal stack format to an imitation of the -%% regular stacktrace. +%% delayed_stacktrace() -> CreateStacktraceFun +%% delayed_stacktrace(#ieval{}) -> CreateStacktraceFun +%% CreateStacktraceFun = fun(NumberOfEntries) +%% +%% Return a fun that can convert the internal stack format to +%% an imitation of the regular stacktrace. %% %% Max three elements, no repeated (recursive) calls to the same %% function and convert argument lists to arity for all but the topmost %% entry (and funs). -exception_stacktrace(complete, #ieval{}=Ieval) -> +delayed_stacktrace() -> + Stack = get(?STACK), + do_delayed_stacktrace(Stack). + +delayed_stacktrace(Ieval) -> #ieval{module=Mod,function=Name,arguments=As} = Ieval, - Stk = [#e{mfa={Mod,Name,As}}|get(?STACK)], - fix_stacktrace(Stk); -exception_stacktrace(no_current, #ieval{}) -> - fix_stacktrace(get(?STACK)). + Stack = [#e{mfa={Mod,Name,As}}|get(?STACK)], + do_delayed_stacktrace(Stack). + +do_delayed_stacktrace(Stack) -> + fun(_NumEntries) -> + fix_stacktrace(Stack) + end. -fix_stacktrace(Stk) -> - case fix_stacktrace2(sublist(Stk, 1, 3)) of +fix_stacktrace(Stack) -> + case fix_stacktrace2(sublist(Stack, 1, 3)) of [] -> []; [H|T] -> -- cgit v1.2.3