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