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 /* 30 * Mdb kernel support module. This module is loaded automatically when the 31 * kvm target is initialized. Any global functions declared here are exported 32 * for the resolution of symbols in subsequently loaded modules. 33 * 34 * WARNING: Do not assume that static variables in mdb_ks will be initialized 35 * to zero. 36 */ 37 38 39 #include <mdb/mdb_target.h> 40 #include <mdb/mdb_param.h> 41 #include <mdb/mdb_modapi.h> 42 #include <mdb/mdb_ks.h> 43 44 #include <sys/types.h> 45 #include <sys/procfs.h> 46 #include <sys/proc.h> 47 #include <sys/dnlc.h> 48 #include <sys/autoconf.h> 49 #include <sys/machelf.h> 50 #include <sys/modctl.h> 51 #include <sys/hwconf.h> 52 #include <sys/kobj.h> 53 #include <sys/fs/autofs.h> 54 #include <sys/ddi_impldefs.h> 55 #include <sys/refstr_impl.h> 56 #include <sys/cpuvar.h> 57 #include <errno.h> 58 59 #include <vm/seg_vn.h> 60 #include <vm/page.h> 61 62 #define MDB_PATH_NELEM 256 /* Maximum path components */ 63 64 typedef struct mdb_path { 65 size_t mdp_nelem; /* Number of components */ 66 uint_t mdp_complete; /* Path completely resolved? */ 67 uintptr_t mdp_vnode[MDB_PATH_NELEM]; /* Array of vnode_t addresses */ 68 char *mdp_name[MDB_PATH_NELEM]; /* Array of name components */ 69 } mdb_path_t; 70 71 static int mdb_autonode2path(uintptr_t, mdb_path_t *); 72 static int mdb_sprintpath(char *, size_t, mdb_path_t *); 73 74 /* 75 * Kernel parameters from <sys/param.h> which we keep in-core: 76 */ 77 unsigned long _mdb_ks_pagesize; 78 unsigned int _mdb_ks_pageshift; 79 unsigned long _mdb_ks_pageoffset; 80 unsigned long long _mdb_ks_pagemask; 81 unsigned long _mdb_ks_mmu_pagesize; 82 unsigned int _mdb_ks_mmu_pageshift; 83 unsigned long _mdb_ks_mmu_pageoffset; 84 unsigned long _mdb_ks_mmu_pagemask; 85 uintptr_t _mdb_ks_kernelbase; 86 uintptr_t _mdb_ks_userlimit; 87 uintptr_t _mdb_ks_userlimit32; 88 uintptr_t _mdb_ks_argsbase; 89 unsigned long _mdb_ks_msg_bsize; 90 unsigned long _mdb_ks_defaultstksz; 91 int _mdb_ks_ncpu; 92 93 /* 94 * In-core copy of DNLC information: 95 */ 96 #define MDB_DNLC_HSIZE 1024 97 #define MDB_DNLC_HASH(vp) (((uintptr_t)(vp) >> 3) & (MDB_DNLC_HSIZE - 1)) 98 #define MDB_DNLC_NCACHE_SZ(ncp) (sizeof (ncache_t) + (ncp)->namlen) 99 #define MDB_DNLC_MAX_RETRY 4 100 101 102 static ncache_t **dnlc_hash; /* mdbs hash array of dnlc entries */ 103 104 /* 105 * This will be the location of the vnodeops pointer for "autofs_vnodeops" 106 * The pointer still needs to be read with mdb_vread() to get the location 107 * of the vnodeops structure for autofs. 108 */ 109 static struct vnodeops *autofs_vnops_ptr; 110 111 /* 112 * STREAMS queue registrations: 113 */ 114 typedef struct mdb_qinfo { 115 const mdb_qops_t *qi_ops; /* Address of ops vector */ 116 uintptr_t qi_addr; /* Address of qinit structure (key) */ 117 struct mdb_qinfo *qi_next; /* Next qinfo in list */ 118 } mdb_qinfo_t; 119 120 static mdb_qinfo_t *qi_head; /* Head of qinfo chain */ 121 122 /* 123 * Device naming callback structure: 124 */ 125 typedef struct nm_query { 126 const char *nm_name; /* Device driver name [in/out] */ 127 major_t nm_major; /* Device major number [in/out] */ 128 ushort_t nm_found; /* Did we find a match? [out] */ 129 } nm_query_t; 130 131 /* 132 * Address-to-modctl callback structure: 133 */ 134 typedef struct a2m_query { 135 uintptr_t a2m_addr; /* Virtual address [in] */ 136 uintptr_t a2m_where; /* Modctl address [out] */ 137 } a2m_query_t; 138 139 /* 140 * Segment-to-mdb_map callback structure: 141 */ 142 typedef struct { 143 struct seg_ops *asm_segvn_ops; /* Address of segvn ops [in] */ 144 void (*asm_callback)(const struct mdb_map *, void *); /* Callb [in] */ 145 void *asm_cbdata; /* Callback data [in] */ 146 } asmap_arg_t; 147 148 static void 149 dnlc_free(void) 150 { 151 ncache_t *ncp, *next; 152 int i; 153 154 if (dnlc_hash == NULL) { 155 return; 156 } 157 158 /* 159 * Free up current dnlc entries 160 */ 161 for (i = 0; i < MDB_DNLC_HSIZE; i++) { 162 for (ncp = dnlc_hash[i]; ncp; ncp = next) { 163 next = ncp->hash_next; 164 mdb_free(ncp, MDB_DNLC_NCACHE_SZ(ncp)); 165 } 166 } 167 mdb_free(dnlc_hash, MDB_DNLC_HSIZE * sizeof (ncache_t *)); 168 dnlc_hash = NULL; 169 } 170 171 char bad_dnlc[] = "inconsistent dnlc chain: %d, ncache va: %p" 172 " - continuing with the rest\n"; 173 174 static int 175 dnlc_load(void) 176 { 177 int i; /* hash index */ 178 int retry_cnt = 0; 179 int skip_bad_chains = 0; 180 int nc_hashsz; /* kernel hash array size */ 181 uintptr_t nc_hash_addr; /* kernel va of ncache hash array */ 182 uintptr_t head; /* kernel va of head of hash chain */ 183 184 /* 185 * If we've already cached the DNLC and we're looking at a dump, 186 * our cache is good forever, so don't bother re-loading. 187 */ 188 if (dnlc_hash && mdb_prop_postmortem) { 189 return (0); 190 } 191 192 /* 193 * For a core dump, retries wont help. 194 * Just print and skip any bad chains. 195 */ 196 if (mdb_prop_postmortem) { 197 skip_bad_chains = 1; 198 } 199 retry: 200 if (retry_cnt++ >= MDB_DNLC_MAX_RETRY) { 201 /* 202 * Give up retrying the rapidly changing dnlc. 203 * Just print and skip any bad chains 204 */ 205 skip_bad_chains = 1; 206 } 207 208 dnlc_free(); /* Free up the mdb hashed dnlc - if any */ 209 210 /* 211 * Although nc_hashsz and the location of nc_hash doesn't currently 212 * change, it may do in the future with a more dynamic dnlc. 213 * So always read these values afresh. 214 */ 215 if (mdb_readvar(&nc_hashsz, "nc_hashsz") == -1) { 216 mdb_warn("failed to read nc_hashsz"); 217 return (-1); 218 } 219 if (mdb_readvar(&nc_hash_addr, "nc_hash") == -1) { 220 mdb_warn("failed to read nc_hash"); 221 return (-1); 222 } 223 224 /* 225 * Allocate the mdb dnlc hash array 226 */ 227 dnlc_hash = mdb_zalloc(MDB_DNLC_HSIZE * sizeof (ncache_t *), UM_SLEEP); 228 229 /* for each kernel hash chain */ 230 for (i = 0, head = nc_hash_addr; i < nc_hashsz; 231 i++, head += sizeof (nc_hash_t)) { 232 nc_hash_t nch; /* kernel hash chain header */ 233 ncache_t *ncp; /* name cache pointer */ 234 int hash; /* mdb hash value */ 235 uintptr_t nc_va; /* kernel va of next ncache */ 236 uintptr_t ncprev_va; /* kernel va of previous ncache */ 237 int khash; /* kernel dnlc hash value */ 238 uchar_t namelen; /* name length */ 239 ncache_t nc; /* name cache entry */ 240 int nc_size; /* size of a name cache entry */ 241 242 /* 243 * We read each element of the nc_hash array individually 244 * just before we process the entries in its chain. This is 245 * because the chain can change so rapidly on a running system. 246 */ 247 if (mdb_vread(&nch, sizeof (nc_hash_t), head) == -1) { 248 mdb_warn("failed to read nc_hash chain header %d", i); 249 dnlc_free(); 250 return (-1); 251 } 252 253 ncprev_va = head; 254 nc_va = (uintptr_t)(nch.hash_next); 255 /* for each entry in the chain */ 256 while (nc_va != head) { 257 /* 258 * The size of the ncache entries varies 259 * because the name is appended to the structure. 260 * So we read in the structure then re-read 261 * for the structure plus name. 262 */ 263 if (mdb_vread(&nc, sizeof (ncache_t), nc_va) == -1) { 264 if (skip_bad_chains) { 265 mdb_warn(bad_dnlc, i, nc_va); 266 break; 267 } 268 goto retry; 269 } 270 nc_size = MDB_DNLC_NCACHE_SZ(&nc); 271 ncp = mdb_alloc(nc_size, UM_SLEEP); 272 if (mdb_vread(ncp, nc_size - 1, nc_va) == -1) { 273 mdb_free(ncp, nc_size); 274 if (skip_bad_chains) { 275 mdb_warn(bad_dnlc, i, nc_va); 276 break; 277 } 278 goto retry; 279 } 280 281 /* 282 * Check for chain consistency 283 */ 284 if ((uintptr_t)ncp->hash_prev != ncprev_va) { 285 mdb_free(ncp, nc_size); 286 if (skip_bad_chains) { 287 mdb_warn(bad_dnlc, i, nc_va); 288 break; 289 } 290 goto retry; 291 } 292 /* 293 * Terminate the new name with a null. 294 * Note, we allowed space for this null when 295 * allocating space for the entry. 296 */ 297 ncp->name[ncp->namlen] = '\0'; 298 299 /* 300 * Validate new entry by re-hashing using the 301 * kernel dnlc hash function and comparing the hash 302 */ 303 DNLCHASH(ncp->name, ncp->dp, khash, namelen); 304 if ((namelen != ncp->namlen) || 305 (khash != ncp->hash)) { 306 mdb_free(ncp, nc_size); 307 if (skip_bad_chains) { 308 mdb_warn(bad_dnlc, i, nc_va); 309 break; 310 } 311 goto retry; 312 } 313 314 /* 315 * Finally put the validated entry into the mdb 316 * hash chains. Reuse the kernel next hash field 317 * for the mdb hash chain pointer. 318 */ 319 hash = MDB_DNLC_HASH(ncp->vp); 320 ncprev_va = nc_va; 321 nc_va = (uintptr_t)(ncp->hash_next); 322 ncp->hash_next = dnlc_hash[hash]; 323 dnlc_hash[hash] = ncp; 324 } 325 } 326 return (0); 327 } 328 329 /*ARGSUSED*/ 330 int 331 dnlcdump(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 332 { 333 ncache_t *ent; 334 int i; 335 336 if ((flags & DCMD_ADDRSPEC) || argc != 0) 337 return (DCMD_USAGE); 338 339 if (dnlc_load() == -1) 340 return (DCMD_ERR); 341 342 mdb_printf("%<u>%-?s %-?s %-32s%</u>\n", "VP", "DVP", "NAME"); 343 344 for (i = 0; i < MDB_DNLC_HSIZE; i++) { 345 for (ent = dnlc_hash[i]; ent != NULL; ent = ent->hash_next) { 346 mdb_printf("%0?p %0?p %s\n", 347 ent->vp, ent->dp, ent->name); 348 } 349 } 350 351 return (DCMD_OK); 352 } 353 354 static int 355 mdb_sprintpath(char *buf, size_t len, mdb_path_t *path) 356 { 357 char *s = buf; 358 int i; 359 360 if (len < sizeof ("/...")) 361 return (-1); 362 363 if (!path->mdp_complete) { 364 (void) strcpy(s, "??"); 365 s += 2; 366 367 if (path->mdp_nelem == 0) 368 return (-1); 369 } 370 371 if (path->mdp_nelem == 0) { 372 (void) strcpy(s, "/"); 373 return (0); 374 } 375 376 for (i = path->mdp_nelem - 1; i >= 0; i--) { 377 /* 378 * Number of bytes left is the distance from where we 379 * are to the end, minus 2 for '/' and '\0' 380 */ 381 ssize_t left = (ssize_t)(&buf[len] - s) - 2; 382 383 if (left <= 0) 384 break; 385 386 *s++ = '/'; 387 (void) strncpy(s, path->mdp_name[i], left); 388 s[left - 1] = '\0'; 389 s += strlen(s); 390 391 if (left < strlen(path->mdp_name[i])) 392 break; 393 } 394 395 if (i >= 0) 396 (void) strcpy(&buf[len - 4], "..."); 397 398 return (0); 399 } 400 401 static int 402 mdb_autonode2path(uintptr_t addr, mdb_path_t *path) 403 { 404 fninfo_t fni; 405 fnnode_t fn; 406 407 vnode_t vn; 408 vfs_t vfs; 409 struct vnodeops *autofs_vnops = NULL; 410 411 /* 412 * "autofs_vnops_ptr" is the address of the pointer to the vnodeops 413 * structure for autofs. We want to read it each time we access 414 * it since autofs could (in theory) be unloaded and reloaded. 415 */ 416 if (mdb_vread(&autofs_vnops, sizeof (autofs_vnops), 417 (uintptr_t)autofs_vnops_ptr) == -1) 418 return (-1); 419 420 if (mdb_vread(&vn, sizeof (vn), addr) == -1) 421 return (-1); 422 423 if (autofs_vnops == NULL || vn.v_op != autofs_vnops) 424 return (-1); 425 426 addr = (uintptr_t)vn.v_data; 427 428 if (mdb_vread(&vfs, sizeof (vfs), (uintptr_t)vn.v_vfsp) == -1 || 429 mdb_vread(&fni, sizeof (fni), (uintptr_t)vfs.vfs_data) == -1 || 430 mdb_vread(&vn, sizeof (vn), (uintptr_t)fni.fi_rootvp) == -1) 431 return (-1); 432 433 for (;;) { 434 size_t elem = path->mdp_nelem++; 435 char elemstr[MAXNAMELEN]; 436 char *c, *p; 437 438 if (elem == MDB_PATH_NELEM) { 439 path->mdp_nelem--; 440 return (-1); 441 } 442 443 if (mdb_vread(&fn, sizeof (fn), addr) != sizeof (fn)) { 444 path->mdp_nelem--; 445 return (-1); 446 } 447 448 if (mdb_readstr(elemstr, sizeof (elemstr), 449 (uintptr_t)fn.fn_name) <= 0) { 450 (void) strcpy(elemstr, "?"); 451 } 452 453 c = mdb_alloc(strlen(elemstr) + 1, UM_SLEEP | UM_GC); 454 (void) strcpy(c, elemstr); 455 456 path->mdp_vnode[elem] = (uintptr_t)fn.fn_vnode; 457 458 if (addr == (uintptr_t)fn.fn_parent) { 459 path->mdp_name[elem] = &c[1]; 460 path->mdp_complete = TRUE; 461 break; 462 } 463 464 if ((p = strrchr(c, '/')) != NULL) 465 path->mdp_name[elem] = p + 1; 466 else 467 path->mdp_name[elem] = c; 468 469 addr = (uintptr_t)fn.fn_parent; 470 } 471 472 return (0); 473 } 474 475 int 476 mdb_vnode2path(uintptr_t addr, char *buf, size_t buflen) 477 { 478 uintptr_t rootdir; 479 ncache_t *ent; 480 vnode_t vp; 481 mdb_path_t path; 482 483 /* 484 * Check to see if we have a cached value for this vnode 485 */ 486 if (mdb_vread(&vp, sizeof (vp), addr) != -1 && 487 vp.v_path != NULL && 488 mdb_readstr(buf, buflen, (uintptr_t)vp.v_path) != -1) 489 return (0); 490 491 if (dnlc_load() == -1) 492 return (-1); 493 494 if (mdb_readvar(&rootdir, "rootdir") == -1) { 495 mdb_warn("failed to read 'rootdir'"); 496 return (-1); 497 } 498 499 bzero(&path, sizeof (mdb_path_t)); 500 again: 501 if ((addr == NULL) && (path.mdp_nelem == 0)) { 502 /* 503 * 0 elems && complete tells sprintpath to just print "/" 504 */ 505 path.mdp_complete = TRUE; 506 goto out; 507 } 508 509 if (addr == rootdir) { 510 path.mdp_complete = TRUE; 511 goto out; 512 } 513 514 for (ent = dnlc_hash[MDB_DNLC_HASH(addr)]; ent; ent = ent->hash_next) { 515 if ((uintptr_t)ent->vp == addr) { 516 if (strcmp(ent->name, "..") == 0 || 517 strcmp(ent->name, ".") == 0) 518 continue; 519 520 path.mdp_vnode[path.mdp_nelem] = (uintptr_t)ent->vp; 521 path.mdp_name[path.mdp_nelem] = ent->name; 522 path.mdp_nelem++; 523 524 if (path.mdp_nelem == MDB_PATH_NELEM) { 525 path.mdp_nelem--; 526 mdb_warn("path exceeded maximum expected " 527 "elements\n"); 528 return (-1); 529 } 530 531 addr = (uintptr_t)ent->dp; 532 goto again; 533 } 534 } 535 536 (void) mdb_autonode2path(addr, &path); 537 538 out: 539 return (mdb_sprintpath(buf, buflen, &path)); 540 } 541 542 543 uintptr_t 544 mdb_pid2proc(pid_t pid, proc_t *proc) 545 { 546 int pid_hashsz, hash; 547 uintptr_t paddr, pidhash, procdir; 548 struct pid pidp; 549 550 if (mdb_readvar(&pidhash, "pidhash") == -1) 551 return (NULL); 552 553 if (mdb_readvar(&pid_hashsz, "pid_hashsz") == -1) 554 return (NULL); 555 556 if (mdb_readvar(&procdir, "procdir") == -1) 557 return (NULL); 558 559 hash = pid & (pid_hashsz - 1); 560 561 if (mdb_vread(&paddr, sizeof (paddr), 562 pidhash + (hash * sizeof (paddr))) == -1) 563 return (NULL); 564 565 while (paddr != 0) { 566 if (mdb_vread(&pidp, sizeof (pidp), paddr) == -1) 567 return (NULL); 568 569 if (pidp.pid_id == pid) { 570 uintptr_t procp; 571 572 if (mdb_vread(&procp, sizeof (procp), procdir + 573 (pidp.pid_prslot * sizeof (procp))) == -1) 574 return (NULL); 575 576 if (proc != NULL) 577 (void) mdb_vread(proc, sizeof (proc_t), procp); 578 579 return (procp); 580 } 581 paddr = (uintptr_t)pidp.pid_link; 582 } 583 return (NULL); 584 } 585 586 int 587 mdb_cpu2cpuid(uintptr_t cpup) 588 { 589 cpu_t cpu; 590 591 if (mdb_vread(&cpu, sizeof (cpu_t), cpup) != sizeof (cpu_t)) 592 return (-1); 593 594 return (cpu.cpu_id); 595 } 596 597 uintptr_t 598 mdb_vnode2page(uintptr_t vp, uintptr_t offset) 599 { 600 long page_hashsz, ndx; 601 uintptr_t page_hash, pp; 602 603 if (mdb_readvar(&page_hashsz, "page_hashsz") == -1 || 604 mdb_readvar(&page_hash, "page_hash") == -1) 605 return (NULL); 606 607 ndx = PAGE_HASH_FUNC(vp, offset); 608 page_hash += ndx * sizeof (uintptr_t); 609 610 mdb_vread(&pp, sizeof (pp), page_hash); 611 612 while (pp != NULL) { 613 page_t page; 614 615 mdb_vread(&page, sizeof (page), pp); 616 617 if ((uintptr_t)page.p_vnode == vp && 618 (uintptr_t)page.p_offset == offset) 619 return (pp); 620 621 pp = (uintptr_t)page.p_hash; 622 } 623 624 return (NULL); 625 } 626 627 char 628 mdb_vtype2chr(vtype_t type, mode_t mode) 629 { 630 static const char vttab[] = { 631 ' ', /* VNON */ 632 ' ', /* VREG */ 633 '/', /* VDIR */ 634 ' ', /* VBLK */ 635 ' ', /* VCHR */ 636 '@', /* VLNK */ 637 '|', /* VFIFO */ 638 '>', /* VDOOR */ 639 ' ', /* VPROC */ 640 '=', /* VSOCK */ 641 ' ', /* VBAD */ 642 }; 643 644 if (type < 0 || type >= sizeof (vttab) / sizeof (vttab[0])) 645 return ('?'); 646 647 if (type == VREG && (mode & 0111) != 0) 648 return ('*'); 649 650 return (vttab[type]); 651 } 652 653 static int 654 a2m_walk_modctl(uintptr_t addr, const struct modctl *m, a2m_query_t *a2m) 655 { 656 struct module mod; 657 658 if (m->mod_mp == NULL) 659 return (0); 660 661 if (mdb_vread(&mod, sizeof (mod), (uintptr_t)m->mod_mp) == -1) { 662 mdb_warn("couldn't read modctl %p's module", addr); 663 return (0); 664 } 665 666 if (a2m->a2m_addr >= (uintptr_t)mod.text && 667 a2m->a2m_addr < (uintptr_t)mod.text + mod.text_size) 668 goto found; 669 670 if (a2m->a2m_addr >= (uintptr_t)mod.data && 671 a2m->a2m_addr < (uintptr_t)mod.data + mod.data_size) 672 goto found; 673 674 return (0); 675 676 found: 677 a2m->a2m_where = addr; 678 return (-1); 679 } 680 681 uintptr_t 682 mdb_addr2modctl(uintptr_t addr) 683 { 684 a2m_query_t a2m; 685 686 a2m.a2m_addr = addr; 687 a2m.a2m_where = NULL; 688 689 (void) mdb_walk("modctl", (mdb_walk_cb_t)a2m_walk_modctl, &a2m); 690 return (a2m.a2m_where); 691 } 692 693 static mdb_qinfo_t * 694 qi_lookup(uintptr_t qinit_addr) 695 { 696 mdb_qinfo_t *qip; 697 698 for (qip = qi_head; qip != NULL; qip = qip->qi_next) { 699 if (qip->qi_addr == qinit_addr) 700 return (qip); 701 } 702 703 return (NULL); 704 } 705 706 void 707 mdb_qops_install(const mdb_qops_t *qops, uintptr_t qinit_addr) 708 { 709 mdb_qinfo_t *qip = qi_lookup(qinit_addr); 710 711 if (qip != NULL) { 712 qip->qi_ops = qops; 713 return; 714 } 715 716 qip = mdb_alloc(sizeof (mdb_qinfo_t), UM_SLEEP); 717 718 qip->qi_ops = qops; 719 qip->qi_addr = qinit_addr; 720 qip->qi_next = qi_head; 721 722 qi_head = qip; 723 } 724 725 void 726 mdb_qops_remove(const mdb_qops_t *qops, uintptr_t qinit_addr) 727 { 728 mdb_qinfo_t *qip, *p = NULL; 729 730 for (qip = qi_head; qip != NULL; p = qip, qip = qip->qi_next) { 731 if (qip->qi_addr == qinit_addr && qip->qi_ops == qops) { 732 if (qi_head == qip) 733 qi_head = qip->qi_next; 734 else 735 p->qi_next = qip->qi_next; 736 mdb_free(qip, sizeof (mdb_qinfo_t)); 737 return; 738 } 739 } 740 } 741 742 char * 743 mdb_qname(const queue_t *q, char *buf, size_t nbytes) 744 { 745 struct module_info mi; 746 struct qinit qi; 747 748 if (mdb_vread(&qi, sizeof (qi), (uintptr_t)q->q_qinfo) == -1) { 749 mdb_warn("failed to read qinit at %p", q->q_qinfo); 750 goto err; 751 } 752 753 if (mdb_vread(&mi, sizeof (mi), (uintptr_t)qi.qi_minfo) == -1) { 754 mdb_warn("failed to read module_info at %p", qi.qi_minfo); 755 goto err; 756 } 757 758 if (mdb_readstr(buf, nbytes, (uintptr_t)mi.mi_idname) <= 0) { 759 mdb_warn("failed to read mi_idname at %p", mi.mi_idname); 760 goto err; 761 } 762 763 return (buf); 764 765 err: 766 (void) mdb_snprintf(buf, nbytes, "???"); 767 return (buf); 768 } 769 770 void 771 mdb_qinfo(const queue_t *q, char *buf, size_t nbytes) 772 { 773 mdb_qinfo_t *qip = qi_lookup((uintptr_t)q->q_qinfo); 774 buf[0] = '\0'; 775 776 if (qip != NULL) 777 qip->qi_ops->q_info(q, buf, nbytes); 778 } 779 780 uintptr_t 781 mdb_qrnext(const queue_t *q) 782 { 783 mdb_qinfo_t *qip = qi_lookup((uintptr_t)q->q_qinfo); 784 785 if (qip != NULL) 786 return (qip->qi_ops->q_rnext(q)); 787 788 return (NULL); 789 } 790 791 uintptr_t 792 mdb_qwnext(const queue_t *q) 793 { 794 mdb_qinfo_t *qip = qi_lookup((uintptr_t)q->q_qinfo); 795 796 if (qip != NULL) 797 return (qip->qi_ops->q_wnext(q)); 798 799 return (NULL); 800 } 801 802 uintptr_t 803 mdb_qrnext_default(const queue_t *q) 804 { 805 return ((uintptr_t)q->q_next); 806 } 807 808 uintptr_t 809 mdb_qwnext_default(const queue_t *q) 810 { 811 return ((uintptr_t)q->q_next); 812 } 813 814 /* 815 * The following three routines borrowed from modsubr.c 816 */ 817 static int 818 nm_hash(const char *name) 819 { 820 char c; 821 int hash = 0; 822 823 for (c = *name++; c; c = *name++) 824 hash ^= c; 825 826 return (hash & MOD_BIND_HASHMASK); 827 } 828 829 static uintptr_t 830 find_mbind(const char *name, uintptr_t *hashtab) 831 { 832 int hashndx; 833 uintptr_t mb; 834 struct bind mb_local; 835 char node_name[MODMAXNAMELEN + 1]; 836 837 838 hashndx = nm_hash(name); 839 mb = hashtab[hashndx]; 840 while (mb) { 841 if (mdb_vread(&mb_local, sizeof (mb_local), mb) == -1) { 842 mdb_warn("failed to read struct bind at %p", mb); 843 return (NULL); 844 } 845 if (mdb_readstr(node_name, sizeof (node_name), 846 (uintptr_t)mb_local.b_name) == -1) { 847 mdb_warn("failed to read node name string at %p", 848 mb_local.b_name); 849 return (NULL); 850 } 851 852 if (strcmp(name, node_name) == 0) 853 break; 854 855 mb = (uintptr_t)mb_local.b_next; 856 } 857 return (mb); 858 } 859 860 int 861 mdb_name_to_major(const char *name, major_t *major) 862 { 863 uintptr_t mbind; 864 uintptr_t mb_hashtab[MOD_BIND_HASHSIZE]; 865 struct bind mbind_local; 866 867 868 if (mdb_readsym(mb_hashtab, sizeof (mb_hashtab), "mb_hashtab") == -1) { 869 mdb_warn("failed to read symbol 'mb_hashtab'"); 870 return (-1); 871 } 872 873 if ((mbind = find_mbind(name, mb_hashtab)) != NULL) { 874 if (mdb_vread(&mbind_local, sizeof (mbind_local), mbind) == 875 -1) { 876 mdb_warn("failed to read mbind struct at %p", mbind); 877 return (-1); 878 } 879 880 *major = (major_t)mbind_local.b_num; 881 return (0); 882 } 883 return (-1); 884 } 885 886 const char * 887 mdb_major_to_name(major_t major) 888 { 889 static char name[MODMAXNAMELEN + 1]; 890 891 uintptr_t devnamesp; 892 struct devnames dn; 893 uint_t devcnt; 894 895 if (mdb_readvar(&devcnt, "devcnt") == -1 || major >= devcnt || 896 mdb_readvar(&devnamesp, "devnamesp") == -1) 897 return (NULL); 898 899 if (mdb_vread(&dn, sizeof (struct devnames), devnamesp + 900 major * sizeof (struct devnames)) != sizeof (struct devnames)) 901 return (NULL); 902 903 if (mdb_readstr(name, MODMAXNAMELEN + 1, (uintptr_t)dn.dn_name) == -1) 904 return (NULL); 905 906 return ((const char *)name); 907 } 908 909 /* 910 * Return the name of the driver attached to the dip in drivername. 911 */ 912 int 913 mdb_devinfo2driver(uintptr_t dip_addr, char *drivername, size_t namebufsize) 914 { 915 struct dev_info devinfo; 916 char bind_name[MODMAXNAMELEN + 1]; 917 major_t major; 918 const char *namestr; 919 920 921 if (mdb_vread(&devinfo, sizeof (devinfo), dip_addr) == -1) { 922 mdb_warn("failed to read devinfo at %p", dip_addr); 923 return (-1); 924 } 925 926 if (mdb_readstr(bind_name, sizeof (bind_name), 927 (uintptr_t)devinfo.devi_binding_name) == -1) { 928 mdb_warn("failed to read binding name at %p", 929 devinfo.devi_binding_name); 930 return (-1); 931 } 932 933 /* 934 * Many->one relation: various names to one major number 935 */ 936 if (mdb_name_to_major(bind_name, &major) == -1) { 937 mdb_warn("failed to translate bind name to major number\n"); 938 return (-1); 939 } 940 941 /* 942 * One->one relation: one major number corresponds to one driver 943 */ 944 if ((namestr = mdb_major_to_name(major)) == NULL) { 945 (void) strncpy(drivername, "???", namebufsize); 946 return (-1); 947 } 948 949 (void) strncpy(drivername, namestr, namebufsize); 950 return (0); 951 } 952 953 /* 954 * Find the name of the driver attached to this dip (if any), given: 955 * - the address of a dip (in core) 956 * - the NAME of the global pointer to the driver's i_ddi_soft_state struct 957 * - pointer to a pointer to receive the address 958 */ 959 int 960 mdb_devinfo2statep(uintptr_t dip_addr, char *soft_statep_name, 961 uintptr_t *statep) 962 { 963 struct dev_info dev_info; 964 965 966 if (mdb_vread(&dev_info, sizeof (dev_info), dip_addr) == -1) { 967 mdb_warn("failed to read devinfo at %p", dip_addr); 968 return (-1); 969 } 970 971 return (mdb_get_soft_state_byname(soft_statep_name, 972 dev_info.devi_instance, statep, NULL, 0)); 973 } 974 975 /* 976 * Returns a pointer to the top of the soft state struct for the instance 977 * specified (in state_addr), given the address of the global soft state 978 * pointer and size of the struct. Also fills in the buffer pointed to by 979 * state_buf_p (if non-NULL) with the contents of the state struct. 980 */ 981 int 982 mdb_get_soft_state_byaddr(uintptr_t ssaddr, uint_t instance, 983 uintptr_t *state_addr, void *state_buf_p, size_t sizeof_state) 984 { 985 struct i_ddi_soft_state ss; 986 void *statep; 987 988 989 if (mdb_vread(&ss, sizeof (ss), ssaddr) == -1) 990 return (-1); 991 992 if (instance >= ss.n_items) 993 return (-1); 994 995 if (mdb_vread(&statep, sizeof (statep), (uintptr_t)ss.array + 996 (sizeof (statep) * instance)) == -1) 997 return (-1); 998 999 if (state_addr != NULL) 1000 *state_addr = (uintptr_t)statep; 1001 1002 if (statep == NULL) { 1003 errno = ENOENT; 1004 return (-1); 1005 } 1006 1007 if (state_buf_p != NULL) { 1008 1009 /* Read the state struct into the buffer in local space. */ 1010 if (mdb_vread(state_buf_p, sizeof_state, 1011 (uintptr_t)statep) == -1) 1012 return (-1); 1013 } 1014 1015 return (0); 1016 } 1017 1018 1019 /* 1020 * Returns a pointer to the top of the soft state struct for the instance 1021 * specified (in state_addr), given the name of the global soft state pointer 1022 * and size of the struct. Also fills in the buffer pointed to by 1023 * state_buf_p (if non-NULL) with the contents of the state struct. 1024 */ 1025 int 1026 mdb_get_soft_state_byname(char *softstatep_name, uint_t instance, 1027 uintptr_t *state_addr, void *state_buf_p, size_t sizeof_state) 1028 { 1029 uintptr_t ssaddr; 1030 1031 if (mdb_readvar((void *)&ssaddr, softstatep_name) == -1) 1032 return (-1); 1033 1034 return (mdb_get_soft_state_byaddr(ssaddr, instance, state_addr, 1035 state_buf_p, sizeof_state)); 1036 } 1037 1038 static const mdb_dcmd_t dcmds[] = { 1039 { "dnlc", NULL, "print DNLC contents", dnlcdump }, 1040 { NULL } 1041 }; 1042 1043 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds }; 1044 1045 /*ARGSUSED*/ 1046 static void 1047 update_vars(void *arg) 1048 { 1049 GElf_Sym sym; 1050 1051 if (mdb_lookup_by_name("auto_vnodeops", &sym) == 0) 1052 autofs_vnops_ptr = (struct vnodeops *)(uintptr_t)sym.st_value; 1053 else 1054 autofs_vnops_ptr = NULL; 1055 1056 (void) mdb_readvar(&_mdb_ks_pagesize, "_pagesize"); 1057 (void) mdb_readvar(&_mdb_ks_pageshift, "_pageshift"); 1058 (void) mdb_readvar(&_mdb_ks_pageoffset, "_pageoffset"); 1059 (void) mdb_readvar(&_mdb_ks_pagemask, "_pagemask"); 1060 (void) mdb_readvar(&_mdb_ks_mmu_pagesize, "_mmu_pagesize"); 1061 (void) mdb_readvar(&_mdb_ks_mmu_pageshift, "_mmu_pageshift"); 1062 (void) mdb_readvar(&_mdb_ks_mmu_pageoffset, "_mmu_pageoffset"); 1063 (void) mdb_readvar(&_mdb_ks_mmu_pagemask, "_mmu_pagemask"); 1064 (void) mdb_readvar(&_mdb_ks_kernelbase, "_kernelbase"); 1065 1066 (void) mdb_readvar(&_mdb_ks_userlimit, "_userlimit"); 1067 (void) mdb_readvar(&_mdb_ks_userlimit32, "_userlimit32"); 1068 (void) mdb_readvar(&_mdb_ks_argsbase, "_argsbase"); 1069 (void) mdb_readvar(&_mdb_ks_msg_bsize, "_msg_bsize"); 1070 (void) mdb_readvar(&_mdb_ks_defaultstksz, "_defaultstksz"); 1071 (void) mdb_readvar(&_mdb_ks_ncpu, "_ncpu"); 1072 } 1073 1074 const mdb_modinfo_t * 1075 _mdb_init(void) 1076 { 1077 /* 1078 * When used with mdb, mdb_ks is a separate dmod. With kmdb, however, 1079 * mdb_ks is compiled into the debugger module. kmdb cannot 1080 * automatically modunload itself when it exits. If it restarts after 1081 * debugger fault, static variables may not be initialized to zero. 1082 * They must be manually reinitialized here. 1083 */ 1084 dnlc_hash = NULL; 1085 qi_head = NULL; 1086 1087 mdb_callback_add(MDB_CALLBACK_STCHG, update_vars, NULL); 1088 1089 update_vars(NULL); 1090 1091 return (&modinfo); 1092 } 1093 1094 void 1095 _mdb_fini(void) 1096 { 1097 dnlc_free(); 1098 while (qi_head != NULL) { 1099 mdb_qinfo_t *qip = qi_head; 1100 qi_head = qip->qi_next; 1101 mdb_free(qip, sizeof (mdb_qinfo_t)); 1102 } 1103 } 1104 1105 /* 1106 * Interface between MDB kproc target and mdb_ks. The kproc target relies 1107 * on looking up and invoking these functions in mdb_ks so that dependencies 1108 * on the current kernel implementation are isolated in mdb_ks. 1109 */ 1110 1111 /* 1112 * Given the address of a proc_t, return the p.p_as pointer; return NULL 1113 * if we were unable to read a proc structure from the given address. 1114 */ 1115 uintptr_t 1116 mdb_kproc_as(uintptr_t proc_addr) 1117 { 1118 proc_t p; 1119 1120 if (mdb_vread(&p, sizeof (p), proc_addr) == sizeof (p)) 1121 return ((uintptr_t)p.p_as); 1122 1123 return (NULL); 1124 } 1125 1126 /* 1127 * Given the address of a proc_t, return the p.p_model value; return 1128 * PR_MODEL_UNKNOWN if we were unable to read a proc structure or if 1129 * the model value does not match one of the two known values. 1130 */ 1131 uint_t 1132 mdb_kproc_model(uintptr_t proc_addr) 1133 { 1134 proc_t p; 1135 1136 if (mdb_vread(&p, sizeof (p), proc_addr) == sizeof (p)) { 1137 switch (p.p_model) { 1138 case DATAMODEL_ILP32: 1139 return (PR_MODEL_ILP32); 1140 case DATAMODEL_LP64: 1141 return (PR_MODEL_LP64); 1142 } 1143 } 1144 1145 return (PR_MODEL_UNKNOWN); 1146 } 1147 1148 /* 1149 * Callback function for walking process's segment list. For each segment, 1150 * we fill in an mdb_map_t describing its properties, and then invoke 1151 * the callback function provided by the kproc target. 1152 */ 1153 static int 1154 asmap_step(uintptr_t addr, const struct seg *seg, asmap_arg_t *asmp) 1155 { 1156 struct segvn_data svd; 1157 mdb_map_t map; 1158 1159 if (seg->s_ops == asmp->asm_segvn_ops && mdb_vread(&svd, 1160 sizeof (svd), (uintptr_t)seg->s_data) == sizeof (svd)) { 1161 1162 if (svd.vp != NULL) { 1163 if (mdb_vnode2path((uintptr_t)svd.vp, map.map_name, 1164 MDB_TGT_MAPSZ) != 0) { 1165 (void) mdb_snprintf(map.map_name, 1166 MDB_TGT_MAPSZ, "[ vnode %p ]", svd.vp); 1167 } 1168 } else 1169 (void) strcpy(map.map_name, "[ anon ]"); 1170 1171 } else { 1172 (void) mdb_snprintf(map.map_name, MDB_TGT_MAPSZ, 1173 "[ seg %p ]", addr); 1174 } 1175 1176 map.map_base = (uintptr_t)seg->s_base; 1177 map.map_size = seg->s_size; 1178 map.map_flags = 0; 1179 1180 asmp->asm_callback((const struct mdb_map *)&map, asmp->asm_cbdata); 1181 return (WALK_NEXT); 1182 } 1183 1184 /* 1185 * Given a process address space, walk its segment list using the seg walker, 1186 * convert the segment data to an mdb_map_t, and pass this information 1187 * back to the kproc target via the given callback function. 1188 */ 1189 int 1190 mdb_kproc_asiter(uintptr_t as, 1191 void (*func)(const struct mdb_map *, void *), void *p) 1192 { 1193 asmap_arg_t arg; 1194 GElf_Sym sym; 1195 1196 arg.asm_segvn_ops = NULL; 1197 arg.asm_callback = func; 1198 arg.asm_cbdata = p; 1199 1200 if (mdb_lookup_by_name("segvn_ops", &sym) == 0) 1201 arg.asm_segvn_ops = (struct seg_ops *)(uintptr_t)sym.st_value; 1202 1203 return (mdb_pwalk("seg", (mdb_walk_cb_t)asmap_step, &arg, as)); 1204 } 1205 1206 /* 1207 * Copy the auxv array from the given process's u-area into the provided 1208 * buffer. If the buffer is NULL, only return the size of the auxv array 1209 * so the caller knows how much space will be required. 1210 */ 1211 int 1212 mdb_kproc_auxv(uintptr_t proc, auxv_t *auxv) 1213 { 1214 if (auxv != NULL) { 1215 proc_t p; 1216 1217 if (mdb_vread(&p, sizeof (p), proc) != sizeof (p)) 1218 return (-1); 1219 1220 bcopy(p.p_user.u_auxv, auxv, 1221 sizeof (auxv_t) * __KERN_NAUXV_IMPL); 1222 } 1223 1224 return (__KERN_NAUXV_IMPL); 1225 } 1226 1227 /* 1228 * Given a process address, return the PID. 1229 */ 1230 pid_t 1231 mdb_kproc_pid(uintptr_t proc_addr) 1232 { 1233 struct pid pid; 1234 proc_t p; 1235 1236 if (mdb_vread(&p, sizeof (p), proc_addr) == sizeof (p) && 1237 mdb_vread(&pid, sizeof (pid), (uintptr_t)p.p_pidp) == sizeof (pid)) 1238 return (pid.pid_id); 1239 1240 return (-1); 1241 } 1242 1243 /* 1244 * Interface between the MDB kvm target and mdb_ks. The kvm target relies 1245 * on looking up and invoking these functions in mdb_ks so that dependencies 1246 * on the current kernel implementation are isolated in mdb_ks. 1247 */ 1248 1249 /* 1250 * Determine whether or not the thread that panicked the given kernel was a 1251 * kernel thread (panic_thread->t_procp == &p0). 1252 */ 1253 void 1254 mdb_dump_print_content(dumphdr_t *dh, pid_t content) 1255 { 1256 GElf_Sym sym; 1257 uintptr_t pt; 1258 uintptr_t procp; 1259 int expcont = 0; 1260 int actcont; 1261 1262 (void) mdb_readvar(&expcont, "dump_conflags"); 1263 actcont = dh->dump_flags & DF_CONTENT; 1264 1265 if (actcont == DF_ALL) { 1266 mdb_printf("dump content: all kernel and user pages\n"); 1267 return; 1268 } else if (actcont == DF_CURPROC) { 1269 mdb_printf("dump content: kernel pages and pages from " 1270 "PID %d", content); 1271 return; 1272 } 1273 1274 mdb_printf("dump content: kernel pages only\n"); 1275 if (!(expcont & DF_CURPROC)) 1276 return; 1277 1278 if (mdb_readvar(&pt, "panic_thread") != sizeof (pt) || pt == NULL) 1279 goto kthreadpanic_err; 1280 1281 if (mdb_vread(&procp, sizeof (procp), pt + OFFSETOF(kthread_t, 1282 t_procp)) == -1 || procp == NULL) 1283 goto kthreadpanic_err; 1284 1285 if (mdb_lookup_by_name("p0", &sym) != 0) 1286 goto kthreadpanic_err; 1287 1288 if (procp == (uintptr_t)sym.st_value) { 1289 mdb_printf(" (curproc requested, but a kernel thread " 1290 "panicked)\n"); 1291 } else { 1292 mdb_printf(" (curproc requested, but the process that " 1293 "panicked could not be dumped)\n"); 1294 } 1295 1296 return; 1297 1298 kthreadpanic_err: 1299 mdb_printf(" (curproc requested, but the process that panicked could " 1300 "not be found)\n"); 1301 } 1302 1303 /* 1304 * Determine the process that was saved in a `curproc' dump. This process will 1305 * be recorded as the first element in dump_pids[]. 1306 */ 1307 int 1308 mdb_dump_find_curproc(void) 1309 { 1310 uintptr_t pidp; 1311 pid_t pid = -1; 1312 1313 if (mdb_readvar(&pidp, "dump_pids") == sizeof (pidp) && 1314 mdb_vread(&pid, sizeof (pid), pidp) == sizeof (pid) && 1315 pid > 0) 1316 return (pid); 1317 else 1318 return (-1); 1319 } 1320 1321 1322 /* 1323 * Following three funcs extracted from sunddi.c 1324 */ 1325 1326 /* 1327 * Return core address of root node of devinfo tree 1328 */ 1329 static uintptr_t 1330 mdb_ddi_root_node(void) 1331 { 1332 uintptr_t top_devinfo_addr; 1333 1334 /* return (top_devinfo); */ 1335 if (mdb_readvar(&top_devinfo_addr, "top_devinfo") == -1) { 1336 mdb_warn("failed to read top_devinfo"); 1337 return (NULL); 1338 } 1339 return (top_devinfo_addr); 1340 } 1341 1342 /* 1343 * Return the name of the devinfo node pointed at by 'dip_addr' in the buffer 1344 * pointed at by 'name.' 1345 * 1346 * - dip_addr is a pointer to a dev_info struct in core. 1347 */ 1348 static char * 1349 mdb_ddi_deviname(uintptr_t dip_addr, char *name, size_t name_size) 1350 { 1351 uintptr_t addrname; 1352 ssize_t length; 1353 char *local_namep = name; 1354 size_t local_name_size = name_size; 1355 struct dev_info local_dip; 1356 1357 1358 if (dip_addr == mdb_ddi_root_node()) { 1359 if (name_size < 1) { 1360 mdb_warn("failed to get node name: buf too small\n"); 1361 return (NULL); 1362 } 1363 1364 *name = '\0'; 1365 return (name); 1366 } 1367 1368 if (name_size < 2) { 1369 mdb_warn("failed to get node name: buf too small\n"); 1370 return (NULL); 1371 } 1372 1373 local_namep = name; 1374 *local_namep++ = '/'; 1375 *local_namep = '\0'; 1376 local_name_size--; 1377 1378 if (mdb_vread(&local_dip, sizeof (struct dev_info), dip_addr) == -1) { 1379 mdb_warn("failed to read devinfo struct"); 1380 } 1381 1382 length = mdb_readstr(local_namep, local_name_size, 1383 (uintptr_t)local_dip.devi_node_name); 1384 if (length == -1) { 1385 mdb_warn("failed to read node name"); 1386 return (NULL); 1387 } 1388 local_namep += length; 1389 local_name_size -= length; 1390 addrname = (uintptr_t)local_dip.devi_addr; 1391 1392 if (addrname != NULL) { 1393 1394 if (local_name_size < 2) { 1395 mdb_warn("not enough room for node address string"); 1396 return (name); 1397 } 1398 *local_namep++ = '@'; 1399 *local_namep = '\0'; 1400 local_name_size--; 1401 1402 length = mdb_readstr(local_namep, local_name_size, addrname); 1403 if (length == -1) { 1404 mdb_warn("failed to read name"); 1405 return (NULL); 1406 } 1407 } 1408 1409 return (name); 1410 } 1411 1412 /* 1413 * Generate the full path under the /devices dir to the device entry. 1414 * 1415 * dip is a pointer to a devinfo struct in core (not in local memory). 1416 */ 1417 char * 1418 mdb_ddi_pathname(uintptr_t dip_addr, char *path, size_t pathlen) 1419 { 1420 struct dev_info local_dip; 1421 uintptr_t parent_dip; 1422 char *bp; 1423 size_t buf_left; 1424 1425 1426 if (dip_addr == mdb_ddi_root_node()) { 1427 *path = '\0'; 1428 return (path); 1429 } 1430 1431 1432 if (mdb_vread(&local_dip, sizeof (struct dev_info), dip_addr) == -1) { 1433 mdb_warn("failed to read devinfo struct"); 1434 } 1435 1436 parent_dip = (uintptr_t)local_dip.devi_parent; 1437 (void) mdb_ddi_pathname(parent_dip, path, pathlen); 1438 1439 bp = path + strlen(path); 1440 buf_left = pathlen - strlen(path); 1441 (void) mdb_ddi_deviname(dip_addr, bp, buf_left); 1442 return (path); 1443 } 1444 1445 1446 /* 1447 * Read in the string value of a refstr, which is appended to the end of 1448 * the structure. 1449 */ 1450 ssize_t 1451 mdb_read_refstr(uintptr_t refstr_addr, char *str, size_t nbytes) 1452 { 1453 struct refstr *r = (struct refstr *)refstr_addr; 1454 1455 return (mdb_readstr(str, nbytes, (uintptr_t)r->rs_string)); 1456 } 1457