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

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

memxxx functions should be provided in the kernel via the same header as in userspace (mem.h).

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