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