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