/*
* 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 <sys/types.h>
#include <udebug.h>
#include <kernel/arch/context_offset.h>
#include "../../../cons.h"
#include "../../../main.h"
#include "../../../include/arch.h"
#define OPCODE_INT3 0xCC
static breakpoint_t *lifted_brkpt;
void arch_breakpoint_add(uintptr_t addr)
{
char brkp[1];
int rc;
breakpoint_t *brk;
int i;
brk = NULL;
for (i = 1; i < MAX_BRKPTS; i++)
if (brk_list[i].set == 0) { brk = brk_list+i; break; }
if (!brk) {
cons_printf("too many breakpoints\n");
return;
}
rc = udebug_mem_read(app_phone, &brk->arch.back, addr, 1);
cons_printf("udebug_mem_read() -> %d\n", rc);
brkp[0] = OPCODE_INT3;
rc = udebug_mem_write(app_phone, brkp, addr, 1);
cons_printf("udebug_mem_write() -> %d\n", rc);
brk->addr = addr;
brk->set = 1;
cons_printf("Added breakpoint %d\n", i);
}
void arch_breakpoint_remove(int id)
{
int rc;
if (id < 1 || id >= MAX_BRKPTS || brk_list[id].set == 0) {
cons_printf("No such breakpoint\n");
return;
}
if (lifted_brkpt == &brk_list[id]) {
lifted_brkpt = NULL;
} else {
rc = udebug_mem_write(app_phone, &brk_list[id].arch.back, brk_list[id].addr, 1);
if (rc
< 0) { printf("error writing mem\n"); return; }
}
brk_list[id].set = 0;
cons_printf("Breakpoint removed\n");
}
void arch_breakpoint_list(void)
{
int i, cnt;
cnt = 0;
for (i = 0; i < MAX_BRKPTS; ++i) {
if (brk_list[i].set != 0) {
cons_printf("Breakpoint %d at 0x%lx\n", i,
brk_list[i].addr);
++cnt;
}
}
if (cnt == 0) cons_printf("No breakpoints set\n");
}
void arch_event_breakpoint(thash_t thread_hash)
{
static istate_t istate;
int rc;
rc = udebug_regs_read(app_phone, thread_hash, &istate);
// cons_printf("udebug_regs_read -> %d\n", rc);
// cons_printf("EIP was 0x%08x\n", istate.eip);
int brk_addr = istate.eip - 1;
int bi;
for (bi = 0; bi < MAX_BRKPTS; bi++) {
if (brk_list[bi].set && brk_list[bi].addr == brk_addr)
break;
}
if (bi < MAX_BRKPTS) {
cons_printf("breakpoint %d hit\n", bi);
istate.eip = brk_addr;
istate.eflags |= 0x0100; /* trap flag */
// cons_printf("setting EIP to 0x%08x\n", istate.eip);
rc = udebug_regs_write(app_phone, thread_hash, &istate);
if (rc
< 0) { printf("error writing regs\n"); return; }
rc = udebug_mem_write(app_phone, &brk_list[bi].arch.back, brk_addr, 1);
if (rc
< 0) { printf("error writing mem\n"); return; }
// cons_printf("udebug_mem_write(phone, 0x%x, 0x%02x, 1) -> %d\n", brk_addr, brk_list[bi].arch.back, rc);
lifted_brkpt = &brk_list[bi];
breakpoint_hit();
} else {
cons_printf("unrecognized breakpoint at 0x%x\n", brk_addr);
}
}
void arch_event_trap(dthread_t *dt)
{
static istate_t istate;
unsigned char brkinstr[1];
int rc;
// cons_printf("trap event\n");
breakpoint_t *lb = lifted_brkpt;
if (lb) {
brkinstr[0] = OPCODE_INT3;
rc = udebug_mem_write(app_phone, brkinstr, lb->addr, 1);
// cons_printf("restore breakpoint -> %d\n", rc);
lifted_brkpt = NULL;
}
if (!dt->arch.singlestep) {
rc = udebug_regs_read(app_phone, dt->hash, &istate);
// cons_printf("udebug_regs_read -> %d\n", rc);
istate.eflags &= ~0x0100; /* trap flag */
rc = udebug_regs_write(app_phone, dt->hash, &istate);
} else {
// printf("ss-hit\n");
singlestep_hit();
}
}
void arch_dump_regs(thash_t thash)
{
static istate_t istate;
int rc;
rc = udebug_regs_read(app_phone, thash, &istate);
if (rc < 0) { cons_printf("Error reading regs\n"); return; }
cons_printf(
"eip:%08x eflags:%08x eax:%08x ebx:%08x ecx:%08x edx:%08x\n"
"esi:%08x edi:%08x cs:%04x ds:%04x es:%04x fs:%04x gs:%04x\n",
istate.eip, istate.eflags, istate.eax, istate.ebx,
istate.ecx, istate.edx, istate.esi, istate.edi, istate.cs,
istate.ds, istate.es, istate.fs, istate.gs);
}
void arch_set_singlestep(dthread_t *dt, int enable)
{
static istate_t istate;
int rc;
rc = udebug_regs_read(app_phone, dt->hash, &istate);
if (rc
< 0) { printf("regs read failed\n"); return; }
if (enable) istate.eflags |= 0x0100; /* trap flag */
else if (!lifted_brkpt) istate.eflags &= ~0x0100; /* trap flag */
rc = udebug_regs_write(app_phone, dt->hash, &istate);
if (rc
< 0) { printf("regs write failed\n"); return; }
dt->arch.singlestep = enable;
}
/** @}
*/