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