Subversion Repositories HelenOS

Rev

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

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