Subversion Repositories HelenOS

Rev

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