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