-
Notifications
You must be signed in to change notification settings - Fork 2
/
zm.c
5141 lines (3760 loc) · 113 KB
/
zm.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) 2015-2019, Fabio Sassi <fabio dot s81 at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <zm.h>
/*
* SECTION BASIC_TOOL
* SECTION MT
* SECTION REPORT
* SECTION CORE
*/
size_t zmg_mcounter = 0;
typedef struct {
short by;
short busycheck;
int fromdeep;
int todeep;
int count;
zm_StateQueue *deepstack;
zm_StateQueue *lockstack;
zm_StateQueue *econtinue;
struct {
int count;
zm_Exception *exception;
zm_State *state;
} justlock;
zm_State *chaintail;
zm_State *running;
const char *refname;
const char *filename;
int nline;
} zm_LockAndImplode;
typedef enum {
/* unexpected error */
ZM_FATAL_U1,
/* unexpected second error (in print dump) */
ZM_FATAL_U2,
/* unexpected error in process task */
ZM_FATAL_UP,
/* user code error (generic) */
ZM_FATAL_GCODE,
/* user code error (inside task) */
ZM_FATAL_TCODE,
/* user code error (using yield) */
ZM_FATAL_YCODE
} zm_fatal_t;
static struct {
zm_VM *vm;
zm_Exception *exception;
struct {
const char *reference;
const char *filename;
int nline;
} ucode;
int first;
struct {
zm_fatal_cb fatalcb;
void *data;
} at;
} zmg_err = { NULL, NULL, {NULL, NULL, 0,}, true, {NULL, NULL}};
static struct {
zm_tlock_cb lockcb;
void *data;
} zmg_mutex = {NULL, NULL};
/* indentation-less special prefix char */
#define ZM_ILS "<"
#define ZM_ELOCK_ON 1
#define ZM_ELOCK_OFF 2
#define ZM_ELOCK_REUSE 3
#define zm_enableFlag(s, FLAG) (s)->flag |= FLAG
#define zm_disableFlag(s, FLAG) (s)->flag &= (0xFFFF ^ FLAG)
#define zm_getCurrentState(vm) ((vm)->session.state)
#define zm_getCurrentWorker(vm) ((vm)->session.worker)
#define zm_getCurrentMachine(vm) ((vm)->session.worker->machine)
#define zm_getCurrentMachineName(vm) ((vm)->session.worker->machine->name)
#define zm_workerName(w) ((w)->machine->name)
#if ZM_DEBUG_LEVEL >= 1
#define ZM_D zm_log
#else
#define ZM_D if (0) zm_log
#endif
#define ZM_ASSERT_VMLOCK(errcode, refname, filename, nline) \
if (!vm->plock) { \
ZM_D("vm->plock %d", vm->plock); \
zm_fatalInitAt(vm, refname, filename, nline); \
zm_fatalDo(ZM_FATAL_GCODE, errcode, "operation permitted only " \
"inside a task"); }
/* ----------------------------------------------------------------------------
* DEBUG PRINT UTILITY (SECTION BASIC_TOOL)
* --------------------------------------------------------------------------*/
static void zm_log(const char *fmt, ...)
{
FILE *out = stdout;
va_list args;
fprintf(out, "zmDEBUG - ");
va_start(args, fmt);
vfprintf(out, fmt, args);
va_end(args);
fprintf(out, "\n");
fflush(out);
}
/* ----------------------------------------------------------------------------
* MEMORY UTILITY (SECTION BASIC_TOOL)
* --------------------------------------------------------------------------*/
static void zm_memfatal(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
exit(EXIT_FAILURE);
}
void *zm_malloc(size_t size)
{
void *ptr = malloc(size);
if (!ptr)
zm_memfatal("zm_malloc: out of mem\n");
return ptr;
}
void *zm_mrealloc(void *ptr, size_t size)
{
ptr = realloc(ptr, size);
if (!ptr)
zm_memfatal("zm_mrealloc: out of mem\n");
return ptr;
}
void zm_mfree(size_t size, void *ptr)
{
free(ptr);
}
/* ----------------------------------------------------------------------------
* STATE QUEUE (SECTION BASIC_TOOL)
* --------------------------------------------------------------------------*/
int zm_queueIsEmpty(zm_StateQueue *queue)
{
return queue->first == NULL;
}
zm_StateQueue* zm_queueNew()
{
zm_StateQueue *result = zm_alloc(zm_StateQueue);
result->first = NULL;
result->last = NULL;
return result;
}
void zm_queueFree(zm_StateQueue *q)
{
assert(q->first == NULL);
zm_free(zm_StateQueue, q);
}
zm_StateList* zm_queueAdd(zm_StateQueue* queue, zm_State *s, void *data)
{
zm_StateList *statelist = zm_alloc(zm_StateList);
statelist->state = s;
statelist->next = NULL;
statelist->data = data;
if (!queue->first) {
queue->first = queue->last = statelist;
return statelist;
}
queue->last->next = statelist;
queue->last = statelist;
return statelist;
}
/* pop the first element of the queue (differ from normal pop that remove */
/* last element inserted)*/
zm_StateList* zm_queuePopStateList0(zm_StateQueue* queue)
{
zm_StateList *result;
if (queue->first == NULL)
return NULL;
result = queue->first;
if (queue->last == queue->first) {
queue->first = NULL;
queue->last = NULL;
} else {
queue->first = queue->first->next;
}
return result;
}
/* pop the first element of the queue (differ from normal pop that remove */
/* last element inserted)*/
zm_State* zm_queuePop0(zm_StateQueue* queue, void** data)
{
zm_State *result;
zm_StateList *first;
first = zm_queuePopStateList0(queue);
if (!first)
return NULL;
if (data)
*data = first->data;
result = first->state;
zm_free(zm_StateList, first);
return result;
}
static zm_State* zm_queueFindPop(zm_StateQueue *q, zm_State *s,
int (*match)(zm_State* s, zm_State* cur))
{
zm_StateList *sl = q->first;
zm_StateList *prev = NULL;
if (!sl)
return NULL;
do {
zm_State *c = sl->state;
if ((match) ? match(s, c) : (c == s)) {
zm_State *found = sl->state;
if (prev)
prev->next = sl->next;
else
q->first = sl->next;
zm_free(zm_StateList, sl);
return found;
}
prev = sl;
sl = sl->next;
} while (sl);
return NULL;
}
/* ----------------------------------------------------------------------------
* PRINT FUNCTION (SECTION BASIC_TOOL)
* --------------------------------------------------------------------------*/
void zm_initPrint(zm_Print *p, FILE *stream, int indent)
{
p->file = stream;
p->indent = indent;
}
void zm_setIndent(zm_Print *out, int indent)
{
out->indent = indent;
}
void zm_addIndent(zm_Print *out, int indent)
{
out->indent += indent;
}
/*
* Indent and print. If fmt starts with ZM_ILS the
* indentation is ignored.
*/
void zm_vprint(zm_Print *out, const char *fmt, va_list args)
{
if (fmt[0] != ZM_ILS[0]) {
#if 0
int i;
for (i = 0; i < out->indent; i++)
fprintf(out->file, " ");
#else
if (out->indent > 0)
fprintf(out->file, "%.*s",
((out->indent > 74) ? 74 : out->indent),
" "
" ");
#endif
} else {
fmt++;
}
vfprintf(out->file, fmt, args);
fflush(out->file);
}
/*
* Indent and print (see zm_vprint)
*/
void zm_print(zm_Print *out, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
zm_vprint(out, fmt, args);
va_end(args);
}
/* ----------------------------------------------------------------------------
* MULTI-THREAD SUPPORT (SECTION MT)
* --------------------------------------------------------------------------*/
static void zm_lockOn(FILE *f)
{
if (!zmg_mutex.lockcb)
return;
zmg_mutex.lockcb(f, zmg_mutex.data, true);
}
static void zm_lockOff(FILE *f)
{
if (!zmg_mutex.lockcb)
return;
zmg_mutex.lockcb(f, zmg_mutex.data, false);
}
void zm_enableMT(zm_tlock_cb cb, void* data)
{
zmg_mutex.lockcb = cb;
zmg_mutex.data = data;
}
/* ----------------------------------------------------------------------------
* ERROR REPORTING (SECTION REPORT)
* --------------------------------------------------------------------------*/
static const char* zm_getModeName(zm_State *s, int compact);
void zm_atFatal(zm_fatal_cb cb, void *data)
{
zmg_err.at.fatalcb = cb;
zmg_err.at.data = data;
}
static void zm_fatalLock()
{
zm_lockOn(stderr);
}
static void zm_fatalInit(zm_VM *vm, const char *ref)
{
zm_fatalLock();
zmg_err.vm = vm;
zmg_err.ucode.reference = ref;
zmg_err.ucode.filename = NULL;
zmg_err.ucode.nline = -1;
}
static void zm_fatalInitAt(zm_VM *vm, const char *ref, const char *fn, int nl)
{
zm_fatalLock();
zmg_err.vm = vm;
zmg_err.ucode.reference = ref;
zmg_err.ucode.filename = fn;
zmg_err.ucode.nline = nl;
}
static void zm_fatalInitByLI(zm_VM *vm, zm_LockAndImplode *li)
{
zm_fatalInitAt(vm, li->refname, li->filename, li->nline);
}
static void zm_fatalException(zm_Exception *e)
{
zmg_err.exception = e;
}
static const char* zm_fatalKind(zm_fatal_t kind)
{
switch(kind) {
case ZM_FATAL_U2:
if (!zmg_err.first)
return "Unexpected (while reporting another error)";
case ZM_FATAL_U1:
return "Unexpected";
case ZM_FATAL_UP:
return "Unexpected during process task";
case ZM_FATAL_GCODE:
return "Error";
case ZM_FATAL_YCODE:
return "Error in yield/raise";
case ZM_FATAL_TCODE:
return "Error in task";
default:
return "??UNKNOW FATAL-KIND??";
}
}
static void zm_fatalPrintWhere(zm_Print *out, zm_fatal_t kind)
{
zm_VM *vm = zmg_err.vm;
zm_State *state = (vm) ? zm_getCurrentState(vm) : NULL;
if ((!zmg_err.ucode.reference) && (!zmg_err.ucode.filename) && (!state))
return;
zm_print(out, "Error occured at:");
if (zmg_err.ucode.reference)
zm_print(out, ZM_ILS" %s\n", zmg_err.ucode.reference);
else
zm_print(out, ZM_ILS"\n");
zm_addIndent(out, 4);
if (zmg_err.ucode.filename) {
zm_print(out, "file: %s\n", zmg_err.ucode.filename);
zm_print(out, "line: %d\n", zmg_err.ucode.nline);
}
if ((state) && (kind != ZM_FATAL_U1) && (kind != ZM_FATAL_U2)) {
zm_print(out, "task: %s (kind=%s)\n",
zm_getCurrentMachineName(vm),
zm_isTask(state) ? "ptask" : "subtask");
zm_addIndent(out, 4);
if (state->codeframe.filename) {
zm_print(out, "last yield at: %s line %d\n",
state->codeframe.filename,
state->codeframe.nline);
}
zm_print(out, "resume=%d iter=%d catch=%d\n",
state->on.resume, state->on.iter, state->on.c4tch);
zm_print(out, "pmode=%s\n", zm_getModeName(state, false));
zm_addIndent(out, -4);
}
zm_addIndent(out, -4);
zm_print(out, "\n\n");
}
static void zm_fatalPrintError(zm_fatal_t kind, const char *ecode,
const char *fmt, va_list args)
{
zm_Print out;
zm_initPrint(&out, stderr, 0);
zm_print(&out, "\n[ZM ver %s] ZM FATAL ERROR: (%s)\n %s: ",
ZM_VERSION, ecode,
zm_fatalKind(kind)
);
zm_vprint(&out, fmt, args);
zm_print(&out, "\n\n");
zm_fatalPrintWhere(&out, kind);
if (zmg_err.exception) {
zm_addIndent(&out, 4);
zm_printException(&out, zmg_err.exception, true);
zm_addIndent(&out, -4);
}
}
static int zm_fatalPrintDump(zm_fatal_t kind)
{
switch(kind) {
case ZM_FATAL_U1: break;
case ZM_FATAL_UP: break;
case ZM_FATAL_U2:
case ZM_FATAL_GCODE:
case ZM_FATAL_YCODE:
case ZM_FATAL_TCODE:
default:
return false;
}
if (zmg_err.vm) {
zm_Print out;
zm_initPrint(&out, stderr, 0);
zm_printVM(&out, zmg_err.vm);
return true;
}
return false;
}
static void zm_fatalTrigger()
{
if (zmg_err.at.fatalcb)
zmg_err.at.fatalcb((zmg_err.vm) ? zmg_err.vm->name : NULL,
(zmg_err.vm) ? zmg_err.vm->data : NULL,
zmg_err.at.data);
}
static void zm_fatalDo(zm_fatal_t kind, const char *ecode,
const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
zm_fatalPrintError(kind, ecode, fmt, args);
va_end(args);
if (zmg_err.first) {
/* The vm dump can produce a second fatal of kind unexpected
error if an inconsistency is found. The second fatal must
print error and exit suddenly */
zmg_err.first = false;
zm_fatalTrigger();
if (zm_fatalPrintDump(kind)) {
/* print again the message after the dump */
va_start(args, fmt);
zm_fatalPrintError(kind, ecode, fmt, args);
va_end(args);
}
}
exit(EXIT_FAILURE);
}
/* undefined zmstate */
void zm_fatalNoYield(zm_VM *vm, int out, const char *fn, int nl)
{
int op = zm_getCurrentState(vm)->on.resume;
if (out) {
zm_fatalInitAt(vm, NULL, fn, nl);
zm_fatalDo(ZM_FATAL_TCODE, "UNDEFSTATE.BRK",
"jump over task definition, search "
"for unbound `break` in zmstate %d", op);
} else if (op == 0) {
zm_fatalInitAt(vm, NULL, fn, nl);
zm_fatalDo(ZM_FATAL_YCODE, "UNDEFSTATE.0", "zmstate 0 is "
"reserved");
} else {
zm_fatalInitAt(vm, NULL, fn, nl);
zm_fatalDo(ZM_FATAL_TCODE, "UNDEFSTATE.N",
"reached end of the task definition "
"(zmstate %d not defined or forgive "
"to zmyield/zmraise)",
op);
}
}
/* ----------------------------------------------------------------------------
* DUMP (SECTION REPORT)
* --------------------------------------------------------------------------*/
static zm_Trace* zm_getTraceHead(zm_Trace *t);
static int zm_hasException(zm_State *s, int kind);
static int zm_hasCaller(zm_State *s);
static zm_State* zm_caller(zm_State *s);
static size_t zm_deep(zm_State *sub);
#define ZM_DEFAULT_STDOUT(out) \
zm_Print defaultout; \
if (!out) { \
out = &defaultout; \
zm_initPrint(out, stdout, 0); \
}
#define ZM_STRCASE(x) case x: return #x
static const char *zm_exceptionKind(int kind)
{
switch(kind) {
ZM_STRCASE(ZM_EXCEPTION_ABORT);
ZM_STRCASE(ZM_EXCEPTION_UNCAUGHT);
ZM_STRCASE(ZM_EXCEPTION_CONTINUE);
ZM_STRCASE(ZM_EXCEPTION_CONTINUEHEAD);
ZM_STRCASE(ZM_EXCEPTION_STARTIMPLOSION);
}
return "unknow exception kind";
}
static const char *zm_implodeFlagName(int implodeby)
{
switch (implodeby) {
ZM_STRCASE(ZM_IMPLODEBY_EXCEPTION);
ZM_STRCASE(ZM_IMPLODEBY_SUB);
ZM_STRCASE(ZM_IMPLODEBY_ROOT);
ZM_STRCASE(ZM_IMPLODEBY_CUR);
}
return "unknow implode flag";
}
#if ZM_DEBUG_LEVEL >= 1
static const char *zm_busyCheckFlagName(int busycheck)
{
switch (busycheck) {
ZM_STRCASE(ZM_WSCHECK_ALL);
ZM_STRCASE(ZM_WSCHECK_NONE);
ZM_STRCASE(ZM_WSCHECK_SKIPFIRST);
}
return "unknow busycheck flag";
}
#endif
#define ZM_STRCASE2(m, c) case m: return ((compact) ? (c) : #m)
static const char* zm_getModeName(zm_State *s, int compact)
{
switch (s->pmode) {
ZM_STRCASE2( ZM_PMODE_NORMAL, "ONRM");
ZM_STRCASE2( ZM_PMODE_END, "OEND");
ZM_STRCASE2( ZM_PMODE_CLOSE, "OCLS");
ZM_STRCASE2( ZM_PMODE_OFF, "ONUL");
ZM_STRCASE2( ZM_PMODE_ASYNCIMPLODE, "OIMP");
default:
return (!compact) ? "ZM_PMODE_?????" : "O???";
}
}
static void zm_printTraceElement(zm_Print *out, zm_Trace *t)
{
zm_print(out, "task-id: %zx\t\n", t->taskid);
zm_print(out, "machine: %s", t->machinename);
if (t->on)
zm_print(out, "\t[zmstate: %d]", t->on);
zm_print(out, "\n");
if (t->filename) {
zm_print(out, "filename: %s\n", t->filename);
zm_print(out, "nline: %d\n", t->nline);
}
}
void zm_printTrace(zm_Print *out, zm_Exception *e)
{
zm_Trace *t = e->etrace;
zm_print(out, "Trace: \n");
zm_addIndent(out, 3);
while (t) {
zm_printTraceElement(out, t);
if (t->next)
zm_print(out, "--------------------\n");
t = t->next;
}
zm_addIndent(out, -3);
}
static void zm_printStateException(zm_Print *out, zm_VM *vm, zm_State *estate)
{
zm_Exception *e = estate->exception;
const char *k = NULL;
int usr = true;
int rse = false;
int imp = false;
switch(e->kind) {
case ZM_EXCEPTION_ABORT: k = "abort"; break;
case ZM_EXCEPTION_UNCAUGHT: k = "uncaught"; break;
case ZM_EXCEPTION_CONTINUE: k = "continue"; break;
case ZM_EXCEPTION_CONTINUEHEAD:
k = "(continue2)";
usr = false;
rse = true;
break;
case ZM_EXCEPTION_STARTIMPLOSION:
k = "(implosion-start)";
usr = false;
imp = true;
break;
default:
zm_fatalInit(vm, NULL);
zm_fatalDo(ZM_FATAL_U2, "PRNTEXCEPT.EUN",
"exception->kind = %d", e->kind);
return;
}
zm_print(out, "kind: %s\n", k);
if (usr) {
if (e->msg)
zm_print(out, "msg: \"%s\"\n", e->msg);
else
zm_print(out, "msg: NULL\n");
zm_print(out, "ecode: %d\n", e->code);
zm_print(out, "data: [ref: %zx]\n", e->data);
zm_print(out, "beforecatch: [ref: %zx]\n", e->beforecatch);
}
if (rse) {
zm_addIndent(out, 3);
zm_print(out, "raise state: [ref: %zx]%s", e->raisestate,
(e->raisestate == estate) ? " (self)" : "");
zm_addIndent(out, -3);
}
if (imp) {
zm_print(out, "implosion start: [ref: %zx]\n", e->raisestate);
zm_print(out, "saved exception: [ref: %zx]\n", e->data);
}
if (e->etrace) {
zm_print(out, "\n");
zm_printTrace(out, e);
}
}
#define ZM_CPRINT(c, e) zm_print(out, (compact) ? (ZM_ILS c) : (ZM_ILS e))
static void zm_printFlags(zm_Print* out, zm_State *s, int compact)
{
if (zm_isSubTask(s)) {
zm_print(out, (compact) ? "[sub]" : "(sub)");
} else if (!compact) {
zm_print(out, "(ptask)");
}
if (s->flag & ZM_STATE_RUN) {
ZM_CPRINT("[rn]", "(run)");
} else {
if (s->flag & ZM_STATE_WAITING) {
if (s->flag & ZM_STATE_EVENTLOCKED)
ZM_CPRINT("[we]", "(waiting-event) ");
else
ZM_CPRINT("[ws]", "(waiting-subtask) ");
} else {
if (s->flag & ZM_STATE_EVENTLOCKED)
/* #EVB_FLAG*/
ZM_CPRINT("[evb]", "(event-binded) ");
else
ZM_CPRINT("[su]", "(suspend) ");
}
}
if (s->flag & ZM_STATE_IMPLOSIONLOCK)
ZM_CPRINT("[IL]", "(implosion lock) ");
if (s->flag & ZM_STATE_AUTOFREE)
/*ZM_CPRINT("[af]", "(autofree) ");*/
ZM_CPRINT("", "(autofree) ");
if (s->flag & ZM_STATE_CATCH)
ZM_CPRINT("[ca]", "(catch) ");
if (s->flag & ZM_STATE_CONTINUEMARK)
ZM_CPRINT("[cm]", "(c-mark) ");
if (s->flag & ZM_STATE_UNUSED)
ZM_CPRINT("[??]", "( unknow ??? ) ");
}
#undef ZM_CPRINT
void zm_printStateCompact(zm_Print *out, zm_State *s)
{
ZM_DEFAULT_STDOUT(out);
#ifdef ZM_DEBUG_MACHINENAME
zm_print(out, "%s-%zx ", s->debugmachinename, s);
#else
zm_print(out, "%zx ", s);
#endif
zm_printFlags(out, s, true);
zm_print(out, ZM_ILS" ");
/*zm_print(out, "on:%d|%d|%d", s->on.resume, s->on.iter,s->on.c4tch);*/
if (zm_hasCaller(s))
zm_print(out, ZM_ILS"caller:%zx ", zm_caller(s));
if (s->pmode != ZM_PMODE_NORMAL)
zm_print(out, ZM_ILS"%s ", zm_getModeName(s, true));
if (s->exception)
zm_print(out, ZM_ILS"!%zx", s->exception);
zm_print(out, ZM_ILS"\n");
}
void zm_printState(zm_Print *out, zm_VM *vm, zm_State *s)
{
ZM_DEFAULT_STDOUT(out);
zm_print(out, "flag: %d ", s->flag);
zm_printFlags(out, s, false);
zm_print(out, (zm_getCurrentState(vm) == s) ? "(now)\n" : "\n");
if (zm_isSubTask(s)) {
size_t i;
zm_print(out, "caller: [ref: %zx]\n", zm_caller(s));
zm_print(out, "parent: (subtask) stacksize = %d\n",zm_deep(s));
for (i = 0; i < zm_deep(s); i++) {
zm_print(out, " parent[%d] = [ref: %zx]\n", i,
s->parent->stack[i]);
}
} else {
zm_print(out, "parent: NULL (ptask)\n");
}
zm_print(out, "subtasks: [ref: %zx]\n", s->subtasks);
zm_print(out, "siblings: prev=[ref: %zx], next=[ref: %zx]\n",
s->siblings.prev, s->siblings.next);
zm_print(out, "on: resume = %d, iter = %d, catch = %d\n",
s->on.resume, s->on.iter, s->on.c4tch);
if (s->pmode != ZM_PMODE_NORMAL)
zm_print(out, "pmode: %s\n", zm_getModeName(s, false));
#ifdef ZM_DEBUG_MACHINENAME
zm_print(out, "machine: %s\n", s->debugmachinename);
#endif
zm_print(out, "next: ");
if (s->flag & ZM_STATE_RUN) {
zm_print(out, ZM_ILS"(state)");
} else {
if (s->flag & ZM_STATE_EVENTLOCKED) {
zm_EventBinder* evb = (zm_EventBinder*)s->next;
zm_print(out, ZM_ILS"(eventbinder->worker: %s)",
zm_workerName((zm_Worker*)evb->statenext));
} else {
zm_print(out, ZM_ILS"(saved worker: %s)",
zm_workerName((zm_Worker*)s->next));
}
}
zm_print(out, ZM_ILS" [ref: %zx]\n", s->next);