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