1 /* $OpenBSD: db_command.c,v 1.79 2017/10/19 16:58:05 bluhm Exp $ */ 2 /* $NetBSD: db_command.c,v 1.20 1996/03/30 22:30:05 christos Exp $ */ 3 4 /* 5 * Mach Operating System 6 * Copyright (c) 1993,1992,1991,1990 Carnegie Mellon University 7 * All Rights Reserved. 8 * 9 * Permission to use, copy, modify and distribute this software and its 10 * documentation is hereby granted, provided that both the copyright 11 * notice and this permission notice appear in all copies of the 12 * software, derivative works or modified versions, and any portions 13 * thereof, and that both notices appear in supporting documentation. 14 * 15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 17 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 18 * 19 * Carnegie Mellon requests users of this software to return to 20 * 21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 22 * School of Computer Science 23 * Carnegie Mellon University 24 * Pittsburgh PA 15213-3890 25 * 26 * any improvements or extensions that they make and grant Carnegie Mellon 27 * the rights to redistribute these changes. 28 */ 29 30 /* 31 * Command dispatcher. 32 */ 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/proc.h> 36 #include <sys/reboot.h> 37 #include <sys/extent.h> 38 #include <sys/pool.h> 39 #include <sys/msgbuf.h> 40 #include <sys/malloc.h> 41 #include <sys/mount.h> 42 43 #include <uvm/uvm_extern.h> 44 #include <machine/db_machdep.h> /* type definitions */ 45 46 #include <ddb/db_access.h> 47 #include <ddb/db_lex.h> 48 #include <ddb/db_output.h> 49 #include <ddb/db_command.h> 50 #include <ddb/db_break.h> 51 #include <ddb/db_watch.h> 52 #include <ddb/db_run.h> 53 #include <ddb/db_sym.h> 54 #include <ddb/db_variables.h> 55 #include <ddb/db_interface.h> 56 #include <ddb/db_extern.h> 57 58 #include <uvm/uvm_ddb.h> 59 60 /* 61 * Exported global variables 62 */ 63 int db_cmd_loop_done; 64 label_t *db_recover; 65 66 /* 67 * if 'ed' style: 'dot' is set at start of last item printed, 68 * and '+' points to next line. 69 * Otherwise: 'dot' points to next item, '..' points to last. 70 */ 71 boolean_t db_ed_style = TRUE; 72 73 db_addr_t db_dot; /* current location */ 74 db_addr_t db_last_addr; /* last explicit address typed */ 75 db_addr_t db_prev; /* last address examined 76 or written */ 77 db_addr_t db_next; /* next address to be examined 78 or written */ 79 80 int db_cmd_search(char *, struct db_command *, struct db_command **); 81 void db_cmd_list(struct db_command *); 82 void db_ctf_pprint_cmd(db_expr_t, int, db_expr_t,char *); 83 void db_map_print_cmd(db_expr_t, int, db_expr_t, char *); 84 void db_buf_print_cmd(db_expr_t, int, db_expr_t, char *); 85 void db_malloc_print_cmd(db_expr_t, int, db_expr_t, char *); 86 void db_mbuf_print_cmd(db_expr_t, int, db_expr_t, char *); 87 void db_mount_print_cmd(db_expr_t, int, db_expr_t, char *); 88 void db_show_all_mounts(db_expr_t, int, db_expr_t, char *); 89 void db_show_all_vnodes(db_expr_t, int, db_expr_t, char *); 90 void db_show_all_bufs(db_expr_t, int, db_expr_t, char *); 91 void db_object_print_cmd(db_expr_t, int, db_expr_t, char *); 92 void db_page_print_cmd(db_expr_t, int, db_expr_t, char *); 93 void db_extent_print_cmd(db_expr_t, int, db_expr_t, char *); 94 void db_pool_print_cmd(db_expr_t, int, db_expr_t, char *); 95 void db_proc_print_cmd(db_expr_t, int, db_expr_t, char *); 96 void db_uvmexp_print_cmd(db_expr_t, int, db_expr_t, char *); 97 void db_vnode_print_cmd(db_expr_t, int, db_expr_t, char *); 98 void db_nfsreq_print_cmd(db_expr_t, int, db_expr_t, char *); 99 void db_nfsnode_print_cmd(db_expr_t, int, db_expr_t, char *); 100 void db_help_cmd(db_expr_t, int, db_expr_t, char *); 101 void db_fncall(db_expr_t, int, db_expr_t, char *); 102 void db_boot_sync_cmd(db_expr_t, int, db_expr_t, char *); 103 void db_boot_crash_cmd(db_expr_t, int, db_expr_t, char *); 104 void db_boot_dump_cmd(db_expr_t, int, db_expr_t, char *); 105 void db_boot_halt_cmd(db_expr_t, int, db_expr_t, char *); 106 void db_boot_reboot_cmd(db_expr_t, int, db_expr_t, char *); 107 void db_boot_poweroff_cmd(db_expr_t, int, db_expr_t, char *); 108 void db_stack_trace_cmd(db_expr_t, int, db_expr_t, char *); 109 void db_dmesg_cmd(db_expr_t, int, db_expr_t, char *); 110 void db_show_panic_cmd(db_expr_t, int, db_expr_t, char *); 111 void db_bcstats_print_cmd(db_expr_t, int, db_expr_t, char *); 112 void db_struct_offset_cmd(db_expr_t, int, db_expr_t, char *); 113 void db_ctf_show_struct(db_expr_t, int, db_expr_t, char *); 114 void db_show_regs(db_expr_t, boolean_t, db_expr_t, char *); 115 void db_write_cmd(db_expr_t, boolean_t, db_expr_t, char *); 116 void db_witness_display(db_expr_t, int, db_expr_t, char *); 117 void db_witness_list(db_expr_t, int, db_expr_t, char *); 118 void db_witness_list_all(db_expr_t, int, db_expr_t, char *); 119 120 121 /* 122 * Utility routine - discard tokens through end-of-line. 123 */ 124 void 125 db_skip_to_eol(void) 126 { 127 int t; 128 do { 129 t = db_read_token(); 130 } while (t != tEOL); 131 } 132 133 /* 134 * Results of command search. 135 */ 136 #define CMD_UNIQUE 0 137 #define CMD_FOUND 1 138 #define CMD_NONE 2 139 #define CMD_AMBIGUOUS 3 140 141 /* 142 * Search for command prefix. 143 */ 144 int 145 db_cmd_search(char *name, struct db_command *table, struct db_command **cmdp) 146 { 147 struct db_command *cmd; 148 int result = CMD_NONE; 149 150 for (cmd = table; cmd->name != 0; cmd++) { 151 char *lp; 152 char *rp; 153 int c; 154 155 lp = name; 156 rp = cmd->name; 157 while ((c = *lp) == *rp) { 158 if (c == 0) { 159 /* complete match */ 160 *cmdp = cmd; 161 return (CMD_UNIQUE); 162 } 163 lp++; 164 rp++; 165 } 166 if (c == 0) { 167 /* end of name, not end of command - 168 partial match */ 169 if (result == CMD_FOUND) { 170 result = CMD_AMBIGUOUS; 171 /* but keep looking for a full match - 172 this lets us match single letters */ 173 } 174 else { 175 *cmdp = cmd; 176 result = CMD_FOUND; 177 } 178 } 179 } 180 return (result); 181 } 182 183 void 184 db_cmd_list(struct db_command *table) 185 { 186 struct db_command *cmd; 187 188 for (cmd = table; cmd->name != 0; cmd++) { 189 db_printf("%-12s", cmd->name); 190 db_end_line(12); 191 } 192 } 193 194 void 195 db_command(struct db_command **last_cmdp, struct db_command *cmd_table) 196 { 197 struct db_command *cmd; 198 int t; 199 char modif[TOK_STRING_SIZE]; 200 db_expr_t addr, count; 201 boolean_t have_addr = FALSE; 202 int result; 203 204 t = db_read_token(); 205 if (t == tEOL) { 206 /* empty line repeats last command, at 'next' */ 207 cmd = *last_cmdp; 208 addr = (db_expr_t)db_next; 209 have_addr = FALSE; 210 count = 1; 211 modif[0] = '\0'; 212 } 213 else if (t == tEXCL) { 214 db_fncall(0, 0, 0, NULL); 215 return; 216 } 217 else if (t != tIDENT) { 218 db_printf("?\n"); 219 db_flush_lex(); 220 return; 221 } 222 else { 223 /* 224 * Search for command 225 */ 226 while (cmd_table) { 227 result = db_cmd_search(db_tok_string, 228 cmd_table, 229 &cmd); 230 switch (result) { 231 case CMD_NONE: 232 db_printf("No such command\n"); 233 db_flush_lex(); 234 return; 235 case CMD_AMBIGUOUS: 236 db_printf("Ambiguous\n"); 237 db_flush_lex(); 238 return; 239 default: 240 break; 241 } 242 if ((cmd_table = cmd->more) != 0) { 243 t = db_read_token(); 244 if (t != tIDENT) { 245 db_cmd_list(cmd_table); 246 db_flush_lex(); 247 return; 248 } 249 } 250 } 251 252 if ((cmd->flag & CS_OWN) == 0) { 253 /* 254 * Standard syntax: 255 * command [/modifier] [addr] [,count] 256 */ 257 t = db_read_token(); 258 if (t == tSLASH) { 259 t = db_read_token(); 260 if (t != tIDENT) { 261 db_printf("Bad modifier\n"); 262 db_flush_lex(); 263 return; 264 } 265 db_strlcpy(modif, db_tok_string, sizeof(modif)); 266 } 267 else { 268 db_unread_token(t); 269 modif[0] = '\0'; 270 } 271 272 if (db_expression(&addr)) { 273 db_dot = (db_addr_t) addr; 274 db_last_addr = db_dot; 275 have_addr = TRUE; 276 } 277 else { 278 addr = (db_expr_t) db_dot; 279 have_addr = FALSE; 280 } 281 t = db_read_token(); 282 if (t == tCOMMA) { 283 if (!db_expression(&count)) { 284 db_printf("Count missing\n"); 285 db_flush_lex(); 286 return; 287 } 288 } 289 else { 290 db_unread_token(t); 291 count = -1; 292 } 293 if ((cmd->flag & CS_MORE) == 0) { 294 db_skip_to_eol(); 295 } 296 } 297 } 298 *last_cmdp = cmd; 299 if (cmd != 0) { 300 /* 301 * Execute the command. 302 */ 303 (*cmd->fcn)(addr, have_addr, count, modif); 304 305 if (cmd->flag & CS_SET_DOT) { 306 /* 307 * If command changes dot, set dot to 308 * previous address displayed (if 'ed' style). 309 */ 310 if (db_ed_style) { 311 db_dot = db_prev; 312 } 313 else { 314 db_dot = db_next; 315 } 316 } 317 else { 318 /* 319 * If command does not change dot, 320 * set 'next' location to be the same. 321 */ 322 db_next = db_dot; 323 } 324 } 325 } 326 327 /*ARGSUSED*/ 328 void 329 db_buf_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 330 { 331 boolean_t full = FALSE; 332 333 if (modif[0] == 'f') 334 full = TRUE; 335 336 vfs_buf_print((void *) addr, full, db_printf); 337 } 338 339 /*ARGSUSED*/ 340 void 341 db_map_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 342 { 343 boolean_t full = FALSE; 344 345 if (modif[0] == 'f') 346 full = TRUE; 347 348 uvm_map_printit((struct vm_map *) addr, full, db_printf); 349 } 350 351 /*ARGSUSED*/ 352 void 353 db_malloc_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 354 { 355 #if defined(MALLOC_DEBUG) 356 extern void debug_malloc_printit(int (*)(const char *, ...), vaddr_t); 357 358 if (!have_addr) 359 addr = 0; 360 361 debug_malloc_printit(db_printf, (vaddr_t)addr); 362 #else 363 malloc_printit(db_printf); 364 #endif 365 } 366 367 /*ARGSUSED*/ 368 void 369 db_mbuf_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 370 { 371 m_print((void *)addr, db_printf); 372 } 373 374 /*ARGSUSED*/ 375 void 376 db_socket_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 377 { 378 so_print((void *)addr, db_printf); 379 } 380 381 /*ARGSUSED*/ 382 void 383 db_mount_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 384 { 385 boolean_t full = FALSE; 386 387 if (modif[0] == 'f') 388 full = TRUE; 389 390 vfs_mount_print((struct mount *) addr, full, db_printf); 391 } 392 393 void 394 db_show_all_mounts(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 395 { 396 boolean_t full = FALSE; 397 struct mount *mp; 398 399 if (modif[0] == 'f') 400 full = TRUE; 401 402 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 403 db_printf("mountpoint %p\n", mp); 404 vfs_mount_print(mp, full, db_printf); 405 } 406 } 407 408 extern struct pool vnode_pool; 409 void 410 db_show_all_vnodes(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 411 { 412 boolean_t full = FALSE; 413 414 if (modif[0] == 'f') 415 full = TRUE; 416 417 pool_walk(&vnode_pool, full, db_printf, vfs_vnode_print); 418 } 419 420 extern struct pool bufpool; 421 void 422 db_show_all_bufs(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 423 { 424 boolean_t full = FALSE; 425 426 if (modif[0] == 'f') 427 full = TRUE; 428 429 pool_walk(&bufpool, full, db_printf, vfs_buf_print); 430 } 431 432 /*ARGSUSED*/ 433 void 434 db_object_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 435 { 436 boolean_t full = FALSE; 437 438 if (modif[0] == 'f') 439 full = TRUE; 440 441 uvm_object_printit((struct uvm_object *) addr, full, db_printf); 442 } 443 444 /*ARGSUSED*/ 445 void 446 db_page_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 447 { 448 boolean_t full = FALSE; 449 450 if (modif[0] == 'f') 451 full = TRUE; 452 453 uvm_page_printit((struct vm_page *) addr, full, db_printf); 454 } 455 456 /*ARGSUSED*/ 457 void 458 db_vnode_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 459 { 460 boolean_t full = FALSE; 461 462 if (modif[0] == 'f') 463 full = TRUE; 464 465 vfs_vnode_print((void *)addr, full, db_printf); 466 } 467 468 #ifdef NFSCLIENT 469 /*ARGSUSED*/ 470 void 471 db_nfsreq_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, 472 char *modif) 473 { 474 boolean_t full = FALSE; 475 476 if (modif[0] == 'f') 477 full = TRUE; 478 479 nfs_request_print((void *)addr, full, db_printf); 480 } 481 482 /*ARGSUSED*/ 483 void 484 db_nfsnode_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, 485 char *modif) 486 { 487 boolean_t full = FALSE; 488 489 if (modif[0] == 'f') 490 full = TRUE; 491 492 nfs_node_print((void *)addr, full, db_printf); 493 } 494 #endif 495 496 497 /*ARGSUSED*/ 498 void 499 db_show_panic_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 500 { 501 if (panicstr) 502 db_printf("%s\n", panicstr); 503 else 504 db_printf("the kernel did not panic\n"); /* yet */ 505 } 506 507 /*ARGSUSED*/ 508 void 509 db_extent_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 510 { 511 extent_print_all(); 512 } 513 514 /*ARGSUSED*/ 515 void 516 db_pool_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 517 { 518 pool_printit((struct pool *)addr, modif, db_printf); 519 } 520 521 /*ARGSUSED*/ 522 void 523 db_proc_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 524 { 525 if (!have_addr) 526 addr = (db_expr_t)curproc; 527 528 proc_printit((struct proc *)addr, modif, db_printf); 529 } 530 531 /*ARGSUSED*/ 532 void 533 db_uvmexp_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 534 { 535 uvmexp_print(db_printf); 536 } 537 538 void bcstats_print(int (*)(const char *, ...)); 539 540 /*ARGSUSED*/ 541 void 542 db_bcstats_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 543 { 544 bcstats_print(db_printf); 545 } 546 547 /* 548 * 'show' commands 549 */ 550 551 struct db_command db_show_all_cmds[] = { 552 { "procs", db_show_all_procs, 0, NULL }, 553 { "callout", db_show_callout, 0, NULL }, 554 { "pools", db_show_all_pools, 0, NULL }, 555 { "mounts", db_show_all_mounts, 0, NULL }, 556 { "vnodes", db_show_all_vnodes, 0, NULL }, 557 { "bufs", db_show_all_bufs, 0, NULL }, 558 #ifdef NFSCLIENT 559 { "nfsreqs", db_show_all_nfsreqs, 0, NULL }, 560 { "nfsnodes", db_show_all_nfsnodes, 0, NULL }, 561 #endif 562 #ifdef WITNESS 563 { "locks", db_witness_list_all, 0, NULL }, 564 #endif 565 { NULL, NULL, 0, NULL } 566 }; 567 568 struct db_command db_show_cmds[] = { 569 { "all", NULL, 0, db_show_all_cmds }, 570 { "bcstats", db_bcstats_print_cmd, 0, NULL }, 571 { "breaks", db_listbreak_cmd, 0, NULL }, 572 { "buf", db_buf_print_cmd, 0, NULL }, 573 { "extents", db_extent_print_cmd, 0, NULL }, 574 #ifdef WITNESS 575 { "locks", db_witness_list, 0, NULL }, 576 #endif 577 { "malloc", db_malloc_print_cmd, 0, NULL }, 578 { "map", db_map_print_cmd, 0, NULL }, 579 { "mbuf", db_mbuf_print_cmd, 0, NULL }, 580 { "mount", db_mount_print_cmd, 0, NULL }, 581 #ifdef NFSCLIENT 582 { "nfsreq", db_nfsreq_print_cmd, 0, NULL }, 583 { "nfsnode", db_nfsnode_print_cmd, 0, NULL }, 584 #endif 585 { "object", db_object_print_cmd, 0, NULL }, 586 { "page", db_page_print_cmd, 0, NULL }, 587 { "panic", db_show_panic_cmd, 0, NULL }, 588 { "pool", db_pool_print_cmd, 0, NULL }, 589 { "proc", db_proc_print_cmd, 0, NULL }, 590 { "registers", db_show_regs, 0, NULL }, 591 { "socket", db_socket_print_cmd, 0, NULL }, 592 { "struct", db_ctf_show_struct, CS_OWN, NULL }, 593 { "uvmexp", db_uvmexp_print_cmd, 0, NULL }, 594 { "vnode", db_vnode_print_cmd, 0, NULL }, 595 { "watches", db_listwatch_cmd, 0, NULL }, 596 #ifdef WITNESS 597 { "witness", db_witness_display, 0, NULL }, 598 #endif 599 { NULL, NULL, 0, NULL } 600 }; 601 602 struct db_command db_boot_cmds[] = { 603 { "sync", db_boot_sync_cmd, 0, 0 }, 604 { "crash", db_boot_crash_cmd, 0, 0 }, 605 { "dump", db_boot_dump_cmd, 0, 0 }, 606 { "halt", db_boot_halt_cmd, 0, 0 }, 607 { "reboot", db_boot_reboot_cmd, 0, 0 }, 608 { "poweroff", db_boot_poweroff_cmd, 0, 0 }, 609 { NULL, } 610 }; 611 612 struct db_command db_command_table[] = { 613 #ifdef DB_MACHINE_COMMANDS 614 /* this must be the first entry, if it exists */ 615 { "machine", NULL, 0, NULL}, 616 #endif 617 { "kill", db_kill_cmd, 0, NULL }, 618 { "print", db_print_cmd, 0, NULL }, 619 { "p", db_print_cmd, 0, NULL }, 620 { "pprint", db_ctf_pprint_cmd, CS_OWN, NULL }, 621 { "examine", db_examine_cmd, CS_SET_DOT, NULL }, 622 { "x", db_examine_cmd, CS_SET_DOT, NULL }, 623 { "search", db_search_cmd, CS_OWN|CS_SET_DOT, NULL }, 624 { "set", db_set_cmd, CS_OWN, NULL }, 625 { "write", db_write_cmd, CS_MORE|CS_SET_DOT, NULL }, 626 { "w", db_write_cmd, CS_MORE|CS_SET_DOT, NULL }, 627 { "delete", db_delete_cmd, 0, NULL }, 628 { "d", db_delete_cmd, 0, NULL }, 629 { "break", db_breakpoint_cmd, 0, NULL }, 630 { "dwatch", db_deletewatch_cmd, 0, NULL }, 631 { "watch", db_watchpoint_cmd, CS_MORE, NULL }, 632 { "step", db_single_step_cmd, 0, NULL }, 633 { "s", db_single_step_cmd, 0, NULL }, 634 { "continue", db_continue_cmd, 0, NULL }, 635 { "c", db_continue_cmd, 0, NULL }, 636 { "until", db_trace_until_call_cmd,0, NULL }, 637 { "next", db_trace_until_matching_cmd,0, NULL }, 638 { "match", db_trace_until_matching_cmd,0, NULL }, 639 { "trace", db_stack_trace_cmd, 0, NULL }, 640 { "call", db_fncall, CS_OWN, NULL }, 641 { "ps", db_show_all_procs, 0, NULL }, 642 { "callout", db_show_callout, 0, NULL }, 643 { "show", NULL, 0, db_show_cmds }, 644 { "boot", NULL, 0, db_boot_cmds }, 645 { "help", db_help_cmd, 0, NULL }, 646 { "hangman", db_hangman, 0, NULL }, 647 { "dmesg", db_dmesg_cmd, 0, NULL }, 648 { NULL, NULL, 0, NULL } 649 }; 650 651 #ifdef DB_MACHINE_COMMANDS 652 653 /* this function should be called to install the machine dependent 654 commands. It should be called before the debugger is enabled */ 655 void db_machine_commands_install(struct db_command *ptr) 656 { 657 db_command_table[0].more = ptr; 658 return; 659 } 660 661 #endif 662 663 struct db_command *db_last_command = NULL; 664 665 void 666 db_help_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif) 667 { 668 db_cmd_list(db_command_table); 669 } 670 671 void 672 db_command_loop(void) 673 { 674 label_t db_jmpbuf; 675 label_t *savejmp; 676 extern int db_output_line; 677 678 /* 679 * Initialize 'prev' and 'next' to dot. 680 */ 681 db_prev = db_dot; 682 db_next = db_dot; 683 684 db_cmd_loop_done = 0; 685 686 savejmp = db_recover; 687 db_recover = &db_jmpbuf; 688 (void) setjmp(&db_jmpbuf); 689 690 while (!db_cmd_loop_done) { 691 692 if (db_print_position() != 0) 693 db_printf("\n"); 694 db_output_line = 0; 695 696 #ifdef MULTIPROCESSOR 697 db_printf("ddb{%d}> ", CPU_INFO_UNIT(curcpu())); 698 #else 699 db_printf("ddb> "); 700 #endif 701 (void) db_read_line(); 702 703 db_command(&db_last_command, db_command_table); 704 } 705 706 db_recover = savejmp; 707 } 708 709 void 710 db_error(char *s) 711 { 712 if (s) 713 db_printf("%s", s); 714 db_flush_lex(); 715 if (db_recover != NULL) 716 longjmp(db_recover); 717 } 718 719 720 /* 721 * Call random function: 722 * !expr(arg,arg,arg) 723 */ 724 /*ARGSUSED*/ 725 void 726 db_fncall(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 727 { 728 db_expr_t fn_addr; 729 #define MAXARGS 11 730 db_expr_t args[MAXARGS]; 731 int nargs = 0; 732 db_expr_t retval; 733 db_expr_t (*func)(db_expr_t, ...); 734 int t; 735 char tmpfmt[28]; 736 737 if (!db_expression(&fn_addr)) { 738 db_printf("Bad function\n"); 739 db_flush_lex(); 740 return; 741 } 742 func = (db_expr_t (*)(db_expr_t, ...)) fn_addr; 743 744 t = db_read_token(); 745 if (t == tLPAREN) { 746 if (db_expression(&args[0])) { 747 nargs++; 748 while ((t = db_read_token()) == tCOMMA) { 749 if (nargs == MAXARGS) { 750 db_printf("Too many arguments\n"); 751 db_flush_lex(); 752 return; 753 } 754 if (!db_expression(&args[nargs])) { 755 db_printf("Argument missing\n"); 756 db_flush_lex(); 757 return; 758 } 759 nargs++; 760 } 761 db_unread_token(t); 762 } 763 if (db_read_token() != tRPAREN) { 764 db_printf("?\n"); 765 db_flush_lex(); 766 return; 767 } 768 } 769 db_skip_to_eol(); 770 771 while (nargs < MAXARGS) { 772 args[nargs++] = 0; 773 } 774 775 retval = (*func)(args[0], args[1], args[2], args[3], args[4], 776 args[5], args[6], args[7], args[8], args[9]); 777 db_printf("%s\n", db_format(tmpfmt, sizeof tmpfmt, retval, 778 DB_FORMAT_N, 1, 0)); 779 } 780 781 void 782 db_boot_sync_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif) 783 { 784 reboot(RB_AUTOBOOT | RB_TIMEBAD | RB_USERREQ); 785 } 786 787 void 788 db_boot_crash_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif) 789 { 790 reboot(RB_NOSYNC | RB_DUMP | RB_TIMEBAD | RB_USERREQ); 791 } 792 793 void 794 db_boot_dump_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif) 795 { 796 reboot(RB_DUMP | RB_TIMEBAD | RB_USERREQ); 797 } 798 799 void 800 db_boot_halt_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif) 801 { 802 reboot(RB_NOSYNC | RB_HALT | RB_TIMEBAD | RB_USERREQ); 803 } 804 805 void 806 db_boot_reboot_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif) 807 { 808 reboot(RB_AUTOBOOT | RB_NOSYNC | RB_TIMEBAD | RB_USERREQ); 809 } 810 811 void 812 db_boot_poweroff_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif) 813 { 814 reboot(RB_NOSYNC | RB_HALT | RB_POWERDOWN | RB_TIMEBAD | RB_USERREQ); 815 } 816 817 void 818 db_dmesg_cmd(db_expr_t addr, int haddr, db_expr_t count, char *modif) 819 { 820 int i, off; 821 char *p; 822 823 if (!msgbufp || msgbufp->msg_magic != MSG_MAGIC) 824 return; 825 off = msgbufp->msg_bufx; 826 if (off > msgbufp->msg_bufs) 827 off = 0; 828 for (i = 0, p = msgbufp->msg_bufc + off; 829 i < msgbufp->msg_bufs; i++, p++) { 830 if (p >= msgbufp->msg_bufc + msgbufp->msg_bufs) 831 p = msgbufp->msg_bufc; 832 if (*p != '\0') 833 db_putchar(*p); 834 } 835 db_putchar('\n'); 836 } 837 838 void 839 db_stack_trace_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count, 840 char *modif) 841 { 842 db_stack_trace_print(addr, have_addr, count, modif, db_printf); 843 } 844 845 void 846 db_show_regs(db_expr_t addr, int have_addr, db_expr_t count, char *modif) 847 { 848 struct db_variable *regp; 849 db_expr_t value, offset; 850 char * name; 851 char tmpfmt[28]; 852 853 for (regp = db_regs; regp < db_eregs; regp++) { 854 db_read_variable(regp, &value); 855 db_printf("%-12s%s", regp->name, db_format(tmpfmt, sizeof tmpfmt, 856 (long)value, DB_FORMAT_N, 1, sizeof(long) * 3)); 857 db_find_xtrn_sym_and_offset((db_addr_t)value, &name, &offset); 858 if (name != 0 && offset <= db_maxoff && offset != value) { 859 db_printf("\t%s", name); 860 if (offset != 0) 861 db_printf("+%s", db_format(tmpfmt, sizeof tmpfmt, 862 (long)offset, DB_FORMAT_R, 1, 0)); 863 } 864 db_printf("\n"); 865 } 866 db_print_loc_and_inst(PC_REGS(&ddb_regs)); 867 } 868 869 /* 870 * Write to file. 871 */ 872 /*ARGSUSED*/ 873 void 874 db_write_cmd(db_expr_t address, boolean_t have_addr, db_expr_t count, 875 char *modif) 876 { 877 db_addr_t addr; 878 db_expr_t old_value; 879 db_expr_t new_value; 880 int size; 881 boolean_t wrote_one = FALSE; 882 char tmpfmt[28]; 883 884 addr = (db_addr_t) address; 885 886 switch (modif[0]) { 887 case 'b': 888 size = 1; 889 break; 890 case 'h': 891 size = 2; 892 break; 893 case 'l': 894 case '\0': 895 size = 4; 896 break; 897 #ifdef __LP64__ 898 case 'q': 899 size = 8; 900 break; 901 #endif 902 default: 903 size = -1; 904 db_error("Unknown size\n"); 905 /*NOTREACHED*/ 906 } 907 908 while (db_expression(&new_value)) { 909 old_value = db_get_value(addr, size, FALSE); 910 db_printsym(addr, DB_STGY_ANY, db_printf); 911 db_printf("\t\t%s\t", db_format(tmpfmt, sizeof tmpfmt, 912 old_value, DB_FORMAT_N, 0, 8)); 913 db_printf("=\t%s\n", db_format(tmpfmt, sizeof tmpfmt, 914 new_value, DB_FORMAT_N, 0, 8)); 915 db_put_value(addr, size, new_value); 916 addr += size; 917 918 wrote_one = TRUE; 919 } 920 921 if (!wrote_one) { 922 db_error("Nothing written.\n"); 923 /*NOTREACHED*/ 924 } 925 926 db_next = addr; 927 db_prev = addr - size; 928 929 db_skip_to_eol(); 930 } 931