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