| 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 hooks and data structure management.
|
|---|
| 36 | *
|
|---|
| 37 | * Udebug is an interface that makes userspace debuggers possible.
|
|---|
| 38 | */
|
|---|
| 39 |
|
|---|
| 40 | #include <synch/waitq.h>
|
|---|
| 41 | #include <debug.h>
|
|---|
| 42 | #include <udebug/udebug.h>
|
|---|
| 43 | #include <errno.h>
|
|---|
| 44 | #include <print.h>
|
|---|
| 45 | #include <arch.h>
|
|---|
| 46 |
|
|---|
| 47 | /** Initialize udebug part of task structure.
|
|---|
| 48 | *
|
|---|
| 49 | * Called as part of task structure initialization.
|
|---|
| 50 | * @param ut Pointer to the structure to initialize.
|
|---|
| 51 | *
|
|---|
| 52 | */
|
|---|
| 53 | void udebug_task_init(udebug_task_t *ut)
|
|---|
| 54 | {
|
|---|
| 55 | mutex_initialize(&ut->lock, MUTEX_PASSIVE);
|
|---|
| 56 | ut->dt_state = UDEBUG_TS_INACTIVE;
|
|---|
| 57 | ut->begin_call = NULL;
|
|---|
| 58 | ut->not_stoppable_count = 0;
|
|---|
| 59 | ut->evmask = 0;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /** Initialize udebug part of thread structure.
|
|---|
| 63 | *
|
|---|
| 64 | * Called as part of thread structure initialization.
|
|---|
| 65 | *
|
|---|
| 66 | * @param ut Pointer to the structure to initialize.
|
|---|
| 67 | *
|
|---|
| 68 | */
|
|---|
| 69 | void udebug_thread_initialize(udebug_thread_t *ut)
|
|---|
| 70 | {
|
|---|
| 71 | mutex_initialize(&ut->lock, MUTEX_PASSIVE);
|
|---|
| 72 | waitq_initialize(&ut->go_wq);
|
|---|
| 73 | condvar_initialize(&ut->active_cv);
|
|---|
| 74 |
|
|---|
| 75 | ut->go_call = NULL;
|
|---|
| 76 | ut->uspace_state = NULL;
|
|---|
| 77 | ut->go = false;
|
|---|
| 78 | ut->stoppable = true;
|
|---|
| 79 | ut->active = false;
|
|---|
| 80 | ut->cur_event = 0; /* None */
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | /** Wait for a GO message.
|
|---|
| 84 | *
|
|---|
| 85 | * When a debugging event occurs in a thread or the thread is stopped,
|
|---|
| 86 | * this function is called to block the thread until a GO message
|
|---|
| 87 | * is received.
|
|---|
| 88 | *
|
|---|
| 89 | * @param wq The wait queue used by the thread to wait for GO messages.
|
|---|
| 90 | *
|
|---|
| 91 | */
|
|---|
| 92 | static void udebug_wait_for_go(waitq_t *wq)
|
|---|
| 93 | {
|
|---|
| 94 | ipl_t ipl = waitq_sleep_prepare(wq);
|
|---|
| 95 |
|
|---|
| 96 | wq->missed_wakeups = 0; /* Enforce blocking. */
|
|---|
| 97 | int rc = waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
|
|---|
| 98 |
|
|---|
| 99 | waitq_sleep_finish(wq, rc, ipl);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /** Start of stoppable section.
|
|---|
| 103 | *
|
|---|
| 104 | * A stoppable section is a section of code where if the thread can
|
|---|
| 105 | * be stoped. In other words, if a STOP operation is issued, the thread
|
|---|
| 106 | * is guaranteed not to execute any userspace instructions until the
|
|---|
| 107 | * thread is resumed.
|
|---|
| 108 | *
|
|---|
| 109 | * Having stoppable sections is better than having stopping points, since
|
|---|
| 110 | * a thread can be stopped even when it is blocked indefinitely in a system
|
|---|
| 111 | * call (whereas it would not reach any stopping point).
|
|---|
| 112 | *
|
|---|
| 113 | */
|
|---|
| 114 | void udebug_stoppable_begin(void)
|
|---|
| 115 | {
|
|---|
| 116 | ASSERT(THREAD);
|
|---|
| 117 | ASSERT(TASK);
|
|---|
| 118 |
|
|---|
| 119 | mutex_lock(&TASK->udebug.lock);
|
|---|
| 120 |
|
|---|
| 121 | int nsc = --TASK->udebug.not_stoppable_count;
|
|---|
| 122 |
|
|---|
| 123 | /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
|
|---|
| 124 | mutex_lock(&THREAD->udebug.lock);
|
|---|
| 125 | ASSERT(THREAD->udebug.stoppable == false);
|
|---|
| 126 | THREAD->udebug.stoppable = true;
|
|---|
| 127 |
|
|---|
| 128 | if ((TASK->udebug.dt_state == UDEBUG_TS_BEGINNING) && (nsc == 0)) {
|
|---|
| 129 | /*
|
|---|
| 130 | * This was the last non-stoppable thread. Reply to
|
|---|
| 131 | * DEBUG_BEGIN call.
|
|---|
| 132 | *
|
|---|
| 133 | */
|
|---|
| 134 |
|
|---|
| 135 | call_t *db_call = TASK->udebug.begin_call;
|
|---|
| 136 | ASSERT(db_call);
|
|---|
| 137 |
|
|---|
| 138 | TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
|
|---|
| 139 | TASK->udebug.begin_call = NULL;
|
|---|
| 140 |
|
|---|
| 141 | IPC_SET_RETVAL(db_call->data, 0);
|
|---|
| 142 | ipc_answer(&TASK->answerbox, db_call);
|
|---|
| 143 | } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
|
|---|
| 144 | /*
|
|---|
| 145 | * Active debugging session
|
|---|
| 146 | */
|
|---|
| 147 |
|
|---|
| 148 | if (THREAD->udebug.active == true &&
|
|---|
| 149 | THREAD->udebug.go == false) {
|
|---|
| 150 | /*
|
|---|
| 151 | * Thread was requested to stop - answer go call
|
|---|
| 152 | *
|
|---|
| 153 | */
|
|---|
| 154 |
|
|---|
| 155 | /* Make sure nobody takes this call away from us */
|
|---|
| 156 | call_t *go_call = THREAD->udebug.go_call;
|
|---|
| 157 | THREAD->udebug.go_call = NULL;
|
|---|
| 158 | ASSERT(go_call);
|
|---|
| 159 |
|
|---|
| 160 | IPC_SET_RETVAL(go_call->data, 0);
|
|---|
| 161 | IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
|
|---|
| 162 |
|
|---|
| 163 | THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
|
|---|
| 164 | ipc_answer(&TASK->answerbox, go_call);
|
|---|
| 165 | }
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | mutex_unlock(&THREAD->udebug.lock);
|
|---|
| 169 | mutex_unlock(&TASK->udebug.lock);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | /** End of a stoppable section.
|
|---|
| 173 | *
|
|---|
| 174 | * This is the point where the thread will block if it is stopped.
|
|---|
| 175 | * (As, by definition, a stopped thread must not leave its stoppable
|
|---|
| 176 | * section).
|
|---|
| 177 | *
|
|---|
| 178 | */
|
|---|
| 179 | void udebug_stoppable_end(void)
|
|---|
| 180 | {
|
|---|
| 181 | restart:
|
|---|
| 182 | mutex_lock(&TASK->udebug.lock);
|
|---|
| 183 | mutex_lock(&THREAD->udebug.lock);
|
|---|
| 184 |
|
|---|
| 185 | if ((THREAD->udebug.active) && (THREAD->udebug.go == false)) {
|
|---|
| 186 | mutex_unlock(&THREAD->udebug.lock);
|
|---|
| 187 | mutex_unlock(&TASK->udebug.lock);
|
|---|
| 188 |
|
|---|
| 189 | udebug_wait_for_go(&THREAD->udebug.go_wq);
|
|---|
| 190 |
|
|---|
| 191 | goto restart;
|
|---|
| 192 | /* Must try again - have to lose stoppability atomically. */
|
|---|
| 193 | } else {
|
|---|
| 194 | ++TASK->udebug.not_stoppable_count;
|
|---|
| 195 | ASSERT(THREAD->udebug.stoppable == true);
|
|---|
| 196 | THREAD->udebug.stoppable = false;
|
|---|
| 197 |
|
|---|
| 198 | mutex_unlock(&THREAD->udebug.lock);
|
|---|
| 199 | mutex_unlock(&TASK->udebug.lock);
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /** Upon being scheduled to run, check if the current thread should stop.
|
|---|
| 204 | *
|
|---|
| 205 | * This function is called from clock().
|
|---|
| 206 | *
|
|---|
| 207 | */
|
|---|
| 208 | void udebug_before_thread_runs(void)
|
|---|
| 209 | {
|
|---|
| 210 | /* Check if we're supposed to stop */
|
|---|
| 211 | udebug_stoppable_begin();
|
|---|
| 212 | udebug_stoppable_end();
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | /** Syscall event hook.
|
|---|
| 216 | *
|
|---|
| 217 | * Must be called before and after servicing a system call. This generates
|
|---|
| 218 | * a SYSCALL_B or SYSCALL_E event, depending on the value of @a end_variant.
|
|---|
| 219 | *
|
|---|
| 220 | */
|
|---|
| 221 | void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
|
|---|
| 222 | unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc,
|
|---|
| 223 | bool end_variant)
|
|---|
| 224 | {
|
|---|
| 225 | udebug_event_t etype =
|
|---|
| 226 | end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
|
|---|
| 227 |
|
|---|
| 228 | mutex_lock(&TASK->udebug.lock);
|
|---|
| 229 | mutex_lock(&THREAD->udebug.lock);
|
|---|
| 230 |
|
|---|
| 231 | /* Must only generate events when in debugging session and is go. */
|
|---|
| 232 | if (THREAD->udebug.active != true || THREAD->udebug.go == false ||
|
|---|
| 233 | (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
|
|---|
| 234 | mutex_unlock(&THREAD->udebug.lock);
|
|---|
| 235 | mutex_unlock(&TASK->udebug.lock);
|
|---|
| 236 | return;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | /* Fill in the GO response. */
|
|---|
| 240 | call_t *call = THREAD->udebug.go_call;
|
|---|
| 241 | THREAD->udebug.go_call = NULL;
|
|---|
| 242 |
|
|---|
| 243 | IPC_SET_RETVAL(call->data, 0);
|
|---|
| 244 | IPC_SET_ARG1(call->data, etype);
|
|---|
| 245 | IPC_SET_ARG2(call->data, id);
|
|---|
| 246 | IPC_SET_ARG3(call->data, rc);
|
|---|
| 247 |
|
|---|
| 248 | THREAD->udebug.syscall_args[0] = a1;
|
|---|
| 249 | THREAD->udebug.syscall_args[1] = a2;
|
|---|
| 250 | THREAD->udebug.syscall_args[2] = a3;
|
|---|
| 251 | THREAD->udebug.syscall_args[3] = a4;
|
|---|
| 252 | THREAD->udebug.syscall_args[4] = a5;
|
|---|
| 253 | THREAD->udebug.syscall_args[5] = a6;
|
|---|
| 254 |
|
|---|
| 255 | /*
|
|---|
| 256 | * Make sure udebug.go is false when going to sleep
|
|---|
| 257 | * in case we get woken up by DEBUG_END. (At which
|
|---|
| 258 | * point it must be back to the initial true value).
|
|---|
| 259 | *
|
|---|
| 260 | */
|
|---|
| 261 | THREAD->udebug.go = false;
|
|---|
| 262 | THREAD->udebug.cur_event = etype;
|
|---|
| 263 |
|
|---|
| 264 | ipc_answer(&TASK->answerbox, call);
|
|---|
| 265 |
|
|---|
| 266 | mutex_unlock(&THREAD->udebug.lock);
|
|---|
| 267 | mutex_unlock(&TASK->udebug.lock);
|
|---|
| 268 |
|
|---|
| 269 | udebug_wait_for_go(&THREAD->udebug.go_wq);
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | /** Thread-creation event hook combined with attaching the thread.
|
|---|
| 273 | *
|
|---|
| 274 | * Must be called when a new userspace thread is created in the debugged
|
|---|
| 275 | * task. Generates a THREAD_B event. Also attaches the thread @a t
|
|---|
| 276 | * to the task @a ta.
|
|---|
| 277 | *
|
|---|
| 278 | * This is necessary to avoid a race condition where the BEGIN and THREAD_READ
|
|---|
| 279 | * requests would be handled inbetween attaching the thread and checking it
|
|---|
| 280 | * for being in a debugging session to send the THREAD_B event. We could then
|
|---|
| 281 | * either miss threads or get some threads both in the thread list
|
|---|
| 282 | * and get a THREAD_B event for them.
|
|---|
| 283 | *
|
|---|
| 284 | * @param thread Structure of the thread being created. Not locked, as the
|
|---|
| 285 | * thread is not executing yet.
|
|---|
| 286 | * @param task Task to which the thread should be attached.
|
|---|
| 287 | *
|
|---|
| 288 | */
|
|---|
| 289 | void udebug_thread_b_event_attach(struct thread *thread, struct task *task)
|
|---|
| 290 | {
|
|---|
| 291 | mutex_lock(&TASK->udebug.lock);
|
|---|
| 292 | mutex_lock(&THREAD->udebug.lock);
|
|---|
| 293 |
|
|---|
| 294 | thread_attach(thread, task);
|
|---|
| 295 |
|
|---|
| 296 | LOG("Check state");
|
|---|
| 297 |
|
|---|
| 298 | /* Must only generate events when in debugging session */
|
|---|
| 299 | if (THREAD->udebug.active != true) {
|
|---|
| 300 | LOG("udebug.active: %s, udebug.go: %s",
|
|---|
| 301 | THREAD->udebug.active ? "Yes(+)" : "No",
|
|---|
| 302 | THREAD->udebug.go ? "Yes(-)" : "No");
|
|---|
| 303 |
|
|---|
| 304 | mutex_unlock(&THREAD->udebug.lock);
|
|---|
| 305 | mutex_unlock(&TASK->udebug.lock);
|
|---|
| 306 | return;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | LOG("Trigger event");
|
|---|
| 310 |
|
|---|
| 311 | call_t *call = THREAD->udebug.go_call;
|
|---|
| 312 |
|
|---|
| 313 | THREAD->udebug.go_call = NULL;
|
|---|
| 314 | IPC_SET_RETVAL(call->data, 0);
|
|---|
| 315 | IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
|
|---|
| 316 | IPC_SET_ARG2(call->data, (unative_t) thread);
|
|---|
| 317 |
|
|---|
| 318 | /*
|
|---|
| 319 | * Make sure udebug.go is false when going to sleep
|
|---|
| 320 | * in case we get woken up by DEBUG_END. (At which
|
|---|
| 321 | * point it must be back to the initial true value).
|
|---|
| 322 | *
|
|---|
| 323 | */
|
|---|
| 324 | THREAD->udebug.go = false;
|
|---|
| 325 | THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
|
|---|
| 326 |
|
|---|
| 327 | ipc_answer(&TASK->answerbox, call);
|
|---|
| 328 |
|
|---|
| 329 | mutex_unlock(&THREAD->udebug.lock);
|
|---|
| 330 | mutex_unlock(&TASK->udebug.lock);
|
|---|
| 331 |
|
|---|
| 332 | LOG("Wait for Go");
|
|---|
| 333 | udebug_wait_for_go(&THREAD->udebug.go_wq);
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | /** Thread-termination event hook.
|
|---|
| 337 | *
|
|---|
| 338 | * Must be called when the current thread is terminating.
|
|---|
| 339 | * Generates a THREAD_E event.
|
|---|
| 340 | *
|
|---|
| 341 | */
|
|---|
| 342 | void udebug_thread_e_event(void)
|
|---|
| 343 | {
|
|---|
| 344 | mutex_lock(&TASK->udebug.lock);
|
|---|
| 345 | mutex_lock(&THREAD->udebug.lock);
|
|---|
| 346 |
|
|---|
| 347 | LOG("Check state");
|
|---|
| 348 |
|
|---|
| 349 | /* Must only generate events when in debugging session. */
|
|---|
| 350 | if (THREAD->udebug.active != true) {
|
|---|
| 351 | LOG("udebug.active: %s, udebug.go: %s",
|
|---|
| 352 | THREAD->udebug.active ? "Yes" : "No",
|
|---|
| 353 | THREAD->udebug.go ? "Yes" : "No");
|
|---|
| 354 |
|
|---|
| 355 | mutex_unlock(&THREAD->udebug.lock);
|
|---|
| 356 | mutex_unlock(&TASK->udebug.lock);
|
|---|
| 357 | return;
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 | LOG("Trigger event");
|
|---|
| 361 |
|
|---|
| 362 | call_t *call = THREAD->udebug.go_call;
|
|---|
| 363 |
|
|---|
| 364 | THREAD->udebug.go_call = NULL;
|
|---|
| 365 | IPC_SET_RETVAL(call->data, 0);
|
|---|
| 366 | IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
|
|---|
| 367 |
|
|---|
| 368 | /* Prevent any further debug activity in thread. */
|
|---|
| 369 | THREAD->udebug.active = false;
|
|---|
| 370 | THREAD->udebug.cur_event = 0; /* None */
|
|---|
| 371 | THREAD->udebug.go = false; /* Set to initial value */
|
|---|
| 372 |
|
|---|
| 373 | ipc_answer(&TASK->answerbox, call);
|
|---|
| 374 |
|
|---|
| 375 | mutex_unlock(&THREAD->udebug.lock);
|
|---|
| 376 | mutex_unlock(&TASK->udebug.lock);
|
|---|
| 377 |
|
|---|
| 378 | /*
|
|---|
| 379 | * This event does not sleep - debugging has finished
|
|---|
| 380 | * in this thread.
|
|---|
| 381 | *
|
|---|
| 382 | */
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | /** Terminate task debugging session.
|
|---|
| 386 | *
|
|---|
| 387 | * Gracefully terminate the debugging session for a task. If the debugger
|
|---|
| 388 | * is still waiting for events on some threads, it will receive a
|
|---|
| 389 | * FINISHED event for each of them.
|
|---|
| 390 | *
|
|---|
| 391 | * @param task Task structure. ta->udebug.lock must be already locked.
|
|---|
| 392 | *
|
|---|
| 393 | * @return Zero on success or negative error code.
|
|---|
| 394 | *
|
|---|
| 395 | */
|
|---|
| 396 | int udebug_task_cleanup(struct task *task)
|
|---|
| 397 | {
|
|---|
| 398 | if ((task->udebug.dt_state != UDEBUG_TS_BEGINNING) &&
|
|---|
| 399 | (task->udebug.dt_state != UDEBUG_TS_ACTIVE)) {
|
|---|
| 400 | return EINVAL;
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | LOG("Task %" PRIu64, task->taskid);
|
|---|
| 404 |
|
|---|
| 405 | /* Finish debugging of all userspace threads */
|
|---|
| 406 | link_t *cur;
|
|---|
| 407 | for (cur = task->th_head.next; cur != &task->th_head; cur = cur->next) {
|
|---|
| 408 | thread_t *thread = list_get_instance(cur, thread_t, th_link);
|
|---|
| 409 |
|
|---|
| 410 | mutex_lock(&thread->udebug.lock);
|
|---|
| 411 | unsigned int flags = thread->flags;
|
|---|
| 412 |
|
|---|
| 413 | /* Only process userspace threads. */
|
|---|
| 414 | if ((flags & THREAD_FLAG_USPACE) != 0) {
|
|---|
| 415 | /* Prevent any further debug activity in thread. */
|
|---|
| 416 | thread->udebug.active = false;
|
|---|
| 417 | thread->udebug.cur_event = 0; /* None */
|
|---|
| 418 |
|
|---|
| 419 | /* Is the thread still go? */
|
|---|
| 420 | if (thread->udebug.go == true) {
|
|---|
| 421 | /*
|
|---|
| 422 | * Yes, so clear go. As active == false,
|
|---|
| 423 | * this doesn't affect anything.
|
|---|
| 424 | (
|
|---|
| 425 | */
|
|---|
| 426 | thread->udebug.go = false;
|
|---|
| 427 |
|
|---|
| 428 | /* Answer GO call */
|
|---|
| 429 | LOG("Answer GO call with EVENT_FINISHED.");
|
|---|
| 430 |
|
|---|
| 431 | IPC_SET_RETVAL(thread->udebug.go_call->data, 0);
|
|---|
| 432 | IPC_SET_ARG1(thread->udebug.go_call->data,
|
|---|
| 433 | UDEBUG_EVENT_FINISHED);
|
|---|
| 434 |
|
|---|
| 435 | ipc_answer(&task->answerbox, thread->udebug.go_call);
|
|---|
| 436 | thread->udebug.go_call = NULL;
|
|---|
| 437 | } else {
|
|---|
| 438 | /*
|
|---|
| 439 | * Debug_stop is already at initial value.
|
|---|
| 440 | * Yet this means the thread needs waking up.
|
|---|
| 441 | *
|
|---|
| 442 | */
|
|---|
| 443 |
|
|---|
| 444 | /*
|
|---|
| 445 | * thread's lock must not be held when calling
|
|---|
| 446 | * waitq_wakeup.
|
|---|
| 447 | *
|
|---|
| 448 | */
|
|---|
| 449 | waitq_wakeup(&thread->udebug.go_wq, WAKEUP_FIRST);
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | mutex_unlock(&thread->udebug.lock);
|
|---|
| 453 | condvar_broadcast(&thread->udebug.active_cv);
|
|---|
| 454 | } else
|
|---|
| 455 | mutex_unlock(&thread->udebug.lock);
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | task->udebug.dt_state = UDEBUG_TS_INACTIVE;
|
|---|
| 459 | task->udebug.debugger = NULL;
|
|---|
| 460 |
|
|---|
| 461 | return 0;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | /** Wait for debugger to handle a fault in this thread.
|
|---|
| 465 | *
|
|---|
| 466 | * When a thread faults and someone is subscribed to the FAULT kernel event,
|
|---|
| 467 | * this function is called to wait for a debugging session to give userspace
|
|---|
| 468 | * a chance to examine the faulting thead/task. When the debugging session
|
|---|
| 469 | * is over, this function returns (so that thread/task cleanup can continue).
|
|---|
| 470 | *
|
|---|
| 471 | */
|
|---|
| 472 | void udebug_thread_fault(void)
|
|---|
| 473 | {
|
|---|
| 474 | udebug_stoppable_begin();
|
|---|
| 475 |
|
|---|
| 476 | /* Wait until a debugger attends to us. */
|
|---|
| 477 | mutex_lock(&THREAD->udebug.lock);
|
|---|
| 478 | while (!THREAD->udebug.active)
|
|---|
| 479 | condvar_wait(&THREAD->udebug.active_cv, &THREAD->udebug.lock);
|
|---|
| 480 | mutex_unlock(&THREAD->udebug.lock);
|
|---|
| 481 |
|
|---|
| 482 | /* Make sure the debugging session is over before proceeding. */
|
|---|
| 483 | mutex_lock(&THREAD->udebug.lock);
|
|---|
| 484 | while (THREAD->udebug.active)
|
|---|
| 485 | condvar_wait(&THREAD->udebug.active_cv, &THREAD->udebug.lock);
|
|---|
| 486 | mutex_unlock(&THREAD->udebug.lock);
|
|---|
| 487 |
|
|---|
| 488 | udebug_stoppable_end();
|
|---|
| 489 | }
|
|---|
| 490 |
|
|---|
| 491 | /** @}
|
|---|
| 492 | */
|
|---|