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