forked from erkyrath/glulxe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugger.c
1770 lines (1545 loc) · 55.5 KB
/
debugger.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
/* debugger.c: Glulxe debugger functions.
Designed by Andrew Plotkin <[email protected]>
http://eblong.com/zarf/glulx/index.html
*/
/* This module is a low-level debugger for Inform 6 (and 7) games.
This code is only compiled in if the "#define VM_DEBUGGER" line in
glulxe.h is uncommented. You will then have to compile with a Glk
library that supports the debug feature. (See gi_debug.h as distributed
with CheapGlk.) You will also need to link the libxml2 library.
When debugging is compiled in, glulxe looks for a "Dbug" chunk in the
Blorb file. This should contain the "gameinfo.dbg" file generated by
the I6 compiler. (Must be the new XML-based debug format.) It also
offers a bunch of new command-line options:
--gameinfo filename: Read the "gameinfo.dbg" from an external file.
(If you're not using Blorb or have not pulled the debug info
into it.)
--cpu: Display time and VM CPU cycles used for each input.
--starttrap: Enter debug mode as soon as the game starts up. (Before
any code has executed.)
--quittrap: Enter debug mode when the game quits.
--crashtrap: Enter debug mode if any fatal error occurs (including
Glk library fatal errors).
You do not need "gameinfo.dbg" data to use the debugger. However, that
file contains the I6-level names of all functions and variables. So
without it, you'll see a lot of "???" in the debug output.
The debug model assumes that you have a "debug console" available, which
can accept debug commands and display responses. (The implementation
of this depends on the Glk library.) You can enter commands whenever
the game is awaiting input, or if it pauses because of the --starttrap
flag, the --crashtrap flag, a breakpoint, or a @debugtrap opcode.
The debugger is currently pretty minimal. You can display the stack
(including local variables), display global variables, and set
breakpoints (only at the beginning of a function). Type "help" into
the debug console for the command list.
(If you've compiled with CheapGlk, then the "debug console" is just
standard output. Enter a line beginning with "/" to send a debug
command -- "/help", etc.) (This "/" convention is *only* used in
CheapGlk. Other libraries will have debug interfaces that don't
suck.)
*/
#include "glk.h"
#include "glulxe.h"
#if VM_DEBUGGER
#include <string.h>
#include <sys/time.h>
#include "gi_debug.h"
#include <libxml/xmlreader.h>
/* Data structures to store the debug info in memory. */
typedef enum grouptype_enum {
grp_None = 0,
grp_Constant = 1,
grp_Attribute = 2,
grp_Property = 3,
grp_Routine = 4,
grp_Global = 5,
grp_Object = 6,
grp_Array = 7,
} grouptype;
/* Used for constants, globals, locals, objects -- the meaning of the value
field varies. */
typedef struct infoconstant_struct {
const xmlChar *identifier;
int32_t value;
} infoconstant;
typedef struct inforoutine_struct {
const xmlChar *identifier;
int32_t address;
int32_t length;
/* Address of the next higher function. May be beyond length if there
are gaps. */
int32_t nextaddress;
/* The locals is a block of infoconstants where the value is
frame-offset. We adopt Inform's assumption that locals are
always 4 bytes long. */
int32_t numlocals;
infoconstant *locals;
} inforoutine;
typedef struct infoobject_struct {
const xmlChar *identifier;
int32_t address;
/* Address of the next higher function. May be beyond length if there
are gaps. */
int32_t nextaddress;
} infoobject;
typedef struct infoarray_struct {
const xmlChar *identifier;
int32_t address;
int32_t bytelength;
int32_t bytesize; /* per element */
int32_t count;
int lengthfield; /* true if the zeroth element is the length */
/* Address of the next higher function. May be beyond length if there
are gaps. */
int32_t nextaddress;
} infoarray;
typedef struct breakpoint_struct {
inforoutine *func;
int32_t address;
struct breakpoint_struct *next; /* linked list of breakpoints */
} breakpoint;
typedef struct debuginfofile_struct {
strid_t str;
int32_t strread;
int32_t strreadmax;
int failed;
grouptype curgrouptype;
int tempcounter;
infoconstant *tempconstant;
inforoutine *temproutine;
infoarray *temparray;
infoobject *tempobject;
int tempnumlocals;
int templocalssize;
infoconstant *templocals;
const xmlChar *storyfileprefix;
xmlHashTablePtr constants;
xmlHashTablePtr attributes;
xmlHashTablePtr properties;
xmlHashTablePtr globals;
xmlHashTablePtr objects;
int numobjects;
infoobject **objectlist; /* array, ordered by address */
xmlHashTablePtr arrays;
int numarrays;
infoarray **arraylist; /* array, ordered by address */
xmlHashTablePtr routines;
int numroutines;
inforoutine **routinelist; /* array, ordered by address */
} debuginfofile;
/* This global holds the loaded debug info, if we have any. */
static debuginfofile *debuginfo = NULL;
/* Lists of breakpoints. */
static breakpoint *funcbreakpoints = NULL; /* linked list */
/* Internal functions used while loading the debug info. */
static int xmlreadstreamfunc(void *rock, char *buffer, int len);
static int xmlreadchunkfunc(void *rock, char *buffer, int len);
static int xmlclosefunc(void *rock);
static void xmlhandlenode(xmlTextReaderPtr reader, debuginfofile *context);
static int finalize_debuginfo(debuginfofile *context);
static debuginfofile *create_debuginfofile(void)
{
debuginfofile *context = (debuginfofile *)malloc(sizeof(debuginfofile));
context->str = NULL;
context->failed = 0;
context->tempconstant = NULL;
context->temparray = NULL;
context->tempobject = NULL;
context->temproutine = NULL;
context->tempnumlocals = 0;
context->templocals = NULL;
context->templocalssize = 0;
context->curgrouptype = grp_None;
context->constants = xmlHashCreate(16);
context->attributes = xmlHashCreate(16);
context->properties = xmlHashCreate(16);
context->globals = xmlHashCreate(16);
context->objects = xmlHashCreate(16);
context->numobjects = 0;
context->objectlist = NULL;
context->arrays = xmlHashCreate(16);
context->numarrays = 0;
context->arraylist = NULL;
context->routines = xmlHashCreate(16);
context->numroutines = 0;
context->routinelist = NULL;
context->storyfileprefix = NULL;
return context;
}
static void free_debuginfofile(debuginfofile *context)
{
if (!context)
return;
context->str = NULL;
context->tempconstant = NULL;
context->temparray = NULL;
context->tempobject = NULL;
context->temproutine = NULL;
/* We don't bother to free the member structures, because this
only happens once at startup and then only on error
conditions. */
if (context->constants) {
xmlHashFree(context->constants, NULL);
context->constants = NULL;
}
if (context->attributes) {
xmlHashFree(context->attributes, NULL);
context->attributes = NULL;
}
if (context->properties) {
xmlHashFree(context->properties, NULL);
context->properties = NULL;
}
if (context->globals) {
xmlHashFree(context->globals, NULL);
context->globals = NULL;
}
if (context->objects) {
xmlHashFree(context->objects, NULL);
context->objects = NULL;
}
if (context->objectlist) {
free(context->objectlist);
context->objectlist = NULL;
}
if (context->arrays) {
xmlHashFree(context->arrays, NULL);
context->arrays = NULL;
}
if (context->arraylist) {
free(context->arraylist);
context->arraylist = NULL;
}
if (context->routines) {
xmlHashFree(context->routines, NULL);
context->routines = NULL;
}
if (context->routinelist) {
free(context->routinelist);
context->routinelist = NULL;
}
free(context);
}
static infoconstant *create_infoconstant(void)
{
infoconstant *cons = (infoconstant *)malloc(sizeof(infoconstant));
cons->identifier = NULL;
cons->value = 0;
return cons;
}
static infoobject *create_infoobject(void)
{
infoobject *object = (infoobject *)malloc(sizeof(infoobject));
object->identifier = NULL;
object->address = 0;
object->nextaddress = 0;
return object;
}
static infoarray *create_infoarray(void)
{
infoarray *arr = (infoarray *)malloc(sizeof(infoarray));
arr->identifier = NULL;
arr->address = 0;
arr->bytelength = 0;
arr->bytesize = 1;
arr->count = 0;
arr->lengthfield = 0;
arr->nextaddress = 0;
return arr;
}
static inforoutine *create_inforoutine(void)
{
inforoutine *func = (inforoutine *)malloc(sizeof(inforoutine));
func->identifier = NULL;
func->address = 0;
func->length = 0;
func->nextaddress = 0;
func->numlocals = 0;
func->locals = NULL;
return func;
}
static breakpoint *create_breakpoint(glui32 addr)
{
breakpoint *bp = (breakpoint *)malloc(sizeof(breakpoint));
bp->func = NULL; /* useful? */
bp->address = addr;
bp->next = NULL;
return bp;
}
static void add_object_to_table(void *obj, void *rock, const xmlChar *name)
{
debuginfofile *context = rock;
infoobject *object = obj;
if (context->tempcounter >= context->numobjects) {
printf("### object overflow!\n"); /*###*/
return;
}
context->objectlist[context->tempcounter++] = object;
}
static int sort_objects_table(const void *obj1, const void *obj2)
{
infoobject **object1 = (infoobject **)obj1;
infoobject **object2 = (infoobject **)obj2;
return ((*object1)->address - (*object2)->address);
}
static int find_object_in_table(const void *keyptr, const void *obj)
{
/* Binary-search callback. We rely on address and nextaddress so
that there are no gaps. */
glui32 addr = *(glui32 *)(keyptr);
infoobject **object = (infoobject **)obj;
if (addr < (*object)->address)
return -1;
if (addr >= (*object)->nextaddress)
return 1;
return 0;
}
static void add_array_to_table(void *obj, void *rock, const xmlChar *name)
{
debuginfofile *context = rock;
infoarray *array = obj;
if (context->tempcounter >= context->numarrays) {
printf("### array overflow!\n"); /*###*/
return;
}
context->arraylist[context->tempcounter++] = array;
}
static int sort_arrays_table(const void *obj1, const void *obj2)
{
infoarray **array1 = (infoarray **)obj1;
infoarray **array2 = (infoarray **)obj2;
return ((*array1)->address - (*array2)->address);
}
static int find_array_in_table(const void *keyptr, const void *obj)
{
/* Binary-search callback. We rely on address and nextaddress so
that there are no gaps. */
glui32 addr = *(glui32 *)(keyptr);
infoarray **array = (infoarray **)obj;
if (addr < (*array)->address)
return -1;
if (addr >= (*array)->nextaddress)
return 1;
return 0;
}
static void add_routine_to_table(void *obj, void *rock, const xmlChar *name)
{
debuginfofile *context = rock;
inforoutine *routine = obj;
if (context->tempcounter >= context->numroutines) {
printf("### array overflow!\n"); /*###*/
return;
}
context->routinelist[context->tempcounter++] = routine;
}
static int sort_routines_table(const void *obj1, const void *obj2)
{
inforoutine **routine1 = (inforoutine **)obj1;
inforoutine **routine2 = (inforoutine **)obj2;
return ((*routine1)->address - (*routine2)->address);
}
static int find_routine_in_table(const void *keyptr, const void *obj)
{
/* Binary-search callback. We rely on address and nextaddress so
that there are no gaps. */
glui32 addr = *(glui32 *)(keyptr);
inforoutine **routine = (inforoutine **)obj;
if (addr < (*routine)->address)
return -1;
if (addr >= (*routine)->nextaddress)
return 1;
return 0;
}
/* Top-level function for loading debug info from a Glk stream.
The debug data must take up the entire file; this will read until EOF.
If successful, fills out the debuginfo global and returns true.
If not, reports an error and returns false.
(The stream will not be closed.)
*/
int debugger_load_info_stream(strid_t stream)
{
xmlTextReaderPtr reader;
debuginfofile *context = create_debuginfofile();
context->str = stream;
context->strread = 0; /* not used */
context->strreadmax = 0; /* not used */
reader = xmlReaderForIO(xmlreadstreamfunc, xmlclosefunc, context,
NULL, NULL,
XML_PARSE_RECOVER|XML_PARSE_NOENT|XML_PARSE_NONET|XML_PARSE_NOCDATA|XML_PARSE_COMPACT);
if (!reader) {
printf("Error: Unable to create XML reader.\n"); /*###*/
free_debuginfofile(context);
return 0;
}
while (1) {
int res = xmlTextReaderRead(reader);
if (res < 0) {
context->failed = 1;
break; /* error */
}
if (res == 0) {
break; /* EOF */
}
xmlhandlenode(reader, context);
}
xmlFreeTextReader(reader);
context->str = NULL; /* the reader didn't close it, but we're done with it. */
if (context->failed) {
printf("Error: Unable to load debug info.\n"); /*###*/
free_debuginfofile(context);
return 0;
}
/* Now that all the data is loaded in, we go through and create some
indexes that will be handy. */
return finalize_debuginfo(context);
}
/* Top-level function for loading debug info from a segment of a Glk stream.
This starts at position pos in the file and reads len bytes.
If successful, fills out the debuginfo global and returns true.
If not, reports an error and returns false.
(The stream will not be closed.)
*/
int debugger_load_info_chunk(strid_t stream, glui32 pos, glui32 len)
{
xmlTextReaderPtr reader;
debuginfofile *context = create_debuginfofile();
context->str = stream;
context->strread = 0;
context->strreadmax = len;
glk_stream_set_position(stream, pos, seekmode_Start);
reader = xmlReaderForIO(xmlreadchunkfunc, xmlclosefunc, context,
NULL, NULL,
XML_PARSE_RECOVER|XML_PARSE_NOENT|XML_PARSE_NONET|XML_PARSE_NOCDATA|XML_PARSE_COMPACT);
if (!reader) {
printf("Error: Unable to create XML reader.\n"); /*###*/
free_debuginfofile(context);
return 0;
}
while (1) {
int res = xmlTextReaderRead(reader);
if (res < 0) {
context->failed = 1;
break; /* error */
}
if (res == 0) {
break; /* EOF */
}
xmlhandlenode(reader, context);
}
xmlFreeTextReader(reader);
context->str = NULL; /* the reader didn't close it, but we're done with it. */
if (context->failed) {
printf("Error: Unable to load debug info.\n"); /*###*/
free_debuginfofile(context);
return 0;
}
/* Now that all the data is loaded in, we go through and create some
indexes that will be handy. */
return finalize_debuginfo(context);
}
/* xmlReader callback to read from a stream (until EOF). */
static int xmlreadstreamfunc(void *rock, char *buffer, int len)
{
debuginfofile *context = rock;
int res = glk_get_buffer_stream(context->str, buffer, len);
if (res < 0)
return -1;
return res;
}
/* xmlReader callback to read from a stream (until a given position). */
static int xmlreadchunkfunc(void *rock, char *buffer, int len)
{
int res;
debuginfofile *context = rock;
if (context->strread >= context->strreadmax)
return 0;
if (len > context->strreadmax - context->strread)
len = context->strreadmax - context->strread;
res = glk_get_buffer_stream(context->str, buffer, len);
if (res < 0)
return -1;
context->strread += res;
return res;
}
/* xmlReader callback to stop reading a stream. We don't actually close
the stream here, just discard the reference. */
static int xmlclosefunc(void *rock)
{
debuginfofile *context = rock;
context->str = NULL;
return 0;
}
/* xmlReader callback: an XML node has arrived. (Might be the beginning of
a tag, the end of a tag, or a block of text.)
All the work of parsing the debug format happens here, which is why this
function is big and ugly.
*/
static void xmlhandlenode(xmlTextReaderPtr reader, debuginfofile *context)
{
int depth = xmlTextReaderDepth(reader);
int nodetype = xmlTextReaderNodeType(reader);
if (depth == 0) {
if (nodetype == XML_ELEMENT_NODE) {
const xmlChar *name = xmlTextReaderConstName(reader);
if (xmlStrcmp(name, BAD_CAST "inform-story-file")) {
printf("Error: This is not an Inform debug info file.\n"); /*###*/
context->failed = 1;
}
}
else if (nodetype == XML_ELEMENT_DECL) {
/* End of document */
context->curgrouptype = grp_None;
context->tempconstant = NULL;
context->temparray = NULL;
context->tempobject = NULL;
context->temproutine = NULL;
}
}
else if (depth == 1) {
if (nodetype == XML_ELEMENT_NODE) {
const xmlChar *name = xmlTextReaderConstName(reader);
if (!xmlStrcmp(name, BAD_CAST "constant")) {
context->curgrouptype = grp_Constant;
context->tempconstant = create_infoconstant();
}
else if (!xmlStrcmp(name, BAD_CAST "attribute")) {
context->curgrouptype = grp_Attribute;
context->tempconstant = create_infoconstant();
}
else if (!xmlStrcmp(name, BAD_CAST "property")) {
context->curgrouptype = grp_Property;
context->tempconstant = create_infoconstant();
}
else if (!xmlStrcmp(name, BAD_CAST "routine")) {
context->curgrouptype = grp_Routine;
context->temproutine = create_inforoutine();
context->tempnumlocals = 0;
}
else if (!xmlStrcmp(name, BAD_CAST "global-variable")) {
context->curgrouptype = grp_Global;
context->tempconstant = create_infoconstant();
}
else if (!xmlStrcmp(name, BAD_CAST "object")) {
context->curgrouptype = grp_Object;
context->tempobject = create_infoobject();
}
else if (!xmlStrcmp(name, BAD_CAST "array")) {
context->curgrouptype = grp_Array;
context->temparray = create_infoarray();
}
else if (!xmlStrcmp(name, BAD_CAST "story-file-prefix")) {
xmlNodePtr nod = xmlTextReaderExpand(reader);
if (nod && nod->children && nod->children->type == XML_TEXT_NODE) {
context->storyfileprefix = xmlStrdup(nod->children->content);
}
}
else {
context->curgrouptype = grp_None;
}
}
else if (nodetype == XML_ELEMENT_DECL) {
/* End of group */
switch (context->curgrouptype) {
case grp_Constant:
if (context->tempconstant) {
infoconstant *dat = context->tempconstant;
context->tempconstant = NULL;
xmlHashAddEntry(context->constants, dat->identifier, dat);
}
break;
case grp_Attribute:
if (context->tempconstant) {
infoconstant *dat = context->tempconstant;
context->tempconstant = NULL;
xmlHashAddEntry(context->attributes, dat->identifier, dat);
}
break;
case grp_Property:
if (context->tempconstant) {
infoconstant *dat = context->tempconstant;
context->tempconstant = NULL;
xmlHashAddEntry(context->properties, dat->identifier, dat);
}
break;
case grp_Global:
if (context->tempconstant) {
infoconstant *dat = context->tempconstant;
context->tempconstant = NULL;
xmlHashAddEntry(context->globals, dat->identifier, dat);
}
break;
case grp_Object:
if (context->tempobject) {
infoobject *dat = context->tempobject;
context->tempobject = NULL;
xmlHashAddEntry(context->objects, dat->identifier, dat);
}
break;
case grp_Array:
if (context->temparray) {
infoarray *dat = context->temparray;
context->temparray = NULL;
dat->count = dat->bytelength / dat->bytesize;
xmlHashAddEntry(context->arrays, dat->identifier, dat);
}
break;
case grp_Routine:
if (context->temproutine) {
inforoutine *dat = context->temproutine;
context->temproutine = NULL;
/* Copy the list of locals into the inforoutine structure.
This loop totally assumes that locals are found
in order in the debug file! */
if (context->tempnumlocals && context->templocals) {
int ix;
dat->numlocals = context->tempnumlocals;
dat->locals = (infoconstant *)malloc(context->tempnumlocals * sizeof(infoconstant));
for (ix=0; ix<context->tempnumlocals; ix++) {
dat->locals[ix].identifier = context->templocals[ix].identifier;
dat->locals[ix].value = context->templocals[ix].value;
context->templocals[ix].identifier = NULL;
context->templocals[ix].value = 0;
}
}
context->tempnumlocals = 0;
/* An address of 0 is impossible. Inform uses this to
indicate a routine that was eliminated because it was
never called. */
if (dat->address != 0) {
xmlHashAddEntry(context->routines, dat->identifier, dat);
}
}
break;
default:
break;
}
context->curgrouptype = grp_None;
}
}
else {
if (nodetype == XML_ELEMENT_NODE) {
const xmlChar *name = xmlTextReaderConstName(reader);
/* These fields are always simple text nodes. */
if (!xmlStrcmp(name, BAD_CAST "identifier")) {
xmlNodePtr nod = xmlTextReaderExpand(reader);
if (nod && nod->children && nod->children->type == XML_TEXT_NODE) {
xmlChar *text = nod->children->content;
if (context->curgrouptype == grp_Constant
|| context->curgrouptype == grp_Attribute
|| context->curgrouptype == grp_Property
|| context->curgrouptype == grp_Global) {
if (context->tempconstant) {
if (depth == 2)
context->tempconstant->identifier = xmlStrdup(text);
}
}
else if (context->curgrouptype == grp_Object) {
if (context->tempobject) {
if (depth == 2)
context->tempobject->identifier = xmlStrdup(text);
}
}
else if (context->curgrouptype == grp_Array) {
if (context->temparray) {
if (depth == 2)
context->temparray->identifier = xmlStrdup(text);
}
}
else if (context->curgrouptype == grp_Routine) {
if (context->temproutine) {
if (depth == 2)
context->temproutine->identifier = xmlStrdup(text);
}
}
}
}
else if (!xmlStrcmp(name, BAD_CAST "value")) {
xmlNodePtr nod = xmlTextReaderExpand(reader);
if (nod && nod->children && nod->children->type == XML_TEXT_NODE) {
if (context->curgrouptype == grp_Constant
|| context->curgrouptype == grp_Attribute
|| context->curgrouptype == grp_Property) {
if (context->tempconstant) {
if (depth == 2)
context->tempconstant->value = strtol((char *)nod->children->content, NULL, 10);
}
}
else if (context->curgrouptype == grp_Object) {
if (context->tempobject) {
if (depth == 2)
context->tempobject->address = strtol((char *)nod->children->content, NULL, 10);
}
}
else if (context->curgrouptype == grp_Array) {
if (context->temparray) {
if (depth == 2)
context->temparray->address = strtol((char *)nod->children->content, NULL, 10);
}
}
else if (context->curgrouptype == grp_Routine) {
if (context->temproutine) {
if (depth == 2)
context->temproutine->address = strtol((char *)nod->children->content, NULL, 10);
}
}
}
}
else if (!xmlStrcmp(name, BAD_CAST "address")) {
xmlNodePtr nod = xmlTextReaderExpand(reader);
if (nod && nod->children && nod->children->type == XML_TEXT_NODE) {
if (context->curgrouptype == grp_Global) {
if (context->tempconstant) {
if (depth == 2)
context->tempconstant->value = strtol((char *)nod->children->content, NULL, 10);
}
}
}
}
else if (!xmlStrcmp(name, BAD_CAST "byte-count")) {
xmlNodePtr nod = xmlTextReaderExpand(reader);
if (nod && nod->children && nod->children->type == XML_TEXT_NODE) {
if (context->curgrouptype == grp_Routine) {
if (context->temproutine) {
if (depth == 2)
context->temproutine->length = strtol((char *)nod->children->content, NULL, 10);
}
}
else if (context->curgrouptype == grp_Array) {
if (context->temparray) {
if (depth == 2)
context->temparray->bytelength = strtol((char *)nod->children->content, NULL, 10);
}
}
}
}
else if (!xmlStrcmp(name, BAD_CAST "bytes-per-element")) {
xmlNodePtr nod = xmlTextReaderExpand(reader);
if (nod && nod->children && nod->children->type == XML_TEXT_NODE) {
if (context->curgrouptype == grp_Array) {
if (context->temparray) {
if (depth == 2)
context->temparray->bytesize = strtol((char *)nod->children->content, NULL, 10);
}
}
}
}
else if (!xmlStrcmp(name, BAD_CAST "zeroth-element-holds-length")) {
xmlNodePtr nod = xmlTextReaderExpand(reader);
if (nod && nod->children && nod->children->type == XML_TEXT_NODE) {
if (context->curgrouptype == grp_Array) {
if (context->temparray) {
if (depth == 2)
if (!xmlStrcmp(nod->children->content, BAD_CAST "true"))
context->temparray->lengthfield = 1;
}
}
}
}
else if (!xmlStrcmp(name, BAD_CAST "local-variable")) {
xmlNodePtr nod = xmlTextReaderExpand(reader);
if (nod) {
infoconstant *templocal;
if (!context->templocals) {
context->templocalssize = 8;
context->templocals = (infoconstant *)malloc(context->templocalssize * sizeof(infoconstant));
}
if (context->tempnumlocals >= context->templocalssize) {
context->templocalssize = 2*context->tempnumlocals + 4;
context->templocals = (infoconstant *)realloc(context->templocals, context->templocalssize * sizeof(infoconstant));
}
templocal = &(context->templocals[context->tempnumlocals]);
context->tempnumlocals += 1;
for (nod = nod->children; nod; nod=nod->next) {
if (nod->type == XML_ELEMENT_NODE) {
if (!xmlStrcmp(nod->name, BAD_CAST "identifier") && nod->children && nod->children->type == XML_TEXT_NODE) {
templocal->identifier = xmlStrdup(nod->children->content);
}
if (!xmlStrcmp(nod->name, BAD_CAST "frame-offset") && nod->children && nod->children->type == XML_TEXT_NODE) {
templocal->value = strtol((char *)nod->children->content, NULL, 10);
}
}
}
}
}
}
}
}
/* This is called after the XML data is parsed. We wrap up what we've
found and store it in the debuginfo global. Returns 1 on success.
*/
static int finalize_debuginfo(debuginfofile *context)
{
int ix;
context->numarrays = xmlHashSize(context->arrays);
context->arraylist = (infoarray **)malloc(context->numarrays * sizeof(infoarray *));
context->tempcounter = 0;
xmlHashScan(context->arrays, add_array_to_table, context);
if (context->tempcounter != context->numarrays)
printf("### array underflow!\n"); /*###*/
qsort(context->arraylist, context->numarrays, sizeof(infoarray *), sort_arrays_table);
for (ix=0; ix<context->numarrays; ix++) {
if (ix+1 < context->numarrays) {
context->arraylist[ix]->nextaddress = context->arraylist[ix+1]->address;
}
else {
context->arraylist[ix]->nextaddress = context->arraylist[ix]->address + context->arraylist[ix]->bytelength;
}
}
context->numobjects = xmlHashSize(context->objects);
context->objectlist = (infoobject **)malloc(context->numobjects * sizeof(infoobject *));
context->tempcounter = 0;
xmlHashScan(context->objects, add_object_to_table, context);
if (context->tempcounter != context->numobjects)
printf("### object underflow!\n"); /*###*/
qsort(context->objectlist, context->numobjects, sizeof(infoobject *), sort_objects_table);
for (ix=0; ix<context->numobjects; ix++) {
if (ix+1 < context->numobjects) {
context->objectlist[ix]->nextaddress = context->objectlist[ix+1]->address;
}
else {
context->objectlist[ix]->nextaddress = context->objectlist[ix]->address + 1;
}
}
context->numroutines = xmlHashSize(context->routines);
context->routinelist = (inforoutine **)malloc(context->numroutines * sizeof(inforoutine *));
context->tempcounter = 0;
xmlHashScan(context->routines, add_routine_to_table, context);
if (context->tempcounter != context->numroutines)
printf("### array underflow!\n"); /*###*/
qsort(context->routinelist, context->numroutines, sizeof(inforoutine *), sort_routines_table);
for (ix=0; ix<context->numroutines; ix++) {
if (ix+1 < context->numroutines) {
context->routinelist[ix]->nextaddress = context->routinelist[ix+1]->address;
}
else {
context->routinelist[ix]->nextaddress = context->routinelist[ix]->address + context->routinelist[ix]->length;
}
}
if (context->templocals) {
free(context->templocals);
context->templocals = NULL;
}
context->templocalssize = 0;
context->tempnumlocals = 0;
/* Install into global. */
debuginfo = context;
return 1;
}
/* Compare main memory to the story-file-prefix we found. If it doesn't
match, display a warning.
*/
void debugger_check_story_file()
{
const unsigned char *cx;
int pos = 0;
int count = 0;
uint32_t word = 0;
int fail = FALSE;
if (!debuginfo || !debuginfo->storyfileprefix)
return;
/* Check that this looks like an Inform 6 game file. See the
Glulx Inform Technical Reference. */
if (Mem4(0x24) != 0x496E666F) { /* 'Info' */
gidebug_output("Warning: This game file does not look like it was generated by Inform.");
}
/* Decode the storyfileprefix, which is in base64. */
for (cx = debuginfo->storyfileprefix; *cx && *cx != '='; cx++) {
unsigned int sixbit = 0;
if (*cx >= 'A' && *cx <= 'Z')
sixbit = (*cx) - 'A';
else if (*cx >= 'a' && *cx <= 'z')
sixbit = ((*cx) - 'a') + 26;
else if (*cx >= '0' && *cx <= '9')
sixbit = ((*cx) - '0') + 52;
else if (*cx == '+')
sixbit = 62;
else if (*cx == '/')
sixbit = 63;
else
sixbit = 0;
switch (count) {
case 0:
word = (sixbit << 18);
break;
case 1:
word |= (sixbit << 12);
break;
case 2:
word |= (sixbit << 6);
break;
case 3:
word |= (sixbit);
break;
}
count++;
if (count == 4) {
unsigned char byte;
byte = (word >> 16) & 0xFF;
if (byte != Mem1(pos))
fail = TRUE;
byte = (word >> 8) & 0xFF;
if (byte != Mem1(pos+1))
fail = TRUE;
byte = (word) & 0xFF;
if (byte != Mem1(pos+2))
fail = TRUE;
pos += 3;