Subversion Repositories HelenOS

Rev

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

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