Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 3028 → Rev 3018

/branches/tracing/kernel/generic/include/udebug/udebug.h
176,8 → 176,6
#ifdef KERNEL
 
#include <synch/mutex.h>
#include <arch/interrupt.h>
#include <atomic.h>
 
typedef enum {
/** Task is not being debugged */
195,7 → 193,6
typedef struct {
/** Synchronize debug ops on this task / access to this structure */
mutex_t lock;
char *lock_owner;
 
udebug_task_state_t dt_state;
call_t *begin_call;
207,20 → 204,14
/** Debugging part of thread_t structure.
*/
typedef struct {
/**
* Prevent deadlock with udebug_before_thread_runs() in interrupt
* handler, without actually disabling interrupts.
* ==0 means "unlocked", >0 means "locked"
*/
atomic_t int_lock;
 
/** Synchronize debug ops on this thread / access to this structure */
mutex_t lock;
SPINLOCK_DECLARE(lock);
 
waitq_t go_wq;
call_t *go_call;
unative_t syscall_args[6];
istate_t *uspace_state;
bool in_before_thread_runs;
 
/** What type of event are we stopped in or 0 if none */
udebug_event_t cur_event;
/branches/tracing/kernel/generic/src/proc/thread.c
176,7 → 176,7
return -1;
}
 
mutex_initialize(&t->udebug.lock);
spinlock_initialize(&t->udebug.lock, "thread_debug_lock");
 
return 0;
}
/branches/tracing/kernel/generic/src/udebug/udebug.c
41,24 → 41,6
#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);
70,14 → 52,7
 
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;
103,12 → 78,11
{
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;
128,10 → 102,11
ASSERT(db_call);
 
/* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
mutex_lock(&THREAD->udebug.lock);
ASSERT(THREAD->udebug.stoppable == false);
ipl = interrupts_disable();
spinlock_lock(&THREAD->udebug.lock);
THREAD->udebug.stoppable = true;
mutex_unlock(&THREAD->udebug.lock);
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
 
TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
TASK->udebug.begin_call = NULL;
147,8 → 122,8
*/
 
/* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
mutex_lock(&THREAD->udebug.lock);
ASSERT(THREAD->udebug.stoppable == false);
ipl = interrupts_disable();
spinlock_lock(&THREAD->udebug.lock);
THREAD->udebug.stoppable = true;
 
if (THREAD->udebug.debug_active && THREAD->udebug.stop) {
165,7 → 140,8
IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
 
THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
mutex_unlock(&THREAD->udebug.lock);
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
 
ipc_answer(&TASK->answerbox, go_call);
 
174,7 → 150,8
/*
* No stop request - nothing happens.
*/
mutex_unlock(&THREAD->udebug.lock);
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&TASK->udebug.lock);
}
} else {
183,10 → 160,11
*/
 
/* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
mutex_lock(&THREAD->udebug.lock);
ASSERT(THREAD->udebug.stoppable == false);
ipl = interrupts_disable();
spinlock_lock(&THREAD->udebug.lock);
THREAD->udebug.stoppable = true;
mutex_unlock(&THREAD->udebug.lock);
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
 
mutex_unlock(&TASK->udebug.lock);
}
194,11 → 172,14
 
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 */
mutex_lock(&THREAD->udebug.lock);
ipl = interrupts_disable();
spinlock_lock(&THREAD->udebug.lock);
 
if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
//klog_printf("udebug_stoppable_end");
208,7 → 189,8
if (THREAD->udebug.debug_active &&
THREAD->udebug.stop == true) {
TASK->udebug.begin_call = NULL;
mutex_unlock(&THREAD->udebug.lock);
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&TASK->udebug.lock);
 
udebug_wait_for_go(&THREAD->udebug.go_wq);
217,14 → 199,12
/* must try again - have to lose stoppability atomically */
} else {
++TASK->udebug.not_stoppable_count;
ASSERT(THREAD->udebug.stoppable == true);
THREAD->udebug.stoppable = false;
 
mutex_unlock(&THREAD->udebug.lock);
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&TASK->udebug.lock);
}
 
udebug_int_unlock();
}
 
/** Upon being scheduled to run, check if the current thread should stop.
232,23 → 212,17
* 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 or deadlock with the thread context.
* not to allow arbitrary recursion.
*/
void udebug_before_thread_runs(void)
{
ipl_t ipl;
 
ASSERT(!PREEMPTION_DISABLED);
 
/*
* Prevent agains re-entering, such as when preempted inside this
* function.
*/
if (atomic_get(&THREAD->udebug.int_lock) != 0)
/* This will happen if we get preempted inside this function. */
if (THREAD->udebug.in_before_thread_runs)
return;
 
udebug_int_lock();
 
THREAD->udebug.in_before_thread_runs = true;
ipl = interrupts_enable();
 
/* Now we're free to do whatever we need (lock mutexes, sleep, etc.) */
258,8 → 232,7
udebug_stoppable_end();
 
interrupts_restore(ipl);
 
udebug_int_unlock();
THREAD->udebug.in_before_thread_runs = false;
}
 
void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
267,20 → 240,22
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) {
mutex_unlock(&THREAD->udebug.lock);
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&TASK->udebug.lock);
return;
}
310,7 → 285,8
THREAD->udebug.stop = true;
 
THREAD->udebug.cur_event = etype;
mutex_unlock(&THREAD->udebug.lock);
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
 
ipc_answer(&TASK->answerbox, call);
 
317,19 → 293,18
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");
 
338,7 → 313,8
klog_printf("- debug_active: %s, udebug.stop: %s",
THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
THREAD->udebug.stop ? "yes(-)" : "no(+)");
mutex_unlock(&THREAD->udebug.lock);
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&TASK->udebug.lock);
return;
}
359,7 → 335,8
THREAD->udebug.stop = true;
 
THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
mutex_unlock(&THREAD->udebug.lock);
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
 
ipc_answer(&TASK->answerbox, call);
 
367,19 → 344,18
 
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");
 
388,7 → 364,8
klog_printf("- debug_active: %s, udebug.stop: %s",
THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
THREAD->udebug.stop ? "yes(-)" : "no(+)");
mutex_unlock(&THREAD->udebug.lock);
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&TASK->udebug.lock);
return;
}
405,13 → 382,13
THREAD->udebug.cur_event = 0; /* none */
THREAD->udebug.stop = true; /* set to initial value */
 
mutex_unlock(&THREAD->udebug.lock);
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
 
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 */
}
 
418,17 → 395,19
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) {
mutex_unlock(&THREAD->udebug.lock);
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&TASK->udebug.lock);
return;
}
449,8 → 428,8
THREAD->udebug.stop = true;
 
THREAD->udebug.cur_event = etype;
mutex_unlock(&THREAD->udebug.lock);
 
spinlock_unlock(&THREAD->udebug.lock);
interrupts_restore(ipl);
klog_printf("- send answer");
 
ipc_answer(&TASK->answerbox, call);
457,8 → 436,6
mutex_unlock(&TASK->udebug.lock);
 
udebug_wait_for_go(&THREAD->udebug.go_wq);
 
udebug_int_unlock();
}
 
void udebug_breakpoint_event(uintptr_t addr)
487,8 → 464,6
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");
499,15 → 474,13
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) {
527,7 → 500,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 {
543,14 → 516,13
waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
}
}
mutex_unlock(&t->udebug.lock);
spinlock_unlock(&t->udebug.lock);
interrupts_restore(ipl);
}
 
ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
ta->udebug.debugger = NULL;
 
udebug_int_unlock();
 
return 0;
}
 
/branches/tracing/kernel/generic/src/udebug/udebug_ops.c
54,102 → 54,74
* Specifically, verifies that thread t exists, is a userspace thread,
* and belongs to the current task (TASK). Verifies, that the thread
* has (or hasn't) go according to having_go (typically false).
* It also locks t->udebug.lock, making sure that t->udebug.debug_active
* is true - that the thread is in a valid debugging session.
* It also locks t->udebug.lock, making sure that t->udebug.debug_active is true
* - that the thread is in a valid debugging session.
*
* With this verified and the t->udebug.lock mutex held, it is ensured
* that the thread cannot leave the debugging session, let alone cease
* to exist.
* Returns EOK if all went well, or an error code otherwise.
* Interrupts must be already disabled when calling this function.
*
* In this function, holding the TASK->udebug.lock mutex prevents the
* thread from leaving the debugging session, while relaxing from
* the t->lock spinlock to the t->udebug.lock mutex.
*
* Returns EOK if all went well, or an error code otherwise.
* Note: This function sports complicated locking.
*/
static int _thread_op_begin(thread_t *t, bool having_go)
{
int rc;
task_id_t taskid;
ipl_t ipl;
 
taskid = TASK->taskid;
 
mutex_lock(&TASK->udebug.lock);
 
/* thread_exists() must be called with threads_lock held */
ipl = interrupts_disable();
/* Must lock threads_lock to ensure continued existence of the thread */
spinlock_lock(&threads_lock);
 
if (!thread_exists(t)) {
spinlock_unlock(&threads_lock);
interrupts_restore(ipl);
mutex_unlock(&TASK->udebug.lock);
return ENOENT;
}
 
/* t->lock is enough to ensure the thread's existence */
spinlock_lock(&t->udebug.lock);
spinlock_lock(&t->lock);
spinlock_unlock(&threads_lock);
/* Now verify that it's the current task */
if (t->task != TASK) {
/* No such thread belonging to callee */
rc = ENOENT;
goto error_exit;
}
 
/* Verify that 't' is a userspace thread */
if ((t->flags & THREAD_FLAG_USPACE) == 0) {
/* It's not, deny its existence */
spinlock_unlock(&t->lock);
interrupts_restore(ipl);
mutex_unlock(&TASK->udebug.lock);
return ENOENT;
rc = ENOENT;
goto error_exit;
}
 
/* Verify debugging state */
if (t->udebug.debug_active != true) {
if ((t->udebug.debug_active != true) || (!t->udebug.stop != having_go)) {
/* Not in debugging session or undesired GO state */
spinlock_unlock(&t->lock);
interrupts_restore(ipl);
mutex_unlock(&TASK->udebug.lock);
return ENOENT;
rc = EINVAL;
goto error_exit;
}
 
/*
* Since the thread has debug_active == true, TASK->udebug.lock
* is enough to ensure its existence and that debug_active remains
* true.
*/
spinlock_unlock(&threads_lock);
spinlock_unlock(&t->lock);
interrupts_restore(ipl);
 
/* Only mutex TASK->udebug.lock left */
/* Now verify that the thread belongs to the current task */
if (t->task != TASK) {
/* No such thread belonging this task*/
mutex_unlock(&TASK->udebug.lock);
return ENOENT;
}
/* Only t->udebug.lock left */
 
/*
* Now we need to grab the thread's debug lock for synchronization
* of the threads stoppability/stop state.
*/
mutex_lock(&t->udebug.lock);
return EOK; /* All went well */
 
/* The big task mutex is no longer needed */
mutex_unlock(&TASK->udebug.lock);
 
if (!t->udebug.stop != having_go) {
/* Not in debugging session or undesired GO state */
mutex_unlock(&t->udebug.lock);
return EINVAL;
}
/* Executed when a check on the thread fails */
error_exit:
spinlock_unlock(&t->lock);
spinlock_unlock(&t->udebug.lock);
spinlock_unlock(&threads_lock);
 
/* Only t->udebug.lock left */
 
return EOK; /* All went well */
/* No locks left here */
return rc; /* Some errors occured */
}
 
 
static void _thread_op_end(thread_t *t)
{
mutex_unlock(&t->udebug.lock);
spinlock_unlock(&t->udebug.lock);
}
 
/**
157,6 → 129,7
*/
int udebug_begin(call_t *call)
{
ipl_t ipl;
int reply;
 
thread_t *t;
191,10 → 164,12
for (cur = TASK->th_head.next; cur != &TASK->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);
if ((t->flags & THREAD_FLAG_USPACE) != 0)
t->udebug.debug_active = true;
mutex_unlock(&t->udebug.lock);
spinlock_unlock(&t->udebug.lock);
interrupts_restore(ipl);
}
 
mutex_unlock(&TASK->udebug.lock);
246,13 → 221,17
 
int udebug_go(thread_t *t, call_t *call)
{
ipl_t ipl;
int rc;
 
// klog_printf("udebug_go()");
 
ipl = interrupts_disable();
 
/* On success, this will lock t->udebug.lock */
rc = _thread_op_begin(t, false);
if (rc != EOK) {
interrupts_restore(ipl);
return rc;
}
 
266,6 → 245,7
waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
 
_thread_op_end(t);
interrupts_restore(ipl);
 
return 0;
}
272,11 → 252,14
 
int udebug_stop(thread_t *t, call_t *call)
{
ipl_t ipl;
int rc;
 
klog_printf("udebug_stop()");
mutex_lock(&TASK->udebug.lock);
 
ipl = interrupts_disable();
 
/*
* On success, this will lock t->udebug.lock. Note that this makes sure
* the thread is not stopped.
283,6 → 266,7
*/
rc = _thread_op_begin(t, true);
if (rc != EOK) {
interrupts_restore(ipl);
return rc;
}
 
292,6 → 276,7
if (!t->udebug.stoppable) {
/* Answer will be sent when the thread becomes stoppable */
_thread_op_end(t);
interrupts_restore(ipl);
return 0;
}
 
311,6 → 296,7
THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
 
_thread_op_end(t);
interrupts_restore(ipl);
 
ipc_answer(&TASK->answerbox, call);
mutex_unlock(&TASK->udebug.lock);
350,7 → 336,6
max_ids = buf_size / sizeof(unative_t);
copied_ids = 0;
 
/* FIXME: make sure the thread isn't past debug shutdown... */
for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
/* Do not write past end of buffer */
if (copied_ids >= max_ids) break;
383,16 → 368,20
int udebug_args_read(thread_t *t, void **buffer)
{
int rc;
ipl_t ipl;
unative_t *arg_buffer;
 
// klog_printf("udebug_args_read()");
klog_printf("udebug_args_read()");
 
/* Prepare a buffer to hold the arguments */
arg_buffer = malloc(6 * sizeof(unative_t), 0);
 
ipl = interrupts_disable();
 
/* On success, this will lock t->udebug.lock */
rc = _thread_op_begin(t, false);
if (rc != EOK) {
interrupts_restore(ipl);
return rc;
}
 
400,6 → 389,8
if (t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_B &&
t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_E) {
_thread_op_end(t);
interrupts_restore(ipl);
 
return EINVAL;
}
 
407,6 → 398,7
memcpy(arg_buffer, t->udebug.syscall_args, 6 * sizeof(unative_t));
 
_thread_op_end(t);
interrupts_restore(ipl);
 
*buffer = arg_buffer;
return 0;
416,12 → 408,16
{
istate_t *state;
int rc;
ipl_t ipl;
 
// klog_printf("udebug_regs_read()");
klog_printf("udebug_regs_read()");
 
ipl = interrupts_disable();
 
/* On success, this will lock t->udebug.lock */
rc = _thread_op_begin(t, false);
if (rc != EOK) {
interrupts_restore(ipl);
return rc;
}
 
428,6 → 424,7
state = t->udebug.uspace_state;
if (state == NULL) {
_thread_op_end(t);
interrupts_restore(ipl);
klog_printf("udebug_regs_read() - istate not available");
return EBUSY;
}
436,6 → 433,7
memcpy(buffer, state, sizeof(istate_t));
 
_thread_op_end(t);
interrupts_restore(ipl);
 
return 0;
}
444,15 → 442,19
{
int rc;
istate_t *state;
ipl_t ipl;
 
klog_printf("udebug_regs_write()");
 
/* Try to change the thread's uspace_state */
 
ipl = interrupts_disable();
 
/* On success, this will lock t->udebug.lock */
rc = _thread_op_begin(t, false);
if (rc != EOK) {
klog_printf("error locking thread");
interrupts_restore(ipl);
return rc;
}
 
459,7 → 461,9
state = t->udebug.uspace_state;
if (state == NULL) {
_thread_op_end(t);
interrupts_restore(ipl);
klog_printf("udebug_regs_write() - istate not available");
 
return EBUSY;
}
 
466,6 → 470,7
memcpy(t->udebug.uspace_state, buffer, sizeof(istate_t));
 
_thread_op_end(t);
interrupts_restore(ipl);
 
return 0;
}