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