summaryrefslogtreecommitdiffstats
path: root/private/mvdm/dbg/dbg.c
blob: a2ab1de9204021cb440908102d639ad512e2da02 (plain) (blame)
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
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
/*
 *  dbg.c - Main Module of DBG DLL.
 *
 *  BobDay 13-Jan-1992 Created
 *
 * WARNING: THIS MODULE APPEARS TO HAVE MANY FUNCTIONS THAT APPEAR IDENTICAL
 *          THEY ARE NOT IDENTICAL.  THEY ARE NEARLY IDENTICAL.  IF YOU CHOOSE
 *          TO COMBINE SIMILAR FUNCTIONALITY INTO SEVERAL SMALLER ROUTINES,
 *          BE VERY CAREFUL TO LOOK FOR THE SMALL SUBTLE DIFFERENCES.
 */

#include <nt.h>
#include <ntrtl.h>
#include <nturtl.h>
#include <ntdbg.h>
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <mvdm.h>
#include <bop.h>
#include <softpc.h>
#include <dbgexp.h>
#include <dbgsvc.h>
#include <vdmdbg.h>
#include <dbginfo.h>

#define MAX_MODULE  64

#define MAX_DBG_FRAME   10

ULONG   ulTHHOOK = 0L;          // Address registered from 16-bit world
LPVOID  lpRemoteAddress = NULL; // Address registered from WOW32
DWORD   lpRemoteBlock   = 0;    // Address registered from WOW32
BOOL    f386;

typedef struct _trapframe {
    WORD    wCode;          /* Noise from DbgDispatchBop */
    WORD    wAX;            /* AX at time of fault */
    WORD    wDS;            /* DS at time of fault */
    WORD    wRetIP;         /* Noise from DPMI */
    WORD    wRetCS;         /* Noise from DPMI */
    WORD    wErrCode;       /* Noise from 16-bit kernel */
    WORD    wIP;            /* IP at time of fault */
    WORD    wCS;            /* CS at time of fault */
    WORD    wFlags;         /* Flags at time of fault */
    WORD    wSP;            /* SS at time of fault */
    WORD    wSS;            /* SP at time of fault */
} TFRAME16;
typedef TFRAME16 UNALIGNED *PTFRAME16;

typedef struct _faultframe {
    WORD    wES;            /* ES at time of fault */
    WORD    wDS;            /* DS at time of fault */
    WORD    wDI;            /* DI at time of fault */
    WORD    wSI;            /* SI at time of fault */
    WORD    wTempBP;        /* Noise from 16-bit kernel stack frame */
    WORD    wTempSP;        /* Noise from 16-bit kernel stack frame */
    WORD    wBX;            /* BX at time of fault */
    WORD    wDX;            /* DX at time of fault */
    WORD    wCX;            /* CX at time of fault */
    WORD    wAX;            /* AX at time of fault */
    WORD    wBP;            /* BP at time of fault */
    WORD    npszMsg;        /* Noise from 16-bit kernel */
    WORD    wPrevIP;        /* Noise from DPMI */
    WORD    wPrevCS;        /* Noise from DPMI */
    WORD    wRetIP;         /* Noise from DPMI */
    WORD    wRetCS;         /* Noise from DPMI */
    WORD    wErrCode;       /* Noise from 16-bit kernel */
    WORD    wIP;            /* IP at time of fault */
    WORD    wCS;            /* CS at time of fault */
    WORD    wFlags;         /* Flags at time of fault */
    WORD    wSP;            /* SS at time of fault */
    WORD    wSS;            /* SP at time of fault */
} FFRAME16;
typedef FFRAME16 UNALIGNED *PFFRAME16;

typedef struct _newtaskframe {
    DWORD   dwNoise;            /* Noise from InitTask         */
    DWORD   dwModulePath;       /* Module path address         */
    DWORD   dwModuleName;       /* Module name address         */
    WORD    hModule;            /* 16-bit Module handle        */
    WORD    hTask;              /* 16-bit Task handle          */
    WORD    wFlags;             /* Flags at time to task start */
    WORD    wDX;                /* DX at time of task start    */
    WORD    wBX;                /* BX at time of task start    */
    WORD    wES;                /* ES at time of task start    */
    WORD    wCX;                /* CX at time of task start    */
    WORD    wAX;                /* AX at time of task start    */
    WORD    wDI;                /* DI at time of task start    */
    WORD    wSI;                /* SI at time of task start    */
    WORD    wDS;                /* DS at time of task start    */
    WORD    wBP;                /* BP at time of task start    */
    WORD    wIP;                /* IP for task start           */
    WORD    wCS;                /* CS for task start           */
} NTFRAME16;
typedef NTFRAME16 UNALIGNED *PNTFRAME16;

#pragma pack(2)

typedef struct _stoptaskframe {
    WORD    wCode;              /* Noise from BOP Dispatcher  */
    DWORD   dwModulePath;       /* Module path address        */
    DWORD   dwModuleName;       /* Module name address        */
    WORD    hModule;            /* 16-bit Module handle       */
    WORD    hTask;              /* 16-bit Task handle         */
} STFRAME16;
typedef STFRAME16 UNALIGNED *PSTFRAME16;

typedef struct _newdllframe {
    WORD    wCode;              /* Noise from DbgDispatchBop  */
    DWORD   dwModulePath;       /* Module path address        */
    DWORD   dwModuleName;       /* Module name address        */
    WORD    hModule;            /* 16-bit Module handle       */
    WORD    hTask;              /* 16-bit Task handle         */
    WORD    wDS;                /* DS at time of dll start    */
    WORD    wAX;                /* AX at time of dll start    */
    WORD    wIP;                /* IP at time of dll start    */
    WORD    wCS;                /* CS at time of dll start    */
    WORD    wFlags;             /* Flags at time of dll start */
} NDFRAME16;
typedef NDFRAME16 UNALIGNED *PNDFRAME16;

#pragma pack()


BOOL  fDebugged = FALSE;

/* DBGInit - DBG Initialiazation routine. (This name may change when DBG is
 *	     converted to DLL).
 *
 * Entry
 *	None
 *
 * Exit
 *	None
 */

BOOL DBGInit (int argc, char *argv[])
{
    HANDLE      hProcess;
    HANDLE      MyDebugPort;
    DWORD       st;

    hProcess = NtCurrentProcess();

    st = NtQueryInformationProcess(
                hProcess,
                ProcessDebugPort,
                (PVOID)&MyDebugPort,
                sizeof(MyDebugPort),
                NULL );
    if ( NT_SUCCESS(st) ) {
        fDebugged = (MyDebugPort != NULL);
    } else {
        fDebugged = FALSE;
    }
    return( TRUE );
}


BOOL SendVDMEvent(
    LPDWORD lpParams
) {
    BOOL    fResult;

    // Slimyness to determine whether the exception was handled or not.

    try {
        RaiseException( STATUS_VDM_EVENT,
                        0,
                        4,
                        lpParams );
        fResult = TRUE;
    } except(EXCEPTION_EXECUTE_HANDLER) {
        fResult = FALSE;
    }
    return( fResult );
}


/* GetNormalContext() - Get the VDM's current context
 *
 * Most of the routines that send VDMEvents need to have a context record
 * associated with them.  This routine is a quick way to get most of the
 * general registers.  Redundant work is being done because AX for example
 * is often on the stack and as such must really be pulled from the frame.
 * Hopefully this is OK because it is fast.
 */
#ifdef i386
#define PX86    px86
#else
#define PX86    0
#endif

VOID GetNormalContext(
    LPVDMCONTEXT        lpvcContext,
    LPVDMINTERNALINFO   lpviInfo,
    LPDWORD             lpdwExceptionParams,
    WORD                wEventType,
#ifdef i386
    PX86CONTEXT         px86
#else
    DWORD               dw
#endif
) {
    //
    // Everything defaults to 0.
    //
    RtlFillMemory( lpvcContext, sizeof(VDMCONTEXT), (UCHAR)0 );
    RtlFillMemory( lpviInfo, sizeof(VDMINTERNALINFO), (UCHAR)0 );
    RtlFillMemory( lpdwExceptionParams, 4*sizeof(DWORD), (UCHAR)0 );

    //
    // Fill in the exception parameters
    //
    lpdwExceptionParams[0] = MAKELONG( wEventType, 0 );
    lpdwExceptionParams[3] = (DWORD)lpviInfo;

    //
    // Fill in the internal info structure
    //
    lpviInfo->wKernelSeg      = HIWORD(ulTHHOOK);
    lpviInfo->dwOffsetTHHOOK  = (DWORD)(LOWORD(ulTHHOOK));
    lpviInfo->vdmContext      = lpvcContext;
    lpviInfo->lpRemoteAddress = lpRemoteAddress;
    lpviInfo->lpRemoteBlock   = lpRemoteBlock;
    lpviInfo->f386            = f386;

    //
    // Fill in the context structure
    //
    lpvcContext->SegEs = (ULONG)getES();
    lpvcContext->SegDs = (ULONG)getDS();
    lpvcContext->SegCs = (ULONG)getCS();
    lpvcContext->SegSs = (ULONG)getSS();

    lpvcContext->EFlags = 0;            // BUGBUG - We need the 16-bit's Flags

#ifdef i386
    //
    // On x86 systems, we really might have some data in the FS and GS
    // registers.  Hopefully the code path through DOSX and KERNEL don't
    // modify them (I traced it and it didn't seem to).  Here is where
    // we attempt to recover them.
    //
    lpvcContext->SegGs = px86->SegGs;
    lpvcContext->SegFs = px86->SegFs;

    // On x86 systems, we really might have some data in the high words
    // of the registers.  Hopefully the code path through DOSX and KERNEL
    // don't modify them (I trace it and it didn't seem to).  Here is where
    // we attempt to recover them.

    lpvcContext->Edi    = MAKELONG(getDI(),    HIWORD(px86->Edi   ));
    lpvcContext->Esi    = MAKELONG(getSI(),    HIWORD(px86->Esi   ));
    lpvcContext->Ebx    = MAKELONG(getBX(),    HIWORD(px86->Ebx   ));
    lpvcContext->Edx    = MAKELONG(getDX(),    HIWORD(px86->Edx   ));
    lpvcContext->Ecx    = MAKELONG(getCX(),    HIWORD(px86->Ecx   ));
    lpvcContext->Eax    = MAKELONG(getAX(),    HIWORD(px86->Eax   ));

    lpvcContext->Ebp    = MAKELONG(getBP(),    HIWORD(px86->Ebp   ));
    lpvcContext->Eip    = MAKELONG(getIP(),    HIWORD(px86->Eip   ));
    lpvcContext->Esp    = MAKELONG(getSP(),    HIWORD(px86->Esp   ));

#else
    // Leave GS & FS zero.

    lpvcContext->Edi = (ULONG)getDI();
    lpvcContext->Esi = (ULONG)getSI();
    lpvcContext->Ebx = (ULONG)getBX();
    lpvcContext->Edx = (ULONG)getDX();
    lpvcContext->Ecx = (ULONG)getCX();
    lpvcContext->Eax = (ULONG)getAX();

    lpvcContext->Ebp = (ULONG)getBP();
    lpvcContext->Eip = (ULONG)getIP();
    lpvcContext->Esp = (ULONG)getSP();

    if ( (getMSW() & MSW_PE) == 0 ) {
        lpvcContext->EFlags |= V86FLAGS_V86;
    }

    // BUGBUG initialize the GDT/LDT stuff for the emulator
#endif
}

void SegmentLoad(
    LPSTR   lpModuleName,
    LPSTR   lpPathName,
    WORD    Selector,
    WORD    Segment,
    BOOL    fData

) {

    if ( fDebugged ) {
        DWORD           EventParams[4];
        VDMCONTEXT      vcContext;
        VDMINTERNALINFO viInfo;
        SEGMENT_NOTE    se;
        UINT            length;
#ifdef i386
        PX86CONTEXT     px86;

        // X86 Only, get pointer to Register Context Block
        px86 = getIntelRegistersPointer();
#endif

        GetNormalContext( &vcContext, &viInfo, EventParams, DBG_SEGLOAD, PX86 );

        RtlFillMemory( &se, sizeof(se), (UCHAR)0 );

        se.Selector1 = Selector;
        se.Segment   = Segment;
        se.Type      = fData;

        length = strlen(lpPathName);
        if ( length >= MAX_PATH16 ) {
            length = MAX_PATH16-1;
        }
        strncpy( se.FileName, lpPathName, length );
        se.FileName[length] = '\0';

        length = strlen(lpModuleName);
        if ( length >= MAX_MODULE ) {
            length = MAX_MODULE-1;
        }
        strncpy( se.Module, lpModuleName, length );
        se.Module[length] = '\0';

        EventParams[2] = (DWORD)&se;

        SendVDMEvent( EventParams );
    }
}

void SegmentMove(
    WORD    OldSelector,
    WORD    NewSelector
) {

    if ( fDebugged ) {
        DWORD           EventParams[4];
        VDMCONTEXT      vcContext;
        VDMINTERNALINFO viInfo;
        SEGMENT_NOTE    se;
#ifdef i386
        PX86CONTEXT     px86;

        // X86 Only, get pointer to Register Context Block
        px86 = getIntelRegistersPointer();
#endif

        GetNormalContext( &vcContext, &viInfo, EventParams, DBG_SEGMOVE, PX86 );

        RtlFillMemory( &se, sizeof(se), (UCHAR)0 );

        se.Selector1   = OldSelector;
        se.Selector2   = NewSelector;

        EventParams[2] = (DWORD)&se;

        SendVDMEvent( EventParams );
    }
}

void SegmentFree(
    WORD    Selector,
    BOOL    fBPRelease
) {

    if ( fDebugged ) {
        DWORD           EventParams[4];
        VDMCONTEXT      vcContext;
        VDMINTERNALINFO viInfo;
        SEGMENT_NOTE    se;
#ifdef i386
        PX86CONTEXT     px86;

        // X86 Only, get pointer to Register Context Block
        px86 = getIntelRegistersPointer();
#endif

        GetNormalContext( &vcContext, &viInfo, EventParams, DBG_SEGFREE, PX86 );

        RtlFillMemory( &se, sizeof(se), (UCHAR)0 );

        se.Selector1   = Selector;
        se.Type        = (WORD)fBPRelease;

        EventParams[2] = (DWORD)&se;

        SendVDMEvent( EventParams );
    }
}

void ModuleLoad(
    LPSTR   lpModuleName,
    LPSTR   lpPathName,
    WORD    Segment,
    DWORD   Length
) {

    if ( fDebugged ) {
        DWORD           EventParams[4];
        VDMCONTEXT      vcContext;
        VDMINTERNALINFO viInfo;
        SEGMENT_NOTE    se;
        UINT            length;
#ifdef i386
        PX86CONTEXT     px86;

        // X86 Only, get pointer to Register Context Block
        px86 = getIntelRegistersPointer();
#endif

        GetNormalContext( &vcContext, &viInfo, EventParams, DBG_MODLOAD, PX86 );

        RtlFillMemory( &se, sizeof(se), (UCHAR)0 );

        se.Segment = Segment;
        se.Length  = Length;

        length = strlen(lpPathName);
        if ( length >= MAX_PATH16 ) {
            length = MAX_PATH16-1;
        }
        strncpy( se.FileName, lpPathName, length );
        se.FileName[length] = '\0';

        length = strlen(lpModuleName);
        if ( length >= MAX_MODULE ) {
            length = MAX_MODULE-1;
        }
        strncpy( se.Module, lpModuleName, length );
        se.Module[length] = '\0';

        EventParams[2] = (DWORD)&se;

        SendVDMEvent( EventParams );
    }
}

void ModuleSegmentMove(
    LPSTR   lpModuleName,
    LPSTR   lpPathName,
    WORD    OldSelector,
    WORD    NewSelector
) {

    if ( fDebugged ) {
        DWORD           EventParams[4];
        VDMCONTEXT      vcContext;
        VDMINTERNALINFO viInfo;
        SEGMENT_NOTE    se;
        UINT            length;
#ifdef i386
        PX86CONTEXT     px86;

        // X86 Only, get pointer to Register Context Block
        px86 = getIntelRegistersPointer();
#endif

        GetNormalContext( &vcContext, &viInfo, EventParams, DBG_SEGMOVE, PX86 );

        RtlFillMemory( &se, sizeof(se), (UCHAR)0 );

        se.Segment     = OldSelector;
        se.Selector2   = NewSelector;
        length = strlen(lpPathName);

        if ( length >= MAX_PATH16 ) {
            length = MAX_PATH16-1;
        }
        strncpy( se.FileName, lpPathName, length );
        se.FileName[length] = '\0';

        length = strlen(lpModuleName);
        if ( length >= MAX_MODULE ) {
            length = MAX_MODULE-1;
        }
        strncpy( se.Module, lpModuleName, length );
        se.Module[length] = '\0';

        EventParams[2] = (DWORD)&se;

        SendVDMEvent( EventParams );
    }
}

void ModuleFree(
    LPSTR   lpModuleName,
    LPSTR   lpPathName
) {

    if ( fDebugged ) {
        DWORD           EventParams[4];
        VDMCONTEXT      vcContext;
        VDMINTERNALINFO viInfo;
        SEGMENT_NOTE    se;
        UINT            length;
#ifdef i386
        PX86CONTEXT     px86;

        // X86 Only, get pointer to Register Context Block
        px86 = getIntelRegistersPointer();
#endif

        GetNormalContext( &vcContext, &viInfo, EventParams, DBG_MODFREE, PX86 );

        RtlFillMemory( &se, sizeof(se), (UCHAR)0 );

        length = strlen(lpPathName);
        if ( length >= MAX_PATH16 ) {
            length = MAX_PATH16-1;
        }
        strncpy( se.FileName, lpPathName, length );
        se.FileName[length] = '\0';

        length = strlen(lpModuleName);
        if ( length >= MAX_MODULE ) {
            length = MAX_MODULE-1;
        }
        strncpy( se.Module, lpModuleName, length );
        se.Module[length] = '\0';

        EventParams[2] = (DWORD)&se;

        SendVDMEvent( EventParams );
    }
}

BOOL SingleStep(
    PTFRAME16   pTFrame
) {
    BOOL        fResult;

    fResult = FALSE;        // Default to Event not handled

    if ( fDebugged ) {
        DWORD           EventParams[4];
        VDMCONTEXT      vcContext;
        VDMINTERNALINFO viInfo;
#ifdef i386
        PX86CONTEXT     px86;

        // X86 Only, get pointer to Register Context Block
        px86 = getIntelRegistersPointer();
#endif

        GetNormalContext( &vcContext, &viInfo, EventParams, DBG_SINGLESTEP, PX86 );

        vcContext.SegDs  = (ULONG)pTFrame->wDS;
        vcContext.SegCs  = (ULONG)pTFrame->wCS;
        vcContext.SegSs  = (ULONG)pTFrame->wSS;

#ifdef i386
        //
        // On x86 systems, we really might have some data in the high words
        // of these registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
        // blow them away.  Here is where we attempt to recover them.
        //
        vcContext.Eax    = MAKELONG(pTFrame->wAX,   HIWORD(px86->Eax   ));
        vcContext.Eip    = MAKELONG(pTFrame->wIP,   HIWORD(px86->Eip   ));
        vcContext.Esp    = MAKELONG(pTFrame->wSP,   HIWORD(px86->Esp   ));
        vcContext.EFlags = MAKELONG(pTFrame->wFlags,HIWORD(px86->EFlags));
#else
        vcContext.Eax    = (ULONG)pTFrame->wAX;

        vcContext.Eip    = (ULONG)pTFrame->wIP;
        vcContext.Esp    = (ULONG)pTFrame->wSP;
        vcContext.EFlags = (ULONG)pTFrame->wFlags;

        if ( (getMSW() & MSW_PE) == 0 ) {
            vcContext.EFlags |= V86FLAGS_V86;
        }
#endif

        fResult = SendVDMEvent( EventParams );

#ifdef i386
        //
        // On x86 systems, we really might have some data in the FS and GS
        // registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
        // blow them away.  Here is where we attempt to restore them.
        //
        px86->SegGs = vcContext.SegGs;
        px86->SegFs = vcContext.SegFs;
#else
        // No need to set FS,GS, they don't exist
#endif

        setES( (WORD)vcContext.SegEs );
        pTFrame->wDS = (WORD)vcContext.SegDs;
        pTFrame->wCS = (WORD)vcContext.SegCs;
        pTFrame->wSS = (WORD)vcContext.SegSs;

#ifdef i386
        //
        // On x86 systems, we really might have some data in the high words
        // of these registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
        // blow them away.  Here is where we attempt to restore them.
        //
        setEDI( vcContext.Edi );
        setESI( vcContext.Esi );
        setEBX( vcContext.Ebx );
        setEDX( vcContext.Edx );
        setECX( vcContext.Ecx );

        pTFrame->wAX = LOWORD(vcContext.Eax);
        px86->Eax = MAKELONG(LOWORD(px86->Eax),HIWORD(vcContext.Eax));

        setEBP( vcContext.Ebp );

        pTFrame->wIP = LOWORD(vcContext.Eip);
        px86->Eip = MAKELONG(LOWORD(px86->Eip),HIWORD(vcContext.Eip));

        pTFrame->wFlags = LOWORD(vcContext.EFlags);
        px86->EFlags = MAKELONG(LOWORD(px86->EFlags),HIWORD(vcContext.EFlags));

        pTFrame->wSP = LOWORD(vcContext.Esp);
        px86->Esp = MAKELONG(LOWORD(px86->Esp),HIWORD(vcContext.Esp));
#else
        setDI( (WORD)vcContext.Edi );
        setSI( (WORD)vcContext.Esi );
        setBX( (WORD)vcContext.Ebx );
        setDX( (WORD)vcContext.Edx );
        setCX( (WORD)vcContext.Ecx );
        pTFrame->wAX = (WORD)vcContext.Eax;


        setBP( (WORD)vcContext.Ebp );
        pTFrame->wIP    = (WORD)vcContext.Eip;
        pTFrame->wFlags = (WORD)vcContext.EFlags;
        pTFrame->wSP    = (WORD)vcContext.Esp;
#endif
    }
    return( fResult );
}

BOOL Breakpoint(
    PTFRAME16   pTFrame
) {
    BOOL        fResult;

    fResult = FALSE;        // Default to Event not handled

    if ( fDebugged ) {
        DWORD           EventParams[4];
        VDMCONTEXT      vcContext;
        VDMINTERNALINFO viInfo;
#ifdef i386
        PX86CONTEXT     px86;

        // X86 Only, get pointer to Register Context Block
        px86 = getIntelRegistersPointer();
#endif

        GetNormalContext( &vcContext, &viInfo, EventParams, DBG_BREAK, PX86 );

        vcContext.SegDs  = (ULONG)pTFrame->wDS;
        vcContext.SegCs  = (ULONG)pTFrame->wCS;
        vcContext.SegSs  = (ULONG)pTFrame->wSS;

#ifdef i386
        //
        // On x86 systems, we really might have some data in the high words
        // of these registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
        // blow them away.  Here is where we attempt to recover them.
        //
        vcContext.Eax    = MAKELONG(pTFrame->wAX,   HIWORD(px86->Eax   ));

        vcContext.Eip    = MAKELONG(pTFrame->wIP,   HIWORD(px86->Eip   ));
        vcContext.Esp    = MAKELONG(pTFrame->wSP,   HIWORD(px86->Esp   ));
        vcContext.EFlags = MAKELONG(pTFrame->wFlags,HIWORD(px86->EFlags));
#else
        vcContext.Eax    = (ULONG)pTFrame->wAX;

        vcContext.Ebp    = (ULONG)getBP();
        vcContext.Eip    = (ULONG)pTFrame->wIP;
        vcContext.Esp    = (ULONG)pTFrame->wSP;
        vcContext.EFlags = (ULONG)pTFrame->wFlags;

        if ( (getMSW() & MSW_PE) == 0 ) {
            vcContext.EFlags |= V86FLAGS_V86;
        }
#endif

        fResult = SendVDMEvent( EventParams );

#ifdef i386
        //
        // On x86 systems, we really might have some data in the FS and GS
        // registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
        // blow them away.  Here is where we attempt to restore them.
        //
        px86->SegGs = (WORD)vcContext.SegGs;
        px86->SegFs = (WORD)vcContext.SegFs;
#else
        // No need to set FS,GS, they don't exist
#endif

        setES( (WORD)vcContext.SegEs );
        pTFrame->wDS = (WORD)vcContext.SegDs;
        pTFrame->wCS = (WORD)vcContext.SegCs;
        pTFrame->wSS = (WORD)vcContext.SegSs;

#ifdef i386
        //
        // On x86 systems, we really might have some data in the high words
        // of these registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
        // blow them away.  Here is where we attempt to restore them.
        //
        setEDI( vcContext.Edi );
        setESI( vcContext.Esi );
        setEBX( vcContext.Ebx );
        setEDX( vcContext.Edx );
        setECX( vcContext.Ecx );

        pTFrame->wAX = LOWORD(vcContext.Eax);
        px86->Eax = MAKELONG(LOWORD(px86->Eax),HIWORD(vcContext.Eax));

        setEBP( vcContext.Ebp );

        pTFrame->wIP = LOWORD(vcContext.Eip);
        px86->Eip = MAKELONG(LOWORD(px86->Eip),HIWORD(vcContext.Eip));

        pTFrame->wFlags = LOWORD(vcContext.EFlags);
        px86->EFlags = MAKELONG(LOWORD(px86->EFlags),HIWORD(vcContext.EFlags));

        pTFrame->wSP = LOWORD(vcContext.Esp);
        px86->Esp = MAKELONG(LOWORD(px86->Esp),HIWORD(vcContext.Esp));
#else
        setDI( (WORD)vcContext.Edi );
        setSI( (WORD)vcContext.Esi );
        setBX( (WORD)vcContext.Ebx );
        setDX( (WORD)vcContext.Edx );
        setCX( (WORD)vcContext.Ecx );
        pTFrame->wAX = (WORD)vcContext.Eax;


        setBP( (WORD)vcContext.Ebp );
        pTFrame->wIP    = (WORD)vcContext.Eip;
        pTFrame->wFlags = (WORD)vcContext.EFlags;
        pTFrame->wSP    = (WORD)vcContext.Esp;
#endif

    }

    return( fResult );
}

BOOL GPFault(
    PFFRAME16   pFFrame
) {
    DWORD           EventParams[4];
    BOOL            fResult;
    VDMCONTEXT      vcContext;
    VDMINTERNALINFO viInfo;
#ifdef i386
    PX86CONTEXT     px86;
#endif

    fResult = FALSE;        // Default to Event not handled

#ifdef i386

    // X86 Only, get pointer to Register Context Block
    px86 = getIntelRegistersPointer();
#endif

    GetNormalContext( &vcContext, &viInfo, EventParams, DBG_GPFAULT, PX86 );

    vcContext.SegEs = (ULONG)pFFrame->wES;
    vcContext.SegDs = (ULONG)pFFrame->wDS;
    vcContext.SegCs = (ULONG)pFFrame->wCS;
    vcContext.SegSs = (ULONG)pFFrame->wSS;

#ifdef i386
    //
    // On x86 systems, we really might have some data in the high words
    // of these registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
    // blow them away.  Here is where we attempt to recover them.
    //
    vcContext.Edi    = MAKELONG(pFFrame->wDI,   HIWORD(px86->Edi   ));
    vcContext.Esi    = MAKELONG(pFFrame->wSI,   HIWORD(px86->Esi   ));
    vcContext.Ebx    = MAKELONG(pFFrame->wBX,   HIWORD(px86->Ebx   ));
    vcContext.Edx    = MAKELONG(pFFrame->wDX,   HIWORD(px86->Edx   ));
    vcContext.Ecx    = MAKELONG(pFFrame->wCX,   HIWORD(px86->Ecx   ));
    vcContext.Eax    = MAKELONG(pFFrame->wAX,   HIWORD(px86->Eax   ));

    vcContext.Ebp    = MAKELONG(pFFrame->wBP,   HIWORD(px86->Ebp   ));
    vcContext.Eip    = MAKELONG(pFFrame->wIP,   HIWORD(px86->Eip   ));
    vcContext.Esp    = MAKELONG(pFFrame->wSP,   HIWORD(px86->Esp   ));
    vcContext.EFlags = MAKELONG(pFFrame->wFlags,HIWORD(px86->EFlags));
#else
    vcContext.Edi    = (ULONG)pFFrame->wDI;
    vcContext.Esi    = (ULONG)pFFrame->wSI;
    vcContext.Ebx    = (ULONG)pFFrame->wBX;
    vcContext.Edx    = (ULONG)pFFrame->wDX;
    vcContext.Ecx    = (ULONG)pFFrame->wCX;
    vcContext.Eax    = (ULONG)pFFrame->wAX;

    vcContext.Ebp    = (ULONG)pFFrame->wBP;
    vcContext.Eip    = (ULONG)pFFrame->wIP;
    vcContext.Esp    = (ULONG)pFFrame->wSP;
    vcContext.EFlags = (ULONG)pFFrame->wFlags;

    if ( (getMSW() & MSW_PE) == 0 ) {
        vcContext.EFlags |= V86FLAGS_V86;
    }
#endif

    if ( fDebugged ) {
        fResult = SendVDMEvent( EventParams );

        if ( !fResult ) {
            DWORD dw;

            dw = SetErrorMode(0);
            try {
                RaiseException((DWORD)DBG_CONTROL_BREAK, 0, 0, (LPDWORD)0);
                fResult = TRUE;
            } except (EXCEPTION_EXECUTE_HANDLER) {
                fResult = FALSE;
            }
            SetErrorMode(dw);
        }

    } else {
        char    text[100];

        // Dump a simulated context

        OutputDebugString("NTVDM:GP Fault detected, register dump follows:\n");

        wsprintf(text,"eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
            vcContext.Eax,
            vcContext.Ebx,
            vcContext.Ecx,
            vcContext.Edx,
            vcContext.Esi,
            vcContext.Edi  );
        OutputDebugString(text);

        wsprintf(text,"eip=%08lx esp=%08lx ebp=%08lx iopl=%d         %s %s %s %s %s %s %s %s\n",
            vcContext.Eip,
            vcContext.Esp,
            vcContext.Ebp,
            (vcContext.EFlags & V86FLAGS_IOPL) >> V86FLAGS_IOPL_BITS,
            (vcContext.EFlags & V86FLAGS_OVERFLOW ) ? "ov" : "nv",
            (vcContext.EFlags & V86FLAGS_DIRECTION) ? "dn" : "up",
            (vcContext.EFlags & V86FLAGS_INTERRUPT) ? "ei" : "di",
            (vcContext.EFlags & V86FLAGS_SIGN     ) ? "ng" : "pl",
            (vcContext.EFlags & V86FLAGS_ZERO     ) ? "zr" : "nz",
            (vcContext.EFlags & V86FLAGS_AUXCARRY ) ? "ac" : "na",
            (vcContext.EFlags & V86FLAGS_PARITY   ) ? "po" : "pe",
            (vcContext.EFlags & V86FLAGS_CARRY    ) ? "cy" : "nc" );
        OutputDebugString(text);

        wsprintf(text,"cs=%04x  ss=%04x  ds=%04x  es=%04x  fs=%04x  gs=%04x             efl=%08lx\n",
            (WORD)vcContext.SegCs,
            (WORD)vcContext.SegSs,
            (WORD)vcContext.SegDs,
            (WORD)vcContext.SegEs,
            (WORD)vcContext.SegFs,
            (WORD)vcContext.SegGs,
            vcContext.EFlags );
        OutputDebugString(text);
    }

#ifdef i386
    //
    // On x86 systems, we really might have some data in the FS and GS
    // registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
    // blow them away.  Here is where we attempt to restore them.
    //
    px86->SegGs = (WORD)vcContext.SegGs;
    px86->SegFs = (WORD)vcContext.SegFs;
#else
    // No need to set FS,GS, they don't exist
#endif

    pFFrame->wES = (WORD)vcContext.SegEs;
    pFFrame->wDS = (WORD)vcContext.SegDs;
    pFFrame->wCS = (WORD)vcContext.SegCs;
    pFFrame->wSS = (WORD)vcContext.SegSs;

#ifdef i386
    //
    // On x86 systems, we really might have some data in the high words
    // of these registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
    // blow them away.  Here is where we attempt to restore them.
    //
    pFFrame->wDI = LOWORD(vcContext.Edi);
    px86->Edi = MAKELONG(LOWORD(px86->Edi),HIWORD(vcContext.Edi));

    pFFrame->wSI = LOWORD(vcContext.Esi);
    px86->Esi = MAKELONG(LOWORD(px86->Esi),HIWORD(vcContext.Esi));

    pFFrame->wBX = LOWORD(vcContext.Ebx);
    px86->Ebx = MAKELONG(LOWORD(px86->Ebx),HIWORD(vcContext.Ebx));

    pFFrame->wDX = LOWORD(vcContext.Edx);
    px86->Edx = MAKELONG(LOWORD(px86->Edx),HIWORD(vcContext.Edx));

    pFFrame->wCX = LOWORD(vcContext.Ecx);
    px86->Ecx = MAKELONG(LOWORD(px86->Ecx),HIWORD(vcContext.Ecx));

    pFFrame->wAX = LOWORD(vcContext.Eax);
    px86->Eax = MAKELONG(LOWORD(px86->Eax),HIWORD(vcContext.Eax));

    pFFrame->wBP = LOWORD(vcContext.Ebp);
    px86->Ebp = MAKELONG(LOWORD(px86->Ebp),HIWORD(vcContext.Ebp));

    pFFrame->wIP = LOWORD(vcContext.Eip);
    px86->Eip = MAKELONG(LOWORD(px86->Eip),HIWORD(vcContext.Eip));

    pFFrame->wFlags = LOWORD(vcContext.EFlags);
    px86->EFlags = MAKELONG(LOWORD(px86->EFlags),HIWORD(vcContext.EFlags));

    pFFrame->wSP = LOWORD(vcContext.Esp);
    px86->Esp = MAKELONG(LOWORD(px86->Esp),HIWORD(vcContext.Esp));
#else
    pFFrame->wDI = (WORD)vcContext.Edi;
    pFFrame->wSI = (WORD)vcContext.Esi;
    pFFrame->wBX = (WORD)vcContext.Ebx;
    pFFrame->wDX = (WORD)vcContext.Edx;
    pFFrame->wCX = (WORD)vcContext.Ecx;
    pFFrame->wAX = (WORD)vcContext.Eax;


    pFFrame->wBP = (WORD)vcContext.Ebp;
    pFFrame->wIP = (WORD)vcContext.Eip;
    pFFrame->wFlags = (WORD)vcContext.EFlags;
    pFFrame->wSP = (WORD)vcContext.Esp;
#endif

    return( fResult );
}

BOOL DivOverflow(
    PTFRAME16   pTFrame
) {
    BOOL        fResult;

    fResult = FALSE;        // Default to Event not handled

    if ( fDebugged ) {
        DWORD           EventParams[4];
        VDMCONTEXT      vcContext;
        VDMINTERNALINFO viInfo;
#ifdef i386
        PX86CONTEXT     px86;

        // X86 Only, get pointer to Register Context Block
        px86 = getIntelRegistersPointer();
#endif

        GetNormalContext( &vcContext, &viInfo, EventParams, DBG_DIVOVERFLOW, PX86 );

        vcContext.SegDs = (ULONG)pTFrame->wDS;
        vcContext.SegCs = (ULONG)pTFrame->wCS;
        vcContext.SegSs = (ULONG)pTFrame->wSS;

#ifdef i386
        //
        // On x86 systems, we really might have some data in the high words
        // of these registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
        // blow them away.  Here is where we attempt to recover them.
        //
        vcContext.Eax    = MAKELONG(pTFrame->wAX,   HIWORD(px86->Eax   ));
        vcContext.Eip    = MAKELONG(pTFrame->wIP,   HIWORD(px86->Eip   ));
        vcContext.Esp    = MAKELONG(pTFrame->wSP,   HIWORD(px86->Esp   ));
        vcContext.EFlags = MAKELONG(pTFrame->wFlags,HIWORD(px86->EFlags));
#else
        vcContext.Eax    = (ULONG)pTFrame->wAX;

        vcContext.Eip    = (ULONG)pTFrame->wIP;
        vcContext.Esp    = (ULONG)pTFrame->wSP;
        vcContext.EFlags = (ULONG)pTFrame->wFlags;

        if ( (getMSW() & MSW_PE) == 0 ) {
            vcContext.EFlags |= V86FLAGS_V86;
        }
#endif

        fResult = SendVDMEvent( EventParams );

#ifdef i386
        //
        // On x86 systems, we really might have some data in the FS and GS
        // registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
        // blow them away.  Here is where we attempt to restore them.
        //
        px86->SegGs = vcContext.SegGs;
        px86->SegFs = vcContext.SegFs;
#else
        // No need to set FS,GS, they don't exist
#endif

        setES( (WORD)vcContext.SegEs );
        pTFrame->wDS = (WORD)vcContext.SegDs;
        pTFrame->wCS = (WORD)vcContext.SegCs;
        pTFrame->wSS = (WORD)vcContext.SegSs;

#ifdef i386
        //
        // On x86 systems, we really might have some data in the high words
        // of these registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
        // blow them away.  Here is where we attempt to restore them.
        //
        setEDI( vcContext.Edi );
        setESI( vcContext.Esi );
        setEBX( vcContext.Ebx );
        setEDX( vcContext.Edx );
        setECX( vcContext.Ecx );

        pTFrame->wAX = LOWORD(vcContext.Eax);
        px86->Eax = MAKELONG(LOWORD(px86->Eax),HIWORD(vcContext.Eax));

        setEBP( vcContext.Ebp );

        pTFrame->wIP = LOWORD(vcContext.Eip);
        px86->Eip = MAKELONG(LOWORD(px86->Eip),HIWORD(vcContext.Eip));

        pTFrame->wFlags = LOWORD(vcContext.EFlags);
        px86->EFlags = MAKELONG(LOWORD(px86->EFlags),HIWORD(vcContext.EFlags));

        pTFrame->wSP = LOWORD(vcContext.Esp);
        px86->Esp = MAKELONG(LOWORD(px86->Esp),HIWORD(vcContext.Esp));
#else
        setDI( (WORD)vcContext.Edi );
        setSI( (WORD)vcContext.Esi );
        setBX( (WORD)vcContext.Ebx );
        setDX( (WORD)vcContext.Edx );
        setCX( (WORD)vcContext.Ecx );
        pTFrame->wAX = (WORD)vcContext.Eax;


        setBP( (WORD)vcContext.Ebp );
        pTFrame->wIP    = (WORD)vcContext.Eip;
        pTFrame->wFlags = (WORD)vcContext.EFlags;
        pTFrame->wSP    = (WORD)vcContext.Esp;
#endif


    }

    return( fResult );
}


BOOL DllStart(
    PNDFRAME16  pNDFrame
) {
    BOOL        fResult;

    fResult = FALSE;        // Default to Event not handled

    if ( fDebugged ) {
        LPSTR           lpModuleName;
        LPSTR           lpModulePath;
        UINT            length;
        UCHAR           fPE;
        DWORD           EventParams[4];
        VDMCONTEXT      vcContext;
        VDMINTERNALINFO viInfo;
        IMAGE_NOTE      im;
#ifdef i386
        PX86CONTEXT     px86;

        // X86 Only, get pointer to Register Context Block
        px86 = getIntelRegistersPointer();
#endif

        GetNormalContext( &vcContext, &viInfo, EventParams, DBG_DLLSTART, PX86 );

        EventParams[2] = (DWORD)&im;

        vcContext.SegDs = (ULONG)pNDFrame->wDS;
        vcContext.SegCs = (ULONG)pNDFrame->wCS;

#ifdef i386
        //
        // On x86 systems, we really might have some data in the high words
        // of these registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
        // blow them away.  Here is where we attempt to recover them.
        //
        vcContext.Eax    = MAKELONG(pNDFrame->wAX,   HIWORD(px86->Eax   ));
        vcContext.Eip    = MAKELONG(pNDFrame->wIP,   HIWORD(px86->Eip   ));
        vcContext.EFlags = MAKELONG(pNDFrame->wFlags,HIWORD(px86->EFlags));
#else
        vcContext.Eax    = (ULONG)pNDFrame->wAX;
        vcContext.Eip    = (ULONG)pNDFrame->wIP;
        vcContext.EFlags = (ULONG)pNDFrame->wFlags;

        if ( (getMSW() & MSW_PE) == 0 ) {
            vcContext.EFlags |= V86FLAGS_V86;
        }
#endif

        // The code in TASK.ASM pops the frame off the stack before it IRETs
        vcContext.Esp += sizeof(NDFRAME16);

        // Get the module's path and name

        fPE = ISPESET;

        lpModuleName = (LPSTR)Sim32GetVDMPointer(
                            (ULONG)pNDFrame->dwModuleName,
                            MAX_MODULE,
                            fPE );

        lpModulePath = (LPSTR)Sim32GetVDMPointer(
                            (ULONG)pNDFrame->dwModulePath,
                            MAX_PATH,
                            fPE );

        length = (UINT)((UCHAR)*lpModuleName++);

        strncpy( im.Module, lpModuleName, length );
        im.Module[length] = '\0';

        length = (UINT)((UCHAR)*lpModulePath);
        lpModulePath += 8;
        length -= 8;

        strncpy( im.FileName, lpModulePath, length );
        im.FileName[length] = '\0';

        im.hModule = pNDFrame->hModule;
        im.hTask   = pNDFrame->hTask;

        fResult = SendVDMEvent( EventParams );

        // See comment about what the code does above
        vcContext.Esp -= sizeof(NDFRAME16);

#ifdef i386
        //
        // On x86 systems, we really might have some data in the FS and GS
        // registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
        // blow them away.  Here is where we attempt to restore them.
        //
        px86->SegGs = vcContext.SegGs;
        px86->SegFs = vcContext.SegFs;
#else
        // No need to set FS,GS, they don't exist
#endif

        setES( (WORD)vcContext.SegEs );
        pNDFrame->wDS = (WORD)vcContext.SegDs;
        pNDFrame->wCS = (WORD)vcContext.SegCs;
        setSS( (WORD)vcContext.SegSs );

#ifdef i386
        //
        // On x86 systems, we really might have some data in the high words
        // of these registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
        // blow them away.  Here is where we attempt to restore them.
        //
        setEDI( vcContext.Edi );
        setESI( vcContext.Esi );
        setEBX( vcContext.Ebx );
        setEDX( vcContext.Edx );
        setECX( vcContext.Ecx );

        pNDFrame->wAX = LOWORD(vcContext.Eax);
        px86->Eax = MAKELONG(LOWORD(px86->Eax),HIWORD(vcContext.Eax));

        setEBP( vcContext.Ebp );

        pNDFrame->wIP = LOWORD(vcContext.Eip);
        px86->Eip = MAKELONG(LOWORD(px86->Eip),HIWORD(vcContext.Eip));

        pNDFrame->wFlags = LOWORD(vcContext.EFlags);
        px86->EFlags = MAKELONG(LOWORD(px86->EFlags),HIWORD(vcContext.EFlags));

        setESP( vcContext.Esp );
#else
        setDI( (WORD)vcContext.Edi );
        setSI( (WORD)vcContext.Esi );
        setBX( (WORD)vcContext.Ebx );
        setDX( (WORD)vcContext.Edx );
        setCX( (WORD)vcContext.Ecx );
        pNDFrame->wAX = (WORD)vcContext.Eax;


        setBP( (WORD)vcContext.Ebp );
        pNDFrame->wIP    = (WORD)vcContext.Eip;
        pNDFrame->wFlags = (WORD)vcContext.EFlags;

        setSP( (WORD)vcContext.Esp );
#endif


    }

    return( fResult );
}


BOOL TaskStop(
    PSTFRAME16  pSTFrame
) {
    BOOL        fResult;

    fResult = FALSE;        // Default to Event not handled

    if ( fDebugged ) {
        LPSTR           lpModuleName;
        LPSTR           lpModulePath;
        UINT            length;
        UCHAR           fPE;
        DWORD           EventParams[4];
        VDMCONTEXT      vcContext;
        VDMINTERNALINFO viInfo;
        IMAGE_NOTE      im;
#ifdef i386
        PX86CONTEXT     px86;

        // X86 Only, get pointer to Register Context Block
        px86 = getIntelRegistersPointer();
#endif

        GetNormalContext( &vcContext, &viInfo, EventParams, DBG_TASKSTOP, PX86 );

        EventParams[2] = (DWORD)&im;

        // The code in TASK.ASM pops the frame off the stack before it IRETs
        vcContext.Esp += sizeof(STFRAME16);

        // Get the module's path and name

        fPE = ISPESET;

        lpModuleName = (LPSTR)Sim32GetVDMPointer(
                            (ULONG)pSTFrame->dwModuleName,
                            MAX_MODULE,
                            fPE );

        lpModulePath = (LPSTR)Sim32GetVDMPointer(
                            (ULONG)pSTFrame->dwModulePath,
                            MAX_PATH,
                            fPE );

        length = (UINT)((UCHAR)*lpModuleName++);

        strncpy( im.Module, lpModuleName, length );
        im.Module[length] = '\0';

        length = (UINT)((UCHAR)*lpModulePath);
        lpModulePath += 8;
        length -= 8;

        strncpy( im.FileName, lpModulePath, length );
        im.FileName[length] = '\0';

        im.hModule = pSTFrame->hModule;
        im.hTask   = pSTFrame->hTask;

        fResult = SendVDMEvent( EventParams );

        // See comment about what the code does above
        vcContext.Esp -= sizeof(STFRAME16);
    }

    return( fResult );
}









//
// I looked through the Win 3.0/Win 3.1 sources, and all the SDM_... commands
// I could find were this:   (The only ones we support for now will be
//                             SDM_LOADSEG,SDM_MOVESEG,SDM_FREESEG/RELEASESEG)
//
// ax = SDM_LOADSEG     =  50h
//       es:di = Module Name
//       bx    = segment number
//       cx    = loaded seg
//       dx    = instance number
//       si    = DataOrCodeSegment
// ax = SDM_MOVESEG     =  51h
//       push  destseg
//       push  sourceseg
//       push  SDM_MOVESEG
// SDM_FREESEG     =  52h
//       bx    = segment addr
// SDM_RELEASESEG  =  5Ch
//       bx    = segment addr
// SDM_5A (DebugDebug)
//       cx    = dataoffset hGlobalHeap
//       dx    = ds (SetKernelDS)
// SDM_4F (DebugInit)
//       push  SDM_4F
// SDM_0 (Out Char)
//       dl    = character to output
// SDM_0F (Out Symbol)
//       dx    = offset
//       cx    = selector
//       es    = data segment of DOSX
// SDM_GLOBALHEAP  =  3 (real mode only)
//       push  pGlobalHeap
//       push  ax
// SDM_CONWRITE    =  12h
//       ds:si = Pointer to string
//       cx    = length of string
// SDM_CONREAD     =  1
//       nothing, returns character read in al
// SDM_DGH         =  56h
//       cx    = seg DumpGlobalHeap
//       bx    = codeoffset DumpGlobalHeap
// SDM_DFL         =  57h
//       cx    = seg DumpFreeList
//       bx    = codeoffset DumpFreeList
// SDM_DLL         =  58h
//       cx    = seg DumpLRUList
//       bx    = codeoffset DumpLRUList
// SDM_LOADTASK    =  59h
//       not used
// SDM_POSTLOAD    =  60h
//       push    ax
// SDM_EXITCALL    =  62h
//       push    ax
// SDM_INT2        =  63h (Ctrl-alt-sysreq)
//       nothing.
// SDM_LOADDLL     =  64h
//       not used
// SDM_DELMODULE   =  65h
//       push    module handle
// SDM_RIN         =  9
//       push    seg ReplaceInst
//       push    codeoffset ReplaceInst
//       push    ax
// SDM_BANKLINE    = 10
//       push    EMS_calc_swap_line
//       push    ax
// SDM_NEWTASK     = 11
//       push   EMS PID
//       push   ax
// SDM_FLUSHTASK   = 12
//       push   EMS PID
//       push   ax
// SDM_SWITCHOUT   = 13
//       ds    = TDB
// SDM_SWITCHIN    = 14
//       ds    = TDB
// SDM_KEYBOARD    = 15
//       not used (thankfully, it is the same number as SDM_0F)
// SDM_MAXFUNC     = 15
//       not used

void DBGDispatch()
{
    UNALIGNED WORD  *stack;
    WORD            mode;
    WORD            selector;
    WORD            segment;
    WORD            new_selector;
    BOOL            fBPRelease;
    BOOL            fData;
    LPSTR           lpModuleName;
    LPSTR           lpPathName;
    UCHAR           fPE;
    PFFRAME16       pFFrame;
    PTFRAME16       pTFrame;
    PNDFRAME16      pNDFrame;
    PSTFRAME16      pSTFrame;
    WORD            wFrame;

    fPE = ISPESET;

    stack = (UNALIGNED WORD *)Sim32GetVDMPointer(
                        ((ULONG)getSS() << 16) + (ULONG)getSP(),
                        MAX_DBG_FRAME, fPE );

    mode = *stack++;
    switch( mode ) {
        case DBG_SEGLOAD:
            selector = *stack++;
            segment  = *stack++;
            lpModuleName = (LPSTR)Sim32GetVDMPointer(
                                    (ULONG)*stack + ((ULONG)(*(stack+1)) << 16),
                                    0, fPE );
            stack += 2;
            lpPathName = (LPSTR)Sim32GetVDMPointer(
                                    (ULONG)*stack + ((ULONG)(*(stack+1)) << 16),
                                    0, fPE );
            if ( lpPathName == NULL ) {
                lpPathName = "";
            }

            stack += 2;
            fData = (BOOL)(*stack++);
            SegmentLoad( lpModuleName, lpPathName, selector, segment, fData );
            break;
        case DBG_SEGMOVE:
            selector = *stack++;
            new_selector = *stack++;
            SegmentMove( selector, new_selector );
            break;
        case DBG_SEGFREE:
            fBPRelease = (BOOL)*stack++;
            selector = *stack++;
            SegmentFree( selector, fBPRelease );
            break;
        case DBG_MODFREE:
            lpModuleName = (LPSTR)Sim32GetVDMPointer(
                                    (ULONG)*stack + ((ULONG)(*(stack+1)) << 16),
                                    0, fPE );
            stack += 2;
            lpPathName = (LPSTR)Sim32GetVDMPointer(
                                    (ULONG)*stack + ((ULONG)(*(stack+1)) << 16),
                                    0, fPE );
            if ( lpPathName == NULL ) {
                lpPathName = "";
            }
            ModuleFree( lpModuleName, lpPathName );
            break;
        case DBG_MODLOAD:

            // Why doesn't this do anything? - See JonLe

            break;

        case DBG_SINGLESTEP:
            pTFrame = (PTFRAME16)Sim32GetVDMPointer(
                        (ULONG)((ULONG)getSS() << 16) + (ULONG)getSP(),
                        MAX_DBG_FRAME, fPE );


            fData = SingleStep( pTFrame );

            setAX((WORD)fData);
            break;

        case DBG_BREAK:
            pTFrame = (PTFRAME16)Sim32GetVDMPointer(
                        (ULONG)((ULONG)getSS() << 16) + (ULONG)getSP(),
                        MAX_DBG_FRAME, fPE );


            fData = Breakpoint( pTFrame );

            setAX((WORD)fData);
            break;
        case DBG_GPFAULT:
            wFrame = getBP() - (WORD)(FIELD_OFFSET(FFRAME16,wBP));

            pFFrame = (PFFRAME16)Sim32GetVDMPointer(
                        ((ULONG)getSS() << 16) + (ULONG)wFrame,
                        MAX_DBG_FRAME, fPE );


            fData = GPFault( pFFrame );

            setAX((WORD)fData);
            break;
        case DBG_DIVOVERFLOW:
            pTFrame = (PTFRAME16)Sim32GetVDMPointer(
                        (ULONG)((ULONG)getSS() << 16) + (ULONG)getSP(),
                        MAX_DBG_FRAME, fPE );


            fData = DivOverflow( pTFrame );

            setAX((WORD)fData);
            break;
        case DBG_DLLSTART:
            pNDFrame = (PNDFRAME16)Sim32GetVDMPointer(
                        (ULONG)((ULONG)getSS() << 16) + (ULONG)getSP(),
                        MAX_DBG_FRAME, fPE );


            fData = DllStart( pNDFrame );

            setAX((WORD)fData);
            break;
        case DBG_TASKSTOP:
            pSTFrame = (PSTFRAME16)Sim32GetVDMPointer(
                        (ULONG)((ULONG)getSS() << 16) + (ULONG)getSP(),
                        MAX_DBG_FRAME, fPE );

            fData = TaskStop( pSTFrame );
            break;
        case DBG_ATTACH:
            break;

        case DBG_TOOLHELP:
            ulTHHOOK = (ULONG)*stack + ((ULONG)(*(stack+1)) << 16);
            stack += 2;
            f386 = (BOOL)*stack;
            break;

        default:
            setAX(0);       // Event not handled
            break;
    }
}

VOID DBGNotifyNewTask(
    LPVOID	lpvNTFrame,
    UINT	uFrameSize
) {
    BOOL        fResult;
    PNTFRAME16	pNTFrame;

    pNTFrame = (PNTFRAME16)lpvNTFrame;

    if ( fDebugged ) {
        LPSTR           lpModuleName;
        LPSTR           lpModulePath;
        UINT            length;
        UCHAR           fPE;
        DWORD           EventParams[4];
        VDMCONTEXT      vcContext;
        VDMINTERNALINFO viInfo;
        IMAGE_NOTE      im;
#ifdef i386
        PX86CONTEXT     px86;

        // X86 Only, get pointer to Register Context Block
        px86 = getIntelRegistersPointer();
#endif

        GetNormalContext( &vcContext, &viInfo, EventParams, DBG_TASKSTART, PX86 );

        EventParams[2] = (DWORD)&im;

        vcContext.SegEs = (ULONG)pNTFrame->wES;
        vcContext.SegDs = (ULONG)pNTFrame->wDS;
        vcContext.SegCs = (ULONG)pNTFrame->wCS;

#ifdef i386
        //
        // On x86 systems, we really might have some data in the high words
        // of these registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
        // blow them away.  Here is where we attempt to recover them.
        //
        vcContext.Edi    = MAKELONG(pNTFrame->wDI,   HIWORD(px86->Edi   ));
        vcContext.Esi    = MAKELONG(pNTFrame->wSI,   HIWORD(px86->Esi   ));
        vcContext.Ebx    = MAKELONG(pNTFrame->wBX,   HIWORD(px86->Ebx   ));
        vcContext.Edx    = MAKELONG(pNTFrame->wDX,   HIWORD(px86->Edx   ));
        vcContext.Ecx    = MAKELONG(pNTFrame->wCX,   HIWORD(px86->Ecx   ));
        vcContext.Eax    = MAKELONG(pNTFrame->wAX,   HIWORD(px86->Eax   ));

        vcContext.Ebp    = MAKELONG(pNTFrame->wBP,   HIWORD(px86->Ebp   ));
        vcContext.Eip    = MAKELONG(pNTFrame->wIP,   HIWORD(px86->Eip   ));
        vcContext.EFlags = MAKELONG(pNTFrame->wFlags,HIWORD(px86->EFlags));

#else
        vcContext.Edi    = (ULONG)pNTFrame->wDI;
        vcContext.Esi    = (ULONG)pNTFrame->wSI;
        vcContext.Ebx    = (ULONG)pNTFrame->wBX;
        vcContext.Edx    = (ULONG)pNTFrame->wDX;
        vcContext.Ecx    = (ULONG)pNTFrame->wCX;
        vcContext.Eax    = (ULONG)pNTFrame->wAX;

        vcContext.Ebp    = (ULONG)pNTFrame->wBP;
        vcContext.Eip    = (ULONG)pNTFrame->wIP;
        vcContext.EFlags = (ULONG)pNTFrame->wFlags;

        if ( (getMSW() & MSW_PE) == 0 ) {
            vcContext.EFlags |= V86FLAGS_V86;
        }
#endif
        // The code in TASKING.ASM does a dec bp before the IRET
        vcContext.Ebp -= 1;
        // The code copies frame off the stack then IRETs
        vcContext.Esp += sizeof(NTFRAME16) + uFrameSize;

        // Get the module's path and name

        fPE = ISPESET;

        lpModuleName = (LPSTR)Sim32GetVDMPointer(
                            (ULONG)pNTFrame->dwModuleName,
                            MAX_MODULE,
                            fPE );

        lpModulePath = (LPSTR)Sim32GetVDMPointer(
                            (ULONG)pNTFrame->dwModulePath,
                            MAX_PATH,
                            fPE );

        length = (UINT)((UCHAR)*lpModuleName++);

        strncpy( im.Module, lpModuleName, length );
        im.Module[length] = '\0';

        length = (UINT)((UCHAR)*lpModulePath);
        lpModulePath += 8;
        length -= 8;

        strncpy( im.FileName, lpModulePath, length );
        im.FileName[length] = '\0';

        im.hModule = pNTFrame->hModule;
        im.hTask   = pNTFrame->hTask;

        fResult = SendVDMEvent( EventParams );

        // See comments about what the code does above
        vcContext.Ebp += 1;
        vcContext.Esp -= sizeof(NTFRAME16) + uFrameSize;

#ifdef i386
        //
        // On x86 systems, we really might have some data in the FS and GS
        // registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
        // blow them away.  Here is where we attempt to restore them.
        //
        px86->SegGs = vcContext.SegGs;
        px86->SegFs = vcContext.SegFs;
#else
        // No need to set FS,GS, they don't exist
#endif

        pNTFrame->wES = (WORD)vcContext.SegEs;
        pNTFrame->wDS = (WORD)vcContext.SegDs;
        pNTFrame->wCS = (WORD)vcContext.SegCs;
        setSS( (WORD)vcContext.SegSs );

#ifdef i386
        //
        // On x86 systems, we really might have some data in the high words
        // of these registers.  Hopefully DOSX.EXE and KRNL286.EXE don't
        // blow them away.  Here is where we attempt to restore them.
        //
        pNTFrame->wDI = LOWORD(vcContext.Edi);
        px86->Edi = MAKELONG(LOWORD(px86->Edi),HIWORD(vcContext.Edi));

        pNTFrame->wSI = LOWORD(vcContext.Esi);
        px86->Esi = MAKELONG(LOWORD(px86->Esi),HIWORD(vcContext.Esi));

        pNTFrame->wBX = LOWORD(vcContext.Ebx);
        px86->Ebx = MAKELONG(LOWORD(px86->Ebx),HIWORD(vcContext.Ebx));

        pNTFrame->wDX = LOWORD(vcContext.Edx);
        px86->Edx = MAKELONG(LOWORD(px86->Edx),HIWORD(vcContext.Edx));

        pNTFrame->wCX = LOWORD(vcContext.Ecx);
        px86->Ecx = MAKELONG(LOWORD(px86->Ecx),HIWORD(vcContext.Ecx));

        pNTFrame->wAX = LOWORD(vcContext.Eax);
        px86->Eax = MAKELONG(LOWORD(px86->Eax),HIWORD(vcContext.Eax));

        pNTFrame->wBP = LOWORD(vcContext.Ebp);
        px86->Ebp = MAKELONG(LOWORD(px86->Ebp),HIWORD(vcContext.Ebp));

        pNTFrame->wIP = LOWORD(vcContext.Eip);
        px86->Eip = MAKELONG(LOWORD(px86->Eip),HIWORD(vcContext.Eip));

        pNTFrame->wFlags = LOWORD(vcContext.EFlags);
        px86->EFlags = MAKELONG(LOWORD(px86->EFlags),HIWORD(vcContext.EFlags));

        setESP( vcContext.Esp );
#else
        pNTFrame->wDI    = (WORD)vcContext.Edi;
        pNTFrame->wSI    = (WORD)vcContext.Esi;
        pNTFrame->wBX    = (WORD)vcContext.Ebx;
        pNTFrame->wDX    = (WORD)vcContext.Edx;
        pNTFrame->wCX    = (WORD)vcContext.Ecx;
        pNTFrame->wAX    = (WORD)vcContext.Eax;

        pNTFrame->wBP    = (WORD)vcContext.Ebp;

        pNTFrame->wIP    = (WORD)vcContext.Eip;
        pNTFrame->wFlags = (WORD)vcContext.EFlags;

        setSP( (WORD)vcContext.Esp );
#endif


    }


}


VOID DBGNotifyRemoteThreadAddress(
    LPVOID  lpAddress,
    DWORD   lpBlock
) {
    lpRemoteAddress = lpAddress;
    lpRemoteBlock   = lpBlock;
}

VOID DBGNotifyDebugged(
    BOOL    fNewDebugged
) {
    fDebugged = fNewDebugged;
}