Rev 2915 | Rev 2922 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2915 | Rev 2918 | ||
---|---|---|---|
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 <unistd.h> |
37 | #include <unistd.h> |
38 | #include <syscall.h> |
38 | #include <syscall.h> |
39 | #include <ipc/ipc.h> |
39 | #include <ipc/ipc.h> |
40 | #include <fibril.h> |
40 | #include <fibril.h> |
41 | #include <errno.h> |
41 | #include <errno.h> |
42 | #include <udebug.h> |
42 | #include <udebug.h> |
43 | #include <async.h> |
43 | #include <async.h> |
44 | #include <string.h> |
44 | #include <string.h> |
45 | 45 | ||
46 | #include "cmd.h" |
46 | #include "cmd.h" |
47 | #include "main.h" |
47 | #include "main.h" |
48 | 48 | ||
49 | void thread_debug_start(unsigned thread_hash); |
49 | void thread_debug_start(unsigned thread_hash); |
50 | 50 | ||
51 | #define INBUF_SIZE 64 |
51 | #define INBUF_SIZE 64 |
52 | char in_buf[INBUF_SIZE]; |
52 | char in_buf[INBUF_SIZE]; |
53 | 53 | ||
54 | #define MAX_ARGC 10 |
54 | #define MAX_ARGC 10 |
55 | int cmd_argc; |
55 | int cmd_argc; |
56 | char *cmd_argv[MAX_ARGC + 1]; /* need one spare field for cmd_split() */ |
56 | char *cmd_argv[MAX_ARGC + 1]; /* need one spare field for cmd_split() */ |
57 | 57 | ||
58 | #define THBUF_SIZE 64 |
58 | #define THBUF_SIZE 64 |
59 | thash_t thread_hash_buf[THBUF_SIZE]; |
59 | thash_t thread_hash_buf[THBUF_SIZE]; |
60 | unsigned n_threads; |
60 | unsigned n_threads; |
61 | 61 | ||
62 | int next_thread_id; |
62 | int next_thread_id; |
63 | 63 | ||
64 | int app_phone; |
64 | int app_phone; |
65 | volatile bool abort_debug; |
65 | volatile bool abort_debug; |
66 | 66 | ||
67 | thash_t thash; |
67 | thash_t thash; |
68 | volatile int paused; |
68 | volatile int paused; |
69 | 69 | ||
70 | void read_line(char *buffer, int n) |
70 | void read_line(char *buffer, int n) |
71 | { |
71 | { |
72 | char c; |
72 | char c; |
73 | int i; |
73 | int i; |
74 | 74 | ||
75 | i = 0; |
75 | i = 0; |
76 | while (i < n - 1) { |
76 | while (i < n - 1) { |
77 | c = getchar(); |
77 | c = getchar(); |
78 | if (c == '\n') break; |
78 | if (c == '\n') break; |
79 | if (c == '\b') { |
79 | if (c == '\b') { |
80 | if (i > 0) { |
80 | if (i > 0) { |
81 | putchar('\b'); |
81 | putchar('\b'); |
82 | --i; |
82 | --i; |
83 | } |
83 | } |
84 | continue; |
84 | continue; |
85 | } |
85 | } |
86 | 86 | ||
87 | putchar(c); |
87 | putchar(c); |
88 | buffer[i++] = c; |
88 | buffer[i++] = c; |
89 | } |
89 | } |
90 | 90 | ||
91 | putchar('\n'); |
91 | putchar('\n'); |
92 | buffer[i] = '\0'; |
92 | buffer[i] = '\0'; |
93 | } |
93 | } |
94 | 94 | ||
95 | void command_split(char *cmd_str) |
95 | void command_split(char *cmd_str) |
96 | { |
96 | { |
97 | char *p = cmd_str; |
97 | char *p = cmd_str; |
98 | 98 | ||
99 | if (*p == '\0') { |
99 | if (*p == '\0') { |
100 | cmd_argc = 0; |
100 | cmd_argc = 0; |
101 | return; |
101 | return; |
102 | } |
102 | } |
103 | 103 | ||
104 | cmd_argc = 1; |
104 | cmd_argc = 1; |
105 | cmd_argv[0] = p; |
105 | cmd_argv[0] = p; |
106 | 106 | ||
107 | while (*p != '\0') { |
107 | while (*p != '\0') { |
108 | if (*p == ' ') { |
108 | if (*p == ' ') { |
109 | cmd_argv[cmd_argc++] = p + 1; |
109 | cmd_argv[cmd_argc++] = p + 1; |
110 | *p = '\0'; |
110 | *p = '\0'; |
111 | } |
111 | } |
112 | ++p; |
112 | ++p; |
113 | } |
113 | } |
114 | } |
114 | } |
115 | 115 | ||
116 | void command_run(void) |
116 | void command_run(void) |
117 | { |
117 | { |
118 | int i; |
118 | int i; |
119 | int cmp_len; |
119 | int cmp_len; |
120 | int len; |
120 | int len; |
121 | 121 | ||
122 | int idx_found; |
122 | int idx_found; |
123 | int num_found; |
123 | int num_found; |
124 | 124 | ||
125 | len = strlen(cmd_argv[0]); |
125 | len = strlen(cmd_argv[0]); |
126 | cmp_len = 1; |
126 | cmp_len = 1; |
127 | 127 | ||
128 | while (cmp_len <= len + 1) { |
128 | while (cmp_len <= len + 1) { |
129 | 129 | ||
130 | num_found = 0; |
130 | num_found = 0; |
131 | i = 0; |
131 | i = 0; |
132 | while (cmd_table[i].name != NULL) { |
132 | while (cmd_table[i].name != NULL) { |
133 | if (strncmp(cmd_table[i].name, cmd_argv[0], cmp_len) == 0) { |
133 | if (strncmp(cmd_table[i].name, cmd_argv[0], cmp_len) == 0) { |
134 | idx_found = i; |
134 | idx_found = i; |
135 | ++num_found; |
135 | ++num_found; |
136 | } |
136 | } |
137 | ++i; |
137 | ++i; |
138 | } |
138 | } |
139 | 139 | ||
140 | if (num_found < 2) break; |
140 | if (num_found < 2) break; |
141 | 141 | ||
142 | --cmp_len; |
142 | --cmp_len; |
143 | } |
143 | } |
144 | 144 | ||
145 | if (num_found == 0) { |
145 | if (num_found == 0) { |
146 | printf("Unknown command. Try one of:\n"); |
146 | printf("Unknown command. Try one of:\n"); |
147 | cmd_help(0, NULL); |
147 | cmd_help(0, NULL); |
148 | return; |
148 | return; |
149 | } |
149 | } |
150 | 150 | ||
151 | if (cmd_argc - 1 != cmd_table[idx_found].argc) { |
151 | if (cmd_argc - 1 != cmd_table[idx_found].argc) { |
152 | printf("Command '%s' expects %d arguments\n", |
152 | printf("Command '%s' expects %d arguments\n", |
153 | cmd_table[idx_found].name, cmd_table[idx_found].argc); |
153 | cmd_table[idx_found].name, cmd_table[idx_found].argc); |
154 | return; |
154 | return; |
155 | } |
155 | } |
156 | 156 | ||
157 | (*cmd_table[idx_found].proc)(cmd_argc, cmd_argv); |
157 | (*cmd_table[idx_found].proc)(cmd_argc, cmd_argv); |
158 | } |
158 | } |
159 | 159 | ||
160 | 160 | ||
161 | int task_connect(int taskid) |
161 | int task_connect(int taskid) |
162 | { |
162 | { |
163 | int rc; |
163 | int rc; |
- | 164 | unsigned evmask; |
|
164 | 165 | ||
165 | printf("ipc_connect_kbox(%d)... ", taskid); |
166 | printf("ipc_connect_kbox(%d)... ", taskid); |
166 | rc = ipc_connect_kbox(taskid); |
167 | rc = ipc_connect_kbox(taskid); |
167 | printf("-> %d\n", rc); |
168 | printf("-> %d\n", rc); |
168 | app_phone = rc; |
169 | app_phone = rc; |
169 | if (rc < 0) return rc; |
170 | if (rc < 0) return rc; |
170 | 171 | ||
171 | printf("udebug_begin()... "); |
172 | printf("udebug_begin()... "); |
172 | rc = udebug_begin(app_phone); |
173 | rc = udebug_begin(app_phone); |
173 | printf("-> %d\n", rc); |
174 | printf("-> %d\n", rc); |
174 | if (rc < 0) return rc; |
175 | if (rc < 0) return rc; |
175 | 176 | ||
- | 177 | evmask = UDEBUG_EM_ALL & ~(UDEBUG_EM_SYSCALL_B | UDEBUG_EM_SYSCALL_E); |
|
176 | printf("udebug_set_evmask(0x%x)... ", UDEBUG_EM_ALL); |
178 | printf("udebug_set_evmask(0x%x)... ", evmask); |
177 | rc = udebug_set_evmask(app_phone, UDEBUG_EM_ALL); |
179 | rc = udebug_set_evmask(app_phone, evmask); |
178 | printf("-> %d\n", rc); |
180 | printf("-> %d\n", rc); |
179 | if (rc < 0) return rc; |
181 | if (rc < 0) return rc; |
180 | 182 | ||
181 | return 0; |
183 | return 0; |
182 | } |
184 | } |
183 | 185 | ||
184 | int get_thread_list(void) |
186 | int get_thread_list(void) |
185 | { |
187 | { |
186 | int rc; |
188 | int rc; |
187 | int tb_copied; |
189 | int tb_copied; |
188 | int tb_needed; |
190 | int tb_needed; |
189 | int i; |
191 | int i; |
190 | 192 | ||
191 | printf("send IPC_M_DEBUG_THREAD_READ message\n"); |
193 | printf("send IPC_M_DEBUG_THREAD_READ message\n"); |
192 | rc = udebug_thread_read(app_phone, (unsigned)thread_hash_buf, |
194 | rc = udebug_thread_read(app_phone, (unsigned)thread_hash_buf, |
193 | THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed); |
195 | THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed); |
194 | printf("-> %d\n", rc); |
196 | printf("-> %d\n", rc); |
195 | if (rc < 0) return rc; |
197 | if (rc < 0) return rc; |
196 | 198 | ||
197 | n_threads = tb_copied / sizeof(unsigned); |
199 | n_threads = tb_copied / sizeof(unsigned); |
198 | 200 | ||
199 | printf("thread IDs:"); |
201 | printf("thread IDs:"); |
200 | for (i=0; i<n_threads; i++) { |
202 | for (i=0; i<n_threads; i++) { |
201 | printf(" %u", thread_hash_buf[i]); |
203 | printf(" %u", thread_hash_buf[i]); |
202 | } |
204 | } |
203 | printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned)); |
205 | printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned)); |
204 | 206 | ||
205 | return 0; |
207 | return 0; |
206 | } |
208 | } |
207 | 209 | ||
208 | void event_thread_b(unsigned hash) |
210 | void event_thread_b(unsigned hash) |
209 | { |
211 | { |
210 | async_serialize_start(); |
212 | async_serialize_start(); |
211 | printf("new thread, hash 0x%x\n", hash); |
213 | printf("new thread, hash 0x%x\n", hash); |
212 | async_serialize_end(); |
214 | async_serialize_end(); |
213 | 215 | ||
214 | thread_debug_start(hash); |
216 | thread_debug_start(hash); |
215 | } |
217 | } |
216 | 218 | ||
217 | void debug_loop(void *thread_hash_arg) |
219 | void debug_loop(void *thread_hash_arg) |
218 | { |
220 | { |
219 | int rc; |
221 | int rc; |
220 | unsigned ev_type; |
222 | unsigned ev_type; |
221 | unsigned thread_hash; |
223 | unsigned thread_hash; |
222 | unsigned thread_id; |
224 | unsigned thread_id; |
223 | unsigned val0, val1; |
225 | unsigned val0, val1; |
224 | 226 | ||
225 | thread_hash = (unsigned)thread_hash_arg; |
227 | thread_hash = (unsigned)thread_hash_arg; |
226 | thread_id = next_thread_id++; |
228 | thread_id = next_thread_id++; |
227 | 229 | ||
228 | printf("debug_loop(%d)\n", thread_id); |
230 | printf("debug_loop(%d)\n", thread_id); |
229 | 231 | ||
230 | while (!abort_debug) { |
232 | while (!abort_debug) { |
231 | 233 | ||
- | 234 | printf("go\n"); |
|
232 | /* Run thread until an event occurs */ |
235 | /* Run thread until an event occurs */ |
233 | rc = udebug_go(app_phone, thread_hash, |
236 | rc = udebug_go(app_phone, thread_hash, |
234 | &ev_type, &val0, &val1); |
237 | &ev_type, &val0, &val1); |
235 | 238 | ||
- | 239 | printf("..ev type %d\n", ev_type); |
|
- | 240 | ||
236 | // printf("rc = %d, ev_type=%d\n", rc, ev_type); |
241 | // printf("rc = %d, ev_type=%d\n", rc, ev_type); |
237 | if (ev_type == UDEBUG_EVENT_FINISHED) { |
242 | if (ev_type == UDEBUG_EVENT_FINISHED) { |
238 | printf("thread %u debugging finished\n", thread_id); |
243 | printf("thread %u debugging finished\n", thread_id); |
239 | break; |
244 | break; |
240 | } |
245 | } |
241 | 246 | ||
242 | if (rc >= 0) { |
247 | if (rc >= 0) { |
243 | switch (ev_type) { |
248 | switch (ev_type) { |
244 | case UDEBUG_EVENT_STOP: |
249 | case UDEBUG_EVENT_STOP: |
245 | printf("stop event\n"); |
250 | printf("stop event\n"); |
246 | printf("waiting for resume\n"); |
251 | printf("waiting for resume\n"); |
247 | while (paused) { |
252 | while (paused) { |
248 | usleep(1000000); |
253 | usleep(1000000); |
249 | fibril_yield(); |
254 | fibril_yield(); |
250 | printf("."); |
255 | printf("."); |
251 | } |
256 | } |
252 | printf("resumed\n"); |
257 | printf("resumed\n"); |
253 | break; |
258 | break; |
254 | case UDEBUG_EVENT_THREAD_B: |
259 | case UDEBUG_EVENT_THREAD_B: |
255 | event_thread_b(val0); |
260 | event_thread_b(val0); |
256 | break; |
261 | break; |
257 | case UDEBUG_EVENT_THREAD_E: |
262 | case UDEBUG_EVENT_THREAD_E: |
258 | printf("thread 0x%x exited\n", val0); |
263 | printf("thread 0x%x exited\n", val0); |
259 | abort_debug = true; |
264 | abort_debug = true; |
260 | break; |
265 | break; |
- | 266 | case UDEBUG_EVENT_BREAKPOINT: |
|
- | 267 | printf("breakpoint reached\n"); |
|
- | 268 | usleep(2000*2000); |
|
- | 269 | break; |
|
261 | default: |
270 | default: |
262 | printf("unknown event type %d\n", ev_type); |
271 | printf("unknown event type %d\n", ev_type); |
263 | break; |
272 | break; |
264 | } |
273 | } |
265 | } |
274 | } |
266 | 275 | ||
267 | } |
276 | } |
268 | 277 | ||
269 | printf("debug_loop(%d) exiting\n", thread_id); |
278 | printf("debug_loop(%d) exiting\n", thread_id); |
270 | } |
279 | } |
271 | 280 | ||
272 | void thread_debug_start(unsigned thread_hash) |
281 | void thread_debug_start(unsigned thread_hash) |
273 | { |
282 | { |
274 | fid_t fid; |
283 | fid_t fid; |
275 | 284 | ||
276 | thash = thread_hash; |
285 | thash = thread_hash; |
277 | 286 | ||
278 | fid = fibril_create(debug_loop, (void *)thread_hash); |
287 | fid = fibril_create(debug_loop, (void *)thread_hash); |
279 | if (fid == 0) { |
288 | if (fid == 0) { |
280 | printf("Warning: Failed creating fibril\n"); |
289 | printf("Warning: Failed creating fibril\n"); |
281 | } |
290 | } |
282 | fibril_add_ready(fid); |
291 | fibril_add_ready(fid); |
283 | } |
292 | } |
284 | 293 | ||
285 | void debug_active_task(void) |
294 | void debug_active_task(void) |
286 | { |
295 | { |
287 | int taskid; |
296 | int taskid; |
288 | int i; |
297 | int i; |
289 | int rc; |
298 | int rc; |
290 | int c; |
299 | int c; |
291 | 300 | ||
292 | printf("Breakpoint Debugger\n"); |
301 | printf("Breakpoint Debugger\n"); |
293 | printf("Press 'c' to connect\n"); |
302 | printf("Press 'c' to connect\n"); |
294 | while ((i = getchar()) != 'c') |
303 | while ((i = getchar()) != 'c') |
295 | putchar(i); |
304 | putchar(i); |
296 | 305 | ||
297 | taskid = 14; |
306 | taskid = 14; |
298 | rc = task_connect(taskid); |
307 | rc = task_connect(taskid); |
299 | if (rc < 0) { |
308 | if (rc < 0) { |
300 | printf("Failed to connect to task %d\n", taskid); |
309 | printf("Failed to connect to task %d\n", taskid); |
301 | return; |
310 | return; |
302 | } |
311 | } |
303 | 312 | ||
304 | printf("Connected to task %d\n", taskid); |
313 | printf("Connected to task %d\n", taskid); |
305 | 314 | ||
306 | rc = get_thread_list(); |
315 | rc = get_thread_list(); |
307 | if (rc < 0) { |
316 | if (rc < 0) { |
308 | printf("Failed to get thread list (error %d)\n", rc); |
317 | printf("Failed to get thread list (error %d)\n", rc); |
309 | return; |
318 | return; |
310 | } |
319 | } |
311 | 320 | ||
312 | abort_debug = false; |
321 | abort_debug = false; |
313 | 322 | ||
314 | for (i = 0; i < n_threads; i++) { |
323 | for (i = 0; i < n_threads; i++) { |
315 | thread_debug_start(thread_hash_buf[i]); |
324 | thread_debug_start(thread_hash_buf[i]); |
316 | } |
325 | } |
317 | 326 | ||
318 | while (!quit) { |
327 | while (!quit) { |
319 | printf("> "); |
328 | printf("> "); |
320 | read_line(in_buf, INBUF_SIZE); |
329 | read_line(in_buf, INBUF_SIZE); |
321 | command_split(in_buf); |
330 | command_split(in_buf); |
322 | if (cmd_argc == 0) continue; |
331 | if (cmd_argc == 0) continue; |
323 | 332 | ||
324 | command_run(); |
333 | command_run(); |
325 | } |
334 | } |
326 | 335 | ||
327 | printf("terminate debugging session...\n"); |
336 | printf("terminate debugging session...\n"); |
328 | abort_debug = true; |
337 | abort_debug = true; |
329 | udebug_end(app_phone); |
338 | udebug_end(app_phone); |
330 | ipc_hangup(app_phone); |
339 | ipc_hangup(app_phone); |
331 | 340 | ||
332 | printf("done\n"); |
341 | printf("done\n"); |
333 | return; |
342 | return; |
334 | } |
343 | } |
335 | 344 | ||
336 | static void main_init(void) |
345 | static void main_init(void) |
337 | { |
346 | { |
338 | next_thread_id = 1; |
347 | next_thread_id = 1; |
339 | paused = 0; |
348 | paused = 0; |
340 | } |
349 | } |
341 | 350 | ||
342 | int main(void) |
351 | int main(void) |
343 | { |
352 | { |
344 | main_init(); |
353 | main_init(); |
345 | 354 | ||
346 | while (1) { |
355 | while (1) { |
347 | debug_active_task(); |
356 | debug_active_task(); |
348 | } |
357 | } |
349 | } |
358 | } |
350 | 359 | ||
351 | /** @} |
360 | /** @} |
352 | */ |
361 | */ |
353 | 362 |