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