This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathdebugger.js
7959 lines (7517 loc) · 358 KB
/
debugger.js
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
/**
* @fileoverview Implements the PCx86 Debugger component
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2019 Jeff Parsons
*
* This file is part of PCjs, a computer emulation software project at <https://www.pcjs.org>.
*
* PCjs 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 3
* of the License, or (at your option) any later version.
*
* PCjs 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 PCjs. If not,
* see <http://www.gnu.org/licenses/gpl.html>.
*
* You are required to include the above copyright notice in every modified copy of this work
* and to display that copyright notice when the software starts running; see COPYRIGHT in
* <https://www.pcjs.org/modules/shared/lib/defines.js>.
*
* Some PCjs files also attempt to load external resource files, such as character-image files,
* ROM files, and disk image files. Those external resource files are not considered part of PCjs
* for purposes of the GNU General Public License, and the author does not claim any copyright
* as to their contents.
*/
"use strict";
if (DEBUGGER) {
if (typeof module !== "undefined") {
var Str = require("../../shared/lib/strlib");
var Usr = require("../../shared/lib/usrlib");
var Web = require("../../shared/lib/weblib");
var Component = require("../../shared/lib/component");
var DbgLib = require("../../shared/lib/debugger");
var Keys = require("../../shared/lib/keys");
var State = require("../../shared/lib/state");
var PCx86 = require("./defines");
var X86 = require("./x86");
var SegX86 = require("./segx86");
var Interrupts = require("./interrupts");
var Messages = require("./messages");
var MemoryX86 = require("./memory");
}
}
/**
* @typedef {Object} DbgAddrX86
* @property {number} [off]
* @property {number} [sel]
* @property {number} [addr]
* @property {number} [type]
* @property {boolean} [fData32]
* @property {boolean} [fAddr32]
* @property {boolean} [fData32Orig]
* @property {boolean} [fAddr32Orig]
* @property {number} [cOverrides]
* @property {boolean} [fComplete]
* @property {boolean} [fTempBreak]
* @property {string} [sCmd]
* @property {Array.<string>} [aCmds]
* @property {number} [nCPUCycles] (added to DbgAddrX86 objects stored in history buffer)
* @property {number} [nDebugCycles] (added to DbgAddrX86 objects stored in history buffer)
* @property {number} [nDebugState] (added to DbgAddrX86 objects stored in history buffer)
*/
/*
* Debugger Breakpoint Tips
*
* Here's an example of our powerful new breakpoint command capabilities:
*
* bp 0397:022B "?'GlobalAlloc(wFlags:[ss:sp+8],dwBytes:[ss:sp+6][ss:sp+4])';g [ss:sp+2]:[ss:sp] '?ax;if ax'"
*
* The above breakpoint will display a pleasing "GlobalAlloc()" string containing the current
* stack parameters, and will briefly stop execution on the return to print the result in AX,
* halting the CPU whenever AX is zero (the default behavior of "if" whenever the expression is
* false is to look for an "else" and automatically halt when there is no "else").
*
* How do you figure out where the code for GlobalAlloc is in the first place? You need to have
* BACKTRACK support enabled (which currently means running the non-COMPILED version), so that as
* the Disk component loads disk images, it will automatically extract symbolic information from all
* "NE" (New Executable) binaries on those disks, which the Debugger's "dt" command can then search
* for you; eg:
*
* ## dt globalalloc
* GLOBALALLOC: KRNL386.EXE 0001:022B len 0xC570
*
* And then you just need to do a bit more sleuthing to find the right CODE segment. And that just
* got easier, now that the PCx86 Debugger mimics portions of the Windows Debugger INT 0x41 interface;
* see intWindowsDebugger() for details. So even if you neglect to run WDEB386.EXE /E inside the
* machine before running Windows, you should still see notifications like:
*
* KERNEL!undefined code(0001)=#0397 len 0000C580
*
* in the PCx86 Debugger output window, as segments are being loaded by the Windows kernel.
*/
/**
* class DebuggerX86
* @unrestricted (allows the class to define properties, both dot and named, outside of the constructor)
*/
class DebuggerX86 extends DbgLib {
/**
* DebuggerX86(parmsDbg)
*
* The DebuggerX86 component is an optional component that implements a variety of user commands
* for controlling the CPU, dumping and editing memory, etc.
*
* DebuggerX86 extends the shared Debugger component and supports the following optional (parmsDbg)
* properties:
*
* commands: string containing zero or more commands, separated by ';'
*
* messages: string containing zero or more message categories to enable;
* multiple categories must be separated by '|' or ';'. Parsed by messageInit().
*
* @this {DebuggerX86}
* @param {Object} parmsDbg
*/
constructor(parmsDbg)
{
super("Debugger", parmsDbg, -1);
if (DEBUGGER) {
/*
* Default number of hex chars in a register and a linear address (ie, for real-mode);
* updated by initBus().
*/
this.cchReg = 4;
this.cchAddr = 5;
this.maskAddr = 0xfffff;
/*
* Most commands that require an address call parseAddr(), which defaults to dbgAddrNextCode
* or dbgAddrNextData when no address has been given. doDump() and doUnassemble(), in turn,
* update dbgAddrNextData and dbgAddrNextCode, respectively, when they're done.
*
* All dbgAddr variables contain properties off, sel, and addr, where sel:off represents the
* segmented address and addr is the corresponding linear address (if known). For certain
* segmented addresses (eg, breakpoint addresses), we pre-compute the linear address and save
* that in addr, so that the breakpoint will still operate as intended even if the mode changes
* later (eg, from real-mode to protected-mode).
*
* Finally, for TEMPORARY breakpoint addresses, we set fTempBreak to true, so that they can be
* automatically cleared when they're hit.
*/
this.dbgAddrNextCode = this.newAddr(0, 0);
this.dbgAddrNextData = this.newAddr(0, 0);
this.dbgAddrAssemble = this.newAddr(0, 0);
/*
* aSymbolTable is an array of SymbolTable objects, one per ROM or other chunk of address space,
* where each object contains the following properties:
*
* sModule
* nSegment
* sel
* off
* addr (physical address, if any; eg, symbols for a ROM)
* len
* aSymbols
* aOffsets
*
* See addSymbols() for more details, since that's how callers add sets of symbols to the table.
*/
this.aSymbolTable = [];
/*
* clearBreakpoints() initializes the breakpoints lists: aBreakExec is a list of addresses
* to halt on whenever attempting to execute an instruction at the corresponding address,
* and aBreakRead and aBreakWrite are lists of addresses to halt on whenever a read or write,
* respectively, occurs at the corresponding address.
*
* NOTE: Curiously, after upgrading the Google Closure Compiler from v20141215 to v20150609,
* the resulting compiled code would crash in clearBreakpoints(), because the (renamed) aBreakRead
* property was already defined. To eliminate whatever was confusing the Closure Compiler, I've
* explicitly initialized all the properties that clearBreakpoints() (re)initializes.
*/
this.aBreakExec = this.aBreakRead = this.aBreakWrite = [];
this.clearBreakpoints();
/*
* The new "bn" command allows you to specify a number of instructions to execute and then stop;
* "bn 0" disables any outstanding count.
*/
this.nBreakIns = 0;
/*
* Execution history is allocated by historyInit() whenever checksEnabled() conditions change.
* Execution history is updated whenever the CPU calls checkInstruction(), which will happen
* only when checksEnabled() returns true (eg, whenever one or more breakpoints have been set).
* This ensures that, by default, the CPU runs as fast as possible.
*/
this.historyInit();
/*
* Initialize Debugger message and command support
*/
this.afnDumpers = {};
this.messageInit(parmsDbg['messages']);
this.sCommandsInit = parmsDbg['commands'];
/*
* Make it easier to access Debugger commands from an external REPL, like the WebStorm "live" console
* window; eg:
*
* pcx86('r')
* pcx86('dw 0:0')
* pcx86('h')
* ...
*/
let dbg = this;
if (window) {
if (window[PCx86.APPCLASS] === undefined) {
window[PCx86.APPCLASS] = function(s) { return dbg.doCommands(s); };
}
} else {
if (global[PCx86.APPCLASS] === undefined) {
global[PCx86.APPCLASS] = function(s) { return dbg.doCommands(s); };
}
}
} // endif DEBUGGER
}
/**
* initBus(bus, cpu, dbg)
*
* @this {DebuggerX86}
* @param {Computer} cmp
* @param {BusX86} bus
* @param {CPUx86} cpu
* @param {DebuggerX86} dbg
*/
initBus(cmp, bus, cpu, dbg)
{
this.bus = bus;
this.cpu = cpu;
this.cmp = cmp;
this.fdc = cmp.getMachineComponent("FDC");
this.hdc = cmp.getMachineComponent("HDC");
this.mouse = cmp.getMachineComponent("Mouse");
/*
* Re-initialize Debugger message and command support as needed
*/
let sMessages = cmp.getMachineParm('messages');
if (sMessages) this.messageInit(sMessages);
this.sCommandsInit = cmp.getMachineParm('commands') || this.sCommandsInit;
/*
* If CHIPSET or VIDEO messages are enabled at startup, we enable ChipSet or Video diagnostic info in the
* instruction history buffer as appropriate.
*/
if (this.messageEnabled(Messages.CHIPSET)) {
this.chipset = cmp.getMachineComponent("ChipSet");
}
else if (this.messageEnabled(Messages.VIDEO)) {
this.video = cmp.getMachineComponent("Video");
}
this.cchAddr = bus.getWidth() >> 2;
this.maskAddr = bus.nBusLimit;
/*
* Allocate a special segment "register", for use whenever a requested selector is not currently loaded
*/
this.segDebugger = new SegX86(this.cpu, SegX86.ID.DBG, "DBG");
this.aaOpDescs = DebuggerX86.aaOpDescs;
if (this.cpu.model >= X86.MODEL_80186) {
this.aaOpDescs = DebuggerX86.aaOpDescs.slice();
this.aaOpDescs[0x0F] = DebuggerX86.aOpDescUndefined;
if (this.cpu.model >= X86.MODEL_80286) {
/*
* TODO: Consider whether the aOpDesc0F table should be split in two: one for 80286-only instructions,
* and one for both 80286 and 80386. For now, the Debugger is not as strict as the CPUx86 is about
* the instructions it supports for each type of CPU, in part because an 80286 machine could still be
* presented with 80386-only code that is simply "skipped over" when then CPU doesn't support it.
*
* Obviously I'm not being entirely consistent, since I don't disassemble *any* 0x0F opcodes for any
* pre-80286 CPUs. But at least I'm being up front about it.
*/
this.aaOpDescs[0x0F] = DebuggerX86.aOpDesc0F;
if (I386 && this.cpu.model >= X86.MODEL_80386) this.cchReg = 8;
}
}
this.messageDump(Messages.BUS, function onDumpBus(asArgs) { dbg.dumpBus(asArgs); });
this.messageDump(Messages.DESC, function onDumpSel(asArgs) { dbg.dumpSel(asArgs); });
this.messageDump(Messages.DOS, function onDumpDOS(asArgs) { dbg.dumpDOS(asArgs); });
this.messageDump(Messages.MEM, function onDumpMem(asArgs) { dbg.dumpMem(asArgs); });
this.messageDump(Messages.TSS, function onDumpTSS(asArgs) { dbg.dumpTSS(asArgs); });
if (Interrupts.WINDBG.ENABLED || Interrupts.WINDBGRM.ENABLED) {
this.fWinDbg = null;
this.cTrapFaults = 0;
this.fIgnoreNextCheckFault = false;
this.cpu.addIntNotify(Interrupts.WINCB.VECTOR, this.intWindowsCallBack.bind(this));
this.cpu.addIntNotify(Interrupts.WINDBG.VECTOR, this.intWindowsDebugger.bind(this));
}
if (Interrupts.WINDBGRM.ENABLED) {
this.fWinDbgRM = null;
this.cpu.addIntNotify(Interrupts.WINDBGRM.VECTOR, this.intWindowsDebuggerRM.bind(this));
}
this.setReady();
}
/**
* addSegmentInfo(dbgAddr, nSegment, sel, fCode, fPrint)
*
* CONDITIONAL: if (Interrupts.WINDBG.ENABLED || Interrupts.WINDBGRM.ENABLED)
*
* @this {DebuggerX86}
* @param {DbgAddrX86} dbgAddr (address of module name)
* @param {number} nSegment (logical segment number)
* @param {number} sel (current selector)
* @param {boolean} fCode (true if code segment, false if data segment)
* @param {boolean} [fPrint] (false means we're merely monitoring, so let WDEB386 print its own notifications)
*/
addSegmentInfo(dbgAddr, nSegment, sel, fCode, fPrint)
{
let sModule = this.getSZ(dbgAddr);
let seg = this.getSegment(sel);
let len = seg? seg.limit + 1 : 0;
let sSection = (fCode? "_CODE" : "_DATA") + Str.toHex(nSegment, 2);
if (fPrint) this.printf(Messages.MEM, "%s %s(%04X)=#%04X len %0X\n", sModule, (fCode? "code" : "data"), nSegment, sel, len);
let off = 0;
let aSymbols = this.findModuleInfo(sModule, nSegment);
aSymbols[sModule + sSection] = off;
this.addSymbols(sModule, nSegment, sel, off, null, len, aSymbols);
}
/**
* removeSegmentInfo(sel, fPrint)
*
* CONDITIONAL: if (Interrupts.WINDBG.ENABLED || Interrupts.WINDBGRM.ENABLED)
*
* @this {DebuggerX86}
* @param {number} sel
* @param {boolean} [fPrint] (false means we're merely monitoring OR we don't really care about these notifications)
*/
removeSegmentInfo(sel, fPrint)
{
let sModuleRemoved = this.removeSymbols(null, sel);
if (fPrint) {
if (sModuleRemoved) {
this.printf(Messages.MEM, "%s #%04X removed\n", sModuleRemoved, sel);
} else {
this.printf(Messages.MEM, "unable to remove module for segment #%04X\n", sel);
}
}
}
/**
* addSectionInfo(dbgAddr, fCode, fPrint)
*
* CONDITIONAL: if (Interrupts.WINDBG.ENABLED || Interrupts.WINDBGRM.ENABLED)
*
* dbgAddr -> D386_Device_Params structure:
* DD_logical_seg dw ? ; logical segment # from map
* DD_actual_sel dw ? ; actual selector value
* DD_base dd ? ; linear address offset for start of segment
* DD_length dd ? ; actual length of segment
* DD_name df ? ; 16:32 ptr to null terminated module name
* DD_sym_name df ? ; 16:32 ptr to null terminated parent name (eg, "DOS386")
* DD_alias_sel dw ? ; alias selector value (0 = none)
*
* @this {DebuggerX86}
* @param {DbgAddrX86} dbgAddr (address of D386_Device_Params)
* @param {boolean} fCode (true if code section, false if data section)
* @param {boolean} [fPrint] (false means we're merely monitoring, so let WDEB386 print its own notifications)
*/
addSectionInfo(dbgAddr, fCode, fPrint)
{
let nSegment = this.getShort(dbgAddr, 2);
let sel = this.getShort(dbgAddr, 2);
let off = this.getLong(dbgAddr, 4);
let len = this.getLong(dbgAddr, 4);
let dbgAddrModule = this.newAddr(this.getLong(dbgAddr, 4), this.getShort(dbgAddr, 2));
let dbgAddrParent = this.newAddr(this.getLong(dbgAddr, 4), this.getShort(dbgAddr, 2));
// sel = this.getShort(dbgAddr, 2) || sel;
let sParent = this.getSZ(dbgAddrParent).toUpperCase();
let sModule = this.getSZ(dbgAddrModule).toUpperCase();
if (sParent == sModule) {
sParent = "";
} else {
sParent += '!';
}
let sSection = (fCode? "_CODE" : "_DATA") + Str.toHex(nSegment, 2);
if (fPrint) {
/*
* Mimics WDEB386 output, except that WDEB386 only displays a linear address, omitting the selector.
*/
this.printf(Messages.MEM, "%s%s %s(%04X)=%04X:%0X len %0X\n", sParent, sModule, (fCode? "code" : "data"), nSegment, sel, off, len);
}
/*
* TODO: Add support for 32-bit symbols; findModuleInfo() relies on Disk.getModuleInfo(),
* and the Disk component doesn't yet know how to parse 32-bit executables.
*/
let aSymbols = this.findModuleInfo(sModule, nSegment);
aSymbols[sModule + sSection] = off;
this.addSymbols(sModule, nSegment, sel, off, null, len, aSymbols);
}
/**
* removeSectionInfo(nSegment, dbgAddr, fPrint)
*
* CONDITIONAL: if (Interrupts.WINDBG.ENABLED || Interrupts.WINDBGRM.ENABLED)
*
* @this {DebuggerX86}
* @param {number} nSegment (logical segment number)
* @param {DbgAddrX86} dbgAddr (address of module)
* @param {boolean} [fPrint] (false means we're merely monitoring OR we don't really care about these notifications)
*/
removeSectionInfo(nSegment, dbgAddr, fPrint)
{
let sModule = this.getSZ(dbgAddr).toUpperCase();
let sModuleRemoved = this.removeSymbols(sModule, nSegment);
if (fPrint) {
if (sModuleRemoved) {
this.printf(Messages.MEM, "%s %04X removed\n", sModule, nSegment);
} else {
this.printf(Messages.MEM, "unable to remove %s for section %04X\n", sModule, nSegment);
}
}
}
/**
* intWindowsCallBack()
*
* CONDITIONAL: if (Interrupts.WINDBG.ENABLED || Interrupts.WINDBGRM.ENABLED)
*
* This intercepts calls to Windows callback addresses, which use INT 0x30 (aka Transfer Space Faults).
*
* We're only interested in one particular callback: the VW32_Int41Dispatch (0x002A002A) that KERNEL32
* issues as 32-bit executable sections are loaded.
*
* At the time that INT 0x30 occurs, a far 32-bit call has been made, preceded by a near 32-bit call,
* preceded by a 32-bit push of the Windows Debugger function # that would normally be in EAX if this had
* been an actual INT 0x41.
*
* NOTE: Regardless whether we're "handling" INT 0x41 or merely "monitoring" INT 0x41, as far as THIS
* interrupt is concerned, we always let the system process it, because execution never continues at the
* instruction following an INT 0x30; in fact, execution doesn't even continue after the far 32-bit call
* (even though the kernel places a "RET 4" after that call). So, rather than recreate all that automatic
* address popping, we let the system do it for us, since it's designed to work whether a debugger (eg,
* WDEB386's DEBUG VxD) is installed or not.
*
* TODO: Consider "consuming" all VW32_Int41Dispatch callbacks, because the Windows 95 kernel goes to
* great effort to pass those requests on to the DEBUG VxD, which end up going nowhere when the VxD isn't
* loaded (to load it, you must either run WDEB386.EXE or install the VxD via SYSTEM.INI). Regrettably,
* Windows 95 assumes that if WDEB386 support is present, then a DEBUG VxD must be present as well.
*
* @this {DebuggerX86}
* @param {number} addr
* @return {boolean} true to proceed with the INT 0x30 software interrupt
*/
intWindowsCallBack(addr)
{
let cpu = this.cpu;
if (this.fWinDbg != null && cpu.regEAX == 0x002A002A) {
let DX = cpu.regEDX & 0xffff;
let SI = cpu.regESI & 0xffff;
let dbgAddr = this.newAddr(cpu.getSP() + 0x0C, cpu.getSS());
let EAX = this.getLong(dbgAddr);
switch(EAX) {
case Interrupts.WINDBG.LOADSEG32:
/*
* SI == segment type:
* 0x0 code selector
* 0x1 data selector
* DX:EBX -> D386_Device_Params structure (see addSectionInfo() for details)
*/
this.addSectionInfo(this.newAddr(cpu.regEBX, DX), !SI, !!this.fWinDbg);
break;
}
}
return true;
}
/**
* intWindowsDebugger()
*
* CONDITIONAL: if (Interrupts.WINDBG.ENABLED || Interrupts.WINDBGRM.ENABLED)
*
* This intercepts calls to the Windows Debugger protected-mode interface (INT 0x41).
*
* It's enabled if Interrupts.WINDBG.ENABLED is true, but it must ALSO be enabled if
* Interrupts.WINDBGRM.ENABLED is true, because if the latter decides to respond to requests,
* then we must start responding, too. Windows assumes that if INT 0x68 support is present,
* then INT 0x41 support must be present as well.
*
* That is why intWindowsDebuggerRM() will also set this.fWinDbg to true: we MUST return false
* for all INT 0x41 requests, so that all requests are consumed, since there's no guarantee
* that a valid INT 0x41 handler will exist inside the machine.
*
* @this {DebuggerX86}
* @param {number} addr
* @return {boolean} true to proceed with the INT 0x41 software interrupt, false to skip
*/
intWindowsDebugger(addr)
{
let dbgAddr;
let cpu = this.cpu;
let AX = cpu.regEAX & 0xffff;
let BX = cpu.regEBX & 0xffff;
let CX = cpu.regECX & 0xffff;
let DX = cpu.regEDX & 0xffff;
let SI = cpu.regESI & 0xffff;
let DI = cpu.regEDI & 0xffff;
let ES = cpu.segES.sel;
if (this.fWinDbg == null) {
if (AX == Interrupts.WINDBG.IS_LOADED) {
/*
* We're only going to respond to this function if no one else did, in which case,
* we'll set fWinDbg to true and handle additional notifications.
*/
cpu.addIntReturn(addr, function(dbg) {
return function onInt41Return(nLevel) {
if ((cpu.regEAX & 0xffff) != Interrupts.WINDBG.LOADED) {
cpu.regEAX = (cpu.regEAX & ~0xffff) | Interrupts.WINDBG.LOADED;
/*
* TODO: We need a DEBUGGER message category; using the MEM category for now.
*/
dbg.printf(Messages.MEM, "INT 0x41 handling enabled\n");
dbg.fWinDbg = true;
} else {
dbg.printf(Messages.MEM, "INT 0x41 monitoring enabled\n");
dbg.fWinDbg = false;
}
};
}(this));
}
return true;
}
/*
* NOTE: If this.fWinDbg is true, then all cases should return false, because we're taking full
* responsibility for all requests (don't assume there's valid interrupt handler inside the machine).
*/
switch(AX) {
case Interrupts.WINDBG.IS_LOADED: // 0x004F
if (this.fWinDbg) {
cpu.regEAX = (cpu.regEAX & ~0xffff) | Interrupts.WINDBG.LOADED;
this.printf(Messages.MEM, "INT 0x41 handling enabled\n");
}
break;
case Interrupts.WINDBG.LOADSEG: // 0x0050
this.addSegmentInfo(this.newAddr(DI, ES), BX+1, CX, !(SI & 0x1), !!this.fWinDbg);
break;
case Interrupts.WINDBG.FREESEG: // 0x0052
this.removeSegmentInfo(BX);
break;
case Interrupts.WINDBG.KRNLVARS: // 0x005A
/*
* BX = version number of this data (0x3A0)
* DX:CX points to:
* WORD hGlobalHeap ****
* WORD pGlobalHeap ****
* WORD hExeHead ****
* WORD hExeSweep
* WORD topPDB
* WORD headPDB
* WORD topsizePDB
* WORD headTDB ****
* WORD curTDB ****
* WORD loadTDB
* WORD LockTDB
* WORD SelTableLen ****
* DWORD SelTableStart ****
*/
break;
case Interrupts.WINDBG.RELSEG: // 0x005C
case Interrupts.WINDBG.EXITCALL: // 0x0062
case Interrupts.WINDBG.LOADDLL: // 0x0064
case Interrupts.WINDBG.DELMODULE: // 0x0065
case Interrupts.WINDBG.UNKNOWN66: // 0x0066
case Interrupts.WINDBG.UNKNOWN67: // 0x0067
/*
* TODO: Figure out what to do with these notifications, if anything
*/
break;
case Interrupts.WINDBG.LOADHIGH: // 0x005D
case Interrupts.WINDBG.REGDOTCMD: // 0x0070
case Interrupts.WINDBG.CONDBP: // 0xF001
break;
case Interrupts.WINDBG.CHECKFAULT: // 0x007F
if (this.fWinDbg) {
/*
* AX == 0 means handle fault normally, 1 means issue TRAPFAULT
*/
cpu.regEAX = (cpu.regEAX & ~0xffff) | (this.fIgnoreNextCheckFault? 0 : 1);
if (DEBUG) this.println("INT 0x41 CHECKFAULT: fault=" + Str.toHexWord(BX) + " type=" + Str.toHexWord(CX) + " trap=" + !this.fIgnoreNextCheckFault);
}
break;
case Interrupts.WINDBG.TRAPFAULT: // 0x0083
/*
* If we responded with AX == 1 to a preceding CHECKFAULT notification, then we should receive the
* following TRAPFAULT notification; additionally, a TRAPFAULT notification may be issued without
* any CHECKFAULT warning if the user was presented with a fault dialog containing a "Debug" button,
* and the user clicked it.
*
* Regardless, whenever we receive this notification, we allocate a temporary breakpoint at the
* reported fault address.
*/
if (this.fWinDbg) {
dbgAddr = this.newAddr(cpu.regEDX, CX);
if (!this.cTrapFaults++) {
this.println("INT 0x41 TRAPFAULT: fault=" + Str.toHexWord(BX) + " error=" + Str.toHexLong(cpu.regESI) + " addr=" + this.toHexAddr(dbgAddr));
this.addBreakpoint(this.aBreakExec, dbgAddr, true);
this.historyInit(true); // temporary breakpoints don't normally trigger history, but in this case, we want it to
} else {
this.println("TRAPFAULT failed");
this.findBreakpoint(this.aBreakExec, dbgAddr, true, true, true);
this.cTrapFaults = 0;
this.stopCPU();
}
}
break;
case Interrupts.WINDBG.GETSYMBOL: // 0x008D
if (this.fWinDbg) cpu.regEAX = (cpu.regEAX & ~0xffff)|1; // AX == 1 means not found
break;
case Interrupts.WINDBG.LOADSEG32: // 0x0150
/*
* SI == segment type:
* 0x0 code selector
* 0x1 data selector
* DX:EBX -> D386_Device_Params structure (see addSectionInfo() for details)
*/
this.addSectionInfo(this.newAddr(cpu.regEBX, DX), !SI, !!this.fWinDbg);
break;
case Interrupts.WINDBG.FREESEG32: // 0x0152
/*
* BX == segment number
* DX:EDI -> module name
*/
this.removeSectionInfo(BX, this.newAddr(cpu.regEDI, DX));
break;
default:
if (DEBUG && this.fWinDbg) {
this.println("INT 0x41: " + Str.toHexWord(AX));
}
break;
}
/*
* Let's try to limit the scope of any "gt" command by resetting this flag after any INT 0x41
*/
this.fIgnoreNextCheckFault = false;
return !this.fWinDbg;
}
/**
* intWindowsDebuggerRM()
*
* CONDITIONAL: if (Interrupts.WINDBGRM.ENABLED)
*
* This intercepts calls to the Windows Debugger real-mode interface (INT 0x68).
*
* @this {DebuggerX86}
* @param {number} addr
* @return {boolean} true to proceed with the INT 0x68 software interrupt, false to skip
*/
intWindowsDebuggerRM(addr)
{
let cpu = this.cpu;
let AL = cpu.regEAX & 0xff;
let AH = (cpu.regEAX >> 8) & 0xff;
let BX = cpu.regEBX & 0xffff;
let CX = cpu.regECX & 0xffff;
let DX = cpu.regEDX & 0xffff;
let DI = cpu.regEDI & 0xffff;
let ES = cpu.segES.sel;
if (this.fWinDbgRM == null) {
if (AH == Interrupts.WINDBGRM.IS_LOADED) {
/*
* It looks like IFSHLP.SYS issues a preliminary INT 0x68 before Windows 95 gets rolling,
* and the Windows Debugger will not have had a chance to load yet, so we need to ignore
* that call. We detect IFSHLP.SYS by looking for "IFS$" in the caller's code segment,
* where the IFSHLP device driver header is located.
*/
if (cpu.getLong((cpu.segCS.sel << 4) + 0x0A) == 0x24534649) {
if (DEBUG) this.println("Ignoring INT 0x68 from IFSHLP.SYS");
return true;
}
/*
* Ditto for WDEB386 itself, which presumably wants to avoid loading on top of itself.
*/
if (cpu.getLong((cpu.segCS.sel << 4) + 0x5F) == 0x42454457) {
if (DEBUG) this.println("Ignoring INT 0x68 from WDEB386.EXE");
return true;
}
/*
* We're only going to respond to this function if no one else did, in which case, we'll set
* fWinDbgRM to true and handle additional notifications.
*/
cpu.addIntReturn(addr, function(dbg) {
return function onInt68Return(nLevel) {
if ((cpu.regEAX & 0xffff) != Interrupts.WINDBGRM.LOADED) {
cpu.regEAX = (cpu.regEAX & ~0xffff) | Interrupts.WINDBGRM.LOADED;
dbg.printf(Messages.MEM, "INT 0x68 handling enabled\n");
/*
* If we turn on INT 0x68 handling, we must also turn on INT 0x41 handling,
* because Windows assumes that the latter handler exists whenever the former does.
*/
dbg.fWinDbg = dbg.fWinDbgRM = true;
} else {
dbg.printf(Messages.MEM, "INT 0x68 monitoring enabled\n");
dbg.fWinDbgRM = false;
}
};
}(this));
}
return true;
}
/*
* NOTE: If this.fWinDbgRM is true, then all cases should return false, because we're taking full
* responsibility for all requests (don't assume there's valid interrupt handler inside the machine).
*/
switch(AH) {
case Interrupts.WINDBGRM.IS_LOADED: // 0x43
if (this.fWinDbgRM) {
cpu.regEAX = (cpu.regEAX & ~0xffff) | Interrupts.WINDBGRM.LOADED;
}
break;
case Interrupts.WINDBGRM.PREP_PMODE: // 0x44
if (this.fWinDbgRM) {
/*
* Use our fancy new "call break" mechanism to obtain a special address that will
* trap all calls, routing control to the specified function (callWindowsDebuggerPMInit).
*/
let a = cpu.segCS.addCallBreak(this.callWindowsDebuggerPMInit.bind(this));
if (a) {
cpu.regEDI = a[0]; // ES:EDI receives the "call break" address
cpu.setES(a[1]);
}
}
break;
case Interrupts.WINDBGRM.FREESEG: // 0x48
this.removeSegmentInfo(BX);
break;
case Interrupts.WINDBGRM.REMOVESEGS: // 0x4F
/*
* TODO: This probably just signals the end of module loading; nothing is required, but we should
* clean up whatever we can....
*/
break;
case Interrupts.WINDBGRM.LOADSEG: // 0x50
if (AL == 0x20) {
/*
* Real-mode EXE
* CX == paragraph
* ES:DI -> module name
*/
this.addSegmentInfo(this.newAddr(DI, ES), 0, CX, true, !!this.fWinDbgRM);
}
else if (AL < 0x80) {
/*
* AL == segment type:
* 0x00 code selector
* 0x01 data selector
* 0x10 code segment
* 0x11 data segment
* 0x40 code segment & sel
* 0x41 data segment & sel
* BX == segment #
* CX == actual segment/selector
* DX == actual selector (if 0x40 or 0x41)
* ES:DI -> module name
*/
this.addSegmentInfo(this.newAddr(DI, ES), BX+1, (AL & 0x40)? DX : CX, !(AL & 0x1), !!this.fWinDbgRM);
}
else {
/*
* AL == segment type:
* 0x80 device driver code seg
* 0x81 device driver data seg
* ES:DI -> D386_Device_Params structure (see addSectionInfo() for details)
*/
this.addSectionInfo(this.newAddr(DI, ES), !(AL & 0x1), !!this.fWinDbgRM);
}
if (this.fWinDbgRM) {
cpu.regEAX = (cpu.regEAX & ~0xff) | 0x01;
}
break;
default:
if (DEBUG && this.fWinDbgRM) {
this.println("INT 0x68: " + Str.toHexByte(AH));
}
break;
}
return !this.fWinDbgRM;
}
/**
* callWindowsDebuggerPMInit()
*
* CONDITIONAL: if (Interrupts.WINDBGRM.ENABLED)
*
* This intercepts calls to the Windows Debugger "PMInit" interface; eg:
*
* AL = function code
*
* 0 - initialize IDT
* ES:EDI points to protected mode IDT
*
* 1 - initialize page checking
* BX = physical selector
* ECX = linear bias
*
* 2 - specify that debug queries are supported
*
* 3 - initialize spare PTE
* EBX = linear address of spare PTE
* EDX = linear address the PTE represents
*
* 4 - set Enter/Exit VMM routine address
* EBX = Enter VMM routine address
* ECX = Exit VMM routine address
* EDX = $_Debug_Out_Service address
* ESI = $_Trace_Out_Service address
* The VMM enter/exit routines must return with a retfd
*
* 5 - get debugger size/physical address
* returns: AL = 0 (don't call AL = 1)
* ECX = size in bytes
* ESI = starting physical code/data address
*
* 6 - set debugger base/initialize spare PTE
* EBX = linear address of spare PTE
* EDX = linear address the PTE represents
* ESI = starting linear address of debug code/data
*
* 7 - enable memory context functions
*
* @this {DebuggerX86}
* @return {boolean} (must always return false to skip the call, because the call is using a CALLBREAK address)
*/
callWindowsDebuggerPMInit()
{
let cpu = this.cpu;
let AL = cpu.regEAX & 0xff;
if (MAXDEBUG) this.println("INT 0x68 callback: " + Str.toHexByte(AL));
if (AL == 5) {
cpu.regECX = cpu.regESI = 0; // our in-machine debugger footprint is zero
cpu.regEAX = (cpu.regEAX & ~0xff) | 0x01; // TODO: Returning a "don't call" response sounds good, but what does it REALLY mean?
}
return false;
}
/**
* setBinding(sHTMLType, sBinding, control, sValue)
*
* @this {DebuggerX86}
* @param {string} sHTMLType is the type of the HTML control (eg, "button", "list", "text", "submit", "textarea", "canvas")
* @param {string} sBinding is the value of the 'binding' parameter stored in the HTML control's "data-value" attribute (eg, "debugInput")
* @param {HTMLElement} control is the HTML control DOM object (eg, HTMLButtonElement)
* @param {string} [sValue] optional data value
* @return {boolean} true if binding was successful, false if unrecognized binding request
*/
setBinding(sHTMLType, sBinding, control, sValue)
{
let dbg = this;
switch (sBinding) {
case "debugInput":
this.bindings[sBinding] = control;
this.controlDebug = /** @type {HTMLInputElement} */ (control);
/*
* For halted machines, this is fine, but for auto-start machines, it can be annoying.
*
* controlInput.focus();
*/
control.onkeydown = function onKeyDownDebugInput(event) {
let sCmd;
if (event.keyCode == Keys.KEYCODE.CR) {
sCmd = dbg.controlDebug.value;
dbg.controlDebug.value = "";
dbg.doCommands(sCmd, true);
}
else if (event.keyCode == Keys.KEYCODE.ESC) {
dbg.controlDebug.value = sCmd = "";
}
else {
if (event.keyCode == Keys.KEYCODE.UP) {
sCmd = dbg.getPrevCommand();
}
else if (event.keyCode == Keys.KEYCODE.DOWN) {
sCmd = dbg.getNextCommand();
}
if (sCmd != null) {
let cch = sCmd.length;
dbg.controlDebug.value = sCmd;
dbg.controlDebug.setSelectionRange(cch, cch);
}
}
if (sCmd != null && event.preventDefault) event.preventDefault();
};
return true;
case "debugEnter":
this.bindings[sBinding] = control;
Web.onClickRepeat(
control,
500, 100,
function onClickDebugEnter(fRepeat) {
if (dbg.controlDebug) {
let sCommands = dbg.controlDebug.value;
dbg.controlDebug.value = "";
dbg.doCommands(sCommands, true);
return true;
}
if (DEBUG) dbg.log("no debugger input buffer");
return false;
}
);
return true;
case "step":
this.bindings[sBinding] = control;
Web.onClickRepeat(
control,
500, 100,
function onClickStep(fRepeat) {
let fCompleted = false;
if (!dbg.isBusy(true)) {
dbg.setBusy(true);
fCompleted = dbg.stepCPU(fRepeat? 1 : 0);
dbg.setBusy(false);
}
return fCompleted;
}
);
return true;
default:
break;
}
return false;
}
/**
* updateFocus()
*
* @this {DebuggerX86}
*/
updateFocus()
{
if (this.controlDebug) this.controlDebug.focus();
}
/**
* getCPUMode()
*
* @this {DebuggerX86}
* @return {boolean} (true if protected mode, false if not)
*/
getCPUMode()
{
return !!(this.cpu && (this.cpu.regCR0 & X86.CR0.MSW.PE) && !(this.cpu.regPS & X86.PS.VM));
}
/**
* getAddressType()
*
* @this {DebuggerX86}
* @return {number}
*/
getAddressType()
{
return this.getCPUMode()? DebuggerX86.ADDRTYPE.PROT : DebuggerX86.ADDRTYPE.REAL;