Subversion Repositories HelenOS

Rev

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