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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <kmdb/kmdb_kvm.h> 30 #include <kmdb/kvm.h> 31 #include <kmdb/kmdb_kdi.h> 32 #include <kmdb/kmdb_promif.h> 33 #include <kmdb/kmdb_module.h> 34 #include <kmdb/kmdb_asmutil.h> 35 #include <mdb/mdb_types.h> 36 #include <mdb/mdb_conf.h> 37 #include <mdb/mdb_err.h> 38 #include <mdb/mdb_modapi.h> 39 #include <mdb/mdb_target_impl.h> 40 #include <mdb/mdb_debug.h> 41 #include <mdb/mdb_string.h> 42 #include <mdb/mdb_ctf.h> 43 #include <mdb/mdb_kreg_impl.h> 44 #include <mdb/mdb_ks.h> 45 #include <mdb/mdb.h> 46 47 #include <strings.h> 48 #include <dlfcn.h> 49 #include <sys/isa_defs.h> 50 #include <sys/kobj.h> 51 #include <sys/kobj_impl.h> 52 #include <sys/bitmap.h> 53 #include <vm/as.h> 54 55 static const char KMT_RTLD_NAME[] = "krtld"; 56 static const char KMT_MODULE[] = "mdb_ks"; 57 static const char KMT_CTFPARENT[] = "genunix"; 58 59 static mdb_list_t kmt_defbp_list; /* List of current deferred bp's */ 60 static int kmt_defbp_lock; /* For list, running kernel holds */ 61 static uint_t kmt_defbp_modchg_isload; /* Whether mod change is load/unload */ 62 static struct modctl *kmt_defbp_modchg_modctl; /* modctl for defbp checking */ 63 static uint_t kmt_defbp_num; /* Number of referenced def'd bp's */ 64 static int kmt_defbp_bpspec; /* vespec for def'd bp activation bp */ 65 66 static const mdb_se_ops_t kmt_brkpt_ops; 67 static const mdb_se_ops_t kmt_wapt_ops; 68 69 static void kmt_sync(mdb_tgt_t *); 70 71 typedef struct kmt_symarg { 72 mdb_tgt_sym_f *sym_cb; /* Caller's callback function */ 73 void *sym_data; /* Callback function argument */ 74 uint_t sym_type; /* Symbol type/binding filter */ 75 mdb_syminfo_t sym_info; /* Symbol id and table id */ 76 const char *sym_obj; /* Containing object */ 77 } kmt_symarg_t; 78 79 typedef struct kmt_maparg { 80 mdb_tgt_t *map_target; /* Target used for mapping iter */ 81 mdb_tgt_map_f *map_cb; /* Caller's callback function */ 82 void *map_data; /* Callback function argument */ 83 } kmt_maparg_t; 84 85 /*ARGSUSED*/ 86 int 87 kmt_setflags(mdb_tgt_t *t, int flags) 88 { 89 /* 90 * We only handle one flag (ALLOWIO), and we can't fail to set or clear 91 * it, so we just blindly replace the t_flags version with the one 92 * passed. 93 */ 94 t->t_flags = (t->t_flags & ~MDB_TGT_F_ALLOWIO) | 95 (flags & MDB_TGT_F_ALLOWIO); 96 97 return (0); 98 } 99 100 /*ARGSUSED*/ 101 const char * 102 kmt_name(mdb_tgt_t *t) 103 { 104 return ("kmdb_kvm"); 105 } 106 107 /*ARGSUSED*/ 108 static const char * 109 kmt_platform(mdb_tgt_t *t) 110 { 111 static char platform[SYS_NMLN]; 112 113 if (kmdb_dpi_get_state(NULL) == DPI_STATE_INIT) 114 return (mdb_conf_platform()); 115 116 if (mdb_tgt_readsym(mdb.m_target, MDB_TGT_AS_VIRT, platform, 117 sizeof (platform), "unix", "platform") != sizeof (platform)) { 118 warn("'platform' symbol is missing from kernel\n"); 119 return ("unknown"); 120 } 121 122 return (platform); 123 } 124 125 static int 126 kmt_uname(mdb_tgt_t *t, struct utsname *utsp) 127 { 128 return (mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, utsp, 129 sizeof (struct utsname), MDB_TGT_OBJ_EXEC, "utsname")); 130 } 131 132 /*ARGSUSED*/ 133 static int 134 kmt_dmodel(mdb_tgt_t *t) 135 { 136 return (MDB_TGT_MODEL_NATIVE); 137 } 138 139 /*ARGSUSED*/ 140 ssize_t 141 kmt_rw(mdb_tgt_t *t, void *buf, size_t nbytes, uint64_t addr, 142 ssize_t (*rw)(void *, size_t, uint64_t)) 143 { 144 size_t n, ndone, chunksz; 145 jmp_buf *oldpcb = NULL; 146 jmp_buf pcb; 147 ssize_t res; 148 149 kmdb_prom_check_interrupt(); 150 151 if (nbytes == 0) 152 return (0); 153 154 /* 155 * Try to process the entire buffer, as requested. If we catch a fault, 156 * try smaller chunks. This allows us to handle regions that cross 157 * mapping boundaries. 158 */ 159 chunksz = nbytes; 160 ndone = 0; 161 if (setjmp(pcb) != 0) { 162 if (chunksz == 1) { 163 /* We failed with the smallest chunk - give up */ 164 kmdb_dpi_restore_fault_hdlr(oldpcb); 165 return (ndone > 0 ? ndone : -1); /* errno set for us */ 166 } else if (chunksz > 4) 167 chunksz = 4; 168 else 169 chunksz = 1; 170 } 171 172 oldpcb = kmdb_dpi_set_fault_hdlr(&pcb); 173 while (nbytes > 0) { 174 n = MIN(chunksz, nbytes); 175 176 if ((res = rw(buf, n, addr)) != n) 177 return (res < 0 ? res : ndone + res); 178 179 addr += n; 180 nbytes -= n; 181 ndone += n; 182 buf = ((caddr_t)buf + n); 183 } 184 185 kmdb_dpi_restore_fault_hdlr(oldpcb); 186 187 return (ndone); 188 } 189 190 static void 191 kmt_bcopy(const void *s1, void *s2, size_t n) 192 { 193 /* 194 * We need to guarantee atomic accesses for certain sizes. bcopy won't 195 * make that guarantee, so we need to do it ourselves. 196 */ 197 #ifdef _LP64 198 if (n == 8 && ((uintptr_t)s1 & 7) == 0 && ((uintptr_t)s2 & 7) == 0) 199 *(uint64_t *)s2 = *(uint64_t *)s1; 200 else 201 #endif 202 if (n == 4 && ((uintptr_t)s1 & 3) == 0 && ((uintptr_t)s2 & 3) == 0) 203 *(uint32_t *)s2 = *(uint32_t *)s1; 204 else if (n == 2 && ((uintptr_t)s1 & 1) == 0 && ((uintptr_t)s2 & 1) == 0) 205 *(uint16_t *)s2 = *(uint16_t *)s1; 206 else if (n == 1) 207 *(uint8_t *)s2 = *(uint8_t *)s1; 208 else 209 bcopy(s1, s2, n); 210 } 211 212 static ssize_t 213 kmt_reader(void *buf, size_t nbytes, uint64_t addr) 214 { 215 kmt_bcopy((void *)(uintptr_t)addr, buf, nbytes); 216 return (nbytes); 217 } 218 219 ssize_t 220 kmt_writer(void *buf, size_t nbytes, uint64_t addr) 221 { 222 kmt_bcopy(buf, (void *)(uintptr_t)addr, nbytes); 223 return (nbytes); 224 } 225 226 /*ARGSUSED*/ 227 static ssize_t 228 kmt_read(mdb_tgt_t *t, void *buf, size_t nbytes, uintptr_t addr) 229 { 230 /* 231 * We don't want to allow reads of I/O-mapped memory. Multi-page reads 232 * that cross into I/O-mapped memory should be restricted to the initial 233 * non-I/O region. Reads that begin in I/O-mapped memory are failed 234 * outright. 235 */ 236 if (!(t->t_flags & MDB_TGT_F_ALLOWIO) && 237 (nbytes = kmdb_kdi_range_is_nontoxic(addr, nbytes, 0)) == 0) 238 return (set_errno(EMDB_NOMAP)); 239 240 return (kmt_rw(t, buf, nbytes, addr, kmt_reader)); 241 } 242 243 /*ARGSUSED*/ 244 static ssize_t 245 kmt_pread(mdb_tgt_t *t, void *buf, size_t nbytes, physaddr_t addr) 246 { 247 return (kmt_rw(t, buf, nbytes, addr, kmdb_kdi_pread)); 248 } 249 250 /*ARGSUSED*/ 251 ssize_t 252 kmt_pwrite(mdb_tgt_t *t, const void *buf, size_t nbytes, physaddr_t addr) 253 { 254 return (kmt_rw(t, (void *)buf, nbytes, addr, kmdb_kdi_pwrite)); 255 } 256 257 static uintptr_t 258 kmt_read_kas(mdb_tgt_t *t) 259 { 260 GElf_Sym sym; 261 262 if (mdb_tgt_lookup_by_name(t, "unix", "kas", &sym, NULL) < 0) { 263 warn("'kas' symbol is missing from kernel\n"); 264 (void) set_errno(EMDB_NOSYM); 265 return (0); 266 } 267 268 return ((uintptr_t)sym.st_value); 269 } 270 271 static int 272 kmt_vtop(mdb_tgt_t *t, mdb_tgt_as_t as, uintptr_t va, physaddr_t *pap) 273 { 274 mdb_module_t *mod; 275 struct as *asp; 276 mdb_var_t *v; 277 278 switch ((uintptr_t)as) { 279 case (uintptr_t)MDB_TGT_AS_PHYS: 280 case (uintptr_t)MDB_TGT_AS_FILE: 281 case (uintptr_t)MDB_TGT_AS_IO: 282 return (set_errno(EINVAL)); 283 case (uintptr_t)MDB_TGT_AS_VIRT: 284 if ((asp = (struct as *)kmt_read_kas(t)) == NULL) 285 return (-1); /* errno is set for us */ 286 break; 287 default: 288 asp = (struct as *)as; 289 290 /* We don't support non-kas vtop */ 291 if (asp != (struct as *)kmt_read_kas(t)) 292 return (set_errno(EMDB_TGTNOTSUP)); 293 } 294 295 if (kmdb_prom_vtop(va, pap) == 0) 296 return (0); 297 298 if ((v = mdb_nv_lookup(&mdb.m_modules, "unix")) != NULL && 299 (mod = mdb_nv_get_cookie(v)) != NULL) { 300 int (*fptr)(uintptr_t, struct as *, physaddr_t *); 301 302 fptr = (int (*)(uintptr_t, struct as *, physaddr_t *)) 303 dlsym(mod->mod_hdl, "platform_vtop"); 304 305 if ((fptr != NULL) && ((*fptr)(va, asp, pap) == 0)) 306 return (0); 307 } 308 309 return (set_errno(EMDB_NOMAP)); 310 } 311 312 /*ARGSUSED*/ 313 static int 314 kmt_cpuregs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 315 { 316 const mdb_tgt_gregset_t *gregs; 317 intptr_t cpuid = DPI_MASTER_CPUID; 318 int i; 319 320 if (flags & DCMD_ADDRSPEC) { 321 if (argc != 0) 322 return (DCMD_USAGE); 323 if ((cpuid = mdb_cpu2cpuid(addr)) < 0) { 324 (void) set_errno(EMDB_NOMAP); 325 mdb_warn("failed to find cpuid for cpu at %p", addr); 326 return (DCMD_ERR); 327 } 328 } 329 330 i = mdb_getopts(argc, argv, 331 'c', MDB_OPT_UINTPTR, &cpuid, 332 NULL); 333 334 argc -= i; 335 argv += i; 336 337 if (argc != 0) 338 return (DCMD_USAGE); 339 340 if ((gregs = kmdb_dpi_get_gregs(cpuid)) == NULL) { 341 warn("failed to retrieve registers for cpu %d", (int)cpuid); 342 return (DCMD_ERR); 343 } 344 345 kmt_printregs(gregs); 346 347 return (DCMD_OK); 348 } 349 350 static int 351 kmt_regs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 352 { 353 if (flags & DCMD_ADDRSPEC) 354 return (DCMD_USAGE); 355 356 return (kmt_cpuregs(addr, flags, argc, argv)); 357 } 358 359 static int 360 kmt_cpustack_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 361 { 362 intptr_t cpuid = DPI_MASTER_CPUID; 363 uint_t verbose = 0; 364 int i; 365 366 if (flags & DCMD_ADDRSPEC) { 367 if ((cpuid = mdb_cpu2cpuid(addr)) < 0) { 368 (void) set_errno(EMDB_NOMAP); 369 mdb_warn("failed to find cpuid for cpu at %p", addr); 370 return (DCMD_ERR); 371 } 372 flags &= ~DCMD_ADDRSPEC; 373 } 374 375 i = mdb_getopts(argc, argv, 376 'c', MDB_OPT_UINTPTR, &cpuid, 377 'v', MDB_OPT_SETBITS, 1, &verbose, 378 NULL); 379 380 argc -= i; 381 argv += i; 382 383 return (kmt_cpustack(addr, flags, argc, argv, cpuid, verbose)); 384 } 385 386 /* 387 * Lasciate ogne speranza, voi ch'intrate. 388 */ 389 static int 390 kmt_call(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 391 { 392 uintptr_t *call_argv, rval; 393 int parse_strings = 1; 394 GElf_Sym sym; 395 jmp_buf *oldpcb = NULL; 396 jmp_buf pcb; 397 int i; 398 399 if (!(flags & DCMD_ADDRSPEC)) 400 return (DCMD_USAGE); 401 402 if (mdb_tgt_lookup_by_addr(mdb.m_target, addr, MDB_TGT_SYM_EXACT, 403 NULL, 0, &sym, NULL) == 0 && GELF_ST_TYPE(sym.st_info) != 404 STT_FUNC) { 405 warn("%a is not a function\n", addr); 406 return (DCMD_ERR); 407 } 408 409 if (argc > 1 && argv[0].a_type == MDB_TYPE_STRING && 410 strcmp(argv[0].a_un.a_str, "-s") == 0) { 411 parse_strings = 0; 412 argc--; 413 argv++; 414 } 415 416 call_argv = mdb_alloc(sizeof (uintptr_t) * argc, UM_SLEEP); 417 418 for (i = 0; i < argc; i++) { 419 switch (argv[i].a_type) { 420 case MDB_TYPE_STRING: 421 /* 422 * mdb_strtoull doesn't return on error, so we have to 423 * pre-check strings suspected to contain numbers. 424 */ 425 if (parse_strings && strisbasenum(argv[i].a_un.a_str)) { 426 call_argv[i] = (uintptr_t)mdb_strtoull( 427 argv[i].a_un.a_str); 428 } else 429 call_argv[i] = (uintptr_t)argv[i].a_un.a_str; 430 431 break; 432 433 case MDB_TYPE_IMMEDIATE: 434 call_argv[i] = argv[i].a_un.a_val; 435 break; 436 437 default: 438 mdb_free(call_argv, 439 sizeof (uintptr_t) * argc); 440 return (DCMD_USAGE); 441 } 442 } 443 444 if (setjmp(pcb) != 0) { 445 warn("call failed: caught a trap\n"); 446 447 kmdb_dpi_restore_fault_hdlr(oldpcb); 448 mdb_free(call_argv, sizeof (uintptr_t) * argc); 449 return (DCMD_ERR); 450 } 451 452 oldpcb = kmdb_dpi_set_fault_hdlr(&pcb); 453 rval = kmdb_dpi_call(addr, argc, call_argv); 454 kmdb_dpi_restore_fault_hdlr(oldpcb); 455 456 if (flags & DCMD_PIPE_OUT) { 457 mdb_printf("%p\n", rval); 458 } else { 459 /* pretty-print the results */ 460 mdb_printf("%p = %a(", rval, addr); 461 for (i = 0; i < argc; i++) { 462 if (i > 0) 463 mdb_printf(", "); 464 if (argv[i].a_type == MDB_TYPE_STRING) { 465 /* I'm ashamed but amused */ 466 char *quote = &("\""[parse_strings && 467 strisbasenum(argv[i].a_un.a_str)]); 468 469 mdb_printf("%s%s%s", quote, argv[i].a_un.a_str, 470 quote); 471 } else 472 mdb_printf("%p", argv[i].a_un.a_val); 473 } 474 mdb_printf(");\n"); 475 } 476 477 mdb_free(call_argv, sizeof (uintptr_t) * argc); 478 479 return (DCMD_OK); 480 } 481 482 /*ARGSUSED*/ 483 int 484 kmt_dump_crumbs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 485 { 486 intptr_t cpu = -1; 487 488 if (flags & DCMD_ADDRSPEC) { 489 if (argc != 0) 490 return (DCMD_USAGE); 491 } else { 492 addr = 0; 493 494 if (mdb_getopts(argc, argv, 495 'c', MDB_OPT_UINTPTR, &cpu, 496 NULL) != argc) 497 return (DCMD_USAGE); 498 } 499 500 kmdb_dpi_dump_crumbs(addr, cpu); 501 502 return (DCMD_OK); 503 } 504 505 /*ARGSUSED*/ 506 static int 507 kmt_noducttape(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 508 { 509 int a = 0; 510 511 return (a/a); 512 } 513 514 static int 515 kmt_dmod_status(char *msg, int state) 516 { 517 kmdb_modctl_t *kmc; 518 mdb_var_t *v; 519 int first = 1, n = 0; 520 521 mdb_nv_rewind(&mdb.m_dmodctl); 522 while ((v = mdb_nv_advance(&mdb.m_dmodctl)) != NULL) { 523 kmc = MDB_NV_COOKIE(v); 524 525 if (kmc->kmc_state != state) 526 continue; 527 528 n++; 529 530 if (msg != NULL) { 531 if (first) { 532 mdb_printf(msg, NULL); 533 first = 0; 534 } 535 536 mdb_printf(" %s", kmc->kmc_modname); 537 } 538 } 539 540 if (!first && msg != NULL) 541 mdb_printf("\n"); 542 543 return (n); 544 } 545 546 /*ARGSUSED*/ 547 static int 548 kmt_status_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 549 { 550 kmt_data_t *kmt = mdb.m_target->t_data; 551 struct utsname uts; 552 kreg_t tt; 553 554 if (mdb_tgt_readsym(mdb.m_target, MDB_TGT_AS_VIRT, &uts, sizeof (uts), 555 "unix", "utsname") != sizeof (uts)) { 556 warn("failed to read 'utsname' struct from kernel\n"); 557 bzero(&uts, sizeof (uts)); 558 (void) strcpy(uts.nodename, "unknown machine"); 559 } 560 561 mdb_printf("debugging live kernel (%d-bit) on %s\n", 562 (int)(sizeof (void *) * NBBY), 563 (*uts.nodename == '\0' ? "(not set)" : uts.nodename)); 564 mdb_printf("operating system: %s %s (%s)\n", 565 uts.release, uts.version, uts.machine); 566 567 if (kmt->kmt_cpu != NULL) { 568 mdb_printf("CPU-specific support: %s\n", 569 kmt_cpu_name(kmt->kmt_cpu)); 570 } 571 572 mdb_printf("DTrace state: %s\n", (kmdb_kdi_dtrace_get_state() == 573 KDI_DTSTATE_DTRACE_ACTIVE ? "active (debugger breakpoints cannot " 574 "be armed)" : "inactive")); 575 576 (void) kmdb_dpi_get_register("tt", &tt); 577 mdb_printf("stopped on: %s\n", kmt_trapname(tt)); 578 579 (void) kmt_dmod_status("pending dmod loads:", KMDB_MC_STATE_LOADING); 580 (void) kmt_dmod_status("pending dmod unloads:", 581 KMDB_MC_STATE_UNLOADING); 582 583 return (DCMD_OK); 584 } 585 586 /*ARGSUSED*/ 587 static int 588 kmt_switch(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 589 { 590 if (!(flags & DCMD_ADDRSPEC) || argc != 0) 591 return (DCMD_USAGE); 592 593 if (kmdb_dpi_switch_master((int)addr) < 0) { 594 warn("failed to switch to CPU %d", (int)addr); 595 return (DCMD_ERR); 596 } 597 598 return (DCMD_OK); 599 } 600 601 static const mdb_dcmd_t kmt_dcmds[] = { 602 { "$c", "?[cnt]", "print stack backtrace", kmt_stack }, 603 { "$C", "?[cnt]", "print stack backtrace", kmt_stackv }, 604 { "$r", NULL, "print general-purpose registers", kmt_regs }, 605 { "$?", NULL, "print status and registers", kmt_regs }, 606 { ":x", ":", "change the active CPU", kmt_switch }, 607 { "call", ":[arg ...]", "call a kernel function", kmt_call }, 608 { "cpustack", "?[-v] [-c cpuid] [cnt]", "print stack backtrace for a " 609 "specific CPU", kmt_cpustack_dcmd }, 610 { "cpuregs", "?[-c cpuid]", "print general-purpose registers for a " 611 "specific CPU", kmt_cpuregs }, 612 { "crumbs", NULL, NULL, kmt_dump_crumbs }, 613 #if defined(__i386) || defined(__amd64) 614 { "in", ":[-L len]", "read from I/O port", kmt_in_dcmd }, 615 { "out", ":[-L len] val", "write to I/O port", kmt_out_dcmd }, 616 { "rdmsr", ":", "read an MSR", kmt_rdmsr }, 617 { "wrmsr", ": val", "write an MSR", kmt_wrmsr }, 618 #endif 619 { "noducttape", NULL, NULL, kmt_noducttape }, 620 { "regs", NULL, "print general-purpose registers", kmt_regs }, 621 { "stack", "?[cnt]", "print stack backtrace", kmt_stack }, 622 { "stackregs", "?", "print stack backtrace and registers", kmt_stackr }, 623 { "status", NULL, "print summary of current target", kmt_status_dcmd }, 624 { "switch", ":", "change the active CPU", kmt_switch }, 625 { NULL } 626 }; 627 628 static uintmax_t 629 kmt_reg_disc_get(const mdb_var_t *v) 630 { 631 mdb_tgt_reg_t r = 0; 632 633 (void) mdb_tgt_getareg(MDB_NV_COOKIE(v), 0, mdb_nv_get_name(v), &r); 634 635 return (r); 636 } 637 638 static void 639 kmt_reg_disc_set(mdb_var_t *v, uintmax_t r) 640 { 641 if (mdb_tgt_putareg(MDB_NV_COOKIE(v), 0, mdb_nv_get_name(v), r) == -1) 642 warn("failed to modify %%%s register", mdb_nv_get_name(v)); 643 } 644 645 static const mdb_nv_disc_t kmt_reg_disc = { 646 kmt_reg_disc_set, 647 kmt_reg_disc_get 648 }; 649 650 /*ARGSUSED*/ 651 static int 652 kmt_getareg(mdb_tgt_t *t, mdb_tgt_tid_t tid, const char *rname, 653 mdb_tgt_reg_t *rp) 654 { 655 kreg_t val; 656 657 if (kmdb_dpi_get_register(rname, &val) < 0) 658 return (set_errno(EMDB_BADREG)); 659 660 *rp = val; 661 return (0); 662 } 663 664 /*ARGSUSED*/ 665 static int 666 kmt_putareg(mdb_tgt_t *t, mdb_tgt_tid_t tid, const char *rname, mdb_tgt_reg_t r) 667 { 668 if (kmdb_dpi_set_register(rname, r) < 0) 669 return (set_errno(EMDB_BADREG)); 670 671 return (0); 672 } 673 674 static void 675 kmt_mod_destroy(kmt_module_t *km) 676 { 677 if (km->km_name != NULL) 678 strfree(km->km_name); 679 if (km->km_symtab != NULL) 680 mdb_gelf_symtab_destroy(km->km_symtab); 681 if (km->km_ctfp != NULL) 682 mdb_ctf_close(km->km_ctfp); 683 } 684 685 static kmt_module_t * 686 kmt_mod_create(mdb_tgt_t *t, struct modctl *ctlp, char *name) 687 { 688 kmt_module_t *km = mdb_zalloc(sizeof (kmt_module_t), UM_SLEEP); 689 struct module *mod; 690 691 km->km_name = mdb_alloc(strlen(name) + 1, UM_SLEEP); 692 strcpy(km->km_name, name); 693 694 bcopy(ctlp, &km->km_modctl, sizeof (struct modctl)); 695 696 if (mdb_tgt_vread(t, &km->km_module, sizeof (struct module), 697 (uintptr_t)km->km_modctl.mod_mp) != sizeof (struct module)) 698 goto create_module_cleanup; 699 mod = &km->km_module; 700 701 if (mod->symhdr != NULL && mod->strhdr != NULL && mod->symtbl != NULL && 702 mod->strings != NULL) { 703 mdb_gelf_ehdr_to_gehdr(&mod->hdr, &km->km_ehdr); 704 705 km->km_symtab = mdb_gelf_symtab_create_raw(&km->km_ehdr, 706 mod->symhdr, mod->symtbl, mod->strhdr, mod->strings, 707 MDB_TGT_SYMTAB); 708 709 km->km_symtab_va = mod->symtbl; 710 km->km_strtab_va = mod->strings; 711 712 if (mdb_tgt_vread(t, &km->km_symtab_hdr, sizeof (Shdr), 713 (uintptr_t)mod->symhdr) != sizeof (Shdr) || 714 mdb_tgt_vread(t, &km->km_strtab_hdr, sizeof (Shdr), 715 (uintptr_t)mod->strhdr) != sizeof (Shdr)) 716 goto create_module_cleanup; 717 } 718 719 /* 720 * We don't want everyone rooting around in the module structure, so we 721 * make copies of the interesting members. 722 */ 723 km->km_text_va = (uintptr_t)mod->text; 724 km->km_text_size = mod->text_size; 725 km->km_data_va = (uintptr_t)mod->data; 726 km->km_data_size = mod->data_size; 727 km->km_bss_va = (uintptr_t)mod->bss; 728 km->km_bss_size = mod->bss_size; 729 km->km_ctf_va = mod->ctfdata; 730 km->km_ctf_size = mod->ctfsize; 731 732 if (mod->flags & KOBJ_PRIM) 733 km->km_flags |= KM_F_PRIMARY; 734 735 return (km); 736 737 create_module_cleanup: 738 warn("failed to read module %s\n", name); 739 kmt_mod_destroy(km); 740 return (NULL); 741 } 742 743 static void 744 kmt_mod_remove(kmt_data_t *kmt, kmt_module_t *km) 745 { 746 mdb_var_t *v = mdb_nv_lookup(&kmt->kmt_modules, km->km_name); 747 748 ASSERT(v != NULL); 749 750 mdb_dprintf(MDB_DBG_KMOD, "removing module %s\n", km->km_name); 751 752 mdb_list_delete(&kmt->kmt_modlist, km); 753 mdb_nv_remove(&kmt->kmt_modules, v); 754 kmt_mod_destroy(km); 755 } 756 757 static int 758 kmt_modlist_update_cb(struct modctl *modp, void *arg) 759 { 760 mdb_tgt_t *t = arg; 761 kmt_data_t *kmt = t->t_data; 762 kmt_module_t *km; 763 mdb_var_t *v; 764 char name[MAXNAMELEN]; 765 766 if (mdb_tgt_readstr(t, MDB_TGT_AS_VIRT, name, MAXNAMELEN, 767 (uintptr_t)modp->mod_modname) <= 0) { 768 warn("failed to read module name at %p", 769 (void *)modp->mod_modname); 770 } 771 772 /* We only care about modules that are actually loaded */ 773 if (!kmdb_kdi_mod_isloaded(modp)) 774 return (0); 775 776 /* 777 * Skip the modules we already know about and that haven't 778 * changed since last time we were here. 779 */ 780 if ((v = mdb_nv_lookup(&kmt->kmt_modules, name)) != NULL) { 781 km = MDB_NV_COOKIE(v); 782 783 if (kmdb_kdi_mod_haschanged(&km->km_modctl, &km->km_module, 784 modp, modp->mod_mp)) { 785 /* 786 * The module has changed since last we saw it. For 787 * safety, remove our old version, and treat it as a 788 * new module. 789 */ 790 mdb_dprintf(MDB_DBG_KMOD, "stutter module %s\n", name); 791 kmt_mod_remove(kmt, km); 792 } else { 793 km->km_seen = 1; 794 return (0); 795 } 796 } 797 798 mdb_dprintf(MDB_DBG_KMOD, "found new module %s\n", name); 799 800 if ((km = kmt_mod_create(t, modp, name)) != NULL) { 801 mdb_list_append(&kmt->kmt_modlist, km); 802 (void) mdb_nv_insert(&kmt->kmt_modules, name, NULL, 803 (uintptr_t)km, 0); 804 km->km_seen = 1; 805 } 806 807 return (0); 808 } 809 810 static void 811 kmt_modlist_update(mdb_tgt_t *t) 812 { 813 kmt_data_t *kmt = t->t_data; 814 kmt_module_t *km, *kmn; 815 816 if (kmdb_kdi_mod_iter(kmt_modlist_update_cb, t) < 0) { 817 warn("failed to complete update of kernel module list\n"); 818 return; 819 } 820 821 km = mdb_list_next(&kmt->kmt_modlist); 822 while (km != NULL) { 823 kmn = mdb_list_next(km); 824 825 if (km->km_seen == 1) { 826 /* Reset the mark for next time */ 827 km->km_seen = 0; 828 } else { 829 /* 830 * We didn't see it on the kernel's module list, so 831 * remove it from our view of the world. 832 */ 833 kmt_mod_remove(kmt, km); 834 } 835 836 km = kmn; 837 } 838 } 839 840 static void 841 kmt_periodic(mdb_tgt_t *t) 842 { 843 (void) mdb_tgt_status(t, &t->t_status); 844 } 845 846 int 847 kmt_lookup_by_addr(mdb_tgt_t *t, uintptr_t addr, uint_t flags, 848 char *buf, size_t nbytes, GElf_Sym *symp, mdb_syminfo_t *sip) 849 { 850 kmt_data_t *kmt = t->t_data; 851 kmt_module_t *km = mdb_list_next(&kmt->kmt_modlist); 852 kmt_module_t *sym_km = NULL; 853 kmt_module_t prmod; 854 GElf_Sym sym; 855 uint_t symid; 856 const char *name; 857 858 /* 859 * We look through the private symbols (if any), then through the module 860 * symbols. We can simplify the loop if we pretend the private symbols 861 * come from a module. 862 */ 863 if (mdb.m_prsym != NULL) { 864 bzero(&prmod, sizeof (kmt_module_t)); 865 prmod.km_name = "<<<prmod>>>"; 866 prmod.km_symtab = mdb.m_prsym; 867 prmod.km_list.ml_next = (mdb_list_t *)km; 868 km = &prmod; 869 } 870 871 /* Symbol resolution isn't available during initialization */ 872 if (kmdb_dpi_get_state(NULL) == DPI_STATE_INIT) 873 return (set_errno(EMDB_NOSYM)); 874 875 for (; km != NULL; km = mdb_list_next(km)) { 876 if (km != &prmod && !kmt->kmt_symavail) 877 continue; 878 879 if (km->km_symtab == NULL) 880 continue; 881 882 if (mdb_gelf_symtab_lookup_by_addr(km->km_symtab, addr, flags, 883 buf, nbytes, symp, &sip->sym_id) != 0 || 884 symp->st_value == 0) 885 continue; 886 887 if (flags & MDB_TGT_SYM_EXACT) { 888 sym_km = km; 889 goto found; 890 } 891 892 /* 893 * If this is the first match we've found, or if this symbol is 894 * closer to the specified address than the last one we found, 895 * use it. 896 */ 897 if (sym_km == NULL || mdb_gelf_sym_closer(symp, &sym, addr)) { 898 sym_km = km; 899 sym = *symp; 900 symid = sip->sym_id; 901 } 902 } 903 904 /* 905 * kmdb dmods are normal kernel modules, loaded by krtld as such. To 906 * avoid polluting modinfo, and to keep from confusing the module 907 * subsystem (many dmods have the same names as real kernel modules), 908 * kmdb keeps their modctls separate, and doesn't allow their loading 909 * to be broadcast via the krtld module load/unload mechanism. As a 910 * result, kmdb_kvm doesn't find out about them, and can't turn their 911 * addresses into symbols. This can be most inconvenient during 912 * debugger faults, as the dmod frames will show up without names. 913 * We weren't able to turn the requested address into a symbol, so we'll 914 * take a spin through the dmods, trying to match our address against 915 * their symbols. 916 */ 917 if (sym_km == NULL) { 918 return (kmdb_module_lookup_by_addr(addr, flags, buf, nbytes, 919 symp, sip)); 920 } 921 922 *symp = sym; 923 sip->sym_id = symid; 924 925 found: 926 /* 927 * Once we've found something, copy the final name into the caller's 928 * buffer and prefix it with the load object name if appropriate. 929 */ 930 name = mdb_gelf_sym_name(sym_km->km_symtab, symp); 931 932 if (sym_km == &prmod) { 933 if (buf != NULL) { 934 (void) strncpy(buf, name, nbytes); 935 buf[nbytes - 1] = '\0'; 936 } 937 sip->sym_table = MDB_TGT_PRVSYM; 938 } else { 939 if (buf != NULL) { 940 if (sym_km->km_flags & KM_F_PRIMARY) { 941 (void) strncpy(buf, name, nbytes); 942 buf[nbytes - 1] = '\0'; 943 } else { 944 (void) mdb_snprintf(buf, nbytes, "%s`%s", 945 sym_km->km_name, name); 946 } 947 } 948 sip->sym_table = MDB_TGT_SYMTAB; 949 } 950 951 return (0); 952 } 953 954 static int 955 kmt_lookup_by_name(mdb_tgt_t *t, const char *obj, const char *name, 956 GElf_Sym *symp, mdb_syminfo_t *sip) 957 { 958 kmt_data_t *kmt = t->t_data; 959 kmt_module_t *km; 960 mdb_var_t *v; 961 GElf_Sym sym; 962 uint_t symid; 963 int n; 964 965 if (!kmt->kmt_symavail) 966 return (set_errno(EMDB_NOSYM)); 967 968 switch ((uintptr_t)obj) { 969 case (uintptr_t)MDB_TGT_OBJ_EXEC: 970 case (uintptr_t)MDB_TGT_OBJ_EVERY: 971 km = mdb_list_next(&kmt->kmt_modlist); 972 n = mdb_nv_size(&kmt->kmt_modules); 973 break; 974 975 case (uintptr_t)MDB_TGT_OBJ_RTLD: 976 obj = KMT_RTLD_NAME; 977 /*FALLTHROUGH*/ 978 979 default: 980 /* 981 * If this is a request for a dmod symbol, let kmdb_module 982 * handle it. 983 */ 984 if (obj != NULL && strncmp(obj, "DMOD`", 5) == 0) { 985 return (kmdb_module_lookup_by_name(obj + 5, name, 986 symp, sip)); 987 } 988 989 if ((v = mdb_nv_lookup(&kmt->kmt_modules, obj)) == NULL) 990 return (set_errno(EMDB_NOOBJ)); 991 992 km = mdb_nv_get_cookie(v); 993 n = 1; 994 } 995 996 /* 997 * kmdb's kvm target is at a bit of a disadvantage compared to mdb's 998 * kvm target when it comes to global symbol lookups. mdb has ksyms, 999 * which hides pesky things like symbols that are undefined in unix, 1000 * but which are defined in genunix. We don't have such a facility - 1001 * we simply iterate through the modules, looking for a given symbol 1002 * in each. Unless we're careful, we'll return the undef in the 1003 * aforementioned case. 1004 */ 1005 for (; n > 0; n--, km = mdb_list_next(km)) { 1006 if (mdb_gelf_symtab_lookup_by_name(km->km_symtab, name, 1007 &sym, &symid) == 0 && sym.st_shndx != SHN_UNDEF) 1008 break; 1009 } 1010 1011 if (n == 0) 1012 return (set_errno(EMDB_NOSYM)); 1013 1014 found: 1015 bcopy(&sym, symp, sizeof (GElf_Sym)); 1016 sip->sym_id = symid; 1017 sip->sym_table = MDB_TGT_SYMTAB; 1018 1019 return (0); 1020 } 1021 1022 static int 1023 kmt_symtab_func(void *data, const GElf_Sym *sym, const char *name, uint_t id) 1024 { 1025 kmt_symarg_t *arg = data; 1026 1027 if (mdb_tgt_sym_match(sym, arg->sym_type)) { 1028 arg->sym_info.sym_id = id; 1029 1030 return (arg->sym_cb(arg->sym_data, sym, name, &arg->sym_info, 1031 arg->sym_obj)); 1032 } 1033 1034 return (0); 1035 } 1036 1037 static void 1038 kmt_symtab_iter(mdb_gelf_symtab_t *gst, uint_t type, const char *obj, 1039 mdb_tgt_sym_f *cb, void *p) 1040 { 1041 kmt_symarg_t arg; 1042 1043 arg.sym_cb = cb; 1044 arg.sym_data = p; 1045 arg.sym_type = type; 1046 arg.sym_info.sym_table = gst->gst_tabid; 1047 arg.sym_obj = obj; 1048 1049 mdb_gelf_symtab_iter(gst, kmt_symtab_func, &arg); 1050 } 1051 1052 static int 1053 kmt_symbol_iter(mdb_tgt_t *t, const char *obj, uint_t which, uint_t type, 1054 mdb_tgt_sym_f *cb, void *data) 1055 { 1056 kmt_data_t *kmt = t->t_data; 1057 kmt_module_t *km; 1058 1059 mdb_gelf_symtab_t *symtab = NULL; 1060 mdb_var_t *v; 1061 1062 if (which == MDB_TGT_DYNSYM) 1063 return (set_errno(EMDB_TGTNOTSUP)); 1064 1065 switch ((uintptr_t)obj) { 1066 case (uintptr_t)MDB_TGT_OBJ_EXEC: 1067 case (uintptr_t)MDB_TGT_OBJ_EVERY: 1068 mdb_nv_rewind(&kmt->kmt_modules); 1069 while ((v = mdb_nv_advance(&kmt->kmt_modules)) != NULL) { 1070 km = mdb_nv_get_cookie(v); 1071 1072 if (km->km_symtab != NULL) { 1073 kmt_symtab_iter(km->km_symtab, type, 1074 km->km_name, cb, data); 1075 } 1076 } 1077 return (0); 1078 1079 case (uintptr_t)MDB_TGT_OBJ_RTLD: 1080 obj = KMT_RTLD_NAME; 1081 /*FALLTHROUGH*/ 1082 1083 default: 1084 if (strncmp(obj, "DMOD`", 5) == 0) { 1085 return (kmdb_module_symbol_iter(obj + 5, type, 1086 cb, data)); 1087 } 1088 1089 if ((v = mdb_nv_lookup(&kmt->kmt_modules, obj)) == NULL) 1090 return (set_errno(EMDB_NOOBJ)); 1091 km = mdb_nv_get_cookie(v); 1092 1093 symtab = km->km_symtab; 1094 } 1095 1096 if (symtab != NULL) 1097 kmt_symtab_iter(symtab, type, obj, cb, data); 1098 1099 return (0); 1100 } 1101 1102 static int 1103 kmt_mapping_walk(uintptr_t addr, const void *data, kmt_maparg_t *marg) 1104 { 1105 /* 1106 * This is a bit sketchy but avoids problematic compilation of this 1107 * target against the current VM implementation. Now that we have 1108 * vmem, we can make this less broken and more informative by changing 1109 * this code to invoke the vmem walker in the near future. 1110 */ 1111 const struct kmt_seg { 1112 caddr_t s_base; 1113 size_t s_size; 1114 } *segp = (const struct kmt_seg *)data; 1115 1116 mdb_map_t map; 1117 GElf_Sym sym; 1118 mdb_syminfo_t info; 1119 1120 map.map_base = (uintptr_t)segp->s_base; 1121 map.map_size = segp->s_size; 1122 map.map_flags = MDB_TGT_MAP_R | MDB_TGT_MAP_W | MDB_TGT_MAP_X; 1123 1124 if (kmt_lookup_by_addr(marg->map_target, addr, MDB_TGT_SYM_EXACT, 1125 map.map_name, MDB_TGT_MAPSZ, &sym, &info) == -1) { 1126 1127 (void) mdb_iob_snprintf(map.map_name, MDB_TGT_MAPSZ, 1128 "%lr", addr); 1129 } 1130 1131 return (marg->map_cb(marg->map_data, &map, map.map_name)); 1132 } 1133 1134 static int 1135 kmt_mapping_iter(mdb_tgt_t *t, mdb_tgt_map_f *func, void *private) 1136 { 1137 kmt_maparg_t m; 1138 uintptr_t kas; 1139 1140 m.map_target = t; 1141 m.map_cb = func; 1142 m.map_data = private; 1143 1144 if ((kas = kmt_read_kas(t)) == NULL) 1145 return (-1); /* errno is set for us */ 1146 1147 return (mdb_pwalk("seg", (mdb_walk_cb_t)kmt_mapping_walk, &m, kas)); 1148 } 1149 1150 static const mdb_map_t * 1151 kmt_mod_to_map(kmt_module_t *km, mdb_map_t *map) 1152 { 1153 (void) strncpy(map->map_name, km->km_name, MDB_TGT_MAPSZ); 1154 map->map_name[MDB_TGT_MAPSZ - 1] = '\0'; 1155 map->map_base = km->km_text_va; 1156 map->map_size = km->km_text_size; 1157 map->map_flags = MDB_TGT_MAP_R | MDB_TGT_MAP_W | MDB_TGT_MAP_X; 1158 1159 return (map); 1160 } 1161 1162 static int 1163 kmt_object_iter(mdb_tgt_t *t, mdb_tgt_map_f *func, void *private) 1164 { 1165 kmt_data_t *kmt = t->t_data; 1166 kmt_module_t *km; 1167 mdb_map_t m; 1168 1169 for (km = mdb_list_next(&kmt->kmt_modlist); km != NULL; 1170 km = mdb_list_next(km)) { 1171 if (func(private, kmt_mod_to_map(km, &m), km->km_name) == -1) 1172 break; 1173 } 1174 1175 return (0); 1176 } 1177 1178 static const mdb_map_t * 1179 kmt_addr_to_map(mdb_tgt_t *t, uintptr_t addr) 1180 { 1181 kmt_data_t *kmt = t->t_data; 1182 kmt_module_t *km; 1183 1184 for (km = mdb_list_next(&kmt->kmt_modlist); km != NULL; 1185 km = mdb_list_next(km)) { 1186 if (addr - km->km_text_va < km->km_text_size || 1187 addr - km->km_data_va < km->km_data_size || 1188 addr - km->km_bss_va < km->km_bss_size) 1189 return (kmt_mod_to_map(km, &kmt->kmt_map)); 1190 } 1191 1192 (void) set_errno(EMDB_NOMAP); 1193 return (NULL); 1194 } 1195 1196 static const mdb_map_t * 1197 kmt_name_to_map(mdb_tgt_t *t, const char *name) 1198 { 1199 kmt_data_t *kmt = t->t_data; 1200 kmt_module_t *km; 1201 mdb_map_t m; 1202 1203 /* 1204 * If name is MDB_TGT_OBJ_EXEC, return the first module on the list, 1205 * which will be unix since we keep kmt_modlist in load order. 1206 */ 1207 if (name == MDB_TGT_OBJ_EXEC) { 1208 return (kmt_mod_to_map(mdb_list_next(&kmt->kmt_modlist), 1209 &m)); 1210 } 1211 1212 if (name == MDB_TGT_OBJ_RTLD) 1213 name = KMT_RTLD_NAME; /* replace MDB_TGT_OBJ_RTLD with krtld */ 1214 1215 for (km = mdb_list_next(&kmt->kmt_modlist); km != NULL; 1216 km = mdb_list_next(km)) { 1217 if (strcmp(name, km->km_name) == 0) 1218 return (kmt_mod_to_map(km, &m)); 1219 } 1220 1221 (void) set_errno(EMDB_NOOBJ); 1222 return (NULL); 1223 } 1224 1225 static ctf_file_t * 1226 kmt_load_ctfdata(mdb_tgt_t *t, kmt_module_t *km) 1227 { 1228 kmt_data_t *kmt = t->t_data; 1229 int err; 1230 1231 if (km->km_ctfp != NULL) 1232 return (km->km_ctfp); 1233 1234 if (km->km_ctf_va == NULL || km->km_symtab == NULL) { 1235 (void) set_errno(EMDB_NOCTF); 1236 return (NULL); 1237 } 1238 1239 if ((km->km_ctfp = mdb_ctf_bufopen(km->km_ctf_va, km->km_ctf_size, 1240 km->km_symtab_va, &km->km_symtab_hdr, km->km_strtab_va, 1241 &km->km_strtab_hdr, &err)) == NULL) { 1242 (void) set_errno(ctf_to_errno(err)); 1243 return (NULL); 1244 } 1245 1246 mdb_dprintf(MDB_DBG_KMOD, "loaded %lu bytes of CTF data for %s\n", 1247 (ulong_t)km->km_ctf_size, km->km_name); 1248 1249 if (ctf_parent_name(km->km_ctfp) != NULL) { 1250 mdb_var_t *v; 1251 1252 if ((v = mdb_nv_lookup(&kmt->kmt_modules, 1253 ctf_parent_name(km->km_ctfp))) != NULL) { 1254 kmt_module_t *pm = mdb_nv_get_cookie(v); 1255 1256 if (pm->km_ctfp == NULL) 1257 (void) kmt_load_ctfdata(t, pm); 1258 1259 if (pm->km_ctfp != NULL && ctf_import(km->km_ctfp, 1260 pm->km_ctfp) == CTF_ERR) { 1261 warn("failed to import parent types into " 1262 "%s: %s\n", km->km_name, 1263 ctf_errmsg(ctf_errno(km->km_ctfp))); 1264 } 1265 } else { 1266 warn("failed to load CTF data for %s - parent %s not " 1267 "loaded\n", km->km_name, 1268 ctf_parent_name(km->km_ctfp)); 1269 } 1270 } 1271 1272 return (km->km_ctfp); 1273 } 1274 1275 ctf_file_t * 1276 kmt_addr_to_ctf(mdb_tgt_t *t, uintptr_t addr) 1277 { 1278 kmt_data_t *kmt = t->t_data; 1279 kmt_module_t *km; 1280 1281 for (km = mdb_list_next(&kmt->kmt_modlist); km != NULL; 1282 km = mdb_list_next(km)) { 1283 if (addr - km->km_text_va < km->km_text_size || 1284 addr - km->km_data_va < km->km_data_size || 1285 addr - km->km_bss_va < km->km_bss_size) 1286 return (kmt_load_ctfdata(t, km)); 1287 } 1288 1289 (void) set_errno(EMDB_NOMAP); 1290 return (NULL); 1291 } 1292 1293 ctf_file_t * 1294 kmt_name_to_ctf(mdb_tgt_t *t, const char *name) 1295 { 1296 kmt_data_t *kt = t->t_data; 1297 kmt_module_t *km; 1298 1299 if (name == MDB_TGT_OBJ_EXEC) 1300 name = KMT_CTFPARENT; /* base CTF data is kept in genunix */ 1301 else if (name == MDB_TGT_OBJ_RTLD) 1302 name = KMT_RTLD_NAME; /* replace with krtld */ 1303 1304 for (km = mdb_list_next(&kt->kmt_modlist); km != NULL; 1305 km = mdb_list_next(km)) { 1306 if (strcmp(name, km->km_name) == 0) 1307 return (kmt_load_ctfdata(t, km)); 1308 } 1309 1310 (void) set_errno(EMDB_NOOBJ); 1311 return (NULL); 1312 } 1313 1314 /*ARGSUSED*/ 1315 static int 1316 kmt_status(mdb_tgt_t *t, mdb_tgt_status_t *tsp) 1317 { 1318 int state; 1319 1320 bzero(tsp, sizeof (mdb_tgt_status_t)); 1321 1322 switch ((state = kmdb_dpi_get_state(NULL))) { 1323 case DPI_STATE_INIT: 1324 tsp->st_state = MDB_TGT_RUNNING; 1325 tsp->st_pc = 0; 1326 break; 1327 1328 case DPI_STATE_STOPPED: 1329 tsp->st_state = MDB_TGT_STOPPED; 1330 1331 (void) kmdb_dpi_get_register("pc", &tsp->st_pc); 1332 break; 1333 1334 case DPI_STATE_FAULTED: 1335 tsp->st_state = MDB_TGT_STOPPED; 1336 1337 (void) kmdb_dpi_get_register("pc", &tsp->st_pc); 1338 1339 tsp->st_flags |= MDB_TGT_ISTOP; 1340 break; 1341 1342 case DPI_STATE_LOST: 1343 tsp->st_state = MDB_TGT_LOST; 1344 1345 (void) kmdb_dpi_get_register("pc", &tsp->st_pc); 1346 break; 1347 } 1348 1349 mdb_dprintf(MDB_DBG_KMOD, "kmt_status, dpi: %d tsp: %d, pc = %p %A\n", 1350 state, tsp->st_state, (void *)tsp->st_pc, tsp->st_pc); 1351 1352 return (0); 1353 } 1354 1355 /* 1356 * Invoked when kmt_defbp_enter_debugger is called, this routine activates and 1357 * deactivates deferred breakpoints in response to module load and unload 1358 * events. 1359 */ 1360 /*ARGSUSED*/ 1361 static void 1362 kmt_defbp_event(mdb_tgt_t *t, int vid, void *private) 1363 { 1364 if (kmt_defbp_modchg_isload) { 1365 if (!mdb_tgt_sespec_activate_all(t) && 1366 (mdb.m_flags & MDB_FL_BPTNOSYMSTOP)) { 1367 /* 1368 * We weren't able to activate the breakpoints. 1369 * If so requested, we'll return without calling 1370 * continue, thus throwing the user into the debugger. 1371 */ 1372 return; 1373 } 1374 1375 } else { 1376 mdb_sespec_t *sep, *nsep; 1377 const mdb_map_t *map, *bpmap; 1378 mdb_map_t modmap; 1379 1380 if ((map = kmt_addr_to_map(t, 1381 (uintptr_t)kmt_defbp_modchg_modctl->mod_text)) == NULL) { 1382 warn("module unload notification for unknown module %s", 1383 kmt_defbp_modchg_modctl->mod_modname); 1384 return; /* drop into the debugger */ 1385 } 1386 1387 bcopy(map, &modmap, sizeof (mdb_map_t)); 1388 1389 for (sep = mdb_list_next(&t->t_active); sep; sep = nsep) { 1390 nsep = mdb_list_next(sep); 1391 1392 if (sep->se_ops == &kmt_brkpt_ops) { 1393 kmt_brkpt_t *kb = sep->se_data; 1394 1395 if ((bpmap = kmt_addr_to_map(t, 1396 kb->kb_addr)) == NULL || 1397 (bpmap->map_base == modmap.map_base && 1398 bpmap->map_size == modmap.map_size)) { 1399 mdb_tgt_sespec_idle_one(t, sep, 1400 EMDB_NOMAP); 1401 } 1402 } 1403 } 1404 } 1405 1406 (void) mdb_tgt_continue(t, NULL); 1407 } 1408 1409 static void 1410 kmt_defbp_enter_debugger(void) 1411 { 1412 /* 1413 * The debugger places a breakpoint here. We can't have a simple 1414 * nop function here, because GCC knows much more than we do, and 1415 * will optimize away the call to it. 1416 */ 1417 (void) get_fp(); 1418 } 1419 1420 /* 1421 * This routine is called while the kernel is running. It attempts to determine 1422 * whether any deferred breakpoints exist for the module being changed (loaded 1423 * or unloaded). If any such breakpoints exist, the debugger will be entered to 1424 * process them. 1425 */ 1426 static void 1427 kmt_defbp_modchg(struct modctl *mctl, int isload) 1428 { 1429 kmt_defbp_t *dbp; 1430 1431 kmt_defbp_lock = 1; 1432 1433 for (dbp = mdb_list_next(&kmt_defbp_list); dbp; 1434 dbp = mdb_list_next(dbp)) { 1435 if (!dbp->dbp_ref) 1436 continue; 1437 1438 if (strcmp(mctl->mod_modname, dbp->dbp_objname) == 0) { 1439 /* 1440 * Activate the breakpoint 1441 */ 1442 kmt_defbp_modchg_isload = isload; 1443 kmt_defbp_modchg_modctl = mctl; 1444 1445 kmt_defbp_enter_debugger(); 1446 break; 1447 } 1448 } 1449 1450 kmt_defbp_lock = 0; 1451 } 1452 1453 /*ARGSUSED*/ 1454 static int 1455 kmt_continue(mdb_tgt_t *t, mdb_tgt_status_t *tsp) 1456 { 1457 int n; 1458 1459 kmdb_dpi_resume(); 1460 1461 /* 1462 * The order of the following two calls is important. If there are 1463 * load acks on the work queue, we'll initialize the dmods they 1464 * represent. This will involve a call to _mdb_init, which may very 1465 * well result in a symbol lookup. If we haven't resynced our view 1466 * of symbols with the current state of the world, this lookup could 1467 * end very badly. We therefore make sure to sync before processing 1468 * the work queue. 1469 */ 1470 kmt_sync(t); 1471 kmdb_dpi_process_work_queue(); 1472 1473 if (kmdb_kdi_get_unload_request()) 1474 t->t_flags |= MDB_TGT_F_UNLOAD; 1475 1476 (void) mdb_tgt_status(t, &t->t_status); 1477 1478 if ((n = kmt_dmod_status(NULL, KMDB_MC_STATE_LOADING) + 1479 kmt_dmod_status(NULL, KMDB_MC_STATE_UNLOADING)) != 0) { 1480 mdb_warn("%d dmod load%c/unload%c pending\n", n, 1481 "s"[n == 1], "s"[n == 1]); 1482 } 1483 1484 return (0); 1485 } 1486 1487 /*ARGSUSED*/ 1488 static int 1489 kmt_step(mdb_tgt_t *t, mdb_tgt_status_t *tsp) 1490 { 1491 int rc; 1492 1493 if ((rc = kmdb_dpi_step()) == 0) 1494 (void) mdb_tgt_status(t, &t->t_status); 1495 1496 return (rc); 1497 } 1498 1499 static int 1500 kmt_defbp_activate(mdb_tgt_t *t) 1501 { 1502 kmdb_dpi_modchg_register(kmt_defbp_modchg); 1503 1504 /* 1505 * The routines that add and arm breakpoints will check for the proper 1506 * DTrace state, but they'll just put this breakpoint on the idle list 1507 * if DTrace is active. It'll correctly move to the active list when 1508 * DTrace deactivates, but that's insufficient for our purposes -- we 1509 * need to do extra processing at that point. We won't get to do said 1510 * processing with with a normal idle->active transition, so we just 1511 * won't add it add it until we're sure that it'll stick. 1512 */ 1513 1514 if (kmdb_kdi_dtrace_get_state() == KDI_DTSTATE_DTRACE_ACTIVE) 1515 return (set_errno(EMDB_DTACTIVE)); 1516 1517 kmt_defbp_bpspec = mdb_tgt_add_vbrkpt(t, 1518 (uintptr_t)kmt_defbp_enter_debugger, 1519 MDB_TGT_SPEC_HIDDEN, kmt_defbp_event, NULL); 1520 1521 return (0); 1522 } 1523 1524 static void 1525 kmt_defbp_deactivate(mdb_tgt_t *t) 1526 { 1527 kmdb_dpi_modchg_cancel(); 1528 1529 if (kmt_defbp_bpspec != 0) { 1530 if (t != NULL) 1531 (void) mdb_tgt_vespec_delete(t, kmt_defbp_bpspec); 1532 1533 kmt_defbp_bpspec = 0; 1534 } 1535 } 1536 1537 static kmt_defbp_t * 1538 kmt_defbp_create(mdb_tgt_t *t, const char *objname, const char *symname) 1539 { 1540 kmt_defbp_t *dbp = mdb_alloc(sizeof (kmt_defbp_t), UM_SLEEP); 1541 1542 mdb_dprintf(MDB_DBG_KMOD, "defbp_create %s`%s\n", objname, symname); 1543 1544 dbp->dbp_objname = strdup(objname); 1545 dbp->dbp_symname = strdup(symname); 1546 dbp->dbp_ref = 1; 1547 1548 kmt_defbp_num++; 1549 1550 if (kmt_defbp_num == 1 || kmt_defbp_bpspec == 0) { 1551 if (kmt_defbp_activate(t) < 0) 1552 warn("failed to activate deferred breakpoints"); 1553 } 1554 1555 mdb_list_append(&kmt_defbp_list, dbp); 1556 1557 return (dbp); 1558 } 1559 1560 static void 1561 kmt_defbp_destroy(kmt_defbp_t *dbp) 1562 { 1563 mdb_dprintf(MDB_DBG_KMOD, "defbp_destroy %s`%s\n", dbp->dbp_objname, 1564 dbp->dbp_symname); 1565 1566 mdb_list_delete(&kmt_defbp_list, dbp); 1567 1568 strfree(dbp->dbp_objname); 1569 strfree(dbp->dbp_symname); 1570 mdb_free(dbp, sizeof (kmt_defbp_t)); 1571 } 1572 1573 static void 1574 kmt_defbp_prune_common(int all) 1575 { 1576 kmt_defbp_t *dbp, *ndbp; 1577 1578 /* We can't remove items from the list while the driver is using it. */ 1579 if (kmt_defbp_lock) 1580 return; 1581 1582 for (dbp = mdb_list_next(&kmt_defbp_list); dbp != NULL; dbp = ndbp) { 1583 ndbp = mdb_list_next(dbp); 1584 1585 if (!all && dbp->dbp_ref) 1586 continue; 1587 1588 kmt_defbp_destroy(dbp); 1589 } 1590 } 1591 1592 static void 1593 kmt_defbp_prune(void) 1594 { 1595 kmt_defbp_prune_common(0); 1596 } 1597 1598 static void 1599 kmt_defbp_destroy_all(void) 1600 { 1601 kmt_defbp_prune_common(1); 1602 } 1603 1604 static void 1605 kmt_defbp_delete(mdb_tgt_t *t, kmt_defbp_t *dbp) 1606 { 1607 dbp->dbp_ref = 0; 1608 1609 ASSERT(kmt_defbp_num > 0); 1610 kmt_defbp_num--; 1611 1612 if (kmt_defbp_num == 0) 1613 kmt_defbp_deactivate(t); 1614 1615 kmt_defbp_prune(); 1616 } 1617 1618 static int 1619 kmt_brkpt_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args) 1620 { 1621 mdb_tgt_status_t tsp; 1622 kmt_bparg_t *ka = args; 1623 kmt_brkpt_t *kb; 1624 GElf_Sym s; 1625 mdb_instr_t instr; 1626 1627 (void) mdb_tgt_status(t, &tsp); 1628 if (tsp.st_state != MDB_TGT_RUNNING && tsp.st_state != MDB_TGT_STOPPED) 1629 return (set_errno(EMDB_NOPROC)); 1630 1631 if (ka->ka_symbol != NULL) { 1632 if (mdb_tgt_lookup_by_scope(t, ka->ka_symbol, &s, NULL) == -1) { 1633 if (errno != EMDB_NOOBJ && !(errno == EMDB_NOSYM && 1634 !(mdb.m_flags & MDB_FL_BPTNOSYMSTOP))) { 1635 warn("breakpoint %s activation failed", 1636 ka->ka_symbol); 1637 } 1638 return (-1); /* errno is set for us */ 1639 } 1640 1641 ka->ka_addr = (uintptr_t)s.st_value; 1642 } 1643 1644 #ifdef __sparc 1645 if (ka->ka_addr & 3) 1646 return (set_errno(EMDB_BPALIGN)); 1647 #endif 1648 1649 if (mdb_vread(&instr, sizeof (instr), ka->ka_addr) != sizeof (instr)) 1650 return (-1); /* errno is set for us */ 1651 1652 if (kmdb_kdi_dtrace_get_state() == KDI_DTSTATE_DTRACE_ACTIVE) 1653 warn("breakpoint will not arm until DTrace is inactive\n"); 1654 1655 kb = mdb_zalloc(sizeof (kmt_brkpt_t), UM_SLEEP); 1656 kb->kb_addr = ka->ka_addr; 1657 sep->se_data = kb; 1658 1659 return (0); 1660 } 1661 1662 /*ARGSUSED*/ 1663 static void 1664 kmt_brkpt_dtor(mdb_tgt_t *t, mdb_sespec_t *sep) 1665 { 1666 mdb_free(sep->se_data, sizeof (kmt_brkpt_t)); 1667 } 1668 1669 /*ARGSUSED*/ 1670 static char * 1671 kmt_brkpt_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep, 1672 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes) 1673 { 1674 uintptr_t addr = NULL; 1675 1676 if (vep != NULL) { 1677 kmt_bparg_t *ka = vep->ve_args; 1678 1679 if (ka->ka_symbol != NULL) { 1680 (void) mdb_iob_snprintf(buf, nbytes, "stop at %s", 1681 ka->ka_symbol); 1682 } else { 1683 (void) mdb_iob_snprintf(buf, nbytes, "stop at %a", 1684 ka->ka_addr); 1685 addr = ka->ka_addr; 1686 } 1687 1688 } else { 1689 addr = ((kmt_brkpt_t *)sep->se_data)->kb_addr; 1690 (void) mdb_iob_snprintf(buf, nbytes, "stop at %a", addr); 1691 } 1692 1693 sp->spec_base = addr; 1694 sp->spec_size = sizeof (mdb_instr_t); 1695 1696 return (buf); 1697 } 1698 1699 static int 1700 kmt_brkpt_secmp(mdb_tgt_t *t, mdb_sespec_t *sep, void *args) 1701 { 1702 kmt_brkpt_t *kb = sep->se_data; 1703 kmt_bparg_t *ka = args; 1704 GElf_Sym sym; 1705 1706 if (ka->ka_symbol != NULL) { 1707 return (mdb_tgt_lookup_by_scope(t, ka->ka_symbol, 1708 &sym, NULL) == 0 && sym.st_value == kb->kb_addr); 1709 } 1710 1711 return (ka->ka_addr == kb->kb_addr); 1712 } 1713 1714 /*ARGSUSED*/ 1715 static int 1716 kmt_brkpt_vecmp(mdb_tgt_t *t, mdb_vespec_t *vep, void *args) 1717 { 1718 kmt_bparg_t *ka1 = vep->ve_args; 1719 kmt_bparg_t *ka2 = args; 1720 1721 if (ka1->ka_symbol != NULL && ka2->ka_symbol != NULL) 1722 return (strcmp(ka1->ka_symbol, ka2->ka_symbol) == 0); 1723 1724 if (ka1->ka_symbol == NULL && ka2->ka_symbol == NULL) 1725 return (ka1->ka_addr == ka2->ka_addr); 1726 1727 return (0); /* fail if one is symbolic, other is an explicit address */ 1728 } 1729 1730 static int 1731 kmt_brkpt_arm(mdb_tgt_t *t, mdb_sespec_t *sep) 1732 { 1733 kmt_data_t *kmt = t->t_data; 1734 kmt_brkpt_t *kb = sep->se_data; 1735 int rv; 1736 1737 if (kmdb_kdi_dtrace_get_state() == KDI_DTSTATE_DTRACE_ACTIVE) 1738 return (set_errno(EMDB_DTACTIVE)); 1739 1740 if ((rv = kmdb_dpi_brkpt_arm(kb->kb_addr, &kb->kb_oinstr)) != 0) 1741 return (rv); 1742 1743 if (kmt->kmt_narmedbpts++ == 0) 1744 (void) kmdb_kdi_dtrace_set(KDI_DTSET_KMDB_BPT_ACTIVATE); 1745 1746 return (0); 1747 } 1748 1749 static int 1750 kmt_brkpt_disarm(mdb_tgt_t *t, mdb_sespec_t *sep) 1751 { 1752 kmt_data_t *kmt = t->t_data; 1753 kmt_brkpt_t *kb = sep->se_data; 1754 int rv; 1755 1756 ASSERT(kmdb_kdi_dtrace_get_state() == KDI_DTSTATE_KMDB_BPT_ACTIVE); 1757 1758 if ((rv = kmdb_dpi_brkpt_disarm(kb->kb_addr, kb->kb_oinstr)) != 0) 1759 return (rv); 1760 1761 if (--kmt->kmt_narmedbpts == 0) 1762 (void) kmdb_kdi_dtrace_set(KDI_DTSET_KMDB_BPT_DEACTIVATE); 1763 1764 return (0); 1765 } 1766 1767 /* 1768 * Determine whether the specified sespec is an armed watchpoint that overlaps 1769 * with the given breakpoint and has the given flags set. We use this to find 1770 * conflicts with breakpoints, below. 1771 */ 1772 static int 1773 kmt_wp_overlap(mdb_sespec_t *sep, kmt_brkpt_t *kb, int flags) 1774 { 1775 const kmdb_wapt_t *wp = sep->se_data; 1776 1777 return (sep->se_state == MDB_TGT_SPEC_ARMED && 1778 sep->se_ops == &kmt_wapt_ops && (wp->wp_wflags & flags) && 1779 kb->kb_addr - wp->wp_addr < wp->wp_size); 1780 } 1781 1782 /* 1783 * We step over breakpoints using our single-stepper. If a conflicting 1784 * watchpoint is present, we must temporarily remove it before stepping over the 1785 * breakpoint so we don't immediately re-trigger the watchpoint. We know the 1786 * watchpoint has already triggered on our trap instruction as part of fetching 1787 * it. Before we return, we must re-install any disabled watchpoints. 1788 */ 1789 static int 1790 kmt_brkpt_cont(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp) 1791 { 1792 kmt_brkpt_t *kb = sep->se_data; 1793 int status = -1; 1794 int error; 1795 1796 for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) { 1797 if (kmt_wp_overlap(sep, kb, MDB_TGT_WA_X)) 1798 (void) kmdb_dpi_wapt_disarm(sep->se_data); 1799 } 1800 1801 if (kmdb_dpi_brkpt_disarm(kb->kb_addr, kb->kb_oinstr) == 0 && 1802 kmt_step(t, tsp) == 0) 1803 status = kmt_status(t, tsp); 1804 1805 error = errno; /* save errno from disarm, step, or status */ 1806 1807 for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) { 1808 if (kmt_wp_overlap(sep, kb, MDB_TGT_WA_X)) 1809 kmdb_dpi_wapt_arm(sep->se_data); 1810 } 1811 1812 (void) set_errno(error); 1813 return (status); 1814 } 1815 1816 /*ARGSUSED*/ 1817 static int 1818 kmt_brkpt_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp) 1819 { 1820 kmt_brkpt_t *kb = sep->se_data; 1821 int state, why; 1822 kreg_t pc; 1823 1824 state = kmdb_dpi_get_state(&why); 1825 (void) kmdb_dpi_get_register("pc", &pc); 1826 1827 return (state == DPI_STATE_FAULTED && why == DPI_STATE_WHY_BKPT && 1828 pc == kb->kb_addr); 1829 } 1830 1831 static const mdb_se_ops_t kmt_brkpt_ops = { 1832 kmt_brkpt_ctor, /* se_ctor */ 1833 kmt_brkpt_dtor, /* se_dtor */ 1834 kmt_brkpt_info, /* se_info */ 1835 kmt_brkpt_secmp, /* se_secmp */ 1836 kmt_brkpt_vecmp, /* se_vecmp */ 1837 kmt_brkpt_arm, /* se_arm */ 1838 kmt_brkpt_disarm, /* se_disarm */ 1839 kmt_brkpt_cont, /* se_cont */ 1840 kmt_brkpt_match /* se_match */ 1841 }; 1842 1843 static int 1844 kmt_wapt_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args) 1845 { 1846 mdb_tgt_status_t tsp; 1847 kmdb_wapt_t *vwp = args; 1848 kmdb_wapt_t *swp; 1849 1850 (void) mdb_tgt_status(t, &tsp); 1851 if (tsp.st_state != MDB_TGT_RUNNING && tsp.st_state != MDB_TGT_STOPPED) 1852 return (set_errno(EMDB_NOPROC)); 1853 1854 swp = mdb_alloc(sizeof (kmdb_wapt_t), UM_SLEEP); 1855 bcopy(vwp, swp, sizeof (kmdb_wapt_t)); 1856 1857 if (kmdb_dpi_wapt_reserve(swp) < 0) { 1858 mdb_free(swp, sizeof (kmdb_wapt_t)); 1859 return (-1); /* errno is set for us */ 1860 } 1861 1862 sep->se_data = swp; 1863 1864 return (0); 1865 } 1866 1867 /*ARGSUSED*/ 1868 static void 1869 kmt_wapt_dtor(mdb_tgt_t *t, mdb_sespec_t *sep) 1870 { 1871 kmdb_wapt_t *wp = sep->se_data; 1872 1873 kmdb_dpi_wapt_release(wp); 1874 mdb_free(wp, sizeof (kmdb_wapt_t)); 1875 } 1876 1877 /*ARGSUSED*/ 1878 static char * 1879 kmt_wapt_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep, 1880 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes) 1881 { 1882 kmdb_wapt_t *wp = vep != NULL ? vep->ve_args : sep->se_data; 1883 const char *fmt; 1884 char desc[24]; 1885 1886 ASSERT(wp->wp_wflags != 0); 1887 desc[0] = '\0'; 1888 1889 switch (wp->wp_wflags) { 1890 case MDB_TGT_WA_R: 1891 (void) strcat(desc, "/read"); 1892 break; 1893 case MDB_TGT_WA_W: 1894 (void) strcat(desc, "/write"); 1895 break; 1896 case MDB_TGT_WA_X: 1897 (void) strcat(desc, "/exec"); 1898 break; 1899 default: 1900 if (wp->wp_wflags & MDB_TGT_WA_R) 1901 (void) strcat(desc, "/r"); 1902 if (wp->wp_wflags & MDB_TGT_WA_W) 1903 (void) strcat(desc, "/w"); 1904 if (wp->wp_wflags & MDB_TGT_WA_X) 1905 (void) strcat(desc, "/x"); 1906 } 1907 1908 switch (wp->wp_type) { 1909 case DPI_WAPT_TYPE_PHYS: 1910 fmt = "stop on %s of phys [%p, %p)"; 1911 break; 1912 1913 case DPI_WAPT_TYPE_VIRT: 1914 fmt = "stop on %s of [%la, %la)"; 1915 break; 1916 1917 case DPI_WAPT_TYPE_IO: 1918 if (wp->wp_size == 1) 1919 fmt = "stop on %s of I/O port %p"; 1920 else 1921 fmt = "stop on %s of I/O port [%p, %p)"; 1922 break; 1923 } 1924 1925 (void) mdb_iob_snprintf(buf, nbytes, fmt, desc + 1, wp->wp_addr, 1926 wp->wp_addr + wp->wp_size); 1927 1928 sp->spec_base = wp->wp_addr; 1929 sp->spec_size = wp->wp_size; 1930 1931 return (buf); 1932 } 1933 1934 /*ARGSUSED*/ 1935 static int 1936 kmt_wapt_secmp(mdb_tgt_t *t, mdb_sespec_t *sep, void *args) 1937 { 1938 kmdb_wapt_t *wp1 = sep->se_data; 1939 kmdb_wapt_t *wp2 = args; 1940 1941 return (wp1->wp_addr == wp2->wp_addr && wp1->wp_size == wp2->wp_size && 1942 wp1->wp_wflags == wp2->wp_wflags); 1943 } 1944 1945 /*ARGSUSED*/ 1946 static int 1947 kmt_wapt_vecmp(mdb_tgt_t *t, mdb_vespec_t *vep, void *args) 1948 { 1949 kmdb_wapt_t *wp1 = vep->ve_args; 1950 kmdb_wapt_t *wp2 = args; 1951 1952 return (wp1->wp_addr == wp2->wp_addr && wp1->wp_size == wp2->wp_size && 1953 wp1->wp_wflags == wp2->wp_wflags); 1954 } 1955 1956 /*ARGSUSED*/ 1957 static int 1958 kmt_wapt_arm(mdb_tgt_t *t, mdb_sespec_t *sep) 1959 { 1960 kmdb_dpi_wapt_arm(sep->se_data); 1961 1962 return (0); 1963 } 1964 1965 /*ARGSUSED*/ 1966 static int 1967 kmt_wapt_disarm(mdb_tgt_t *t, mdb_sespec_t *sep) 1968 { 1969 kmdb_dpi_wapt_disarm(sep->se_data); 1970 1971 return (0); 1972 } 1973 1974 /* 1975 * Determine whether the specified sespec is an armed breakpoint at the given 1976 * %pc. We use this to find conflicts with watchpoints below. 1977 */ 1978 static int 1979 kmt_bp_overlap(mdb_sespec_t *sep, uintptr_t pc) 1980 { 1981 kmt_brkpt_t *kb = sep->se_data; 1982 1983 return (sep->se_state == MDB_TGT_SPEC_ARMED && 1984 sep->se_ops == &kmt_brkpt_ops && kb->kb_addr == pc); 1985 } 1986 1987 /* 1988 * We step over watchpoints using our single-stepper. If a conflicting 1989 * breakpoint is present, we must temporarily disarm it before stepping over 1990 * the watchpoint so we do not immediately re-trigger the breakpoint. This is 1991 * similar to the case handled in kmt_brkpt_cont(), above. 1992 */ 1993 static int 1994 kmt_wapt_cont(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp) 1995 { 1996 mdb_sespec_t *bep = NULL; 1997 int status = -1; 1998 int error, why; 1999 2000 /* 2001 * If we stopped for anything other than a watchpoint, check to see 2002 * if there's a breakpoint here. 2003 */ 2004 if (!(kmdb_dpi_get_state(&why) == DPI_STATE_FAULTED && 2005 (why == DPI_STATE_WHY_V_WAPT || why == DPI_STATE_WHY_P_WAPT))) { 2006 kreg_t pc; 2007 2008 (void) kmdb_dpi_get_register("pc", &pc); 2009 2010 for (bep = mdb_list_next(&t->t_active); bep != NULL; 2011 bep = mdb_list_next(bep)) { 2012 if (kmt_bp_overlap(bep, pc)) { 2013 (void) bep->se_ops->se_disarm(t, bep); 2014 bep->se_state = MDB_TGT_SPEC_ACTIVE; 2015 break; 2016 } 2017 } 2018 } 2019 2020 kmdb_dpi_wapt_disarm(sep->se_data); 2021 if (kmt_step(t, tsp) == 0) 2022 status = kmt_status(t, tsp); 2023 2024 error = errno; /* save errno from step or status */ 2025 2026 if (bep != NULL) 2027 mdb_tgt_sespec_arm_one(t, bep); 2028 2029 (void) set_errno(error); 2030 return (status); 2031 } 2032 2033 /*ARGSUSED*/ 2034 static int 2035 kmt_wapt_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp) 2036 { 2037 return (kmdb_dpi_wapt_match(sep->se_data)); 2038 } 2039 2040 static const mdb_se_ops_t kmt_wapt_ops = { 2041 kmt_wapt_ctor, /* se_ctor */ 2042 kmt_wapt_dtor, /* se_dtor */ 2043 kmt_wapt_info, /* se_info */ 2044 kmt_wapt_secmp, /* se_secmp */ 2045 kmt_wapt_vecmp, /* se_vecmp */ 2046 kmt_wapt_arm, /* se_arm */ 2047 kmt_wapt_disarm, /* se_disarm */ 2048 kmt_wapt_cont, /* se_cont */ 2049 kmt_wapt_match /* se_match */ 2050 }; 2051 2052 /*ARGSUSED*/ 2053 static int 2054 kmt_trap_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args) 2055 { 2056 sep->se_data = args; /* trap number */ 2057 2058 return (0); 2059 } 2060 2061 /*ARGSUSED*/ 2062 static char * 2063 kmt_trap_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep, 2064 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes) 2065 { 2066 const char *name; 2067 int trapnum; 2068 2069 if (vep != NULL) 2070 trapnum = (intptr_t)vep->ve_args; 2071 else 2072 trapnum = (intptr_t)sep->se_data; 2073 2074 if (trapnum == KMT_TRAP_ALL) 2075 name = "any trap"; 2076 else if (trapnum == KMT_TRAP_NOTENUM) 2077 name = "miscellaneous trap"; 2078 else 2079 name = kmt_trapname(trapnum); 2080 2081 (void) mdb_iob_snprintf(buf, nbytes, "single-step stop on %s", name); 2082 2083 return (buf); 2084 } 2085 2086 /*ARGSUSED2*/ 2087 static int 2088 kmt_trap_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp) 2089 { 2090 int spectt = (intptr_t)sep->se_data; 2091 kmt_data_t *kmt = t->t_data; 2092 kreg_t tt; 2093 2094 (void) kmdb_dpi_get_register("tt", &tt); 2095 2096 switch (spectt) { 2097 case KMT_TRAP_ALL: 2098 return (1); 2099 case KMT_TRAP_NOTENUM: 2100 return (tt > kmt->kmt_trapmax || 2101 !BT_TEST(kmt->kmt_trapmap, tt)); 2102 default: 2103 return (tt == spectt); 2104 } 2105 } 2106 2107 static const mdb_se_ops_t kmt_trap_ops = { 2108 kmt_trap_ctor, /* se_ctor */ 2109 no_se_dtor, /* se_dtor */ 2110 kmt_trap_info, /* se_info */ 2111 no_se_secmp, /* se_secmp */ 2112 no_se_vecmp, /* se_vecmp */ 2113 no_se_arm, /* se_arm */ 2114 no_se_disarm, /* se_disarm */ 2115 no_se_cont, /* se_cont */ 2116 kmt_trap_match /* se_match */ 2117 }; 2118 2119 static void 2120 kmt_bparg_dtor(mdb_vespec_t *vep) 2121 { 2122 kmt_bparg_t *ka = vep->ve_args; 2123 2124 if (ka->ka_symbol != NULL) 2125 strfree(ka->ka_symbol); 2126 2127 if (ka->ka_defbp != NULL) 2128 kmt_defbp_delete(mdb.m_target, ka->ka_defbp); 2129 2130 mdb_free(ka, sizeof (kmt_bparg_t)); 2131 } 2132 2133 static int 2134 kmt_add_vbrkpt(mdb_tgt_t *t, uintptr_t addr, 2135 int spec_flags, mdb_tgt_se_f *func, void *data) 2136 { 2137 kmt_bparg_t *ka = mdb_alloc(sizeof (kmt_bparg_t), UM_SLEEP); 2138 2139 ka->ka_addr = addr; 2140 ka->ka_symbol = NULL; 2141 ka->ka_defbp = NULL; 2142 2143 return (mdb_tgt_vespec_insert(t, &kmt_brkpt_ops, spec_flags, 2144 func, data, ka, kmt_bparg_dtor)); 2145 } 2146 2147 static int 2148 kmt_add_sbrkpt(mdb_tgt_t *t, const char *fullname, 2149 int spec_flags, mdb_tgt_se_f *func, void *data) 2150 { 2151 kmt_bparg_t *ka; 2152 kmt_defbp_t *dbp; 2153 GElf_Sym sym; 2154 char *tick, *objname, *symname; 2155 int serrno; 2156 2157 if ((tick = strchr(fullname, '`')) == fullname) { 2158 (void) set_errno(EMDB_NOOBJ); 2159 return (0); 2160 } 2161 2162 /* 2163 * Deferred breakpoints are always scoped. If we didn't find a tick, 2164 * there's no scope. We'll create a vbrkpt, but only if we can turn the 2165 * provided string into an address. 2166 */ 2167 if (tick == NULL) { 2168 uintptr_t addr; 2169 2170 if (strisbasenum(fullname)) { 2171 addr = mdb_strtoull(fullname); /* a bare address */ 2172 } else if (mdb_tgt_lookup_by_name(t, MDB_TGT_OBJ_EVERY, 2173 fullname, &sym, NULL) < 0) { 2174 (void) set_errno(EMDB_NOSYM); 2175 return (0); 2176 } else { 2177 addr = (uintptr_t)sym.st_value; /* unscoped sym name */ 2178 } 2179 2180 return (kmt_add_vbrkpt(t, addr, spec_flags, func, data)); 2181 } 2182 2183 if (*(tick + 1) == '\0') { 2184 (void) set_errno(EMDB_NOSYM); 2185 return (0); 2186 } 2187 2188 objname = strndup(fullname, tick - fullname); 2189 symname = tick + 1; 2190 2191 if (mdb_tgt_lookup_by_name(t, objname, symname, NULL, NULL) < 0 && 2192 errno != EMDB_NOOBJ) { 2193 serrno = errno; 2194 strfree(objname); 2195 2196 (void) set_errno(serrno); 2197 return (0); /* errno is set for us */ 2198 } 2199 2200 dbp = kmt_defbp_create(t, objname, symname); 2201 strfree(objname); 2202 2203 ka = mdb_alloc(sizeof (kmt_bparg_t), UM_SLEEP); 2204 ka->ka_symbol = strdup(fullname); 2205 ka->ka_addr = NULL; 2206 ka->ka_defbp = dbp; 2207 2208 return (mdb_tgt_vespec_insert(t, &kmt_brkpt_ops, spec_flags, 2209 func, data, ka, kmt_bparg_dtor)); 2210 } 2211 2212 static int 2213 kmt_wparg_overlap(const kmdb_wapt_t *wp1, const kmdb_wapt_t *wp2) 2214 { 2215 /* Assume the watchpoint spaces don't overlap */ 2216 if (wp1->wp_type != wp2->wp_type) 2217 return (0); 2218 2219 if (wp2->wp_addr + wp2->wp_size <= wp1->wp_addr) 2220 return (0); /* no range overlap */ 2221 2222 if (wp1->wp_addr + wp1->wp_size <= wp2->wp_addr) 2223 return (0); /* no range overlap */ 2224 2225 return (wp1->wp_addr != wp2->wp_addr || wp1->wp_size != wp2->wp_size || 2226 wp1->wp_wflags != wp2->wp_wflags); 2227 } 2228 2229 static void 2230 kmt_wparg_dtor(mdb_vespec_t *vep) 2231 { 2232 mdb_free(vep->ve_args, sizeof (kmdb_wapt_t)); 2233 } 2234 2235 static int 2236 kmt_add_wapt_common(mdb_tgt_t *t, uintptr_t addr, size_t len, uint_t wflags, 2237 int spec_flags, mdb_tgt_se_f *func, void *data, int type) 2238 { 2239 kmdb_wapt_t *wp = mdb_alloc(sizeof (kmdb_wapt_t), UM_SLEEP); 2240 mdb_sespec_t *sep; 2241 2242 wp->wp_addr = addr; 2243 wp->wp_size = len; 2244 wp->wp_type = type; 2245 wp->wp_wflags = wflags; 2246 2247 if (kmdb_dpi_wapt_validate(wp) < 0) 2248 return (0); /* errno is set for us */ 2249 2250 for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) { 2251 if (sep->se_ops == &kmt_wapt_ops && 2252 mdb_list_next(&sep->se_velist) != NULL && 2253 kmt_wparg_overlap(wp, sep->se_data)) 2254 goto wapt_dup; 2255 } 2256 2257 for (sep = mdb_list_next(&t->t_idle); sep; sep = mdb_list_next(sep)) { 2258 if (sep->se_ops == &kmt_wapt_ops && kmt_wparg_overlap(wp, 2259 ((mdb_vespec_t *)mdb_list_next(&sep->se_velist))->ve_args)) 2260 goto wapt_dup; 2261 } 2262 2263 return (mdb_tgt_vespec_insert(t, &kmt_wapt_ops, spec_flags, 2264 func, data, wp, kmt_wparg_dtor)); 2265 2266 wapt_dup: 2267 mdb_free(wp, sizeof (kmdb_wapt_t)); 2268 (void) set_errno(EMDB_WPDUP); 2269 return (0); 2270 } 2271 2272 static int 2273 kmt_add_pwapt(mdb_tgt_t *t, physaddr_t addr, size_t len, uint_t wflags, 2274 int spec_flags, mdb_tgt_se_f *func, void *data) 2275 { 2276 return (kmt_add_wapt_common(t, (uintptr_t)addr, len, wflags, spec_flags, 2277 func, data, DPI_WAPT_TYPE_PHYS)); 2278 } 2279 2280 static int 2281 kmt_add_vwapt(mdb_tgt_t *t, uintptr_t addr, size_t len, uint_t wflags, 2282 int spec_flags, mdb_tgt_se_f *func, void *data) 2283 { 2284 return (kmt_add_wapt_common(t, addr, len, wflags, spec_flags, func, 2285 data, DPI_WAPT_TYPE_VIRT)); 2286 } 2287 2288 static int 2289 kmt_add_iowapt(mdb_tgt_t *t, uintptr_t addr, size_t len, uint_t wflags, 2290 int spec_flags, mdb_tgt_se_f *func, void *data) 2291 { 2292 return (kmt_add_wapt_common(t, addr, len, wflags, spec_flags, func, 2293 data, DPI_WAPT_TYPE_IO)); 2294 } 2295 2296 static int 2297 kmt_add_trap(mdb_tgt_t *t, int trapnum, int spec_flags, mdb_tgt_se_f *func, 2298 void *data) 2299 { 2300 kmt_data_t *kmt = t->t_data; 2301 2302 if (trapnum != KMT_TRAP_ALL && trapnum != KMT_TRAP_NOTENUM) { 2303 if (trapnum < 0 || trapnum > kmt->kmt_trapmax) { 2304 (void) set_errno(EMDB_BADFLTNUM); 2305 return (0); 2306 } 2307 2308 BT_SET(kmt->kmt_trapmap, trapnum); 2309 } 2310 2311 return (mdb_tgt_vespec_insert(t, &kmt_trap_ops, spec_flags, func, data, 2312 (void *)(uintptr_t)trapnum, no_ve_dtor)); 2313 } 2314 2315 /*ARGSUSED*/ 2316 static uintmax_t 2317 kmt_cpuid_disc_get(const mdb_var_t *v) 2318 { 2319 return (kmdb_dpi_get_master_cpuid()); 2320 } 2321 2322 static const mdb_nv_disc_t kmt_cpuid_disc = { 2323 NULL, 2324 kmt_cpuid_disc_get 2325 }; 2326 2327 /* 2328 * This routine executes while the kernel is running. 2329 */ 2330 void 2331 kmt_activate(mdb_tgt_t *t) 2332 { 2333 kmt_data_t *kmt = t->t_data; 2334 2335 mdb_prop_postmortem = FALSE; 2336 mdb_prop_kernel = TRUE; 2337 2338 (void) mdb_tgt_register_dcmds(t, &kmt_dcmds[0], MDB_MOD_FORCE); 2339 mdb_tgt_register_regvars(t, kmt->kmt_rds, &kmt_reg_disc, 0); 2340 } 2341 2342 static void 2343 kmt_destroy(mdb_tgt_t *t) 2344 { 2345 kmt_data_t *kmt = t->t_data; 2346 kmt_module_t *km, *pkm; 2347 2348 mdb_nv_destroy(&kmt->kmt_modules); 2349 for (km = mdb_list_prev(&kmt->kmt_modlist); km != NULL; km = pkm) { 2350 pkm = mdb_list_prev(km); 2351 mdb_free(km, sizeof (kmt_module_t)); 2352 } 2353 2354 if (!kmt_defbp_lock) 2355 kmt_defbp_destroy_all(); 2356 2357 if (kmt->kmt_trapmap != NULL) 2358 mdb_free(kmt->kmt_trapmap, BT_SIZEOFMAP(kmt->kmt_trapmax)); 2359 2360 if (kmt->kmt_cpu != NULL) 2361 kmt_cpu_destroy(kmt->kmt_cpu); 2362 2363 if (kmt != NULL) 2364 mdb_free(kmt, sizeof (kmt_data_t)); 2365 } 2366 2367 static const mdb_tgt_ops_t kmt_ops = { 2368 kmt_setflags, /* t_setflags */ 2369 (int (*)()) mdb_tgt_notsup, /* t_setcontext */ 2370 kmt_activate, /* t_activate */ 2371 (void (*)()) mdb_tgt_nop, /* t_deactivate */ 2372 kmt_periodic, /* t_periodic */ 2373 kmt_destroy, /* t_destroy */ 2374 kmt_name, /* t_name */ 2375 (const char *(*)()) mdb_conf_isa, /* t_isa */ 2376 kmt_platform, /* t_platform */ 2377 kmt_uname, /* t_uname */ 2378 kmt_dmodel, /* t_dmodel */ 2379 (ssize_t (*)()) mdb_tgt_notsup, /* t_aread */ 2380 (ssize_t (*)()) mdb_tgt_notsup, /* t_awrite */ 2381 kmt_read, /* t_vread */ 2382 kmt_write, /* t_vwrite */ 2383 kmt_pread, /* t_pread */ 2384 kmt_pwrite, /* t_pwrite */ 2385 kmt_read, /* t_fread */ 2386 kmt_write, /* t_fwrite */ 2387 kmt_ioread, /* t_ioread */ 2388 kmt_iowrite, /* t_iowrite */ 2389 kmt_vtop, /* t_vtop */ 2390 kmt_lookup_by_name, /* t_lookup_by_name */ 2391 kmt_lookup_by_addr, /* t_lookup_by_addr */ 2392 kmt_symbol_iter, /* t_symbol_iter */ 2393 kmt_mapping_iter, /* t_mapping_iter */ 2394 kmt_object_iter, /* t_object_iter */ 2395 kmt_addr_to_map, /* t_addr_to_map */ 2396 kmt_name_to_map, /* t_name_to_map */ 2397 kmt_addr_to_ctf, /* t_addr_to_ctf */ 2398 kmt_name_to_ctf, /* t_name_to_ctf */ 2399 kmt_status, /* t_status */ 2400 (int (*)()) mdb_tgt_notsup, /* t_run */ 2401 kmt_step, /* t_step */ 2402 kmt_step_out, /* t_step_out */ 2403 kmt_step_branch, /* t_step_branch */ 2404 kmt_next, /* t_next */ 2405 kmt_continue, /* t_cont */ 2406 (int (*)()) mdb_tgt_notsup, /* t_signal */ 2407 kmt_add_vbrkpt, /* t_add_vbrkpt */ 2408 kmt_add_sbrkpt, /* t_add_sbrkpt */ 2409 kmt_add_pwapt, /* t_add_pwapt */ 2410 kmt_add_vwapt, /* t_add_vwapt */ 2411 kmt_add_iowapt, /* t_add_iowapt */ 2412 (int (*)()) mdb_tgt_null, /* t_add_sysenter */ 2413 (int (*)()) mdb_tgt_null, /* t_add_sysexit */ 2414 (int (*)()) mdb_tgt_null, /* t_add_signal */ 2415 kmt_add_trap, /* t_add_fault */ 2416 kmt_getareg, /* t_getareg */ 2417 kmt_putareg, /* t_putareg */ 2418 (int (*)()) mdb_tgt_nop /* XXX t_stack_iter */ 2419 }; 2420 2421 /* 2422 * Called immediately upon resumption of the system after a step or continue. 2423 * Allows us to synchronize kmt's view of the world with reality. 2424 */ 2425 /*ARGSUSED*/ 2426 static void 2427 kmt_sync(mdb_tgt_t *t) 2428 { 2429 kmt_data_t *kmt = t->t_data; 2430 int symavail; 2431 2432 mdb_dprintf(MDB_DBG_KMOD, "synchronizing with kernel\n"); 2433 2434 symavail = kmt->kmt_symavail; 2435 kmt->kmt_symavail = FALSE; 2436 2437 /* 2438 * Resync our view of the world if the modules have changed, or if we 2439 * didn't have any symbols coming into this function. The latter will 2440 * only happen on startup. 2441 */ 2442 if (kmdb_kdi_mods_changed() || !symavail) 2443 kmt_modlist_update(t); 2444 2445 /* 2446 * It would be nice if we could run this less frequently, perhaps 2447 * after a dvec-initiated trigger. 2448 */ 2449 kmdb_module_sync(); 2450 2451 kmt->kmt_symavail = TRUE; 2452 2453 mdb_dprintf(MDB_DBG_KMOD, "synchronization complete\n"); 2454 2455 kmt_defbp_prune(); 2456 2457 if (kmt_defbp_num > 0 && kmt_defbp_bpspec == 0 && 2458 kmdb_kdi_dtrace_get_state() != KDI_DTSTATE_DTRACE_ACTIVE) { 2459 /* 2460 * Deferred breakpoints were created while DTrace was active, 2461 * and consequently the deferred breakpoint enabling mechanism 2462 * wasn't activated. Activate it now, and then try to activate 2463 * the deferred breakpoints. We do this so that we can catch 2464 * the ones which may apply to modules that have been loaded 2465 * while they were waiting for DTrace to deactivate. 2466 */ 2467 (void) kmt_defbp_activate(t); 2468 (void) mdb_tgt_sespec_activate_all(t); 2469 } 2470 2471 if (kmt->kmt_cpu_retry && ((kmt->kmt_cpu = kmt_cpu_create(t)) != 2472 NULL || errno != EAGAIN)) 2473 kmt->kmt_cpu_retry = FALSE; 2474 2475 (void) mdb_tgt_status(t, &t->t_status); 2476 } 2477 2478 /* 2479 * This routine executes while the kernel is running. 2480 */ 2481 /*ARGSUSED*/ 2482 int 2483 kmdb_kvm_create(mdb_tgt_t *t, int argc, const char *argv[]) 2484 { 2485 kmt_data_t *kmt; 2486 2487 if (argc != 0) 2488 return (set_errno(EINVAL)); 2489 2490 kmt = mdb_zalloc(sizeof (kmt_data_t), UM_SLEEP); 2491 t->t_data = kmt; 2492 t->t_ops = &kmt_ops; 2493 t->t_flags |= MDB_TGT_F_RDWR; /* kmdb is always r/w */ 2494 2495 (void) mdb_nv_insert(&mdb.m_nv, "cpuid", &kmt_cpuid_disc, 0, 2496 MDB_NV_PERSIST | MDB_NV_RDONLY); 2497 2498 (void) mdb_nv_create(&kmt->kmt_modules, UM_SLEEP); 2499 2500 kmt_init_isadep(t); 2501 2502 kmt->kmt_symavail = FALSE; 2503 kmt->kmt_cpu_retry = TRUE; 2504 2505 bzero(&kmt_defbp_list, sizeof (mdb_list_t)); 2506 2507 return (0); 2508 2509 create_err: 2510 kmt_destroy(t); 2511 2512 return (-1); 2513 } 2514 2515 /* 2516 * This routine is called once, when kmdb first has control of the world. 2517 */ 2518 void 2519 kmdb_kvm_startup(void) 2520 { 2521 mdb_dprintf(MDB_DBG_KMOD, "kmdb_kvm startup\n"); 2522 2523 kmt_sync(mdb.m_target); 2524 (void) mdb_module_load_builtin(KMT_MODULE); 2525 kmt_startup_isadep(mdb.m_target); 2526 2527 /* 2528 * This is here because we need to write the deferred breakpoint 2529 * breakpoint when the debugger starts. Our normal r/o write routines 2530 * don't work when the kernel is running, so we have to do it during 2531 * startup. 2532 */ 2533 (void) mdb_tgt_sespec_activate_all(mdb.m_target); 2534 } 2535 2536 /* 2537 * This routine is called after kmdb has loaded its initial set of modules. 2538 */ 2539 void 2540 kmdb_kvm_poststartup(void) 2541 { 2542 mdb_dprintf(MDB_DBG_KMOD, "kmdb_kvm post-startup\n"); 2543 2544 (void) mdb_dis_select(kmt_def_dismode()); 2545 } 2546