Subversion Repositories HelenOS

Rev

Rev 2942 | Rev 2947 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2923 svoboda 1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup debug
30
 * @{
31
 */
32
/** @file
33
 */
34
 
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <sys/types.h>
38
#include <udebug.h>
39
 
2941 svoboda 40
#include <kernel/arch/context_offset.h>
41
 
2936 svoboda 42
#include "../../../cons.h"
2923 svoboda 43
#include "../../../main.h"
44
#include "../../../include/arch.h"
45
 
46
#define OPCODE_INT3     0xCC
47
 
2943 svoboda 48
static breakpoint_t *lifted_brkpt;
49
 
2923 svoboda 50
void arch_breakpoint_add(uintptr_t addr)
51
{
52
    char brkp[1];
53
    int rc;
54
    breakpoint_t *brk;
55
    int i;
56
 
57
    brk = NULL;
2943 svoboda 58
    for (i = 1; i < MAX_BRKPTS; i++)
2942 svoboda 59
        if (brk_list[i].set == 0) { brk = brk_list+i; break; }
2923 svoboda 60
 
61
    if (!brk) {
2936 svoboda 62
        cons_printf("too many breakpoints\n");
2923 svoboda 63
        return;
64
    }
65
 
66
    rc = udebug_mem_read(app_phone, &brk->arch.back, addr, 1);
2936 svoboda 67
    cons_printf("udebug_mem_read() -> %d\n", rc);
2923 svoboda 68
    brkp[0] = OPCODE_INT3;
69
    rc = udebug_mem_write(app_phone, brkp, addr, 1);
2936 svoboda 70
    cons_printf("udebug_mem_write() -> %d\n", rc);
2923 svoboda 71
 
72
    brk->addr = addr;
73
    brk->set = 1;
2943 svoboda 74
 
75
    cons_printf("Added breakpoint %d\n", i);
2923 svoboda 76
}
77
 
2943 svoboda 78
void arch_breakpoint_remove(int id)
79
{
80
    int rc;
2923 svoboda 81
 
2943 svoboda 82
    if (id < 1 || id >= MAX_BRKPTS || brk_list[id].set == 0) {
83
        cons_printf("No such breakpoint\n");
84
        return;
85
    }
86
 
87
    if (lifted_brkpt == &brk_list[id]) {
88
        lifted_brkpt = NULL;
89
    } else {
90
            rc = udebug_mem_write(app_phone, &brk_list[id].arch.back, brk_list[id].addr, 1);
91
        if (rc < 0) { printf("error writing mem\n"); return; }     
92
    }
93
 
94
    brk_list[id].set = 0;
95
 
96
    cons_printf("Breakpoint removed\n");
97
}
98
 
99
void arch_breakpoint_list(void)
100
{
101
    int i;
102
 
103
    for (i = 0; i < MAX_BRKPTS; ++i) {
104
        if (brk_list[i].set != 0) {
105
            printf("Breakpoint %d at 0x%lx\n", i, brk_list[i].addr);
106
        }
107
    }
108
}
109
 
2923 svoboda 110
void arch_event_breakpoint(thash_t thread_hash)
111
{
2942 svoboda 112
    static istate_t istate;
2923 svoboda 113
    int rc;
114
 
2941 svoboda 115
    rc = udebug_regs_read(app_phone, thread_hash, &istate);
2942 svoboda 116
//  cons_printf("udebug_regs_read -> %d\n", rc);
117
//  cons_printf("EIP was 0x%08x\n", istate.eip);
2941 svoboda 118
    int brk_addr = istate.eip - 1;
2923 svoboda 119
    int bi;
120
    for (bi = 0; bi < MAX_BRKPTS; bi++) {
121
        if (brk_list[bi].set && brk_list[bi].addr == brk_addr)
122
            break;
123
    }
2924 svoboda 124
 
2923 svoboda 125
    if (bi < MAX_BRKPTS) {
2936 svoboda 126
        cons_printf("breakpoint %d hit\n", bi);
2924 svoboda 127
 
2941 svoboda 128
        istate.eip = brk_addr;
129
        istate.eflags |= 0x0100; /* trap flag */
2942 svoboda 130
//      cons_printf("setting EIP to 0x%08x\n", istate.eip);
2941 svoboda 131
        rc = udebug_regs_write(app_phone, thread_hash, &istate);
2942 svoboda 132
        if (rc < 0) { printf("error writing regs\n"); return; }
2923 svoboda 133
            rc = udebug_mem_write(app_phone, &brk_list[bi].arch.back, brk_addr, 1);
2942 svoboda 134
        if (rc < 0) { printf("error writing mem\n"); return; }
135
//      cons_printf("udebug_mem_write(phone, 0x%x, 0x%02x, 1) -> %d\n", brk_addr, brk_list[bi].arch.back, rc);
2923 svoboda 136
        lifted_brkpt = &brk_list[bi];
2942 svoboda 137
 
138
        breakpoint_hit();
2923 svoboda 139
    } else {
2936 svoboda 140
        cons_printf("unrecognized breakpoint at 0x%x\n", brk_addr);
2923 svoboda 141
    }
142
}
143
 
2942 svoboda 144
void arch_event_trap(dthread_t *dt)
2923 svoboda 145
{
2942 svoboda 146
    static istate_t istate;
2923 svoboda 147
    unsigned char brkinstr[1];
148
    int rc;
149
 
2942 svoboda 150
//  cons_printf("trap event\n");
2924 svoboda 151
 
2923 svoboda 152
    breakpoint_t *lb = lifted_brkpt;
2942 svoboda 153
    if (lb) {
154
        brkinstr[0] = OPCODE_INT3;
155
        rc = udebug_mem_write(app_phone, brkinstr, lb->addr, 1);
156
//      cons_printf("restore breakpoint -> %d\n", rc);
157
        lifted_brkpt = NULL;
158
    }
2923 svoboda 159
 
2942 svoboda 160
    if (!dt->arch.singlestep) {
161
        rc = udebug_regs_read(app_phone, dt->hash, &istate);
162
//      cons_printf("udebug_regs_read -> %d\n", rc);
163
        istate.eflags &= ~0x0100; /* trap flag */
164
        rc = udebug_regs_write(app_phone, dt->hash, &istate);
165
    } else {
166
//      printf("ss-hit\n");
167
        singlestep_hit();
168
    }
2923 svoboda 169
}
170
 
2941 svoboda 171
void arch_dump_regs(thash_t thash)
172
{
2942 svoboda 173
    static istate_t istate;
2941 svoboda 174
    int rc;
175
 
176
    printf("arch_dump_regs...\n");
177
 
178
    rc = udebug_regs_read(app_phone, thash, &istate);
179
    if (rc < 0) { cons_printf("Error reading regs\n"); return; }
180
 
181
    cons_printf(
182
        "eip:%08x eflags:%08x eax:%08x ebx:%08x ecx:%08x edx:%08x\n"
183
        "esi:%08x edi:%08x cs:%04x ds:%04x es:%04x fs:%04x gs:%04x\n",
184
        istate.eip, istate.eflags, istate.eax, istate.ebx,
185
        istate.ecx, istate.edx, istate.esi, istate.edi, istate.cs,
186
        istate.ds, istate.es, istate.fs, istate.gs);
187
}
188
 
2942 svoboda 189
void arch_set_singlestep(dthread_t *dt, int enable)
190
{
191
    static istate_t istate;
192
    int rc;
193
 
194
    rc = udebug_regs_read(app_phone, dt->hash, &istate);
195
    if (rc < 0) { printf("regs read failed\n"); return; }
196
 
197
    if (enable) istate.eflags |= 0x0100; /* trap flag */
2943 svoboda 198
    else if (!lifted_brkpt) istate.eflags &= ~0x0100; /* trap flag */
2942 svoboda 199
 
200
    rc = udebug_regs_write(app_phone, dt->hash, &istate);  
201
    if (rc < 0) { printf("regs write failed\n"); return; }
202
 
203
    dt->arch.singlestep = enable;
204
}
205
 
2923 svoboda 206
/** @}
207
 */