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