Subversion Repositories HelenOS

Rev

Rev 2854 | Rev 2867 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2832 svoboda 1
/** @addtogroup sctrace
2
 * @{
3
 */
4
/** @file
5
 */
6
 
7
#include <stdio.h>
8
#include <unistd.h>
9
#include <syscall.h>
10
#include <ipc/ipc.h>
2835 svoboda 11
#include <fibril.h>
12
#include <errno.h>
2838 svoboda 13
#include <udebug.h>
2862 svoboda 14
#include <async.h>
2832 svoboda 15
 
16
#include "syscalls.h"
17
#include "errors.h"
18
#include "debug_api.h"
19
 
20
#define TIDBUF_SIZE 64
21
unsigned threadid_buf[TIDBUF_SIZE];
2850 svoboda 22
unsigned n_threads;
2832 svoboda 23
 
24
int phoneid;
2835 svoboda 25
int abort_trace;
2832 svoboda 26
 
27
int task_connect(int taskid)
28
{
29
    int rc;
30
 
31
    printf("ipc_connect_task(%d)...\n", taskid);
32
    rc = ipc_connect_kbox(taskid);
33
    printf("-> %d\n", rc);
34
    phoneid = rc;
35
    if (rc < 0) return rc;
36
 
37
    printf("debug_begin()\n");
38
    rc = debug_begin(phoneid);
39
    printf("-> %d\n", rc);
40
    if (rc < 0) return rc;
41
 
42
    return 0;
43
}
44
 
45
int get_thread_list(void)
46
{
47
    int rc;
48
    int tb_copied;
49
    int tb_needed;
50
    int i;
51
 
52
 
53
    printf("send IPC_M_DEBUG_THREAD_READ message\n");
54
    rc = debug_thread_read(phoneid, (unsigned)threadid_buf,
55
        TIDBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
56
    printf("-> %d\n", rc);
57
    if (rc < 0) return rc;
58
 
2850 svoboda 59
    n_threads = tb_copied / sizeof(unsigned);
60
 
2832 svoboda 61
    printf("thread IDs:");
2850 svoboda 62
    for (i=0; i<n_threads; i++) {
2832 svoboda 63
        printf(" %u", threadid_buf[i]);
64
    }
65
    printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned));
66
 
67
    return 0;
68
}
69
 
70
void print_sc_retval(int retval, rv_type_t rv_type)
71
{
72
    printf (" -> ");
73
    if (rv_type == RV_INTEGER) {
74
        printf("%d", retval);
75
    } else if (rv_type == RV_HASH) {
76
        printf("0x%08x", retval);
77
    } else if (rv_type == RV_ERRNO) {
78
        if (retval >= -15 && retval <= 0) {
79
            printf("%d %s (%s)", retval,
80
                err_desc[retval].name,
81
                err_desc[retval].desc);
82
        } else {
83
            printf("%d", retval);
84
        }
85
    } else if (rv_type == RV_INT_ERRNO) {
86
        if (retval >= -15 && retval < 0) {
87
            printf("%d %s (%s)", retval,
88
                err_desc[retval].name,
89
                err_desc[retval].desc);
90
        } else {
91
            printf("%d", retval);
92
        }
93
    }
94
    putchar('\n');
95
}
96
 
97
void print_sc_args(unsigned *sc_args, int n)
98
{
99
    int i;
100
 
101
    putchar('(');
102
    if (n > 0) printf("%d", sc_args[0]);
103
    for (i=1; i<n; i++) {
104
        printf(", %d", sc_args[i]);
105
    }
106
    putchar(')');
107
}
108
 
109
void sc_ipc_call_async_slow(unsigned *sc_args)
110
{
111
    unsigned ipc_args[6];
112
    int rc;
113
 
114
    memset(ipc_args, 0, sizeof(ipc_args));
115
    rc = debug_mem_read(phoneid, ipc_args, sc_args[1], sizeof(ipc_args));
116
 
117
    if (rc >= 0) {
118
        printf("args: (%u, %u, %u, %u, %u, %u)\n",
119
            ipc_args[0], ipc_args[1], ipc_args[2],
120
            ipc_args[3], ipc_args[4], ipc_args[5]);
121
    }
122
}
123
 
2850 svoboda 124
void trace_loop(void *thread_idx_arg)
2835 svoboda 125
{
2832 svoboda 126
    int rc;
127
    unsigned sc_args[6];
128
    unsigned copied;
129
    unsigned ev_type;
130
    unsigned sc_id;
131
    int sc_rc;
132
    int rv_type;
2850 svoboda 133
    unsigned thread_idx;
2832 svoboda 134
 
2850 svoboda 135
    thread_idx = (unsigned)thread_idx_arg;
136
    printf("trace_loop(%d)\n", thread_idx);
2835 svoboda 137
 
138
    while (!abort_trace) {
139
 
2832 svoboda 140
        /* Run thread until a syscall is executed */
2850 svoboda 141
        rc = debug_go(phoneid, threadid_buf[thread_idx],
2832 svoboda 142
            &ev_type, &sc_id, &sc_rc);
143
 
2838 svoboda 144
        printf("rc = %d, ev_type=%d\n", rc, ev_type);
145
        if (ev_type == UDEBUG_EVENT_FINISHED) {
2850 svoboda 146
            printf("thread %u debugging finished\n", threadid_buf[thread_idx]);
2838 svoboda 147
            break;
148
        }
149
 
2832 svoboda 150
        /* Read syscall arguments */
151
        if (rc >= 0) {
2850 svoboda 152
            rc = debug_args_read(phoneid, threadid_buf[thread_idx],
2832 svoboda 153
                sc_args);
154
        }
155
 
2862 svoboda 156
        async_serialize_start();
2854 svoboda 157
 
2850 svoboda 158
        printf("[%d] ", thread_idx);
159
 
2832 svoboda 160
        /* Print syscall name, id and arguments */
161
        if (rc >= 0) {
162
            printf("%s", syscall_desc[sc_id].name);
163
            print_sc_args(sc_args, syscall_desc[sc_id].n_args);
164
            rv_type = syscall_desc[sc_id].rv_type;
165
            print_sc_retval(sc_rc, rv_type);
166
        }
167
 
2862 svoboda 168
        async_serialize_end();
2854 svoboda 169
 
2832 svoboda 170
        switch (sc_id) {
171
        case SYS_IPC_CALL_ASYNC_SLOW:
172
            sc_ipc_call_async_slow(sc_args);
173
            break;
174
        default:
175
            break;
176
        }
177
    }
2835 svoboda 178
 
2850 svoboda 179
    printf("trace_loop(%d) exiting\n", thread_idx);
2832 svoboda 180
}
181
 
182
 
183
void trace_active_task(void)
184
{
185
    int taskid;
186
    int i;
187
    int rc;
2850 svoboda 188
    fid_t fid;
2832 svoboda 189
 
190
    printf("Syscall Tracer\n");
191
    printf("Press 'c' to connect\n");
192
    while ((i = getchar()) != 'c')
193
        putchar(i);
194
 
2850 svoboda 195
    taskid = 14;
2832 svoboda 196
    rc = task_connect(taskid);
197
    if (rc < 0) {
198
        printf("Failed to connect to task %d\n", taskid);
199
        return;
200
    }
201
 
202
    printf("Connected to task %d\n", taskid);
203
 
204
    rc = get_thread_list();
205
    if (rc < 0) {
206
        printf("Failed to get thread list (error %d)\n", rc);
207
        return;
208
    }
209
 
2850 svoboda 210
    abort_trace = 0;
2832 svoboda 211
 
2850 svoboda 212
    for (i = 0; i < n_threads; i++) {
2853 svoboda 213
        fid = fibril_create(trace_loop, (void *)i);
2850 svoboda 214
        if (fid == 0) {
215
            printf("Warning: Failed creating fibril\n");
216
        }
217
        fibril_add_ready(fid);
218
    }
219
 
220
    getchar();
221
 
222
    printf("terminate debugging session...\n");
223
    abort_trace = 1;
2835 svoboda 224
    debug_end(phoneid);
2850 svoboda 225
    ipc_hangup(phoneid);
226
 
2832 svoboda 227
    printf("done\n");
228
    return;
229
}
230
 
231
int main(void)
232
{
2835 svoboda 233
    while (1) {
234
        trace_active_task();
235
    }
2832 svoboda 236
}
237
 
238
/** @}
239
 */