/*
* 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 <assert.h>
#include <sys/types.h>
#include <udebug.h>
#include "../../../cons.h"
#include "../../../main.h"
#include "../../../breakpoint.h"
#include "../../../include/arch.h"
#define OPCODE_BREAK 0x0000000d
static istate_t istate;
int arch_breakpoint_set(breakpoint_t *b)
{
return bstore_push(&b->arch.bs, b->addr, OPCODE_BREAK);
}
int arch_breakpoint_remove(breakpoint_t *b)
{
return bstore_pop(&b->arch.bs);
}
static void _ev_breakpoint(thash_t thread_hash)
{
breakpoint_t *b;
dthread_t *dt;
int rc;
uint32_t epc;
int brk_addr;
uint32_t brkp;
brkp = OPCODE_BREAK;
cons_printf("arch_event_breakpoint\n");
rc = udebug_regs_read(app_phone, thread_hash, &istate);
cons_printf("udebug_regs_read -> %d\n", rc);
epc = istate_get_pc(&istate);
cons_printf("EPC was 0x%08x\n", epc);
brk_addr = epc;
dt = dthread_get();
if (active_bkpt != NULL) {
assert(active_bkpt
->arch.
bs.
address == brk_addr
);
/* A breakpoint-clearing BRK has been hit */
cons_printf("restoring breakpoint %d\n", b->id);
rc = bstore_pop(&b->arch.bs);
if (rc != 0) return;
rc = bstore_push(&b->arch.bs, brk_addr - 4, OPCODE_BREAK);
if (rc != 0) return;
active_bkpt = NULL;
return;
}
b = breakpoint_find_by_addr(brk_addr);
if (b == NULL) {
cons_printf("Unrecognized breakpoint at 0x%lx\n", brk_addr);
}
/* A breakpoint has been hit */
cons_printf("breakpoint_hit...\n");
breakpoint_hit(b);
/* While in breakpoint_hit(), singlestep was activated */
if (dt->arch.singlestep) return;
cons_printf("move breakpoint\b");
rc = bstore_pop(&b->arch.bs);
if (rc != 0) return;
/*
* There could be another breakpoint at brk_addr + 4,
* but that's okay. We'll pop the active breakpoint bs
* before doing anything else.
*/
rc = bstore_push(&b->arch.bs, brk_addr + 4, OPCODE_BREAK);
if (rc != 0) return;
active_bkpt = b;
b->active = true;
cons_printf("end_hit...\n");
}
static void _ev_singlestep(thash_t thread_hash)
{
dthread_t *dt;
int rc;
uint32_t epc;
int brk_addr;
uint32_t brkp;
dt = dthread_get();
brkp = OPCODE_BREAK;
cons_printf("arch_event_breakpoint\n");
rc = udebug_regs_read(app_phone, thread_hash, &istate);
cons_printf("udebug_regs_read -> %d\n", rc);
epc = istate_get_pc(&istate);
cons_printf("EPC was 0x%08x\n", epc);
brk_addr = epc;
if (dt->arch.cur.valid) {
cons_printf("restore breakpoint BRK\n");
rc = bstore_pop(&dt->arch.cur);
}
cons_printf("clear singlestep BRK\n");
rc = bstore_pop(&dt->arch.next);
dt->arch.singlestep = false;
singlestep_hit();
}
void arch_event_breakpoint(thash_t thread_hash)
{
dthread_t *dt;
dt = dthread_get();
if (dt->arch.singlestep) {
_ev_singlestep(thread_hash);
} else {
_ev_breakpoint(thread_hash);
}
}
void arch_event_trap(dthread_t *dt)
{
/* Unused */
(void)dt;
}
void arch_dump_regs(thash_t thash)
{
/* TODO */
}
void arch_singlestep(dthread_t *dt)
{
int rc;
uint32_t epc;
breakpoint_t *b;
uint32_t old_instr;
assert(dt
->arch.
singlestep == false);
cons_printf("arch_singlestep(dt)\n");
rc = udebug_regs_read(app_phone, dt->hash, &istate);
cons_printf("udebug_regs_read -> %d\n", rc);
epc = istate_get_pc(&istate);
cons_printf("EPC was 0x%08x\n", epc);
cons_printf("initial set singlestep\n");
b = breakpoint_find_by_addr(epc);
if (b != NULL) {
/* Cover breakpoint with old instruction */
old_instr = b->arch.bs.value;
rc = bstore_push(&dt->arch.cur, epc, old_instr);
if (rc < 0) return;
}
/* Cover next instruction with BREAK */
rc = bstore_push(&dt->arch.next, epc + 4, OPCODE_BREAK);
if (rc < 0) return;
dt->arch.singlestep = true;
dthread_resume(dt);
}
/** @}
*/