source: mainline/uspace/app/trace/trace.c@ 9abe2e5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9abe2e5 was 9c4cf0d, checked in by Jakub Jermar <jakub@…>, 9 years ago

Rename close() to vfs_put()

This is motivated mainly by the fact that a file handle does not
necessarily correspond to an open file and close() was no longer the
the opposite operation to open().

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