/*
* 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 "../../../main.h"
#include "../../../include/arch.h"
#define OPCODE_BREAK 0x0000000d
void arch_breakpoint_add(uintptr_t addr)
{
uint32_t brkp;
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;
break;
}
if (!brk) {
printf("too many breakpoints\n");
return;
}
rc = udebug_mem_read(app_phone, &brk->arch.back, addr, sizeof(&brk->arch.back));
printf("udebug_mem_read() -> %d\n", rc
);
brkp = OPCODE_BREAK;
rc = udebug_mem_write(app_phone, &brkp, addr, sizeof(brkp));
// for (i=0; i<256; i++) rc = udebug_mem_write(app_phone, &brkp, addr+4*i, sizeof(brkp));
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;
uint32_t epc;
int brk_addr;
uint32_t brkp;
brkp = OPCODE_BREAK;
rc = udebug_regs_read(app_phone, thread_hash, buffer);
printf("udebug_regs_read -> %d\n", rc
);
epc = buffer[EOFFSET_EPC/sizeof(unsigned)];
printf("EPC was 0x%08x\n", epc
);
brk_addr = epc;
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) {
printf("breakpoint %d hit\n", bi
);
rc = udebug_mem_write(app_phone, &brk_list[bi].arch.back, brk_addr, 4);
printf("udebug_mem_write(phone, 0x%x, 0x%02x, 1) -> %d\n", brk_addr
, brk_list
[bi
].
arch.
back, rc
);
rc = udebug_mem_read(app_phone, &brk_list[bi].arch.back, brk_addr+4, 4);
rc = udebug_mem_write(app_phone, &brkp, brk_addr+4, 4);
lifted_brkpt = &brk_list[bi];
return;
}
for (bi = 0; bi < MAX_BRKPTS; bi++) {
if (brk_list[bi].set && brk_list[bi].addr + 4 == brk_addr)
break;
}
if (bi < MAX_BRKPTS) {
printf("restoring breakpoint %d\n", bi
);
rc = udebug_mem_write(app_phone, &brk_list[bi].arch.back, brk_addr, 4);
rc = udebug_mem_read(app_phone, &brk_list[bi].arch.back, brk_addr-4, 4);
rc = udebug_mem_write(app_phone, &brkp, brk_addr-4, 4);
lifted_brkpt = NULL;
return;
}
printf("unrecognized breakpoint at 0x%x\n", brk_addr
);
}
void arch_event_trap(thash_t thread_hash)
{
/* Unused */
(void)thread_hash;
}
/** @}
*/