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