source: mainline/kernel/generic/src/udebug/udebug_ops.c@ 3a2f8aa

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3a2f8aa was 3698e44, checked in by Jiri Svoboda <jiri@…>, 16 years ago

Add ability to determine task name and load symbol table from the binary executable. Resolve symbol names in stack traces when dumping.

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