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