Rev 3684 | Rev 4393 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2894 | svoboda | 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 | |||
| 2887 | svoboda | 29 | /** @addtogroup generic |
| 30 | * @{ |
||
| 31 | */ |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @file |
||
| 2894 | svoboda | 35 | * @brief Udebug operations. |
| 3471 | svoboda | 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. |
||
| 2887 | svoboda | 40 | */ |
| 41 | |||
| 3471 | svoboda | 42 | #include <debug.h> |
| 2887 | svoboda | 43 | #include <proc/task.h> |
| 44 | #include <proc/thread.h> |
||
| 45 | #include <arch.h> |
||
| 46 | #include <errno.h> |
||
| 4377 | svoboda | 47 | #include <print.h> |
| 2887 | svoboda | 48 | #include <syscall/copy.h> |
| 49 | #include <ipc/ipc.h> |
||
| 50 | #include <udebug/udebug.h> |
||
| 51 | #include <udebug/udebug_ops.h> |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Prepare a thread for a debugging operation. |
||
| 55 | * |
||
| 3018 | svoboda | 56 | * Simply put, return thread t with t->udebug.lock held, |
| 2887 | svoboda | 57 | * but only if it verifies all conditions. |
| 58 | * |
||
| 59 | * Specifically, verifies that thread t exists, is a userspace thread, |
||
| 2898 | svoboda | 60 | * and belongs to the current task (TASK). Verifies, that the thread |
| 3606 | svoboda | 61 | * is (or is not) go according to being_go (typically false). |
| 3684 | svoboda | 62 | * It also locks t->udebug.lock, making sure that t->udebug.active |
| 3026 | svoboda | 63 | * is true - that the thread is in a valid debugging session. |
| 2887 | svoboda | 64 | * |
| 3026 | svoboda | 65 | * With this verified and the t->udebug.lock mutex held, it is ensured |
| 66 | * that the thread cannot leave the debugging session, let alone cease |
||
| 67 | * to exist. |
||
| 68 | * |
||
| 69 | * In this function, holding the TASK->udebug.lock mutex prevents the |
||
| 70 | * thread from leaving the debugging session, while relaxing from |
||
| 71 | * the t->lock spinlock to the t->udebug.lock mutex. |
||
| 72 | * |
||
| 3471 | svoboda | 73 | * @param t Pointer, need not at all be valid. |
| 3606 | svoboda | 74 | * @param being_go Required thread state. |
| 3471 | svoboda | 75 | * |
| 2887 | svoboda | 76 | * Returns EOK if all went well, or an error code otherwise. |
| 77 | */ |
||
| 3606 | svoboda | 78 | static int _thread_op_begin(thread_t *t, bool being_go) |
| 2887 | svoboda | 79 | { |
| 80 | task_id_t taskid; |
||
| 3026 | svoboda | 81 | ipl_t ipl; |
| 2887 | svoboda | 82 | |
| 83 | taskid = TASK->taskid; |
||
| 84 | |||
| 3026 | svoboda | 85 | mutex_lock(&TASK->udebug.lock); |
| 86 | |||
| 87 | /* thread_exists() must be called with threads_lock held */ |
||
| 88 | ipl = interrupts_disable(); |
||
| 2887 | svoboda | 89 | spinlock_lock(&threads_lock); |
| 90 | |||
| 91 | if (!thread_exists(t)) { |
||
| 92 | spinlock_unlock(&threads_lock); |
||
| 3026 | svoboda | 93 | interrupts_restore(ipl); |
| 94 | mutex_unlock(&TASK->udebug.lock); |
||
| 2887 | svoboda | 95 | return ENOENT; |
| 96 | } |
||
| 97 | |||
| 3026 | svoboda | 98 | /* t->lock is enough to ensure the thread's existence */ |
| 2887 | svoboda | 99 | spinlock_lock(&t->lock); |
| 3026 | svoboda | 100 | spinlock_unlock(&threads_lock); |
| 2887 | svoboda | 101 | |
| 3606 | svoboda | 102 | /* Verify that 't' is a userspace thread. */ |
| 2887 | svoboda | 103 | if ((t->flags & THREAD_FLAG_USPACE) == 0) { |
| 104 | /* It's not, deny its existence */ |
||
| 3026 | svoboda | 105 | spinlock_unlock(&t->lock); |
| 106 | interrupts_restore(ipl); |
||
| 107 | mutex_unlock(&TASK->udebug.lock); |
||
| 108 | return ENOENT; |
||
| 2887 | svoboda | 109 | } |
| 110 | |||
| 3606 | svoboda | 111 | /* Verify debugging state. */ |
| 3684 | svoboda | 112 | if (t->udebug.active != true) { |
| 2898 | svoboda | 113 | /* Not in debugging session or undesired GO state */ |
| 3026 | svoboda | 114 | spinlock_unlock(&t->lock); |
| 115 | interrupts_restore(ipl); |
||
| 116 | mutex_unlock(&TASK->udebug.lock); |
||
| 117 | return ENOENT; |
||
| 2887 | svoboda | 118 | } |
| 119 | |||
| 3026 | svoboda | 120 | /* |
| 3684 | svoboda | 121 | * Since the thread has active == true, TASK->udebug.lock |
| 122 | * is enough to ensure its existence and that active remains |
||
| 3026 | svoboda | 123 | * true. |
| 124 | */ |
||
| 2887 | svoboda | 125 | spinlock_unlock(&t->lock); |
| 3026 | svoboda | 126 | interrupts_restore(ipl); |
| 2887 | svoboda | 127 | |
| 3606 | svoboda | 128 | /* Only mutex TASK->udebug.lock left. */ |
| 3026 | svoboda | 129 | |
| 3606 | svoboda | 130 | /* Now verify that the thread belongs to the current task. */ |
| 3026 | svoboda | 131 | if (t->task != TASK) { |
| 132 | /* No such thread belonging this task*/ |
||
| 133 | mutex_unlock(&TASK->udebug.lock); |
||
| 134 | return ENOENT; |
||
| 135 | } |
||
| 2887 | svoboda | 136 | |
| 3026 | svoboda | 137 | /* |
| 138 | * Now we need to grab the thread's debug lock for synchronization |
||
| 139 | * of the threads stoppability/stop state. |
||
| 140 | */ |
||
| 141 | mutex_lock(&t->udebug.lock); |
||
| 2887 | svoboda | 142 | |
| 3606 | svoboda | 143 | /* The big task mutex is no longer needed. */ |
| 3026 | svoboda | 144 | mutex_unlock(&TASK->udebug.lock); |
| 2887 | svoboda | 145 | |
| 3606 | svoboda | 146 | if (t->udebug.go != being_go) { |
| 147 | /* Not in debugging session or undesired GO state. */ |
||
| 3026 | svoboda | 148 | mutex_unlock(&t->udebug.lock); |
| 149 | return EINVAL; |
||
| 150 | } |
||
| 2887 | svoboda | 151 | |
| 3606 | svoboda | 152 | /* Only t->udebug.lock left. */ |
| 3026 | svoboda | 153 | |
| 3606 | svoboda | 154 | return EOK; /* All went well. */ |
| 2887 | svoboda | 155 | } |
| 156 | |||
| 3471 | svoboda | 157 | /** End debugging operation on a thread. */ |
| 2887 | svoboda | 158 | static void _thread_op_end(thread_t *t) |
| 159 | { |
||
| 3026 | svoboda | 160 | mutex_unlock(&t->udebug.lock); |
| 2887 | svoboda | 161 | } |
| 162 | |||
| 3471 | svoboda | 163 | /** Begin debugging the current task. |
| 164 | * |
||
| 165 | * Initiates a debugging session for the current task (and its threads). |
||
| 166 | * When the debugging session has started a reply will be sent to the |
||
| 167 | * UDEBUG_BEGIN call. This may happen immediately in this function if |
||
| 168 | * all the threads in this task are stoppable at the moment and in this |
||
| 169 | * case the function returns 1. |
||
| 170 | * |
||
| 171 | * Otherwise the function returns 0 and the reply will be sent as soon as |
||
| 172 | * all the threads become stoppable (i.e. they can be considered stopped). |
||
| 173 | * |
||
| 174 | * @param call The BEGIN call we are servicing. |
||
| 175 | * @return 0 (OK, but not done yet), 1 (done) or negative error code. |
||
| 2887 | svoboda | 176 | */ |
| 177 | int udebug_begin(call_t *call) |
||
| 178 | { |
||
| 179 | int reply; |
||
| 180 | |||
| 181 | thread_t *t; |
||
| 182 | link_t *cur; |
||
| 183 | |||
| 3471 | svoboda | 184 | LOG("udebug_begin()\n"); |
| 2887 | svoboda | 185 | |
| 3016 | svoboda | 186 | mutex_lock(&TASK->udebug.lock); |
| 3471 | svoboda | 187 | LOG("debugging task %llu\n", TASK->taskid); |
| 2887 | svoboda | 188 | |
| 3014 | svoboda | 189 | if (TASK->udebug.dt_state != UDEBUG_TS_INACTIVE) { |
| 3016 | svoboda | 190 | mutex_unlock(&TASK->udebug.lock); |
| 3471 | svoboda | 191 | LOG("udebug_begin(): busy error\n"); |
| 2887 | svoboda | 192 | |
| 193 | return EBUSY; |
||
| 194 | } |
||
| 195 | |||
| 3014 | svoboda | 196 | TASK->udebug.dt_state = UDEBUG_TS_BEGINNING; |
| 197 | TASK->udebug.begin_call = call; |
||
| 198 | TASK->udebug.debugger = call->sender; |
||
| 2887 | svoboda | 199 | |
| 3014 | svoboda | 200 | if (TASK->udebug.not_stoppable_count == 0) { |
| 201 | TASK->udebug.dt_state = UDEBUG_TS_ACTIVE; |
||
| 202 | TASK->udebug.begin_call = NULL; |
||
| 2887 | svoboda | 203 | reply = 1; /* immediate reply */ |
| 204 | } else { |
||
| 205 | reply = 0; /* no reply */ |
||
| 206 | } |
||
| 207 | |||
| 3684 | svoboda | 208 | /* Set udebug.active on all of the task's userspace threads. */ |
| 2887 | svoboda | 209 | |
| 210 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
||
| 211 | t = list_get_instance(cur, thread_t, th_link); |
||
| 212 | |||
| 3026 | svoboda | 213 | mutex_lock(&t->udebug.lock); |
| 2887 | svoboda | 214 | if ((t->flags & THREAD_FLAG_USPACE) != 0) |
| 3684 | svoboda | 215 | t->udebug.active = true; |
| 3026 | svoboda | 216 | mutex_unlock(&t->udebug.lock); |
| 2887 | svoboda | 217 | } |
| 218 | |||
| 3016 | svoboda | 219 | mutex_unlock(&TASK->udebug.lock); |
| 2887 | svoboda | 220 | |
| 3471 | svoboda | 221 | LOG("udebug_begin() done (%s)\n", |
| 2887 | svoboda | 222 | reply ? "reply" : "stoppability wait"); |
| 223 | |||
| 224 | return reply; |
||
| 225 | } |
||
| 226 | |||
| 3471 | svoboda | 227 | /** Finish debugging the current task. |
| 228 | * |
||
| 229 | * Closes the debugging session for the current task. |
||
| 230 | * @return Zero on success or negative error code. |
||
| 231 | */ |
||
| 2887 | svoboda | 232 | int udebug_end(void) |
| 233 | { |
||
| 234 | int rc; |
||
| 235 | |||
| 3471 | svoboda | 236 | LOG("udebug_end()\n"); |
| 2887 | svoboda | 237 | |
| 3016 | svoboda | 238 | mutex_lock(&TASK->udebug.lock); |
| 3471 | svoboda | 239 | LOG("task %" PRIu64 "\n", TASK->taskid); |
| 2887 | svoboda | 240 | |
| 241 | rc = udebug_task_cleanup(TASK); |
||
| 242 | |||
| 3016 | svoboda | 243 | mutex_unlock(&TASK->udebug.lock); |
| 2887 | svoboda | 244 | |
| 3016 | svoboda | 245 | return rc; |
| 2887 | svoboda | 246 | } |
| 247 | |||
| 3471 | svoboda | 248 | /** Set the event mask. |
| 249 | * |
||
| 250 | * Sets the event mask that determines which events are enabled. |
||
| 251 | * |
||
| 252 | * @param mask Or combination of events that should be enabled. |
||
| 253 | * @return Zero on success or negative error code. |
||
| 254 | */ |
||
| 2899 | svoboda | 255 | int udebug_set_evmask(udebug_evmask_t mask) |
| 256 | { |
||
| 3471 | svoboda | 257 | LOG("udebug_set_mask()\n"); |
| 2899 | svoboda | 258 | |
| 3016 | svoboda | 259 | mutex_lock(&TASK->udebug.lock); |
| 2899 | svoboda | 260 | |
| 3014 | svoboda | 261 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
| 3016 | svoboda | 262 | mutex_unlock(&TASK->udebug.lock); |
| 3471 | svoboda | 263 | LOG("udebug_set_mask(): not active debuging session\n"); |
| 2899 | svoboda | 264 | |
| 265 | return EINVAL; |
||
| 266 | } |
||
| 267 | |||
| 3014 | svoboda | 268 | TASK->udebug.evmask = mask; |
| 2899 | svoboda | 269 | |
| 3016 | svoboda | 270 | mutex_unlock(&TASK->udebug.lock); |
| 2899 | svoboda | 271 | |
| 272 | return 0; |
||
| 273 | } |
||
| 274 | |||
| 3471 | svoboda | 275 | /** Give thread GO. |
| 276 | * |
||
| 3606 | svoboda | 277 | * Upon recieving a go message, the thread is given GO. Being GO |
| 3471 | svoboda | 278 | * means the thread is allowed to execute userspace code (until |
| 279 | * a debugging event or STOP occurs, at which point the thread loses GO. |
||
| 280 | * |
||
| 281 | * @param t The thread to operate on (unlocked and need not be valid). |
||
| 282 | * @param call The GO call that we are servicing. |
||
| 283 | */ |
||
| 2887 | svoboda | 284 | int udebug_go(thread_t *t, call_t *call) |
| 285 | { |
||
| 286 | int rc; |
||
| 287 | |||
| 3606 | svoboda | 288 | /* On success, this will lock t->udebug.lock. */ |
| 2898 | svoboda | 289 | rc = _thread_op_begin(t, false); |
| 2887 | svoboda | 290 | if (rc != EOK) { |
| 291 | return rc; |
||
| 292 | } |
||
| 293 | |||
| 3018 | svoboda | 294 | t->udebug.go_call = call; |
| 3606 | svoboda | 295 | t->udebug.go = true; |
| 3018 | svoboda | 296 | t->udebug.cur_event = 0; /* none */ |
| 2887 | svoboda | 297 | |
| 298 | /* |
||
| 3606 | svoboda | 299 | * Neither t's lock nor threads_lock may be held during wakeup. |
| 2887 | svoboda | 300 | */ |
| 3018 | svoboda | 301 | waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST); |
| 2887 | svoboda | 302 | |
| 303 | _thread_op_end(t); |
||
| 304 | |||
| 305 | return 0; |
||
| 306 | } |
||
| 307 | |||
| 3471 | svoboda | 308 | /** Stop a thread (i.e. take its GO away) |
| 309 | * |
||
| 310 | * Generates a STOP event as soon as the thread becomes stoppable (i.e. |
||
| 311 | * can be considered stopped). |
||
| 312 | * |
||
| 313 | * @param t The thread to operate on (unlocked and need not be valid). |
||
| 314 | * @param call The GO call that we are servicing. |
||
| 315 | */ |
||
| 2898 | svoboda | 316 | int udebug_stop(thread_t *t, call_t *call) |
| 317 | { |
||
| 318 | int rc; |
||
| 2887 | svoboda | 319 | |
| 3471 | svoboda | 320 | LOG("udebug_stop()\n"); |
| 2898 | svoboda | 321 | |
| 322 | /* |
||
| 3018 | svoboda | 323 | * On success, this will lock t->udebug.lock. Note that this makes sure |
| 2898 | svoboda | 324 | * the thread is not stopped. |
| 325 | */ |
||
| 326 | rc = _thread_op_begin(t, true); |
||
| 327 | if (rc != EOK) { |
||
| 328 | return rc; |
||
| 329 | } |
||
| 330 | |||
| 3606 | svoboda | 331 | /* Take GO away from the thread. */ |
| 332 | t->udebug.go = false; |
||
| 2898 | svoboda | 333 | |
| 3606 | svoboda | 334 | if (t->udebug.stoppable != true) { |
| 335 | /* Answer will be sent when the thread becomes stoppable. */ |
||
| 2898 | svoboda | 336 | _thread_op_end(t); |
| 337 | return 0; |
||
| 338 | } |
||
| 339 | |||
| 340 | /* |
||
| 3606 | svoboda | 341 | * Answer GO call. |
| 2898 | svoboda | 342 | */ |
| 3471 | svoboda | 343 | LOG("udebug_stop - answering go call\n"); |
| 2898 | svoboda | 344 | |
| 3606 | svoboda | 345 | /* Make sure nobody takes this call away from us. */ |
| 3018 | svoboda | 346 | call = t->udebug.go_call; |
| 347 | t->udebug.go_call = NULL; |
||
| 2898 | svoboda | 348 | |
| 349 | IPC_SET_RETVAL(call->data, 0); |
||
| 350 | IPC_SET_ARG1(call->data, UDEBUG_EVENT_STOP); |
||
| 3471 | svoboda | 351 | LOG("udebug_stop/ipc_answer\n"); |
| 2898 | svoboda | 352 | |
| 3018 | svoboda | 353 | THREAD->udebug.cur_event = UDEBUG_EVENT_STOP; |
| 3016 | svoboda | 354 | |
| 2898 | svoboda | 355 | _thread_op_end(t); |
| 356 | |||
| 3606 | svoboda | 357 | mutex_lock(&TASK->udebug.lock); |
| 2898 | svoboda | 358 | ipc_answer(&TASK->answerbox, call); |
| 3016 | svoboda | 359 | mutex_unlock(&TASK->udebug.lock); |
| 2898 | svoboda | 360 | |
| 3471 | svoboda | 361 | LOG("udebog_stop/done\n"); |
| 2898 | svoboda | 362 | return 0; |
| 363 | } |
||
| 364 | |||
| 3471 | svoboda | 365 | /** Read the list of userspace threads in the current task. |
| 366 | * |
||
| 367 | * The list takes the form of a sequence of thread hashes (i.e. the pointers |
||
| 368 | * to thread structures). A buffer of size @a buf_size is allocated and |
||
| 369 | * a pointer to it written to @a buffer. The sequence of hashes is written |
||
| 370 | * into this buffer. |
||
| 371 | * |
||
| 372 | * If the sequence is longer than @a buf_size bytes, only as much hashes |
||
| 373 | * as can fit are copied. The number of thread hashes copied is stored |
||
| 374 | * in @a n. |
||
| 375 | * |
||
| 376 | * The rationale for having @a buf_size is that this function is only |
||
| 377 | * used for servicing the THREAD_READ message, which always specifies |
||
| 378 | * a maximum size for the userspace buffer. |
||
| 379 | * |
||
| 380 | * @param buffer The buffer for storing thread hashes. |
||
| 381 | * @param buf_size Buffer size in bytes. |
||
| 382 | * @param n The actual number of hashes copied will be stored here. |
||
| 383 | */ |
||
| 2897 | svoboda | 384 | int udebug_thread_read(void **buffer, size_t buf_size, size_t *n) |
| 2887 | svoboda | 385 | { |
| 386 | thread_t *t; |
||
| 387 | link_t *cur; |
||
| 388 | unative_t tid; |
||
| 2897 | svoboda | 389 | unsigned copied_ids; |
| 2887 | svoboda | 390 | ipl_t ipl; |
| 391 | unative_t *id_buffer; |
||
| 392 | int flags; |
||
| 2897 | svoboda | 393 | size_t max_ids; |
| 2887 | svoboda | 394 | |
| 3471 | svoboda | 395 | LOG("udebug_thread_read()\n"); |
| 2887 | svoboda | 396 | |
| 2897 | svoboda | 397 | /* Allocate a buffer to hold thread IDs */ |
| 398 | id_buffer = malloc(buf_size, 0); |
||
| 399 | |||
| 3016 | svoboda | 400 | mutex_lock(&TASK->udebug.lock); |
| 2887 | svoboda | 401 | |
| 402 | /* Verify task state */ |
||
| 3014 | svoboda | 403 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
| 3016 | svoboda | 404 | mutex_unlock(&TASK->udebug.lock); |
| 2887 | svoboda | 405 | return EINVAL; |
| 406 | } |
||
| 407 | |||
| 3016 | svoboda | 408 | ipl = interrupts_disable(); |
| 409 | spinlock_lock(&TASK->lock); |
||
| 2897 | svoboda | 410 | /* Copy down the thread IDs */ |
| 2887 | svoboda | 411 | |
| 2897 | svoboda | 412 | max_ids = buf_size / sizeof(unative_t); |
| 413 | copied_ids = 0; |
||
| 414 | |||
| 3026 | svoboda | 415 | /* FIXME: make sure the thread isn't past debug shutdown... */ |
| 2887 | svoboda | 416 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
| 2897 | svoboda | 417 | /* Do not write past end of buffer */ |
| 418 | if (copied_ids >= max_ids) break; |
||
| 2887 | svoboda | 419 | |
| 420 | t = list_get_instance(cur, thread_t, th_link); |
||
| 421 | |||
| 422 | spinlock_lock(&t->lock); |
||
| 423 | flags = t->flags; |
||
| 424 | spinlock_unlock(&t->lock); |
||
| 425 | |||
| 3606 | svoboda | 426 | /* Not interested in kernel threads. */ |
| 2887 | svoboda | 427 | if ((flags & THREAD_FLAG_USPACE) != 0) { |
| 2897 | svoboda | 428 | /* Using thread struct pointer as identification hash */ |
| 2887 | svoboda | 429 | tid = (unative_t) t; |
| 430 | id_buffer[copied_ids++] = tid; |
||
| 431 | } |
||
| 432 | } |
||
| 433 | |||
| 434 | spinlock_unlock(&TASK->lock); |
||
| 435 | interrupts_restore(ipl); |
||
| 436 | |||
| 3016 | svoboda | 437 | mutex_unlock(&TASK->udebug.lock); |
| 438 | |||
| 2887 | svoboda | 439 | *buffer = id_buffer; |
| 440 | *n = copied_ids * sizeof(unative_t); |
||
| 441 | |||
| 442 | return 0; |
||
| 443 | } |
||
| 444 | |||
| 3471 | svoboda | 445 | /** Read the arguments of a system call. |
| 446 | * |
||
| 447 | * The arguments of the system call being being executed are copied |
||
| 448 | * to an allocated buffer and a pointer to it is written to @a buffer. |
||
| 449 | * The size of the buffer is exactly such that it can hold the maximum number |
||
| 450 | * of system-call arguments. |
||
| 451 | * |
||
| 452 | * Unless the thread is currently blocked in a SYSCALL_B or SYSCALL_E event, |
||
| 453 | * this function will fail with an EINVAL error code. |
||
| 454 | * |
||
| 455 | * @param buffer The buffer for storing thread hashes. |
||
| 456 | */ |
||
| 2887 | svoboda | 457 | int udebug_args_read(thread_t *t, void **buffer) |
| 458 | { |
||
| 459 | int rc; |
||
| 460 | unative_t *arg_buffer; |
||
| 461 | |||
| 3606 | svoboda | 462 | /* Prepare a buffer to hold the arguments. */ |
| 2896 | svoboda | 463 | arg_buffer = malloc(6 * sizeof(unative_t), 0); |
| 464 | |||
| 3606 | svoboda | 465 | /* On success, this will lock t->udebug.lock. */ |
| 2898 | svoboda | 466 | rc = _thread_op_begin(t, false); |
| 2887 | svoboda | 467 | if (rc != EOK) { |
| 468 | return rc; |
||
| 469 | } |
||
| 470 | |||
| 3606 | svoboda | 471 | /* Additionally we need to verify that we are inside a syscall. */ |
| 3018 | svoboda | 472 | if (t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_B && |
| 473 | t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_E) { |
||
| 2887 | svoboda | 474 | _thread_op_end(t); |
| 475 | return EINVAL; |
||
| 476 | } |
||
| 477 | |||
| 3606 | svoboda | 478 | /* Copy to a local buffer before releasing the lock. */ |
| 3018 | svoboda | 479 | memcpy(arg_buffer, t->udebug.syscall_args, 6 * sizeof(unative_t)); |
| 2887 | svoboda | 480 | |
| 481 | _thread_op_end(t); |
||
| 482 | |||
| 483 | *buffer = arg_buffer; |
||
| 484 | return 0; |
||
| 485 | } |
||
| 486 | |||
| 2919 | svoboda | 487 | int udebug_regs_read(thread_t *t, void *buffer) |
| 2887 | svoboda | 488 | { |
| 489 | istate_t *state; |
||
| 490 | int rc; |
||
| 491 | |||
| 3424 | svoboda | 492 | // printf("udebug_regs_read()\n"); |
| 2887 | svoboda | 493 | |
| 3018 | svoboda | 494 | /* On success, this will lock t->udebug.lock */ |
| 2898 | svoboda | 495 | rc = _thread_op_begin(t, false); |
| 2887 | svoboda | 496 | if (rc != EOK) { |
| 497 | return rc; |
||
| 498 | } |
||
| 499 | |||
| 3018 | svoboda | 500 | state = t->udebug.uspace_state; |
| 2887 | svoboda | 501 | if (state == NULL) { |
| 502 | _thread_op_end(t); |
||
| 3611 | svoboda | 503 | LOG("udebug_regs_read() - istate not available\n"); |
| 2887 | svoboda | 504 | return EBUSY; |
| 505 | } |
||
| 506 | |||
| 2896 | svoboda | 507 | /* Copy to the allocated buffer */ |
| 2919 | svoboda | 508 | memcpy(buffer, state, sizeof(istate_t)); |
| 2887 | svoboda | 509 | |
| 510 | _thread_op_end(t); |
||
| 511 | |||
| 512 | return 0; |
||
| 513 | } |
||
| 514 | |||
| 515 | int udebug_regs_write(thread_t *t, void *buffer) |
||
| 516 | { |
||
| 517 | int rc; |
||
| 518 | istate_t *state; |
||
| 519 | |||
| 3611 | svoboda | 520 | LOG("udebug_regs_write()\n"); |
| 2887 | svoboda | 521 | |
| 522 | /* Try to change the thread's uspace_state */ |
||
| 523 | |||
| 3018 | svoboda | 524 | /* On success, this will lock t->udebug.lock */ |
| 2898 | svoboda | 525 | rc = _thread_op_begin(t, false); |
| 2887 | svoboda | 526 | if (rc != EOK) { |
| 3611 | svoboda | 527 | LOG("error locking thread\n"); |
| 2887 | svoboda | 528 | return rc; |
| 529 | } |
||
| 530 | |||
| 3018 | svoboda | 531 | state = t->udebug.uspace_state; |
| 2887 | svoboda | 532 | if (state == NULL) { |
| 533 | _thread_op_end(t); |
||
| 3611 | svoboda | 534 | LOG("udebug_regs_write() - istate not available\n"); |
| 2887 | svoboda | 535 | return EBUSY; |
| 536 | } |
||
| 537 | |||
| 3018 | svoboda | 538 | memcpy(t->udebug.uspace_state, buffer, sizeof(istate_t)); |
| 2887 | svoboda | 539 | |
| 540 | _thread_op_end(t); |
||
| 541 | |||
| 542 | return 0; |
||
| 543 | } |
||
| 544 | |||
| 3471 | svoboda | 545 | /** Read the memory of the debugged task. |
| 546 | * |
||
| 547 | * Reads @a n bytes from the address space of the debugged task, starting |
||
| 548 | * from @a uspace_addr. The bytes are copied into an allocated buffer |
||
| 549 | * and a pointer to it is written into @a buffer. |
||
| 550 | * |
||
| 551 | * @param uspace_addr Address from where to start reading. |
||
| 552 | * @param n Number of bytes to read. |
||
| 553 | * @param buffer For storing a pointer to the allocated buffer. |
||
| 554 | */ |
||
| 2887 | svoboda | 555 | int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer) |
| 556 | { |
||
| 557 | void *data_buffer; |
||
| 558 | int rc; |
||
| 559 | |||
| 3016 | svoboda | 560 | /* Verify task state */ |
| 561 | mutex_lock(&TASK->udebug.lock); |
||
| 2887 | svoboda | 562 | |
| 3016 | svoboda | 563 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
| 564 | mutex_unlock(&TASK->udebug.lock); |
||
| 565 | return EBUSY; |
||
| 566 | } |
||
| 567 | |||
| 2896 | svoboda | 568 | data_buffer = malloc(n, 0); |
| 569 | |||
| 2887 | svoboda | 570 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
| 571 | * be a problem */ |
||
| 572 | rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n); |
||
| 3016 | svoboda | 573 | mutex_unlock(&TASK->udebug.lock); |
| 2887 | svoboda | 574 | |
| 3016 | svoboda | 575 | if (rc != 0) return rc; |
| 576 | |||
| 2887 | svoboda | 577 | *buffer = data_buffer; |
| 578 | return 0; |
||
| 579 | } |
||
| 580 | |||
| 581 | int udebug_mem_write(unative_t uspace_addr, void *data, size_t n) |
||
| 582 | { |
||
| 583 | int rc; |
||
| 584 | |||
| 3611 | svoboda | 585 | LOG("udebug_mem_write()\n"); |
| 2887 | svoboda | 586 | |
| 3127 | svoboda | 587 | /* n must be positive */ |
| 588 | if (n < 1) |
||
| 589 | return EINVAL; |
||
| 590 | |||
| 2887 | svoboda | 591 | /* Verify task state */ |
| 3016 | svoboda | 592 | mutex_lock(&TASK->udebug.lock); |
| 2887 | svoboda | 593 | |
| 3016 | svoboda | 594 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
| 595 | mutex_unlock(&TASK->udebug.lock); |
||
| 2887 | svoboda | 596 | return EBUSY; |
| 3016 | svoboda | 597 | } |
| 2887 | svoboda | 598 | |
| 3611 | svoboda | 599 | LOG("dst=%u, size=%u\n", uspace_addr, n); |
| 2887 | svoboda | 600 | |
| 601 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
||
| 602 | * be a problem */ |
||
| 3013 | svoboda | 603 | // rc = copy_to_uspace((void *)uspace_addr, data, n); |
| 604 | // if (rc) return rc; |
||
| 2887 | svoboda | 605 | |
| 3013 | svoboda | 606 | rc = as_debug_write(uspace_addr, data, n); |
| 3127 | svoboda | 607 | |
| 3611 | svoboda | 608 | LOG("rc=%d\n", rc); |
| 3013 | svoboda | 609 | |
| 3016 | svoboda | 610 | mutex_unlock(&TASK->udebug.lock); |
| 611 | |||
| 3013 | svoboda | 612 | return rc; |
| 2887 | svoboda | 613 | } |
| 614 | |||
| 615 | /** @} |
||
| 616 | */ |