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