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