HelenOS sources
This source file includes following definitions.
- udebug_task_init
- udebug_thread_initialize
- udebug_wait_for_go
- udebug_stoppable_begin
- udebug_stoppable_end
- udebug_before_thread_runs
- udebug_syscall_event
- udebug_thread_b_event_attach
- udebug_thread_e_event
- udebug_task_cleanup
- udebug_thread_fault
#include <assert.h>
#include <debug.h>
#include <synch/waitq.h>
#include <udebug/udebug.h>
#include <errno.h>
#include <arch.h>
#include <proc/task.h>
#include <proc/thread.h>
void udebug_task_init(udebug_task_t *ut)
{
mutex_initialize(&ut->lock, MUTEX_PASSIVE);
ut->dt_state = UDEBUG_TS_INACTIVE;
ut->begin_call = NULL;
ut->not_stoppable_count = 0;
ut->evmask = 0;
}
void udebug_thread_initialize(udebug_thread_t *ut)
{
mutex_initialize(&ut->lock, MUTEX_PASSIVE);
waitq_initialize(&ut->go_wq);
condvar_initialize(&ut->active_cv);
ut->go_call = NULL;
ut->uspace_state = NULL;
ut->go = false;
ut->stoppable = true;
ut->active = false;
ut->cur_event = 0;
}
static void udebug_wait_for_go(waitq_t *wq)
{
waitq_sleep(wq);
}
void udebug_stoppable_begin(void)
{
assert(THREAD);
assert(TASK);
mutex_lock(&TASK->udebug.lock);
int nsc = --TASK->udebug.not_stoppable_count;
mutex_lock(&THREAD->udebug.lock);
assert(THREAD->udebug.stoppable == false);
THREAD->udebug.stoppable = true;
if ((TASK->udebug.dt_state == UDEBUG_TS_BEGINNING) && (nsc == 0)) {
call_t *db_call = TASK->udebug.begin_call;
assert(db_call);
TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
TASK->udebug.begin_call = NULL;
ipc_set_retval(&db_call->data, 0);
ipc_answer(&TASK->answerbox, db_call);
} else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
if (THREAD->udebug.active == true &&
THREAD->udebug.go == false) {
call_t *go_call = THREAD->udebug.go_call;
THREAD->udebug.go_call = NULL;
assert(go_call);
ipc_set_retval(&go_call->data, 0);
ipc_set_arg1(&go_call->data, UDEBUG_EVENT_STOP);
THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
ipc_answer(&TASK->answerbox, go_call);
}
}
mutex_unlock(&THREAD->udebug.lock);
mutex_unlock(&TASK->udebug.lock);
}
void udebug_stoppable_end(void)
{
restart:
mutex_lock(&TASK->udebug.lock);
mutex_lock(&THREAD->udebug.lock);
if ((THREAD->udebug.active) && (THREAD->udebug.go == false)) {
mutex_unlock(&THREAD->udebug.lock);
mutex_unlock(&TASK->udebug.lock);
udebug_wait_for_go(&THREAD->udebug.go_wq);
goto restart;
} else {
++TASK->udebug.not_stoppable_count;
assert(THREAD->udebug.stoppable == true);
THREAD->udebug.stoppable = false;
mutex_unlock(&THREAD->udebug.lock);
mutex_unlock(&TASK->udebug.lock);
}
}
void udebug_before_thread_runs(void)
{
udebug_stoppable_begin();
udebug_stoppable_end();
}
void udebug_syscall_event(sysarg_t a1, sysarg_t a2, sysarg_t a3,
sysarg_t a4, sysarg_t a5, sysarg_t a6, sysarg_t id, sysarg_t rc,
bool end_variant)
{
udebug_event_t etype =
end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
mutex_lock(&TASK->udebug.lock);
mutex_lock(&THREAD->udebug.lock);
if (THREAD->udebug.active != true || THREAD->udebug.go == false ||
(TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
mutex_unlock(&THREAD->udebug.lock);
mutex_unlock(&TASK->udebug.lock);
return;
}
call_t *call = THREAD->udebug.go_call;
THREAD->udebug.go_call = NULL;
ipc_set_retval(&call->data, 0);
ipc_set_arg1(&call->data, etype);
ipc_set_arg2(&call->data, id);
ipc_set_arg3(&call->data, rc);
THREAD->udebug.syscall_args[0] = a1;
THREAD->udebug.syscall_args[1] = a2;
THREAD->udebug.syscall_args[2] = a3;
THREAD->udebug.syscall_args[3] = a4;
THREAD->udebug.syscall_args[4] = a5;
THREAD->udebug.syscall_args[5] = a6;
THREAD->udebug.go = false;
THREAD->udebug.cur_event = etype;
ipc_answer(&TASK->answerbox, call);
mutex_unlock(&THREAD->udebug.lock);
mutex_unlock(&TASK->udebug.lock);
udebug_wait_for_go(&THREAD->udebug.go_wq);
}
void udebug_thread_b_event_attach(struct thread *thread, struct task *task)
{
mutex_lock(&TASK->udebug.lock);
mutex_lock(&THREAD->udebug.lock);
thread_attach(thread, task);
LOG("Check state");
if (THREAD->udebug.active != true) {
LOG("udebug.active: %s, udebug.go: %s",
THREAD->udebug.active ? "Yes(+)" : "No",
THREAD->udebug.go ? "Yes(-)" : "No");
mutex_unlock(&THREAD->udebug.lock);
mutex_unlock(&TASK->udebug.lock);
return;
}
LOG("Trigger event");
call_t *call = THREAD->udebug.go_call;
THREAD->udebug.go_call = NULL;
ipc_set_retval(&call->data, 0);
ipc_set_arg1(&call->data, UDEBUG_EVENT_THREAD_B);
ipc_set_arg2(&call->data, (sysarg_t) thread);
THREAD->udebug.go = false;
THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
ipc_answer(&TASK->answerbox, call);
mutex_unlock(&THREAD->udebug.lock);
mutex_unlock(&TASK->udebug.lock);
LOG("Wait for Go");
udebug_wait_for_go(&THREAD->udebug.go_wq);
}
void udebug_thread_e_event(void)
{
mutex_lock(&TASK->udebug.lock);
mutex_lock(&THREAD->udebug.lock);
LOG("Check state");
if (THREAD->udebug.active != true) {
LOG("udebug.active: %s, udebug.go: %s",
THREAD->udebug.active ? "Yes" : "No",
THREAD->udebug.go ? "Yes" : "No");
mutex_unlock(&THREAD->udebug.lock);
mutex_unlock(&TASK->udebug.lock);
return;
}
LOG("Trigger event");
call_t *call = THREAD->udebug.go_call;
THREAD->udebug.go_call = NULL;
ipc_set_retval(&call->data, 0);
ipc_set_arg1(&call->data, UDEBUG_EVENT_THREAD_E);
THREAD->udebug.active = false;
THREAD->udebug.cur_event = 0;
THREAD->udebug.go = false;
ipc_answer(&TASK->answerbox, call);
mutex_unlock(&THREAD->udebug.lock);
mutex_unlock(&TASK->udebug.lock);
}
errno_t udebug_task_cleanup(struct task *task)
{
assert(mutex_locked(&task->udebug.lock));
if ((task->udebug.dt_state != UDEBUG_TS_BEGINNING) &&
(task->udebug.dt_state != UDEBUG_TS_ACTIVE)) {
return EINVAL;
}
LOG("Task %" PRIu64, task->taskid);
list_foreach(task->threads, th_link, thread_t, thread) {
mutex_lock(&thread->udebug.lock);
if (thread->uspace) {
thread->udebug.active = false;
thread->udebug.cur_event = 0;
if (thread->udebug.go == true) {
thread->udebug.go = false;
LOG("Answer GO call with EVENT_FINISHED.");
ipc_set_retval(&thread->udebug.go_call->data, 0);
ipc_set_arg1(&thread->udebug.go_call->data,
UDEBUG_EVENT_FINISHED);
ipc_answer(&task->answerbox, thread->udebug.go_call);
thread->udebug.go_call = NULL;
} else {
waitq_close(&thread->udebug.go_wq);
}
mutex_unlock(&thread->udebug.lock);
condvar_broadcast(&thread->udebug.active_cv);
} else
mutex_unlock(&thread->udebug.lock);
}
task->udebug.dt_state = UDEBUG_TS_INACTIVE;
task->udebug.debugger = NULL;
return 0;
}
void udebug_thread_fault(void)
{
udebug_stoppable_begin();
mutex_lock(&THREAD->udebug.lock);
while (!THREAD->udebug.active)
condvar_wait(&THREAD->udebug.active_cv, &THREAD->udebug.lock);
mutex_unlock(&THREAD->udebug.lock);
udebug_stoppable_end();
}
HelenOS homepage, sources at GitHub