source: mainline/uspace/app/trace/trace.c@ c7325dd6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c7325dd6 was 7c014d1, checked in by Martin Decky <martin@…>, 14 years ago

console and framebuffer server rewrite

  • Property mode set to 100644
File size: 19.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>
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>
[9ad5b5cc]45#include <bool.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>
59#include "../../srv/vfs/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 free(task_ldr);
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");
[79ae36dd]160 printf("ipc_connect_task(%" PRIu64 ") -> %d ", task_id, errno);
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
[f019cc07]287 if (sc_rc == (sysarg_t) IPC_CALLRET_FATAL ||
288 sc_rc == (sysarg_t) IPC_CALLRET_TEMPORARY)
[9a1b20c]289 return;
290
291 phoneid = sc_args[0];
292
[228e490]293 IPC_SET_IMETHOD(call, sc_args[1]);
[9a1b20c]294 IPC_SET_ARG1(call, sc_args[2]);
295 IPC_SET_ARG2(call, sc_args[3]);
296 IPC_SET_ARG3(call, sc_args[4]);
297 IPC_SET_ARG4(call, sc_args[5]);
298 IPC_SET_ARG5(call, 0);
299
300 ipcp_call_out(phoneid, &call, sc_rc);
301}
302
[a5c3f73]303static void sc_ipc_call_async_slow(sysarg_t *sc_args, sysarg_t sc_rc)
[9a1b20c]304{
305 ipc_call_t call;
306 int rc;
307
[f019cc07]308 if (sc_rc == (sysarg_t) IPC_CALLRET_FATAL ||
309 sc_rc == (sysarg_t) IPC_CALLRET_TEMPORARY)
[9a1b20c]310 return;
311
312 memset(&call, 0, sizeof(call));
[79ae36dd]313 rc = udebug_mem_read(sess, &call.args, sc_args[1], sizeof(call.args));
[9a1b20c]314
315 if (rc >= 0) {
316 ipcp_call_out(sc_args[0], &call, sc_rc);
317 }
318}
319
[a5c3f73]320static void sc_ipc_call_sync_fast(sysarg_t *sc_args)
[9a1b20c]321{
322 ipc_call_t question, reply;
323 int rc;
[79ae36dd]324 int phoneid;
[9a1b20c]325
[79ae36dd]326 phoneid = sc_args[0];
[9a1b20c]327
[228e490]328 IPC_SET_IMETHOD(question, sc_args[1]);
[9a1b20c]329 IPC_SET_ARG1(question, sc_args[2]);
330 IPC_SET_ARG2(question, sc_args[3]);
331 IPC_SET_ARG3(question, sc_args[4]);
332 IPC_SET_ARG4(question, 0);
333 IPC_SET_ARG5(question, 0);
334
335 memset(&reply, 0, sizeof(reply));
[79ae36dd]336 rc = udebug_mem_read(sess, &reply.args, sc_args[5], sizeof(reply.args));
337 if (rc < 0)
338 return;
339
340 ipcp_call_sync(phoneid, &question, &reply);
[9a1b20c]341}
342
[c8404d4]343static void sc_ipc_call_sync_slow_b(unsigned thread_id, sysarg_t *sc_args)
[9a1b20c]344{
[f019cc07]345 ipc_call_t question;
[9a1b20c]346 int rc;
347
348 memset(&question, 0, sizeof(question));
[79ae36dd]349 rc = udebug_mem_read(sess, &question.args, sc_args[1],
[f019cc07]350 sizeof(question.args));
351
[c8404d4]352 if (rc < 0) {
353 printf("Error: mem_read->%d\n", rc);
354 return;
355 }
356
357 thread_ipc_req[thread_id] = question;
358}
359
360static void sc_ipc_call_sync_slow_e(unsigned thread_id, sysarg_t *sc_args)
361{
[f019cc07]362 ipc_call_t reply;
[c8404d4]363 int rc;
[9a1b20c]364
365 memset(&reply, 0, sizeof(reply));
[79ae36dd]366 rc = udebug_mem_read(sess, &reply.args, sc_args[2],
[f019cc07]367 sizeof(reply.args));
368
[c8404d4]369 if (rc < 0) {
370 printf("Error: mem_read->%d\n", rc);
371 return;
372 }
[9a1b20c]373
[c8404d4]374 ipcp_call_sync(sc_args[0], &thread_ipc_req[thread_id], &reply);
[9a1b20c]375}
376
[a5c3f73]377static void sc_ipc_wait(sysarg_t *sc_args, int sc_rc)
[9a1b20c]378{
379 ipc_call_t call;
380 int rc;
381
382 if (sc_rc == 0) return;
383
384 memset(&call, 0, sizeof(call));
[79ae36dd]385 rc = udebug_mem_read(sess, &call, sc_args[0], sizeof(call));
386
387 if (rc >= 0)
[9a1b20c]388 ipcp_call_in(&call, sc_rc);
389}
390
[a5c3f73]391static void event_syscall_b(unsigned thread_id, uintptr_t thread_hash,
392 unsigned sc_id, sysarg_t sc_rc)
[9a1b20c]393{
[a5c3f73]394 sysarg_t sc_args[6];
[9a1b20c]395 int rc;
396
397 /* Read syscall arguments */
[79ae36dd]398 rc = udebug_args_read(sess, thread_hash, sc_args);
[9a1b20c]399
400 if (rc < 0) {
401 printf("error\n");
402 return;
403 }
404
[1643855]405 if ((display_mask & DM_SYSCALL) != 0) {
406 /* Print syscall name and arguments */
407 printf("%s", syscall_desc[sc_id].name);
408 print_sc_args(sc_args, syscall_desc[sc_id].n_args);
409 }
[9a1b20c]410
[c8404d4]411 switch (sc_id) {
412 case SYS_IPC_CALL_SYNC_SLOW:
413 sc_ipc_call_sync_slow_b(thread_id, sc_args);
414 break;
415 default:
416 break;
417 }
[9a1b20c]418}
419
[a5c3f73]420static void event_syscall_e(unsigned thread_id, uintptr_t thread_hash,
421 unsigned sc_id, sysarg_t sc_rc)
[9a1b20c]422{
[a5c3f73]423 sysarg_t sc_args[6];
[9a1b20c]424 int rv_type;
425 int rc;
426
427 /* Read syscall arguments */
[79ae36dd]428 rc = udebug_args_read(sess, thread_hash, sc_args);
[9a1b20c]429
430// printf("[%d] ", thread_id);
431
432 if (rc < 0) {
433 printf("error\n");
434 return;
435 }
436
[1643855]437 if ((display_mask & DM_SYSCALL) != 0) {
438 /* Print syscall return value */
439 rv_type = syscall_desc[sc_id].rv_type;
440 print_sc_retval(sc_rc, rv_type);
441 }
[9a1b20c]442
443 switch (sc_id) {
444 case SYS_IPC_CALL_ASYNC_FAST:
445 sc_ipc_call_async_fast(sc_args, sc_rc);
446 break;
447 case SYS_IPC_CALL_ASYNC_SLOW:
448 sc_ipc_call_async_slow(sc_args, sc_rc);
449 break;
450 case SYS_IPC_CALL_SYNC_FAST:
451 sc_ipc_call_sync_fast(sc_args);
452 break;
453 case SYS_IPC_CALL_SYNC_SLOW:
[c8404d4]454 sc_ipc_call_sync_slow_e(thread_id, sc_args);
[9a1b20c]455 break;
456 case SYS_IPC_WAIT:
457 sc_ipc_wait(sc_args, sc_rc);
458 break;
459 default:
460 break;
461 }
462}
463
[a5c3f73]464static void event_thread_b(uintptr_t hash)
[9a1b20c]465{
[7e752b2]466 printf("New thread, hash %p\n", (void *) hash);
[9a1b20c]467 thread_trace_start(hash);
468}
469
470static int trace_loop(void *thread_hash_arg)
471{
472 int rc;
473 unsigned ev_type;
[a5c3f73]474 uintptr_t thread_hash;
[9a1b20c]475 unsigned thread_id;
[a5c3f73]476 sysarg_t val0, val1;
[9a1b20c]477
[a5c3f73]478 thread_hash = (uintptr_t)thread_hash_arg;
[9a1b20c]479 thread_id = next_thread_id++;
[c8404d4]480 if (thread_id >= THBUF_SIZE) {
481 printf("Too many threads.\n");
482 return ELIMIT;
483 }
[9a1b20c]484
[7e752b2]485 printf("Start tracing thread [%u] (hash %p).\n",
486 thread_id, (void *) thread_hash);
[9a1b20c]487
488 while (!abort_trace) {
489
[84683fdc]490 fibril_mutex_lock(&state_lock);
[741fd16]491 if (paused) {
[7e752b2]492 printf("Thread [%u] paused. Press R to resume.\n",
[654a30a]493 thread_id);
494
495 while (paused)
[84683fdc]496 fibril_condvar_wait(&state_cv, &state_lock);
[654a30a]497
[7e752b2]498 printf("Thread [%u] resumed.\n", thread_id);
[741fd16]499 }
[84683fdc]500 fibril_mutex_unlock(&state_lock);
[741fd16]501
[9a1b20c]502 /* Run thread until an event occurs */
[79ae36dd]503 rc = udebug_go(sess, thread_hash,
[9a1b20c]504 &ev_type, &val0, &val1);
505
506// printf("rc = %d, ev_type=%d\n", rc, ev_type);
507 if (ev_type == UDEBUG_EVENT_FINISHED) {
[c9a29d6]508 /* Done tracing this thread */
[9a1b20c]509 break;
510 }
511
512 if (rc >= 0) {
513 switch (ev_type) {
514 case UDEBUG_EVENT_SYSCALL_B:
515 event_syscall_b(thread_id, thread_hash, val0, (int)val1);
516 break;
517 case UDEBUG_EVENT_SYSCALL_E:
518 event_syscall_e(thread_id, thread_hash, val0, (int)val1);
519 break;
520 case UDEBUG_EVENT_STOP:
[c9a29d6]521 printf("Stop event\n");
[84683fdc]522 fibril_mutex_lock(&state_lock);
[9ad5b5cc]523 paused = true;
[84683fdc]524 fibril_mutex_unlock(&state_lock);
[9a1b20c]525 break;
526 case UDEBUG_EVENT_THREAD_B:
527 event_thread_b(val0);
528 break;
529 case UDEBUG_EVENT_THREAD_E:
[7e752b2]530 printf("Thread %" PRIun " exited.\n", val0);
[84683fdc]531 fibril_mutex_lock(&state_lock);
[9ad5b5cc]532 abort_trace = true;
[84683fdc]533 fibril_condvar_broadcast(&state_cv);
534 fibril_mutex_unlock(&state_lock);
[9a1b20c]535 break;
536 default:
[ef687799]537 printf("Unknown event type %d.\n", ev_type);
[9a1b20c]538 break;
539 }
540 }
541
542 }
543
[ef687799]544 printf("Finished tracing thread [%d].\n", thread_id);
[9a1b20c]545 return 0;
546}
547
[a5c3f73]548void thread_trace_start(uintptr_t thread_hash)
[9a1b20c]549{
550 fid_t fid;
551
552 thash = thread_hash;
553
554 fid = fibril_create(trace_loop, (void *)thread_hash);
555 if (fid == 0) {
556 printf("Warning: Failed creating fibril\n");
557 }
558 fibril_add_ready(fid);
559}
560
[a000878c]561static loader_t *preload_task(const char *path, char **argv,
[4470e26]562 task_id_t *task_id)
[9a1b20c]563{
[4470e26]564 loader_t *ldr;
[9a1b20c]565 int rc;
566
[a000878c]567 /* Spawn a program loader */
[08b777e]568 ldr = loader_connect();
[4470e26]569 if (ldr == NULL)
570 return 0;
571
572 /* Get task ID. */
573 rc = loader_get_task_id(ldr, task_id);
574 if (rc != EOK)
575 goto error;
576
577 /* Send program pathname */
578 rc = loader_set_pathname(ldr, path);
579 if (rc != EOK)
580 goto error;
581
582 /* Send arguments */
[a000878c]583 rc = loader_set_args(ldr, (const char **) argv);
[4470e26]584 if (rc != EOK)
585 goto error;
586
[8e1dc00]587 /* Send default files */
[7171760]588 int *files[4];
589 int fd_stdin;
590 int fd_stdout;
591 int fd_stderr;
[8e1dc00]592
[7171760]593 if ((stdin != NULL) && (fhandle(stdin, &fd_stdin) == EOK))
594 files[0] = &fd_stdin;
[8e1dc00]595 else
596 files[0] = NULL;
597
[7171760]598 if ((stdout != NULL) && (fhandle(stdout, &fd_stdout) == EOK))
599 files[1] = &fd_stdout;
[8e1dc00]600 else
601 files[1] = NULL;
602
[7171760]603 if ((stderr != NULL) && (fhandle(stderr, &fd_stderr) == EOK))
604 files[2] = &fd_stderr;
[8e1dc00]605 else
606 files[2] = NULL;
607
608 files[3] = NULL;
609
610 rc = loader_set_files(ldr, files);
611 if (rc != EOK)
612 goto error;
613
[4470e26]614 /* Load the program. */
615 rc = loader_load_program(ldr);
616 if (rc != EOK)
617 goto error;
618
619 /* Success */
620 return ldr;
621
622 /* Error exit */
623error:
624 loader_abort(ldr);
625 free(ldr);
626 return NULL;
627}
[9a1b20c]628
[84683fdc]629static int cev_fibril(void *arg)
630{
631 (void) arg;
[79ae36dd]632
633 console_ctrl_t *console = console_init(stdin, stdout);
634
[84683fdc]635 while (true) {
636 fibril_mutex_lock(&state_lock);
637 while (cev_valid)
638 fibril_condvar_wait(&state_cv, &state_lock);
639 fibril_mutex_unlock(&state_lock);
[79ae36dd]640
641 if (!console_get_kbd_event(console, &cev))
[84683fdc]642 return -1;
[79ae36dd]643
[84683fdc]644 fibril_mutex_lock(&state_lock);
[9ad5b5cc]645 cev_valid = true;
[84683fdc]646 fibril_condvar_broadcast(&state_cv);
[79ae36dd]647 fibril_mutex_unlock(&state_lock);
[84683fdc]648 }
649}
650
[4470e26]651static void trace_task(task_id_t task_id)
652{
[79ae36dd]653 kbd_event_t ev;
[9d8a1ed]654 bool done;
[4470e26]655 int i;
656 int rc;
[9a1b20c]657
658 ipcp_init();
[c9a29d6]659
[9a1b20c]660 rc = get_thread_list();
661 if (rc < 0) {
662 printf("Failed to get thread list (error %d)\n", rc);
663 return;
664 }
665
[9ad5b5cc]666 abort_trace = false;
[9a1b20c]667
668 for (i = 0; i < n_threads; i++) {
669 thread_trace_start(thread_hash_buf[i]);
670 }
671
[9d8a1ed]672 done = false;
673
674 while (!done) {
[84683fdc]675 fibril_mutex_lock(&state_lock);
676 while (!cev_valid && !abort_trace)
677 fibril_condvar_wait(&state_cv, &state_lock);
678 fibril_mutex_unlock(&state_lock);
679
680 ev = cev;
681
682 fibril_mutex_lock(&state_lock);
683 cev_valid = false;
684 fibril_condvar_broadcast(&state_cv);
685 fibril_mutex_unlock(&state_lock);
686
687 if (abort_trace)
688 break;
[9d8a1ed]689
690 if (ev.type != KEY_PRESS)
691 continue;
692
693 switch (ev.key) {
694 case KC_Q:
695 done = true;
696 break;
697 case KC_P:
[741fd16]698 printf("Pause...\n");
[79ae36dd]699 rc = udebug_stop(sess, thash);
[654a30a]700 if (rc != EOK)
701 printf("Error: stop -> %d\n", rc);
[9d8a1ed]702 break;
703 case KC_R:
[84683fdc]704 fibril_mutex_lock(&state_lock);
[9ad5b5cc]705 paused = false;
[84683fdc]706 fibril_condvar_broadcast(&state_cv);
707 fibril_mutex_unlock(&state_lock);
[741fd16]708 printf("Resume...\n");
[9d8a1ed]709 break;
[7c014d1]710 default:
711 break;
[9a1b20c]712 }
713 }
714
[c9a29d6]715 printf("\nTerminate debugging session...\n");
[9ad5b5cc]716 abort_trace = true;
[79ae36dd]717 udebug_end(sess);
718 async_hangup(sess);
[9a1b20c]719
720 ipcp_cleanup();
721
[c9a29d6]722 printf("Done\n");
[9a1b20c]723 return;
724}
725
726static void main_init(void)
727{
728 proto_t *p;
729 oper_t *o;
730
[abf3564]731 val_type_t arg_def[OPER_MAX_ARGS] = {
732 V_INTEGER,
733 V_INTEGER,
734 V_INTEGER,
735 V_INTEGER,
736 V_INTEGER
737 };
738
739 val_type_t resp_def[OPER_MAX_ARGS] = {
740 V_INTEGER,
741 V_INTEGER,
742 V_INTEGER,
743 V_INTEGER,
[fa09449]744 V_INTEGER
[abf3564]745 };
746
[9a1b20c]747 next_thread_id = 1;
[9ad5b5cc]748 paused = false;
749 cev_valid = false;
[84683fdc]750
751 fibril_mutex_initialize(&state_lock);
752 fibril_condvar_initialize(&state_cv);
[9a1b20c]753
754 proto_init();
755
756 p = proto_new("vfs");
[a5facfb]757 o = oper_new("open", 2, arg_def, V_INT_ERRNO, 0, resp_def);
758 proto_add_oper(p, VFS_IN_OPEN, o);
[abf3564]759 o = oper_new("read", 1, arg_def, V_ERRNO, 1, resp_def);
[4198f9c3]760 proto_add_oper(p, VFS_IN_READ, o);
[abf3564]761 o = oper_new("write", 1, arg_def, V_ERRNO, 1, resp_def);
[4198f9c3]762 proto_add_oper(p, VFS_IN_WRITE, o);
[a5facfb]763 o = oper_new("seek", 3, arg_def, V_ERRNO, 0, resp_def);
764 proto_add_oper(p, VFS_IN_SEEK, o);
[abf3564]765 o = oper_new("truncate", 5, arg_def, V_ERRNO, 0, resp_def);
[4198f9c3]766 proto_add_oper(p, VFS_IN_TRUNCATE, o);
[a5facfb]767 o = oper_new("fstat", 1, arg_def, V_ERRNO, 0, resp_def);
768 proto_add_oper(p, VFS_IN_FSTAT, o);
769 o = oper_new("close", 1, arg_def, V_ERRNO, 0, resp_def);
770 proto_add_oper(p, VFS_IN_CLOSE, o);
[abf3564]771 o = oper_new("mount", 2, arg_def, V_ERRNO, 0, resp_def);
[4198f9c3]772 proto_add_oper(p, VFS_IN_MOUNT, o);
[abf3564]773/* o = oper_new("unmount", 0, arg_def);
[4198f9c3]774 proto_add_oper(p, VFS_IN_UNMOUNT, o);*/
[a5facfb]775 o = oper_new("sync", 1, arg_def, V_ERRNO, 0, resp_def);
776 proto_add_oper(p, VFS_IN_SYNC, o);
[741a7af9]777 o = oper_new("mkdir", 1, arg_def, V_ERRNO, 0, resp_def);
[4198f9c3]778 proto_add_oper(p, VFS_IN_MKDIR, o);
[741a7af9]779 o = oper_new("unlink", 0, arg_def, V_ERRNO, 0, resp_def);
[4198f9c3]780 proto_add_oper(p, VFS_IN_UNLINK, o);
[741a7af9]781 o = oper_new("rename", 0, arg_def, V_ERRNO, 0, resp_def);
[4198f9c3]782 proto_add_oper(p, VFS_IN_RENAME, o);
[a5facfb]783 o = oper_new("stat", 0, arg_def, V_ERRNO, 0, resp_def);
784 proto_add_oper(p, VFS_IN_STAT, o);
[9a1b20c]785
786 proto_register(SERVICE_VFS, p);
787}
788
789static void print_syntax()
790{
[47e0a05b]791 printf("Syntax:\n");
792 printf("\ttrace [+<events>] <executable> [<arg1> [...]]\n");
793 printf("or\ttrace [+<events>] -t <task_id>\n");
[1643855]794 printf("Events: (default is +tp)\n");
795 printf("\n");
796 printf("\tt ... Thread creation and termination\n");
797 printf("\ts ... System calls\n");
798 printf("\ti ... Low-level IPC\n");
799 printf("\tp ... Protocol level\n");
800 printf("\n");
[47e0a05b]801 printf("Examples:\n");
802 printf("\ttrace +s /app/tetris\n");
803 printf("\ttrace +tsip -t 12\n");
[9a1b20c]804}
805
[a000878c]806static display_mask_t parse_display_mask(const char *text)
[9a1b20c]807{
[f1cc9db]808 display_mask_t dm = 0;
[a000878c]809 const char *c = text;
810
[1643855]811 while (*c) {
812 switch (*c) {
[a000878c]813 case 't':
814 dm = dm | DM_THREAD;
815 break;
816 case 's':
817 dm = dm | DM_SYSCALL;
818 break;
819 case 'i':
820 dm = dm | DM_IPC;
821 break;
822 case 'p':
823 dm = dm | DM_SYSTEM | DM_USER;
824 break;
[1643855]825 default:
[ef687799]826 printf("Unexpected event type '%c'.\n", *c);
[1643855]827 exit(1);
828 }
[a000878c]829
[1643855]830 ++c;
831 }
[a000878c]832
[1643855]833 return dm;
834}
835
836static int parse_args(int argc, char *argv[])
837{
[9a1b20c]838 char *err_p;
839
[47e0a05b]840 task_id = 0;
841
[a000878c]842 --argc;
843 ++argv;
[1643855]844
[47e0a05b]845 while (argc > 0) {
[a000878c]846 char *arg = *argv;
[1643855]847 if (arg[0] == '+') {
848 display_mask = parse_display_mask(&arg[1]);
[47e0a05b]849 } else if (arg[0] == '-') {
850 if (arg[1] == 't') {
851 /* Trace an already running task */
[a000878c]852 --argc;
853 ++argv;
[47e0a05b]854 task_id = strtol(*argv, &err_p, 10);
[4470e26]855 task_ldr = NULL;
[9ad5b5cc]856 task_wait_for = false;
[47e0a05b]857 if (*err_p) {
858 printf("Task ID syntax error\n");
859 print_syntax();
860 return -1;
861 }
862 } else {
[7e752b2]863 printf("Uknown option '%c'\n", arg[0]);
[47e0a05b]864 print_syntax();
865 return -1;
866 }
[1643855]867 } else {
[47e0a05b]868 break;
[1643855]869 }
[a000878c]870
871 --argc;
872 ++argv;
[1643855]873 }
874
[47e0a05b]875 if (task_id != 0) {
[a000878c]876 if (argc == 0)
877 return 0;
[47e0a05b]878 printf("Extra arguments\n");
[9a1b20c]879 print_syntax();
[47e0a05b]880 return -1;
[9a1b20c]881 }
882
[47e0a05b]883 if (argc < 1) {
884 printf("Missing argument\n");
[9a1b20c]885 print_syntax();
[1643855]886 return -1;
[9a1b20c]887 }
888
[4470e26]889 /* Preload the specified program file. */
[47e0a05b]890 printf("Spawning '%s' with arguments:\n", *argv);
[a000878c]891
892 char **cp = argv;
893 while (*cp)
894 printf("'%s'\n", *cp++);
895
[4470e26]896 task_ldr = preload_task(*argv, argv, &task_id);
[9ad5b5cc]897 task_wait_for = true;
[47e0a05b]898
[1643855]899 return 0;
900}
901
902int main(int argc, char *argv[])
903{
[4470e26]904 int rc;
[6e3a44a]905 task_exit_t texit;
906 int retval;
[4470e26]907
[1643855]908 printf("System Call / IPC Tracer\n");
[ef687799]909 printf("Controls: Q - Quit, P - Pause, R - Resume\n");
[1643855]910
911 display_mask = DM_THREAD | DM_SYSTEM | DM_USER;
912
913 if (parse_args(argc, argv) < 0)
914 return 1;
915
[9a1b20c]916 main_init();
[4470e26]917
918 rc = connect_task(task_id);
919 if (rc < 0) {
[7e752b2]920 printf("Failed connecting to task %" PRIu64 ".\n", task_id);
[4470e26]921 return 1;
922 }
923
[7e752b2]924 printf("Connected to task %" PRIu64 ".\n", task_id);
[4470e26]925
[6e3a44a]926 if (task_ldr != NULL)
[4470e26]927 program_run();
928
[84683fdc]929 cev_fibril_start();
[4470e26]930 trace_task(task_id);
[1643855]931
[6e3a44a]932 if (task_wait_for) {
933 printf("Waiting for task to exit.\n");
934
935 rc = task_wait(task_id, &texit, &retval);
936 if (rc != EOK) {
937 printf("Failed waiting for task.\n");
938 return -1;
939 }
940
941 if (texit == TASK_EXIT_NORMAL) {
942 printf("Task exited normally, return value %d.\n",
943 retval);
944 } else {
945 printf("Task exited unexpectedly.\n");
946 }
947 }
948
[1643855]949 return 0;
[9a1b20c]950}
951
952/** @}
953 */
Note: See TracBrowser for help on using the repository browser.