Subversion Repositories HelenOS

Rev

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

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