Subversion Repositories HelenOS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3006 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 <stdlib.h>
36
#include <libadt/list.h>
37
#include <assert.h>
38
#include <futex.h>
39
#include <async.h>
40
#include <errno.h>
41
 
42
#include "include/arch.h"
43
#include "main.h"
44
#include "breakpoint.h"
45
 
46
static int last_bkpt_id = 0;
47
 
48
/** List of breakpoints that are set on the application */
49
link_t bkpts;
50
 
51
/** Pointer to currently active breakpoint or NULL if none */
52
breakpoint_t *active_bkpt = NULL;
53
 
54
void breakpoint_init(void)
55
{
56
    list_initialize(&bkpts);
57
}
58
 
59
int breakpoint_add(uintptr_t addr)
60
{
61
    breakpoint_t *old_b, *b;
62
    int rc;
63
 
64
    old_b = breakpoint_find_by_addr(addr);
65
    if (old_b != NULL) {
66
        printf("Breakpoint already exists at address 0x%lx. ID:%d\n",
67
            old_b->addr, old_b->id);
68
        return EEXISTS;
69
    }
70
 
71
    b = malloc(sizeof(breakpoint_t));
72
    if (!b) {
73
        printf("malloc failed\n");
74
        return ENOMEM;
75
    }
76
 
77
    b->addr = addr;
78
    rc = arch_breakpoint_set(b);
79
    if (rc < 0) {
80
        printf("Failed to add breakpoint at 0x%x\n", addr);
81
        return rc;
82
    }
83
 
84
    b->id = ++last_bkpt_id;
85
 
86
    list_append(&b->link, &bkpts);
87
 
88
    printf("Added breakpoint %d at address 0x%lx\n", b->id, b->addr);
89
 
90
    return 0;
91
}
92
 
93
/** Get breakpoint struct by breakpoint id.
94
 *
95
 * @param bid Breakpoint id.
96
 *
97
 * @return Pointer to breakpoint struct or NULL if not found.
98
 */
99
breakpoint_t *breakpoint_find_by_id(int bid)
100
{
101
    link_t *cur;
102
    breakpoint_t *b;
103
 
104
    for (cur = bkpts.next; cur != &bkpts; cur = cur->next) {
105
        b = list_get_instance(cur, breakpoint_t, link);
106
        if (b->id == bid) return b;
107
    }
108
 
109
    return NULL;
110
}
111
 
112
/** Get breakpoint struct by breakpoint id.
113
 *
114
 * @param bid Breakpoint id.
115
 *
116
 * @return Pointer to breakpoint struct or NULL if not found.
117
 */
118
breakpoint_t *breakpoint_find_by_addr(uintptr_t addr)
119
{
120
    link_t *cur;
121
    breakpoint_t *b;
122
 
123
    for (cur = bkpts.next; cur != &bkpts; cur = cur->next) {
124
        b = list_get_instance(cur, breakpoint_t, link);
125
        if (b->addr == addr) return b;
126
    }
127
 
128
    return NULL;
129
}
130
 
131
void breakpoint_remove(int bid)
132
{
133
    breakpoint_t *b;
134
    int rc;
135
 
136
    b = breakpoint_find_by_id(bid);
137
    if (!b) {
138
        printf("No such breakpoint\n");
139
        return;
140
    }
141
 
142
    rc = arch_breakpoint_remove(b);
143
    if (rc < 0) {
144
        printf("Failed to remove breakpoint %d\n", b->id);
145
        return;
146
    }
147
 
148
    list_remove(&b->link);
149
    free(b);
150
}
151
 
152
void breakpoint_list(void)
153
{
154
    breakpoint_t *b;
155
    link_t *cur;
156
    int count;
157
 
158
    count = 0;
159
    for (cur = bkpts.next; cur != &bkpts; cur = cur->next) {
160
        b = list_get_instance(cur, breakpoint_t, link);
161
        printf("%d at 0x%lx\n", b->id, b->addr);
162
        ++count;
163
    }
164
    if (!count) printf("No breakpoints set\n");
165
}
166
 
167
/** @}
168
 */