1f7185bb551c95cc0e205d8d701d24bcb7449c19
[reactos.git] / reactos / lib / ntdll / dbg / debug.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: lib/ntdll/dbg/debug.c
6 * PURPOSE: User mode debugger support functions
7 * PROGRAMMER: Eric Kohl
8 * UPDATE HISTORY:
9 * 14/04/2000 Created
10 */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ntdll.h>
15 #define NDEBUG
16 #include <debug.h>
17
18 /* FUNCTIONS *****************************************************************/
19
20 static HANDLE DbgSsApiPort = NULL;
21 static HANDLE DbgSsReplyPort = NULL;
22 static NTSTATUS (STDCALL * DbgSsCallback)(PVOID,PVOID) = NULL;
23
24
25 typedef struct _LPC_DBGSS_MESSAGE
26 {
27 LPC_MESSAGE Header;
28 ULONG Unknown1;
29 ULONG Unknown2;
30 ULONG Unknown3;
31 ULONG Unknown4;
32 } LPC_DBGSS_MESSAGE, *PLPC_DBGSS_MESSAGE;
33
34
35 /* FUNCTIONS *****************************************************************/
36
37 VOID STDCALL
38 DbgSsServerThread(PVOID Unused)
39 {
40 LPC_DBGSS_MESSAGE Message;
41 NTSTATUS Status;
42
43 for (;;)
44 {
45 Status = NtReplyWaitReceivePort (DbgSsApiPort,
46 NULL,
47 NULL,
48 (PLPC_MESSAGE)&Message);
49 if (!NT_SUCCESS(Status))
50 {
51 DbgPrint ("DbgSs: NtReplyWaitReceivePort failed - Status == %lx\n",
52 Status);
53
54 DbgBreakPoint ();
55 }
56 else
57 {
58 /* FIXME: missing code!! */
59
60 }
61 }
62 }
63
64
65 /*
66 * @unimplemented
67 */
68 NTSTATUS STDCALL
69 DbgSsHandleKmApiMsg(ULONG Unknown1,
70 HANDLE EventHandle)
71 {
72 return STATUS_NOT_IMPLEMENTED;
73 }
74
75
76 /*
77 * @implemented
78 */
79 NTSTATUS STDCALL
80 DbgSsInitialize(HANDLE ReplyPort,
81 PVOID Callback,
82 ULONG Unknown2,
83 ULONG Unknown3)
84 {
85 SECURITY_QUALITY_OF_SERVICE Qos;
86 UNICODE_STRING PortName = RTL_CONSTANT_STRING(L"\\DbgSsApiPort");
87 NTSTATUS Status;
88
89 Qos.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
90 Qos.ImpersonationLevel = SecurityIdentification;
91 Qos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
92 Qos.EffectiveOnly = TRUE;
93
94 Status = NtConnectPort (&DbgSsApiPort,
95 &PortName,
96 &Qos,
97 NULL,
98 NULL,
99 NULL,
100 NULL,
101 NULL);
102 if (!NT_SUCCESS(Status))
103 return Status;
104
105 DbgSsReplyPort = ReplyPort;
106 DbgSsCallback = Callback;
107 // UnknownData2 = Unknown2;
108 // UnknownData3 = Unknown3;
109
110 Status = RtlCreateUserThread (NtCurrentProcess (),
111 NULL,
112 FALSE,
113 0,
114 0,
115 0,
116 (PTHREAD_START_ROUTINE)DbgSsServerThread,
117 NULL,
118 NULL,
119 NULL);
120
121 return Status;
122 }
123
124
125 /*
126 * @implemented
127 */
128 NTSTATUS STDCALL
129 DbgUiConnectToDbg(VOID)
130 {
131 SECURITY_QUALITY_OF_SERVICE Qos;
132 UNICODE_STRING PortName = RTL_CONSTANT_STRING(L"\\DbgUiApiPort");
133 NTSTATUS Status;
134 PTEB Teb;
135 ULONG InfoSize;
136
137 Teb = NtCurrentTeb ();
138
139 Qos.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
140 Qos.ImpersonationLevel = SecurityIdentification;
141 Qos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
142 Qos.EffectiveOnly = TRUE;
143
144 InfoSize = sizeof(ULONG);
145
146 Status = NtConnectPort (&Teb->DbgSsReserved[1],
147 &PortName,
148 &Qos,
149 NULL,
150 NULL,
151 NULL,
152 &Teb->DbgSsReserved[0],
153 &InfoSize);
154 if (!NT_SUCCESS(Status))
155 {
156 Teb->DbgSsReserved[1] = NULL;
157 return Status;
158 }
159
160 NtRegisterThreadTerminatePort(Teb->DbgSsReserved[1]);
161
162 return Status;
163 }
164
165
166 /*
167 * @unimplemented
168 */
169 NTSTATUS STDCALL
170 DbgUiContinue(PCLIENT_ID ClientId,
171 ULONG ContinueStatus)
172 {
173 return STATUS_NOT_IMPLEMENTED;
174 }
175
176
177 /*
178 * @unimplemented
179 */
180 NTSTATUS STDCALL
181 DbgUiWaitStateChange(ULONG Unknown1,
182 ULONG Unknown2)
183 {
184 return STATUS_NOT_IMPLEMENTED;
185 }
186
187 VOID STDCALL DbgUiRemoteBreakin(VOID)
188 {
189 DbgBreakPoint();
190
191 RtlExitUserThread(STATUS_SUCCESS);
192 }
193
194 NTSTATUS STDCALL DbgUiIssueRemoteBreakin(HANDLE Process)
195 {
196 HANDLE hThread;
197 CLIENT_ID cidClientId;
198 NTSTATUS nErrCode;
199 ULONG nStackSize = PAGE_SIZE;
200
201 nErrCode = RtlCreateUserThread
202 (
203 Process,
204 NULL,
205 FALSE,
206 0,
207 nStackSize,
208 nStackSize,
209 (PTHREAD_START_ROUTINE)DbgUiRemoteBreakin,
210 NULL,
211 &hThread,
212 &cidClientId
213 );
214
215 if(!NT_SUCCESS(nErrCode)) return nErrCode;
216
217 NtClose(hThread);
218
219 return STATUS_SUCCESS;
220 }
221
222 /* EOF */