Subversion Repositories HelenOS

Rev

Rev 2862 | Rev 2868 | 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
 
2867 svoboda 124
void event_syscall(unsigned thread_idx, unsigned sc_id, int sc_rc)
125
{
126
    unsigned sc_args[6];
127
    int rv_type;
128
    int rc;
129
 
130
    /* Read syscall arguments */
131
    rc = debug_args_read(phoneid, threadid_buf[thread_idx], sc_args);
132
 
133
    async_serialize_start();
134
 
135
    printf("[%d] ", thread_idx);
136
 
137
    if (rc < 0) {
138
        printf("error\n");
139
        async_serialize_end();
140
        return;
141
    }
142
 
143
    /* Print syscall name, id and arguments */
144
    printf("%s", syscall_desc[sc_id].name);
145
    print_sc_args(sc_args, syscall_desc[sc_id].n_args);
146
    rv_type = syscall_desc[sc_id].rv_type;
147
    print_sc_retval(sc_rc, rv_type);
148
 
149
    switch (sc_id) {
150
    case SYS_IPC_CALL_ASYNC_SLOW:
151
        sc_ipc_call_async_slow(sc_args);
152
        break;
153
    default:
154
        break;
155
    }
156
 
157
    async_serialize_end();
158
}
159
 
160
void event_new_thread(unsigned hash)
161
{
162
    async_serialize_start();
163
    printf("new thread, hash 0x%x\n", hash);
164
    async_serialize_end();
165
}
166
 
2850 svoboda 167
void trace_loop(void *thread_idx_arg)
2835 svoboda 168
{
2832 svoboda 169
    int rc;
170
    unsigned ev_type;
2850 svoboda 171
    unsigned thread_idx;
2867 svoboda 172
    unsigned val0, val1;
2832 svoboda 173
 
2850 svoboda 174
    thread_idx = (unsigned)thread_idx_arg;
175
    printf("trace_loop(%d)\n", thread_idx);
2835 svoboda 176
 
177
    while (!abort_trace) {
178
 
2867 svoboda 179
        /* Run thread until an event occurs */
2850 svoboda 180
        rc = debug_go(phoneid, threadid_buf[thread_idx],
2867 svoboda 181
            &ev_type, &val0, &val1);
2832 svoboda 182
 
2838 svoboda 183
        printf("rc = %d, ev_type=%d\n", rc, ev_type);
184
        if (ev_type == UDEBUG_EVENT_FINISHED) {
2850 svoboda 185
            printf("thread %u debugging finished\n", threadid_buf[thread_idx]);
2838 svoboda 186
            break;
187
        }
188
 
2832 svoboda 189
        if (rc >= 0) {
2867 svoboda 190
            switch (ev_type) {
191
            case UDEBUG_EVENT_SYSCALL:
192
                event_syscall(thread_idx, val0, (int)val1);
193
                break;
194
            case UDEBUG_EVENT_NEW_THREAD:
195
                event_new_thread(val0);
196
                break;
197
            default:
198
                printf("unknown event type %d\n", ev_type);
199
                break;
200
            }
2832 svoboda 201
        }
202
 
203
    }
2835 svoboda 204
 
2850 svoboda 205
    printf("trace_loop(%d) exiting\n", thread_idx);
2832 svoboda 206
}
207
 
208
 
209
void trace_active_task(void)
210
{
211
    int taskid;
212
    int i;
213
    int rc;
2850 svoboda 214
    fid_t fid;
2832 svoboda 215
 
216
    printf("Syscall Tracer\n");
217
    printf("Press 'c' to connect\n");
218
    while ((i = getchar()) != 'c')
219
        putchar(i);
220
 
2850 svoboda 221
    taskid = 14;
2832 svoboda 222
    rc = task_connect(taskid);
223
    if (rc < 0) {
224
        printf("Failed to connect to task %d\n", taskid);
225
        return;
226
    }
227
 
228
    printf("Connected to task %d\n", taskid);
229
 
230
    rc = get_thread_list();
231
    if (rc < 0) {
232
        printf("Failed to get thread list (error %d)\n", rc);
233
        return;
234
    }
235
 
2850 svoboda 236
    abort_trace = 0;
2832 svoboda 237
 
2850 svoboda 238
    for (i = 0; i < n_threads; i++) {
2853 svoboda 239
        fid = fibril_create(trace_loop, (void *)i);
2850 svoboda 240
        if (fid == 0) {
241
            printf("Warning: Failed creating fibril\n");
242
        }
243
        fibril_add_ready(fid);
244
    }
245
 
246
    getchar();
247
 
248
    printf("terminate debugging session...\n");
249
    abort_trace = 1;
2835 svoboda 250
    debug_end(phoneid);
2850 svoboda 251
    ipc_hangup(phoneid);
252
 
2832 svoboda 253
    printf("done\n");
254
    return;
255
}
256
 
257
int main(void)
258
{
2835 svoboda 259
    while (1) {
260
        trace_active_task();
261
    }
2832 svoboda 262
}
263
 
264
/** @}
265
 */