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