Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
40 | halyavin | 1 | ; this code uses following additions to system structures: |
2 | ; in additional app info at 80000..8FFFF: |
||
3 | ; AC (dword) 0 or debugger slot |
||
4 | ; BC (dword) address of debug event memory |
||
5 | ; new process slot state: |
||
6 | ; 1 = suspended |
||
7 | ; 2 = suspended waiting for event |
||
8 | ; new event: 8 (and new possible bitflag for A8 in additional app info) |
||
9 | ; diamond, 2006 |
||
10 | sys_debug_services: |
||
11 | cmp eax, 8 |
||
12 | ja @f |
||
13 | jmp dword [sys_debug_services_table+eax*4] |
||
14 | @@: ret |
||
15 | sys_debug_services_table: |
||
16 | dd debug_set_event_data |
||
17 | dd debug_getcontext |
||
18 | dd debug_setcontext |
||
19 | dd debug_detach |
||
20 | dd debug_suspend |
||
21 | dd debug_resume |
||
22 | dd debug_read_process_memory |
||
23 | dd debug_write_process_memory |
||
24 | dd debug_terminate |
||
25 | |||
26 | debug_set_event_data: |
||
27 | ; in: ebx = pointer |
||
28 | ; destroys eax |
||
29 | mov eax, [0x3000] |
||
30 | shl eax, 8 |
||
31 | mov [eax+0x80000+0xBC], ebx |
||
32 | ret |
||
33 | |||
34 | get_debuggee_slot: |
||
35 | ; in: ebx=PID |
||
36 | ; out: CF=1 if error |
||
37 | ; CF=0 and eax=slot*0x20 if ok |
||
38 | ; out: interrupts disabled |
||
39 | cli |
||
40 | mov eax, ebx |
||
41 | call pid_to_slot |
||
42 | test eax, eax |
||
43 | jz .ret_bad |
||
44 | shl eax, 5 |
||
45 | push ebx |
||
46 | mov ebx, [0x3000] |
||
47 | cmp [0x80000+eax*8+0xAC], ebx |
||
48 | pop ebx |
||
49 | jnz .ret_bad |
||
50 | ; clc ; automatically |
||
51 | ret |
||
52 | .ret_bad: |
||
53 | stc |
||
54 | ret |
||
55 | |||
56 | debug_detach: |
||
57 | ; in: ebx=pid |
||
58 | ; destroys eax,ebx |
||
59 | call get_debuggee_slot |
||
60 | jc .ret |
||
61 | and dword [eax*8+0x80000+0xAC], 0 |
||
62 | call do_resume |
||
63 | .ret: |
||
64 | sti |
||
65 | ret |
||
66 | |||
67 | debug_terminate: |
||
68 | ; in: ebx=pid |
||
69 | call get_debuggee_slot |
||
70 | jc debug_detach.ret |
||
71 | mov ebx, eax |
||
72 | shr ebx, 5 |
||
73 | push 2 |
||
74 | pop eax |
||
75 | jmp sys_system |
||
76 | |||
77 | debug_suspend: |
||
78 | ; in: ebx=pid |
||
79 | ; destroys eax,ebx |
||
80 | call get_debuggee_slot |
||
81 | jc .ret |
||
82 | mov bl, [0x3000+eax+0xA] ; process state |
||
83 | test bl, bl |
||
84 | jz .1 |
||
85 | cmp bl, 5 |
||
86 | jnz .ret |
||
87 | mov bl, 2 |
||
88 | .2: mov [0x3000+eax+0xA], bl |
||
89 | .ret: |
||
90 | sti |
||
91 | ret |
||
92 | .1: |
||
93 | inc ebx |
||
94 | jmp .2 |
||
95 | |||
96 | do_resume: |
||
97 | mov bl, [0x3000+eax+0xA] |
||
98 | cmp bl, 1 |
||
99 | jz .1 |
||
100 | cmp bl, 2 |
||
101 | jnz .ret |
||
102 | mov bl, 5 |
||
103 | .2: mov [0x3000+eax+0xA], bl |
||
104 | .ret: ret |
||
105 | .1: dec ebx |
||
106 | jmp .2 |
||
107 | |||
108 | debug_resume: |
||
109 | ; in: ebx=pid |
||
110 | ; destroys eax,ebx |
||
111 | call get_debuggee_slot |
||
112 | jc .ret |
||
113 | call do_resume |
||
114 | .ret: sti |
||
115 | ret |
||
116 | |||
117 | debug_getcontext: |
||
118 | ; in: |
||
119 | ; ebx=pid |
||
120 | ; ecx=sizeof(CONTEXT) |
||
121 | ; edx->CONTEXT |
||
122 | ; destroys eax,ecx,edx,esi,edi |
||
123 | cmp ecx, 28h |
||
124 | jnz .ret |
||
125 | add edx, std_application_base_address |
||
126 | push ebx |
||
127 | mov ebx, edx |
||
128 | call check_region |
||
129 | pop ebx |
||
130 | dec eax |
||
131 | jnz .ret |
||
132 | call get_debuggee_slot |
||
133 | jc .ret |
||
134 | imul eax, tss_step/32 |
||
135 | add eax, tss_data |
||
136 | mov edi, edx |
||
137 | cmp [l.cs - tss_sceleton + eax], app_code |
||
138 | jnz .ring0 |
||
139 | lea esi, [l.eip - tss_sceleton + eax] |
||
140 | shr ecx, 2 |
||
141 | rep movsd |
||
142 | jmp .ret |
||
143 | .ring0: |
||
144 | ; note that following code assumes that all interrupt/exception handlers |
||
145 | ; saves ring-3 context by push ds es, pushad in this order |
||
146 | mov esi, [l.esp0 - tss_sceleton + eax] |
||
147 | ; top of ring0 stack: ring3 stack ptr (ss+esp), iret data (cs+eip+eflags), ds, es, pushad |
||
148 | sub esi, 8+12+8+20h |
||
149 | lodsd |
||
150 | mov [edi+24h], eax |
||
151 | lodsd |
||
152 | mov [edi+20h], eax |
||
153 | lodsd |
||
154 | mov [edi+1Ch], eax |
||
155 | lodsd |
||
156 | lodsd |
||
157 | mov [edi+14h], eax |
||
158 | lodsd |
||
159 | mov [edi+10h], eax |
||
160 | lodsd |
||
161 | mov [edi+0Ch], eax |
||
162 | lodsd |
||
163 | mov [edi+8], eax |
||
164 | add esi, 8 |
||
165 | lodsd |
||
166 | mov [edi], eax |
||
167 | lodsd |
||
168 | lodsd |
||
169 | mov [edi+4], eax |
||
170 | lodsd |
||
171 | mov [edi+18h], eax |
||
172 | .ret: |
||
173 | sti |
||
174 | ret |
||
175 | |||
176 | debug_setcontext: |
||
177 | ; in: |
||
178 | ; ebx=pid |
||
179 | ; ecx=sizeof(CONTEXT) |
||
180 | ; edx->CONTEXT |
||
181 | ; destroys eax,ecx,edx,esi,edi |
||
182 | cmp ecx, 28h |
||
183 | jnz .ret |
||
184 | add edx, std_application_base_address |
||
185 | push ebx |
||
186 | mov ebx, edx |
||
187 | call check_region |
||
188 | pop ebx |
||
189 | dec eax |
||
190 | jnz .ret |
||
191 | call get_debuggee_slot |
||
192 | jc .stiret |
||
193 | imul eax, tss_step/32 |
||
194 | add eax, tss_data |
||
195 | mov esi, edx |
||
196 | cmp [l.cs - tss_sceleton + eax], app_code |
||
197 | jnz .ring0 |
||
198 | lea edi, [l.eip - tss_sceleton + eax] |
||
199 | shr ecx, 2 |
||
200 | rep movsd |
||
201 | jmp .stiret |
||
202 | .ring0: |
||
203 | mov edi, [l.esp0 - tss_sceleton + eax] |
||
204 | sub edi, 8+12+8+20h |
||
205 | mov eax, [esi+24h] |
||
206 | stosd |
||
207 | mov eax, [esi+20h] |
||
208 | stosd |
||
209 | mov eax, [esi+1Ch] |
||
210 | stosd |
||
211 | scasd |
||
212 | mov eax, [esi+14h] |
||
213 | stosd |
||
214 | mov eax, [esi+10h] |
||
215 | stosd |
||
216 | mov eax, [esi+0Ch] |
||
217 | stosd |
||
218 | mov eax, [esi+8] |
||
219 | stosd |
||
220 | add edi, 8 |
||
221 | mov eax, [esi] |
||
222 | stosd |
||
223 | scasd |
||
224 | mov eax, [esi+4] |
||
225 | stosd |
||
226 | mov eax, [esi+18h] |
||
227 | stosd |
||
228 | .stiret: |
||
229 | sti |
||
230 | .ret: |
||
231 | ret |
||
232 | |||
233 | debug_read_process_memory: |
||
234 | ; in: |
||
235 | ; ebx=pid |
||
236 | ; ecx=length |
||
237 | ; esi->buffer in debugger |
||
238 | ; edx=address in debuggee |
||
239 | ; out: [esp+36]=eax=sizeof(read) |
||
240 | ; destroys all |
||
241 | add esi, std_application_base_address |
||
242 | push ebx |
||
243 | mov ebx, esi |
||
244 | call check_region |
||
245 | pop ebx |
||
246 | dec eax |
||
247 | jnz .ret |
||
248 | call get_debuggee_slot |
||
249 | jc .stiret |
||
250 | mov ebx, esi |
||
251 | call read_process_memory |
||
252 | .stiret: |
||
253 | sti |
||
254 | .ret: |
||
255 | ret |
||
256 | |||
257 | debug_write_process_memory: |
||
258 | ; in: |
||
259 | ; ebx=pid |
||
260 | ; ecx=length |
||
261 | ; esi->buffer in debugger |
||
262 | ; edx=address in debuggee |
||
263 | ; out: [esp+36]=eax=sizeof(write) |
||
264 | ; destroys all |
||
265 | add esi, std_application_base_address |
||
266 | push ebx |
||
267 | mov ebx, esi |
||
268 | call check_region |
||
269 | pop ebx |
||
270 | dec eax |
||
271 | jnz .ret |
||
272 | call get_debuggee_slot |
||
273 | jc .stiret |
||
274 | mov ebx, esi |
||
275 | call write_process_memory |
||
276 | .stiret: |
||
277 | sti |
||
278 | .ret: |
||
279 | ret |
||
280 | |||
281 | debugger_notify: |
||
282 | ; in: eax=debugger slot |
||
283 | ; ecx=size of debug message |
||
284 | ; [esp+4]..[esp+4+ecx]=message |
||
285 | ; interrupts must be disabled! |
||
286 | ; destroys all general registers |
||
287 | ; interrupts remain disabled |
||
288 | mov ebp, eax |
||
289 | shl eax, 8 |
||
290 | .1: |
||
291 | mov edx, [0x80000+eax+0xBC] |
||
292 | test edx, edx |
||
293 | jz .ret |
||
294 | ; read buffer header |
||
295 | push ecx |
||
296 | push eax |
||
297 | push eax |
||
298 | mov eax, ebp |
||
299 | mov ebx, esp |
||
300 | mov ecx, 8 |
||
301 | call read_process_memory |
||
302 | cmp eax, ecx |
||
303 | jz @f |
||
304 | add esp, 12 |
||
305 | jmp .ret |
||
306 | @@: |
||
307 | cmp dword [ebx], 0 |
||
308 | jg @f |
||
309 | .2: |
||
310 | pop ecx |
||
311 | pop ecx |
||
312 | pop ecx |
||
313 | sti |
||
314 | call change_task |
||
315 | cli |
||
316 | jmp .1 |
||
317 | @@: |
||
318 | mov ecx, [ebx+8] |
||
319 | add ecx, [ebx+4] |
||
320 | cmp ecx, [ebx] |
||
321 | ja .2 |
||
322 | ; advance buffer position |
||
323 | push ecx |
||
324 | mov ecx, 4 |
||
325 | sub ebx, ecx |
||
326 | mov eax, ebp |
||
327 | add edx, ecx |
||
328 | call write_process_memory |
||
329 | pop eax |
||
330 | ; write message |
||
331 | mov eax, ebp |
||
332 | add edx, ecx |
||
333 | add edx, [ebx+8] |
||
334 | add ebx, 20 |
||
335 | pop ecx |
||
336 | pop ecx |
||
337 | pop ecx |
||
338 | call write_process_memory |
||
339 | ; new debug event |
||
340 | mov eax, ebp |
||
341 | shl eax, 8 |
||
342 | or byte [0x80000+eax+0xA8+1], 1 ; set flag 100h |
||
343 | .ret: |
||
344 | ret |