Rev 3606 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2894 | 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 | |||
3435 | svoboda | 29 | /** @addtogroup trace |
2832 | svoboda | 30 | * @{ |
31 | */ |
||
32 | /** @file |
||
33 | */ |
||
34 | |||
35 | #include <stdio.h> |
||
2878 | svoboda | 36 | #include <stdlib.h> |
2832 | svoboda | 37 | #include <unistd.h> |
38 | #include <ipc/ipc.h> |
||
2835 | svoboda | 39 | #include <fibril.h> |
40 | #include <errno.h> |
||
2838 | svoboda | 41 | #include <udebug.h> |
2862 | svoboda | 42 | #include <async.h> |
3471 | svoboda | 43 | #include <task.h> |
44 | #include <loader/loader.h> |
||
2832 | svoboda | 45 | |
3536 | svoboda | 46 | #include <libc.h> |
47 | |||
2880 | svoboda | 48 | // Temporary: service and method names |
2878 | svoboda | 49 | #include "proto.h" |
50 | #include <ipc/services.h> |
||
2880 | svoboda | 51 | #include "../../srv/vfs/vfs.h" |
4377 | svoboda | 52 | #include <ipc/console.h> |
2878 | svoboda | 53 | |
2832 | svoboda | 54 | #include "syscalls.h" |
2874 | svoboda | 55 | #include "ipcp.h" |
2832 | svoboda | 56 | #include "errors.h" |
3471 | svoboda | 57 | #include "trace.h" |
2832 | svoboda | 58 | |
2868 | svoboda | 59 | #define THBUF_SIZE 64 |
3471 | svoboda | 60 | uintptr_t thread_hash_buf[THBUF_SIZE]; |
61 | int n_threads; |
||
2832 | svoboda | 62 | |
2868 | svoboda | 63 | int next_thread_id; |
64 | |||
2832 | svoboda | 65 | int phoneid; |
2835 | svoboda | 66 | int abort_trace; |
2832 | svoboda | 67 | |
3471 | svoboda | 68 | uintptr_t thash; |
2898 | svoboda | 69 | volatile int paused; |
70 | |||
3471 | svoboda | 71 | void thread_trace_start(uintptr_t thread_hash); |
2868 | svoboda | 72 | |
2882 | svoboda | 73 | static proto_t *proto_console; |
3471 | svoboda | 74 | static task_id_t task_id; |
75 | static loader_t *task_ldr; |
||
2868 | svoboda | 76 | |
3471 | svoboda | 77 | /** Combination of events/data to print. */ |
78 | display_mask_t display_mask; |
||
79 | |||
80 | static int program_run_fibril(void *arg); |
||
81 | |||
82 | static void program_run(void) |
||
2832 | svoboda | 83 | { |
3471 | svoboda | 84 | fid_t fid; |
85 | |||
86 | fid = fibril_create(program_run_fibril, NULL); |
||
87 | if (fid == 0) { |
||
88 | printf("Error creating fibril\n"); |
||
89 | exit(1); |
||
90 | } |
||
91 | |||
92 | fibril_add_ready(fid); |
||
93 | } |
||
94 | |||
95 | static int program_run_fibril(void *arg) |
||
96 | { |
||
2832 | svoboda | 97 | int rc; |
98 | |||
3471 | svoboda | 99 | /* |
100 | * This must be done in background as it will block until |
||
101 | * we let the task reply to this call. |
||
102 | */ |
||
103 | rc = loader_run(task_ldr); |
||
104 | if (rc != 0) { |
||
105 | printf("Error running program\n"); |
||
106 | exit(1); |
||
107 | } |
||
108 | |||
109 | free(task_ldr); |
||
110 | task_ldr = NULL; |
||
111 | |||
112 | printf("program_run_fibril exiting\n"); |
||
113 | return 0; |
||
114 | } |
||
115 | |||
116 | |||
117 | static int connect_task(task_id_t task_id) |
||
118 | { |
||
119 | int rc; |
||
120 | |||
3428 | svoboda | 121 | rc = ipc_connect_kbox(task_id); |
3471 | svoboda | 122 | |
123 | if (rc == ENOTSUP) { |
||
124 | printf("You do not have userspace debugging support " |
||
125 | "compiled in the kernel.\n"); |
||
126 | printf("Compile kernel with 'Support for userspace debuggers' " |
||
127 | "(CONFIG_UDEBUG) enabled.\n"); |
||
128 | return rc; |
||
129 | } |
||
130 | |||
131 | if (rc < 0) { |
||
132 | printf("Error connecting\n"); |
||
133 | printf("ipc_connect_task(%lld) -> %d ", task_id, rc); |
||
134 | return rc; |
||
135 | } |
||
136 | |||
2832 | svoboda | 137 | phoneid = rc; |
138 | |||
2904 | svoboda | 139 | rc = udebug_begin(phoneid); |
3471 | svoboda | 140 | if (rc < 0) { |
141 | printf("udebug_begin() -> %d\n", rc); |
||
142 | return rc; |
||
143 | } |
||
2832 | svoboda | 144 | |
2904 | svoboda | 145 | rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL); |
3471 | svoboda | 146 | if (rc < 0) { |
147 | printf("udebug_set_evmask(0x%x) -> %d\n ", UDEBUG_EM_ALL, rc); |
||
148 | return rc; |
||
149 | } |
||
2899 | svoboda | 150 | |
2832 | svoboda | 151 | return 0; |
152 | } |
||
153 | |||
2946 | svoboda | 154 | static int get_thread_list(void) |
2832 | svoboda | 155 | { |
156 | int rc; |
||
2946 | svoboda | 157 | size_t tb_copied; |
158 | size_t tb_needed; |
||
2832 | svoboda | 159 | int i; |
160 | |||
2946 | svoboda | 161 | rc = udebug_thread_read(phoneid, thread_hash_buf, |
2868 | svoboda | 162 | THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed); |
3471 | svoboda | 163 | if (rc < 0) { |
164 | printf("udebug_thread_read() -> %d\n", rc); |
||
165 | return rc; |
||
166 | } |
||
2832 | svoboda | 167 | |
3471 | svoboda | 168 | n_threads = tb_copied / sizeof(uintptr_t); |
2850 | svoboda | 169 | |
3471 | svoboda | 170 | printf("Threads:"); |
171 | for (i = 0; i < n_threads; i++) { |
||
172 | printf(" [%d] (hash 0x%lx)", 1+i, thread_hash_buf[i]); |
||
2832 | svoboda | 173 | } |
3471 | svoboda | 174 | printf("\ntotal of %u threads\n", tb_needed / sizeof(uintptr_t)); |
2832 | svoboda | 175 | |
176 | return 0; |
||
177 | } |
||
178 | |||
3471 | svoboda | 179 | void val_print(sysarg_t val, val_type_t v_type) |
2832 | svoboda | 180 | { |
3471 | svoboda | 181 | switch (v_type) { |
182 | case V_VOID: |
||
183 | printf("<void>"); |
||
184 | break; |
||
185 | |||
186 | case V_INTEGER: |
||
187 | printf("%ld", val); |
||
188 | break; |
||
189 | |||
190 | case V_HASH: |
||
191 | case V_PTR: |
||
192 | printf("0x%08lx", val); |
||
193 | break; |
||
194 | |||
195 | case V_ERRNO: |
||
196 | if (val >= -15 && val <= 0) { |
||
197 | printf("%ld %s (%s)", val, |
||
198 | err_desc[-val].name, |
||
199 | err_desc[-val].desc); |
||
2832 | svoboda | 200 | } else { |
3471 | svoboda | 201 | printf("%ld", val); |
2832 | svoboda | 202 | } |
3471 | svoboda | 203 | break; |
204 | case V_INT_ERRNO: |
||
205 | if (val >= -15 && val < 0) { |
||
206 | printf("%ld %s (%s)", val, |
||
207 | err_desc[-val].name, |
||
208 | err_desc[-val].desc); |
||
2832 | svoboda | 209 | } else { |
3471 | svoboda | 210 | printf("%ld", val); |
2832 | svoboda | 211 | } |
3471 | svoboda | 212 | break; |
213 | |||
214 | case V_CHAR: |
||
215 | if (val >= 0x20 && val < 0x7f) { |
||
216 | printf("'%c'", val); |
||
217 | } else { |
||
218 | switch (val) { |
||
219 | case '\a': printf("'\\a'"); break; |
||
220 | case '\b': printf("'\\b'"); break; |
||
221 | case '\n': printf("'\\n'"); break; |
||
222 | case '\r': printf("'\\r'"); break; |
||
223 | case '\t': printf("'\\t'"); break; |
||
224 | case '\\': printf("'\\\\'"); break; |
||
225 | default: printf("'\\x%02lX'", val); break; |
||
226 | } |
||
227 | } |
||
228 | break; |
||
2832 | svoboda | 229 | } |
3471 | svoboda | 230 | } |
231 | |||
232 | |||
233 | static void print_sc_retval(sysarg_t retval, val_type_t val_type) |
||
234 | { |
||
235 | printf(" -> "); |
||
236 | val_print(retval, val_type); |
||
2832 | svoboda | 237 | putchar('\n'); |
238 | } |
||
239 | |||
3471 | svoboda | 240 | static void print_sc_args(sysarg_t *sc_args, int n) |
2832 | svoboda | 241 | { |
242 | int i; |
||
243 | |||
244 | putchar('('); |
||
3471 | svoboda | 245 | if (n > 0) printf("%ld", sc_args[0]); |
246 | for (i = 1; i < n; i++) { |
||
247 | printf(", %ld", sc_args[i]); |
||
2832 | svoboda | 248 | } |
249 | putchar(')'); |
||
250 | } |
||
251 | |||
3471 | svoboda | 252 | static void sc_ipc_call_async_fast(sysarg_t *sc_args, sysarg_t sc_rc) |
2871 | svoboda | 253 | { |
254 | ipc_call_t call; |
||
3471 | svoboda | 255 | ipcarg_t phoneid; |
2872 | svoboda | 256 | |
257 | if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY) |
||
258 | return; |
||
2871 | svoboda | 259 | |
260 | phoneid = sc_args[0]; |
||
261 | |||
262 | IPC_SET_METHOD(call, sc_args[1]); |
||
263 | IPC_SET_ARG1(call, sc_args[2]); |
||
264 | IPC_SET_ARG2(call, sc_args[3]); |
||
265 | IPC_SET_ARG3(call, sc_args[4]); |
||
266 | IPC_SET_ARG4(call, sc_args[5]); |
||
267 | IPC_SET_ARG5(call, 0); |
||
268 | |||
2874 | svoboda | 269 | ipcp_call_out(phoneid, &call, sc_rc); |
2871 | svoboda | 270 | } |
271 | |||
3471 | svoboda | 272 | static void sc_ipc_call_async_slow(sysarg_t *sc_args, sysarg_t sc_rc) |
2832 | svoboda | 273 | { |
2871 | svoboda | 274 | ipc_call_t call; |
2832 | svoboda | 275 | int rc; |
276 | |||
2872 | svoboda | 277 | if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY) |
278 | return; |
||
279 | |||
2871 | svoboda | 280 | memset(&call, 0, sizeof(call)); |
2904 | svoboda | 281 | rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args)); |
2832 | svoboda | 282 | |
283 | if (rc >= 0) { |
||
2874 | svoboda | 284 | ipcp_call_out(sc_args[0], &call, sc_rc); |
2832 | svoboda | 285 | } |
286 | } |
||
287 | |||
3471 | svoboda | 288 | static void sc_ipc_call_sync_fast(sysarg_t *sc_args) |
2872 | svoboda | 289 | { |
290 | ipc_call_t question, reply; |
||
291 | int rc; |
||
292 | int phoneidx; |
||
293 | |||
2877 | svoboda | 294 | // printf("sc_ipc_call_sync_fast()\n"); |
2872 | svoboda | 295 | phoneidx = sc_args[0]; |
296 | |||
297 | IPC_SET_METHOD(question, sc_args[1]); |
||
298 | IPC_SET_ARG1(question, sc_args[2]); |
||
299 | IPC_SET_ARG2(question, sc_args[3]); |
||
300 | IPC_SET_ARG3(question, sc_args[4]); |
||
301 | IPC_SET_ARG4(question, 0); |
||
302 | IPC_SET_ARG5(question, 0); |
||
303 | |||
2877 | svoboda | 304 | // printf("memset\n"); |
2872 | svoboda | 305 | memset(&reply, 0, sizeof(reply)); |
2904 | svoboda | 306 | // printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n", |
2877 | svoboda | 307 | // phoneid, &reply.args, sc_args[5], sizeof(reply.args)); |
2904 | svoboda | 308 | rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args)); |
2877 | svoboda | 309 | // printf("dmr->%d\n", rc); |
2872 | svoboda | 310 | if (rc < 0) return; |
311 | |||
2877 | svoboda | 312 | // printf("call ipc_call_sync\n"); |
2874 | svoboda | 313 | ipcp_call_sync(phoneidx, &question, &reply); |
2872 | svoboda | 314 | } |
315 | |||
3471 | svoboda | 316 | static void sc_ipc_call_sync_slow(sysarg_t *sc_args) |
2872 | svoboda | 317 | { |
318 | ipc_call_t question, reply; |
||
319 | int rc; |
||
320 | |||
321 | memset(&question, 0, sizeof(question)); |
||
2904 | svoboda | 322 | rc = udebug_mem_read(phoneid, &question.args, sc_args[1], sizeof(question.args)); |
2872 | svoboda | 323 | printf("dmr->%d\n", rc); |
324 | if (rc < 0) return; |
||
325 | |||
326 | memset(&reply, 0, sizeof(reply)); |
||
2904 | svoboda | 327 | rc = udebug_mem_read(phoneid, &reply.args, sc_args[2], sizeof(reply.args)); |
2872 | svoboda | 328 | printf("dmr->%d\n", rc); |
329 | if (rc < 0) return; |
||
330 | |||
2874 | svoboda | 331 | ipcp_call_sync(sc_args[0], &question, &reply); |
2872 | svoboda | 332 | } |
333 | |||
3471 | svoboda | 334 | static void sc_ipc_wait(sysarg_t *sc_args, int sc_rc) |
2871 | svoboda | 335 | { |
336 | ipc_call_t call; |
||
337 | int rc; |
||
338 | |||
2946 | svoboda | 339 | if (sc_rc == 0) return; |
2871 | svoboda | 340 | |
341 | memset(&call, 0, sizeof(call)); |
||
2904 | svoboda | 342 | rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call)); |
343 | // printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n", |
||
2872 | svoboda | 344 | // phoneid, (int)&call, sc_args[0], sizeof(call), rc); |
2871 | svoboda | 345 | |
346 | if (rc >= 0) { |
||
2874 | svoboda | 347 | ipcp_call_in(&call, sc_rc); |
2871 | svoboda | 348 | } |
349 | } |
||
350 | |||
3471 | svoboda | 351 | static void event_syscall_b(unsigned thread_id, uintptr_t thread_hash, |
352 | unsigned sc_id, sysarg_t sc_rc) |
||
2867 | svoboda | 353 | { |
3471 | svoboda | 354 | sysarg_t sc_args[6]; |
2867 | svoboda | 355 | int rc; |
356 | |||
357 | /* Read syscall arguments */ |
||
2904 | svoboda | 358 | rc = udebug_args_read(phoneid, thread_hash, sc_args); |
2867 | svoboda | 359 | |
360 | async_serialize_start(); |
||
361 | |||
2872 | svoboda | 362 | // printf("[%d] ", thread_id); |
2867 | svoboda | 363 | |
364 | if (rc < 0) { |
||
365 | printf("error\n"); |
||
366 | async_serialize_end(); |
||
367 | return; |
||
368 | } |
||
369 | |||
3471 | svoboda | 370 | if ((display_mask & DM_SYSCALL) != 0) { |
371 | /* Print syscall name and arguments */ |
||
372 | printf("%s", syscall_desc[sc_id].name); |
||
373 | print_sc_args(sc_args, syscall_desc[sc_id].n_args); |
||
374 | } |
||
2901 | svoboda | 375 | |
376 | async_serialize_end(); |
||
377 | } |
||
378 | |||
3471 | svoboda | 379 | static void event_syscall_e(unsigned thread_id, uintptr_t thread_hash, |
380 | unsigned sc_id, sysarg_t sc_rc) |
||
2901 | svoboda | 381 | { |
3471 | svoboda | 382 | sysarg_t sc_args[6]; |
2901 | svoboda | 383 | int rv_type; |
384 | int rc; |
||
385 | |||
386 | /* Read syscall arguments */ |
||
2904 | svoboda | 387 | rc = udebug_args_read(phoneid, thread_hash, sc_args); |
2901 | svoboda | 388 | |
389 | async_serialize_start(); |
||
390 | |||
391 | // printf("[%d] ", thread_id); |
||
392 | |||
393 | if (rc < 0) { |
||
394 | printf("error\n"); |
||
395 | async_serialize_end(); |
||
396 | return; |
||
397 | } |
||
398 | |||
3471 | svoboda | 399 | if ((display_mask & DM_SYSCALL) != 0) { |
400 | /* Print syscall return value */ |
||
401 | rv_type = syscall_desc[sc_id].rv_type; |
||
402 | print_sc_retval(sc_rc, rv_type); |
||
403 | } |
||
2867 | svoboda | 404 | |
405 | switch (sc_id) { |
||
2871 | svoboda | 406 | case SYS_IPC_CALL_ASYNC_FAST: |
2872 | svoboda | 407 | sc_ipc_call_async_fast(sc_args, sc_rc); |
2871 | svoboda | 408 | break; |
2867 | svoboda | 409 | case SYS_IPC_CALL_ASYNC_SLOW: |
2872 | svoboda | 410 | sc_ipc_call_async_slow(sc_args, sc_rc); |
2867 | svoboda | 411 | break; |
2872 | svoboda | 412 | case SYS_IPC_CALL_SYNC_FAST: |
413 | sc_ipc_call_sync_fast(sc_args); |
||
414 | break; |
||
415 | case SYS_IPC_CALL_SYNC_SLOW: |
||
416 | sc_ipc_call_sync_slow(sc_args); |
||
417 | break; |
||
2871 | svoboda | 418 | case SYS_IPC_WAIT: |
419 | sc_ipc_wait(sc_args, sc_rc); |
||
420 | break; |
||
2867 | svoboda | 421 | default: |
422 | break; |
||
423 | } |
||
424 | |||
425 | async_serialize_end(); |
||
426 | } |
||
427 | |||
3471 | svoboda | 428 | static void event_thread_b(uintptr_t hash) |
2867 | svoboda | 429 | { |
430 | async_serialize_start(); |
||
3471 | svoboda | 431 | printf("New thread, hash 0x%lx\n", hash); |
2867 | svoboda | 432 | async_serialize_end(); |
2868 | svoboda | 433 | |
434 | thread_trace_start(hash); |
||
2867 | svoboda | 435 | } |
436 | |||
2946 | svoboda | 437 | static int trace_loop(void *thread_hash_arg) |
2835 | svoboda | 438 | { |
2832 | svoboda | 439 | int rc; |
440 | unsigned ev_type; |
||
3471 | svoboda | 441 | uintptr_t thread_hash; |
2868 | svoboda | 442 | unsigned thread_id; |
3471 | svoboda | 443 | sysarg_t val0, val1; |
2832 | svoboda | 444 | |
3471 | svoboda | 445 | thread_hash = (uintptr_t)thread_hash_arg; |
2868 | svoboda | 446 | thread_id = next_thread_id++; |
2835 | svoboda | 447 | |
3606 | svoboda | 448 | printf("Start tracing thread [%d] (hash 0x%lx).\n", thread_id, thread_hash); |
2868 | svoboda | 449 | |
2835 | svoboda | 450 | while (!abort_trace) { |
451 | |||
3606 | svoboda | 452 | if (paused) { |
453 | printf("Press R to resume (and be patient).\n"); |
||
454 | while (paused) { |
||
455 | usleep(1000000); |
||
456 | fibril_yield(); |
||
457 | printf("."); |
||
458 | } |
||
459 | printf("Resumed\n"); |
||
460 | } |
||
461 | |||
2867 | svoboda | 462 | /* Run thread until an event occurs */ |
2904 | svoboda | 463 | rc = udebug_go(phoneid, thread_hash, |
2867 | svoboda | 464 | &ev_type, &val0, &val1); |
2832 | svoboda | 465 | |
2872 | svoboda | 466 | // printf("rc = %d, ev_type=%d\n", rc, ev_type); |
2838 | svoboda | 467 | if (ev_type == UDEBUG_EVENT_FINISHED) { |
3471 | svoboda | 468 | /* Done tracing this thread */ |
2838 | svoboda | 469 | break; |
470 | } |
||
471 | |||
2832 | svoboda | 472 | if (rc >= 0) { |
2867 | svoboda | 473 | switch (ev_type) { |
2901 | svoboda | 474 | case UDEBUG_EVENT_SYSCALL_B: |
475 | event_syscall_b(thread_id, thread_hash, val0, (int)val1); |
||
2867 | svoboda | 476 | break; |
2901 | svoboda | 477 | case UDEBUG_EVENT_SYSCALL_E: |
478 | event_syscall_e(thread_id, thread_hash, val0, (int)val1); |
||
479 | break; |
||
2898 | svoboda | 480 | case UDEBUG_EVENT_STOP: |
3471 | svoboda | 481 | printf("Stop event\n"); |
2898 | svoboda | 482 | break; |
2903 | svoboda | 483 | case UDEBUG_EVENT_THREAD_B: |
484 | event_thread_b(val0); |
||
2867 | svoboda | 485 | break; |
2903 | svoboda | 486 | case UDEBUG_EVENT_THREAD_E: |
3606 | svoboda | 487 | printf("Thread 0x%lx exited.\n", val0); |
2903 | svoboda | 488 | abort_trace = 1; |
489 | break; |
||
2867 | svoboda | 490 | default: |
3606 | svoboda | 491 | printf("Unknown event type %d.\n", ev_type); |
2867 | svoboda | 492 | break; |
493 | } |
||
2832 | svoboda | 494 | } |
495 | |||
496 | } |
||
2835 | svoboda | 497 | |
3606 | svoboda | 498 | printf("Finished tracing thread [%d].\n", thread_id); |
2946 | svoboda | 499 | return 0; |
2832 | svoboda | 500 | } |
501 | |||
3471 | svoboda | 502 | void thread_trace_start(uintptr_t thread_hash) |
2868 | svoboda | 503 | { |
504 | fid_t fid; |
||
2832 | svoboda | 505 | |
2898 | svoboda | 506 | thash = thread_hash; |
507 | |||
2868 | svoboda | 508 | fid = fibril_create(trace_loop, (void *)thread_hash); |
509 | if (fid == 0) { |
||
510 | printf("Warning: Failed creating fibril\n"); |
||
511 | } |
||
512 | fibril_add_ready(fid); |
||
513 | } |
||
514 | |||
3471 | svoboda | 515 | static loader_t *preload_task(const char *path, char *const argv[], |
516 | task_id_t *task_id) |
||
2832 | svoboda | 517 | { |
3471 | svoboda | 518 | loader_t *ldr; |
2832 | svoboda | 519 | int rc; |
520 | |||
3471 | svoboda | 521 | /* Spawn a program loader */ |
4377 | svoboda | 522 | ldr = loader_connect(); |
3471 | svoboda | 523 | if (ldr == NULL) |
524 | return 0; |
||
2832 | svoboda | 525 | |
3471 | svoboda | 526 | /* Get task ID. */ |
527 | rc = loader_get_task_id(ldr, task_id); |
||
528 | if (rc != EOK) |
||
529 | goto error; |
||
2832 | svoboda | 530 | |
3471 | svoboda | 531 | /* Send program pathname */ |
532 | rc = loader_set_pathname(ldr, path); |
||
533 | if (rc != EOK) |
||
534 | goto error; |
||
2832 | svoboda | 535 | |
3471 | svoboda | 536 | /* Send arguments */ |
537 | rc = loader_set_args(ldr, argv); |
||
538 | if (rc != EOK) |
||
539 | goto error; |
||
540 | |||
541 | /* Load the program. */ |
||
542 | rc = loader_load_program(ldr); |
||
543 | if (rc != EOK) |
||
544 | goto error; |
||
545 | |||
546 | /* Success */ |
||
547 | return ldr; |
||
548 | |||
549 | /* Error exit */ |
||
550 | error: |
||
551 | loader_abort(ldr); |
||
552 | free(ldr); |
||
553 | return NULL; |
||
554 | } |
||
555 | |||
556 | static void trace_task(task_id_t task_id) |
||
557 | { |
||
558 | int i; |
||
559 | int rc; |
||
560 | int c; |
||
561 | |||
2873 | svoboda | 562 | ipcp_init(); |
563 | |||
3471 | svoboda | 564 | /* |
565 | * User apps now typically have console on phone 3. |
||
566 | * (Phones 1 and 2 are used by the loader). |
||
567 | */ |
||
568 | ipcp_connection_set(3, 0, proto_console); |
||
569 | |||
2832 | svoboda | 570 | rc = get_thread_list(); |
571 | if (rc < 0) { |
||
572 | printf("Failed to get thread list (error %d)\n", rc); |
||
573 | return; |
||
574 | } |
||
575 | |||
2850 | svoboda | 576 | abort_trace = 0; |
2832 | svoboda | 577 | |
2850 | svoboda | 578 | for (i = 0; i < n_threads; i++) { |
2868 | svoboda | 579 | thread_trace_start(thread_hash_buf[i]); |
2850 | svoboda | 580 | } |
581 | |||
2898 | svoboda | 582 | while(1) { |
583 | c = getchar(); |
||
584 | if (c == 'q') break; |
||
585 | if (c == 'p') { |
||
3606 | svoboda | 586 | printf("Pause...\n"); |
2898 | svoboda | 587 | paused = 1; |
2904 | svoboda | 588 | rc = udebug_stop(phoneid, thash); |
2898 | svoboda | 589 | printf("stop -> %d\n", rc); |
590 | } |
||
591 | if (c == 'r') { |
||
592 | paused = 0; |
||
3606 | svoboda | 593 | printf("Resume...\n"); |
2898 | svoboda | 594 | } |
595 | } |
||
2850 | svoboda | 596 | |
3471 | svoboda | 597 | printf("\nTerminate debugging session...\n"); |
2850 | svoboda | 598 | abort_trace = 1; |
2904 | svoboda | 599 | udebug_end(phoneid); |
2850 | svoboda | 600 | ipc_hangup(phoneid); |
601 | |||
2873 | svoboda | 602 | ipcp_cleanup(); |
603 | |||
3471 | svoboda | 604 | printf("Done\n"); |
2832 | svoboda | 605 | return; |
606 | } |
||
607 | |||
2878 | svoboda | 608 | static void main_init(void) |
2832 | svoboda | 609 | { |
2878 | svoboda | 610 | proto_t *p; |
2880 | svoboda | 611 | oper_t *o; |
2878 | svoboda | 612 | |
3471 | svoboda | 613 | val_type_t arg_def[OPER_MAX_ARGS] = { |
614 | V_INTEGER, |
||
615 | V_INTEGER, |
||
616 | V_INTEGER, |
||
617 | V_INTEGER, |
||
618 | V_INTEGER |
||
619 | }; |
||
620 | |||
621 | val_type_t resp_def[OPER_MAX_ARGS] = { |
||
622 | V_INTEGER, |
||
623 | V_INTEGER, |
||
624 | V_INTEGER, |
||
625 | V_INTEGER, |
||
4377 | svoboda | 626 | V_INTEGER |
3471 | svoboda | 627 | }; |
628 | |||
2868 | svoboda | 629 | next_thread_id = 1; |
2898 | svoboda | 630 | paused = 0; |
2868 | svoboda | 631 | |
2878 | svoboda | 632 | proto_init(); |
633 | |||
2880 | svoboda | 634 | p = proto_new("vfs"); |
3471 | svoboda | 635 | o = oper_new("read", 1, arg_def, V_ERRNO, 1, resp_def); |
2883 | svoboda | 636 | proto_add_oper(p, VFS_READ, o); |
3471 | svoboda | 637 | o = oper_new("write", 1, arg_def, V_ERRNO, 1, resp_def); |
2883 | svoboda | 638 | proto_add_oper(p, VFS_WRITE, o); |
3471 | svoboda | 639 | o = oper_new("truncate", 5, arg_def, V_ERRNO, 0, resp_def); |
2883 | svoboda | 640 | proto_add_oper(p, VFS_TRUNCATE, o); |
3471 | svoboda | 641 | o = oper_new("mount", 2, arg_def, V_ERRNO, 0, resp_def); |
2880 | svoboda | 642 | proto_add_oper(p, VFS_MOUNT, o); |
3471 | svoboda | 643 | /* o = oper_new("unmount", 0, arg_def); |
644 | proto_add_oper(p, VFS_UNMOUNT, o);*/ |
||
4377 | svoboda | 645 | o = oper_new("open", 2, arg_def, V_INT_ERRNO, 0, resp_def); |
646 | proto_add_oper(p, VFS_OPEN, o); |
||
647 | o = oper_new("close", 1, arg_def, V_ERRNO, 0, resp_def); |
||
648 | proto_add_oper(p, VFS_CLOSE, o); |
||
649 | o = oper_new("seek", 3, arg_def, V_ERRNO, 0, resp_def); |
||
650 | proto_add_oper(p, VFS_SEEK, o); |
||
651 | o = oper_new("mkdir", 1, arg_def, V_ERRNO, 0, resp_def); |
||
652 | proto_add_oper(p, VFS_MKDIR, o); |
||
653 | o = oper_new("unlink", 0, arg_def, V_ERRNO, 0, resp_def); |
||
654 | proto_add_oper(p, VFS_UNLINK, o); |
||
655 | o = oper_new("rename", 0, arg_def, V_ERRNO, 0, resp_def); |
||
656 | proto_add_oper(p, VFS_RENAME, o); |
||
2880 | svoboda | 657 | |
2878 | svoboda | 658 | proto_register(SERVICE_VFS, p); |
2882 | svoboda | 659 | |
660 | p = proto_new("console"); |
||
4377 | svoboda | 661 | resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER; |
662 | resp_def[2] = V_INTEGER; resp_def[3] = V_CHAR; |
||
663 | o = oper_new("getkey", 0, arg_def, V_ERRNO, 4, resp_def); |
||
664 | proto_add_oper(p, CONSOLE_GETKEY, o); |
||
3471 | svoboda | 665 | |
666 | arg_def[0] = V_CHAR; |
||
667 | o = oper_new("putchar", 1, arg_def, V_VOID, 0, resp_def); |
||
2882 | svoboda | 668 | proto_add_oper(p, CONSOLE_PUTCHAR, o); |
4377 | svoboda | 669 | o = oper_new("write", 0, arg_def, V_VOID, 0, resp_def); |
670 | proto_add_oper(p, CONSOLE_WRITE, o); |
||
3471 | svoboda | 671 | o = oper_new("clear", 0, arg_def, V_VOID, 0, resp_def); |
2883 | svoboda | 672 | proto_add_oper(p, CONSOLE_CLEAR, o); |
3471 | svoboda | 673 | |
674 | arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER; |
||
675 | o = oper_new("goto", 2, arg_def, V_VOID, 0, resp_def); |
||
2883 | svoboda | 676 | proto_add_oper(p, CONSOLE_GOTO, o); |
3471 | svoboda | 677 | |
678 | resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER; |
||
679 | o = oper_new("getsize", 0, arg_def, V_INTEGER, 2, resp_def); |
||
2883 | svoboda | 680 | proto_add_oper(p, CONSOLE_GETSIZE, o); |
3471 | svoboda | 681 | o = oper_new("flush", 0, arg_def, V_VOID, 0, resp_def); |
2883 | svoboda | 682 | proto_add_oper(p, CONSOLE_FLUSH, o); |
3471 | svoboda | 683 | |
4377 | svoboda | 684 | arg_def[0] = V_INTEGER; |
685 | o = oper_new("set_style", 1, arg_def, V_VOID, 0, resp_def); |
||
686 | proto_add_oper(p, CONSOLE_SET_STYLE, o); |
||
687 | arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER; arg_def[2] = V_INTEGER; |
||
688 | o = oper_new("set_color", 3, arg_def, V_VOID, 0, resp_def); |
||
689 | proto_add_oper(p, CONSOLE_SET_COLOR, o); |
||
3471 | svoboda | 690 | arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER; |
4377 | svoboda | 691 | o = oper_new("set_rgb_color", 2, arg_def, V_VOID, 0, resp_def); |
692 | proto_add_oper(p, CONSOLE_SET_RGB_COLOR, o); |
||
3471 | svoboda | 693 | o = oper_new("cursor_visibility", 1, arg_def, V_VOID, 0, resp_def); |
3428 | svoboda | 694 | proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o); |
2882 | svoboda | 695 | |
696 | proto_console = p; |
||
697 | proto_register(SERVICE_CONSOLE, p); |
||
2878 | svoboda | 698 | } |
699 | |||
3428 | svoboda | 700 | static void print_syntax() |
2878 | svoboda | 701 | { |
3471 | svoboda | 702 | printf("Syntax:\n"); |
703 | printf("\ttrace [+<events>] <executable> [<arg1> [...]]\n"); |
||
704 | printf("or\ttrace [+<events>] -t <task_id>\n"); |
||
705 | printf("Events: (default is +tp)\n"); |
||
706 | printf("\n"); |
||
707 | printf("\tt ... Thread creation and termination\n"); |
||
708 | printf("\ts ... System calls\n"); |
||
709 | printf("\ti ... Low-level IPC\n"); |
||
710 | printf("\tp ... Protocol level\n"); |
||
3473 | svoboda | 711 | printf("\n"); |
712 | printf("Examples:\n"); |
||
713 | printf("\ttrace +s /app/tetris\n"); |
||
714 | printf("\ttrace +tsip -t 12\n"); |
||
3428 | svoboda | 715 | } |
2878 | svoboda | 716 | |
3471 | svoboda | 717 | static display_mask_t parse_display_mask(char *text) |
3428 | svoboda | 718 | { |
3471 | svoboda | 719 | display_mask_t dm; |
720 | char *c; |
||
721 | |||
722 | c = text; |
||
723 | |||
724 | while (*c) { |
||
725 | switch (*c) { |
||
726 | case 't': dm = dm | DM_THREAD; break; |
||
727 | case 's': dm = dm | DM_SYSCALL; break; |
||
728 | case 'i': dm = dm | DM_IPC; break; |
||
729 | case 'p': dm = dm | DM_SYSTEM | DM_USER; break; |
||
730 | default: |
||
3606 | svoboda | 731 | printf("Unexpected event type '%c'.\n", *c); |
3471 | svoboda | 732 | exit(1); |
733 | } |
||
734 | |||
735 | ++c; |
||
736 | } |
||
737 | |||
738 | return dm; |
||
739 | } |
||
740 | |||
741 | static int parse_args(int argc, char *argv[]) |
||
742 | { |
||
743 | char *arg; |
||
3428 | svoboda | 744 | char *err_p; |
745 | |||
3471 | svoboda | 746 | task_id = 0; |
747 | |||
748 | --argc; ++argv; |
||
749 | |||
750 | while (argc > 0) { |
||
751 | arg = *argv; |
||
752 | if (arg[0] == '+') { |
||
753 | display_mask = parse_display_mask(&arg[1]); |
||
754 | } else if (arg[0] == '-') { |
||
755 | if (arg[1] == 't') { |
||
756 | /* Trace an already running task */ |
||
757 | --argc; ++argv; |
||
758 | task_id = strtol(*argv, &err_p, 10); |
||
759 | task_ldr = NULL; |
||
760 | if (*err_p) { |
||
761 | printf("Task ID syntax error\n"); |
||
762 | print_syntax(); |
||
763 | return -1; |
||
764 | } |
||
765 | } else { |
||
766 | printf("Uknown option '%s'\n", arg[0]); |
||
767 | print_syntax(); |
||
768 | return -1; |
||
769 | } |
||
770 | } else { |
||
771 | break; |
||
772 | } |
||
773 | |||
774 | --argc; ++argv; |
||
775 | } |
||
776 | |||
777 | if (task_id != 0) { |
||
778 | if (argc == 0) return 0; |
||
779 | printf("Extra arguments\n"); |
||
3428 | svoboda | 780 | print_syntax(); |
3471 | svoboda | 781 | return -1; |
2835 | svoboda | 782 | } |
3428 | svoboda | 783 | |
3471 | svoboda | 784 | if (argc < 1) { |
785 | printf("Missing argument\n"); |
||
786 | print_syntax(); |
||
787 | return -1; |
||
788 | } |
||
3428 | svoboda | 789 | |
3471 | svoboda | 790 | /* Preload the specified program file. */ |
791 | printf("Spawning '%s' with arguments:\n", *argv); |
||
792 | { |
||
793 | char **cp = argv; |
||
794 | while (*cp) printf("'%s'\n", *cp++); |
||
795 | } |
||
796 | task_ldr = preload_task(*argv, argv, &task_id); |
||
797 | |||
798 | return 0; |
||
799 | } |
||
800 | |||
801 | int main(int argc, char *argv[]) |
||
802 | { |
||
803 | int rc; |
||
804 | |||
805 | printf("System Call / IPC Tracer\n"); |
||
3606 | svoboda | 806 | printf("Controls: Q - Quit, P - Pause, R - Resume\n"); |
3471 | svoboda | 807 | |
808 | display_mask = DM_THREAD | DM_SYSTEM | DM_USER; |
||
809 | |||
810 | if (parse_args(argc, argv) < 0) |
||
3428 | svoboda | 811 | return 1; |
3471 | svoboda | 812 | |
813 | main_init(); |
||
814 | |||
815 | rc = connect_task(task_id); |
||
816 | if (rc < 0) { |
||
3606 | svoboda | 817 | printf("Failed connecting to task %lld.\n", task_id); |
3471 | svoboda | 818 | return 1; |
3428 | svoboda | 819 | } |
820 | |||
3606 | svoboda | 821 | printf("Connected to task %lld.\n", task_id); |
3471 | svoboda | 822 | |
823 | if (task_ldr != NULL) { |
||
824 | program_run(); |
||
825 | } |
||
826 | |||
827 | trace_task(task_id); |
||
828 | |||
829 | return 0; |
||
2832 | svoboda | 830 | } |
831 | |||
832 | /** @} |
||
833 | */ |