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