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