1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 /* 26 * Copyright 2012 DEY Storage Systems, Inc. All rights reserved. 27 * Copyright (c) 2018, Joyent, Inc. All rights reserved. 28 * Copyright (c) 2013 by Delphix. All rights reserved. 29 * Copyright 2015 Gary Mills 30 * Copyright 2020 OmniOS Community Edition (OmniOSce) Association. 31 * Copyright 2024 Oxide Computer Company 32 * Copyright 2025 Edgecast Cloud LLC. 33 */ 34 35 #include <sys/types.h> 36 #include <sys/utsname.h> 37 #include <sys/sysmacros.h> 38 #include <sys/proc.h> 39 40 #include <alloca.h> 41 #include <rtld_db.h> 42 #include <libgen.h> 43 #include <limits.h> 44 #include <string.h> 45 #include <stdlib.h> 46 #include <unistd.h> 47 #include <errno.h> 48 #include <gelf.h> 49 #include <stddef.h> 50 #include <signal.h> 51 52 #include "libproc.h" 53 #include "Pcontrol.h" 54 #include "P32ton.h" 55 #include "Putil.h" 56 #include "proc_fd.h" 57 #ifdef __x86 58 #include "Pcore_linux.h" 59 #endif 60 61 /* 62 * Pcore.c - Code to initialize a ps_prochandle from a core dump. We 63 * allocate an additional structure to hold information from the core 64 * file, and attach this to the standard ps_prochandle in place of the 65 * ability to examine /proc/<pid>/ files. 66 */ 67 68 /* 69 * Basic i/o function for reading and writing from the process address space 70 * stored in the core file and associated shared libraries. We compute the 71 * appropriate fd and offsets, and let the provided prw function do the rest. 72 */ 73 static ssize_t 74 core_rw(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr, 75 ssize_t (*prw)(int, void *, size_t, off64_t)) 76 { 77 ssize_t resid = n; 78 79 while (resid != 0) { 80 map_info_t *mp = Paddr2mptr(P, addr); 81 82 uintptr_t mapoff; 83 ssize_t len; 84 off64_t off; 85 int fd; 86 87 if (mp == NULL) 88 break; /* No mapping for this address */ 89 90 if (mp->map_pmap.pr_mflags & MA_RESERVED1) { 91 if (mp->map_file == NULL || mp->map_file->file_fd < 0) 92 break; /* No file or file not open */ 93 94 fd = mp->map_file->file_fd; 95 } else 96 fd = P->asfd; 97 98 mapoff = addr - mp->map_pmap.pr_vaddr; 99 len = MIN(resid, mp->map_pmap.pr_size - mapoff); 100 off = mp->map_offset + mapoff; 101 102 if ((len = prw(fd, buf, len, off)) <= 0) 103 break; 104 105 resid -= len; 106 addr += len; 107 buf = (char *)buf + len; 108 } 109 110 /* 111 * Important: Be consistent with the behavior of i/o on the as file: 112 * writing to an invalid address yields EIO; reading from an invalid 113 * address falls through to returning success and zero bytes. 114 */ 115 if (resid == n && n != 0 && prw != pread64) { 116 errno = EIO; 117 return (-1); 118 } 119 120 return (n - resid); 121 } 122 123 static ssize_t 124 Pread_core(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr, 125 void *data) 126 { 127 return (core_rw(P, buf, n, addr, pread64)); 128 } 129 130 static ssize_t 131 Pwrite_core(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr, 132 void *data) 133 { 134 return (core_rw(P, (void *)buf, n, addr, 135 (ssize_t (*)(int, void *, size_t, off64_t)) pwrite64)); 136 } 137 138 static int 139 Pcred_core(struct ps_prochandle *P, prcred_t *pcrp, int ngroups, void *data) 140 { 141 core_info_t *core = data; 142 143 if (core->core_cred != NULL) { 144 /* 145 * Avoid returning more supplementary group data than the 146 * caller has allocated in their buffer. We expect them to 147 * check pr_ngroups afterward and potentially call us again. 148 */ 149 ngroups = MIN(ngroups, core->core_cred->pr_ngroups); 150 151 (void) memcpy(pcrp, core->core_cred, 152 sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t)); 153 154 return (0); 155 } 156 157 errno = ENODATA; 158 return (-1); 159 } 160 161 static int 162 Psecflags_core(struct ps_prochandle *P, prsecflags_t **psf, void *data) 163 { 164 core_info_t *core = data; 165 166 if (core->core_secflags == NULL) { 167 errno = ENODATA; 168 return (-1); 169 } 170 171 if ((*psf = calloc(1, sizeof (prsecflags_t))) == NULL) 172 return (-1); 173 174 (void) memcpy(*psf, core->core_secflags, sizeof (prsecflags_t)); 175 176 return (0); 177 } 178 179 static int 180 Ppriv_core(struct ps_prochandle *P, prpriv_t **pprv, void *data) 181 { 182 core_info_t *core = data; 183 184 if (core->core_priv == NULL) { 185 errno = ENODATA; 186 return (-1); 187 } 188 189 *pprv = malloc(core->core_priv_size); 190 if (*pprv == NULL) { 191 return (-1); 192 } 193 194 (void) memcpy(*pprv, core->core_priv, core->core_priv_size); 195 return (0); 196 } 197 198 static const psinfo_t * 199 Ppsinfo_core(struct ps_prochandle *P, psinfo_t *psinfo, void *data) 200 { 201 return (&P->psinfo); 202 } 203 204 static void 205 Pfini_core(struct ps_prochandle *P, void *data) 206 { 207 core_info_t *core = data; 208 209 if (core != NULL) { 210 extern void __priv_free_info(void *); 211 lwp_info_t *lwp; 212 213 while ((lwp = list_remove_head(&core->core_lwp_head)) != NULL) { 214 #ifdef __sparc 215 if (lwp->lwp_gwins != NULL) 216 free(lwp->lwp_gwins); 217 if (lwp->lwp_xregs != NULL) 218 free(lwp->lwp_xregs); 219 if (lwp->lwp_asrs != NULL) 220 free(lwp->lwp_asrs); 221 #endif 222 free(lwp); 223 } 224 225 if (core->core_platform != NULL) 226 free(core->core_platform); 227 if (core->core_uts != NULL) 228 free(core->core_uts); 229 if (core->core_cred != NULL) 230 free(core->core_cred); 231 if (core->core_priv != NULL) 232 free(core->core_priv); 233 if (core->core_privinfo != NULL) 234 __priv_free_info(core->core_privinfo); 235 if (core->core_ppii != NULL) 236 free(core->core_ppii); 237 if (core->core_zonename != NULL) 238 free(core->core_zonename); 239 if (core->core_secflags != NULL) 240 free(core->core_secflags); 241 if (core->core_upanic != NULL) 242 free(core->core_upanic); 243 if (core->core_cwd != NULL) 244 free(core->core_cwd); 245 #ifdef __x86 246 if (core->core_ldt != NULL) 247 free(core->core_ldt); 248 #endif 249 250 free(core); 251 } 252 } 253 254 static char * 255 Pplatform_core(struct ps_prochandle *P, char *s, size_t n, void *data) 256 { 257 core_info_t *core = data; 258 259 if (core->core_platform == NULL) { 260 errno = ENODATA; 261 return (NULL); 262 } 263 (void) strncpy(s, core->core_platform, n - 1); 264 s[n - 1] = '\0'; 265 return (s); 266 } 267 268 static int 269 Puname_core(struct ps_prochandle *P, struct utsname *u, void *data) 270 { 271 core_info_t *core = data; 272 273 if (core->core_uts == NULL) { 274 errno = ENODATA; 275 return (-1); 276 } 277 (void) memcpy(u, core->core_uts, sizeof (struct utsname)); 278 return (0); 279 } 280 281 static char * 282 Pzonename_core(struct ps_prochandle *P, char *s, size_t n, void *data) 283 { 284 core_info_t *core = data; 285 286 if (core->core_zonename == NULL) { 287 errno = ENODATA; 288 return (NULL); 289 } 290 (void) strlcpy(s, core->core_zonename, n); 291 return (s); 292 } 293 294 static int 295 Pcwd_core(struct ps_prochandle *P, prcwd_t **cwdp, void *data) 296 { 297 prcwd_t *cwd; 298 core_info_t *core = data; 299 300 if (core->core_cwd == NULL) { 301 errno = ENODATA; 302 return (-1); 303 } 304 305 if ((cwd = calloc(1, sizeof (prcwd_t))) == NULL) 306 return (-1); 307 308 (void) memcpy(cwd, core->core_cwd, sizeof (prcwd_t)); 309 cwd->prcwd_fsname[sizeof (cwd->prcwd_fsname) - 1] = '\0'; 310 cwd->prcwd_mntpt[sizeof (cwd->prcwd_mntpt) - 1] = '\0'; 311 cwd->prcwd_mntspec[sizeof (cwd->prcwd_mntpt) - 1] = '\0'; 312 cwd->prcwd_cwd[sizeof (cwd->prcwd_mntpt) - 1] = '\0'; 313 *cwdp = cwd; 314 315 return (0); 316 } 317 318 #ifdef __x86 319 static int 320 Pldt_core(struct ps_prochandle *P, struct ssd *pldt, int nldt, void *data) 321 { 322 core_info_t *core = data; 323 324 if (pldt == NULL || nldt == 0) 325 return (core->core_nldt); 326 327 if (core->core_ldt != NULL) { 328 nldt = MIN(nldt, core->core_nldt); 329 330 (void) memcpy(pldt, core->core_ldt, 331 nldt * sizeof (struct ssd)); 332 333 return (nldt); 334 } 335 336 errno = ENODATA; 337 return (-1); 338 } 339 #endif 340 341 static const ps_ops_t P_core_ops = { 342 .pop_pread = Pread_core, 343 .pop_pwrite = Pwrite_core, 344 .pop_cred = Pcred_core, 345 .pop_priv = Ppriv_core, 346 .pop_psinfo = Ppsinfo_core, 347 .pop_fini = Pfini_core, 348 .pop_platform = Pplatform_core, 349 .pop_uname = Puname_core, 350 .pop_zonename = Pzonename_core, 351 .pop_secflags = Psecflags_core, 352 .pop_cwd = Pcwd_core, 353 #ifdef __x86 354 .pop_ldt = Pldt_core 355 #endif 356 }; 357 358 /* 359 * Return the lwp_info_t for the given lwpid. If no such lwpid has been 360 * encountered yet, allocate a new structure and return a pointer to it. 361 * Create a list of lwp_info_t structures sorted in decreasing lwp_id order. 362 */ 363 static lwp_info_t * 364 lwpid2info(struct ps_prochandle *P, lwpid_t id) 365 { 366 core_info_t *core = P->data; 367 lwp_info_t *lwp, *prev; 368 369 for (lwp = list_head(&core->core_lwp_head); lwp != NULL; 370 lwp = list_next(&core->core_lwp_head, lwp)) { 371 if (lwp->lwp_id == id) { 372 core->core_lwp = lwp; 373 return (lwp); 374 } 375 if (lwp->lwp_id < id) { 376 break; 377 } 378 } 379 380 prev = lwp; 381 if ((lwp = calloc(1, sizeof (lwp_info_t))) == NULL) 382 return (NULL); 383 384 list_insert_before(&core->core_lwp_head, prev, lwp); 385 lwp->lwp_id = id; 386 387 core->core_lwp = lwp; 388 389 return (lwp); 390 } 391 392 /* 393 * The core file itself contains a series of NOTE segments containing saved 394 * structures from /proc at the time the process died. For each note we 395 * comprehend, we define a function to read it in from the core file, 396 * convert it to our native data model if necessary, and store it inside 397 * the ps_prochandle. Each function is invoked by Pfgrab_core() with the 398 * seek pointer on P->asfd positioned appropriately. We populate a table 399 * of pointers to these note functions below. 400 */ 401 402 static int 403 note_pstatus(struct ps_prochandle *P, size_t nbytes) 404 { 405 #ifdef _LP64 406 core_info_t *core = P->data; 407 408 if (core->core_dmodel == PR_MODEL_ILP32) { 409 pstatus32_t ps32; 410 411 if (nbytes < sizeof (pstatus32_t) || 412 read(P->asfd, &ps32, sizeof (ps32)) != sizeof (ps32)) 413 goto err; 414 415 pstatus_32_to_n(&ps32, &P->status); 416 417 } else 418 #endif 419 if (nbytes < sizeof (pstatus_t) || 420 read(P->asfd, &P->status, sizeof (pstatus_t)) != sizeof (pstatus_t)) 421 goto err; 422 423 P->orig_status = P->status; 424 P->pid = P->status.pr_pid; 425 426 return (0); 427 428 err: 429 Pdprintf("Pgrab_core: failed to read NT_PSTATUS\n"); 430 return (-1); 431 } 432 433 static int 434 note_lwpstatus(struct ps_prochandle *P, size_t nbytes) 435 { 436 lwp_info_t *lwp; 437 lwpstatus_t lps; 438 439 #ifdef _LP64 440 core_info_t *core = P->data; 441 442 if (core->core_dmodel == PR_MODEL_ILP32) { 443 lwpstatus32_t l32; 444 445 if (nbytes < sizeof (lwpstatus32_t) || 446 read(P->asfd, &l32, sizeof (l32)) != sizeof (l32)) 447 goto err; 448 449 lwpstatus_32_to_n(&l32, &lps); 450 } else 451 #endif 452 if (nbytes < sizeof (lwpstatus_t) || 453 read(P->asfd, &lps, sizeof (lps)) != sizeof (lps)) 454 goto err; 455 456 if ((lwp = lwpid2info(P, lps.pr_lwpid)) == NULL) { 457 Pdprintf("Pgrab_core: failed to add NT_LWPSTATUS\n"); 458 return (-1); 459 } 460 461 /* 462 * Erase a useless and confusing artifact of the kernel implementation: 463 * the lwps which did *not* create the core will show SIGKILL. We can 464 * be assured this is bogus because SIGKILL can't produce core files. 465 */ 466 if (lps.pr_cursig == SIGKILL) 467 lps.pr_cursig = 0; 468 469 (void) memcpy(&lwp->lwp_status, &lps, sizeof (lps)); 470 return (0); 471 472 err: 473 Pdprintf("Pgrab_core: failed to read NT_LWPSTATUS\n"); 474 return (-1); 475 } 476 477 #ifdef __x86 478 479 static void 480 lx_prpsinfo32_to_psinfo(lx_prpsinfo32_t *p32, psinfo_t *psinfo) 481 { 482 psinfo->pr_flag = p32->pr_flag; 483 psinfo->pr_pid = p32->pr_pid; 484 psinfo->pr_ppid = p32->pr_ppid; 485 psinfo->pr_uid = p32->pr_uid; 486 psinfo->pr_gid = p32->pr_gid; 487 psinfo->pr_sid = p32->pr_sid; 488 psinfo->pr_pgid = p32->pr_pgrp; 489 490 (void) memcpy(psinfo->pr_fname, p32->pr_fname, 491 sizeof (psinfo->pr_fname)); 492 (void) memcpy(psinfo->pr_psargs, p32->pr_psargs, 493 sizeof (psinfo->pr_psargs)); 494 } 495 496 static void 497 lx_prpsinfo64_to_psinfo(lx_prpsinfo64_t *p64, psinfo_t *psinfo) 498 { 499 psinfo->pr_flag = p64->pr_flag; 500 psinfo->pr_pid = p64->pr_pid; 501 psinfo->pr_ppid = p64->pr_ppid; 502 psinfo->pr_uid = p64->pr_uid; 503 psinfo->pr_gid = p64->pr_gid; 504 psinfo->pr_sid = p64->pr_sid; 505 psinfo->pr_pgid = p64->pr_pgrp; 506 psinfo->pr_pgid = p64->pr_pgrp; 507 508 (void) memcpy(psinfo->pr_fname, p64->pr_fname, 509 sizeof (psinfo->pr_fname)); 510 (void) memcpy(psinfo->pr_psargs, p64->pr_psargs, 511 sizeof (psinfo->pr_psargs)); 512 } 513 514 static int 515 note_linux_psinfo(struct ps_prochandle *P, size_t nbytes) 516 { 517 core_info_t *core = P->data; 518 lx_prpsinfo32_t p32; 519 lx_prpsinfo64_t p64; 520 521 if (core->core_dmodel == PR_MODEL_ILP32) { 522 if (nbytes < sizeof (p32) || 523 read(P->asfd, &p32, sizeof (p32)) != sizeof (p32)) 524 goto err; 525 526 lx_prpsinfo32_to_psinfo(&p32, &P->psinfo); 527 } else { 528 if (nbytes < sizeof (p64) || 529 read(P->asfd, &p64, sizeof (p64)) != sizeof (p64)) 530 goto err; 531 532 lx_prpsinfo64_to_psinfo(&p64, &P->psinfo); 533 } 534 535 536 P->status.pr_pid = P->psinfo.pr_pid; 537 P->status.pr_ppid = P->psinfo.pr_ppid; 538 P->status.pr_pgid = P->psinfo.pr_pgid; 539 P->status.pr_sid = P->psinfo.pr_sid; 540 541 P->psinfo.pr_nlwp = 0; 542 P->status.pr_nlwp = 0; 543 544 return (0); 545 err: 546 Pdprintf("Pgrab_core: failed to read NT_PSINFO\n"); 547 return (-1); 548 } 549 550 static void 551 lx_prstatus64_to_lwp(lx_prstatus64_t *prs64, lwp_info_t *lwp) 552 { 553 LTIME_TO_TIMESPEC(lwp->lwp_status.pr_utime, prs64->pr_utime); 554 LTIME_TO_TIMESPEC(lwp->lwp_status.pr_stime, prs64->pr_stime); 555 556 lwp->lwp_status.pr_reg[REG_R15] = prs64->pr_reg.lxr_r15; 557 lwp->lwp_status.pr_reg[REG_R14] = prs64->pr_reg.lxr_r14; 558 lwp->lwp_status.pr_reg[REG_R13] = prs64->pr_reg.lxr_r13; 559 lwp->lwp_status.pr_reg[REG_R12] = prs64->pr_reg.lxr_r12; 560 lwp->lwp_status.pr_reg[REG_R11] = prs64->pr_reg.lxr_r11; 561 lwp->lwp_status.pr_reg[REG_R10] = prs64->pr_reg.lxr_r10; 562 lwp->lwp_status.pr_reg[REG_R9] = prs64->pr_reg.lxr_r9; 563 lwp->lwp_status.pr_reg[REG_R8] = prs64->pr_reg.lxr_r8; 564 565 lwp->lwp_status.pr_reg[REG_RDI] = prs64->pr_reg.lxr_rdi; 566 lwp->lwp_status.pr_reg[REG_RSI] = prs64->pr_reg.lxr_rsi; 567 lwp->lwp_status.pr_reg[REG_RBP] = prs64->pr_reg.lxr_rbp; 568 lwp->lwp_status.pr_reg[REG_RBX] = prs64->pr_reg.lxr_rbx; 569 lwp->lwp_status.pr_reg[REG_RDX] = prs64->pr_reg.lxr_rdx; 570 lwp->lwp_status.pr_reg[REG_RCX] = prs64->pr_reg.lxr_rcx; 571 lwp->lwp_status.pr_reg[REG_RAX] = prs64->pr_reg.lxr_rax; 572 573 lwp->lwp_status.pr_reg[REG_RIP] = prs64->pr_reg.lxr_rip; 574 lwp->lwp_status.pr_reg[REG_CS] = prs64->pr_reg.lxr_cs; 575 lwp->lwp_status.pr_reg[REG_RSP] = prs64->pr_reg.lxr_rsp; 576 lwp->lwp_status.pr_reg[REG_FS] = prs64->pr_reg.lxr_fs; 577 lwp->lwp_status.pr_reg[REG_SS] = prs64->pr_reg.lxr_ss; 578 lwp->lwp_status.pr_reg[REG_GS] = prs64->pr_reg.lxr_gs; 579 lwp->lwp_status.pr_reg[REG_ES] = prs64->pr_reg.lxr_es; 580 lwp->lwp_status.pr_reg[REG_DS] = prs64->pr_reg.lxr_ds; 581 582 lwp->lwp_status.pr_reg[REG_GSBASE] = prs64->pr_reg.lxr_gs_base; 583 lwp->lwp_status.pr_reg[REG_FSBASE] = prs64->pr_reg.lxr_fs_base; 584 } 585 586 static void 587 lx_prstatus32_to_lwp(lx_prstatus32_t *prs32, lwp_info_t *lwp) 588 { 589 LTIME_TO_TIMESPEC(lwp->lwp_status.pr_utime, prs32->pr_utime); 590 LTIME_TO_TIMESPEC(lwp->lwp_status.pr_stime, prs32->pr_stime); 591 592 #ifdef __amd64 593 lwp->lwp_status.pr_reg[REG_GS] = prs32->pr_reg.lxr_gs; 594 lwp->lwp_status.pr_reg[REG_FS] = prs32->pr_reg.lxr_fs; 595 lwp->lwp_status.pr_reg[REG_DS] = prs32->pr_reg.lxr_ds; 596 lwp->lwp_status.pr_reg[REG_ES] = prs32->pr_reg.lxr_es; 597 lwp->lwp_status.pr_reg[REG_RDI] = prs32->pr_reg.lxr_di; 598 lwp->lwp_status.pr_reg[REG_RSI] = prs32->pr_reg.lxr_si; 599 lwp->lwp_status.pr_reg[REG_RBP] = prs32->pr_reg.lxr_bp; 600 lwp->lwp_status.pr_reg[REG_RBX] = prs32->pr_reg.lxr_bx; 601 lwp->lwp_status.pr_reg[REG_RDX] = prs32->pr_reg.lxr_dx; 602 lwp->lwp_status.pr_reg[REG_RCX] = prs32->pr_reg.lxr_cx; 603 lwp->lwp_status.pr_reg[REG_RAX] = prs32->pr_reg.lxr_ax; 604 lwp->lwp_status.pr_reg[REG_RIP] = prs32->pr_reg.lxr_ip; 605 lwp->lwp_status.pr_reg[REG_CS] = prs32->pr_reg.lxr_cs; 606 lwp->lwp_status.pr_reg[REG_RFL] = prs32->pr_reg.lxr_flags; 607 lwp->lwp_status.pr_reg[REG_RSP] = prs32->pr_reg.lxr_sp; 608 lwp->lwp_status.pr_reg[REG_SS] = prs32->pr_reg.lxr_ss; 609 #else /* __amd64 */ 610 lwp->lwp_status.pr_reg[EBX] = prs32->pr_reg.lxr_bx; 611 lwp->lwp_status.pr_reg[ECX] = prs32->pr_reg.lxr_cx; 612 lwp->lwp_status.pr_reg[EDX] = prs32->pr_reg.lxr_dx; 613 lwp->lwp_status.pr_reg[ESI] = prs32->pr_reg.lxr_si; 614 lwp->lwp_status.pr_reg[EDI] = prs32->pr_reg.lxr_di; 615 lwp->lwp_status.pr_reg[EBP] = prs32->pr_reg.lxr_bp; 616 lwp->lwp_status.pr_reg[EAX] = prs32->pr_reg.lxr_ax; 617 lwp->lwp_status.pr_reg[EIP] = prs32->pr_reg.lxr_ip; 618 lwp->lwp_status.pr_reg[UESP] = prs32->pr_reg.lxr_sp; 619 620 lwp->lwp_status.pr_reg[DS] = prs32->pr_reg.lxr_ds; 621 lwp->lwp_status.pr_reg[ES] = prs32->pr_reg.lxr_es; 622 lwp->lwp_status.pr_reg[FS] = prs32->pr_reg.lxr_fs; 623 lwp->lwp_status.pr_reg[GS] = prs32->pr_reg.lxr_gs; 624 lwp->lwp_status.pr_reg[CS] = prs32->pr_reg.lxr_cs; 625 lwp->lwp_status.pr_reg[SS] = prs32->pr_reg.lxr_ss; 626 627 lwp->lwp_status.pr_reg[EFL] = prs32->pr_reg.lxr_flags; 628 #endif /* !__amd64 */ 629 } 630 631 static int 632 note_linux_prstatus(struct ps_prochandle *P, size_t nbytes) 633 { 634 core_info_t *core = P->data; 635 636 lx_prstatus64_t prs64; 637 lx_prstatus32_t prs32; 638 lwp_info_t *lwp; 639 lwpid_t tid; 640 641 Pdprintf("looking for model %d, %ld/%ld\n", core->core_dmodel, 642 (ulong_t)nbytes, (ulong_t)sizeof (prs32)); 643 if (core->core_dmodel == PR_MODEL_ILP32) { 644 if (nbytes < sizeof (prs32) || 645 read(P->asfd, &prs32, sizeof (prs32)) != nbytes) 646 goto err; 647 tid = prs32.pr_pid; 648 } else { 649 if (nbytes < sizeof (prs64) || 650 read(P->asfd, &prs64, sizeof (prs64)) != nbytes) 651 goto err; 652 tid = prs64.pr_pid; 653 } 654 655 if ((lwp = lwpid2info(P, tid)) == NULL) { 656 Pdprintf("Pgrab_core: failed to add lwpid2info " 657 "linux_prstatus\n"); 658 return (-1); 659 } 660 661 P->psinfo.pr_nlwp++; 662 P->status.pr_nlwp++; 663 664 lwp->lwp_status.pr_lwpid = tid; 665 666 if (core->core_dmodel == PR_MODEL_ILP32) 667 lx_prstatus32_to_lwp(&prs32, lwp); 668 else 669 lx_prstatus64_to_lwp(&prs64, lwp); 670 671 return (0); 672 err: 673 Pdprintf("Pgrab_core: failed to read NT_PRSTATUS\n"); 674 return (-1); 675 } 676 677 #endif /* __x86 */ 678 679 static int 680 note_psinfo(struct ps_prochandle *P, size_t nbytes) 681 { 682 #ifdef _LP64 683 core_info_t *core = P->data; 684 685 if (core->core_dmodel == PR_MODEL_ILP32) { 686 psinfo32_t ps32; 687 688 if (nbytes < sizeof (psinfo32_t) || 689 read(P->asfd, &ps32, sizeof (ps32)) != sizeof (ps32)) 690 goto err; 691 692 psinfo_32_to_n(&ps32, &P->psinfo); 693 } else 694 #endif 695 if (nbytes < sizeof (psinfo_t) || 696 read(P->asfd, &P->psinfo, sizeof (psinfo_t)) != sizeof (psinfo_t)) 697 goto err; 698 699 Pdprintf("pr_fname = <%s>\n", P->psinfo.pr_fname); 700 Pdprintf("pr_psargs = <%s>\n", P->psinfo.pr_psargs); 701 Pdprintf("pr_wstat = 0x%x\n", P->psinfo.pr_wstat); 702 703 return (0); 704 705 err: 706 Pdprintf("Pgrab_core: failed to read NT_PSINFO\n"); 707 return (-1); 708 } 709 710 static int 711 note_lwpsinfo(struct ps_prochandle *P, size_t nbytes) 712 { 713 lwp_info_t *lwp; 714 lwpsinfo_t lps; 715 716 #ifdef _LP64 717 core_info_t *core = P->data; 718 719 if (core->core_dmodel == PR_MODEL_ILP32) { 720 lwpsinfo32_t l32; 721 722 if (nbytes < sizeof (lwpsinfo32_t) || 723 read(P->asfd, &l32, sizeof (l32)) != sizeof (l32)) 724 goto err; 725 726 lwpsinfo_32_to_n(&l32, &lps); 727 } else 728 #endif 729 if (nbytes < sizeof (lwpsinfo_t) || 730 read(P->asfd, &lps, sizeof (lps)) != sizeof (lps)) 731 goto err; 732 733 if ((lwp = lwpid2info(P, lps.pr_lwpid)) == NULL) { 734 Pdprintf("Pgrab_core: failed to add NT_LWPSINFO\n"); 735 return (-1); 736 } 737 738 (void) memcpy(&lwp->lwp_psinfo, &lps, sizeof (lps)); 739 return (0); 740 741 err: 742 Pdprintf("Pgrab_core: failed to read NT_LWPSINFO\n"); 743 return (-1); 744 } 745 746 static int 747 note_lwpname(struct ps_prochandle *P, size_t nbytes) 748 { 749 prlwpname_t name; 750 lwp_info_t *lwp; 751 752 if (nbytes != sizeof (name) || 753 read(P->asfd, &name, sizeof (name)) != sizeof (name)) 754 goto err; 755 756 if ((lwp = lwpid2info(P, name.pr_lwpid)) == NULL) 757 goto err; 758 759 if (strlcpy(lwp->lwp_name, name.pr_lwpname, 760 sizeof (lwp->lwp_name)) >= sizeof (lwp->lwp_name)) { 761 errno = ENAMETOOLONG; 762 goto err; 763 } 764 765 return (0); 766 767 err: 768 Pdprintf("Pgrab_core: failed to read NT_LWPNAME\n"); 769 return (-1); 770 } 771 772 static int 773 note_fdinfo(struct ps_prochandle *P, size_t nbytes) 774 { 775 prfdinfo_core_t prfd; 776 fd_info_t *fip; 777 778 if ((nbytes < sizeof (prfd)) || 779 (read(P->asfd, &prfd, sizeof (prfd)) != sizeof (prfd))) { 780 Pdprintf("Pgrab_core: failed to read NT_FDINFO\n"); 781 return (-1); 782 } 783 784 if ((fip = Pfd2info(P, prfd.pr_fd)) == NULL) { 785 Pdprintf("Pgrab_core: failed to add NT_FDINFO\n"); 786 return (-1); 787 } 788 if (fip->fd_info == NULL) { 789 if (proc_fdinfo_from_core(&prfd, &fip->fd_info) != 0) { 790 Pdprintf("Pgrab_core: failed to convert NT_FDINFO\n"); 791 return (-1); 792 } 793 } 794 795 return (0); 796 } 797 798 static int 799 note_platform(struct ps_prochandle *P, size_t nbytes) 800 { 801 core_info_t *core = P->data; 802 char *plat; 803 804 if (core->core_platform != NULL) 805 return (0); /* Already seen */ 806 807 if (nbytes != 0 && ((plat = malloc(nbytes + 1)) != NULL)) { 808 if (read(P->asfd, plat, nbytes) != nbytes) { 809 Pdprintf("Pgrab_core: failed to read NT_PLATFORM\n"); 810 free(plat); 811 return (-1); 812 } 813 plat[nbytes - 1] = '\0'; 814 core->core_platform = plat; 815 } 816 817 return (0); 818 } 819 820 static int 821 note_secflags(struct ps_prochandle *P, size_t nbytes) 822 { 823 core_info_t *core = P->data; 824 prsecflags_t *psf; 825 826 if (core->core_secflags != NULL) 827 return (0); /* Already seen */ 828 829 if (sizeof (*psf) != nbytes) { 830 Pdprintf("Pgrab_core: NT_SECFLAGS changed size." 831 " Need to handle a version change?\n"); 832 return (-1); 833 } 834 835 if (nbytes != 0 && ((psf = malloc(nbytes)) != NULL)) { 836 if (read(P->asfd, psf, nbytes) != nbytes) { 837 Pdprintf("Pgrab_core: failed to read NT_SECFLAGS\n"); 838 free(psf); 839 return (-1); 840 } 841 842 core->core_secflags = psf; 843 } 844 845 return (0); 846 } 847 848 static int 849 note_utsname(struct ps_prochandle *P, size_t nbytes) 850 { 851 core_info_t *core = P->data; 852 size_t ubytes = sizeof (struct utsname); 853 struct utsname *utsp; 854 855 if (core->core_uts != NULL || nbytes < ubytes) 856 return (0); /* Already seen or bad size */ 857 858 if ((utsp = malloc(ubytes)) == NULL) 859 return (-1); 860 861 if (read(P->asfd, utsp, ubytes) != ubytes) { 862 Pdprintf("Pgrab_core: failed to read NT_UTSNAME\n"); 863 free(utsp); 864 return (-1); 865 } 866 867 if (_libproc_debug) { 868 Pdprintf("uts.sysname = \"%s\"\n", utsp->sysname); 869 Pdprintf("uts.nodename = \"%s\"\n", utsp->nodename); 870 Pdprintf("uts.release = \"%s\"\n", utsp->release); 871 Pdprintf("uts.version = \"%s\"\n", utsp->version); 872 Pdprintf("uts.machine = \"%s\"\n", utsp->machine); 873 } 874 875 core->core_uts = utsp; 876 return (0); 877 } 878 879 static int 880 note_content(struct ps_prochandle *P, size_t nbytes) 881 { 882 core_info_t *core = P->data; 883 core_content_t content; 884 885 if (sizeof (core->core_content) != nbytes) 886 return (-1); 887 888 if (read(P->asfd, &content, sizeof (content)) != sizeof (content)) 889 return (-1); 890 891 core->core_content = content; 892 893 Pdprintf("core content = %llx\n", content); 894 895 return (0); 896 } 897 898 static int 899 note_cred(struct ps_prochandle *P, size_t nbytes) 900 { 901 core_info_t *core = P->data; 902 prcred_t *pcrp; 903 int ngroups; 904 const size_t min_size = sizeof (prcred_t) - sizeof (gid_t); 905 906 /* 907 * We allow for prcred_t notes that are actually smaller than a 908 * prcred_t since the last member isn't essential if there are 909 * no group memberships. This allows for more flexibility when it 910 * comes to slightly malformed -- but still valid -- notes. 911 */ 912 if (core->core_cred != NULL || nbytes < min_size) 913 return (0); /* Already seen or bad size */ 914 915 ngroups = (nbytes - min_size) / sizeof (gid_t); 916 nbytes = sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t); 917 918 if ((pcrp = malloc(nbytes)) == NULL) 919 return (-1); 920 921 if (read(P->asfd, pcrp, nbytes) != nbytes) { 922 Pdprintf("Pgrab_core: failed to read NT_PRCRED\n"); 923 free(pcrp); 924 return (-1); 925 } 926 927 if (pcrp->pr_ngroups > ngroups) { 928 Pdprintf( 929 "pr_ngroups = %d; resetting to %d based on note size\n", 930 pcrp->pr_ngroups, ngroups); 931 pcrp->pr_ngroups = ngroups; 932 } 933 934 core->core_cred = pcrp; 935 return (0); 936 } 937 938 #ifdef __x86 939 static int 940 note_ldt(struct ps_prochandle *P, size_t nbytes) 941 { 942 core_info_t *core = P->data; 943 struct ssd *pldt; 944 uint_t nldt; 945 946 if (core->core_ldt != NULL || nbytes < sizeof (struct ssd)) 947 return (0); /* Already seen or bad size */ 948 949 nldt = nbytes / sizeof (struct ssd); 950 nbytes = nldt * sizeof (struct ssd); 951 952 if ((pldt = malloc(nbytes)) == NULL) 953 return (-1); 954 955 if (read(P->asfd, pldt, nbytes) != nbytes) { 956 Pdprintf("Pgrab_core: failed to read NT_LDT\n"); 957 free(pldt); 958 return (-1); 959 } 960 961 core->core_ldt = pldt; 962 core->core_nldt = nldt; 963 return (0); 964 } 965 #endif /* __i386 */ 966 967 static int 968 note_priv(struct ps_prochandle *P, size_t nbytes) 969 { 970 core_info_t *core = P->data; 971 prpriv_t *pprvp; 972 973 if (core->core_priv != NULL || nbytes < sizeof (prpriv_t)) 974 return (0); /* Already seen or bad size */ 975 976 if ((pprvp = malloc(nbytes)) == NULL) 977 return (-1); 978 979 if (read(P->asfd, pprvp, nbytes) != nbytes) { 980 Pdprintf("Pgrab_core: failed to read NT_PRPRIV\n"); 981 free(pprvp); 982 return (-1); 983 } 984 985 core->core_priv = pprvp; 986 core->core_priv_size = nbytes; 987 return (0); 988 } 989 990 static int 991 note_priv_info(struct ps_prochandle *P, size_t nbytes) 992 { 993 core_info_t *core = P->data; 994 extern void *__priv_parse_info(); 995 priv_impl_info_t *ppii; 996 997 if (core->core_privinfo != NULL || 998 nbytes < sizeof (priv_impl_info_t)) 999 return (0); /* Already seen or bad size */ 1000 1001 if ((ppii = malloc(nbytes)) == NULL) 1002 return (-1); 1003 1004 if (read(P->asfd, ppii, nbytes) != nbytes || 1005 PRIV_IMPL_INFO_SIZE(ppii) != nbytes) { 1006 Pdprintf("Pgrab_core: failed to read NT_PRPRIVINFO\n"); 1007 free(ppii); 1008 return (-1); 1009 } 1010 1011 core->core_privinfo = __priv_parse_info(ppii); 1012 core->core_ppii = ppii; 1013 return (0); 1014 } 1015 1016 static int 1017 note_zonename(struct ps_prochandle *P, size_t nbytes) 1018 { 1019 core_info_t *core = P->data; 1020 char *zonename; 1021 1022 if (core->core_zonename != NULL) 1023 return (0); /* Already seen */ 1024 1025 if (nbytes != 0) { 1026 if ((zonename = malloc(nbytes)) == NULL) 1027 return (-1); 1028 if (read(P->asfd, zonename, nbytes) != nbytes) { 1029 Pdprintf("Pgrab_core: failed to read NT_ZONENAME\n"); 1030 free(zonename); 1031 return (-1); 1032 } 1033 zonename[nbytes - 1] = '\0'; 1034 core->core_zonename = zonename; 1035 } 1036 1037 return (0); 1038 } 1039 1040 static int 1041 note_auxv(struct ps_prochandle *P, size_t nbytes) 1042 { 1043 size_t n, i; 1044 1045 #ifdef _LP64 1046 core_info_t *core = P->data; 1047 1048 if (core->core_dmodel == PR_MODEL_ILP32) { 1049 auxv32_t *a32; 1050 1051 n = nbytes / sizeof (auxv32_t); 1052 nbytes = n * sizeof (auxv32_t); 1053 a32 = alloca(nbytes); 1054 1055 if (read(P->asfd, a32, nbytes) != nbytes) { 1056 Pdprintf("Pgrab_core: failed to read NT_AUXV\n"); 1057 return (-1); 1058 } 1059 1060 if ((P->auxv = malloc(sizeof (auxv_t) * (n + 1))) == NULL) 1061 return (-1); 1062 1063 for (i = 0; i < n; i++) 1064 auxv_32_to_n(&a32[i], &P->auxv[i]); 1065 1066 } else { 1067 #endif 1068 n = nbytes / sizeof (auxv_t); 1069 nbytes = n * sizeof (auxv_t); 1070 1071 if ((P->auxv = malloc(nbytes + sizeof (auxv_t))) == NULL) 1072 return (-1); 1073 1074 if (read(P->asfd, P->auxv, nbytes) != nbytes) { 1075 free(P->auxv); 1076 P->auxv = NULL; 1077 return (-1); 1078 } 1079 #ifdef _LP64 1080 } 1081 #endif 1082 1083 if (_libproc_debug) { 1084 for (i = 0; i < n; i++) { 1085 Pdprintf("P->auxv[%lu] = ( %d, 0x%lx )\n", (ulong_t)i, 1086 P->auxv[i].a_type, P->auxv[i].a_un.a_val); 1087 } 1088 } 1089 1090 /* 1091 * Defensive coding for loops which depend upon the auxv array being 1092 * terminated by an AT_NULL element; in each case, we've allocated 1093 * P->auxv to have an additional element which we force to be AT_NULL. 1094 */ 1095 P->auxv[n].a_type = AT_NULL; 1096 P->auxv[n].a_un.a_val = 0L; 1097 P->nauxv = (int)n; 1098 1099 return (0); 1100 } 1101 1102 /* 1103 * The xregs are not a fixed size on all architectures (notably x86) and in 1104 * general the prxregset_t has become opaque to deal with this. This means that 1105 * validating the note itself can be a little more challenging. Especially as 1106 * this can change across time. In this case we require that our consumers 1107 * perform this validation. 1108 */ 1109 static int 1110 note_xreg(struct ps_prochandle *P, size_t nbytes) 1111 { 1112 core_info_t *core = P->data; 1113 lwp_info_t *lwp = core->core_lwp; 1114 prxregset_t *xregs; 1115 ssize_t sret; 1116 1117 if (lwp == NULL || lwp->lwp_xregs != NULL) 1118 return (0); /* No lwp yet, already seen, or bad size */ 1119 1120 if ((xregs = malloc(nbytes)) == NULL) 1121 return (-1); 1122 1123 sret = read(P->asfd, xregs, nbytes); 1124 if (sret < 0 || (size_t)sret != nbytes) { 1125 Pdprintf("Pgrab_core: failed to read NT_PRXREG\n"); 1126 free(xregs); 1127 return (-1); 1128 } 1129 1130 lwp->lwp_xregs = xregs; 1131 lwp->lwp_xregsize = nbytes; 1132 return (0); 1133 } 1134 1135 #ifdef __sparc 1136 static int 1137 note_gwindows(struct ps_prochandle *P, size_t nbytes) 1138 { 1139 core_info_t *core = P->data; 1140 lwp_info_t *lwp = core->core_lwp; 1141 1142 if (lwp == NULL || lwp->lwp_gwins != NULL || nbytes == 0) 1143 return (0); /* No lwp yet or already seen or no data */ 1144 1145 if ((lwp->lwp_gwins = malloc(sizeof (gwindows_t))) == NULL) 1146 return (-1); 1147 1148 /* 1149 * Since the amount of gwindows data varies with how many windows were 1150 * actually saved, we just read up to the minimum of the note size 1151 * and the size of the gwindows_t type. It doesn't matter if the read 1152 * fails since we have to zero out gwindows first anyway. 1153 */ 1154 #ifdef _LP64 1155 if (core->core_dmodel == PR_MODEL_ILP32) { 1156 gwindows32_t g32; 1157 1158 (void) memset(&g32, 0, sizeof (g32)); 1159 (void) read(P->asfd, &g32, MIN(nbytes, sizeof (g32))); 1160 gwindows_32_to_n(&g32, lwp->lwp_gwins); 1161 1162 } else { 1163 #endif 1164 (void) memset(lwp->lwp_gwins, 0, sizeof (gwindows_t)); 1165 (void) read(P->asfd, lwp->lwp_gwins, 1166 MIN(nbytes, sizeof (gwindows_t))); 1167 #ifdef _LP64 1168 } 1169 #endif 1170 return (0); 1171 } 1172 1173 #ifdef __sparcv9 1174 static int 1175 note_asrs(struct ps_prochandle *P, size_t nbytes) 1176 { 1177 core_info_t *core = P->data; 1178 lwp_info_t *lwp = core->core_lwp; 1179 int64_t *asrs; 1180 1181 if (lwp == NULL || lwp->lwp_asrs != NULL || nbytes < sizeof (asrset_t)) 1182 return (0); /* No lwp yet, already seen, or bad size */ 1183 1184 if ((asrs = malloc(sizeof (asrset_t))) == NULL) 1185 return (-1); 1186 1187 if (read(P->asfd, asrs, sizeof (asrset_t)) != sizeof (asrset_t)) { 1188 Pdprintf("Pgrab_core: failed to read NT_ASRS\n"); 1189 free(asrs); 1190 return (-1); 1191 } 1192 1193 lwp->lwp_asrs = asrs; 1194 return (0); 1195 } 1196 #endif /* __sparcv9 */ 1197 #endif /* __sparc */ 1198 1199 static int 1200 note_spymaster(struct ps_prochandle *P, size_t nbytes) 1201 { 1202 #ifdef _LP64 1203 core_info_t *core = P->data; 1204 1205 if (core->core_dmodel == PR_MODEL_ILP32) { 1206 psinfo32_t ps32; 1207 1208 if (nbytes < sizeof (psinfo32_t) || 1209 read(P->asfd, &ps32, sizeof (ps32)) != sizeof (ps32)) 1210 goto err; 1211 1212 psinfo_32_to_n(&ps32, &P->spymaster); 1213 } else 1214 #endif 1215 if (nbytes < sizeof (psinfo_t) || read(P->asfd, 1216 &P->spymaster, sizeof (psinfo_t)) != sizeof (psinfo_t)) 1217 goto err; 1218 1219 Pdprintf("spymaster pr_fname = <%s>\n", P->psinfo.pr_fname); 1220 Pdprintf("spymaster pr_psargs = <%s>\n", P->psinfo.pr_psargs); 1221 Pdprintf("spymaster pr_wstat = 0x%x\n", P->psinfo.pr_wstat); 1222 1223 return (0); 1224 1225 err: 1226 Pdprintf("Pgrab_core: failed to read NT_SPYMASTER\n"); 1227 return (-1); 1228 } 1229 1230 static int 1231 note_upanic(struct ps_prochandle *P, size_t nbytes) 1232 { 1233 core_info_t *core = P->data; 1234 prupanic_t *pru; 1235 1236 if (core->core_upanic != NULL) 1237 return (0); 1238 1239 if (sizeof (*pru) != nbytes) { 1240 Pdprintf("Pgrab_core: NT_UPANIC changed size." 1241 " Need to handle a version change?\n"); 1242 return (-1); 1243 } 1244 1245 if (nbytes != 0 && ((pru = malloc(nbytes)) != NULL)) { 1246 if (read(P->asfd, pru, nbytes) != nbytes) { 1247 Pdprintf("Pgrab_core: failed to read NT_UPANIC\n"); 1248 free(pru); 1249 return (-1); 1250 } 1251 1252 core->core_upanic = pru; 1253 } 1254 1255 return (0); 1256 } 1257 1258 static int 1259 note_cwd(struct ps_prochandle *P, size_t nbytes) 1260 { 1261 core_info_t *core = P->data; 1262 prcwd_t *cwd; 1263 1264 if (core->core_cwd != NULL) 1265 return (0); 1266 1267 if (sizeof (*cwd) != nbytes) { 1268 Pdprintf("Pgrab_core: NT_CWD changed size." 1269 " Need to handle a version change?\n"); 1270 return (-1); 1271 } 1272 1273 if (nbytes != 0 && ((cwd = malloc(nbytes)) != NULL)) { 1274 if (read(P->asfd, cwd, nbytes) != nbytes) { 1275 Pdprintf("Pgrab_core: failed to read NT_CWD\n"); 1276 free(cwd); 1277 return (-1); 1278 } 1279 1280 core->core_cwd = cwd; 1281 } 1282 1283 return (0); 1284 } 1285 1286 static int 1287 note_notsup(struct ps_prochandle *P, size_t nbytes) 1288 { 1289 Pdprintf("skipping unsupported note type of size %ld bytes\n", 1290 (ulong_t)nbytes); 1291 return (0); 1292 } 1293 1294 #if NT_NUM != NT_CWD 1295 #error "NT_NUM has grown. Update nhdlrs array" 1296 #endif 1297 1298 /* 1299 * Populate a table of function pointers indexed by Note type with our 1300 * functions to process each type of core file note: 1301 */ 1302 static int (*nhdlrs[NT_NUM + 1])(struct ps_prochandle *, size_t) = { 1303 #ifdef __x86 1304 [NT_PRSTATUS] = note_linux_prstatus, 1305 #endif 1306 [NT_PRFPREG] = note_notsup, 1307 #ifdef __x86 1308 [NT_PRPSINFO] = note_linux_psinfo, 1309 #endif 1310 [NT_PRXREG] = note_xreg, 1311 [NT_PLATFORM] = note_platform, 1312 [NT_AUXV] = note_auxv, 1313 #ifdef __sparc 1314 [NT_GWINDOWS] = note_gwindows, 1315 #ifdef __sparcv9 1316 [NT_ASRS] = note_asrs, 1317 #endif 1318 #endif 1319 #ifdef __x86 1320 [NT_LDT] = note_ldt, 1321 #endif 1322 [NT_PSTATUS] = note_pstatus, 1323 [NT_PSINFO] = note_psinfo, 1324 [NT_PRCRED] = note_cred, 1325 [NT_UTSNAME] = note_utsname, 1326 [NT_LWPSTATUS] = note_lwpstatus, 1327 [NT_LWPSINFO] = note_lwpsinfo, 1328 [NT_PRPRIV] = note_priv, 1329 [NT_PRPRIVINFO] = note_priv_info, 1330 [NT_CONTENT] = note_content, 1331 [NT_ZONENAME] = note_zonename, 1332 [NT_FDINFO] = note_fdinfo, 1333 [NT_SPYMASTER] = note_spymaster, 1334 [NT_SECFLAGS] = note_secflags, 1335 [NT_LWPNAME] = note_lwpname, 1336 [NT_UPANIC] = note_upanic, 1337 [NT_CWD] = note_cwd 1338 }; 1339 1340 static void 1341 core_report_mapping(struct ps_prochandle *P, GElf_Phdr *php) 1342 { 1343 prkillinfo_t killinfo; 1344 siginfo_t *si = &killinfo.prk_info; 1345 char signame[SIG2STR_MAX], sig[64], info[64]; 1346 void *addr = (void *)(uintptr_t)php->p_vaddr; 1347 1348 const char *errfmt = "core file data for mapping at %p not saved: %s\n"; 1349 const char *incfmt = "core file incomplete due to %s%s\n"; 1350 const char *msgfmt = "mappings at and above %p are missing\n"; 1351 1352 if (!(php->p_flags & PF_SUNW_KILLED)) { 1353 int err = 0; 1354 1355 (void) pread64(P->asfd, &err, 1356 sizeof (err), (off64_t)php->p_offset); 1357 1358 Perror_printf(P, errfmt, addr, strerror(err)); 1359 Pdprintf(errfmt, addr, strerror(err)); 1360 return; 1361 } 1362 1363 if (!(php->p_flags & PF_SUNW_SIGINFO)) 1364 return; 1365 1366 (void) memset(&killinfo, 0, sizeof (killinfo)); 1367 1368 (void) pread64(P->asfd, &killinfo, 1369 sizeof (killinfo), (off64_t)php->p_offset); 1370 1371 /* 1372 * While there is (or at least should be) only one segment that has 1373 * PF_SUNW_SIGINFO set, the signal information there is globally 1374 * useful (even if only to those debugging libproc consumers); we hang 1375 * the signal information gleaned here off of the ps_prochandle. 1376 */ 1377 P->map_missing = php->p_vaddr; 1378 P->killinfo = killinfo.prk_info; 1379 1380 if (sig2str(si->si_signo, signame) == -1) { 1381 (void) snprintf(sig, sizeof (sig), 1382 "<Unknown signal: 0x%x>, ", si->si_signo); 1383 } else { 1384 (void) snprintf(sig, sizeof (sig), "SIG%s, ", signame); 1385 } 1386 1387 if (si->si_code == SI_USER || si->si_code == SI_QUEUE) { 1388 (void) snprintf(info, sizeof (info), 1389 "pid=%d uid=%d zone=%d ctid=%d", 1390 si->si_pid, si->si_uid, si->si_zoneid, si->si_ctid); 1391 } else { 1392 (void) snprintf(info, sizeof (info), 1393 "code=%d", si->si_code); 1394 } 1395 1396 Perror_printf(P, incfmt, sig, info); 1397 Perror_printf(P, msgfmt, addr); 1398 1399 Pdprintf(incfmt, sig, info); 1400 Pdprintf(msgfmt, addr); 1401 } 1402 1403 /* 1404 * Add information on the address space mapping described by the given 1405 * PT_LOAD program header. We fill in more information on the mapping later. 1406 */ 1407 static int 1408 core_add_mapping(struct ps_prochandle *P, GElf_Phdr *php) 1409 { 1410 core_info_t *core = P->data; 1411 prmap_t pmap; 1412 1413 Pdprintf("mapping base %llx filesz %llx memsz %llx offset %llx\n", 1414 (u_longlong_t)php->p_vaddr, (u_longlong_t)php->p_filesz, 1415 (u_longlong_t)php->p_memsz, (u_longlong_t)php->p_offset); 1416 1417 pmap.pr_vaddr = (uintptr_t)php->p_vaddr; 1418 pmap.pr_size = php->p_memsz; 1419 1420 /* 1421 * If Pgcore() or elfcore() fail to write a mapping, they will set 1422 * PF_SUNW_FAILURE in the Phdr and try to stash away the errno for us. 1423 */ 1424 if (php->p_flags & PF_SUNW_FAILURE) { 1425 core_report_mapping(P, php); 1426 } else if (php->p_filesz != 0 && php->p_offset >= core->core_size) { 1427 Perror_printf(P, "core file may be corrupt -- data for mapping " 1428 "at %p is missing\n", (void *)(uintptr_t)php->p_vaddr); 1429 Pdprintf("core file may be corrupt -- data for mapping " 1430 "at %p is missing\n", (void *)(uintptr_t)php->p_vaddr); 1431 } 1432 1433 /* 1434 * The mapping name and offset will hopefully be filled in 1435 * by the librtld_db agent. Unfortunately, if it isn't a 1436 * shared library mapping, this information is gone forever. 1437 */ 1438 pmap.pr_mapname[0] = '\0'; 1439 pmap.pr_offset = 0; 1440 1441 pmap.pr_mflags = 0; 1442 if (php->p_flags & PF_R) 1443 pmap.pr_mflags |= MA_READ; 1444 if (php->p_flags & PF_W) 1445 pmap.pr_mflags |= MA_WRITE; 1446 if (php->p_flags & PF_X) 1447 pmap.pr_mflags |= MA_EXEC; 1448 1449 if (php->p_filesz == 0) 1450 pmap.pr_mflags |= MA_RESERVED1; 1451 1452 /* 1453 * At the time of adding this mapping, we just zero the pagesize. 1454 * Once we've processed more of the core file, we'll have the 1455 * pagesize from the auxv's AT_PAGESZ element and we can fill this in. 1456 */ 1457 pmap.pr_pagesize = 0; 1458 1459 /* 1460 * Unfortunately whether or not the mapping was a System V 1461 * shared memory segment is lost. We use -1 to mark it as not shm. 1462 */ 1463 pmap.pr_shmid = -1; 1464 1465 return (Padd_mapping(P, php->p_offset, NULL, &pmap)); 1466 } 1467 1468 /* 1469 * Given a virtual address, name the mapping at that address using the 1470 * specified name, and return the map_info_t pointer. 1471 */ 1472 static map_info_t * 1473 core_name_mapping(struct ps_prochandle *P, uintptr_t addr, const char *name) 1474 { 1475 map_info_t *mp = Paddr2mptr(P, addr); 1476 1477 if (mp != NULL) { 1478 (void) strncpy(mp->map_pmap.pr_mapname, name, PRMAPSZ); 1479 mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0'; 1480 } 1481 1482 return (mp); 1483 } 1484 1485 /* 1486 * libproc uses libelf for all of its symbol table manipulation. This function 1487 * takes a symbol table and string table from a core file and places them 1488 * in a memory backed elf file. 1489 */ 1490 static void 1491 fake_up_symtab(struct ps_prochandle *P, const elf_file_header_t *ehdr, 1492 GElf_Shdr *symtab, GElf_Shdr *strtab) 1493 { 1494 size_t size; 1495 off64_t off, base; 1496 map_info_t *mp; 1497 file_info_t *fp; 1498 Elf_Scn *scn; 1499 Elf_Data *data; 1500 1501 if (symtab->sh_addr == 0 || 1502 (mp = Paddr2mptr(P, symtab->sh_addr)) == NULL || 1503 (fp = mp->map_file) == NULL) { 1504 Pdprintf("fake_up_symtab: invalid section\n"); 1505 return; 1506 } 1507 1508 if (fp->file_symtab.sym_data_pri != NULL) { 1509 Pdprintf("Symbol table already loaded (sh_addr 0x%lx)\n", 1510 (long)symtab->sh_addr); 1511 return; 1512 } 1513 1514 if (P->status.pr_dmodel == PR_MODEL_ILP32) { 1515 struct { 1516 Elf32_Ehdr ehdr; 1517 Elf32_Shdr shdr[3]; 1518 char data[1]; 1519 } *b; 1520 1521 base = sizeof (b->ehdr) + sizeof (b->shdr); 1522 size = base + symtab->sh_size + strtab->sh_size; 1523 1524 if ((b = calloc(1, size)) == NULL) 1525 return; 1526 1527 (void) memcpy(b->ehdr.e_ident, ehdr->e_ident, 1528 sizeof (ehdr->e_ident)); 1529 b->ehdr.e_type = ehdr->e_type; 1530 b->ehdr.e_machine = ehdr->e_machine; 1531 b->ehdr.e_version = ehdr->e_version; 1532 b->ehdr.e_flags = ehdr->e_flags; 1533 b->ehdr.e_ehsize = sizeof (b->ehdr); 1534 b->ehdr.e_shoff = sizeof (b->ehdr); 1535 b->ehdr.e_shentsize = sizeof (b->shdr[0]); 1536 b->ehdr.e_shnum = 3; 1537 off = 0; 1538 1539 b->shdr[1].sh_size = symtab->sh_size; 1540 b->shdr[1].sh_type = SHT_SYMTAB; 1541 b->shdr[1].sh_offset = off + base; 1542 b->shdr[1].sh_entsize = sizeof (Elf32_Sym); 1543 b->shdr[1].sh_link = 2; 1544 b->shdr[1].sh_info = symtab->sh_info; 1545 b->shdr[1].sh_addralign = symtab->sh_addralign; 1546 1547 if (pread64(P->asfd, &b->data[off], b->shdr[1].sh_size, 1548 symtab->sh_offset) != b->shdr[1].sh_size) { 1549 Pdprintf("fake_up_symtab: pread of symtab[1] failed\n"); 1550 free(b); 1551 return; 1552 } 1553 1554 off += b->shdr[1].sh_size; 1555 1556 b->shdr[2].sh_flags = SHF_STRINGS; 1557 b->shdr[2].sh_size = strtab->sh_size; 1558 b->shdr[2].sh_type = SHT_STRTAB; 1559 b->shdr[2].sh_offset = off + base; 1560 b->shdr[2].sh_info = strtab->sh_info; 1561 b->shdr[2].sh_addralign = 1; 1562 1563 if (pread64(P->asfd, &b->data[off], b->shdr[2].sh_size, 1564 strtab->sh_offset) != b->shdr[2].sh_size) { 1565 Pdprintf("fake_up_symtab: pread of symtab[2] failed\n"); 1566 free(b); 1567 return; 1568 } 1569 1570 off += b->shdr[2].sh_size; 1571 1572 fp->file_symtab.sym_elf = elf_memory((char *)b, size); 1573 if (fp->file_symtab.sym_elf == NULL) { 1574 free(b); 1575 return; 1576 } 1577 1578 fp->file_symtab.sym_elfmem = b; 1579 #ifdef _LP64 1580 } else { 1581 struct { 1582 Elf64_Ehdr ehdr; 1583 Elf64_Shdr shdr[3]; 1584 char data[1]; 1585 } *b; 1586 1587 base = sizeof (b->ehdr) + sizeof (b->shdr); 1588 size = base + symtab->sh_size + strtab->sh_size; 1589 1590 if ((b = calloc(1, size)) == NULL) 1591 return; 1592 1593 (void) memcpy(b->ehdr.e_ident, ehdr->e_ident, 1594 sizeof (ehdr->e_ident)); 1595 b->ehdr.e_type = ehdr->e_type; 1596 b->ehdr.e_machine = ehdr->e_machine; 1597 b->ehdr.e_version = ehdr->e_version; 1598 b->ehdr.e_flags = ehdr->e_flags; 1599 b->ehdr.e_ehsize = sizeof (b->ehdr); 1600 b->ehdr.e_shoff = sizeof (b->ehdr); 1601 b->ehdr.e_shentsize = sizeof (b->shdr[0]); 1602 b->ehdr.e_shnum = 3; 1603 off = 0; 1604 1605 b->shdr[1].sh_size = symtab->sh_size; 1606 b->shdr[1].sh_type = SHT_SYMTAB; 1607 b->shdr[1].sh_offset = off + base; 1608 b->shdr[1].sh_entsize = sizeof (Elf64_Sym); 1609 b->shdr[1].sh_link = 2; 1610 b->shdr[1].sh_info = symtab->sh_info; 1611 b->shdr[1].sh_addralign = symtab->sh_addralign; 1612 1613 if (pread64(P->asfd, &b->data[off], b->shdr[1].sh_size, 1614 symtab->sh_offset) != b->shdr[1].sh_size) { 1615 free(b); 1616 return; 1617 } 1618 1619 off += b->shdr[1].sh_size; 1620 1621 b->shdr[2].sh_flags = SHF_STRINGS; 1622 b->shdr[2].sh_size = strtab->sh_size; 1623 b->shdr[2].sh_type = SHT_STRTAB; 1624 b->shdr[2].sh_offset = off + base; 1625 b->shdr[2].sh_info = strtab->sh_info; 1626 b->shdr[2].sh_addralign = 1; 1627 1628 if (pread64(P->asfd, &b->data[off], b->shdr[2].sh_size, 1629 strtab->sh_offset) != b->shdr[2].sh_size) { 1630 free(b); 1631 return; 1632 } 1633 1634 off += b->shdr[2].sh_size; 1635 1636 fp->file_symtab.sym_elf = elf_memory((char *)b, size); 1637 if (fp->file_symtab.sym_elf == NULL) { 1638 free(b); 1639 return; 1640 } 1641 1642 fp->file_symtab.sym_elfmem = b; 1643 #endif 1644 } 1645 1646 if ((scn = elf_getscn(fp->file_symtab.sym_elf, 1)) == NULL || 1647 (fp->file_symtab.sym_data_pri = elf_getdata(scn, NULL)) == NULL || 1648 (scn = elf_getscn(fp->file_symtab.sym_elf, 2)) == NULL || 1649 (data = elf_getdata(scn, NULL)) == NULL) { 1650 Pdprintf("fake_up_symtab: failed to get section data at %p\n", 1651 (void *)scn); 1652 goto err; 1653 } 1654 1655 fp->file_symtab.sym_strs = data->d_buf; 1656 fp->file_symtab.sym_strsz = data->d_size; 1657 fp->file_symtab.sym_symn = symtab->sh_size / symtab->sh_entsize; 1658 fp->file_symtab.sym_hdr_pri = *symtab; 1659 fp->file_symtab.sym_strhdr = *strtab; 1660 1661 optimize_symtab(&fp->file_symtab); 1662 1663 return; 1664 err: 1665 (void) elf_end(fp->file_symtab.sym_elf); 1666 free(fp->file_symtab.sym_elfmem); 1667 fp->file_symtab.sym_elf = NULL; 1668 fp->file_symtab.sym_elfmem = NULL; 1669 } 1670 1671 static void 1672 core_phdr_to_gelf(const Elf32_Phdr *src, GElf_Phdr *dst) 1673 { 1674 dst->p_type = src->p_type; 1675 dst->p_flags = src->p_flags; 1676 dst->p_offset = (Elf64_Off)src->p_offset; 1677 dst->p_vaddr = (Elf64_Addr)src->p_vaddr; 1678 dst->p_paddr = (Elf64_Addr)src->p_paddr; 1679 dst->p_filesz = (Elf64_Xword)src->p_filesz; 1680 dst->p_memsz = (Elf64_Xword)src->p_memsz; 1681 dst->p_align = (Elf64_Xword)src->p_align; 1682 } 1683 1684 static void 1685 core_shdr_to_gelf(const Elf32_Shdr *src, GElf_Shdr *dst) 1686 { 1687 dst->sh_name = src->sh_name; 1688 dst->sh_type = src->sh_type; 1689 dst->sh_flags = (Elf64_Xword)src->sh_flags; 1690 dst->sh_addr = (Elf64_Addr)src->sh_addr; 1691 dst->sh_offset = (Elf64_Off)src->sh_offset; 1692 dst->sh_size = (Elf64_Xword)src->sh_size; 1693 dst->sh_link = src->sh_link; 1694 dst->sh_info = src->sh_info; 1695 dst->sh_addralign = (Elf64_Xword)src->sh_addralign; 1696 dst->sh_entsize = (Elf64_Xword)src->sh_entsize; 1697 } 1698 1699 /* 1700 * Perform elf_begin on efp->e_fd and verify the ELF file's type and class. 1701 */ 1702 static int 1703 core_elf_fdopen(elf_file_t *efp, GElf_Half type, int *perr) 1704 { 1705 #ifdef _BIG_ENDIAN 1706 uchar_t order = ELFDATA2MSB; 1707 #else 1708 uchar_t order = ELFDATA2LSB; 1709 #endif 1710 Elf32_Ehdr e32; 1711 int is_noelf = -1; 1712 int isa_err = 0; 1713 1714 /* 1715 * Because 32-bit libelf cannot deal with large files, we need to read, 1716 * check, and convert the file header manually in case type == ET_CORE. 1717 */ 1718 if (pread64(efp->e_fd, &e32, sizeof (e32), 0) != sizeof (e32)) { 1719 if (perr != NULL) 1720 *perr = G_FORMAT; 1721 goto err; 1722 } 1723 if ((is_noelf = memcmp(&e32.e_ident[EI_MAG0], ELFMAG, SELFMAG)) != 0 || 1724 e32.e_type != type || (isa_err = (e32.e_ident[EI_DATA] != order)) || 1725 e32.e_version != EV_CURRENT) { 1726 if (perr != NULL) { 1727 if (is_noelf == 0 && isa_err) { 1728 *perr = G_ISAINVAL; 1729 } else { 1730 *perr = G_FORMAT; 1731 } 1732 } 1733 goto err; 1734 } 1735 1736 /* 1737 * If the file is 64-bit and we are 32-bit, fail with G_LP64. If the 1738 * file is 64-bit and we are 64-bit, re-read the header as a Elf64_Ehdr, 1739 * and convert it to a elf_file_header_t. Otherwise, the file is 1740 * 32-bit, so convert e32 to a elf_file_header_t. 1741 */ 1742 if (e32.e_ident[EI_CLASS] == ELFCLASS64) { 1743 #ifdef _LP64 1744 Elf64_Ehdr e64; 1745 1746 if (pread64(efp->e_fd, &e64, sizeof (e64), 0) != sizeof (e64)) { 1747 if (perr != NULL) 1748 *perr = G_FORMAT; 1749 goto err; 1750 } 1751 1752 (void) memcpy(efp->e_hdr.e_ident, e64.e_ident, EI_NIDENT); 1753 efp->e_hdr.e_type = e64.e_type; 1754 efp->e_hdr.e_machine = e64.e_machine; 1755 efp->e_hdr.e_version = e64.e_version; 1756 efp->e_hdr.e_entry = e64.e_entry; 1757 efp->e_hdr.e_phoff = e64.e_phoff; 1758 efp->e_hdr.e_shoff = e64.e_shoff; 1759 efp->e_hdr.e_flags = e64.e_flags; 1760 efp->e_hdr.e_ehsize = e64.e_ehsize; 1761 efp->e_hdr.e_phentsize = e64.e_phentsize; 1762 efp->e_hdr.e_phnum = (Elf64_Word)e64.e_phnum; 1763 efp->e_hdr.e_shentsize = e64.e_shentsize; 1764 efp->e_hdr.e_shnum = (Elf64_Word)e64.e_shnum; 1765 efp->e_hdr.e_shstrndx = (Elf64_Word)e64.e_shstrndx; 1766 #else /* _LP64 */ 1767 if (perr != NULL) 1768 *perr = G_LP64; 1769 goto err; 1770 #endif /* _LP64 */ 1771 } else { 1772 (void) memcpy(efp->e_hdr.e_ident, e32.e_ident, EI_NIDENT); 1773 efp->e_hdr.e_type = e32.e_type; 1774 efp->e_hdr.e_machine = e32.e_machine; 1775 efp->e_hdr.e_version = e32.e_version; 1776 efp->e_hdr.e_entry = (Elf64_Addr)e32.e_entry; 1777 efp->e_hdr.e_phoff = (Elf64_Off)e32.e_phoff; 1778 efp->e_hdr.e_shoff = (Elf64_Off)e32.e_shoff; 1779 efp->e_hdr.e_flags = e32.e_flags; 1780 efp->e_hdr.e_ehsize = e32.e_ehsize; 1781 efp->e_hdr.e_phentsize = e32.e_phentsize; 1782 efp->e_hdr.e_phnum = (Elf64_Word)e32.e_phnum; 1783 efp->e_hdr.e_shentsize = e32.e_shentsize; 1784 efp->e_hdr.e_shnum = (Elf64_Word)e32.e_shnum; 1785 efp->e_hdr.e_shstrndx = (Elf64_Word)e32.e_shstrndx; 1786 } 1787 1788 /* 1789 * If the number of section headers or program headers or the section 1790 * header string table index would overflow their respective fields 1791 * in the ELF header, they're stored in the section header at index 1792 * zero. To simplify use elsewhere, we look for those sentinel values 1793 * here. 1794 */ 1795 if ((efp->e_hdr.e_shnum == 0 && efp->e_hdr.e_shoff != 0) || 1796 efp->e_hdr.e_shstrndx == SHN_XINDEX || 1797 efp->e_hdr.e_phnum == PN_XNUM) { 1798 GElf_Shdr shdr; 1799 1800 Pdprintf("extended ELF header\n"); 1801 1802 if (efp->e_hdr.e_shoff == 0) { 1803 if (perr != NULL) 1804 *perr = G_FORMAT; 1805 goto err; 1806 } 1807 1808 if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32) { 1809 Elf32_Shdr shdr32; 1810 1811 if (pread64(efp->e_fd, &shdr32, sizeof (shdr32), 1812 efp->e_hdr.e_shoff) != sizeof (shdr32)) { 1813 if (perr != NULL) 1814 *perr = G_FORMAT; 1815 goto err; 1816 } 1817 1818 core_shdr_to_gelf(&shdr32, &shdr); 1819 } else { 1820 if (pread64(efp->e_fd, &shdr, sizeof (shdr), 1821 efp->e_hdr.e_shoff) != sizeof (shdr)) { 1822 if (perr != NULL) 1823 *perr = G_FORMAT; 1824 goto err; 1825 } 1826 } 1827 1828 if (efp->e_hdr.e_shnum == 0) { 1829 efp->e_hdr.e_shnum = shdr.sh_size; 1830 Pdprintf("section header count %lu\n", 1831 (ulong_t)shdr.sh_size); 1832 } 1833 1834 if (efp->e_hdr.e_shstrndx == SHN_XINDEX) { 1835 efp->e_hdr.e_shstrndx = shdr.sh_link; 1836 Pdprintf("section string index %u\n", shdr.sh_link); 1837 } 1838 1839 if (efp->e_hdr.e_phnum == PN_XNUM && shdr.sh_info != 0) { 1840 efp->e_hdr.e_phnum = shdr.sh_info; 1841 Pdprintf("program header count %u\n", shdr.sh_info); 1842 } 1843 1844 } else if (efp->e_hdr.e_phoff != 0) { 1845 GElf_Phdr phdr; 1846 uint64_t phnum; 1847 1848 /* 1849 * It's possible this core file came from a system that 1850 * accidentally truncated the e_phnum field without correctly 1851 * using the extended format in the section header at index 1852 * zero. We try to detect and correct that specific type of 1853 * corruption by using the knowledge that the core dump 1854 * routines usually place the data referenced by the first 1855 * program header immediately after the last header element. 1856 */ 1857 if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32) { 1858 Elf32_Phdr phdr32; 1859 1860 if (pread64(efp->e_fd, &phdr32, sizeof (phdr32), 1861 efp->e_hdr.e_phoff) != sizeof (phdr32)) { 1862 if (perr != NULL) 1863 *perr = G_FORMAT; 1864 goto err; 1865 } 1866 1867 core_phdr_to_gelf(&phdr32, &phdr); 1868 } else { 1869 if (pread64(efp->e_fd, &phdr, sizeof (phdr), 1870 efp->e_hdr.e_phoff) != sizeof (phdr)) { 1871 if (perr != NULL) 1872 *perr = G_FORMAT; 1873 goto err; 1874 } 1875 } 1876 1877 phnum = phdr.p_offset - efp->e_hdr.e_ehsize - 1878 (uint64_t)efp->e_hdr.e_shnum * efp->e_hdr.e_shentsize; 1879 phnum /= efp->e_hdr.e_phentsize; 1880 1881 if (phdr.p_offset != 0 && phnum != efp->e_hdr.e_phnum) { 1882 Pdprintf("suspicious program header count %u %u\n", 1883 (uint_t)phnum, efp->e_hdr.e_phnum); 1884 1885 /* 1886 * If the new program header count we computed doesn't 1887 * jive with count in the ELF header, we'll use the 1888 * data that's there and hope for the best. 1889 * 1890 * If it does, it's also possible that the section 1891 * header offset is incorrect; we'll check that and 1892 * possibly try to fix it. 1893 */ 1894 if (phnum <= INT_MAX && 1895 (uint16_t)phnum == efp->e_hdr.e_phnum) { 1896 1897 if (efp->e_hdr.e_shoff == efp->e_hdr.e_phoff + 1898 efp->e_hdr.e_phentsize * 1899 (uint_t)efp->e_hdr.e_phnum) { 1900 efp->e_hdr.e_shoff = 1901 efp->e_hdr.e_phoff + 1902 efp->e_hdr.e_phentsize * phnum; 1903 } 1904 1905 efp->e_hdr.e_phnum = (Elf64_Word)phnum; 1906 Pdprintf("using new program header count\n"); 1907 } else { 1908 Pdprintf("inconsistent program header count\n"); 1909 } 1910 } 1911 } 1912 1913 /* 1914 * The libelf implementation was never ported to be large-file aware. 1915 * This is typically not a problem for your average executable or 1916 * shared library, but a large 32-bit core file can exceed 2GB in size. 1917 * So if type is ET_CORE, we don't bother doing elf_begin; the code 1918 * in Pfgrab_core() below will do its own i/o and struct conversion. 1919 */ 1920 1921 if (type == ET_CORE) { 1922 efp->e_elf = NULL; 1923 return (0); 1924 } 1925 1926 if ((efp->e_elf = elf_begin(efp->e_fd, ELF_C_READ, NULL)) == NULL) { 1927 if (perr != NULL) 1928 *perr = G_ELF; 1929 goto err; 1930 } 1931 1932 return (0); 1933 1934 err: 1935 efp->e_elf = NULL; 1936 return (-1); 1937 } 1938 1939 /* 1940 * Open the specified file and then do a core_elf_fdopen on it. 1941 */ 1942 static int 1943 core_elf_open(elf_file_t *efp, const char *path, GElf_Half type, int *perr) 1944 { 1945 (void) memset(efp, 0, sizeof (elf_file_t)); 1946 1947 if ((efp->e_fd = open64(path, O_RDONLY)) >= 0) { 1948 if (core_elf_fdopen(efp, type, perr) == 0) 1949 return (0); 1950 1951 (void) close(efp->e_fd); 1952 efp->e_fd = -1; 1953 } 1954 1955 return (-1); 1956 } 1957 1958 /* 1959 * Close the ELF handle and file descriptor. 1960 */ 1961 static void 1962 core_elf_close(elf_file_t *efp) 1963 { 1964 if (efp->e_elf != NULL) { 1965 (void) elf_end(efp->e_elf); 1966 efp->e_elf = NULL; 1967 } 1968 1969 if (efp->e_fd != -1) { 1970 (void) close(efp->e_fd); 1971 efp->e_fd = -1; 1972 } 1973 } 1974 1975 /* 1976 * Given an ELF file for a statically linked executable, locate the likely 1977 * primary text section and fill in rl_base with its virtual address. 1978 */ 1979 static map_info_t * 1980 core_find_text(struct ps_prochandle *P, Elf *elf, rd_loadobj_t *rlp) 1981 { 1982 GElf_Phdr phdr; 1983 uint_t i; 1984 size_t nphdrs; 1985 1986 if (elf_getphdrnum(elf, &nphdrs) == -1) 1987 return (NULL); 1988 1989 for (i = 0; i < nphdrs; i++) { 1990 if (gelf_getphdr(elf, i, &phdr) != NULL && 1991 phdr.p_type == PT_LOAD && (phdr.p_flags & PF_X)) { 1992 rlp->rl_base = phdr.p_vaddr; 1993 return (Paddr2mptr(P, rlp->rl_base)); 1994 } 1995 } 1996 1997 return (NULL); 1998 } 1999 2000 /* 2001 * Given an ELF file and the librtld_db structure corresponding to its primary 2002 * text mapping, deduce where its data segment was loaded and fill in 2003 * rl_data_base and prmap_t.pr_offset accordingly. 2004 */ 2005 static map_info_t * 2006 core_find_data(struct ps_prochandle *P, Elf *elf, rd_loadobj_t *rlp) 2007 { 2008 GElf_Ehdr ehdr; 2009 GElf_Phdr phdr; 2010 map_info_t *mp; 2011 uint_t i, pagemask; 2012 size_t nphdrs; 2013 2014 rlp->rl_data_base = (uintptr_t)NULL; 2015 2016 /* 2017 * Find the first loadable, writeable Phdr and compute rl_data_base 2018 * as the virtual address at which is was loaded. 2019 */ 2020 if (gelf_getehdr(elf, &ehdr) == NULL || 2021 elf_getphdrnum(elf, &nphdrs) == -1) 2022 return (NULL); 2023 2024 for (i = 0; i < nphdrs; i++) { 2025 if (gelf_getphdr(elf, i, &phdr) != NULL && 2026 phdr.p_type == PT_LOAD && (phdr.p_flags & PF_W)) { 2027 rlp->rl_data_base = phdr.p_vaddr; 2028 if (ehdr.e_type == ET_DYN) 2029 rlp->rl_data_base += rlp->rl_base; 2030 break; 2031 } 2032 } 2033 2034 /* 2035 * If we didn't find an appropriate phdr or if the address we 2036 * computed has no mapping, return NULL. 2037 */ 2038 if (rlp->rl_data_base == (uintptr_t)NULL || 2039 (mp = Paddr2mptr(P, rlp->rl_data_base)) == NULL) 2040 return (NULL); 2041 2042 /* 2043 * It wouldn't be procfs-related code if we didn't make use of 2044 * unclean knowledge of segvn, even in userland ... the prmap_t's 2045 * pr_offset field will be the segvn offset from mmap(2)ing the 2046 * data section, which will be the file offset & PAGEMASK. 2047 */ 2048 pagemask = ~(mp->map_pmap.pr_pagesize - 1); 2049 mp->map_pmap.pr_offset = phdr.p_offset & pagemask; 2050 2051 return (mp); 2052 } 2053 2054 /* 2055 * Librtld_db agent callback for iterating over load object mappings. 2056 * For each load object, we allocate a new file_info_t, perform naming, 2057 * and attempt to construct a symbol table for the load object. 2058 */ 2059 static int 2060 core_iter_mapping(const rd_loadobj_t *rlp, struct ps_prochandle *P) 2061 { 2062 core_info_t *core = P->data; 2063 char lname[PATH_MAX], buf[PATH_MAX]; 2064 file_info_t *fp; 2065 map_info_t *mp; 2066 2067 if (Pread_string(P, lname, PATH_MAX, (off_t)rlp->rl_nameaddr) <= 0) { 2068 Pdprintf("failed to read name %p\n", (void *)rlp->rl_nameaddr); 2069 return (1); /* Keep going; forget this if we can't get a name */ 2070 } 2071 2072 Pdprintf("rd_loadobj name = \"%s\" rl_base = %p\n", 2073 lname, (void *)rlp->rl_base); 2074 2075 if ((mp = Paddr2mptr(P, rlp->rl_base)) == NULL) { 2076 Pdprintf("no mapping for %p\n", (void *)rlp->rl_base); 2077 return (1); /* No mapping; advance to next mapping */ 2078 } 2079 2080 /* 2081 * Create a new file_info_t for this mapping, and therefore for 2082 * this load object. 2083 * 2084 * If there's an ELF header at the beginning of this mapping, 2085 * file_info_new() will try to use its section headers to 2086 * identify any other mappings that belong to this load object. 2087 */ 2088 if ((fp = mp->map_file) == NULL && 2089 (fp = file_info_new(P, mp)) == NULL) { 2090 core->core_errno = errno; 2091 Pdprintf("failed to malloc mapping data\n"); 2092 return (0); /* Abort */ 2093 } 2094 fp->file_map = mp; 2095 2096 /* Create a local copy of the load object representation */ 2097 if ((fp->file_lo = calloc(1, sizeof (rd_loadobj_t))) == NULL) { 2098 core->core_errno = errno; 2099 Pdprintf("failed to malloc mapping data\n"); 2100 return (0); /* Abort */ 2101 } 2102 *fp->file_lo = *rlp; 2103 2104 if (lname[0] != '\0') { 2105 /* 2106 * Naming dance part 1: if we got a name from librtld_db, then 2107 * copy this name to the prmap_t if it is unnamed. If the 2108 * file_info_t is unnamed, name it after the lname. 2109 */ 2110 if (mp->map_pmap.pr_mapname[0] == '\0') { 2111 (void) strncpy(mp->map_pmap.pr_mapname, lname, PRMAPSZ); 2112 mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0'; 2113 } 2114 2115 if (fp->file_lname == NULL) 2116 fp->file_lname = strdup(lname); 2117 2118 } else if (fp->file_lname == NULL && 2119 mp->map_pmap.pr_mapname[0] != '\0') { 2120 /* 2121 * Naming dance part 2: if the mapping is named and the 2122 * file_info_t is not, name the file after the mapping. 2123 */ 2124 fp->file_lname = strdup(mp->map_pmap.pr_mapname); 2125 } 2126 2127 if ((fp->file_rname == NULL) && 2128 (Pfindmap(P, mp, buf, sizeof (buf)) != NULL)) 2129 fp->file_rname = strdup(buf); 2130 2131 if (fp->file_lname != NULL) 2132 fp->file_lbase = basename(fp->file_lname); 2133 if (fp->file_rname != NULL) 2134 fp->file_rbase = basename(fp->file_rname); 2135 2136 /* Associate the file and the mapping. */ 2137 (void) strncpy(fp->file_pname, mp->map_pmap.pr_mapname, PRMAPSZ); 2138 fp->file_pname[PRMAPSZ - 1] = '\0'; 2139 2140 /* 2141 * If no section headers were available then we'll have to 2142 * identify this load object's other mappings with what we've 2143 * got: the start and end of the object's corresponding 2144 * address space. 2145 */ 2146 if (fp->file_saddrs == NULL) { 2147 for (mp = fp->file_map + 1; mp < P->mappings + P->map_count && 2148 mp->map_pmap.pr_vaddr < rlp->rl_bend; mp++) { 2149 2150 if (mp->map_file == NULL) { 2151 Pdprintf("core_iter_mapping %s: associating " 2152 "segment at %p\n", 2153 fp->file_pname, 2154 (void *)mp->map_pmap.pr_vaddr); 2155 mp->map_file = fp; 2156 fp->file_ref++; 2157 } else { 2158 Pdprintf("core_iter_mapping %s: segment at " 2159 "%p already associated with %s\n", 2160 fp->file_pname, 2161 (void *)mp->map_pmap.pr_vaddr, 2162 (mp == fp->file_map ? "this file" : 2163 mp->map_file->file_pname)); 2164 } 2165 } 2166 } 2167 2168 /* Ensure that all this file's mappings are named. */ 2169 for (mp = fp->file_map; mp < P->mappings + P->map_count && 2170 mp->map_file == fp; mp++) { 2171 if (mp->map_pmap.pr_mapname[0] == '\0' && 2172 !(mp->map_pmap.pr_mflags & MA_BREAK)) { 2173 (void) strncpy(mp->map_pmap.pr_mapname, fp->file_pname, 2174 PRMAPSZ); 2175 mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0'; 2176 } 2177 } 2178 2179 /* Attempt to build a symbol table for this file. */ 2180 Pbuild_file_symtab(P, fp); 2181 if (fp->file_elf == NULL) 2182 Pdprintf("core_iter_mapping: no symtab for %s\n", 2183 fp->file_pname); 2184 2185 /* Locate the start of a data segment associated with this file. */ 2186 if ((mp = core_find_data(P, fp->file_elf, fp->file_lo)) != NULL) { 2187 Pdprintf("found data for %s at %p (pr_offset 0x%llx)\n", 2188 fp->file_pname, (void *)fp->file_lo->rl_data_base, 2189 mp->map_pmap.pr_offset); 2190 } else { 2191 Pdprintf("core_iter_mapping: no data found for %s\n", 2192 fp->file_pname); 2193 } 2194 2195 return (1); /* Advance to next mapping */ 2196 } 2197 2198 /* 2199 * Callback function for Pfindexec(). In order to confirm a given pathname, 2200 * we verify that we can open it as an ELF file of type ET_EXEC or ET_DYN. 2201 */ 2202 static int 2203 core_exec_open(const char *path, void *efp) 2204 { 2205 if (core_elf_open(efp, path, ET_EXEC, NULL) == 0) 2206 return (1); 2207 if (core_elf_open(efp, path, ET_DYN, NULL) == 0) 2208 return (1); 2209 return (0); 2210 } 2211 2212 /* 2213 * Attempt to load any section headers found in the core file. If present, 2214 * this will refer to non-loadable data added to the core file by the kernel 2215 * based on coreadm(8) settings, including CTF data and the symbol table. 2216 */ 2217 static void 2218 core_load_shdrs(struct ps_prochandle *P, elf_file_t *efp) 2219 { 2220 GElf_Shdr *shp, *shdrs = NULL; 2221 char *shstrtab = NULL; 2222 ulong_t shstrtabsz; 2223 const char *name; 2224 map_info_t *mp; 2225 2226 size_t nbytes; 2227 void *buf; 2228 int i; 2229 2230 if (efp->e_hdr.e_shstrndx >= efp->e_hdr.e_shnum) { 2231 Pdprintf("corrupt shstrndx (%u) exceeds shnum (%u)\n", 2232 efp->e_hdr.e_shstrndx, efp->e_hdr.e_shnum); 2233 return; 2234 } 2235 2236 /* 2237 * Read the section header table from the core file and then iterate 2238 * over the section headers, converting each to a GElf_Shdr. 2239 */ 2240 if ((shdrs = malloc(efp->e_hdr.e_shnum * sizeof (GElf_Shdr))) == NULL) { 2241 Pdprintf("failed to malloc %u section headers: %s\n", 2242 (uint_t)efp->e_hdr.e_shnum, strerror(errno)); 2243 return; 2244 } 2245 2246 nbytes = efp->e_hdr.e_shnum * efp->e_hdr.e_shentsize; 2247 if ((buf = malloc(nbytes)) == NULL) { 2248 Pdprintf("failed to malloc %d bytes: %s\n", (int)nbytes, 2249 strerror(errno)); 2250 free(shdrs); 2251 goto out; 2252 } 2253 2254 if (pread64(efp->e_fd, buf, nbytes, efp->e_hdr.e_shoff) != nbytes) { 2255 Pdprintf("failed to read section headers at off %lld: %s\n", 2256 (longlong_t)efp->e_hdr.e_shoff, strerror(errno)); 2257 free(buf); 2258 goto out; 2259 } 2260 2261 for (i = 0; i < efp->e_hdr.e_shnum; i++) { 2262 void *p = (uchar_t *)buf + efp->e_hdr.e_shentsize * i; 2263 2264 if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32) 2265 core_shdr_to_gelf(p, &shdrs[i]); 2266 else 2267 (void) memcpy(&shdrs[i], p, sizeof (GElf_Shdr)); 2268 } 2269 2270 free(buf); 2271 buf = NULL; 2272 2273 /* 2274 * Read the .shstrtab section from the core file, terminating it with 2275 * an extra \0 so that a corrupt section will not cause us to die. 2276 */ 2277 shp = &shdrs[efp->e_hdr.e_shstrndx]; 2278 shstrtabsz = shp->sh_size; 2279 2280 if ((shstrtab = malloc(shstrtabsz + 1)) == NULL) { 2281 Pdprintf("failed to allocate %lu bytes for shstrtab\n", 2282 (ulong_t)shstrtabsz); 2283 goto out; 2284 } 2285 2286 if (pread64(efp->e_fd, shstrtab, shstrtabsz, 2287 shp->sh_offset) != shstrtabsz) { 2288 Pdprintf("failed to read %lu bytes of shstrs at off %lld: %s\n", 2289 shstrtabsz, (longlong_t)shp->sh_offset, strerror(errno)); 2290 goto out; 2291 } 2292 2293 shstrtab[shstrtabsz] = '\0'; 2294 2295 /* 2296 * Now iterate over each section in the section header table, locating 2297 * sections of interest and initializing more of the ps_prochandle. 2298 */ 2299 for (i = 0; i < efp->e_hdr.e_shnum; i++) { 2300 shp = &shdrs[i]; 2301 name = shstrtab + shp->sh_name; 2302 2303 if (shp->sh_name >= shstrtabsz) { 2304 Pdprintf("skipping section [%d]: corrupt sh_name\n", i); 2305 continue; 2306 } 2307 2308 if (shp->sh_link >= efp->e_hdr.e_shnum) { 2309 Pdprintf("skipping section [%d]: corrupt sh_link\n", i); 2310 continue; 2311 } 2312 2313 Pdprintf("found section header %s (sh_addr 0x%llx)\n", 2314 name, (u_longlong_t)shp->sh_addr); 2315 2316 if (strcmp(name, ".SUNW_ctf") == 0) { 2317 if ((mp = Paddr2mptr(P, shp->sh_addr)) == NULL) { 2318 Pdprintf("no map at addr 0x%llx for %s [%d]\n", 2319 (u_longlong_t)shp->sh_addr, name, i); 2320 continue; 2321 } 2322 2323 if (mp->map_file == NULL || 2324 mp->map_file->file_ctf_buf != NULL) { 2325 Pdprintf("no mapping file or duplicate buffer " 2326 "for %s [%d]\n", name, i); 2327 continue; 2328 } 2329 2330 if ((buf = malloc(shp->sh_size)) == NULL || 2331 pread64(efp->e_fd, buf, shp->sh_size, 2332 shp->sh_offset) != shp->sh_size) { 2333 Pdprintf("skipping section %s [%d]: %s\n", 2334 name, i, strerror(errno)); 2335 free(buf); 2336 continue; 2337 } 2338 2339 mp->map_file->file_ctf_size = shp->sh_size; 2340 mp->map_file->file_ctf_buf = buf; 2341 2342 if (shdrs[shp->sh_link].sh_type == SHT_DYNSYM) 2343 mp->map_file->file_ctf_dyn = 1; 2344 2345 } else if (strcmp(name, ".symtab") == 0) { 2346 fake_up_symtab(P, &efp->e_hdr, 2347 shp, &shdrs[shp->sh_link]); 2348 } 2349 } 2350 out: 2351 free(shstrtab); 2352 free(shdrs); 2353 } 2354 2355 /* 2356 * Main engine for core file initialization: given an fd for the core file 2357 * and an optional pathname, construct the ps_prochandle. The aout_path can 2358 * either be a suggested executable pathname, or a suggested directory to 2359 * use as a possible current working directory. 2360 */ 2361 struct ps_prochandle * 2362 Pfgrab_core(int core_fd, const char *aout_path, int *perr) 2363 { 2364 struct ps_prochandle *P; 2365 core_info_t *core_info; 2366 map_info_t *stk_mp, *brk_mp; 2367 const char *execname; 2368 char *interp; 2369 int i, notes, pagesize; 2370 uintptr_t addr, base_addr; 2371 struct stat64 stbuf; 2372 void *phbuf, *php; 2373 size_t nbytes; 2374 #ifdef __x86 2375 boolean_t from_linux = B_FALSE; 2376 #endif 2377 2378 elf_file_t aout; 2379 elf_file_t core; 2380 2381 Elf_Scn *scn, *intp_scn = NULL; 2382 Elf_Data *dp; 2383 2384 GElf_Phdr phdr, note_phdr; 2385 GElf_Shdr shdr; 2386 GElf_Xword nleft; 2387 2388 if (elf_version(EV_CURRENT) == EV_NONE) { 2389 Pdprintf("libproc ELF version is more recent than libelf\n"); 2390 *perr = G_ELF; 2391 return (NULL); 2392 } 2393 2394 aout.e_elf = NULL; 2395 aout.e_fd = -1; 2396 2397 core.e_elf = NULL; 2398 core.e_fd = core_fd; 2399 2400 /* 2401 * Allocate and initialize a ps_prochandle structure for the core. 2402 * There are several key pieces of initialization here: 2403 * 2404 * 1. The PS_DEAD state flag marks this prochandle as a core file. 2405 * PS_DEAD also thus prevents all operations which require state 2406 * to be PS_STOP from operating on this handle. 2407 * 2408 * 2. We keep the core file fd in P->asfd since the core file contains 2409 * the remnants of the process address space. 2410 * 2411 * 3. We set the P->info_valid bit because all information about the 2412 * core is determined by the end of this function; there is no need 2413 * for proc_update_maps() to reload mappings at any later point. 2414 * 2415 * 4. The read/write ops vector uses our core_rw() function defined 2416 * above to handle i/o requests. 2417 */ 2418 if ((P = malloc(sizeof (struct ps_prochandle))) == NULL) { 2419 *perr = G_STRANGE; 2420 return (NULL); 2421 } 2422 2423 (void) memset(P, 0, sizeof (struct ps_prochandle)); 2424 (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL); 2425 P->state = PS_DEAD; 2426 P->pid = (pid_t)-1; 2427 P->asfd = core.e_fd; 2428 P->ctlfd = -1; 2429 P->statfd = -1; 2430 P->agentctlfd = -1; 2431 P->agentstatfd = -1; 2432 P->zoneroot = NULL; 2433 P->info_valid = 1; 2434 Pinit_ops(&P->ops, &P_core_ops); 2435 2436 Pinitsym(P); 2437 Pinitfd(P); 2438 2439 /* 2440 * Fstat and open the core file and make sure it is a valid ELF core. 2441 */ 2442 if (fstat64(P->asfd, &stbuf) == -1) { 2443 *perr = G_STRANGE; 2444 goto err; 2445 } 2446 2447 if (core_elf_fdopen(&core, ET_CORE, perr) == -1) 2448 goto err; 2449 2450 /* 2451 * Allocate and initialize a core_info_t to hang off the ps_prochandle 2452 * structure. We keep all core-specific information in this structure. 2453 */ 2454 if ((core_info = calloc(1, sizeof (core_info_t))) == NULL) { 2455 *perr = G_STRANGE; 2456 goto err; 2457 } 2458 2459 P->data = core_info; 2460 list_create(&core_info->core_lwp_head, sizeof (lwp_info_t), 2461 offsetof(lwp_info_t, lwp_list)); 2462 core_info->core_size = stbuf.st_size; 2463 /* 2464 * In the days before adjustable core file content, this was the 2465 * default core file content. For new core files, this value will 2466 * be overwritten by the NT_CONTENT note section. 2467 */ 2468 core_info->core_content = CC_CONTENT_STACK | CC_CONTENT_HEAP | 2469 CC_CONTENT_DATA | CC_CONTENT_RODATA | CC_CONTENT_ANON | 2470 CC_CONTENT_SHANON; 2471 2472 switch (core.e_hdr.e_ident[EI_CLASS]) { 2473 case ELFCLASS32: 2474 core_info->core_dmodel = PR_MODEL_ILP32; 2475 break; 2476 case ELFCLASS64: 2477 core_info->core_dmodel = PR_MODEL_LP64; 2478 break; 2479 default: 2480 *perr = G_FORMAT; 2481 goto err; 2482 } 2483 core_info->core_osabi = core.e_hdr.e_ident[EI_OSABI]; 2484 2485 /* 2486 * Because the core file may be a large file, we can't use libelf to 2487 * read the Phdrs. We use e_phnum and e_phentsize to simplify things. 2488 */ 2489 nbytes = core.e_hdr.e_phnum * core.e_hdr.e_phentsize; 2490 2491 if ((phbuf = malloc(nbytes)) == NULL) { 2492 *perr = G_STRANGE; 2493 goto err; 2494 } 2495 2496 if (pread64(core_fd, phbuf, nbytes, core.e_hdr.e_phoff) != nbytes) { 2497 *perr = G_STRANGE; 2498 free(phbuf); 2499 goto err; 2500 } 2501 2502 /* 2503 * Iterate through the program headers in the core file. 2504 * We're interested in two types of Phdrs: PT_NOTE (which 2505 * contains a set of saved /proc structures), and PT_LOAD (which 2506 * represents a memory mapping from the process's address space). 2507 * In the case of PT_NOTE, we're interested in the last PT_NOTE 2508 * in the core file; currently the first PT_NOTE (if present) 2509 * contains /proc structs in the pre-2.6 unstructured /proc format. 2510 */ 2511 for (php = phbuf, notes = 0, i = 0; i < core.e_hdr.e_phnum; i++) { 2512 if (core.e_hdr.e_ident[EI_CLASS] == ELFCLASS64) 2513 (void) memcpy(&phdr, php, sizeof (GElf_Phdr)); 2514 else 2515 core_phdr_to_gelf(php, &phdr); 2516 2517 switch (phdr.p_type) { 2518 case PT_NOTE: 2519 note_phdr = phdr; 2520 notes++; 2521 break; 2522 2523 case PT_LOAD: 2524 if (core_add_mapping(P, &phdr) == -1) { 2525 *perr = G_STRANGE; 2526 free(phbuf); 2527 goto err; 2528 } 2529 break; 2530 default: 2531 Pdprintf("Pgrab_core: unknown phdr %d\n", phdr.p_type); 2532 break; 2533 } 2534 2535 php = (char *)php + core.e_hdr.e_phentsize; 2536 } 2537 2538 free(phbuf); 2539 2540 Psort_mappings(P); 2541 2542 /* 2543 * If we couldn't find anything of type PT_NOTE, or only one PT_NOTE 2544 * was present, abort. The core file is either corrupt or too old. 2545 */ 2546 if (notes == 0 || (notes == 1 && core_info->core_osabi == 2547 ELFOSABI_SOLARIS)) { 2548 *perr = G_NOTE; 2549 goto err; 2550 } 2551 2552 /* 2553 * Advance the seek pointer to the start of the PT_NOTE data 2554 */ 2555 if (lseek64(P->asfd, note_phdr.p_offset, SEEK_SET) == (off64_t)-1) { 2556 Pdprintf("Pgrab_core: failed to lseek to PT_NOTE data\n"); 2557 *perr = G_STRANGE; 2558 goto err; 2559 } 2560 2561 /* 2562 * Now process the PT_NOTE structures. Each one is preceded by 2563 * an Elf{32/64}_Nhdr structure describing its type and size. 2564 * 2565 * +--------+ 2566 * | header | 2567 * +--------+ 2568 * | name | 2569 * | ... | 2570 * +--------+ 2571 * | desc | 2572 * | ... | 2573 * +--------+ 2574 */ 2575 for (nleft = note_phdr.p_filesz; nleft > 0; ) { 2576 Elf64_Nhdr nhdr; 2577 off64_t off, namesz, descsz; 2578 2579 /* 2580 * Although <sys/elf.h> defines both Elf32_Nhdr and Elf64_Nhdr 2581 * as different types, they are both of the same content and 2582 * size, so we don't need to worry about 32/64 conversion here. 2583 */ 2584 if (read(P->asfd, &nhdr, sizeof (nhdr)) != sizeof (nhdr)) { 2585 Pdprintf( 2586 "Pgrab_core: failed to read ELF note header\n"); 2587 *perr = G_NOTE; 2588 goto err; 2589 } 2590 2591 /* 2592 * According to the System V ABI, the amount of padding 2593 * following the name field should align the description 2594 * field on a 4 byte boundary for 32-bit binaries or on an 8 2595 * byte boundary for 64-bit binaries. However, this change 2596 * was not made correctly during the 64-bit port so all 2597 * descriptions can assume only 4-byte alignment. We ignore 2598 * the name field and the padding to 4-byte alignment. 2599 */ 2600 namesz = P2ROUNDUP((off64_t)nhdr.n_namesz, (off64_t)4); 2601 2602 if (lseek64(P->asfd, namesz, SEEK_CUR) == (off64_t)-1) { 2603 Pdprintf("failed to seek past name and padding\n"); 2604 *perr = G_STRANGE; 2605 goto err; 2606 } 2607 2608 Pdprintf("Note hdr n_type=%u n_namesz=%u n_descsz=%u\n", 2609 nhdr.n_type, nhdr.n_namesz, nhdr.n_descsz); 2610 2611 off = lseek64(P->asfd, (off64_t)0L, SEEK_CUR); 2612 2613 /* 2614 * Invoke the note handler function from our table 2615 */ 2616 if (nhdr.n_type < ARRAY_SIZE(nhdlrs) && 2617 nhdlrs[nhdr.n_type] != NULL) { 2618 if (nhdlrs[nhdr.n_type](P, nhdr.n_descsz) < 0) { 2619 Pdprintf("handler for type %d returned < 0", 2620 nhdr.n_type); 2621 *perr = G_NOTE; 2622 goto err; 2623 } 2624 /* 2625 * The presence of either of these notes indicates that 2626 * the dump was generated on Linux. 2627 */ 2628 #ifdef __x86 2629 if (nhdr.n_type == NT_PRSTATUS || 2630 nhdr.n_type == NT_PRPSINFO) 2631 from_linux = B_TRUE; 2632 #endif 2633 } else { 2634 (void) note_notsup(P, nhdr.n_descsz); 2635 } 2636 2637 /* 2638 * Seek past the current note data to the next Elf_Nhdr 2639 */ 2640 descsz = P2ROUNDUP((off64_t)nhdr.n_descsz, (off64_t)4); 2641 if (lseek64(P->asfd, off + descsz, SEEK_SET) == (off64_t)-1) { 2642 Pdprintf("Pgrab_core: failed to seek to next nhdr\n"); 2643 *perr = G_STRANGE; 2644 goto err; 2645 } 2646 2647 /* 2648 * Subtract the size of the header and its data from what 2649 * we have left to process. 2650 */ 2651 nleft -= sizeof (nhdr) + namesz + descsz; 2652 } 2653 2654 #ifdef __x86 2655 if (from_linux) { 2656 size_t pid; 2657 lwp_info_t *lwp; 2658 2659 P->status.pr_dmodel = core_info->core_dmodel; 2660 2661 pid = P->status.pr_pid; 2662 2663 for (lwp = list_head(&core_info->core_lwp_head); lwp != NULL; 2664 lwp = list_next(&core_info->core_lwp_head, lwp)) { 2665 Pdprintf("Linux thread with id %d\n", lwp->lwp_id); 2666 2667 /* 2668 * In the case we don't have a valid psinfo (i.e. pid is 2669 * 0, probably because of gdb creating the core) assume 2670 * lowest pid count is the first thread (what if the 2671 * next thread wraps the pid around?) 2672 */ 2673 if (P->status.pr_pid == 0 && 2674 ((pid == 0 && lwp->lwp_id > 0) || 2675 (lwp->lwp_id < pid))) { 2676 pid = lwp->lwp_id; 2677 } 2678 } 2679 2680 if (P->status.pr_pid != pid) { 2681 Pdprintf("No valid pid, setting to %ld\n", 2682 (ulong_t)pid); 2683 P->status.pr_pid = pid; 2684 P->psinfo.pr_pid = pid; 2685 } 2686 2687 /* 2688 * Consumers like mdb expect the first thread to actually have 2689 * an id of 1, on linux that is actually the pid. Find the the 2690 * thread with our process id, and set the id to 1 2691 */ 2692 if ((lwp = lwpid2info(P, pid)) == NULL) { 2693 Pdprintf("Couldn't find first thread\n"); 2694 *perr = G_STRANGE; 2695 goto err; 2696 } 2697 2698 Pdprintf("setting representative thread: %d\n", lwp->lwp_id); 2699 2700 lwp->lwp_id = 1; 2701 lwp->lwp_status.pr_lwpid = 1; 2702 2703 /* set representative thread */ 2704 (void) memcpy(&P->status.pr_lwp, &lwp->lwp_status, 2705 sizeof (P->status.pr_lwp)); 2706 } 2707 #endif /* __x86 */ 2708 2709 if (nleft != 0) { 2710 Pdprintf("Pgrab_core: note section malformed\n"); 2711 *perr = G_STRANGE; 2712 goto err; 2713 } 2714 2715 /* 2716 * Now that we can get AT_PAGESZ we can fill in the correct pr_pagesize 2717 * for each mapping. 2718 */ 2719 if ((pagesize = Pgetauxval(P, AT_PAGESZ)) == -1) { 2720 pagesize = getpagesize(); 2721 Pdprintf("AT_PAGESZ missing; defaulting to %d\n", pagesize); 2722 } 2723 2724 for (i = 0; i < P->map_count; i++) { 2725 P->mappings[i].map_pmap.pr_pagesize = pagesize; 2726 } 2727 2728 /* 2729 * Locate and label the mappings corresponding to the end of the 2730 * heap (MA_BREAK) and the base of the stack (MA_STACK). 2731 */ 2732 if ((P->status.pr_brkbase != 0 || P->status.pr_brksize != 0) && 2733 (brk_mp = Paddr2mptr(P, P->status.pr_brkbase + 2734 P->status.pr_brksize - 1)) != NULL) 2735 brk_mp->map_pmap.pr_mflags |= MA_BREAK; 2736 else 2737 brk_mp = NULL; 2738 2739 if ((stk_mp = Paddr2mptr(P, P->status.pr_stkbase)) != NULL) 2740 stk_mp->map_pmap.pr_mflags |= MA_STACK; 2741 2742 /* 2743 * At this point, we have enough information to look for the 2744 * executable and open it: we have access to the auxv, a psinfo_t, 2745 * and the ability to read from mappings provided by the core file. 2746 */ 2747 (void) Pfindexec(P, aout_path, core_exec_open, &aout); 2748 Pdprintf("P->execname = \"%s\"\n", P->execname ? P->execname : "NULL"); 2749 execname = P->execname ? P->execname : "a.out"; 2750 2751 /* 2752 * Iterate through the sections, looking for the .dynamic and .interp 2753 * sections. If we encounter them, remember their section pointers. 2754 */ 2755 for (scn = NULL; (scn = elf_nextscn(aout.e_elf, scn)) != NULL; ) { 2756 char *sname; 2757 2758 if ((gelf_getshdr(scn, &shdr) == NULL) || 2759 (sname = elf_strptr(aout.e_elf, aout.e_hdr.e_shstrndx, 2760 (size_t)shdr.sh_name)) == NULL) 2761 continue; 2762 2763 if (strcmp(sname, ".interp") == 0) 2764 intp_scn = scn; 2765 } 2766 2767 /* 2768 * Get the AT_BASE auxv element. If this is missing (-1), then 2769 * we assume this is a statically-linked executable. 2770 */ 2771 base_addr = Pgetauxval(P, AT_BASE); 2772 2773 /* 2774 * In order to get librtld_db initialized, we'll need to identify 2775 * and name the mapping corresponding to the run-time linker. The 2776 * AT_BASE auxv element tells us the address where it was mapped, 2777 * and the .interp section of the executable tells us its path. 2778 * If for some reason that doesn't pan out, just use ld.so.1. 2779 */ 2780 if (intp_scn != NULL && (dp = elf_getdata(intp_scn, NULL)) != NULL && 2781 dp->d_size != 0) { 2782 Pdprintf(".interp = <%s>\n", (char *)dp->d_buf); 2783 interp = dp->d_buf; 2784 2785 } else if (base_addr != (uintptr_t)-1L) { 2786 if (core_info->core_dmodel == PR_MODEL_LP64) 2787 interp = "/usr/lib/64/ld.so.1"; 2788 else 2789 interp = "/usr/lib/ld.so.1"; 2790 2791 Pdprintf(".interp section is missing or could not be read; " 2792 "defaulting to %s\n", interp); 2793 } else 2794 Pdprintf("detected statically linked executable\n"); 2795 2796 /* 2797 * If we have an AT_BASE element, name the mapping at that address 2798 * using the interpreter pathname. Name the corresponding data 2799 * mapping after the interpreter as well. 2800 */ 2801 if (base_addr != (uintptr_t)-1L) { 2802 elf_file_t intf; 2803 2804 P->map_ldso = core_name_mapping(P, base_addr, interp); 2805 2806 if (core_elf_open(&intf, interp, ET_DYN, NULL) == 0) { 2807 rd_loadobj_t rl; 2808 map_info_t *dmp; 2809 2810 rl.rl_base = base_addr; 2811 dmp = core_find_data(P, intf.e_elf, &rl); 2812 2813 if (dmp != NULL) { 2814 Pdprintf("renamed data at %p to %s\n", 2815 (void *)rl.rl_data_base, interp); 2816 (void) strncpy(dmp->map_pmap.pr_mapname, 2817 interp, PRMAPSZ); 2818 dmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0'; 2819 } 2820 } 2821 2822 core_elf_close(&intf); 2823 } 2824 2825 /* 2826 * If we have an AT_ENTRY element, name the mapping at that address 2827 * using the special name "a.out" just like /proc does. 2828 */ 2829 if ((addr = Pgetauxval(P, AT_ENTRY)) != (uintptr_t)-1L) 2830 P->map_exec = core_name_mapping(P, addr, "a.out"); 2831 2832 /* 2833 * If we're a statically linked executable (or we're on x86 and looking 2834 * at a Linux core dump), then just locate the executable's text and 2835 * data and name them after the executable. 2836 */ 2837 #ifndef __x86 2838 if (base_addr == (uintptr_t)-1L) { 2839 #else 2840 if (base_addr == (uintptr_t)-1L || from_linux) { 2841 #endif 2842 Pdprintf("looking for text and data: %s\n", execname); 2843 map_info_t *tmp, *dmp; 2844 file_info_t *fp; 2845 rd_loadobj_t rl; 2846 2847 if ((tmp = core_find_text(P, aout.e_elf, &rl)) != NULL && 2848 (dmp = core_find_data(P, aout.e_elf, &rl)) != NULL) { 2849 (void) strncpy(tmp->map_pmap.pr_mapname, 2850 execname, PRMAPSZ); 2851 tmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0'; 2852 (void) strncpy(dmp->map_pmap.pr_mapname, 2853 execname, PRMAPSZ); 2854 dmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0'; 2855 } 2856 2857 if ((P->map_exec = tmp) != NULL && 2858 (fp = malloc(sizeof (file_info_t))) != NULL) { 2859 2860 (void) memset(fp, 0, sizeof (file_info_t)); 2861 2862 list_insert_head(&P->file_head, fp); 2863 tmp->map_file = fp; 2864 P->num_files++; 2865 2866 fp->file_ref = 1; 2867 fp->file_fd = -1; 2868 fp->file_dbgfile = -1; 2869 2870 fp->file_lo = malloc(sizeof (rd_loadobj_t)); 2871 fp->file_lname = strdup(execname); 2872 2873 if (fp->file_lo) 2874 *fp->file_lo = rl; 2875 if (fp->file_lname) 2876 fp->file_lbase = basename(fp->file_lname); 2877 if (fp->file_rname) 2878 fp->file_rbase = basename(fp->file_rname); 2879 2880 (void) strcpy(fp->file_pname, 2881 P->mappings[0].map_pmap.pr_mapname); 2882 fp->file_map = tmp; 2883 2884 Pbuild_file_symtab(P, fp); 2885 2886 if (dmp != NULL) { 2887 dmp->map_file = fp; 2888 fp->file_ref++; 2889 } 2890 } 2891 } 2892 2893 core_elf_close(&aout); 2894 2895 /* 2896 * We now have enough information to initialize librtld_db. 2897 * After it warms up, we can iterate through the load object chain 2898 * in the core, which will allow us to construct the file info 2899 * we need to provide symbol information for the other shared 2900 * libraries, and also to fill in the missing mapping names. 2901 */ 2902 rd_log(_libproc_debug); 2903 2904 if ((P->rap = rd_new(P)) != NULL) { 2905 (void) rd_loadobj_iter(P->rap, (rl_iter_f *) 2906 core_iter_mapping, P); 2907 2908 if (core_info->core_errno != 0) { 2909 errno = core_info->core_errno; 2910 *perr = G_STRANGE; 2911 goto err; 2912 } 2913 } else 2914 Pdprintf("failed to initialize rtld_db agent\n"); 2915 2916 /* 2917 * If there are sections, load them and process the data from any 2918 * sections that we can use to annotate the file_info_t's. 2919 */ 2920 core_load_shdrs(P, &core); 2921 2922 /* 2923 * If we previously located a stack or break mapping, and they are 2924 * still anonymous, we now assume that they were MAP_ANON mappings. 2925 * If brk_mp turns out to now have a name, then the heap is still 2926 * sitting at the end of the executable's data+bss mapping: remove 2927 * the previous MA_BREAK setting to be consistent with /proc. 2928 */ 2929 if (stk_mp != NULL && stk_mp->map_pmap.pr_mapname[0] == '\0') 2930 stk_mp->map_pmap.pr_mflags |= MA_ANON; 2931 if (brk_mp != NULL && brk_mp->map_pmap.pr_mapname[0] == '\0') 2932 brk_mp->map_pmap.pr_mflags |= MA_ANON; 2933 else if (brk_mp != NULL) 2934 brk_mp->map_pmap.pr_mflags &= ~MA_BREAK; 2935 2936 *perr = 0; 2937 return (P); 2938 2939 err: 2940 Pfree(P); 2941 core_elf_close(&aout); 2942 return (NULL); 2943 } 2944 2945 /* 2946 * Grab a core file using a pathname. We just open it and call Pfgrab_core(). 2947 */ 2948 struct ps_prochandle * 2949 Pgrab_core(const char *core, const char *aout, int gflag, int *perr) 2950 { 2951 int fd, oflag = (gflag & PGRAB_RDONLY) ? O_RDONLY : O_RDWR; 2952 2953 if ((fd = open64(core, oflag)) >= 0) 2954 return (Pfgrab_core(fd, aout, perr)); 2955 2956 if (errno != ENOENT) 2957 *perr = G_STRANGE; 2958 else 2959 *perr = G_NOCORE; 2960 2961 return (NULL); 2962 } 2963 2964 int 2965 Pupanic(struct ps_prochandle *P, prupanic_t **pru) 2966 { 2967 core_info_t *core; 2968 2969 if (P->state != PS_DEAD) { 2970 errno = ENODATA; 2971 return (-1); 2972 } 2973 2974 core = P->data; 2975 if (core->core_upanic == NULL) { 2976 errno = ENOENT; 2977 return (-1); 2978 } 2979 2980 if (core->core_upanic->pru_version != PRUPANIC_VERSION_1) { 2981 errno = EINVAL; 2982 return (-1); 2983 } 2984 2985 if ((*pru = calloc(1, sizeof (prupanic_t))) == NULL) 2986 return (-1); 2987 (void) memcpy(*pru, core->core_upanic, sizeof (prupanic_t)); 2988 2989 return (0); 2990 } 2991 2992 void 2993 Pupanic_free(prupanic_t *pru) 2994 { 2995 free(pru); 2996 } 2997