Subversion Repositories HelenOS

Rev

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

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