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