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