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