Subversion Repositories HelenOS

Rev

Rev 3444 | Rev 3452 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3444 Rev 3447
1
/*
1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup trace
29
/** @addtogroup trace
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <stdio.h>
35
#include <stdio.h>
36
#include <stdlib.h>
36
#include <stdlib.h>
37
#include <unistd.h>
37
#include <unistd.h>
38
#include <syscall.h>
38
#include <syscall.h>
39
#include <ipc/ipc.h>
39
#include <ipc/ipc.h>
40
#include <fibril.h>
40
#include <fibril.h>
41
#include <errno.h>
41
#include <errno.h>
42
#include <udebug.h>
42
#include <udebug.h>
43
#include <async.h>
43
#include <async.h>
-
 
44
#include <task.h>
44
 
45
 
45
// Temporary: service and method names
46
// Temporary: service and method names
46
#include "proto.h"
47
#include "proto.h"
47
#include <ipc/services.h>
48
#include <ipc/services.h>
48
#include "../../srv/vfs/vfs.h"
49
#include "../../srv/vfs/vfs.h"
49
#include "../../srv/console/console.h"
50
#include "../../srv/console/console.h"
50
 
51
 
51
#include "syscalls.h"
52
#include "syscalls.h"
52
#include "ipcp.h"
53
#include "ipcp.h"
53
#include "errors.h"
54
#include "errors.h"
54
#include "trace.h"
55
#include "trace.h"
55
 
56
 
56
#define THBUF_SIZE 64
57
#define THBUF_SIZE 64
57
unsigned thread_hash_buf[THBUF_SIZE];
58
unsigned thread_hash_buf[THBUF_SIZE];
58
unsigned n_threads;
59
unsigned n_threads;
59
 
60
 
60
int next_thread_id;
61
int next_thread_id;
61
 
62
 
62
int phoneid;
63
int phoneid;
63
int abort_trace;
64
int abort_trace;
64
 
65
 
65
unsigned thash;
66
unsigned thash;
66
volatile int paused;
67
volatile int paused;
67
 
68
 
68
void thread_trace_start(unsigned thread_hash);
69
void thread_trace_start(unsigned thread_hash);
69
 
70
 
70
static proto_t *proto_console;
71
static proto_t *proto_console;
71
static task_id_t task_id;
72
static task_id_t task_id;
72
 
73
 
73
/** Combination of events/data to print. */
74
/** Combination of events/data to print. */
74
display_mask_t display_mask;
75
display_mask_t display_mask;
75
 
76
 
76
static int task_connect(task_id_t task_id)
77
static int task_connect(task_id_t task_id)
77
{
78
{
78
    int rc;
79
    int rc;
79
 
80
 
80
    rc = ipc_connect_kbox(task_id);
81
    rc = ipc_connect_kbox(task_id);
81
 
82
 
82
    if (rc == ENOTSUP) {
83
    if (rc == ENOTSUP) {
83
        printf("You do not have userspace debugging support "
84
        printf("You do not have userspace debugging support "
84
            "compiled in the kernel.\n");
85
            "compiled in the kernel.\n");
85
        printf("Compile kernel with 'Support for userspace debuggers' "
86
        printf("Compile kernel with 'Support for userspace debuggers' "
86
            "(CONFIG_UDEBUG) enabled.\n");
87
            "(CONFIG_UDEBUG) enabled.\n");
87
        return rc;
88
        return rc;
88
    }
89
    }
89
 
90
 
90
    if (rc < 0) {
91
    if (rc < 0) {
91
        printf("Error connecting\n");
92
        printf("Error connecting\n");
92
        printf("ipc_connect_task(%lld) -> %d ", task_id, rc);
93
        printf("ipc_connect_task(%lld) -> %d ", task_id, rc);
93
        return rc;
94
        return rc;
94
    }
95
    }
95
 
96
 
96
    phoneid = rc;
97
    phoneid = rc;
97
 
98
 
98
    rc = udebug_begin(phoneid);
99
    rc = udebug_begin(phoneid);
99
    if (rc < 0) {
100
    if (rc < 0) {
100
        printf("udebug_begin() -> %d\n", rc);
101
        printf("udebug_begin() -> %d\n", rc);
101
        return rc;
102
        return rc;
102
    }
103
    }
103
 
104
 
104
    rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL);
105
    rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL);
105
    if (rc < 0) {
106
    if (rc < 0) {
106
        printf("udebug_set_evmask(0x%x) -> %d\n ", UDEBUG_EM_ALL, rc);
107
        printf("udebug_set_evmask(0x%x) -> %d\n ", UDEBUG_EM_ALL, rc);
107
        return rc;
108
        return rc;
108
    }
109
    }
109
 
110
 
110
    return 0;
111
    return 0;
111
}
112
}
112
 
113
 
113
static int get_thread_list(void)
114
static int get_thread_list(void)
114
{
115
{
115
    int rc;
116
    int rc;
116
    size_t tb_copied;
117
    size_t tb_copied;
117
    size_t tb_needed;
118
    size_t tb_needed;
118
    int i;
119
    int i;
119
 
120
 
120
    rc = udebug_thread_read(phoneid, thread_hash_buf,
121
    rc = udebug_thread_read(phoneid, thread_hash_buf,
121
        THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
122
        THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
122
    if (rc < 0) {
123
    if (rc < 0) {
123
        printf("udebug_thread_read() -> %d\n", rc);
124
        printf("udebug_thread_read() -> %d\n", rc);
124
        return rc;
125
        return rc;
125
    }
126
    }
126
 
127
 
127
    n_threads = tb_copied / sizeof(unsigned);
128
    n_threads = tb_copied / sizeof(unsigned);
128
 
129
 
129
    printf("Threads:");
130
    printf("Threads:");
130
    for (i = 0; i < n_threads; i++) {
131
    for (i = 0; i < n_threads; i++) {
131
        printf(" [%d] (hash 0x%u)", 1+i, thread_hash_buf[i]);
132
        printf(" [%d] (hash 0x%x)", 1+i, thread_hash_buf[i]);
132
    }
133
    }
133
    printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned));
134
    printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned));
134
 
135
 
135
    return 0;
136
    return 0;
136
}
137
}
137
 
138
 
138
static void print_sc_retval(int retval, rv_type_t rv_type)
139
static void print_sc_retval(int retval, rv_type_t rv_type)
139
{
140
{
140
    printf (" -> ");
141
    printf (" -> ");
141
    if (rv_type == RV_INTEGER) {
142
    if (rv_type == RV_INTEGER) {
142
        printf("%d", retval);
143
        printf("%d", retval);
143
    } else if (rv_type == RV_HASH) {
144
    } else if (rv_type == RV_HASH) {
144
        printf("0x%08x", retval);
145
        printf("0x%08x", retval);
145
    } else if (rv_type == RV_ERRNO) {
146
    } else if (rv_type == RV_ERRNO) {
146
        if (retval >= -15 && retval <= 0) {
147
        if (retval >= -15 && retval <= 0) {
147
            printf("%d %s (%s)", retval,
148
            printf("%d %s (%s)", retval,
148
                err_desc[retval].name,
149
                err_desc[retval].name,
149
                err_desc[retval].desc);
150
                err_desc[retval].desc);
150
        } else {
151
        } else {
151
            printf("%d", retval);
152
            printf("%d", retval);
152
        }
153
        }
153
    } else if (rv_type == RV_INT_ERRNO) {
154
    } else if (rv_type == RV_INT_ERRNO) {
154
        if (retval >= -15 && retval < 0) {
155
        if (retval >= -15 && retval < 0) {
155
            printf("%d %s (%s)", retval,
156
            printf("%d %s (%s)", retval,
156
                err_desc[retval].name,
157
                err_desc[retval].name,
157
                err_desc[retval].desc);
158
                err_desc[retval].desc);
158
        } else {
159
        } else {
159
            printf("%d", retval);
160
            printf("%d", retval);
160
        }
161
        }
161
    }
162
    }
162
    putchar('\n');
163
    putchar('\n');
163
}
164
}
164
 
165
 
165
static void print_sc_args(unsigned *sc_args, int n)
166
static void print_sc_args(unsigned *sc_args, int n)
166
{
167
{
167
    int i;
168
    int i;
168
 
169
 
169
    putchar('(');
170
    putchar('(');
170
    if (n > 0) printf("%d", sc_args[0]);
171
    if (n > 0) printf("%d", sc_args[0]);
171
    for (i=1; i<n; i++) {
172
    for (i=1; i<n; i++) {
172
        printf(", %d", sc_args[i]);
173
        printf(", %d", sc_args[i]);
173
    }
174
    }
174
    putchar(')');
175
    putchar(')');
175
}
176
}
176
 
177
 
177
static void sc_ipc_call_async_fast(unsigned *sc_args, int sc_rc)
178
static void sc_ipc_call_async_fast(unsigned *sc_args, int sc_rc)
178
{
179
{
179
    ipc_call_t call;
180
    ipc_call_t call;
180
    int phoneid;
181
    int phoneid;
181
   
182
   
182
    if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
183
    if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
183
        return;
184
        return;
184
 
185
 
185
    phoneid = sc_args[0];
186
    phoneid = sc_args[0];
186
 
187
 
187
    IPC_SET_METHOD(call, sc_args[1]);
188
    IPC_SET_METHOD(call, sc_args[1]);
188
    IPC_SET_ARG1(call, sc_args[2]);
189
    IPC_SET_ARG1(call, sc_args[2]);
189
    IPC_SET_ARG2(call, sc_args[3]);
190
    IPC_SET_ARG2(call, sc_args[3]);
190
    IPC_SET_ARG3(call, sc_args[4]);
191
    IPC_SET_ARG3(call, sc_args[4]);
191
    IPC_SET_ARG4(call, sc_args[5]);
192
    IPC_SET_ARG4(call, sc_args[5]);
192
    IPC_SET_ARG5(call, 0);
193
    IPC_SET_ARG5(call, 0);
193
 
194
 
194
    ipcp_call_out(phoneid, &call, sc_rc);
195
    ipcp_call_out(phoneid, &call, sc_rc);
195
}
196
}
196
 
197
 
197
static void sc_ipc_call_async_slow(unsigned *sc_args, int sc_rc)
198
static void sc_ipc_call_async_slow(unsigned *sc_args, int sc_rc)
198
{
199
{
199
    ipc_call_t call;
200
    ipc_call_t call;
200
    int rc;
201
    int rc;
201
 
202
 
202
    if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
203
    if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
203
        return;
204
        return;
204
 
205
 
205
    memset(&call, 0, sizeof(call));
206
    memset(&call, 0, sizeof(call));
206
    rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
207
    rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
207
 
208
 
208
    if (rc >= 0) {
209
    if (rc >= 0) {
209
        ipcp_call_out(sc_args[0], &call, sc_rc);
210
        ipcp_call_out(sc_args[0], &call, sc_rc);
210
    }
211
    }
211
}
212
}
212
 
213
 
213
static void sc_ipc_call_sync_fast(unsigned *sc_args)
214
static void sc_ipc_call_sync_fast(unsigned *sc_args)
214
{
215
{
215
    ipc_call_t question, reply;
216
    ipc_call_t question, reply;
216
    int rc;
217
    int rc;
217
    int phoneidx;
218
    int phoneidx;
218
 
219
 
219
//  printf("sc_ipc_call_sync_fast()\n");
220
//  printf("sc_ipc_call_sync_fast()\n");
220
    phoneidx = sc_args[0];
221
    phoneidx = sc_args[0];
221
 
222
 
222
    IPC_SET_METHOD(question, sc_args[1]);
223
    IPC_SET_METHOD(question, sc_args[1]);
223
    IPC_SET_ARG1(question, sc_args[2]);
224
    IPC_SET_ARG1(question, sc_args[2]);
224
    IPC_SET_ARG2(question, sc_args[3]);
225
    IPC_SET_ARG2(question, sc_args[3]);
225
    IPC_SET_ARG3(question, sc_args[4]);
226
    IPC_SET_ARG3(question, sc_args[4]);
226
    IPC_SET_ARG4(question, 0);
227
    IPC_SET_ARG4(question, 0);
227
    IPC_SET_ARG5(question, 0);
228
    IPC_SET_ARG5(question, 0);
228
 
229
 
229
//  printf("memset\n");
230
//  printf("memset\n");
230
    memset(&reply, 0, sizeof(reply));
231
    memset(&reply, 0, sizeof(reply));
231
//  printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
232
//  printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
232
//      phoneid, &reply.args, sc_args[5], sizeof(reply.args));
233
//      phoneid, &reply.args, sc_args[5], sizeof(reply.args));
233
    rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
234
    rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
234
//  printf("dmr->%d\n", rc);
235
//  printf("dmr->%d\n", rc);
235
    if (rc < 0) return;
236
    if (rc < 0) return;
236
 
237
 
237
//  printf("call ipc_call_sync\n");
238
//  printf("call ipc_call_sync\n");
238
    ipcp_call_sync(phoneidx, &question, &reply);
239
    ipcp_call_sync(phoneidx, &question, &reply);
239
}
240
}
240
 
241
 
241
static void sc_ipc_call_sync_slow(unsigned *sc_args)
242
static void sc_ipc_call_sync_slow(unsigned *sc_args)
242
{
243
{
243
    ipc_call_t question, reply;
244
    ipc_call_t question, reply;
244
    int rc;
245
    int rc;
245
 
246
 
246
    memset(&question, 0, sizeof(question));
247
    memset(&question, 0, sizeof(question));
247
    rc = udebug_mem_read(phoneid, &question.args, sc_args[1], sizeof(question.args));
248
    rc = udebug_mem_read(phoneid, &question.args, sc_args[1], sizeof(question.args));
248
    printf("dmr->%d\n", rc);
249
    printf("dmr->%d\n", rc);
249
    if (rc < 0) return;
250
    if (rc < 0) return;
250
 
251
 
251
    memset(&reply, 0, sizeof(reply));
252
    memset(&reply, 0, sizeof(reply));
252
    rc = udebug_mem_read(phoneid, &reply.args, sc_args[2], sizeof(reply.args));
253
    rc = udebug_mem_read(phoneid, &reply.args, sc_args[2], sizeof(reply.args));
253
    printf("dmr->%d\n", rc);
254
    printf("dmr->%d\n", rc);
254
    if (rc < 0) return;
255
    if (rc < 0) return;
255
 
256
 
256
    ipcp_call_sync(sc_args[0], &question, &reply);
257
    ipcp_call_sync(sc_args[0], &question, &reply);
257
}
258
}
258
 
259
 
259
static void sc_ipc_wait(unsigned *sc_args, int sc_rc)
260
static void sc_ipc_wait(unsigned *sc_args, int sc_rc)
260
{
261
{
261
    ipc_call_t call;
262
    ipc_call_t call;
262
    int rc;
263
    int rc;
263
 
264
 
264
    if (sc_rc == 0) return;
265
    if (sc_rc == 0) return;
265
 
266
 
266
    memset(&call, 0, sizeof(call));
267
    memset(&call, 0, sizeof(call));
267
    rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
268
    rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
268
//  printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
269
//  printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
269
//      phoneid, (int)&call, sc_args[0], sizeof(call), rc);
270
//      phoneid, (int)&call, sc_args[0], sizeof(call), rc);
270
 
271
 
271
    if (rc >= 0) {
272
    if (rc >= 0) {
272
        ipcp_call_in(&call, sc_rc);
273
        ipcp_call_in(&call, sc_rc);
273
    }
274
    }
274
}
275
}
275
 
276
 
276
static void event_syscall_b(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
277
static void event_syscall_b(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
277
{
278
{
278
    unsigned sc_args[6];
279
    unsigned sc_args[6];
279
    int rc;
280
    int rc;
280
 
281
 
281
    /* Read syscall arguments */
282
    /* Read syscall arguments */
282
    rc = udebug_args_read(phoneid, thread_hash, sc_args);
283
    rc = udebug_args_read(phoneid, thread_hash, sc_args);
283
 
284
 
284
    async_serialize_start();
285
    async_serialize_start();
285
 
286
 
286
//  printf("[%d] ", thread_id);
287
//  printf("[%d] ", thread_id);
287
 
288
 
288
    if (rc < 0) {
289
    if (rc < 0) {
289
        printf("error\n");
290
        printf("error\n");
290
        async_serialize_end();
291
        async_serialize_end();
291
        return;
292
        return;
292
    }
293
    }
293
 
294
 
294
    if ((display_mask & DM_SYSCALL) != 0) {
295
    if ((display_mask & DM_SYSCALL) != 0) {
295
        /* Print syscall name and arguments */
296
        /* Print syscall name and arguments */
296
        printf("%s", syscall_desc[sc_id].name);
297
        printf("%s", syscall_desc[sc_id].name);
297
        print_sc_args(sc_args, syscall_desc[sc_id].n_args);
298
        print_sc_args(sc_args, syscall_desc[sc_id].n_args);
298
    }
299
    }
299
 
300
 
300
    async_serialize_end();
301
    async_serialize_end();
301
}
302
}
302
 
303
 
303
static void event_syscall_e(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
304
static void event_syscall_e(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
304
{
305
{
305
    unsigned sc_args[6];
306
    unsigned sc_args[6];
306
    int rv_type;
307
    int rv_type;
307
    int rc;
308
    int rc;
308
 
309
 
309
    /* Read syscall arguments */
310
    /* Read syscall arguments */
310
    rc = udebug_args_read(phoneid, thread_hash, sc_args);
311
    rc = udebug_args_read(phoneid, thread_hash, sc_args);
311
 
312
 
312
    async_serialize_start();
313
    async_serialize_start();
313
 
314
 
314
//  printf("[%d] ", thread_id);
315
//  printf("[%d] ", thread_id);
315
 
316
 
316
    if (rc < 0) {
317
    if (rc < 0) {
317
        printf("error\n");
318
        printf("error\n");
318
        async_serialize_end();
319
        async_serialize_end();
319
        return;
320
        return;
320
    }
321
    }
321
 
322
 
322
    if ((display_mask & DM_SYSCALL) != 0) {
323
    if ((display_mask & DM_SYSCALL) != 0) {
323
        /* Print syscall return value */
324
        /* Print syscall return value */
324
        rv_type = syscall_desc[sc_id].rv_type;
325
        rv_type = syscall_desc[sc_id].rv_type;
325
        print_sc_retval(sc_rc, rv_type);
326
        print_sc_retval(sc_rc, rv_type);
326
    }
327
    }
327
 
328
 
328
    switch (sc_id) {
329
    switch (sc_id) {
329
    case SYS_IPC_CALL_ASYNC_FAST:
330
    case SYS_IPC_CALL_ASYNC_FAST:
330
        sc_ipc_call_async_fast(sc_args, sc_rc);
331
        sc_ipc_call_async_fast(sc_args, sc_rc);
331
        break;
332
        break;
332
    case SYS_IPC_CALL_ASYNC_SLOW:
333
    case SYS_IPC_CALL_ASYNC_SLOW:
333
        sc_ipc_call_async_slow(sc_args, sc_rc);
334
        sc_ipc_call_async_slow(sc_args, sc_rc);
334
        break;
335
        break;
335
    case SYS_IPC_CALL_SYNC_FAST:
336
    case SYS_IPC_CALL_SYNC_FAST:
336
        sc_ipc_call_sync_fast(sc_args);
337
        sc_ipc_call_sync_fast(sc_args);
337
        break;
338
        break;
338
    case SYS_IPC_CALL_SYNC_SLOW:
339
    case SYS_IPC_CALL_SYNC_SLOW:
339
        sc_ipc_call_sync_slow(sc_args);
340
        sc_ipc_call_sync_slow(sc_args);
340
        break;
341
        break;
341
    case SYS_IPC_WAIT:
342
    case SYS_IPC_WAIT:
342
        sc_ipc_wait(sc_args, sc_rc);
343
        sc_ipc_wait(sc_args, sc_rc);
343
        break;
344
        break;
344
    default:
345
    default:
345
        break;
346
        break;
346
    }
347
    }
347
 
348
 
348
    async_serialize_end();
349
    async_serialize_end();
349
}
350
}
350
 
351
 
351
static void event_thread_b(unsigned hash)
352
static void event_thread_b(unsigned hash)
352
{
353
{
353
    async_serialize_start();
354
    async_serialize_start();
354
    printf("New thread, hash 0x%x\n", hash);
355
    printf("New thread, hash 0x%x\n", hash);
355
    async_serialize_end();
356
    async_serialize_end();
356
 
357
 
357
    thread_trace_start(hash);
358
    thread_trace_start(hash);
358
}
359
}
359
 
360
 
360
static int trace_loop(void *thread_hash_arg)
361
static int trace_loop(void *thread_hash_arg)
361
{
362
{
362
    int rc;
363
    int rc;
363
    unsigned ev_type;
364
    unsigned ev_type;
364
    unsigned thread_hash;
365
    unsigned thread_hash;
365
    unsigned thread_id;
366
    unsigned thread_id;
366
    unsigned val0, val1;
367
    unsigned val0, val1;
367
 
368
 
368
    thread_hash = (unsigned)thread_hash_arg;
369
    thread_hash = (unsigned)thread_hash_arg;
369
    thread_id = next_thread_id++;
370
    thread_id = next_thread_id++;
370
 
371
 
371
    printf("Start tracing thread [%d] (hash 0x%x)\n", thread_id, thread_hash);
372
    printf("Start tracing thread [%d] (hash 0x%x)\n", thread_id, thread_hash);
372
 
373
 
373
    while (!abort_trace) {
374
    while (!abort_trace) {
374
 
375
 
375
        /* Run thread until an event occurs */
376
        /* Run thread until an event occurs */
376
        rc = udebug_go(phoneid, thread_hash,
377
        rc = udebug_go(phoneid, thread_hash,
377
            &ev_type, &val0, &val1);
378
            &ev_type, &val0, &val1);
378
 
379
 
379
//      printf("rc = %d, ev_type=%d\n", rc, ev_type);
380
//      printf("rc = %d, ev_type=%d\n", rc, ev_type);
380
        if (ev_type == UDEBUG_EVENT_FINISHED) {
381
        if (ev_type == UDEBUG_EVENT_FINISHED) {
381
            /* Done tracing this thread */
382
            /* Done tracing this thread */
382
            break;
383
            break;
383
        }
384
        }
384
 
385
 
385
        if (rc >= 0) {
386
        if (rc >= 0) {
386
            switch (ev_type) {
387
            switch (ev_type) {
387
            case UDEBUG_EVENT_SYSCALL_B:
388
            case UDEBUG_EVENT_SYSCALL_B:
388
                event_syscall_b(thread_id, thread_hash, val0, (int)val1);
389
                event_syscall_b(thread_id, thread_hash, val0, (int)val1);
389
                break;
390
                break;
390
            case UDEBUG_EVENT_SYSCALL_E:
391
            case UDEBUG_EVENT_SYSCALL_E:
391
                event_syscall_e(thread_id, thread_hash, val0, (int)val1);
392
                event_syscall_e(thread_id, thread_hash, val0, (int)val1);
392
                break;
393
                break;
393
            case UDEBUG_EVENT_STOP:
394
            case UDEBUG_EVENT_STOP:
394
                printf("Stop event\n");
395
                printf("Stop event\n");
395
                printf("Waiting for resume\n");
396
                printf("Waiting for resume\n");
396
                while (paused) {
397
                while (paused) {
397
                    usleep(1000000);
398
                    usleep(1000000);
398
                    fibril_yield();
399
                    fibril_yield();
399
                    printf(".");
400
                    printf(".");
400
                }
401
                }
401
                printf("Resumed\n");
402
                printf("Resumed\n");
402
                break;
403
                break;
403
            case UDEBUG_EVENT_THREAD_B:
404
            case UDEBUG_EVENT_THREAD_B:
404
                event_thread_b(val0);
405
                event_thread_b(val0);
405
                break;
406
                break;
406
            case UDEBUG_EVENT_THREAD_E:
407
            case UDEBUG_EVENT_THREAD_E:
407
                printf("Thread 0x%x exited\n", val0);
408
                printf("Thread 0x%x exited\n", val0);
408
                abort_trace = 1;
409
                abort_trace = 1;
409
                break;
410
                break;
410
            default:
411
            default:
411
                printf("Unknown event type %d\n", ev_type);
412
                printf("Unknown event type %d\n", ev_type);
412
                break;
413
                break;
413
            }
414
            }
414
        }
415
        }
415
 
416
 
416
    }
417
    }
417
 
418
 
418
    printf("Finished tracing thread [%d]\n", thread_id);
419
    printf("Finished tracing thread [%d]\n", thread_id);
419
    return 0;
420
    return 0;
420
}
421
}
421
 
422
 
422
void thread_trace_start(unsigned thread_hash)
423
void thread_trace_start(unsigned thread_hash)
423
{
424
{
424
    fid_t fid;
425
    fid_t fid;
425
 
426
 
426
    thash = thread_hash;
427
    thash = thread_hash;
427
 
428
 
428
    fid = fibril_create(trace_loop, (void *)thread_hash);
429
    fid = fibril_create(trace_loop, (void *)thread_hash);
429
    if (fid == 0) {
430
    if (fid == 0) {
430
        printf("Warning: Failed creating fibril\n");
431
        printf("Warning: Failed creating fibril\n");
431
    }
432
    }
432
    fibril_add_ready(fid);
433
    fibril_add_ready(fid);
433
}
434
}
434
 
435
 
435
static void trace_active_task(task_id_t task_id)
436
static void trace_active_task(task_id_t task_id)
436
{
437
{
437
    int i;
438
    int i;
438
    int rc;
439
    int rc;
439
    int c;
440
    int c;
440
 
441
 
441
    rc = task_connect(task_id);
442
    rc = task_connect(task_id);
442
    if (rc < 0) {
443
    if (rc < 0) {
443
        printf("Failed to connect to task %lld\n", task_id);
444
        printf("Failed to connect to task %lld\n", task_id);
444
        return;
445
        return;
445
    }
446
    }
446
 
447
 
447
    printf("Connected to task %lld\n", task_id);
448
    printf("Connected to task %lld\n", task_id);
448
 
449
 
449
    ipcp_init();
450
    ipcp_init();
450
 
451
 
451
    /*
452
    /*
452
     * User apps now typically have console on phone 3.
453
     * User apps now typically have console on phone 3.
453
     * (Phones 1 and 2 are used by the loader).
454
     * (Phones 1 and 2 are used by the loader).
454
     */
455
     */
455
    ipcp_connection_set(3, 0, proto_console);
456
    ipcp_connection_set(3, 0, proto_console);
456
 
457
 
457
    rc = get_thread_list();
458
    rc = get_thread_list();
458
    if (rc < 0) {
459
    if (rc < 0) {
459
        printf("Failed to get thread list (error %d)\n", rc);
460
        printf("Failed to get thread list (error %d)\n", rc);
460
        return;
461
        return;
461
    }
462
    }
462
 
463
 
463
    abort_trace = 0;
464
    abort_trace = 0;
464
 
465
 
465
    for (i = 0; i < n_threads; i++) {
466
    for (i = 0; i < n_threads; i++) {
466
        thread_trace_start(thread_hash_buf[i]);
467
        thread_trace_start(thread_hash_buf[i]);
467
    }
468
    }
468
 
469
 
469
    while(1) {
470
    while(1) {
470
        c = getchar();
471
        c = getchar();
471
        if (c == 'q') break;
472
        if (c == 'q') break;
472
        if (c == 'p') {
473
        if (c == 'p') {
473
            paused = 1;
474
            paused = 1;
474
            rc = udebug_stop(phoneid, thash);
475
            rc = udebug_stop(phoneid, thash);
475
            printf("stop -> %d\n", rc);
476
            printf("stop -> %d\n", rc);
476
        }
477
        }
477
        if (c == 'r') {
478
        if (c == 'r') {
478
            paused = 0;
479
            paused = 0;
479
        }
480
        }
480
    }
481
    }
481
 
482
 
482
    printf("\nTerminate debugging session...\n");
483
    printf("\nTerminate debugging session...\n");
483
    abort_trace = 1;
484
    abort_trace = 1;
484
    udebug_end(phoneid);
485
    udebug_end(phoneid);
485
    ipc_hangup(phoneid);
486
    ipc_hangup(phoneid);
486
 
487
 
487
    ipcp_cleanup();
488
    ipcp_cleanup();
488
 
489
 
489
    printf("Done\n");
490
    printf("Done\n");
490
    return;
491
    return;
491
}
492
}
492
 
493
 
493
static void main_init(void)
494
static void main_init(void)
494
{
495
{
495
    proto_t *p;
496
    proto_t *p;
496
    oper_t *o;
497
    oper_t *o;
497
 
498
 
498
    next_thread_id = 1;
499
    next_thread_id = 1;
499
    paused = 0;
500
    paused = 0;
500
 
501
 
501
    proto_init();
502
    proto_init();
502
 
503
 
503
    p = proto_new("vfs");
504
    p = proto_new("vfs");
504
    o = oper_new("read");
505
    o = oper_new("read");
505
    proto_add_oper(p, VFS_READ, o);
506
    proto_add_oper(p, VFS_READ, o);
506
    o = oper_new("write");
507
    o = oper_new("write");
507
    proto_add_oper(p, VFS_WRITE, o);
508
    proto_add_oper(p, VFS_WRITE, o);
508
    o = oper_new("truncate");
509
    o = oper_new("truncate");
509
    proto_add_oper(p, VFS_TRUNCATE, o);
510
    proto_add_oper(p, VFS_TRUNCATE, o);
510
    o = oper_new("mount");
511
    o = oper_new("mount");
511
    proto_add_oper(p, VFS_MOUNT, o);
512
    proto_add_oper(p, VFS_MOUNT, o);
512
    o = oper_new("unmount");
513
    o = oper_new("unmount");
513
    proto_add_oper(p, VFS_UNMOUNT, o);
514
    proto_add_oper(p, VFS_UNMOUNT, o);
514
 
515
 
515
    proto_register(SERVICE_VFS, p);
516
    proto_register(SERVICE_VFS, p);
516
 
517
 
517
    p = proto_new("console");
518
    p = proto_new("console");
518
    o = oper_new("getchar");
519
    o = oper_new("getchar");
519
    proto_add_oper(p, CONSOLE_GETCHAR, o);
520
    proto_add_oper(p, CONSOLE_GETCHAR, o);
520
    o = oper_new("putchar");
521
    o = oper_new("putchar");
521
    proto_add_oper(p, CONSOLE_PUTCHAR, o);
522
    proto_add_oper(p, CONSOLE_PUTCHAR, o);
522
    o = oper_new("clear");
523
    o = oper_new("clear");
523
    proto_add_oper(p, CONSOLE_CLEAR, o);
524
    proto_add_oper(p, CONSOLE_CLEAR, o);
524
    o = oper_new("goto");
525
    o = oper_new("goto");
525
    proto_add_oper(p, CONSOLE_GOTO, o);
526
    proto_add_oper(p, CONSOLE_GOTO, o);
526
    o = oper_new("getsize");
527
    o = oper_new("getsize");
527
    proto_add_oper(p, CONSOLE_GETSIZE, o);
528
    proto_add_oper(p, CONSOLE_GETSIZE, o);
528
    o = oper_new("flush");
529
    o = oper_new("flush");
529
    proto_add_oper(p, CONSOLE_FLUSH, o);
530
    proto_add_oper(p, CONSOLE_FLUSH, o);
530
    o = oper_new("set_style");
531
    o = oper_new("set_style");
531
    proto_add_oper(p, CONSOLE_SET_STYLE, o);
532
    proto_add_oper(p, CONSOLE_SET_STYLE, o);
532
    o = oper_new("cursor_visibility");
533
    o = oper_new("cursor_visibility");
533
    proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
534
    proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
534
    o = oper_new("flush");
535
    o = oper_new("flush");
535
    proto_add_oper(p, CONSOLE_FLUSH, o);
536
    proto_add_oper(p, CONSOLE_FLUSH, o);
536
 
537
 
537
    proto_console = p;
538
    proto_console = p;
538
    proto_register(SERVICE_CONSOLE, p);
539
    proto_register(SERVICE_CONSOLE, p);
539
}
540
}
540
 
541
 
541
static void print_syntax()
542
static void print_syntax()
542
{
543
{
-
 
544
    printf("Syntax:\n");
-
 
545
    printf("\ttrace [+<events>] <executable> [<arg1> [...]]\n");
543
    printf("Syntax: trace [+<events>] <task_id>\n");
546
    printf("or\ttrace [+<events>] -t <task_id>\n");
544
    printf("Events: (default is +tp)\n");
547
    printf("Events: (default is +tp)\n");
545
    printf("\n");
548
    printf("\n");
546
    printf("\tt ... Thread creation and termination\n");
549
    printf("\tt ... Thread creation and termination\n");
547
    printf("\ts ... System calls\n");
550
    printf("\ts ... System calls\n");
548
    printf("\ti ... Low-level IPC\n");
551
    printf("\ti ... Low-level IPC\n");
549
    printf("\tp ... Protocol level\n");
552
    printf("\tp ... Protocol level\n");
550
    printf("\n");
553
    printf("\n");
-
 
554
    printf("Examples:\n");
-
 
555
    printf("\ttrace +s /app/tetris\n");
551
    printf("Example: trace +tsip 12\n");
556
    printf("\ttrace +tsip -t 12\n");
552
}
557
}
553
 
558
 
554
static display_mask_t parse_display_mask(char *text)
559
static display_mask_t parse_display_mask(char *text)
555
{
560
{
556
    display_mask_t dm;
561
    display_mask_t dm;
557
    char *c;
562
    char *c;
558
 
563
 
559
    c = text;
564
    c = text;
560
 
565
 
561
    while (*c) {
566
    while (*c) {
562
        switch (*c) {
567
        switch (*c) {
563
        case 't': dm = dm | DM_THREAD; break;
568
        case 't': dm = dm | DM_THREAD; break;
564
        case 's': dm = dm | DM_SYSCALL; break;
569
        case 's': dm = dm | DM_SYSCALL; break;
565
        case 'i': dm = dm | DM_IPC; break;
570
        case 'i': dm = dm | DM_IPC; break;
566
        case 'p': dm = dm | DM_SYSTEM | DM_USER; break;
571
        case 'p': dm = dm | DM_SYSTEM | DM_USER; break;
567
        default:
572
        default:
568
            printf("Unexpected event type '%c'\n", *c);
573
            printf("Unexpected event type '%c'\n", *c);
569
            exit(1);
574
            exit(1);
570
        }
575
        }
571
 
576
 
572
        ++c;
577
        ++c;
573
    }
578
    }
574
 
579
 
575
    return dm;
580
    return dm;
576
}
581
}
577
 
582
 
578
static int parse_args(int argc, char *argv[])
583
static int parse_args(int argc, char *argv[])
579
{
584
{
580
    char *arg;
585
    char *arg;
581
    char *err_p;
586
    char *err_p;
582
 
587
 
-
 
588
    task_id = 0;
-
 
589
 
583
    --argc; ++argv;
590
    --argc; ++argv;
584
 
591
 
585
    while (argc > 1) {
592
    while (argc > 0) {
586
        arg = *argv;
593
        arg = *argv;
587
        if (arg[0] == '+') {
594
        if (arg[0] == '+') {
588
            display_mask = parse_display_mask(&arg[1]);
595
            display_mask = parse_display_mask(&arg[1]);
-
 
596
        } else if (arg[0] == '-') {
-
 
597
            if (arg[1] == 't') {
-
 
598
                /* Trace an already running task */
-
 
599
                --argc; ++argv;
-
 
600
                task_id = strtol(*argv, &err_p, 10);
-
 
601
                if (*err_p) {
-
 
602
                    printf("Task ID syntax error\n");
-
 
603
                    print_syntax();
-
 
604
                    return -1;
-
 
605
                }
-
 
606
            } else {
-
 
607
                printf("Uknown option '%s'\n", arg[0]);
-
 
608
                print_syntax();
-
 
609
                return -1;
-
 
610
            }
589
        } else {
611
        } else {
590
            printf("Unexpected argument '%s'\n", arg);
-
 
591
            print_syntax();
-
 
592
            return -1;
612
            break;
593
        }
613
        }
594
 
614
 
595
        --argc; ++argv;
615
        --argc; ++argv;
596
    }
616
    }
597
 
617
 
598
    if (argc != 1) {
618
    if (task_id != 0) {
-
 
619
        if (argc == 0) return;
599
        printf("Missing argument\n");
620
        printf("Extra arguments\n");
600
        print_syntax();
621
        print_syntax();
601
        return 1;
622
        return -1;
602
    }
623
    }
603
 
624
 
604
    task_id = strtol(*argv, &err_p, 10);
-
 
605
 
-
 
606
    if (*err_p) {
625
    if (argc < 1) {
607
        printf("Task ID syntax error\n");
626
        printf("Missing argument\n");
608
        print_syntax();
627
        print_syntax();
609
        return -1;
628
        return -1;
610
    }
629
    }
611
 
630
 
-
 
631
    /* Execute the specified command and trace the new task. */
-
 
632
    printf("Spawning '%s' with arguments:\n", *argv);
-
 
633
    {
-
 
634
        char **cp = argv;
-
 
635
        while (*cp) printf("'%s'\n", *cp++);
-
 
636
    }
-
 
637
    task_id = task_spawn(*argv, argv);
-
 
638
 
612
    return 0;
639
    return 0;
613
}
640
}
614
 
641
 
615
int main(int argc, char *argv[])
642
int main(int argc, char *argv[])
616
{
643
{
617
    printf("System Call / IPC Tracer\n");
644
    printf("System Call / IPC Tracer\n");
618
 
645
 
619
    display_mask = DM_THREAD | DM_SYSTEM | DM_USER;
646
    display_mask = DM_THREAD | DM_SYSTEM | DM_USER;
620
 
647
 
621
    if (parse_args(argc, argv) < 0)
648
    if (parse_args(argc, argv) < 0)
622
        return 1;
649
        return 1;
623
 
650
 
624
    main_init();
651
    main_init();
625
    trace_active_task(task_id);
652
    trace_active_task(task_id);
626
 
653
 
627
    return 0;
654
    return 0;
628
}
655
}
629
 
656
 
630
/** @}
657
/** @}
631
 */
658
 */
632
 
659