/*
 * Copyright (c) 2008 Jiri Svoboda
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/** @addtogroup debug
 * @{
 */
/** @file
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bool.h>
#include <udebug.h>
#include <sys/types.h>

#include "main.h"
#include "cons.h"
#include "dthread.h"
#include "include/arch.h"
#include "cmd.h"

static void cmd_break(int argc, char *argv[]);
static void cmd_ct(int argc, char *argv[]);
static void cmd_dbreak(int argc, char *argv[]);
static void cmd_go(int argc, char *argv[]);
static void cmd_istep(int argc, char *argv[]);
static void cmd_lbrk(int argc, char *argv[]);
void	    cmd_help(int argc, char *argv[]);
static void cmd_memr(int argc, char *argv[]);
static void cmd_pwt(int argc, char *argv[]);
static void cmd_regs(int argc, char *argv[]);
static void cmd_stop(int argc, char *argv[]);
static void cmd_threads(int argc, char *argv[]);
static void cmd_quit(int argc, char *argv[]);

volatile bool quit = false;

cmd_desc_t cmd_table[] = {
	{ 1,	"break",	cmd_break },
	{ 1,	"ct",		cmd_ct },
	{ 1,	"dbreak",	cmd_dbreak },
	{ 0,	"go",		cmd_go },
	{ 0,	"help",		cmd_help },
	{ 2,	"memr",		cmd_memr },
	{ 0,	"pwt",		cmd_pwt },
	{ 0,	"regs",		cmd_regs },
	{ 0 ,	"stop",		cmd_stop },
	{ 0,	"istep",	cmd_istep },
	{ 0,	"lbrk",		cmd_lbrk },
	{ 0,	"threads",	cmd_threads },
	{ 0,	"quit",		cmd_quit },
	{ -1,	NULL,		NULL }
};

static void cmd_break(int argc, char *argv[])
{
	uintptr_t addr;

	(void)argc;
	addr = strtoul(argv[1], NULL, 0);

	cons_printf("You requested a breakpoint at 0x%x\n", addr);
	arch_breakpoint_add(addr);
}

static void cmd_ct(int argc, char *argv[])
{
	int tid;

	link_t *cur;
	dthread_t *dt;

	(void)argc;
	tid = strtoul(argv[1], NULL, 0);

	dt = NULL;
	for (cur = dthreads.next; cur != &dthreads; cur = cur->next) {
		dt = list_get_instance(cur, dthread_t, link);
		if (dt->id == tid) break;
	}
	
	if (dt->id == tid) {
		cwt = dt;
		cons_printf("changed working thread to: %d [hash 0x%x]\n",
		    cwt->id, cwt->hash);
	} else {
		cons_printf("no such thread\n");
	}
}

static void cmd_dbreak(int argc, char *argv[])
{
	int bid;

	(void)argc;
	bid = strtoul(argv[1], NULL, 0);

	printf("remove breakpoint %d\n", bid);

	arch_breakpoint_remove(bid);
}


void cmd_go(int argc, char *argv[])
{
	link_t *cur;
	dthread_t *dt;

	(void)argc; (void)argv;

	dt = NULL;
	for (cur = dthreads.next; cur != &dthreads; cur = cur->next) {
		dt = list_get_instance(cur, dthread_t, link);
		arch_set_singlestep(dt, 0);
		dthread_resume(dt);
	}	
}

void cmd_help(int argc, char *argv[])
{
	int i;

	(void)argc; (void)argv;
	i = 0;
	while (cmd_table[i].name != NULL) {
		cons_printf("%s\n", cmd_table[i].name);
		++i;
	}
}

void cmd_istep(int argc, char *argv[])
{
	(void)argc; (void)argv;

	arch_set_singlestep(cwt, 1);
	dthread_resume(cwt);
}

static void cmd_lbrk(int argc, char *argv[])
{
	(void)argc; (void)argv;

	arch_breakpoint_list();
}


#define BYTES_PER_LINE 16

static void cmd_memr(int argc, char *argv[])
{
	uintptr_t addr;
	size_t length;
	uint8_t buf[BYTES_PER_LINE];
	int to_read, i;
	int rc;

	(void)argc;
	addr = strtoul(argv[1], NULL, 0);
	length = strtoul(argv[2], NULL, 0);

	while (length > 0) {
		to_read = length < BYTES_PER_LINE ? length : BYTES_PER_LINE;

		rc = udebug_mem_read(app_phone, buf, addr, to_read);
		if (rc < 0) {
			cons_printf("error %d\n", rc);
			return;
		}

		cons_printf("0x%x:", addr);
		for (i = 0; i < to_read; ++i) {
			cons_printf(" %02x", buf[i]);
		}
		for (i = to_read; i < BYTES_PER_LINE; ++i) {
			cons_printf("   ");
		}

		putchar ('\t');

		for (i = 0; i < to_read; ++i) {
			if (buf[i] >= 32 && buf[i] < 127)
				putchar(buf[i]);
			else
				putchar('.');
		}
		cons_printf("\n");

		addr += to_read;
		length -= to_read;
	}
}

void cmd_pwt(int argc, char *argv[])
{
	(void)argc; (void)argv;

	cons_printf("working thread: %d [hash 0x%x]\n", cwt->id, cwt->hash);
}

void cmd_regs(int argc, char *argv[])
{
	(void)argc; (void)argv;

	if (cwt) arch_dump_regs(cwt->hash);
}

void cmd_stop(int argc, char *argv[])
{
	link_t *cur;
	dthread_t *dt;
	int rc;

	(void)argc; (void)argv;

	dt = NULL;
	for (cur = dthreads.next; cur != &dthreads; cur = cur->next) {
		dt = list_get_instance(cur, dthread_t, link);
		if (!dt->stopped) {
			rc = udebug_stop(app_phone, dt->hash);
			if (rc < 0) {
				printf("failed halting thread %d\n", dt->id);
			}
		}
	}	
}


void cmd_threads(int argc, char *argv[])
{
	link_t *cur;
	dthread_t *dt;

	(void)argc;

	dt = NULL;
	for (cur = dthreads.next; cur != &dthreads; cur = cur->next) {
		dt = list_get_instance(cur, dthread_t, link);
		cons_printf("%d [hash 0x%x]\n", dt->id, dt->hash);
	}	
}


static void cmd_quit(int argc, char *argv[])
{
	(void)argc; (void)argv;
	quit = true;
}

/** @}
 */
