/
dbg_ieval.erl
1558 lines (1415 loc) · 47.8 KB
/
dbg_ieval.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1998-2011. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
-module(dbg_ieval).
-export([eval/3,exit_info/5]).
-export([eval_expr/3]).
-export([check_exit_msg/3,exception/4]).
-include("dbg_ieval.hrl").
%%====================================================================
%% External exports
%%====================================================================
%%--------------------------------------------------------------------
%% eval(Mod, Func, Args) -> Meta
%% Mod = Func = atom()
%% Args = [term()]
%% MFA = {Mod,Func,Args} | {Mod,Func,Arity} | {Fun,Args}
%% Arity = integer()
%% Meta = pid()
%% Entry point from debugged process (dbg_debugged).
%% Immediately returns the pid for the meta process.
%% The evaluated value will later be sent as a message to
%% the calling process.
%%--------------------------------------------------------------------
eval(Mod, Func, Args) ->
Debugged = self(),
Int = dbg_iserver:find(),
case dbg_iserver:call(Int, {get_meta,Debugged}) of
{ok,Meta} ->
Meta ! {re_entry, Debugged, {eval,{Mod,Func,Args}}},
Meta;
{error,not_interpreted} ->
spawn(fun() ->
meta(Int, Debugged, Mod, Func, Args)
end)
end.
%%--------------------------------------------------------------------
%% exit_info(Int, AttPid, OrigPid, Reason, ExitInfo)
%% Int = AttPid = OrigPid = pid()
%% Reason = term()
%% ExitInfo = {{Mod,Line}, Bs, Stack} | {}
%% Meta process started when attaching to a terminated process.
%% Spawned (by dbg_iserver) in response to user request.
%%--------------------------------------------------------------------
exit_info(Int, AttPid, OrigPid, Reason, ExitInfo) ->
put(int, Int),
put(attached, AttPid),
put(breakpoints, dbg_iserver:call(Int, all_breaks)),
put(self, OrigPid),
put(exit_info, ExitInfo),
case ExitInfo of
{{Mod,Line},Bs,S} ->
dbg_istk:from_external(S),
Le = dbg_istk:stack_level(),
dbg_icmd:tell_attached({exit_at, {Mod, Line}, Reason, Le}),
exit_loop(OrigPid, Reason, Bs,#ieval{module=Mod,line=Line});
{} ->
dbg_istk:init(),
dbg_icmd:tell_attached({exit_at, null, Reason, 1}),
exit_loop(OrigPid, Reason, erl_eval:new_bindings(),#ieval{})
end.
%%--------------------------------------------------------------------
%% eval_expr(Expr, Bs, Ieval) -> {value, Value, Bs}
%%
%% Evalute a shell expression in the real process.
%% Called (dbg_icmd) in response to a user request.
%%--------------------------------------------------------------------
eval_expr(Expr, Bs, Ieval) ->
%% Save current exit info
ExitInfo = get(exit_info),
Stacktrace = get(stacktrace),
%% Emulate a surrounding catch
try debugged_cmd({eval,Expr,Bs}, Bs, Ieval)
catch
Class:Reason ->
Result = case Class of
throw -> Reason;
_ -> {'EXIT', Reason}
end,
%% Reset exit info
put(exit_info, ExitInfo),
put(stacktrace, Stacktrace),
{value, Result, Bs}
end.
%%--------------------------------------------------------------------
%% check_exit_msg(Msg, Bs, Ieval)
%% Msg = term()
%% Check if Msg is an 'EXIT' msg from the iserver or a 'DOWN' msg
%% from the debugged process. If so exit with correct reason.
%%--------------------------------------------------------------------
check_exit_msg({'EXIT', Int, Reason}, _Bs, #ieval{level=Le}) ->
%% This *must* be interpreter which has terminated,
%% we are not linked to anyone else
if
Le =:= 1 ->
exit(Reason);
Le > 1 ->
exit({Int, Reason})
end;
check_exit_msg({'DOWN',_,_,_,Reason}, Bs,
#ieval{level=Le, module=Mod, line=Li}) ->
%% This *must* be Debugged which has terminated,
%% we are not monitoring anyone else
%% Inform Int about current position, bindings and stack
ExitInfo =
case get(exit_info) of
%% Debugged has been terminated by someone
%% - really the position, bindings and stack are of no
%% importance in this case
%% If we don't save them, however, post-mortem analysis
%% of the process isn't possible
undefined when Le =:= 1 -> % died outside interpreted code
{};
undefined when Le > 1 ->
StackExternal = (dbg_istk:delayed_to_external())(),
{{Mod, Li}, Bs, StackExternal};
%% Debugged has terminated due to an exception
ExitInfo0 when is_function(ExitInfo0, 0) ->
ExitInfo0()
end,
dbg_iserver:cast(get(int), {set_exit_info,self(),ExitInfo}),
if
Le =:= 1 ->
exit(Reason);
Le > 1 ->
exit({get(self), Reason})
end;
check_exit_msg(_Msg, _Bs, _Ieval) ->
ignore.
%%--------------------------------------------------------------------
%% exception(Class, Reason, Bs, Ieval)
%% Class = error | exit | throw
%% Reason = term()
%% Bs = bindings()
%% Ieval = #ieval{}
%% Store information about where in the code the error is located
%% and then raise the exception.
%%--------------------------------------------------------------------
exception(Class, Reason, Bs, Ieval) ->
exception(Class, Reason, Bs, Ieval, false).
exception(Class, Reason, Bs, Ieval, false) ->
do_exception(Class, Reason,
dbg_istk:delayed_stacktrace(no_args, Ieval),
Bs, Ieval);
exception(Class, Reason, Bs, Ieval, true) ->
do_exception(Class, Reason,
dbg_istk:delayed_stacktrace(include_args, Ieval),
Bs, Ieval).
do_exception(Class, Reason, Stacktrace, Bs, #ieval{module=M, line=Line}) ->
StackFun = dbg_istk:delayed_to_external(),
ExitInfo = fun() ->
{{M,Line},Bs,StackFun()}
end,
put(exit_info, ExitInfo),
put(stacktrace, Stacktrace),
erlang:Class(Reason).
%%====================================================================
%% Internal functions
%%====================================================================
%%--Loops-------------------------------------------------------------
%% Entry point for first-time initialization of meta process
meta(Int, Debugged, M, F, As) ->
process_flag(trap_exit, true),
erlang:monitor(process, Debugged),
%% Inform dbg_iserver, get the initial status in return
Pargs = case {M, F} of
%% If it's a fun we're evaluating, show a text
%% representation of the fun and its arguments,
%% not dbg_ieval:eval_fun(...)
{dbg_ieval, eval_fun} ->
{Mx, Fx} = lists:last(As),
{Mx, Fx, lists:nth(2, As)};
_ ->
{M, F, As}
end,
Status = dbg_iserver:call(Int, {new_process,Debugged,self(),Pargs}),
%% Initiate process dictionary
put(int, Int), % pid() dbg_iserver
put(attached, undefined),% pid() attached process
put(breakpoints, dbg_iserver:call(Int, all_breaks)),
put(cache, []),
put(next_break, Status), % break | running (other values later)
put(self, Debugged), % pid() interpreted process
dbg_istk:init(),
put(stacktrace, []),
put(trace_stack, dbg_iserver:call(Int, get_stack_trace)),
put(trace, false), % bool() Trace on/off
put(user_eval, []),
%% Send the result of the meta process
Ieval = #ieval{},
Debugged ! {sys, self(), eval_mfa(Debugged,M,F,As,Ieval)},
dbg_iserver:cast(Int, {set_status, self(), idle, {}}),
dbg_icmd:tell_attached(idle),
meta_loop(Debugged, erl_eval:new_bindings(), Ieval).
debugged_cmd(Cmd, Bs, Ieval) ->
Debugged = get(self),
Debugged ! {sys, self(), {command,Cmd}},
meta_loop(Debugged, Bs, Ieval).
meta_loop(Debugged, Bs, #ieval{level=Le} = Ieval) ->
receive
%% The following messages can only be received when Meta is
%% waiting for Debugged to evaluate non-interpreted code
%% or a Bif. Le>1
{sys, Debugged, {value,Val}} ->
{value, Val, Bs};
{sys, Debugged, {value,Val,Bs2}} ->
{value, Val, Bs2};
{sys, Debugged, {exception,{Class,Reason,Stk}}} ->
case get(exit_info) of
%% Error occurred outside of interpreted code.
undefined ->
MakeStk0 = dbg_istk:delayed_stacktrace(),
MakeStk = fun(Depth0) ->
Depth = max(0, Depth0 - length(Stk)),
Stk ++ MakeStk0(Depth)
end,
do_exception(Class, Reason, MakeStk, Bs, Ieval);
%% Error must have occured within a re-entry to
%% interpreted code, simply raise the exception
_ ->
erlang:Class(Reason)
end;
%% Re-entry to Meta from non-interpreted code
{re_entry, Debugged, {eval,{M,F,As}}} when Le =:= 1 ->
%% Reset process dictionary
%% This is really only necessary if the process left
%% interpreted code at a call level > 1
dbg_istk:init(),
put(stacktrace, []),
put(exit_info, undefined),
dbg_iserver:cast(get(int), {set_status,self(),running,{}}),
dbg_icmd:tell_attached(running),
%% Tell attached process(es) to update source code.
dbg_icmd:tell_attached({re_entry,M,F}),
%% Send the result of the meta process
Debugged ! {sys,self(),eval_mfa(Debugged,M,F,As,Ieval)},
dbg_iserver:cast(get(int), {set_status,self(),idle,{}}),
dbg_icmd:tell_attached(idle),
meta_loop(Debugged, Bs, Ieval);
%% Evaluation in Debugged results in call to interpreted
%% function (probably? a fun)
{re_entry, Debugged, {eval,{M,F,As}}} when Le>1 ->
Ieval2 = Ieval#ieval{module=undefined, line=-1},
Debugged ! {sys,self(),eval_mfa(Debugged,M,F,As,Ieval2)},
meta_loop(Debugged, Bs, Ieval);
Msg ->
check_exit_msg(Msg, Bs, Ieval),
dbg_icmd:handle_msg(Msg, idle, Bs, Ieval),
meta_loop(Debugged, Bs, Ieval)
end.
exit_loop(OrigPid, Reason, Bs, Ieval) ->
receive
Msg ->
check_exit_msg(Msg, Bs, Ieval),
dbg_icmd:handle_msg(Msg, exit_at, Bs, Ieval),
exit_loop(OrigPid, Reason, Bs, Ieval)
end.
%%--Trace function----------------------------------------------------
%%--------------------------------------------------------------------
%% trace(What, Args)
%% What = send | receivex | received | call | return | bif
%% Args depends on What, see the code.
%%--------------------------------------------------------------------
trace(What, Args) ->
trace(What, Args, get(trace)).
trace(return, {_Le,{dbg_apply,_,_,_}}, _Bool) ->
ignore;
trace(What, Args, true) ->
Str = case What of
send ->
{To,Msg} = Args,
io_lib:format("==> ~w : ~p~n", [To, Msg]);
receivex ->
{Le, TimeoutP} = Args,
Tail = case TimeoutP of
true -> "with timeout~n";
false -> "~n"
end,
io_lib:format(" (~w) receive " ++ Tail, [Le]);
received when Args =:= null ->
io_lib:format("~n", []);
received -> % Args=Msg
io_lib:format("~n<== ~p~n", [Args]);
call ->
{Called, {Le,Li,M,F,As}} = Args,
case Called of
extern ->
io_lib:format("++ (~w) <~w> ~w:~w~s~n",
[Le,Li,M,F,format_args(As)]);
local ->
io_lib:format("++ (~w) <~w> ~w~s~n",
[Le,Li,F,format_args(As)])
end;
call_fun ->
{Le,Li,F,As} = Args,
io_lib:format("++ (~w) <~w> ~w~s~n",
[Le, Li, F, format_args(As)]);
return ->
{Le,Val} = Args,
io_lib:format("-- (~w) ~p~n", [Le, Val]);
bif ->
{Le,Li,M,F,As} = Args,
io_lib:format("++ (~w) <~w> ~w:~w~s~n",
[Le, Li, M, F, format_args(As)])
end,
dbg_icmd:tell_attached({trace_output, Str});
trace(_What, _Args, false) ->
ignore.
format_args(As) when is_list(As) ->
[$(,format_args1(As),$)];
format_args(A) ->
[$/,io_lib:format("~p", [A])].
format_args1([A]) ->
[io_lib:format("~p", [A])];
format_args1([A|As]) ->
[io_lib:format("~p", [A]),$,|format_args1(As)];
format_args1([]) ->
[].
%%--Other useful functions--------------------------------------------
%% Mimic catch behaviour
catch_value(error, Reason) ->
{'EXIT',{Reason,get_stacktrace()}};
catch_value(exit, Reason) ->
{'EXIT',Reason};
catch_value(throw, Reason) ->
Reason.
%%--Code interpretation-----------------------------------------------
%%--------------------------------------------------------------------
%% Top level function of meta evaluator.
%% Return message to be replied to the target process.
%%--------------------------------------------------------------------
eval_mfa(Debugged, M, F, As, #ieval{level=Le}=Ieval0) ->
Int = get(int),
Bs = erl_eval:new_bindings(),
Ieval = Ieval0#ieval{level=Le+1,top=true},
try do_eval_function(M, F, As, Bs, extern, Ieval) of
{value, Val, _Bs} ->
trace(return, {Le,Val}),
{ready, Val}
catch
exit:{Debugged, Reason} ->
exit(Reason);
exit:{Int, Reason} ->
exit(Reason);
Class:Reason ->
{exception, {Class, Reason, get_stacktrace()}}
end.
eval_function(Mod, Name, As, Bs, Called, Ieval0, Lc) ->
Tail = Lc andalso get(trace_stack) =:= no_tail,
case Tail of
false ->
Ieval = dbg_istk:push(Bs, Ieval0, Lc),
{value,Val,_} = do_eval_function(Mod, Name, As, Bs, Called, Ieval),
dbg_istk:pop(),
trace(return, {Ieval#ieval.level,Val}),
{value,Val,Bs};
true ->
do_eval_function(Mod, Name, As, Bs, Called, Ieval0)
end.
do_eval_function(Mod, Fun, As0, Bs0, _, Ieval0) when is_function(Fun);
Mod =:= ?MODULE,
Fun =:= eval_fun ->
#ieval{level=Le,line=Li,top=Top} = Ieval0,
case lambda(Fun, As0) of
{[{clause,Fc,_,_,_}|_]=Cs,Module,Name,As,Bs} ->
Ieval = Ieval0#ieval{module=Module,function=Name,
arguments=As0,line=Fc},
trace(call_fun, {Le,Li,Name,As}),
fnk_clauses(Cs, As, Bs, Ieval);
not_interpreted when Top -> % We are leaving interpreted code
trace(call_fun, {Le,Li,Fun,As0}),
{value, {dbg_apply,erlang,apply,[Fun,As0]}, Bs0};
not_interpreted ->
trace(call_fun, {Le,Li,Fun,As0}),
debugged_cmd({apply,erlang,apply,[Fun,As0]}, Bs0, Ieval0);
{error,Reason} ->
%% It's ok not to push anything in this case, the error
%% reason contains information about the culprit
%% ({badarity,{{Mod,Name},As}})
exception(error, Reason, Bs0, Ieval0)
end;
%% Common Test adaptation
do_eval_function(ct_line, line, As, Bs, extern, #ieval{level=Le}=Ieval) ->
debugged_cmd({apply,ct_line,line,As}, Bs, Ieval#ieval{level=Le+1}),
{value, ignore, Bs};
do_eval_function(Mod, Name, As0, Bs0, Called, Ieval0) ->
#ieval{level=Le,line=Li,top=Top} = Ieval0,
trace(call, {Called, {Le,Li,Mod,Name,As0}}),
Ieval = Ieval0#ieval{module=Mod,function=Name,arguments=As0},
case get_function(Mod, Name, As0, Called) of
[{clause,FcLine,_,_,_}|_]=Cs ->
fnk_clauses(Cs, As0, erl_eval:new_bindings(),
Ieval#ieval{line=FcLine});
not_interpreted when Top -> % We are leaving interpreted code
{value, {dbg_apply,Mod,Name,As0}, Bs0};
not_interpreted ->
debugged_cmd({apply,Mod,Name,As0}, Bs0, Ieval);
undef ->
exception(error, undef, Bs0, Ieval, true)
end.
lambda(eval_fun, [Cs,As,Bs,{Mod,Name}=F]) ->
%% Fun defined in interpreted code, called from outside
if
length(element(3,hd(Cs))) =:= length(As) ->
db_ref(Mod), %% Adds ref between module and process
{Cs,Mod,Name,As,Bs};
true ->
{error,{badarity,{F,As}}}
end;
lambda(Fun, As) when is_function(Fun) ->
%% Fun called from within interpreted code...
case erlang:fun_info(Fun, module) of
%% ... and the fun was defined in interpreted code
{module, ?MODULE} ->
{env, [{Mod,Name},Bs,Cs]} = erlang:fun_info(Fun, env),
{arity, Arity} = erlang:fun_info(Fun, arity),
if
length(As) =:= Arity ->
db_ref(Mod), %% Adds ref between module and process
{Cs,Mod,Name,As,Bs};
true ->
{error,{badarity,{Fun,As}}}
end;
%% ... and the fun was defined outside interpreted code
_ ->
not_interpreted
end.
get_function(Mod, Name, Args, local) ->
Arity = length(Args),
Key = {Mod,Name,Arity},
case cached(Key) of
false ->
DbRef = db_ref(Mod),
case dbg_idb:match_object(DbRef, {{Mod,Name,Arity,'_'},'_'}) of
[{{Mod,Name,Arity,Exp},Clauses}] ->
cache(Key, {Exp,Clauses}),
Clauses;
_ -> undef
end;
{_Exp,Cs} -> Cs
end;
get_function(Mod, Name, Args, extern) ->
Arity = length(Args),
Key = {Mod,Name,Arity},
case cached(Key) of
false ->
case db_ref(Mod) of
not_found -> not_interpreted;
DbRef ->
case dbg_idb:lookup(DbRef, {Mod,Name,Arity,true}) of
{ok,Data} ->
cache(Key, {true,Data}),
Data;
not_found ->
case dbg_idb:lookup(DbRef, module) of
{ok,_} -> undef;
not_found -> not_interpreted
end
end
end;
{true,Cs} -> Cs;
{false,_} -> undef
end.
db_ref(Mod) ->
case get([Mod|db]) of
undefined ->
case dbg_iserver:call(get(int),
{get_module_db, Mod, get(self)}) of
not_found ->
not_found;
ModDb ->
Node = node(get(int)),
DbRef = if
Node =/= node() -> {Node,ModDb};
true -> ModDb
end,
put([Mod|db], DbRef),
DbRef
end;
DbRef ->
DbRef
end.
cache(Key, Data) ->
put(cache, lists:sublist([{Key,Data}|get(cache)], 5)).
cached(Key) ->
case lists:keyfind(Key, 1, get(cache)) of
{Key,Data} -> Data;
false -> false
end.
%% Try to find a matching function clause
%% #ieval.level is set, the other fields must be set in this function
fnk_clauses([{clause,Line,Pars,Gs,Body}|Cs], As, Bs0, Ieval) ->
case head_match(Pars, As, [], Bs0) of
{match,Bs1} ->
Bs = add_bindings(Bs1, Bs0),
case guard(Gs, Bs) of
true ->
seq(Body, Bs, Ieval#ieval{line=Line});
false ->
fnk_clauses(Cs, As, Bs0, Ieval)
end;
nomatch ->
fnk_clauses(Cs, As, Bs0, Ieval)
end;
fnk_clauses([], _As, Bs, Ieval) ->
exception(error, function_clause, Bs, Ieval, true).
seq([E], Bs0, Ieval) ->
case dbg_icmd:cmd(E, Bs0, Ieval) of
{skip,Bs} ->
{value,skipped,Bs};
Bs ->
expr(E, Bs, Ieval)
end;
seq([E|Es], Bs0, Ieval) ->
case dbg_icmd:cmd(E, Bs0, Ieval) of
{skip,Bs} ->
seq(Es, Bs, Ieval);
Bs1 ->
{value,_,Bs} = expr(E, Bs1, Ieval#ieval{top=false}),
seq(Es, Bs, Ieval)
end;
seq([], Bs, _) ->
{value,true,Bs}.
%% Variable
expr({var,Line,V}, Bs, Ieval) ->
case binding(V, Bs) of
{value,Val} ->
{value,Val,Bs};
unbound ->
exception(error, {unbound,V}, Bs, Ieval#ieval{line=Line})
end;
expr({value,_,Val}, Bs, _Ieval) ->
{value,Val,Bs};
expr({value,Val}, Bs, _Ieval) -> % Special case straight values
{value,Val,Bs};
%% List
expr({cons,Line,H0,T0}, Bs0, Ieval0) ->
Ieval = Ieval0#ieval{line=Line,top=false},
{value,H,Bs1} = expr(H0, Bs0, Ieval),
{value,T,Bs2} = expr(T0, Bs0, Ieval),
{value,[H|T],merge_bindings(Bs2, Bs1, Ieval)};
%% Tuple
expr({tuple,Line,Es0}, Bs0, Ieval) ->
{Vs,Bs} = eval_list(Es0, Bs0, Ieval#ieval{line=Line}),
{value,list_to_tuple(Vs),Bs};
%% A block of statements
expr({block,Line,Es},Bs,Ieval) ->
seq(Es, Bs, Ieval#ieval{line=Line});
%% Catch statement
expr({'catch',Line,Expr}, Bs0, Ieval) ->
try expr(Expr, Bs0, Ieval#ieval{line=Line, top=false})
catch
Class:Reason ->
%% Exception caught, reset exit info
put(exit_info, undefined),
dbg_istk:pop(Ieval#ieval.level),
Value = catch_value(Class, Reason),
trace(return, {Ieval#ieval.level,Value}),
{value, Value, Bs0}
end;
%% Try-catch statement
expr({'try',Line,Es,CaseCs,CatchCs,[]}, Bs0, Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
try seq(Es, Bs0, Ieval#ieval{top=false}) of
{value,Val,Bs} = Value ->
case CaseCs of
[] -> Value;
_ ->
case_clauses(Val, CaseCs, Bs, try_clause, Ieval)
end
catch
Class:Reason when CatchCs =/= [] ->
catch_clauses({Class,Reason,[]}, CatchCs, Bs0, Ieval)
end;
expr({'try',Line,Es,CaseCs,CatchCs,As}, Bs0, Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
try seq(Es, Bs0, Ieval#ieval{top=false}) of
{value,Val,Bs} = Value ->
case CaseCs of
[] -> Value;
_ ->
case_clauses(Val, CaseCs, Bs, try_clause, Ieval)
end
catch
Class:Reason when CatchCs =/= [] ->
catch_clauses({Class,Reason,[]}, CatchCs, Bs0, Ieval)
after
seq(As, Bs0, Ieval#ieval{top=false})
end;
%% Case statement
expr({'case',Line,E,Cs}, Bs0, Ieval) ->
{value,Val,Bs} =
expr(E, Bs0, Ieval#ieval{line=Line, top=false}),
case_clauses(Val, Cs, Bs, case_clause, Ieval#ieval{line=Line});
%% If statement
expr({'if',Line,Cs}, Bs, Ieval) ->
if_clauses(Cs, Bs, Ieval#ieval{line=Line});
%% Andalso/orelse
expr({'andalso',Line,E1,E2}, Bs, Ieval) ->
case expr(E1, Bs, Ieval#ieval{line=Line, top=false}) of
{value,false,_}=Res ->
Res;
{value,true,_} ->
expr(E2, Bs, Ieval#ieval{line=Line, top=false});
{value,Val,Bs} ->
exception(error, {badarg,Val}, Bs, Ieval)
end;
expr({'orelse',Line,E1,E2}, Bs, Ieval) ->
case expr(E1, Bs, Ieval#ieval{line=Line, top=false}) of
{value,true,_}=Res ->
Res;
{value,false,_} ->
expr(E2, Bs, Ieval#ieval{line=Line, top=false});
{value,Val,_} ->
exception(error, {badarg,Val}, Bs, Ieval)
end;
%% Matching expression
expr({match,Line,Lhs,Rhs0}, Bs0, Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
{value,Rhs,Bs1} = expr(Rhs0, Bs0, Ieval#ieval{top=false}),
case match(Lhs, Rhs, Bs1) of
{match,Bs} ->
{value,Rhs,Bs};
nomatch ->
exception(error, {badmatch,Rhs}, Bs1, Ieval)
end;
%% Construct a fun
expr({make_fun,Line,Name,Cs}, Bs, #ieval{module=Module}=Ieval) ->
Arity = length(element(3,hd(Cs))),
Info = {Module,Name},
Fun =
case Arity of
0 -> fun() -> eval_fun(Cs, [], Bs, Info) end;
1 -> fun(A) -> eval_fun(Cs, [A], Bs,Info) end;
2 -> fun(A,B) -> eval_fun(Cs, [A,B], Bs,Info) end;
3 -> fun(A,B,C) -> eval_fun(Cs, [A,B,C], Bs,Info) end;
4 -> fun(A,B,C,D) -> eval_fun(Cs, [A,B,C,D], Bs,Info) end;
5 -> fun(A,B,C,D,E) -> eval_fun(Cs, [A,B,C,D,E], Bs,Info) end;
6 -> fun(A,B,C,D,E,F) -> eval_fun(Cs, [A,B,C,D,E,F], Bs,Info) end;
7 -> fun(A,B,C,D,E,F,G) ->
eval_fun(Cs, [A,B,C,D,E,F,G], Bs,Info) end;
8 -> fun(A,B,C,D,E,F,G,H) ->
eval_fun(Cs, [A,B,C,D,E,F,G,H], Bs,Info) end;
9 -> fun(A,B,C,D,E,F,G,H,I) ->
eval_fun(Cs, [A,B,C,D,E,F,G,H,I], Bs,Info) end;
10 -> fun(A,B,C,D,E,F,G,H,I,J) ->
eval_fun(Cs, [A,B,C,D,E,F,G,H,I,J], Bs,Info) end;
11 -> fun(A,B,C,D,E,F,G,H,I,J,K) ->
eval_fun(Cs, [A,B,C,D,E,F,G,H,I,J,K], Bs,Info) end;
12 -> fun(A,B,C,D,E,F,G,H,I,J,K,L) ->
eval_fun(Cs, [A,B,C,D,E,F,G,H,I,J,K,L], Bs,Info) end;
13 -> fun(A,B,C,D,E,F,G,H,I,J,K,L,M) ->
eval_fun(Cs, [A,B,C,D,E,F,G,H,I,J,K,L,M], Bs,Info) end;
14 -> fun(A,B,C,D,E,F,G,H,I,J,K,L,M,N) ->
eval_fun(Cs, [A,B,C,D,E,F,G,H,I,J,K,L,M,N], Bs,Info) end;
15 -> fun(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O) ->
eval_fun(Cs, [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O], Bs,Info) end;
16 -> fun(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P) ->
eval_fun(Cs, [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P], Bs,Info) end;
17 -> fun(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q) ->
eval_fun(Cs, [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q], Bs,Info) end;
18 -> fun(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R) ->
eval_fun(Cs, [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R], Bs,Info) end;
19 -> fun(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S) ->
eval_fun(Cs, [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S],Bs,Info) end;
20 -> fun(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T) ->
eval_fun(Cs, [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T],Bs,Info) end;
_Other ->
exception(error, {'argument_limit',{'fun',Cs}}, Bs,
Ieval#ieval{line=Line})
end,
{value,Fun,Bs};
%% Common test adaptation
expr({call_remote,0,ct_line,line,As0,Lc}, Bs0, Ieval0) ->
{As,_Bs} = eval_list(As0, Bs0, Ieval0),
eval_function(ct_line, line, As, Bs0, extern, Ieval0, Lc);
%% Local function call
expr({local_call,Line,F,As0,Lc}, Bs0, #ieval{module=M} = Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
{As,Bs} = eval_list(As0, Bs0, Ieval),
eval_function(M, F, As, Bs, local, Ieval, Lc);
%% Remote function call
expr({call_remote,Line,M,F,As0,Lc}, Bs0, Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
{As,Bs} = eval_list(As0, Bs0, Ieval),
eval_function(M, F, As, Bs, extern, Ieval, Lc);
%% Emulated semantics of some BIFs
expr({dbg,Line,self,[]}, Bs, #ieval{level=Le}) ->
trace(bif, {Le,Line,erlang,self,[]}),
Self = get(self),
trace(return, {Le,Self}),
{value,Self,Bs};
expr({dbg,Line,get_stacktrace,[]}, Bs, #ieval{level=Le}) ->
trace(bif, {Le,Line,erlang,get_stacktrace,[]}),
Stacktrace = get_stacktrace(),
trace(return, {Le,Stacktrace}),
{value,Stacktrace,Bs};
expr({dbg,Line,raise,As0}, Bs0, #ieval{level=Le}=Ieval0) ->
%% Since erlang:get_stacktrace/0 is emulated, we will
%% need to emulate erlang:raise/3 too so that we can
%% capture the stacktrace.
Ieval = Ieval0#ieval{line=Line},
{[Class,Reason,Stk0]=As,Bs} = eval_list(As0, Bs0, Ieval),
trace(bif, {Le,Line,erlang,raise,As}),
try
%% Evaluate raise/3 for error checking and
%% truncating of the stacktrace to the correct depth.
Error = erlang:raise(Class, Reason, Stk0),
trace(return, {Le,Error}),
{value,Error,Bs}
catch
_:_ ->
Stk = erlang:get_stacktrace(), %Possibly truncated.
StkFun = fun(_) -> Stk end,
do_exception(Class, Reason, StkFun, Bs, Ieval)
end;
expr({dbg,Line,throw,As0}, Bs0, #ieval{level=Le}=Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
{[Term],Bs} = eval_list(As0, Bs0, Ieval),
trace(bif, {Le,Line,erlang,throw,[Term]}),
exception(throw, Term, Bs, Ieval);
expr({dbg,Line,error,As0}, Bs0, #ieval{level=Le}=Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
{[Term],Bs} = eval_list(As0, Bs0, Ieval),
trace(bif, {Le,Line,erlang,error,[Term]}),
exception(error, Term, Bs, Ieval);
expr({dbg,Line,exit,As0}, Bs0, #ieval{level=Le}=Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
{[Term],Bs} = eval_list(As0, Bs0, Ieval),
trace(bif, {Le,Line,erlang,exit,[Term]}),
exception(exit, Term, Bs, Ieval);
%% Call to "safe" BIF, ie a BIF that can be executed in Meta process
expr({safe_bif,Line,M,F,As0}, Bs0, #ieval{level=Le}=Ieval0) ->
Ieval1 = Ieval0#ieval{line=Line},
{As,Bs} = eval_list(As0, Bs0, Ieval1),
trace(bif, {Le,Line,M,F,As}),
Ieval2 = dbg_istk:push(Bs0, Ieval1, false),
Ieval = Ieval2#ieval{module=M,function=F,arguments=As,line=-1},
{_,Value,_} = Res = safe_bif(M, F, As, Bs, Ieval),
trace(return, {Le,Value}),
dbg_istk:pop(),
Res;
%% Call to a BIF that must be evaluated in the correct process
expr({bif,Line,M,F,As0}, Bs0, #ieval{level=Le}=Ieval0) ->
Ieval1 = Ieval0#ieval{line=Line},
{As,Bs} = eval_list(As0, Bs0, Ieval1),
trace(bif, {Le,Line,M,F,As}),
Ieval2 = dbg_istk:push(Bs0, Ieval1, false),
Ieval = Ieval2#ieval{module=M,function=F,arguments=As,line=-1},
{_,Value,_} = Res = debugged_cmd({apply,M,F,As}, Bs, Ieval),
trace(return, {Le,Value}),
dbg_istk:pop(),
Res;
%% Call to an operation
expr({op,Line,Op,As0}, Bs0, Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
{As,Bs} = eval_list(As0, Bs0, Ieval),
try apply(erlang,Op,As) of
Value ->
{value,Value,Bs}
catch
Class:Reason ->
exception(Class, Reason, Bs, Ieval)
end;
%% apply/2 (fun)
expr({apply_fun,Line,Fun0,As0,Lc}, Bs0, #ieval{level=Le}=Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
FunValue = case expr(Fun0, Bs0, Ieval) of
{value,{dbg_apply,Mx,Fx,Asx},Bsx} ->
debugged_cmd({apply,Mx,Fx,Asx},
Bsx, Ieval#ieval{level=Le+1});
OtherFunValue ->
OtherFunValue
end,
case FunValue of
{value,Fun,Bs1} when is_function(Fun) ->
{As,Bs} = eval_list(As0, Bs1, Ieval),
eval_function(undefined, Fun, As, Bs, extern, Ieval, Lc);
{value,{M,F},Bs1} when is_atom(M), is_atom(F) ->
{As,Bs} = eval_list(As0, Bs1, Ieval),
eval_function(M, F, As, Bs, extern, Ieval, Lc);
{value,BadFun,Bs1} ->
exception(error, {badfun,BadFun}, Bs1, Ieval)
end;
%% apply/3
expr({apply,Line,As0,Lc}, Bs0, Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
{[M,F,As],Bs} = eval_list(As0, Bs0, Ieval),
eval_function(M, F, As, Bs, extern, Ieval, Lc);
%% Receive statement
expr({'receive',Line,Cs}, Bs0, #ieval{level=Le}=Ieval) ->
trace(receivex, {Le,false}),
eval_receive(get(self), Cs, Bs0, Ieval#ieval{line=Line});
%% Receive..after statement
expr({'receive',Line,Cs,To,ToExprs}, Bs0, #ieval{level=Le}=Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
{value,ToVal,ToBs} = expr(To, Bs0, Ieval#ieval{top=false}),
trace(receivex, {Le,true}),
check_timeoutvalue(ToVal, ToBs, To, Ieval),
{Stamp,_} = statistics(wall_clock),
eval_receive(get(self), Cs, ToVal, ToExprs, ToBs, Bs0,
0, Stamp, Ieval);
%% Send (!)
expr({send,Line,To0,Msg0}, Bs0, Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
Ieval1 = Ieval#ieval{top=false},
{value,To,Bs1} = expr(To0, Bs0, Ieval1),
{value,Msg,Bs2} = expr(Msg0, Bs0, Ieval1),
Bs = merge_bindings(Bs2, Bs1, Ieval),
eval_send(To, Msg, Bs, Ieval);
%% Binary
expr({bin,Line,Fs}, Bs0, Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
try
eval_bits:expr_grp(Fs, Bs0,
fun (E, B) -> expr(E, B, Ieval) end,
[],
false)
catch
Class:Reason ->
exception(Class, Reason, Bs0, Ieval)
end;
%% List comprehension
expr({lc,_Line,E,Qs}, Bs, Ieval) ->
eval_lc(E, Qs, Bs, Ieval);
expr({bc,_Line,E,Qs}, Bs, Ieval) ->
eval_bc(E, Qs, Bs, Ieval);
%% Brutal exit on unknown expressions/clauses/values/etc.
expr(E, _Bs, _Ieval) ->
erlang:error({'NYI',E}).
%% Interpreted fun() called from uninterpreted module, recurse
eval_fun(Cs, As, Bs, Info) ->
dbg_debugged:eval(?MODULE, eval_fun, [Cs,As,Bs,Info]).
%% eval_lc(Expr,[Qualifier],Bindings,IevalState) ->
%% {value,Value,Bindings}.
%% This is evaluating list comprehensions "straight out of the book".
%% Copied from rv's implementation in erl_eval.
eval_lc(E, Qs, Bs, Ieval) ->
{value,eval_lc1(E, Qs, Bs, Ieval),Bs}.
eval_lc1(E, [{generate,Line,P,L0}|Qs], Bs0, Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
{value,L1,Bs1} = expr(L0, Bs0, Ieval#ieval{top=false}),
CompFun = fun(NewBs) -> eval_lc1(E, Qs, NewBs, Ieval) end,
eval_generate(L1, P, Bs1, CompFun, Ieval);
eval_lc1(E, [{b_generate,Line,P,L0}|Qs], Bs0, Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
{value,Bin,_} = expr(L0, Bs0, Ieval#ieval{top=false}),
CompFun = fun(NewBs) -> eval_lc1(E, Qs, NewBs, Ieval) end,
eval_b_generate(Bin, P, Bs0, CompFun, Ieval);
eval_lc1(E, [{guard,Q}|Qs], Bs0, Ieval) ->
case guard(Q, Bs0) of
true -> eval_lc1(E, Qs, Bs0, Ieval);
false -> []
end;
eval_lc1(E, [Q|Qs], Bs0, Ieval) ->
case expr(Q, Bs0, Ieval#ieval{top=false}) of
{value,true,Bs} -> eval_lc1(E, Qs, Bs, Ieval);
{value,false,_Bs} -> [];
{value,V,Bs} -> exception(error, {bad_filter,V}, Bs, Ieval)
end;
eval_lc1(E, [], Bs, Ieval) ->
{value,V,_} = expr(E, Bs, Ieval#ieval{top=false}),
[V].
%% eval_bc(Expr,[Qualifier],Bindings,IevalState) ->
%% {value,Value,Bindings}.
%% This is evaluating list comprehensions "straight out of the book".
%% Copied from rv's implementation in erl_eval.
eval_bc(E, Qs, Bs, Ieval) ->
Val = erlang:list_to_bitstring(eval_bc1(E, Qs, Bs, Ieval)),
{value,Val,Bs}.
eval_bc1(E, [{generate,Line,P,L0}|Qs], Bs0, Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
{value,L1,Bs1} = expr(L0, Bs0, Ieval#ieval{top=false}),
CompFun = fun(NewBs) -> eval_bc1(E, Qs, NewBs, Ieval) end,
eval_generate(L1, P, Bs1, CompFun, Ieval);
eval_bc1(E, [{b_generate,Line,P,L0}|Qs], Bs0, Ieval0) ->
Ieval = Ieval0#ieval{line=Line},
{value,Bin,_} = expr(L0, Bs0, Ieval#ieval{top=false}),
CompFun = fun(NewBs) -> eval_bc1(E, Qs, NewBs, Ieval) end,
eval_b_generate(Bin, P, Bs0, CompFun, Ieval);
eval_bc1(E, [{guard,Q}|Qs], Bs0, Ieval) ->
case guard(Q, Bs0) of
true -> eval_bc1(E, Qs, Bs0, Ieval);