/*
 * 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 <stdlib.h>
#include <adt/list.h>
#include <assert.h>
#include <futex.h>
#include <async.h>
#include <errno.h>

#include "include/arch.h"
#include "main.h"
#include "breakpoint.h"

static int last_bkpt_id = 0;

/** List of breakpoints that are set on the application */
link_t bkpts;

/** Pointer to currently active breakpoint or NULL if none */
breakpoint_t *active_bkpt = NULL;

void breakpoint_init(void)
{
	list_initialize(&bkpts);
}

int breakpoint_add(uintptr_t addr)
{
	breakpoint_t *old_b, *b;
	int rc;

	old_b = breakpoint_find_by_addr(addr);
	if (old_b != NULL) {
		printf("Breakpoint already exists at address 0x%lx. ID:%d\n",
			old_b->addr, old_b->id);
		return EEXISTS;
	}

	b = malloc(sizeof(breakpoint_t));
	if (!b) {
		printf("malloc failed\n");
		return ENOMEM;
	}

	b->addr = addr;
	rc = arch_breakpoint_set(b);
	if (rc < 0) {
		printf("Failed to add breakpoint at 0x%x\n", addr);
		return rc;
	}

	b->id = ++last_bkpt_id;

	list_append(&b->link, &bkpts);

	printf("Added breakpoint %d at address 0x%lx\n", b->id, b->addr);

	return 0;
}

/** Get breakpoint struct by breakpoint id.
 *
 * @param bid Breakpoint id.
 *
 * @return Pointer to breakpoint struct or NULL if not found.
 */
breakpoint_t *breakpoint_find_by_id(int bid)
{
	link_t *cur;
	breakpoint_t *b;

	for (cur = bkpts.next; cur != &bkpts; cur = cur->next) {
		b = list_get_instance(cur, breakpoint_t, link);
		if (b->id == bid) return b;
	}

	return NULL;
}

/** Get breakpoint struct by breakpoint id.
 *
 * @param bid Breakpoint id.
 *
 * @return Pointer to breakpoint struct or NULL if not found.
 */
breakpoint_t *breakpoint_find_by_addr(uintptr_t addr)
{
	link_t *cur;
	breakpoint_t *b;

	for (cur = bkpts.next; cur != &bkpts; cur = cur->next) {
		b = list_get_instance(cur, breakpoint_t, link);
		if (b->addr == addr) return b;
	}

	return NULL;
}

void breakpoint_remove(int bid)
{
	breakpoint_t *b;
	int rc;

	b = breakpoint_find_by_id(bid);
	if (!b) {
		printf("No such breakpoint\n");
		return;
	}

	rc = arch_breakpoint_remove(b);
	if (rc < 0) {
		printf("Failed to remove breakpoint %d\n", b->id);
		return;
	}

	list_remove(&b->link);
	free(b);
}

void breakpoint_list(void)
{
	breakpoint_t *b;
	link_t *cur;
	int count;

	count = 0;
	for (cur = bkpts.next; cur != &bkpts; cur = cur->next) {
		b = list_get_instance(cur, breakpoint_t, link);
		printf("%d at 0x%lx\n", b->id, b->addr);
		++count;
	}
	if (!count) printf("No breakpoints set\n");
}

/** @}
 */
