Subversion Repositories HelenOS

Rev

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

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