source: mainline/kernel/generic/src/udebug/udebug_ops.c@ 55821eea

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 55821eea was 19f857a, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Rename string.h to str.h to avoid header conflict with standard C string.h.

  • Property mode set to 100644
File size: 15.5 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 <str.h>
49#include <syscall/copy.h>
50#include <ipc/ipc.h>
51#include <udebug/udebug.h>
52#include <udebug/udebug_ops.h>
53#include <memstr.h>
54
55/**
56 * Prepare a thread for a debugging operation.
57 *
58 * Simply put, return thread t with t->udebug.lock held,
59 * but only if it verifies all conditions.
60 *
61 * Specifically, verifies that thread t exists, is a userspace thread,
62 * and belongs to the current task (TASK). Verifies, that the thread
63 * is (or is not) go according to being_go (typically false).
64 * It also locks t->udebug.lock, making sure that t->udebug.active
65 * is true - that the thread is in a valid debugging session.
66 *
67 * With this verified and the t->udebug.lock mutex held, it is ensured
68 * that the thread cannot leave the debugging session, let alone cease
69 * to exist.
70 *
71 * In this function, holding the TASK->udebug.lock mutex prevents the
72 * thread from leaving the debugging session, while relaxing from
73 * the t->lock spinlock to the t->udebug.lock mutex.
74 *
75 * @param t Pointer, need not at all be valid.
76 * @param being_go Required thread state.
77 *
78 * Returns EOK if all went well, or an error code otherwise.
79 */
80static int _thread_op_begin(thread_t *t, bool being_go)
81{
82 ipl_t ipl;
83
84 mutex_lock(&TASK->udebug.lock);
85
86 /* thread_exists() must be called with threads_lock held */
87 ipl = interrupts_disable();
88 spinlock_lock(&threads_lock);
89
90 if (!thread_exists(t)) {
91 spinlock_unlock(&threads_lock);
92 interrupts_restore(ipl);
93 mutex_unlock(&TASK->udebug.lock);
94 return ENOENT;
95 }
96
97 /* t->lock is enough to ensure the thread's existence */
98 spinlock_lock(&t->lock);
99 spinlock_unlock(&threads_lock);
100
101 /* Verify that 't' is a userspace thread. */
102 if ((t->flags & THREAD_FLAG_USPACE) == 0) {
103 /* It's not, deny its existence */
104 spinlock_unlock(&t->lock);
105 interrupts_restore(ipl);
106 mutex_unlock(&TASK->udebug.lock);
107 return ENOENT;
108 }
109
110 /* Verify debugging state. */
111 if (t->udebug.active != true) {
112 /* Not in debugging session or undesired GO state */
113 spinlock_unlock(&t->lock);
114 interrupts_restore(ipl);
115 mutex_unlock(&TASK->udebug.lock);
116 return ENOENT;
117 }
118
119 /*
120 * Since the thread has active == true, TASK->udebug.lock
121 * is enough to ensure its existence and that active remains
122 * true.
123 */
124 spinlock_unlock(&t->lock);
125 interrupts_restore(ipl);
126
127 /* Only mutex TASK->udebug.lock left. */
128
129 /* Now verify that the thread belongs to the current task. */
130 if (t->task != TASK) {
131 /* No such thread belonging this task*/
132 mutex_unlock(&TASK->udebug.lock);
133 return ENOENT;
134 }
135
136 /*
137 * Now we need to grab the thread's debug lock for synchronization
138 * of the threads stoppability/stop state.
139 */
140 mutex_lock(&t->udebug.lock);
141
142 /* The big task mutex is no longer needed. */
143 mutex_unlock(&TASK->udebug.lock);
144
145 if (t->udebug.go != being_go) {
146 /* Not in debugging session or undesired GO state. */
147 mutex_unlock(&t->udebug.lock);
148 return EINVAL;
149 }
150
151 /* Only t->udebug.lock left. */
152
153 return EOK; /* All went well. */
154}
155
156/** End debugging operation on a thread. */
157static void _thread_op_end(thread_t *t)
158{
159 mutex_unlock(&t->udebug.lock);
160}
161
162/** Begin debugging the current task.
163 *
164 * Initiates a debugging session for the current task (and its threads).
165 * When the debugging session has started a reply will be sent to the
166 * UDEBUG_BEGIN call. This may happen immediately in this function if
167 * all the threads in this task are stoppable at the moment and in this
168 * case the function returns 1.
169 *
170 * Otherwise the function returns 0 and the reply will be sent as soon as
171 * all the threads become stoppable (i.e. they can be considered stopped).
172 *
173 * @param call The BEGIN call we are servicing.
174 * @return 0 (OK, but not done yet), 1 (done) or negative error code.
175 */
176int udebug_begin(call_t *call)
177{
178 int reply;
179
180 thread_t *t;
181 link_t *cur;
182
183 LOG("Debugging task %llu", TASK->taskid);
184 mutex_lock(&TASK->udebug.lock);
185
186 if (TASK->udebug.dt_state != UDEBUG_TS_INACTIVE) {
187 mutex_unlock(&TASK->udebug.lock);
188 return EBUSY;
189 }
190
191 TASK->udebug.dt_state = UDEBUG_TS_BEGINNING;
192 TASK->udebug.begin_call = call;
193 TASK->udebug.debugger = call->sender;
194
195 if (TASK->udebug.not_stoppable_count == 0) {
196 TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
197 TASK->udebug.begin_call = NULL;
198 reply = 1; /* immediate reply */
199 } else {
200 reply = 0; /* no reply */
201 }
202
203 /* Set udebug.active on all of the task's userspace threads. */
204
205 for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
206 t = list_get_instance(cur, thread_t, th_link);
207
208 mutex_lock(&t->udebug.lock);
209 if ((t->flags & THREAD_FLAG_USPACE) != 0) {
210 t->udebug.active = true;
211 mutex_unlock(&t->udebug.lock);
212 condvar_broadcast(&t->udebug.active_cv);
213 } else {
214 mutex_unlock(&t->udebug.lock);
215 }
216 }
217
218 mutex_unlock(&TASK->udebug.lock);
219 return reply;
220}
221
222/** Finish debugging the current task.
223 *
224 * Closes the debugging session for the current task.
225 * @return Zero on success or negative error code.
226 */
227int udebug_end(void)
228{
229 int rc;
230
231 LOG("Task %" PRIu64, TASK->taskid);
232
233 mutex_lock(&TASK->udebug.lock);
234 rc = udebug_task_cleanup(TASK);
235 mutex_unlock(&TASK->udebug.lock);
236
237 return rc;
238}
239
240/** Set the event mask.
241 *
242 * Sets the event mask that determines which events are enabled.
243 *
244 * @param mask Or combination of events that should be enabled.
245 * @return Zero on success or negative error code.
246 */
247int udebug_set_evmask(udebug_evmask_t mask)
248{
249 LOG("mask = 0x%x", mask);
250
251 mutex_lock(&TASK->udebug.lock);
252
253 if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
254 mutex_unlock(&TASK->udebug.lock);
255 return EINVAL;
256 }
257
258 TASK->udebug.evmask = mask;
259 mutex_unlock(&TASK->udebug.lock);
260
261 return 0;
262}
263
264/** Give thread GO.
265 *
266 * Upon recieving a go message, the thread is given GO. Being GO
267 * means the thread is allowed to execute userspace code (until
268 * a debugging event or STOP occurs, at which point the thread loses GO.
269 *
270 * @param t The thread to operate on (unlocked and need not be valid).
271 * @param call The GO call that we are servicing.
272 */
273int udebug_go(thread_t *t, call_t *call)
274{
275 int rc;
276
277 /* On success, this will lock t->udebug.lock. */
278 rc = _thread_op_begin(t, false);
279 if (rc != EOK) {
280 return rc;
281 }
282
283 t->udebug.go_call = call;
284 t->udebug.go = true;
285 t->udebug.cur_event = 0; /* none */
286
287 /*
288 * Neither t's lock nor threads_lock may be held during wakeup.
289 */
290 waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
291
292 _thread_op_end(t);
293
294 return 0;
295}
296
297/** Stop a thread (i.e. take its GO away)
298 *
299 * Generates a STOP event as soon as the thread becomes stoppable (i.e.
300 * can be considered stopped).
301 *
302 * @param t The thread to operate on (unlocked and need not be valid).
303 * @param call The GO call that we are servicing.
304 */
305int udebug_stop(thread_t *t, call_t *call)
306{
307 int rc;
308
309 LOG("udebug_stop()");
310
311 /*
312 * On success, this will lock t->udebug.lock. Note that this makes sure
313 * the thread is not stopped.
314 */
315 rc = _thread_op_begin(t, true);
316 if (rc != EOK) {
317 return rc;
318 }
319
320 /* Take GO away from the thread. */
321 t->udebug.go = false;
322
323 if (t->udebug.stoppable != true) {
324 /* Answer will be sent when the thread becomes stoppable. */
325 _thread_op_end(t);
326 return 0;
327 }
328
329 /*
330 * Answer GO call.
331 */
332
333 /* Make sure nobody takes this call away from us. */
334 call = t->udebug.go_call;
335 t->udebug.go_call = NULL;
336
337 IPC_SET_RETVAL(call->data, 0);
338 IPC_SET_ARG1(call->data, UDEBUG_EVENT_STOP);
339
340 THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
341
342 _thread_op_end(t);
343
344 mutex_lock(&TASK->udebug.lock);
345 ipc_answer(&TASK->answerbox, call);
346 mutex_unlock(&TASK->udebug.lock);
347
348 return 0;
349}
350
351/** Read the list of userspace threads in the current task.
352 *
353 * The list takes the form of a sequence of thread hashes (i.e. the pointers
354 * to thread structures). A buffer of size @a buf_size is allocated and
355 * a pointer to it written to @a buffer. The sequence of hashes is written
356 * into this buffer.
357 *
358 * If the sequence is longer than @a buf_size bytes, only as much hashes
359 * as can fit are copied. The number of bytes copied is stored in @a stored.
360 * The total number of thread bytes that could have been saved had there been
361 * enough space is stored in @a needed.
362 *
363 * The rationale for having @a buf_size is that this function is only
364 * used for servicing the THREAD_READ message, which always specifies
365 * a maximum size for the userspace buffer.
366 *
367 * @param buffer The buffer for storing thread hashes.
368 * @param buf_size Buffer size in bytes.
369 * @param stored The actual number of bytes copied will be stored here.
370 * @param needed Total number of hashes that could have been saved.
371 */
372int udebug_thread_read(void **buffer, size_t buf_size, size_t *stored,
373 size_t *needed)
374{
375 thread_t *t;
376 link_t *cur;
377 unative_t tid;
378 size_t copied_ids;
379 size_t extra_ids;
380 ipl_t ipl;
381 unative_t *id_buffer;
382 int flags;
383 size_t max_ids;
384
385 LOG("udebug_thread_read()");
386
387 /* Allocate a buffer to hold thread IDs */
388 id_buffer = malloc(buf_size + 1, 0);
389
390 mutex_lock(&TASK->udebug.lock);
391
392 /* Verify task state */
393 if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
394 mutex_unlock(&TASK->udebug.lock);
395 return EINVAL;
396 }
397
398 ipl = interrupts_disable();
399 spinlock_lock(&TASK->lock);
400 /* Copy down the thread IDs */
401
402 max_ids = buf_size / sizeof(unative_t);
403 copied_ids = 0;
404 extra_ids = 0;
405
406 /* FIXME: make sure the thread isn't past debug shutdown... */
407 for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
408 t = list_get_instance(cur, thread_t, th_link);
409
410 spinlock_lock(&t->lock);
411 flags = t->flags;
412 spinlock_unlock(&t->lock);
413
414 /* Not interested in kernel threads. */
415 if ((flags & THREAD_FLAG_USPACE) == 0)
416 continue;
417
418 if (copied_ids < max_ids) {
419 /* Using thread struct pointer as identification hash */
420 tid = (unative_t) t;
421 id_buffer[copied_ids++] = tid;
422 } else {
423 extra_ids++;
424 }
425 }
426
427 spinlock_unlock(&TASK->lock);
428 interrupts_restore(ipl);
429
430 mutex_unlock(&TASK->udebug.lock);
431
432 *buffer = id_buffer;
433 *stored = copied_ids * sizeof(unative_t);
434 *needed = (copied_ids + extra_ids) * sizeof(unative_t);
435
436 return 0;
437}
438
439/** Read task name.
440 *
441 * Returns task name as non-terminated string in a newly allocated buffer.
442 * Also returns the size of the data.
443 *
444 * @param data Place to store pointer to newly allocated block.
445 * @param data_size Place to store size of the data.
446 *
447 * @returns EOK.
448 */
449int udebug_name_read(char **data, size_t *data_size)
450{
451 size_t name_size;
452
453 name_size = str_size(TASK->name) + 1;
454 *data = malloc(name_size, 0);
455 *data_size = name_size;
456
457 memcpy(*data, TASK->name, name_size);
458
459 return 0;
460}
461
462/** Read the arguments of a system call.
463 *
464 * The arguments of the system call being being executed are copied
465 * to an allocated buffer and a pointer to it is written to @a buffer.
466 * The size of the buffer is exactly such that it can hold the maximum number
467 * of system-call arguments.
468 *
469 * Unless the thread is currently blocked in a SYSCALL_B or SYSCALL_E event,
470 * this function will fail with an EINVAL error code.
471 *
472 * @param t Thread where call arguments are to be read.
473 * @param buffer Place to store pointer to new buffer.
474 * @return EOK on success, ENOENT if @a t is invalid, EINVAL
475 * if thread state is not valid for this operation.
476 */
477int udebug_args_read(thread_t *t, void **buffer)
478{
479 int rc;
480 unative_t *arg_buffer;
481
482 /* Prepare a buffer to hold the arguments. */
483 arg_buffer = malloc(6 * sizeof(unative_t), 0);
484
485 /* On success, this will lock t->udebug.lock. */
486 rc = _thread_op_begin(t, false);
487 if (rc != EOK) {
488 return rc;
489 }
490
491 /* Additionally we need to verify that we are inside a syscall. */
492 if (t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_B &&
493 t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_E) {
494 _thread_op_end(t);
495 return EINVAL;
496 }
497
498 /* Copy to a local buffer before releasing the lock. */
499 memcpy(arg_buffer, t->udebug.syscall_args, 6 * sizeof(unative_t));
500
501 _thread_op_end(t);
502
503 *buffer = arg_buffer;
504 return 0;
505}
506
507/** Read the register state of the thread.
508 *
509 * The contents of the thread's istate structure are copied to a newly
510 * allocated buffer and a pointer to it is written to @a buffer. The size of
511 * the buffer will be sizeof(istate_t).
512 *
513 * Currently register state cannot be read if the thread is inside a system
514 * call (as opposed to an exception). This is an implementation limit.
515 *
516 * @param t Thread whose state is to be read.
517 * @param buffer Place to store pointer to new buffer.
518 * @return EOK on success, ENOENT if @a t is invalid, EINVAL
519 * if thread is not in valid state, EBUSY if istate
520 * is not available.
521 */
522int udebug_regs_read(thread_t *t, void **buffer)
523{
524 istate_t *state, *state_buf;
525 int rc;
526
527 /* Prepare a buffer to hold the data. */
528 state_buf = malloc(sizeof(istate_t), 0);
529
530 /* On success, this will lock t->udebug.lock */
531 rc = _thread_op_begin(t, false);
532 if (rc != EOK) {
533 return rc;
534 }
535
536 state = t->udebug.uspace_state;
537 if (state == NULL) {
538 _thread_op_end(t);
539 return EBUSY;
540 }
541
542 /* Copy to the allocated buffer */
543 memcpy(state_buf, state, sizeof(istate_t));
544
545 _thread_op_end(t);
546
547 *buffer = (void *) state_buf;
548 return 0;
549}
550
551/** Read the memory of the debugged task.
552 *
553 * Reads @a n bytes from the address space of the debugged task, starting
554 * from @a uspace_addr. The bytes are copied into an allocated buffer
555 * and a pointer to it is written into @a buffer.
556 *
557 * @param uspace_addr Address from where to start reading.
558 * @param n Number of bytes to read.
559 * @param buffer For storing a pointer to the allocated buffer.
560 */
561int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer)
562{
563 void *data_buffer;
564 int rc;
565
566 /* Verify task state */
567 mutex_lock(&TASK->udebug.lock);
568
569 if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
570 mutex_unlock(&TASK->udebug.lock);
571 return EBUSY;
572 }
573
574 data_buffer = malloc(n, 0);
575
576 /* NOTE: this is not strictly from a syscall... but that shouldn't
577 * be a problem */
578 rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n);
579 mutex_unlock(&TASK->udebug.lock);
580
581 if (rc != 0) return rc;
582
583 *buffer = data_buffer;
584 return 0;
585}
586
587/** @}
588 */
Note: See TracBrowser for help on using the repository browser.