Subversion Repositories HelenOS

Rev

Rev 4653 | Rev 4655 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3438 svoboda 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>
3447 svoboda 43
#include <task.h>
4496 decky 44
#include <mem.h>
45
#include <string.h>
3470 svoboda 46
#include <loader/loader.h>
4653 svoboda 47
#include <io/console.h>
48
#include <io/keycode.h>
4654 svoboda 49
#include <fibril_sync.h>
3438 svoboda 50
 
3485 jermar 51
#include <libc.h>
52
 
3438 svoboda 53
// Temporary: service and method names
54
#include "proto.h"
55
#include <ipc/services.h>
56
#include "../../srv/vfs/vfs.h"
3747 svoboda 57
#include <ipc/console.h>
3438 svoboda 58
 
59
#include "syscalls.h"
60
#include "ipcp.h"
61
#include "errors.h"
3444 svoboda 62
#include "trace.h"
3438 svoboda 63
 
64
#define THBUF_SIZE 64
3454 svoboda 65
uintptr_t thread_hash_buf[THBUF_SIZE];
66
int n_threads;
3438 svoboda 67
 
68
int next_thread_id;
69
 
70
int phoneid;
71
int abort_trace;
72
 
3454 svoboda 73
uintptr_t thash;
3438 svoboda 74
volatile int paused;
4654 svoboda 75
fibril_condvar_t paused_cv;
76
fibril_mutex_t paused_lock;
3438 svoboda 77
 
3454 svoboda 78
void thread_trace_start(uintptr_t thread_hash);
3438 svoboda 79
 
80
static proto_t *proto_console;
3444 svoboda 81
static task_id_t task_id;
3470 svoboda 82
static loader_t *task_ldr;
3438 svoboda 83
 
3444 svoboda 84
/** Combination of events/data to print. */
85
display_mask_t display_mask;
86
 
3470 svoboda 87
static int program_run_fibril(void *arg);
88
 
89
static void program_run(void)
3438 svoboda 90
{
3470 svoboda 91
    fid_t fid;
92
 
93
    fid = fibril_create(program_run_fibril, NULL);
94
    if (fid == 0) {
95
        printf("Error creating fibril\n");
96
        exit(1);
97
    }
98
 
99
    fibril_add_ready(fid);
100
}
101
 
102
static int program_run_fibril(void *arg)
103
{
3438 svoboda 104
    int rc;
105
 
3470 svoboda 106
    /*
107
     * This must be done in background as it will block until
108
     * we let the task reply to this call.
109
     */
110
    rc = loader_run(task_ldr);
111
    if (rc != 0) {
112
        printf("Error running program\n");
113
        exit(1);
114
    }
115
 
116
    free(task_ldr);
117
    task_ldr = NULL;
118
 
119
    printf("program_run_fibril exiting\n");
120
    return 0;
121
}
122
 
123
 
124
static int connect_task(task_id_t task_id)
125
{
126
    int rc;
127
 
3438 svoboda 128
    rc = ipc_connect_kbox(task_id);
3439 svoboda 129
 
130
    if (rc == ENOTSUP) {
131
        printf("You do not have userspace debugging support "
132
            "compiled in the kernel.\n");
133
        printf("Compile kernel with 'Support for userspace debuggers' "
134
            "(CONFIG_UDEBUG) enabled.\n");
3442 svoboda 135
        return rc;
3439 svoboda 136
    }
137
 
3442 svoboda 138
    if (rc < 0) {
139
        printf("Error connecting\n");
140
        printf("ipc_connect_task(%lld) -> %d ", task_id, rc);
141
        return rc;
142
    }
143
 
3438 svoboda 144
    phoneid = rc;
145
 
146
    rc = udebug_begin(phoneid);
3442 svoboda 147
    if (rc < 0) {
148
        printf("udebug_begin() -> %d\n", rc);
149
        return rc;
150
    }
3438 svoboda 151
 
152
    rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL);
3442 svoboda 153
    if (rc < 0) {
154
        printf("udebug_set_evmask(0x%x) -> %d\n ", UDEBUG_EM_ALL, rc);
155
        return rc;
156
    }
3438 svoboda 157
 
158
    return 0;
159
}
160
 
161
static int get_thread_list(void)
162
{
163
    int rc;
164
    size_t tb_copied;
165
    size_t tb_needed;
166
    int i;
167
 
168
    rc = udebug_thread_read(phoneid, thread_hash_buf,
169
        THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
3442 svoboda 170
    if (rc < 0) {
171
        printf("udebug_thread_read() -> %d\n", rc);
172
        return rc;
173
    }
3438 svoboda 174
 
3454 svoboda 175
    n_threads = tb_copied / sizeof(uintptr_t);
3438 svoboda 176
 
3442 svoboda 177
    printf("Threads:");
178
    for (i = 0; i < n_threads; i++) {
3454 svoboda 179
        printf(" [%d] (hash 0x%lx)", 1+i, thread_hash_buf[i]);
3438 svoboda 180
    }
3454 svoboda 181
    printf("\ntotal of %u threads\n", tb_needed / sizeof(uintptr_t));
3438 svoboda 182
 
183
    return 0;
184
}
185
 
3455 svoboda 186
void val_print(sysarg_t val, val_type_t v_type)
3438 svoboda 187
{
3452 svoboda 188
    switch (v_type) {
189
    case V_VOID:
190
        printf("<void>");
191
        break;
192
 
193
    case V_INTEGER:
3455 svoboda 194
        printf("%ld", val);
3452 svoboda 195
        break;
196
 
197
    case V_HASH:
3470 svoboda 198
    case V_PTR:
3455 svoboda 199
        printf("0x%08lx", val);
3452 svoboda 200
        break;
201
 
202
    case V_ERRNO:
203
        if (val >= -15 && val <= 0) {
3455 svoboda 204
            printf("%ld %s (%s)", val,
3452 svoboda 205
                err_desc[-val].name,
206
                err_desc[-val].desc);
3438 svoboda 207
        } else {
3455 svoboda 208
            printf("%ld", val);
3438 svoboda 209
        }
3452 svoboda 210
        break;
211
    case V_INT_ERRNO:
212
        if (val >= -15 && val < 0) {
3455 svoboda 213
            printf("%ld %s (%s)", val,
3452 svoboda 214
                err_desc[-val].name,
215
                err_desc[-val].desc);
3438 svoboda 216
        } else {
3455 svoboda 217
            printf("%ld", val);
3438 svoboda 218
        }
3452 svoboda 219
        break;
220
 
221
    case V_CHAR:
222
        if (val >= 0x20 && val < 0x7f) {
223
            printf("'%c'", val);
224
        } else {
225
            switch (val) {
226
            case '\a': printf("'\\a'"); break;
227
            case '\b': printf("'\\b'"); break;
228
            case '\n': printf("'\\n'"); break;
229
            case '\r': printf("'\\r'"); break;
230
            case '\t': printf("'\\t'"); break;
231
            case '\\': printf("'\\\\'"); break;
3455 svoboda 232
            default: printf("'\\x%02lX'", val); break;
3452 svoboda 233
            }
234
        }
235
        break;
3438 svoboda 236
    }
3452 svoboda 237
}
238
 
239
 
3455 svoboda 240
static void print_sc_retval(sysarg_t retval, val_type_t val_type)
3452 svoboda 241
{
242
    printf(" -> ");
243
    val_print(retval, val_type);
3438 svoboda 244
    putchar('\n');
245
}
246
 
3454 svoboda 247
static void print_sc_args(sysarg_t *sc_args, int n)
3438 svoboda 248
{
249
    int i;
250
 
251
    putchar('(');
3454 svoboda 252
    if (n > 0) printf("%ld", sc_args[0]);
3452 svoboda 253
    for (i = 1; i < n; i++) {
3454 svoboda 254
        printf(", %ld", sc_args[i]);
3438 svoboda 255
    }
256
    putchar(')');
257
}
258
 
3454 svoboda 259
static void sc_ipc_call_async_fast(sysarg_t *sc_args, sysarg_t sc_rc)
3438 svoboda 260
{
261
    ipc_call_t call;
3454 svoboda 262
    ipcarg_t phoneid;
3438 svoboda 263
 
264
    if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
265
        return;
266
 
267
    phoneid = sc_args[0];
268
 
269
    IPC_SET_METHOD(call, sc_args[1]);
270
    IPC_SET_ARG1(call, sc_args[2]);
271
    IPC_SET_ARG2(call, sc_args[3]);
272
    IPC_SET_ARG3(call, sc_args[4]);
273
    IPC_SET_ARG4(call, sc_args[5]);
274
    IPC_SET_ARG5(call, 0);
275
 
276
    ipcp_call_out(phoneid, &call, sc_rc);
277
}
278
 
3454 svoboda 279
static void sc_ipc_call_async_slow(sysarg_t *sc_args, sysarg_t sc_rc)
3438 svoboda 280
{
281
    ipc_call_t call;
282
    int rc;
283
 
284
    if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
285
        return;
286
 
287
    memset(&call, 0, sizeof(call));
288
    rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
289
 
290
    if (rc >= 0) {
291
        ipcp_call_out(sc_args[0], &call, sc_rc);
292
    }
293
}
294
 
3454 svoboda 295
static void sc_ipc_call_sync_fast(sysarg_t *sc_args)
3438 svoboda 296
{
297
    ipc_call_t question, reply;
298
    int rc;
299
    int phoneidx;
300
 
301
//  printf("sc_ipc_call_sync_fast()\n");
302
    phoneidx = sc_args[0];
303
 
304
    IPC_SET_METHOD(question, sc_args[1]);
305
    IPC_SET_ARG1(question, sc_args[2]);
306
    IPC_SET_ARG2(question, sc_args[3]);
307
    IPC_SET_ARG3(question, sc_args[4]);
308
    IPC_SET_ARG4(question, 0);
309
    IPC_SET_ARG5(question, 0);
310
 
311
//  printf("memset\n");
312
    memset(&reply, 0, sizeof(reply));
313
//  printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
314
//      phoneid, &reply.args, sc_args[5], sizeof(reply.args));
315
    rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
316
//  printf("dmr->%d\n", rc);
317
    if (rc < 0) return;
318
 
319
//  printf("call ipc_call_sync\n");
320
    ipcp_call_sync(phoneidx, &question, &reply);
321
}
322
 
3454 svoboda 323
static void sc_ipc_call_sync_slow(sysarg_t *sc_args)
3438 svoboda 324
{
325
    ipc_call_t question, reply;
326
    int rc;
327
 
328
    memset(&question, 0, sizeof(question));
329
    rc = udebug_mem_read(phoneid, &question.args, sc_args[1], sizeof(question.args));
330
    printf("dmr->%d\n", rc);
331
    if (rc < 0) return;
332
 
333
    memset(&reply, 0, sizeof(reply));
334
    rc = udebug_mem_read(phoneid, &reply.args, sc_args[2], sizeof(reply.args));
335
    printf("dmr->%d\n", rc);
336
    if (rc < 0) return;
337
 
338
    ipcp_call_sync(sc_args[0], &question, &reply);
339
}
340
 
3454 svoboda 341
static void sc_ipc_wait(sysarg_t *sc_args, int sc_rc)
3438 svoboda 342
{
343
    ipc_call_t call;
344
    int rc;
345
 
346
    if (sc_rc == 0) return;
347
 
348
    memset(&call, 0, sizeof(call));
349
    rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
350
//  printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
351
//      phoneid, (int)&call, sc_args[0], sizeof(call), rc);
352
 
353
    if (rc >= 0) {
354
        ipcp_call_in(&call, sc_rc);
355
    }
356
}
357
 
3454 svoboda 358
static void event_syscall_b(unsigned thread_id, uintptr_t thread_hash,
359
    unsigned sc_id, sysarg_t sc_rc)
3438 svoboda 360
{
3454 svoboda 361
    sysarg_t sc_args[6];
3438 svoboda 362
    int rc;
363
 
364
    /* Read syscall arguments */
365
    rc = udebug_args_read(phoneid, thread_hash, sc_args);
366
 
367
    async_serialize_start();
368
 
369
//  printf("[%d] ", thread_id);
370
 
371
    if (rc < 0) {
372
        printf("error\n");
373
        async_serialize_end();
374
        return;
375
    }
376
 
3444 svoboda 377
    if ((display_mask & DM_SYSCALL) != 0) {
378
        /* Print syscall name and arguments */
379
        printf("%s", syscall_desc[sc_id].name);
380
        print_sc_args(sc_args, syscall_desc[sc_id].n_args);
381
    }
3438 svoboda 382
 
383
    async_serialize_end();
384
}
385
 
3454 svoboda 386
static void event_syscall_e(unsigned thread_id, uintptr_t thread_hash,
387
    unsigned sc_id, sysarg_t sc_rc)
3438 svoboda 388
{
3454 svoboda 389
    sysarg_t sc_args[6];
3438 svoboda 390
    int rv_type;
391
    int rc;
392
 
393
    /* Read syscall arguments */
394
    rc = udebug_args_read(phoneid, thread_hash, sc_args);
395
 
396
    async_serialize_start();
397
 
398
//  printf("[%d] ", thread_id);
399
 
400
    if (rc < 0) {
401
        printf("error\n");
402
        async_serialize_end();
403
        return;
404
    }
405
 
3444 svoboda 406
    if ((display_mask & DM_SYSCALL) != 0) {
407
        /* Print syscall return value */
408
        rv_type = syscall_desc[sc_id].rv_type;
409
        print_sc_retval(sc_rc, rv_type);
410
    }
3438 svoboda 411
 
412
    switch (sc_id) {
413
    case SYS_IPC_CALL_ASYNC_FAST:
414
        sc_ipc_call_async_fast(sc_args, sc_rc);
415
        break;
416
    case SYS_IPC_CALL_ASYNC_SLOW:
417
        sc_ipc_call_async_slow(sc_args, sc_rc);
418
        break;
419
    case SYS_IPC_CALL_SYNC_FAST:
420
        sc_ipc_call_sync_fast(sc_args);
421
        break;
422
    case SYS_IPC_CALL_SYNC_SLOW:
423
        sc_ipc_call_sync_slow(sc_args);
424
        break;
425
    case SYS_IPC_WAIT:
426
        sc_ipc_wait(sc_args, sc_rc);
427
        break;
428
    default:
429
        break;
430
    }
431
 
432
    async_serialize_end();
433
}
434
 
3454 svoboda 435
static void event_thread_b(uintptr_t hash)
3438 svoboda 436
{
437
    async_serialize_start();
3454 svoboda 438
    printf("New thread, hash 0x%lx\n", hash);
3438 svoboda 439
    async_serialize_end();
440
 
441
    thread_trace_start(hash);
442
}
443
 
444
static int trace_loop(void *thread_hash_arg)
445
{
446
    int rc;
447
    unsigned ev_type;
3454 svoboda 448
    uintptr_t thread_hash;
3438 svoboda 449
    unsigned thread_id;
3454 svoboda 450
    sysarg_t val0, val1;
3438 svoboda 451
 
3454 svoboda 452
    thread_hash = (uintptr_t)thread_hash_arg;
3438 svoboda 453
    thread_id = next_thread_id++;
454
 
3605 svoboda 455
    printf("Start tracing thread [%d] (hash 0x%lx).\n", thread_id, thread_hash);
3438 svoboda 456
 
457
    while (!abort_trace) {
458
 
4654 svoboda 459
        fibril_mutex_lock(&paused_lock);
3603 svoboda 460
        if (paused) {
4654 svoboda 461
            printf("Thread [%d] paused. Press R to resume.\n",
462
                thread_id);
463
 
464
            while (paused)
465
                fibril_condvar_wait(&paused_cv, &paused_lock);
466
 
467
            printf("Thread [%d] resumed.\n", thread_id);
3603 svoboda 468
        }
4654 svoboda 469
        fibril_mutex_unlock(&paused_lock);
3603 svoboda 470
 
3438 svoboda 471
        /* Run thread until an event occurs */
472
        rc = udebug_go(phoneid, thread_hash,
473
            &ev_type, &val0, &val1);
474
 
475
//      printf("rc = %d, ev_type=%d\n", rc, ev_type);
476
        if (ev_type == UDEBUG_EVENT_FINISHED) {
3442 svoboda 477
            /* Done tracing this thread */
3438 svoboda 478
            break;
479
        }
480
 
481
        if (rc >= 0) {
482
            switch (ev_type) {
483
            case UDEBUG_EVENT_SYSCALL_B:
484
                event_syscall_b(thread_id, thread_hash, val0, (int)val1);
485
                break;
486
            case UDEBUG_EVENT_SYSCALL_E:
487
                event_syscall_e(thread_id, thread_hash, val0, (int)val1);
488
                break;
489
            case UDEBUG_EVENT_STOP:
3442 svoboda 490
                printf("Stop event\n");
4654 svoboda 491
                fibril_mutex_lock(&paused_lock);
492
                paused = 1;
493
                fibril_mutex_unlock(&paused_lock);
3438 svoboda 494
                break;
495
            case UDEBUG_EVENT_THREAD_B:
496
                event_thread_b(val0);
497
                break;
498
            case UDEBUG_EVENT_THREAD_E:
3605 svoboda 499
                printf("Thread 0x%lx exited.\n", val0);
3438 svoboda 500
                abort_trace = 1;
501
                break;
502
            default:
3605 svoboda 503
                printf("Unknown event type %d.\n", ev_type);
3438 svoboda 504
                break;
505
            }
506
        }
507
 
508
    }
509
 
3605 svoboda 510
    printf("Finished tracing thread [%d].\n", thread_id);
3438 svoboda 511
    return 0;
512
}
513
 
3454 svoboda 514
void thread_trace_start(uintptr_t thread_hash)
3438 svoboda 515
{
516
    fid_t fid;
517
 
518
    thash = thread_hash;
519
 
520
    fid = fibril_create(trace_loop, (void *)thread_hash);
521
    if (fid == 0) {
522
        printf("Warning: Failed creating fibril\n");
523
    }
524
    fibril_add_ready(fid);
525
}
526
 
3470 svoboda 527
static loader_t *preload_task(const char *path, char *const argv[],
528
    task_id_t *task_id)
3438 svoboda 529
{
3470 svoboda 530
    loader_t *ldr;
531
    int rc;
532
 
533
    /* Spawn a program loader */   
3897 svoboda 534
    ldr = loader_connect();
3470 svoboda 535
    if (ldr == NULL)
536
        return 0;
537
 
538
    /* Get task ID. */
539
    rc = loader_get_task_id(ldr, task_id);
540
    if (rc != EOK)
541
        goto error;
542
 
543
    /* Send program pathname */
544
    rc = loader_set_pathname(ldr, path);
545
    if (rc != EOK)
546
        goto error;
547
 
548
    /* Send arguments */
549
    rc = loader_set_args(ldr, argv);
550
    if (rc != EOK)
551
        goto error;
552
 
553
    /* Load the program. */
554
    rc = loader_load_program(ldr);
555
    if (rc != EOK)
556
        goto error;
557
 
558
    /* Success */
559
    return ldr;
560
 
561
    /* Error exit */
562
error:
563
    loader_abort(ldr);
564
    free(ldr);
565
    return NULL;
566
}
567
 
568
static void trace_task(task_id_t task_id)
569
{
4653 svoboda 570
    console_event_t ev;
571
    bool done;
3438 svoboda 572
    int i;
573
    int rc;
574
 
575
    ipcp_init();
576
 
3442 svoboda 577
    /*
578
     * User apps now typically have console on phone 3.
579
     * (Phones 1 and 2 are used by the loader).
580
     */
581
    ipcp_connection_set(3, 0, proto_console);
582
 
3438 svoboda 583
    rc = get_thread_list();
584
    if (rc < 0) {
585
        printf("Failed to get thread list (error %d)\n", rc);
586
        return;
587
    }
588
 
589
    abort_trace = 0;
590
 
591
    for (i = 0; i < n_threads; i++) {
592
        thread_trace_start(thread_hash_buf[i]);
593
    }
594
 
4653 svoboda 595
    done = false;
596
 
597
    while (!done) {
598
        if (!console_get_event(fphone(stdin), &ev))
599
            return;
600
 
601
        if (ev.type != KEY_PRESS)
602
            continue;
603
 
604
        switch (ev.key) {
605
        case KC_Q:
606
            done = true;
607
            break;
608
        case KC_P:
3603 svoboda 609
            printf("Pause...\n");
3438 svoboda 610
            rc = udebug_stop(phoneid, thash);
4654 svoboda 611
            if (rc != EOK)
612
                printf("Error: stop -> %d\n", rc);
4653 svoboda 613
            break;
614
        case KC_R:
4654 svoboda 615
            fibril_mutex_lock(&paused_lock);
3438 svoboda 616
            paused = 0;
4654 svoboda 617
            fibril_condvar_broadcast(&paused_cv);
618
            fibril_mutex_unlock(&paused_lock);
3603 svoboda 619
            printf("Resume...\n");
4653 svoboda 620
            break;
3438 svoboda 621
        }
622
    }
623
 
3442 svoboda 624
    printf("\nTerminate debugging session...\n");
3438 svoboda 625
    abort_trace = 1;
626
    udebug_end(phoneid);
627
    ipc_hangup(phoneid);
628
 
629
    ipcp_cleanup();
630
 
3442 svoboda 631
    printf("Done\n");
3438 svoboda 632
    return;
633
}
634
 
635
static void main_init(void)
636
{
637
    proto_t *p;
638
    oper_t *o;
639
 
3452 svoboda 640
    val_type_t arg_def[OPER_MAX_ARGS] = {
641
        V_INTEGER,
642
        V_INTEGER,
643
        V_INTEGER,
644
        V_INTEGER,
645
        V_INTEGER      
646
    };
647
 
648
    val_type_t resp_def[OPER_MAX_ARGS] = {
649
        V_INTEGER,
650
        V_INTEGER,
651
        V_INTEGER,
652
        V_INTEGER,
3905 svoboda 653
        V_INTEGER
3452 svoboda 654
    };
655
 
3438 svoboda 656
    next_thread_id = 1;
657
    paused = 0;
4654 svoboda 658
    fibril_mutex_initialize(&paused_lock);
659
    fibril_condvar_initialize(&paused_cv);
3438 svoboda 660
 
661
    proto_init();
662
 
663
    p = proto_new("vfs");
3452 svoboda 664
    o = oper_new("read", 1, arg_def, V_ERRNO, 1, resp_def);
4584 jermar 665
    proto_add_oper(p, VFS_IN_READ, o);
3452 svoboda 666
    o = oper_new("write", 1, arg_def, V_ERRNO, 1, resp_def);
4584 jermar 667
    proto_add_oper(p, VFS_IN_WRITE, o);
3452 svoboda 668
    o = oper_new("truncate", 5, arg_def, V_ERRNO, 0, resp_def);
4584 jermar 669
    proto_add_oper(p, VFS_IN_TRUNCATE, o);
3452 svoboda 670
    o = oper_new("mount", 2, arg_def, V_ERRNO, 0, resp_def);
4584 jermar 671
    proto_add_oper(p, VFS_IN_MOUNT, o);
3452 svoboda 672
/*  o = oper_new("unmount", 0, arg_def);
4584 jermar 673
    proto_add_oper(p, VFS_IN_UNMOUNT, o);*/
3850 svoboda 674
    o = oper_new("open", 2, arg_def, V_INT_ERRNO, 0, resp_def);
4584 jermar 675
    proto_add_oper(p, VFS_IN_OPEN, o);
3850 svoboda 676
    o = oper_new("close", 1, arg_def, V_ERRNO, 0, resp_def);
4584 jermar 677
    proto_add_oper(p, VFS_IN_CLOSE, o);
3850 svoboda 678
    o = oper_new("seek", 3, arg_def, V_ERRNO, 0, resp_def);
4584 jermar 679
    proto_add_oper(p, VFS_IN_SEEK, o);
3850 svoboda 680
    o = oper_new("mkdir", 1, arg_def, V_ERRNO, 0, resp_def);
4584 jermar 681
    proto_add_oper(p, VFS_IN_MKDIR, o);
3850 svoboda 682
    o = oper_new("unlink", 0, arg_def, V_ERRNO, 0, resp_def);
4584 jermar 683
    proto_add_oper(p, VFS_IN_UNLINK, o);
3850 svoboda 684
    o = oper_new("rename", 0, arg_def, V_ERRNO, 0, resp_def);
4584 jermar 685
    proto_add_oper(p, VFS_IN_RENAME, o);
3438 svoboda 686
 
687
    proto_register(SERVICE_VFS, p);
688
 
689
    p = proto_new("console");
3905 svoboda 690
    resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER;
691
    resp_def[2] = V_INTEGER; resp_def[3] = V_CHAR;
692
    o = oper_new("getkey", 0, arg_def, V_ERRNO, 4, resp_def);
3452 svoboda 693
 
694
    arg_def[0] = V_CHAR;
695
    o = oper_new("clear", 0, arg_def, V_VOID, 0, resp_def);
3438 svoboda 696
    proto_add_oper(p, CONSOLE_CLEAR, o);
3452 svoboda 697
 
698
    arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER;
699
    o = oper_new("goto", 2, arg_def, V_VOID, 0, resp_def);
3438 svoboda 700
    proto_add_oper(p, CONSOLE_GOTO, o);
3452 svoboda 701
 
702
    resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER;
703
    o = oper_new("getsize", 0, arg_def, V_INTEGER, 2, resp_def);
4489 decky 704
    proto_add_oper(p, CONSOLE_GET_SIZE, o);
3452 svoboda 705
 
3767 svoboda 706
    arg_def[0] = V_INTEGER;
3850 svoboda 707
    o = oper_new("set_style", 1, arg_def, V_VOID, 0, resp_def);
3767 svoboda 708
    proto_add_oper(p, CONSOLE_SET_STYLE, o);
709
    arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER; arg_def[2] = V_INTEGER;
3850 svoboda 710
    o = oper_new("set_color", 3, arg_def, V_VOID, 0, resp_def);
3767 svoboda 711
    proto_add_oper(p, CONSOLE_SET_COLOR, o);
3452 svoboda 712
    arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER;
3850 svoboda 713
    o = oper_new("set_rgb_color", 2, arg_def, V_VOID, 0, resp_def);
3767 svoboda 714
    proto_add_oper(p, CONSOLE_SET_RGB_COLOR, o);
3452 svoboda 715
    o = oper_new("cursor_visibility", 1, arg_def, V_VOID, 0, resp_def);
3438 svoboda 716
    proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
717
 
718
    proto_console = p;
719
    proto_register(SERVICE_CONSOLE, p);
720
}
721
 
722
static void print_syntax()
723
{
3447 svoboda 724
    printf("Syntax:\n");
725
    printf("\ttrace [+<events>] <executable> [<arg1> [...]]\n");
726
    printf("or\ttrace [+<events>] -t <task_id>\n");
3444 svoboda 727
    printf("Events: (default is +tp)\n");
728
    printf("\n");
729
    printf("\tt ... Thread creation and termination\n");
730
    printf("\ts ... System calls\n");
731
    printf("\ti ... Low-level IPC\n");
732
    printf("\tp ... Protocol level\n");
733
    printf("\n");
3447 svoboda 734
    printf("Examples:\n");
735
    printf("\ttrace +s /app/tetris\n");
736
    printf("\ttrace +tsip -t 12\n");
3438 svoboda 737
}
738
 
3444 svoboda 739
static display_mask_t parse_display_mask(char *text)
3438 svoboda 740
{
3444 svoboda 741
    display_mask_t dm;
742
    char *c;
743
 
744
    c = text;
745
 
746
    while (*c) {
747
        switch (*c) {
748
        case 't': dm = dm | DM_THREAD; break;
749
        case 's': dm = dm | DM_SYSCALL; break;
750
        case 'i': dm = dm | DM_IPC; break;
751
        case 'p': dm = dm | DM_SYSTEM | DM_USER; break;
752
        default:
3605 svoboda 753
            printf("Unexpected event type '%c'.\n", *c);
3444 svoboda 754
            exit(1);
755
        }
756
 
757
        ++c;
758
    }
759
 
760
    return dm;
761
}
762
 
763
static int parse_args(int argc, char *argv[])
764
{
765
    char *arg;
3438 svoboda 766
    char *err_p;
767
 
3447 svoboda 768
    task_id = 0;
769
 
3444 svoboda 770
    --argc; ++argv;
771
 
3447 svoboda 772
    while (argc > 0) {
3444 svoboda 773
        arg = *argv;
774
        if (arg[0] == '+') {
775
            display_mask = parse_display_mask(&arg[1]);
3447 svoboda 776
        } else if (arg[0] == '-') {
777
            if (arg[1] == 't') {
778
                /* Trace an already running task */
779
                --argc; ++argv;
780
                task_id = strtol(*argv, &err_p, 10);
3470 svoboda 781
                task_ldr = NULL;
3447 svoboda 782
                if (*err_p) {
783
                    printf("Task ID syntax error\n");
784
                    print_syntax();
785
                    return -1;
786
                }
787
            } else {
788
                printf("Uknown option '%s'\n", arg[0]);
789
                print_syntax();
790
                return -1;
791
            }
3444 svoboda 792
        } else {
3447 svoboda 793
            break;
3444 svoboda 794
        }
795
 
796
        --argc; ++argv;
797
    }
798
 
3447 svoboda 799
    if (task_id != 0) {
3454 svoboda 800
        if (argc == 0) return 0;
3447 svoboda 801
        printf("Extra arguments\n");
3438 svoboda 802
        print_syntax();
3447 svoboda 803
        return -1;
3438 svoboda 804
    }
805
 
3447 svoboda 806
    if (argc < 1) {
807
        printf("Missing argument\n");
3438 svoboda 808
        print_syntax();
3444 svoboda 809
        return -1;
3438 svoboda 810
    }
811
 
3470 svoboda 812
    /* Preload the specified program file. */
3447 svoboda 813
    printf("Spawning '%s' with arguments:\n", *argv);
814
    {
815
        char **cp = argv;
816
        while (*cp) printf("'%s'\n", *cp++);
817
    }
3470 svoboda 818
    task_ldr = preload_task(*argv, argv, &task_id);
3447 svoboda 819
 
3444 svoboda 820
    return 0;
821
}
822
 
823
int main(int argc, char *argv[])
824
{
3470 svoboda 825
    int rc;
826
 
3444 svoboda 827
    printf("System Call / IPC Tracer\n");
3605 svoboda 828
    printf("Controls: Q - Quit, P - Pause, R - Resume\n");
3444 svoboda 829
 
830
    display_mask = DM_THREAD | DM_SYSTEM | DM_USER;
831
 
832
    if (parse_args(argc, argv) < 0)
833
        return 1;
834
 
3438 svoboda 835
    main_init();
3444 svoboda 836
 
3470 svoboda 837
    rc = connect_task(task_id);
838
    if (rc < 0) {
3605 svoboda 839
        printf("Failed connecting to task %lld.\n", task_id);
3470 svoboda 840
        return 1;
841
    }
842
 
3605 svoboda 843
    printf("Connected to task %lld.\n", task_id);
3470 svoboda 844
 
845
    if (task_ldr != NULL) {
846
        program_run();
847
    }
848
 
849
    trace_task(task_id);
850
 
3444 svoboda 851
    return 0;
3438 svoboda 852
}
853
 
854
/** @}
855
 */