summaryrefslogblamecommitdiffstats
path: root/private/ntos/dll/dllssstb.c
blob: d56f494f3e6df51ce7f6fa6f4641189d84a19def (plain) (tree)
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









































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                     
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    dllssstb.c

Abstract:

    Debug Subsystem DbgSs API Stubs

Author:

    Mark Lucovsky (markl) 22-Jan-1990

Revision History:

--*/

#include "dbgdllp.h"
#include "ldrp.h"

NTSTATUS
DbgSspConnectToDbg( VOID )

/*++

Routine Description:

    This routine makes a connection between the caller and the
    DbgSs port in the Dbg subsystem.

Arguments:

    None.

Return Value:

    TBD.

--*/

{
    NTSTATUS st;
    UNICODE_STRING PortName;
    SECURITY_QUALITY_OF_SERVICE DynamicQos;

    //
    // Set up the security quality of service parameters to use over the
    // port.  Use the most efficient (least overhead) - which is dynamic
    // rather than static tracking.
    //

    DynamicQos.ImpersonationLevel = SecurityImpersonation;
    DynamicQos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
    DynamicQos.EffectiveOnly = TRUE;


    RtlInitUnicodeString(&PortName,L"\\DbgSsApiPort");
    st = NtConnectPort(
            &DbgSspApiPort,
            &PortName,
            &DynamicQos,
            NULL,
            NULL,
            NULL,
            NULL,
            0L
            );

    return st;

}

NTSTATUS
DbgSspException (
    IN PDBGSS_CONTINUE_KEY ContinueKey,
    IN PCLIENT_ID AppClientId,
    IN PDBGKM_EXCEPTION Exception
    )

/*++

Routine Description:

    This function is called by emulation subsystems to report that
    an exception occured in a thread.

Arguments:

    ContinueKey - Supplies the captured DbgKm API message that needs a reply
        should this API complete successfully.

    AppClientId - Supplies the address of the ClientId of the thread that
        encountered an exception.

    Exception - Supplies the address of the Exception message as sent
        through the applications DebugPort.

Return Value:

    TBD

--*/

{

    NTSTATUS st;
    DBGSS_APIMSG ApiMsg;

    PDBGKM_EXCEPTION args;

    args = &ApiMsg.u.Exception;

    DBGSS_FORMAT_API_MSG(ApiMsg,DbgSsExceptionApi,sizeof(*args),AppClientId,ContinueKey);

    *args = *Exception;

    st = NtRequestPort(DbgSspApiPort, (PPORT_MESSAGE) &ApiMsg);

    return st;
}

NTSTATUS
DbgSspCreateThread (
    IN PDBGSS_CONTINUE_KEY ContinueKey,
    IN PCLIENT_ID AppClientId,
    IN PDBGKM_CREATE_THREAD NewThread
    )

/*++

Routine Description:

    This function is called by emulation subsystems to report the
    creation of a new thread to the Dbg subsystem.

Arguments:

    ContinueKey - Supplies the captured DbgKm API message that needs a reply
        should this API complete successfully.

    AppClientId - Supplies the address of the new thread's ClientId.

    NewThread - Supplies the address of the NewThread message as sent through
        the applications DebugPort. The calling subsystem may modify the
        SubSystemKey field of this message.

Return Value:

    TBD

--*/

{

    NTSTATUS st;
    DBGSS_APIMSG ApiMsg;

    PDBGKM_CREATE_THREAD args;

    args = &ApiMsg.u.CreateThread;

    DBGSS_FORMAT_API_MSG(ApiMsg,DbgSsCreateThreadApi,sizeof(*args),AppClientId, ContinueKey);

    *args = *NewThread;

    st = NtRequestPort(DbgSspApiPort, (PPORT_MESSAGE) &ApiMsg);

    return st;
}


NTSTATUS
DbgSspCreateProcess (
    IN PDBGSS_CONTINUE_KEY ContinueKey,
    IN PCLIENT_ID AppClientId,
    IN PCLIENT_ID DebugUiClientId,
    IN PDBGKM_CREATE_PROCESS NewProcess
    )

/*++

Routine Description:

    This function is called by emulation subsystems to report the
    creation of a new process to the Dbg subsystem.  It is the
    responsibility of individual subsystems to track a process and its
    controlling DebugUi.

Arguments:

    ContinueKey - Supplies the captured DbgKm API message that needs a reply
        should this API complete successfully.

    AppClientId - Supplies the address of the new thread's ClientId.

    DebugUiClientId - Supplies the address of the ClientId of the user
        interface controlling the new thread.

    NewProcess - Supplies the address of the NewProcess message as sent through
        the applications DebugPort. The calling subsystem may modify the
        SubSystemKey fields of this message.

Return Value:

    TBD

--*/

{

    NTSTATUS st;
    DBGSS_APIMSG ApiMsg;

    PDBGSS_CREATE_PROCESS args;

    args = &ApiMsg.u.CreateProcessInfo;

    DBGSS_FORMAT_API_MSG(ApiMsg,DbgSsCreateProcessApi,sizeof(*args),AppClientId,ContinueKey);

    args->DebugUiClientId = *DebugUiClientId;
    args->NewProcess = *NewProcess;

    st = NtRequestPort(DbgSspApiPort, (PPORT_MESSAGE) &ApiMsg);

    return st;

}

NTSTATUS
DbgSspExitThread (
    IN PDBGSS_CONTINUE_KEY ContinueKey,
    IN PCLIENT_ID AppClientId,
    IN PDBGKM_EXIT_THREAD ExitThread
    )

/*++

Routine Description:

    This function is called by emulation subsystems to report that
    a thread is exiting.

Arguments:

    ContinueKey - Supplies the captured DbgKm API message that needs a reply
        should this API complete successfully.

    AppClientId - Supplies the address of the exiting thread's ClientId.

    ExitThread - Supplies the address of the ExitThread message as sent
        through the applications DebugPort.

Return Value:

    TBD

--*/

{

    NTSTATUS st;
    DBGSS_APIMSG ApiMsg;

    PDBGKM_EXIT_THREAD args;

    args = &ApiMsg.u.ExitThread;

    DBGSS_FORMAT_API_MSG(ApiMsg,DbgSsExitThreadApi,sizeof(*args),AppClientId,ContinueKey);

    *args = *ExitThread;

    st = NtRequestPort(DbgSspApiPort, (PPORT_MESSAGE) &ApiMsg);

    return st;
}

NTSTATUS
DbgSspExitProcess (
    IN PDBGSS_CONTINUE_KEY ContinueKey,
    IN PCLIENT_ID AppClientId,
    IN PDBGKM_EXIT_PROCESS ExitProcess
    )

/*++

Routine Description:

    This function is called by emulation subsystems to report that
    a process is exiting.

Arguments:

    ContinueKey - Supplies the captured DbgKm API message that needs a reply
        should this API complete successfully.

    AppClientId - Supplies the address of the ClientId of the last thread
        in the process to exit.

    ExitProcess - Supplies the address of the ExitProcess message as sent
        through the applications DebugPort.

Return Value:

    TBD

--*/

{

    NTSTATUS st;
    DBGSS_APIMSG ApiMsg;

    PDBGKM_EXIT_PROCESS args;

    args = &ApiMsg.u.ExitProcess;

    DBGSS_FORMAT_API_MSG(ApiMsg,DbgSsExitProcessApi,sizeof(*args),AppClientId,ContinueKey);

    *args = *ExitProcess;

    st = NtRequestPort(DbgSspApiPort, (PPORT_MESSAGE) &ApiMsg);

    return st;
}

NTSTATUS
DbgSspLoadDll (
    IN PDBGSS_CONTINUE_KEY ContinueKey,
    IN PCLIENT_ID AppClientId,
    IN PDBGKM_LOAD_DLL LoadDll
    )

/*++

Routine Description:

    This function is called by emulation subsystems to report that
    a a process has loaded a DLL

Arguments:

    ContinueKey - Supplies the captured DbgKm API message that needs a reply
        should this API complete successfully.

    AppClientId - Supplies the address of the ClientId of the thread
        that mapped the section.

    LoadDll - Supplies the address of the LoadDll message as sent
        through the applications DebugPort.

Return Value:

    TBD

--*/

{

    NTSTATUS st;
    DBGSS_APIMSG ApiMsg;

    PDBGKM_LOAD_DLL args;

    args = &ApiMsg.u.LoadDll;

    DBGSS_FORMAT_API_MSG(ApiMsg,DbgSsLoadDllApi,sizeof(*args),AppClientId,ContinueKey);

    *args = *LoadDll;

    st = NtRequestPort(DbgSspApiPort, (PPORT_MESSAGE) &ApiMsg);

    return st;
}

NTSTATUS
DbgSspUnloadDll (
    IN PDBGSS_CONTINUE_KEY ContinueKey,
    IN PCLIENT_ID AppClientId,
    IN PDBGKM_UNLOAD_DLL UnloadDll
    )

/*++

Routine Description:

    This function is called by emulation subsystems to report that
    a a process has un-mapped a view of a section

Arguments:

    ContinueKey - Supplies the captured DbgKm API message that needs a reply
        should this API complete successfully.

    AppClientId - Supplies the address of the ClientId of the thread
        that mapped the section.

    UnloadDll - Supplies the address of the UnloadDll message as sent
        through the applications DebugPort.

Return Value:

    TBD

--*/

{

    NTSTATUS st;
    DBGSS_APIMSG ApiMsg;

    PDBGKM_UNLOAD_DLL args;

    args = &ApiMsg.u.UnloadDll;

    DBGSS_FORMAT_API_MSG(ApiMsg,DbgSsUnloadDllApi,sizeof(*args),AppClientId,ContinueKey);

    *args = *UnloadDll;

    st = NtRequestPort(DbgSspApiPort, (PPORT_MESSAGE) &ApiMsg);

    return st;
}


NTSTATUS
DbgSsInitialize(
    IN HANDLE KmReplyPort,
    IN PDBGSS_UI_LOOKUP UiLookUpRoutine,
    IN PDBGSS_SUBSYSTEMKEY_LOOKUP SubsystemKeyLookupRoutine OPTIONAL,
    IN PDBGSS_DBGKM_APIMSG_FILTER KmApiMsgFilter OPTIONAL
    )

/*++

Routine Description:

    This function is called by a subsystem to initialize
    portions of the debug subsystem dll. The main purpose of
    this function is to set up callouts that are needed in order
    to use DbgSsHandleKmApiMsg, and to connect to the debug server.

Arguments:

    KmReplyPort - Supplies a handle to the port that the subsystem
        receives DbgKm API messages on.

    UiLookupRoutine - Supplies the address of a function that will
        be called upon receipt of a process creation message. The
        purpose of this function is to identify the client id of the
        debug ui controlling the process.

    SubsystemKeyLookupRoutine - Supplies the address of a function that
        will be called upon receipt of process creation and thread
        creation messages.  The purpose of this function is to allow a
        subsystem to correlate a key value with a given process or
        thread.

    KmApiMsgFilter - Supplies the address of a function that will be
        called upon receipt of a DbgKm Api message. This function can
        take any action. If it returns any value other than DBG_CONTINUE,
        DbgSsHandleKmApiMsg will not process the message. This function
        is called before any other call outs occur.

Return Value:

    Return code of DbgSsConnectToDbg.

--*/

{
    NTSTATUS st;

    st = DbgSspConnectToDbg();

    if (NT_SUCCESS(st)) {
        DbgSspKmReplyPort = KmReplyPort;
        DbgSspUiLookUpRoutine = UiLookUpRoutine;
        DbgSspSubsystemKeyLookupRoutine = SubsystemKeyLookupRoutine;
        DbgSspKmApiMsgFilter = KmApiMsgFilter;

        st = RtlCreateUserThread(
                NtCurrentProcess(),
                NULL,
                FALSE,
                0L,
                0L,
                0L,
                DbgSspSrvApiLoop,
                NULL,
                NULL,
                NULL
                );
        ASSERT( NT_SUCCESS(st) );
    }

    return st;
}


#if DBG
PSZ DbgpKmApiName[ DbgKmMaxApiNumber+1 ] = {
    "DbgKmException",
    "DbgKmCreateThread",
    "DbgKmCreateProcess",
    "DbgKmExitThread",
    "DbgKmExitProcess",
    "DbgKmLoadDll",
    "DbgKmUnloadDll",
    "Unknown DbgKm Api Number"
};
#endif // DBG

VOID
DbgSsHandleKmApiMsg(
    IN PDBGKM_APIMSG ApiMsg,
    IN HANDLE ReplyEvent OPTIONAL
    )

/*++

Routine Description:

    This function is called by a subsystem upon receipt of a message whose
    type is LPC_DEBUG_EVENT. The purpose of this function is to propagate
    the debug event message to the debug server.

    A number of callouts are performed prior to propagating the message:

      - For all messages, the KmApiMsgFilter is called (if it was
        supplied during DbgSsInitialize.  If this returns anything other
        that DBG_CONTINUE, the message is not propagated by this
        function.  The caller is responsible for event propagation, and
        for replying to the thread reporting the debug event.

      - For create process messages, the UiLookupRoutine is called.  If
        a success code is returned than message is propagated.
        Otherwise, a reply is generated to the thread reporting the
        debug event.

     -  For create process and create thread messages,
        SubsystemKeyLookupRoutine is called.  Failure does not effect
        propagation.  It simply inhibits the update of messages
        SubSystemKey field.

    Based on the debug event type, a DBGSS_APIMSG is formated and sent
    as a datagram to the debug server.

Arguments:

    ApiMsg - Supplies the debug event message to propagate to the debug
        server.

    ReplyEvent - An optional parameter, that if specified supplies a handle
        to an event that is to be signaled rather that generating a reply
        to this message.

Return Value:

    None.

--*/


{
    NTSTATUS st;
    CLIENT_ID DebugUiClientId;
    ULONG SubsystemKey;
    PDBGSS_CONTINUE_KEY ContinueKey;

    ApiMsg->ReturnedStatus = STATUS_PENDING;

#if DBG && 0
    if (ApiMsg->ApiNumber >= DbgKmMaxApiNumber ) {
        ApiMsg->ApiNumber = DbgKmMaxApiNumber;
    }
    DbgPrint( "DBG:  %s Api Request received from %lx.%lx\n",
              DbgpKmApiName[ ApiMsg->ApiNumber ],
              ApiMsg->h.ClientId.UniqueProcess,
              ApiMsg->h.ClientId.UniqueThread
           );
#endif // DBG

    if (DbgSspKmApiMsgFilter) {
        if ( (DbgSspKmApiMsgFilter)(ApiMsg) != DBG_CONTINUE ) {
            return;
        }
    }

    ContinueKey = (PDBGSS_CONTINUE_KEY) RtlAllocateHeap(RtlProcessHeap(), MAKE_TAG( DBG_TAG ), sizeof(*ContinueKey));
    if ( !ContinueKey ) {
        ApiMsg->ReturnedStatus = STATUS_NO_MEMORY;
        if ( ARGUMENT_PRESENT(ReplyEvent) ) {
            st = NtSetEvent(ReplyEvent,NULL);
        } else {
            st = NtReplyPort(DbgSspKmReplyPort,
                             (PPORT_MESSAGE)ApiMsg
                            );
        }
        ASSERT(NT_SUCCESS(st));
        return;
    }
    ContinueKey->KmApiMsg = *ApiMsg;

    ContinueKey->ReplyEvent = ReplyEvent;

    switch (ApiMsg->ApiNumber) {

    case DbgKmExceptionApi :
        st = DbgSspException(
                ContinueKey,
                &ApiMsg->h.ClientId,
                &ApiMsg->u.Exception
                );

        break;

    case DbgKmCreateThreadApi :

        if ( DbgSspSubsystemKeyLookupRoutine ) {

            st = (DbgSspSubsystemKeyLookupRoutine)(
                    &ApiMsg->h.ClientId,
                    &SubsystemKey,
                    FALSE
                    );

            if ( NT_SUCCESS(st) ) {
                ApiMsg->u.CreateThread.SubSystemKey = SubsystemKey;
            }
        }

        st = DbgSspCreateThread(
                ContinueKey,
                &ApiMsg->h.ClientId,
                &ApiMsg->u.CreateThread
                );

        break;

    case DbgKmCreateProcessApi :

        st = (DbgSspUiLookUpRoutine)(
                &ApiMsg->h.ClientId,
                &DebugUiClientId
                );
        if ( !NT_SUCCESS(st) ) {
            break;
        }

        if ( DbgSspSubsystemKeyLookupRoutine ) {

            st = (DbgSspSubsystemKeyLookupRoutine)(
                    &ApiMsg->h.ClientId,
                    &SubsystemKey,
                    TRUE
                    );

            if ( NT_SUCCESS(st) ) {
                ApiMsg->u.CreateProcessInfo.SubSystemKey = SubsystemKey;
            }

            st = (DbgSspSubsystemKeyLookupRoutine)(
                    &ApiMsg->h.ClientId,
                    &SubsystemKey,
                    FALSE
                    );

            if ( NT_SUCCESS(st) ) {
                ApiMsg->u.CreateProcessInfo.InitialThread.SubSystemKey = SubsystemKey;
            }
        }

        st = DbgSspCreateProcess(
                ContinueKey,
                &ApiMsg->h.ClientId,
                &DebugUiClientId,
                &ApiMsg->u.CreateProcessInfo
                );
        break;


    case DbgKmExitThreadApi :
        st = DbgSspExitThread(
                ContinueKey,
                &ApiMsg->h.ClientId,
                &ApiMsg->u.ExitThread
                );
        break;

    case DbgKmExitProcessApi :
        st = DbgSspExitProcess(
                ContinueKey,
                &ApiMsg->h.ClientId,
                &ApiMsg->u.ExitProcess
                );
        break;

    case DbgKmLoadDllApi :
        st = DbgSspLoadDll(
                ContinueKey,
                &ApiMsg->h.ClientId,
                &ApiMsg->u.LoadDll
                );
        break;

    case DbgKmUnloadDllApi :
        st = DbgSspUnloadDll(
                ContinueKey,
                &ApiMsg->h.ClientId,
                &ApiMsg->u.UnloadDll
                );
        break;

    default :
        st = STATUS_NOT_IMPLEMENTED;
    }

    if ( !NT_SUCCESS(st) ) {
        ApiMsg->ReturnedStatus = st;
        RtlFreeHeap(RtlProcessHeap(), 0, ContinueKey);
        if ( ARGUMENT_PRESENT(ReplyEvent) ) {
            st = NtSetEvent(ReplyEvent,NULL);
        } else {
            st = NtReplyPort(DbgSspKmReplyPort,
                             (PPORT_MESSAGE)ApiMsg
                            );
        }
        ASSERT(NT_SUCCESS(st));
    }
}


NTSTATUS
DbgSspSrvApiLoop(
    IN PVOID ThreadParameter
    )

/*++

Routine Description:

    This loop services Dbg Subsystem server originated messages.

Arguments:

    ThreadParameter - Not used.

Return Value:

    None.

--*/

{
    DBGSRV_APIMSG DbgSrvApiMsg;
    PDBGSS_CONTINUE_KEY ContinueKey;
    NTSTATUS st;

    for(;;) {

        st = NtReplyWaitReceivePort(
                DbgSspApiPort,
                NULL,
                NULL,
                (PPORT_MESSAGE) &DbgSrvApiMsg
                );

        if (!NT_SUCCESS( st )) {
            DbgPrint( "DBGSS: NtReplyWaitReceivePort failed - Status == %x\n", st );
            DbgBreakPoint();
            continue;
            }

        ASSERT(DbgSrvApiMsg.ApiNumber < DbgSrvMaxApiNumber);

        switch (DbgSrvApiMsg.ApiNumber ) {
        case DbgSrvContinueApi :

            //
            // Might want to implement continue status based callout
            // like DBG_TERMINATE_PROCESS/THREAD
            //

            ContinueKey = (PDBGSS_CONTINUE_KEY) DbgSrvApiMsg.ContinueKey;
            ContinueKey->KmApiMsg.ReturnedStatus = DbgSrvApiMsg.ReturnedStatus;

            if ( ContinueKey->ReplyEvent ) {
                st = NtSetEvent(ContinueKey->ReplyEvent,NULL);
            } else {
                st = NtReplyPort(DbgSspKmReplyPort,
                                 (PPORT_MESSAGE) &ContinueKey->KmApiMsg
                                );
            }

            RtlFreeHeap(RtlProcessHeap(), 0, ContinueKey);
            break;
        default :
            ASSERT(FALSE);
        }
    }

    //
    // Make the compiler happy
    //

    return st;
}