Subversion Repositories Kolibri OS

Rev

Rev 44 | Rev 66 | Go to most recent revision | Details | Compare with Previous | 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
44 halyavin 239
; out: [esp+36]=sizeof(read)
40 halyavin 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
44 halyavin 247
	jnz	.err
40 halyavin 248
	call	get_debuggee_slot
44 halyavin 249
	jc	.err
250
	shr	eax, 5
40 halyavin 251
	mov	ebx, esi
252
	call	read_process_memory
253
	sti
44 halyavin 254
	mov	dword [esp+36], eax
40 halyavin 255
	ret
44 halyavin 256
.err:
257
	or	dword [esp+36], -1
258
	ret
40 halyavin 259
 
260
debug_write_process_memory:
261
; in:
262
; ebx=pid
263
; ecx=length
264
; esi->buffer in debugger
265
; edx=address in debuggee
44 halyavin 266
; out: [esp+36]=sizeof(write)
40 halyavin 267
; destroys all
268
	add	esi, std_application_base_address
269
	push	ebx
270
	mov	ebx, esi
271
	call	check_region
272
	pop	ebx
273
	dec	eax
44 halyavin 274
	jnz	debug_read_process_memory.err
40 halyavin 275
	call	get_debuggee_slot
44 halyavin 276
	jc	debug_read_process_memory.err
277
	shr	eax, 5
40 halyavin 278
	mov	ebx, esi
279
	call	write_process_memory
280
	sti
44 halyavin 281
	mov	[esp+36], eax
40 halyavin 282
	ret
283
 
284
debugger_notify:
285
; in: eax=debugger slot
286
;     ecx=size of debug message
287
;     [esp+4]..[esp+4+ecx]=message
288
; interrupts must be disabled!
289
; destroys all general registers
290
; interrupts remain disabled
291
	mov	ebp, eax
292
	shl	eax, 8
63 halyavin 293
	mov	edi, [timer_ticks]
294
	add	edi, 500	; 5 sec timeout
40 halyavin 295
.1:
296
	mov	edx, [0x80000+eax+0xBC]
297
	test	edx, edx
298
	jz	.ret
299
; read buffer header
300
	push	ecx
301
	push	eax
302
	push	eax
303
	mov	eax, ebp
304
	mov	ebx, esp
305
	mov	ecx, 8
306
	call	read_process_memory
307
	cmp	eax, ecx
308
	jz	@f
309
	add	esp, 12
310
	jmp	.ret
311
@@:
312
	cmp	dword [ebx], 0
313
	jg	@f
314
.2:
315
	pop	ecx
316
	pop	ecx
317
	pop	ecx
63 halyavin 318
	cmp	[timer_ticks], edi
319
	jae	.ret
40 halyavin 320
	sti
321
	call	change_task
322
	cli
323
	jmp	.1
324
@@:
325
	mov	ecx, [ebx+8]
326
	add	ecx, [ebx+4]
327
	cmp	ecx, [ebx]
328
	ja	.2
329
; advance buffer position
330
	push	ecx
331
	mov	ecx, 4
332
	sub	ebx, ecx
333
	mov	eax, ebp
334
	add	edx, ecx
335
	call	write_process_memory
336
	pop	eax
337
; write message
338
	mov	eax, ebp
339
	add	edx, ecx
340
	add	edx, [ebx+8]
341
	add	ebx, 20
342
	pop	ecx
343
	pop	ecx
344
	pop	ecx
345
	call	write_process_memory
346
; new debug event
347
	mov	eax, ebp
348
	shl	eax, 8
349
	or	byte [0x80000+eax+0xA8+1], 1	; set flag 100h
350
.ret:
351
	ret