Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 3025 → Rev 3026

/branches/tracing/kernel/generic/include/udebug/udebug.h
176,6 → 176,8
#ifdef KERNEL
 
#include <synch/mutex.h>
#include <arch/interrupt.h>
#include <atomic.h>
 
typedef enum {
/** Task is not being debugged */
193,6 → 195,7
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;
204,14 → 207,20
/** 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 */
SPINLOCK_DECLARE(lock);
mutex_t 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;
}
 
spinlock_initialize(&t->udebug.lock, "thread_debug_lock");
mutex_initialize(&t->udebug.lock);
 
return 0;
}
/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;
}
 
/branches/tracing/kernel/generic/src/udebug/udebug_ops.c
54,74 → 54,102
* 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.
*
* 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.
* Interrupts must be already disabled when calling this function.
*
* 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;
 
/* Must lock threads_lock to ensure continued existence of the thread */
mutex_lock(&TASK->udebug.lock);
 
/* thread_exists() must be called with threads_lock held */
ipl = interrupts_disable();
spinlock_lock(&threads_lock);
 
if (!thread_exists(t)) {
spinlock_unlock(&threads_lock);
interrupts_restore(ipl);
mutex_unlock(&TASK->udebug.lock);
return ENOENT;
}
 
spinlock_lock(&t->udebug.lock);
/* t->lock is enough to ensure the thread's existence */
spinlock_lock(&t->lock);
/* Now verify that it's the current task */
if (t->task != TASK) {
/* No such thread belonging to callee */
rc = ENOENT;
goto error_exit;
}
spinlock_unlock(&threads_lock);
 
/* Verify that 't' is a userspace thread */
if ((t->flags & THREAD_FLAG_USPACE) == 0) {
/* It's not, deny its existence */
rc = ENOENT;
goto error_exit;
spinlock_unlock(&t->lock);
interrupts_restore(ipl);
mutex_unlock(&TASK->udebug.lock);
return ENOENT;
}
 
if ((t->udebug.debug_active != true) || (!t->udebug.stop != having_go)) {
/* Verify debugging state */
if (t->udebug.debug_active != true) {
/* Not in debugging session or undesired GO state */
rc = EINVAL;
goto error_exit;
spinlock_unlock(&t->lock);
interrupts_restore(ipl);
mutex_unlock(&TASK->udebug.lock);
return ENOENT;
}
 
spinlock_unlock(&threads_lock);
/*
* Since the thread has debug_active == true, TASK->udebug.lock
* is enough to ensure its existence and that debug_active remains
* true.
*/
spinlock_unlock(&t->lock);
interrupts_restore(ipl);
 
/* Only t->udebug.lock left */
/* 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;
}
 
return EOK; /* All went well */
/*
* Now we need to grab the thread's debug lock for synchronization
* of the threads stoppability/stop state.
*/
mutex_lock(&t->udebug.lock);
 
/* The big task mutex is no longer needed */
mutex_unlock(&TASK->udebug.lock);
 
/* Executed when a check on the thread fails */
error_exit:
spinlock_unlock(&t->lock);
spinlock_unlock(&t->udebug.lock);
spinlock_unlock(&threads_lock);
if (!t->udebug.stop != having_go) {
/* Not in debugging session or undesired GO state */
mutex_unlock(&t->udebug.lock);
return EINVAL;
}
 
/* No locks left here */
return rc; /* Some errors occured */
/* Only t->udebug.lock left */
 
return EOK; /* All went well */
}
 
 
static void _thread_op_end(thread_t *t)
{
spinlock_unlock(&t->udebug.lock);
mutex_unlock(&t->udebug.lock);
}
 
/**
129,7 → 157,6
*/
int udebug_begin(call_t *call)
{
ipl_t ipl;
int reply;
 
thread_t *t;
164,12 → 191,10
for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
t = list_get_instance(cur, thread_t, th_link);
 
ipl = interrupts_disable();
spinlock_lock(&t->udebug.lock);
mutex_lock(&t->udebug.lock);
if ((t->flags & THREAD_FLAG_USPACE) != 0)
t->udebug.debug_active = true;
spinlock_unlock(&t->udebug.lock);
interrupts_restore(ipl);
mutex_unlock(&t->udebug.lock);
}
 
mutex_unlock(&TASK->udebug.lock);
221,17 → 246,13
 
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;
}
 
245,7 → 266,6
waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
 
_thread_op_end(t);
interrupts_restore(ipl);
 
return 0;
}
252,14 → 272,11
 
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.
266,7 → 283,6
*/
rc = _thread_op_begin(t, true);
if (rc != EOK) {
interrupts_restore(ipl);
return rc;
}
 
276,7 → 292,6
if (!t->udebug.stoppable) {
/* Answer will be sent when the thread becomes stoppable */
_thread_op_end(t);
interrupts_restore(ipl);
return 0;
}
 
296,7 → 311,6
THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
 
_thread_op_end(t);
interrupts_restore(ipl);
 
ipc_answer(&TASK->answerbox, call);
mutex_unlock(&TASK->udebug.lock);
336,6 → 350,7
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;
368,20 → 383,16
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;
}
 
389,8 → 400,6
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;
}
 
398,7 → 407,6
memcpy(arg_buffer, t->udebug.syscall_args, 6 * sizeof(unative_t));
 
_thread_op_end(t);
interrupts_restore(ipl);
 
*buffer = arg_buffer;
return 0;
408,16 → 416,12
{
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;
}
 
424,7 → 428,6
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;
}
433,7 → 436,6
memcpy(buffer, state, sizeof(istate_t));
 
_thread_op_end(t);
interrupts_restore(ipl);
 
return 0;
}
442,19 → 444,15
{
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;
}
 
461,9 → 459,7
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;
}
 
470,7 → 466,6
memcpy(t->udebug.uspace_state, buffer, sizeof(istate_t));
 
_thread_op_end(t);
interrupts_restore(ipl);
 
return 0;
}