source: mainline/uspace/app/trace/trace.c@ 223efc0

Last change on this file since 223efc0 was 9af1c61, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Print errno values as string, rather than just numbers.

  • Property mode set to 100644
File size: 18.2 KB
RevLine 
[9a1b20c]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 trace
30 * @{
31 */
32/** @file
33 */
34
35#include <stdio.h>
36#include <stdlib.h>
[8d2dd7f2]37#include <stdbool.h>
38#include <stdint.h>
39#include <stddef.h>
[9af1c61]40#include <str_error.h>
[7354b5e]41#include <inttypes.h>
[9a1b20c]42#include <fibril.h>
43#include <errno.h>
44#include <udebug.h>
45#include <async.h>
[47e0a05b]46#include <task.h>
[3bf907a]47#include <mem.h>
[19f857a]48#include <str.h>
[4470e26]49#include <loader/loader.h>
[9d8a1ed]50#include <io/console.h>
51#include <io/keycode.h>
[1e4cada]52#include <fibril_synch.h>
[df02460]53#include <vfs/vfs.h>
[9a1b20c]54
[f2ef7fd]55#include <libc.h>
56
[28a3e74]57/* Temporary: service and method names */
[9a1b20c]58#include "proto.h"
59#include <ipc/services.h>
[6afc9d7]60#include <ipc/vfs.h>
[d3e6935]61#include <ipc/console.h>
[9a1b20c]62
63#include "syscalls.h"
64#include "ipcp.h"
65#include "errors.h"
[1643855]66#include "trace.h"
[9a1b20c]67
68#define THBUF_SIZE 64
[a5c3f73]69uintptr_t thread_hash_buf[THBUF_SIZE];
70int n_threads;
[9a1b20c]71
72int next_thread_id;
73
[c8404d4]74ipc_call_t thread_ipc_req[THBUF_SIZE];
75
[79ae36dd]76async_sess_t *sess;
[9ad5b5cc]77bool abort_trace;
[9a1b20c]78
[a5c3f73]79uintptr_t thash;
[9ad5b5cc]80static bool paused;
81static fibril_condvar_t state_cv;
82static fibril_mutex_t state_lock;
[84683fdc]83
[9ad5b5cc]84static bool cev_valid;
[79ae36dd]85static kbd_event_t cev;
[9a1b20c]86
[a5c3f73]87void thread_trace_start(uintptr_t thread_hash);
[9a1b20c]88
[1643855]89static task_id_t task_id;
[4470e26]90static loader_t *task_ldr;
[9ad5b5cc]91static bool task_wait_for;
[1643855]92
93/** Combination of events/data to print. */
94display_mask_t display_mask;
[9a1b20c]95
[4470e26]96static int program_run_fibril(void *arg);
[84683fdc]97static int cev_fibril(void *arg);
[4470e26]98
99static void program_run(void)
100{
101 fid_t fid;
102
103 fid = fibril_create(program_run_fibril, NULL);
104 if (fid == 0) {
105 printf("Error creating fibril\n");
106 exit(1);
107 }
108
109 fibril_add_ready(fid);
110}
111
[84683fdc]112static void cev_fibril_start(void)
113{
114 fid_t fid;
115
116 fid = fibril_create(cev_fibril, NULL);
117 if (fid == 0) {
118 printf("Error creating fibril\n");
119 exit(1);
120 }
121
122 fibril_add_ready(fid);
123}
124
[4470e26]125static int program_run_fibril(void *arg)
126{
127 int rc;
128
129 /*
130 * This must be done in background as it will block until
131 * we let the task reply to this call.
132 */
133 rc = loader_run(task_ldr);
134 if (rc != 0) {
135 printf("Error running program\n");
136 exit(1);
137 }
138
139 task_ldr = NULL;
140
141 printf("program_run_fibril exiting\n");
142 return 0;
143}
144
145
146static int connect_task(task_id_t task_id)
[9a1b20c]147{
[79ae36dd]148 async_sess_t *ksess = async_connect_kbox(task_id);
149
150 if (!ksess) {
151 if (errno == ENOTSUP) {
152 printf("You do not have userspace debugging support "
153 "compiled in the kernel.\n");
154 printf("Compile kernel with 'Support for userspace debuggers' "
155 "(CONFIG_UDEBUG) enabled.\n");
156 return errno;
157 }
158
[c9a29d6]159 printf("Error connecting\n");
[9af1c61]160 printf("ipc_connect_task(%" PRIu64 ") -> %s ", task_id, str_error_name(errno));
[79ae36dd]161 return errno;
[fb9b0b0]162 }
[79ae36dd]163
164 int rc = udebug_begin(ksess);
[c9a29d6]165 if (rc < 0) {
166 printf("udebug_begin() -> %d\n", rc);
167 return rc;
168 }
[79ae36dd]169
170 rc = udebug_set_evmask(ksess, UDEBUG_EM_ALL);
[c9a29d6]171 if (rc < 0) {
172 printf("udebug_set_evmask(0x%x) -> %d\n ", UDEBUG_EM_ALL, rc);
173 return rc;
174 }
[79ae36dd]175
176 sess = ksess;
[9a1b20c]177 return 0;
178}
179
180static int get_thread_list(void)
181{
182 int rc;
183 size_t tb_copied;
184 size_t tb_needed;
185 int i;
186
[79ae36dd]187 rc = udebug_thread_read(sess, thread_hash_buf,
[9a1b20c]188 THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
[c9a29d6]189 if (rc < 0) {
190 printf("udebug_thread_read() -> %d\n", rc);
191 return rc;
192 }
[9a1b20c]193
[a5c3f73]194 n_threads = tb_copied / sizeof(uintptr_t);
[9a1b20c]195
[c9a29d6]196 printf("Threads:");
197 for (i = 0; i < n_threads; i++) {
[7e752b2]198 printf(" [%d] (hash %p)", 1 + i, (void *) thread_hash_buf[i]);
[9a1b20c]199 }
[7e752b2]200 printf("\ntotal of %zu threads\n", tb_needed / sizeof(uintptr_t));
[9a1b20c]201
202 return 0;
203}
204
[356acd0]205void val_print(sysarg_t val, val_type_t v_type)
[9a1b20c]206{
[f019cc07]207 long sval;
208
209 sval = (long) val;
210
[abf3564]211 switch (v_type) {
212 case V_VOID:
213 printf("<void>");
214 break;
215
216 case V_INTEGER:
[f019cc07]217 printf("%ld", sval);
[abf3564]218 break;
219
220 case V_HASH:
[4470e26]221 case V_PTR:
[7e752b2]222 printf("%p", (void *) val);
[abf3564]223 break;
224
225 case V_ERRNO:
[f019cc07]226 if (sval >= -15 && sval <= 0) {
227 printf("%ld %s (%s)", sval,
228 err_desc[-sval].name,
229 err_desc[-sval].desc);
[9a1b20c]230 } else {
[f019cc07]231 printf("%ld", sval);
[9a1b20c]232 }
[abf3564]233 break;
234 case V_INT_ERRNO:
[f019cc07]235 if (sval >= -15 && sval < 0) {
236 printf("%ld %s (%s)", sval,
237 err_desc[-sval].name,
238 err_desc[-sval].desc);
[abf3564]239 } else {
[f019cc07]240 printf("%ld", sval);
[abf3564]241 }
242 break;
243
244 case V_CHAR:
[f019cc07]245 if (sval >= 0x20 && sval < 0x7f) {
[7e752b2]246 printf("'%c'", (char) sval);
[9a1b20c]247 } else {
[f019cc07]248 switch (sval) {
[abf3564]249 case '\a': printf("'\\a'"); break;
250 case '\b': printf("'\\b'"); break;
251 case '\n': printf("'\\n'"); break;
252 case '\r': printf("'\\r'"); break;
253 case '\t': printf("'\\t'"); break;
254 case '\\': printf("'\\\\'"); break;
[7e752b2]255 default: printf("'\\x%02" PRIxn "'", val); break;
[abf3564]256 }
[9a1b20c]257 }
[abf3564]258 break;
[9a1b20c]259 }
[abf3564]260}
261
262
[356acd0]263static void print_sc_retval(sysarg_t retval, val_type_t val_type)
[abf3564]264{
265 printf(" -> ");
266 val_print(retval, val_type);
[9a1b20c]267 putchar('\n');
268}
269
[a5c3f73]270static void print_sc_args(sysarg_t *sc_args, int n)
[9a1b20c]271{
272 int i;
273
274 putchar('(');
[7e752b2]275 if (n > 0) printf("%" PRIun, sc_args[0]);
[abf3564]276 for (i = 1; i < n; i++) {
[7e752b2]277 printf(", %" PRIun, sc_args[i]);
[9a1b20c]278 }
279 putchar(')');
280}
281
[a5c3f73]282static void sc_ipc_call_async_fast(sysarg_t *sc_args, sysarg_t sc_rc)
[9a1b20c]283{
284 ipc_call_t call;
[96b02eb9]285 sysarg_t phoneid;
[9a1b20c]286
[7b8f933]287 if (sc_rc != (sysarg_t) EOK)
[9a1b20c]288 return;
289
290 phoneid = sc_args[0];
291
[228e490]292 IPC_SET_IMETHOD(call, sc_args[1]);
[9a1b20c]293 IPC_SET_ARG1(call, sc_args[2]);
294 IPC_SET_ARG2(call, sc_args[3]);
295 IPC_SET_ARG3(call, sc_args[4]);
296 IPC_SET_ARG4(call, sc_args[5]);
297 IPC_SET_ARG5(call, 0);
298
299 ipcp_call_out(phoneid, &call, sc_rc);
300}
301
[a5c3f73]302static void sc_ipc_call_async_slow(sysarg_t *sc_args, sysarg_t sc_rc)
[9a1b20c]303{
304 ipc_call_t call;
305 int rc;
306
[7b8f933]307 if (sc_rc != (sysarg_t) EOK)
[9a1b20c]308 return;
309
310 memset(&call, 0, sizeof(call));
[79ae36dd]311 rc = udebug_mem_read(sess, &call.args, sc_args[1], sizeof(call.args));
[9a1b20c]312
313 if (rc >= 0) {
314 ipcp_call_out(sc_args[0], &call, sc_rc);
315 }
316}
317
[a5c3f73]318static void sc_ipc_wait(sysarg_t *sc_args, int sc_rc)
[9a1b20c]319{
320 ipc_call_t call;
321 int rc;
322
323 if (sc_rc == 0) return;
324
325 memset(&call, 0, sizeof(call));
[79ae36dd]326 rc = udebug_mem_read(sess, &call, sc_args[0], sizeof(call));
327
328 if (rc >= 0)
[9a1b20c]329 ipcp_call_in(&call, sc_rc);
330}
331
[a5c3f73]332static void event_syscall_b(unsigned thread_id, uintptr_t thread_hash,
333 unsigned sc_id, sysarg_t sc_rc)
[9a1b20c]334{
[a5c3f73]335 sysarg_t sc_args[6];
[9a1b20c]336 int rc;
337
338 /* Read syscall arguments */
[79ae36dd]339 rc = udebug_args_read(sess, thread_hash, sc_args);
[9a1b20c]340
341 if (rc < 0) {
342 printf("error\n");
343 return;
344 }
345
[1643855]346 if ((display_mask & DM_SYSCALL) != 0) {
347 /* Print syscall name and arguments */
[a3b339b4]348 if (syscall_desc_defined(sc_id)) {
349 printf("%s", syscall_desc[sc_id].name);
350 print_sc_args(sc_args, syscall_desc[sc_id].n_args);
351 }
352 else {
353 printf("unknown_syscall<%d>", sc_id);
354 print_sc_args(sc_args, 6);
355 }
[1643855]356 }
[9a1b20c]357}
358
[a5c3f73]359static void event_syscall_e(unsigned thread_id, uintptr_t thread_hash,
360 unsigned sc_id, sysarg_t sc_rc)
[9a1b20c]361{
[a5c3f73]362 sysarg_t sc_args[6];
[9a1b20c]363 int rv_type;
364 int rc;
365
366 /* Read syscall arguments */
[79ae36dd]367 rc = udebug_args_read(sess, thread_hash, sc_args);
[9a1b20c]368
369// printf("[%d] ", thread_id);
370
371 if (rc < 0) {
372 printf("error\n");
373 return;
374 }
375
[1643855]376 if ((display_mask & DM_SYSCALL) != 0) {
377 /* Print syscall return value */
[a3b339b4]378 if (syscall_desc_defined(sc_id))
379 rv_type = syscall_desc[sc_id].rv_type;
380 else
381 rv_type = V_PTR;
[1643855]382 print_sc_retval(sc_rc, rv_type);
383 }
[9a1b20c]384
385 switch (sc_id) {
386 case SYS_IPC_CALL_ASYNC_FAST:
387 sc_ipc_call_async_fast(sc_args, sc_rc);
388 break;
389 case SYS_IPC_CALL_ASYNC_SLOW:
390 sc_ipc_call_async_slow(sc_args, sc_rc);
391 break;
392 case SYS_IPC_WAIT:
393 sc_ipc_wait(sc_args, sc_rc);
394 break;
395 default:
396 break;
397 }
398}
399
[a5c3f73]400static void event_thread_b(uintptr_t hash)
[9a1b20c]401{
[7e752b2]402 printf("New thread, hash %p\n", (void *) hash);
[9a1b20c]403 thread_trace_start(hash);
404}
405
406static int trace_loop(void *thread_hash_arg)
407{
408 int rc;
409 unsigned ev_type;
[a5c3f73]410 uintptr_t thread_hash;
[9a1b20c]411 unsigned thread_id;
[a5c3f73]412 sysarg_t val0, val1;
[9a1b20c]413
[a5c3f73]414 thread_hash = (uintptr_t)thread_hash_arg;
[9a1b20c]415 thread_id = next_thread_id++;
[c8404d4]416 if (thread_id >= THBUF_SIZE) {
417 printf("Too many threads.\n");
418 return ELIMIT;
419 }
[9a1b20c]420
[7e752b2]421 printf("Start tracing thread [%u] (hash %p).\n",
422 thread_id, (void *) thread_hash);
[9a1b20c]423
424 while (!abort_trace) {
425
[84683fdc]426 fibril_mutex_lock(&state_lock);
[741fd16]427 if (paused) {
[7e752b2]428 printf("Thread [%u] paused. Press R to resume.\n",
[654a30a]429 thread_id);
430
431 while (paused)
[84683fdc]432 fibril_condvar_wait(&state_cv, &state_lock);
[654a30a]433
[7e752b2]434 printf("Thread [%u] resumed.\n", thread_id);
[741fd16]435 }
[84683fdc]436 fibril_mutex_unlock(&state_lock);
[741fd16]437
[9a1b20c]438 /* Run thread until an event occurs */
[79ae36dd]439 rc = udebug_go(sess, thread_hash,
[9a1b20c]440 &ev_type, &val0, &val1);
441
442// printf("rc = %d, ev_type=%d\n", rc, ev_type);
443 if (ev_type == UDEBUG_EVENT_FINISHED) {
[c9a29d6]444 /* Done tracing this thread */
[9a1b20c]445 break;
446 }
447
448 if (rc >= 0) {
449 switch (ev_type) {
450 case UDEBUG_EVENT_SYSCALL_B:
451 event_syscall_b(thread_id, thread_hash, val0, (int)val1);
452 break;
453 case UDEBUG_EVENT_SYSCALL_E:
454 event_syscall_e(thread_id, thread_hash, val0, (int)val1);
455 break;
456 case UDEBUG_EVENT_STOP:
[c9a29d6]457 printf("Stop event\n");
[84683fdc]458 fibril_mutex_lock(&state_lock);
[9ad5b5cc]459 paused = true;
[84683fdc]460 fibril_mutex_unlock(&state_lock);
[9a1b20c]461 break;
462 case UDEBUG_EVENT_THREAD_B:
463 event_thread_b(val0);
464 break;
465 case UDEBUG_EVENT_THREAD_E:
[7e752b2]466 printf("Thread %" PRIun " exited.\n", val0);
[84683fdc]467 fibril_mutex_lock(&state_lock);
[9ad5b5cc]468 abort_trace = true;
[84683fdc]469 fibril_condvar_broadcast(&state_cv);
470 fibril_mutex_unlock(&state_lock);
[9a1b20c]471 break;
472 default:
[ef687799]473 printf("Unknown event type %d.\n", ev_type);
[9a1b20c]474 break;
475 }
476 }
477
478 }
479
[ef687799]480 printf("Finished tracing thread [%d].\n", thread_id);
[9a1b20c]481 return 0;
482}
483
[a5c3f73]484void thread_trace_start(uintptr_t thread_hash)
[9a1b20c]485{
486 fid_t fid;
487
488 thash = thread_hash;
489
490 fid = fibril_create(trace_loop, (void *)thread_hash);
491 if (fid == 0) {
492 printf("Warning: Failed creating fibril\n");
493 }
494 fibril_add_ready(fid);
495}
496
[a000878c]497static loader_t *preload_task(const char *path, char **argv,
[4470e26]498 task_id_t *task_id)
[9a1b20c]499{
[4470e26]500 loader_t *ldr;
[9a1b20c]501 int rc;
502
[a000878c]503 /* Spawn a program loader */
[08b777e]504 ldr = loader_connect();
[4470e26]505 if (ldr == NULL)
[a3b339b4]506 return NULL;
[4470e26]507
508 /* Get task ID. */
509 rc = loader_get_task_id(ldr, task_id);
510 if (rc != EOK)
511 goto error;
512
[5126f80]513 /* Send program. */
[bb9ec2d]514 rc = loader_set_program_path(ldr, path);
[4470e26]515 if (rc != EOK)
516 goto error;
517
518 /* Send arguments */
[a000878c]519 rc = loader_set_args(ldr, (const char **) argv);
[4470e26]520 if (rc != EOK)
521 goto error;
522
[8e1dc00]523 /* Send default files */
[5126f80]524 int fd_root;
[7171760]525 int fd_stdin;
526 int fd_stdout;
527 int fd_stderr;
[8e1dc00]528
[5126f80]529 fd_root = vfs_root();
530 if (fd_root >= 0) {
531 rc = loader_add_inbox(ldr, "root", fd_root);
[9c4cf0d]532 vfs_put(fd_root);
[5126f80]533 if (rc != EOK)
534 goto error;
535 }
536
[bb9ec2d]537 if ((stdin != NULL) && (vfs_fhandle(stdin, &fd_stdin) == EOK)) {
538 rc = loader_add_inbox(ldr, "stdin", fd_stdin);
539 if (rc != EOK)
540 goto error;
541 }
[8e1dc00]542
[bb9ec2d]543 if ((stdout != NULL) && (vfs_fhandle(stdout, &fd_stdout) == EOK)) {
544 rc = loader_add_inbox(ldr, "stdout", fd_stdout);
545 if (rc != EOK)
546 goto error;
547 }
[8e1dc00]548
[bb9ec2d]549 if ((stderr != NULL) && (vfs_fhandle(stderr, &fd_stderr) == EOK)) {
550 rc = loader_add_inbox(ldr, "stderr", fd_stderr);
551 if (rc != EOK)
552 goto error;
553 }
[8e1dc00]554
[4470e26]555 /* Load the program. */
556 rc = loader_load_program(ldr);
557 if (rc != EOK)
558 goto error;
559
560 /* Success */
561 return ldr;
562
563 /* Error exit */
564error:
565 loader_abort(ldr);
566 return NULL;
567}
[9a1b20c]568
[84683fdc]569static int cev_fibril(void *arg)
570{
[07b7c48]571 cons_event_t event;
572
[84683fdc]573 (void) arg;
[79ae36dd]574
575 console_ctrl_t *console = console_init(stdin, stdout);
576
[84683fdc]577 while (true) {
578 fibril_mutex_lock(&state_lock);
579 while (cev_valid)
580 fibril_condvar_wait(&state_cv, &state_lock);
581 fibril_mutex_unlock(&state_lock);
[79ae36dd]582
[07b7c48]583 if (!console_get_event(console, &event))
[84683fdc]584 return -1;
[79ae36dd]585
[07b7c48]586 if (event.type == CEV_KEY) {
587 fibril_mutex_lock(&state_lock);
588 cev = event.ev.key;
589 cev_valid = true;
590 fibril_condvar_broadcast(&state_cv);
591 fibril_mutex_unlock(&state_lock);
592 }
[84683fdc]593 }
594}
595
[4470e26]596static void trace_task(task_id_t task_id)
597{
[79ae36dd]598 kbd_event_t ev;
[9d8a1ed]599 bool done;
[4470e26]600 int i;
601 int rc;
[9a1b20c]602
603 ipcp_init();
[c9a29d6]604
[9a1b20c]605 rc = get_thread_list();
606 if (rc < 0) {
607 printf("Failed to get thread list (error %d)\n", rc);
608 return;
609 }
610
[9ad5b5cc]611 abort_trace = false;
[9a1b20c]612
613 for (i = 0; i < n_threads; i++) {
614 thread_trace_start(thread_hash_buf[i]);
615 }
616
[9d8a1ed]617 done = false;
618
619 while (!done) {
[84683fdc]620 fibril_mutex_lock(&state_lock);
621 while (!cev_valid && !abort_trace)
622 fibril_condvar_wait(&state_cv, &state_lock);
623 fibril_mutex_unlock(&state_lock);
624
625 ev = cev;
626
627 fibril_mutex_lock(&state_lock);
628 cev_valid = false;
629 fibril_condvar_broadcast(&state_cv);
630 fibril_mutex_unlock(&state_lock);
631
632 if (abort_trace)
633 break;
[9d8a1ed]634
635 if (ev.type != KEY_PRESS)
636 continue;
637
638 switch (ev.key) {
639 case KC_Q:
640 done = true;
641 break;
642 case KC_P:
[741fd16]643 printf("Pause...\n");
[79ae36dd]644 rc = udebug_stop(sess, thash);
[654a30a]645 if (rc != EOK)
646 printf("Error: stop -> %d\n", rc);
[9d8a1ed]647 break;
648 case KC_R:
[84683fdc]649 fibril_mutex_lock(&state_lock);
[9ad5b5cc]650 paused = false;
[84683fdc]651 fibril_condvar_broadcast(&state_cv);
652 fibril_mutex_unlock(&state_lock);
[741fd16]653 printf("Resume...\n");
[9d8a1ed]654 break;
[7c014d1]655 default:
656 break;
[9a1b20c]657 }
658 }
659
[c9a29d6]660 printf("\nTerminate debugging session...\n");
[9ad5b5cc]661 abort_trace = true;
[79ae36dd]662 udebug_end(sess);
663 async_hangup(sess);
[9a1b20c]664
665 ipcp_cleanup();
666
[c9a29d6]667 printf("Done\n");
[9a1b20c]668 return;
669}
670
671static void main_init(void)
672{
673 proto_t *p;
674 oper_t *o;
675
[abf3564]676 val_type_t arg_def[OPER_MAX_ARGS] = {
677 V_INTEGER,
678 V_INTEGER,
679 V_INTEGER,
680 V_INTEGER,
681 V_INTEGER
682 };
683
684 val_type_t resp_def[OPER_MAX_ARGS] = {
685 V_INTEGER,
686 V_INTEGER,
687 V_INTEGER,
688 V_INTEGER,
[fa09449]689 V_INTEGER
[abf3564]690 };
691
[9a1b20c]692 next_thread_id = 1;
[9ad5b5cc]693 paused = false;
694 cev_valid = false;
[84683fdc]695
696 fibril_mutex_initialize(&state_lock);
697 fibril_condvar_initialize(&state_cv);
[9a1b20c]698
699 proto_init();
700
701 p = proto_new("vfs");
[58898d1d]702 o = oper_new("read", 3, arg_def, V_ERRNO, 1, resp_def);
[4198f9c3]703 proto_add_oper(p, VFS_IN_READ, o);
[58898d1d]704 o = oper_new("write", 3, arg_def, V_ERRNO, 1, resp_def);
[4198f9c3]705 proto_add_oper(p, VFS_IN_WRITE, o);
[9c4cf0d]706 o = oper_new("vfs_resize", 5, arg_def, V_ERRNO, 0, resp_def);
[67e881c]707 proto_add_oper(p, VFS_IN_RESIZE, o);
[9c4cf0d]708 o = oper_new("vfs_stat", 1, arg_def, V_ERRNO, 0, resp_def);
[fe91f66]709 proto_add_oper(p, VFS_IN_STAT, o);
[9c4cf0d]710 o = oper_new("vfs_put", 1, arg_def, V_ERRNO, 0, resp_def);
711 proto_add_oper(p, VFS_IN_PUT, o);
712 o = oper_new("vfs_mount", 2, arg_def, V_ERRNO, 0, resp_def);
[4198f9c3]713 proto_add_oper(p, VFS_IN_MOUNT, o);
[abf3564]714/* o = oper_new("unmount", 0, arg_def);
[4198f9c3]715 proto_add_oper(p, VFS_IN_UNMOUNT, o);*/
[9c4cf0d]716 o = oper_new("vfs_sync", 1, arg_def, V_ERRNO, 0, resp_def);
[a5facfb]717 proto_add_oper(p, VFS_IN_SYNC, o);
[741a7af9]718 o = oper_new("rename", 0, arg_def, V_ERRNO, 0, resp_def);
[4198f9c3]719 proto_add_oper(p, VFS_IN_RENAME, o);
[9c4cf0d]720 o = oper_new("vfs_statfs", 0, arg_def, V_ERRNO, 0, resp_def);
[5930e3f]721 proto_add_oper(p, VFS_IN_STATFS, o);
[9c4cf0d]722 o = oper_new("vfs_walk", 2, arg_def, V_INT_ERRNO, 0, resp_def);
[ff8c87c]723 proto_add_oper(p, VFS_IN_WALK, o);
[9c4cf0d]724 o = oper_new("vfs_open", 2, arg_def, V_ERRNO, 0, resp_def);
[fe91f66]725 proto_add_oper(p, VFS_IN_OPEN, o);
[9c4cf0d]726 o = oper_new("vfs_unlink", 3, arg_def, V_ERRNO, 0, resp_def);
[fe91f66]727 proto_add_oper(p, VFS_IN_UNLINK, o);
[9a1b20c]728
729 proto_register(SERVICE_VFS, p);
730}
731
[193d280c]732static void print_syntax(void)
[9a1b20c]733{
[47e0a05b]734 printf("Syntax:\n");
735 printf("\ttrace [+<events>] <executable> [<arg1> [...]]\n");
736 printf("or\ttrace [+<events>] -t <task_id>\n");
[1643855]737 printf("Events: (default is +tp)\n");
738 printf("\n");
739 printf("\tt ... Thread creation and termination\n");
740 printf("\ts ... System calls\n");
741 printf("\ti ... Low-level IPC\n");
742 printf("\tp ... Protocol level\n");
743 printf("\n");
[47e0a05b]744 printf("Examples:\n");
745 printf("\ttrace +s /app/tetris\n");
746 printf("\ttrace +tsip -t 12\n");
[9a1b20c]747}
748
[a000878c]749static display_mask_t parse_display_mask(const char *text)
[9a1b20c]750{
[f1cc9db]751 display_mask_t dm = 0;
[a000878c]752 const char *c = text;
753
[1643855]754 while (*c) {
755 switch (*c) {
[a000878c]756 case 't':
757 dm = dm | DM_THREAD;
758 break;
759 case 's':
760 dm = dm | DM_SYSCALL;
761 break;
762 case 'i':
763 dm = dm | DM_IPC;
764 break;
765 case 'p':
766 dm = dm | DM_SYSTEM | DM_USER;
767 break;
[1643855]768 default:
[ef687799]769 printf("Unexpected event type '%c'.\n", *c);
[1643855]770 exit(1);
771 }
[a000878c]772
[1643855]773 ++c;
774 }
[a000878c]775
[1643855]776 return dm;
777}
778
779static int parse_args(int argc, char *argv[])
780{
[9a1b20c]781 char *err_p;
782
[47e0a05b]783 task_id = 0;
784
[a000878c]785 --argc;
786 ++argv;
[1643855]787
[47e0a05b]788 while (argc > 0) {
[a000878c]789 char *arg = *argv;
[1643855]790 if (arg[0] == '+') {
791 display_mask = parse_display_mask(&arg[1]);
[47e0a05b]792 } else if (arg[0] == '-') {
793 if (arg[1] == 't') {
794 /* Trace an already running task */
[a000878c]795 --argc;
796 ++argv;
[47e0a05b]797 task_id = strtol(*argv, &err_p, 10);
[4470e26]798 task_ldr = NULL;
[9ad5b5cc]799 task_wait_for = false;
[47e0a05b]800 if (*err_p) {
801 printf("Task ID syntax error\n");
802 print_syntax();
803 return -1;
804 }
805 } else {
[7e752b2]806 printf("Uknown option '%c'\n", arg[0]);
[47e0a05b]807 print_syntax();
808 return -1;
809 }
[1643855]810 } else {
[47e0a05b]811 break;
[1643855]812 }
[a000878c]813
814 --argc;
815 ++argv;
[1643855]816 }
817
[47e0a05b]818 if (task_id != 0) {
[a000878c]819 if (argc == 0)
820 return 0;
[47e0a05b]821 printf("Extra arguments\n");
[9a1b20c]822 print_syntax();
[47e0a05b]823 return -1;
[9a1b20c]824 }
825
[47e0a05b]826 if (argc < 1) {
827 printf("Missing argument\n");
[9a1b20c]828 print_syntax();
[1643855]829 return -1;
[9a1b20c]830 }
831
[4470e26]832 /* Preload the specified program file. */
[47e0a05b]833 printf("Spawning '%s' with arguments:\n", *argv);
[a000878c]834
835 char **cp = argv;
836 while (*cp)
837 printf("'%s'\n", *cp++);
838
[4470e26]839 task_ldr = preload_task(*argv, argv, &task_id);
[9ad5b5cc]840 task_wait_for = true;
[47e0a05b]841
[1643855]842 return 0;
843}
844
845int main(int argc, char *argv[])
846{
[4470e26]847 int rc;
[6e3a44a]848 task_exit_t texit;
849 int retval;
[4470e26]850
[1643855]851 printf("System Call / IPC Tracer\n");
[ef687799]852 printf("Controls: Q - Quit, P - Pause, R - Resume\n");
[1643855]853
854 display_mask = DM_THREAD | DM_SYSTEM | DM_USER;
855
856 if (parse_args(argc, argv) < 0)
857 return 1;
858
[9a1b20c]859 main_init();
[4470e26]860
861 rc = connect_task(task_id);
862 if (rc < 0) {
[7e752b2]863 printf("Failed connecting to task %" PRIu64 ".\n", task_id);
[4470e26]864 return 1;
865 }
866
[7e752b2]867 printf("Connected to task %" PRIu64 ".\n", task_id);
[4470e26]868
[6e3a44a]869 if (task_ldr != NULL)
[4470e26]870 program_run();
871
[84683fdc]872 cev_fibril_start();
[4470e26]873 trace_task(task_id);
[1643855]874
[6e3a44a]875 if (task_wait_for) {
876 printf("Waiting for task to exit.\n");
877
[1c635d6]878 rc = task_wait_task_id(task_id, &texit, &retval);
[6e3a44a]879 if (rc != EOK) {
880 printf("Failed waiting for task.\n");
881 return -1;
882 }
883
884 if (texit == TASK_EXIT_NORMAL) {
885 printf("Task exited normally, return value %d.\n",
886 retval);
887 } else {
888 printf("Task exited unexpectedly.\n");
889 }
890 }
891
[1643855]892 return 0;
[9a1b20c]893}
894
895/** @}
896 */
Note: See TracBrowser for help on using the repository browser.