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