Subversion Repositories HelenOS

Rev

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