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