Subversion Repositories HelenOS

Rev

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