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