Subversion Repositories HelenOS

Rev

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

Rev 4657 Rev 4658
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 <ipc/ipc.h>
38
#include <ipc/ipc.h>
39
#include <fibril.h>
39
#include <fibril.h>
40
#include <errno.h>
40
#include <errno.h>
41
#include <udebug.h>
41
#include <udebug.h>
42
#include <async.h>
42
#include <async.h>
43
#include <task.h>
43
#include <task.h>
44
#include <mem.h>
44
#include <mem.h>
45
#include <string.h>
45
#include <string.h>
46
#include <loader/loader.h>
46
#include <loader/loader.h>
47
#include <io/console.h>
47
#include <io/console.h>
48
#include <io/keycode.h>
48
#include <io/keycode.h>
49
#include <fibril_sync.h>
49
#include <fibril_sync.h>
50
 
50
 
51
#include <libc.h>
51
#include <libc.h>
52
 
52
 
53
// Temporary: service and method names
53
// Temporary: service and method names
54
#include "proto.h"
54
#include "proto.h"
55
#include <ipc/services.h>
55
#include <ipc/services.h>
56
#include "../../srv/vfs/vfs.h"
56
#include "../../srv/vfs/vfs.h"
57
#include <ipc/console.h>
57
#include <ipc/console.h>
58
 
58
 
59
#include "syscalls.h"
59
#include "syscalls.h"
60
#include "ipcp.h"
60
#include "ipcp.h"
61
#include "errors.h"
61
#include "errors.h"
62
#include "trace.h"
62
#include "trace.h"
63
 
63
 
64
#define THBUF_SIZE 64
64
#define THBUF_SIZE 64
65
uintptr_t thread_hash_buf[THBUF_SIZE];
65
uintptr_t thread_hash_buf[THBUF_SIZE];
66
int n_threads;
66
int n_threads;
67
 
67
 
68
int next_thread_id;
68
int next_thread_id;
69
 
69
 
70
int phoneid;
70
int phoneid;
71
int abort_trace;
71
int abort_trace;
72
 
72
 
73
uintptr_t thash;
73
uintptr_t thash;
74
volatile int paused;
74
volatile int paused;
75
fibril_condvar_t state_cv;
75
fibril_condvar_t state_cv;
76
fibril_mutex_t state_lock;
76
fibril_mutex_t state_lock;
77
 
77
 
78
int cev_valid;
78
int cev_valid;
79
console_event_t cev;
79
console_event_t cev;
80
 
80
 
81
void thread_trace_start(uintptr_t thread_hash);
81
void thread_trace_start(uintptr_t thread_hash);
82
 
82
 
83
static proto_t *proto_console;
83
static proto_t *proto_console;
84
static task_id_t task_id;
84
static task_id_t task_id;
85
static loader_t *task_ldr;
85
static loader_t *task_ldr;
-
 
86
static int task_wait_for;
86
 
87
 
87
/** Combination of events/data to print. */
88
/** Combination of events/data to print. */
88
display_mask_t display_mask;
89
display_mask_t display_mask;
89
 
90
 
90
static int program_run_fibril(void *arg);
91
static int program_run_fibril(void *arg);
91
static int cev_fibril(void *arg);
92
static int cev_fibril(void *arg);
92
 
93
 
93
static void program_run(void)
94
static void program_run(void)
94
{
95
{
95
    fid_t fid;
96
    fid_t fid;
96
 
97
 
97
    fid = fibril_create(program_run_fibril, NULL);
98
    fid = fibril_create(program_run_fibril, NULL);
98
    if (fid == 0) {
99
    if (fid == 0) {
99
        printf("Error creating fibril\n");
100
        printf("Error creating fibril\n");
100
        exit(1);
101
        exit(1);
101
    }
102
    }
102
 
103
 
103
    fibril_add_ready(fid);
104
    fibril_add_ready(fid);
104
}
105
}
105
 
106
 
106
static void cev_fibril_start(void)
107
static void cev_fibril_start(void)
107
{
108
{
108
    fid_t fid;
109
    fid_t fid;
109
 
110
 
110
    fid = fibril_create(cev_fibril, NULL);
111
    fid = fibril_create(cev_fibril, NULL);
111
    if (fid == 0) {
112
    if (fid == 0) {
112
        printf("Error creating fibril\n");
113
        printf("Error creating fibril\n");
113
        exit(1);
114
        exit(1);
114
    }
115
    }
115
 
116
 
116
    fibril_add_ready(fid);
117
    fibril_add_ready(fid);
117
}
118
}
118
 
119
 
119
static int program_run_fibril(void *arg)
120
static int program_run_fibril(void *arg)
120
{
121
{
121
    int rc;
122
    int rc;
122
 
123
 
123
    /*
124
    /*
124
     * This must be done in background as it will block until
125
     * This must be done in background as it will block until
125
     * we let the task reply to this call.
126
     * we let the task reply to this call.
126
     */
127
     */
127
    rc = loader_run(task_ldr);
128
    rc = loader_run(task_ldr);
128
    if (rc != 0) {
129
    if (rc != 0) {
129
        printf("Error running program\n");
130
        printf("Error running program\n");
130
        exit(1);
131
        exit(1);
131
    }
132
    }
132
 
133
 
133
    free(task_ldr);
134
    free(task_ldr);
134
    task_ldr = NULL;
135
    task_ldr = NULL;
135
 
136
 
136
    printf("program_run_fibril exiting\n");
137
    printf("program_run_fibril exiting\n");
137
    return 0;
138
    return 0;
138
}
139
}
139
 
140
 
140
 
141
 
141
static int connect_task(task_id_t task_id)
142
static int connect_task(task_id_t task_id)
142
{
143
{
143
    int rc;
144
    int rc;
144
 
145
 
145
    rc = ipc_connect_kbox(task_id);
146
    rc = ipc_connect_kbox(task_id);
146
 
147
 
147
    if (rc == ENOTSUP) {
148
    if (rc == ENOTSUP) {
148
        printf("You do not have userspace debugging support "
149
        printf("You do not have userspace debugging support "
149
            "compiled in the kernel.\n");
150
            "compiled in the kernel.\n");
150
        printf("Compile kernel with 'Support for userspace debuggers' "
151
        printf("Compile kernel with 'Support for userspace debuggers' "
151
            "(CONFIG_UDEBUG) enabled.\n");
152
            "(CONFIG_UDEBUG) enabled.\n");
152
        return rc;
153
        return rc;
153
    }
154
    }
154
 
155
 
155
    if (rc < 0) {
156
    if (rc < 0) {
156
        printf("Error connecting\n");
157
        printf("Error connecting\n");
157
        printf("ipc_connect_task(%lld) -> %d ", task_id, rc);
158
        printf("ipc_connect_task(%lld) -> %d ", task_id, rc);
158
        return rc;
159
        return rc;
159
    }
160
    }
160
 
161
 
161
    phoneid = rc;
162
    phoneid = rc;
162
 
163
 
163
    rc = udebug_begin(phoneid);
164
    rc = udebug_begin(phoneid);
164
    if (rc < 0) {
165
    if (rc < 0) {
165
        printf("udebug_begin() -> %d\n", rc);
166
        printf("udebug_begin() -> %d\n", rc);
166
        return rc;
167
        return rc;
167
    }
168
    }
168
 
169
 
169
    rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL);
170
    rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL);
170
    if (rc < 0) {
171
    if (rc < 0) {
171
        printf("udebug_set_evmask(0x%x) -> %d\n ", UDEBUG_EM_ALL, rc);
172
        printf("udebug_set_evmask(0x%x) -> %d\n ", UDEBUG_EM_ALL, rc);
172
        return rc;
173
        return rc;
173
    }
174
    }
174
 
175
 
175
    return 0;
176
    return 0;
176
}
177
}
177
 
178
 
178
static int get_thread_list(void)
179
static int get_thread_list(void)
179
{
180
{
180
    int rc;
181
    int rc;
181
    size_t tb_copied;
182
    size_t tb_copied;
182
    size_t tb_needed;
183
    size_t tb_needed;
183
    int i;
184
    int i;
184
 
185
 
185
    rc = udebug_thread_read(phoneid, thread_hash_buf,
186
    rc = udebug_thread_read(phoneid, thread_hash_buf,
186
        THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
187
        THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
187
    if (rc < 0) {
188
    if (rc < 0) {
188
        printf("udebug_thread_read() -> %d\n", rc);
189
        printf("udebug_thread_read() -> %d\n", rc);
189
        return rc;
190
        return rc;
190
    }
191
    }
191
 
192
 
192
    n_threads = tb_copied / sizeof(uintptr_t);
193
    n_threads = tb_copied / sizeof(uintptr_t);
193
 
194
 
194
    printf("Threads:");
195
    printf("Threads:");
195
    for (i = 0; i < n_threads; i++) {
196
    for (i = 0; i < n_threads; i++) {
196
        printf(" [%d] (hash 0x%lx)", 1+i, thread_hash_buf[i]);
197
        printf(" [%d] (hash 0x%lx)", 1+i, thread_hash_buf[i]);
197
    }
198
    }
198
    printf("\ntotal of %u threads\n", tb_needed / sizeof(uintptr_t));
199
    printf("\ntotal of %u threads\n", tb_needed / sizeof(uintptr_t));
199
 
200
 
200
    return 0;
201
    return 0;
201
}
202
}
202
 
203
 
203
void val_print(sysarg_t val, val_type_t v_type)
204
void val_print(sysarg_t val, val_type_t v_type)
204
{
205
{
205
    switch (v_type) {
206
    switch (v_type) {
206
    case V_VOID:
207
    case V_VOID:
207
        printf("<void>");
208
        printf("<void>");
208
        break;
209
        break;
209
 
210
 
210
    case V_INTEGER:
211
    case V_INTEGER:
211
        printf("%ld", val);
212
        printf("%ld", val);
212
        break;
213
        break;
213
 
214
 
214
    case V_HASH:
215
    case V_HASH:
215
    case V_PTR:
216
    case V_PTR:
216
        printf("0x%08lx", val);
217
        printf("0x%08lx", val);
217
        break;
218
        break;
218
 
219
 
219
    case V_ERRNO:
220
    case V_ERRNO:
220
        if (val >= -15 && val <= 0) {
221
        if (val >= -15 && val <= 0) {
221
            printf("%ld %s (%s)", val,
222
            printf("%ld %s (%s)", val,
222
                err_desc[-val].name,
223
                err_desc[-val].name,
223
                err_desc[-val].desc);
224
                err_desc[-val].desc);
224
        } else {
225
        } else {
225
            printf("%ld", val);
226
            printf("%ld", val);
226
        }
227
        }
227
        break;
228
        break;
228
    case V_INT_ERRNO:
229
    case V_INT_ERRNO:
229
        if (val >= -15 && val < 0) {
230
        if (val >= -15 && val < 0) {
230
            printf("%ld %s (%s)", val,
231
            printf("%ld %s (%s)", val,
231
                err_desc[-val].name,
232
                err_desc[-val].name,
232
                err_desc[-val].desc);
233
                err_desc[-val].desc);
233
        } else {
234
        } else {
234
            printf("%ld", val);
235
            printf("%ld", val);
235
        }
236
        }
236
        break;
237
        break;
237
 
238
 
238
    case V_CHAR:
239
    case V_CHAR:
239
        if (val >= 0x20 && val < 0x7f) {
240
        if (val >= 0x20 && val < 0x7f) {
240
            printf("'%c'", val);
241
            printf("'%c'", val);
241
        } else {
242
        } else {
242
            switch (val) {
243
            switch (val) {
243
            case '\a': printf("'\\a'"); break;
244
            case '\a': printf("'\\a'"); break;
244
            case '\b': printf("'\\b'"); break;
245
            case '\b': printf("'\\b'"); break;
245
            case '\n': printf("'\\n'"); break;
246
            case '\n': printf("'\\n'"); break;
246
            case '\r': printf("'\\r'"); break;
247
            case '\r': printf("'\\r'"); break;
247
            case '\t': printf("'\\t'"); break;
248
            case '\t': printf("'\\t'"); break;
248
            case '\\': printf("'\\\\'"); break;
249
            case '\\': printf("'\\\\'"); break;
249
            default: printf("'\\x%02lX'", val); break;
250
            default: printf("'\\x%02lX'", val); break;
250
            }
251
            }
251
        }
252
        }
252
        break;
253
        break;
253
    }
254
    }
254
}
255
}
255
 
256
 
256
 
257
 
257
static void print_sc_retval(sysarg_t retval, val_type_t val_type)
258
static void print_sc_retval(sysarg_t retval, val_type_t val_type)
258
{
259
{
259
    printf(" -> ");
260
    printf(" -> ");
260
    val_print(retval, val_type);
261
    val_print(retval, val_type);
261
    putchar('\n');
262
    putchar('\n');
262
}
263
}
263
 
264
 
264
static void print_sc_args(sysarg_t *sc_args, int n)
265
static void print_sc_args(sysarg_t *sc_args, int n)
265
{
266
{
266
    int i;
267
    int i;
267
 
268
 
268
    putchar('(');
269
    putchar('(');
269
    if (n > 0) printf("%ld", sc_args[0]);
270
    if (n > 0) printf("%ld", sc_args[0]);
270
    for (i = 1; i < n; i++) {
271
    for (i = 1; i < n; i++) {
271
        printf(", %ld", sc_args[i]);
272
        printf(", %ld", sc_args[i]);
272
    }
273
    }
273
    putchar(')');
274
    putchar(')');
274
}
275
}
275
 
276
 
276
static void sc_ipc_call_async_fast(sysarg_t *sc_args, sysarg_t sc_rc)
277
static void sc_ipc_call_async_fast(sysarg_t *sc_args, sysarg_t sc_rc)
277
{
278
{
278
    ipc_call_t call;
279
    ipc_call_t call;
279
    ipcarg_t phoneid;
280
    ipcarg_t phoneid;
280
   
281
   
281
    if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
282
    if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
282
        return;
283
        return;
283
 
284
 
284
    phoneid = sc_args[0];
285
    phoneid = sc_args[0];
285
 
286
 
286
    IPC_SET_METHOD(call, sc_args[1]);
287
    IPC_SET_METHOD(call, sc_args[1]);
287
    IPC_SET_ARG1(call, sc_args[2]);
288
    IPC_SET_ARG1(call, sc_args[2]);
288
    IPC_SET_ARG2(call, sc_args[3]);
289
    IPC_SET_ARG2(call, sc_args[3]);
289
    IPC_SET_ARG3(call, sc_args[4]);
290
    IPC_SET_ARG3(call, sc_args[4]);
290
    IPC_SET_ARG4(call, sc_args[5]);
291
    IPC_SET_ARG4(call, sc_args[5]);
291
    IPC_SET_ARG5(call, 0);
292
    IPC_SET_ARG5(call, 0);
292
 
293
 
293
    ipcp_call_out(phoneid, &call, sc_rc);
294
    ipcp_call_out(phoneid, &call, sc_rc);
294
}
295
}
295
 
296
 
296
static void sc_ipc_call_async_slow(sysarg_t *sc_args, sysarg_t sc_rc)
297
static void sc_ipc_call_async_slow(sysarg_t *sc_args, sysarg_t sc_rc)
297
{
298
{
298
    ipc_call_t call;
299
    ipc_call_t call;
299
    int rc;
300
    int rc;
300
 
301
 
301
    if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
302
    if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
302
        return;
303
        return;
303
 
304
 
304
    memset(&call, 0, sizeof(call));
305
    memset(&call, 0, sizeof(call));
305
    rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
306
    rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
306
 
307
 
307
    if (rc >= 0) {
308
    if (rc >= 0) {
308
        ipcp_call_out(sc_args[0], &call, sc_rc);
309
        ipcp_call_out(sc_args[0], &call, sc_rc);
309
    }
310
    }
310
}
311
}
311
 
312
 
312
static void sc_ipc_call_sync_fast(sysarg_t *sc_args)
313
static void sc_ipc_call_sync_fast(sysarg_t *sc_args)
313
{
314
{
314
    ipc_call_t question, reply;
315
    ipc_call_t question, reply;
315
    int rc;
316
    int rc;
316
    int phoneidx;
317
    int phoneidx;
317
 
318
 
318
//  printf("sc_ipc_call_sync_fast()\n");
319
//  printf("sc_ipc_call_sync_fast()\n");
319
    phoneidx = sc_args[0];
320
    phoneidx = sc_args[0];
320
 
321
 
321
    IPC_SET_METHOD(question, sc_args[1]);
322
    IPC_SET_METHOD(question, sc_args[1]);
322
    IPC_SET_ARG1(question, sc_args[2]);
323
    IPC_SET_ARG1(question, sc_args[2]);
323
    IPC_SET_ARG2(question, sc_args[3]);
324
    IPC_SET_ARG2(question, sc_args[3]);
324
    IPC_SET_ARG3(question, sc_args[4]);
325
    IPC_SET_ARG3(question, sc_args[4]);
325
    IPC_SET_ARG4(question, 0);
326
    IPC_SET_ARG4(question, 0);
326
    IPC_SET_ARG5(question, 0);
327
    IPC_SET_ARG5(question, 0);
327
 
328
 
328
//  printf("memset\n");
329
//  printf("memset\n");
329
    memset(&reply, 0, sizeof(reply));
330
    memset(&reply, 0, sizeof(reply));
330
//  printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
331
//  printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
331
//      phoneid, &reply.args, sc_args[5], sizeof(reply.args));
332
//      phoneid, &reply.args, sc_args[5], sizeof(reply.args));
332
    rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
333
    rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
333
//  printf("dmr->%d\n", rc);
334
//  printf("dmr->%d\n", rc);
334
    if (rc < 0) return;
335
    if (rc < 0) return;
335
 
336
 
336
//  printf("call ipc_call_sync\n");
337
//  printf("call ipc_call_sync\n");
337
    ipcp_call_sync(phoneidx, &question, &reply);
338
    ipcp_call_sync(phoneidx, &question, &reply);
338
}
339
}
339
 
340
 
340
static void sc_ipc_call_sync_slow(sysarg_t *sc_args)
341
static void sc_ipc_call_sync_slow(sysarg_t *sc_args)
341
{
342
{
342
    ipc_call_t question, reply;
343
    ipc_call_t question, reply;
343
    int rc;
344
    int rc;
344
 
345
 
345
    memset(&question, 0, sizeof(question));
346
    memset(&question, 0, sizeof(question));
346
    rc = udebug_mem_read(phoneid, &question.args, sc_args[1], sizeof(question.args));
347
    rc = udebug_mem_read(phoneid, &question.args, sc_args[1], sizeof(question.args));
347
    printf("dmr->%d\n", rc);
348
    printf("dmr->%d\n", rc);
348
    if (rc < 0) return;
349
    if (rc < 0) return;
349
 
350
 
350
    memset(&reply, 0, sizeof(reply));
351
    memset(&reply, 0, sizeof(reply));
351
    rc = udebug_mem_read(phoneid, &reply.args, sc_args[2], sizeof(reply.args));
352
    rc = udebug_mem_read(phoneid, &reply.args, sc_args[2], sizeof(reply.args));
352
    printf("dmr->%d\n", rc);
353
    printf("dmr->%d\n", rc);
353
    if (rc < 0) return;
354
    if (rc < 0) return;
354
 
355
 
355
    ipcp_call_sync(sc_args[0], &question, &reply);
356
    ipcp_call_sync(sc_args[0], &question, &reply);
356
}
357
}
357
 
358
 
358
static void sc_ipc_wait(sysarg_t *sc_args, int sc_rc)
359
static void sc_ipc_wait(sysarg_t *sc_args, int sc_rc)
359
{
360
{
360
    ipc_call_t call;
361
    ipc_call_t call;
361
    int rc;
362
    int rc;
362
 
363
 
363
    if (sc_rc == 0) return;
364
    if (sc_rc == 0) return;
364
 
365
 
365
    memset(&call, 0, sizeof(call));
366
    memset(&call, 0, sizeof(call));
366
    rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
367
    rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
367
//  printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
368
//  printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
368
//      phoneid, (int)&call, sc_args[0], sizeof(call), rc);
369
//      phoneid, (int)&call, sc_args[0], sizeof(call), rc);
369
 
370
 
370
    if (rc >= 0) {
371
    if (rc >= 0) {
371
        ipcp_call_in(&call, sc_rc);
372
        ipcp_call_in(&call, sc_rc);
372
    }
373
    }
373
}
374
}
374
 
375
 
375
static void event_syscall_b(unsigned thread_id, uintptr_t thread_hash,
376
static void event_syscall_b(unsigned thread_id, uintptr_t thread_hash,
376
    unsigned sc_id, sysarg_t sc_rc)
377
    unsigned sc_id, sysarg_t sc_rc)
377
{
378
{
378
    sysarg_t sc_args[6];
379
    sysarg_t sc_args[6];
379
    int rc;
380
    int rc;
380
 
381
 
381
    /* Read syscall arguments */
382
    /* Read syscall arguments */
382
    rc = udebug_args_read(phoneid, thread_hash, sc_args);
383
    rc = udebug_args_read(phoneid, thread_hash, sc_args);
383
 
384
 
384
    async_serialize_start();
385
    async_serialize_start();
385
 
386
 
386
//  printf("[%d] ", thread_id);
387
//  printf("[%d] ", thread_id);
387
 
388
 
388
    if (rc < 0) {
389
    if (rc < 0) {
389
        printf("error\n");
390
        printf("error\n");
390
        async_serialize_end();
391
        async_serialize_end();
391
        return;
392
        return;
392
    }
393
    }
393
 
394
 
394
    if ((display_mask & DM_SYSCALL) != 0) {
395
    if ((display_mask & DM_SYSCALL) != 0) {
395
        /* Print syscall name and arguments */
396
        /* Print syscall name and arguments */
396
        printf("%s", syscall_desc[sc_id].name);
397
        printf("%s", syscall_desc[sc_id].name);
397
        print_sc_args(sc_args, syscall_desc[sc_id].n_args);
398
        print_sc_args(sc_args, syscall_desc[sc_id].n_args);
398
    }
399
    }
399
 
400
 
400
    async_serialize_end();
401
    async_serialize_end();
401
}
402
}
402
 
403
 
403
static void event_syscall_e(unsigned thread_id, uintptr_t thread_hash,
404
static void event_syscall_e(unsigned thread_id, uintptr_t thread_hash,
404
    unsigned sc_id, sysarg_t sc_rc)
405
    unsigned sc_id, sysarg_t sc_rc)
405
{
406
{
406
    sysarg_t sc_args[6];
407
    sysarg_t sc_args[6];
407
    int rv_type;
408
    int rv_type;
408
    int rc;
409
    int rc;
409
 
410
 
410
    /* Read syscall arguments */
411
    /* Read syscall arguments */
411
    rc = udebug_args_read(phoneid, thread_hash, sc_args);
412
    rc = udebug_args_read(phoneid, thread_hash, sc_args);
412
 
413
 
413
    async_serialize_start();
414
    async_serialize_start();
414
 
415
 
415
//  printf("[%d] ", thread_id);
416
//  printf("[%d] ", thread_id);
416
 
417
 
417
    if (rc < 0) {
418
    if (rc < 0) {
418
        printf("error\n");
419
        printf("error\n");
419
        async_serialize_end();
420
        async_serialize_end();
420
        return;
421
        return;
421
    }
422
    }
422
 
423
 
423
    if ((display_mask & DM_SYSCALL) != 0) {
424
    if ((display_mask & DM_SYSCALL) != 0) {
424
        /* Print syscall return value */
425
        /* Print syscall return value */
425
        rv_type = syscall_desc[sc_id].rv_type;
426
        rv_type = syscall_desc[sc_id].rv_type;
426
        print_sc_retval(sc_rc, rv_type);
427
        print_sc_retval(sc_rc, rv_type);
427
    }
428
    }
428
 
429
 
429
    switch (sc_id) {
430
    switch (sc_id) {
430
    case SYS_IPC_CALL_ASYNC_FAST:
431
    case SYS_IPC_CALL_ASYNC_FAST:
431
        sc_ipc_call_async_fast(sc_args, sc_rc);
432
        sc_ipc_call_async_fast(sc_args, sc_rc);
432
        break;
433
        break;
433
    case SYS_IPC_CALL_ASYNC_SLOW:
434
    case SYS_IPC_CALL_ASYNC_SLOW:
434
        sc_ipc_call_async_slow(sc_args, sc_rc);
435
        sc_ipc_call_async_slow(sc_args, sc_rc);
435
        break;
436
        break;
436
    case SYS_IPC_CALL_SYNC_FAST:
437
    case SYS_IPC_CALL_SYNC_FAST:
437
        sc_ipc_call_sync_fast(sc_args);
438
        sc_ipc_call_sync_fast(sc_args);
438
        break;
439
        break;
439
    case SYS_IPC_CALL_SYNC_SLOW:
440
    case SYS_IPC_CALL_SYNC_SLOW:
440
        sc_ipc_call_sync_slow(sc_args);
441
        sc_ipc_call_sync_slow(sc_args);
441
        break;
442
        break;
442
    case SYS_IPC_WAIT:
443
    case SYS_IPC_WAIT:
443
        sc_ipc_wait(sc_args, sc_rc);
444
        sc_ipc_wait(sc_args, sc_rc);
444
        break;
445
        break;
445
    default:
446
    default:
446
        break;
447
        break;
447
    }
448
    }
448
 
449
 
449
    async_serialize_end();
450
    async_serialize_end();
450
}
451
}
451
 
452
 
452
static void event_thread_b(uintptr_t hash)
453
static void event_thread_b(uintptr_t hash)
453
{
454
{
454
    async_serialize_start();
455
    async_serialize_start();
455
    printf("New thread, hash 0x%lx\n", hash);
456
    printf("New thread, hash 0x%lx\n", hash);
456
    async_serialize_end();
457
    async_serialize_end();
457
 
458
 
458
    thread_trace_start(hash);
459
    thread_trace_start(hash);
459
}
460
}
460
 
461
 
461
static int trace_loop(void *thread_hash_arg)
462
static int trace_loop(void *thread_hash_arg)
462
{
463
{
463
    int rc;
464
    int rc;
464
    unsigned ev_type;
465
    unsigned ev_type;
465
    uintptr_t thread_hash;
466
    uintptr_t thread_hash;
466
    unsigned thread_id;
467
    unsigned thread_id;
467
    sysarg_t val0, val1;
468
    sysarg_t val0, val1;
468
 
469
 
469
    thread_hash = (uintptr_t)thread_hash_arg;
470
    thread_hash = (uintptr_t)thread_hash_arg;
470
    thread_id = next_thread_id++;
471
    thread_id = next_thread_id++;
471
 
472
 
472
    printf("Start tracing thread [%d] (hash 0x%lx).\n", thread_id, thread_hash);
473
    printf("Start tracing thread [%d] (hash 0x%lx).\n", thread_id, thread_hash);
473
 
474
 
474
    while (!abort_trace) {
475
    while (!abort_trace) {
475
 
476
 
476
        fibril_mutex_lock(&state_lock);
477
        fibril_mutex_lock(&state_lock);
477
        if (paused) {
478
        if (paused) {
478
            printf("Thread [%d] paused. Press R to resume.\n",
479
            printf("Thread [%d] paused. Press R to resume.\n",
479
                thread_id);
480
                thread_id);
480
 
481
 
481
            while (paused)
482
            while (paused)
482
                fibril_condvar_wait(&state_cv, &state_lock);
483
                fibril_condvar_wait(&state_cv, &state_lock);
483
 
484
 
484
            printf("Thread [%d] resumed.\n", thread_id);
485
            printf("Thread [%d] resumed.\n", thread_id);
485
        }
486
        }
486
        fibril_mutex_unlock(&state_lock);
487
        fibril_mutex_unlock(&state_lock);
487
 
488
 
488
        /* Run thread until an event occurs */
489
        /* Run thread until an event occurs */
489
        rc = udebug_go(phoneid, thread_hash,
490
        rc = udebug_go(phoneid, thread_hash,
490
            &ev_type, &val0, &val1);
491
            &ev_type, &val0, &val1);
491
 
492
 
492
//      printf("rc = %d, ev_type=%d\n", rc, ev_type);
493
//      printf("rc = %d, ev_type=%d\n", rc, ev_type);
493
        if (ev_type == UDEBUG_EVENT_FINISHED) {
494
        if (ev_type == UDEBUG_EVENT_FINISHED) {
494
            /* Done tracing this thread */
495
            /* Done tracing this thread */
495
            break;
496
            break;
496
        }
497
        }
497
 
498
 
498
        if (rc >= 0) {
499
        if (rc >= 0) {
499
            switch (ev_type) {
500
            switch (ev_type) {
500
            case UDEBUG_EVENT_SYSCALL_B:
501
            case UDEBUG_EVENT_SYSCALL_B:
501
                event_syscall_b(thread_id, thread_hash, val0, (int)val1);
502
                event_syscall_b(thread_id, thread_hash, val0, (int)val1);
502
                break;
503
                break;
503
            case UDEBUG_EVENT_SYSCALL_E:
504
            case UDEBUG_EVENT_SYSCALL_E:
504
                event_syscall_e(thread_id, thread_hash, val0, (int)val1);
505
                event_syscall_e(thread_id, thread_hash, val0, (int)val1);
505
                break;
506
                break;
506
            case UDEBUG_EVENT_STOP:
507
            case UDEBUG_EVENT_STOP:
507
                printf("Stop event\n");
508
                printf("Stop event\n");
508
                fibril_mutex_lock(&state_lock);
509
                fibril_mutex_lock(&state_lock);
509
                paused = 1;
510
                paused = 1;
510
                fibril_mutex_unlock(&state_lock);
511
                fibril_mutex_unlock(&state_lock);
511
                break;
512
                break;
512
            case UDEBUG_EVENT_THREAD_B:
513
            case UDEBUG_EVENT_THREAD_B:
513
                event_thread_b(val0);
514
                event_thread_b(val0);
514
                break;
515
                break;
515
            case UDEBUG_EVENT_THREAD_E:
516
            case UDEBUG_EVENT_THREAD_E:
516
                printf("Thread 0x%lx exited.\n", val0);
517
                printf("Thread 0x%lx exited.\n", val0);
517
                fibril_mutex_lock(&state_lock);
518
                fibril_mutex_lock(&state_lock);
518
                abort_trace = 1;
519
                abort_trace = 1;
519
                fibril_condvar_broadcast(&state_cv);
520
                fibril_condvar_broadcast(&state_cv);
520
                fibril_mutex_unlock(&state_lock);
521
                fibril_mutex_unlock(&state_lock);
521
                break;
522
                break;
522
            default:
523
            default:
523
                printf("Unknown event type %d.\n", ev_type);
524
                printf("Unknown event type %d.\n", ev_type);
524
                break;
525
                break;
525
            }
526
            }
526
        }
527
        }
527
 
528
 
528
    }
529
    }
529
 
530
 
530
    printf("Finished tracing thread [%d].\n", thread_id);
531
    printf("Finished tracing thread [%d].\n", thread_id);
531
    return 0;
532
    return 0;
532
}
533
}
533
 
534
 
534
void thread_trace_start(uintptr_t thread_hash)
535
void thread_trace_start(uintptr_t thread_hash)
535
{
536
{
536
    fid_t fid;
537
    fid_t fid;
537
 
538
 
538
    thash = thread_hash;
539
    thash = thread_hash;
539
 
540
 
540
    fid = fibril_create(trace_loop, (void *)thread_hash);
541
    fid = fibril_create(trace_loop, (void *)thread_hash);
541
    if (fid == 0) {
542
    if (fid == 0) {
542
        printf("Warning: Failed creating fibril\n");
543
        printf("Warning: Failed creating fibril\n");
543
    }
544
    }
544
    fibril_add_ready(fid);
545
    fibril_add_ready(fid);
545
}
546
}
546
 
547
 
547
static loader_t *preload_task(const char *path, char *const argv[],
548
static loader_t *preload_task(const char *path, char *const argv[],
548
    task_id_t *task_id)
549
    task_id_t *task_id)
549
{
550
{
550
    loader_t *ldr;
551
    loader_t *ldr;
551
    int rc;
552
    int rc;
552
 
553
 
553
    /* Spawn a program loader */   
554
    /* Spawn a program loader */   
554
    ldr = loader_connect();
555
    ldr = loader_connect();
555
    if (ldr == NULL)
556
    if (ldr == NULL)
556
        return 0;
557
        return 0;
557
 
558
 
558
    /* Get task ID. */
559
    /* Get task ID. */
559
    rc = loader_get_task_id(ldr, task_id);
560
    rc = loader_get_task_id(ldr, task_id);
560
    if (rc != EOK)
561
    if (rc != EOK)
561
        goto error;
562
        goto error;
562
 
563
 
563
    /* Send program pathname */
564
    /* Send program pathname */
564
    rc = loader_set_pathname(ldr, path);
565
    rc = loader_set_pathname(ldr, path);
565
    if (rc != EOK)
566
    if (rc != EOK)
566
        goto error;
567
        goto error;
567
 
568
 
568
    /* Send arguments */
569
    /* Send arguments */
569
    rc = loader_set_args(ldr, argv);
570
    rc = loader_set_args(ldr, argv);
570
    if (rc != EOK)
571
    if (rc != EOK)
571
        goto error;
572
        goto error;
572
 
573
 
573
    /* Send default files */
574
    /* Send default files */
574
    fdi_node_t *files[4];
575
    fdi_node_t *files[4];
575
    fdi_node_t stdin_node;
576
    fdi_node_t stdin_node;
576
    fdi_node_t stdout_node;
577
    fdi_node_t stdout_node;
577
    fdi_node_t stderr_node;
578
    fdi_node_t stderr_node;
578
   
579
   
579
    if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
580
    if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
580
        files[0] = &stdin_node;
581
        files[0] = &stdin_node;
581
    else
582
    else
582
        files[0] = NULL;
583
        files[0] = NULL;
583
   
584
   
584
    if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
585
    if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
585
        files[1] = &stdout_node;
586
        files[1] = &stdout_node;
586
    else
587
    else
587
        files[1] = NULL;
588
        files[1] = NULL;
588
   
589
   
589
    if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
590
    if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
590
        files[2] = &stderr_node;
591
        files[2] = &stderr_node;
591
    else
592
    else
592
        files[2] = NULL;
593
        files[2] = NULL;
593
   
594
   
594
    files[3] = NULL;
595
    files[3] = NULL;
595
   
596
   
596
    rc = loader_set_files(ldr, files);
597
    rc = loader_set_files(ldr, files);
597
    if (rc != EOK)
598
    if (rc != EOK)
598
        goto error;
599
        goto error;
599
 
600
 
600
    /* Load the program. */
601
    /* Load the program. */
601
    rc = loader_load_program(ldr);
602
    rc = loader_load_program(ldr);
602
    if (rc != EOK)
603
    if (rc != EOK)
603
        goto error;
604
        goto error;
604
 
605
 
605
    /* Success */
606
    /* Success */
606
    return ldr;
607
    return ldr;
607
 
608
 
608
    /* Error exit */
609
    /* Error exit */
609
error:
610
error:
610
    loader_abort(ldr);
611
    loader_abort(ldr);
611
    free(ldr);
612
    free(ldr);
612
    return NULL;
613
    return NULL;
613
}
614
}
614
 
615
 
615
static int cev_fibril(void *arg)
616
static int cev_fibril(void *arg)
616
{
617
{
617
    (void) arg;
618
    (void) arg;
618
 
619
 
619
    while (true) {
620
    while (true) {
620
        fibril_mutex_lock(&state_lock);
621
        fibril_mutex_lock(&state_lock);
621
        while (cev_valid)
622
        while (cev_valid)
622
            fibril_condvar_wait(&state_cv, &state_lock);
623
            fibril_condvar_wait(&state_cv, &state_lock);
623
        fibril_mutex_unlock(&state_lock);
624
        fibril_mutex_unlock(&state_lock);
624
 
625
 
625
        if (!console_get_event(fphone(stdin), &cev))
626
        if (!console_get_event(fphone(stdin), &cev))
626
            return -1;
627
            return -1;
627
 
628
 
628
        fibril_mutex_lock(&state_lock);
629
        fibril_mutex_lock(&state_lock);
629
        cev_valid = 1;
630
        cev_valid = 1;
630
        fibril_condvar_broadcast(&state_cv);
631
        fibril_condvar_broadcast(&state_cv);
631
        fibril_mutex_unlock(&state_lock);      
632
        fibril_mutex_unlock(&state_lock);      
632
    }
633
    }
633
}
634
}
634
 
635
 
635
static void trace_task(task_id_t task_id)
636
static void trace_task(task_id_t task_id)
636
{
637
{
637
    console_event_t ev;
638
    console_event_t ev;
638
    bool done;
639
    bool done;
639
    int i;
640
    int i;
640
    int rc;
641
    int rc;
641
 
642
 
642
    ipcp_init();
643
    ipcp_init();
643
 
644
 
644
    /*
645
    /*
645
     * User apps now typically have console on phone 3.
646
     * User apps now typically have console on phone 3.
646
     * (Phones 1 and 2 are used by the loader).
647
     * (Phones 1 and 2 are used by the loader).
647
     */
648
     */
648
    ipcp_connection_set(3, 0, proto_console);
649
    ipcp_connection_set(3, 0, proto_console);
649
 
650
 
650
    rc = get_thread_list();
651
    rc = get_thread_list();
651
    if (rc < 0) {
652
    if (rc < 0) {
652
        printf("Failed to get thread list (error %d)\n", rc);
653
        printf("Failed to get thread list (error %d)\n", rc);
653
        return;
654
        return;
654
    }
655
    }
655
 
656
 
656
    abort_trace = 0;
657
    abort_trace = 0;
657
 
658
 
658
    for (i = 0; i < n_threads; i++) {
659
    for (i = 0; i < n_threads; i++) {
659
        thread_trace_start(thread_hash_buf[i]);
660
        thread_trace_start(thread_hash_buf[i]);
660
    }
661
    }
661
 
662
 
662
    done = false;
663
    done = false;
663
 
664
 
664
    while (!done) {
665
    while (!done) {
665
        fibril_mutex_lock(&state_lock);
666
        fibril_mutex_lock(&state_lock);
666
        while (!cev_valid && !abort_trace)
667
        while (!cev_valid && !abort_trace)
667
            fibril_condvar_wait(&state_cv, &state_lock);
668
            fibril_condvar_wait(&state_cv, &state_lock);
668
        fibril_mutex_unlock(&state_lock);
669
        fibril_mutex_unlock(&state_lock);
669
 
670
 
670
        ev = cev;
671
        ev = cev;
671
 
672
 
672
        fibril_mutex_lock(&state_lock);
673
        fibril_mutex_lock(&state_lock);
673
        cev_valid = false;
674
        cev_valid = false;
674
        fibril_condvar_broadcast(&state_cv);
675
        fibril_condvar_broadcast(&state_cv);
675
        fibril_mutex_unlock(&state_lock);
676
        fibril_mutex_unlock(&state_lock);
676
 
677
 
677
        if (abort_trace)
678
        if (abort_trace)
678
            break;
679
            break;
679
 
680
 
680
        if (ev.type != KEY_PRESS)
681
        if (ev.type != KEY_PRESS)
681
            continue;
682
            continue;
682
 
683
 
683
        switch (ev.key) {
684
        switch (ev.key) {
684
        case KC_Q:
685
        case KC_Q:
685
            done = true;
686
            done = true;
686
            break;
687
            break;
687
        case KC_P:
688
        case KC_P:
688
            printf("Pause...\n");
689
            printf("Pause...\n");
689
            rc = udebug_stop(phoneid, thash);
690
            rc = udebug_stop(phoneid, thash);
690
            if (rc != EOK)
691
            if (rc != EOK)
691
                printf("Error: stop -> %d\n", rc);
692
                printf("Error: stop -> %d\n", rc);
692
            break;
693
            break;
693
        case KC_R:
694
        case KC_R:
694
            fibril_mutex_lock(&state_lock);
695
            fibril_mutex_lock(&state_lock);
695
            paused = 0;
696
            paused = 0;
696
            fibril_condvar_broadcast(&state_cv);
697
            fibril_condvar_broadcast(&state_cv);
697
            fibril_mutex_unlock(&state_lock);
698
            fibril_mutex_unlock(&state_lock);
698
            printf("Resume...\n");
699
            printf("Resume...\n");
699
            break;
700
            break;
700
        }
701
        }
701
    }
702
    }
702
 
703
 
703
    printf("\nTerminate debugging session...\n");
704
    printf("\nTerminate debugging session...\n");
704
    abort_trace = 1;
705
    abort_trace = 1;
705
    udebug_end(phoneid);
706
    udebug_end(phoneid);
706
    ipc_hangup(phoneid);
707
    ipc_hangup(phoneid);
707
 
708
 
708
    ipcp_cleanup();
709
    ipcp_cleanup();
709
 
710
 
710
    printf("Done\n");
711
    printf("Done\n");
711
    return;
712
    return;
712
}
713
}
713
 
714
 
714
static void main_init(void)
715
static void main_init(void)
715
{
716
{
716
    proto_t *p;
717
    proto_t *p;
717
    oper_t *o;
718
    oper_t *o;
718
 
719
 
719
    val_type_t arg_def[OPER_MAX_ARGS] = {
720
    val_type_t arg_def[OPER_MAX_ARGS] = {
720
        V_INTEGER,
721
        V_INTEGER,
721
        V_INTEGER,
722
        V_INTEGER,
722
        V_INTEGER,
723
        V_INTEGER,
723
        V_INTEGER,
724
        V_INTEGER,
724
        V_INTEGER      
725
        V_INTEGER      
725
    };
726
    };
726
 
727
 
727
    val_type_t resp_def[OPER_MAX_ARGS] = {
728
    val_type_t resp_def[OPER_MAX_ARGS] = {
728
        V_INTEGER,
729
        V_INTEGER,
729
        V_INTEGER,
730
        V_INTEGER,
730
        V_INTEGER,
731
        V_INTEGER,
731
        V_INTEGER,
732
        V_INTEGER,
732
        V_INTEGER
733
        V_INTEGER
733
    };
734
    };
734
 
735
 
735
    next_thread_id = 1;
736
    next_thread_id = 1;
736
    paused = 0;
737
    paused = 0;
737
    cev_valid = 0;
738
    cev_valid = 0;
738
 
739
 
739
    fibril_mutex_initialize(&state_lock);
740
    fibril_mutex_initialize(&state_lock);
740
    fibril_condvar_initialize(&state_cv);
741
    fibril_condvar_initialize(&state_cv);
741
 
742
 
742
    proto_init();
743
    proto_init();
743
 
744
 
744
    p = proto_new("vfs");
745
    p = proto_new("vfs");
745
    o = oper_new("read", 1, arg_def, V_ERRNO, 1, resp_def);
746
    o = oper_new("read", 1, arg_def, V_ERRNO, 1, resp_def);
746
    proto_add_oper(p, VFS_IN_READ, o);
747
    proto_add_oper(p, VFS_IN_READ, o);
747
    o = oper_new("write", 1, arg_def, V_ERRNO, 1, resp_def);
748
    o = oper_new("write", 1, arg_def, V_ERRNO, 1, resp_def);
748
    proto_add_oper(p, VFS_IN_WRITE, o);
749
    proto_add_oper(p, VFS_IN_WRITE, o);
749
    o = oper_new("truncate", 5, arg_def, V_ERRNO, 0, resp_def);
750
    o = oper_new("truncate", 5, arg_def, V_ERRNO, 0, resp_def);
750
    proto_add_oper(p, VFS_IN_TRUNCATE, o);
751
    proto_add_oper(p, VFS_IN_TRUNCATE, o);
751
    o = oper_new("mount", 2, arg_def, V_ERRNO, 0, resp_def);
752
    o = oper_new("mount", 2, arg_def, V_ERRNO, 0, resp_def);
752
    proto_add_oper(p, VFS_IN_MOUNT, o);
753
    proto_add_oper(p, VFS_IN_MOUNT, o);
753
/*  o = oper_new("unmount", 0, arg_def);
754
/*  o = oper_new("unmount", 0, arg_def);
754
    proto_add_oper(p, VFS_IN_UNMOUNT, o);*/
755
    proto_add_oper(p, VFS_IN_UNMOUNT, o);*/
755
    o = oper_new("open", 2, arg_def, V_INT_ERRNO, 0, resp_def);
756
    o = oper_new("open", 2, arg_def, V_INT_ERRNO, 0, resp_def);
756
    proto_add_oper(p, VFS_IN_OPEN, o);
757
    proto_add_oper(p, VFS_IN_OPEN, o);
757
    o = oper_new("close", 1, arg_def, V_ERRNO, 0, resp_def);
758
    o = oper_new("close", 1, arg_def, V_ERRNO, 0, resp_def);
758
    proto_add_oper(p, VFS_IN_CLOSE, o);
759
    proto_add_oper(p, VFS_IN_CLOSE, o);
759
    o = oper_new("seek", 3, arg_def, V_ERRNO, 0, resp_def);
760
    o = oper_new("seek", 3, arg_def, V_ERRNO, 0, resp_def);
760
    proto_add_oper(p, VFS_IN_SEEK, o);
761
    proto_add_oper(p, VFS_IN_SEEK, o);
761
    o = oper_new("mkdir", 1, arg_def, V_ERRNO, 0, resp_def);
762
    o = oper_new("mkdir", 1, arg_def, V_ERRNO, 0, resp_def);
762
    proto_add_oper(p, VFS_IN_MKDIR, o);
763
    proto_add_oper(p, VFS_IN_MKDIR, o);
763
    o = oper_new("unlink", 0, arg_def, V_ERRNO, 0, resp_def);
764
    o = oper_new("unlink", 0, arg_def, V_ERRNO, 0, resp_def);
764
    proto_add_oper(p, VFS_IN_UNLINK, o);
765
    proto_add_oper(p, VFS_IN_UNLINK, o);
765
    o = oper_new("rename", 0, arg_def, V_ERRNO, 0, resp_def);
766
    o = oper_new("rename", 0, arg_def, V_ERRNO, 0, resp_def);
766
    proto_add_oper(p, VFS_IN_RENAME, o);
767
    proto_add_oper(p, VFS_IN_RENAME, o);
767
 
768
 
768
    proto_register(SERVICE_VFS, p);
769
    proto_register(SERVICE_VFS, p);
769
 
770
 
770
    p = proto_new("console");
771
    p = proto_new("console");
771
    resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER;
772
    resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER;
772
    resp_def[2] = V_INTEGER; resp_def[3] = V_CHAR;
773
    resp_def[2] = V_INTEGER; resp_def[3] = V_CHAR;
773
    o = oper_new("getkey", 0, arg_def, V_ERRNO, 4, resp_def);
774
    o = oper_new("getkey", 0, arg_def, V_ERRNO, 4, resp_def);
774
 
775
 
775
    arg_def[0] = V_CHAR;
776
    arg_def[0] = V_CHAR;
776
    o = oper_new("clear", 0, arg_def, V_VOID, 0, resp_def);
777
    o = oper_new("clear", 0, arg_def, V_VOID, 0, resp_def);
777
    proto_add_oper(p, CONSOLE_CLEAR, o);
778
    proto_add_oper(p, CONSOLE_CLEAR, o);
778
 
779
 
779
    arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER;
780
    arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER;
780
    o = oper_new("goto", 2, arg_def, V_VOID, 0, resp_def);
781
    o = oper_new("goto", 2, arg_def, V_VOID, 0, resp_def);
781
    proto_add_oper(p, CONSOLE_GOTO, o);
782
    proto_add_oper(p, CONSOLE_GOTO, o);
782
 
783
 
783
    resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER;
784
    resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER;
784
    o = oper_new("getsize", 0, arg_def, V_INTEGER, 2, resp_def);
785
    o = oper_new("getsize", 0, arg_def, V_INTEGER, 2, resp_def);
785
    proto_add_oper(p, CONSOLE_GET_SIZE, o);
786
    proto_add_oper(p, CONSOLE_GET_SIZE, o);
786
 
787
 
787
    arg_def[0] = V_INTEGER;
788
    arg_def[0] = V_INTEGER;
788
    o = oper_new("set_style", 1, arg_def, V_VOID, 0, resp_def);
789
    o = oper_new("set_style", 1, arg_def, V_VOID, 0, resp_def);
789
    proto_add_oper(p, CONSOLE_SET_STYLE, o);
790
    proto_add_oper(p, CONSOLE_SET_STYLE, o);
790
    arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER; arg_def[2] = V_INTEGER;
791
    arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER; arg_def[2] = V_INTEGER;
791
    o = oper_new("set_color", 3, arg_def, V_VOID, 0, resp_def);
792
    o = oper_new("set_color", 3, arg_def, V_VOID, 0, resp_def);
792
    proto_add_oper(p, CONSOLE_SET_COLOR, o);
793
    proto_add_oper(p, CONSOLE_SET_COLOR, o);
793
    arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER;
794
    arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER;
794
    o = oper_new("set_rgb_color", 2, arg_def, V_VOID, 0, resp_def);
795
    o = oper_new("set_rgb_color", 2, arg_def, V_VOID, 0, resp_def);
795
    proto_add_oper(p, CONSOLE_SET_RGB_COLOR, o);
796
    proto_add_oper(p, CONSOLE_SET_RGB_COLOR, o);
796
    o = oper_new("cursor_visibility", 1, arg_def, V_VOID, 0, resp_def);
797
    o = oper_new("cursor_visibility", 1, arg_def, V_VOID, 0, resp_def);
797
    proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
798
    proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
798
 
799
 
799
    proto_console = p;
800
    proto_console = p;
800
    proto_register(SERVICE_CONSOLE, p);
801
    proto_register(SERVICE_CONSOLE, p);
801
}
802
}
802
 
803
 
803
static void print_syntax()
804
static void print_syntax()
804
{
805
{
805
    printf("Syntax:\n");
806
    printf("Syntax:\n");
806
    printf("\ttrace [+<events>] <executable> [<arg1> [...]]\n");
807
    printf("\ttrace [+<events>] <executable> [<arg1> [...]]\n");
807
    printf("or\ttrace [+<events>] -t <task_id>\n");
808
    printf("or\ttrace [+<events>] -t <task_id>\n");
808
    printf("Events: (default is +tp)\n");
809
    printf("Events: (default is +tp)\n");
809
    printf("\n");
810
    printf("\n");
810
    printf("\tt ... Thread creation and termination\n");
811
    printf("\tt ... Thread creation and termination\n");
811
    printf("\ts ... System calls\n");
812
    printf("\ts ... System calls\n");
812
    printf("\ti ... Low-level IPC\n");
813
    printf("\ti ... Low-level IPC\n");
813
    printf("\tp ... Protocol level\n");
814
    printf("\tp ... Protocol level\n");
814
    printf("\n");
815
    printf("\n");
815
    printf("Examples:\n");
816
    printf("Examples:\n");
816
    printf("\ttrace +s /app/tetris\n");
817
    printf("\ttrace +s /app/tetris\n");
817
    printf("\ttrace +tsip -t 12\n");
818
    printf("\ttrace +tsip -t 12\n");
818
}
819
}
819
 
820
 
820
static display_mask_t parse_display_mask(char *text)
821
static display_mask_t parse_display_mask(char *text)
821
{
822
{
822
    display_mask_t dm;
823
    display_mask_t dm;
823
    char *c;
824
    char *c;
824
 
825
 
825
    c = text;
826
    c = text;
826
 
827
 
827
    while (*c) {
828
    while (*c) {
828
        switch (*c) {
829
        switch (*c) {
829
        case 't': dm = dm | DM_THREAD; break;
830
        case 't': dm = dm | DM_THREAD; break;
830
        case 's': dm = dm | DM_SYSCALL; break;
831
        case 's': dm = dm | DM_SYSCALL; break;
831
        case 'i': dm = dm | DM_IPC; break;
832
        case 'i': dm = dm | DM_IPC; break;
832
        case 'p': dm = dm | DM_SYSTEM | DM_USER; break;
833
        case 'p': dm = dm | DM_SYSTEM | DM_USER; break;
833
        default:
834
        default:
834
            printf("Unexpected event type '%c'.\n", *c);
835
            printf("Unexpected event type '%c'.\n", *c);
835
            exit(1);
836
            exit(1);
836
        }
837
        }
837
 
838
 
838
        ++c;
839
        ++c;
839
    }
840
    }
840
 
841
 
841
    return dm;
842
    return dm;
842
}
843
}
843
 
844
 
844
static int parse_args(int argc, char *argv[])
845
static int parse_args(int argc, char *argv[])
845
{
846
{
846
    char *arg;
847
    char *arg;
847
    char *err_p;
848
    char *err_p;
848
 
849
 
849
    task_id = 0;
850
    task_id = 0;
850
 
851
 
851
    --argc; ++argv;
852
    --argc; ++argv;
852
 
853
 
853
    while (argc > 0) {
854
    while (argc > 0) {
854
        arg = *argv;
855
        arg = *argv;
855
        if (arg[0] == '+') {
856
        if (arg[0] == '+') {
856
            display_mask = parse_display_mask(&arg[1]);
857
            display_mask = parse_display_mask(&arg[1]);
857
        } else if (arg[0] == '-') {
858
        } else if (arg[0] == '-') {
858
            if (arg[1] == 't') {
859
            if (arg[1] == 't') {
859
                /* Trace an already running task */
860
                /* Trace an already running task */
860
                --argc; ++argv;
861
                --argc; ++argv;
861
                task_id = strtol(*argv, &err_p, 10);
862
                task_id = strtol(*argv, &err_p, 10);
862
                task_ldr = NULL;
863
                task_ldr = NULL;
-
 
864
                task_wait_for = 0;
863
                if (*err_p) {
865
                if (*err_p) {
864
                    printf("Task ID syntax error\n");
866
                    printf("Task ID syntax error\n");
865
                    print_syntax();
867
                    print_syntax();
866
                    return -1;
868
                    return -1;
867
                }
869
                }
868
            } else {
870
            } else {
869
                printf("Uknown option '%s'\n", arg[0]);
871
                printf("Uknown option '%s'\n", arg[0]);
870
                print_syntax();
872
                print_syntax();
871
                return -1;
873
                return -1;
872
            }
874
            }
873
        } else {
875
        } else {
874
            break;
876
            break;
875
        }
877
        }
876
 
878
 
877
        --argc; ++argv;
879
        --argc; ++argv;
878
    }
880
    }
879
 
881
 
880
    if (task_id != 0) {
882
    if (task_id != 0) {
881
        if (argc == 0) return 0;
883
        if (argc == 0) return 0;
882
        printf("Extra arguments\n");
884
        printf("Extra arguments\n");
883
        print_syntax();
885
        print_syntax();
884
        return -1;
886
        return -1;
885
    }
887
    }
886
 
888
 
887
    if (argc < 1) {
889
    if (argc < 1) {
888
        printf("Missing argument\n");
890
        printf("Missing argument\n");
889
        print_syntax();
891
        print_syntax();
890
        return -1;
892
        return -1;
891
    }
893
    }
892
 
894
 
893
    /* Preload the specified program file. */
895
    /* Preload the specified program file. */
894
    printf("Spawning '%s' with arguments:\n", *argv);
896
    printf("Spawning '%s' with arguments:\n", *argv);
895
    {
897
    {
896
        char **cp = argv;
898
        char **cp = argv;
897
        while (*cp) printf("'%s'\n", *cp++);
899
        while (*cp) printf("'%s'\n", *cp++);
898
    }
900
    }
899
    task_ldr = preload_task(*argv, argv, &task_id);
901
    task_ldr = preload_task(*argv, argv, &task_id);
-
 
902
    task_wait_for = 1;
900
 
903
 
901
    return 0;
904
    return 0;
902
}
905
}
903
 
906
 
904
int main(int argc, char *argv[])
907
int main(int argc, char *argv[])
905
{
908
{
906
    int rc;
909
    int rc;
-
 
910
    task_exit_t texit;
-
 
911
    int retval;
907
 
912
 
908
    printf("System Call / IPC Tracer\n");
913
    printf("System Call / IPC Tracer\n");
909
    printf("Controls: Q - Quit, P - Pause, R - Resume\n");
914
    printf("Controls: Q - Quit, P - Pause, R - Resume\n");
910
 
915
 
911
    display_mask = DM_THREAD | DM_SYSTEM | DM_USER;
916
    display_mask = DM_THREAD | DM_SYSTEM | DM_USER;
912
 
917
 
913
    if (parse_args(argc, argv) < 0)
918
    if (parse_args(argc, argv) < 0)
914
        return 1;
919
        return 1;
915
 
920
 
916
    main_init();
921
    main_init();
917
 
922
 
918
    rc = connect_task(task_id);
923
    rc = connect_task(task_id);
919
    if (rc < 0) {
924
    if (rc < 0) {
920
        printf("Failed connecting to task %lld.\n", task_id);
925
        printf("Failed connecting to task %lld.\n", task_id);
921
        return 1;
926
        return 1;
922
    }
927
    }
923
 
928
 
924
    printf("Connected to task %lld.\n", task_id);
929
    printf("Connected to task %lld.\n", task_id);
925
 
930
 
926
    if (task_ldr != NULL) {
931
    if (task_ldr != NULL)
927
        program_run();
932
        program_run();
928
    }
-
 
929
 
933
 
930
    cev_fibril_start();
934
    cev_fibril_start();
931
    trace_task(task_id);
935
    trace_task(task_id);
932
 
936
 
-
 
937
    if (task_wait_for) {
-
 
938
        printf("Waiting for task to exit.\n");
-
 
939
 
-
 
940
        rc = task_wait(task_id, &texit, &retval);
-
 
941
        if (rc != EOK) {
-
 
942
            printf("Failed waiting for task.\n");
-
 
943
            return -1;
-
 
944
        }
-
 
945
 
-
 
946
        if (texit == TASK_EXIT_NORMAL) {
-
 
947
            printf("Task exited normally, return value %d.\n",
-
 
948
                retval);
-
 
949
        } else {
-
 
950
            printf("Task exited unexpectedly.\n");
-
 
951
        }
-
 
952
    }
-
 
953
 
933
    return 0;
954
    return 0;
934
}
955
}
935
 
956
 
936
/** @}
957
/** @}
937
 */
958
 */
938
 
959