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