/*
* 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 "../../../main.h"
#include "../../../include/arch.h"
#define OPCODE_INT3 0xCC
#define ISTATE_OFF_EIP 12
#define ISTATE_OFF_EFLAGS 14
void arch_breakpoint_add(uintptr_t addr)
{
char brkp[1];
int rc;
breakpoint_t *brk;
int i;
brk = NULL;
for (i = 0; i < MAX_BRKPTS; i++)
if (brk_list[i].set == 0) brk = brk_list+i;
if (!brk) {
printf("too many breakpoints\n");
return;
}
rc = udebug_mem_read(app_phone, &brk->arch.back, addr, 1);
printf("udebug_mem_read() -> %d\n", rc
);
brkp[0] = OPCODE_INT3;
rc = udebug_mem_write(app_phone, brkp, addr, 1);
printf("udebug_mem_write() -> %d\n", rc
);
brk->addr = addr;
brk->set = 1;
}
static unsigned buffer[1024];
static breakpoint_t *lifted_brkpt;
void arch_event_breakpoint(thash_t thread_hash)
{
int rc;
rc = udebug_regs_read(app_phone, thread_hash, buffer);
printf("udebug_regs_read -> %d\n", rc
);
printf("EIP was 0x%08x\n", buffer
[ISTATE_OFF_EIP
]);
int brk_addr = buffer[ISTATE_OFF_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) {
buffer[ISTATE_OFF_EIP] = brk_addr;
buffer[ISTATE_OFF_EFLAGS] |= 0x0100; /* trap flag */
printf("setting EIP to 0x%08x\n", buffer
[ISTATE_OFF_EIP
]);
rc = udebug_regs_write(app_phone, thread_hash, buffer);
rc = udebug_mem_write(app_phone, &brk_list[bi].arch.back, brk_addr, 1);
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];
} else {
printf("unrecognized breakpoint at 0x%x\n", brk_addr
);
}
}
void arch_event_trap(thash_t thread_hash)
{
unsigned char brkinstr[1];
int rc;
breakpoint_t *lb = lifted_brkpt;
brkinstr[0] = OPCODE_INT3;
rc = udebug_mem_write(app_phone, brkinstr, lb->addr, 1);
printf("restore breakpoint -> %d\n", rc
);
rc = udebug_regs_read(app_phone, thread_hash, buffer);
printf("udebug_regs_read -> %d\n", rc
);
buffer[ISTATE_OFF_EFLAGS] &= ~0x0100; /* trap flag */
rc = udebug_regs_write(app_phone, thread_hash, buffer);
}
/** @}
*/