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