1: %%
    2: %% %CopyrightBegin%
    3: %% 
    4: %% Copyright Ericsson AB 2007-2013. All Rights Reserved.
    5: %% 
    6: %% The contents of this file are subject to the Erlang Public License,
    7: %% Version 1.1, (the "License"); you may not use this file except in
    8: %% compliance with the License. You should have received a copy of the
    9: %% Erlang Public License along with this software. If not, it can be
   10: %% retrieved online at http://www.erlang.org/.
   11: %% 
   12: %% Software distributed under the License is distributed on an "AS IS"
   13: %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
   14: %% the License for the specific language governing rights and limitations
   15: %% under the License.
   16: %% 
   17: %% %CopyrightEnd%
   18: %%
   19: 
   20: -module(nofrag_SUITE).
   21: 
   22: -include_lib("test_server/include/test_server.hrl").
   23: 
   24: -export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
   25: 	 init_per_group/2,end_per_group/2,
   26: 	 init_per_testcase/2,end_per_testcase/2,
   27: 	 error_handler/1,error_handler_apply/1,
   28: 	 error_handler_fixed_apply/1,error_handler_fun/1,
   29: 	 debug_breakpoint/1]).
   30: 
   31: %% Exported functions for an error_handler module.
   32: -export([undefined_function/3,undefined_lambda/3,breakpoint/3]).
   33: 
   34: suite() -> [{ct_hooks,[ts_install_cth]}].
   35: 
   36: all() -> 
   37:     [error_handler, error_handler_apply,
   38:      error_handler_fixed_apply, error_handler_fun,
   39:      debug_breakpoint].
   40: 
   41: groups() -> 
   42:     [].
   43: 
   44: init_per_suite(Config) ->
   45:     Config.
   46: 
   47: end_per_suite(_Config) ->
   48:     ok.
   49: 
   50: init_per_group(_GroupName, Config) ->
   51:     Config.
   52: 
   53: end_per_group(_GroupName, Config) ->
   54:     Config.
   55: 
   56: 
   57: init_per_testcase(Func, Config) when is_atom(Func), is_list(Config) ->
   58:     Dog = ?t:timetrap(?t:minutes(3)),
   59:     [{watchdog,Dog}|Config].
   60: 
   61: end_per_testcase(_Func, Config) ->
   62:     Dog = ?config(watchdog, Config),
   63:     ?t:timetrap_cancel(Dog).
   64: 
   65: error_handler(Config) when is_list(Config) ->
   66:     ?line process_flag(error_handler, ?MODULE),
   67:     %% The term_to_binary/1 - binary_to_term/1 roundtrip is a good way
   68:     %% to traverse the entire term.
   69:     ?line Term = collect(1024),
   70:     ?line Term = binary_to_term(term_to_binary(Term)),
   71:     ?line 1024 = length(Term),
   72:     ?line [[a,b,c,d,[e,f,g]]] = lists:usort(Term),
   73:     ok.
   74: 
   75: collect(0) ->
   76:     [];
   77: collect(N) ->
   78:     %% The next line calls the error handle function, which is
   79:     %% ?MODULE:undefined_function/3 (it simply returns the list
   80:     %% of args, i.e. [a,b,c,d,[e,f,g]]).
   81:     C = fooblurf:x(a, b, c, d, [e,f,id(g)]),
   82: 
   83:     %% The variable C will be saved onto the stack frame; if C
   84:     %% points into a heap fragment the garbage collector will reach
   85:     %% it and the emulator will crash sooner or later (sooner if
   86:     %% the emulator is debug-compiled).
   87:     Res = collect(N-1),
   88:     [C|Res].
   89: 
   90: collect_apply(0, _) ->
   91:     [];
   92: collect_apply(N, Mod) ->
   93:     %% The next line calls the error handle function, which is
   94:     %% ?MODULE:undefined_function/3 (it simply returns the list
   95:     %% of args).
   96: 
   97:     C = apply(Mod, xyz, id([{a,id(42)},b,c,d,[e,f,id(g)]])),
   98: 
   99:     %% The variable C will be saved onto the stack frame; if C
  100:     %% points into a heap fragment the garbage collector will reach
  101:     %% it and the emulator will crash sooner or later (sooner if
  102:     %% the emulator is debug-compiled).
  103:     Res = collect_apply(N-1, Mod),
  104:     [C|Res].
  105: 
  106: error_handler_apply(Config) when is_list(Config) ->
  107:     ?line process_flag(error_handler, ?MODULE),
  108: 
  109:     %% The term_to_binary/1 - binary_to_term/1 roundtrip is a good way
  110:     %% to traverse the entire term.
  111:     ?line Term = collect_apply(1024, fooblurfbar),
  112:     ?line Term = binary_to_term(term_to_binary(Term)),
  113:     ?line 1024 = length(Term),
  114:     ?line [[{a,42},b,c,d,[e,f,g]]] = lists:usort(Term),
  115:     ok.
  116: 
  117: error_handler_fixed_apply(Config) when is_list(Config) ->
  118:     ?line process_flag(error_handler, ?MODULE),
  119: 
  120:     %% The term_to_binary/1 - binary_to_term/1 roundtrip is a good way
  121:     %% to traverse the entire term.
  122:     ?line Term = collect_fixed_apply(1024, fooblurfbar),
  123:     ?line Term = binary_to_term(term_to_binary(Term)),
  124:     ?line 1024 = length(Term),
  125:     ?line [[{a,2},b,c,d,[e,f,g]]] = lists:usort(Term),
  126:     ok.
  127: 
  128: collect_fixed_apply(0, _) ->
  129:     [];
  130: collect_fixed_apply(N, Mod) ->
  131:     %% The next line calls the error handle function, which is
  132:     %% ?MODULE:undefined_function/3 (it simply returns the list
  133:     %% of args).
  134:     C = Mod:x({a,id(2)}, b, c, d, [e,f,id(g)]),
  135: 
  136:     %% The variable C will be saved onto the stack frame; if C
  137:     %% points into a heap fragment the garbage collector will reach
  138:     %% it and the emulator will crash sooner or later (sooner if
  139:     %% the emulator is debug-compiled).
  140:     Res = collect_fixed_apply(N-1, Mod),
  141:     [C|Res].
  142: 
  143: undefined_function(_Mod, _Name, Args) ->
  144:     Args.
  145: 
  146: error_handler_fun(Config) when is_list(Config) ->
  147:     ?line process_flag(error_handler, ?MODULE),
  148: 
  149:     %% fun(A, B, C) -> {A,B,C,X} end in module foobarblurf.
  150:     B = <<131,112,0,0,0,84,3,109,96,69,208,5,175,207,75,36,93,112,218,232,222,22,251,0,
  151: 	 0,0,0,0,0,0,1,100,0,11,102,111,111,98,97,114,98,108,117,114,102,97,0,98,5,
  152: 	 244,197,144,103,100,0,13,110,111,110,111,100,101,64,110,111,104,111,115,116,
  153: 	 0,0,0,46,0,0,0,0,0,104,3,97,1,97,2,97,3>>,
  154:     ?line Fun = binary_to_term(B),
  155:     ?line Term = collect_fun(1024, Fun),
  156:     ?line Term = binary_to_term(term_to_binary(Term)),
  157:     ?line 1024 = length(Term),
  158:     ?line [[{foo,bar},{99,1.0},[e,f,g]]] = lists:usort(Term),
  159:     ?line {env,[{1,2,3}]} = erlang:fun_info(Fun, env),
  160:     ok.
  161: 
  162: collect_fun(0, _) ->
  163:     [];
  164: collect_fun(N, Fun) ->
  165:     %% The next line calls the error handle function, which is
  166:     %% ?MODULE:undefined_lambda/3 (it simply returns the list
  167:     %% of args).
  168:     C = Fun({foo,id(bar)}, {99,id(1.0)}, [e,f,id(g)]),
  169: 
  170:     %% The variable C will be saved onto the stack frame; if C
  171:     %% points into a heap fragment the garbage collector will reach
  172:     %% it and the emulator will crash sooner or later (sooner if
  173:     %% the emulator is debug-compiled).
  174:     Res = collect_fun(N-1, Fun),
  175:     [C|Res].
  176: 
  177: undefined_lambda(foobarblurf, Fun, Args) when is_function(Fun) ->
  178:     Args.
  179: 
  180: debug_breakpoint(Config) when is_list(Config) ->
  181:     ?line process_flag(error_handler, ?MODULE),
  182:     ?line erts_debug:breakpoint({?MODULE,foobar,5}, true),
  183:     ?line Term = break_collect(1024),
  184:     ?line Term = binary_to_term(term_to_binary(Term)),
  185:     ?line 1024 = length(Term),
  186:     ?line [[a,b,c,{d,e},[f,g,h]]] = lists:usort(Term),
  187:     ?line erts_debug:breakpoint({?MODULE,foobar,5}, false),
  188:     ok.
  189: 
  190: break_collect(0) ->
  191:     [];
  192: break_collect(N) ->
  193:     C = foobar(a, b, c, {id(d),e}, [f,g,id(h)]),
  194:     Res = break_collect(N-1),
  195:     [C|Res].
  196: 
  197: breakpoint(?MODULE, foobar, Args) ->
  198:     Args.
  199: 
  200: foobar(_, _, _, _, _) ->
  201:     exit(dont_execute_me).
  202: 
  203: id(I) -> I.
  204: 
  205: