dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / mdb / common / kmdb / kmdb_kvm.c
blobc9c7576f452df54032ddf4b5ff7c2c9b93fb8c3f
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
26 #include <kmdb/kmdb_kvm.h>
27 #include <kmdb/kvm.h>
28 #include <kmdb/kmdb_kdi.h>
29 #include <kmdb/kmdb_promif.h>
30 #include <kmdb/kmdb_module.h>
31 #include <kmdb/kmdb_asmutil.h>
32 #include <mdb/mdb_types.h>
33 #include <mdb/mdb_conf.h>
34 #include <mdb/mdb_err.h>
35 #include <mdb/mdb_modapi.h>
36 #include <mdb/mdb_target_impl.h>
37 #include <mdb/mdb_debug.h>
38 #include <mdb/mdb_string.h>
39 #include <mdb/mdb_ctf.h>
40 #include <mdb/mdb_kreg_impl.h>
41 #include <mdb/mdb_ks.h>
42 #include <mdb/mdb.h>
44 #include <strings.h>
45 #include <dlfcn.h>
46 #include <sys/isa_defs.h>
47 #include <sys/kobj.h>
48 #include <sys/kobj_impl.h>
49 #include <sys/bitmap.h>
50 #include <vm/as.h>
52 static const char KMT_RTLD_NAME[] = "krtld";
53 static const char KMT_MODULE[] = "mdb_ks";
54 static const char KMT_CTFPARENT[] = "genunix";
56 static mdb_list_t kmt_defbp_list; /* List of current deferred bp's */
57 static int kmt_defbp_lock; /* For list, running kernel holds */
58 static uint_t kmt_defbp_modchg_isload; /* Whether mod change is load/unload */
59 static struct modctl *kmt_defbp_modchg_modctl; /* modctl for defbp checking */
60 static uint_t kmt_defbp_num; /* Number of referenced def'd bp's */
61 static int kmt_defbp_bpspec; /* vespec for def'd bp activation bp */
63 static const mdb_se_ops_t kmt_brkpt_ops;
64 static const mdb_se_ops_t kmt_wapt_ops;
66 static void kmt_sync(mdb_tgt_t *);
68 typedef struct kmt_symarg {
69 mdb_tgt_sym_f *sym_cb; /* Caller's callback function */
70 void *sym_data; /* Callback function argument */
71 uint_t sym_type; /* Symbol type/binding filter */
72 mdb_syminfo_t sym_info; /* Symbol id and table id */
73 const char *sym_obj; /* Containing object */
74 } kmt_symarg_t;
76 typedef struct kmt_maparg {
77 mdb_tgt_t *map_target; /* Target used for mapping iter */
78 mdb_tgt_map_f *map_cb; /* Caller's callback function */
79 void *map_data; /* Callback function argument */
80 } kmt_maparg_t;
82 /*ARGSUSED*/
83 int
84 kmt_setflags(mdb_tgt_t *t, int flags)
87 * We only handle one flag (ALLOWIO), and we can't fail to set or clear
88 * it, so we just blindly replace the t_flags version with the one
89 * passed.
91 t->t_flags = (t->t_flags & ~MDB_TGT_F_ALLOWIO) |
92 (flags & MDB_TGT_F_ALLOWIO);
94 return (0);
97 /*ARGSUSED*/
98 const char *
99 kmt_name(mdb_tgt_t *t)
101 return ("kmdb_kvm");
104 /*ARGSUSED*/
105 static const char *
106 kmt_platform(mdb_tgt_t *t)
108 static char platform[SYS_NMLN];
110 if (kmdb_dpi_get_state(NULL) == DPI_STATE_INIT)
111 return (mdb_conf_platform());
113 if (mdb_tgt_readsym(mdb.m_target, MDB_TGT_AS_VIRT, platform,
114 sizeof (platform), "unix", "platform") != sizeof (platform)) {
115 warn("'platform' symbol is missing from kernel\n");
116 return ("unknown");
119 return (platform);
122 static int
123 kmt_uname(mdb_tgt_t *t, struct utsname *utsp)
125 return (mdb_tgt_readsym(t, MDB_TGT_AS_VIRT, utsp,
126 sizeof (struct utsname), MDB_TGT_OBJ_EXEC, "utsname"));
129 /*ARGSUSED*/
130 static int
131 kmt_dmodel(mdb_tgt_t *t)
133 return (MDB_TGT_MODEL_NATIVE);
136 /*ARGSUSED*/
137 ssize_t
138 kmt_rw(mdb_tgt_t *t, void *buf, size_t nbytes, uint64_t addr,
139 ssize_t (*rw)(void *, size_t, uint64_t))
142 * chunksz needs to be volatile because of the use of setjmp() in this
143 * function.
145 volatile size_t chunksz;
146 size_t n, ndone;
147 jmp_buf *oldpcb = NULL;
148 jmp_buf pcb;
149 ssize_t res;
151 kmdb_prom_check_interrupt();
153 if (nbytes == 0)
154 return (0);
157 * Try to process the entire buffer, as requested. If we catch a fault,
158 * try smaller chunks. This allows us to handle regions that cross
159 * mapping boundaries.
161 chunksz = nbytes;
162 ndone = 0;
163 if (setjmp(pcb) != 0) {
164 if (chunksz == 1) {
165 /* We failed with the smallest chunk - give up */
166 kmdb_dpi_restore_fault_hdlr(oldpcb);
167 return (ndone > 0 ? ndone : -1); /* errno set for us */
168 } else if (chunksz > 4)
169 chunksz = 4;
170 else
171 chunksz = 1;
174 oldpcb = kmdb_dpi_set_fault_hdlr(&pcb);
175 while (nbytes > 0) {
176 n = MIN(chunksz, nbytes);
178 if ((res = rw(buf, n, addr)) != n)
179 return (res < 0 ? res : ndone + res);
181 addr += n;
182 nbytes -= n;
183 ndone += n;
184 buf = ((caddr_t)buf + n);
187 kmdb_dpi_restore_fault_hdlr(oldpcb);
189 return (ndone);
192 static void
193 kmt_bcopy(const void *s1, void *s2, size_t n)
196 * We need to guarantee atomic accesses for certain sizes. bcopy won't
197 * make that guarantee, so we need to do it ourselves.
199 #ifdef _LP64
200 if (n == 8 && ((uintptr_t)s1 & 7) == 0 && ((uintptr_t)s2 & 7) == 0)
201 *(uint64_t *)s2 = *(uint64_t *)s1;
202 else
203 #endif
204 if (n == 4 && ((uintptr_t)s1 & 3) == 0 && ((uintptr_t)s2 & 3) == 0)
205 *(uint32_t *)s2 = *(uint32_t *)s1;
206 else if (n == 2 && ((uintptr_t)s1 & 1) == 0 && ((uintptr_t)s2 & 1) == 0)
207 *(uint16_t *)s2 = *(uint16_t *)s1;
208 else if (n == 1)
209 *(uint8_t *)s2 = *(uint8_t *)s1;
210 else
211 bcopy(s1, s2, n);
214 static ssize_t
215 kmt_reader(void *buf, size_t nbytes, uint64_t addr)
217 kmt_bcopy((void *)(uintptr_t)addr, buf, nbytes);
218 return (nbytes);
221 ssize_t
222 kmt_writer(void *buf, size_t nbytes, uint64_t addr)
224 kmt_bcopy(buf, (void *)(uintptr_t)addr, nbytes);
225 return (nbytes);
228 /*ARGSUSED*/
229 static ssize_t
230 kmt_read(mdb_tgt_t *t, void *buf, size_t nbytes, uintptr_t addr)
233 * We don't want to allow reads of I/O-mapped memory. Multi-page reads
234 * that cross into I/O-mapped memory should be restricted to the initial
235 * non-I/O region. Reads that begin in I/O-mapped memory are failed
236 * outright.
238 if (!(t->t_flags & MDB_TGT_F_ALLOWIO) &&
239 (nbytes = kmdb_kdi_range_is_nontoxic(addr, nbytes, 0)) == 0)
240 return (set_errno(EMDB_NOMAP));
242 return (kmt_rw(t, buf, nbytes, addr, kmt_reader));
245 /*ARGSUSED*/
246 static ssize_t
247 kmt_pread(mdb_tgt_t *t, void *buf, size_t nbytes, physaddr_t addr)
249 return (kmt_rw(t, buf, nbytes, addr, kmdb_kdi_pread));
252 /*ARGSUSED*/
253 ssize_t
254 kmt_pwrite(mdb_tgt_t *t, const void *buf, size_t nbytes, physaddr_t addr)
256 return (kmt_rw(t, (void *)buf, nbytes, addr, kmdb_kdi_pwrite));
259 static uintptr_t
260 kmt_read_kas(mdb_tgt_t *t)
262 GElf_Sym sym;
264 if (mdb_tgt_lookup_by_name(t, "unix", "kas", &sym, NULL) < 0) {
265 warn("'kas' symbol is missing from kernel\n");
266 (void) set_errno(EMDB_NOSYM);
267 return (0);
270 return ((uintptr_t)sym.st_value);
273 static int
274 kmt_vtop(mdb_tgt_t *t, mdb_tgt_as_t as, uintptr_t va, physaddr_t *pap)
276 mdb_module_t *mod;
277 struct as *asp;
278 mdb_var_t *v;
280 switch ((uintptr_t)as) {
281 case (uintptr_t)MDB_TGT_AS_PHYS:
282 case (uintptr_t)MDB_TGT_AS_FILE:
283 case (uintptr_t)MDB_TGT_AS_IO:
284 return (set_errno(EINVAL));
285 case (uintptr_t)MDB_TGT_AS_VIRT:
286 if ((asp = (struct as *)kmt_read_kas(t)) == NULL)
287 return (-1); /* errno is set for us */
288 break;
289 default:
290 asp = (struct as *)as;
292 /* We don't support non-kas vtop */
293 if (asp != (struct as *)kmt_read_kas(t))
294 return (set_errno(EMDB_TGTNOTSUP));
297 if (kmdb_prom_vtop(va, pap) == 0)
298 return (0);
300 if ((v = mdb_nv_lookup(&mdb.m_modules, "unix")) != NULL &&
301 (mod = mdb_nv_get_cookie(v)) != NULL) {
302 int (*fptr)(uintptr_t, struct as *, physaddr_t *);
304 fptr = (int (*)(uintptr_t, struct as *, physaddr_t *))
305 dlsym(mod->mod_hdl, "platform_vtop");
307 if ((fptr != NULL) && ((*fptr)(va, asp, pap) == 0))
308 return (0);
311 return (set_errno(EMDB_NOMAP));
314 /*ARGSUSED*/
315 static int
316 kmt_cpuregs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
318 const mdb_tgt_gregset_t *gregs;
319 intptr_t cpuid = DPI_MASTER_CPUID;
320 int i;
322 if (flags & DCMD_ADDRSPEC) {
323 if (argc != 0)
324 return (DCMD_USAGE);
325 if ((cpuid = mdb_cpu2cpuid(addr)) < 0) {
326 (void) set_errno(EMDB_NOMAP);
327 mdb_warn("failed to find cpuid for cpu at %p", addr);
328 return (DCMD_ERR);
332 i = mdb_getopts(argc, argv,
333 'c', MDB_OPT_UINTPTR, &cpuid,
334 NULL);
336 argc -= i;
337 argv += i;
339 if (argc != 0)
340 return (DCMD_USAGE);
342 if ((gregs = kmdb_dpi_get_gregs(cpuid)) == NULL) {
343 warn("failed to retrieve registers for cpu %d", (int)cpuid);
344 return (DCMD_ERR);
347 kmt_printregs(gregs);
349 return (DCMD_OK);
352 static int
353 kmt_regs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
355 if (flags & DCMD_ADDRSPEC)
356 return (DCMD_USAGE);
358 return (kmt_cpuregs(addr, flags, argc, argv));
361 static int
362 kmt_cpustack_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
364 intptr_t cpuid = DPI_MASTER_CPUID;
365 uint_t verbose = 0;
366 int i;
368 if (flags & DCMD_ADDRSPEC) {
369 if ((cpuid = mdb_cpu2cpuid(addr)) < 0) {
370 (void) set_errno(EMDB_NOMAP);
371 mdb_warn("failed to find cpuid for cpu at %p", addr);
372 return (DCMD_ERR);
374 flags &= ~DCMD_ADDRSPEC;
377 i = mdb_getopts(argc, argv,
378 'c', MDB_OPT_UINTPTR, &cpuid,
379 'v', MDB_OPT_SETBITS, 1, &verbose,
380 NULL);
382 argc -= i;
383 argv += i;
385 return (kmt_cpustack(addr, flags, argc, argv, cpuid, verbose));
389 * Lasciate ogne speranza, voi ch'intrate.
391 static int
392 kmt_call(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
394 uintptr_t *call_argv, rval;
395 int parse_strings = 1;
396 GElf_Sym sym;
397 jmp_buf *oldpcb = NULL;
398 jmp_buf pcb;
399 int i;
401 if (!(flags & DCMD_ADDRSPEC))
402 return (DCMD_USAGE);
404 if (mdb_tgt_lookup_by_addr(mdb.m_target, addr, MDB_TGT_SYM_EXACT,
405 NULL, 0, &sym, NULL) == 0 && GELF_ST_TYPE(sym.st_info) !=
406 STT_FUNC) {
407 warn("%a is not a function\n", addr);
408 return (DCMD_ERR);
411 if (argc > 1 && argv[0].a_type == MDB_TYPE_STRING &&
412 strcmp(argv[0].a_un.a_str, "-s") == 0) {
413 parse_strings = 0;
414 argc--;
415 argv++;
418 call_argv = mdb_alloc(sizeof (uintptr_t) * argc, UM_SLEEP);
420 for (i = 0; i < argc; i++) {
421 switch (argv[i].a_type) {
422 case MDB_TYPE_STRING:
424 * mdb_strtoull doesn't return on error, so we have to
425 * pre-check strings suspected to contain numbers.
427 if (parse_strings && strisbasenum(argv[i].a_un.a_str)) {
428 call_argv[i] = (uintptr_t)mdb_strtoull(
429 argv[i].a_un.a_str);
430 } else
431 call_argv[i] = (uintptr_t)argv[i].a_un.a_str;
433 break;
435 case MDB_TYPE_IMMEDIATE:
436 call_argv[i] = argv[i].a_un.a_val;
437 break;
439 default:
440 mdb_free(call_argv,
441 sizeof (uintptr_t) * argc);
442 return (DCMD_USAGE);
446 if (setjmp(pcb) != 0) {
447 warn("call failed: caught a trap\n");
449 kmdb_dpi_restore_fault_hdlr(oldpcb);
450 mdb_free(call_argv, sizeof (uintptr_t) * argc);
451 return (DCMD_ERR);
454 oldpcb = kmdb_dpi_set_fault_hdlr(&pcb);
455 rval = kmdb_dpi_call(addr, argc, call_argv);
456 kmdb_dpi_restore_fault_hdlr(oldpcb);
458 if (flags & DCMD_PIPE_OUT) {
459 mdb_printf("%p\n", rval);
460 } else {
461 /* pretty-print the results */
462 mdb_printf("%p = %a(", rval, addr);
463 for (i = 0; i < argc; i++) {
464 if (i > 0)
465 mdb_printf(", ");
466 if (argv[i].a_type == MDB_TYPE_STRING) {
467 /* I'm ashamed but amused */
468 char *quote = &("\""[parse_strings &&
469 strisbasenum(argv[i].a_un.a_str)]);
471 mdb_printf("%s%s%s", quote, argv[i].a_un.a_str,
472 quote);
473 } else
474 mdb_printf("%p", argv[i].a_un.a_val);
476 mdb_printf(");\n");
479 mdb_free(call_argv, sizeof (uintptr_t) * argc);
481 return (DCMD_OK);
484 /*ARGSUSED*/
486 kmt_dump_crumbs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
488 intptr_t cpu = -1;
490 if (flags & DCMD_ADDRSPEC) {
491 if (argc != 0)
492 return (DCMD_USAGE);
493 } else {
494 addr = 0;
496 if (mdb_getopts(argc, argv,
497 'c', MDB_OPT_UINTPTR, &cpu,
498 NULL) != argc)
499 return (DCMD_USAGE);
502 kmdb_dpi_dump_crumbs(addr, cpu);
504 return (DCMD_OK);
507 /*ARGSUSED*/
508 static int
509 kmt_noducttape(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
511 int a = 0;
513 return (a/a);
516 static int
517 kmt_dmod_status(char *msg, int state)
519 kmdb_modctl_t *kmc;
520 mdb_var_t *v;
521 int first = 1, n = 0;
523 mdb_nv_rewind(&mdb.m_dmodctl);
524 while ((v = mdb_nv_advance(&mdb.m_dmodctl)) != NULL) {
525 kmc = MDB_NV_COOKIE(v);
527 if (kmc->kmc_state != state)
528 continue;
530 n++;
532 if (msg != NULL) {
533 if (first) {
534 mdb_printf(msg, NULL);
535 first = 0;
538 mdb_printf(" %s", kmc->kmc_modname);
542 if (!first && msg != NULL)
543 mdb_printf("\n");
545 return (n);
548 /*ARGSUSED*/
549 static int
550 kmt_status_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
552 kmt_data_t *kmt = mdb.m_target->t_data;
553 struct utsname uts;
554 char uuid[37];
555 kreg_t tt;
557 if (mdb_tgt_readsym(mdb.m_target, MDB_TGT_AS_VIRT, &uts, sizeof (uts),
558 "unix", "utsname") != sizeof (uts)) {
559 warn("failed to read 'utsname' struct from kernel\n");
560 bzero(&uts, sizeof (uts));
561 (void) strcpy(uts.nodename, "unknown machine");
564 mdb_printf("debugging live kernel (%d-bit) on %s\n",
565 (int)(sizeof (void *) * NBBY),
566 (*uts.nodename == '\0' ? "(not set)" : uts.nodename));
567 mdb_printf("operating system: %s %s (%s)\n",
568 uts.release, uts.version, uts.machine);
570 if (mdb_tgt_readsym(mdb.m_target, MDB_TGT_AS_VIRT, uuid, sizeof (uuid),
571 "genunix", "dump_osimage_uuid") != sizeof (uuid)) {
572 warn("failed to read 'dump_osimage_uuid' string from kernel\n");
573 (void) strcpy(uuid, "(error)");
574 } else if (*uuid == '\0') {
575 (void) strcpy(uuid, "(not set)");
576 } else if (uuid[36] != '\0') {
577 (void) strcpy(uuid, "(invalid)");
579 mdb_printf("image uuid: %s\n", uuid);
581 if (kmt->kmt_cpu != NULL) {
582 mdb_printf("CPU-specific support: %s\n",
583 kmt_cpu_name(kmt->kmt_cpu));
586 mdb_printf("DTrace state: %s\n", (kmdb_kdi_dtrace_get_state() ==
587 KDI_DTSTATE_DTRACE_ACTIVE ? "active (debugger breakpoints cannot "
588 "be armed)" : "inactive"));
590 (void) kmdb_dpi_get_register("tt", &tt);
591 mdb_printf("stopped on: %s\n", kmt_trapname(tt));
593 (void) kmt_dmod_status("pending dmod loads:", KMDB_MC_STATE_LOADING);
594 (void) kmt_dmod_status("pending dmod unloads:",
595 KMDB_MC_STATE_UNLOADING);
597 return (DCMD_OK);
600 /*ARGSUSED*/
601 static int
602 kmt_switch(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
604 if (!(flags & DCMD_ADDRSPEC) || argc != 0)
605 return (DCMD_USAGE);
607 if (kmdb_dpi_switch_master((int)addr) < 0) {
608 warn("failed to switch to CPU %d", (int)addr);
609 return (DCMD_ERR);
612 return (DCMD_OK);
615 static const mdb_dcmd_t kmt_dcmds[] = {
616 { "$c", "?[cnt]", "print stack backtrace", kmt_stack },
617 { "$C", "?[cnt]", "print stack backtrace", kmt_stackv },
618 { "$r", NULL, "print general-purpose registers", kmt_regs },
619 { "$?", NULL, "print status and registers", kmt_regs },
620 { ":x", ":", "change the active CPU", kmt_switch },
621 { "call", ":[arg ...]", "call a kernel function", kmt_call },
622 { "cpustack", "?[-v] [-c cpuid] [cnt]", "print stack backtrace for a "
623 "specific CPU", kmt_cpustack_dcmd },
624 { "cpuregs", "?[-c cpuid]", "print general-purpose registers for a "
625 "specific CPU", kmt_cpuregs },
626 { "crumbs", NULL, NULL, kmt_dump_crumbs },
627 #if defined(__i386) || defined(__amd64)
628 { "in", ":[-L len]", "read from I/O port", kmt_in_dcmd },
629 { "out", ":[-L len] val", "write to I/O port", kmt_out_dcmd },
630 { "rdmsr", ":", "read an MSR", kmt_rdmsr },
631 { "wrmsr", ": val", "write an MSR", kmt_wrmsr },
632 { "rdpcicfg", ": bus dev func", "read a register in PCI config space",
633 kmt_rdpcicfg },
634 { "wrpcicfg", ": bus dev func val", "write a register in PCI config "
635 "space", kmt_wrpcicfg },
636 #endif
637 { "noducttape", NULL, NULL, kmt_noducttape },
638 { "regs", NULL, "print general-purpose registers", kmt_regs },
639 { "stack", "?[cnt]", "print stack backtrace", kmt_stack },
640 { "stackregs", "?", "print stack backtrace and registers", kmt_stackr },
641 { "status", NULL, "print summary of current target", kmt_status_dcmd },
642 { "switch", ":", "change the active CPU", kmt_switch },
643 { NULL }
646 static uintmax_t
647 kmt_reg_disc_get(const mdb_var_t *v)
649 mdb_tgt_reg_t r = 0;
651 (void) mdb_tgt_getareg(MDB_NV_COOKIE(v), 0, mdb_nv_get_name(v), &r);
653 return (r);
656 static void
657 kmt_reg_disc_set(mdb_var_t *v, uintmax_t r)
659 if (mdb_tgt_putareg(MDB_NV_COOKIE(v), 0, mdb_nv_get_name(v), r) == -1)
660 warn("failed to modify %%%s register", mdb_nv_get_name(v));
663 static const mdb_nv_disc_t kmt_reg_disc = {
664 kmt_reg_disc_set,
665 kmt_reg_disc_get
668 /*ARGSUSED*/
669 static int
670 kmt_getareg(mdb_tgt_t *t, mdb_tgt_tid_t tid, const char *rname,
671 mdb_tgt_reg_t *rp)
673 kreg_t val;
675 if (kmdb_dpi_get_register(rname, &val) < 0)
676 return (set_errno(EMDB_BADREG));
678 *rp = val;
679 return (0);
682 /*ARGSUSED*/
683 static int
684 kmt_putareg(mdb_tgt_t *t, mdb_tgt_tid_t tid, const char *rname, mdb_tgt_reg_t r)
686 if (kmdb_dpi_set_register(rname, r) < 0)
687 return (set_errno(EMDB_BADREG));
689 return (0);
692 static void
693 kmt_mod_destroy(kmt_module_t *km)
695 if (km->km_name != NULL)
696 strfree(km->km_name);
697 if (km->km_symtab != NULL)
698 mdb_gelf_symtab_destroy(km->km_symtab);
699 if (km->km_ctfp != NULL)
700 mdb_ctf_close(km->km_ctfp);
703 static kmt_module_t *
704 kmt_mod_create(mdb_tgt_t *t, struct modctl *ctlp, char *name)
706 kmt_module_t *km = mdb_zalloc(sizeof (kmt_module_t), UM_SLEEP);
707 struct module *mod;
709 km->km_name = mdb_alloc(strlen(name) + 1, UM_SLEEP);
710 (void) strcpy(km->km_name, name);
712 bcopy(ctlp, &km->km_modctl, sizeof (struct modctl));
714 if (mdb_tgt_vread(t, &km->km_module, sizeof (struct module),
715 (uintptr_t)km->km_modctl.mod_mp) != sizeof (struct module))
716 goto create_module_cleanup;
717 mod = &km->km_module;
719 if (mod->symhdr != NULL && mod->strhdr != NULL && mod->symtbl != NULL &&
720 mod->strings != NULL) {
721 mdb_gelf_ehdr_to_gehdr(&mod->hdr, &km->km_ehdr);
723 km->km_symtab = mdb_gelf_symtab_create_raw(&km->km_ehdr,
724 mod->symhdr, mod->symtbl, mod->strhdr, mod->strings,
725 MDB_TGT_SYMTAB);
727 km->km_symtab_va = mod->symtbl;
728 km->km_strtab_va = mod->strings;
730 if (mdb_tgt_vread(t, &km->km_symtab_hdr, sizeof (Shdr),
731 (uintptr_t)mod->symhdr) != sizeof (Shdr) ||
732 mdb_tgt_vread(t, &km->km_strtab_hdr, sizeof (Shdr),
733 (uintptr_t)mod->strhdr) != sizeof (Shdr))
734 goto create_module_cleanup;
738 * We don't want everyone rooting around in the module structure, so we
739 * make copies of the interesting members.
741 km->km_text_va = (uintptr_t)mod->text;
742 km->km_text_size = mod->text_size;
743 km->km_data_va = (uintptr_t)mod->data;
744 km->km_data_size = mod->data_size;
745 km->km_bss_va = (uintptr_t)mod->bss;
746 km->km_bss_size = mod->bss_size;
747 km->km_ctf_va = mod->ctfdata;
748 km->km_ctf_size = mod->ctfsize;
750 if (mod->flags & KOBJ_PRIM)
751 km->km_flags |= KM_F_PRIMARY;
753 return (km);
755 create_module_cleanup:
756 warn("failed to read module %s\n", name);
757 kmt_mod_destroy(km);
758 return (NULL);
761 static void
762 kmt_mod_remove(kmt_data_t *kmt, kmt_module_t *km)
764 mdb_var_t *v = mdb_nv_lookup(&kmt->kmt_modules, km->km_name);
766 ASSERT(v != NULL);
768 mdb_dprintf(MDB_DBG_KMOD, "removing module %s\n", km->km_name);
770 mdb_list_delete(&kmt->kmt_modlist, km);
771 mdb_nv_remove(&kmt->kmt_modules, v);
772 kmt_mod_destroy(km);
775 static int
776 kmt_modlist_update_cb(struct modctl *modp, void *arg)
778 mdb_tgt_t *t = arg;
779 kmt_data_t *kmt = t->t_data;
780 kmt_module_t *km;
781 mdb_var_t *v;
782 char name[MAXNAMELEN];
784 if (mdb_tgt_readstr(t, MDB_TGT_AS_VIRT, name, MAXNAMELEN,
785 (uintptr_t)modp->mod_modname) <= 0) {
786 warn("failed to read module name at %p",
787 (void *)modp->mod_modname);
790 /* We only care about modules that are actually loaded */
791 if (!kmdb_kdi_mod_isloaded(modp))
792 return (0);
795 * Skip the modules we already know about and that haven't
796 * changed since last time we were here.
798 if ((v = mdb_nv_lookup(&kmt->kmt_modules, name)) != NULL) {
799 km = MDB_NV_COOKIE(v);
801 if (kmdb_kdi_mod_haschanged(&km->km_modctl, &km->km_module,
802 modp, modp->mod_mp)) {
804 * The module has changed since last we saw it. For
805 * safety, remove our old version, and treat it as a
806 * new module.
808 mdb_dprintf(MDB_DBG_KMOD, "stutter module %s\n", name);
809 kmt_mod_remove(kmt, km);
810 } else {
811 km->km_seen = 1;
812 return (0);
816 mdb_dprintf(MDB_DBG_KMOD, "found new module %s\n", name);
818 if ((km = kmt_mod_create(t, modp, name)) != NULL) {
819 mdb_list_append(&kmt->kmt_modlist, km);
820 (void) mdb_nv_insert(&kmt->kmt_modules, name, NULL,
821 (uintptr_t)km, 0);
822 km->km_seen = 1;
825 return (0);
828 static void
829 kmt_modlist_update(mdb_tgt_t *t)
831 kmt_data_t *kmt = t->t_data;
832 kmt_module_t *km, *kmn;
834 if (kmdb_kdi_mod_iter(kmt_modlist_update_cb, t) < 0) {
835 warn("failed to complete update of kernel module list\n");
836 return;
839 km = mdb_list_next(&kmt->kmt_modlist);
840 while (km != NULL) {
841 kmn = mdb_list_next(km);
843 if (km->km_seen == 1) {
844 /* Reset the mark for next time */
845 km->km_seen = 0;
846 } else {
848 * We didn't see it on the kernel's module list, so
849 * remove it from our view of the world.
851 kmt_mod_remove(kmt, km);
854 km = kmn;
858 static void
859 kmt_periodic(mdb_tgt_t *t)
861 (void) mdb_tgt_status(t, &t->t_status);
865 kmt_lookup_by_addr(mdb_tgt_t *t, uintptr_t addr, uint_t flags,
866 char *buf, size_t nbytes, GElf_Sym *symp, mdb_syminfo_t *sip)
868 kmt_data_t *kmt = t->t_data;
869 kmt_module_t *km = mdb_list_next(&kmt->kmt_modlist);
870 kmt_module_t *sym_km = NULL;
871 kmt_module_t prmod;
872 GElf_Sym sym;
873 uint_t symid;
874 const char *name;
877 * We look through the private symbols (if any), then through the module
878 * symbols. We can simplify the loop if we pretend the private symbols
879 * come from a module.
881 if (mdb.m_prsym != NULL) {
882 bzero(&prmod, sizeof (kmt_module_t));
883 prmod.km_name = "<<<prmod>>>";
884 prmod.km_symtab = mdb.m_prsym;
885 prmod.km_list.ml_next = (mdb_list_t *)km;
886 km = &prmod;
889 /* Symbol resolution isn't available during initialization */
890 if (kmdb_dpi_get_state(NULL) == DPI_STATE_INIT)
891 return (set_errno(EMDB_NOSYM));
893 for (; km != NULL; km = mdb_list_next(km)) {
894 if (km != &prmod && !kmt->kmt_symavail)
895 continue;
897 if (km->km_symtab == NULL)
898 continue;
900 if (mdb_gelf_symtab_lookup_by_addr(km->km_symtab, addr, flags,
901 buf, nbytes, symp, &sip->sym_id) != 0 ||
902 symp->st_value == 0)
903 continue;
905 if (flags & MDB_TGT_SYM_EXACT) {
906 sym_km = km;
907 goto found;
911 * If this is the first match we've found, or if this symbol is
912 * closer to the specified address than the last one we found,
913 * use it.
915 if (sym_km == NULL || mdb_gelf_sym_closer(symp, &sym, addr)) {
916 sym_km = km;
917 sym = *symp;
918 symid = sip->sym_id;
923 * kmdb dmods are normal kernel modules, loaded by krtld as such. To
924 * avoid polluting modinfo, and to keep from confusing the module
925 * subsystem (many dmods have the same names as real kernel modules),
926 * kmdb keeps their modctls separate, and doesn't allow their loading
927 * to be broadcast via the krtld module load/unload mechanism. As a
928 * result, kmdb_kvm doesn't find out about them, and can't turn their
929 * addresses into symbols. This can be most inconvenient during
930 * debugger faults, as the dmod frames will show up without names.
931 * We weren't able to turn the requested address into a symbol, so we'll
932 * take a spin through the dmods, trying to match our address against
933 * their symbols.
935 if (sym_km == NULL) {
936 return (kmdb_module_lookup_by_addr(addr, flags, buf, nbytes,
937 symp, sip));
940 *symp = sym;
941 sip->sym_id = symid;
943 found:
945 * Once we've found something, copy the final name into the caller's
946 * buffer and prefix it with the load object name if appropriate.
948 name = mdb_gelf_sym_name(sym_km->km_symtab, symp);
950 if (sym_km == &prmod) {
951 if (buf != NULL) {
952 (void) strncpy(buf, name, nbytes);
953 buf[nbytes - 1] = '\0';
955 sip->sym_table = MDB_TGT_PRVSYM;
956 } else {
957 if (buf != NULL) {
958 if (sym_km->km_flags & KM_F_PRIMARY) {
959 (void) strncpy(buf, name, nbytes);
960 buf[nbytes - 1] = '\0';
961 } else {
962 (void) mdb_snprintf(buf, nbytes, "%s`%s",
963 sym_km->km_name, name);
966 sip->sym_table = MDB_TGT_SYMTAB;
969 return (0);
972 static int
973 kmt_lookup_by_name(mdb_tgt_t *t, const char *obj, const char *name,
974 GElf_Sym *symp, mdb_syminfo_t *sip)
976 kmt_data_t *kmt = t->t_data;
977 kmt_module_t *km;
978 mdb_var_t *v;
979 GElf_Sym sym;
980 uint_t symid;
981 int n;
983 if (!kmt->kmt_symavail)
984 return (set_errno(EMDB_NOSYM));
986 switch ((uintptr_t)obj) {
987 case (uintptr_t)MDB_TGT_OBJ_EXEC:
988 case (uintptr_t)MDB_TGT_OBJ_EVERY:
989 km = mdb_list_next(&kmt->kmt_modlist);
990 n = mdb_nv_size(&kmt->kmt_modules);
991 break;
993 case (uintptr_t)MDB_TGT_OBJ_RTLD:
994 obj = kmt->kmt_rtld_name;
995 /*FALLTHROUGH*/
997 default:
999 * If this is a request for a dmod symbol, let kmdb_module
1000 * handle it.
1002 if (obj != NULL && strncmp(obj, "DMOD`", 5) == 0) {
1003 return (kmdb_module_lookup_by_name(obj + 5, name,
1004 symp, sip));
1007 if ((v = mdb_nv_lookup(&kmt->kmt_modules, obj)) == NULL)
1008 return (set_errno(EMDB_NOOBJ));
1010 km = mdb_nv_get_cookie(v);
1011 n = 1;
1015 * kmdb's kvm target is at a bit of a disadvantage compared to mdb's
1016 * kvm target when it comes to global symbol lookups. mdb has ksyms,
1017 * which hides pesky things like symbols that are undefined in unix,
1018 * but which are defined in genunix. We don't have such a facility -
1019 * we simply iterate through the modules, looking for a given symbol
1020 * in each. Unless we're careful, we'll return the undef in the
1021 * aforementioned case.
1023 for (; n > 0; n--, km = mdb_list_next(km)) {
1024 if (mdb_gelf_symtab_lookup_by_name(km->km_symtab, name,
1025 &sym, &symid) == 0 && sym.st_shndx != SHN_UNDEF)
1026 break;
1029 if (n == 0)
1030 return (set_errno(EMDB_NOSYM));
1032 found:
1033 bcopy(&sym, symp, sizeof (GElf_Sym));
1034 sip->sym_id = symid;
1035 sip->sym_table = MDB_TGT_SYMTAB;
1037 return (0);
1040 static int
1041 kmt_symtab_func(void *data, const GElf_Sym *sym, const char *name, uint_t id)
1043 kmt_symarg_t *arg = data;
1045 if (mdb_tgt_sym_match(sym, arg->sym_type)) {
1046 arg->sym_info.sym_id = id;
1048 return (arg->sym_cb(arg->sym_data, sym, name, &arg->sym_info,
1049 arg->sym_obj));
1052 return (0);
1055 static void
1056 kmt_symtab_iter(mdb_gelf_symtab_t *gst, uint_t type, const char *obj,
1057 mdb_tgt_sym_f *cb, void *p)
1059 kmt_symarg_t arg;
1061 arg.sym_cb = cb;
1062 arg.sym_data = p;
1063 arg.sym_type = type;
1064 arg.sym_info.sym_table = gst->gst_tabid;
1065 arg.sym_obj = obj;
1067 mdb_gelf_symtab_iter(gst, kmt_symtab_func, &arg);
1070 static int
1071 kmt_symbol_iter(mdb_tgt_t *t, const char *obj, uint_t which, uint_t type,
1072 mdb_tgt_sym_f *cb, void *data)
1074 kmt_data_t *kmt = t->t_data;
1075 kmt_module_t *km;
1077 mdb_gelf_symtab_t *symtab = NULL;
1078 mdb_var_t *v;
1080 if (which == MDB_TGT_DYNSYM)
1081 return (set_errno(EMDB_TGTNOTSUP));
1083 switch ((uintptr_t)obj) {
1084 case (uintptr_t)MDB_TGT_OBJ_EXEC:
1085 case (uintptr_t)MDB_TGT_OBJ_EVERY:
1086 mdb_nv_rewind(&kmt->kmt_modules);
1087 while ((v = mdb_nv_advance(&kmt->kmt_modules)) != NULL) {
1088 km = mdb_nv_get_cookie(v);
1090 if (km->km_symtab != NULL) {
1091 kmt_symtab_iter(km->km_symtab, type,
1092 km->km_name, cb, data);
1095 return (0);
1097 case (uintptr_t)MDB_TGT_OBJ_RTLD:
1098 obj = kmt->kmt_rtld_name;
1099 /*FALLTHROUGH*/
1101 default:
1102 if (strncmp(obj, "DMOD`", 5) == 0) {
1103 return (kmdb_module_symbol_iter(obj + 5, type,
1104 cb, data));
1107 if ((v = mdb_nv_lookup(&kmt->kmt_modules, obj)) == NULL)
1108 return (set_errno(EMDB_NOOBJ));
1109 km = mdb_nv_get_cookie(v);
1111 symtab = km->km_symtab;
1114 if (symtab != NULL)
1115 kmt_symtab_iter(symtab, type, obj, cb, data);
1117 return (0);
1120 static int
1121 kmt_mapping_walk(uintptr_t addr, const void *data, kmt_maparg_t *marg)
1124 * This is a bit sketchy but avoids problematic compilation of this
1125 * target against the current VM implementation. Now that we have
1126 * vmem, we can make this less broken and more informative by changing
1127 * this code to invoke the vmem walker in the near future.
1129 const struct kmt_seg {
1130 caddr_t s_base;
1131 size_t s_size;
1132 } *segp = (const struct kmt_seg *)data;
1134 mdb_map_t map;
1135 GElf_Sym sym;
1136 mdb_syminfo_t info;
1138 map.map_base = (uintptr_t)segp->s_base;
1139 map.map_size = segp->s_size;
1140 map.map_flags = MDB_TGT_MAP_R | MDB_TGT_MAP_W | MDB_TGT_MAP_X;
1142 if (kmt_lookup_by_addr(marg->map_target, addr, MDB_TGT_SYM_EXACT,
1143 map.map_name, MDB_TGT_MAPSZ, &sym, &info) == -1) {
1145 (void) mdb_iob_snprintf(map.map_name, MDB_TGT_MAPSZ,
1146 "%lr", addr);
1149 return (marg->map_cb(marg->map_data, &map, map.map_name));
1152 static int
1153 kmt_mapping_iter(mdb_tgt_t *t, mdb_tgt_map_f *func, void *private)
1155 kmt_maparg_t m;
1156 uintptr_t kas;
1158 m.map_target = t;
1159 m.map_cb = func;
1160 m.map_data = private;
1162 if ((kas = kmt_read_kas(t)) == (uintptr_t)NULL)
1163 return (-1); /* errno is set for us */
1165 return (mdb_pwalk("seg", (mdb_walk_cb_t)kmt_mapping_walk, &m, kas));
1168 static const mdb_map_t *
1169 kmt_mod_to_map(kmt_module_t *km, mdb_map_t *map)
1171 (void) strncpy(map->map_name, km->km_name, MDB_TGT_MAPSZ);
1172 map->map_name[MDB_TGT_MAPSZ - 1] = '\0';
1173 map->map_base = km->km_text_va;
1174 map->map_size = km->km_text_size;
1175 map->map_flags = MDB_TGT_MAP_R | MDB_TGT_MAP_W | MDB_TGT_MAP_X;
1177 return (map);
1180 static int
1181 kmt_object_iter(mdb_tgt_t *t, mdb_tgt_map_f *func, void *private)
1183 kmt_data_t *kmt = t->t_data;
1184 kmt_module_t *km;
1185 mdb_map_t m;
1187 for (km = mdb_list_next(&kmt->kmt_modlist); km != NULL;
1188 km = mdb_list_next(km)) {
1189 if (func(private, kmt_mod_to_map(km, &m), km->km_name) == -1)
1190 break;
1193 return (0);
1196 static const mdb_map_t *
1197 kmt_addr_to_map(mdb_tgt_t *t, uintptr_t addr)
1199 kmt_data_t *kmt = t->t_data;
1200 kmt_module_t *km;
1202 for (km = mdb_list_next(&kmt->kmt_modlist); km != NULL;
1203 km = mdb_list_next(km)) {
1204 if (addr - km->km_text_va < km->km_text_size ||
1205 addr - km->km_data_va < km->km_data_size ||
1206 addr - km->km_bss_va < km->km_bss_size)
1207 return (kmt_mod_to_map(km, &kmt->kmt_map));
1210 (void) set_errno(EMDB_NOMAP);
1211 return (NULL);
1214 static kmt_module_t *
1215 kmt_module_by_name(kmt_data_t *kmt, const char *name)
1217 kmt_module_t *km;
1219 for (km = mdb_list_next(&kmt->kmt_modlist); km != NULL;
1220 km = mdb_list_next(km)) {
1221 if (strcmp(name, km->km_name) == 0)
1222 return (km);
1225 return (NULL);
1228 static const mdb_map_t *
1229 kmt_name_to_map(mdb_tgt_t *t, const char *name)
1231 kmt_data_t *kmt = t->t_data;
1232 kmt_module_t *km;
1233 mdb_map_t m;
1236 * If name is MDB_TGT_OBJ_EXEC, return the first module on the list,
1237 * which will be unix since we keep kmt_modlist in load order.
1239 if (name == MDB_TGT_OBJ_EXEC) {
1240 return (kmt_mod_to_map(mdb_list_next(&kmt->kmt_modlist),
1241 &m));
1244 if (name == MDB_TGT_OBJ_RTLD)
1245 name = kmt->kmt_rtld_name;
1247 if ((km = kmt_module_by_name(kmt, name)) != NULL)
1248 return (kmt_mod_to_map(km, &m));
1250 (void) set_errno(EMDB_NOOBJ);
1251 return (NULL);
1254 static ctf_file_t *
1255 kmt_load_ctfdata(mdb_tgt_t *t, kmt_module_t *km)
1257 kmt_data_t *kmt = t->t_data;
1258 int err;
1260 if (km->km_ctfp != NULL)
1261 return (km->km_ctfp);
1263 if (km->km_ctf_va == NULL || km->km_symtab == NULL) {
1264 (void) set_errno(EMDB_NOCTF);
1265 return (NULL);
1268 if ((km->km_ctfp = mdb_ctf_bufopen(km->km_ctf_va, km->km_ctf_size,
1269 km->km_symtab_va, &km->km_symtab_hdr, km->km_strtab_va,
1270 &km->km_strtab_hdr, &err)) == NULL) {
1271 (void) set_errno(ctf_to_errno(err));
1272 return (NULL);
1275 mdb_dprintf(MDB_DBG_KMOD, "loaded %lu bytes of CTF data for %s\n",
1276 (ulong_t)km->km_ctf_size, km->km_name);
1278 if (ctf_parent_name(km->km_ctfp) != NULL) {
1279 mdb_var_t *v;
1281 if ((v = mdb_nv_lookup(&kmt->kmt_modules,
1282 ctf_parent_name(km->km_ctfp))) != NULL) {
1283 kmt_module_t *pm = mdb_nv_get_cookie(v);
1285 if (pm->km_ctfp == NULL)
1286 (void) kmt_load_ctfdata(t, pm);
1288 if (pm->km_ctfp != NULL && ctf_import(km->km_ctfp,
1289 pm->km_ctfp) == CTF_ERR) {
1290 warn("failed to import parent types into "
1291 "%s: %s\n", km->km_name,
1292 ctf_errmsg(ctf_errno(km->km_ctfp)));
1294 } else {
1295 warn("failed to load CTF data for %s - parent %s not "
1296 "loaded\n", km->km_name,
1297 ctf_parent_name(km->km_ctfp));
1301 return (km->km_ctfp);
1304 ctf_file_t *
1305 kmt_addr_to_ctf(mdb_tgt_t *t, uintptr_t addr)
1307 kmt_data_t *kmt = t->t_data;
1308 kmt_module_t *km;
1310 for (km = mdb_list_next(&kmt->kmt_modlist); km != NULL;
1311 km = mdb_list_next(km)) {
1312 if (addr - km->km_text_va < km->km_text_size ||
1313 addr - km->km_data_va < km->km_data_size ||
1314 addr - km->km_bss_va < km->km_bss_size)
1315 return (kmt_load_ctfdata(t, km));
1318 return (kmdb_module_addr_to_ctf(addr));
1321 ctf_file_t *
1322 kmt_name_to_ctf(mdb_tgt_t *t, const char *name)
1324 kmt_data_t *kt = t->t_data;
1325 kmt_module_t *km;
1327 if (name == MDB_TGT_OBJ_EXEC) {
1328 name = KMT_CTFPARENT;
1329 } else if (name == MDB_TGT_OBJ_RTLD) {
1330 name = kt->kmt_rtld_name;
1331 } else if (strncmp(name, "DMOD`", 5) == 0) {
1332 /* Request for CTF data for a DMOD symbol */
1333 return (kmdb_module_name_to_ctf(name + 5));
1336 if ((km = kmt_module_by_name(kt, name)) != NULL)
1337 return (kmt_load_ctfdata(t, km));
1339 (void) set_errno(EMDB_NOOBJ);
1340 return (NULL);
1343 /*ARGSUSED*/
1344 static int
1345 kmt_status(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
1347 int state;
1349 bzero(tsp, sizeof (mdb_tgt_status_t));
1351 switch ((state = kmdb_dpi_get_state(NULL))) {
1352 case DPI_STATE_INIT:
1353 tsp->st_state = MDB_TGT_RUNNING;
1354 tsp->st_pc = 0;
1355 break;
1357 case DPI_STATE_STOPPED:
1358 tsp->st_state = MDB_TGT_STOPPED;
1360 (void) kmdb_dpi_get_register("pc", &tsp->st_pc);
1361 break;
1363 case DPI_STATE_FAULTED:
1364 tsp->st_state = MDB_TGT_STOPPED;
1366 (void) kmdb_dpi_get_register("pc", &tsp->st_pc);
1368 tsp->st_flags |= MDB_TGT_ISTOP;
1369 break;
1371 case DPI_STATE_LOST:
1372 tsp->st_state = MDB_TGT_LOST;
1374 (void) kmdb_dpi_get_register("pc", &tsp->st_pc);
1375 break;
1378 mdb_dprintf(MDB_DBG_KMOD, "kmt_status, dpi: %d tsp: %d, pc = %p %A\n",
1379 state, tsp->st_state, (void *)tsp->st_pc, tsp->st_pc);
1381 return (0);
1385 * Invoked when kmt_defbp_enter_debugger is called, this routine activates and
1386 * deactivates deferred breakpoints in response to module load and unload
1387 * events.
1389 /*ARGSUSED*/
1390 static void
1391 kmt_defbp_event(mdb_tgt_t *t, int vid, void *private)
1393 if (kmt_defbp_modchg_isload) {
1394 if (!mdb_tgt_sespec_activate_all(t) &&
1395 (mdb.m_flags & MDB_FL_BPTNOSYMSTOP)) {
1397 * We weren't able to activate the breakpoints.
1398 * If so requested, we'll return without calling
1399 * continue, thus throwing the user into the debugger.
1401 return;
1404 } else {
1405 mdb_sespec_t *sep, *nsep;
1406 const mdb_map_t *map, *bpmap;
1407 mdb_map_t modmap;
1409 if ((map = kmt_addr_to_map(t,
1410 (uintptr_t)kmt_defbp_modchg_modctl->mod_text)) == NULL) {
1411 warn("module unload notification for unknown module %s",
1412 kmt_defbp_modchg_modctl->mod_modname);
1413 return; /* drop into the debugger */
1416 bcopy(map, &modmap, sizeof (mdb_map_t));
1418 for (sep = mdb_list_next(&t->t_active); sep; sep = nsep) {
1419 nsep = mdb_list_next(sep);
1421 if (sep->se_ops == &kmt_brkpt_ops) {
1422 kmt_brkpt_t *kb = sep->se_data;
1424 if ((bpmap = kmt_addr_to_map(t,
1425 kb->kb_addr)) == NULL ||
1426 (bpmap->map_base == modmap.map_base &&
1427 bpmap->map_size == modmap.map_size)) {
1428 mdb_tgt_sespec_idle_one(t, sep,
1429 EMDB_NOMAP);
1435 (void) mdb_tgt_continue(t, NULL);
1438 static void
1439 kmt_defbp_enter_debugger(void)
1442 * The debugger places a breakpoint here. We can't have a simple
1443 * nop function here, because GCC knows much more than we do, and
1444 * will optimize away the call to it.
1446 (void) get_fp();
1450 * This routine is called while the kernel is running. It attempts to determine
1451 * whether any deferred breakpoints exist for the module being changed (loaded
1452 * or unloaded). If any such breakpoints exist, the debugger will be entered to
1453 * process them.
1455 static void
1456 kmt_defbp_modchg(struct modctl *mctl, int isload)
1458 kmt_defbp_t *dbp;
1460 kmt_defbp_lock = 1;
1462 for (dbp = mdb_list_next(&kmt_defbp_list); dbp;
1463 dbp = mdb_list_next(dbp)) {
1464 if (!dbp->dbp_ref)
1465 continue;
1467 if (strcmp(mctl->mod_modname, dbp->dbp_objname) == 0) {
1469 * Activate the breakpoint
1471 kmt_defbp_modchg_isload = isload;
1472 kmt_defbp_modchg_modctl = mctl;
1474 kmt_defbp_enter_debugger();
1475 break;
1479 kmt_defbp_lock = 0;
1482 /*ARGSUSED*/
1483 static int
1484 kmt_continue(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
1486 int n;
1488 kmdb_dpi_resume();
1491 * The order of the following two calls is important. If there are
1492 * load acks on the work queue, we'll initialize the dmods they
1493 * represent. This will involve a call to _mdb_init, which may very
1494 * well result in a symbol lookup. If we haven't resynced our view
1495 * of symbols with the current state of the world, this lookup could
1496 * end very badly. We therefore make sure to sync before processing
1497 * the work queue.
1499 kmt_sync(t);
1500 kmdb_dpi_process_work_queue();
1502 if (kmdb_kdi_get_unload_request())
1503 t->t_flags |= MDB_TGT_F_UNLOAD;
1505 (void) mdb_tgt_status(t, &t->t_status);
1507 if ((n = kmt_dmod_status(NULL, KMDB_MC_STATE_LOADING) +
1508 kmt_dmod_status(NULL, KMDB_MC_STATE_UNLOADING)) != 0) {
1509 mdb_warn("%d dmod load%c/unload%c pending\n", n,
1510 "s"[n == 1], "s"[n == 1]);
1513 return (0);
1516 /*ARGSUSED*/
1517 static int
1518 kmt_step(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
1520 int rc;
1522 if ((rc = kmdb_dpi_step()) == 0)
1523 (void) mdb_tgt_status(t, &t->t_status);
1525 return (rc);
1528 static int
1529 kmt_defbp_activate(mdb_tgt_t *t)
1531 kmdb_dpi_modchg_register(kmt_defbp_modchg);
1534 * The routines that add and arm breakpoints will check for the proper
1535 * DTrace state, but they'll just put this breakpoint on the idle list
1536 * if DTrace is active. It'll correctly move to the active list when
1537 * DTrace deactivates, but that's insufficient for our purposes -- we
1538 * need to do extra processing at that point. We won't get to do said
1539 * processing with with a normal idle->active transition, so we just
1540 * won't add it add it until we're sure that it'll stick.
1543 if (kmdb_kdi_dtrace_get_state() == KDI_DTSTATE_DTRACE_ACTIVE)
1544 return (set_errno(EMDB_DTACTIVE));
1546 kmt_defbp_bpspec = mdb_tgt_add_vbrkpt(t,
1547 (uintptr_t)kmt_defbp_enter_debugger,
1548 MDB_TGT_SPEC_HIDDEN, kmt_defbp_event, NULL);
1550 return (0);
1553 static void
1554 kmt_defbp_deactivate(mdb_tgt_t *t)
1556 kmdb_dpi_modchg_cancel();
1558 if (kmt_defbp_bpspec != 0) {
1559 if (t != NULL)
1560 (void) mdb_tgt_vespec_delete(t, kmt_defbp_bpspec);
1562 kmt_defbp_bpspec = 0;
1566 static kmt_defbp_t *
1567 kmt_defbp_create(mdb_tgt_t *t, const char *objname, const char *symname)
1569 kmt_defbp_t *dbp = mdb_alloc(sizeof (kmt_defbp_t), UM_SLEEP);
1571 mdb_dprintf(MDB_DBG_KMOD, "defbp_create %s`%s\n", objname, symname);
1573 dbp->dbp_objname = strdup(objname);
1574 dbp->dbp_symname = strdup(symname);
1575 dbp->dbp_ref = 1;
1577 kmt_defbp_num++;
1579 if (kmt_defbp_num == 1 || kmt_defbp_bpspec == 0) {
1580 if (kmt_defbp_activate(t) < 0)
1581 warn("failed to activate deferred breakpoints");
1584 mdb_list_append(&kmt_defbp_list, dbp);
1586 return (dbp);
1589 static void
1590 kmt_defbp_destroy(kmt_defbp_t *dbp)
1592 mdb_dprintf(MDB_DBG_KMOD, "defbp_destroy %s`%s\n", dbp->dbp_objname,
1593 dbp->dbp_symname);
1595 mdb_list_delete(&kmt_defbp_list, dbp);
1597 strfree(dbp->dbp_objname);
1598 strfree(dbp->dbp_symname);
1599 mdb_free(dbp, sizeof (kmt_defbp_t));
1602 static void
1603 kmt_defbp_prune_common(int all)
1605 kmt_defbp_t *dbp, *ndbp;
1607 /* We can't remove items from the list while the driver is using it. */
1608 if (kmt_defbp_lock)
1609 return;
1611 for (dbp = mdb_list_next(&kmt_defbp_list); dbp != NULL; dbp = ndbp) {
1612 ndbp = mdb_list_next(dbp);
1614 if (!all && dbp->dbp_ref)
1615 continue;
1617 kmt_defbp_destroy(dbp);
1621 static void
1622 kmt_defbp_prune(void)
1624 kmt_defbp_prune_common(0);
1627 static void
1628 kmt_defbp_destroy_all(void)
1630 kmt_defbp_prune_common(1);
1633 static void
1634 kmt_defbp_delete(mdb_tgt_t *t, kmt_defbp_t *dbp)
1636 dbp->dbp_ref = 0;
1638 ASSERT(kmt_defbp_num > 0);
1639 kmt_defbp_num--;
1641 if (kmt_defbp_num == 0)
1642 kmt_defbp_deactivate(t);
1644 kmt_defbp_prune();
1647 static int
1648 kmt_brkpt_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
1650 mdb_tgt_status_t tsp;
1651 kmt_bparg_t *ka = args;
1652 kmt_brkpt_t *kb;
1653 GElf_Sym s;
1654 mdb_instr_t instr;
1656 (void) mdb_tgt_status(t, &tsp);
1657 if (tsp.st_state != MDB_TGT_RUNNING && tsp.st_state != MDB_TGT_STOPPED)
1658 return (set_errno(EMDB_NOPROC));
1660 if (ka->ka_symbol != NULL) {
1661 if (mdb_tgt_lookup_by_scope(t, ka->ka_symbol, &s, NULL) == -1) {
1662 if (errno != EMDB_NOOBJ && !(errno == EMDB_NOSYM &&
1663 !(mdb.m_flags & MDB_FL_BPTNOSYMSTOP))) {
1664 warn("breakpoint %s activation failed",
1665 ka->ka_symbol);
1667 return (-1); /* errno is set for us */
1670 ka->ka_addr = (uintptr_t)s.st_value;
1674 if (mdb_vread(&instr, sizeof (instr), ka->ka_addr) != sizeof (instr))
1675 return (-1); /* errno is set for us */
1677 if (kmdb_kdi_dtrace_get_state() == KDI_DTSTATE_DTRACE_ACTIVE)
1678 warn("breakpoint will not arm until DTrace is inactive\n");
1680 kb = mdb_zalloc(sizeof (kmt_brkpt_t), UM_SLEEP);
1681 kb->kb_addr = ka->ka_addr;
1682 sep->se_data = kb;
1684 return (0);
1687 /*ARGSUSED*/
1688 static void
1689 kmt_brkpt_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
1691 mdb_free(sep->se_data, sizeof (kmt_brkpt_t));
1694 /*ARGSUSED*/
1695 static char *
1696 kmt_brkpt_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
1697 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
1699 uintptr_t addr = (uintptr_t)NULL;
1702 if (vep != NULL) {
1703 kmt_bparg_t *ka = vep->ve_args;
1705 if (ka->ka_symbol != NULL) {
1706 (void) mdb_iob_snprintf(buf, nbytes, "stop at %s",
1707 ka->ka_symbol);
1708 } else {
1709 (void) mdb_iob_snprintf(buf, nbytes, "stop at %a",
1710 ka->ka_addr);
1711 addr = ka->ka_addr;
1714 } else {
1715 addr = ((kmt_brkpt_t *)sep->se_data)->kb_addr;
1716 (void) mdb_iob_snprintf(buf, nbytes, "stop at %a", addr);
1719 sp->spec_base = addr;
1720 sp->spec_size = sizeof (mdb_instr_t);
1722 return (buf);
1725 static int
1726 kmt_brkpt_secmp(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
1728 kmt_brkpt_t *kb = sep->se_data;
1729 kmt_bparg_t *ka = args;
1730 GElf_Sym sym;
1732 if (ka->ka_symbol != NULL) {
1733 return (mdb_tgt_lookup_by_scope(t, ka->ka_symbol,
1734 &sym, NULL) == 0 && sym.st_value == kb->kb_addr);
1737 return (ka->ka_addr == kb->kb_addr);
1740 /*ARGSUSED*/
1741 static int
1742 kmt_brkpt_vecmp(mdb_tgt_t *t, mdb_vespec_t *vep, void *args)
1744 kmt_bparg_t *ka1 = vep->ve_args;
1745 kmt_bparg_t *ka2 = args;
1747 if (ka1->ka_symbol != NULL && ka2->ka_symbol != NULL)
1748 return (strcmp(ka1->ka_symbol, ka2->ka_symbol) == 0);
1750 if (ka1->ka_symbol == NULL && ka2->ka_symbol == NULL)
1751 return (ka1->ka_addr == ka2->ka_addr);
1753 return (0); /* fail if one is symbolic, other is an explicit address */
1756 static int
1757 kmt_brkpt_arm(mdb_tgt_t *t, mdb_sespec_t *sep)
1759 kmt_data_t *kmt = t->t_data;
1760 kmt_brkpt_t *kb = sep->se_data;
1761 int rv;
1763 if (kmdb_kdi_dtrace_get_state() == KDI_DTSTATE_DTRACE_ACTIVE)
1764 return (set_errno(EMDB_DTACTIVE));
1766 if ((rv = kmdb_dpi_brkpt_arm(kb->kb_addr, &kb->kb_oinstr)) != 0)
1767 return (rv);
1769 if (kmt->kmt_narmedbpts++ == 0)
1770 (void) kmdb_kdi_dtrace_set(KDI_DTSET_KMDB_BPT_ACTIVATE);
1772 return (0);
1775 static int
1776 kmt_brkpt_disarm(mdb_tgt_t *t, mdb_sespec_t *sep)
1778 kmt_data_t *kmt = t->t_data;
1779 kmt_brkpt_t *kb = sep->se_data;
1780 int rv;
1782 ASSERT(kmdb_kdi_dtrace_get_state() == KDI_DTSTATE_KMDB_BPT_ACTIVE);
1784 if ((rv = kmdb_dpi_brkpt_disarm(kb->kb_addr, kb->kb_oinstr)) != 0)
1785 return (rv);
1787 if (--kmt->kmt_narmedbpts == 0)
1788 (void) kmdb_kdi_dtrace_set(KDI_DTSET_KMDB_BPT_DEACTIVATE);
1790 return (0);
1794 * Determine whether the specified sespec is an armed watchpoint that overlaps
1795 * with the given breakpoint and has the given flags set. We use this to find
1796 * conflicts with breakpoints, below.
1798 static int
1799 kmt_wp_overlap(mdb_sespec_t *sep, kmt_brkpt_t *kb, int flags)
1801 const kmdb_wapt_t *wp = sep->se_data;
1803 return (sep->se_state == MDB_TGT_SPEC_ARMED &&
1804 sep->se_ops == &kmt_wapt_ops && (wp->wp_wflags & flags) &&
1805 kb->kb_addr - wp->wp_addr < wp->wp_size);
1809 * We step over breakpoints using our single-stepper. If a conflicting
1810 * watchpoint is present, we must temporarily remove it before stepping over the
1811 * breakpoint so we don't immediately re-trigger the watchpoint. We know the
1812 * watchpoint has already triggered on our trap instruction as part of fetching
1813 * it. Before we return, we must re-install any disabled watchpoints.
1815 static int
1816 kmt_brkpt_cont(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
1818 kmt_brkpt_t *kb = sep->se_data;
1819 int status = -1;
1820 int error;
1822 for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
1823 if (kmt_wp_overlap(sep, kb, MDB_TGT_WA_X))
1824 (void) kmdb_dpi_wapt_disarm(sep->se_data);
1827 if (kmdb_dpi_brkpt_disarm(kb->kb_addr, kb->kb_oinstr) == 0 &&
1828 kmt_step(t, tsp) == 0)
1829 status = kmt_status(t, tsp);
1831 error = errno; /* save errno from disarm, step, or status */
1833 for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
1834 if (kmt_wp_overlap(sep, kb, MDB_TGT_WA_X))
1835 kmdb_dpi_wapt_arm(sep->se_data);
1838 (void) set_errno(error);
1839 return (status);
1842 /*ARGSUSED*/
1843 static int
1844 kmt_brkpt_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
1846 kmt_brkpt_t *kb = sep->se_data;
1847 int state, why;
1848 kreg_t pc;
1850 state = kmdb_dpi_get_state(&why);
1851 (void) kmdb_dpi_get_register("pc", &pc);
1853 return (state == DPI_STATE_FAULTED && why == DPI_STATE_WHY_BKPT &&
1854 pc == kb->kb_addr);
1857 static const mdb_se_ops_t kmt_brkpt_ops = {
1858 kmt_brkpt_ctor, /* se_ctor */
1859 kmt_brkpt_dtor, /* se_dtor */
1860 kmt_brkpt_info, /* se_info */
1861 kmt_brkpt_secmp, /* se_secmp */
1862 kmt_brkpt_vecmp, /* se_vecmp */
1863 kmt_brkpt_arm, /* se_arm */
1864 kmt_brkpt_disarm, /* se_disarm */
1865 kmt_brkpt_cont, /* se_cont */
1866 kmt_brkpt_match /* se_match */
1869 static int
1870 kmt_wapt_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
1872 mdb_tgt_status_t tsp;
1873 kmdb_wapt_t *vwp = args;
1874 kmdb_wapt_t *swp;
1876 (void) mdb_tgt_status(t, &tsp);
1877 if (tsp.st_state != MDB_TGT_RUNNING && tsp.st_state != MDB_TGT_STOPPED)
1878 return (set_errno(EMDB_NOPROC));
1880 swp = mdb_alloc(sizeof (kmdb_wapt_t), UM_SLEEP);
1881 bcopy(vwp, swp, sizeof (kmdb_wapt_t));
1883 if (kmdb_dpi_wapt_reserve(swp) < 0) {
1884 mdb_free(swp, sizeof (kmdb_wapt_t));
1885 return (-1); /* errno is set for us */
1888 sep->se_data = swp;
1890 return (0);
1893 /*ARGSUSED*/
1894 static void
1895 kmt_wapt_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
1897 kmdb_wapt_t *wp = sep->se_data;
1899 kmdb_dpi_wapt_release(wp);
1900 mdb_free(wp, sizeof (kmdb_wapt_t));
1903 /*ARGSUSED*/
1904 static char *
1905 kmt_wapt_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
1906 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
1908 kmdb_wapt_t *wp = vep != NULL ? vep->ve_args : sep->se_data;
1909 const char *fmt;
1910 char desc[24];
1912 ASSERT(wp->wp_wflags != 0);
1913 desc[0] = '\0';
1915 switch (wp->wp_wflags) {
1916 case MDB_TGT_WA_R:
1917 (void) strcat(desc, "/read");
1918 break;
1919 case MDB_TGT_WA_W:
1920 (void) strcat(desc, "/write");
1921 break;
1922 case MDB_TGT_WA_X:
1923 (void) strcat(desc, "/exec");
1924 break;
1925 default:
1926 if (wp->wp_wflags & MDB_TGT_WA_R)
1927 (void) strcat(desc, "/r");
1928 if (wp->wp_wflags & MDB_TGT_WA_W)
1929 (void) strcat(desc, "/w");
1930 if (wp->wp_wflags & MDB_TGT_WA_X)
1931 (void) strcat(desc, "/x");
1934 switch (wp->wp_type) {
1935 case DPI_WAPT_TYPE_PHYS:
1936 fmt = "stop on %s of phys [%p, %p)";
1937 break;
1939 case DPI_WAPT_TYPE_VIRT:
1940 fmt = "stop on %s of [%la, %la)";
1941 break;
1943 case DPI_WAPT_TYPE_IO:
1944 if (wp->wp_size == 1)
1945 fmt = "stop on %s of I/O port %p";
1946 else
1947 fmt = "stop on %s of I/O port [%p, %p)";
1948 break;
1951 (void) mdb_iob_snprintf(buf, nbytes, fmt, desc + 1, wp->wp_addr,
1952 wp->wp_addr + wp->wp_size);
1954 sp->spec_base = wp->wp_addr;
1955 sp->spec_size = wp->wp_size;
1957 return (buf);
1960 /*ARGSUSED*/
1961 static int
1962 kmt_wapt_secmp(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
1964 kmdb_wapt_t *wp1 = sep->se_data;
1965 kmdb_wapt_t *wp2 = args;
1967 return (wp1->wp_addr == wp2->wp_addr && wp1->wp_size == wp2->wp_size &&
1968 wp1->wp_wflags == wp2->wp_wflags);
1971 /*ARGSUSED*/
1972 static int
1973 kmt_wapt_vecmp(mdb_tgt_t *t, mdb_vespec_t *vep, void *args)
1975 kmdb_wapt_t *wp1 = vep->ve_args;
1976 kmdb_wapt_t *wp2 = args;
1978 return (wp1->wp_addr == wp2->wp_addr && wp1->wp_size == wp2->wp_size &&
1979 wp1->wp_wflags == wp2->wp_wflags);
1982 /*ARGSUSED*/
1983 static int
1984 kmt_wapt_arm(mdb_tgt_t *t, mdb_sespec_t *sep)
1986 kmdb_dpi_wapt_arm(sep->se_data);
1988 return (0);
1991 /*ARGSUSED*/
1992 static int
1993 kmt_wapt_disarm(mdb_tgt_t *t, mdb_sespec_t *sep)
1995 kmdb_dpi_wapt_disarm(sep->se_data);
1997 return (0);
2001 * Determine whether the specified sespec is an armed breakpoint at the given
2002 * %pc. We use this to find conflicts with watchpoints below.
2004 static int
2005 kmt_bp_overlap(mdb_sespec_t *sep, uintptr_t pc)
2007 kmt_brkpt_t *kb = sep->se_data;
2009 return (sep->se_state == MDB_TGT_SPEC_ARMED &&
2010 sep->se_ops == &kmt_brkpt_ops && kb->kb_addr == pc);
2014 * We step over watchpoints using our single-stepper. If a conflicting
2015 * breakpoint is present, we must temporarily disarm it before stepping over
2016 * the watchpoint so we do not immediately re-trigger the breakpoint. This is
2017 * similar to the case handled in kmt_brkpt_cont(), above.
2019 static int
2020 kmt_wapt_cont(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
2022 mdb_sespec_t *bep = NULL;
2023 int status = -1;
2024 int error, why;
2027 * If we stopped for anything other than a watchpoint, check to see
2028 * if there's a breakpoint here.
2030 if (!(kmdb_dpi_get_state(&why) == DPI_STATE_FAULTED &&
2031 (why == DPI_STATE_WHY_V_WAPT || why == DPI_STATE_WHY_P_WAPT))) {
2032 kreg_t pc;
2034 (void) kmdb_dpi_get_register("pc", &pc);
2036 for (bep = mdb_list_next(&t->t_active); bep != NULL;
2037 bep = mdb_list_next(bep)) {
2038 if (kmt_bp_overlap(bep, pc)) {
2039 (void) bep->se_ops->se_disarm(t, bep);
2040 bep->se_state = MDB_TGT_SPEC_ACTIVE;
2041 break;
2046 kmdb_dpi_wapt_disarm(sep->se_data);
2047 if (kmt_step(t, tsp) == 0)
2048 status = kmt_status(t, tsp);
2050 error = errno; /* save errno from step or status */
2052 if (bep != NULL)
2053 mdb_tgt_sespec_arm_one(t, bep);
2055 (void) set_errno(error);
2056 return (status);
2059 /*ARGSUSED*/
2060 static int
2061 kmt_wapt_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
2063 return (kmdb_dpi_wapt_match(sep->se_data));
2066 static const mdb_se_ops_t kmt_wapt_ops = {
2067 kmt_wapt_ctor, /* se_ctor */
2068 kmt_wapt_dtor, /* se_dtor */
2069 kmt_wapt_info, /* se_info */
2070 kmt_wapt_secmp, /* se_secmp */
2071 kmt_wapt_vecmp, /* se_vecmp */
2072 kmt_wapt_arm, /* se_arm */
2073 kmt_wapt_disarm, /* se_disarm */
2074 kmt_wapt_cont, /* se_cont */
2075 kmt_wapt_match /* se_match */
2078 /*ARGSUSED*/
2079 static int
2080 kmt_trap_ctor(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
2082 sep->se_data = args; /* trap number */
2084 return (0);
2087 /*ARGSUSED*/
2088 static char *
2089 kmt_trap_info(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_vespec_t *vep,
2090 mdb_tgt_spec_desc_t *sp, char *buf, size_t nbytes)
2092 const char *name;
2093 int trapnum;
2095 if (vep != NULL)
2096 trapnum = (intptr_t)vep->ve_args;
2097 else
2098 trapnum = (intptr_t)sep->se_data;
2100 if (trapnum == KMT_TRAP_ALL)
2101 name = "any trap";
2102 else if (trapnum == KMT_TRAP_NOTENUM)
2103 name = "miscellaneous trap";
2104 else
2105 name = kmt_trapname(trapnum);
2107 (void) mdb_iob_snprintf(buf, nbytes, "single-step stop on %s", name);
2109 return (buf);
2112 /*ARGSUSED2*/
2113 static int
2114 kmt_trap_match(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
2116 int spectt = (intptr_t)sep->se_data;
2117 kmt_data_t *kmt = t->t_data;
2118 kreg_t tt;
2120 (void) kmdb_dpi_get_register("tt", &tt);
2122 switch (spectt) {
2123 case KMT_TRAP_ALL:
2124 return (1);
2125 case KMT_TRAP_NOTENUM:
2126 return (tt > kmt->kmt_trapmax ||
2127 !BT_TEST(kmt->kmt_trapmap, tt));
2128 default:
2129 return (tt == spectt);
2133 static const mdb_se_ops_t kmt_trap_ops = {
2134 kmt_trap_ctor, /* se_ctor */
2135 no_se_dtor, /* se_dtor */
2136 kmt_trap_info, /* se_info */
2137 no_se_secmp, /* se_secmp */
2138 no_se_vecmp, /* se_vecmp */
2139 no_se_arm, /* se_arm */
2140 no_se_disarm, /* se_disarm */
2141 no_se_cont, /* se_cont */
2142 kmt_trap_match /* se_match */
2145 static void
2146 kmt_bparg_dtor(mdb_vespec_t *vep)
2148 kmt_bparg_t *ka = vep->ve_args;
2150 if (ka->ka_symbol != NULL)
2151 strfree(ka->ka_symbol);
2153 if (ka->ka_defbp != NULL)
2154 kmt_defbp_delete(mdb.m_target, ka->ka_defbp);
2156 mdb_free(ka, sizeof (kmt_bparg_t));
2159 static int
2160 kmt_add_vbrkpt(mdb_tgt_t *t, uintptr_t addr,
2161 int spec_flags, mdb_tgt_se_f *func, void *data)
2163 kmt_bparg_t *ka = mdb_alloc(sizeof (kmt_bparg_t), UM_SLEEP);
2165 ka->ka_addr = addr;
2166 ka->ka_symbol = NULL;
2167 ka->ka_defbp = NULL;
2169 return (mdb_tgt_vespec_insert(t, &kmt_brkpt_ops, spec_flags,
2170 func, data, ka, kmt_bparg_dtor));
2173 static int
2174 kmt_add_sbrkpt(mdb_tgt_t *t, const char *fullname,
2175 int spec_flags, mdb_tgt_se_f *func, void *data)
2177 kmt_bparg_t *ka;
2178 kmt_defbp_t *dbp;
2179 GElf_Sym sym;
2180 char *tick, *objname, *symname;
2181 int serrno;
2183 if ((tick = strchr(fullname, '`')) == fullname) {
2184 (void) set_errno(EMDB_NOOBJ);
2185 return (0);
2189 * Deferred breakpoints are always scoped. If we didn't find a tick,
2190 * there's no scope. We'll create a vbrkpt, but only if we can turn the
2191 * provided string into an address.
2193 if (tick == NULL) {
2194 uintptr_t addr;
2196 if (strisbasenum(fullname)) {
2197 addr = mdb_strtoull(fullname); /* a bare address */
2198 } else if (mdb_tgt_lookup_by_name(t, MDB_TGT_OBJ_EVERY,
2199 fullname, &sym, NULL) < 0) {
2200 (void) set_errno(EMDB_NOSYM);
2201 return (0);
2202 } else {
2203 addr = (uintptr_t)sym.st_value; /* unscoped sym name */
2206 return (kmt_add_vbrkpt(t, addr, spec_flags, func, data));
2209 if (*(tick + 1) == '\0') {
2210 (void) set_errno(EMDB_NOSYM);
2211 return (0);
2214 objname = strndup(fullname, tick - fullname);
2215 symname = tick + 1;
2217 if (mdb_tgt_lookup_by_name(t, objname, symname, NULL, NULL) < 0 &&
2218 errno != EMDB_NOOBJ) {
2219 serrno = errno;
2220 strfree(objname);
2222 (void) set_errno(serrno);
2223 return (0); /* errno is set for us */
2226 dbp = kmt_defbp_create(t, objname, symname);
2227 strfree(objname);
2229 ka = mdb_alloc(sizeof (kmt_bparg_t), UM_SLEEP);
2230 ka->ka_symbol = strdup(fullname);
2231 ka->ka_addr = (uintptr_t)NULL;
2232 ka->ka_defbp = dbp;
2234 return (mdb_tgt_vespec_insert(t, &kmt_brkpt_ops, spec_flags,
2235 func, data, ka, kmt_bparg_dtor));
2238 static int
2239 kmt_wparg_overlap(const kmdb_wapt_t *wp1, const kmdb_wapt_t *wp2)
2241 /* Assume the watchpoint spaces don't overlap */
2242 if (wp1->wp_type != wp2->wp_type)
2243 return (0);
2245 if (wp2->wp_addr + wp2->wp_size <= wp1->wp_addr)
2246 return (0); /* no range overlap */
2248 if (wp1->wp_addr + wp1->wp_size <= wp2->wp_addr)
2249 return (0); /* no range overlap */
2251 return (wp1->wp_addr != wp2->wp_addr || wp1->wp_size != wp2->wp_size ||
2252 wp1->wp_wflags != wp2->wp_wflags);
2255 static void
2256 kmt_wparg_dtor(mdb_vespec_t *vep)
2258 mdb_free(vep->ve_args, sizeof (kmdb_wapt_t));
2261 static int
2262 kmt_add_wapt_common(mdb_tgt_t *t, uintptr_t addr, size_t len, uint_t wflags,
2263 int spec_flags, mdb_tgt_se_f *func, void *data, int type)
2265 kmdb_wapt_t *wp = mdb_alloc(sizeof (kmdb_wapt_t), UM_SLEEP);
2266 mdb_sespec_t *sep;
2268 wp->wp_addr = addr;
2269 wp->wp_size = len;
2270 wp->wp_type = type;
2271 wp->wp_wflags = wflags;
2273 if (kmdb_dpi_wapt_validate(wp) < 0)
2274 return (0); /* errno is set for us */
2276 for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
2277 if (sep->se_ops == &kmt_wapt_ops &&
2278 mdb_list_next(&sep->se_velist) != NULL &&
2279 kmt_wparg_overlap(wp, sep->se_data))
2280 goto wapt_dup;
2283 for (sep = mdb_list_next(&t->t_idle); sep; sep = mdb_list_next(sep)) {
2284 if (sep->se_ops == &kmt_wapt_ops && kmt_wparg_overlap(wp,
2285 ((mdb_vespec_t *)mdb_list_next(&sep->se_velist))->ve_args))
2286 goto wapt_dup;
2289 return (mdb_tgt_vespec_insert(t, &kmt_wapt_ops, spec_flags,
2290 func, data, wp, kmt_wparg_dtor));
2292 wapt_dup:
2293 mdb_free(wp, sizeof (kmdb_wapt_t));
2294 (void) set_errno(EMDB_WPDUP);
2295 return (0);
2298 static int
2299 kmt_add_pwapt(mdb_tgt_t *t, physaddr_t addr, size_t len, uint_t wflags,
2300 int spec_flags, mdb_tgt_se_f *func, void *data)
2302 return (kmt_add_wapt_common(t, (uintptr_t)addr, len, wflags, spec_flags,
2303 func, data, DPI_WAPT_TYPE_PHYS));
2306 static int
2307 kmt_add_vwapt(mdb_tgt_t *t, uintptr_t addr, size_t len, uint_t wflags,
2308 int spec_flags, mdb_tgt_se_f *func, void *data)
2310 return (kmt_add_wapt_common(t, addr, len, wflags, spec_flags, func,
2311 data, DPI_WAPT_TYPE_VIRT));
2314 static int
2315 kmt_add_iowapt(mdb_tgt_t *t, uintptr_t addr, size_t len, uint_t wflags,
2316 int spec_flags, mdb_tgt_se_f *func, void *data)
2318 return (kmt_add_wapt_common(t, addr, len, wflags, spec_flags, func,
2319 data, DPI_WAPT_TYPE_IO));
2322 static int
2323 kmt_add_trap(mdb_tgt_t *t, int trapnum, int spec_flags, mdb_tgt_se_f *func,
2324 void *data)
2326 kmt_data_t *kmt = t->t_data;
2328 if (trapnum != KMT_TRAP_ALL && trapnum != KMT_TRAP_NOTENUM) {
2329 if (trapnum < 0 || trapnum > kmt->kmt_trapmax) {
2330 (void) set_errno(EMDB_BADFLTNUM);
2331 return (0);
2334 BT_SET(kmt->kmt_trapmap, trapnum);
2337 return (mdb_tgt_vespec_insert(t, &kmt_trap_ops, spec_flags, func, data,
2338 (void *)(uintptr_t)trapnum, no_ve_dtor));
2341 /*ARGSUSED*/
2342 static uintmax_t
2343 kmt_cpuid_disc_get(const mdb_var_t *v)
2345 return (kmdb_dpi_get_master_cpuid());
2348 static const mdb_nv_disc_t kmt_cpuid_disc = {
2349 NULL,
2350 kmt_cpuid_disc_get
2354 * This routine executes while the kernel is running.
2356 void
2357 kmt_activate(mdb_tgt_t *t)
2359 kmt_data_t *kmt = t->t_data;
2361 mdb_prop_postmortem = FALSE;
2362 mdb_prop_kernel = TRUE;
2364 (void) mdb_tgt_register_dcmds(t, &kmt_dcmds[0], MDB_MOD_FORCE);
2365 mdb_tgt_register_regvars(t, kmt->kmt_rds, &kmt_reg_disc, 0);
2368 * Force load of the MDB krtld module, in case it's been rolled into
2369 * unix.
2371 (void) mdb_module_load(KMT_RTLD_NAME, MDB_MOD_SILENT | MDB_MOD_DEFER);
2374 static void
2375 kmt_destroy(mdb_tgt_t *t)
2377 kmt_data_t *kmt = t->t_data;
2378 kmt_module_t *km, *pkm;
2380 mdb_nv_destroy(&kmt->kmt_modules);
2381 for (km = mdb_list_prev(&kmt->kmt_modlist); km != NULL; km = pkm) {
2382 pkm = mdb_list_prev(km);
2383 mdb_free(km, sizeof (kmt_module_t));
2386 if (!kmt_defbp_lock)
2387 kmt_defbp_destroy_all();
2389 if (kmt->kmt_trapmap != NULL)
2390 mdb_free(kmt->kmt_trapmap, BT_SIZEOFMAP(kmt->kmt_trapmax));
2392 if (kmt->kmt_cpu != NULL)
2393 kmt_cpu_destroy(kmt->kmt_cpu);
2395 if (kmt != NULL)
2396 mdb_free(kmt, sizeof (kmt_data_t));
2399 static const mdb_tgt_ops_t kmt_ops = {
2400 kmt_setflags, /* t_setflags */
2401 (int (*)()) mdb_tgt_notsup, /* t_setcontext */
2402 kmt_activate, /* t_activate */
2403 (void (*)()) mdb_tgt_nop, /* t_deactivate */
2404 kmt_periodic, /* t_periodic */
2405 kmt_destroy, /* t_destroy */
2406 kmt_name, /* t_name */
2407 (const char *(*)()) mdb_conf_isa, /* t_isa */
2408 kmt_platform, /* t_platform */
2409 kmt_uname, /* t_uname */
2410 kmt_dmodel, /* t_dmodel */
2411 (ssize_t (*)()) mdb_tgt_notsup, /* t_aread */
2412 (ssize_t (*)()) mdb_tgt_notsup, /* t_awrite */
2413 kmt_read, /* t_vread */
2414 kmt_write, /* t_vwrite */
2415 kmt_pread, /* t_pread */
2416 kmt_pwrite, /* t_pwrite */
2417 kmt_read, /* t_fread */
2418 kmt_write, /* t_fwrite */
2419 kmt_ioread, /* t_ioread */
2420 kmt_iowrite, /* t_iowrite */
2421 kmt_vtop, /* t_vtop */
2422 kmt_lookup_by_name, /* t_lookup_by_name */
2423 kmt_lookup_by_addr, /* t_lookup_by_addr */
2424 kmt_symbol_iter, /* t_symbol_iter */
2425 kmt_mapping_iter, /* t_mapping_iter */
2426 kmt_object_iter, /* t_object_iter */
2427 kmt_addr_to_map, /* t_addr_to_map */
2428 kmt_name_to_map, /* t_name_to_map */
2429 kmt_addr_to_ctf, /* t_addr_to_ctf */
2430 kmt_name_to_ctf, /* t_name_to_ctf */
2431 kmt_status, /* t_status */
2432 (int (*)()) mdb_tgt_notsup, /* t_run */
2433 kmt_step, /* t_step */
2434 kmt_step_out, /* t_step_out */
2435 kmt_step_branch, /* t_step_branch */
2436 kmt_next, /* t_next */
2437 kmt_continue, /* t_cont */
2438 (int (*)()) mdb_tgt_notsup, /* t_signal */
2439 kmt_add_vbrkpt, /* t_add_vbrkpt */
2440 kmt_add_sbrkpt, /* t_add_sbrkpt */
2441 kmt_add_pwapt, /* t_add_pwapt */
2442 kmt_add_vwapt, /* t_add_vwapt */
2443 kmt_add_iowapt, /* t_add_iowapt */
2444 (int (*)()) mdb_tgt_null, /* t_add_sysenter */
2445 (int (*)()) mdb_tgt_null, /* t_add_sysexit */
2446 (int (*)()) mdb_tgt_null, /* t_add_signal */
2447 kmt_add_trap, /* t_add_fault */
2448 kmt_getareg, /* t_getareg */
2449 kmt_putareg, /* t_putareg */
2450 (int (*)()) mdb_tgt_nop, /* XXX t_stack_iter */
2451 (int (*)()) mdb_tgt_notsup /* t_auxv */
2455 * Called immediately upon resumption of the system after a step or continue.
2456 * Allows us to synchronize kmt's view of the world with reality.
2458 /*ARGSUSED*/
2459 static void
2460 kmt_sync(mdb_tgt_t *t)
2462 kmt_data_t *kmt = t->t_data;
2463 int symavail;
2465 mdb_dprintf(MDB_DBG_KMOD, "synchronizing with kernel\n");
2467 symavail = kmt->kmt_symavail;
2468 kmt->kmt_symavail = FALSE;
2471 * Resync our view of the world if the modules have changed, or if we
2472 * didn't have any symbols coming into this function. The latter will
2473 * only happen on startup.
2475 if (kmdb_kdi_mods_changed() || !symavail)
2476 kmt_modlist_update(t);
2479 * It would be nice if we could run this less frequently, perhaps
2480 * after a dvec-initiated trigger.
2482 kmdb_module_sync();
2484 kmt->kmt_symavail = TRUE;
2486 mdb_dprintf(MDB_DBG_KMOD, "synchronization complete\n");
2488 kmt_defbp_prune();
2490 if (kmt_defbp_num > 0 && kmt_defbp_bpspec == 0 &&
2491 kmdb_kdi_dtrace_get_state() != KDI_DTSTATE_DTRACE_ACTIVE) {
2493 * Deferred breakpoints were created while DTrace was active,
2494 * and consequently the deferred breakpoint enabling mechanism
2495 * wasn't activated. Activate it now, and then try to activate
2496 * the deferred breakpoints. We do this so that we can catch
2497 * the ones which may apply to modules that have been loaded
2498 * while they were waiting for DTrace to deactivate.
2500 (void) kmt_defbp_activate(t);
2501 (void) mdb_tgt_sespec_activate_all(t);
2504 if (kmt->kmt_cpu_retry && ((kmt->kmt_cpu = kmt_cpu_create(t)) !=
2505 NULL || errno != EAGAIN))
2506 kmt->kmt_cpu_retry = FALSE;
2508 (void) mdb_tgt_status(t, &t->t_status);
2512 * This routine executes while the kernel is running.
2514 /*ARGSUSED*/
2516 kmdb_kvm_create(mdb_tgt_t *t, int argc, const char *argv[])
2518 kmt_data_t *kmt;
2520 if (argc != 0)
2521 return (set_errno(EINVAL));
2523 kmt = mdb_zalloc(sizeof (kmt_data_t), UM_SLEEP);
2524 t->t_data = kmt;
2525 t->t_ops = &kmt_ops;
2526 t->t_flags |= MDB_TGT_F_RDWR; /* kmdb is always r/w */
2528 (void) mdb_nv_insert(&mdb.m_nv, "cpuid", &kmt_cpuid_disc, 0,
2529 MDB_NV_PERSIST | MDB_NV_RDONLY);
2531 (void) mdb_nv_create(&kmt->kmt_modules, UM_SLEEP);
2533 kmt_init_isadep(t);
2535 kmt->kmt_symavail = FALSE;
2536 kmt->kmt_cpu_retry = TRUE;
2538 bzero(&kmt_defbp_list, sizeof (mdb_list_t));
2540 return (0);
2542 create_err:
2543 kmt_destroy(t);
2545 return (-1);
2549 * This routine is called once, when kmdb first has control of the world.
2551 void
2552 kmdb_kvm_startup(void)
2554 kmt_data_t *kmt = mdb.m_target->t_data;
2556 mdb_dprintf(MDB_DBG_KMOD, "kmdb_kvm startup\n");
2558 kmt_sync(mdb.m_target);
2559 (void) mdb_module_load_builtin(KMT_MODULE);
2560 kmt_startup_isadep(mdb.m_target);
2563 * This is here because we need to write the deferred breakpoint
2564 * breakpoint when the debugger starts. Our normal r/o write routines
2565 * don't work when the kernel is running, so we have to do it during
2566 * startup.
2568 (void) mdb_tgt_sespec_activate_all(mdb.m_target);
2570 kmt->kmt_rtld_name = KMT_RTLD_NAME;
2572 if (kmt_module_by_name(kmt, KMT_RTLD_NAME) == NULL)
2573 kmt->kmt_rtld_name = "unix";
2577 * This routine is called after kmdb has loaded its initial set of modules.
2579 void
2580 kmdb_kvm_poststartup(void)
2582 mdb_dprintf(MDB_DBG_KMOD, "kmdb_kvm post-startup\n");
2584 (void) mdb_dis_select(kmt_def_dismode());