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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
// Script debugger functionality. Absolutely not threadsafe.
#include "sci/sci.h"
#include "sci/console.h"
#include "sci/debug.h"
#include "sci/engine/state.h"
#include "sci/engine/kernel.h"
#include "sci/engine/script.h"
#include "sci/engine/scriptdebug.h"
#include "common/algorithm.h"
namespace Sci {
//#define VM_DEBUG_SEND
// This table is only used for debugging. Don't include it for devices
// with not enough available memory (e.g. phones), where REDUCE_MEMORY_USAGE
// is defined
#ifndef REDUCE_MEMORY_USAGE
const char *opcodeNames[] = {
"bnot", "add", "sub", "mul", "div",
"mod", "shr", "shl", "xor", "and",
"or", "neg", "not", "eq?", "ne?",
"gt?", "ge?", "lt?", "le?", "ugt?",
"uge?", "ult?", "ule?", "bt", "bnt",
"jmp", "ldi", "push", "pushi", "toss",
"dup", "link", "call", "callk", "callb",
"calle", "ret", "send", "dummy", "dummy",
"class", "dummy", "self", "super", "&rest",
"lea", "selfID", "dummy", "pprev", "pToa",
"aTop", "pTos", "sTop", "ipToa", "dpToa",
"ipTos", "dpTos", "lofsa", "lofss", "push0",
"push1", "push2", "pushSelf", "line", "lag",
"lal", "lat", "lap", "lsg", "lsl",
"lst", "lsp", "lagi", "lali", "lati",
"lapi", "lsgi", "lsli", "lsti", "lspi",
"sag", "sal", "sat", "sap", "ssg",
"ssl", "sst", "ssp", "sagi", "sali",
"sati", "sapi", "ssgi", "ssli", "ssti",
"sspi", "+ag", "+al", "+at", "+ap",
"+sg", "+sl", "+st", "+sp", "+agi",
"+ali", "+ati", "+api", "+sgi", "+sli",
"+sti", "+spi", "-ag", "-al", "-at",
"-ap", "-sg", "-sl", "-st", "-sp",
"-agi", "-ali", "-ati", "-api", "-sgi",
"-sli", "-sti", "-spi"
};
#endif // REDUCE_MEMORY_USAGE
void DebugState::updateActiveBreakpointTypes() {
int type = 0;
for (Common::List<Breakpoint>::iterator bp = _breakpoints.begin(); bp != _breakpoints.end(); ++bp) {
if (bp->_action != BREAK_NONE)
type |= bp->_type;
}
_activeBreakpointTypes = type;
}
// Disassembles one command from the heap, returns address of next command or 0 if a ret was encountered.
reg_t disassemble(EngineState *s, reg32_t pos, const Object *obj, bool printBWTag, bool printBytecode) {
SegmentObj *mobj = s->_segMan->getSegment(pos.getSegment(), SEG_TYPE_SCRIPT);
Script *script_entity = NULL;
reg_t retval;
retval.setSegment(pos.getSegment());
retval.setOffset(pos.getOffset() + 1);
uint16 param_value = 0xffff; // Suppress GCC warning by setting default value, chose value as invalid to getKernelName etc.
uint i = 0;
Kernel *kernel = g_sci->getKernel();
if (!mobj) {
warning("Disassembly failed: Segment %04x non-existent or not a script", pos.getSegment());
return retval;
} else
script_entity = (Script *)mobj;
uint scr_size = script_entity->getBufSize();
if (pos.getOffset() >= scr_size) {
warning("Trying to disassemble beyond end of script");
return NULL_REG;
}
const byte *scr = script_entity->getBuf();
int16 opparams[4];
byte opsize;
uint bytecount = readPMachineInstruction(scr + pos.getOffset(), opsize, opparams);
const byte opcode = opsize >> 1;
debugN("%04x:%04x: ", PRINT_REG(pos));
if (printBytecode) {
if (pos.getOffset() + bytecount > scr_size) {
warning("Operation arguments extend beyond end of script");
return retval;
}
for (i = 0; i < bytecount; i++)
debugN("%02x ", scr[pos.getOffset() + i]);
for (i = bytecount; i < 5; i++)
debugN(" ");
}
opsize &= 1; // byte if true, word if false
if (printBWTag)
debugN("[%c] ", opsize ? 'B' : 'W');
if (opcode == op_pushSelf && opsize && g_sci->getGameId() != GID_FANMADE) { // 0x3e (62)
// Debug opcode op_file
debugN("file \"%s\"\n", scr + pos.getOffset() + 1); // +1: op_pushSelf size
retval.incOffset(bytecount - 1);
return retval;
}
#ifndef REDUCE_MEMORY_USAGE
debugN("%-5s", opcodeNames[opcode]);
#endif
static const char *defaultSeparator = "\t\t; ";
i = 0;
while (g_sci->_opcode_formats[opcode][i]) {
switch (g_sci->_opcode_formats[opcode][i++]) {
case Script_Invalid:
warning("-Invalid operation-");
break;
case Script_SByte:
case Script_Byte:
param_value = scr[retval.getOffset()];
debugN("\t%02x", param_value);
if (param_value > 9) {
debugN("%s%u", defaultSeparator, param_value);
}
retval.incOffset(1);
break;
case Script_Word:
case Script_SWord:
param_value = READ_SCI11ENDIAN_UINT16(&scr[retval.getOffset()]);
debugN("\t%04x", param_value);
if (param_value > 9) {
debugN("%s%u", defaultSeparator, param_value);
}
retval.incOffset(2);
break;
case Script_SVariable:
case Script_Variable:
case Script_Property:
case Script_Global:
case Script_Local:
case Script_Temp:
case Script_Param:
if (opsize) {
param_value = scr[retval.getOffset()];
retval.incOffset(1);
} else {
param_value = READ_SCI11ENDIAN_UINT16(&scr[retval.getOffset()]);
retval.incOffset(2);
}
if (opcode == op_callk) {
debugN("\t%s[%x],", (param_value < kernel->_kernelFuncs.size()) ?
((param_value < kernel->getKernelNamesSize()) ? kernel->getKernelName(param_value).c_str() : "[Unknown(postulated)]")
: "<invalid>", param_value);
} else if (opcode == op_class) {
const reg_t classAddr = s->_segMan->getClassAddress(param_value, SCRIPT_GET_DONT_LOAD, retval.getSegment());
if (!classAddr.isNull()) {
debugN("\t%s", s->_segMan->getObjectName(classAddr));
debugN(opsize ? "[%02x]" : "[%04x]", param_value);
} else {
debugN(opsize ? "\t%02x" : "\t%04x", param_value);
}
} else if (opcode == op_super) {
if (obj != nullptr) {
debugN("\t%s", s->_segMan->getObjectName(obj->getSuperClassSelector()));
debugN(opsize ? "[%02x]" : "[%04x]", param_value);
} else {
debugN(opsize ? "\t%02x" : "\t%04x", param_value);
}
debugN(",");
#ifdef ENABLE_SCI32
} else if (
opcode == op_pToa || opcode == op_aTop ||
opcode == op_pTos || opcode == op_sTop ||
opcode == op_ipToa || opcode == op_dpToa ||
opcode == op_ipTos || opcode == op_dpTos) {
const char *selectorName;
if (getSciVersion() == SCI_VERSION_3) {
if (param_value < kernel->getSelectorNamesSize()) {
selectorName = kernel->getSelectorName(param_value).c_str();
} else {
selectorName = "<invalid>";
}
} else {
if (obj != nullptr) {
const Object *const super = obj->getClass(s->_segMan);
assert(super);
selectorName = kernel->getSelectorName(super->getVarSelector(param_value / 2)).c_str();
} else {
selectorName = "<unavailable>";
}
}
debugN("\t%s[%x]", selectorName, param_value);
#endif
} else {
const char *separator = defaultSeparator;
debugN(opsize ? "\t%02x" : "\t%04x", param_value);
if (param_value > 9) {
debugN("%s%u", separator, param_value);
separator = ", ";
}
if (param_value >= 0x20 && param_value <= 0x7e) {
debugN("%s'%c'", separator, param_value);
separator = ", ";
}
if (opcode == op_pushi && param_value < kernel->getSelectorNamesSize()) {
debugN("%s%s", separator, kernel->getSelectorName(param_value).c_str());
}
}
break;
case Script_Offset: {
assert(opcode == op_lofsa || opcode == op_lofss);
if (opsize) {
param_value = scr[retval.getOffset()];
retval.incOffset(1);
} else {
param_value = READ_SCI11ENDIAN_UINT16(&scr[retval.getOffset()]);
retval.incOffset(2);
}
const uint32 offset = findOffset(param_value, script_entity, retval.getOffset());
reg_t addr;
addr.setSegment(retval.getSegment());
addr.setOffset(offset);
if (getSciVersion() == SCI_VERSION_3 && !s->_segMan->isObject(addr)) {
debugN("\t\"%s\"", s->_segMan->derefString(addr));
} else {
debugN("\t%s", s->_segMan->getObjectName(addr));
}
debugN(opsize ? "[%02x]" : "[%04x]", offset);
break;
}
case Script_SRelative:
if (opsize) {
int8 offset = (int8)scr[retval.getOffset()];
retval.incOffset(1);
debugN("\t%02x [%04x]", 0xff & offset, kOffsetMask & (retval.getOffset() + offset));
} else {
int16 offset = (int16)READ_SCI11ENDIAN_UINT16(&scr[retval.getOffset()]);
retval.incOffset(2);
debugN("\t%04x [%04x]", 0xffff & offset, kOffsetMask & (retval.getOffset() + offset));
}
break;
case Script_End:
retval = NULL_REG;
break;
default:
error("Internal assertion failed in disassemble()");
}
}
if (pos == s->xs->addr.pc) { // Extra information if debugging the current opcode
if ((opcode == op_pTos) || (opcode == op_sTop) || (opcode == op_pToa) || (opcode == op_aTop) ||
(opcode == op_dpToa) || (opcode == op_ipToa) || (opcode == op_dpTos) || (opcode == op_ipTos)) {
obj = s->_segMan->getObject(s->xs->objp);
if (!obj) {
warning("Attempted to reference on non-object at %04x:%04x", PRINT_REG(s->xs->objp));
} else {
if (getSciVersion() == SCI_VERSION_3)
debugN("\t(%s)", g_sci->getKernel()->getSelectorName(param_value).c_str());
else
debugN("\t(%s)", g_sci->getKernel()->getSelectorName(obj->propertyOffsetToId(s->_segMan, param_value)).c_str());
}
}
}
debugN("\n");
if (pos == s->xs->addr.pc) { // Extra information if debugging the current opcode
if (opcode == op_callk) {
int stackframe = (scr[pos.getOffset() + 2] >> 1) + (s->r_rest);
int argc = ((s->xs->sp)[- stackframe - 1]).getOffset();
bool oldScriptHeader = (getSciVersion() == SCI_VERSION_0_EARLY);
if (!oldScriptHeader)
argc += (s->r_rest);
debugN(" Kernel params: (");
for (int j = 0; j < argc; j++) {
debugN("%04x:%04x", PRINT_REG((s->xs->sp)[j - stackframe]));
if (j + 1 < argc)
debugN(", ");
}
debugN(")\n");
} else if ((opcode == op_send) || (opcode == op_self)) {
int restmod = s->r_rest;
int stackframe = (scr[pos.getOffset() + 1] >> 1) + restmod;
reg_t *sb = s->xs->sp;
uint16 selector;
reg_t fun_ref;
while (stackframe > 0) {
int argc = sb[- stackframe + 1].getOffset();
const char *name = NULL;
reg_t called_obj_addr = s->xs->objp;
if (opcode == op_send)
called_obj_addr = s->r_acc;
else if (opcode == op_self)
called_obj_addr = s->xs->objp;
selector = sb[- stackframe].getOffset();
name = s->_segMan->getObjectName(called_obj_addr);
if (!name)
name = "<invalid>";
debugN(" %s::%s[", name, g_sci->getKernel()->getSelectorName(selector).c_str());
if (!s->_segMan->getObject(called_obj_addr)) {
debugN("INVALID_OBJ");
} else {
switch (lookupSelector(s->_segMan, called_obj_addr, selector, 0, &fun_ref)) {
case kSelectorMethod:
debugN("FUNCT");
argc += restmod;
restmod = 0;
break;
case kSelectorVariable:
debugN("VAR");
break;
case kSelectorNone:
debugN("INVALID");
break;
}
}
debugN("](");
while (argc--) {
debugN("%04x:%04x", PRINT_REG(sb[- stackframe + 2]));
if (argc)
debugN(", ");
stackframe--;
}
debugN(")\n");
stackframe -= 2;
} // while (stackframe > 0)
} // Send-like opcodes
} // (heappos == *p_pc)
return retval;
}
bool isJumpOpcode(EngineState *s, reg_t pos, reg_t& jumpTarget) {
SegmentObj *mobj = s->_segMan->getSegment(pos.getSegment(), SEG_TYPE_SCRIPT);
if (!mobj)
return false;
Script *script_entity = (Script *)mobj;
uint scr_size = script_entity->getScriptSize();
if (pos.getOffset() >= scr_size)
return false;
const byte *scr = script_entity->getBuf();
int16 opparams[4];
byte opsize;
int bytecount = readPMachineInstruction(scr + pos.getOffset(), opsize, opparams);
const byte opcode = opsize >> 1;
switch (opcode) {
case op_bt:
case op_bnt:
case op_jmp:
{
reg_t jmpTarget = pos + bytecount + opparams[0];
// QFG2 has invalid jumps outside the script buffer in script 260
if (jmpTarget.getOffset() >= scr_size)
return false;
jumpTarget = jmpTarget;
}
return true;
default:
return false;
}
}
void SciEngine::scriptDebug() {
EngineState *s = _gamestate;
if (_debugState.seeking && !_debugState.breakpointWasHit) { // Are we looking for something special?
if (_debugState.seeking == kDebugSeekStepOver) {
// are we above seek-level? resume then
if (_debugState.seekLevel < (int)s->_executionStack.size())
return;
_debugState.seeking = kDebugSeekNothing;
}
if (_debugState.seeking != kDebugSeekNothing) {
const reg32_t pc = s->xs->addr.pc;
SegmentObj *mobj = s->_segMan->getSegment(pc.getSegment(), SEG_TYPE_SCRIPT);
if (mobj) {
Script *scr = (Script *)mobj;
const byte *code_buf = scr->getBuf();
uint16 code_buf_size = scr->getBufSize(); // TODO: change to a 32-bit integer for large SCI3 scripts
int opcode = pc.getOffset() >= code_buf_size ? 0 : code_buf[pc.getOffset()];
int op = opcode >> 1;
uint16 paramb1 = pc.getOffset() + 1 >= code_buf_size ? 0 : code_buf[pc.getOffset() + 1];
uint16 paramf1 = (opcode & 1) ? paramb1 : (pc.getOffset() + 2 >= code_buf_size ? 0 : (int16)READ_SCI11ENDIAN_UINT16(code_buf + pc.getOffset() + 1));
switch (_debugState.seeking) {
case kDebugSeekSpecialCallk:
if (paramb1 != _debugState.seekSpecial)
return;
case kDebugSeekCallk:
if (op != op_callk)
return;
break;
case kDebugSeekLevelRet:
if ((op != op_ret) || (_debugState.seekLevel < (int)s->_executionStack.size()-1))
return;
break;
case kDebugSeekGlobal:
if (op < op_sag)
return;
if ((op & 0x3) > 1)
return; // param or temp
if ((op & 0x3) && s->_executionStack.back().local_segment > 0)
return; // locals and not running in script.000
if (paramf1 != _debugState.seekSpecial)
return; // CORRECT global?
break;
default:
break;
}
_debugState.seeking = kDebugSeekNothing;
}
}
// OK, found whatever we were looking for
}
debugN("Step #%d\n", s->scriptStepCounter);
disassemble(s, s->xs->addr.pc, s->_segMan->getObject(s->xs->objp), false, true);
if (_debugState.runningStep) {
_debugState.runningStep--;
return;
}
_debugState.debugging = false;
_console->attach();
}
void Kernel::dumpScriptObject(const SciSpan<const byte> &script, SciSpan<const byte> object) {
const int16 species = object.getInt16SEAt(8);
const int16 superclass = object.getInt16SEAt(10);
const int16 namepos = object.getInt16SEAt(14);
int i = 0;
debugN("Object\n");
//-4 because the size includes the two-word header
Common::hexdump(object.getUnsafeDataAt(0, object.size() - 4), object.size() - 4, 16, object.sourceByteOffset());
debugN("Name: %s\n", namepos ? script.getStringAt(namepos).c_str() : "<unknown>");
debugN("Superclass: %x\n", superclass);
debugN("Species: %x\n", species);
debugN("-info-: %x\n", object.getInt16SEAt(12) & 0xFFFF);
debugN("Function area offset: %x\n", object.getInt16SEAt(4));
int16 selectors = object.getInt16SEAt(6);
debugN("Selectors [%x]:\n", selectors);
object += 8;
while (selectors--) {
debugN(" [#%03x] = 0x%x\n", i++, object.getInt16SEAt(0) & 0xFFFF);
object += 2;
}
selectors = object.getInt16SEAt(0);
int16 overloads = selectors;
debugN("Overridden functions: %x\n", overloads);
object += 2;
if (overloads < 100) {
while (overloads--) {
const int16 selector = object.getInt16SEAt(0);
debugN(" [%03x] %s: @", selector & 0xFFFF, (selector >= 0 && selector < (int)_selectorNames.size()) ? _selectorNames[selector].c_str() : "<?>");
debugN("%04x\n", object.getInt16SEAt(selectors * 2 + 2) & 0xFFFF);
object += 2;
}
}
}
void Kernel::dumpScriptClass(const SciSpan<const byte> &script, SciSpan<const byte> clazz) {
const int16 species = clazz.getInt16SEAt(8);
const int16 superclass = clazz.getInt16SEAt(10);
const int16 namepos = clazz.getInt16SEAt(14);
debugN("Class\n");
Common::hexdump(clazz.getUnsafeDataAt(0, clazz.size() - 4), clazz.size() - 4, 16, clazz.sourceByteOffset());
debugN("Name: %s\n", namepos ? script.getStringAt(namepos).c_str() : "<unknown>");
debugN("Superclass: %x\n", superclass);
debugN("Species: %x\n", species);
debugN("-info-: %x\n", clazz.getInt16SEAt(12) & 0xFFFF);
debugN("Function area offset: %x\n", clazz.getInt16SEAt(4));
int16 selectors = clazz.getInt16SEAt(6);
int16 selectorsize = selectors;
debugN("Selectors [%x]:\n", selectors);
clazz += 8;
selectorsize <<= 1;
while (selectors--) {
const int16 selector = clazz.getInt16SEAt(selectorsize);
debugN(" [%03x] %s = 0x%x\n", selector & 0xFFFF, (selector >= 0 && selector < (int)_selectorNames.size()) ? _selectorNames[selector].c_str() : "<?>", clazz.getInt16SEAt(0) & 0xFFFF);
clazz += 2;
}
clazz += selectorsize;
int16 overloads = clazz.getInt16SEAt(0);
selectors = overloads;
debugN("Overloaded functions: %x\n", overloads);
clazz += 2;
while (overloads--) {
int16 selector = clazz.getInt16SEAt(0);
debugN("selector=%d; selectorNames.size() =%d\n", selector, _selectorNames.size());
debugN(" [%03x] %s: @", selector & 0xFFFF, (selector >= 0 && selector < (int)_selectorNames.size()) ?
_selectorNames[selector].c_str() : "<?>");
debugN("%04x\n", clazz.getInt16SEAt(selectors * 2 + 2) & 0xFFFF);
clazz += 2;
}
}
void Kernel::dissectScript(int scriptNumber, Vocabulary *vocab) {
int objectctr[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint32 _seeker = 0;
Resource *script = _resMan->findResource(ResourceId(kResourceTypeScript, scriptNumber), false);
if (!script) {
warning("dissectScript(): Script not found!\n");
return;
}
while (_seeker < script->size()) {
int objType = script->getInt16SEAt(_seeker);
int objsize;
uint32 seeker = _seeker + 4;
if (!objType) {
debugN("End of script object (#0) encountered.\n");
debugN("Classes: %i, Objects: %i, Export: %i,\n Var: %i (all base 10)\n",
objectctr[6], objectctr[1], objectctr[7], objectctr[10]);
return;
}
debugN("\n");
objsize = script->getInt16SEAt(_seeker + 2);
debugN("Obj type #%x, size 0x%x: ", objType, objsize);
_seeker += objsize;
if (objType >= 0 && objType < ARRAYSIZE(objectctr))
objectctr[objType]++;
switch (objType) {
case SCI_OBJ_OBJECT:
dumpScriptObject(*script, script->subspan(seeker, objsize));
break;
case SCI_OBJ_CODE:
debugN("Code\n");
Common::hexdump(script->getUnsafeDataAt(seeker, objsize - 4), objsize - 4, 16, seeker);
break;
case SCI_OBJ_SYNONYMS:
debugN("Synonyms\n");
Common::hexdump(script->getUnsafeDataAt(seeker, objsize - 4), objsize - 4, 16, seeker);
break;
case SCI_OBJ_SAID:
debugN("Said\n");
Common::hexdump(script->getUnsafeDataAt(seeker, objsize - 4), objsize - 4, 16, seeker);
debugN("%04x: ", seeker);
vocab->debugDecipherSaidBlock(script->subspan(seeker));
debugN("\n");
break;
case SCI_OBJ_STRINGS:
debugN("Strings\n");
while (script->getUint8At(seeker)) {
const Common::String string = script->getStringAt(seeker);
debugN("%04x: %s", seeker, string.c_str());
seeker += string.size() + 1;
if (seeker > script->size()) {
debugN("[TRUNCATED]");
}
debugN("\n");
}
seeker++; // the ending zero byte
break;
case SCI_OBJ_CLASS:
dumpScriptClass(*script, script->subspan(seeker, objsize));
break;
case SCI_OBJ_EXPORTS:
debugN("Exports\n");
Common::hexdump(script->getUnsafeDataAt(seeker, objsize - 4), objsize - 4, 16, seeker);
break;
case SCI_OBJ_POINTERS:
debugN("Pointers\n");
Common::hexdump(script->getUnsafeDataAt(seeker, objsize - 4), objsize - 4, 16, seeker);
break;
case 9:
debugN("<unknown>\n");
Common::hexdump(script->getUnsafeDataAt(seeker, objsize - 4), objsize - 4, 16, seeker);
break;
case SCI_OBJ_LOCALVARS:
debugN("Local vars\n");
Common::hexdump(script->getUnsafeDataAt(seeker, objsize - 4), objsize - 4, 16, seeker);
break;
default:
debugN("Unsupported!\n");
return;
}
}
debugN("Script ends without terminator\n");
}
bool SciEngine::checkSelectorBreakpoint(BreakpointType breakpointType, reg_t send_obj, int selector) {
Common::String methodName = _gamestate->_segMan->getObjectName(send_obj);
methodName += "::" + getKernel()->getSelectorName(selector);
bool found = false;
Common::List<Breakpoint>::const_iterator bp;
for (bp = _debugState._breakpoints.begin(); bp != _debugState._breakpoints.end(); ++bp) {
if (bp->_action == BREAK_NONE || bp->_type != breakpointType)
continue;
if (bp->_name == methodName ||
(bp->_name.hasSuffix("::") && methodName.hasPrefix(bp->_name))) {
if (!found) // Show message once, but allow multiple actions
_console->debugPrintf("Break on %s (in [%04x:%04x])\n", methodName.c_str(), PRINT_REG(send_obj));
found = true;
if (bp->_action == BREAK_BREAK) {
_debugState.debugging = true;
_debugState.breakpointWasHit = true;
} else if (bp->_action == BREAK_BACKTRACE) {
logBacktrace();
} else if (bp->_action == BREAK_INSPECT) {
printObject(send_obj);
}
}
}
return found;
}
bool SciEngine::checkExportBreakpoint(uint16 script, uint16 pubfunct) {
if (!(_debugState._activeBreakpointTypes & BREAK_EXPORT))
return false;
bool found = false;
uint32 bpaddress = (script << 16 | pubfunct);
Common::List<Breakpoint>::const_iterator bp;
for (bp = _debugState._breakpoints.begin(); bp != _debugState._breakpoints.end(); ++bp) {
if (bp->_action == BREAK_NONE || bp->_type != BREAK_EXPORT)
continue;
if (bp->_address == bpaddress) {
if (!found) // Show message once, but allow multiple actions
_console->debugPrintf("Break on script %d, export %d\n", script, pubfunct);
found = true;
if (bp->_action == BREAK_BREAK) {
_debugState.debugging = true;
_debugState.breakpointWasHit = true;
} else if (bp->_action == BREAK_BACKTRACE) {
logBacktrace();
} else if (bp->_action == BREAK_INSPECT) {
// Ignoring this mode, to make it identical to BREAK_LOG
}
}
}
return found;
}
bool SciEngine::checkAddressBreakpoint(const reg32_t &address) {
if (!(_debugState._activeBreakpointTypes & BREAK_ADDRESS))
return false;
bool found = false;
Common::List<Breakpoint>::const_iterator bp;
for (bp = _debugState._breakpoints.begin(); bp != _debugState._breakpoints.end(); ++bp) {
if (bp->_action == BREAK_NONE || bp->_type != BREAK_ADDRESS)
continue;
if (bp->_regAddress == address) {
if (!found)
_console->debugPrintf("Break at %04x:%04x\n", PRINT_REG(address));
found = true;
if (bp->_action == BREAK_BREAK) {
_debugState.debugging = true;
_debugState.breakpointWasHit = true;
} else if (bp->_action == BREAK_BACKTRACE) {
logBacktrace();
} else if (bp->_action == BREAK_INSPECT) {
// Ignoring this mode, to make it identical to BREAK_LOG
}
}
}
return found;
}
bool matchKernelBreakpointPattern(const Common::String &pattern, const Common::String &name) {
// Pattern:
// A comma-separated list of atoms.
// An atom is a (possibly empty) word, optionally with a ! prefix (for
// a negative-match), and/or a * suffix (for a prefix-match).
// The last matching atom in the pattern takes effect.
// Examples:
// FrameOut : matches only FrameOut
// * : matches everything
// *,!FrameOut : matches everything except FrameOut
// InitBresen,DoBresen : matches InitBresen and DoBresen
// DoSound*,!DoSoundUpdateCues : matches all DoSound sub-functions except
// DoSoundUpdateCues
bool result = false;
Common::String::const_iterator i = pattern.begin();
while (i != pattern.end()) {
Common::String::const_iterator next = Common::find(i, pattern.end(), ',');
bool negative = *i == '!';
if (negative)
i++;
Common::String atom(i, next - i);
bool wildcard = atom.lastChar() == '*';
if (wildcard)
atom.deleteLastChar();
if ((!wildcard && atom == name) || (wildcard && name.hasPrefix(atom)))
result = !negative;
i = next;
if (i != pattern.end())
++i; // skip comma
}
return result;
}
bool SciEngine::checkKernelBreakpoint(const Common::String &name) {
if (!(_debugState._activeBreakpointTypes & BREAK_KERNEL))
return false;
bool found = false;
Common::List<Breakpoint>::const_iterator bp;
for (bp = _debugState._breakpoints.begin(); bp != _debugState._breakpoints.end(); ++bp) {
if (bp->_action == BREAK_NONE || bp->_type != BREAK_KERNEL)
continue;
if (matchKernelBreakpointPattern(bp->_name, name)) {
if (bp->_action == BREAK_BREAK) {
if (!found)
_console->debugPrintf("Break on k%s\n", name.c_str());
_debugState.debugging = true;
_debugState.breakpointWasHit = true;
} else if (bp->_action == BREAK_BACKTRACE) {
if (!found)
_console->debugPrintf("Break on k%s\n", name.c_str());
logBacktrace();
} else if (bp->_action == BREAK_INSPECT) {
// Ignoring this mode, to make it identical to BREAK_LOG
}
found = true;
}
}
return found;
}
void debugSelectorCall(reg_t send_obj, Selector selector, int argc, StackPtr argp, ObjVarRef &varp, reg_t funcp, SegManager *segMan, SelectorType selectorType) {
int activeBreakpointTypes = g_sci->_debugState._activeBreakpointTypes;
const char *objectName = segMan->getObjectName(send_obj);
const char *selectorName = g_sci->getKernel()->getSelectorName(selector).c_str();
Console *con = g_sci->getSciDebugger();
#ifdef VM_DEBUG_SEND
debugN("Send to %04x:%04x (%s), selector %04x (%s):", PRINT_REG(send_obj),
segMan->getObjectName(send_obj), selector,
g_sci->getKernel()->getSelectorName(selector).c_str());
#endif // VM_DEBUG_SEND
switch (selectorType) {
case kSelectorNone:
debugN("\n");
break;
case kSelectorVariable:
#ifdef VM_DEBUG_SEND
if (argc)
debugN("Varselector: Write %04x:%04x\n", PRINT_REG(argp[1]));
else
debugN("Varselector: Read\n");
#endif // VM_DEBUG_SEND
// argc == 0: read selector
// argc == 1: write selector
// argc can be bigger than 1 in some cases, because of a script bug.
// Usually, these aren't fatal.
if ((activeBreakpointTypes & BREAK_SELECTORREAD) ||
(activeBreakpointTypes & BREAK_SELECTORWRITE) ||
argc > 1) {
reg_t selectorValue = *varp.getPointer(segMan);
if (!argc && (activeBreakpointTypes & BREAK_SELECTORREAD)) {
if (g_sci->checkSelectorBreakpoint(BREAK_SELECTORREAD, send_obj, selector))
con->debugPrintf("Read from selector (%s:%s): %04x:%04x\n",
objectName, selectorName,
PRINT_REG(selectorValue));
} else if (argc && (activeBreakpointTypes & BREAK_SELECTORWRITE)) {
if (g_sci->checkSelectorBreakpoint(BREAK_SELECTORWRITE, send_obj, selector))
con->debugPrintf("Write to selector (%s:%s): change %04x:%04x to %04x:%04x\n",
objectName, selectorName,
PRINT_REG(selectorValue), PRINT_REG(argp[1]));
}
if (argc > 1)
debug(kDebugLevelScripts, "Write to selector (%s:%s): change %04x:%04x to %04x:%04x, argc == %d\n",
objectName, selectorName,
PRINT_REG(selectorValue), PRINT_REG(argp[1]), argc);
}
break;
case kSelectorMethod:
#ifndef VM_DEBUG_SEND
if (activeBreakpointTypes & BREAK_SELECTOREXEC) {
if (g_sci->checkSelectorBreakpoint(BREAK_SELECTOREXEC, send_obj, selector)) {
#else
if (true) {
if (true) {
#endif
con->debugPrintf("%s::%s(", objectName, selectorName);
for (int i = 0; i < argc; i++) {
con->debugPrintf("%04x:%04x", PRINT_REG(argp[i+1]));
if (i + 1 < argc)
con->debugPrintf(", ");
}
con->debugPrintf(") at %04x:%04x\n", PRINT_REG(funcp));
}
}
break;
} // switch
}
void debugPropertyAccess(Object *obj, reg_t objp, unsigned int index, reg_t curValue, reg_t newValue, SegManager *segMan, BreakpointType breakpointType) {
const Object *var_container = obj;
if (!obj->isClass() && getSciVersion() != SCI_VERSION_3)
var_container = segMan->getObject(obj->getSuperClassSelector());
uint16 varSelector;
if (getSciVersion() == SCI_VERSION_3) {
varSelector = index;
} else {
index >>= 1;
if (index >= var_container->getVarCount()) {
// TODO: error, warning, debug?
return;
}
varSelector = var_container->getVarSelector(index);
}
if (g_sci->checkSelectorBreakpoint(breakpointType, objp, varSelector)) {
// checkSelectorBreakpoint has already triggered the breakpoint.
// We just output the relevant data here.
Console *con = g_sci->getSciDebugger();
const char *objectName = segMan->getObjectName(objp);
const char *selectorName = g_sci->getKernel()->getSelectorName(varSelector).c_str();
if (breakpointType == BREAK_SELECTORWRITE) {
con->debugPrintf("Write to selector (%s:%s): change %04x:%04x to %04x:%04x\n",
objectName, selectorName,
PRINT_REG(curValue), PRINT_REG(newValue));
} else if (breakpointType == BREAK_SELECTORREAD) {
con->debugPrintf("Read from selector (%s:%s): %04x:%04x\n",
objectName, selectorName,
PRINT_REG(curValue));
} else {
assert(false);
}
}
}
void logKernelCall(const KernelFunction *kernelCall, const KernelSubFunction *kernelSubCall, EngineState *s, int argc, reg_t *argv, reg_t result) {
Kernel *kernel = g_sci->getKernel();
if (!kernelSubCall) {
debugN("k%s: ", kernelCall->name);
} else {
int callNameLen = strlen(kernelCall->name);
if (strncmp(kernelCall->name, kernelSubCall->name, callNameLen) == 0) {
const char *subCallName = kernelSubCall->name + callNameLen;
debugN("k%s(%s): ", kernelCall->name, subCallName);
} else {
debugN("k%s(%s): ", kernelCall->name, kernelSubCall->name);
}
}
for (int parmNr = 0; parmNr < argc; parmNr++) {
if (parmNr)
debugN(", ");
uint16 regType = kernel->findRegType(argv[parmNr]);
if (regType & SIG_TYPE_NULL)
debugN("0");
else if (regType & SIG_TYPE_UNINITIALIZED)
debugN("UNINIT");
else if (regType & SIG_IS_INVALID)
debugN("INVALID");
else if (regType & SIG_TYPE_INTEGER)
debugN("%d", argv[parmNr].getOffset());
else {
debugN("%04x:%04x", PRINT_REG(argv[parmNr]));
switch (regType) {
case SIG_TYPE_OBJECT:
debugN(" (%s)", s->_segMan->getObjectName(argv[parmNr]));
break;
case SIG_TYPE_REFERENCE:
{
SegmentObj *mobj = s->_segMan->getSegmentObj(argv[parmNr].getSegment());
if (mobj) {
switch (mobj->getType()) {
case SEG_TYPE_HUNK:
{
HunkTable &ht = *(HunkTable *)mobj;
int index = argv[parmNr].getOffset();
if (ht.isValidEntry(index)) {
// NOTE: This ", deleted" isn't as useful as it could
// be because it prints the status _after_ the kernel
// call.
debugN(" ('%s' hunk%s)", ht[index].type, ht[index].mem ? "" : ", deleted");
} else
debugN(" (INVALID hunk ref)");
break;
}
default:
// TODO: Any other segment types which could
// use special handling?
if (kernelCall->function == kSaid) {
SegmentRef saidSpec = s->_segMan->dereference(argv[parmNr]);
if (saidSpec.isRaw) {
debugN(" ('");
g_sci->getVocabulary()->debugDecipherSaidBlock(SciSpan<const byte>(saidSpec.raw, saidSpec.maxSize, Common::String::format("said %04x:%04x", PRINT_REG(argv[parmNr]))));
debugN("')");
} else {
debugN(" (non-raw said-spec)");
}
} else {
debugN(" ('%s')", s->_segMan->getString(argv[parmNr]).c_str());
}
break;
}
}
}
default:
break;
}
}
}
if (result.isPointer())
debugN(" = %04x:%04x\n", PRINT_REG(result));
else
debugN(" = %d\n", result.getOffset());
}
void logBacktrace() {
Console *con = g_sci->getSciDebugger();
EngineState *s = g_sci->getEngineState();
con->debugPrintf("Call stack (current base: 0x%x):\n", s->executionStackBase);
Common::List<ExecStack>::const_iterator iter;
uint i = 0;
for (iter = s->_executionStack.begin();
iter != s->_executionStack.end(); ++iter, ++i) {
const ExecStack &call = *iter;
const char *objname = s->_segMan->getObjectName(call.sendp);
int paramc, totalparamc;
switch (call.type) {
case EXEC_STACK_TYPE_CALL: // Normal function
if (call.type == EXEC_STACK_TYPE_CALL)
con->debugPrintf(" %x: script %d - ", i, s->_segMan->getScript(call.addr.pc.getSegment())->getScriptNumber());
if (call.debugSelector != -1) {
con->debugPrintf("%s::%s(", objname, g_sci->getKernel()->getSelectorName(call.debugSelector).c_str());
} else if (call.debugExportId != -1) {
con->debugPrintf("export %d (", call.debugExportId);
} else if (call.debugLocalCallOffset != -1) {
con->debugPrintf("call %x (", call.debugLocalCallOffset);
}
break;
case EXEC_STACK_TYPE_KERNEL: // Kernel function
if (call.debugKernelSubFunction == -1)
con->debugPrintf(" %x:[%x] k%s(", i, call.debugOrigin, g_sci->getKernel()->getKernelName(call.debugKernelFunction).c_str());
else
con->debugPrintf(" %x:[%x] k%s(", i, call.debugOrigin, g_sci->getKernel()->getKernelName(call.debugKernelFunction, call.debugKernelSubFunction).c_str());
break;
case EXEC_STACK_TYPE_VARSELECTOR:
con->debugPrintf(" %x:[%x] vs%s %s::%s (", i, call.debugOrigin, (call.argc) ? "write" : "read",
objname, g_sci->getKernel()->getSelectorName(call.debugSelector).c_str());
break;
}
totalparamc = call.argc;
if (totalparamc > 16)
totalparamc = 16;
for (paramc = 1; paramc <= totalparamc; paramc++) {
con->debugPrintf("%04x:%04x", PRINT_REG(call.variables_argp[paramc]));
if (paramc < call.argc)
con->debugPrintf(", ");
}
if (call.argc > 16)
con->debugPrintf("...");
con->debugPrintf(")\n ");
if (call.debugOrigin != -1)
con->debugPrintf("by %x ", call.debugOrigin);
con->debugPrintf("obj@%04x:%04x", PRINT_REG(call.objp));
if (call.type == EXEC_STACK_TYPE_CALL) {
con->debugPrintf(" pc=%04x:%04x", PRINT_REG(call.addr.pc));
if (call.sp == CALL_SP_CARRY)
con->debugPrintf(" sp,fp:carry");
else {
con->debugPrintf(" sp=ST:%04x", (unsigned)(call.sp - s->stack_base));
con->debugPrintf(" fp=ST:%04x", (unsigned)(call.fp - s->stack_base));
}
} else
con->debugPrintf(" pc:none");
con->debugPrintf(" argp:ST:%04x", (unsigned)(call.variables_argp - s->stack_base));
con->debugPrintf("\n");
}
}
bool printObject(reg_t pos) {
Console *con = g_sci->getSciDebugger();
EngineState *s = g_sci->getEngineState();
const Object *obj = s->_segMan->getObject(pos);
const Object *var_container = obj;
uint i;
if (!obj) {
con->debugPrintf("[%04x:%04x]: Not an object.\n", PRINT_REG(pos));
return false;
}
// Object header
con->debugPrintf("[%04x:%04x] %s : %3d vars, %3d methods\n", PRINT_REG(pos), s->_segMan->getObjectName(pos),
obj->getVarCount(), obj->getMethodCount());
if (!obj->isClass())
var_container = s->_segMan->getObject(obj->getSuperClassSelector());
con->debugPrintf(" -- member variables:\n");
if (getSciVersion() == SCI_VERSION_3) {
con->debugPrintf(" (----) [---] -size- = 0000:%04x (%d)\n", obj->getVarCount(), obj->getVarCount());
con->debugPrintf(" (----) [---] -classScript- = %04x:%04x (%d)\n", PRINT_REG(obj->getClassScriptSelector()), obj->getClassScriptSelector().getOffset());
con->debugPrintf(" (----) [---] -species- = %04x:%04x (%s)\n", PRINT_REG(obj->getSpeciesSelector()), s->_segMan->getObjectName(obj->getSpeciesSelector()));
con->debugPrintf(" (----) [---] -super- = %04x:%04x (%s)\n", PRINT_REG(obj->getSuperClassSelector()), s->_segMan->getObjectName(obj->getSuperClassSelector()));
con->debugPrintf(" (----) [---] -info- = %04x:%04x (%d)\n", PRINT_REG(obj->getInfoSelector()), obj->getInfoSelector().getOffset());
}
for (i = 0; (uint)i < obj->getVarCount(); i++) {
con->debugPrintf(" ");
if (var_container && i < var_container->getVarCount()) {
uint16 varSelector = var_container->getVarSelector(i);
// Times two commented out for now for easy parsing of vocab.994
con->debugPrintf("(%04x) [%03x] %s = ", i /* *2 */, varSelector, g_sci->getKernel()->getSelectorName(varSelector).c_str());
} else
con->debugPrintf("p#%x = ", i);
reg_t val = obj->getVariable(i);
con->debugPrintf("%04x:%04x", PRINT_REG(val));
if (!val.getSegment())
con->debugPrintf(" (%d)", val.getOffset());
const Object *ref = s->_segMan->getObject(val);
if (ref)
con->debugPrintf(" (%s)", s->_segMan->getObjectName(val));
con->debugPrintf("\n");
}
con->debugPrintf(" -- methods:\n");
Common::Array<Selector> foundMethods;
const Object *protoObj = obj;
do {
for (i = 0; i < protoObj->getMethodCount(); i++) {
const Selector selector = protoObj->getFuncSelector(i);
if (Common::find(foundMethods.begin(), foundMethods.end(), selector) == foundMethods.end()) {
reg_t fptr = protoObj->getFunction(i);
con->debugPrintf(" [%03x] ", selector);
if (protoObj != obj) {
con->debugPrintf("%s::", s->_segMan->getObjectName(protoObj->getPos()));
}
con->debugPrintf("%s = %04x:%04x\n", g_sci->getKernel()->getSelectorName(selector).c_str(), PRINT_REG(fptr));
foundMethods.push_back(selector);
}
}
} while ((protoObj = s->_segMan->getObject(protoObj->getSuperClassSelector())));
Script *scr = s->_segMan->getScriptIfLoaded(pos.getSegment());
if (scr)
con->debugPrintf("\nOwner script: %d\n", scr->getScriptNumber());
return true;
}
} // End of namespace Sci
|