Subversion Repositories HelenOS

Rev

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