00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#include "dbgdllp.h"
00022
#include "ldrp.h"
00023
00024
NTSTATUS
00025 DbgSspConnectToDbg( VOID )
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 {
00045
NTSTATUS st;
00046 UNICODE_STRING
PortName;
00047 SECURITY_QUALITY_OF_SERVICE
DynamicQos;
00048
00049
00050
00051
00052
00053
00054
00055
DynamicQos.ImpersonationLevel = SecurityImpersonation;
00056
DynamicQos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
00057
DynamicQos.EffectiveOnly =
TRUE;
00058
00059
00060
RtlInitUnicodeString(&
PortName,
L"\\DbgSsApiPort");
00061 st =
NtConnectPort(
00062 &
DbgSspApiPort,
00063 &
PortName,
00064 &
DynamicQos,
00065
NULL,
00066
NULL,
00067
NULL,
00068
NULL,
00069 0
L
00070 );
00071
00072
return st;
00073
00074 }
00075
00076
NTSTATUS
00077 DbgSspException (
00078 IN
PDBGSS_CONTINUE_KEY ContinueKey,
00079 IN PCLIENT_ID AppClientId,
00080 IN PDBGKM_EXCEPTION Exception
00081 )
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 {
00108
00109
NTSTATUS st;
00110 DBGSS_APIMSG ApiMsg;
00111
00112 PDBGKM_EXCEPTION args;
00113
00114 args = &ApiMsg.u.Exception;
00115
00116 DBGSS_FORMAT_API_MSG(ApiMsg,DbgSsExceptionApi,
sizeof(*args),AppClientId,ContinueKey);
00117
00118 *args = *Exception;
00119
00120 st =
NtRequestPort(
DbgSspApiPort, (PPORT_MESSAGE) &ApiMsg);
00121
00122
return st;
00123 }
00124
00125
NTSTATUS
00126 DbgSspCreateThread (
00127 IN
PDBGSS_CONTINUE_KEY ContinueKey,
00128 IN PCLIENT_ID AppClientId,
00129 IN PDBGKM_CREATE_THREAD NewThread
00130 )
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 {
00157
00158
NTSTATUS st;
00159 DBGSS_APIMSG ApiMsg;
00160
00161 PDBGKM_CREATE_THREAD args;
00162
00163 args = &ApiMsg.u.CreateThread;
00164
00165 DBGSS_FORMAT_API_MSG(ApiMsg,DbgSsCreateThreadApi,
sizeof(*args),AppClientId, ContinueKey);
00166
00167 *args = *NewThread;
00168
00169 st =
NtRequestPort(
DbgSspApiPort, (PPORT_MESSAGE) &ApiMsg);
00170
00171
return st;
00172 }
00173
00174
00175
NTSTATUS
00176 DbgSspCreateProcess (
00177 IN
PDBGSS_CONTINUE_KEY ContinueKey,
00178 IN PCLIENT_ID AppClientId,
00179 IN PCLIENT_ID DebugUiClientId,
00180 IN PDBGKM_CREATE_PROCESS NewProcess
00181 )
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 {
00213
00214
NTSTATUS st;
00215 DBGSS_APIMSG ApiMsg;
00216
00217 PDBGSS_CREATE_PROCESS args;
00218
00219 args = &ApiMsg.u.CreateProcessInfo;
00220
00221 DBGSS_FORMAT_API_MSG(ApiMsg,DbgSsCreateProcessApi,
sizeof(*args),AppClientId,ContinueKey);
00222
00223 args->DebugUiClientId = *DebugUiClientId;
00224 args->NewProcess = *NewProcess;
00225
00226 st =
NtRequestPort(
DbgSspApiPort, (PPORT_MESSAGE) &ApiMsg);
00227
00228
return st;
00229
00230 }
00231
00232
NTSTATUS
00233 DbgSspExitThread (
00234 IN
PDBGSS_CONTINUE_KEY ContinueKey,
00235 IN PCLIENT_ID AppClientId,
00236 IN PDBGKM_EXIT_THREAD ExitThread
00237 )
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 {
00263
00264
NTSTATUS st;
00265 DBGSS_APIMSG ApiMsg;
00266
00267 PDBGKM_EXIT_THREAD args;
00268
00269 args = &ApiMsg.u.ExitThread;
00270
00271 DBGSS_FORMAT_API_MSG(ApiMsg,DbgSsExitThreadApi,
sizeof(*args),AppClientId,ContinueKey);
00272
00273 *args = *ExitThread;
00274
00275 st =
NtRequestPort(
DbgSspApiPort, (PPORT_MESSAGE) &ApiMsg);
00276
00277
return st;
00278 }
00279
00280
NTSTATUS
00281 DbgSspExitProcess (
00282 IN
PDBGSS_CONTINUE_KEY ContinueKey,
00283 IN PCLIENT_ID AppClientId,
00284 IN PDBGKM_EXIT_PROCESS ExitProcess
00285 )
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311 {
00312
00313
NTSTATUS st;
00314 DBGSS_APIMSG ApiMsg;
00315
00316 PDBGKM_EXIT_PROCESS args;
00317
00318 args = &ApiMsg.u.ExitProcess;
00319
00320 DBGSS_FORMAT_API_MSG(ApiMsg,DbgSsExitProcessApi,
sizeof(*args),AppClientId,ContinueKey);
00321
00322 *args = *ExitProcess;
00323
00324 st =
NtRequestPort(
DbgSspApiPort, (PPORT_MESSAGE) &ApiMsg);
00325
00326
return st;
00327 }
00328
00329
NTSTATUS
00330 DbgSspLoadDll (
00331 IN
PDBGSS_CONTINUE_KEY ContinueKey,
00332 IN PCLIENT_ID AppClientId,
00333 IN PDBGKM_LOAD_DLL LoadDll
00334 )
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 {
00361
00362
NTSTATUS st;
00363 DBGSS_APIMSG ApiMsg;
00364
00365 PDBGKM_LOAD_DLL args;
00366
00367 args = &ApiMsg.u.LoadDll;
00368
00369 DBGSS_FORMAT_API_MSG(ApiMsg,DbgSsLoadDllApi,
sizeof(*args),AppClientId,ContinueKey);
00370
00371 *args = *LoadDll;
00372
00373 st =
NtRequestPort(
DbgSspApiPort, (PPORT_MESSAGE) &ApiMsg);
00374
00375
return st;
00376 }
00377
00378
NTSTATUS
00379 DbgSspUnloadDll (
00380 IN
PDBGSS_CONTINUE_KEY ContinueKey,
00381 IN PCLIENT_ID AppClientId,
00382 IN PDBGKM_UNLOAD_DLL UnloadDll
00383 )
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409 {
00410
00411
NTSTATUS st;
00412 DBGSS_APIMSG ApiMsg;
00413
00414 PDBGKM_UNLOAD_DLL args;
00415
00416 args = &ApiMsg.u.UnloadDll;
00417
00418 DBGSS_FORMAT_API_MSG(ApiMsg,DbgSsUnloadDllApi,
sizeof(*args),AppClientId,ContinueKey);
00419
00420 *args = *UnloadDll;
00421
00422 st =
NtRequestPort(
DbgSspApiPort, (PPORT_MESSAGE) &ApiMsg);
00423
00424
return st;
00425 }
00426
00427
00428
NTSTATUS
00429 DbgSsInitialize(
00430 IN HANDLE KmReplyPort,
00431 IN PDBGSS_UI_LOOKUP UiLookUpRoutine,
00432 IN PDBGSS_SUBSYSTEMKEY_LOOKUP SubsystemKeyLookupRoutine OPTIONAL,
00433 IN PDBGSS_DBGKM_APIMSG_FILTER KmApiMsgFilter OPTIONAL
00434 )
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473 {
00474
NTSTATUS st;
00475
00476 st =
DbgSspConnectToDbg();
00477
00478
if (
NT_SUCCESS(st)) {
00479
DbgSspKmReplyPort = KmReplyPort;
00480
DbgSspUiLookUpRoutine = UiLookUpRoutine;
00481
DbgSspSubsystemKeyLookupRoutine = SubsystemKeyLookupRoutine;
00482
DbgSspKmApiMsgFilter = KmApiMsgFilter;
00483
00484 st =
RtlCreateUserThread(
00485 NtCurrentProcess(),
00486
NULL,
00487
FALSE,
00488 0
L,
00489 0
L,
00490 0
L,
00491
DbgSspSrvApiLoop,
00492
NULL,
00493
NULL,
00494
NULL
00495 );
00496
ASSERT(
NT_SUCCESS(st) );
00497 }
00498
00499
return st;
00500 }
00501
00502
00503
#if DBG
00504
PSZ DbgpKmApiName[ DbgKmMaxApiNumber+1 ] = {
00505
"DbgKmException",
00506
"DbgKmCreateThread",
00507
"DbgKmCreateProcess",
00508
"DbgKmExitThread",
00509
"DbgKmExitProcess",
00510
"DbgKmLoadDll",
00511
"DbgKmUnloadDll",
00512
"Unknown DbgKm Api Number"
00513 };
00514
#endif // DBG
00515
00516
VOID
00517 DbgSsHandleKmApiMsg(
00518 IN PDBGKM_APIMSG ApiMsg,
00519 IN HANDLE ReplyEvent OPTIONAL
00520 )
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567 {
00568
NTSTATUS st;
00569 CLIENT_ID DebugUiClientId;
00570 ULONG SubsystemKey;
00571
PDBGSS_CONTINUE_KEY ContinueKey;
00572
00573 ApiMsg->ReturnedStatus = STATUS_PENDING;
00574
00575
#if DBG && 0
00576
if (ApiMsg->ApiNumber >= DbgKmMaxApiNumber ) {
00577 ApiMsg->ApiNumber = DbgKmMaxApiNumber;
00578 }
00579
DbgPrint(
"DBG: %s Api Request received from %lx.%lx\n",
00580 DbgpKmApiName[ ApiMsg->ApiNumber ],
00581 ApiMsg->h.ClientId.UniqueProcess,
00582 ApiMsg->h.ClientId.UniqueThread
00583 );
00584
#endif // DBG
00585
00586
if (
DbgSspKmApiMsgFilter) {
00587
if ( (
DbgSspKmApiMsgFilter)(ApiMsg) != DBG_CONTINUE ) {
00588
return;
00589 }
00590 }
00591
00592 ContinueKey = (
PDBGSS_CONTINUE_KEY)
RtlAllocateHeap(RtlProcessHeap(),
MAKE_TAG(
DBG_TAG ),
sizeof(*ContinueKey));
00593
if ( !ContinueKey ) {
00594 ApiMsg->ReturnedStatus = STATUS_NO_MEMORY;
00595
if ( ARGUMENT_PRESENT(ReplyEvent) ) {
00596 st =
NtSetEvent(ReplyEvent,
NULL);
00597 }
else {
00598 st =
NtReplyPort(
DbgSspKmReplyPort,
00599 (PPORT_MESSAGE)ApiMsg
00600 );
00601 }
00602
ASSERT(
NT_SUCCESS(st));
00603
return;
00604 }
00605 ContinueKey->KmApiMsg = *ApiMsg;
00606
00607 ContinueKey->ReplyEvent = ReplyEvent;
00608
00609
switch (ApiMsg->ApiNumber) {
00610
00611
case DbgKmExceptionApi :
00612 st =
DbgSspException(
00613 ContinueKey,
00614 &ApiMsg->h.ClientId,
00615 &ApiMsg->u.Exception
00616 );
00617
00618
break;
00619
00620
case DbgKmCreateThreadApi :
00621
00622
if (
DbgSspSubsystemKeyLookupRoutine ) {
00623
00624 st = (
DbgSspSubsystemKeyLookupRoutine)(
00625 &ApiMsg->h.ClientId,
00626 &SubsystemKey,
00627
FALSE
00628 );
00629
00630
if (
NT_SUCCESS(st) ) {
00631 ApiMsg->u.CreateThread.SubSystemKey = SubsystemKey;
00632 }
00633 }
00634
00635 st =
DbgSspCreateThread(
00636 ContinueKey,
00637 &ApiMsg->h.ClientId,
00638 &ApiMsg->u.CreateThread
00639 );
00640
00641
break;
00642
00643
case DbgKmCreateProcessApi :
00644
00645 st = (
DbgSspUiLookUpRoutine)(
00646 &ApiMsg->h.ClientId,
00647 &DebugUiClientId
00648 );
00649
if ( !
NT_SUCCESS(st) ) {
00650
break;
00651 }
00652
00653
if (
DbgSspSubsystemKeyLookupRoutine ) {
00654
00655 st = (
DbgSspSubsystemKeyLookupRoutine)(
00656 &ApiMsg->h.ClientId,
00657 &SubsystemKey,
00658
TRUE
00659 );
00660
00661
if (
NT_SUCCESS(st) ) {
00662 ApiMsg->u.CreateProcessInfo.SubSystemKey = SubsystemKey;
00663 }
00664
00665 st = (
DbgSspSubsystemKeyLookupRoutine)(
00666 &ApiMsg->h.ClientId,
00667 &SubsystemKey,
00668
FALSE
00669 );
00670
00671
if (
NT_SUCCESS(st) ) {
00672 ApiMsg->u.CreateProcessInfo.InitialThread.SubSystemKey = SubsystemKey;
00673 }
00674 }
00675
00676 st =
DbgSspCreateProcess(
00677 ContinueKey,
00678 &ApiMsg->h.ClientId,
00679 &DebugUiClientId,
00680 &ApiMsg->u.CreateProcessInfo
00681 );
00682
break;
00683
00684
00685
case DbgKmExitThreadApi :
00686 st =
DbgSspExitThread(
00687 ContinueKey,
00688 &ApiMsg->h.ClientId,
00689 &ApiMsg->u.ExitThread
00690 );
00691
break;
00692
00693
case DbgKmExitProcessApi :
00694 st =
DbgSspExitProcess(
00695 ContinueKey,
00696 &ApiMsg->h.ClientId,
00697 &ApiMsg->u.ExitProcess
00698 );
00699
break;
00700
00701
case DbgKmLoadDllApi :
00702 st =
DbgSspLoadDll(
00703 ContinueKey,
00704 &ApiMsg->h.ClientId,
00705 &ApiMsg->u.LoadDll
00706 );
00707
break;
00708
00709
case DbgKmUnloadDllApi :
00710 st =
DbgSspUnloadDll(
00711 ContinueKey,
00712 &ApiMsg->h.ClientId,
00713 &ApiMsg->u.UnloadDll
00714 );
00715
break;
00716
00717
default :
00718 st = STATUS_NOT_IMPLEMENTED;
00719 }
00720
00721
if ( !
NT_SUCCESS(st) ) {
00722 ApiMsg->ReturnedStatus = st;
00723
RtlFreeHeap(RtlProcessHeap(), 0, ContinueKey);
00724
if ( ARGUMENT_PRESENT(ReplyEvent) ) {
00725 st =
NtSetEvent(ReplyEvent,
NULL);
00726 }
else {
00727 st =
NtReplyPort(
DbgSspKmReplyPort,
00728 (PPORT_MESSAGE)ApiMsg
00729 );
00730 }
00731
ASSERT(
NT_SUCCESS(st));
00732 }
00733 }
00734
00735
00736
NTSTATUS
00737 DbgSspSrvApiLoop(
00738 IN PVOID ThreadParameter
00739 )
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757 {
00758 DBGSRV_APIMSG DbgSrvApiMsg;
00759
PDBGSS_CONTINUE_KEY ContinueKey;
00760
NTSTATUS st;
00761
00762
for(;;) {
00763
00764 st =
NtReplyWaitReceivePort(
00765
DbgSspApiPort,
00766
NULL,
00767
NULL,
00768 (PPORT_MESSAGE) &DbgSrvApiMsg
00769 );
00770
00771
if (!
NT_SUCCESS( st )) {
00772
continue;
00773 }
00774
00775
ASSERT(DbgSrvApiMsg.ApiNumber < DbgSrvMaxApiNumber);
00776
00777
switch (DbgSrvApiMsg.ApiNumber ) {
00778
case DbgSrvContinueApi :
00779
00780
00781
00782
00783
00784
00785 ContinueKey = (
PDBGSS_CONTINUE_KEY) DbgSrvApiMsg.ContinueKey;
00786 ContinueKey->
KmApiMsg.ReturnedStatus = DbgSrvApiMsg.ReturnedStatus;
00787
00788
if ( ContinueKey->
ReplyEvent ) {
00789 st =
NtSetEvent(ContinueKey->
ReplyEvent,
NULL);
00790 }
else {
00791 st =
NtReplyPort(
DbgSspKmReplyPort,
00792 (PPORT_MESSAGE) &ContinueKey->
KmApiMsg
00793 );
00794 }
00795
00796
RtlFreeHeap(RtlProcessHeap(), 0, ContinueKey);
00797
break;
00798
default :
00799
ASSERT(
FALSE);
00800 }
00801 }
00802
00803
00804
00805
00806
00807
return st;
00808 }