Subversion Repositories HelenOS

Rev

Rev 3435 | Rev 3471 | 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>
2832 svoboda 44
 
2880 svoboda 45
// Temporary: service and method names
2878 svoboda 46
#include "proto.h"
47
#include <ipc/services.h>
2880 svoboda 48
#include "../../srv/vfs/vfs.h"
2882 svoboda 49
#include "../../srv/console/console.h"
2878 svoboda 50
 
2832 svoboda 51
#include "syscalls.h"
2874 svoboda 52
#include "ipcp.h"
2832 svoboda 53
#include "errors.h"
54
 
2868 svoboda 55
#define THBUF_SIZE 64
56
unsigned thread_hash_buf[THBUF_SIZE];
2850 svoboda 57
unsigned n_threads;
2832 svoboda 58
 
2868 svoboda 59
int next_thread_id;
60
 
2832 svoboda 61
int phoneid;
2835 svoboda 62
int abort_trace;
2832 svoboda 63
 
2898 svoboda 64
unsigned thash;
65
volatile int paused;
66
 
2868 svoboda 67
void thread_trace_start(unsigned thread_hash);
68
 
2882 svoboda 69
static proto_t *proto_console;
2868 svoboda 70
 
3428 svoboda 71
static int task_connect(task_id_t task_id)
2832 svoboda 72
{
73
    int rc;
74
 
3428 svoboda 75
    printf("ipc_connect_task(%lld)... ", task_id);
76
    rc = ipc_connect_kbox(task_id);
2832 svoboda 77
    printf("-> %d\n", rc);
78
    phoneid = rc;
79
    if (rc < 0) return rc;
80
 
2904 svoboda 81
    printf("udebug_begin()... ");
82
    rc = udebug_begin(phoneid);
2832 svoboda 83
    printf("-> %d\n", rc);
84
    if (rc < 0) return rc;
85
 
2904 svoboda 86
    printf("udebug_set_evmask(0x%x)... ", UDEBUG_EM_ALL);
87
    rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL);
2899 svoboda 88
    printf("-> %d\n", rc);
89
    if (rc < 0) return rc;
90
 
2832 svoboda 91
    return 0;
92
}
93
 
2946 svoboda 94
static int get_thread_list(void)
2832 svoboda 95
{
96
    int rc;
2946 svoboda 97
    size_t tb_copied;
98
    size_t tb_needed;
2832 svoboda 99
    int i;
100
 
101
    printf("send IPC_M_DEBUG_THREAD_READ message\n");
2946 svoboda 102
    rc = udebug_thread_read(phoneid, thread_hash_buf,
2868 svoboda 103
        THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
2832 svoboda 104
    printf("-> %d\n", rc);
105
    if (rc < 0) return rc;
106
 
2850 svoboda 107
    n_threads = tb_copied / sizeof(unsigned);
108
 
2832 svoboda 109
    printf("thread IDs:");
2850 svoboda 110
    for (i=0; i<n_threads; i++) {
2868 svoboda 111
        printf(" %u", thread_hash_buf[i]);
2832 svoboda 112
    }
113
    printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned));
114
 
115
    return 0;
116
}
117
 
2946 svoboda 118
static void print_sc_retval(int retval, rv_type_t rv_type)
2832 svoboda 119
{
120
    printf (" -> ");
121
    if (rv_type == RV_INTEGER) {
122
        printf("%d", retval);
123
    } else if (rv_type == RV_HASH) {
124
        printf("0x%08x", retval);
125
    } else if (rv_type == RV_ERRNO) {
126
        if (retval >= -15 && retval <= 0) {
127
            printf("%d %s (%s)", retval,
128
                err_desc[retval].name,
129
                err_desc[retval].desc);
130
        } else {
131
            printf("%d", retval);
132
        }
133
    } else if (rv_type == RV_INT_ERRNO) {
134
        if (retval >= -15 && retval < 0) {
135
            printf("%d %s (%s)", retval,
136
                err_desc[retval].name,
137
                err_desc[retval].desc);
138
        } else {
139
            printf("%d", retval);
140
        }
141
    }
142
    putchar('\n');
143
}
144
 
2946 svoboda 145
static void print_sc_args(unsigned *sc_args, int n)
2832 svoboda 146
{
147
    int i;
148
 
149
    putchar('(');
150
    if (n > 0) printf("%d", sc_args[0]);
151
    for (i=1; i<n; i++) {
152
        printf(", %d", sc_args[i]);
153
    }
154
    putchar(')');
155
}
156
 
2946 svoboda 157
static void sc_ipc_call_async_fast(unsigned *sc_args, int sc_rc)
2871 svoboda 158
{
159
    ipc_call_t call;
160
    int phoneid;
2872 svoboda 161
 
162
    if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
163
        return;
2871 svoboda 164
 
165
    phoneid = sc_args[0];
166
 
167
    IPC_SET_METHOD(call, sc_args[1]);
168
    IPC_SET_ARG1(call, sc_args[2]);
169
    IPC_SET_ARG2(call, sc_args[3]);
170
    IPC_SET_ARG3(call, sc_args[4]);
171
    IPC_SET_ARG4(call, sc_args[5]);
172
    IPC_SET_ARG5(call, 0);
173
 
2874 svoboda 174
    ipcp_call_out(phoneid, &call, sc_rc);
2871 svoboda 175
}
176
 
2946 svoboda 177
static void sc_ipc_call_async_slow(unsigned *sc_args, int sc_rc)
2832 svoboda 178
{
2871 svoboda 179
    ipc_call_t call;
2832 svoboda 180
    int rc;
181
 
2872 svoboda 182
    if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
183
        return;
184
 
2871 svoboda 185
    memset(&call, 0, sizeof(call));
2904 svoboda 186
    rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
2832 svoboda 187
 
188
    if (rc >= 0) {
2874 svoboda 189
        ipcp_call_out(sc_args[0], &call, sc_rc);
2832 svoboda 190
    }
191
}
192
 
2946 svoboda 193
static void sc_ipc_call_sync_fast(unsigned *sc_args)
2872 svoboda 194
{
195
    ipc_call_t question, reply;
196
    int rc;
197
    int phoneidx;
198
 
2877 svoboda 199
//  printf("sc_ipc_call_sync_fast()\n");
2872 svoboda 200
    phoneidx = sc_args[0];
201
 
202
    IPC_SET_METHOD(question, sc_args[1]);
203
    IPC_SET_ARG1(question, sc_args[2]);
204
    IPC_SET_ARG2(question, sc_args[3]);
205
    IPC_SET_ARG3(question, sc_args[4]);
206
    IPC_SET_ARG4(question, 0);
207
    IPC_SET_ARG5(question, 0);
208
 
2877 svoboda 209
//  printf("memset\n");
2872 svoboda 210
    memset(&reply, 0, sizeof(reply));
2904 svoboda 211
//  printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
2877 svoboda 212
//      phoneid, &reply.args, sc_args[5], sizeof(reply.args));
2904 svoboda 213
    rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
2877 svoboda 214
//  printf("dmr->%d\n", rc);
2872 svoboda 215
    if (rc < 0) return;
216
 
2877 svoboda 217
//  printf("call ipc_call_sync\n");
2874 svoboda 218
    ipcp_call_sync(phoneidx, &question, &reply);
2872 svoboda 219
}
220
 
2946 svoboda 221
static void sc_ipc_call_sync_slow(unsigned *sc_args)
2872 svoboda 222
{
223
    ipc_call_t question, reply;
224
    int rc;
225
 
226
    memset(&question, 0, sizeof(question));
2904 svoboda 227
    rc = udebug_mem_read(phoneid, &question.args, sc_args[1], sizeof(question.args));
2872 svoboda 228
    printf("dmr->%d\n", rc);
229
    if (rc < 0) return;
230
 
231
    memset(&reply, 0, sizeof(reply));
2904 svoboda 232
    rc = udebug_mem_read(phoneid, &reply.args, sc_args[2], sizeof(reply.args));
2872 svoboda 233
    printf("dmr->%d\n", rc);
234
    if (rc < 0) return;
235
 
2874 svoboda 236
    ipcp_call_sync(sc_args[0], &question, &reply);
2872 svoboda 237
}
238
 
2946 svoboda 239
static void sc_ipc_wait(unsigned *sc_args, int sc_rc)
2871 svoboda 240
{
241
    ipc_call_t call;
242
    int rc;
243
 
2946 svoboda 244
    if (sc_rc == 0) return;
2871 svoboda 245
 
246
    memset(&call, 0, sizeof(call));
2904 svoboda 247
    rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
248
//  printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
2872 svoboda 249
//      phoneid, (int)&call, sc_args[0], sizeof(call), rc);
2871 svoboda 250
 
251
    if (rc >= 0) {
2874 svoboda 252
        ipcp_call_in(&call, sc_rc);
2871 svoboda 253
    }
254
}
255
 
2946 svoboda 256
static void event_syscall_b(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
2867 svoboda 257
{
258
    unsigned sc_args[6];
259
    int rc;
260
 
261
    /* Read syscall arguments */
2904 svoboda 262
    rc = udebug_args_read(phoneid, thread_hash, sc_args);
2867 svoboda 263
 
264
    async_serialize_start();
265
 
2872 svoboda 266
//  printf("[%d] ", thread_id);
2867 svoboda 267
 
268
    if (rc < 0) {
269
        printf("error\n");
270
        async_serialize_end();
271
        return;
272
    }
273
 
274
    /* Print syscall name, id and arguments */
275
    printf("%s", syscall_desc[sc_id].name);
276
    print_sc_args(sc_args, syscall_desc[sc_id].n_args);
2901 svoboda 277
 
278
    async_serialize_end();
279
}
280
 
2946 svoboda 281
static void event_syscall_e(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
2901 svoboda 282
{
283
    unsigned sc_args[6];
284
    int rv_type;
285
    int rc;
286
 
287
    /* Read syscall arguments */
2904 svoboda 288
    rc = udebug_args_read(phoneid, thread_hash, sc_args);
2901 svoboda 289
 
290
    async_serialize_start();
291
 
292
//  printf("[%d] ", thread_id);
293
 
294
    if (rc < 0) {
295
        printf("error\n");
296
        async_serialize_end();
297
        return;
298
    }
299
 
2867 svoboda 300
    rv_type = syscall_desc[sc_id].rv_type;
301
    print_sc_retval(sc_rc, rv_type);
302
 
303
    switch (sc_id) {
2871 svoboda 304
    case SYS_IPC_CALL_ASYNC_FAST:
2872 svoboda 305
        sc_ipc_call_async_fast(sc_args, sc_rc);
2871 svoboda 306
        break;
2867 svoboda 307
    case SYS_IPC_CALL_ASYNC_SLOW:
2872 svoboda 308
        sc_ipc_call_async_slow(sc_args, sc_rc);
2867 svoboda 309
        break;
2872 svoboda 310
    case SYS_IPC_CALL_SYNC_FAST:
311
        sc_ipc_call_sync_fast(sc_args);
312
        break;
313
    case SYS_IPC_CALL_SYNC_SLOW:
314
        sc_ipc_call_sync_slow(sc_args);
315
        break;
2871 svoboda 316
    case SYS_IPC_WAIT:
317
        sc_ipc_wait(sc_args, sc_rc);
318
        break;
2867 svoboda 319
    default:
320
        break;
321
    }
322
 
323
    async_serialize_end();
324
}
325
 
2946 svoboda 326
static void event_thread_b(unsigned hash)
2867 svoboda 327
{
328
    async_serialize_start();
329
    printf("new thread, hash 0x%x\n", hash);
330
    async_serialize_end();
2868 svoboda 331
 
332
    thread_trace_start(hash);
2867 svoboda 333
}
334
 
2946 svoboda 335
static int trace_loop(void *thread_hash_arg)
2835 svoboda 336
{
2832 svoboda 337
    int rc;
338
    unsigned ev_type;
2868 svoboda 339
    unsigned thread_hash;
340
    unsigned thread_id;
2867 svoboda 341
    unsigned val0, val1;
2832 svoboda 342
 
2868 svoboda 343
    thread_hash = (unsigned)thread_hash_arg;
344
    thread_id = next_thread_id++;
2835 svoboda 345
 
2868 svoboda 346
    printf("trace_loop(%d)\n", thread_id); 
347
 
2835 svoboda 348
    while (!abort_trace) {
349
 
2867 svoboda 350
        /* Run thread until an event occurs */
2904 svoboda 351
        rc = udebug_go(phoneid, thread_hash,
2867 svoboda 352
            &ev_type, &val0, &val1);
2832 svoboda 353
 
2872 svoboda 354
//      printf("rc = %d, ev_type=%d\n", rc, ev_type);
2838 svoboda 355
        if (ev_type == UDEBUG_EVENT_FINISHED) {
2868 svoboda 356
            printf("thread %u debugging finished\n", thread_id);
2838 svoboda 357
            break;
358
        }
359
 
2832 svoboda 360
        if (rc >= 0) {
2867 svoboda 361
            switch (ev_type) {
2901 svoboda 362
            case UDEBUG_EVENT_SYSCALL_B:
363
                event_syscall_b(thread_id, thread_hash, val0, (int)val1);
2867 svoboda 364
                break;
2901 svoboda 365
            case UDEBUG_EVENT_SYSCALL_E:
366
                event_syscall_e(thread_id, thread_hash, val0, (int)val1);
367
                break;
2898 svoboda 368
            case UDEBUG_EVENT_STOP:
369
                printf("stop event\n");
370
                printf("waiting for resume\n");
371
                while (paused) {
372
                    usleep(1000000);
373
                    fibril_yield();
374
                    printf(".");
375
                }
376
                printf("resumed\n");
377
                break;
2903 svoboda 378
            case UDEBUG_EVENT_THREAD_B:
379
                event_thread_b(val0);
2867 svoboda 380
                break;
2903 svoboda 381
            case UDEBUG_EVENT_THREAD_E:
382
                printf("thread 0x%x exited\n", val0);
383
                abort_trace = 1;
384
                break;
2867 svoboda 385
            default:
386
                printf("unknown event type %d\n", ev_type);
387
                break;
388
            }
2832 svoboda 389
        }
390
 
391
    }
2835 svoboda 392
 
2868 svoboda 393
    printf("trace_loop(%d) exiting\n", thread_id);
2946 svoboda 394
    return 0;
2832 svoboda 395
}
396
 
2868 svoboda 397
void thread_trace_start(unsigned thread_hash)
398
{
399
    fid_t fid;
2832 svoboda 400
 
2898 svoboda 401
    thash = thread_hash;
402
 
2868 svoboda 403
    fid = fibril_create(trace_loop, (void *)thread_hash);
404
    if (fid == 0) {
405
        printf("Warning: Failed creating fibril\n");
406
    }
407
    fibril_add_ready(fid);
408
}
409
 
3428 svoboda 410
static void trace_active_task(task_id_t task_id)
2832 svoboda 411
{
412
    int i;
413
    int rc;
2898 svoboda 414
    int c;
2832 svoboda 415
 
416
    printf("Syscall Tracer\n");
417
 
3428 svoboda 418
    rc = task_connect(task_id);
2832 svoboda 419
    if (rc < 0) {
3428 svoboda 420
        printf("Failed to connect to task %lld\n", task_id);
2832 svoboda 421
        return;
422
    }
423
 
3428 svoboda 424
    printf("Connected to task %lld\n", task_id);
2832 svoboda 425
 
2873 svoboda 426
    ipcp_init();
2882 svoboda 427
    ipcp_connection_set(1, 0, proto_console);
2873 svoboda 428
 
2832 svoboda 429
    rc = get_thread_list();
430
    if (rc < 0) {
431
        printf("Failed to get thread list (error %d)\n", rc);
432
        return;
433
    }
434
 
2850 svoboda 435
    abort_trace = 0;
2832 svoboda 436
 
2850 svoboda 437
    for (i = 0; i < n_threads; i++) {
2868 svoboda 438
        thread_trace_start(thread_hash_buf[i]);
2850 svoboda 439
    }
440
 
2898 svoboda 441
    while(1) {
442
        c = getchar();
443
        if (c == 'q') break;
444
        if (c == 'p') {
445
            paused = 1;
2904 svoboda 446
            rc = udebug_stop(phoneid, thash);
2898 svoboda 447
            printf("stop -> %d\n", rc);
448
        }
449
        if (c == 'r') {
450
            paused = 0;
451
        }
452
    }
2850 svoboda 453
 
454
    printf("terminate debugging session...\n");
455
    abort_trace = 1;
2904 svoboda 456
    udebug_end(phoneid);
2850 svoboda 457
    ipc_hangup(phoneid);
458
 
2873 svoboda 459
    ipcp_cleanup();
460
 
2832 svoboda 461
    printf("done\n");
462
    return;
463
}
464
 
2878 svoboda 465
static void main_init(void)
2832 svoboda 466
{
2878 svoboda 467
    proto_t *p;
2880 svoboda 468
    oper_t *o;
2878 svoboda 469
 
2868 svoboda 470
    next_thread_id = 1;
2898 svoboda 471
    paused = 0;
2868 svoboda 472
 
2878 svoboda 473
    proto_init();
474
 
2880 svoboda 475
    p = proto_new("vfs");
2883 svoboda 476
    o = oper_new("read");
477
    proto_add_oper(p, VFS_READ, o);
478
    o = oper_new("write");
479
    proto_add_oper(p, VFS_WRITE, o);
480
    o = oper_new("truncate");
481
    proto_add_oper(p, VFS_TRUNCATE, o);
482
    o = oper_new("mount");
2880 svoboda 483
    proto_add_oper(p, VFS_MOUNT, o);
2883 svoboda 484
    o = oper_new("unmount");
485
    proto_add_oper(p, VFS_UNMOUNT, o);
2880 svoboda 486
 
2878 svoboda 487
    proto_register(SERVICE_VFS, p);
2882 svoboda 488
 
489
    p = proto_new("console");
2883 svoboda 490
    o = oper_new("getchar");
491
    proto_add_oper(p, CONSOLE_GETCHAR, o);
492
    o = oper_new("putchar");
2882 svoboda 493
    proto_add_oper(p, CONSOLE_PUTCHAR, o);
2883 svoboda 494
    o = oper_new("clear");
495
    proto_add_oper(p, CONSOLE_CLEAR, o);
496
    o = oper_new("goto");
497
    proto_add_oper(p, CONSOLE_GOTO, o);
498
    o = oper_new("getsize");
499
    proto_add_oper(p, CONSOLE_GETSIZE, o);
500
    o = oper_new("flush");
501
    proto_add_oper(p, CONSOLE_FLUSH, o);
502
    o = oper_new("set_style");
503
    proto_add_oper(p, CONSOLE_SET_STYLE, o);
3428 svoboda 504
    o = oper_new("cursor_visibility");
505
    proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
2883 svoboda 506
    o = oper_new("flush");
507
    proto_add_oper(p, CONSOLE_FLUSH, o);
2882 svoboda 508
 
509
    proto_console = p;
510
    proto_register(SERVICE_CONSOLE, p);
2878 svoboda 511
}
512
 
3428 svoboda 513
static void print_syntax()
2878 svoboda 514
{
3435 svoboda 515
    printf("syntax: trace <task_id>\n");
3428 svoboda 516
}
2878 svoboda 517
 
3428 svoboda 518
int main(int argc, char *argv[])
519
{
520
    task_id_t task_id;
521
    char *err_p;
522
 
523
    if (argc != 2) {
524
        printf("Mising argument\n");
525
        print_syntax();
526
        return 1;
2835 svoboda 527
    }
3428 svoboda 528
 
529
    task_id = strtol(argv[1], &err_p, 10);
530
 
531
    if (*err_p) {
532
        printf("Task ID syntax error\n");
533
        print_syntax();
534
        return 1;
535
    }
536
 
537
    main_init();
538
    trace_active_task(task_id);
2832 svoboda 539
}
540
 
541
/** @}
542
 */