Rev 3471 | Rev 3611 | 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 | |||
| 2801 | svoboda | 29 | /** @addtogroup generic |
| 30 | * @{ |
||
| 31 | */ |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @file |
||
| 3471 | svoboda | 35 | * @brief Udebug hooks and data structure management. |
| 3031 | svoboda | 36 | * |
| 3471 | svoboda | 37 | * Udebug is an interface that makes userspace debuggers possible. |
| 2801 | svoboda | 38 | */ |
| 39 | |||
| 40 | #include <synch/waitq.h> |
||
| 3471 | svoboda | 41 | #include <debug.h> |
| 2813 | svoboda | 42 | #include <udebug/udebug.h> |
| 2870 | svoboda | 43 | #include <errno.h> |
| 2801 | svoboda | 44 | #include <arch.h> |
| 45 | |||
| 3026 | svoboda | 46 | |
| 3471 | svoboda | 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 | */ |
||
| 3014 | svoboda | 52 | void udebug_task_init(udebug_task_t *ut) |
| 53 | { |
||
| 3426 | svoboda | 54 | mutex_initialize(&ut->lock, MUTEX_PASSIVE); |
| 3014 | svoboda | 55 | ut->dt_state = UDEBUG_TS_INACTIVE; |
| 56 | ut->begin_call = NULL; |
||
| 57 | ut->not_stoppable_count = 0; |
||
| 58 | ut->evmask = 0; |
||
| 59 | } |
||
| 60 | |||
| 3471 | svoboda | 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 | */ |
||
| 3018 | svoboda | 66 | void udebug_thread_initialize(udebug_thread_t *ut) |
| 67 | { |
||
| 3426 | svoboda | 68 | mutex_initialize(&ut->lock, MUTEX_PASSIVE); |
| 3018 | svoboda | 69 | waitq_initialize(&ut->go_wq); |
| 3026 | svoboda | 70 | |
| 3018 | svoboda | 71 | ut->go_call = NULL; |
| 72 | ut->uspace_state = NULL; |
||
| 3606 | svoboda | 73 | ut->go = false; |
| 3018 | svoboda | 74 | ut->stoppable = true; |
| 75 | ut->debug_active = false; |
||
| 76 | ut->cur_event = 0; /* none */ |
||
| 77 | } |
||
| 78 | |||
| 3471 | svoboda | 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 | */ |
||
| 2917 | svoboda | 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 | |||
| 3030 | svoboda | 100 | /** Do a preliminary check that a debugging session is in progress. |
| 101 | * |
||
| 3471 | svoboda | 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. |
||
| 3030 | svoboda | 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 | |||
| 3471 | svoboda | 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 | */ |
||
| 2804 | svoboda | 131 | void udebug_stoppable_begin(void) |
| 2801 | svoboda | 132 | { |
| 2804 | svoboda | 133 | int nsc; |
| 2898 | svoboda | 134 | call_t *db_call, *go_call; |
| 2804 | svoboda | 135 | |
| 2902 | svoboda | 136 | ASSERT(THREAD); |
| 137 | ASSERT(TASK); |
||
| 138 | |||
| 3030 | svoboda | 139 | /* Early check for undebugged tasks */ |
| 140 | if (!udebug_thread_precheck()) { |
||
| 141 | return; |
||
| 142 | } |
||
| 143 | |||
| 3016 | svoboda | 144 | mutex_lock(&TASK->udebug.lock); |
| 2804 | svoboda | 145 | |
| 3014 | svoboda | 146 | nsc = --TASK->udebug.not_stoppable_count; |
| 2804 | svoboda | 147 | |
| 3032 | svoboda | 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; |
||
| 2804 | svoboda | 152 | |
| 3014 | svoboda | 153 | if (TASK->udebug.dt_state == UDEBUG_TS_BEGINNING && nsc == 0) { |
| 2898 | svoboda | 154 | /* |
| 155 | * This was the last non-stoppable thread. Reply to |
||
| 156 | * DEBUG_BEGIN call. |
||
| 157 | */ |
||
| 158 | |||
| 3014 | svoboda | 159 | db_call = TASK->udebug.begin_call; |
| 2902 | svoboda | 160 | ASSERT(db_call); |
| 161 | |||
| 3014 | svoboda | 162 | TASK->udebug.dt_state = UDEBUG_TS_ACTIVE; |
| 163 | TASK->udebug.begin_call = NULL; |
||
| 2804 | svoboda | 164 | |
| 165 | IPC_SET_RETVAL(db_call->data, 0); |
||
| 166 | ipc_answer(&TASK->answerbox, db_call); |
||
| 2898 | svoboda | 167 | |
| 3014 | svoboda | 168 | } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) { |
| 2898 | svoboda | 169 | /* |
| 170 | * Active debugging session |
||
| 171 | */ |
||
| 172 | |||
| 3606 | svoboda | 173 | if (THREAD->udebug.debug_active == true && |
| 174 | THREAD->udebug.go == false) { |
||
| 2898 | svoboda | 175 | /* |
| 176 | * Thread was requested to stop - answer go call |
||
| 177 | */ |
||
| 178 | |||
| 179 | /* Make sure nobody takes this call away from us */ |
||
| 3018 | svoboda | 180 | go_call = THREAD->udebug.go_call; |
| 181 | THREAD->udebug.go_call = NULL; |
||
| 2902 | svoboda | 182 | ASSERT(go_call); |
| 2898 | svoboda | 183 | |
| 184 | IPC_SET_RETVAL(go_call->data, 0); |
||
| 185 | IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP); |
||
| 186 | |||
| 3018 | svoboda | 187 | THREAD->udebug.cur_event = UDEBUG_EVENT_STOP; |
| 2898 | svoboda | 188 | |
| 189 | ipc_answer(&TASK->answerbox, go_call); |
||
| 190 | } |
||
| 3032 | svoboda | 191 | } |
| 2898 | svoboda | 192 | |
| 3032 | svoboda | 193 | mutex_unlock(&THREAD->udebug.lock); |
| 194 | mutex_unlock(&TASK->udebug.lock); |
||
| 2804 | svoboda | 195 | } |
| 196 | |||
| 3471 | svoboda | 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 | */ |
||
| 2804 | svoboda | 202 | void udebug_stoppable_end(void) |
| 203 | { |
||
| 3030 | svoboda | 204 | /* Early check for undebugged tasks */ |
| 205 | if (!udebug_thread_precheck()) { |
||
| 206 | return; |
||
| 207 | } |
||
| 208 | |||
| 2804 | svoboda | 209 | restart: |
| 3016 | svoboda | 210 | mutex_lock(&TASK->udebug.lock); |
| 3026 | svoboda | 211 | mutex_lock(&THREAD->udebug.lock); |
| 2898 | svoboda | 212 | |
| 3018 | svoboda | 213 | if (THREAD->udebug.debug_active && |
| 3606 | svoboda | 214 | THREAD->udebug.go == false) { |
| 3014 | svoboda | 215 | TASK->udebug.begin_call = NULL; |
| 3026 | svoboda | 216 | mutex_unlock(&THREAD->udebug.lock); |
| 3016 | svoboda | 217 | mutex_unlock(&TASK->udebug.lock); |
| 2823 | svoboda | 218 | |
| 3018 | svoboda | 219 | udebug_wait_for_go(&THREAD->udebug.go_wq); |
| 2917 | svoboda | 220 | |
| 2804 | svoboda | 221 | goto restart; |
| 3606 | svoboda | 222 | /* Must try again - have to lose stoppability atomically. */ |
| 2804 | svoboda | 223 | } else { |
| 3014 | svoboda | 224 | ++TASK->udebug.not_stoppable_count; |
| 3026 | svoboda | 225 | ASSERT(THREAD->udebug.stoppable == true); |
| 3018 | svoboda | 226 | THREAD->udebug.stoppable = false; |
| 2898 | svoboda | 227 | |
| 3026 | svoboda | 228 | mutex_unlock(&THREAD->udebug.lock); |
| 3016 | svoboda | 229 | mutex_unlock(&TASK->udebug.lock); |
| 2801 | svoboda | 230 | } |
| 231 | } |
||
| 232 | |||
| 3015 | svoboda | 233 | /** Upon being scheduled to run, check if the current thread should stop. |
| 234 | * |
||
| 235 | * This function is called from clock(). Preemption is enabled. |
||
| 236 | * interrupts are disabled, but since this is called after |
||
| 237 | * being scheduled-in, we can enable them, if we're careful enough |
||
| 3026 | svoboda | 238 | * not to allow arbitrary recursion or deadlock with the thread context. |
| 3015 | svoboda | 239 | */ |
| 240 | void udebug_before_thread_runs(void) |
||
| 241 | { |
||
| 242 | ipl_t ipl; |
||
| 243 | |||
| 3108 | svoboda | 244 | return; |
| 3026 | svoboda | 245 | |
| 3015 | svoboda | 246 | ipl = interrupts_enable(); |
| 247 | |||
| 3016 | svoboda | 248 | /* Now we're free to do whatever we need (lock mutexes, sleep, etc.) */ |
| 3015 | svoboda | 249 | |
| 250 | /* Check if we're supposed to stop */ |
||
| 251 | udebug_stoppable_begin(); |
||
| 252 | udebug_stoppable_end(); |
||
| 253 | |||
| 254 | interrupts_restore(ipl); |
||
| 255 | } |
||
| 256 | |||
| 3471 | svoboda | 257 | /** Syscall event hook. |
| 258 | * |
||
| 259 | * Must be called before and after servicing a system call. This generates |
||
| 260 | * a SYSCALL_B or SYSCALL_E event, depending on the value of @a end_variant. |
||
| 261 | */ |
||
| 2805 | svoboda | 262 | void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3, |
| 2901 | svoboda | 263 | unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc, |
| 264 | bool end_variant) |
||
| 2801 | svoboda | 265 | { |
| 2805 | svoboda | 266 | call_t *call; |
| 2901 | svoboda | 267 | udebug_event_t etype; |
| 2805 | svoboda | 268 | |
| 2901 | svoboda | 269 | etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B; |
| 270 | |||
| 3030 | svoboda | 271 | /* Early check for undebugged tasks */ |
| 272 | if (!udebug_thread_precheck()) { |
||
| 273 | return; |
||
| 274 | } |
||
| 275 | |||
| 3016 | svoboda | 276 | mutex_lock(&TASK->udebug.lock); |
| 3026 | svoboda | 277 | mutex_lock(&THREAD->udebug.lock); |
| 3016 | svoboda | 278 | |
| 3606 | svoboda | 279 | /* Must only generate events when in debugging session and is go. */ |
| 3018 | svoboda | 280 | if (THREAD->udebug.debug_active != true || |
| 3606 | svoboda | 281 | THREAD->udebug.go == false || |
| 3014 | svoboda | 282 | (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) { |
| 3026 | svoboda | 283 | mutex_unlock(&THREAD->udebug.lock); |
| 3016 | svoboda | 284 | mutex_unlock(&TASK->udebug.lock); |
| 2867 | svoboda | 285 | return; |
| 286 | } |
||
| 2804 | svoboda | 287 | |
| 3424 | svoboda | 288 | //printf("udebug_syscall_event\n"); |
| 3018 | svoboda | 289 | call = THREAD->udebug.go_call; |
| 290 | THREAD->udebug.go_call = NULL; |
||
| 3016 | svoboda | 291 | |
| 2867 | svoboda | 292 | IPC_SET_RETVAL(call->data, 0); |
| 2901 | svoboda | 293 | IPC_SET_ARG1(call->data, etype); |
| 2867 | svoboda | 294 | IPC_SET_ARG2(call->data, id); |
| 295 | IPC_SET_ARG3(call->data, rc); |
||
| 3424 | svoboda | 296 | //printf("udebug_syscall_event/ipc_answer\n"); |
| 2805 | svoboda | 297 | |
| 3018 | svoboda | 298 | THREAD->udebug.syscall_args[0] = a1; |
| 299 | THREAD->udebug.syscall_args[1] = a2; |
||
| 300 | THREAD->udebug.syscall_args[2] = a3; |
||
| 301 | THREAD->udebug.syscall_args[3] = a4; |
||
| 302 | THREAD->udebug.syscall_args[4] = a5; |
||
| 303 | THREAD->udebug.syscall_args[5] = a6; |
||
| 2866 | svoboda | 304 | |
| 2867 | svoboda | 305 | /* |
| 3606 | svoboda | 306 | * Make sure udebug.go is false when going to sleep |
| 2867 | svoboda | 307 | * in case we get woken up by DEBUG_END. (At which |
| 308 | * point it must be back to the initial true value). |
||
| 309 | */ |
||
| 3606 | svoboda | 310 | THREAD->udebug.go = false; |
| 3018 | svoboda | 311 | THREAD->udebug.cur_event = etype; |
| 2867 | svoboda | 312 | |
| 3016 | svoboda | 313 | ipc_answer(&TASK->answerbox, call); |
| 314 | |||
| 3032 | svoboda | 315 | mutex_unlock(&THREAD->udebug.lock); |
| 3016 | svoboda | 316 | mutex_unlock(&TASK->udebug.lock); |
| 317 | |||
| 3018 | svoboda | 318 | udebug_wait_for_go(&THREAD->udebug.go_wq); |
| 2867 | svoboda | 319 | } |
| 320 | |||
| 3471 | svoboda | 321 | /** Thread-creation event hook. |
| 322 | * |
||
| 323 | * Must be called when a new userspace thread is created in the debugged |
||
| 324 | * task. Generates a THREAD_B event. |
||
| 325 | * |
||
| 326 | * @param t Structure of the thread being created. Not locked, as the |
||
| 327 | * thread is not executing yet. |
||
| 328 | */ |
||
| 2903 | svoboda | 329 | void udebug_thread_b_event(struct thread *t) |
| 2867 | svoboda | 330 | { |
| 331 | call_t *call; |
||
| 332 | |||
| 3016 | svoboda | 333 | mutex_lock(&TASK->udebug.lock); |
| 3026 | svoboda | 334 | mutex_lock(&THREAD->udebug.lock); |
| 3016 | svoboda | 335 | |
| 3471 | svoboda | 336 | LOG("udebug_thread_b_event\n"); |
| 337 | LOG("- check state\n"); |
||
| 2867 | svoboda | 338 | |
| 339 | /* Must only generate events when in debugging session */ |
||
| 3018 | svoboda | 340 | if (THREAD->udebug.debug_active != true) { |
| 3606 | svoboda | 341 | LOG("- debug_active: %s, udebug.go: %s\n", |
| 3018 | svoboda | 342 | THREAD->udebug.debug_active ? "yes(+)" : "no(-)", |
| 3606 | svoboda | 343 | THREAD->udebug.go ? "yes(-)" : "no(+)"); |
| 3026 | svoboda | 344 | mutex_unlock(&THREAD->udebug.lock); |
| 3016 | svoboda | 345 | mutex_unlock(&TASK->udebug.lock); |
| 2867 | svoboda | 346 | return; |
| 2801 | svoboda | 347 | } |
| 2867 | svoboda | 348 | |
| 3471 | svoboda | 349 | LOG("- trigger event\n"); |
| 2867 | svoboda | 350 | |
| 3018 | svoboda | 351 | call = THREAD->udebug.go_call; |
| 352 | THREAD->udebug.go_call = NULL; |
||
| 2867 | svoboda | 353 | IPC_SET_RETVAL(call->data, 0); |
| 2903 | svoboda | 354 | IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B); |
| 2867 | svoboda | 355 | IPC_SET_ARG2(call->data, (unative_t)t); |
| 356 | |||
| 357 | /* |
||
| 3606 | svoboda | 358 | * Make sure udebug.go is false when going to sleep |
| 2867 | svoboda | 359 | * in case we get woken up by DEBUG_END. (At which |
| 360 | * point it must be back to the initial true value). |
||
| 361 | */ |
||
| 3606 | svoboda | 362 | THREAD->udebug.go = false; |
| 3018 | svoboda | 363 | THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B; |
| 2867 | svoboda | 364 | |
| 3016 | svoboda | 365 | ipc_answer(&TASK->answerbox, call); |
| 2867 | svoboda | 366 | |
| 3032 | svoboda | 367 | mutex_unlock(&THREAD->udebug.lock); |
| 3016 | svoboda | 368 | mutex_unlock(&TASK->udebug.lock); |
| 369 | |||
| 3471 | svoboda | 370 | LOG("- sleep\n"); |
| 3018 | svoboda | 371 | udebug_wait_for_go(&THREAD->udebug.go_wq); |
| 2801 | svoboda | 372 | } |
| 373 | |||
| 3471 | svoboda | 374 | /** Thread-termination event hook. |
| 375 | * |
||
| 376 | * Must be called when the current thread is terminating. |
||
| 377 | * Generates a THREAD_E event. |
||
| 378 | */ |
||
| 2903 | svoboda | 379 | void udebug_thread_e_event(void) |
| 380 | { |
||
| 381 | call_t *call; |
||
| 382 | |||
| 3016 | svoboda | 383 | mutex_lock(&TASK->udebug.lock); |
| 3026 | svoboda | 384 | mutex_lock(&THREAD->udebug.lock); |
| 3016 | svoboda | 385 | |
| 3471 | svoboda | 386 | LOG("udebug_thread_e_event\n"); |
| 387 | LOG("- check state\n"); |
||
| 2903 | svoboda | 388 | |
| 3606 | svoboda | 389 | /* Must only generate events when in debugging session. */ |
| 3018 | svoboda | 390 | if (THREAD->udebug.debug_active != true) { |
| 3606 | svoboda | 391 | /* printf("- debug_active: %s, udebug.go: %s\n", |
| 3018 | svoboda | 392 | THREAD->udebug.debug_active ? "yes(+)" : "no(-)", |
| 3606 | svoboda | 393 | THREAD->udebug.go ? "yes(-)" : "no(+)");*/ |
| 3026 | svoboda | 394 | mutex_unlock(&THREAD->udebug.lock); |
| 3016 | svoboda | 395 | mutex_unlock(&TASK->udebug.lock); |
| 2903 | svoboda | 396 | return; |
| 397 | } |
||
| 398 | |||
| 3471 | svoboda | 399 | LOG("- trigger event\n"); |
| 2903 | svoboda | 400 | |
| 3018 | svoboda | 401 | call = THREAD->udebug.go_call; |
| 402 | THREAD->udebug.go_call = NULL; |
||
| 2903 | svoboda | 403 | IPC_SET_RETVAL(call->data, 0); |
| 404 | IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E); |
||
| 405 | |||
| 3606 | svoboda | 406 | /* Prevent any further debug activity in thread. */ |
| 3018 | svoboda | 407 | THREAD->udebug.debug_active = false; |
| 408 | THREAD->udebug.cur_event = 0; /* none */ |
||
| 3606 | svoboda | 409 | THREAD->udebug.go = false; /* set to initial value */ |
| 3016 | svoboda | 410 | |
| 411 | ipc_answer(&TASK->answerbox, call); |
||
| 2903 | svoboda | 412 | |
| 3032 | svoboda | 413 | mutex_unlock(&THREAD->udebug.lock); |
| 3016 | svoboda | 414 | mutex_unlock(&TASK->udebug.lock); |
| 2903 | svoboda | 415 | |
| 3606 | svoboda | 416 | /* |
| 417 | * This event does not sleep - debugging has finished |
||
| 418 | * in this thread. |
||
| 419 | */ |
||
| 2903 | svoboda | 420 | } |
| 421 | |||
| 2921 | svoboda | 422 | static void breakpoint_trap_event(uintptr_t addr, udebug_event_t etype) |
| 2918 | svoboda | 423 | { |
| 424 | call_t *call; |
||
| 2903 | svoboda | 425 | |
| 3016 | svoboda | 426 | mutex_lock(&TASK->udebug.lock); |
| 3026 | svoboda | 427 | mutex_lock(&THREAD->udebug.lock); |
| 3016 | svoboda | 428 | |
| 3606 | svoboda | 429 | /* Must only generate events when in debugging session and have go. */ |
| 3018 | svoboda | 430 | if (THREAD->udebug.debug_active != true || |
| 3606 | svoboda | 431 | THREAD->udebug.go == false) { |
| 3026 | svoboda | 432 | mutex_unlock(&THREAD->udebug.lock); |
| 3016 | svoboda | 433 | mutex_unlock(&TASK->udebug.lock); |
| 2918 | svoboda | 434 | return; |
| 435 | } |
||
| 436 | |||
| 3030 | svoboda | 437 | /* Verify that the event is enabled */ |
| 438 | if ((TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) { |
||
| 439 | mutex_unlock(&THREAD->udebug.lock); |
||
| 440 | mutex_unlock(&TASK->udebug.lock); |
||
| 441 | return; |
||
| 442 | } |
||
| 443 | |||
| 3424 | svoboda | 444 | printf("udebug_breakpoint/trap_event\n"); |
| 3018 | svoboda | 445 | call = THREAD->udebug.go_call; |
| 446 | THREAD->udebug.go_call = NULL; |
||
| 3016 | svoboda | 447 | |
| 2918 | svoboda | 448 | IPC_SET_RETVAL(call->data, 0); |
| 449 | IPC_SET_ARG1(call->data, etype); |
||
| 450 | IPC_SET_ARG2(call->data, addr); |
||
| 451 | |||
| 452 | /* |
||
| 3606 | svoboda | 453 | * Make sure udebug.go is false when going to sleep |
| 2918 | svoboda | 454 | * in case we get woken up by DEBUG_END. (At which |
| 455 | * point it must be back to the initial true value). |
||
| 456 | */ |
||
| 3606 | svoboda | 457 | THREAD->udebug.go = false; |
| 3018 | svoboda | 458 | THREAD->udebug.cur_event = etype; |
| 3026 | svoboda | 459 | |
| 3424 | svoboda | 460 | printf("- send answer\n"); |
| 3032 | svoboda | 461 | ipc_answer(&TASK->answerbox, call); |
| 2918 | svoboda | 462 | |
| 3032 | svoboda | 463 | mutex_unlock(&THREAD->udebug.lock); |
| 3016 | svoboda | 464 | mutex_unlock(&TASK->udebug.lock); |
| 2918 | svoboda | 465 | |
| 3018 | svoboda | 466 | udebug_wait_for_go(&THREAD->udebug.go_wq); |
| 2918 | svoboda | 467 | } |
| 468 | |||
| 2921 | svoboda | 469 | void udebug_breakpoint_event(uintptr_t addr) |
| 470 | { |
||
| 471 | breakpoint_trap_event(addr, UDEBUG_EVENT_BREAKPOINT); |
||
| 472 | } |
||
| 2918 | svoboda | 473 | |
| 2921 | svoboda | 474 | void udebug_trap_event(uintptr_t addr) |
| 475 | { |
||
| 476 | breakpoint_trap_event(addr, UDEBUG_EVENT_TRAP); |
||
| 477 | } |
||
| 478 | |||
| 2870 | svoboda | 479 | /** |
| 480 | * Terminate task debugging session. |
||
| 481 | * |
||
| 3471 | svoboda | 482 | * Gracefully terminates the debugging session for a task. If the debugger |
| 483 | * is still waiting for events on some threads, it will receive a |
||
| 484 | * FINISHED event for each of them. |
||
| 485 | * |
||
| 486 | * @param ta Task structure. ta->udebug.lock must be already locked. |
||
| 487 | * @return Zero on success or negative error code. |
||
| 2870 | svoboda | 488 | */ |
| 489 | int udebug_task_cleanup(struct task *ta) |
||
| 490 | { |
||
| 491 | thread_t *t; |
||
| 492 | link_t *cur; |
||
| 493 | int flags; |
||
| 3016 | svoboda | 494 | ipl_t ipl; |
| 2867 | svoboda | 495 | |
| 3471 | svoboda | 496 | LOG("udebug_task_cleanup()\n"); |
| 497 | LOG("task %" PRIu64 "\n", ta->taskid); |
||
| 2870 | svoboda | 498 | |
| 3032 | svoboda | 499 | if (ta->udebug.dt_state != UDEBUG_TS_BEGINNING && |
| 3014 | svoboda | 500 | ta->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
| 3471 | svoboda | 501 | LOG("udebug_task_cleanup(): task not being debugged\n"); |
| 2870 | svoboda | 502 | return EINVAL; |
| 503 | } |
||
| 504 | |||
| 505 | /* Finish debugging of all userspace threads */ |
||
| 506 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
||
| 507 | t = list_get_instance(cur, thread_t, th_link); |
||
| 508 | |||
| 3026 | svoboda | 509 | mutex_lock(&t->udebug.lock); |
| 510 | |||
| 3016 | svoboda | 511 | ipl = interrupts_disable(); |
| 2870 | svoboda | 512 | spinlock_lock(&t->lock); |
| 513 | |||
| 514 | flags = t->flags; |
||
| 515 | |||
| 516 | spinlock_unlock(&t->lock); |
||
| 3026 | svoboda | 517 | interrupts_restore(ipl); |
| 2870 | svoboda | 518 | |
| 3606 | svoboda | 519 | /* Only process userspace threads. */ |
| 2870 | svoboda | 520 | if ((flags & THREAD_FLAG_USPACE) != 0) { |
| 3606 | svoboda | 521 | /* Prevent any further debug activity in thread. */ |
| 3018 | svoboda | 522 | t->udebug.debug_active = false; |
| 523 | t->udebug.cur_event = 0; /* none */ |
||
| 2870 | svoboda | 524 | |
| 3606 | svoboda | 525 | /* Is the thread still go? */ |
| 526 | if (t->udebug.go == true) { |
||
| 2870 | svoboda | 527 | /* |
| 528 | * Yes, so clear go. As debug_active == false, |
||
| 529 | * this doesn't affect anything. |
||
| 530 | */ |
||
| 3606 | svoboda | 531 | t->udebug.go = false; |
| 2870 | svoboda | 532 | |
| 533 | /* Answer GO call */ |
||
| 3471 | svoboda | 534 | LOG("answer GO call with EVENT_FINISHED\n"); |
| 3018 | svoboda | 535 | IPC_SET_RETVAL(t->udebug.go_call->data, 0); |
| 3471 | svoboda | 536 | IPC_SET_ARG1(t->udebug.go_call->data, |
| 537 | UDEBUG_EVENT_FINISHED); |
||
| 3026 | svoboda | 538 | |
| 3018 | svoboda | 539 | ipc_answer(&ta->answerbox, t->udebug.go_call); |
| 540 | t->udebug.go_call = NULL; |
||
| 2870 | svoboda | 541 | } else { |
| 542 | /* |
||
| 543 | * Debug_stop is already at initial value. |
||
| 544 | * Yet this means the thread needs waking up. |
||
| 545 | */ |
||
| 546 | |||
| 547 | /* |
||
| 548 | * t's lock must not be held when calling |
||
| 549 | * waitq_wakeup. |
||
| 550 | */ |
||
| 3018 | svoboda | 551 | waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST); |
| 2870 | svoboda | 552 | } |
| 553 | } |
||
| 3026 | svoboda | 554 | mutex_unlock(&t->udebug.lock); |
| 2870 | svoboda | 555 | } |
| 556 | |||
| 3014 | svoboda | 557 | ta->udebug.dt_state = UDEBUG_TS_INACTIVE; |
| 558 | ta->udebug.debugger = NULL; |
||
| 2870 | svoboda | 559 | |
| 560 | return 0; |
||
| 561 | } |
||
| 562 | |||
| 563 | |||
| 2801 | svoboda | 564 | /** @} |
| 565 | */ |