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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 86bbca4 was 86bbca4, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Only return error code from udebug_begin().

Nobody should ever need the extra information.

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