/** @addtogroup generic
 * @{
 */

/**
 * @file
 * @brief	Tdebug.
 */
 
#include <synch/waitq.h>
#include <console/klog.h>
#include <udebug/udebug.h>
#include <arch.h>

void udebug_stoppable_begin(void)
{
	int nsc;
	call_t *db_call;
	ipl_t ipl;

	ipl = interrupts_disable();
	spinlock_lock(&TASK->lock);

	nsc = --TASK->not_stoppable_count;
	db_call = TASK->debug_begin_call;

	if (TASK->dt_state == UDEBUG_TS_BEGINNING) {
		klog_printf("udebug_stoppable_begin");
		klog_printf(" - nsc := %d", nsc);
	}

	if (TASK->dt_state == UDEBUG_TS_BEGINNING && nsc == 0) {
		TASK->dt_state = UDEBUG_TS_ACTIVE;
		TASK->debug_begin_call = NULL;
		spinlock_unlock(&TASK->lock);
		interrupts_restore(ipl);

		IPC_SET_RETVAL(db_call->data, 0);
		klog_printf("udebug_stoppable_begin/ipc_answer");
		ipc_answer(&TASK->answerbox, db_call);		
	} else {
	        spinlock_unlock(&TASK->lock);
		interrupts_restore(ipl);
	}
}

void udebug_stoppable_end(void)
{
	ipl_t ipl;

restart:
	ipl = interrupts_disable();
	spinlock_lock(&TASK->lock);

	if ((TASK->dt_state == UDEBUG_TS_BEGINNING ||
	    TASK->dt_state == UDEBUG_TS_ACTIVE) &&
	    THREAD->debug_stop == true) {
		TASK->debug_begin_call = NULL;
		spinlock_unlock(&TASK->lock);
		interrupts_restore(ipl);

		klog_printf("udebug_stoppable_end: waitq_sleep");
		waitq_sleep(&THREAD->go_wq);
		goto restart;
		/* must try again - have to lose stoppability atomically */
	} else {
		++TASK->not_stoppable_count;
		spinlock_unlock(&TASK->lock);
		interrupts_restore(ipl);
	}
}

void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
    unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc)
{
	call_t *call;
	ipl_t ipl;

	ipl = interrupts_disable();
	spinlock_lock(&THREAD->debug_lock);

	/* Must only generate events when in debugging session and have go */
	if (THREAD->debug_active != true ||
	    THREAD->debug_stop == true) {
		spinlock_unlock(&THREAD->debug_lock);
		interrupts_restore(ipl);
		return;
	}

	klog_printf("udebug_syscall_event");
	call = THREAD->debug_go_call;
	IPC_SET_RETVAL(call->data, 0);
	IPC_SET_ARG1(call->data, UDEBUG_EVENT_SYSCALL);
	IPC_SET_ARG2(call->data, id);
	IPC_SET_ARG3(call->data, rc);
	klog_printf("udebug_syscall_event/ipc_answer");

	THREAD->syscall_args[0] = a1;
	THREAD->syscall_args[1] = a2;
	THREAD->syscall_args[2] = a3;
	THREAD->syscall_args[3] = a4;
	THREAD->syscall_args[4] = a5;
	THREAD->syscall_args[5] = a6;

	/*
	 * Make sure debug_stop is true when going to sleep
	 * in case we get woken up by DEBUG_END. (At which
	 * point it must be back to the initial true value).
	 */
	THREAD->debug_stop = true;

	THREAD->cur_event = UDEBUG_EVENT_SYSCALL;
	spinlock_unlock(&THREAD->debug_lock);

	spinlock_lock(&TASK->lock);
	ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
	spinlock_unlock(&TASK->lock);

	interrupts_restore(ipl);

	waitq_sleep(&THREAD->go_wq);
}

void udebug_new_thread_event(struct thread *t)
{
	call_t *call;
	ipl_t ipl;

	ipl = interrupts_disable();
	spinlock_lock(&THREAD->debug_lock);

	klog_printf("udebug_new_thread_event");
	klog_printf("- check state");

	/* Must only generate events when in debugging session */
	if (THREAD->debug_active != true) {
		klog_printf("- debug_active: %s, debug_stop: %s",
			THREAD->debug_active ? "yes(+)" : "no(-)",
			THREAD->debug_stop ? "yes(-)" : "no(+)");
		spinlock_unlock(&THREAD->debug_lock);
		interrupts_restore(ipl);
		return;
	}

	klog_printf("- trigger event");

	call = THREAD->debug_go_call;
	IPC_SET_RETVAL(call->data, 0);
	IPC_SET_ARG1(call->data, UDEBUG_EVENT_NEW_THREAD);
	IPC_SET_ARG2(call->data, (unative_t)t);

	/*
	 * Make sure debug_stop is true when going to sleep
	 * in case we get woken up by DEBUG_END. (At which
	 * point it must be back to the initial true value).
	 */
	THREAD->debug_stop = true;

	THREAD->cur_event = UDEBUG_EVENT_NEW_THREAD;
	spinlock_unlock(&THREAD->debug_lock);

	spinlock_lock(&TASK->lock);
	ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
	spinlock_unlock(&TASK->lock);

	interrupts_restore(ipl);
	klog_printf("- sleep");

	waitq_sleep(&THREAD->go_wq);
}


/** @}
 */
