Rev 8869 | Rev 9715 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2288 | clevermous | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
5363 | yogev_ezra | 3 | ;; Copyright (C) KolibriOS team 2004-2015. All rights reserved. ;; |
2288 | clevermous | 4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
||
6 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
7 | |||
8 | $Revision: 8876 $ |
||
9 | |||
10 | |||
11 | ; diamond, 2006 |
||
12 | sys_debug_services: |
||
13 | cmp ebx, 9 |
||
14 | ja @f |
||
15 | jmp dword [sys_debug_services_table+ebx*4] |
||
16 | @@: |
||
17 | ret |
||
18 | iglobal |
||
19 | align 4 |
||
20 | sys_debug_services_table: |
||
21 | dd debug_set_event_data |
||
22 | dd debug_getcontext |
||
23 | dd debug_setcontext |
||
24 | dd debug_detach |
||
25 | dd debug_suspend |
||
26 | dd debug_resume |
||
27 | dd debug_read_process_memory |
||
28 | dd debug_write_process_memory |
||
29 | dd debug_terminate |
||
30 | dd debug_set_drx |
||
31 | endg |
||
32 | debug_set_event_data: |
||
33 | ; in: ecx = pointer |
||
34 | ; destroys eax |
||
35 | mov eax, [current_slot] |
||
36 | mov [eax+APPDATA.dbg_event_mem], ecx |
||
37 | ret |
||
38 | |||
39 | get_debuggee_slot: |
||
40 | ; in: ecx=PID |
||
41 | ; out: CF=1 if error |
||
42 | ; CF=0 and eax=slot*0x20 if ok |
||
43 | ; out: interrupts disabled |
||
44 | cli |
||
45 | mov eax, ecx |
||
46 | call pid_to_slot |
||
47 | test eax, eax |
||
48 | jz .ret_bad |
||
49 | shl eax, 5 |
||
50 | push ebx |
||
8869 | rgimad | 51 | mov ebx, [current_slot_idx] |
2288 | clevermous | 52 | cmp [SLOT_BASE+eax*8+APPDATA.debugger_slot], ebx |
53 | pop ebx |
||
54 | jnz .ret_bad |
||
55 | ; clc ; automatically |
||
56 | ret |
||
57 | .ret_bad: |
||
58 | stc |
||
59 | ret |
||
60 | |||
61 | debug_detach: |
||
62 | ; in: ecx=pid |
||
63 | ; destroys eax,ebx |
||
64 | call get_debuggee_slot |
||
65 | jc .ret |
||
66 | and dword [eax*8+SLOT_BASE+APPDATA.debugger_slot], 0 |
||
67 | call do_resume |
||
68 | .ret: |
||
69 | sti |
||
70 | ret |
||
71 | |||
72 | debug_terminate: |
||
73 | ; in: ecx=pid |
||
74 | call get_debuggee_slot |
||
75 | jc debug_detach.ret |
||
76 | mov ecx, eax |
||
77 | shr ecx, 5 |
||
78 | ; push 2 |
||
79 | ; pop ebx |
||
80 | mov edx, esi |
||
81 | jmp sysfn_terminate |
||
82 | |||
83 | debug_suspend: |
||
84 | ; in: ecx=pid |
||
85 | ; destroys eax,ecx |
||
8534 | Coldy | 86 | ; { Patch by Coldy (rev. 7125), reason: http://board.kolibrios.org/viewtopic.php?f=1&t=1712&p=75957#p75957 |
87 | ; cli |
||
88 | ; mov eax, ecx |
||
89 | ; call pid_to_slot |
||
90 | ; shl eax, 5 |
||
91 | ; jz .ret |
||
92 | call get_debuggee_slot |
||
93 | jc .ret |
||
94 | ; } End patch |
||
8869 | rgimad | 95 | mov cl, [TASK_TABLE+eax+TASKDATA.state] ; process state |
2288 | clevermous | 96 | test cl, cl |
97 | jz .1 |
||
98 | cmp cl, 5 |
||
99 | jnz .ret |
||
100 | mov cl, 2 |
||
101 | .2: |
||
8869 | rgimad | 102 | mov [TASK_TABLE+eax+TASKDATA.state], cl |
2288 | clevermous | 103 | .ret: |
104 | sti |
||
105 | ret |
||
106 | .1: |
||
107 | inc ecx |
||
108 | jmp .2 |
||
109 | |||
110 | do_resume: |
||
8869 | rgimad | 111 | mov cl, [TASK_TABLE+eax+TASKDATA.state] |
2288 | clevermous | 112 | cmp cl, 1 |
113 | jz .1 |
||
114 | cmp cl, 2 |
||
115 | jnz .ret |
||
116 | mov cl, 5 |
||
117 | .2: |
||
8869 | rgimad | 118 | mov [TASK_TABLE+eax+TASKDATA.state], cl |
2288 | clevermous | 119 | .ret: |
120 | ret |
||
121 | .1: |
||
122 | dec ecx |
||
123 | jmp .2 |
||
124 | |||
125 | debug_resume: |
||
126 | ; in: ecx=pid |
||
127 | ; destroys eax,ebx |
||
128 | cli |
||
129 | mov eax, ecx |
||
130 | call pid_to_slot |
||
131 | shl eax, 5 |
||
132 | jz .ret |
||
133 | call do_resume |
||
134 | .ret: |
||
135 | sti |
||
136 | ret |
||
137 | |||
138 | debug_getcontext: |
||
139 | ; in: |
||
140 | ; ecx=pid |
||
141 | ; edx=sizeof(CONTEXT) |
||
142 | ; esi->CONTEXT |
||
4893 | Serge | 143 | ; destroys eax,ebx,ecx,edx,esi,edi |
144 | |||
145 | xor ebx, ebx ; 0 - get only gp regs |
||
146 | cmp edx, 40 |
||
147 | je .std_ctx |
||
148 | |||
149 | cmp edx, 48+288 |
||
150 | jne .ret |
||
151 | |||
152 | inc ebx ; 1 - get sse context |
||
153 | ; TODO legacy 32-bit FPU/MMX context |
||
154 | .std_ctx: |
||
2288 | clevermous | 155 | call get_debuggee_slot |
156 | jc .ret |
||
4893 | Serge | 157 | |
158 | shr eax, 5 |
||
159 | cmp eax, [fpu_owner] |
||
160 | jne @f |
||
161 | inc bh ; set swap context flag |
||
162 | @@: |
||
163 | shl eax, 8 |
||
2288 | clevermous | 164 | mov edi, esi |
4893 | Serge | 165 | mov eax, [eax+SLOT_BASE+APPDATA.pl0_stack] |
2288 | clevermous | 166 | lea esi, [eax+RING0_STACK_SIZE] |
167 | |||
168 | .ring0: |
||
169 | ; note that following code assumes that all interrupt/exception handlers |
||
7124 | dunkaist | 170 | ; save ring-3 context by pushad in this order |
2288 | clevermous | 171 | ; top of ring0 stack: ring3 stack ptr (ss+esp), iret data (cs+eip+eflags), pushad |
172 | sub esi, 8+12+20h |
||
173 | lodsd ;edi |
||
174 | mov [edi+24h], eax |
||
175 | lodsd ;esi |
||
176 | mov [edi+20h], eax |
||
177 | lodsd ; ebp |
||
178 | mov [edi+1Ch], eax |
||
179 | lodsd ;esp |
||
180 | lodsd ;ebx |
||
181 | mov [edi+14h], eax |
||
182 | lodsd ;edx |
||
183 | mov [edi+10h], eax |
||
184 | lodsd ;ecx |
||
185 | mov [edi+0Ch], eax |
||
186 | lodsd ;eax |
||
187 | mov [edi+8], eax |
||
188 | lodsd ;eip |
||
189 | mov [edi], eax |
||
190 | lodsd ;cs |
||
191 | lodsd ;eflags |
||
192 | mov [edi+4], eax |
||
193 | lodsd ;esp |
||
194 | mov [edi+18h], eax |
||
4893 | Serge | 195 | |
196 | dec bl |
||
197 | js .ret |
||
198 | dec bl |
||
199 | jns .ret |
||
200 | |||
201 | test bh, bh ; check swap flag |
||
202 | jz @F |
||
203 | |||
204 | ffree st0 ; swap context |
||
205 | @@: |
||
206 | |||
207 | add esi, 4 ;top of ring0 stack |
||
208 | ;fpu/sse context saved here |
||
209 | add edi, 40 |
||
210 | mov eax, 1 ;sse context |
||
211 | stosd |
||
212 | xor eax, eax ;reserved dword |
||
213 | stosd |
||
214 | |||
215 | mov ecx, 288/4 |
||
216 | rep movsd ;copy sse context |
||
217 | |||
2288 | clevermous | 218 | .ret: |
219 | sti |
||
220 | ret |
||
221 | |||
222 | debug_setcontext: |
||
223 | ; in: |
||
224 | ; ecx=pid |
||
225 | ; edx=sizeof(CONTEXT) |
||
226 | ; esi->CONTEXT |
||
227 | ; destroys eax,ecx,edx,esi,edi |
||
228 | cmp edx, 28h |
||
229 | jnz .ret |
||
8840 | rgimad | 230 | |
2288 | clevermous | 231 | call get_debuggee_slot |
232 | jc .stiret |
||
233 | ; mov esi, edx |
||
234 | mov eax, [eax*8+SLOT_BASE+APPDATA.pl0_stack] |
||
235 | lea edi, [eax+RING0_STACK_SIZE] |
||
236 | |||
237 | .ring0: |
||
238 | sub edi, 8+12+20h |
||
239 | mov eax, [esi+24h] ;edi |
||
240 | stosd |
||
241 | mov eax, [esi+20h] ;esi |
||
242 | stosd |
||
243 | mov eax, [esi+1Ch] ;ebp |
||
244 | stosd |
||
245 | scasd |
||
246 | mov eax, [esi+14h] ;ebx |
||
247 | stosd |
||
248 | mov eax, [esi+10h] ;edx |
||
249 | stosd |
||
250 | mov eax, [esi+0Ch] ;ecx |
||
251 | stosd |
||
252 | mov eax, [esi+8] ;eax |
||
253 | stosd |
||
254 | mov eax, [esi] ;eip |
||
255 | stosd |
||
256 | scasd |
||
257 | mov eax, [esi+4] ;eflags |
||
258 | stosd |
||
259 | mov eax, [esi+18h] ;esp |
||
260 | stosd |
||
261 | .stiret: |
||
262 | sti |
||
263 | .ret: |
||
264 | ret |
||
265 | |||
266 | debug_set_drx: |
||
267 | call get_debuggee_slot |
||
268 | jc .errret |
||
269 | mov ebp, eax |
||
270 | lea eax, [eax*8+SLOT_BASE+APPDATA.dbg_regs] |
||
271 | ; [eax]=dr0, [eax+4]=dr1, [eax+8]=dr2, [eax+C]=dr3 |
||
272 | ; [eax+10]=dr7 |
||
273 | cmp esi, OS_BASE |
||
274 | jae .errret |
||
275 | cmp dl, 3 |
||
276 | ja .errret |
||
277 | mov ecx, dr7 |
||
278 | ;fix me |
||
279 | xchg ecx, edx |
||
280 | shr edx, cl |
||
281 | shr edx, cl |
||
282 | xchg ecx, edx |
||
283 | |||
284 | test ecx, 2 ; bit 1+2*index = G0..G3, global break enable |
||
285 | jnz .errret2 |
||
286 | test dh, dh |
||
287 | jns .new |
||
288 | ; clear breakpoint |
||
289 | movzx edx, dl |
||
290 | add edx, edx |
||
291 | and dword [eax+edx*2], 0 ; clear DR |
||
292 | btr dword [eax+10h], edx ; clear L bit |
||
293 | test byte [eax+10h], 55h |
||
294 | jnz .okret |
||
295 | ; imul eax, ebp, tss_step/32 |
||
296 | ; and byte [eax + tss_data + TSS._trap], not 1 |
||
297 | and [ebp*8 + SLOT_BASE+APPDATA.dbg_state], not 1 |
||
298 | .okret: |
||
299 | and dword [esp+32], 0 |
||
300 | sti |
||
301 | ret |
||
302 | .errret: |
||
303 | sti |
||
304 | mov dword [esp+32], 1 |
||
305 | ret |
||
306 | .errret2: |
||
307 | sti |
||
308 | mov dword [esp+32], 2 |
||
309 | ret |
||
310 | .new: |
||
311 | ; add new breakpoint |
||
312 | ; dl=index; dh=flags; esi=address |
||
313 | test dh, 0xF0 |
||
314 | jnz .errret |
||
315 | mov cl, dh |
||
316 | and cl, 3 |
||
317 | cmp cl, 2 |
||
318 | jz .errret |
||
319 | mov cl, dh |
||
320 | shr cl, 2 |
||
321 | cmp cl, 2 |
||
322 | jz .errret |
||
323 | |||
324 | mov ebx, esi |
||
325 | test bl, dl |
||
326 | |||
327 | jnz .errret |
||
328 | or byte [eax+10h+1], 3 ; set GE and LE flags |
||
329 | |||
330 | movzx edx, dh |
||
331 | movzx ecx, dl |
||
332 | add ecx, ecx |
||
333 | bts dword [eax+10h], ecx ; set L flag |
||
334 | add ecx, ecx |
||
335 | mov [eax+ecx], ebx;esi ; set DR |
||
336 | shl edx, cl |
||
337 | mov ebx, 0xF |
||
338 | shl ebx, cl |
||
339 | not ebx |
||
340 | and [eax+10h+2], bx |
||
341 | or [eax+10h+2], dx ; set R/W and LEN fields |
||
342 | ; imul eax, ebp, tss_step/32 |
||
343 | ; or byte [eax + tss_data + TSS._trap], 1 |
||
344 | or [ebp*8 + SLOT_BASE+APPDATA.dbg_state], 1 |
||
345 | jmp .okret |
||
346 | |||
347 | debug_read_process_memory: |
||
348 | ; in: |
||
349 | ; ecx=pid |
||
350 | ; edx=length |
||
351 | ; edi->buffer in debugger |
||
352 | ; esi=address in debuggee |
||
353 | ; out: [esp+36]=sizeof(read) |
||
354 | ; destroys all |
||
355 | call get_debuggee_slot |
||
356 | jc .err |
||
357 | shr eax, 5 |
||
358 | mov ecx, edi |
||
359 | call read_process_memory |
||
360 | sti |
||
361 | mov dword [esp+32], eax |
||
362 | ret |
||
363 | .err: |
||
364 | or dword [esp+32], -1 |
||
365 | ret |
||
366 | |||
367 | debug_write_process_memory: |
||
368 | ; in: |
||
369 | ; ecx=pid |
||
370 | ; edx=length |
||
371 | ; edi->buffer in debugger |
||
372 | ; esi=address in debuggee |
||
373 | ; out: [esp+36]=sizeof(write) |
||
374 | ; destroys all |
||
375 | call get_debuggee_slot |
||
376 | jc debug_read_process_memory.err |
||
377 | shr eax, 5 |
||
378 | mov ecx, edi |
||
379 | call write_process_memory |
||
380 | sti |
||
381 | mov [esp+32], eax |
||
382 | ret |
||
383 | |||
384 | debugger_notify: |
||
385 | ; in: eax=debugger slot |
||
386 | ; ecx=size of debug message |
||
387 | ; [esp+4]..[esp+4+ecx]=message |
||
388 | ; interrupts must be disabled! |
||
389 | ; destroys all general registers |
||
390 | ; interrupts remain disabled |
||
391 | xchg ebp, eax |
||
392 | mov edi, [timer_ticks] |
||
393 | add edi, 500 ; 5 sec timeout |
||
394 | .1: |
||
395 | mov eax, ebp |
||
396 | shl eax, 8 |
||
397 | mov esi, [SLOT_BASE+eax+APPDATA.dbg_event_mem] |
||
398 | test esi, esi |
||
399 | jz .ret |
||
400 | ; read buffer header |
||
401 | push ecx |
||
402 | push eax |
||
403 | push eax |
||
404 | mov eax, ebp |
||
405 | mov ecx, esp |
||
406 | mov edx, 8 |
||
407 | call read_process_memory |
||
408 | cmp eax, edx |
||
409 | jz @f |
||
410 | add esp, 12 |
||
411 | jmp .ret |
||
412 | @@: |
||
413 | cmp dword [ecx], 0 |
||
414 | jg @f |
||
415 | .2: |
||
416 | pop ecx |
||
417 | pop ecx |
||
418 | pop ecx |
||
8869 | rgimad | 419 | cmp dword [current_slot_idx], 1 |
2288 | clevermous | 420 | jnz .notos |
421 | cmp [timer_ticks], edi |
||
422 | jae .ret |
||
423 | .notos: |
||
424 | sti |
||
425 | call change_task |
||
426 | cli |
||
427 | jmp .1 |
||
428 | @@: |
||
429 | mov edx, [ecx+8] |
||
430 | add edx, [ecx+4] |
||
431 | cmp edx, [ecx] |
||
432 | ja .2 |
||
433 | ; advance buffer position |
||
434 | push edx |
||
435 | mov edx, 4 |
||
436 | sub ecx, edx |
||
437 | mov eax, ebp |
||
438 | add esi, edx |
||
439 | call write_process_memory |
||
440 | pop eax |
||
441 | ; write message |
||
442 | mov eax, ebp |
||
443 | add esi, edx |
||
444 | add esi, [ecx+8] |
||
445 | add ecx, 20 |
||
446 | pop edx |
||
447 | pop edx |
||
448 | pop edx |
||
449 | call write_process_memory |
||
450 | ; new debug event |
||
451 | mov eax, ebp |
||
8876 | rgimad | 452 | shl eax, BSF sizeof.APPDATA |
453 | or [SLOT_BASE+eax+APPDATA.occurred_events], EVENT_DEBUG |
||
2288 | clevermous | 454 | .ret: |
455 | ret |