Subversion Repositories HelenOS

Rev

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

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