Subversion Repositories HelenOS

Rev

Rev 2923 | Rev 2935 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2923 Rev 2924
1
/*
1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup debug
29
/** @addtogroup debug
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <stdio.h>
35
#include <stdio.h>
36
#include <stdlib.h>
36
#include <stdlib.h>
37
#include <sys/types.h>
37
#include <sys/types.h>
38
#include <udebug.h>
38
#include <udebug.h>
39
 
39
 
40
#include "../../../main.h"
40
#include "../../../main.h"
41
#include "../../../include/arch.h"
41
#include "../../../include/arch.h"
42
 
42
 
43
#define OPCODE_INT3     0xCC
43
#define OPCODE_INT3     0xCC
44
 
44
 
45
#define ISTATE_OFF_EIP      12
45
#define ISTATE_OFF_EIP      12
46
#define ISTATE_OFF_EFLAGS   14
46
#define ISTATE_OFF_EFLAGS   14
47
 
47
 
48
void arch_breakpoint_add(uintptr_t addr)
48
void arch_breakpoint_add(uintptr_t addr)
49
{
49
{
50
    char brkp[1];
50
    char brkp[1];
51
    int rc;
51
    int rc;
52
    breakpoint_t *brk;
52
    breakpoint_t *brk;
53
    int i;
53
    int i;
54
 
54
 
55
    brk = NULL;
55
    brk = NULL;
56
    for (i = 0; i < MAX_BRKPTS; i++)
56
    for (i = 0; i < MAX_BRKPTS; i++)
57
        if (brk_list[i].set == 0) brk = brk_list+i;
57
        if (brk_list[i].set == 0) brk = brk_list+i;
58
 
58
 
59
    if (!brk) {
59
    if (!brk) {
60
        printf("too many breakpoints\n");
60
        printf("too many breakpoints\n");
61
        return;
61
        return;
62
    }
62
    }
63
 
63
 
64
    rc = udebug_mem_read(app_phone, &brk->arch.back, addr, 1);
64
    rc = udebug_mem_read(app_phone, &brk->arch.back, addr, 1);
65
    printf("udebug_mem_read() -> %d\n", rc);
65
    printf("udebug_mem_read() -> %d\n", rc);
66
    brkp[0] = OPCODE_INT3;
66
    brkp[0] = OPCODE_INT3;
67
    rc = udebug_mem_write(app_phone, brkp, addr, 1);
67
    rc = udebug_mem_write(app_phone, brkp, addr, 1);
68
    printf("udebug_mem_write() -> %d\n", rc);
68
    printf("udebug_mem_write() -> %d\n", rc);
69
 
69
 
70
    brk->addr = addr;
70
    brk->addr = addr;
71
    brk->set = 1;
71
    brk->set = 1;
72
}
72
}
73
 
73
 
74
static unsigned buffer[1024];
74
static unsigned buffer[1024];
75
static breakpoint_t *lifted_brkpt;
75
static breakpoint_t *lifted_brkpt;
76
 
76
 
77
void arch_event_breakpoint(thash_t thread_hash)
77
void arch_event_breakpoint(thash_t thread_hash)
78
{
78
{
79
    int rc;
79
    int rc;
80
 
80
 
81
    rc = udebug_regs_read(app_phone, thread_hash, buffer);
81
    rc = udebug_regs_read(app_phone, thread_hash, buffer);
82
    printf("udebug_regs_read -> %d\n", rc);
82
    printf("udebug_regs_read -> %d\n", rc);
83
    printf("EIP was 0x%08x\n", buffer[ISTATE_OFF_EIP]);
83
    printf("EIP was 0x%08x\n", buffer[ISTATE_OFF_EIP]);
84
    int brk_addr = buffer[ISTATE_OFF_EIP] - 1;
84
    int brk_addr = buffer[ISTATE_OFF_EIP] - 1;
85
    int bi;
85
    int bi;
86
    for (bi = 0; bi < MAX_BRKPTS; bi++) {
86
    for (bi = 0; bi < MAX_BRKPTS; bi++) {
87
        if (brk_list[bi].set && brk_list[bi].addr == brk_addr)
87
        if (brk_list[bi].set && brk_list[bi].addr == brk_addr)
88
            break;
88
            break;
89
    }
89
    }
-
 
90
 
90
    if (bi < MAX_BRKPTS) {
91
    if (bi < MAX_BRKPTS) {
-
 
92
        printf("breakpoint %d hit\n", bi);
-
 
93
 
91
        buffer[ISTATE_OFF_EIP] = brk_addr;
94
        buffer[ISTATE_OFF_EIP] = brk_addr;
92
        buffer[ISTATE_OFF_EFLAGS] |= 0x0100; /* trap flag */
95
        buffer[ISTATE_OFF_EFLAGS] |= 0x0100; /* trap flag */
93
        printf("setting EIP to 0x%08x\n", buffer[ISTATE_OFF_EIP]);
96
        printf("setting EIP to 0x%08x\n", buffer[ISTATE_OFF_EIP]);
94
        rc = udebug_regs_write(app_phone, thread_hash, buffer);
97
        rc = udebug_regs_write(app_phone, thread_hash, buffer);
95
            rc = udebug_mem_write(app_phone, &brk_list[bi].arch.back, brk_addr, 1);
98
            rc = udebug_mem_write(app_phone, &brk_list[bi].arch.back, brk_addr, 1);
96
        printf("udebug_mem_write(phone, 0x%x, 0x%02x, 1) -> %d\n", brk_addr, brk_list[bi].arch.back, rc);
99
        printf("udebug_mem_write(phone, 0x%x, 0x%02x, 1) -> %d\n", brk_addr, brk_list[bi].arch.back, rc);
97
        lifted_brkpt = &brk_list[bi];
100
        lifted_brkpt = &brk_list[bi];
98
    } else {
101
    } else {
99
        printf("unrecognized breakpoint at 0x%x\n", brk_addr);
102
        printf("unrecognized breakpoint at 0x%x\n", brk_addr);
100
    }
103
    }
101
}
104
}
102
 
105
 
103
void arch_event_trap(thash_t thread_hash)
106
void arch_event_trap(thash_t thread_hash)
104
{
107
{
105
    unsigned char brkinstr[1];
108
    unsigned char brkinstr[1];
106
    int rc;
109
    int rc;
107
 
110
 
-
 
111
    printf("trap event\n");
-
 
112
 
108
    breakpoint_t *lb = lifted_brkpt;
113
    breakpoint_t *lb = lifted_brkpt;
109
    brkinstr[0] = OPCODE_INT3;
114
    brkinstr[0] = OPCODE_INT3;
110
    rc = udebug_mem_write(app_phone, brkinstr, lb->addr, 1);
115
    rc = udebug_mem_write(app_phone, brkinstr, lb->addr, 1);
111
    printf("restore breakpoint -> %d\n", rc);
116
    printf("restore breakpoint -> %d\n", rc);
112
 
117
 
113
    rc = udebug_regs_read(app_phone, thread_hash, buffer);
118
    rc = udebug_regs_read(app_phone, thread_hash, buffer);
114
    printf("udebug_regs_read -> %d\n", rc);
119
    printf("udebug_regs_read -> %d\n", rc);
115
    buffer[ISTATE_OFF_EFLAGS] &= ~0x0100; /* trap flag */
120
    buffer[ISTATE_OFF_EFLAGS] &= ~0x0100; /* trap flag */
116
    rc = udebug_regs_write(app_phone, thread_hash, buffer);
121
    rc = udebug_regs_write(app_phone, thread_hash, buffer);
117
}
122
}
118
 
123
 
119
/** @}
124
/** @}
120
 */
125
 */
121
 
126