Subversion Repositories HelenOS

Rev

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

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