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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since edba2b6f was 19f857a, checked in by Jiri Svoboda <jiri@…>, 16 years ago

Rename string.h to str.h to avoid header conflict with standard C string.h.

  • Property mode set to 100644
File size: 21.5 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 <ipc/ipc.h>
39#include <fibril.h>
40#include <errno.h>
41#include <udebug.h>
42#include <async.h>
[47e0a05b]43#include <task.h>
[3bf907a]44#include <mem.h>
[19f857a]45#include <str.h>
[9ad5b5cc]46#include <bool.h>
[4470e26]47#include <loader/loader.h>
[9d8a1ed]48#include <io/console.h>
49#include <io/keycode.h>
[1e4cada]50#include <fibril_synch.h>
[1ccafee]51#include <sys/types.h>
52#include <sys/typefmt.h>
[9a1b20c]53
[f2ef7fd]54#include <libc.h>
55
[9a1b20c]56// Temporary: service and method names
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
[9a1b20c]75int phoneid;
[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;
84static console_event_t cev;
[9a1b20c]85
[a5c3f73]86void thread_trace_start(uintptr_t thread_hash);
[9a1b20c]87
88static proto_t *proto_console;
[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 free(task_ldr);
140 task_ldr = NULL;
141
142 printf("program_run_fibril exiting\n");
143 return 0;
144}
145
146
147static int connect_task(task_id_t task_id)
[9a1b20c]148{
149 int rc;
150
151 rc = ipc_connect_kbox(task_id);
[fb9b0b0]152
153 if (rc == ENOTSUP) {
154 printf("You do not have userspace debugging support "
155 "compiled in the kernel.\n");
156 printf("Compile kernel with 'Support for userspace debuggers' "
157 "(CONFIG_UDEBUG) enabled.\n");
[c9a29d6]158 return rc;
159 }
160
161 if (rc < 0) {
162 printf("Error connecting\n");
[1ccafee]163 printf("ipc_connect_task(%" PRIdTASKID ") -> %d ", task_id, rc);
[c9a29d6]164 return rc;
[fb9b0b0]165 }
166
[9a1b20c]167 phoneid = rc;
168
169 rc = udebug_begin(phoneid);
[c9a29d6]170 if (rc < 0) {
171 printf("udebug_begin() -> %d\n", rc);
172 return rc;
173 }
[9a1b20c]174
175 rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL);
[c9a29d6]176 if (rc < 0) {
177 printf("udebug_set_evmask(0x%x) -> %d\n ", UDEBUG_EM_ALL, rc);
178 return rc;
179 }
[9a1b20c]180
181 return 0;
182}
183
184static int get_thread_list(void)
185{
186 int rc;
187 size_t tb_copied;
188 size_t tb_needed;
189 int i;
190
191 rc = udebug_thread_read(phoneid, thread_hash_buf,
192 THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
[c9a29d6]193 if (rc < 0) {
194 printf("udebug_thread_read() -> %d\n", rc);
195 return rc;
196 }
[9a1b20c]197
[a5c3f73]198 n_threads = tb_copied / sizeof(uintptr_t);
[9a1b20c]199
[c9a29d6]200 printf("Threads:");
201 for (i = 0; i < n_threads; i++) {
[fb6f1a5]202 printf(" [%d] (hash %p)", 1+i, thread_hash_buf[i]);
[9a1b20c]203 }
[a5c3f73]204 printf("\ntotal of %u threads\n", tb_needed / sizeof(uintptr_t));
[9a1b20c]205
206 return 0;
207}
208
[356acd0]209void val_print(sysarg_t val, val_type_t v_type)
[9a1b20c]210{
[f019cc07]211 long sval;
212
213 sval = (long) val;
214
[abf3564]215 switch (v_type) {
216 case V_VOID:
217 printf("<void>");
218 break;
219
220 case V_INTEGER:
[f019cc07]221 printf("%ld", sval);
[abf3564]222 break;
223
224 case V_HASH:
[4470e26]225 case V_PTR:
[fb6f1a5]226 printf("%p", val);
[abf3564]227 break;
228
229 case V_ERRNO:
[f019cc07]230 if (sval >= -15 && sval <= 0) {
231 printf("%ld %s (%s)", sval,
232 err_desc[-sval].name,
233 err_desc[-sval].desc);
[9a1b20c]234 } else {
[f019cc07]235 printf("%ld", sval);
[9a1b20c]236 }
[abf3564]237 break;
238 case V_INT_ERRNO:
[f019cc07]239 if (sval >= -15 && sval < 0) {
240 printf("%ld %s (%s)", sval,
241 err_desc[-sval].name,
242 err_desc[-sval].desc);
[abf3564]243 } else {
[f019cc07]244 printf("%ld", sval);
[abf3564]245 }
246 break;
247
248 case V_CHAR:
[f019cc07]249 if (sval >= 0x20 && sval < 0x7f) {
250 printf("'%c'", sval);
[9a1b20c]251 } else {
[f019cc07]252 switch (sval) {
[abf3564]253 case '\a': printf("'\\a'"); break;
254 case '\b': printf("'\\b'"); break;
255 case '\n': printf("'\\n'"); break;
256 case '\r': printf("'\\r'"); break;
257 case '\t': printf("'\\t'"); break;
258 case '\\': printf("'\\\\'"); break;
[356acd0]259 default: printf("'\\x%02lX'", val); break;
[abf3564]260 }
[9a1b20c]261 }
[abf3564]262 break;
[9a1b20c]263 }
[abf3564]264}
265
266
[356acd0]267static void print_sc_retval(sysarg_t retval, val_type_t val_type)
[abf3564]268{
269 printf(" -> ");
270 val_print(retval, val_type);
[9a1b20c]271 putchar('\n');
272}
273
[a5c3f73]274static void print_sc_args(sysarg_t *sc_args, int n)
[9a1b20c]275{
276 int i;
277
278 putchar('(');
[1ccafee]279 if (n > 0) printf("%" PRIdSYSARG, sc_args[0]);
[abf3564]280 for (i = 1; i < n; i++) {
[1ccafee]281 printf(", %" PRIdSYSARG, sc_args[i]);
[9a1b20c]282 }
283 putchar(')');
284}
285
[a5c3f73]286static void sc_ipc_call_async_fast(sysarg_t *sc_args, sysarg_t sc_rc)
[9a1b20c]287{
288 ipc_call_t call;
[a5c3f73]289 ipcarg_t phoneid;
[9a1b20c]290
[f019cc07]291 if (sc_rc == (sysarg_t) IPC_CALLRET_FATAL ||
292 sc_rc == (sysarg_t) IPC_CALLRET_TEMPORARY)
[9a1b20c]293 return;
294
295 phoneid = sc_args[0];
296
297 IPC_SET_METHOD(call, sc_args[1]);
298 IPC_SET_ARG1(call, sc_args[2]);
299 IPC_SET_ARG2(call, sc_args[3]);
300 IPC_SET_ARG3(call, sc_args[4]);
301 IPC_SET_ARG4(call, sc_args[5]);
302 IPC_SET_ARG5(call, 0);
303
304 ipcp_call_out(phoneid, &call, sc_rc);
305}
306
[a5c3f73]307static void sc_ipc_call_async_slow(sysarg_t *sc_args, sysarg_t sc_rc)
[9a1b20c]308{
309 ipc_call_t call;
310 int rc;
311
[f019cc07]312 if (sc_rc == (sysarg_t) IPC_CALLRET_FATAL ||
313 sc_rc == (sysarg_t) IPC_CALLRET_TEMPORARY)
[9a1b20c]314 return;
315
316 memset(&call, 0, sizeof(call));
317 rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
318
319 if (rc >= 0) {
320 ipcp_call_out(sc_args[0], &call, sc_rc);
321 }
322}
323
[a5c3f73]324static void sc_ipc_call_sync_fast(sysarg_t *sc_args)
[9a1b20c]325{
326 ipc_call_t question, reply;
327 int rc;
328 int phoneidx;
329
330// printf("sc_ipc_call_sync_fast()\n");
331 phoneidx = sc_args[0];
332
333 IPC_SET_METHOD(question, sc_args[1]);
334 IPC_SET_ARG1(question, sc_args[2]);
335 IPC_SET_ARG2(question, sc_args[3]);
336 IPC_SET_ARG3(question, sc_args[4]);
337 IPC_SET_ARG4(question, 0);
338 IPC_SET_ARG5(question, 0);
339
340// printf("memset\n");
341 memset(&reply, 0, sizeof(reply));
342// printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
343// phoneid, &reply.args, sc_args[5], sizeof(reply.args));
344 rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
345// printf("dmr->%d\n", rc);
346 if (rc < 0) return;
347
348// printf("call ipc_call_sync\n");
349 ipcp_call_sync(phoneidx, &question, &reply);
350}
351
[c8404d4]352static void sc_ipc_call_sync_slow_b(unsigned thread_id, sysarg_t *sc_args)
[9a1b20c]353{
[f019cc07]354 ipc_call_t question;
[9a1b20c]355 int rc;
356
357 memset(&question, 0, sizeof(question));
[f019cc07]358 rc = udebug_mem_read(phoneid, &question.args, sc_args[1],
359 sizeof(question.args));
360
[c8404d4]361 if (rc < 0) {
362 printf("Error: mem_read->%d\n", rc);
363 return;
364 }
365
366 thread_ipc_req[thread_id] = question;
367}
368
369static void sc_ipc_call_sync_slow_e(unsigned thread_id, sysarg_t *sc_args)
370{
[f019cc07]371 ipc_call_t reply;
[c8404d4]372 int rc;
[9a1b20c]373
374 memset(&reply, 0, sizeof(reply));
[f019cc07]375 rc = udebug_mem_read(phoneid, &reply.args, sc_args[2],
376 sizeof(reply.args));
377
[c8404d4]378 if (rc < 0) {
379 printf("Error: mem_read->%d\n", rc);
380 return;
381 }
[9a1b20c]382
[c8404d4]383 ipcp_call_sync(sc_args[0], &thread_ipc_req[thread_id], &reply);
[9a1b20c]384}
385
[a5c3f73]386static void sc_ipc_wait(sysarg_t *sc_args, int sc_rc)
[9a1b20c]387{
388 ipc_call_t call;
389 int rc;
390
391 if (sc_rc == 0) return;
392
393 memset(&call, 0, sizeof(call));
394 rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
395// printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
396// phoneid, (int)&call, sc_args[0], sizeof(call), rc);
397
398 if (rc >= 0) {
399 ipcp_call_in(&call, sc_rc);
400 }
401}
402
[a5c3f73]403static void event_syscall_b(unsigned thread_id, uintptr_t thread_hash,
404 unsigned sc_id, sysarg_t sc_rc)
[9a1b20c]405{
[a5c3f73]406 sysarg_t sc_args[6];
[9a1b20c]407 int rc;
408
409 /* Read syscall arguments */
410 rc = udebug_args_read(phoneid, thread_hash, sc_args);
411
412 async_serialize_start();
413
414// printf("[%d] ", thread_id);
415
416 if (rc < 0) {
417 printf("error\n");
418 async_serialize_end();
419 return;
420 }
421
[1643855]422 if ((display_mask & DM_SYSCALL) != 0) {
423 /* Print syscall name and arguments */
424 printf("%s", syscall_desc[sc_id].name);
425 print_sc_args(sc_args, syscall_desc[sc_id].n_args);
426 }
[9a1b20c]427
[c8404d4]428 switch (sc_id) {
429 case SYS_IPC_CALL_SYNC_SLOW:
430 sc_ipc_call_sync_slow_b(thread_id, sc_args);
431 break;
432 default:
433 break;
434 }
435
[9a1b20c]436 async_serialize_end();
437}
438
[a5c3f73]439static void event_syscall_e(unsigned thread_id, uintptr_t thread_hash,
440 unsigned sc_id, sysarg_t sc_rc)
[9a1b20c]441{
[a5c3f73]442 sysarg_t sc_args[6];
[9a1b20c]443 int rv_type;
444 int rc;
445
446 /* Read syscall arguments */
447 rc = udebug_args_read(phoneid, thread_hash, sc_args);
448
449 async_serialize_start();
450
451// printf("[%d] ", thread_id);
452
453 if (rc < 0) {
454 printf("error\n");
455 async_serialize_end();
456 return;
457 }
458
[1643855]459 if ((display_mask & DM_SYSCALL) != 0) {
460 /* Print syscall return value */
461 rv_type = syscall_desc[sc_id].rv_type;
462 print_sc_retval(sc_rc, rv_type);
463 }
[9a1b20c]464
465 switch (sc_id) {
466 case SYS_IPC_CALL_ASYNC_FAST:
467 sc_ipc_call_async_fast(sc_args, sc_rc);
468 break;
469 case SYS_IPC_CALL_ASYNC_SLOW:
470 sc_ipc_call_async_slow(sc_args, sc_rc);
471 break;
472 case SYS_IPC_CALL_SYNC_FAST:
473 sc_ipc_call_sync_fast(sc_args);
474 break;
475 case SYS_IPC_CALL_SYNC_SLOW:
[c8404d4]476 sc_ipc_call_sync_slow_e(thread_id, sc_args);
[9a1b20c]477 break;
478 case SYS_IPC_WAIT:
479 sc_ipc_wait(sc_args, sc_rc);
480 break;
481 default:
482 break;
483 }
484
485 async_serialize_end();
486}
487
[a5c3f73]488static void event_thread_b(uintptr_t hash)
[9a1b20c]489{
490 async_serialize_start();
[a5c3f73]491 printf("New thread, hash 0x%lx\n", hash);
[9a1b20c]492 async_serialize_end();
493
494 thread_trace_start(hash);
495}
496
497static int trace_loop(void *thread_hash_arg)
498{
499 int rc;
500 unsigned ev_type;
[a5c3f73]501 uintptr_t thread_hash;
[9a1b20c]502 unsigned thread_id;
[a5c3f73]503 sysarg_t val0, val1;
[9a1b20c]504
[a5c3f73]505 thread_hash = (uintptr_t)thread_hash_arg;
[9a1b20c]506 thread_id = next_thread_id++;
[c8404d4]507 if (thread_id >= THBUF_SIZE) {
508 printf("Too many threads.\n");
509 return ELIMIT;
510 }
[9a1b20c]511
[fb6f1a5]512 printf("Start tracing thread [%d] (hash %p).\n", thread_id, thread_hash);
[9a1b20c]513
514 while (!abort_trace) {
515
[84683fdc]516 fibril_mutex_lock(&state_lock);
[741fd16]517 if (paused) {
[654a30a]518 printf("Thread [%d] paused. Press R to resume.\n",
519 thread_id);
520
521 while (paused)
[84683fdc]522 fibril_condvar_wait(&state_cv, &state_lock);
[654a30a]523
524 printf("Thread [%d] resumed.\n", thread_id);
[741fd16]525 }
[84683fdc]526 fibril_mutex_unlock(&state_lock);
[741fd16]527
[9a1b20c]528 /* Run thread until an event occurs */
529 rc = udebug_go(phoneid, thread_hash,
530 &ev_type, &val0, &val1);
531
532// printf("rc = %d, ev_type=%d\n", rc, ev_type);
533 if (ev_type == UDEBUG_EVENT_FINISHED) {
[c9a29d6]534 /* Done tracing this thread */
[9a1b20c]535 break;
536 }
537
538 if (rc >= 0) {
539 switch (ev_type) {
540 case UDEBUG_EVENT_SYSCALL_B:
541 event_syscall_b(thread_id, thread_hash, val0, (int)val1);
542 break;
543 case UDEBUG_EVENT_SYSCALL_E:
544 event_syscall_e(thread_id, thread_hash, val0, (int)val1);
545 break;
546 case UDEBUG_EVENT_STOP:
[c9a29d6]547 printf("Stop event\n");
[84683fdc]548 fibril_mutex_lock(&state_lock);
[9ad5b5cc]549 paused = true;
[84683fdc]550 fibril_mutex_unlock(&state_lock);
[9a1b20c]551 break;
552 case UDEBUG_EVENT_THREAD_B:
553 event_thread_b(val0);
554 break;
555 case UDEBUG_EVENT_THREAD_E:
[fb6f1a5]556 printf("Thread %p exited.\n", val0);
[84683fdc]557 fibril_mutex_lock(&state_lock);
[9ad5b5cc]558 abort_trace = true;
[84683fdc]559 fibril_condvar_broadcast(&state_cv);
560 fibril_mutex_unlock(&state_lock);
[9a1b20c]561 break;
562 default:
[ef687799]563 printf("Unknown event type %d.\n", ev_type);
[9a1b20c]564 break;
565 }
566 }
567
568 }
569
[ef687799]570 printf("Finished tracing thread [%d].\n", thread_id);
[9a1b20c]571 return 0;
572}
573
[a5c3f73]574void thread_trace_start(uintptr_t thread_hash)
[9a1b20c]575{
576 fid_t fid;
577
578 thash = thread_hash;
579
580 fid = fibril_create(trace_loop, (void *)thread_hash);
581 if (fid == 0) {
582 printf("Warning: Failed creating fibril\n");
583 }
584 fibril_add_ready(fid);
585}
586
[a000878c]587static loader_t *preload_task(const char *path, char **argv,
[4470e26]588 task_id_t *task_id)
[9a1b20c]589{
[4470e26]590 loader_t *ldr;
[9a1b20c]591 int rc;
592
[a000878c]593 /* Spawn a program loader */
[08b777e]594 ldr = loader_connect();
[4470e26]595 if (ldr == NULL)
596 return 0;
597
598 /* Get task ID. */
599 rc = loader_get_task_id(ldr, task_id);
600 if (rc != EOK)
601 goto error;
602
603 /* Send program pathname */
604 rc = loader_set_pathname(ldr, path);
605 if (rc != EOK)
606 goto error;
607
608 /* Send arguments */
[a000878c]609 rc = loader_set_args(ldr, (const char **) argv);
[4470e26]610 if (rc != EOK)
611 goto error;
612
[8e1dc00]613 /* Send default files */
614 fdi_node_t *files[4];
615 fdi_node_t stdin_node;
616 fdi_node_t stdout_node;
617 fdi_node_t stderr_node;
618
619 if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
620 files[0] = &stdin_node;
621 else
622 files[0] = NULL;
623
624 if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
625 files[1] = &stdout_node;
626 else
627 files[1] = NULL;
628
629 if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
630 files[2] = &stderr_node;
631 else
632 files[2] = NULL;
633
634 files[3] = NULL;
635
636 rc = loader_set_files(ldr, files);
637 if (rc != EOK)
638 goto error;
639
[4470e26]640 /* Load the program. */
641 rc = loader_load_program(ldr);
642 if (rc != EOK)
643 goto error;
644
645 /* Success */
646 return ldr;
647
648 /* Error exit */
649error:
650 loader_abort(ldr);
651 free(ldr);
652 return NULL;
653}
[9a1b20c]654
[84683fdc]655static int cev_fibril(void *arg)
656{
657 (void) arg;
658
659 while (true) {
660 fibril_mutex_lock(&state_lock);
661 while (cev_valid)
662 fibril_condvar_wait(&state_cv, &state_lock);
663 fibril_mutex_unlock(&state_lock);
664
665 if (!console_get_event(fphone(stdin), &cev))
666 return -1;
667
668 fibril_mutex_lock(&state_lock);
[9ad5b5cc]669 cev_valid = true;
[84683fdc]670 fibril_condvar_broadcast(&state_cv);
671 fibril_mutex_unlock(&state_lock);
672 }
673}
674
[4470e26]675static void trace_task(task_id_t task_id)
676{
[9d8a1ed]677 console_event_t ev;
678 bool done;
[4470e26]679 int i;
680 int rc;
[9a1b20c]681
682 ipcp_init();
[c9a29d6]683
684 /*
685 * User apps now typically have console on phone 3.
686 * (Phones 1 and 2 are used by the loader).
687 */
688 ipcp_connection_set(3, 0, proto_console);
[9a1b20c]689
690 rc = get_thread_list();
691 if (rc < 0) {
692 printf("Failed to get thread list (error %d)\n", rc);
693 return;
694 }
695
[9ad5b5cc]696 abort_trace = false;
[9a1b20c]697
698 for (i = 0; i < n_threads; i++) {
699 thread_trace_start(thread_hash_buf[i]);
700 }
701
[9d8a1ed]702 done = false;
703
704 while (!done) {
[84683fdc]705 fibril_mutex_lock(&state_lock);
706 while (!cev_valid && !abort_trace)
707 fibril_condvar_wait(&state_cv, &state_lock);
708 fibril_mutex_unlock(&state_lock);
709
710 ev = cev;
711
712 fibril_mutex_lock(&state_lock);
713 cev_valid = false;
714 fibril_condvar_broadcast(&state_cv);
715 fibril_mutex_unlock(&state_lock);
716
717 if (abort_trace)
718 break;
[9d8a1ed]719
720 if (ev.type != KEY_PRESS)
721 continue;
722
723 switch (ev.key) {
724 case KC_Q:
725 done = true;
726 break;
727 case KC_P:
[741fd16]728 printf("Pause...\n");
[9a1b20c]729 rc = udebug_stop(phoneid, thash);
[654a30a]730 if (rc != EOK)
731 printf("Error: stop -> %d\n", rc);
[9d8a1ed]732 break;
733 case KC_R:
[84683fdc]734 fibril_mutex_lock(&state_lock);
[9ad5b5cc]735 paused = false;
[84683fdc]736 fibril_condvar_broadcast(&state_cv);
737 fibril_mutex_unlock(&state_lock);
[741fd16]738 printf("Resume...\n");
[9d8a1ed]739 break;
[9a1b20c]740 }
741 }
742
[c9a29d6]743 printf("\nTerminate debugging session...\n");
[9ad5b5cc]744 abort_trace = true;
[9a1b20c]745 udebug_end(phoneid);
746 ipc_hangup(phoneid);
747
748 ipcp_cleanup();
749
[c9a29d6]750 printf("Done\n");
[9a1b20c]751 return;
752}
753
754static void main_init(void)
755{
756 proto_t *p;
757 oper_t *o;
758
[abf3564]759 val_type_t arg_def[OPER_MAX_ARGS] = {
760 V_INTEGER,
761 V_INTEGER,
762 V_INTEGER,
763 V_INTEGER,
764 V_INTEGER
765 };
766
767 val_type_t resp_def[OPER_MAX_ARGS] = {
768 V_INTEGER,
769 V_INTEGER,
770 V_INTEGER,
771 V_INTEGER,
[fa09449]772 V_INTEGER
[abf3564]773 };
774
[9a1b20c]775 next_thread_id = 1;
[9ad5b5cc]776 paused = false;
777 cev_valid = false;
[84683fdc]778
779 fibril_mutex_initialize(&state_lock);
780 fibril_condvar_initialize(&state_cv);
[9a1b20c]781
782 proto_init();
783
784 p = proto_new("vfs");
[a5facfb]785 o = oper_new("open", 2, arg_def, V_INT_ERRNO, 0, resp_def);
786 proto_add_oper(p, VFS_IN_OPEN, o);
787 o = oper_new("open_node", 4, arg_def, V_INT_ERRNO, 0, resp_def);
788 proto_add_oper(p, VFS_IN_OPEN_NODE, o);
[abf3564]789 o = oper_new("read", 1, arg_def, V_ERRNO, 1, resp_def);
[4198f9c3]790 proto_add_oper(p, VFS_IN_READ, o);
[abf3564]791 o = oper_new("write", 1, arg_def, V_ERRNO, 1, resp_def);
[4198f9c3]792 proto_add_oper(p, VFS_IN_WRITE, o);
[a5facfb]793 o = oper_new("seek", 3, arg_def, V_ERRNO, 0, resp_def);
794 proto_add_oper(p, VFS_IN_SEEK, o);
[abf3564]795 o = oper_new("truncate", 5, arg_def, V_ERRNO, 0, resp_def);
[4198f9c3]796 proto_add_oper(p, VFS_IN_TRUNCATE, o);
[a5facfb]797 o = oper_new("fstat", 1, arg_def, V_ERRNO, 0, resp_def);
798 proto_add_oper(p, VFS_IN_FSTAT, o);
799 o = oper_new("close", 1, arg_def, V_ERRNO, 0, resp_def);
800 proto_add_oper(p, VFS_IN_CLOSE, o);
[abf3564]801 o = oper_new("mount", 2, arg_def, V_ERRNO, 0, resp_def);
[4198f9c3]802 proto_add_oper(p, VFS_IN_MOUNT, o);
[abf3564]803/* o = oper_new("unmount", 0, arg_def);
[4198f9c3]804 proto_add_oper(p, VFS_IN_UNMOUNT, o);*/
[a5facfb]805 o = oper_new("sync", 1, arg_def, V_ERRNO, 0, resp_def);
806 proto_add_oper(p, VFS_IN_SYNC, o);
[741a7af9]807 o = oper_new("mkdir", 1, arg_def, V_ERRNO, 0, resp_def);
[4198f9c3]808 proto_add_oper(p, VFS_IN_MKDIR, o);
[741a7af9]809 o = oper_new("unlink", 0, arg_def, V_ERRNO, 0, resp_def);
[4198f9c3]810 proto_add_oper(p, VFS_IN_UNLINK, o);
[741a7af9]811 o = oper_new("rename", 0, arg_def, V_ERRNO, 0, resp_def);
[4198f9c3]812 proto_add_oper(p, VFS_IN_RENAME, o);
[a5facfb]813 o = oper_new("stat", 0, arg_def, V_ERRNO, 0, resp_def);
814 proto_add_oper(p, VFS_IN_STAT, o);
[9a1b20c]815
816 proto_register(SERVICE_VFS, p);
817
818 p = proto_new("console");
[a5facfb]819
820 o = oper_new("write", 1, arg_def, V_ERRNO, 1, resp_def);
821 proto_add_oper(p, VFS_IN_WRITE, o);
822
[fa09449]823 resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER;
824 resp_def[2] = V_INTEGER; resp_def[3] = V_CHAR;
825 o = oper_new("getkey", 0, arg_def, V_ERRNO, 4, resp_def);
[abf3564]826
827 arg_def[0] = V_CHAR;
828 o = oper_new("clear", 0, arg_def, V_VOID, 0, resp_def);
[9a1b20c]829 proto_add_oper(p, CONSOLE_CLEAR, o);
[abf3564]830
831 arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER;
832 o = oper_new("goto", 2, arg_def, V_VOID, 0, resp_def);
[9a1b20c]833 proto_add_oper(p, CONSOLE_GOTO, o);
[abf3564]834
835 resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER;
836 o = oper_new("getsize", 0, arg_def, V_INTEGER, 2, resp_def);
[69e68e3]837 proto_add_oper(p, CONSOLE_GET_SIZE, o);
[abf3564]838
[9805cde]839 arg_def[0] = V_INTEGER;
[741a7af9]840 o = oper_new("set_style", 1, arg_def, V_VOID, 0, resp_def);
[9a1b20c]841 proto_add_oper(p, CONSOLE_SET_STYLE, o);
[9805cde]842 arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER; arg_def[2] = V_INTEGER;
[741a7af9]843 o = oper_new("set_color", 3, arg_def, V_VOID, 0, resp_def);
[9805cde]844 proto_add_oper(p, CONSOLE_SET_COLOR, o);
845 arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER;
[741a7af9]846 o = oper_new("set_rgb_color", 2, arg_def, V_VOID, 0, resp_def);
[9805cde]847 proto_add_oper(p, CONSOLE_SET_RGB_COLOR, o);
[abf3564]848 o = oper_new("cursor_visibility", 1, arg_def, V_VOID, 0, resp_def);
[9a1b20c]849 proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
850
851 proto_console = p;
852 proto_register(SERVICE_CONSOLE, p);
853}
854
855static void print_syntax()
856{
[47e0a05b]857 printf("Syntax:\n");
858 printf("\ttrace [+<events>] <executable> [<arg1> [...]]\n");
859 printf("or\ttrace [+<events>] -t <task_id>\n");
[1643855]860 printf("Events: (default is +tp)\n");
861 printf("\n");
862 printf("\tt ... Thread creation and termination\n");
863 printf("\ts ... System calls\n");
864 printf("\ti ... Low-level IPC\n");
865 printf("\tp ... Protocol level\n");
866 printf("\n");
[47e0a05b]867 printf("Examples:\n");
868 printf("\ttrace +s /app/tetris\n");
869 printf("\ttrace +tsip -t 12\n");
[9a1b20c]870}
871
[a000878c]872static display_mask_t parse_display_mask(const char *text)
[9a1b20c]873{
[1643855]874 display_mask_t dm;
[a000878c]875 const char *c = text;
876
[1643855]877 while (*c) {
878 switch (*c) {
[a000878c]879 case 't':
880 dm = dm | DM_THREAD;
881 break;
882 case 's':
883 dm = dm | DM_SYSCALL;
884 break;
885 case 'i':
886 dm = dm | DM_IPC;
887 break;
888 case 'p':
889 dm = dm | DM_SYSTEM | DM_USER;
890 break;
[1643855]891 default:
[ef687799]892 printf("Unexpected event type '%c'.\n", *c);
[1643855]893 exit(1);
894 }
[a000878c]895
[1643855]896 ++c;
897 }
[a000878c]898
[1643855]899 return dm;
900}
901
902static int parse_args(int argc, char *argv[])
903{
[9a1b20c]904 char *err_p;
905
[47e0a05b]906 task_id = 0;
907
[a000878c]908 --argc;
909 ++argv;
[1643855]910
[47e0a05b]911 while (argc > 0) {
[a000878c]912 char *arg = *argv;
[1643855]913 if (arg[0] == '+') {
914 display_mask = parse_display_mask(&arg[1]);
[47e0a05b]915 } else if (arg[0] == '-') {
916 if (arg[1] == 't') {
917 /* Trace an already running task */
[a000878c]918 --argc;
919 ++argv;
[47e0a05b]920 task_id = strtol(*argv, &err_p, 10);
[4470e26]921 task_ldr = NULL;
[9ad5b5cc]922 task_wait_for = false;
[47e0a05b]923 if (*err_p) {
924 printf("Task ID syntax error\n");
925 print_syntax();
926 return -1;
927 }
928 } else {
929 printf("Uknown option '%s'\n", arg[0]);
930 print_syntax();
931 return -1;
932 }
[1643855]933 } else {
[47e0a05b]934 break;
[1643855]935 }
[a000878c]936
937 --argc;
938 ++argv;
[1643855]939 }
940
[47e0a05b]941 if (task_id != 0) {
[a000878c]942 if (argc == 0)
943 return 0;
[47e0a05b]944 printf("Extra arguments\n");
[9a1b20c]945 print_syntax();
[47e0a05b]946 return -1;
[9a1b20c]947 }
948
[47e0a05b]949 if (argc < 1) {
950 printf("Missing argument\n");
[9a1b20c]951 print_syntax();
[1643855]952 return -1;
[9a1b20c]953 }
954
[4470e26]955 /* Preload the specified program file. */
[47e0a05b]956 printf("Spawning '%s' with arguments:\n", *argv);
[a000878c]957
958 char **cp = argv;
959 while (*cp)
960 printf("'%s'\n", *cp++);
961
[4470e26]962 task_ldr = preload_task(*argv, argv, &task_id);
[9ad5b5cc]963 task_wait_for = true;
[47e0a05b]964
[1643855]965 return 0;
966}
967
968int main(int argc, char *argv[])
969{
[4470e26]970 int rc;
[6e3a44a]971 task_exit_t texit;
972 int retval;
[4470e26]973
[1643855]974 printf("System Call / IPC Tracer\n");
[ef687799]975 printf("Controls: Q - Quit, P - Pause, R - Resume\n");
[1643855]976
977 display_mask = DM_THREAD | DM_SYSTEM | DM_USER;
978
979 if (parse_args(argc, argv) < 0)
980 return 1;
981
[9a1b20c]982 main_init();
[4470e26]983
984 rc = connect_task(task_id);
985 if (rc < 0) {
[1ccafee]986 printf("Failed connecting to task %" PRIdTASKID ".\n", task_id);
[4470e26]987 return 1;
988 }
989
[1ccafee]990 printf("Connected to task %" PRIdTASKID ".\n", task_id);
[4470e26]991
[6e3a44a]992 if (task_ldr != NULL)
[4470e26]993 program_run();
994
[84683fdc]995 cev_fibril_start();
[4470e26]996 trace_task(task_id);
[1643855]997
[6e3a44a]998 if (task_wait_for) {
999 printf("Waiting for task to exit.\n");
1000
1001 rc = task_wait(task_id, &texit, &retval);
1002 if (rc != EOK) {
1003 printf("Failed waiting for task.\n");
1004 return -1;
1005 }
1006
1007 if (texit == TASK_EXIT_NORMAL) {
1008 printf("Task exited normally, return value %d.\n",
1009 retval);
1010 } else {
1011 printf("Task exited unexpectedly.\n");
1012 }
1013 }
1014
[1643855]1015 return 0;
[9a1b20c]1016}
1017
1018/** @}
1019 */
Note: See TracBrowser for help on using the repository browser.