Subversion Repositories HelenOS

Rev

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

Rev 3439 Rev 3442
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
 
44
 
45
// Temporary: service and method names
45
// Temporary: service and method names
46
#include "proto.h"
46
#include "proto.h"
47
#include <ipc/services.h>
47
#include <ipc/services.h>
48
#include "../../srv/vfs/vfs.h"
48
#include "../../srv/vfs/vfs.h"
49
#include "../../srv/console/console.h"
49
#include "../../srv/console/console.h"
50
 
50
 
51
#include "syscalls.h"
51
#include "syscalls.h"
52
#include "ipcp.h"
52
#include "ipcp.h"
53
#include "errors.h"
53
#include "errors.h"
54
 
54
 
55
#define THBUF_SIZE 64
55
#define THBUF_SIZE 64
56
unsigned thread_hash_buf[THBUF_SIZE];
56
unsigned thread_hash_buf[THBUF_SIZE];
57
unsigned n_threads;
57
unsigned n_threads;
58
 
58
 
59
int next_thread_id;
59
int next_thread_id;
60
 
60
 
61
int phoneid;
61
int phoneid;
62
int abort_trace;
62
int abort_trace;
63
 
63
 
64
unsigned thash;
64
unsigned thash;
65
volatile int paused;
65
volatile int paused;
66
 
66
 
67
void thread_trace_start(unsigned thread_hash);
67
void thread_trace_start(unsigned thread_hash);
68
 
68
 
69
static proto_t *proto_console;
69
static proto_t *proto_console;
70
 
70
 
71
static int task_connect(task_id_t task_id)
71
static int task_connect(task_id_t task_id)
72
{
72
{
73
    int rc;
73
    int rc;
74
 
74
 
75
    printf("ipc_connect_task(%lld)... ", task_id);
-
 
76
    rc = ipc_connect_kbox(task_id);
75
    rc = ipc_connect_kbox(task_id);
77
    printf("-> %d\n", rc);
-
 
78
 
76
 
79
    if (rc == ENOTSUP) {
77
    if (rc == ENOTSUP) {
80
        printf("You do not have userspace debugging support "
78
        printf("You do not have userspace debugging support "
81
            "compiled in the kernel.\n");
79
            "compiled in the kernel.\n");
82
        printf("Compile kernel with 'Support for userspace debuggers' "
80
        printf("Compile kernel with 'Support for userspace debuggers' "
83
            "(CONFIG_UDEBUG) enabled.\n");
81
            "(CONFIG_UDEBUG) enabled.\n");
-
 
82
        return rc;
-
 
83
    }
-
 
84
 
-
 
85
    if (rc < 0) {
-
 
86
        printf("Error connecting\n");
-
 
87
        printf("ipc_connect_task(%lld) -> %d ", task_id, rc);
-
 
88
        return rc;
84
    }
89
    }
85
 
90
 
86
    phoneid = rc;
91
    phoneid = rc;
87
    if (rc < 0) return rc;
-
 
88
 
92
 
89
    printf("udebug_begin()... ");
-
 
90
    rc = udebug_begin(phoneid);
93
    rc = udebug_begin(phoneid);
-
 
94
    if (rc < 0) {
91
    printf("-> %d\n", rc);
95
        printf("udebug_begin() -> %d\n", rc);
92
    if (rc < 0) return rc;
96
        return rc;
-
 
97
    }
93
 
98
 
94
    printf("udebug_set_evmask(0x%x)... ", UDEBUG_EM_ALL);
-
 
95
    rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL);
99
    rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL);
96
    printf("-> %d\n", rc);
100
    if (rc < 0) {
-
 
101
        printf("udebug_set_evmask(0x%x) -> %d\n ", UDEBUG_EM_ALL, rc);
97
    if (rc < 0) return rc;
102
        return rc;
-
 
103
    }
98
 
104
 
99
    return 0;
105
    return 0;
100
}
106
}
101
 
107
 
102
static int get_thread_list(void)
108
static int get_thread_list(void)
103
{
109
{
104
    int rc;
110
    int rc;
105
    size_t tb_copied;
111
    size_t tb_copied;
106
    size_t tb_needed;
112
    size_t tb_needed;
107
    int i;
113
    int i;
108
 
114
 
109
    printf("send IPC_M_DEBUG_THREAD_READ message\n");
-
 
110
    rc = udebug_thread_read(phoneid, thread_hash_buf,
115
    rc = udebug_thread_read(phoneid, thread_hash_buf,
111
        THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
116
        THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
-
 
117
    if (rc < 0) {
112
    printf("-> %d\n", rc);
118
        printf("udebug_thread_read() -> %d\n", rc);
113
    if (rc < 0) return rc;
119
        return rc;
-
 
120
    }
114
 
121
 
115
    n_threads = tb_copied / sizeof(unsigned);
122
    n_threads = tb_copied / sizeof(unsigned);
116
 
123
 
117
    printf("thread IDs:");
124
    printf("Threads:");
118
    for (i=0; i<n_threads; i++) {
125
    for (i = 0; i < n_threads; i++) {
119
        printf(" %u", thread_hash_buf[i]);
126
        printf(" [%d] (hash 0x%u)", 1+i, thread_hash_buf[i]);
120
    }
127
    }
121
    printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned));
128
    printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned));
122
 
129
 
123
    return 0;
130
    return 0;
124
}
131
}
125
 
132
 
126
static void print_sc_retval(int retval, rv_type_t rv_type)
133
static void print_sc_retval(int retval, rv_type_t rv_type)
127
{
134
{
128
    printf (" -> ");
135
    printf (" -> ");
129
    if (rv_type == RV_INTEGER) {
136
    if (rv_type == RV_INTEGER) {
130
        printf("%d", retval);
137
        printf("%d", retval);
131
    } else if (rv_type == RV_HASH) {
138
    } else if (rv_type == RV_HASH) {
132
        printf("0x%08x", retval);
139
        printf("0x%08x", retval);
133
    } else if (rv_type == RV_ERRNO) {
140
    } else if (rv_type == RV_ERRNO) {
134
        if (retval >= -15 && retval <= 0) {
141
        if (retval >= -15 && retval <= 0) {
135
            printf("%d %s (%s)", retval,
142
            printf("%d %s (%s)", retval,
136
                err_desc[retval].name,
143
                err_desc[retval].name,
137
                err_desc[retval].desc);
144
                err_desc[retval].desc);
138
        } else {
145
        } else {
139
            printf("%d", retval);
146
            printf("%d", retval);
140
        }
147
        }
141
    } else if (rv_type == RV_INT_ERRNO) {
148
    } else if (rv_type == RV_INT_ERRNO) {
142
        if (retval >= -15 && retval < 0) {
149
        if (retval >= -15 && retval < 0) {
143
            printf("%d %s (%s)", retval,
150
            printf("%d %s (%s)", retval,
144
                err_desc[retval].name,
151
                err_desc[retval].name,
145
                err_desc[retval].desc);
152
                err_desc[retval].desc);
146
        } else {
153
        } else {
147
            printf("%d", retval);
154
            printf("%d", retval);
148
        }
155
        }
149
    }
156
    }
150
    putchar('\n');
157
    putchar('\n');
151
}
158
}
152
 
159
 
153
static void print_sc_args(unsigned *sc_args, int n)
160
static void print_sc_args(unsigned *sc_args, int n)
154
{
161
{
155
    int i;
162
    int i;
156
 
163
 
157
    putchar('(');
164
    putchar('(');
158
    if (n > 0) printf("%d", sc_args[0]);
165
    if (n > 0) printf("%d", sc_args[0]);
159
    for (i=1; i<n; i++) {
166
    for (i=1; i<n; i++) {
160
        printf(", %d", sc_args[i]);
167
        printf(", %d", sc_args[i]);
161
    }
168
    }
162
    putchar(')');
169
    putchar(')');
163
}
170
}
164
 
171
 
165
static void sc_ipc_call_async_fast(unsigned *sc_args, int sc_rc)
172
static void sc_ipc_call_async_fast(unsigned *sc_args, int sc_rc)
166
{
173
{
167
    ipc_call_t call;
174
    ipc_call_t call;
168
    int phoneid;
175
    int phoneid;
169
   
176
   
170
    if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
177
    if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
171
        return;
178
        return;
172
 
179
 
173
    phoneid = sc_args[0];
180
    phoneid = sc_args[0];
174
 
181
 
175
    IPC_SET_METHOD(call, sc_args[1]);
182
    IPC_SET_METHOD(call, sc_args[1]);
176
    IPC_SET_ARG1(call, sc_args[2]);
183
    IPC_SET_ARG1(call, sc_args[2]);
177
    IPC_SET_ARG2(call, sc_args[3]);
184
    IPC_SET_ARG2(call, sc_args[3]);
178
    IPC_SET_ARG3(call, sc_args[4]);
185
    IPC_SET_ARG3(call, sc_args[4]);
179
    IPC_SET_ARG4(call, sc_args[5]);
186
    IPC_SET_ARG4(call, sc_args[5]);
180
    IPC_SET_ARG5(call, 0);
187
    IPC_SET_ARG5(call, 0);
181
 
188
 
182
    ipcp_call_out(phoneid, &call, sc_rc);
189
    ipcp_call_out(phoneid, &call, sc_rc);
183
}
190
}
184
 
191
 
185
static void sc_ipc_call_async_slow(unsigned *sc_args, int sc_rc)
192
static void sc_ipc_call_async_slow(unsigned *sc_args, int sc_rc)
186
{
193
{
187
    ipc_call_t call;
194
    ipc_call_t call;
188
    int rc;
195
    int rc;
189
 
196
 
190
    if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
197
    if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
191
        return;
198
        return;
192
 
199
 
193
    memset(&call, 0, sizeof(call));
200
    memset(&call, 0, sizeof(call));
194
    rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
201
    rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
195
 
202
 
196
    if (rc >= 0) {
203
    if (rc >= 0) {
197
        ipcp_call_out(sc_args[0], &call, sc_rc);
204
        ipcp_call_out(sc_args[0], &call, sc_rc);
198
    }
205
    }
199
}
206
}
200
 
207
 
201
static void sc_ipc_call_sync_fast(unsigned *sc_args)
208
static void sc_ipc_call_sync_fast(unsigned *sc_args)
202
{
209
{
203
    ipc_call_t question, reply;
210
    ipc_call_t question, reply;
204
    int rc;
211
    int rc;
205
    int phoneidx;
212
    int phoneidx;
206
 
213
 
207
//  printf("sc_ipc_call_sync_fast()\n");
214
//  printf("sc_ipc_call_sync_fast()\n");
208
    phoneidx = sc_args[0];
215
    phoneidx = sc_args[0];
209
 
216
 
210
    IPC_SET_METHOD(question, sc_args[1]);
217
    IPC_SET_METHOD(question, sc_args[1]);
211
    IPC_SET_ARG1(question, sc_args[2]);
218
    IPC_SET_ARG1(question, sc_args[2]);
212
    IPC_SET_ARG2(question, sc_args[3]);
219
    IPC_SET_ARG2(question, sc_args[3]);
213
    IPC_SET_ARG3(question, sc_args[4]);
220
    IPC_SET_ARG3(question, sc_args[4]);
214
    IPC_SET_ARG4(question, 0);
221
    IPC_SET_ARG4(question, 0);
215
    IPC_SET_ARG5(question, 0);
222
    IPC_SET_ARG5(question, 0);
216
 
223
 
217
//  printf("memset\n");
224
//  printf("memset\n");
218
    memset(&reply, 0, sizeof(reply));
225
    memset(&reply, 0, sizeof(reply));
219
//  printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
226
//  printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
220
//      phoneid, &reply.args, sc_args[5], sizeof(reply.args));
227
//      phoneid, &reply.args, sc_args[5], sizeof(reply.args));
221
    rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
228
    rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
222
//  printf("dmr->%d\n", rc);
229
//  printf("dmr->%d\n", rc);
223
    if (rc < 0) return;
230
    if (rc < 0) return;
224
 
231
 
225
//  printf("call ipc_call_sync\n");
232
//  printf("call ipc_call_sync\n");
226
    ipcp_call_sync(phoneidx, &question, &reply);
233
    ipcp_call_sync(phoneidx, &question, &reply);
227
}
234
}
228
 
235
 
229
static void sc_ipc_call_sync_slow(unsigned *sc_args)
236
static void sc_ipc_call_sync_slow(unsigned *sc_args)
230
{
237
{
231
    ipc_call_t question, reply;
238
    ipc_call_t question, reply;
232
    int rc;
239
    int rc;
233
 
240
 
234
    memset(&question, 0, sizeof(question));
241
    memset(&question, 0, sizeof(question));
235
    rc = udebug_mem_read(phoneid, &question.args, sc_args[1], sizeof(question.args));
242
    rc = udebug_mem_read(phoneid, &question.args, sc_args[1], sizeof(question.args));
236
    printf("dmr->%d\n", rc);
243
    printf("dmr->%d\n", rc);
237
    if (rc < 0) return;
244
    if (rc < 0) return;
238
 
245
 
239
    memset(&reply, 0, sizeof(reply));
246
    memset(&reply, 0, sizeof(reply));
240
    rc = udebug_mem_read(phoneid, &reply.args, sc_args[2], sizeof(reply.args));
247
    rc = udebug_mem_read(phoneid, &reply.args, sc_args[2], sizeof(reply.args));
241
    printf("dmr->%d\n", rc);
248
    printf("dmr->%d\n", rc);
242
    if (rc < 0) return;
249
    if (rc < 0) return;
243
 
250
 
244
    ipcp_call_sync(sc_args[0], &question, &reply);
251
    ipcp_call_sync(sc_args[0], &question, &reply);
245
}
252
}
246
 
253
 
247
static void sc_ipc_wait(unsigned *sc_args, int sc_rc)
254
static void sc_ipc_wait(unsigned *sc_args, int sc_rc)
248
{
255
{
249
    ipc_call_t call;
256
    ipc_call_t call;
250
    int rc;
257
    int rc;
251
 
258
 
252
    if (sc_rc == 0) return;
259
    if (sc_rc == 0) return;
253
 
260
 
254
    memset(&call, 0, sizeof(call));
261
    memset(&call, 0, sizeof(call));
255
    rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
262
    rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
256
//  printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
263
//  printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
257
//      phoneid, (int)&call, sc_args[0], sizeof(call), rc);
264
//      phoneid, (int)&call, sc_args[0], sizeof(call), rc);
258
 
265
 
259
    if (rc >= 0) {
266
    if (rc >= 0) {
260
        ipcp_call_in(&call, sc_rc);
267
        ipcp_call_in(&call, sc_rc);
261
    }
268
    }
262
}
269
}
263
 
270
 
264
static void event_syscall_b(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
271
static void event_syscall_b(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
265
{
272
{
266
    unsigned sc_args[6];
273
    unsigned sc_args[6];
267
    int rc;
274
    int rc;
268
 
275
 
269
    /* Read syscall arguments */
276
    /* Read syscall arguments */
270
    rc = udebug_args_read(phoneid, thread_hash, sc_args);
277
    rc = udebug_args_read(phoneid, thread_hash, sc_args);
271
 
278
 
272
    async_serialize_start();
279
    async_serialize_start();
273
 
280
 
274
//  printf("[%d] ", thread_id);
281
//  printf("[%d] ", thread_id);
275
 
282
 
276
    if (rc < 0) {
283
    if (rc < 0) {
277
        printf("error\n");
284
        printf("error\n");
278
        async_serialize_end();
285
        async_serialize_end();
279
        return;
286
        return;
280
    }
287
    }
281
 
288
 
282
    /* Print syscall name, id and arguments */
289
    /* Print syscall name, id and arguments */
283
    printf("%s", syscall_desc[sc_id].name);
290
    printf("%s", syscall_desc[sc_id].name);
284
    print_sc_args(sc_args, syscall_desc[sc_id].n_args);
291
    print_sc_args(sc_args, syscall_desc[sc_id].n_args);
285
 
292
 
286
    async_serialize_end();
293
    async_serialize_end();
287
}
294
}
288
 
295
 
289
static void event_syscall_e(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
296
static void event_syscall_e(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
290
{
297
{
291
    unsigned sc_args[6];
298
    unsigned sc_args[6];
292
    int rv_type;
299
    int rv_type;
293
    int rc;
300
    int rc;
294
 
301
 
295
    /* Read syscall arguments */
302
    /* Read syscall arguments */
296
    rc = udebug_args_read(phoneid, thread_hash, sc_args);
303
    rc = udebug_args_read(phoneid, thread_hash, sc_args);
297
 
304
 
298
    async_serialize_start();
305
    async_serialize_start();
299
 
306
 
300
//  printf("[%d] ", thread_id);
307
//  printf("[%d] ", thread_id);
301
 
308
 
302
    if (rc < 0) {
309
    if (rc < 0) {
303
        printf("error\n");
310
        printf("error\n");
304
        async_serialize_end();
311
        async_serialize_end();
305
        return;
312
        return;
306
    }
313
    }
307
 
314
 
308
    rv_type = syscall_desc[sc_id].rv_type;
315
    rv_type = syscall_desc[sc_id].rv_type;
309
    print_sc_retval(sc_rc, rv_type);
316
    print_sc_retval(sc_rc, rv_type);
310
 
317
 
311
    switch (sc_id) {
318
    switch (sc_id) {
312
    case SYS_IPC_CALL_ASYNC_FAST:
319
    case SYS_IPC_CALL_ASYNC_FAST:
313
        sc_ipc_call_async_fast(sc_args, sc_rc);
320
        sc_ipc_call_async_fast(sc_args, sc_rc);
314
        break;
321
        break;
315
    case SYS_IPC_CALL_ASYNC_SLOW:
322
    case SYS_IPC_CALL_ASYNC_SLOW:
316
        sc_ipc_call_async_slow(sc_args, sc_rc);
323
        sc_ipc_call_async_slow(sc_args, sc_rc);
317
        break;
324
        break;
318
    case SYS_IPC_CALL_SYNC_FAST:
325
    case SYS_IPC_CALL_SYNC_FAST:
319
        sc_ipc_call_sync_fast(sc_args);
326
        sc_ipc_call_sync_fast(sc_args);
320
        break;
327
        break;
321
    case SYS_IPC_CALL_SYNC_SLOW:
328
    case SYS_IPC_CALL_SYNC_SLOW:
322
        sc_ipc_call_sync_slow(sc_args);
329
        sc_ipc_call_sync_slow(sc_args);
323
        break;
330
        break;
324
    case SYS_IPC_WAIT:
331
    case SYS_IPC_WAIT:
325
        sc_ipc_wait(sc_args, sc_rc);
332
        sc_ipc_wait(sc_args, sc_rc);
326
        break;
333
        break;
327
    default:
334
    default:
328
        break;
335
        break;
329
    }
336
    }
330
 
337
 
331
    async_serialize_end();
338
    async_serialize_end();
332
}
339
}
333
 
340
 
334
static void event_thread_b(unsigned hash)
341
static void event_thread_b(unsigned hash)
335
{
342
{
336
    async_serialize_start();
343
    async_serialize_start();
337
    printf("new thread, hash 0x%x\n", hash);
344
    printf("New thread, hash 0x%x\n", hash);
338
    async_serialize_end();
345
    async_serialize_end();
339
 
346
 
340
    thread_trace_start(hash);
347
    thread_trace_start(hash);
341
}
348
}
342
 
349
 
343
static int trace_loop(void *thread_hash_arg)
350
static int trace_loop(void *thread_hash_arg)
344
{
351
{
345
    int rc;
352
    int rc;
346
    unsigned ev_type;
353
    unsigned ev_type;
347
    unsigned thread_hash;
354
    unsigned thread_hash;
348
    unsigned thread_id;
355
    unsigned thread_id;
349
    unsigned val0, val1;
356
    unsigned val0, val1;
350
 
357
 
351
    thread_hash = (unsigned)thread_hash_arg;
358
    thread_hash = (unsigned)thread_hash_arg;
352
    thread_id = next_thread_id++;
359
    thread_id = next_thread_id++;
353
 
360
 
354
    printf("trace_loop(%d)\n", thread_id); 
361
    printf("Start tracing thread [%d] (hash 0x%x)\n", thread_id, thread_hash);
355
 
362
 
356
    while (!abort_trace) {
363
    while (!abort_trace) {
357
 
364
 
358
        /* Run thread until an event occurs */
365
        /* Run thread until an event occurs */
359
        rc = udebug_go(phoneid, thread_hash,
366
        rc = udebug_go(phoneid, thread_hash,
360
            &ev_type, &val0, &val1);
367
            &ev_type, &val0, &val1);
361
 
368
 
362
//      printf("rc = %d, ev_type=%d\n", rc, ev_type);
369
//      printf("rc = %d, ev_type=%d\n", rc, ev_type);
363
        if (ev_type == UDEBUG_EVENT_FINISHED) {
370
        if (ev_type == UDEBUG_EVENT_FINISHED) {
364
            printf("thread %u debugging finished\n", thread_id);
371
            /* Done tracing this thread */
365
            break;
372
            break;
366
        }
373
        }
367
 
374
 
368
        if (rc >= 0) {
375
        if (rc >= 0) {
369
            switch (ev_type) {
376
            switch (ev_type) {
370
            case UDEBUG_EVENT_SYSCALL_B:
377
            case UDEBUG_EVENT_SYSCALL_B:
371
                event_syscall_b(thread_id, thread_hash, val0, (int)val1);
378
                event_syscall_b(thread_id, thread_hash, val0, (int)val1);
372
                break;
379
                break;
373
            case UDEBUG_EVENT_SYSCALL_E:
380
            case UDEBUG_EVENT_SYSCALL_E:
374
                event_syscall_e(thread_id, thread_hash, val0, (int)val1);
381
                event_syscall_e(thread_id, thread_hash, val0, (int)val1);
375
                break;
382
                break;
376
            case UDEBUG_EVENT_STOP:
383
            case UDEBUG_EVENT_STOP:
377
                printf("stop event\n");
384
                printf("Stop event\n");
378
                printf("waiting for resume\n");
385
                printf("Waiting for resume\n");
379
                while (paused) {
386
                while (paused) {
380
                    usleep(1000000);
387
                    usleep(1000000);
381
                    fibril_yield();
388
                    fibril_yield();
382
                    printf(".");
389
                    printf(".");
383
                }
390
                }
384
                printf("resumed\n");
391
                printf("Resumed\n");
385
                break;
392
                break;
386
            case UDEBUG_EVENT_THREAD_B:
393
            case UDEBUG_EVENT_THREAD_B:
387
                event_thread_b(val0);
394
                event_thread_b(val0);
388
                break;
395
                break;
389
            case UDEBUG_EVENT_THREAD_E:
396
            case UDEBUG_EVENT_THREAD_E:
390
                printf("thread 0x%x exited\n", val0);
397
                printf("Thread 0x%x exited\n", val0);
391
                abort_trace = 1;
398
                abort_trace = 1;
392
                break;
399
                break;
393
            default:
400
            default:
394
                printf("unknown event type %d\n", ev_type);
401
                printf("Unknown event type %d\n", ev_type);
395
                break;
402
                break;
396
            }
403
            }
397
        }
404
        }
398
 
405
 
399
    }
406
    }
400
 
407
 
401
    printf("trace_loop(%d) exiting\n", thread_id);
408
    printf("Finished tracing thread [%d]\n", thread_id);
402
    return 0;
409
    return 0;
403
}
410
}
404
 
411
 
405
void thread_trace_start(unsigned thread_hash)
412
void thread_trace_start(unsigned thread_hash)
406
{
413
{
407
    fid_t fid;
414
    fid_t fid;
408
 
415
 
409
    thash = thread_hash;
416
    thash = thread_hash;
410
 
417
 
411
    fid = fibril_create(trace_loop, (void *)thread_hash);
418
    fid = fibril_create(trace_loop, (void *)thread_hash);
412
    if (fid == 0) {
419
    if (fid == 0) {
413
        printf("Warning: Failed creating fibril\n");
420
        printf("Warning: Failed creating fibril\n");
414
    }
421
    }
415
    fibril_add_ready(fid);
422
    fibril_add_ready(fid);
416
}
423
}
417
 
424
 
418
static void trace_active_task(task_id_t task_id)
425
static void trace_active_task(task_id_t task_id)
419
{
426
{
420
    int i;
427
    int i;
421
    int rc;
428
    int rc;
422
    int c;
429
    int c;
423
 
430
 
424
    printf("Syscall Tracer\n");
431
    printf("Syscall Tracer\n");
425
 
432
 
426
    rc = task_connect(task_id);
433
    rc = task_connect(task_id);
427
    if (rc < 0) {
434
    if (rc < 0) {
428
        printf("Failed to connect to task %lld\n", task_id);
435
        printf("Failed to connect to task %lld\n", task_id);
429
        return;
436
        return;
430
    }
437
    }
431
 
438
 
432
    printf("Connected to task %lld\n", task_id);
439
    printf("Connected to task %lld\n", task_id);
433
 
440
 
434
    ipcp_init();
441
    ipcp_init();
-
 
442
 
-
 
443
    /*
-
 
444
     * User apps now typically have console on phone 3.
-
 
445
     * (Phones 1 and 2 are used by the loader).
-
 
446
     */
435
    ipcp_connection_set(1, 0, proto_console);
447
    ipcp_connection_set(3, 0, proto_console);
436
 
448
 
437
    rc = get_thread_list();
449
    rc = get_thread_list();
438
    if (rc < 0) {
450
    if (rc < 0) {
439
        printf("Failed to get thread list (error %d)\n", rc);
451
        printf("Failed to get thread list (error %d)\n", rc);
440
        return;
452
        return;
441
    }
453
    }
442
 
454
 
443
    abort_trace = 0;
455
    abort_trace = 0;
444
 
456
 
445
    for (i = 0; i < n_threads; i++) {
457
    for (i = 0; i < n_threads; i++) {
446
        thread_trace_start(thread_hash_buf[i]);
458
        thread_trace_start(thread_hash_buf[i]);
447
    }
459
    }
448
 
460
 
449
    while(1) {
461
    while(1) {
450
        c = getchar();
462
        c = getchar();
451
        if (c == 'q') break;
463
        if (c == 'q') break;
452
        if (c == 'p') {
464
        if (c == 'p') {
453
            paused = 1;
465
            paused = 1;
454
            rc = udebug_stop(phoneid, thash);
466
            rc = udebug_stop(phoneid, thash);
455
            printf("stop -> %d\n", rc);
467
            printf("stop -> %d\n", rc);
456
        }
468
        }
457
        if (c == 'r') {
469
        if (c == 'r') {
458
            paused = 0;
470
            paused = 0;
459
        }
471
        }
460
    }
472
    }
461
 
473
 
462
    printf("terminate debugging session...\n");
474
    printf("\nTerminate debugging session...\n");
463
    abort_trace = 1;
475
    abort_trace = 1;
464
    udebug_end(phoneid);
476
    udebug_end(phoneid);
465
    ipc_hangup(phoneid);
477
    ipc_hangup(phoneid);
466
 
478
 
467
    ipcp_cleanup();
479
    ipcp_cleanup();
468
 
480
 
469
    printf("done\n");
481
    printf("Done\n");
470
    return;
482
    return;
471
}
483
}
472
 
484
 
473
static void main_init(void)
485
static void main_init(void)
474
{
486
{
475
    proto_t *p;
487
    proto_t *p;
476
    oper_t *o;
488
    oper_t *o;
477
 
489
 
478
    next_thread_id = 1;
490
    next_thread_id = 1;
479
    paused = 0;
491
    paused = 0;
480
 
492
 
481
    proto_init();
493
    proto_init();
482
 
494
 
483
    p = proto_new("vfs");
495
    p = proto_new("vfs");
484
    o = oper_new("read");
496
    o = oper_new("read");
485
    proto_add_oper(p, VFS_READ, o);
497
    proto_add_oper(p, VFS_READ, o);
486
    o = oper_new("write");
498
    o = oper_new("write");
487
    proto_add_oper(p, VFS_WRITE, o);
499
    proto_add_oper(p, VFS_WRITE, o);
488
    o = oper_new("truncate");
500
    o = oper_new("truncate");
489
    proto_add_oper(p, VFS_TRUNCATE, o);
501
    proto_add_oper(p, VFS_TRUNCATE, o);
490
    o = oper_new("mount");
502
    o = oper_new("mount");
491
    proto_add_oper(p, VFS_MOUNT, o);
503
    proto_add_oper(p, VFS_MOUNT, o);
492
    o = oper_new("unmount");
504
    o = oper_new("unmount");
493
    proto_add_oper(p, VFS_UNMOUNT, o);
505
    proto_add_oper(p, VFS_UNMOUNT, o);
494
 
506
 
495
    proto_register(SERVICE_VFS, p);
507
    proto_register(SERVICE_VFS, p);
496
 
508
 
497
    p = proto_new("console");
509
    p = proto_new("console");
498
    o = oper_new("getchar");
510
    o = oper_new("getchar");
499
    proto_add_oper(p, CONSOLE_GETCHAR, o);
511
    proto_add_oper(p, CONSOLE_GETCHAR, o);
500
    o = oper_new("putchar");
512
    o = oper_new("putchar");
501
    proto_add_oper(p, CONSOLE_PUTCHAR, o);
513
    proto_add_oper(p, CONSOLE_PUTCHAR, o);
502
    o = oper_new("clear");
514
    o = oper_new("clear");
503
    proto_add_oper(p, CONSOLE_CLEAR, o);
515
    proto_add_oper(p, CONSOLE_CLEAR, o);
504
    o = oper_new("goto");
516
    o = oper_new("goto");
505
    proto_add_oper(p, CONSOLE_GOTO, o);
517
    proto_add_oper(p, CONSOLE_GOTO, o);
506
    o = oper_new("getsize");
518
    o = oper_new("getsize");
507
    proto_add_oper(p, CONSOLE_GETSIZE, o);
519
    proto_add_oper(p, CONSOLE_GETSIZE, o);
508
    o = oper_new("flush");
520
    o = oper_new("flush");
509
    proto_add_oper(p, CONSOLE_FLUSH, o);
521
    proto_add_oper(p, CONSOLE_FLUSH, o);
510
    o = oper_new("set_style");
522
    o = oper_new("set_style");
511
    proto_add_oper(p, CONSOLE_SET_STYLE, o);
523
    proto_add_oper(p, CONSOLE_SET_STYLE, o);
512
    o = oper_new("cursor_visibility");
524
    o = oper_new("cursor_visibility");
513
    proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
525
    proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
514
    o = oper_new("flush");
526
    o = oper_new("flush");
515
    proto_add_oper(p, CONSOLE_FLUSH, o);
527
    proto_add_oper(p, CONSOLE_FLUSH, o);
516
 
528
 
517
    proto_console = p;
529
    proto_console = p;
518
    proto_register(SERVICE_CONSOLE, p);
530
    proto_register(SERVICE_CONSOLE, p);
519
}
531
}
520
 
532
 
521
static void print_syntax()
533
static void print_syntax()
522
{
534
{
523
    printf("syntax: trace <task_id>\n");
535
    printf("Syntax: trace <task_id>\n");
524
}
536
}
525
 
537
 
526
int main(int argc, char *argv[])
538
int main(int argc, char *argv[])
527
{
539
{
528
    task_id_t task_id;
540
    task_id_t task_id;
529
    char *err_p;
541
    char *err_p;
530
 
542
 
531
    if (argc != 2) {
543
    if (argc != 2) {
532
        printf("Mising argument\n");
544
        printf("Mising argument\n");
533
        print_syntax();
545
        print_syntax();
534
        return 1;
546
        return 1;
535
    }
547
    }
536
 
548
 
537
    task_id = strtol(argv[1], &err_p, 10);
549
    task_id = strtol(argv[1], &err_p, 10);
538
 
550
 
539
    if (*err_p) {
551
    if (*err_p) {
540
        printf("Task ID syntax error\n");
552
        printf("Task ID syntax error\n");
541
        print_syntax();
553
        print_syntax();
542
        return 1;
554
        return 1;
543
    }
555
    }
544
 
556
 
545
    main_init();
557
    main_init();
546
    trace_active_task(task_id);
558
    trace_active_task(task_id);
547
}
559
}
548
 
560
 
549
/** @}
561
/** @}
550
 */
562
 */
551
 
563