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