1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 * Copyright 2012, Josef 'Jeff' Sipek <jeffpc@31bits.net>. All rights reserved. 25 */ 26 27 /* 28 * Copyright (c) 2013, Joyent, Inc. All rights reserved. 29 */ 30 31 #include <sys/types.h> 32 #include <sys/mman.h> 33 #include <sys/priocntl.h> 34 #include <sys/rtpriocntl.h> 35 #include <sys/resource.h> 36 #include <sys/termios.h> 37 #include <sys/param.h> 38 #include <sys/regset.h> 39 #include <sys/frame.h> 40 #include <sys/stack.h> 41 #include <sys/reg.h> 42 43 #include <libproc.h> 44 #include <libscf.h> 45 #include <alloca.h> 46 #include <unistd.h> 47 #include <string.h> 48 #include <stdlib.h> 49 #include <fcntl.h> 50 #include <dlfcn.h> 51 #include <libctf.h> 52 #include <errno.h> 53 #include <kvm.h> 54 55 #include <mdb/mdb_lex.h> 56 #include <mdb/mdb_debug.h> 57 #include <mdb/mdb_signal.h> 58 #include <mdb/mdb_string.h> 59 #include <mdb/mdb_modapi.h> 60 #include <mdb/mdb_target.h> 61 #include <mdb/mdb_gelf.h> 62 #include <mdb/mdb_conf.h> 63 #include <mdb/mdb_err.h> 64 #include <mdb/mdb_io_impl.h> 65 #include <mdb/mdb_frame.h> 66 #include <mdb/mdb_set.h> 67 #include <kmdb/kmdb_kctl.h> 68 #include <mdb/mdb.h> 69 70 #ifndef STACK_BIAS 71 #define STACK_BIAS 0 72 #endif 73 74 #if defined(__sparc) 75 #define STACK_REGISTER SP 76 #else 77 #define STACK_REGISTER REG_FP 78 #endif 79 80 #ifdef _LP64 81 #define MDB_DEF_IPATH \ 82 "%r/usr/platform/%p/lib/adb/%i:" \ 83 "%r/usr/platform/%m/lib/adb/%i:" \ 84 "%r/usr/lib/adb/%i" 85 #define MDB_DEF_LPATH \ 86 "%r/usr/platform/%p/lib/mdb/%t/%i:" \ 87 "%r/usr/platform/%m/lib/mdb/%t/%i:" \ 88 "%r/usr/lib/mdb/%t/%i" 89 #else 90 #define MDB_DEF_IPATH \ 91 "%r/usr/platform/%p/lib/adb:" \ 92 "%r/usr/platform/%m/lib/adb:" \ 93 "%r/usr/lib/adb" 94 #define MDB_DEF_LPATH \ 95 "%r/usr/platform/%p/lib/mdb/%t:" \ 96 "%r/usr/platform/%m/lib/mdb/%t:" \ 97 "%r/usr/lib/mdb/%t" 98 #endif 99 100 #define MDB_DEF_PROMPT "> " 101 102 /* 103 * Similar to the panic_* variables in the kernel, we keep some relevant 104 * information stored in a set of global _mdb_abort_* variables; in the 105 * event that the debugger dumps core, these will aid core dump analysis. 106 */ 107 const char *volatile _mdb_abort_str; /* reason for failure */ 108 siginfo_t _mdb_abort_info; /* signal info for fatal signal */ 109 ucontext_t _mdb_abort_ctx; /* context fatal signal interrupted */ 110 int _mdb_abort_rcount; /* number of times resume requested */ 111 int _mdb_self_fd = -1; /* fd for self as for valid_frame */ 112 113 static void 114 terminate(int status) 115 { 116 (void) mdb_signal_blockall(); 117 mdb_destroy(); 118 exit(status); 119 } 120 121 static void 122 print_frame(uintptr_t pc, int fnum) 123 { 124 Dl_info dli; 125 126 if (dladdr((void *)pc, &dli)) { 127 mdb_iob_printf(mdb.m_err, " [%d] %s`%s+0x%lx()\n", fnum, 128 strbasename(dli.dli_fname), dli.dli_sname, 129 pc - (uintptr_t)dli.dli_saddr); 130 } else 131 mdb_iob_printf(mdb.m_err, " [%d] %p()\n", fnum, pc); 132 } 133 134 static int 135 valid_frame(struct frame *fr) 136 { 137 static struct frame fake; 138 uintptr_t addr = (uintptr_t)fr; 139 140 if (pread(_mdb_self_fd, &fake, sizeof (fake), addr) != sizeof (fake)) { 141 mdb_iob_printf(mdb.m_err, " invalid frame (%p)\n", fr); 142 return (0); 143 } 144 145 if (addr & (STACK_ALIGN - 1)) { 146 mdb_iob_printf(mdb.m_err, " mis-aligned frame (%p)\n", fr); 147 return (0); 148 } 149 150 return (1); 151 } 152 153 /*ARGSUSED*/ 154 static void 155 flt_handler(int sig, siginfo_t *sip, ucontext_t *ucp, void *data) 156 { 157 static const struct rlimit rl = { 158 (rlim_t)RLIM_INFINITY, (rlim_t)RLIM_INFINITY 159 }; 160 161 const mdb_idcmd_t *idcp = NULL; 162 163 if (mdb.m_frame != NULL && mdb.m_frame->f_cp != NULL) 164 idcp = mdb.m_frame->f_cp->c_dcmd; 165 166 if (sip != NULL) 167 bcopy(sip, &_mdb_abort_info, sizeof (_mdb_abort_info)); 168 if (ucp != NULL) 169 bcopy(ucp, &_mdb_abort_ctx, sizeof (_mdb_abort_ctx)); 170 171 _mdb_abort_info.si_signo = sig; 172 (void) mdb_signal_sethandler(sig, SIG_DFL, NULL); 173 174 /* 175 * If there is no current dcmd, or the current dcmd comes from a 176 * builtin module, we don't allow resume and always core dump. 177 */ 178 if (idcp == NULL || idcp->idc_modp == NULL || 179 idcp->idc_modp == &mdb.m_rmod || idcp->idc_modp->mod_hdl == NULL) 180 goto dump; 181 182 if (mdb.m_term != NULL) { 183 struct frame *fr = (struct frame *) 184 (ucp->uc_mcontext.gregs[STACK_REGISTER] + STACK_BIAS); 185 186 char signame[SIG2STR_MAX]; 187 int i = 1; 188 char c; 189 190 if (sig2str(sig, signame) == -1) { 191 mdb_iob_printf(mdb.m_err, 192 "\n*** %s: received signal %d at:\n", 193 mdb.m_pname, sig); 194 } else { 195 mdb_iob_printf(mdb.m_err, 196 "\n*** %s: received signal %s at:\n", 197 mdb.m_pname, signame); 198 } 199 200 if (ucp->uc_mcontext.gregs[REG_PC] != 0) 201 print_frame(ucp->uc_mcontext.gregs[REG_PC], i++); 202 203 while (fr != NULL && valid_frame(fr) && fr->fr_savpc != 0) { 204 print_frame(fr->fr_savpc, i++); 205 fr = (struct frame *) 206 ((uintptr_t)fr->fr_savfp + STACK_BIAS); 207 } 208 209 query: 210 mdb_iob_printf(mdb.m_err, "\n%s: (c)ore dump, (q)uit, " 211 "(r)ecover, or (s)top for debugger [cqrs]? ", mdb.m_pname); 212 213 mdb_iob_flush(mdb.m_err); 214 215 for (;;) { 216 if (IOP_READ(mdb.m_term, &c, sizeof (c)) != sizeof (c)) 217 goto dump; 218 219 switch (c) { 220 case 'c': 221 case 'C': 222 (void) setrlimit(RLIMIT_CORE, &rl); 223 mdb_iob_printf(mdb.m_err, "\n%s: attempting " 224 "to dump core ...\n", mdb.m_pname); 225 goto dump; 226 227 case 'q': 228 case 'Q': 229 mdb_iob_discard(mdb.m_out); 230 mdb_iob_nl(mdb.m_err); 231 (void) mdb_signal_unblockall(); 232 terminate(1); 233 /*NOTREACHED*/ 234 235 case 'r': 236 case 'R': 237 mdb_iob_printf(mdb.m_err, "\n%s: unloading " 238 "module '%s' ...\n", mdb.m_pname, 239 idcp->idc_modp->mod_name); 240 241 (void) mdb_module_unload( 242 idcp->idc_modp->mod_name, 0); 243 244 (void) mdb_signal_sethandler(sig, 245 flt_handler, NULL); 246 247 _mdb_abort_rcount++; 248 mdb.m_intr = 0; 249 mdb.m_pend = 0; 250 251 (void) mdb_signal_unblockall(); 252 longjmp(mdb.m_frame->f_pcb, MDB_ERR_ABORT); 253 /*NOTREACHED*/ 254 255 case 's': 256 case 'S': 257 mdb_iob_printf(mdb.m_err, "\n%s: " 258 "attempting to stop pid %d ...\n", 259 mdb.m_pname, (int)getpid()); 260 261 /* 262 * Stop ourself; if this fails or we are 263 * subsequently continued, ask again. 264 */ 265 (void) mdb_signal_raise(SIGSTOP); 266 (void) mdb_signal_unblockall(); 267 goto query; 268 } 269 } 270 } 271 272 dump: 273 if (SI_FROMUSER(sip)) { 274 (void) mdb_signal_block(sig); 275 (void) mdb_signal_raise(sig); 276 } 277 278 (void) sigfillset(&ucp->uc_sigmask); 279 (void) sigdelset(&ucp->uc_sigmask, sig); 280 281 if (_mdb_abort_str == NULL) 282 _mdb_abort_str = "fatal signal received"; 283 284 ucp->uc_flags |= UC_SIGMASK; 285 (void) setcontext(ucp); 286 } 287 288 /*ARGSUSED*/ 289 static void 290 int_handler(int sig, siginfo_t *sip, ucontext_t *ucp, void *data) 291 { 292 if (mdb.m_intr == 0) 293 longjmp(mdb.m_frame->f_pcb, MDB_ERR_SIGINT); 294 else 295 mdb.m_pend++; 296 } 297 298 static void 299 control_kmdb(int start) 300 { 301 int fd; 302 303 if ((fd = open("/dev/kmdb", O_RDONLY)) < 0) 304 die("failed to open /dev/kmdb"); 305 306 if (start) { 307 char *state = mdb_get_config(); 308 309 if (ioctl(fd, KMDB_IOC_START, state) < 0) 310 die("failed to start kmdb"); 311 312 strfree(state); 313 } else { 314 if (ioctl(fd, KMDB_IOC_STOP) < 0) 315 die("failed to stop kmdb"); 316 } 317 318 (void) close(fd); 319 } 320 321 static void 322 usage(int status) 323 { 324 mdb_iob_printf(mdb.m_err, "Usage: %s [-fkmuwyAFKMSUW] [+/-o option] " 325 "[-p pid] [-s dist] [-I path] [-L path]\n\t[-P prompt] " 326 "[-R root] [-V dis-version] [object [core] | core | suffix]\n\n", 327 mdb.m_pname); 328 329 mdb_iob_puts(mdb.m_err, 330 "\t-f force raw file debugging mode\n" 331 "\t-k force kernel debugging mode\n" 332 "\t-m disable demand-loading of module symbols\n" 333 "\t-o set specified debugger option (+o to unset)\n" 334 "\t-p attach to specified process-id\n" 335 "\t-s set symbol matching distance\n" 336 "\t-u force user program debugging mode\n" 337 "\t-w enable write mode\n" 338 "\t-y send terminal initialization sequences for tty mode\n" 339 "\t-A disable automatic loading of mdb modules\n" 340 "\t-F enable forcible takeover mode\n" 341 "\t-K stop operating system and enter live kernel debugger\n" 342 "\t-M preload all module symbols\n" 343 "\t-I set initial path for macro files\n" 344 "\t-L set initial path for module libs\n" 345 "\t-P set command-line prompt\n" 346 "\t-R set root directory for pathname expansion\n" 347 "\t-S suppress processing of ~/.mdbrc file\n" 348 "\t-U unload live kernel debugger\n" 349 "\t-W enable I/O-mapped memory access (kernel only)\n" 350 "\t-V set disassembler version\n"); 351 352 terminate(status); 353 } 354 355 static char * 356 mdb_scf_console_term(void) 357 { 358 scf_simple_prop_t *prop; 359 char *term = NULL; 360 361 if ((prop = scf_simple_prop_get(NULL, 362 "svc:/system/console-login:default", "ttymon", 363 "terminal_type")) == NULL) 364 return (NULL); 365 366 if (scf_simple_prop_type(prop) == SCF_TYPE_ASTRING && 367 (term = scf_simple_prop_next_astring(prop)) != NULL) 368 term = strdup(term); 369 370 scf_simple_prop_free(prop); 371 return (term); 372 } 373 374 /* 375 * Unpleasant hack: we might be debugging a hypervisor domain dump. 376 * Earlier versions use a non-ELF file. Later versions are ELF, but are 377 * /always/ ELF64, so our standard ehdr check isn't good enough. Since 378 * we don't want to know too much about the file format, we'll ask 379 * mdb_kb. 380 */ 381 #ifdef __x86 382 static int 383 identify_xvm_file(const char *file, int *longmode) 384 { 385 int (*identify)(const char *, int *); 386 387 if (mdb_module_load("mdb_kb", MDB_MOD_GLOBAL | MDB_MOD_SILENT) != 0) 388 return (0); 389 390 identify = (int (*)())dlsym(RTLD_NEXT, "xkb_identify"); 391 392 if (identify == NULL) 393 return (0); 394 395 return (identify(file, longmode)); 396 } 397 #else 398 /*ARGSUSED*/ 399 static int 400 identify_xvm_file(const char *file, int *longmode) 401 { 402 return (0); 403 } 404 #endif /* __x86 */ 405 406 int 407 main(int argc, char *argv[], char *envp[]) 408 { 409 extern int mdb_kvm_is_compressed_dump(mdb_io_t *); 410 extern int mdb_kvm_is_dump(mdb_io_t *); 411 mdb_tgt_ctor_f *tgt_ctor = NULL; 412 const char **tgt_argv = alloca((argc + 2) * sizeof (char *)); 413 int tgt_argc = 0; 414 mdb_tgt_t *tgt; 415 416 char object[MAXPATHLEN], execname[MAXPATHLEN]; 417 mdb_io_t *in_io, *out_io, *err_io, *null_io; 418 struct termios tios; 419 int status, c; 420 char *p; 421 422 const char *Iflag = NULL, *Lflag = NULL, *Vflag = NULL, *pidarg = NULL; 423 int fflag = 0, Kflag = 0, Rflag = 0, Sflag = 0, Oflag = 0, Uflag = 0; 424 425 int ttylike; 426 int longmode = 0; 427 428 stack_t sigstack; 429 430 if (realpath(getexecname(), execname) == NULL) { 431 (void) strncpy(execname, argv[0], MAXPATHLEN); 432 execname[MAXPATHLEN - 1] = '\0'; 433 } 434 435 mdb_create(execname, argv[0]); 436 bzero(tgt_argv, argc * sizeof (char *)); 437 argv[0] = (char *)mdb.m_pname; 438 _mdb_self_fd = open("/proc/self/as", O_RDONLY); 439 440 mdb.m_env = envp; 441 442 out_io = mdb_fdio_create(STDOUT_FILENO); 443 mdb.m_out = mdb_iob_create(out_io, MDB_IOB_WRONLY); 444 445 err_io = mdb_fdio_create(STDERR_FILENO); 446 mdb.m_err = mdb_iob_create(err_io, MDB_IOB_WRONLY); 447 mdb_iob_clrflags(mdb.m_err, MDB_IOB_AUTOWRAP); 448 449 null_io = mdb_nullio_create(); 450 mdb.m_null = mdb_iob_create(null_io, MDB_IOB_WRONLY); 451 452 in_io = mdb_fdio_create(STDIN_FILENO); 453 if ((mdb.m_termtype = getenv("TERM")) != NULL) { 454 mdb.m_termtype = strdup(mdb.m_termtype); 455 mdb.m_flags |= MDB_FL_TERMGUESS; 456 } 457 mdb.m_term = NULL; 458 459 mdb_dmode(mdb_dstr2mode(getenv("MDB_DEBUG"))); 460 mdb.m_pgid = getpgrp(); 461 462 if (getenv("_MDB_EXEC") != NULL) 463 mdb.m_flags |= MDB_FL_EXEC; 464 465 /* 466 * Setup an alternate signal stack. When tearing down pipelines in 467 * terminate(), we may have to destroy the stack of the context in 468 * which we are currently executing the signal handler. 469 */ 470 sigstack.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE, 471 MAP_PRIVATE | MAP_ANON, -1, 0); 472 if (sigstack.ss_sp == MAP_FAILED) 473 die("could not allocate signal stack"); 474 sigstack.ss_size = SIGSTKSZ; 475 sigstack.ss_flags = 0; 476 if (sigaltstack(&sigstack, NULL) != 0) 477 die("could not set signal stack"); 478 479 (void) mdb_signal_sethandler(SIGPIPE, SIG_IGN, NULL); 480 (void) mdb_signal_sethandler(SIGQUIT, SIG_IGN, NULL); 481 482 (void) mdb_signal_sethandler(SIGILL, flt_handler, NULL); 483 (void) mdb_signal_sethandler(SIGTRAP, flt_handler, NULL); 484 (void) mdb_signal_sethandler(SIGIOT, flt_handler, NULL); 485 (void) mdb_signal_sethandler(SIGEMT, flt_handler, NULL); 486 (void) mdb_signal_sethandler(SIGFPE, flt_handler, NULL); 487 (void) mdb_signal_sethandler(SIGBUS, flt_handler, NULL); 488 (void) mdb_signal_sethandler(SIGSEGV, flt_handler, NULL); 489 490 (void) mdb_signal_sethandler(SIGHUP, (mdb_signal_f *)terminate, NULL); 491 (void) mdb_signal_sethandler(SIGTERM, (mdb_signal_f *)terminate, NULL); 492 493 for (mdb.m_rdvers = RD_VERSION; mdb.m_rdvers > 0; mdb.m_rdvers--) { 494 if (rd_init(mdb.m_rdvers) == RD_OK) 495 break; 496 } 497 498 for (mdb.m_ctfvers = CTF_VERSION; mdb.m_ctfvers > 0; mdb.m_ctfvers--) { 499 if (ctf_version(mdb.m_ctfvers) != -1) 500 break; 501 } 502 503 if ((p = getenv("HISTSIZE")) != NULL && strisnum(p)) { 504 mdb.m_histlen = strtoi(p); 505 if (mdb.m_histlen < 1) 506 mdb.m_histlen = 1; 507 } 508 509 while (optind < argc) { 510 while ((c = getopt(argc, argv, 511 "fkmo:p:s:uwyACD:FI:KL:MOP:R:SUV:W")) != (int)EOF) { 512 switch (c) { 513 case 'f': 514 fflag++; 515 tgt_ctor = mdb_rawfile_tgt_create; 516 break; 517 case 'k': 518 tgt_ctor = mdb_kvm_tgt_create; 519 break; 520 case 'm': 521 mdb.m_tgtflags |= MDB_TGT_F_NOLOAD; 522 mdb.m_tgtflags &= ~MDB_TGT_F_PRELOAD; 523 break; 524 case 'o': 525 if (!mdb_set_options(optarg, TRUE)) 526 terminate(2); 527 break; 528 case 'p': 529 tgt_ctor = mdb_proc_tgt_create; 530 pidarg = optarg; 531 break; 532 case 's': 533 if (!strisnum(optarg)) { 534 warn("expected integer following -s\n"); 535 terminate(2); 536 } 537 mdb.m_symdist = (size_t)(uint_t)strtoi(optarg); 538 break; 539 case 'u': 540 tgt_ctor = mdb_proc_tgt_create; 541 break; 542 case 'w': 543 mdb.m_tgtflags |= MDB_TGT_F_RDWR; 544 break; 545 case 'y': 546 mdb.m_flags |= MDB_FL_USECUP; 547 break; 548 case 'A': 549 (void) mdb_set_options("nomods", TRUE); 550 break; 551 case 'C': 552 (void) mdb_set_options("noctf", TRUE); 553 break; 554 case 'D': 555 mdb_dmode(mdb_dstr2mode(optarg)); 556 break; 557 case 'F': 558 mdb.m_tgtflags |= MDB_TGT_F_FORCE; 559 break; 560 case 'I': 561 Iflag = optarg; 562 break; 563 case 'L': 564 Lflag = optarg; 565 break; 566 case 'K': 567 Kflag++; 568 break; 569 case 'M': 570 mdb.m_tgtflags |= MDB_TGT_F_PRELOAD; 571 mdb.m_tgtflags &= ~MDB_TGT_F_NOLOAD; 572 break; 573 case 'O': 574 Oflag++; 575 break; 576 case 'P': 577 if (!mdb_set_prompt(optarg)) 578 terminate(2); 579 break; 580 case 'R': 581 (void) strncpy(mdb.m_root, optarg, MAXPATHLEN); 582 mdb.m_root[MAXPATHLEN - 1] = '\0'; 583 Rflag++; 584 break; 585 case 'S': 586 Sflag++; 587 break; 588 case 'U': 589 Uflag++; 590 break; 591 case 'V': 592 Vflag = optarg; 593 break; 594 case 'W': 595 mdb.m_tgtflags |= MDB_TGT_F_ALLOWIO; 596 break; 597 case '?': 598 if (optopt == '?') 599 usage(0); 600 /* FALLTHROUGH */ 601 default: 602 usage(2); 603 } 604 } 605 606 if (optind < argc) { 607 const char *arg = argv[optind++]; 608 609 if (arg[0] == '+' && strlen(arg) == 2) { 610 if (arg[1] != 'o') { 611 warn("illegal option -- %s\n", arg); 612 terminate(2); 613 } 614 if (optind >= argc) { 615 warn("option requires an argument -- " 616 "%s\n", arg); 617 terminate(2); 618 } 619 if (!mdb_set_options(argv[optind++], FALSE)) 620 terminate(2); 621 } else 622 tgt_argv[tgt_argc++] = arg; 623 } 624 } 625 626 if (rd_ctl(RD_CTL_SET_HELPPATH, (void *)mdb.m_root) != RD_OK) { 627 warn("cannot set librtld_db helper path to %s\n", mdb.m_root); 628 terminate(2); 629 } 630 631 if (mdb.m_debug & MDB_DBG_HELP) 632 terminate(0); /* Quit here if we've printed out the tokens */ 633 634 635 if (Iflag != NULL && strchr(Iflag, ';') != NULL) { 636 warn("macro path cannot contain semicolons\n"); 637 terminate(2); 638 } 639 640 if (Lflag != NULL && strchr(Lflag, ';') != NULL) { 641 warn("module path cannot contain semicolons\n"); 642 terminate(2); 643 } 644 645 if (Kflag || Uflag) { 646 char *nm; 647 648 if (tgt_ctor != NULL || Iflag != NULL) { 649 warn("neither -f, -k, -p, -u, nor -I " 650 "may be used with -K\n"); 651 usage(2); 652 } 653 654 if (Lflag != NULL) 655 mdb_set_lpath(Lflag); 656 657 if ((nm = ttyname(STDIN_FILENO)) == NULL || 658 strcmp(nm, "/dev/console") != 0) { 659 /* 660 * Due to the consequences of typing mdb -K instead of 661 * mdb -k on a tty other than /dev/console, we require 662 * -F when starting kmdb from a tty other than 663 * /dev/console. 664 */ 665 if (!(mdb.m_tgtflags & MDB_TGT_F_FORCE)) { 666 die("-F must also be supplied to start kmdb " 667 "from non-console tty\n"); 668 } 669 670 if (mdb.m_termtype == NULL || (mdb.m_flags & 671 MDB_FL_TERMGUESS)) { 672 if (mdb.m_termtype != NULL) 673 strfree(mdb.m_termtype); 674 675 if ((mdb.m_termtype = mdb_scf_console_term()) != 676 NULL) 677 mdb.m_flags |= MDB_FL_TERMGUESS; 678 } 679 } else { 680 /* 681 * When on console, $TERM (if set) takes precedence over 682 * the SMF setting. 683 */ 684 if (mdb.m_termtype == NULL && (mdb.m_termtype = 685 mdb_scf_console_term()) != NULL) 686 mdb.m_flags |= MDB_FL_TERMGUESS; 687 } 688 689 control_kmdb(Kflag); 690 terminate(0); 691 /*NOTREACHED*/ 692 } 693 694 /* 695 * If standard input appears to have tty attributes, attempt to 696 * initialize a terminal i/o backend on top of stdin and stdout. 697 */ 698 ttylike = (IOP_CTL(in_io, TCGETS, &tios) == 0); 699 if (ttylike) { 700 if ((mdb.m_term = mdb_termio_create(mdb.m_termtype, 701 in_io, out_io)) == NULL) { 702 if (!(mdb.m_flags & MDB_FL_EXEC)) { 703 warn("term init failed: command-line editing " 704 "and prompt will not be available\n"); 705 } 706 } else { 707 in_io = mdb.m_term; 708 } 709 } 710 711 mdb.m_in = mdb_iob_create(in_io, MDB_IOB_RDONLY); 712 if (mdb.m_term != NULL) { 713 mdb_iob_setpager(mdb.m_out, mdb.m_term); 714 if (mdb.m_flags & MDB_FL_PAGER) 715 mdb_iob_setflags(mdb.m_out, MDB_IOB_PGENABLE); 716 else 717 mdb_iob_clrflags(mdb.m_out, MDB_IOB_PGENABLE); 718 } else if (ttylike) 719 mdb_iob_setflags(mdb.m_in, MDB_IOB_TTYLIKE); 720 else 721 mdb_iob_setbuf(mdb.m_in, mdb_alloc(1, UM_SLEEP), 1); 722 723 mdb_pservice_init(); 724 mdb_lex_reset(); 725 726 if ((mdb.m_shell = getenv("SHELL")) == NULL) 727 mdb.m_shell = "/bin/sh"; 728 729 /* 730 * If the debugger state is to be inherited from a previous instance, 731 * restore it now prior to path evaluation so that %R is updated. 732 */ 733 if ((p = getenv(MDB_CONFIG_ENV_VAR)) != NULL) { 734 mdb_set_config(p); 735 (void) unsetenv(MDB_CONFIG_ENV_VAR); 736 } 737 738 /* 739 * Path evaluation part 1: Create the initial module path to allow 740 * the target constructor to load a support module. Then expand 741 * any command-line arguments that modify the paths. 742 */ 743 if (Iflag != NULL) 744 mdb_set_ipath(Iflag); 745 else 746 mdb_set_ipath(MDB_DEF_IPATH); 747 748 if (Lflag != NULL) 749 mdb_set_lpath(Lflag); 750 else 751 mdb_set_lpath(MDB_DEF_LPATH); 752 753 if (mdb_get_prompt() == NULL && !(mdb.m_flags & MDB_FL_ADB)) 754 (void) mdb_set_prompt(MDB_DEF_PROMPT); 755 756 if (tgt_ctor == mdb_kvm_tgt_create) { 757 if (pidarg != NULL) { 758 warn("-p and -k options are mutually exclusive\n"); 759 terminate(2); 760 } 761 762 if (tgt_argc == 0) 763 tgt_argv[tgt_argc++] = "/dev/ksyms"; 764 if (tgt_argc == 1 && strisnum(tgt_argv[0]) == 0) { 765 if (mdb.m_tgtflags & MDB_TGT_F_ALLOWIO) 766 tgt_argv[tgt_argc++] = "/dev/allkmem"; 767 else 768 tgt_argv[tgt_argc++] = "/dev/kmem"; 769 } 770 } 771 772 if (pidarg != NULL) { 773 if (tgt_argc != 0) { 774 warn("-p may not be used with other arguments\n"); 775 terminate(2); 776 } 777 if (proc_arg_psinfo(pidarg, PR_ARG_PIDS, NULL, &status) == -1) { 778 die("cannot attach to %s: %s\n", 779 pidarg, Pgrab_error(status)); 780 } 781 if (strchr(pidarg, '/') != NULL) 782 (void) mdb_iob_snprintf(object, MAXPATHLEN, 783 "%s/object/a.out", pidarg); 784 else 785 (void) mdb_iob_snprintf(object, MAXPATHLEN, 786 "/proc/%s/object/a.out", pidarg); 787 tgt_argv[tgt_argc++] = object; 788 tgt_argv[tgt_argc++] = pidarg; 789 } 790 791 /* 792 * Find the first argument that is not a special "-" token. If one is 793 * found, we will examine this file and make some inferences below. 794 */ 795 for (c = 0; c < tgt_argc && strcmp(tgt_argv[c], "-") == 0; c++) 796 continue; 797 798 if (c < tgt_argc) { 799 Elf32_Ehdr ehdr; 800 mdb_io_t *io; 801 802 /* 803 * If special "-" tokens preceded an argument, shift the entire 804 * argument list to the left to remove the leading "-" args. 805 */ 806 if (c > 0) { 807 bcopy(&tgt_argv[c], tgt_argv, 808 sizeof (const char *) * (tgt_argc - c)); 809 tgt_argc -= c; 810 } 811 812 if (fflag) 813 goto tcreate; /* skip re-exec and just create target */ 814 815 /* 816 * If we just have an object file name, and that file doesn't 817 * exist, and it's a string of digits, infer it to be a 818 * sequence number referring to a pair of crash dump files. 819 */ 820 if (tgt_argc == 1 && access(tgt_argv[0], F_OK) == -1 && 821 strisnum(tgt_argv[0])) { 822 823 size_t len = strlen(tgt_argv[0]) + 8; 824 const char *object = tgt_argv[0]; 825 826 tgt_argv[0] = alloca(len); 827 tgt_argv[1] = alloca(len); 828 829 (void) strcpy((char *)tgt_argv[0], "unix."); 830 (void) strcat((char *)tgt_argv[0], object); 831 (void) strcpy((char *)tgt_argv[1], "vmcore."); 832 (void) strcat((char *)tgt_argv[1], object); 833 834 if (access(tgt_argv[0], F_OK) == -1 && 835 access(tgt_argv[1], F_OK) != -1) { 836 /* 837 * If we have a vmcore but not a unix file, 838 * set the symbol table to be the vmcore to 839 * force libkvm to extract it out of the dump. 840 */ 841 tgt_argv[0] = tgt_argv[1]; 842 } else if (access(tgt_argv[0], F_OK) == -1 && 843 access(tgt_argv[1], F_OK) == -1) { 844 (void) strcpy((char *)tgt_argv[1], "vmdump."); 845 (void) strcat((char *)tgt_argv[1], object); 846 if (access(tgt_argv[1], F_OK) == 0) { 847 mdb_iob_printf(mdb.m_err, 848 "cannot open compressed dump; " 849 "decompress using savecore -f %s\n", 850 tgt_argv[1]); 851 terminate(0); 852 } 853 } 854 855 tgt_argc = 2; 856 } 857 858 /* 859 * We need to open the object file in order to determine its 860 * ELF class and potentially re-exec ourself. 861 */ 862 if ((io = mdb_fdio_create_path(NULL, tgt_argv[0], 863 O_RDONLY, 0)) == NULL) 864 die("failed to open %s", tgt_argv[0]); 865 866 if (tgt_argc == 1) { 867 if (mdb_kvm_is_compressed_dump(io)) { 868 /* 869 * We have a single vmdump.N compressed dump 870 * file; give a helpful message. 871 */ 872 mdb_iob_printf(mdb.m_err, 873 "cannot open compressed dump; " 874 "decompress using savecore -f %s\n", 875 tgt_argv[0]); 876 terminate(0); 877 } else if (mdb_kvm_is_dump(io)) { 878 /* 879 * We have an uncompressed dump as our only 880 * argument; specify the dump as the symbol 881 * table to force libkvm to dig it out of the 882 * dump. 883 */ 884 tgt_argv[tgt_argc++] = tgt_argv[0]; 885 } 886 } 887 888 /* 889 * If the target is unknown or is not the rawfile target, do 890 * a gelf_check to determine if the file is an ELF file. If 891 * it is not and the target is unknown, use the rawfile tgt. 892 * Otherwise an ELF-based target is needed, so we must abort. 893 */ 894 if (mdb_gelf_check(io, &ehdr, ET_NONE) == -1) { 895 if (tgt_ctor != NULL) { 896 (void) mdb_gelf_check(io, &ehdr, ET_EXEC); 897 mdb_io_destroy(io); 898 terminate(1); 899 } else 900 tgt_ctor = mdb_rawfile_tgt_create; 901 } 902 903 mdb_io_destroy(io); 904 905 if (identify_xvm_file(tgt_argv[0], &longmode) == 1) { 906 #ifdef _LP64 907 if (!longmode) 908 goto reexec; 909 #else 910 if (longmode) 911 goto reexec; 912 #endif 913 tgt_ctor = mdb_kvm_tgt_create; 914 goto tcreate; 915 } 916 917 /* 918 * The object file turned out to be a user core file (ET_CORE), 919 * and no other arguments were specified, swap 0 and 1. The 920 * proc target will infer the executable for us. 921 */ 922 if (ehdr.e_type == ET_CORE) { 923 tgt_argv[tgt_argc++] = tgt_argv[0]; 924 tgt_argv[0] = NULL; 925 tgt_ctor = mdb_proc_tgt_create; 926 } 927 928 /* 929 * If tgt_argv[1] is filled in, open it up and determine if it 930 * is a vmcore file. If it is, gelf_check will fail and we 931 * set tgt_ctor to 'kvm'; otherwise we use the default. 932 */ 933 if (tgt_argc > 1 && strcmp(tgt_argv[1], "-") != 0 && 934 tgt_argv[0] != NULL && pidarg == NULL) { 935 Elf32_Ehdr chdr; 936 937 if (access(tgt_argv[1], F_OK) == -1) 938 die("failed to access %s", tgt_argv[1]); 939 940 /* *.N case: drop vmdump.N from the list */ 941 if (tgt_argc == 3) { 942 if ((io = mdb_fdio_create_path(NULL, 943 tgt_argv[2], O_RDONLY, 0)) == NULL) 944 die("failed to open %s", tgt_argv[2]); 945 if (mdb_kvm_is_compressed_dump(io)) 946 tgt_argv[--tgt_argc] = NULL; 947 mdb_io_destroy(io); 948 } 949 950 if ((io = mdb_fdio_create_path(NULL, tgt_argv[1], 951 O_RDONLY, 0)) == NULL) 952 die("failed to open %s", tgt_argv[1]); 953 954 if (mdb_gelf_check(io, &chdr, ET_NONE) == -1) 955 tgt_ctor = mdb_kvm_tgt_create; 956 957 mdb_io_destroy(io); 958 } 959 960 /* 961 * At this point, we've read the ELF header for either an 962 * object file or core into ehdr. If the class does not match 963 * ours, attempt to exec the mdb of the appropriate class. 964 */ 965 #ifdef _LP64 966 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) 967 goto reexec; 968 #else 969 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) 970 goto reexec; 971 #endif 972 } 973 974 tcreate: 975 if (tgt_ctor == NULL) 976 tgt_ctor = mdb_proc_tgt_create; 977 978 tgt = mdb_tgt_create(tgt_ctor, mdb.m_tgtflags, tgt_argc, tgt_argv); 979 980 if (tgt == NULL) { 981 if (errno == EINVAL) 982 usage(2); /* target can return EINVAL to get usage */ 983 if (errno == EMDB_TGT) 984 terminate(1); /* target already printed error msg */ 985 die("failed to initialize target"); 986 } 987 988 mdb_tgt_activate(tgt); 989 990 mdb_create_loadable_disasms(); 991 992 if (Vflag != NULL && mdb_dis_select(Vflag) == -1) 993 warn("invalid disassembler mode -- %s\n", Vflag); 994 995 996 if (Rflag && mdb.m_term != NULL) 997 warn("Using proto area %s\n", mdb.m_root); 998 999 /* 1000 * If the target was successfully constructed and -O was specified, 1001 * we now attempt to enter piggy-mode for debugging jurassic problems. 1002 */ 1003 if (Oflag) { 1004 pcinfo_t pci; 1005 1006 (void) strcpy(pci.pc_clname, "RT"); 1007 1008 if (priocntl(P_LWPID, P_MYID, PC_GETCID, (caddr_t)&pci) != -1) { 1009 pcparms_t pcp; 1010 rtparms_t *rtp = (rtparms_t *)pcp.pc_clparms; 1011 1012 rtp->rt_pri = 35; 1013 rtp->rt_tqsecs = 0; 1014 rtp->rt_tqnsecs = RT_TQDEF; 1015 1016 pcp.pc_cid = pci.pc_cid; 1017 1018 if (priocntl(P_LWPID, P_MYID, PC_SETPARMS, 1019 (caddr_t)&pcp) == -1) { 1020 warn("failed to set RT parameters"); 1021 Oflag = 0; 1022 } 1023 } else { 1024 warn("failed to get RT class id"); 1025 Oflag = 0; 1026 } 1027 1028 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) { 1029 warn("failed to lock address space"); 1030 Oflag = 0; 1031 } 1032 1033 if (Oflag) 1034 mdb_printf("%s: oink, oink!\n", mdb.m_pname); 1035 } 1036 1037 /* 1038 * Path evaluation part 2: Re-evaluate the path now that the target 1039 * is ready (and thus we have access to the real platform string). 1040 * Do this before reading ~/.mdbrc to allow path modifications prior 1041 * to performing module auto-loading. 1042 */ 1043 mdb_set_ipath(mdb.m_ipathstr); 1044 mdb_set_lpath(mdb.m_lpathstr); 1045 1046 if (!Sflag && (p = getenv("HOME")) != NULL) { 1047 char rcpath[MAXPATHLEN]; 1048 mdb_io_t *rc_io; 1049 int fd; 1050 1051 (void) mdb_iob_snprintf(rcpath, MAXPATHLEN, "%s/.mdbrc", p); 1052 fd = open64(rcpath, O_RDONLY); 1053 1054 if (fd >= 0 && (rc_io = mdb_fdio_create_named(fd, rcpath))) { 1055 mdb_iob_t *iob = mdb_iob_create(rc_io, MDB_IOB_RDONLY); 1056 mdb_iob_t *old = mdb.m_in; 1057 1058 mdb.m_in = iob; 1059 (void) mdb_run(); 1060 mdb.m_in = old; 1061 } 1062 } 1063 1064 if (!(mdb.m_flags & MDB_FL_NOMODS)) 1065 mdb_module_load_all(0); 1066 1067 (void) mdb_signal_sethandler(SIGINT, int_handler, NULL); 1068 while ((status = mdb_run()) == MDB_ERR_ABORT || 1069 status == MDB_ERR_OUTPUT) { 1070 /* 1071 * If a write failed on stdout, give up. A more informative 1072 * error message will already have been printed by mdb_run(). 1073 */ 1074 if (status == MDB_ERR_OUTPUT && 1075 mdb_iob_getflags(mdb.m_out) & MDB_IOB_ERR) { 1076 mdb_warn("write to stdout failed, exiting\n"); 1077 break; 1078 } 1079 continue; 1080 } 1081 1082 terminate((status == MDB_ERR_QUIT || status == 0) ? 0 : 1); 1083 /*NOTREACHED*/ 1084 return (0); 1085 1086 reexec: 1087 if ((p = strrchr(execname, '/')) == NULL) 1088 die("cannot determine absolute pathname\n"); 1089 #ifdef _LP64 1090 #ifdef __sparc 1091 (void) strcpy(p, "/../sparcv7/"); 1092 #else 1093 (void) strcpy(p, "/../i86/"); 1094 #endif 1095 #else 1096 #ifdef __sparc 1097 (void) strcpy(p, "/../sparcv9/"); 1098 #else 1099 (void) strcpy(p, "/../amd64/"); 1100 #endif 1101 #endif 1102 (void) strcat(p, mdb.m_pname); 1103 1104 if (mdb.m_term != NULL) 1105 (void) IOP_CTL(in_io, TCSETSW, &tios); 1106 1107 (void) putenv("_MDB_EXEC=1"); 1108 (void) execv(execname, argv); 1109 1110 /* 1111 * If execv fails, suppress ENOEXEC. Experience shows the most common 1112 * reason is that the machine is booted under a 32-bit kernel, in which 1113 * case it is clearer to only print the message below. 1114 */ 1115 if (errno != ENOEXEC) 1116 warn("failed to exec %s", execname); 1117 #ifdef _LP64 1118 die("64-bit %s cannot debug 32-bit program %s\n", 1119 mdb.m_pname, tgt_argv[0] ? 1120 tgt_argv[0] : tgt_argv[1]); 1121 #else 1122 die("32-bit %s cannot debug 64-bit program %s\n", 1123 mdb.m_pname, tgt_argv[0] ? 1124 tgt_argv[0] : tgt_argv[1]); 1125 #endif 1126 1127 goto tcreate; 1128 } 1129