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