source: mainline/kernel/generic/src/udebug/udebug_ops.c@ 41df2827

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 41df2827 was 41df2827, checked in by Jakub Jermar <jakub@…>, 16 years ago

Fix missing includes.

  • Property mode set to 100644
File size: 13.3 KB
Line 
1/*
2 * Copyright (c) 2008 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup generic
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Udebug operations.
36 *
37 * Udebug operations on tasks and threads are implemented here. The
38 * functions defined here are called from the udebug_ipc module
39 * when servicing udebug IPC messages.
40 */
41
42#include <debug.h>
43#include <proc/task.h>
44#include <proc/thread.h>
45#include <arch.h>
46#include <errno.h>
47#include <print.h>
48#include <syscall/copy.h>
49#include <ipc/ipc.h>
50#include <udebug/udebug.h>
51#include <udebug/udebug_ops.h>
52#include <memstr.h>
53
54/**
55 * Prepare a thread for a debugging operation.
56 *
57 * Simply put, return thread t with t->udebug.lock held,
58 * but only if it verifies all conditions.
59 *
60 * Specifically, verifies that thread t exists, is a userspace thread,
61 * and belongs to the current task (TASK). Verifies, that the thread
62 * is (or is not) go according to being_go (typically false).
63 * It also locks t->udebug.lock, making sure that t->udebug.active
64 * is true - that the thread is in a valid debugging session.
65 *
66 * With this verified and the t->udebug.lock mutex held, it is ensured
67 * that the thread cannot leave the debugging session, let alone cease
68 * to exist.
69 *
70 * In this function, holding the TASK->udebug.lock mutex prevents the
71 * thread from leaving the debugging session, while relaxing from
72 * the t->lock spinlock to the t->udebug.lock mutex.
73 *
74 * @param t Pointer, need not at all be valid.
75 * @param being_go Required thread state.
76 *
77 * Returns EOK if all went well, or an error code otherwise.
78 */
79static int _thread_op_begin(thread_t *t, bool being_go)
80{
81 task_id_t taskid;
82 ipl_t ipl;
83
84 taskid = TASK->taskid;
85
86 mutex_lock(&TASK->udebug.lock);
87
88 /* thread_exists() must be called with threads_lock held */
89 ipl = interrupts_disable();
90 spinlock_lock(&threads_lock);
91
92 if (!thread_exists(t)) {
93 spinlock_unlock(&threads_lock);
94 interrupts_restore(ipl);
95 mutex_unlock(&TASK->udebug.lock);
96 return ENOENT;
97 }
98
99 /* t->lock is enough to ensure the thread's existence */
100 spinlock_lock(&t->lock);
101 spinlock_unlock(&threads_lock);
102
103 /* Verify that 't' is a userspace thread. */
104 if ((t->flags & THREAD_FLAG_USPACE) == 0) {
105 /* It's not, deny its existence */
106 spinlock_unlock(&t->lock);
107 interrupts_restore(ipl);
108 mutex_unlock(&TASK->udebug.lock);
109 return ENOENT;
110 }
111
112 /* Verify debugging state. */
113 if (t->udebug.active != true) {
114 /* Not in debugging session or undesired GO state */
115 spinlock_unlock(&t->lock);
116 interrupts_restore(ipl);
117 mutex_unlock(&TASK->udebug.lock);
118 return ENOENT;
119 }
120
121 /*
122 * Since the thread has active == true, TASK->udebug.lock
123 * is enough to ensure its existence and that active remains
124 * true.
125 */
126 spinlock_unlock(&t->lock);
127 interrupts_restore(ipl);
128
129 /* Only mutex TASK->udebug.lock left. */
130
131 /* Now verify that the thread belongs to the current task. */
132 if (t->task != TASK) {
133 /* No such thread belonging this task*/
134 mutex_unlock(&TASK->udebug.lock);
135 return ENOENT;
136 }
137
138 /*
139 * Now we need to grab the thread's debug lock for synchronization
140 * of the threads stoppability/stop state.
141 */
142 mutex_lock(&t->udebug.lock);
143
144 /* The big task mutex is no longer needed. */
145 mutex_unlock(&TASK->udebug.lock);
146
147 if (t->udebug.go != being_go) {
148 /* Not in debugging session or undesired GO state. */
149 mutex_unlock(&t->udebug.lock);
150 return EINVAL;
151 }
152
153 /* Only t->udebug.lock left. */
154
155 return EOK; /* All went well. */
156}
157
158/** End debugging operation on a thread. */
159static void _thread_op_end(thread_t *t)
160{
161 mutex_unlock(&t->udebug.lock);
162}
163
164/** Begin debugging the current task.
165 *
166 * Initiates a debugging session for the current task (and its threads).
167 * When the debugging session has started a reply will be sent to the
168 * UDEBUG_BEGIN call. This may happen immediately in this function if
169 * all the threads in this task are stoppable at the moment and in this
170 * case the function returns 1.
171 *
172 * Otherwise the function returns 0 and the reply will be sent as soon as
173 * all the threads become stoppable (i.e. they can be considered stopped).
174 *
175 * @param call The BEGIN call we are servicing.
176 * @return 0 (OK, but not done yet), 1 (done) or negative error code.
177 */
178int udebug_begin(call_t *call)
179{
180 int reply;
181
182 thread_t *t;
183 link_t *cur;
184
185 LOG("Debugging task %llu", TASK->taskid);
186 mutex_lock(&TASK->udebug.lock);
187
188 if (TASK->udebug.dt_state != UDEBUG_TS_INACTIVE) {
189 mutex_unlock(&TASK->udebug.lock);
190 return EBUSY;
191 }
192
193 TASK->udebug.dt_state = UDEBUG_TS_BEGINNING;
194 TASK->udebug.begin_call = call;
195 TASK->udebug.debugger = call->sender;
196
197 if (TASK->udebug.not_stoppable_count == 0) {
198 TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
199 TASK->udebug.begin_call = NULL;
200 reply = 1; /* immediate reply */
201 } else {
202 reply = 0; /* no reply */
203 }
204
205 /* Set udebug.active on all of the task's userspace threads. */
206
207 for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
208 t = list_get_instance(cur, thread_t, th_link);
209
210 mutex_lock(&t->udebug.lock);
211 if ((t->flags & THREAD_FLAG_USPACE) != 0)
212 t->udebug.active = true;
213 mutex_unlock(&t->udebug.lock);
214 }
215
216 mutex_unlock(&TASK->udebug.lock);
217 return reply;
218}
219
220/** Finish debugging the current task.
221 *
222 * Closes the debugging session for the current task.
223 * @return Zero on success or negative error code.
224 */
225int udebug_end(void)
226{
227 int rc;
228
229 LOG("Task %" PRIu64, TASK->taskid);
230
231 mutex_lock(&TASK->udebug.lock);
232 rc = udebug_task_cleanup(TASK);
233 mutex_unlock(&TASK->udebug.lock);
234
235 return rc;
236}
237
238/** Set the event mask.
239 *
240 * Sets the event mask that determines which events are enabled.
241 *
242 * @param mask Or combination of events that should be enabled.
243 * @return Zero on success or negative error code.
244 */
245int udebug_set_evmask(udebug_evmask_t mask)
246{
247 LOG("mask = 0x%x", mask);
248
249 mutex_lock(&TASK->udebug.lock);
250
251 if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
252 mutex_unlock(&TASK->udebug.lock);
253 return EINVAL;
254 }
255
256 TASK->udebug.evmask = mask;
257 mutex_unlock(&TASK->udebug.lock);
258
259 return 0;
260}
261
262/** Give thread GO.
263 *
264 * Upon recieving a go message, the thread is given GO. Being GO
265 * means the thread is allowed to execute userspace code (until
266 * a debugging event or STOP occurs, at which point the thread loses GO.
267 *
268 * @param t The thread to operate on (unlocked and need not be valid).
269 * @param call The GO call that we are servicing.
270 */
271int udebug_go(thread_t *t, call_t *call)
272{
273 int rc;
274
275 /* On success, this will lock t->udebug.lock. */
276 rc = _thread_op_begin(t, false);
277 if (rc != EOK) {
278 return rc;
279 }
280
281 t->udebug.go_call = call;
282 t->udebug.go = true;
283 t->udebug.cur_event = 0; /* none */
284
285 /*
286 * Neither t's lock nor threads_lock may be held during wakeup.
287 */
288 waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
289
290 _thread_op_end(t);
291
292 return 0;
293}
294
295/** Stop a thread (i.e. take its GO away)
296 *
297 * Generates a STOP event as soon as the thread becomes stoppable (i.e.
298 * can be considered stopped).
299 *
300 * @param t The thread to operate on (unlocked and need not be valid).
301 * @param call The GO call that we are servicing.
302 */
303int udebug_stop(thread_t *t, call_t *call)
304{
305 int rc;
306
307 LOG("udebug_stop()");
308
309 /*
310 * On success, this will lock t->udebug.lock. Note that this makes sure
311 * the thread is not stopped.
312 */
313 rc = _thread_op_begin(t, true);
314 if (rc != EOK) {
315 return rc;
316 }
317
318 /* Take GO away from the thread. */
319 t->udebug.go = false;
320
321 if (t->udebug.stoppable != true) {
322 /* Answer will be sent when the thread becomes stoppable. */
323 _thread_op_end(t);
324 return 0;
325 }
326
327 /*
328 * Answer GO call.
329 */
330
331 /* Make sure nobody takes this call away from us. */
332 call = t->udebug.go_call;
333 t->udebug.go_call = NULL;
334
335 IPC_SET_RETVAL(call->data, 0);
336 IPC_SET_ARG1(call->data, UDEBUG_EVENT_STOP);
337
338 THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
339
340 _thread_op_end(t);
341
342 mutex_lock(&TASK->udebug.lock);
343 ipc_answer(&TASK->answerbox, call);
344 mutex_unlock(&TASK->udebug.lock);
345
346 return 0;
347}
348
349/** Read the list of userspace threads in the current task.
350 *
351 * The list takes the form of a sequence of thread hashes (i.e. the pointers
352 * to thread structures). A buffer of size @a buf_size is allocated and
353 * a pointer to it written to @a buffer. The sequence of hashes is written
354 * into this buffer.
355 *
356 * If the sequence is longer than @a buf_size bytes, only as much hashes
357 * as can fit are copied. The number of thread hashes copied is stored
358 * in @a n.
359 *
360 * The rationale for having @a buf_size is that this function is only
361 * used for servicing the THREAD_READ message, which always specifies
362 * a maximum size for the userspace buffer.
363 *
364 * @param buffer The buffer for storing thread hashes.
365 * @param buf_size Buffer size in bytes.
366 * @param n The actual number of hashes copied will be stored here.
367 */
368int udebug_thread_read(void **buffer, size_t buf_size, size_t *n)
369{
370 thread_t *t;
371 link_t *cur;
372 unative_t tid;
373 unsigned copied_ids;
374 ipl_t ipl;
375 unative_t *id_buffer;
376 int flags;
377 size_t max_ids;
378
379 LOG("udebug_thread_read()");
380
381 /* Allocate a buffer to hold thread IDs */
382 id_buffer = malloc(buf_size, 0);
383
384 mutex_lock(&TASK->udebug.lock);
385
386 /* Verify task state */
387 if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
388 mutex_unlock(&TASK->udebug.lock);
389 return EINVAL;
390 }
391
392 ipl = interrupts_disable();
393 spinlock_lock(&TASK->lock);
394 /* Copy down the thread IDs */
395
396 max_ids = buf_size / sizeof(unative_t);
397 copied_ids = 0;
398
399 /* FIXME: make sure the thread isn't past debug shutdown... */
400 for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
401 /* Do not write past end of buffer */
402 if (copied_ids >= max_ids) break;
403
404 t = list_get_instance(cur, thread_t, th_link);
405
406 spinlock_lock(&t->lock);
407 flags = t->flags;
408 spinlock_unlock(&t->lock);
409
410 /* Not interested in kernel threads. */
411 if ((flags & THREAD_FLAG_USPACE) != 0) {
412 /* Using thread struct pointer as identification hash */
413 tid = (unative_t) t;
414 id_buffer[copied_ids++] = tid;
415 }
416 }
417
418 spinlock_unlock(&TASK->lock);
419 interrupts_restore(ipl);
420
421 mutex_unlock(&TASK->udebug.lock);
422
423 *buffer = id_buffer;
424 *n = copied_ids * sizeof(unative_t);
425
426 return 0;
427}
428
429/** Read the arguments of a system call.
430 *
431 * The arguments of the system call being being executed are copied
432 * to an allocated buffer and a pointer to it is written to @a buffer.
433 * The size of the buffer is exactly such that it can hold the maximum number
434 * of system-call arguments.
435 *
436 * Unless the thread is currently blocked in a SYSCALL_B or SYSCALL_E event,
437 * this function will fail with an EINVAL error code.
438 *
439 * @param buffer The buffer for storing thread hashes.
440 */
441int udebug_args_read(thread_t *t, void **buffer)
442{
443 int rc;
444 unative_t *arg_buffer;
445
446 /* Prepare a buffer to hold the arguments. */
447 arg_buffer = malloc(6 * sizeof(unative_t), 0);
448
449 /* On success, this will lock t->udebug.lock. */
450 rc = _thread_op_begin(t, false);
451 if (rc != EOK) {
452 return rc;
453 }
454
455 /* Additionally we need to verify that we are inside a syscall. */
456 if (t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_B &&
457 t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_E) {
458 _thread_op_end(t);
459 return EINVAL;
460 }
461
462 /* Copy to a local buffer before releasing the lock. */
463 memcpy(arg_buffer, t->udebug.syscall_args, 6 * sizeof(unative_t));
464
465 _thread_op_end(t);
466
467 *buffer = arg_buffer;
468 return 0;
469}
470
471/** Read the memory of the debugged task.
472 *
473 * Reads @a n bytes from the address space of the debugged task, starting
474 * from @a uspace_addr. The bytes are copied into an allocated buffer
475 * and a pointer to it is written into @a buffer.
476 *
477 * @param uspace_addr Address from where to start reading.
478 * @param n Number of bytes to read.
479 * @param buffer For storing a pointer to the allocated buffer.
480 */
481int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer)
482{
483 void *data_buffer;
484 int rc;
485
486 /* Verify task state */
487 mutex_lock(&TASK->udebug.lock);
488
489 if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
490 mutex_unlock(&TASK->udebug.lock);
491 return EBUSY;
492 }
493
494 data_buffer = malloc(n, 0);
495
496 /* NOTE: this is not strictly from a syscall... but that shouldn't
497 * be a problem */
498 rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n);
499 mutex_unlock(&TASK->udebug.lock);
500
501 if (rc != 0) return rc;
502
503 *buffer = data_buffer;
504 return 0;
505}
506
507/** @}
508 */
Note: See TracBrowser for help on using the repository browser.