Rev 4377 | 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> |
4377 | svoboda | 44 | #include <print.h> |
2801 | svoboda | 45 | #include <arch.h> |
46 | |||
3026 | svoboda | 47 | |
3471 | svoboda | 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 | */ |
||
3014 | svoboda | 53 | void udebug_task_init(udebug_task_t *ut) |
54 | { |
||
3426 | svoboda | 55 | mutex_initialize(&ut->lock, MUTEX_PASSIVE); |
3014 | svoboda | 56 | ut->dt_state = UDEBUG_TS_INACTIVE; |
57 | ut->begin_call = NULL; |
||
58 | ut->not_stoppable_count = 0; |
||
59 | ut->evmask = 0; |
||
60 | } |
||
61 | |||
3471 | svoboda | 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 | */ |
||
3018 | svoboda | 67 | void udebug_thread_initialize(udebug_thread_t *ut) |
68 | { |
||
3426 | svoboda | 69 | mutex_initialize(&ut->lock, MUTEX_PASSIVE); |
3018 | svoboda | 70 | waitq_initialize(&ut->go_wq); |
3026 | svoboda | 71 | |
3018 | svoboda | 72 | ut->go_call = NULL; |
73 | ut->uspace_state = NULL; |
||
3606 | svoboda | 74 | ut->go = false; |
3018 | svoboda | 75 | ut->stoppable = true; |
3684 | svoboda | 76 | ut->active = false; |
3018 | svoboda | 77 | ut->cur_event = 0; /* none */ |
78 | } |
||
79 | |||
3471 | svoboda | 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 | */ |
||
2917 | svoboda | 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 | |||
3471 | svoboda | 101 | /** Start of stoppable section. |
102 | * |
||
103 | * A stoppable section is a section of code where if the thread can be stoped. In other words, |
||
104 | * if a STOP operation is issued, the thread is guaranteed not to execute |
||
105 | * any userspace instructions until the thread is resumed. |
||
106 | * |
||
107 | * Having stoppable sections is better than having stopping points, since |
||
108 | * a thread can be stopped even when it is blocked indefinitely in a system |
||
109 | * call (whereas it would not reach any stopping point). |
||
110 | */ |
||
2804 | svoboda | 111 | void udebug_stoppable_begin(void) |
2801 | svoboda | 112 | { |
2804 | svoboda | 113 | int nsc; |
2898 | svoboda | 114 | call_t *db_call, *go_call; |
2804 | svoboda | 115 | |
2902 | svoboda | 116 | ASSERT(THREAD); |
117 | ASSERT(TASK); |
||
118 | |||
3016 | svoboda | 119 | mutex_lock(&TASK->udebug.lock); |
2804 | svoboda | 120 | |
3014 | svoboda | 121 | nsc = --TASK->udebug.not_stoppable_count; |
2804 | svoboda | 122 | |
3032 | svoboda | 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; |
||
2804 | svoboda | 127 | |
3014 | svoboda | 128 | if (TASK->udebug.dt_state == UDEBUG_TS_BEGINNING && nsc == 0) { |
2898 | svoboda | 129 | /* |
130 | * This was the last non-stoppable thread. Reply to |
||
131 | * DEBUG_BEGIN call. |
||
132 | */ |
||
133 | |||
3014 | svoboda | 134 | db_call = TASK->udebug.begin_call; |
2902 | svoboda | 135 | ASSERT(db_call); |
136 | |||
3014 | svoboda | 137 | TASK->udebug.dt_state = UDEBUG_TS_ACTIVE; |
138 | TASK->udebug.begin_call = NULL; |
||
2804 | svoboda | 139 | |
140 | IPC_SET_RETVAL(db_call->data, 0); |
||
141 | ipc_answer(&TASK->answerbox, db_call); |
||
2898 | svoboda | 142 | |
3014 | svoboda | 143 | } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) { |
2898 | svoboda | 144 | /* |
145 | * Active debugging session |
||
146 | */ |
||
147 | |||
3684 | svoboda | 148 | if (THREAD->udebug.active == true && |
3606 | svoboda | 149 | THREAD->udebug.go == false) { |
2898 | svoboda | 150 | /* |
151 | * Thread was requested to stop - answer go call |
||
152 | */ |
||
153 | |||
154 | /* Make sure nobody takes this call away from us */ |
||
3018 | svoboda | 155 | go_call = THREAD->udebug.go_call; |
156 | THREAD->udebug.go_call = NULL; |
||
2902 | svoboda | 157 | ASSERT(go_call); |
2898 | svoboda | 158 | |
159 | IPC_SET_RETVAL(go_call->data, 0); |
||
160 | IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP); |
||
161 | |||
3018 | svoboda | 162 | THREAD->udebug.cur_event = UDEBUG_EVENT_STOP; |
2898 | svoboda | 163 | |
164 | ipc_answer(&TASK->answerbox, go_call); |
||
165 | } |
||
3032 | svoboda | 166 | } |
2898 | svoboda | 167 | |
3032 | svoboda | 168 | mutex_unlock(&THREAD->udebug.lock); |
169 | mutex_unlock(&TASK->udebug.lock); |
||
2804 | svoboda | 170 | } |
171 | |||
3471 | svoboda | 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 section). |
||
176 | */ |
||
2804 | svoboda | 177 | void udebug_stoppable_end(void) |
178 | { |
||
179 | restart: |
||
3016 | svoboda | 180 | mutex_lock(&TASK->udebug.lock); |
3026 | svoboda | 181 | mutex_lock(&THREAD->udebug.lock); |
2898 | svoboda | 182 | |
3684 | svoboda | 183 | if (THREAD->udebug.active && THREAD->udebug.go == false) { |
3014 | svoboda | 184 | TASK->udebug.begin_call = NULL; |
3026 | svoboda | 185 | mutex_unlock(&THREAD->udebug.lock); |
3016 | svoboda | 186 | mutex_unlock(&TASK->udebug.lock); |
2823 | svoboda | 187 | |
3018 | svoboda | 188 | udebug_wait_for_go(&THREAD->udebug.go_wq); |
2917 | svoboda | 189 | |
2804 | svoboda | 190 | goto restart; |
3606 | svoboda | 191 | /* Must try again - have to lose stoppability atomically. */ |
2804 | svoboda | 192 | } else { |
3014 | svoboda | 193 | ++TASK->udebug.not_stoppable_count; |
3026 | svoboda | 194 | ASSERT(THREAD->udebug.stoppable == true); |
3018 | svoboda | 195 | THREAD->udebug.stoppable = false; |
2898 | svoboda | 196 | |
3026 | svoboda | 197 | mutex_unlock(&THREAD->udebug.lock); |
3016 | svoboda | 198 | mutex_unlock(&TASK->udebug.lock); |
2801 | svoboda | 199 | } |
200 | } |
||
201 | |||
3015 | svoboda | 202 | /** Upon being scheduled to run, check if the current thread should stop. |
203 | * |
||
3611 | svoboda | 204 | * This function is called from clock(). |
3015 | svoboda | 205 | */ |
206 | void udebug_before_thread_runs(void) |
||
207 | { |
||
3611 | svoboda | 208 | /* Check if we are supposed to stop. */ |
3015 | svoboda | 209 | udebug_stoppable_begin(); |
210 | udebug_stoppable_end(); |
||
211 | } |
||
212 | |||
3471 | svoboda | 213 | /** Syscall event hook. |
214 | * |
||
215 | * Must be called before and after servicing a system call. This generates |
||
216 | * a SYSCALL_B or SYSCALL_E event, depending on the value of @a end_variant. |
||
217 | */ |
||
2805 | svoboda | 218 | void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3, |
2901 | svoboda | 219 | unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc, |
220 | bool end_variant) |
||
2801 | svoboda | 221 | { |
2805 | svoboda | 222 | call_t *call; |
2901 | svoboda | 223 | udebug_event_t etype; |
2805 | svoboda | 224 | |
2901 | svoboda | 225 | etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B; |
226 | |||
3016 | svoboda | 227 | mutex_lock(&TASK->udebug.lock); |
3026 | svoboda | 228 | mutex_lock(&THREAD->udebug.lock); |
3016 | svoboda | 229 | |
3606 | svoboda | 230 | /* Must only generate events when in debugging session and is go. */ |
3684 | svoboda | 231 | if (THREAD->udebug.active != true || THREAD->udebug.go == false || |
3014 | svoboda | 232 | (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) { |
3026 | svoboda | 233 | mutex_unlock(&THREAD->udebug.lock); |
3016 | svoboda | 234 | mutex_unlock(&TASK->udebug.lock); |
2867 | svoboda | 235 | return; |
236 | } |
||
2804 | svoboda | 237 | |
4692 | svoboda | 238 | /* Fill in the GO response. */ |
3018 | svoboda | 239 | call = THREAD->udebug.go_call; |
240 | THREAD->udebug.go_call = NULL; |
||
3016 | svoboda | 241 | |
2867 | svoboda | 242 | IPC_SET_RETVAL(call->data, 0); |
2901 | svoboda | 243 | IPC_SET_ARG1(call->data, etype); |
2867 | svoboda | 244 | IPC_SET_ARG2(call->data, id); |
245 | IPC_SET_ARG3(call->data, rc); |
||
2805 | svoboda | 246 | |
3018 | svoboda | 247 | THREAD->udebug.syscall_args[0] = a1; |
248 | THREAD->udebug.syscall_args[1] = a2; |
||
249 | THREAD->udebug.syscall_args[2] = a3; |
||
250 | THREAD->udebug.syscall_args[3] = a4; |
||
251 | THREAD->udebug.syscall_args[4] = a5; |
||
252 | THREAD->udebug.syscall_args[5] = a6; |
||
2866 | svoboda | 253 | |
2867 | svoboda | 254 | /* |
3606 | svoboda | 255 | * Make sure udebug.go is false when going to sleep |
2867 | svoboda | 256 | * in case we get woken up by DEBUG_END. (At which |
257 | * point it must be back to the initial true value). |
||
258 | */ |
||
3606 | svoboda | 259 | THREAD->udebug.go = false; |
3018 | svoboda | 260 | THREAD->udebug.cur_event = etype; |
2867 | svoboda | 261 | |
3016 | svoboda | 262 | ipc_answer(&TASK->answerbox, call); |
263 | |||
3032 | svoboda | 264 | mutex_unlock(&THREAD->udebug.lock); |
3016 | svoboda | 265 | mutex_unlock(&TASK->udebug.lock); |
266 | |||
3018 | svoboda | 267 | udebug_wait_for_go(&THREAD->udebug.go_wq); |
2867 | svoboda | 268 | } |
269 | |||
3623 | svoboda | 270 | /** Thread-creation event hook combined with attaching the thread. |
3471 | svoboda | 271 | * |
272 | * Must be called when a new userspace thread is created in the debugged |
||
3623 | svoboda | 273 | * task. Generates a THREAD_B event. Also attaches the thread @a t |
274 | * to the task @a ta. |
||
3471 | svoboda | 275 | * |
3623 | svoboda | 276 | * This is necessary to avoid a race condition where the BEGIN and THREAD_READ |
277 | * requests would be handled inbetween attaching the thread and checking it |
||
278 | * for being in a debugging session to send the THREAD_B event. We could then |
||
279 | * either miss threads or get some threads both in the thread list |
||
280 | * and get a THREAD_B event for them. |
||
281 | * |
||
3471 | svoboda | 282 | * @param t Structure of the thread being created. Not locked, as the |
283 | * thread is not executing yet. |
||
3623 | svoboda | 284 | * @param ta Task to which the thread should be attached. |
3471 | svoboda | 285 | */ |
3623 | svoboda | 286 | void udebug_thread_b_event_attach(struct thread *t, struct task *ta) |
2867 | svoboda | 287 | { |
288 | call_t *call; |
||
289 | |||
3016 | svoboda | 290 | mutex_lock(&TASK->udebug.lock); |
3026 | svoboda | 291 | mutex_lock(&THREAD->udebug.lock); |
3016 | svoboda | 292 | |
3623 | svoboda | 293 | thread_attach(t, ta); |
294 | |||
4692 | svoboda | 295 | LOG("Check state"); |
2867 | svoboda | 296 | |
297 | /* Must only generate events when in debugging session */ |
||
3684 | svoboda | 298 | if (THREAD->udebug.active != true) { |
4692 | svoboda | 299 | LOG("udebug.active: %s, udebug.go: %s", |
300 | THREAD->udebug.active ? "Yes(+)" : "No", |
||
301 | THREAD->udebug.go ? "Yes(-)" : "No"); |
||
3026 | svoboda | 302 | mutex_unlock(&THREAD->udebug.lock); |
3016 | svoboda | 303 | mutex_unlock(&TASK->udebug.lock); |
2867 | svoboda | 304 | return; |
2801 | svoboda | 305 | } |
2867 | svoboda | 306 | |
4692 | svoboda | 307 | LOG("Trigger event"); |
3018 | svoboda | 308 | call = THREAD->udebug.go_call; |
309 | THREAD->udebug.go_call = NULL; |
||
2867 | svoboda | 310 | IPC_SET_RETVAL(call->data, 0); |
2903 | svoboda | 311 | IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B); |
2867 | svoboda | 312 | IPC_SET_ARG2(call->data, (unative_t)t); |
313 | |||
314 | /* |
||
3606 | svoboda | 315 | * Make sure udebug.go is false when going to sleep |
2867 | svoboda | 316 | * in case we get woken up by DEBUG_END. (At which |
317 | * point it must be back to the initial true value). |
||
318 | */ |
||
3606 | svoboda | 319 | THREAD->udebug.go = false; |
3018 | svoboda | 320 | THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B; |
2867 | svoboda | 321 | |
3016 | svoboda | 322 | ipc_answer(&TASK->answerbox, call); |
2867 | svoboda | 323 | |
3032 | svoboda | 324 | mutex_unlock(&THREAD->udebug.lock); |
3016 | svoboda | 325 | mutex_unlock(&TASK->udebug.lock); |
326 | |||
4692 | svoboda | 327 | LOG("Wait for Go"); |
3018 | svoboda | 328 | udebug_wait_for_go(&THREAD->udebug.go_wq); |
2801 | svoboda | 329 | } |
330 | |||
3471 | svoboda | 331 | /** Thread-termination event hook. |
332 | * |
||
333 | * Must be called when the current thread is terminating. |
||
334 | * Generates a THREAD_E event. |
||
335 | */ |
||
2903 | svoboda | 336 | void udebug_thread_e_event(void) |
337 | { |
||
338 | call_t *call; |
||
339 | |||
3016 | svoboda | 340 | mutex_lock(&TASK->udebug.lock); |
3026 | svoboda | 341 | mutex_lock(&THREAD->udebug.lock); |
3016 | svoboda | 342 | |
4692 | svoboda | 343 | LOG("Check state"); |
2903 | svoboda | 344 | |
3606 | svoboda | 345 | /* Must only generate events when in debugging session. */ |
3684 | svoboda | 346 | if (THREAD->udebug.active != true) { |
4692 | svoboda | 347 | LOG("udebug.active: %s, udebug.go: %s", |
348 | THREAD->udebug.active ? "Yes" : "No", |
||
349 | THREAD->udebug.go ? "Yes" : "No"); |
||
3026 | svoboda | 350 | mutex_unlock(&THREAD->udebug.lock); |
3016 | svoboda | 351 | mutex_unlock(&TASK->udebug.lock); |
2903 | svoboda | 352 | return; |
353 | } |
||
354 | |||
4692 | svoboda | 355 | LOG("Trigger event"); |
3018 | svoboda | 356 | call = THREAD->udebug.go_call; |
357 | THREAD->udebug.go_call = NULL; |
||
2903 | svoboda | 358 | IPC_SET_RETVAL(call->data, 0); |
359 | IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E); |
||
360 | |||
3606 | svoboda | 361 | /* Prevent any further debug activity in thread. */ |
3684 | svoboda | 362 | THREAD->udebug.active = false; |
3018 | svoboda | 363 | THREAD->udebug.cur_event = 0; /* none */ |
3606 | svoboda | 364 | THREAD->udebug.go = false; /* set to initial value */ |
3016 | svoboda | 365 | |
366 | ipc_answer(&TASK->answerbox, call); |
||
2903 | svoboda | 367 | |
3032 | svoboda | 368 | mutex_unlock(&THREAD->udebug.lock); |
3016 | svoboda | 369 | mutex_unlock(&TASK->udebug.lock); |
2903 | svoboda | 370 | |
3606 | svoboda | 371 | /* |
372 | * This event does not sleep - debugging has finished |
||
373 | * in this thread. |
||
374 | */ |
||
2903 | svoboda | 375 | } |
376 | |||
2921 | svoboda | 377 | static void breakpoint_trap_event(uintptr_t addr, udebug_event_t etype) |
2918 | svoboda | 378 | { |
379 | call_t *call; |
||
2903 | svoboda | 380 | |
3016 | svoboda | 381 | mutex_lock(&TASK->udebug.lock); |
3026 | svoboda | 382 | mutex_lock(&THREAD->udebug.lock); |
3016 | svoboda | 383 | |
3606 | svoboda | 384 | /* Must only generate events when in debugging session and have go. */ |
3684 | svoboda | 385 | if (THREAD->udebug.active != true || THREAD->udebug.go == false) { |
3026 | svoboda | 386 | mutex_unlock(&THREAD->udebug.lock); |
3016 | svoboda | 387 | mutex_unlock(&TASK->udebug.lock); |
2918 | svoboda | 388 | return; |
389 | } |
||
390 | |||
3030 | svoboda | 391 | /* Verify that the event is enabled */ |
392 | if ((TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) { |
||
393 | mutex_unlock(&THREAD->udebug.lock); |
||
394 | mutex_unlock(&TASK->udebug.lock); |
||
395 | return; |
||
396 | } |
||
397 | |||
3611 | svoboda | 398 | LOG("udebug_breakpoint/trap_event\n"); |
3018 | svoboda | 399 | call = THREAD->udebug.go_call; |
400 | THREAD->udebug.go_call = NULL; |
||
3016 | svoboda | 401 | |
2918 | svoboda | 402 | IPC_SET_RETVAL(call->data, 0); |
403 | IPC_SET_ARG1(call->data, etype); |
||
404 | IPC_SET_ARG2(call->data, addr); |
||
405 | |||
406 | /* |
||
3606 | svoboda | 407 | * Make sure udebug.go is false when going to sleep |
2918 | svoboda | 408 | * in case we get woken up by DEBUG_END. (At which |
409 | * point it must be back to the initial true value). |
||
410 | */ |
||
3606 | svoboda | 411 | THREAD->udebug.go = false; |
3018 | svoboda | 412 | THREAD->udebug.cur_event = etype; |
3026 | svoboda | 413 | |
3611 | svoboda | 414 | LOG("- send answer\n"); |
3032 | svoboda | 415 | ipc_answer(&TASK->answerbox, call); |
2918 | svoboda | 416 | |
3032 | svoboda | 417 | mutex_unlock(&THREAD->udebug.lock); |
3016 | svoboda | 418 | mutex_unlock(&TASK->udebug.lock); |
2918 | svoboda | 419 | |
3018 | svoboda | 420 | udebug_wait_for_go(&THREAD->udebug.go_wq); |
2918 | svoboda | 421 | } |
422 | |||
2921 | svoboda | 423 | void udebug_breakpoint_event(uintptr_t addr) |
424 | { |
||
425 | breakpoint_trap_event(addr, UDEBUG_EVENT_BREAKPOINT); |
||
426 | } |
||
2918 | svoboda | 427 | |
2921 | svoboda | 428 | void udebug_trap_event(uintptr_t addr) |
429 | { |
||
430 | breakpoint_trap_event(addr, UDEBUG_EVENT_TRAP); |
||
431 | } |
||
432 | |||
2870 | svoboda | 433 | /** |
434 | * Terminate task debugging session. |
||
435 | * |
||
3471 | svoboda | 436 | * Gracefully terminates the debugging session for a task. If the debugger |
437 | * is still waiting for events on some threads, it will receive a |
||
438 | * FINISHED event for each of them. |
||
439 | * |
||
440 | * @param ta Task structure. ta->udebug.lock must be already locked. |
||
441 | * @return Zero on success or negative error code. |
||
2870 | svoboda | 442 | */ |
443 | int udebug_task_cleanup(struct task *ta) |
||
444 | { |
||
445 | thread_t *t; |
||
446 | link_t *cur; |
||
447 | int flags; |
||
3016 | svoboda | 448 | ipl_t ipl; |
2867 | svoboda | 449 | |
3032 | svoboda | 450 | if (ta->udebug.dt_state != UDEBUG_TS_BEGINNING && |
3014 | svoboda | 451 | ta->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
2870 | svoboda | 452 | return EINVAL; |
453 | } |
||
454 | |||
4692 | svoboda | 455 | LOG("Task %" PRIu64, ta->taskid); |
456 | |||
2870 | svoboda | 457 | /* Finish debugging of all userspace threads */ |
458 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
||
459 | t = list_get_instance(cur, thread_t, th_link); |
||
460 | |||
3026 | svoboda | 461 | mutex_lock(&t->udebug.lock); |
462 | |||
3016 | svoboda | 463 | ipl = interrupts_disable(); |
2870 | svoboda | 464 | spinlock_lock(&t->lock); |
465 | |||
466 | flags = t->flags; |
||
467 | |||
468 | spinlock_unlock(&t->lock); |
||
3026 | svoboda | 469 | interrupts_restore(ipl); |
2870 | svoboda | 470 | |
3606 | svoboda | 471 | /* Only process userspace threads. */ |
2870 | svoboda | 472 | if ((flags & THREAD_FLAG_USPACE) != 0) { |
3606 | svoboda | 473 | /* Prevent any further debug activity in thread. */ |
3684 | svoboda | 474 | t->udebug.active = false; |
3018 | svoboda | 475 | t->udebug.cur_event = 0; /* none */ |
2870 | svoboda | 476 | |
3606 | svoboda | 477 | /* Is the thread still go? */ |
478 | if (t->udebug.go == true) { |
||
2870 | svoboda | 479 | /* |
3684 | svoboda | 480 | * Yes, so clear go. As active == false, |
2870 | svoboda | 481 | * this doesn't affect anything. |
482 | */ |
||
3606 | svoboda | 483 | t->udebug.go = false; |
2870 | svoboda | 484 | |
485 | /* Answer GO call */ |
||
4692 | svoboda | 486 | LOG("Answer GO call with EVENT_FINISHED."); |
3018 | svoboda | 487 | IPC_SET_RETVAL(t->udebug.go_call->data, 0); |
3471 | svoboda | 488 | IPC_SET_ARG1(t->udebug.go_call->data, |
489 | UDEBUG_EVENT_FINISHED); |
||
3026 | svoboda | 490 | |
3018 | svoboda | 491 | ipc_answer(&ta->answerbox, t->udebug.go_call); |
492 | t->udebug.go_call = NULL; |
||
2870 | svoboda | 493 | } else { |
494 | /* |
||
495 | * Debug_stop is already at initial value. |
||
496 | * Yet this means the thread needs waking up. |
||
497 | */ |
||
498 | |||
499 | /* |
||
500 | * t's lock must not be held when calling |
||
501 | * waitq_wakeup. |
||
502 | */ |
||
3018 | svoboda | 503 | waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST); |
2870 | svoboda | 504 | } |
505 | } |
||
3026 | svoboda | 506 | mutex_unlock(&t->udebug.lock); |
2870 | svoboda | 507 | } |
508 | |||
3014 | svoboda | 509 | ta->udebug.dt_state = UDEBUG_TS_INACTIVE; |
510 | ta->udebug.debugger = NULL; |
||
2870 | svoboda | 511 | |
512 | return 0; |
||
513 | } |
||
514 | |||
515 | |||
2801 | svoboda | 516 | /** @} |
517 | */ |