Rev 3100 | Rev 3472 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2911 | 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 debug |
||
| 30 | * @{ |
||
| 31 | */ |
||
| 32 | /** @file |
||
| 33 | */ |
||
| 34 | |||
| 35 | #include <stdio.h> |
||
| 36 | #include <stdlib.h> |
||
| 37 | #include <unistd.h> |
||
| 38 | #include <syscall.h> |
||
| 39 | #include <ipc/ipc.h> |
||
| 40 | #include <fibril.h> |
||
| 41 | #include <errno.h> |
||
| 42 | #include <udebug.h> |
||
| 43 | #include <async.h> |
||
| 44 | #include <string.h> |
||
| 45 | |||
| 46 | #include "cmd.h" |
||
| 2936 | svoboda | 47 | #include "cons.h" |
| 2938 | svoboda | 48 | #include "dthread.h" |
| 3005 | svoboda | 49 | #include "breakpoint.h" |
| 2923 | svoboda | 50 | #include "include/arch.h" |
| 2915 | svoboda | 51 | #include "main.h" |
| 2911 | svoboda | 52 | |
| 53 | void thread_debug_start(unsigned thread_hash); |
||
| 54 | |||
| 2936 | svoboda | 55 | #define IN_BUF_SIZE 64 |
| 56 | static char in_buf[IN_BUF_SIZE]; |
||
| 2911 | svoboda | 57 | |
| 58 | #define MAX_ARGC 10 |
||
| 59 | int cmd_argc; |
||
| 60 | char *cmd_argv[MAX_ARGC + 1]; /* need one spare field for cmd_split() */ |
||
| 61 | |||
| 2937 | svoboda | 62 | |
| 2911 | svoboda | 63 | int next_thread_id; |
| 64 | |||
| 65 | int app_phone; |
||
| 66 | volatile bool abort_debug; |
||
| 67 | |||
| 68 | volatile int paused; |
||
| 69 | |||
| 2938 | svoboda | 70 | static void command_split(char *cmd_str) |
| 2911 | svoboda | 71 | { |
| 72 | char *p = cmd_str; |
||
| 73 | |||
| 74 | if (*p == '\0') { |
||
| 75 | cmd_argc = 0; |
||
| 76 | return; |
||
| 77 | } |
||
| 78 | |||
| 79 | cmd_argc = 1; |
||
| 80 | cmd_argv[0] = p; |
||
| 81 | |||
| 82 | while (*p != '\0') { |
||
| 83 | if (*p == ' ') { |
||
| 84 | cmd_argv[cmd_argc++] = p + 1; |
||
| 85 | *p = '\0'; |
||
| 86 | } |
||
| 87 | ++p; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 2938 | svoboda | 91 | static void command_run(void) |
| 2911 | svoboda | 92 | { |
| 93 | int i; |
||
| 94 | int cmp_len; |
||
| 95 | int len; |
||
| 96 | |||
| 97 | int idx_found; |
||
| 98 | int num_found; |
||
| 99 | |||
| 100 | len = strlen(cmd_argv[0]); |
||
| 101 | cmp_len = 1; |
||
| 102 | |||
| 2946 | svoboda | 103 | /* Silence warnings */ |
| 104 | num_found = 0; |
||
| 105 | idx_found = 0; |
||
| 106 | |||
| 2911 | svoboda | 107 | while (cmp_len <= len + 1) { |
| 108 | |||
| 109 | num_found = 0; |
||
| 110 | i = 0; |
||
| 111 | while (cmd_table[i].name != NULL) { |
||
| 112 | if (strncmp(cmd_table[i].name, cmd_argv[0], cmp_len) == 0) { |
||
| 113 | idx_found = i; |
||
| 114 | ++num_found; |
||
| 115 | } |
||
| 116 | ++i; |
||
| 117 | } |
||
| 118 | |||
| 119 | if (num_found < 2) break; |
||
| 120 | |||
| 2947 | svoboda | 121 | ++cmp_len; |
| 2911 | svoboda | 122 | } |
| 123 | |||
| 124 | if (num_found == 0) { |
||
| 2936 | svoboda | 125 | cons_printf("Unknown command. Try one of:\n"); |
| 2911 | svoboda | 126 | cmd_help(0, NULL); |
| 127 | return; |
||
| 128 | } |
||
| 129 | |||
| 130 | if (cmd_argc - 1 != cmd_table[idx_found].argc) { |
||
| 2936 | svoboda | 131 | cons_printf("Command '%s' expects %d arguments\n", |
| 2911 | svoboda | 132 | cmd_table[idx_found].name, cmd_table[idx_found].argc); |
| 133 | return; |
||
| 134 | } |
||
| 135 | |||
| 136 | (*cmd_table[idx_found].proc)(cmd_argc, cmd_argv); |
||
| 137 | } |
||
| 138 | |||
| 2938 | svoboda | 139 | static void thread_stop(void) |
| 2936 | svoboda | 140 | { |
| 2939 | svoboda | 141 | dthread_t *dt; |
| 2940 | svoboda | 142 | uintptr_t pc; |
| 2939 | svoboda | 143 | |
| 144 | dt = dthread_get(); |
||
| 2940 | svoboda | 145 | pc = dthread_get_pc(dt); |
| 146 | cons_printf("[thread %d] stopped at 0x%lx\n", dt->id, pc); |
||
| 2939 | svoboda | 147 | dthread_stop_me(); |
| 148 | cons_printf("[thread %d] go\n", dt->id); |
||
| 2936 | svoboda | 149 | } |
| 150 | |||
| 2935 | svoboda | 151 | /* |
| 152 | * Called by a fibril (from arch code) when a breakpoint is hit. |
||
| 153 | */ |
||
| 3005 | svoboda | 154 | void breakpoint_hit(breakpoint_t *b) |
| 2935 | svoboda | 155 | { |
| 3005 | svoboda | 156 | dthread_t *dt; |
| 157 | |||
| 158 | dt = dthread_get(); |
||
| 159 | cons_printf("Thread %d hit breakpoint %d at 0x%lx\n", |
||
| 160 | dt->id, b->id, b->addr); |
||
| 2936 | svoboda | 161 | thread_stop(); |
| 2935 | svoboda | 162 | } |
| 2911 | svoboda | 163 | |
| 2942 | svoboda | 164 | /* |
| 165 | * Called by a fibril (from arch code) when a single instruction |
||
| 166 | * in singlestep is executed |
||
| 167 | */ |
||
| 168 | void singlestep_hit(void) |
||
| 169 | { |
||
| 170 | cons_printf("singlestep hit\n"); |
||
| 171 | thread_stop(); |
||
| 172 | } |
||
| 173 | |||
| 3429 | svoboda | 174 | static int task_connect(task_id_t task_id) |
| 2911 | svoboda | 175 | { |
| 176 | int rc; |
||
| 2918 | svoboda | 177 | unsigned evmask; |
| 2911 | svoboda | 178 | |
| 3429 | svoboda | 179 | cons_printf("ipc_connect_kbox(%;;d)... ", task_id); |
| 180 | rc = ipc_connect_kbox(task_id); |
||
| 2936 | svoboda | 181 | cons_printf("-> %d\n", rc); |
| 2911 | svoboda | 182 | app_phone = rc; |
| 183 | if (rc < 0) return rc; |
||
| 184 | |||
| 2936 | svoboda | 185 | cons_printf("udebug_begin()... "); |
| 2911 | svoboda | 186 | rc = udebug_begin(app_phone); |
| 2936 | svoboda | 187 | cons_printf("-> %d\n", rc); |
| 2911 | svoboda | 188 | if (rc < 0) return rc; |
| 189 | |||
| 2918 | svoboda | 190 | evmask = UDEBUG_EM_ALL & ~(UDEBUG_EM_SYSCALL_B | UDEBUG_EM_SYSCALL_E); |
| 2936 | svoboda | 191 | cons_printf("udebug_set_evmask(0x%x)... ", evmask); |
| 2918 | svoboda | 192 | rc = udebug_set_evmask(app_phone, evmask); |
| 2936 | svoboda | 193 | cons_printf("-> %d\n", rc); |
| 2911 | svoboda | 194 | if (rc < 0) return rc; |
| 195 | |||
| 196 | return 0; |
||
| 197 | } |
||
| 198 | |||
| 2940 | svoboda | 199 | #define THASH_BUF_INIT_LENGTH 32 |
| 200 | |||
| 201 | static int get_thread_list(thash_t **thash_buf_ptr, int *n) |
||
| 2911 | svoboda | 202 | { |
| 203 | int rc; |
||
| 2946 | svoboda | 204 | size_t tb_copied; |
| 205 | size_t tb_needed; |
||
| 2911 | svoboda | 206 | int i; |
| 2940 | svoboda | 207 | size_t tb_size; |
| 208 | thash_t *thash_buf; |
||
| 2911 | svoboda | 209 | |
| 2940 | svoboda | 210 | tb_size = THASH_BUF_INIT_LENGTH * sizeof(thash_t); |
| 211 | thash_buf = malloc(tb_size); |
||
| 212 | |||
| 2946 | svoboda | 213 | rc = udebug_thread_read(app_phone, thash_buf, |
| 2940 | svoboda | 214 | tb_size, &tb_copied, &tb_needed); |
| 2911 | svoboda | 215 | if (rc < 0) return rc; |
| 216 | |||
| 2940 | svoboda | 217 | if (tb_needed > tb_size) { |
| 218 | /* Larger buffer needed */ |
||
| 2911 | svoboda | 219 | |
| 2940 | svoboda | 220 | free(thash_buf); |
| 221 | |||
| 222 | tb_size = tb_needed; |
||
| 223 | thash_buf = malloc(tb_size); |
||
| 224 | |||
| 225 | if (!thash_buf) { |
||
| 226 | printf("malloc failed\n"); |
||
| 227 | exit(1); |
||
| 228 | } |
||
| 229 | |||
| 230 | /* Try again */ |
||
| 231 | |||
| 2946 | svoboda | 232 | rc = udebug_thread_read(app_phone, thash_buf, |
| 2940 | svoboda | 233 | tb_size, &tb_copied, &tb_needed); |
| 234 | |||
| 235 | if (rc < 0) return rc; |
||
| 2911 | svoboda | 236 | } |
| 237 | |||
| 2940 | svoboda | 238 | *n = tb_copied / sizeof(thash_t); |
| 239 | |||
| 240 | cons_printf("thread hashes:"); |
||
| 241 | |||
| 242 | for (i = 0; i < *n; ++i) { |
||
| 243 | cons_printf("0x%x\n", thash_buf[i]); |
||
| 244 | } |
||
| 245 | |||
| 246 | cons_printf("Total of %u threads\n", *n); |
||
| 247 | |||
| 248 | *thash_buf_ptr = thash_buf; |
||
| 249 | |||
| 250 | return 0; |
||
| 2911 | svoboda | 251 | } |
| 252 | |||
| 2946 | svoboda | 253 | static void event_thread_b(unsigned hash) |
| 2911 | svoboda | 254 | { |
| 255 | async_serialize_start(); |
||
| 2936 | svoboda | 256 | cons_printf("new thread, hash 0x%x\n", hash); |
| 2911 | svoboda | 257 | async_serialize_end(); |
| 258 | |||
| 259 | thread_debug_start(hash); |
||
| 260 | } |
||
| 261 | |||
| 2923 | svoboda | 262 | static void debug_event(thash_t thash, udebug_event_t ev_type, sysarg_t val0) |
| 263 | { |
||
| 264 | switch (ev_type) { |
||
| 265 | case UDEBUG_EVENT_STOP: |
||
| 2936 | svoboda | 266 | cons_printf("stop event\n"); |
| 2939 | svoboda | 267 | thread_stop(); |
| 2923 | svoboda | 268 | break; |
| 269 | case UDEBUG_EVENT_THREAD_B: |
||
| 270 | event_thread_b(val0); |
||
| 271 | break; |
||
| 272 | case UDEBUG_EVENT_THREAD_E: |
||
| 2936 | svoboda | 273 | cons_printf("thread 0x%x exited\n", val0); |
| 2923 | svoboda | 274 | abort_debug = true; |
| 275 | break; |
||
| 276 | case UDEBUG_EVENT_BREAKPOINT: |
||
| 277 | arch_event_breakpoint(thash); |
||
| 278 | break; |
||
| 279 | case UDEBUG_EVENT_TRAP: |
||
| 2942 | svoboda | 280 | arch_event_trap(dthread_get()); |
| 2923 | svoboda | 281 | break; |
| 282 | default: |
||
| 2936 | svoboda | 283 | cons_printf("unknown event type %d\n", ev_type); |
| 2923 | svoboda | 284 | break; |
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 2946 | svoboda | 288 | static int debug_loop(void *dt_arg) |
| 2911 | svoboda | 289 | { |
| 290 | int rc; |
||
| 2923 | svoboda | 291 | udebug_event_t ev_type; |
| 2911 | svoboda | 292 | unsigned val0, val1; |
| 2938 | svoboda | 293 | dthread_t *dt; |
| 2911 | svoboda | 294 | |
| 2938 | svoboda | 295 | dt = (dthread_t *)dt_arg; |
| 2911 | svoboda | 296 | |
| 2938 | svoboda | 297 | cons_printf("debug_loop(%d)\n", dt->id); |
| 2911 | svoboda | 298 | |
| 299 | while (!abort_debug) { |
||
| 300 | |||
| 301 | /* Run thread until an event occurs */ |
||
| 2938 | svoboda | 302 | rc = udebug_go(app_phone, dt->hash, &ev_type, &val0, &val1); |
| 2911 | svoboda | 303 | |
| 304 | if (ev_type == UDEBUG_EVENT_FINISHED) { |
||
| 2938 | svoboda | 305 | cons_printf("thread %u debugging finished\n", dt->id); |
| 2911 | svoboda | 306 | break; |
| 307 | } |
||
| 2939 | svoboda | 308 | if (rc >= 0) debug_event(dt->hash, ev_type, val0); |
| 2911 | svoboda | 309 | } |
| 310 | |||
| 2938 | svoboda | 311 | cons_printf("debug_loop(%d) exiting\n", dt->id); |
| 2946 | svoboda | 312 | return 0; |
| 2911 | svoboda | 313 | } |
| 314 | |||
| 2937 | svoboda | 315 | void thread_debug_start(unsigned thash) |
| 2911 | svoboda | 316 | { |
| 317 | fid_t fid; |
||
| 2938 | svoboda | 318 | dthread_t *dt; |
| 2911 | svoboda | 319 | |
| 2938 | svoboda | 320 | dt = dthread_new(thash); |
| 2911 | svoboda | 321 | |
| 2938 | svoboda | 322 | fid = fibril_create(debug_loop, (void *)dt); |
| 2911 | svoboda | 323 | if (fid == 0) { |
| 2936 | svoboda | 324 | cons_printf("Warning: Failed creating fibril\n"); |
| 2911 | svoboda | 325 | } |
| 2939 | svoboda | 326 | dt->fid = fid; |
| 327 | |||
| 2911 | svoboda | 328 | fibril_add_ready(fid); |
| 329 | } |
||
| 330 | |||
| 3429 | svoboda | 331 | static void debug_active_task(task_id_t task_id) |
| 2911 | svoboda | 332 | { |
| 333 | int taskid; |
||
| 334 | int i; |
||
| 335 | int rc; |
||
| 336 | |||
| 2940 | svoboda | 337 | thash_t *thash_buffer; |
| 338 | int n_threads; |
||
| 339 | |||
| 2936 | svoboda | 340 | cons_printf("Breakpoint Debugger\n"); |
| 2911 | svoboda | 341 | |
| 3429 | svoboda | 342 | rc = task_connect(task_id); |
| 2911 | svoboda | 343 | if (rc < 0) { |
| 3429 | svoboda | 344 | cons_printf("Failed to connect to task %lld\n", task_id); |
| 2911 | svoboda | 345 | return; |
| 346 | } |
||
| 347 | |||
| 3429 | svoboda | 348 | cons_printf("Connected to task %lld\n", task_id); |
| 2911 | svoboda | 349 | |
| 2940 | svoboda | 350 | rc = get_thread_list(&thash_buffer, &n_threads); |
| 2911 | svoboda | 351 | if (rc < 0) { |
| 2940 | svoboda | 352 | cons_printf("Failed to get thread list\n", rc); |
| 2911 | svoboda | 353 | return; |
| 354 | } |
||
| 355 | |||
| 356 | abort_debug = false; |
||
| 357 | |||
| 2940 | svoboda | 358 | for (i = 0; i < n_threads; i++) { |
| 359 | thread_debug_start(thash_buffer[i]); |
||
| 2911 | svoboda | 360 | } |
| 361 | |||
| 362 | while (!quit) { |
||
| 2936 | svoboda | 363 | cons_read_line(in_buf, IN_BUF_SIZE); |
| 2911 | svoboda | 364 | command_split(in_buf); |
| 365 | if (cmd_argc == 0) continue; |
||
| 366 | |||
| 367 | command_run(); |
||
| 368 | } |
||
| 369 | |||
| 2936 | svoboda | 370 | cons_printf("terminate debugging session...\n"); |
| 2911 | svoboda | 371 | abort_debug = true; |
| 372 | udebug_end(app_phone); |
||
| 373 | ipc_hangup(app_phone); |
||
| 374 | |||
| 2936 | svoboda | 375 | cons_printf("done\n"); |
| 2911 | svoboda | 376 | return; |
| 377 | } |
||
| 378 | |||
| 379 | static void main_init(void) |
||
| 380 | { |
||
| 381 | next_thread_id = 1; |
||
| 382 | paused = 0; |
||
| 2938 | svoboda | 383 | |
| 384 | list_initialize(&dthreads); |
||
| 3005 | svoboda | 385 | breakpoint_init(); |
| 2938 | svoboda | 386 | cwt = NULL; |
| 2911 | svoboda | 387 | } |
| 388 | |||
| 3429 | svoboda | 389 | static void print_syntax() |
| 2911 | svoboda | 390 | { |
| 3429 | svoboda | 391 | printf("syntax: debug <task_id>\n"); |
| 392 | } |
||
| 2911 | svoboda | 393 | |
| 3429 | svoboda | 394 | int main(int argc, char *argv[]) |
| 395 | { |
||
| 396 | task_id_t task_id; |
||
| 397 | char *err_p; |
||
| 398 | |||
| 399 | if (argc != 2) { |
||
| 400 | printf("Mising argument\n"); |
||
| 401 | print_syntax(); |
||
| 402 | return 1; |
||
| 2911 | svoboda | 403 | } |
| 3429 | svoboda | 404 | |
| 405 | task_id = strtol(argv[1], &err_p, 10); |
||
| 406 | |||
| 407 | if (*err_p) { |
||
| 408 | printf("Task ID syntax error\n"); |
||
| 409 | print_syntax(); |
||
| 410 | return 1; |
||
| 411 | } |
||
| 412 | |||
| 413 | main_init(); |
||
| 414 | debug_active_task(task_id); |
||
| 2911 | svoboda | 415 | } |
| 416 | |||
| 417 | /** @} |
||
| 418 | */ |