-
Notifications
You must be signed in to change notification settings - Fork 4
/
error.c
1400 lines (1248 loc) · 36.1 KB
/
error.c
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
/*
* Copyright (c) 1993-2012 David Gay and Gustav Hållberg
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose, without fee, and without written agreement is hereby granted,
* provided that the above copyright notice and the following two paragraphs
* appear in all copies of this software.
*
* IN NO EVENT SHALL DAVID GAY OR GUSTAV HALLBERG BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DAVID GAY OR
* GUSTAV HALLBERG HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* DAVID GAY AND GUSTAV HALLBERG SPECIFICALLY DISCLAIM ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
* "AS IS" BASIS, AND DAVID GAY AND GUSTAV HALLBERG HAVE NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include "mudlle-config.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#define AFTER_SKIP_FRAMES 16
#define BEFORE_SKIP_FRAMES 32
#include "alloc.h"
#include "builtins.h"
#include "context.h"
#include "dwarf.h"
#include "error.h"
#include "global.h"
#include "ins.h"
#include "print.h"
#include "stack.h"
#include "strbuf.h"
#include "utils.h"
#include "runtime/arith.h"
#include "runtime/basic.h"
#include "runtime/mudlle-string.h"
#include "runtime/runtime.h"
#include "runtime/support.h"
const char *const mudlle_errors[] = {
"bad function",
"stack underflow",
"bad type",
"divide by zero",
"bad index",
"bad value",
"variable is read-only",
"function probably has an infinite loop",
"function probably has an infinite recursion",
"wrong number of parameters",
"security violation",
"value is read only",
"user interrupt",
"pattern not matched",
"compilation error",
"abort"
};
CASSERT_VLEN(mudlle_errors, last_runtime_error);
int suppress_extra_calltrace;
static void display_code_location(struct code *code, uint32_t lineno)
{
GCPRO(code);
bool use_fname = !use_nicename || !TYPE(code->nicename, string);
if (use_fname && TYPE(code->nicename, string)
&& !string_equalp(code->nicename, code->filename))
{
pputs(" [", muderr);
pswrite(muderr, code->nicename);
pputc(']', muderr);
}
pputs(" at ", muderr);
pswrite(muderr, use_fname ? code->filename : code->nicename);
pprintf(muderr, ":%lu", (ulong)lineno);
UNGCPRO();
}
static void output_arg(value arg)
{
output_value(muderr, prt_write, arg);
}
static uint32_t get_icode_line(struct call_stack_mudlle *frame)
{
struct icode *code = frame->code;
uint32_t offset = (frame->offset - 1
- ((uint8_t *)(&code->constants[code->nb_constants])
- (uint8_t *)code));
return dwarf_lookup_line_number(&code->code, offset);
}
static void print_arg_name(struct vector *argv, int i)
{
if (argv == NULL)
return;
struct list *e = argv->data[i];
assert(TYPE(e, pair));
struct string *name = e->car;
if (TYPE(name, string))
{
pswrite(muderr, name);
pputc('=', muderr);
}
}
static struct vector *maybe_get_arguments(struct code *code, int nargs)
{
if (code_is_vararg(code))
return NULL;
struct vector *v = code->arguments.argv;
return vector_len(v) == nargs ? v : NULL;
}
static void print_bytecode_frame(struct call_stack *frame, bool onstack)
{
struct call_stack_mudlle *mframe = (struct call_stack_mudlle *)frame;
struct icode *fcode = mframe->code;
GCPRO(fcode);
if (fcode->code.varname)
pswrite(muderr, fcode->code.varname);
else
pputs("<fn>", muderr);
pputc('(', muderr);
struct vector *arguments = maybe_get_arguments(&fcode->code, mframe->nargs);
for (int i = 0; i < mframe->nargs; i++)
{
value v;
/* Warning: This is somewhat intimate with the implementation of
the compiler */
if (onstack)
v = stack_get(mframe->nargs - i - 1);
else
{
struct variable *argi
= mframe->locals->data[mframe->nargs - i - 1];
v = argi->vvalue;
}
if (i > 0)
pputs(", ", muderr);
print_arg_name(arguments, i);
output_arg(v);
}
pputc(')', muderr);
uint32_t lineno = get_icode_line((struct call_stack_mudlle *)frame);
display_code_location(&fcode->code, lineno);
pputc('\n', muderr);
UNGCPRO();
}
static void print_called_value(value called)
{
if (is_any_primitive(called))
pswrite(muderr, ((struct primitive *)called)->op->name);
else if (TYPE(called, closure))
{
struct code *code = ((struct closure *)called)->code;
if (code->varname)
pswrite(muderr, code->varname);
else
pputs("<fn>", muderr);
}
else
output_arg(called);
}
struct c_info {
const struct prim_op *op;
const char *name;
int formal_args, actual_args;
bool is_vararg, is_operator;
};
static void c_frame_info(struct c_info *dst, const struct call_stack *frame)
{
struct call_stack_c_header *cframe = (struct call_stack_c_header *)frame;
const struct prim_op *op;
switch (cframe->s.type)
{
case call_string_args:
case call_string_argv:
*dst = (struct c_info){
.name = cframe->u.name,
.is_vararg = cframe->s.type != call_string_args
};
return;
case call_c:
op = cframe->u.prim->op;
break;
case call_primop:
op = cframe->u.op;
break;
case call_invalid:
case call_invalid_argp:
/* special handling in print_c_frame() */
*dst = (struct c_info){ .name = NULL };
return;
default:
abort();
}
*dst = (struct c_info){
.op = op,
.name = op->name->str,
.is_vararg = op->nargs == NVARARGS,
.is_operator = op->flags & OP_OPERATOR,
.formal_args = op->nargs
};
}
static value get_c_stack_arg(const struct call_stack *frame,
const struct c_info *info, int arg)
{
return stack_get(info->actual_args - arg - 1);
}
static value get_c_vararg_arg(const struct call_stack *frame,
const struct c_info *info, int arg)
{
struct call_stack_c *cframe = (struct call_stack_c *)frame;
int frame_args = cframe->c.nargs - 1;
if (arg < frame_args)
return cframe->args[arg];
int varg = arg - frame_args;
struct vector *argv = cframe->args[frame_args];
return argv->data[varg];
}
static value get_c_argp(const struct call_stack *frame,
const struct c_info *info, int arg)
{
struct call_stack_c_argp *vframe = (struct call_stack_c_argp *)frame;
return vframe->argp[arg];
}
static value get_c_arg(const struct call_stack *frame,
const struct c_info *info, int arg)
{
struct call_stack_c *cframe = (struct call_stack_c *)frame;
return cframe->args[arg];
}
static void print_prim_suffix(const struct prim_op *op,
const struct session_context *next_session)
{
}
static bool print_global_name(value midx)
{
if (!integerp(midx))
return false;
long gidx = intval(midx);
if (gidx <= 0 || gidx >= intval(environment->used))
return false;
pswrite(muderr, GNAME(gidx));
return true;
}
static void print_c_frame(const struct call_stack *frame, bool onstack,
const struct prim_op *last_primop,
const struct session_context *next_session)
{
struct call_stack_c *cframe = (struct call_stack_c *)frame;
struct c_info info;
c_frame_info(&info, frame);
if (last_primop != NULL && info.op == last_primop)
return;
value (*getarg)(const struct call_stack *, const struct c_info *info, int);
info.actual_args = cframe->c.nargs;
if (cframe->c.s.type == call_invalid_argp)
getarg = get_c_argp;
else if (info.is_vararg)
{
assert(!onstack);
int frame_args = cframe->c.nargs - 1;
assert(frame_args >= 0);
struct vector *argv = cframe->args[frame_args];
assert(TYPE(argv, vector));
info.actual_args = frame_args + vector_len(argv);
getarg = get_c_vararg_arg;
}
else
getarg = onstack ? get_c_stack_arg : get_c_arg;
if (info.actual_args == info.formal_args && info.is_operator)
{
bool is_set = false;
switch (info.actual_args)
{
case 1:
if (info.op == global_read_ext)
{
if (!print_global_name(getarg(frame, &info, 0)))
goto not_op;
goto done;
}
pputs(info.op == negate_prim_ext
? "-"
: (info.op == dereference_prim_ext
? "*"
: info.name),
muderr);
output_arg(getarg(frame, &info, 0));
goto done;
case 3:
is_set = info.op == setb_prim_ext;
assert(is_set);
/* fallthrough */
case 2: ;
bool is_gset = info.op == global_write_ext;
bool is_ref = info.op == ref_prim_ext;
bool is_setref = info.op == set_refb_prim_ext;
if (!is_gset)
{
if (is_setref)
pputc('*', muderr);
output_arg(getarg(frame, &info, 0));
}
else if (!print_global_name(getarg(frame, &info, 0)))
goto not_op;
if (!is_setref && !is_gset)
{
if (is_ref || is_set)
pputc('[', muderr);
else
pprintf(muderr, " %s ", info.name);
output_arg(getarg(frame, &info, 1));
if (is_ref || is_set)
pputc(']', muderr);
}
if (is_set || is_setref || is_gset)
{
pputs(" = ", muderr);
output_arg(getarg(frame, &info, is_set ? 2 : 1));
}
goto done;
default:
abort();
}
}
not_op:
if (frame->type == call_invalid || frame->type == call_invalid_argp)
print_called_value(cframe->c.u.value);
else
{
assert(info.name != NULL);
pputs(info.name, muderr);
}
pputc('(', muderr);
const char *prefix = "";
for (int i = 0; i < info.actual_args; ++i)
{
pputs(prefix, muderr);
value arg = getarg(frame, &info, i);
if (i == 0 && info.op != NULL && (info.op->flags & OP_NUL_STR)
&& TYPE(arg, string))
write_nul_string(muderr, arg);
else
output_arg(arg);
prefix = ", ";
}
pputc(')', muderr);
if (info.op != NULL)
print_prim_suffix(info.op, next_session);
done:
pputc('\n', muderr);
}
#ifndef NOCOMPILER
static void print_args(value *args, int nargs, bool nul_str,
struct vector *arguments)
{
bool vararg = nargs < 0;
if (vararg)
nargs = vector_len((struct vector *)*args);
GCPRO(arguments);
pputc('(', muderr);
const char *prefix = "";
for (int n = 0; n < nargs; ++n)
{
pputs(prefix, muderr);
print_arg_name(arguments, n);
value arg = vararg ? ((struct vector *)*args)->data[n] : args[n];
if (n == 0 && nul_str && TYPE(arg, string))
write_nul_string(muderr, arg);
else
output_arg(arg);
prefix = ", ";
}
UNGCPRO();
pputc(')', muderr);
}
/* nargs < 0 means *args points to a gc-protected argument vector */
static void print_mcode(struct mcode *base, ulong ofs, value *args, int nargs,
void *data)
{
/* address is the return address (or pc + 1 for segv) */
if (ofs > 0)
--ofs;
uint32_t line = dwarf_lookup_line_number(&base->code, ofs);
GCPRO(base);
if (base->code.varname)
pswrite(muderr, base->code.varname);
else
pputs("<fn>", muderr);
struct vector *arguments = maybe_get_arguments(&base->code, nargs);
print_args(args, nargs, false, arguments);
pputs(" [c]", muderr);
display_code_location(&base->code, line);
pputc('\n', muderr);
UNGCPRO();
}
static void print_prim(const struct prim_op *op,
const struct session_context *next_session,
value *args, int nargs)
{
pswrite(muderr, op->name);
if (args == NULL)
pputs("(<compiled>)", muderr);
else
print_args(args, nargs, op->flags & OP_NUL_STR, NULL);
print_prim_suffix(op, next_session);
pputc('\n', muderr);
}
static void print_any(value called, value *args, int nargs)
{
print_called_value(called);
print_args(args, nargs, false, NULL);
pputc('\n', muderr);
}
#endif /* !NOCOMPILER */
#if (defined __i386__ || defined __x86_64__) && !defined(NOCOMPILER)
#ifdef __i386__
enum cpu_reg {
x86_reg_eax = 0,
x86_reg_ebx = 3,
x86_reg_ecx = 1,
x86_reg_edx = 2,
x86_reg_esp = 4,
x86_reg_ebp = 5,
x86_reg_esi = 6,
x86_reg_edi = 7,
reg_argcount = x86_reg_eax,
reg_closure_in = x86_reg_edx
};
#elif defined __x86_64__
enum cpu_reg {
x64_reg_rax = 0,
x64_reg_rbx = 3,
x64_reg_rcx = 1,
x64_reg_rdx = 2,
x64_reg_rsp = 4,
x64_reg_rbp = 5,
x64_reg_rsi = 6,
x64_reg_rdi = 7,
x64_reg_r8 = 8,
x64_reg_r9 = 9,
x64_reg_r10 = 10,
x64_reg_r11 = 11,
x64_reg_r12 = 12,
x64_reg_r13 = 13,
x64_reg_r14 = 14,
x64_reg_r15 = 15,
reg_argcount = x64_reg_rax,
reg_arg0 = x64_reg_rdi,
reg_arg1 = x64_reg_rsi,
reg_arg2 = x64_reg_rdx,
reg_closure_in = x64_reg_r10
};
static bool get_imm64_mov(const uint8_t *op, enum cpu_reg reg, int64_t *v)
{
uint8_t rex = 0x48 | ((reg >> 3) & 1);
if (op[0] != rex
|| op[1] != (0xb8 | (reg & 7)))
{
abort();
}
*v = *(int64_t *)(op + 2);
return true;
}
#endif /* __x86_64__ */
static bool get_imm32_mov(const uint8_t *op, enum cpu_reg reg, int32_t *v)
{
assert(reg <= 7); /* not supported (yet) */
if (op[0] != (0xb8 | reg))
{
abort();
}
*v = *(int32_t *)(op + 1);
return true;
}
static void handle_primitive_frame(
ulong pcadr,
const struct prim_op *last_primop,
const struct session_context *next_session,
void (*primfunc)(
const struct prim_op *op,
const struct session_context *next_session,
value *args, int nargs),
void (*anyfunc)(value called, value *args, int nargs),
ulong *last_sp)
{
value *cargs = NULL;
int cnargs = -1;
const uint8_t *op;
ulong primadr;
#ifdef __i386__
/* check for relative call to primitive */
op = (const uint8_t *)(pcadr - 5);
if (op[0] != 0xe8)
return;
primadr = pcadr + *(long *)(op + 1);
#elif defined __x86_64__
/* check for %rip-relative call to primitive */
op = (const uint8_t *)(pcadr - 6);
if (op[0] != 0xff || op[1] != 0x15)
return;
int32_t ofs = *(int32_t *)(op + 2);
primadr = *(ulong *)(pcadr + ofs);
#else
#error Unsupported architecture
#endif
const struct prim_op *prim = NULL;
if (primadr == (ulong)bcall_secure)
{
#ifdef __i386__
const int seclev_bytes = 5; /* mov imm32 */
#elif defined __x86_64__
const int seclev_bytes = 6; /* rex mov imm32 */
#else
#error Unsupported architecture
#endif
/* (x86 only) push args...
* mov $prim,closure_in
* mov $nargs,argcount or xor argcount,argcount
* mov $seclev,%ecx (x86) / %r11 (x86-64)
* call bcall_secure */
bool is_xor = ((const uint16_t *)(op - seclev_bytes))[-1] == 0xc031;
int argc_bytes = is_xor ? 2 : 5;
#ifdef __i386__
int32_t i;
if (is_xor)
i = 0;
else if (!get_imm32_mov(op - seclev_bytes - 5, reg_argcount, &i))
return;
cnargs = i;
cargs = (value *)last_sp;
if (!get_imm32_mov(op - seclev_bytes - argc_bytes - 5,
reg_closure_in, &i))
return;
#elif defined __x86_64__
int64_t i;
if (!get_imm64_mov(op - seclev_bytes - argc_bytes - 10,
reg_closure_in, &i))
return;
#else
#error Unsupported architecture
#endif
prim = ((struct primitive *)i)->op;
}
#ifdef __x86_64__
else if (primadr == (ulong)bcall_prim
|| primadr == (ulong)bcall_prim_noalloc)
{
/* mov callee,closure_in
* call bcall_prim{,_noalloc} */
int64_t i;
if (!get_imm64_mov(op - 10, reg_closure_in, &i))
return;
primadr = i;
}
#endif /* __x86_64__ */
else if (primadr == (ulong)bcall)
{
/* push args...
* mov callee,closure_in
* mov $nargs,argcount or xor argcount,argcount
* call bcall */
if (last_sp[-3] == (ulong)bcall_primitive_tail)
{
/* last_sp: [..0] mudlle args, [-1] mudlle pc, [-2] bp,
[-3] bcall_primitive_tail, [-4] callee,
[-5] argcount */
primadr = last_sp[-4];
cnargs = last_sp[-5];
cargs = (value *)last_sp;
}
}
else if (primadr == (ulong)bapply_varargs)
{
#ifdef __i386__
/* push argvector
* mov callee,%edx
* mov seclev,%ecx
* call bapply_varargs */
/* last_sp: [-1] mudlle pc, [-2] bp, [-3] argvector,
[-4] callee */
primadr = last_sp[-4];
#elif defined __x86_64__
/* push argvector
mov callee,arg0
mov seclev,arg2d
call bapply_varargs */
const int seclev_bytes = 5; /* mov imm32 */
int64_t i64;
if (!get_imm64_mov(op - seclev_bytes - 10, reg_arg0, &i64))
return;
primadr = i64;
#else
#error Unsupported architecture
#endif
cnargs = -1;
cargs = (value *)last_sp;
}
else if (primadr == (ulong)bcall_varargs)
{
#ifdef __i386__
/* push args
* mov callee,%edx
* mov $argcount,%eax
* mov $seclev,%ecx
* call bapply_varargs */
const enum cpu_reg argreg = x86_reg_eax;
const enum cpu_reg calleereg = x86_reg_edx;
const int seclev_bytes = 5; /* mov imm32 */
#elif defined __x86_64__
/* push args
* mov callee,arg0
* mov $argcount,arg1
* mov $seclev,arg2d
* call bapply_varargs */
const enum cpu_reg argreg = reg_arg1;
const enum cpu_reg calleereg = reg_arg0;
const int seclev_bytes = 5; /* mov imm32 */
#else
#error Unsupported architecture
#endif
/* note that argcount is > 1; argcount = 0 ends up as
bapply_varargs */
/* last_sp: [..0] args, [-1] mudlle pc, [-2] bp */
cargs = (value *)last_sp;
int32_t i;
if (!get_imm32_mov(op - seclev_bytes - 5, argreg, &i))
return;
cnargs = i;
#ifdef __i386__
if (!get_imm32_mov(op - seclev_bytes - 2 * 5, calleereg, &i))
return;
primadr = i;
#elif defined __x86_64__
int64_t i64;
if (!get_imm64_mov(op - seclev_bytes - 5 - 10, calleereg, &i64))
return;
primadr = i64;
#else
#error Unsupported architecture
#endif
}
if (prim == NULL)
prim = lookup_primitive(primadr);
if (prim && prim != last_primop)
primfunc(prim, next_session, cargs, cnargs);
}
static void handle_mcode_frame(
ulong pcadr,
void (*func)(struct mcode *, ulong ofs, value *args,
int nargs, void *data),
const struct prim_op *last_primop,
const struct session_context *next_session,
void (*primfunc)(
const struct prim_op *op,
const struct session_context *next_session,
value *args, int nargs),
void (*anyfunc)(value called, value *args, int nargs),
ulong *last_sp, ulong *bp,
int nargs,
void *data)
{
struct mcode *mcode = find_pc_mcode(pcadr, (ulong)gcblock,
(ulong)gcblock + gcblocksize);
if (mcode == NULL)
return;
if (primfunc)
handle_primitive_frame(pcadr, last_primop, next_session, primfunc,
anyfunc, last_sp);
/* args..., pc, *bp */
value *args = (value *)bp + 2;
if (nargs >= 0)
;
else if (code_is_vararg(&mcode->code))
{
/* vararg argument vector is the first spilled local */
args = (value *)bp - 1;
assert(TYPE((struct vector *)*args, vector));
}
else
nargs = vector_len(mcode->code.arguments.argv);
func(mcode, pcadr - (ulong)&mcode->mcode[0], args, nargs, data);
}
static struct ccontext *iterate_cc_frame(
struct ccontext *cc,
void (*func)(struct mcode *mcode, ulong ofs, value *args, int nargs,
void *data),
const struct prim_op *last_primop,
const struct session_context *next_session,
void (*primfunc)(const struct prim_op *op,
const struct session_context *next_session,
value *args, int nargs),
void (*anyfunc)(value called, value *args, int nargs),
int nargs,
void *data)
{
assert(cc->frame_start);
ulong *sp, *bp;
ccontext_frame(cc, &bp, &sp);
/* The return address is at sp[-1] */
handle_mcode_frame(sp[-1], func, last_primop, next_session, primfunc,
anyfunc, sp, bp, nargs, data);
int count = 0;
while (bp < cc->frame_start)
{
if (count++ == BEFORE_SKIP_FRAMES)
{
ulong *frames[AFTER_SKIP_FRAMES];
int i = 0;
while (bp < cc->frame_start && i < AFTER_SKIP_FRAMES)
{
frames[i++] = bp;
bp = (ulong *)bp[0];
}
if (bp >= cc->frame_start)
bp = frames[0];
else
{
i = 0;
while (bp < cc->frame_start)
{
frames[i++ % AFTER_SKIP_FRAMES] = bp;
bp = (ulong *)bp[0];
}
pprintf(muderr, " *** %d frame%s skipped ***\n", i,
i == 1 ? "" : "s");
bp = frames[i % AFTER_SKIP_FRAMES];
}
}
/* bp[<=-1] are locals
* bp[-1] is the vararg vector (if applicable)
* bp[0] is previous bp
* bp[1] is return address
* bp[>=2] are arguments
* sp[0] -> bp[-2] is mudlle values
*/
/* Not using closure because plan on removing it in some cases */
handle_mcode_frame(bp[1], func, NULL, NULL, NULL, NULL, NULL,
(ulong *)(bp[0]), -1, data);
assert(bp[0] > (ulong)bp);
bp = (ulong *)bp[0];
}
assert(bp == cc->frame_start);
return next_ccontext(cc);
}
static struct ccontext *print_cc_frame(
struct ccontext *cc,
const struct prim_op *last_primop,
const struct session_context *next_session,
int nargs)
{
return iterate_cc_frame(cc, print_mcode, last_primop, next_session,
print_prim, print_any, nargs, NULL);
}
#endif /* (__i386__ || __x86_64__) && !NOCOMPILER */
#if !defined(USE_CCONTEXT) && !defined(NOCOMPILER)
static struct ccontext *print_cc_frame(struct ccontext *cc)
{
pputs("<compiled>\n", muderr);
return cc;
}
#endif
static void print_call_trace(enum runtime_error error, bool onstack,
int c_onstack_nargs)
{
struct session_context *session = session_context, *next_session = NULL;
struct catch_context *catch_ctxt = catch_context;
int count = 0;
#ifndef NOCOMPILER
struct ccontext *cc = &ccontext;
#endif
const struct prim_op *last_primop = NULL;
if (error == error_none)
last_primop = warning_prim_ext;
pputs("Call trace is:\n", muderr);
for (struct call_stack *scan = call_stack; scan; scan = scan->next)
{
/* not sure if 'while' is necessary here... */
while (catch_ctxt && scan == catch_ctxt->old_call_stack)
{
if (catch_ctxt->call_trace_mode == call_trace_barrier)
return;
catch_ctxt = catch_ctxt->parent;
}
if (count++ == BEFORE_SKIP_FRAMES)
{
struct call_stack *scans[AFTER_SKIP_FRAMES];
for (int i = 0;
i < AFTER_SKIP_FRAMES && scan;
++i, scan = scan->next)
scans[i] = scan;
if (scan == NULL)
scan = scans[0];
else
{
int i;
for (i = 0; scan; scan = scan->next)
scans[i++ % AFTER_SKIP_FRAMES] = scan;
pprintf(muderr, " *** %d frame%s skipped ***\n", i,
i == 1 ? "" : "s");
scan = scans[i % AFTER_SKIP_FRAMES];
}
}
const struct prim_op *this_primop = NULL;
switch (scan->type)
{
case call_primop:
this_primop = ((struct call_stack_c_header *)scan)->u.op;
/* fallthrough */
case call_string_args:
case call_string_argv:
case call_c:
case call_invalid:
case call_invalid_argp:
print_c_frame(scan, onstack, last_primop, next_session);
break;
case call_bytecode:
print_bytecode_frame(scan, onstack);
break;
case call_compiled:
#ifdef NOCOMPILER
abort();
#else
cc = print_cc_frame(cc, last_primop, next_session, c_onstack_nargs);
break;
#endif
case call_session:
next_session = session;
session = ((struct session_context *)scan)->parent;
break;
}
last_primop = this_primop;
/* Only the first frame can be on the stack */
onstack = false;
c_onstack_nargs = -1;
}
}
struct get_cc_stack_trace_data {
struct vector *vec;
int idx;
bool lines;
};
#ifndef NOCOMPILER
static void count_stack_depth(struct mcode *mcode, ulong ofs, value *args,
int nargs, void *data)
{
++*(int *)data;
}
static void get_cc_stack_trace(struct mcode *mcode, ulong ofs, value *args,
int nargs, void *data)
{
struct get_cc_stack_trace_data *sdata = data;
value v = mcode;
if (sdata->lines)
{
/* address is the return address (or pc + 1 for segv) */
if (ofs > 0)
--ofs;
v = alloc_list(v, makeint(dwarf_lookup_line_number(&mcode->code, ofs)));
}
sdata->vec->data[sdata->idx++] = v;
}
#endif
struct vector *get_mudlle_call_trace(bool lines)
{
#ifndef NOCOMPILER
struct ccontext *cc = &ccontext;
#endif
int depth = 0;
for (struct call_stack *scan = call_stack; scan; scan = scan->next)
{
#ifndef NOCOMPILER
if (scan->type == call_compiled)
{
cc = iterate_cc_frame(cc, count_stack_depth, NULL, NULL, NULL, NULL,
-1, &depth);
continue;
}
#endif
if (scan->type == call_session)
continue;
++depth;
}
struct get_cc_stack_trace_data sdata = {
.vec = alloc_vector(depth),
.idx = 0,
.lines = lines
};
GCPRO(sdata.vec);
#ifndef NOCOMPILER
cc = &ccontext;
#endif
for (struct call_stack *scan = call_stack; scan; scan = scan->next)
{
value v;
switch (scan->type)
{
case call_string_args:
case call_string_argv: