source: mainline/uspace/app/trace/trace.c@ 99013b84

Last change on this file since 99013b84 was 3fafe5e0, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix incorrectly indented double-slash comments.

  • Property mode set to 100644
File size: 18.4 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"
[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
[b7fd2a0]95static errno_t program_run_fibril(void *arg);
96static errno_t 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
[b7fd2a0]124static errno_t program_run_fibril(void *arg)
[4470e26]125{
[b7fd2a0]126 errno_t rc;
[4470e26]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);
[a53ed3a]133 if (rc != EOK) {
[4470e26]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
[b7fd2a0]145static errno_t connect_task(task_id_t task_id)
[9a1b20c]146{
[79ae36dd]147 async_sess_t *ksess = async_connect_kbox(task_id);
[a35b458]148
[79ae36dd]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 }
[a35b458]157
[c9a29d6]158 printf("Error connecting\n");
[9af1c61]159 printf("ipc_connect_task(%" PRIu64 ") -> %s ", task_id, str_error_name(errno));
[79ae36dd]160 return errno;
[fb9b0b0]161 }
[a35b458]162
[b7fd2a0]163 errno_t rc = udebug_begin(ksess);
[d5c1051]164 if (rc != EOK) {
[c1694b6b]165 printf("udebug_begin() -> %s\n", str_error_name(rc));
[c9a29d6]166 return rc;
167 }
[a35b458]168
[79ae36dd]169 rc = udebug_set_evmask(ksess, UDEBUG_EM_ALL);
[d5c1051]170 if (rc != EOK) {
[c1694b6b]171 printf("udebug_set_evmask(0x%x) -> %s\n ", UDEBUG_EM_ALL, str_error_name(rc));
[c9a29d6]172 return rc;
173 }
[a35b458]174
[79ae36dd]175 sess = ksess;
[9a1b20c]176 return 0;
177}
178
[b7fd2a0]179static errno_t get_thread_list(void)
[9a1b20c]180{
[b7fd2a0]181 errno_t rc;
[9a1b20c]182 size_t tb_copied;
183 size_t tb_needed;
184 int i;
185
[79ae36dd]186 rc = udebug_thread_read(sess, thread_hash_buf,
[1433ecda]187 THBUF_SIZE * sizeof(unsigned), &tb_copied, &tb_needed);
[d5c1051]188 if (rc != EOK) {
[c1694b6b]189 printf("udebug_thread_read() -> %s\n", str_error_name(rc));
[c9a29d6]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,
[b7fd2a0]227 str_error_name((errno_t) sval),
228 str_error((errno_t) sval));
[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,
[b7fd2a0]236 str_error_name((errno_t) sval),
237 str_error((errno_t) sval));
[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) {
[1433ecda]248 case '\a':
249 printf("'\\a'");
250 break;
251 case '\b':
252 printf("'\\b'");
253 break;
254 case '\n':
255 printf("'\\n'");
256 break;
257 case '\r':
258 printf("'\\r'");
259 break;
260 case '\t':
261 printf("'\\t'");
262 break;
263 case '\\':
264 printf("'\\\\'");
265 break;
266 default:
267 printf("'\\x%02" PRIxn "'", val);
268 break;
[abf3564]269 }
[9a1b20c]270 }
[abf3564]271 break;
[9a1b20c]272 }
[abf3564]273}
274
275
[1569a9b]276static void print_sc_retval(sysarg_t retval, val_type_t val_type)
[abf3564]277{
278 printf(" -> ");
279 val_print(retval, val_type);
[9a1b20c]280 putchar('\n');
281}
282
[a5c3f73]283static void print_sc_args(sysarg_t *sc_args, int n)
[9a1b20c]284{
285 int i;
286
287 putchar('(');
[1433ecda]288 if (n > 0)
289 printf("%" PRIun, sc_args[0]);
[abf3564]290 for (i = 1; i < n; i++) {
[7e752b2]291 printf(", %" PRIun, sc_args[i]);
[9a1b20c]292 }
293 putchar(')');
294}
295
[b7fd2a0]296static void sc_ipc_call_async_fast(sysarg_t *sc_args, errno_t sc_rc)
[9a1b20c]297{
298 ipc_call_t call;
[eadaeae8]299 cap_phone_handle_t phandle;
[a35b458]300
[25a179e]301 if (sc_rc != EOK)
[9a1b20c]302 return;
303
[eadaeae8]304 phandle = (cap_phone_handle_t) sc_args[0];
[9a1b20c]305
[228e490]306 IPC_SET_IMETHOD(call, sc_args[1]);
[9a1b20c]307 IPC_SET_ARG1(call, sc_args[2]);
308 IPC_SET_ARG2(call, sc_args[3]);
309 IPC_SET_ARG3(call, sc_args[4]);
310 IPC_SET_ARG4(call, sc_args[5]);
311 IPC_SET_ARG5(call, 0);
312
[eadaeae8]313 ipcp_call_out(phandle, &call, 0);
[9a1b20c]314}
315
[b7fd2a0]316static void sc_ipc_call_async_slow(sysarg_t *sc_args, errno_t sc_rc)
[9a1b20c]317{
318 ipc_call_t call;
[b7fd2a0]319 errno_t rc;
[9a1b20c]320
[1569a9b]321 if (sc_rc != EOK)
[9a1b20c]322 return;
323
324 memset(&call, 0, sizeof(call));
[79ae36dd]325 rc = udebug_mem_read(sess, &call.args, sc_args[1], sizeof(call.args));
[9a1b20c]326
[1569a9b]327 if (rc == EOK) {
[eadaeae8]328 ipcp_call_out((cap_phone_handle_t) sc_args[0], &call, 0);
[9a1b20c]329 }
330}
331
[eadaeae8]332static void sc_ipc_wait(sysarg_t *sc_args, cap_call_handle_t sc_rc)
[9a1b20c]333{
334 ipc_call_t call;
[b7fd2a0]335 errno_t rc;
[9a1b20c]336
[1433ecda]337 if (sc_rc == 0)
338 return;
[9a1b20c]339
340 memset(&call, 0, sizeof(call));
[79ae36dd]341 rc = udebug_mem_read(sess, &call, sc_args[0], sizeof(call));
[a35b458]342
[d5c1051]343 if (rc == EOK)
[9a1b20c]344 ipcp_call_in(&call, sc_rc);
345}
346
[a5c3f73]347static void event_syscall_b(unsigned thread_id, uintptr_t thread_hash,
348 unsigned sc_id, sysarg_t sc_rc)
[9a1b20c]349{
[a5c3f73]350 sysarg_t sc_args[6];
[b7fd2a0]351 errno_t rc;
[9a1b20c]352
353 /* Read syscall arguments */
[79ae36dd]354 rc = udebug_args_read(sess, thread_hash, sc_args);
[9a1b20c]355
[d5c1051]356 if (rc != EOK) {
[9a1b20c]357 printf("error\n");
358 return;
359 }
360
[1643855]361 if ((display_mask & DM_SYSCALL) != 0) {
362 /* Print syscall name and arguments */
[a3b339b4]363 if (syscall_desc_defined(sc_id)) {
364 printf("%s", syscall_desc[sc_id].name);
365 print_sc_args(sc_args, syscall_desc[sc_id].n_args);
[1433ecda]366 } else {
[a3b339b4]367 printf("unknown_syscall<%d>", sc_id);
368 print_sc_args(sc_args, 6);
369 }
[1643855]370 }
[9a1b20c]371}
372
[a5c3f73]373static void event_syscall_e(unsigned thread_id, uintptr_t thread_hash,
374 unsigned sc_id, sysarg_t sc_rc)
[9a1b20c]375{
[a5c3f73]376 sysarg_t sc_args[6];
[9a1b20c]377 int rv_type;
[b7fd2a0]378 errno_t rc;
[9a1b20c]379
380 /* Read syscall arguments */
[79ae36dd]381 rc = udebug_args_read(sess, thread_hash, sc_args);
[9a1b20c]382
[d5c1051]383 if (rc != EOK) {
[9a1b20c]384 printf("error\n");
385 return;
386 }
387
[1643855]388 if ((display_mask & DM_SYSCALL) != 0) {
389 /* Print syscall return value */
[a3b339b4]390 if (syscall_desc_defined(sc_id))
391 rv_type = syscall_desc[sc_id].rv_type;
392 else
393 rv_type = V_PTR;
[1643855]394 print_sc_retval(sc_rc, rv_type);
395 }
[9a1b20c]396
397 switch (sc_id) {
398 case SYS_IPC_CALL_ASYNC_FAST:
[b7fd2a0]399 sc_ipc_call_async_fast(sc_args, (errno_t) sc_rc);
[9a1b20c]400 break;
401 case SYS_IPC_CALL_ASYNC_SLOW:
[b7fd2a0]402 sc_ipc_call_async_slow(sc_args, (errno_t) sc_rc);
[9a1b20c]403 break;
404 case SYS_IPC_WAIT:
[eadaeae8]405 sc_ipc_wait(sc_args, (cap_call_handle_t) sc_rc);
[9a1b20c]406 break;
407 default:
408 break;
409 }
410}
411
[a5c3f73]412static void event_thread_b(uintptr_t hash)
[9a1b20c]413{
[7e752b2]414 printf("New thread, hash %p\n", (void *) hash);
[9a1b20c]415 thread_trace_start(hash);
416}
417
[b7fd2a0]418static errno_t trace_loop(void *thread_hash_arg)
[9a1b20c]419{
[b7fd2a0]420 errno_t rc;
[9a1b20c]421 unsigned ev_type;
[a5c3f73]422 uintptr_t thread_hash;
[9a1b20c]423 unsigned thread_id;
[a5c3f73]424 sysarg_t val0, val1;
[9a1b20c]425
[a5c3f73]426 thread_hash = (uintptr_t)thread_hash_arg;
[9a1b20c]427 thread_id = next_thread_id++;
[c8404d4]428 if (thread_id >= THBUF_SIZE) {
429 printf("Too many threads.\n");
430 return ELIMIT;
431 }
[9a1b20c]432
[7e752b2]433 printf("Start tracing thread [%u] (hash %p).\n",
434 thread_id, (void *) thread_hash);
[9a1b20c]435
436 while (!abort_trace) {
437
[84683fdc]438 fibril_mutex_lock(&state_lock);
[741fd16]439 if (paused) {
[7e752b2]440 printf("Thread [%u] paused. Press R to resume.\n",
[654a30a]441 thread_id);
442
443 while (paused)
[84683fdc]444 fibril_condvar_wait(&state_cv, &state_lock);
[654a30a]445
[7e752b2]446 printf("Thread [%u] resumed.\n", thread_id);
[741fd16]447 }
[84683fdc]448 fibril_mutex_unlock(&state_lock);
[741fd16]449
[9a1b20c]450 /* Run thread until an event occurs */
[79ae36dd]451 rc = udebug_go(sess, thread_hash,
[9a1b20c]452 &ev_type, &val0, &val1);
453
454 if (ev_type == UDEBUG_EVENT_FINISHED) {
[c9a29d6]455 /* Done tracing this thread */
[9a1b20c]456 break;
457 }
458
[d5c1051]459 if (rc == EOK) {
[9a1b20c]460 switch (ev_type) {
461 case UDEBUG_EVENT_SYSCALL_B:
462 event_syscall_b(thread_id, thread_hash, val0, (int)val1);
463 break;
464 case UDEBUG_EVENT_SYSCALL_E:
465 event_syscall_e(thread_id, thread_hash, val0, (int)val1);
466 break;
467 case UDEBUG_EVENT_STOP:
[c9a29d6]468 printf("Stop event\n");
[84683fdc]469 fibril_mutex_lock(&state_lock);
[9ad5b5cc]470 paused = true;
[84683fdc]471 fibril_mutex_unlock(&state_lock);
[9a1b20c]472 break;
473 case UDEBUG_EVENT_THREAD_B:
474 event_thread_b(val0);
475 break;
476 case UDEBUG_EVENT_THREAD_E:
[7e752b2]477 printf("Thread %" PRIun " exited.\n", val0);
[84683fdc]478 fibril_mutex_lock(&state_lock);
[9ad5b5cc]479 abort_trace = true;
[84683fdc]480 fibril_condvar_broadcast(&state_cv);
481 fibril_mutex_unlock(&state_lock);
[9a1b20c]482 break;
483 default:
[ef687799]484 printf("Unknown event type %d.\n", ev_type);
[9a1b20c]485 break;
486 }
487 }
488
489 }
490
[ef687799]491 printf("Finished tracing thread [%d].\n", thread_id);
[9a1b20c]492 return 0;
493}
494
[a5c3f73]495void thread_trace_start(uintptr_t thread_hash)
[9a1b20c]496{
497 fid_t fid;
498
499 thash = thread_hash;
500
501 fid = fibril_create(trace_loop, (void *)thread_hash);
502 if (fid == 0) {
503 printf("Warning: Failed creating fibril\n");
504 }
505 fibril_add_ready(fid);
506}
507
[a000878c]508static loader_t *preload_task(const char *path, char **argv,
[4470e26]509 task_id_t *task_id)
[9a1b20c]510{
[4470e26]511 loader_t *ldr;
[b7fd2a0]512 errno_t rc;
[9a1b20c]513
[a000878c]514 /* Spawn a program loader */
[08b777e]515 ldr = loader_connect();
[4470e26]516 if (ldr == NULL)
[a3b339b4]517 return NULL;
[4470e26]518
519 /* Get task ID. */
520 rc = loader_get_task_id(ldr, task_id);
521 if (rc != EOK)
522 goto error;
523
[5126f80]524 /* Send program. */
[bb9ec2d]525 rc = loader_set_program_path(ldr, path);
[4470e26]526 if (rc != EOK)
527 goto error;
528
529 /* Send arguments */
[a000878c]530 rc = loader_set_args(ldr, (const char **) argv);
[4470e26]531 if (rc != EOK)
532 goto error;
533
[8e1dc00]534 /* Send default files */
[5126f80]535 int fd_root;
[7171760]536 int fd_stdin;
537 int fd_stdout;
538 int fd_stderr;
[a35b458]539
[5126f80]540 fd_root = vfs_root();
541 if (fd_root >= 0) {
542 rc = loader_add_inbox(ldr, "root", fd_root);
[9c4cf0d]543 vfs_put(fd_root);
[5126f80]544 if (rc != EOK)
545 goto error;
546 }
[a35b458]547
[bb9ec2d]548 if ((stdin != NULL) && (vfs_fhandle(stdin, &fd_stdin) == EOK)) {
549 rc = loader_add_inbox(ldr, "stdin", fd_stdin);
550 if (rc != EOK)
551 goto error;
552 }
[a35b458]553
[bb9ec2d]554 if ((stdout != NULL) && (vfs_fhandle(stdout, &fd_stdout) == EOK)) {
555 rc = loader_add_inbox(ldr, "stdout", fd_stdout);
556 if (rc != EOK)
557 goto error;
558 }
[a35b458]559
[bb9ec2d]560 if ((stderr != NULL) && (vfs_fhandle(stderr, &fd_stderr) == EOK)) {
561 rc = loader_add_inbox(ldr, "stderr", fd_stderr);
562 if (rc != EOK)
563 goto error;
564 }
[a35b458]565
[4470e26]566 /* Load the program. */
567 rc = loader_load_program(ldr);
568 if (rc != EOK)
569 goto error;
570
571 /* Success */
572 return ldr;
573
574 /* Error exit */
575error:
576 loader_abort(ldr);
577 return NULL;
578}
[9a1b20c]579
[b7fd2a0]580static errno_t cev_fibril(void *arg)
[84683fdc]581{
[07b7c48]582 cons_event_t event;
583
[84683fdc]584 (void) arg;
[a35b458]585
[79ae36dd]586 console_ctrl_t *console = console_init(stdin, stdout);
[a35b458]587
[84683fdc]588 while (true) {
589 fibril_mutex_lock(&state_lock);
590 while (cev_valid)
591 fibril_condvar_wait(&state_cv, &state_lock);
592 fibril_mutex_unlock(&state_lock);
[a35b458]593
[07b7c48]594 if (!console_get_event(console, &event))
[1569a9b]595 return EINVAL;
[a35b458]596
[07b7c48]597 if (event.type == CEV_KEY) {
598 fibril_mutex_lock(&state_lock);
599 cev = event.ev.key;
600 cev_valid = true;
601 fibril_condvar_broadcast(&state_cv);
602 fibril_mutex_unlock(&state_lock);
603 }
[84683fdc]604 }
605}
606
[4470e26]607static void trace_task(task_id_t task_id)
608{
[79ae36dd]609 kbd_event_t ev;
[9d8a1ed]610 bool done;
[4470e26]611 int i;
[b7fd2a0]612 errno_t rc;
[9a1b20c]613
614 ipcp_init();
[c9a29d6]615
[9a1b20c]616 rc = get_thread_list();
[d5c1051]617 if (rc != EOK) {
[c1694b6b]618 printf("Failed to get thread list (%s)\n", str_error(rc));
[9a1b20c]619 return;
620 }
621
[9ad5b5cc]622 abort_trace = false;
[9a1b20c]623
624 for (i = 0; i < n_threads; i++) {
625 thread_trace_start(thread_hash_buf[i]);
626 }
627
[9d8a1ed]628 done = false;
629
630 while (!done) {
[84683fdc]631 fibril_mutex_lock(&state_lock);
632 while (!cev_valid && !abort_trace)
633 fibril_condvar_wait(&state_cv, &state_lock);
634 fibril_mutex_unlock(&state_lock);
635
636 ev = cev;
637
638 fibril_mutex_lock(&state_lock);
639 cev_valid = false;
640 fibril_condvar_broadcast(&state_cv);
641 fibril_mutex_unlock(&state_lock);
642
643 if (abort_trace)
644 break;
[9d8a1ed]645
646 if (ev.type != KEY_PRESS)
647 continue;
648
649 switch (ev.key) {
650 case KC_Q:
651 done = true;
652 break;
653 case KC_P:
[741fd16]654 printf("Pause...\n");
[79ae36dd]655 rc = udebug_stop(sess, thash);
[654a30a]656 if (rc != EOK)
[c1694b6b]657 printf("Error: stop -> %s\n", str_error_name(rc));
[9d8a1ed]658 break;
659 case KC_R:
[84683fdc]660 fibril_mutex_lock(&state_lock);
[9ad5b5cc]661 paused = false;
[84683fdc]662 fibril_condvar_broadcast(&state_cv);
663 fibril_mutex_unlock(&state_lock);
[741fd16]664 printf("Resume...\n");
[9d8a1ed]665 break;
[7c014d1]666 default:
667 break;
[9a1b20c]668 }
669 }
670
[c9a29d6]671 printf("\nTerminate debugging session...\n");
[9ad5b5cc]672 abort_trace = true;
[79ae36dd]673 udebug_end(sess);
674 async_hangup(sess);
[9a1b20c]675
676 ipcp_cleanup();
677
[c9a29d6]678 printf("Done\n");
[9a1b20c]679 return;
680}
681
682static void main_init(void)
683{
684 proto_t *p;
685 oper_t *o;
686
[abf3564]687 val_type_t arg_def[OPER_MAX_ARGS] = {
688 V_INTEGER,
689 V_INTEGER,
690 V_INTEGER,
691 V_INTEGER,
[1b20da0]692 V_INTEGER
[abf3564]693 };
694
695 val_type_t resp_def[OPER_MAX_ARGS] = {
696 V_INTEGER,
697 V_INTEGER,
698 V_INTEGER,
699 V_INTEGER,
[fa09449]700 V_INTEGER
[abf3564]701 };
702
[9a1b20c]703 next_thread_id = 1;
[9ad5b5cc]704 paused = false;
705 cev_valid = false;
[84683fdc]706
707 fibril_mutex_initialize(&state_lock);
708 fibril_condvar_initialize(&state_cv);
[9a1b20c]709
710 proto_init();
711
712 p = proto_new("vfs");
[58898d1d]713 o = oper_new("read", 3, arg_def, V_ERRNO, 1, resp_def);
[4198f9c3]714 proto_add_oper(p, VFS_IN_READ, o);
[58898d1d]715 o = oper_new("write", 3, arg_def, V_ERRNO, 1, resp_def);
[4198f9c3]716 proto_add_oper(p, VFS_IN_WRITE, o);
[9c4cf0d]717 o = oper_new("vfs_resize", 5, arg_def, V_ERRNO, 0, resp_def);
[67e881c]718 proto_add_oper(p, VFS_IN_RESIZE, o);
[9c4cf0d]719 o = oper_new("vfs_stat", 1, arg_def, V_ERRNO, 0, resp_def);
[fe91f66]720 proto_add_oper(p, VFS_IN_STAT, o);
[9c4cf0d]721 o = oper_new("vfs_put", 1, arg_def, V_ERRNO, 0, resp_def);
722 proto_add_oper(p, VFS_IN_PUT, o);
723 o = oper_new("vfs_mount", 2, arg_def, V_ERRNO, 0, resp_def);
[4198f9c3]724 proto_add_oper(p, VFS_IN_MOUNT, o);
[abf3564]725/* o = oper_new("unmount", 0, arg_def);
[4198f9c3]726 proto_add_oper(p, VFS_IN_UNMOUNT, o);*/
[9c4cf0d]727 o = oper_new("vfs_sync", 1, arg_def, V_ERRNO, 0, resp_def);
[a5facfb]728 proto_add_oper(p, VFS_IN_SYNC, o);
[741a7af9]729 o = oper_new("rename", 0, arg_def, V_ERRNO, 0, resp_def);
[4198f9c3]730 proto_add_oper(p, VFS_IN_RENAME, o);
[9c4cf0d]731 o = oper_new("vfs_statfs", 0, arg_def, V_ERRNO, 0, resp_def);
[5930e3f]732 proto_add_oper(p, VFS_IN_STATFS, o);
[9c4cf0d]733 o = oper_new("vfs_walk", 2, arg_def, V_INT_ERRNO, 0, resp_def);
[ff8c87c]734 proto_add_oper(p, VFS_IN_WALK, o);
[9c4cf0d]735 o = oper_new("vfs_open", 2, arg_def, V_ERRNO, 0, resp_def);
[fe91f66]736 proto_add_oper(p, VFS_IN_OPEN, o);
[9c4cf0d]737 o = oper_new("vfs_unlink", 3, arg_def, V_ERRNO, 0, resp_def);
[fe91f66]738 proto_add_oper(p, VFS_IN_UNLINK, o);
[9a1b20c]739
740 proto_register(SERVICE_VFS, p);
741}
742
[193d280c]743static void print_syntax(void)
[9a1b20c]744{
[47e0a05b]745 printf("Syntax:\n");
746 printf("\ttrace [+<events>] <executable> [<arg1> [...]]\n");
747 printf("or\ttrace [+<events>] -t <task_id>\n");
[1643855]748 printf("Events: (default is +tp)\n");
749 printf("\n");
750 printf("\tt ... Thread creation and termination\n");
751 printf("\ts ... System calls\n");
752 printf("\ti ... Low-level IPC\n");
753 printf("\tp ... Protocol level\n");
754 printf("\n");
[47e0a05b]755 printf("Examples:\n");
756 printf("\ttrace +s /app/tetris\n");
757 printf("\ttrace +tsip -t 12\n");
[9a1b20c]758}
759
[a000878c]760static display_mask_t parse_display_mask(const char *text)
[9a1b20c]761{
[f1cc9db]762 display_mask_t dm = 0;
[a000878c]763 const char *c = text;
[a35b458]764
[1643855]765 while (*c) {
766 switch (*c) {
[a000878c]767 case 't':
768 dm = dm | DM_THREAD;
769 break;
770 case 's':
771 dm = dm | DM_SYSCALL;
772 break;
773 case 'i':
774 dm = dm | DM_IPC;
775 break;
776 case 'p':
777 dm = dm | DM_SYSTEM | DM_USER;
778 break;
[1643855]779 default:
[ef687799]780 printf("Unexpected event type '%c'.\n", *c);
[1643855]781 exit(1);
782 }
[a35b458]783
[1643855]784 ++c;
785 }
[a35b458]786
[1643855]787 return dm;
788}
789
790static int parse_args(int argc, char *argv[])
791{
[9a1b20c]792 char *err_p;
793
[47e0a05b]794 task_id = 0;
795
[a000878c]796 --argc;
797 ++argv;
[1643855]798
[47e0a05b]799 while (argc > 0) {
[a000878c]800 char *arg = *argv;
[1643855]801 if (arg[0] == '+') {
802 display_mask = parse_display_mask(&arg[1]);
[47e0a05b]803 } else if (arg[0] == '-') {
804 if (arg[1] == 't') {
805 /* Trace an already running task */
[a000878c]806 --argc;
807 ++argv;
[47e0a05b]808 task_id = strtol(*argv, &err_p, 10);
[4470e26]809 task_ldr = NULL;
[9ad5b5cc]810 task_wait_for = false;
[47e0a05b]811 if (*err_p) {
812 printf("Task ID syntax error\n");
813 print_syntax();
814 return -1;
815 }
816 } else {
[7e752b2]817 printf("Uknown option '%c'\n", arg[0]);
[47e0a05b]818 print_syntax();
819 return -1;
820 }
[1643855]821 } else {
[47e0a05b]822 break;
[1643855]823 }
[a35b458]824
[a000878c]825 --argc;
826 ++argv;
[1643855]827 }
828
[47e0a05b]829 if (task_id != 0) {
[a000878c]830 if (argc == 0)
831 return 0;
[47e0a05b]832 printf("Extra arguments\n");
[9a1b20c]833 print_syntax();
[47e0a05b]834 return -1;
[9a1b20c]835 }
836
[47e0a05b]837 if (argc < 1) {
838 printf("Missing argument\n");
[9a1b20c]839 print_syntax();
[1643855]840 return -1;
[9a1b20c]841 }
842
[4470e26]843 /* Preload the specified program file. */
[47e0a05b]844 printf("Spawning '%s' with arguments:\n", *argv);
[a35b458]845
[a000878c]846 char **cp = argv;
847 while (*cp)
848 printf("'%s'\n", *cp++);
[a35b458]849
[4470e26]850 task_ldr = preload_task(*argv, argv, &task_id);
[9ad5b5cc]851 task_wait_for = true;
[47e0a05b]852
[1643855]853 return 0;
854}
855
856int main(int argc, char *argv[])
857{
[b7fd2a0]858 errno_t rc;
[6e3a44a]859 task_exit_t texit;
860 int retval;
[4470e26]861
[1643855]862 printf("System Call / IPC Tracer\n");
[ef687799]863 printf("Controls: Q - Quit, P - Pause, R - Resume\n");
[1643855]864
865 display_mask = DM_THREAD | DM_SYSTEM | DM_USER;
866
867 if (parse_args(argc, argv) < 0)
868 return 1;
869
[9a1b20c]870 main_init();
[4470e26]871
872 rc = connect_task(task_id);
[d5c1051]873 if (rc != EOK) {
[7e752b2]874 printf("Failed connecting to task %" PRIu64 ".\n", task_id);
[4470e26]875 return 1;
876 }
877
[7e752b2]878 printf("Connected to task %" PRIu64 ".\n", task_id);
[4470e26]879
[6e3a44a]880 if (task_ldr != NULL)
[4470e26]881 program_run();
882
[84683fdc]883 cev_fibril_start();
[4470e26]884 trace_task(task_id);
[1643855]885
[6e3a44a]886 if (task_wait_for) {
887 printf("Waiting for task to exit.\n");
888
[1c635d6]889 rc = task_wait_task_id(task_id, &texit, &retval);
[6e3a44a]890 if (rc != EOK) {
891 printf("Failed waiting for task.\n");
892 return -1;
893 }
894
895 if (texit == TASK_EXIT_NORMAL) {
896 printf("Task exited normally, return value %d.\n",
897 retval);
898 } else {
899 printf("Task exited unexpectedly.\n");
900 }
901 }
902
[1643855]903 return 0;
[9a1b20c]904}
905
906/** @}
907 */
Note: See TracBrowser for help on using the repository browser.