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

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

Implement fault notifications and task monitoring service.

  • Property mode set to 100644
File size: 13.7 KB
RevLine 
[9a1b20c]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.
[7dc62af]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.
[9a1b20c]40 */
41
[0108984a]42#include <debug.h>
[9a1b20c]43#include <proc/task.h>
44#include <proc/thread.h>
45#include <arch.h>
46#include <errno.h>
[1902113]47#include <print.h>
[9a1b20c]48#include <syscall/copy.h>
49#include <ipc/ipc.h>
50#include <udebug/udebug.h>
51#include <udebug/udebug_ops.h>
[41df2827]52#include <memstr.h>
[9a1b20c]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
[1378b2b]62 * is (or is not) go according to being_go (typically false).
[8af9950]63 * It also locks t->udebug.lock, making sure that t->udebug.active
[9a1b20c]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 *
[7dc62af]74 * @param t Pointer, need not at all be valid.
[1378b2b]75 * @param being_go Required thread state.
[7dc62af]76 *
[9a1b20c]77 * Returns EOK if all went well, or an error code otherwise.
78 */
[1378b2b]79static int _thread_op_begin(thread_t *t, bool being_go)
[9a1b20c]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
[1378b2b]103 /* Verify that 't' is a userspace thread. */
[9a1b20c]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
[1378b2b]112 /* Verify debugging state. */
[8af9950]113 if (t->udebug.active != true) {
[9a1b20c]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 /*
[8af9950]122 * Since the thread has active == true, TASK->udebug.lock
123 * is enough to ensure its existence and that active remains
[9a1b20c]124 * true.
125 */
126 spinlock_unlock(&t->lock);
127 interrupts_restore(ipl);
128
[1378b2b]129 /* Only mutex TASK->udebug.lock left. */
[9a1b20c]130
[1378b2b]131 /* Now verify that the thread belongs to the current task. */
[9a1b20c]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
[1378b2b]144 /* The big task mutex is no longer needed. */
[9a1b20c]145 mutex_unlock(&TASK->udebug.lock);
146
[384c488]147 if (t->udebug.go != being_go) {
[1378b2b]148 /* Not in debugging session or undesired GO state. */
[9a1b20c]149 mutex_unlock(&t->udebug.lock);
150 return EINVAL;
151 }
152
[1378b2b]153 /* Only t->udebug.lock left. */
[9a1b20c]154
[1378b2b]155 return EOK; /* All went well. */
[9a1b20c]156}
157
[7dc62af]158/** End debugging operation on a thread. */
[9a1b20c]159static void _thread_op_end(thread_t *t)
160{
161 mutex_unlock(&t->udebug.lock);
162}
163
[7dc62af]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.
[9a1b20c]177 */
178int udebug_begin(call_t *call)
179{
180 int reply;
181
182 thread_t *t;
183 link_t *cur;
184
[ae5aa90]185 LOG("Debugging task %llu", TASK->taskid);
[9a1b20c]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
[8af9950]205 /* Set udebug.active on all of the task's userspace threads. */
[9a1b20c]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);
[a074b4f]211 if ((t->flags & THREAD_FLAG_USPACE) != 0) {
[8af9950]212 t->udebug.active = true;
[a074b4f]213 mutex_unlock(&t->udebug.lock);
214 condvar_broadcast(&t->udebug.active_cv);
215 } else {
216 mutex_unlock(&t->udebug.lock);
217 }
[9a1b20c]218 }
219
220 mutex_unlock(&TASK->udebug.lock);
221 return reply;
222}
223
[7dc62af]224/** Finish debugging the current task.
225 *
226 * Closes the debugging session for the current task.
227 * @return Zero on success or negative error code.
228 */
[9a1b20c]229int udebug_end(void)
230{
231 int rc;
232
[ae5aa90]233 LOG("Task %" PRIu64, TASK->taskid);
[9a1b20c]234
235 mutex_lock(&TASK->udebug.lock);
236 rc = udebug_task_cleanup(TASK);
237 mutex_unlock(&TASK->udebug.lock);
238
239 return rc;
240}
241
[7dc62af]242/** Set the event mask.
243 *
244 * Sets the event mask that determines which events are enabled.
245 *
246 * @param mask Or combination of events that should be enabled.
247 * @return Zero on success or negative error code.
248 */
[9a1b20c]249int udebug_set_evmask(udebug_evmask_t mask)
250{
[ae5aa90]251 LOG("mask = 0x%x", mask);
[9a1b20c]252
253 mutex_lock(&TASK->udebug.lock);
254
255 if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
256 mutex_unlock(&TASK->udebug.lock);
257 return EINVAL;
258 }
259
260 TASK->udebug.evmask = mask;
261 mutex_unlock(&TASK->udebug.lock);
262
263 return 0;
264}
265
[7dc62af]266/** Give thread GO.
267 *
[1378b2b]268 * Upon recieving a go message, the thread is given GO. Being GO
[7dc62af]269 * means the thread is allowed to execute userspace code (until
270 * a debugging event or STOP occurs, at which point the thread loses GO.
271 *
272 * @param t The thread to operate on (unlocked and need not be valid).
273 * @param call The GO call that we are servicing.
274 */
[9a1b20c]275int udebug_go(thread_t *t, call_t *call)
276{
277 int rc;
278
[1378b2b]279 /* On success, this will lock t->udebug.lock. */
[9a1b20c]280 rc = _thread_op_begin(t, false);
281 if (rc != EOK) {
282 return rc;
283 }
284
285 t->udebug.go_call = call;
[384c488]286 t->udebug.go = true;
[9a1b20c]287 t->udebug.cur_event = 0; /* none */
288
289 /*
[1378b2b]290 * Neither t's lock nor threads_lock may be held during wakeup.
[9a1b20c]291 */
292 waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
293
294 _thread_op_end(t);
295
296 return 0;
297}
298
[7dc62af]299/** Stop a thread (i.e. take its GO away)
300 *
301 * Generates a STOP event as soon as the thread becomes stoppable (i.e.
302 * can be considered stopped).
303 *
304 * @param t The thread to operate on (unlocked and need not be valid).
305 * @param call The GO call that we are servicing.
306 */
[9a1b20c]307int udebug_stop(thread_t *t, call_t *call)
308{
309 int rc;
310
[ae5aa90]311 LOG("udebug_stop()");
[9a1b20c]312
313 /*
314 * On success, this will lock t->udebug.lock. Note that this makes sure
315 * the thread is not stopped.
316 */
317 rc = _thread_op_begin(t, true);
318 if (rc != EOK) {
319 return rc;
320 }
321
[1378b2b]322 /* Take GO away from the thread. */
[384c488]323 t->udebug.go = false;
[9a1b20c]324
[384c488]325 if (t->udebug.stoppable != true) {
[1378b2b]326 /* Answer will be sent when the thread becomes stoppable. */
[9a1b20c]327 _thread_op_end(t);
328 return 0;
329 }
330
331 /*
[1378b2b]332 * Answer GO call.
[9a1b20c]333 */
334
[1378b2b]335 /* Make sure nobody takes this call away from us. */
[9a1b20c]336 call = t->udebug.go_call;
337 t->udebug.go_call = NULL;
338
339 IPC_SET_RETVAL(call->data, 0);
340 IPC_SET_ARG1(call->data, UDEBUG_EVENT_STOP);
341
342 THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
343
344 _thread_op_end(t);
345
[741fd16]346 mutex_lock(&TASK->udebug.lock);
[9a1b20c]347 ipc_answer(&TASK->answerbox, call);
348 mutex_unlock(&TASK->udebug.lock);
349
350 return 0;
351}
352
[7dc62af]353/** Read the list of userspace threads in the current task.
354 *
355 * The list takes the form of a sequence of thread hashes (i.e. the pointers
356 * to thread structures). A buffer of size @a buf_size is allocated and
357 * a pointer to it written to @a buffer. The sequence of hashes is written
358 * into this buffer.
359 *
360 * If the sequence is longer than @a buf_size bytes, only as much hashes
[336db295]361 * as can fit are copied. The number of bytes copied is stored in @a stored.
362 * The total number of thread bytes that could have been saved had there been
363 * enough space is stored in @a needed.
[7dc62af]364 *
365 * The rationale for having @a buf_size is that this function is only
366 * used for servicing the THREAD_READ message, which always specifies
367 * a maximum size for the userspace buffer.
368 *
369 * @param buffer The buffer for storing thread hashes.
370 * @param buf_size Buffer size in bytes.
[336db295]371 * @param stored The actual number of bytes copied will be stored here.
372 * @param needed Total number of hashes that could have been saved.
[7dc62af]373 */
[336db295]374int udebug_thread_read(void **buffer, size_t buf_size, size_t *stored,
375 size_t *needed)
[9a1b20c]376{
377 thread_t *t;
378 link_t *cur;
379 unative_t tid;
[336db295]380 size_t copied_ids;
381 size_t extra_ids;
[9a1b20c]382 ipl_t ipl;
383 unative_t *id_buffer;
384 int flags;
385 size_t max_ids;
386
[ae5aa90]387 LOG("udebug_thread_read()");
[9a1b20c]388
389 /* Allocate a buffer to hold thread IDs */
[336db295]390 id_buffer = malloc(buf_size + 1, 0);
[9a1b20c]391
392 mutex_lock(&TASK->udebug.lock);
393
394 /* Verify task state */
395 if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
396 mutex_unlock(&TASK->udebug.lock);
397 return EINVAL;
398 }
399
400 ipl = interrupts_disable();
401 spinlock_lock(&TASK->lock);
402 /* Copy down the thread IDs */
403
404 max_ids = buf_size / sizeof(unative_t);
405 copied_ids = 0;
[336db295]406 extra_ids = 0;
[9a1b20c]407
408 /* FIXME: make sure the thread isn't past debug shutdown... */
409 for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
410 t = list_get_instance(cur, thread_t, th_link);
411
412 spinlock_lock(&t->lock);
413 flags = t->flags;
414 spinlock_unlock(&t->lock);
415
[1378b2b]416 /* Not interested in kernel threads. */
[336db295]417 if ((flags & THREAD_FLAG_USPACE) == 0)
418 continue;
419
420 if (copied_ids < max_ids) {
[9a1b20c]421 /* Using thread struct pointer as identification hash */
422 tid = (unative_t) t;
423 id_buffer[copied_ids++] = tid;
[336db295]424 } else {
425 extra_ids++;
[9a1b20c]426 }
427 }
428
429 spinlock_unlock(&TASK->lock);
430 interrupts_restore(ipl);
431
432 mutex_unlock(&TASK->udebug.lock);
433
434 *buffer = id_buffer;
[336db295]435 *stored = copied_ids * sizeof(unative_t);
436 *needed = (copied_ids + extra_ids) * sizeof(unative_t);
[9a1b20c]437
438 return 0;
439}
440
[7dc62af]441/** Read the arguments of a system call.
442 *
443 * The arguments of the system call being being executed are copied
444 * to an allocated buffer and a pointer to it is written to @a buffer.
445 * The size of the buffer is exactly such that it can hold the maximum number
446 * of system-call arguments.
447 *
448 * Unless the thread is currently blocked in a SYSCALL_B or SYSCALL_E event,
449 * this function will fail with an EINVAL error code.
450 *
451 * @param buffer The buffer for storing thread hashes.
452 */
[9a1b20c]453int udebug_args_read(thread_t *t, void **buffer)
454{
455 int rc;
456 unative_t *arg_buffer;
457
[1378b2b]458 /* Prepare a buffer to hold the arguments. */
[9a1b20c]459 arg_buffer = malloc(6 * sizeof(unative_t), 0);
460
[1378b2b]461 /* On success, this will lock t->udebug.lock. */
[9a1b20c]462 rc = _thread_op_begin(t, false);
463 if (rc != EOK) {
464 return rc;
465 }
466
[1378b2b]467 /* Additionally we need to verify that we are inside a syscall. */
[9a1b20c]468 if (t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_B &&
469 t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_E) {
470 _thread_op_end(t);
471 return EINVAL;
472 }
473
[1378b2b]474 /* Copy to a local buffer before releasing the lock. */
[9a1b20c]475 memcpy(arg_buffer, t->udebug.syscall_args, 6 * sizeof(unative_t));
476
477 _thread_op_end(t);
478
479 *buffer = arg_buffer;
480 return 0;
481}
482
[7dc62af]483/** Read the memory of the debugged task.
484 *
485 * Reads @a n bytes from the address space of the debugged task, starting
486 * from @a uspace_addr. The bytes are copied into an allocated buffer
487 * and a pointer to it is written into @a buffer.
488 *
489 * @param uspace_addr Address from where to start reading.
490 * @param n Number of bytes to read.
491 * @param buffer For storing a pointer to the allocated buffer.
492 */
[9a1b20c]493int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer)
494{
495 void *data_buffer;
496 int rc;
497
498 /* Verify task state */
499 mutex_lock(&TASK->udebug.lock);
500
501 if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
502 mutex_unlock(&TASK->udebug.lock);
503 return EBUSY;
504 }
505
506 data_buffer = malloc(n, 0);
507
508 /* NOTE: this is not strictly from a syscall... but that shouldn't
509 * be a problem */
510 rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n);
511 mutex_unlock(&TASK->udebug.lock);
512
513 if (rc != 0) return rc;
514
515 *buffer = data_buffer;
516 return 0;
517}
518
519/** @}
520 */
Note: See TracBrowser for help on using the repository browser.