Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 3025 → Rev 3026

/branches/tracing/kernel/generic/src/udebug/udebug.c
41,6 → 41,24
#include <errno.h>
#include <arch.h>
 
/*
* FIXME: Don't grab TASK->udebug.lock in this module, synchronize
* only with THREAD->udebug.lock.
*
* For this reason, TASK->udebug.lock is not guarded against the interrupt
* handler in udebug_ops.c. (which could deadlock)
*/
 
static inline void udebug_int_lock(void)
{
atomic_inc(&THREAD->udebug.int_lock);
}
 
static inline void udebug_int_unlock(void)
{
atomic_dec(&THREAD->udebug.int_lock);
}
 
void udebug_task_init(udebug_task_t *ut)
{
mutex_initialize(&ut->lock);
52,7 → 70,14
 
void udebug_thread_initialize(udebug_thread_t *ut)
{
mutex_initialize(&ut->lock);
waitq_initialize(&ut->go_wq);
 
/*
* At the beginning the thread is stoppable, so int_lock be set, too.
*/
atomic_set(&ut->int_lock, 1);
 
ut->go_call = NULL;
ut->uspace_state = NULL;
ut->stop = true;
78,11 → 103,12
{
int nsc;
call_t *db_call, *go_call;
ipl_t ipl;
 
ASSERT(THREAD);
ASSERT(TASK);
 
udebug_int_lock();
 
mutex_lock(&TASK->udebug.lock);
 
nsc = --TASK->udebug.not_stoppable_count;
102,11 → 128,10
ASSERT(db_call);
 
/* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
ipl = interrupts_disable();
spinlock_lock(&THREAD->udebug.lock);
mutex_lock(&THREAD->udebug.lock);
ASSERT(THREAD->udebug.stoppable == false);
THREAD->udebug.stoppable = true;
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&THREAD->udebug.lock);
 
TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
TASK->udebug.begin_call = NULL;
122,8 → 147,8
*/
 
/* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
ipl = interrupts_disable();
spinlock_lock(&THREAD->udebug.lock);
mutex_lock(&THREAD->udebug.lock);
ASSERT(THREAD->udebug.stoppable == false);
THREAD->udebug.stoppable = true;
 
if (THREAD->udebug.debug_active && THREAD->udebug.stop) {
140,8 → 165,7
IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
 
THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&THREAD->udebug.lock);
 
ipc_answer(&TASK->answerbox, go_call);
 
150,8 → 174,7
/*
* No stop request - nothing happens.
*/
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&THREAD->udebug.lock);
mutex_unlock(&TASK->udebug.lock);
}
} else {
160,11 → 183,10
*/
 
/* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
ipl = interrupts_disable();
spinlock_lock(&THREAD->udebug.lock);
mutex_lock(&THREAD->udebug.lock);
ASSERT(THREAD->udebug.stoppable == false);
THREAD->udebug.stoppable = true;
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&THREAD->udebug.lock);
 
mutex_unlock(&TASK->udebug.lock);
}
172,14 → 194,11
 
void udebug_stoppable_end(void)
{
ipl_t ipl;
 
restart:
mutex_lock(&TASK->udebug.lock);
 
/* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
ipl = interrupts_disable();
spinlock_lock(&THREAD->udebug.lock);
mutex_lock(&THREAD->udebug.lock);
 
if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
//klog_printf("udebug_stoppable_end");
189,8 → 208,7
if (THREAD->udebug.debug_active &&
THREAD->udebug.stop == true) {
TASK->udebug.begin_call = NULL;
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&THREAD->udebug.lock);
mutex_unlock(&TASK->udebug.lock);
 
udebug_wait_for_go(&THREAD->udebug.go_wq);
199,12 → 217,14
/* must try again - have to lose stoppability atomically */
} else {
++TASK->udebug.not_stoppable_count;
ASSERT(THREAD->udebug.stoppable == true);
THREAD->udebug.stoppable = false;
 
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&THREAD->udebug.lock);
mutex_unlock(&TASK->udebug.lock);
}
 
udebug_int_unlock();
}
 
/** Upon being scheduled to run, check if the current thread should stop.
212,17 → 232,23
* This function is called from clock(). Preemption is enabled.
* interrupts are disabled, but since this is called after
* being scheduled-in, we can enable them, if we're careful enough
* not to allow arbitrary recursion.
* not to allow arbitrary recursion or deadlock with the thread context.
*/
void udebug_before_thread_runs(void)
{
ipl_t ipl;
 
/* This will happen if we get preempted inside this function. */
if (THREAD->udebug.in_before_thread_runs)
ASSERT(!PREEMPTION_DISABLED);
 
/*
* Prevent agains re-entering, such as when preempted inside this
* function.
*/
if (atomic_get(&THREAD->udebug.int_lock) != 0)
return;
 
THREAD->udebug.in_before_thread_runs = true;
udebug_int_lock();
 
ipl = interrupts_enable();
 
/* Now we're free to do whatever we need (lock mutexes, sleep, etc.) */
232,7 → 258,8
udebug_stoppable_end();
 
interrupts_restore(ipl);
THREAD->udebug.in_before_thread_runs = false;
 
udebug_int_unlock();
}
 
void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
240,22 → 267,20
bool end_variant)
{
call_t *call;
ipl_t ipl;
udebug_event_t etype;
 
etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
 
udebug_int_lock();
 
mutex_lock(&TASK->udebug.lock);
mutex_lock(&THREAD->udebug.lock);
 
ipl = interrupts_disable();
spinlock_lock(&THREAD->udebug.lock);
 
/* Must only generate events when in debugging session and have go */
if (THREAD->udebug.debug_active != true ||
THREAD->udebug.stop == true ||
(TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&THREAD->udebug.lock);
mutex_unlock(&TASK->udebug.lock);
return;
}
285,8 → 310,7
THREAD->udebug.stop = true;
 
THREAD->udebug.cur_event = etype;
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&THREAD->udebug.lock);
 
ipc_answer(&TASK->answerbox, call);
 
293,18 → 317,19
mutex_unlock(&TASK->udebug.lock);
 
udebug_wait_for_go(&THREAD->udebug.go_wq);
 
udebug_int_unlock();
}
 
void udebug_thread_b_event(struct thread *t)
{
call_t *call;
ipl_t ipl;
 
udebug_int_lock();
 
mutex_lock(&TASK->udebug.lock);
mutex_lock(&THREAD->udebug.lock);
 
ipl = interrupts_disable();
spinlock_lock(&THREAD->udebug.lock);
 
klog_printf("udebug_thread_b_event");
klog_printf("- check state");
 
313,8 → 338,7
klog_printf("- debug_active: %s, udebug.stop: %s",
THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
THREAD->udebug.stop ? "yes(-)" : "no(+)");
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&THREAD->udebug.lock);
mutex_unlock(&TASK->udebug.lock);
return;
}
335,8 → 359,7
THREAD->udebug.stop = true;
 
THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&THREAD->udebug.lock);
 
ipc_answer(&TASK->answerbox, call);
 
344,18 → 367,19
 
klog_printf("- sleep");
udebug_wait_for_go(&THREAD->udebug.go_wq);
 
udebug_int_unlock();
}
 
void udebug_thread_e_event(void)
{
call_t *call;
ipl_t ipl;
 
udebug_int_lock();
 
mutex_lock(&TASK->udebug.lock);
mutex_lock(&THREAD->udebug.lock);
 
ipl = interrupts_disable();
spinlock_lock(&THREAD->udebug.lock);
 
klog_printf("udebug_thread_e_event");
klog_printf("- check state");
 
364,8 → 388,7
klog_printf("- debug_active: %s, udebug.stop: %s",
THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
THREAD->udebug.stop ? "yes(-)" : "no(+)");
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&THREAD->udebug.lock);
mutex_unlock(&TASK->udebug.lock);
return;
}
382,13 → 405,13
THREAD->udebug.cur_event = 0; /* none */
THREAD->udebug.stop = true; /* set to initial value */
 
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&THREAD->udebug.lock);
 
ipc_answer(&TASK->answerbox, call);
 
mutex_unlock(&TASK->udebug.lock);
 
/* Leave int_lock enabled */
/* This event does not sleep - debugging has finished in this thread */
}
 
395,19 → 418,17
static void breakpoint_trap_event(uintptr_t addr, udebug_event_t etype)
{
call_t *call;
ipl_t ipl;
 
udebug_int_lock();
 
mutex_lock(&TASK->udebug.lock);
mutex_lock(&THREAD->udebug.lock);
 
ipl = interrupts_disable();
spinlock_lock(&THREAD->udebug.lock);
 
/* Must only generate events when in debugging session and have go */
if (THREAD->udebug.debug_active != true ||
THREAD->udebug.stop == true ||
(TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&THREAD->udebug.lock);
mutex_unlock(&TASK->udebug.lock);
return;
}
428,8 → 449,8
THREAD->udebug.stop = true;
 
THREAD->udebug.cur_event = etype;
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&THREAD->udebug.lock);
 
klog_printf("- send answer");
 
ipc_answer(&TASK->answerbox, call);
436,6 → 457,8
mutex_unlock(&TASK->udebug.lock);
 
udebug_wait_for_go(&THREAD->udebug.go_wq);
 
udebug_int_unlock();
}
 
void udebug_breakpoint_event(uintptr_t addr)
464,6 → 487,8
klog_printf("udebug_task_cleanup()");
klog_printf("task %llu", ta->taskid);
 
udebug_int_lock();
 
if (ta->udebug.dt_state == UDEBUG_TS_BEGINNING &&
ta->udebug.dt_state != UDEBUG_TS_ACTIVE) {
klog_printf("udebug_task_cleanup(): task not being debugged");
474,13 → 499,15
for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
t = list_get_instance(cur, thread_t, th_link);
 
mutex_lock(&t->udebug.lock);
 
ipl = interrupts_disable();
spinlock_lock(&t->udebug.lock);
spinlock_lock(&t->lock);
 
flags = t->flags;
 
spinlock_unlock(&t->lock);
interrupts_restore(ipl);
 
/* Only process userspace threads */
if ((flags & THREAD_FLAG_USPACE) != 0) {
500,7 → 527,7
klog_printf("answer GO call with EVENT_FINISHED");
IPC_SET_RETVAL(t->udebug.go_call->data, 0);
IPC_SET_ARG1(t->udebug.go_call->data, UDEBUG_EVENT_FINISHED);
/* FIXME: must not call with interrupts disabled!!*/
 
ipc_answer(&ta->answerbox, t->udebug.go_call);
t->udebug.go_call = NULL;
} else {
516,13 → 543,14
waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
}
}
spinlock_unlock(&t->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&t->udebug.lock);
}
 
ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
ta->udebug.debugger = NULL;
 
udebug_int_unlock();
 
return 0;
}