Subversion Repositories HelenOS

Rev

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

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