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