Subversion Repositories HelenOS

Rev

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

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