Print this page
XXX AVX procfs
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libproc/common/Pcore.c
+++ new/usr/src/lib/libproc/common/Pcore.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25 /*
26 26 * Copyright 2012 DEY Storage Systems, Inc. All rights reserved.
27 27 */
28 28
29 29 #include <sys/types.h>
30 30 #include <sys/utsname.h>
31 31 #include <sys/sysmacros.h>
32 32
33 33 #include <alloca.h>
34 34 #include <rtld_db.h>
35 35 #include <libgen.h>
36 36 #include <limits.h>
37 37 #include <string.h>
38 38 #include <stdlib.h>
39 39 #include <unistd.h>
40 40 #include <errno.h>
41 41 #include <gelf.h>
42 42 #include <stddef.h>
43 43
44 44 #include "libproc.h"
45 45 #include "Pcontrol.h"
46 46 #include "P32ton.h"
47 47 #include "Putil.h"
48 48
49 49 /*
50 50 * Pcore.c - Code to initialize a ps_prochandle from a core dump. We
51 51 * allocate an additional structure to hold information from the core
52 52 * file, and attach this to the standard ps_prochandle in place of the
53 53 * ability to examine /proc/<pid>/ files.
54 54 */
55 55
56 56 /*
57 57 * Basic i/o function for reading and writing from the process address space
58 58 * stored in the core file and associated shared libraries. We compute the
59 59 * appropriate fd and offsets, and let the provided prw function do the rest.
60 60 */
61 61 static ssize_t
62 62 core_rw(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr,
63 63 ssize_t (*prw)(int, void *, size_t, off64_t))
64 64 {
65 65 ssize_t resid = n;
66 66
67 67 while (resid != 0) {
68 68 map_info_t *mp = Paddr2mptr(P, addr);
69 69
70 70 uintptr_t mapoff;
71 71 ssize_t len;
72 72 off64_t off;
73 73 int fd;
74 74
75 75 if (mp == NULL)
76 76 break; /* No mapping for this address */
77 77
78 78 if (mp->map_pmap.pr_mflags & MA_RESERVED1) {
79 79 if (mp->map_file == NULL || mp->map_file->file_fd < 0)
80 80 break; /* No file or file not open */
81 81
82 82 fd = mp->map_file->file_fd;
83 83 } else
84 84 fd = P->asfd;
85 85
86 86 mapoff = addr - mp->map_pmap.pr_vaddr;
87 87 len = MIN(resid, mp->map_pmap.pr_size - mapoff);
88 88 off = mp->map_offset + mapoff;
89 89
90 90 if ((len = prw(fd, buf, len, off)) <= 0)
91 91 break;
92 92
93 93 resid -= len;
94 94 addr += len;
95 95 buf = (char *)buf + len;
96 96 }
97 97
98 98 /*
99 99 * Important: Be consistent with the behavior of i/o on the as file:
100 100 * writing to an invalid address yields EIO; reading from an invalid
101 101 * address falls through to returning success and zero bytes.
102 102 */
103 103 if (resid == n && n != 0 && prw != pread64) {
104 104 errno = EIO;
105 105 return (-1);
106 106 }
107 107
108 108 return (n - resid);
109 109 }
110 110
111 111 static ssize_t
112 112 Pread_core(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr)
113 113 {
114 114 return (core_rw(P, buf, n, addr, pread64));
115 115 }
116 116
117 117 static ssize_t
118 118 Pwrite_core(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr)
119 119 {
120 120 return (core_rw(P, (void *)buf, n, addr,
121 121 (ssize_t (*)(int, void *, size_t, off64_t)) pwrite64));
122 122 }
123 123
124 124 static const ps_rwops_t P_core_ops = { Pread_core, Pwrite_core };
125 125
126 126 /*
127 127 * Return the lwp_info_t for the given lwpid. If no such lwpid has been
128 128 * encountered yet, allocate a new structure and return a pointer to it.
129 129 * Create a list of lwp_info_t structures sorted in decreasing lwp_id order.
130 130 */
131 131 static lwp_info_t *
132 132 lwpid2info(struct ps_prochandle *P, lwpid_t id)
133 133 {
134 134 lwp_info_t *lwp = list_next(&P->core->core_lwp_head);
135 135 lwp_info_t *next;
136 136 uint_t i;
137 137
138 138 for (i = 0; i < P->core->core_nlwp; i++, lwp = list_next(lwp)) {
139 139 if (lwp->lwp_id == id) {
140 140 P->core->core_lwp = lwp;
141 141 return (lwp);
142 142 }
143 143 if (lwp->lwp_id < id) {
144 144 break;
145 145 }
146 146 }
147 147
148 148 next = lwp;
149 149 if ((lwp = calloc(1, sizeof (lwp_info_t))) == NULL)
150 150 return (NULL);
151 151
152 152 list_link(lwp, next);
153 153 lwp->lwp_id = id;
154 154
155 155 P->core->core_lwp = lwp;
156 156 P->core->core_nlwp++;
157 157
158 158 return (lwp);
159 159 }
160 160
161 161 /*
162 162 * The core file itself contains a series of NOTE segments containing saved
163 163 * structures from /proc at the time the process died. For each note we
164 164 * comprehend, we define a function to read it in from the core file,
165 165 * convert it to our native data model if necessary, and store it inside
166 166 * the ps_prochandle. Each function is invoked by Pfgrab_core() with the
167 167 * seek pointer on P->asfd positioned appropriately. We populate a table
168 168 * of pointers to these note functions below.
169 169 */
170 170
171 171 static int
172 172 note_pstatus(struct ps_prochandle *P, size_t nbytes)
173 173 {
174 174 #ifdef _LP64
175 175 if (P->core->core_dmodel == PR_MODEL_ILP32) {
176 176 pstatus32_t ps32;
177 177
178 178 if (nbytes < sizeof (pstatus32_t) ||
179 179 read(P->asfd, &ps32, sizeof (ps32)) != sizeof (ps32))
180 180 goto err;
181 181
182 182 pstatus_32_to_n(&ps32, &P->status);
183 183
184 184 } else
185 185 #endif
186 186 if (nbytes < sizeof (pstatus_t) ||
187 187 read(P->asfd, &P->status, sizeof (pstatus_t)) != sizeof (pstatus_t))
188 188 goto err;
189 189
190 190 P->orig_status = P->status;
191 191 P->pid = P->status.pr_pid;
192 192
193 193 return (0);
194 194
195 195 err:
196 196 dprintf("Pgrab_core: failed to read NT_PSTATUS\n");
197 197 return (-1);
198 198 }
199 199
200 200 static int
201 201 note_lwpstatus(struct ps_prochandle *P, size_t nbytes)
202 202 {
203 203 lwp_info_t *lwp;
204 204 lwpstatus_t lps;
205 205
206 206 #ifdef _LP64
207 207 if (P->core->core_dmodel == PR_MODEL_ILP32) {
208 208 lwpstatus32_t l32;
209 209
210 210 if (nbytes < sizeof (lwpstatus32_t) ||
211 211 read(P->asfd, &l32, sizeof (l32)) != sizeof (l32))
212 212 goto err;
213 213
214 214 lwpstatus_32_to_n(&l32, &lps);
215 215 } else
216 216 #endif
217 217 if (nbytes < sizeof (lwpstatus_t) ||
218 218 read(P->asfd, &lps, sizeof (lps)) != sizeof (lps))
219 219 goto err;
220 220
221 221 if ((lwp = lwpid2info(P, lps.pr_lwpid)) == NULL) {
222 222 dprintf("Pgrab_core: failed to add NT_LWPSTATUS\n");
223 223 return (-1);
224 224 }
225 225
226 226 /*
227 227 * Erase a useless and confusing artifact of the kernel implementation:
228 228 * the lwps which did *not* create the core will show SIGKILL. We can
229 229 * be assured this is bogus because SIGKILL can't produce core files.
230 230 */
231 231 if (lps.pr_cursig == SIGKILL)
232 232 lps.pr_cursig = 0;
233 233
234 234 (void) memcpy(&lwp->lwp_status, &lps, sizeof (lps));
235 235 return (0);
236 236
237 237 err:
238 238 dprintf("Pgrab_core: failed to read NT_LWPSTATUS\n");
239 239 return (-1);
240 240 }
241 241
242 242 static int
243 243 note_psinfo(struct ps_prochandle *P, size_t nbytes)
244 244 {
245 245 #ifdef _LP64
246 246 if (P->core->core_dmodel == PR_MODEL_ILP32) {
247 247 psinfo32_t ps32;
248 248
249 249 if (nbytes < sizeof (psinfo32_t) ||
250 250 read(P->asfd, &ps32, sizeof (ps32)) != sizeof (ps32))
251 251 goto err;
252 252
253 253 psinfo_32_to_n(&ps32, &P->psinfo);
254 254 } else
255 255 #endif
256 256 if (nbytes < sizeof (psinfo_t) ||
257 257 read(P->asfd, &P->psinfo, sizeof (psinfo_t)) != sizeof (psinfo_t))
258 258 goto err;
259 259
260 260 dprintf("pr_fname = <%s>\n", P->psinfo.pr_fname);
261 261 dprintf("pr_psargs = <%s>\n", P->psinfo.pr_psargs);
262 262 dprintf("pr_wstat = 0x%x\n", P->psinfo.pr_wstat);
263 263
264 264 return (0);
265 265
266 266 err:
267 267 dprintf("Pgrab_core: failed to read NT_PSINFO\n");
268 268 return (-1);
269 269 }
270 270
271 271 static int
272 272 note_lwpsinfo(struct ps_prochandle *P, size_t nbytes)
273 273 {
274 274 lwp_info_t *lwp;
275 275 lwpsinfo_t lps;
276 276
277 277 #ifdef _LP64
278 278 if (P->core->core_dmodel == PR_MODEL_ILP32) {
279 279 lwpsinfo32_t l32;
280 280
281 281 if (nbytes < sizeof (lwpsinfo32_t) ||
282 282 read(P->asfd, &l32, sizeof (l32)) != sizeof (l32))
283 283 goto err;
284 284
285 285 lwpsinfo_32_to_n(&l32, &lps);
286 286 } else
287 287 #endif
288 288 if (nbytes < sizeof (lwpsinfo_t) ||
289 289 read(P->asfd, &lps, sizeof (lps)) != sizeof (lps))
290 290 goto err;
291 291
292 292 if ((lwp = lwpid2info(P, lps.pr_lwpid)) == NULL) {
293 293 dprintf("Pgrab_core: failed to add NT_LWPSINFO\n");
294 294 return (-1);
295 295 }
296 296
297 297 (void) memcpy(&lwp->lwp_psinfo, &lps, sizeof (lps));
298 298 return (0);
299 299
300 300 err:
301 301 dprintf("Pgrab_core: failed to read NT_LWPSINFO\n");
302 302 return (-1);
303 303 }
304 304
305 305 static int
306 306 note_fdinfo(struct ps_prochandle *P, size_t nbytes)
307 307 {
308 308 prfdinfo_t prfd;
309 309 fd_info_t *fip;
310 310
311 311 if ((nbytes < sizeof (prfd)) ||
312 312 (read(P->asfd, &prfd, sizeof (prfd)) != sizeof (prfd))) {
313 313 dprintf("Pgrab_core: failed to read NT_FDINFO\n");
314 314 return (-1);
315 315 }
316 316
317 317 if ((fip = Pfd2info(P, prfd.pr_fd)) == NULL) {
318 318 dprintf("Pgrab_core: failed to add NT_FDINFO\n");
319 319 return (-1);
320 320 }
321 321 (void) memcpy(&fip->fd_info, &prfd, sizeof (prfd));
322 322 return (0);
323 323 }
324 324
325 325 static int
326 326 note_platform(struct ps_prochandle *P, size_t nbytes)
327 327 {
328 328 char *plat;
329 329
330 330 if (P->core->core_platform != NULL)
331 331 return (0); /* Already seen */
332 332
333 333 if (nbytes != 0 && ((plat = malloc(nbytes + 1)) != NULL)) {
334 334 if (read(P->asfd, plat, nbytes) != nbytes) {
335 335 dprintf("Pgrab_core: failed to read NT_PLATFORM\n");
336 336 free(plat);
337 337 return (-1);
338 338 }
339 339 plat[nbytes - 1] = '\0';
340 340 P->core->core_platform = plat;
341 341 }
342 342
343 343 return (0);
344 344 }
345 345
346 346 static int
347 347 note_utsname(struct ps_prochandle *P, size_t nbytes)
348 348 {
349 349 size_t ubytes = sizeof (struct utsname);
350 350 struct utsname *utsp;
351 351
352 352 if (P->core->core_uts != NULL || nbytes < ubytes)
353 353 return (0); /* Already seen or bad size */
354 354
355 355 if ((utsp = malloc(ubytes)) == NULL)
356 356 return (-1);
357 357
358 358 if (read(P->asfd, utsp, ubytes) != ubytes) {
359 359 dprintf("Pgrab_core: failed to read NT_UTSNAME\n");
360 360 free(utsp);
361 361 return (-1);
362 362 }
363 363
364 364 if (_libproc_debug) {
365 365 dprintf("uts.sysname = \"%s\"\n", utsp->sysname);
366 366 dprintf("uts.nodename = \"%s\"\n", utsp->nodename);
367 367 dprintf("uts.release = \"%s\"\n", utsp->release);
368 368 dprintf("uts.version = \"%s\"\n", utsp->version);
369 369 dprintf("uts.machine = \"%s\"\n", utsp->machine);
370 370 }
371 371
372 372 P->core->core_uts = utsp;
373 373 return (0);
374 374 }
375 375
376 376 static int
377 377 note_content(struct ps_prochandle *P, size_t nbytes)
378 378 {
379 379 core_content_t content;
380 380
381 381 if (sizeof (P->core->core_content) != nbytes)
382 382 return (-1);
383 383
384 384 if (read(P->asfd, &content, sizeof (content)) != sizeof (content))
385 385 return (-1);
386 386
387 387 P->core->core_content = content;
388 388
389 389 dprintf("core content = %llx\n", content);
390 390
391 391 return (0);
392 392 }
393 393
394 394 static int
395 395 note_cred(struct ps_prochandle *P, size_t nbytes)
396 396 {
397 397 prcred_t *pcrp;
398 398 int ngroups;
399 399 const size_t min_size = sizeof (prcred_t) - sizeof (gid_t);
400 400
401 401 /*
402 402 * We allow for prcred_t notes that are actually smaller than a
403 403 * prcred_t since the last member isn't essential if there are
404 404 * no group memberships. This allows for more flexibility when it
405 405 * comes to slightly malformed -- but still valid -- notes.
406 406 */
407 407 if (P->core->core_cred != NULL || nbytes < min_size)
408 408 return (0); /* Already seen or bad size */
409 409
410 410 ngroups = (nbytes - min_size) / sizeof (gid_t);
411 411 nbytes = sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t);
412 412
413 413 if ((pcrp = malloc(nbytes)) == NULL)
414 414 return (-1);
415 415
416 416 if (read(P->asfd, pcrp, nbytes) != nbytes) {
417 417 dprintf("Pgrab_core: failed to read NT_PRCRED\n");
418 418 free(pcrp);
419 419 return (-1);
420 420 }
421 421
422 422 if (pcrp->pr_ngroups > ngroups) {
423 423 dprintf("pr_ngroups = %d; resetting to %d based on note size\n",
424 424 pcrp->pr_ngroups, ngroups);
425 425 pcrp->pr_ngroups = ngroups;
426 426 }
427 427
428 428 P->core->core_cred = pcrp;
429 429 return (0);
430 430 }
431 431
432 432 #if defined(__i386) || defined(__amd64)
433 433 static int
434 434 note_ldt(struct ps_prochandle *P, size_t nbytes)
435 435 {
436 436 struct ssd *pldt;
437 437 uint_t nldt;
438 438
439 439 if (P->core->core_ldt != NULL || nbytes < sizeof (struct ssd))
440 440 return (0); /* Already seen or bad size */
441 441
442 442 nldt = nbytes / sizeof (struct ssd);
443 443 nbytes = nldt * sizeof (struct ssd);
444 444
445 445 if ((pldt = malloc(nbytes)) == NULL)
446 446 return (-1);
447 447
448 448 if (read(P->asfd, pldt, nbytes) != nbytes) {
449 449 dprintf("Pgrab_core: failed to read NT_LDT\n");
450 450 free(pldt);
451 451 return (-1);
452 452 }
453 453
454 454 P->core->core_ldt = pldt;
455 455 P->core->core_nldt = nldt;
456 456 return (0);
457 457 }
458 458 #endif /* __i386 */
459 459
460 460 static int
461 461 note_priv(struct ps_prochandle *P, size_t nbytes)
462 462 {
463 463 prpriv_t *pprvp;
464 464
465 465 if (P->core->core_priv != NULL || nbytes < sizeof (prpriv_t))
466 466 return (0); /* Already seen or bad size */
467 467
468 468 if ((pprvp = malloc(nbytes)) == NULL)
469 469 return (-1);
470 470
471 471 if (read(P->asfd, pprvp, nbytes) != nbytes) {
472 472 dprintf("Pgrab_core: failed to read NT_PRPRIV\n");
473 473 free(pprvp);
474 474 return (-1);
475 475 }
476 476
477 477 P->core->core_priv = pprvp;
478 478 P->core->core_priv_size = nbytes;
479 479 return (0);
480 480 }
481 481
482 482 static int
483 483 note_priv_info(struct ps_prochandle *P, size_t nbytes)
484 484 {
485 485 extern void *__priv_parse_info();
486 486 priv_impl_info_t *ppii;
487 487
488 488 if (P->core->core_privinfo != NULL ||
489 489 nbytes < sizeof (priv_impl_info_t))
490 490 return (0); /* Already seen or bad size */
491 491
492 492 if ((ppii = malloc(nbytes)) == NULL)
493 493 return (-1);
494 494
495 495 if (read(P->asfd, ppii, nbytes) != nbytes ||
496 496 PRIV_IMPL_INFO_SIZE(ppii) != nbytes) {
497 497 dprintf("Pgrab_core: failed to read NT_PRPRIVINFO\n");
498 498 free(ppii);
499 499 return (-1);
500 500 }
501 501
502 502 P->core->core_privinfo = __priv_parse_info(ppii);
503 503 P->core->core_ppii = ppii;
504 504 return (0);
505 505 }
506 506
507 507 static int
508 508 note_zonename(struct ps_prochandle *P, size_t nbytes)
509 509 {
510 510 char *zonename;
511 511
512 512 if (P->core->core_zonename != NULL)
513 513 return (0); /* Already seen */
514 514
515 515 if (nbytes != 0) {
516 516 if ((zonename = malloc(nbytes)) == NULL)
517 517 return (-1);
518 518 if (read(P->asfd, zonename, nbytes) != nbytes) {
519 519 dprintf("Pgrab_core: failed to read NT_ZONENAME\n");
520 520 free(zonename);
521 521 return (-1);
522 522 }
523 523 zonename[nbytes - 1] = '\0';
524 524 P->core->core_zonename = zonename;
525 525 }
526 526
527 527 return (0);
528 528 }
529 529
530 530 static int
531 531 note_auxv(struct ps_prochandle *P, size_t nbytes)
532 532 {
533 533 size_t n, i;
534 534
535 535 #ifdef _LP64
536 536 if (P->core->core_dmodel == PR_MODEL_ILP32) {
537 537 auxv32_t *a32;
538 538
539 539 n = nbytes / sizeof (auxv32_t);
540 540 nbytes = n * sizeof (auxv32_t);
541 541 a32 = alloca(nbytes);
542 542
543 543 if (read(P->asfd, a32, nbytes) != nbytes) {
544 544 dprintf("Pgrab_core: failed to read NT_AUXV\n");
545 545 return (-1);
546 546 }
547 547
548 548 if ((P->auxv = malloc(sizeof (auxv_t) * (n + 1))) == NULL)
549 549 return (-1);
550 550
551 551 for (i = 0; i < n; i++)
552 552 auxv_32_to_n(&a32[i], &P->auxv[i]);
553 553
554 554 } else {
555 555 #endif
556 556 n = nbytes / sizeof (auxv_t);
557 557 nbytes = n * sizeof (auxv_t);
558 558
559 559 if ((P->auxv = malloc(nbytes + sizeof (auxv_t))) == NULL)
560 560 return (-1);
561 561
562 562 if (read(P->asfd, P->auxv, nbytes) != nbytes) {
563 563 free(P->auxv);
564 564 P->auxv = NULL;
565 565 return (-1);
566 566 }
567 567 #ifdef _LP64
568 568 }
569 569 #endif
570 570
571 571 if (_libproc_debug) {
572 572 for (i = 0; i < n; i++) {
573 573 dprintf("P->auxv[%lu] = ( %d, 0x%lx )\n", (ulong_t)i,
574 574 P->auxv[i].a_type, P->auxv[i].a_un.a_val);
575 575 }
576 576 }
577 577
578 578 /*
579 579 * Defensive coding for loops which depend upon the auxv array being
↓ open down ↓ |
579 lines elided |
↑ open up ↑ |
580 580 * terminated by an AT_NULL element; in each case, we've allocated
581 581 * P->auxv to have an additional element which we force to be AT_NULL.
582 582 */
583 583 P->auxv[n].a_type = AT_NULL;
584 584 P->auxv[n].a_un.a_val = 0L;
585 585 P->nauxv = (int)n;
586 586
587 587 return (0);
588 588 }
589 589
590 -#ifdef __sparc
591 590 static int
592 591 note_xreg(struct ps_prochandle *P, size_t nbytes)
593 592 {
594 593 lwp_info_t *lwp = P->core->core_lwp;
595 594 size_t xbytes = sizeof (prxregset_t);
596 595 prxregset_t *xregs;
597 596
598 597 if (lwp == NULL || lwp->lwp_xregs != NULL || nbytes < xbytes)
599 598 return (0); /* No lwp yet, already seen, or bad size */
600 599
601 600 if ((xregs = malloc(xbytes)) == NULL)
602 601 return (-1);
603 -
602 +#ifdef __sparc
604 603 if (read(P->asfd, xregs, xbytes) != xbytes) {
604 +#else
605 + panic("port me");
606 +#endif
605 607 dprintf("Pgrab_core: failed to read NT_PRXREG\n");
606 608 free(xregs);
607 609 return (-1);
608 610 }
609 -
610 611 lwp->lwp_xregs = xregs;
611 612 return (0);
612 613 }
613 614
615 +#ifdef __sparc
614 616 static int
615 617 note_gwindows(struct ps_prochandle *P, size_t nbytes)
616 618 {
617 619 lwp_info_t *lwp = P->core->core_lwp;
618 620
619 621 if (lwp == NULL || lwp->lwp_gwins != NULL || nbytes == 0)
620 622 return (0); /* No lwp yet or already seen or no data */
621 623
622 624 if ((lwp->lwp_gwins = malloc(sizeof (gwindows_t))) == NULL)
623 625 return (-1);
624 626
625 627 /*
626 628 * Since the amount of gwindows data varies with how many windows were
627 629 * actually saved, we just read up to the minimum of the note size
628 630 * and the size of the gwindows_t type. It doesn't matter if the read
629 631 * fails since we have to zero out gwindows first anyway.
630 632 */
631 633 #ifdef _LP64
632 634 if (P->core->core_dmodel == PR_MODEL_ILP32) {
633 635 gwindows32_t g32;
634 636
635 637 (void) memset(&g32, 0, sizeof (g32));
636 638 (void) read(P->asfd, &g32, MIN(nbytes, sizeof (g32)));
637 639 gwindows_32_to_n(&g32, lwp->lwp_gwins);
638 640
639 641 } else {
640 642 #endif
641 643 (void) memset(lwp->lwp_gwins, 0, sizeof (gwindows_t));
642 644 (void) read(P->asfd, lwp->lwp_gwins,
643 645 MIN(nbytes, sizeof (gwindows_t)));
644 646 #ifdef _LP64
645 647 }
646 648 #endif
647 649 return (0);
648 650 }
649 651
650 652 #ifdef __sparcv9
651 653 static int
652 654 note_asrs(struct ps_prochandle *P, size_t nbytes)
653 655 {
654 656 lwp_info_t *lwp = P->core->core_lwp;
655 657 int64_t *asrs;
656 658
657 659 if (lwp == NULL || lwp->lwp_asrs != NULL || nbytes < sizeof (asrset_t))
658 660 return (0); /* No lwp yet, already seen, or bad size */
659 661
660 662 if ((asrs = malloc(sizeof (asrset_t))) == NULL)
661 663 return (-1);
662 664
663 665 if (read(P->asfd, asrs, sizeof (asrset_t)) != sizeof (asrset_t)) {
664 666 dprintf("Pgrab_core: failed to read NT_ASRS\n");
665 667 free(asrs);
666 668 return (-1);
667 669 }
668 670
669 671 lwp->lwp_asrs = asrs;
670 672 return (0);
671 673 }
672 674 #endif /* __sparcv9 */
673 675 #endif /* __sparc */
674 676
675 677 /*ARGSUSED*/
676 678 static int
677 679 note_notsup(struct ps_prochandle *P, size_t nbytes)
678 680 {
679 681 dprintf("skipping unsupported note type\n");
680 682 return (0);
681 683 }
↓ open down ↓ |
58 lines elided |
↑ open up ↑ |
682 684
683 685 /*
684 686 * Populate a table of function pointers indexed by Note type with our
685 687 * functions to process each type of core file note:
686 688 */
687 689 static int (*nhdlrs[])(struct ps_prochandle *, size_t) = {
688 690 note_notsup, /* 0 unassigned */
689 691 note_notsup, /* 1 NT_PRSTATUS (old) */
690 692 note_notsup, /* 2 NT_PRFPREG (old) */
691 693 note_notsup, /* 3 NT_PRPSINFO (old) */
692 -#ifdef __sparc
693 694 note_xreg, /* 4 NT_PRXREG */
694 -#else
695 - note_notsup, /* 4 NT_PRXREG */
696 -#endif
697 695 note_platform, /* 5 NT_PLATFORM */
698 696 note_auxv, /* 6 NT_AUXV */
699 697 #ifdef __sparc
700 698 note_gwindows, /* 7 NT_GWINDOWS */
701 699 #ifdef __sparcv9
702 700 note_asrs, /* 8 NT_ASRS */
703 701 #else
704 702 note_notsup, /* 8 NT_ASRS */
705 703 #endif
706 704 #else
707 705 note_notsup, /* 7 NT_GWINDOWS */
708 706 note_notsup, /* 8 NT_ASRS */
709 707 #endif
710 708 #if defined(__i386) || defined(__amd64)
711 709 note_ldt, /* 9 NT_LDT */
712 710 #else
713 711 note_notsup, /* 9 NT_LDT */
714 712 #endif
715 713 note_pstatus, /* 10 NT_PSTATUS */
716 714 note_notsup, /* 11 unassigned */
717 715 note_notsup, /* 12 unassigned */
718 716 note_psinfo, /* 13 NT_PSINFO */
719 717 note_cred, /* 14 NT_PRCRED */
720 718 note_utsname, /* 15 NT_UTSNAME */
721 719 note_lwpstatus, /* 16 NT_LWPSTATUS */
722 720 note_lwpsinfo, /* 17 NT_LWPSINFO */
723 721 note_priv, /* 18 NT_PRPRIV */
724 722 note_priv_info, /* 19 NT_PRPRIVINFO */
725 723 note_content, /* 20 NT_CONTENT */
726 724 note_zonename, /* 21 NT_ZONENAME */
727 725 note_fdinfo, /* 22 NT_FDINFO */
728 726 };
729 727
730 728 /*
731 729 * Add information on the address space mapping described by the given
732 730 * PT_LOAD program header. We fill in more information on the mapping later.
733 731 */
734 732 static int
735 733 core_add_mapping(struct ps_prochandle *P, GElf_Phdr *php)
736 734 {
737 735 int err = 0;
738 736 prmap_t pmap;
739 737
740 738 dprintf("mapping base %llx filesz %llu memsz %llu offset %llu\n",
741 739 (u_longlong_t)php->p_vaddr, (u_longlong_t)php->p_filesz,
742 740 (u_longlong_t)php->p_memsz, (u_longlong_t)php->p_offset);
743 741
744 742 pmap.pr_vaddr = (uintptr_t)php->p_vaddr;
745 743 pmap.pr_size = php->p_memsz;
746 744
747 745 /*
748 746 * If Pgcore() or elfcore() fail to write a mapping, they will set
749 747 * PF_SUNW_FAILURE in the Phdr and try to stash away the errno for us.
750 748 */
751 749 if (php->p_flags & PF_SUNW_FAILURE) {
752 750 (void) pread64(P->asfd, &err,
753 751 sizeof (err), (off64_t)php->p_offset);
754 752
755 753 Perror_printf(P, "core file data for mapping at %p not saved: "
756 754 "%s\n", (void *)(uintptr_t)php->p_vaddr, strerror(err));
757 755 dprintf("core file data for mapping at %p not saved: %s\n",
758 756 (void *)(uintptr_t)php->p_vaddr, strerror(err));
759 757
760 758 } else if (php->p_filesz != 0 && php->p_offset >= P->core->core_size) {
761 759 Perror_printf(P, "core file may be corrupt -- data for mapping "
762 760 "at %p is missing\n", (void *)(uintptr_t)php->p_vaddr);
763 761 dprintf("core file may be corrupt -- data for mapping "
764 762 "at %p is missing\n", (void *)(uintptr_t)php->p_vaddr);
765 763 }
766 764
767 765 /*
768 766 * The mapping name and offset will hopefully be filled in
769 767 * by the librtld_db agent. Unfortunately, if it isn't a
770 768 * shared library mapping, this information is gone forever.
771 769 */
772 770 pmap.pr_mapname[0] = '\0';
773 771 pmap.pr_offset = 0;
774 772
775 773 pmap.pr_mflags = 0;
776 774 if (php->p_flags & PF_R)
777 775 pmap.pr_mflags |= MA_READ;
778 776 if (php->p_flags & PF_W)
779 777 pmap.pr_mflags |= MA_WRITE;
780 778 if (php->p_flags & PF_X)
781 779 pmap.pr_mflags |= MA_EXEC;
782 780
783 781 if (php->p_filesz == 0)
784 782 pmap.pr_mflags |= MA_RESERVED1;
785 783
786 784 /*
787 785 * At the time of adding this mapping, we just zero the pagesize.
788 786 * Once we've processed more of the core file, we'll have the
789 787 * pagesize from the auxv's AT_PAGESZ element and we can fill this in.
790 788 */
791 789 pmap.pr_pagesize = 0;
792 790
793 791 /*
794 792 * Unfortunately whether or not the mapping was a System V
795 793 * shared memory segment is lost. We use -1 to mark it as not shm.
796 794 */
797 795 pmap.pr_shmid = -1;
798 796
799 797 return (Padd_mapping(P, php->p_offset, NULL, &pmap));
800 798 }
801 799
802 800 /*
803 801 * Given a virtual address, name the mapping at that address using the
804 802 * specified name, and return the map_info_t pointer.
805 803 */
806 804 static map_info_t *
807 805 core_name_mapping(struct ps_prochandle *P, uintptr_t addr, const char *name)
808 806 {
809 807 map_info_t *mp = Paddr2mptr(P, addr);
810 808
811 809 if (mp != NULL) {
812 810 (void) strncpy(mp->map_pmap.pr_mapname, name, PRMAPSZ);
813 811 mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
814 812 }
815 813
816 814 return (mp);
817 815 }
818 816
819 817 /*
820 818 * libproc uses libelf for all of its symbol table manipulation. This function
821 819 * takes a symbol table and string table from a core file and places them
822 820 * in a memory backed elf file.
823 821 */
824 822 static void
825 823 fake_up_symtab(struct ps_prochandle *P, const elf_file_header_t *ehdr,
826 824 GElf_Shdr *symtab, GElf_Shdr *strtab)
827 825 {
828 826 size_t size;
829 827 off64_t off, base;
830 828 map_info_t *mp;
831 829 file_info_t *fp;
832 830 Elf_Scn *scn;
833 831 Elf_Data *data;
834 832
835 833 if (symtab->sh_addr == 0 ||
836 834 (mp = Paddr2mptr(P, symtab->sh_addr)) == NULL ||
837 835 (fp = mp->map_file) == NULL) {
838 836 dprintf("fake_up_symtab: invalid section\n");
839 837 return;
840 838 }
841 839
842 840 if (fp->file_symtab.sym_data_pri != NULL) {
843 841 dprintf("Symbol table already loaded (sh_addr 0x%lx)\n",
844 842 (long)symtab->sh_addr);
845 843 return;
846 844 }
847 845
848 846 if (P->status.pr_dmodel == PR_MODEL_ILP32) {
849 847 struct {
850 848 Elf32_Ehdr ehdr;
851 849 Elf32_Shdr shdr[3];
852 850 char data[1];
853 851 } *b;
854 852
855 853 base = sizeof (b->ehdr) + sizeof (b->shdr);
856 854 size = base + symtab->sh_size + strtab->sh_size;
857 855
858 856 if ((b = calloc(1, size)) == NULL)
859 857 return;
860 858
861 859 (void) memcpy(b->ehdr.e_ident, ehdr->e_ident,
862 860 sizeof (ehdr->e_ident));
863 861 b->ehdr.e_type = ehdr->e_type;
864 862 b->ehdr.e_machine = ehdr->e_machine;
865 863 b->ehdr.e_version = ehdr->e_version;
866 864 b->ehdr.e_flags = ehdr->e_flags;
867 865 b->ehdr.e_ehsize = sizeof (b->ehdr);
868 866 b->ehdr.e_shoff = sizeof (b->ehdr);
869 867 b->ehdr.e_shentsize = sizeof (b->shdr[0]);
870 868 b->ehdr.e_shnum = 3;
871 869 off = 0;
872 870
873 871 b->shdr[1].sh_size = symtab->sh_size;
874 872 b->shdr[1].sh_type = SHT_SYMTAB;
875 873 b->shdr[1].sh_offset = off + base;
876 874 b->shdr[1].sh_entsize = sizeof (Elf32_Sym);
877 875 b->shdr[1].sh_link = 2;
878 876 b->shdr[1].sh_info = symtab->sh_info;
879 877 b->shdr[1].sh_addralign = symtab->sh_addralign;
880 878
881 879 if (pread64(P->asfd, &b->data[off], b->shdr[1].sh_size,
882 880 symtab->sh_offset) != b->shdr[1].sh_size) {
883 881 dprintf("fake_up_symtab: pread of symtab[1] failed\n");
884 882 free(b);
885 883 return;
886 884 }
887 885
888 886 off += b->shdr[1].sh_size;
889 887
890 888 b->shdr[2].sh_flags = SHF_STRINGS;
891 889 b->shdr[2].sh_size = strtab->sh_size;
892 890 b->shdr[2].sh_type = SHT_STRTAB;
893 891 b->shdr[2].sh_offset = off + base;
894 892 b->shdr[2].sh_info = strtab->sh_info;
895 893 b->shdr[2].sh_addralign = 1;
896 894
897 895 if (pread64(P->asfd, &b->data[off], b->shdr[2].sh_size,
898 896 strtab->sh_offset) != b->shdr[2].sh_size) {
899 897 dprintf("fake_up_symtab: pread of symtab[2] failed\n");
900 898 free(b);
901 899 return;
902 900 }
903 901
904 902 off += b->shdr[2].sh_size;
905 903
906 904 fp->file_symtab.sym_elf = elf_memory((char *)b, size);
907 905 if (fp->file_symtab.sym_elf == NULL) {
908 906 free(b);
909 907 return;
910 908 }
911 909
912 910 fp->file_symtab.sym_elfmem = b;
913 911 #ifdef _LP64
914 912 } else {
915 913 struct {
916 914 Elf64_Ehdr ehdr;
917 915 Elf64_Shdr shdr[3];
918 916 char data[1];
919 917 } *b;
920 918
921 919 base = sizeof (b->ehdr) + sizeof (b->shdr);
922 920 size = base + symtab->sh_size + strtab->sh_size;
923 921
924 922 if ((b = calloc(1, size)) == NULL)
925 923 return;
926 924
927 925 (void) memcpy(b->ehdr.e_ident, ehdr->e_ident,
928 926 sizeof (ehdr->e_ident));
929 927 b->ehdr.e_type = ehdr->e_type;
930 928 b->ehdr.e_machine = ehdr->e_machine;
931 929 b->ehdr.e_version = ehdr->e_version;
932 930 b->ehdr.e_flags = ehdr->e_flags;
933 931 b->ehdr.e_ehsize = sizeof (b->ehdr);
934 932 b->ehdr.e_shoff = sizeof (b->ehdr);
935 933 b->ehdr.e_shentsize = sizeof (b->shdr[0]);
936 934 b->ehdr.e_shnum = 3;
937 935 off = 0;
938 936
939 937 b->shdr[1].sh_size = symtab->sh_size;
940 938 b->shdr[1].sh_type = SHT_SYMTAB;
941 939 b->shdr[1].sh_offset = off + base;
942 940 b->shdr[1].sh_entsize = sizeof (Elf64_Sym);
943 941 b->shdr[1].sh_link = 2;
944 942 b->shdr[1].sh_info = symtab->sh_info;
945 943 b->shdr[1].sh_addralign = symtab->sh_addralign;
946 944
947 945 if (pread64(P->asfd, &b->data[off], b->shdr[1].sh_size,
948 946 symtab->sh_offset) != b->shdr[1].sh_size) {
949 947 free(b);
950 948 return;
951 949 }
952 950
953 951 off += b->shdr[1].sh_size;
954 952
955 953 b->shdr[2].sh_flags = SHF_STRINGS;
956 954 b->shdr[2].sh_size = strtab->sh_size;
957 955 b->shdr[2].sh_type = SHT_STRTAB;
958 956 b->shdr[2].sh_offset = off + base;
959 957 b->shdr[2].sh_info = strtab->sh_info;
960 958 b->shdr[2].sh_addralign = 1;
961 959
962 960 if (pread64(P->asfd, &b->data[off], b->shdr[2].sh_size,
963 961 strtab->sh_offset) != b->shdr[2].sh_size) {
964 962 free(b);
965 963 return;
966 964 }
967 965
968 966 off += b->shdr[2].sh_size;
969 967
970 968 fp->file_symtab.sym_elf = elf_memory((char *)b, size);
971 969 if (fp->file_symtab.sym_elf == NULL) {
972 970 free(b);
973 971 return;
974 972 }
975 973
976 974 fp->file_symtab.sym_elfmem = b;
977 975 #endif
978 976 }
979 977
980 978 if ((scn = elf_getscn(fp->file_symtab.sym_elf, 1)) == NULL ||
981 979 (fp->file_symtab.sym_data_pri = elf_getdata(scn, NULL)) == NULL ||
982 980 (scn = elf_getscn(fp->file_symtab.sym_elf, 2)) == NULL ||
983 981 (data = elf_getdata(scn, NULL)) == NULL) {
984 982 dprintf("fake_up_symtab: failed to get section data at %p\n",
985 983 (void *)scn);
986 984 goto err;
987 985 }
988 986
989 987 fp->file_symtab.sym_strs = data->d_buf;
990 988 fp->file_symtab.sym_strsz = data->d_size;
991 989 fp->file_symtab.sym_symn = symtab->sh_size / symtab->sh_entsize;
992 990 fp->file_symtab.sym_hdr_pri = *symtab;
993 991 fp->file_symtab.sym_strhdr = *strtab;
994 992
995 993 optimize_symtab(&fp->file_symtab);
996 994
997 995 return;
998 996 err:
999 997 (void) elf_end(fp->file_symtab.sym_elf);
1000 998 free(fp->file_symtab.sym_elfmem);
1001 999 fp->file_symtab.sym_elf = NULL;
1002 1000 fp->file_symtab.sym_elfmem = NULL;
1003 1001 }
1004 1002
1005 1003 static void
1006 1004 core_phdr_to_gelf(const Elf32_Phdr *src, GElf_Phdr *dst)
1007 1005 {
1008 1006 dst->p_type = src->p_type;
1009 1007 dst->p_flags = src->p_flags;
1010 1008 dst->p_offset = (Elf64_Off)src->p_offset;
1011 1009 dst->p_vaddr = (Elf64_Addr)src->p_vaddr;
1012 1010 dst->p_paddr = (Elf64_Addr)src->p_paddr;
1013 1011 dst->p_filesz = (Elf64_Xword)src->p_filesz;
1014 1012 dst->p_memsz = (Elf64_Xword)src->p_memsz;
1015 1013 dst->p_align = (Elf64_Xword)src->p_align;
1016 1014 }
1017 1015
1018 1016 static void
1019 1017 core_shdr_to_gelf(const Elf32_Shdr *src, GElf_Shdr *dst)
1020 1018 {
1021 1019 dst->sh_name = src->sh_name;
1022 1020 dst->sh_type = src->sh_type;
1023 1021 dst->sh_flags = (Elf64_Xword)src->sh_flags;
1024 1022 dst->sh_addr = (Elf64_Addr)src->sh_addr;
1025 1023 dst->sh_offset = (Elf64_Off)src->sh_offset;
1026 1024 dst->sh_size = (Elf64_Xword)src->sh_size;
1027 1025 dst->sh_link = src->sh_link;
1028 1026 dst->sh_info = src->sh_info;
1029 1027 dst->sh_addralign = (Elf64_Xword)src->sh_addralign;
1030 1028 dst->sh_entsize = (Elf64_Xword)src->sh_entsize;
1031 1029 }
1032 1030
1033 1031 /*
1034 1032 * Perform elf_begin on efp->e_fd and verify the ELF file's type and class.
1035 1033 */
1036 1034 static int
1037 1035 core_elf_fdopen(elf_file_t *efp, GElf_Half type, int *perr)
1038 1036 {
1039 1037 #ifdef _BIG_ENDIAN
1040 1038 uchar_t order = ELFDATA2MSB;
1041 1039 #else
1042 1040 uchar_t order = ELFDATA2LSB;
1043 1041 #endif
1044 1042 Elf32_Ehdr e32;
1045 1043 int is_noelf = -1;
1046 1044 int isa_err = 0;
1047 1045
1048 1046 /*
1049 1047 * Because 32-bit libelf cannot deal with large files, we need to read,
1050 1048 * check, and convert the file header manually in case type == ET_CORE.
1051 1049 */
1052 1050 if (pread64(efp->e_fd, &e32, sizeof (e32), 0) != sizeof (e32)) {
1053 1051 if (perr != NULL)
1054 1052 *perr = G_FORMAT;
1055 1053 goto err;
1056 1054 }
1057 1055 if ((is_noelf = memcmp(&e32.e_ident[EI_MAG0], ELFMAG, SELFMAG)) != 0 ||
1058 1056 e32.e_type != type || (isa_err = (e32.e_ident[EI_DATA] != order)) ||
1059 1057 e32.e_version != EV_CURRENT) {
1060 1058 if (perr != NULL) {
1061 1059 if (is_noelf == 0 && isa_err) {
1062 1060 *perr = G_ISAINVAL;
1063 1061 } else {
1064 1062 *perr = G_FORMAT;
1065 1063 }
1066 1064 }
1067 1065 goto err;
1068 1066 }
1069 1067
1070 1068 /*
1071 1069 * If the file is 64-bit and we are 32-bit, fail with G_LP64. If the
1072 1070 * file is 64-bit and we are 64-bit, re-read the header as a Elf64_Ehdr,
1073 1071 * and convert it to a elf_file_header_t. Otherwise, the file is
1074 1072 * 32-bit, so convert e32 to a elf_file_header_t.
1075 1073 */
1076 1074 if (e32.e_ident[EI_CLASS] == ELFCLASS64) {
1077 1075 #ifdef _LP64
1078 1076 Elf64_Ehdr e64;
1079 1077
1080 1078 if (pread64(efp->e_fd, &e64, sizeof (e64), 0) != sizeof (e64)) {
1081 1079 if (perr != NULL)
1082 1080 *perr = G_FORMAT;
1083 1081 goto err;
1084 1082 }
1085 1083
1086 1084 (void) memcpy(efp->e_hdr.e_ident, e64.e_ident, EI_NIDENT);
1087 1085 efp->e_hdr.e_type = e64.e_type;
1088 1086 efp->e_hdr.e_machine = e64.e_machine;
1089 1087 efp->e_hdr.e_version = e64.e_version;
1090 1088 efp->e_hdr.e_entry = e64.e_entry;
1091 1089 efp->e_hdr.e_phoff = e64.e_phoff;
1092 1090 efp->e_hdr.e_shoff = e64.e_shoff;
1093 1091 efp->e_hdr.e_flags = e64.e_flags;
1094 1092 efp->e_hdr.e_ehsize = e64.e_ehsize;
1095 1093 efp->e_hdr.e_phentsize = e64.e_phentsize;
1096 1094 efp->e_hdr.e_phnum = (Elf64_Word)e64.e_phnum;
1097 1095 efp->e_hdr.e_shentsize = e64.e_shentsize;
1098 1096 efp->e_hdr.e_shnum = (Elf64_Word)e64.e_shnum;
1099 1097 efp->e_hdr.e_shstrndx = (Elf64_Word)e64.e_shstrndx;
1100 1098 #else /* _LP64 */
1101 1099 if (perr != NULL)
1102 1100 *perr = G_LP64;
1103 1101 goto err;
1104 1102 #endif /* _LP64 */
1105 1103 } else {
1106 1104 (void) memcpy(efp->e_hdr.e_ident, e32.e_ident, EI_NIDENT);
1107 1105 efp->e_hdr.e_type = e32.e_type;
1108 1106 efp->e_hdr.e_machine = e32.e_machine;
1109 1107 efp->e_hdr.e_version = e32.e_version;
1110 1108 efp->e_hdr.e_entry = (Elf64_Addr)e32.e_entry;
1111 1109 efp->e_hdr.e_phoff = (Elf64_Off)e32.e_phoff;
1112 1110 efp->e_hdr.e_shoff = (Elf64_Off)e32.e_shoff;
1113 1111 efp->e_hdr.e_flags = e32.e_flags;
1114 1112 efp->e_hdr.e_ehsize = e32.e_ehsize;
1115 1113 efp->e_hdr.e_phentsize = e32.e_phentsize;
1116 1114 efp->e_hdr.e_phnum = (Elf64_Word)e32.e_phnum;
1117 1115 efp->e_hdr.e_shentsize = e32.e_shentsize;
1118 1116 efp->e_hdr.e_shnum = (Elf64_Word)e32.e_shnum;
1119 1117 efp->e_hdr.e_shstrndx = (Elf64_Word)e32.e_shstrndx;
1120 1118 }
1121 1119
1122 1120 /*
1123 1121 * If the number of section headers or program headers or the section
1124 1122 * header string table index would overflow their respective fields
1125 1123 * in the ELF header, they're stored in the section header at index
1126 1124 * zero. To simplify use elsewhere, we look for those sentinel values
1127 1125 * here.
1128 1126 */
1129 1127 if ((efp->e_hdr.e_shnum == 0 && efp->e_hdr.e_shoff != 0) ||
1130 1128 efp->e_hdr.e_shstrndx == SHN_XINDEX ||
1131 1129 efp->e_hdr.e_phnum == PN_XNUM) {
1132 1130 GElf_Shdr shdr;
1133 1131
1134 1132 dprintf("extended ELF header\n");
1135 1133
1136 1134 if (efp->e_hdr.e_shoff == 0) {
1137 1135 if (perr != NULL)
1138 1136 *perr = G_FORMAT;
1139 1137 goto err;
1140 1138 }
1141 1139
1142 1140 if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32) {
1143 1141 Elf32_Shdr shdr32;
1144 1142
1145 1143 if (pread64(efp->e_fd, &shdr32, sizeof (shdr32),
1146 1144 efp->e_hdr.e_shoff) != sizeof (shdr32)) {
1147 1145 if (perr != NULL)
1148 1146 *perr = G_FORMAT;
1149 1147 goto err;
1150 1148 }
1151 1149
1152 1150 core_shdr_to_gelf(&shdr32, &shdr);
1153 1151 } else {
1154 1152 if (pread64(efp->e_fd, &shdr, sizeof (shdr),
1155 1153 efp->e_hdr.e_shoff) != sizeof (shdr)) {
1156 1154 if (perr != NULL)
1157 1155 *perr = G_FORMAT;
1158 1156 goto err;
1159 1157 }
1160 1158 }
1161 1159
1162 1160 if (efp->e_hdr.e_shnum == 0) {
1163 1161 efp->e_hdr.e_shnum = shdr.sh_size;
1164 1162 dprintf("section header count %lu\n",
1165 1163 (ulong_t)shdr.sh_size);
1166 1164 }
1167 1165
1168 1166 if (efp->e_hdr.e_shstrndx == SHN_XINDEX) {
1169 1167 efp->e_hdr.e_shstrndx = shdr.sh_link;
1170 1168 dprintf("section string index %u\n", shdr.sh_link);
1171 1169 }
1172 1170
1173 1171 if (efp->e_hdr.e_phnum == PN_XNUM && shdr.sh_info != 0) {
1174 1172 efp->e_hdr.e_phnum = shdr.sh_info;
1175 1173 dprintf("program header count %u\n", shdr.sh_info);
1176 1174 }
1177 1175
1178 1176 } else if (efp->e_hdr.e_phoff != 0) {
1179 1177 GElf_Phdr phdr;
1180 1178 uint64_t phnum;
1181 1179
1182 1180 /*
1183 1181 * It's possible this core file came from a system that
1184 1182 * accidentally truncated the e_phnum field without correctly
1185 1183 * using the extended format in the section header at index
1186 1184 * zero. We try to detect and correct that specific type of
1187 1185 * corruption by using the knowledge that the core dump
1188 1186 * routines usually place the data referenced by the first
1189 1187 * program header immediately after the last header element.
1190 1188 */
1191 1189 if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32) {
1192 1190 Elf32_Phdr phdr32;
1193 1191
1194 1192 if (pread64(efp->e_fd, &phdr32, sizeof (phdr32),
1195 1193 efp->e_hdr.e_phoff) != sizeof (phdr32)) {
1196 1194 if (perr != NULL)
1197 1195 *perr = G_FORMAT;
1198 1196 goto err;
1199 1197 }
1200 1198
1201 1199 core_phdr_to_gelf(&phdr32, &phdr);
1202 1200 } else {
1203 1201 if (pread64(efp->e_fd, &phdr, sizeof (phdr),
1204 1202 efp->e_hdr.e_phoff) != sizeof (phdr)) {
1205 1203 if (perr != NULL)
1206 1204 *perr = G_FORMAT;
1207 1205 goto err;
1208 1206 }
1209 1207 }
1210 1208
1211 1209 phnum = phdr.p_offset - efp->e_hdr.e_ehsize -
1212 1210 (uint64_t)efp->e_hdr.e_shnum * efp->e_hdr.e_shentsize;
1213 1211 phnum /= efp->e_hdr.e_phentsize;
1214 1212
1215 1213 if (phdr.p_offset != 0 && phnum != efp->e_hdr.e_phnum) {
1216 1214 dprintf("suspicious program header count %u %u\n",
1217 1215 (uint_t)phnum, efp->e_hdr.e_phnum);
1218 1216
1219 1217 /*
1220 1218 * If the new program header count we computed doesn't
1221 1219 * jive with count in the ELF header, we'll use the
1222 1220 * data that's there and hope for the best.
1223 1221 *
1224 1222 * If it does, it's also possible that the section
1225 1223 * header offset is incorrect; we'll check that and
1226 1224 * possibly try to fix it.
1227 1225 */
1228 1226 if (phnum <= INT_MAX &&
1229 1227 (uint16_t)phnum == efp->e_hdr.e_phnum) {
1230 1228
1231 1229 if (efp->e_hdr.e_shoff == efp->e_hdr.e_phoff +
1232 1230 efp->e_hdr.e_phentsize *
1233 1231 (uint_t)efp->e_hdr.e_phnum) {
1234 1232 efp->e_hdr.e_shoff =
1235 1233 efp->e_hdr.e_phoff +
1236 1234 efp->e_hdr.e_phentsize * phnum;
1237 1235 }
1238 1236
1239 1237 efp->e_hdr.e_phnum = (Elf64_Word)phnum;
1240 1238 dprintf("using new program header count\n");
1241 1239 } else {
1242 1240 dprintf("inconsistent program header count\n");
1243 1241 }
1244 1242 }
1245 1243 }
1246 1244
1247 1245 /*
1248 1246 * The libelf implementation was never ported to be large-file aware.
1249 1247 * This is typically not a problem for your average executable or
1250 1248 * shared library, but a large 32-bit core file can exceed 2GB in size.
1251 1249 * So if type is ET_CORE, we don't bother doing elf_begin; the code
1252 1250 * in Pfgrab_core() below will do its own i/o and struct conversion.
1253 1251 */
1254 1252
1255 1253 if (type == ET_CORE) {
1256 1254 efp->e_elf = NULL;
1257 1255 return (0);
1258 1256 }
1259 1257
1260 1258 if ((efp->e_elf = elf_begin(efp->e_fd, ELF_C_READ, NULL)) == NULL) {
1261 1259 if (perr != NULL)
1262 1260 *perr = G_ELF;
1263 1261 goto err;
1264 1262 }
1265 1263
1266 1264 return (0);
1267 1265
1268 1266 err:
1269 1267 efp->e_elf = NULL;
1270 1268 return (-1);
1271 1269 }
1272 1270
1273 1271 /*
1274 1272 * Open the specified file and then do a core_elf_fdopen on it.
1275 1273 */
1276 1274 static int
1277 1275 core_elf_open(elf_file_t *efp, const char *path, GElf_Half type, int *perr)
1278 1276 {
1279 1277 (void) memset(efp, 0, sizeof (elf_file_t));
1280 1278
1281 1279 if ((efp->e_fd = open64(path, O_RDONLY)) >= 0) {
1282 1280 if (core_elf_fdopen(efp, type, perr) == 0)
1283 1281 return (0);
1284 1282
1285 1283 (void) close(efp->e_fd);
1286 1284 efp->e_fd = -1;
1287 1285 }
1288 1286
1289 1287 return (-1);
1290 1288 }
1291 1289
1292 1290 /*
1293 1291 * Close the ELF handle and file descriptor.
1294 1292 */
1295 1293 static void
1296 1294 core_elf_close(elf_file_t *efp)
1297 1295 {
1298 1296 if (efp->e_elf != NULL) {
1299 1297 (void) elf_end(efp->e_elf);
1300 1298 efp->e_elf = NULL;
1301 1299 }
1302 1300
1303 1301 if (efp->e_fd != -1) {
1304 1302 (void) close(efp->e_fd);
1305 1303 efp->e_fd = -1;
1306 1304 }
1307 1305 }
1308 1306
1309 1307 /*
1310 1308 * Given an ELF file for a statically linked executable, locate the likely
1311 1309 * primary text section and fill in rl_base with its virtual address.
1312 1310 */
1313 1311 static map_info_t *
1314 1312 core_find_text(struct ps_prochandle *P, Elf *elf, rd_loadobj_t *rlp)
1315 1313 {
1316 1314 GElf_Phdr phdr;
1317 1315 uint_t i;
1318 1316 size_t nphdrs;
1319 1317
1320 1318 if (elf_getphdrnum(elf, &nphdrs) == -1)
1321 1319 return (NULL);
1322 1320
1323 1321 for (i = 0; i < nphdrs; i++) {
1324 1322 if (gelf_getphdr(elf, i, &phdr) != NULL &&
1325 1323 phdr.p_type == PT_LOAD && (phdr.p_flags & PF_X)) {
1326 1324 rlp->rl_base = phdr.p_vaddr;
1327 1325 return (Paddr2mptr(P, rlp->rl_base));
1328 1326 }
1329 1327 }
1330 1328
1331 1329 return (NULL);
1332 1330 }
1333 1331
1334 1332 /*
1335 1333 * Given an ELF file and the librtld_db structure corresponding to its primary
1336 1334 * text mapping, deduce where its data segment was loaded and fill in
1337 1335 * rl_data_base and prmap_t.pr_offset accordingly.
1338 1336 */
1339 1337 static map_info_t *
1340 1338 core_find_data(struct ps_prochandle *P, Elf *elf, rd_loadobj_t *rlp)
1341 1339 {
1342 1340 GElf_Ehdr ehdr;
1343 1341 GElf_Phdr phdr;
1344 1342 map_info_t *mp;
1345 1343 uint_t i, pagemask;
1346 1344 size_t nphdrs;
1347 1345
1348 1346 rlp->rl_data_base = NULL;
1349 1347
1350 1348 /*
1351 1349 * Find the first loadable, writeable Phdr and compute rl_data_base
1352 1350 * as the virtual address at which is was loaded.
1353 1351 */
1354 1352 if (gelf_getehdr(elf, &ehdr) == NULL ||
1355 1353 elf_getphdrnum(elf, &nphdrs) == -1)
1356 1354 return (NULL);
1357 1355
1358 1356 for (i = 0; i < nphdrs; i++) {
1359 1357 if (gelf_getphdr(elf, i, &phdr) != NULL &&
1360 1358 phdr.p_type == PT_LOAD && (phdr.p_flags & PF_W)) {
1361 1359 rlp->rl_data_base = phdr.p_vaddr;
1362 1360 if (ehdr.e_type == ET_DYN)
1363 1361 rlp->rl_data_base += rlp->rl_base;
1364 1362 break;
1365 1363 }
1366 1364 }
1367 1365
1368 1366 /*
1369 1367 * If we didn't find an appropriate phdr or if the address we
1370 1368 * computed has no mapping, return NULL.
1371 1369 */
1372 1370 if (rlp->rl_data_base == NULL ||
1373 1371 (mp = Paddr2mptr(P, rlp->rl_data_base)) == NULL)
1374 1372 return (NULL);
1375 1373
1376 1374 /*
1377 1375 * It wouldn't be procfs-related code if we didn't make use of
1378 1376 * unclean knowledge of segvn, even in userland ... the prmap_t's
1379 1377 * pr_offset field will be the segvn offset from mmap(2)ing the
1380 1378 * data section, which will be the file offset & PAGEMASK.
1381 1379 */
1382 1380 pagemask = ~(mp->map_pmap.pr_pagesize - 1);
1383 1381 mp->map_pmap.pr_offset = phdr.p_offset & pagemask;
1384 1382
1385 1383 return (mp);
1386 1384 }
1387 1385
1388 1386 /*
1389 1387 * Librtld_db agent callback for iterating over load object mappings.
1390 1388 * For each load object, we allocate a new file_info_t, perform naming,
1391 1389 * and attempt to construct a symbol table for the load object.
1392 1390 */
1393 1391 static int
1394 1392 core_iter_mapping(const rd_loadobj_t *rlp, struct ps_prochandle *P)
1395 1393 {
1396 1394 char lname[PATH_MAX], buf[PATH_MAX];
1397 1395 file_info_t *fp;
1398 1396 map_info_t *mp;
1399 1397
1400 1398 if (Pread_string(P, lname, PATH_MAX, (off_t)rlp->rl_nameaddr) <= 0) {
1401 1399 dprintf("failed to read name %p\n", (void *)rlp->rl_nameaddr);
1402 1400 return (1); /* Keep going; forget this if we can't get a name */
1403 1401 }
1404 1402
1405 1403 dprintf("rd_loadobj name = \"%s\" rl_base = %p\n",
1406 1404 lname, (void *)rlp->rl_base);
1407 1405
1408 1406 if ((mp = Paddr2mptr(P, rlp->rl_base)) == NULL) {
1409 1407 dprintf("no mapping for %p\n", (void *)rlp->rl_base);
1410 1408 return (1); /* No mapping; advance to next mapping */
1411 1409 }
1412 1410
1413 1411 /*
1414 1412 * Create a new file_info_t for this mapping, and therefore for
1415 1413 * this load object.
1416 1414 *
1417 1415 * If there's an ELF header at the beginning of this mapping,
1418 1416 * file_info_new() will try to use its section headers to
1419 1417 * identify any other mappings that belong to this load object.
1420 1418 */
1421 1419 if ((fp = mp->map_file) == NULL &&
1422 1420 (fp = file_info_new(P, mp)) == NULL) {
1423 1421 P->core->core_errno = errno;
1424 1422 dprintf("failed to malloc mapping data\n");
1425 1423 return (0); /* Abort */
1426 1424 }
1427 1425 fp->file_map = mp;
1428 1426
1429 1427 /* Create a local copy of the load object representation */
1430 1428 if ((fp->file_lo = calloc(1, sizeof (rd_loadobj_t))) == NULL) {
1431 1429 P->core->core_errno = errno;
1432 1430 dprintf("failed to malloc mapping data\n");
1433 1431 return (0); /* Abort */
1434 1432 }
1435 1433 *fp->file_lo = *rlp;
1436 1434
1437 1435 if (lname[0] != '\0') {
1438 1436 /*
1439 1437 * Naming dance part 1: if we got a name from librtld_db, then
1440 1438 * copy this name to the prmap_t if it is unnamed. If the
1441 1439 * file_info_t is unnamed, name it after the lname.
1442 1440 */
1443 1441 if (mp->map_pmap.pr_mapname[0] == '\0') {
1444 1442 (void) strncpy(mp->map_pmap.pr_mapname, lname, PRMAPSZ);
1445 1443 mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
1446 1444 }
1447 1445
1448 1446 if (fp->file_lname == NULL)
1449 1447 fp->file_lname = strdup(lname);
1450 1448
1451 1449 } else if (fp->file_lname == NULL &&
1452 1450 mp->map_pmap.pr_mapname[0] != '\0') {
1453 1451 /*
1454 1452 * Naming dance part 2: if the mapping is named and the
1455 1453 * file_info_t is not, name the file after the mapping.
1456 1454 */
1457 1455 fp->file_lname = strdup(mp->map_pmap.pr_mapname);
1458 1456 }
1459 1457
1460 1458 if ((fp->file_rname == NULL) &&
1461 1459 (Pfindmap(P, mp, buf, sizeof (buf)) != NULL))
1462 1460 fp->file_rname = strdup(buf);
1463 1461
1464 1462 if (fp->file_lname != NULL)
1465 1463 fp->file_lbase = basename(fp->file_lname);
1466 1464 if (fp->file_rname != NULL)
1467 1465 fp->file_rbase = basename(fp->file_rname);
1468 1466
1469 1467 /* Associate the file and the mapping. */
1470 1468 (void) strncpy(fp->file_pname, mp->map_pmap.pr_mapname, PRMAPSZ);
1471 1469 fp->file_pname[PRMAPSZ - 1] = '\0';
1472 1470
1473 1471 /*
1474 1472 * If no section headers were available then we'll have to
1475 1473 * identify this load object's other mappings with what we've
1476 1474 * got: the start and end of the object's corresponding
1477 1475 * address space.
1478 1476 */
1479 1477 if (fp->file_saddrs == NULL) {
1480 1478 for (mp = fp->file_map + 1; mp < P->mappings + P->map_count &&
1481 1479 mp->map_pmap.pr_vaddr < rlp->rl_bend; mp++) {
1482 1480
1483 1481 if (mp->map_file == NULL) {
1484 1482 dprintf("core_iter_mapping %s: associating "
1485 1483 "segment at %p\n",
1486 1484 fp->file_pname,
1487 1485 (void *)mp->map_pmap.pr_vaddr);
1488 1486 mp->map_file = fp;
1489 1487 fp->file_ref++;
1490 1488 } else {
1491 1489 dprintf("core_iter_mapping %s: segment at "
1492 1490 "%p already associated with %s\n",
1493 1491 fp->file_pname,
1494 1492 (void *)mp->map_pmap.pr_vaddr,
1495 1493 (mp == fp->file_map ? "this file" :
1496 1494 mp->map_file->file_pname));
1497 1495 }
1498 1496 }
1499 1497 }
1500 1498
1501 1499 /* Ensure that all this file's mappings are named. */
1502 1500 for (mp = fp->file_map; mp < P->mappings + P->map_count &&
1503 1501 mp->map_file == fp; mp++) {
1504 1502 if (mp->map_pmap.pr_mapname[0] == '\0' &&
1505 1503 !(mp->map_pmap.pr_mflags & MA_BREAK)) {
1506 1504 (void) strncpy(mp->map_pmap.pr_mapname, fp->file_pname,
1507 1505 PRMAPSZ);
1508 1506 mp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
1509 1507 }
1510 1508 }
1511 1509
1512 1510 /* Attempt to build a symbol table for this file. */
1513 1511 Pbuild_file_symtab(P, fp);
1514 1512 if (fp->file_elf == NULL)
1515 1513 dprintf("core_iter_mapping: no symtab for %s\n",
1516 1514 fp->file_pname);
1517 1515
1518 1516 /* Locate the start of a data segment associated with this file. */
1519 1517 if ((mp = core_find_data(P, fp->file_elf, fp->file_lo)) != NULL) {
1520 1518 dprintf("found data for %s at %p (pr_offset 0x%llx)\n",
1521 1519 fp->file_pname, (void *)fp->file_lo->rl_data_base,
1522 1520 mp->map_pmap.pr_offset);
1523 1521 } else {
1524 1522 dprintf("core_iter_mapping: no data found for %s\n",
1525 1523 fp->file_pname);
1526 1524 }
1527 1525
1528 1526 return (1); /* Advance to next mapping */
1529 1527 }
1530 1528
1531 1529 /*
1532 1530 * Callback function for Pfindexec(). In order to confirm a given pathname,
1533 1531 * we verify that we can open it as an ELF file of type ET_EXEC or ET_DYN.
1534 1532 */
1535 1533 static int
1536 1534 core_exec_open(const char *path, void *efp)
1537 1535 {
1538 1536 if (core_elf_open(efp, path, ET_EXEC, NULL) == 0)
1539 1537 return (1);
1540 1538 if (core_elf_open(efp, path, ET_DYN, NULL) == 0)
1541 1539 return (1);
1542 1540 return (0);
1543 1541 }
1544 1542
1545 1543 /*
1546 1544 * Attempt to load any section headers found in the core file. If present,
1547 1545 * this will refer to non-loadable data added to the core file by the kernel
1548 1546 * based on coreadm(1M) settings, including CTF data and the symbol table.
1549 1547 */
1550 1548 static void
1551 1549 core_load_shdrs(struct ps_prochandle *P, elf_file_t *efp)
1552 1550 {
1553 1551 GElf_Shdr *shp, *shdrs = NULL;
1554 1552 char *shstrtab = NULL;
1555 1553 ulong_t shstrtabsz;
1556 1554 const char *name;
1557 1555 map_info_t *mp;
1558 1556
1559 1557 size_t nbytes;
1560 1558 void *buf;
1561 1559 int i;
1562 1560
1563 1561 if (efp->e_hdr.e_shstrndx >= efp->e_hdr.e_shnum) {
1564 1562 dprintf("corrupt shstrndx (%u) exceeds shnum (%u)\n",
1565 1563 efp->e_hdr.e_shstrndx, efp->e_hdr.e_shnum);
1566 1564 return;
1567 1565 }
1568 1566
1569 1567 /*
1570 1568 * Read the section header table from the core file and then iterate
1571 1569 * over the section headers, converting each to a GElf_Shdr.
1572 1570 */
1573 1571 if ((shdrs = malloc(efp->e_hdr.e_shnum * sizeof (GElf_Shdr))) == NULL) {
1574 1572 dprintf("failed to malloc %u section headers: %s\n",
1575 1573 (uint_t)efp->e_hdr.e_shnum, strerror(errno));
1576 1574 return;
1577 1575 }
1578 1576
1579 1577 nbytes = efp->e_hdr.e_shnum * efp->e_hdr.e_shentsize;
1580 1578 if ((buf = malloc(nbytes)) == NULL) {
1581 1579 dprintf("failed to malloc %d bytes: %s\n", (int)nbytes,
1582 1580 strerror(errno));
1583 1581 free(shdrs);
1584 1582 goto out;
1585 1583 }
1586 1584
1587 1585 if (pread64(efp->e_fd, buf, nbytes, efp->e_hdr.e_shoff) != nbytes) {
1588 1586 dprintf("failed to read section headers at off %lld: %s\n",
1589 1587 (longlong_t)efp->e_hdr.e_shoff, strerror(errno));
1590 1588 free(buf);
1591 1589 goto out;
1592 1590 }
1593 1591
1594 1592 for (i = 0; i < efp->e_hdr.e_shnum; i++) {
1595 1593 void *p = (uchar_t *)buf + efp->e_hdr.e_shentsize * i;
1596 1594
1597 1595 if (efp->e_hdr.e_ident[EI_CLASS] == ELFCLASS32)
1598 1596 core_shdr_to_gelf(p, &shdrs[i]);
1599 1597 else
1600 1598 (void) memcpy(&shdrs[i], p, sizeof (GElf_Shdr));
1601 1599 }
1602 1600
1603 1601 free(buf);
1604 1602 buf = NULL;
1605 1603
1606 1604 /*
1607 1605 * Read the .shstrtab section from the core file, terminating it with
1608 1606 * an extra \0 so that a corrupt section will not cause us to die.
1609 1607 */
1610 1608 shp = &shdrs[efp->e_hdr.e_shstrndx];
1611 1609 shstrtabsz = shp->sh_size;
1612 1610
1613 1611 if ((shstrtab = malloc(shstrtabsz + 1)) == NULL) {
1614 1612 dprintf("failed to allocate %lu bytes for shstrtab\n",
1615 1613 (ulong_t)shstrtabsz);
1616 1614 goto out;
1617 1615 }
1618 1616
1619 1617 if (pread64(efp->e_fd, shstrtab, shstrtabsz,
1620 1618 shp->sh_offset) != shstrtabsz) {
1621 1619 dprintf("failed to read %lu bytes of shstrs at off %lld: %s\n",
1622 1620 shstrtabsz, (longlong_t)shp->sh_offset, strerror(errno));
1623 1621 goto out;
1624 1622 }
1625 1623
1626 1624 shstrtab[shstrtabsz] = '\0';
1627 1625
1628 1626 /*
1629 1627 * Now iterate over each section in the section header table, locating
1630 1628 * sections of interest and initializing more of the ps_prochandle.
1631 1629 */
1632 1630 for (i = 0; i < efp->e_hdr.e_shnum; i++) {
1633 1631 shp = &shdrs[i];
1634 1632 name = shstrtab + shp->sh_name;
1635 1633
1636 1634 if (shp->sh_name >= shstrtabsz) {
1637 1635 dprintf("skipping section [%d]: corrupt sh_name\n", i);
1638 1636 continue;
1639 1637 }
1640 1638
1641 1639 if (shp->sh_link >= efp->e_hdr.e_shnum) {
1642 1640 dprintf("skipping section [%d]: corrupt sh_link\n", i);
1643 1641 continue;
1644 1642 }
1645 1643
1646 1644 dprintf("found section header %s (sh_addr 0x%llx)\n",
1647 1645 name, (u_longlong_t)shp->sh_addr);
1648 1646
1649 1647 if (strcmp(name, ".SUNW_ctf") == 0) {
1650 1648 if ((mp = Paddr2mptr(P, shp->sh_addr)) == NULL) {
1651 1649 dprintf("no map at addr 0x%llx for %s [%d]\n",
1652 1650 (u_longlong_t)shp->sh_addr, name, i);
1653 1651 continue;
1654 1652 }
1655 1653
1656 1654 if (mp->map_file == NULL ||
1657 1655 mp->map_file->file_ctf_buf != NULL) {
1658 1656 dprintf("no mapping file or duplicate buffer "
1659 1657 "for %s [%d]\n", name, i);
1660 1658 continue;
1661 1659 }
1662 1660
1663 1661 if ((buf = malloc(shp->sh_size)) == NULL ||
1664 1662 pread64(efp->e_fd, buf, shp->sh_size,
1665 1663 shp->sh_offset) != shp->sh_size) {
1666 1664 dprintf("skipping section %s [%d]: %s\n",
1667 1665 name, i, strerror(errno));
1668 1666 free(buf);
1669 1667 continue;
1670 1668 }
1671 1669
1672 1670 mp->map_file->file_ctf_size = shp->sh_size;
1673 1671 mp->map_file->file_ctf_buf = buf;
1674 1672
1675 1673 if (shdrs[shp->sh_link].sh_type == SHT_DYNSYM)
1676 1674 mp->map_file->file_ctf_dyn = 1;
1677 1675
1678 1676 } else if (strcmp(name, ".symtab") == 0) {
1679 1677 fake_up_symtab(P, &efp->e_hdr,
1680 1678 shp, &shdrs[shp->sh_link]);
1681 1679 }
1682 1680 }
1683 1681 out:
1684 1682 free(shstrtab);
1685 1683 free(shdrs);
1686 1684 }
1687 1685
1688 1686 /*
1689 1687 * Main engine for core file initialization: given an fd for the core file
1690 1688 * and an optional pathname, construct the ps_prochandle. The aout_path can
1691 1689 * either be a suggested executable pathname, or a suggested directory to
1692 1690 * use as a possible current working directory.
1693 1691 */
1694 1692 struct ps_prochandle *
1695 1693 Pfgrab_core(int core_fd, const char *aout_path, int *perr)
1696 1694 {
1697 1695 struct ps_prochandle *P;
1698 1696 map_info_t *stk_mp, *brk_mp;
1699 1697 const char *execname;
1700 1698 char *interp;
1701 1699 int i, notes, pagesize;
1702 1700 uintptr_t addr, base_addr;
1703 1701 struct stat64 stbuf;
1704 1702 void *phbuf, *php;
1705 1703 size_t nbytes;
1706 1704
1707 1705 elf_file_t aout;
1708 1706 elf_file_t core;
1709 1707
1710 1708 Elf_Scn *scn, *intp_scn = NULL;
1711 1709 Elf_Data *dp;
1712 1710
1713 1711 GElf_Phdr phdr, note_phdr;
1714 1712 GElf_Shdr shdr;
1715 1713 GElf_Xword nleft;
1716 1714
1717 1715 if (elf_version(EV_CURRENT) == EV_NONE) {
1718 1716 dprintf("libproc ELF version is more recent than libelf\n");
1719 1717 *perr = G_ELF;
1720 1718 return (NULL);
1721 1719 }
1722 1720
1723 1721 aout.e_elf = NULL;
1724 1722 aout.e_fd = -1;
1725 1723
1726 1724 core.e_elf = NULL;
1727 1725 core.e_fd = core_fd;
1728 1726
1729 1727 /*
1730 1728 * Allocate and initialize a ps_prochandle structure for the core.
1731 1729 * There are several key pieces of initialization here:
1732 1730 *
1733 1731 * 1. The PS_DEAD state flag marks this prochandle as a core file.
1734 1732 * PS_DEAD also thus prevents all operations which require state
1735 1733 * to be PS_STOP from operating on this handle.
1736 1734 *
1737 1735 * 2. We keep the core file fd in P->asfd since the core file contains
1738 1736 * the remnants of the process address space.
1739 1737 *
1740 1738 * 3. We set the P->info_valid bit because all information about the
1741 1739 * core is determined by the end of this function; there is no need
1742 1740 * for proc_update_maps() to reload mappings at any later point.
1743 1741 *
1744 1742 * 4. The read/write ops vector uses our core_rw() function defined
1745 1743 * above to handle i/o requests.
1746 1744 */
1747 1745 if ((P = malloc(sizeof (struct ps_prochandle))) == NULL) {
1748 1746 *perr = G_STRANGE;
1749 1747 return (NULL);
1750 1748 }
1751 1749
1752 1750 (void) memset(P, 0, sizeof (struct ps_prochandle));
1753 1751 (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
1754 1752 P->state = PS_DEAD;
1755 1753 P->pid = (pid_t)-1;
1756 1754 P->asfd = core.e_fd;
1757 1755 P->ctlfd = -1;
1758 1756 P->statfd = -1;
1759 1757 P->agentctlfd = -1;
1760 1758 P->agentstatfd = -1;
1761 1759 P->zoneroot = NULL;
1762 1760 P->info_valid = 1;
1763 1761 P->ops = &P_core_ops;
1764 1762
1765 1763 Pinitsym(P);
1766 1764
1767 1765 /*
1768 1766 * Fstat and open the core file and make sure it is a valid ELF core.
1769 1767 */
1770 1768 if (fstat64(P->asfd, &stbuf) == -1) {
1771 1769 *perr = G_STRANGE;
1772 1770 goto err;
1773 1771 }
1774 1772
1775 1773 if (core_elf_fdopen(&core, ET_CORE, perr) == -1)
1776 1774 goto err;
1777 1775
1778 1776 /*
1779 1777 * Allocate and initialize a core_info_t to hang off the ps_prochandle
1780 1778 * structure. We keep all core-specific information in this structure.
1781 1779 */
1782 1780 if ((P->core = calloc(1, sizeof (core_info_t))) == NULL) {
1783 1781 *perr = G_STRANGE;
1784 1782 goto err;
1785 1783 }
1786 1784
1787 1785 list_link(&P->core->core_lwp_head, NULL);
1788 1786 P->core->core_size = stbuf.st_size;
1789 1787 /*
1790 1788 * In the days before adjustable core file content, this was the
1791 1789 * default core file content. For new core files, this value will
1792 1790 * be overwritten by the NT_CONTENT note section.
1793 1791 */
1794 1792 P->core->core_content = CC_CONTENT_STACK | CC_CONTENT_HEAP |
1795 1793 CC_CONTENT_DATA | CC_CONTENT_RODATA | CC_CONTENT_ANON |
1796 1794 CC_CONTENT_SHANON;
1797 1795
1798 1796 switch (core.e_hdr.e_ident[EI_CLASS]) {
1799 1797 case ELFCLASS32:
1800 1798 P->core->core_dmodel = PR_MODEL_ILP32;
1801 1799 break;
1802 1800 case ELFCLASS64:
1803 1801 P->core->core_dmodel = PR_MODEL_LP64;
1804 1802 break;
1805 1803 default:
1806 1804 *perr = G_FORMAT;
1807 1805 goto err;
1808 1806 }
1809 1807
1810 1808 /*
1811 1809 * Because the core file may be a large file, we can't use libelf to
1812 1810 * read the Phdrs. We use e_phnum and e_phentsize to simplify things.
1813 1811 */
1814 1812 nbytes = core.e_hdr.e_phnum * core.e_hdr.e_phentsize;
1815 1813
1816 1814 if ((phbuf = malloc(nbytes)) == NULL) {
1817 1815 *perr = G_STRANGE;
1818 1816 goto err;
1819 1817 }
1820 1818
1821 1819 if (pread64(core_fd, phbuf, nbytes, core.e_hdr.e_phoff) != nbytes) {
1822 1820 *perr = G_STRANGE;
1823 1821 free(phbuf);
1824 1822 goto err;
1825 1823 }
1826 1824
1827 1825 /*
1828 1826 * Iterate through the program headers in the core file.
1829 1827 * We're interested in two types of Phdrs: PT_NOTE (which
1830 1828 * contains a set of saved /proc structures), and PT_LOAD (which
1831 1829 * represents a memory mapping from the process's address space).
1832 1830 * In the case of PT_NOTE, we're interested in the last PT_NOTE
1833 1831 * in the core file; currently the first PT_NOTE (if present)
1834 1832 * contains /proc structs in the pre-2.6 unstructured /proc format.
1835 1833 */
1836 1834 for (php = phbuf, notes = 0, i = 0; i < core.e_hdr.e_phnum; i++) {
1837 1835 if (core.e_hdr.e_ident[EI_CLASS] == ELFCLASS64)
1838 1836 (void) memcpy(&phdr, php, sizeof (GElf_Phdr));
1839 1837 else
1840 1838 core_phdr_to_gelf(php, &phdr);
1841 1839
1842 1840 switch (phdr.p_type) {
1843 1841 case PT_NOTE:
1844 1842 note_phdr = phdr;
1845 1843 notes++;
1846 1844 break;
1847 1845
1848 1846 case PT_LOAD:
1849 1847 if (core_add_mapping(P, &phdr) == -1) {
1850 1848 *perr = G_STRANGE;
1851 1849 free(phbuf);
1852 1850 goto err;
1853 1851 }
1854 1852 break;
1855 1853 }
1856 1854
1857 1855 php = (char *)php + core.e_hdr.e_phentsize;
1858 1856 }
1859 1857
1860 1858 free(phbuf);
1861 1859
1862 1860 Psort_mappings(P);
1863 1861
1864 1862 /*
1865 1863 * If we couldn't find anything of type PT_NOTE, or only one PT_NOTE
1866 1864 * was present, abort. The core file is either corrupt or too old.
1867 1865 */
1868 1866 if (notes == 0 || notes == 1) {
1869 1867 *perr = G_NOTE;
1870 1868 goto err;
1871 1869 }
1872 1870
1873 1871 /*
1874 1872 * Advance the seek pointer to the start of the PT_NOTE data
1875 1873 */
1876 1874 if (lseek64(P->asfd, note_phdr.p_offset, SEEK_SET) == (off64_t)-1) {
1877 1875 dprintf("Pgrab_core: failed to lseek to PT_NOTE data\n");
1878 1876 *perr = G_STRANGE;
1879 1877 goto err;
1880 1878 }
1881 1879
1882 1880 /*
1883 1881 * Now process the PT_NOTE structures. Each one is preceded by
1884 1882 * an Elf{32/64}_Nhdr structure describing its type and size.
1885 1883 *
1886 1884 * +--------+
1887 1885 * | header |
1888 1886 * +--------+
1889 1887 * | name |
1890 1888 * | ... |
1891 1889 * +--------+
1892 1890 * | desc |
1893 1891 * | ... |
1894 1892 * +--------+
1895 1893 */
1896 1894 for (nleft = note_phdr.p_filesz; nleft > 0; ) {
1897 1895 Elf64_Nhdr nhdr;
1898 1896 off64_t off, namesz;
1899 1897
1900 1898 /*
1901 1899 * Although <sys/elf.h> defines both Elf32_Nhdr and Elf64_Nhdr
1902 1900 * as different types, they are both of the same content and
1903 1901 * size, so we don't need to worry about 32/64 conversion here.
1904 1902 */
1905 1903 if (read(P->asfd, &nhdr, sizeof (nhdr)) != sizeof (nhdr)) {
1906 1904 dprintf("Pgrab_core: failed to read ELF note header\n");
1907 1905 *perr = G_NOTE;
1908 1906 goto err;
1909 1907 }
1910 1908
1911 1909 /*
1912 1910 * According to the System V ABI, the amount of padding
1913 1911 * following the name field should align the description
1914 1912 * field on a 4 byte boundary for 32-bit binaries or on an 8
1915 1913 * byte boundary for 64-bit binaries. However, this change
1916 1914 * was not made correctly during the 64-bit port so all
1917 1915 * descriptions can assume only 4-byte alignment. We ignore
1918 1916 * the name field and the padding to 4-byte alignment.
1919 1917 */
1920 1918 namesz = P2ROUNDUP((off64_t)nhdr.n_namesz, (off64_t)4);
1921 1919 if (lseek64(P->asfd, namesz, SEEK_CUR) == (off64_t)-1) {
1922 1920 dprintf("failed to seek past name and padding\n");
1923 1921 *perr = G_STRANGE;
1924 1922 goto err;
1925 1923 }
1926 1924
1927 1925 dprintf("Note hdr n_type=%u n_namesz=%u n_descsz=%u\n",
1928 1926 nhdr.n_type, nhdr.n_namesz, nhdr.n_descsz);
1929 1927
1930 1928 off = lseek64(P->asfd, (off64_t)0L, SEEK_CUR);
1931 1929
1932 1930 /*
1933 1931 * Invoke the note handler function from our table
1934 1932 */
1935 1933 if (nhdr.n_type < sizeof (nhdlrs) / sizeof (nhdlrs[0])) {
1936 1934 if (nhdlrs[nhdr.n_type](P, nhdr.n_descsz) < 0) {
1937 1935 *perr = G_NOTE;
1938 1936 goto err;
1939 1937 }
1940 1938 } else
1941 1939 (void) note_notsup(P, nhdr.n_descsz);
1942 1940
1943 1941 /*
1944 1942 * Seek past the current note data to the next Elf_Nhdr
1945 1943 */
1946 1944 if (lseek64(P->asfd, off + nhdr.n_descsz,
1947 1945 SEEK_SET) == (off64_t)-1) {
1948 1946 dprintf("Pgrab_core: failed to seek to next nhdr\n");
1949 1947 *perr = G_STRANGE;
1950 1948 goto err;
1951 1949 }
1952 1950
1953 1951 /*
1954 1952 * Subtract the size of the header and its data from what
1955 1953 * we have left to process.
1956 1954 */
1957 1955 nleft -= sizeof (nhdr) + namesz + nhdr.n_descsz;
1958 1956 }
1959 1957
1960 1958 if (nleft != 0) {
1961 1959 dprintf("Pgrab_core: note section malformed\n");
1962 1960 *perr = G_STRANGE;
1963 1961 goto err;
1964 1962 }
1965 1963
1966 1964 if ((pagesize = Pgetauxval(P, AT_PAGESZ)) == -1) {
1967 1965 pagesize = getpagesize();
1968 1966 dprintf("AT_PAGESZ missing; defaulting to %d\n", pagesize);
1969 1967 }
1970 1968
1971 1969 /*
1972 1970 * Locate and label the mappings corresponding to the end of the
1973 1971 * heap (MA_BREAK) and the base of the stack (MA_STACK).
1974 1972 */
1975 1973 if ((P->status.pr_brkbase != 0 || P->status.pr_brksize != 0) &&
1976 1974 (brk_mp = Paddr2mptr(P, P->status.pr_brkbase +
1977 1975 P->status.pr_brksize - 1)) != NULL)
1978 1976 brk_mp->map_pmap.pr_mflags |= MA_BREAK;
1979 1977 else
1980 1978 brk_mp = NULL;
1981 1979
1982 1980 if ((stk_mp = Paddr2mptr(P, P->status.pr_stkbase)) != NULL)
1983 1981 stk_mp->map_pmap.pr_mflags |= MA_STACK;
1984 1982
1985 1983 /*
1986 1984 * At this point, we have enough information to look for the
1987 1985 * executable and open it: we have access to the auxv, a psinfo_t,
1988 1986 * and the ability to read from mappings provided by the core file.
1989 1987 */
1990 1988 (void) Pfindexec(P, aout_path, core_exec_open, &aout);
1991 1989 dprintf("P->execname = \"%s\"\n", P->execname ? P->execname : "NULL");
1992 1990 execname = P->execname ? P->execname : "a.out";
1993 1991
1994 1992 /*
1995 1993 * Iterate through the sections, looking for the .dynamic and .interp
1996 1994 * sections. If we encounter them, remember their section pointers.
1997 1995 */
1998 1996 for (scn = NULL; (scn = elf_nextscn(aout.e_elf, scn)) != NULL; ) {
1999 1997 char *sname;
2000 1998
2001 1999 if ((gelf_getshdr(scn, &shdr) == NULL) ||
2002 2000 (sname = elf_strptr(aout.e_elf, aout.e_hdr.e_shstrndx,
2003 2001 (size_t)shdr.sh_name)) == NULL)
2004 2002 continue;
2005 2003
2006 2004 if (strcmp(sname, ".interp") == 0)
2007 2005 intp_scn = scn;
2008 2006 }
2009 2007
2010 2008 /*
2011 2009 * Get the AT_BASE auxv element. If this is missing (-1), then
2012 2010 * we assume this is a statically-linked executable.
2013 2011 */
2014 2012 base_addr = Pgetauxval(P, AT_BASE);
2015 2013
2016 2014 /*
2017 2015 * In order to get librtld_db initialized, we'll need to identify
2018 2016 * and name the mapping corresponding to the run-time linker. The
2019 2017 * AT_BASE auxv element tells us the address where it was mapped,
2020 2018 * and the .interp section of the executable tells us its path.
2021 2019 * If for some reason that doesn't pan out, just use ld.so.1.
2022 2020 */
2023 2021 if (intp_scn != NULL && (dp = elf_getdata(intp_scn, NULL)) != NULL &&
2024 2022 dp->d_size != 0) {
2025 2023 dprintf(".interp = <%s>\n", (char *)dp->d_buf);
2026 2024 interp = dp->d_buf;
2027 2025
2028 2026 } else if (base_addr != (uintptr_t)-1L) {
2029 2027 if (P->core->core_dmodel == PR_MODEL_LP64)
2030 2028 interp = "/usr/lib/64/ld.so.1";
2031 2029 else
2032 2030 interp = "/usr/lib/ld.so.1";
2033 2031
2034 2032 dprintf(".interp section is missing or could not be read; "
2035 2033 "defaulting to %s\n", interp);
2036 2034 } else
2037 2035 dprintf("detected statically linked executable\n");
2038 2036
2039 2037 /*
2040 2038 * If we have an AT_BASE element, name the mapping at that address
2041 2039 * using the interpreter pathname. Name the corresponding data
2042 2040 * mapping after the interpreter as well.
2043 2041 */
2044 2042 if (base_addr != (uintptr_t)-1L) {
2045 2043 elf_file_t intf;
2046 2044
2047 2045 P->map_ldso = core_name_mapping(P, base_addr, interp);
2048 2046
2049 2047 if (core_elf_open(&intf, interp, ET_DYN, NULL) == 0) {
2050 2048 rd_loadobj_t rl;
2051 2049 map_info_t *dmp;
2052 2050
2053 2051 rl.rl_base = base_addr;
2054 2052 dmp = core_find_data(P, intf.e_elf, &rl);
2055 2053
2056 2054 if (dmp != NULL) {
2057 2055 dprintf("renamed data at %p to %s\n",
2058 2056 (void *)rl.rl_data_base, interp);
2059 2057 (void) strncpy(dmp->map_pmap.pr_mapname,
2060 2058 interp, PRMAPSZ);
2061 2059 dmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2062 2060 }
2063 2061 }
2064 2062
2065 2063 core_elf_close(&intf);
2066 2064 }
2067 2065
2068 2066 /*
2069 2067 * If we have an AT_ENTRY element, name the mapping at that address
2070 2068 * using the special name "a.out" just like /proc does.
2071 2069 */
2072 2070 if ((addr = Pgetauxval(P, AT_ENTRY)) != (uintptr_t)-1L)
2073 2071 P->map_exec = core_name_mapping(P, addr, "a.out");
2074 2072
2075 2073 /*
2076 2074 * If we're a statically linked executable, then just locate the
2077 2075 * executable's text and data and name them after the executable.
2078 2076 */
2079 2077 if (base_addr == (uintptr_t)-1L) {
2080 2078 map_info_t *tmp, *dmp;
2081 2079 file_info_t *fp;
2082 2080 rd_loadobj_t rl;
2083 2081
2084 2082 if ((tmp = core_find_text(P, aout.e_elf, &rl)) != NULL &&
2085 2083 (dmp = core_find_data(P, aout.e_elf, &rl)) != NULL) {
2086 2084 (void) strncpy(tmp->map_pmap.pr_mapname,
2087 2085 execname, PRMAPSZ);
2088 2086 tmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2089 2087 (void) strncpy(dmp->map_pmap.pr_mapname,
2090 2088 execname, PRMAPSZ);
2091 2089 dmp->map_pmap.pr_mapname[PRMAPSZ - 1] = '\0';
2092 2090 }
2093 2091
2094 2092 if ((P->map_exec = tmp) != NULL &&
2095 2093 (fp = malloc(sizeof (file_info_t))) != NULL) {
2096 2094
2097 2095 (void) memset(fp, 0, sizeof (file_info_t));
2098 2096
2099 2097 list_link(fp, &P->file_head);
2100 2098 tmp->map_file = fp;
2101 2099 P->num_files++;
2102 2100
2103 2101 fp->file_ref = 1;
2104 2102 fp->file_fd = -1;
2105 2103
2106 2104 fp->file_lo = malloc(sizeof (rd_loadobj_t));
2107 2105 fp->file_lname = strdup(execname);
2108 2106
2109 2107 if (fp->file_lo)
2110 2108 *fp->file_lo = rl;
2111 2109 if (fp->file_lname)
2112 2110 fp->file_lbase = basename(fp->file_lname);
2113 2111 if (fp->file_rname)
2114 2112 fp->file_rbase = basename(fp->file_rname);
2115 2113
2116 2114 (void) strcpy(fp->file_pname,
2117 2115 P->mappings[0].map_pmap.pr_mapname);
2118 2116 fp->file_map = tmp;
2119 2117
2120 2118 Pbuild_file_symtab(P, fp);
2121 2119
2122 2120 if (dmp != NULL) {
2123 2121 dmp->map_file = fp;
2124 2122 fp->file_ref++;
2125 2123 }
2126 2124 }
2127 2125 }
2128 2126
2129 2127 core_elf_close(&aout);
2130 2128
2131 2129 /*
2132 2130 * We now have enough information to initialize librtld_db.
2133 2131 * After it warms up, we can iterate through the load object chain
2134 2132 * in the core, which will allow us to construct the file info
2135 2133 * we need to provide symbol information for the other shared
2136 2134 * libraries, and also to fill in the missing mapping names.
2137 2135 */
2138 2136 rd_log(_libproc_debug);
2139 2137
2140 2138 if ((P->rap = rd_new(P)) != NULL) {
2141 2139 (void) rd_loadobj_iter(P->rap, (rl_iter_f *)
2142 2140 core_iter_mapping, P);
2143 2141
2144 2142 if (P->core->core_errno != 0) {
2145 2143 errno = P->core->core_errno;
2146 2144 *perr = G_STRANGE;
2147 2145 goto err;
2148 2146 }
2149 2147 } else
2150 2148 dprintf("failed to initialize rtld_db agent\n");
2151 2149
2152 2150 /*
2153 2151 * If there are sections, load them and process the data from any
2154 2152 * sections that we can use to annotate the file_info_t's.
2155 2153 */
2156 2154 core_load_shdrs(P, &core);
2157 2155
2158 2156 /*
2159 2157 * If we previously located a stack or break mapping, and they are
2160 2158 * still anonymous, we now assume that they were MAP_ANON mappings.
2161 2159 * If brk_mp turns out to now have a name, then the heap is still
2162 2160 * sitting at the end of the executable's data+bss mapping: remove
2163 2161 * the previous MA_BREAK setting to be consistent with /proc.
2164 2162 */
2165 2163 if (stk_mp != NULL && stk_mp->map_pmap.pr_mapname[0] == '\0')
2166 2164 stk_mp->map_pmap.pr_mflags |= MA_ANON;
2167 2165 if (brk_mp != NULL && brk_mp->map_pmap.pr_mapname[0] == '\0')
2168 2166 brk_mp->map_pmap.pr_mflags |= MA_ANON;
2169 2167 else if (brk_mp != NULL)
2170 2168 brk_mp->map_pmap.pr_mflags &= ~MA_BREAK;
2171 2169
2172 2170 *perr = 0;
2173 2171 return (P);
2174 2172
2175 2173 err:
2176 2174 Pfree(P);
2177 2175 core_elf_close(&aout);
2178 2176 return (NULL);
2179 2177 }
2180 2178
2181 2179 /*
2182 2180 * Grab a core file using a pathname. We just open it and call Pfgrab_core().
2183 2181 */
2184 2182 struct ps_prochandle *
2185 2183 Pgrab_core(const char *core, const char *aout, int gflag, int *perr)
2186 2184 {
2187 2185 int fd, oflag = (gflag & PGRAB_RDONLY) ? O_RDONLY : O_RDWR;
2188 2186
2189 2187 if ((fd = open64(core, oflag)) >= 0)
2190 2188 return (Pfgrab_core(fd, aout, perr));
2191 2189
2192 2190 if (errno != ENOENT)
2193 2191 *perr = G_STRANGE;
2194 2192 else
2195 2193 *perr = G_NOCORE;
2196 2194
2197 2195 return (NULL);
2198 2196 }
↓ open down ↓ |
1492 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX