Subversion Repositories HelenOS

Rev

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