4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 * Copyright 2012 DEY Storage Systems, Inc. All rights reserved.
28 * Copyright 2015 Joyent, Inc.
29 * Copyright (c) 2013 by Delphix. All rights reserved.
32 #define _STRUCTURED_PROC 1
42 #include <sys/machelf.h>
43 #include <sys/sysmacros.h>
44 #include <sys/systeminfo.h>
46 #include <sys/utsname.h>
48 #include <sys/old_procfs.h>
64 static const char *shstrtab_data
[] = {
74 typedef struct shstrtab
{
80 struct ps_prochandle
*P
;
85 core_content_t pgc_content
;
89 shstrtab_t pgc_shstrtab
;
98 gc_pwrite64(int fd
, const void *buf
, size_t len
, off64_t off
)
102 err
= pwrite64(fd
, buf
, len
, off
);
108 * We will take a page from ZFS's book here and use the otherwise
109 * unused EBADE to mean a short write. Typically this will actually
110 * result from ENOSPC or EDQUOT, but we can't be sure.
121 shstrtab_init(shstrtab_t
*s
)
123 bzero(&s
->sst_ndx
, sizeof (s
->sst_ndx
));
128 shstrtab_ndx(shstrtab_t
*s
, shstrtype_t type
)
132 if ((ret
= s
->sst_ndx
[type
]) != 0 || type
== STR_NONE
)
135 ret
= s
->sst_ndx
[type
] = s
->sst_cur
;
136 s
->sst_cur
+= strlen(shstrtab_data
[type
]) + 1;
142 shstrtab_size(const shstrtab_t
*s
)
148 Pgcore(struct ps_prochandle
*P
, const char *fname
, core_content_t content
)
154 if ((fd
= creat64(fname
, 0666)) < 0)
157 if ((err
= Pfgcore(P
, fd
, content
)) != 0) {
160 (void) unlink(fname
);
169 * Since we don't want to use the old-school procfs interfaces, we use the
170 * new-style data structures we already have to construct the old-style
171 * data structures. We include these data structures in core files for
172 * backward compatability.
176 mkprstatus(struct ps_prochandle
*P
, const lwpstatus_t
*lsp
,
177 const lwpsinfo_t
*lip
, prstatus_t
*psp
)
179 bzero(psp
, sizeof (*psp
));
181 if (lsp
->pr_flags
& PR_STOPPED
)
182 psp
->pr_flags
= 0x0001;
183 if (lsp
->pr_flags
& PR_ISTOP
)
184 psp
->pr_flags
= 0x0002;
185 if (lsp
->pr_flags
& PR_DSTOP
)
186 psp
->pr_flags
= 0x0004;
187 if (lsp
->pr_flags
& PR_ASLEEP
)
188 psp
->pr_flags
= 0x0008;
189 if (lsp
->pr_flags
& PR_FORK
)
190 psp
->pr_flags
= 0x0010;
191 if (lsp
->pr_flags
& PR_RLC
)
192 psp
->pr_flags
= 0x0020;
194 * Note that PR_PTRACE (0x0040) from <sys/old_procfs.h> is never set;
195 * PR_PCOMPAT corresponds to PR_PTRACE in the newer <sys/procfs.h>.
197 if (lsp
->pr_flags
& PR_PCINVAL
)
198 psp
->pr_flags
= 0x0080;
199 if (lsp
->pr_flags
& PR_ISSYS
)
200 psp
->pr_flags
= 0x0100;
201 if (lsp
->pr_flags
& PR_STEP
)
202 psp
->pr_flags
= 0x0200;
203 if (lsp
->pr_flags
& PR_KLC
)
204 psp
->pr_flags
= 0x0400;
205 if (lsp
->pr_flags
& PR_ASYNC
)
206 psp
->pr_flags
= 0x0800;
207 if (lsp
->pr_flags
& PR_PTRACE
)
208 psp
->pr_flags
= 0x1000;
209 if (lsp
->pr_flags
& PR_MSACCT
)
210 psp
->pr_flags
= 0x2000;
211 if (lsp
->pr_flags
& PR_BPTADJ
)
212 psp
->pr_flags
= 0x4000;
213 if (lsp
->pr_flags
& PR_ASLWP
)
214 psp
->pr_flags
= 0x8000;
216 psp
->pr_why
= lsp
->pr_why
;
217 psp
->pr_what
= lsp
->pr_what
;
218 psp
->pr_info
= lsp
->pr_info
;
219 psp
->pr_cursig
= lsp
->pr_cursig
;
220 psp
->pr_nlwp
= P
->status
.pr_nlwp
;
221 psp
->pr_sigpend
= P
->status
.pr_sigpend
;
222 psp
->pr_sighold
= lsp
->pr_lwphold
;
223 psp
->pr_altstack
= lsp
->pr_altstack
;
224 psp
->pr_action
= lsp
->pr_action
;
225 psp
->pr_pid
= P
->status
.pr_pid
;
226 psp
->pr_ppid
= P
->status
.pr_ppid
;
227 psp
->pr_pgrp
= P
->status
.pr_pgid
;
228 psp
->pr_sid
= P
->status
.pr_sid
;
229 psp
->pr_utime
= P
->status
.pr_utime
;
230 psp
->pr_stime
= P
->status
.pr_stime
;
231 psp
->pr_cutime
= P
->status
.pr_cutime
;
232 psp
->pr_cstime
= P
->status
.pr_cstime
;
233 (void) strncpy(psp
->pr_clname
, lsp
->pr_clname
, sizeof (psp
->pr_clname
));
234 psp
->pr_syscall
= lsp
->pr_syscall
;
235 psp
->pr_nsysarg
= lsp
->pr_nsysarg
;
236 bcopy(lsp
->pr_sysarg
, psp
->pr_sysarg
, sizeof (psp
->pr_sysarg
));
237 psp
->pr_who
= lsp
->pr_lwpid
;
238 psp
->pr_lwppend
= lsp
->pr_lwppend
;
239 psp
->pr_oldcontext
= (ucontext_t
*)lsp
->pr_oldcontext
;
240 psp
->pr_brkbase
= (caddr_t
)P
->status
.pr_brkbase
;
241 psp
->pr_brksize
= P
->status
.pr_brksize
;
242 psp
->pr_stkbase
= (caddr_t
)P
->status
.pr_stkbase
;
243 psp
->pr_stksize
= P
->status
.pr_stksize
;
244 psp
->pr_processor
= (short)lip
->pr_onpro
;
245 psp
->pr_bind
= (short)lip
->pr_bindpro
;
246 psp
->pr_instr
= lsp
->pr_instr
;
247 bcopy(lsp
->pr_reg
, psp
->pr_reg
, sizeof (psp
->pr_sysarg
));
251 mkprpsinfo(struct ps_prochandle
*P
, prpsinfo_t
*psp
)
253 bzero(psp
, sizeof (*psp
));
254 psp
->pr_state
= P
->psinfo
.pr_lwp
.pr_state
;
255 psp
->pr_sname
= P
->psinfo
.pr_lwp
.pr_sname
;
256 psp
->pr_zomb
= (psp
->pr_state
== SZOMB
);
257 psp
->pr_nice
= P
->psinfo
.pr_lwp
.pr_nice
;
258 psp
->pr_flag
= P
->psinfo
.pr_lwp
.pr_flag
;
259 psp
->pr_uid
= P
->psinfo
.pr_uid
;
260 psp
->pr_gid
= P
->psinfo
.pr_gid
;
261 psp
->pr_pid
= P
->psinfo
.pr_pid
;
262 psp
->pr_ppid
= P
->psinfo
.pr_ppid
;
263 psp
->pr_pgrp
= P
->psinfo
.pr_pgid
;
264 psp
->pr_sid
= P
->psinfo
.pr_sid
;
265 psp
->pr_addr
= (caddr_t
)P
->psinfo
.pr_addr
;
266 psp
->pr_size
= P
->psinfo
.pr_size
;
267 psp
->pr_rssize
= P
->psinfo
.pr_rssize
;
268 psp
->pr_wchan
= (caddr_t
)P
->psinfo
.pr_lwp
.pr_wchan
;
269 psp
->pr_start
= P
->psinfo
.pr_start
;
270 psp
->pr_time
= P
->psinfo
.pr_time
;
271 psp
->pr_pri
= P
->psinfo
.pr_lwp
.pr_pri
;
272 psp
->pr_oldpri
= P
->psinfo
.pr_lwp
.pr_oldpri
;
273 psp
->pr_cpu
= P
->psinfo
.pr_lwp
.pr_cpu
;
274 psp
->pr_ottydev
= cmpdev(P
->psinfo
.pr_ttydev
);
275 psp
->pr_lttydev
= P
->psinfo
.pr_ttydev
;
276 (void) strncpy(psp
->pr_clname
, P
->psinfo
.pr_lwp
.pr_clname
,
277 sizeof (psp
->pr_clname
));
278 (void) strncpy(psp
->pr_fname
, P
->psinfo
.pr_fname
,
279 sizeof (psp
->pr_fname
));
280 bcopy(&P
->psinfo
.pr_psargs
, &psp
->pr_psargs
,
281 sizeof (psp
->pr_psargs
));
282 psp
->pr_syscall
= P
->psinfo
.pr_lwp
.pr_syscall
;
283 psp
->pr_ctime
= P
->psinfo
.pr_ctime
;
284 psp
->pr_bysize
= psp
->pr_size
* PAGESIZE
;
285 psp
->pr_byrssize
= psp
->pr_rssize
* PAGESIZE
;
286 psp
->pr_argc
= P
->psinfo
.pr_argc
;
287 psp
->pr_argv
= (char **)P
->psinfo
.pr_argv
;
288 psp
->pr_envp
= (char **)P
->psinfo
.pr_envp
;
289 psp
->pr_wstat
= P
->psinfo
.pr_wstat
;
290 psp
->pr_pctcpu
= P
->psinfo
.pr_pctcpu
;
291 psp
->pr_pctmem
= P
->psinfo
.pr_pctmem
;
292 psp
->pr_euid
= P
->psinfo
.pr_euid
;
293 psp
->pr_egid
= P
->psinfo
.pr_egid
;
295 psp
->pr_dmodel
= P
->psinfo
.pr_dmodel
;
301 mkprstatus32(struct ps_prochandle
*P
, const lwpstatus_t
*lsp
,
302 const lwpsinfo_t
*lip
, prstatus32_t
*psp
)
304 bzero(psp
, sizeof (*psp
));
306 if (lsp
->pr_flags
& PR_STOPPED
)
307 psp
->pr_flags
= 0x0001;
308 if (lsp
->pr_flags
& PR_ISTOP
)
309 psp
->pr_flags
= 0x0002;
310 if (lsp
->pr_flags
& PR_DSTOP
)
311 psp
->pr_flags
= 0x0004;
312 if (lsp
->pr_flags
& PR_ASLEEP
)
313 psp
->pr_flags
= 0x0008;
314 if (lsp
->pr_flags
& PR_FORK
)
315 psp
->pr_flags
= 0x0010;
316 if (lsp
->pr_flags
& PR_RLC
)
317 psp
->pr_flags
= 0x0020;
319 * Note that PR_PTRACE (0x0040) from <sys/old_procfs.h> is never set;
320 * PR_PCOMPAT corresponds to PR_PTRACE in the newer <sys/procfs.h>.
322 if (lsp
->pr_flags
& PR_PCINVAL
)
323 psp
->pr_flags
= 0x0080;
324 if (lsp
->pr_flags
& PR_ISSYS
)
325 psp
->pr_flags
= 0x0100;
326 if (lsp
->pr_flags
& PR_STEP
)
327 psp
->pr_flags
= 0x0200;
328 if (lsp
->pr_flags
& PR_KLC
)
329 psp
->pr_flags
= 0x0400;
330 if (lsp
->pr_flags
& PR_ASYNC
)
331 psp
->pr_flags
= 0x0800;
332 if (lsp
->pr_flags
& PR_PTRACE
)
333 psp
->pr_flags
= 0x1000;
334 if (lsp
->pr_flags
& PR_MSACCT
)
335 psp
->pr_flags
= 0x2000;
336 if (lsp
->pr_flags
& PR_BPTADJ
)
337 psp
->pr_flags
= 0x4000;
338 if (lsp
->pr_flags
& PR_ASLWP
)
339 psp
->pr_flags
= 0x8000;
341 psp
->pr_why
= lsp
->pr_why
;
342 psp
->pr_what
= lsp
->pr_what
;
343 siginfo_n_to_32(&lsp
->pr_info
, &psp
->pr_info
);
344 psp
->pr_cursig
= lsp
->pr_cursig
;
345 psp
->pr_nlwp
= P
->status
.pr_nlwp
;
346 psp
->pr_sigpend
= P
->status
.pr_sigpend
;
347 psp
->pr_sighold
= lsp
->pr_lwphold
;
348 stack_n_to_32(&lsp
->pr_altstack
, &psp
->pr_altstack
);
349 sigaction_n_to_32(&lsp
->pr_action
, &psp
->pr_action
);
350 psp
->pr_pid
= P
->status
.pr_pid
;
351 psp
->pr_ppid
= P
->status
.pr_ppid
;
352 psp
->pr_pgrp
= P
->status
.pr_pgid
;
353 psp
->pr_sid
= P
->status
.pr_sid
;
354 timestruc_n_to_32(&P
->status
.pr_utime
, &psp
->pr_utime
);
355 timestruc_n_to_32(&P
->status
.pr_stime
, &psp
->pr_stime
);
356 timestruc_n_to_32(&P
->status
.pr_cutime
, &psp
->pr_cutime
);
357 timestruc_n_to_32(&P
->status
.pr_cstime
, &psp
->pr_cstime
);
358 (void) strncpy(psp
->pr_clname
, lsp
->pr_clname
, sizeof (psp
->pr_clname
));
359 psp
->pr_syscall
= lsp
->pr_syscall
;
360 psp
->pr_nsysarg
= lsp
->pr_nsysarg
;
361 bcopy(lsp
->pr_sysarg
, psp
->pr_sysarg
, sizeof (psp
->pr_sysarg
));
362 psp
->pr_who
= lsp
->pr_lwpid
;
363 psp
->pr_lwppend
= lsp
->pr_lwppend
;
364 psp
->pr_oldcontext
= (caddr32_t
)lsp
->pr_oldcontext
;
365 psp
->pr_brkbase
= (caddr32_t
)P
->status
.pr_brkbase
;
366 psp
->pr_brksize
= P
->status
.pr_brksize
;
367 psp
->pr_stkbase
= (caddr32_t
)P
->status
.pr_stkbase
;
368 psp
->pr_stksize
= P
->status
.pr_stksize
;
369 psp
->pr_processor
= (short)lip
->pr_onpro
;
370 psp
->pr_bind
= (short)lip
->pr_bindpro
;
371 psp
->pr_instr
= lsp
->pr_instr
;
372 bcopy(lsp
->pr_reg
, psp
->pr_reg
, sizeof (psp
->pr_sysarg
));
376 mkprpsinfo32(struct ps_prochandle
*P
, prpsinfo32_t
*psp
)
378 bzero(psp
, sizeof (*psp
));
379 psp
->pr_state
= P
->psinfo
.pr_lwp
.pr_state
;
380 psp
->pr_sname
= P
->psinfo
.pr_lwp
.pr_sname
;
381 psp
->pr_zomb
= (psp
->pr_state
== SZOMB
);
382 psp
->pr_nice
= P
->psinfo
.pr_lwp
.pr_nice
;
383 psp
->pr_flag
= P
->psinfo
.pr_lwp
.pr_flag
;
384 psp
->pr_uid
= P
->psinfo
.pr_uid
;
385 psp
->pr_gid
= P
->psinfo
.pr_gid
;
386 psp
->pr_pid
= P
->psinfo
.pr_pid
;
387 psp
->pr_ppid
= P
->psinfo
.pr_ppid
;
388 psp
->pr_pgrp
= P
->psinfo
.pr_pgid
;
389 psp
->pr_sid
= P
->psinfo
.pr_sid
;
390 psp
->pr_addr
= (caddr32_t
)P
->psinfo
.pr_addr
;
391 psp
->pr_size
= P
->psinfo
.pr_size
;
392 psp
->pr_rssize
= P
->psinfo
.pr_rssize
;
393 psp
->pr_wchan
= (caddr32_t
)P
->psinfo
.pr_lwp
.pr_wchan
;
394 timestruc_n_to_32(&P
->psinfo
.pr_start
, &psp
->pr_start
);
395 timestruc_n_to_32(&P
->psinfo
.pr_time
, &psp
->pr_time
);
396 psp
->pr_pri
= P
->psinfo
.pr_lwp
.pr_pri
;
397 psp
->pr_oldpri
= P
->psinfo
.pr_lwp
.pr_oldpri
;
398 psp
->pr_cpu
= P
->psinfo
.pr_lwp
.pr_cpu
;
399 psp
->pr_ottydev
= cmpdev(P
->psinfo
.pr_ttydev
);
400 psp
->pr_lttydev
= prcmpldev(P
->psinfo
.pr_ttydev
);
401 (void) strncpy(psp
->pr_clname
, P
->psinfo
.pr_lwp
.pr_clname
,
402 sizeof (psp
->pr_clname
));
403 (void) strncpy(psp
->pr_fname
, P
->psinfo
.pr_fname
,
404 sizeof (psp
->pr_fname
));
405 bcopy(&P
->psinfo
.pr_psargs
, &psp
->pr_psargs
,
406 sizeof (psp
->pr_psargs
));
407 psp
->pr_syscall
= P
->psinfo
.pr_lwp
.pr_syscall
;
408 timestruc_n_to_32(&P
->psinfo
.pr_ctime
, &psp
->pr_ctime
);
409 psp
->pr_bysize
= psp
->pr_size
* PAGESIZE
;
410 psp
->pr_byrssize
= psp
->pr_rssize
* PAGESIZE
;
411 psp
->pr_argc
= P
->psinfo
.pr_argc
;
412 psp
->pr_argv
= (caddr32_t
)P
->psinfo
.pr_argv
;
413 psp
->pr_envp
= (caddr32_t
)P
->psinfo
.pr_envp
;
414 psp
->pr_wstat
= P
->psinfo
.pr_wstat
;
415 psp
->pr_pctcpu
= P
->psinfo
.pr_pctcpu
;
416 psp
->pr_pctmem
= P
->psinfo
.pr_pctmem
;
417 psp
->pr_euid
= P
->psinfo
.pr_euid
;
418 psp
->pr_egid
= P
->psinfo
.pr_egid
;
420 psp
->pr_dmodel
= P
->psinfo
.pr_dmodel
;
426 write_note(int fd
, uint_t type
, const void *desc
, size_t descsz
, off64_t
*offp
)
429 * Note headers are the same regardless of the data model of the
430 * ELF file; we arbitrarily use Elf64_Nhdr here.
437 bzero(&n
, sizeof (n
));
438 bcopy("CORE", n
.name
, 4);
439 n
.nhdr
.n_type
= type
;
441 n
.nhdr
.n_descsz
= roundup(descsz
, 4);
443 if (gc_pwrite64(fd
, &n
, sizeof (n
), *offp
) != 0)
448 if (gc_pwrite64(fd
, desc
, n
.nhdr
.n_descsz
, *offp
) != 0)
451 *offp
+= n
.nhdr
.n_descsz
;
457 old_per_lwp(void *data
, const lwpstatus_t
*lsp
, const lwpsinfo_t
*lip
)
459 pgcore_t
*pgc
= data
;
460 struct ps_prochandle
*P
= pgc
->P
;
463 * Legacy core files don't contain information about zombie LWPs.
464 * We use Plwp_iter_all() so that we get the lwpsinfo_t structure
470 if (P
->status
.pr_dmodel
== PR_MODEL_NATIVE
) {
472 mkprstatus(P
, lsp
, lip
, &prstatus
);
473 if (write_note(pgc
->pgc_fd
, NT_PRSTATUS
, &prstatus
,
474 sizeof (prstatus_t
), pgc
->pgc_doff
) != 0)
476 if (write_note(pgc
->pgc_fd
, NT_PRFPREG
, &lsp
->pr_fpreg
,
477 sizeof (prfpregset_t
), pgc
->pgc_doff
) != 0)
483 mkprstatus32(P
, lsp
, lip
, &pr32
);
484 if (write_note(pgc
->pgc_fd
, NT_PRSTATUS
, &pr32
,
485 sizeof (prstatus32_t
), pgc
->pgc_doff
) != 0)
487 prfpregset_n_to_32(&lsp
->pr_fpreg
, &pf32
);
488 if (write_note(pgc
->pgc_fd
, NT_PRFPREG
, &pf32
,
489 sizeof (prfpregset32_t
), pgc
->pgc_doff
) != 0)
497 if (Plwp_getxregs(P
, lsp
->pr_lwpid
, &xregs
) == 0 &&
498 write_note(pgc
->pgc_fd
, NT_PRXREG
, &xregs
,
499 sizeof (prxregset_t
), pgc
->pgc_doff
) != 0)
508 new_per_lwp(void *data
, const lwpstatus_t
*lsp
, const lwpsinfo_t
*lip
)
510 pgcore_t
*pgc
= data
;
511 struct ps_prochandle
*P
= pgc
->P
;
515 * If lsp is NULL this indicates that this is a zombie LWP in
516 * which case we dump only the lwpsinfo_t structure and none of
517 * the other ancillary LWP state data.
519 if (P
->status
.pr_dmodel
== PR_MODEL_NATIVE
) {
520 if (write_note(pgc
->pgc_fd
, NT_LWPSINFO
, lip
,
521 sizeof (lwpsinfo_t
), pgc
->pgc_doff
) != 0)
525 if (write_note(pgc
->pgc_fd
, NT_LWPSTATUS
, lsp
,
526 sizeof (lwpstatus_t
), pgc
->pgc_doff
) != 0)
532 lwpsinfo_n_to_32(lip
, &li32
);
533 if (write_note(pgc
->pgc_fd
, NT_LWPSINFO
, &li32
,
534 sizeof (lwpsinfo32_t
), pgc
->pgc_doff
) != 0)
538 lwpstatus_n_to_32(lsp
, &ls32
);
539 if (write_note(pgc
->pgc_fd
, NT_LWPSTATUS
, &ls32
,
540 sizeof (lwpstatus32_t
), pgc
->pgc_doff
) != 0)
551 if (Plwp_getxregs(P
, lsp
->pr_lwpid
, &xregs
) == 0) {
552 if (write_note(pgc
->pgc_fd
, NT_PRXREG
, &xregs
,
553 sizeof (prxregset_t
), pgc
->pgc_doff
) != 0)
557 if (Plwp_getgwindows(P
, lsp
->pr_lwpid
, &gwins
) == 0 &&
559 size
= sizeof (gwins
) - sizeof (gwins
.wbuf
) +
560 gwins
.wbcnt
* sizeof (gwins
.wbuf
[0]);
562 if (write_note(pgc
->pgc_fd
, NT_GWINDOWS
, &gwins
, size
,
569 if (P
->status
.pr_dmodel
== PR_MODEL_LP64
) {
571 if (Plwp_getasrs(P
, lsp
->pr_lwpid
, asrs
) == 0) {
572 if (write_note(pgc
->pgc_fd
, NT_ASRS
, &asrs
,
573 sizeof (asrset_t
), pgc
->pgc_doff
) != 0)
577 #endif /* __sparcv9 */
580 if (!(lsp
->pr_flags
& PR_AGENT
))
583 if (Plwp_getspymaster(P
, lsp
->pr_lwpid
, &ps
) != 0)
586 if (P
->status
.pr_dmodel
== PR_MODEL_NATIVE
) {
587 if (write_note(pgc
->pgc_fd
, NT_SPYMASTER
, &ps
,
588 sizeof (psinfo_t
), pgc
->pgc_doff
) != 0)
593 psinfo_n_to_32(&ps
, &ps32
);
594 if (write_note(pgc
->pgc_fd
, NT_SPYMASTER
, &ps32
,
595 sizeof (psinfo32_t
), pgc
->pgc_doff
) != 0)
605 iter_fd(void *data
, prfdinfo_t
*fdinfo
)
607 fditer_t
*iter
= data
;
609 if (write_note(iter
->fd_fd
, NT_FDINFO
, fdinfo
,
610 sizeof (*fdinfo
), iter
->fd_doff
) != 0)
616 count_sections(pgcore_t
*pgc
)
618 struct ps_prochandle
*P
= pgc
->P
;
623 if (!(pgc
->pgc_content
& (CC_CONTENT_CTF
| CC_CONTENT_SYMTAB
)))
626 fptr
= list_next(&P
->file_head
);
627 for (cnt
= P
->num_files
; cnt
> 0; cnt
--, fptr
= list_next(fptr
)) {
630 Pbuild_file_symtab(P
, fptr
);
632 if ((pgc
->pgc_content
& CC_CONTENT_CTF
) &&
633 Pbuild_file_ctf(P
, fptr
) != NULL
) {
638 if (fptr
->file_ctf_dyn
) {
639 sym
= &fptr
->file_dynsym
;
641 sym
= &fptr
->file_symtab
;
645 if (sym
->sym_data_pri
!= NULL
&& sym
->sym_symn
!= 0 &&
646 sym
->sym_strs
!= NULL
)
650 if ((pgc
->pgc_content
& CC_CONTENT_SYMTAB
) && !hit_symtab
&&
651 fptr
->file_symtab
.sym_data_pri
!= NULL
&&
652 fptr
->file_symtab
.sym_symn
!= 0 &&
653 fptr
->file_symtab
.sym_strs
!= NULL
) {
658 return (nshdrs
== 0 ? 0 : nshdrs
+ 2);
662 write_shdr(pgcore_t
*pgc
, shstrtype_t name
, uint_t type
, ulong_t flags
,
663 uintptr_t addr
, ulong_t offset
, size_t size
, uint_t link
, uint_t info
,
664 uintptr_t addralign
, uintptr_t entsize
)
666 if (pgc
->P
->status
.pr_dmodel
== PR_MODEL_ILP32
) {
669 bzero(&shdr
, sizeof (shdr
));
670 shdr
.sh_name
= shstrtab_ndx(&pgc
->pgc_shstrtab
, name
);
672 shdr
.sh_flags
= flags
;
673 shdr
.sh_addr
= (Elf32_Addr
)addr
;
674 shdr
.sh_offset
= offset
;
678 shdr
.sh_addralign
= addralign
;
679 shdr
.sh_entsize
= entsize
;
681 if (gc_pwrite64(pgc
->pgc_fd
, &shdr
, sizeof (shdr
),
682 *pgc
->pgc_soff
) != 0)
685 *pgc
->pgc_soff
+= sizeof (shdr
);
690 bzero(&shdr
, sizeof (shdr
));
691 shdr
.sh_name
= shstrtab_ndx(&pgc
->pgc_shstrtab
, name
);
693 shdr
.sh_flags
= flags
;
695 shdr
.sh_offset
= offset
;
699 shdr
.sh_addralign
= addralign
;
700 shdr
.sh_entsize
= entsize
;
702 if (gc_pwrite64(pgc
->pgc_fd
, &shdr
, sizeof (shdr
),
703 *pgc
->pgc_soff
) != 0)
706 *pgc
->pgc_soff
+= sizeof (shdr
);
714 dump_symtab(pgcore_t
*pgc
, file_info_t
*fptr
, uint_t index
, int dynsym
)
716 sym_tbl_t
*sym
= dynsym
? &fptr
->file_dynsym
: &fptr
->file_symtab
;
717 shstrtype_t symname
= dynsym
? STR_DYNSYM
: STR_SYMTAB
;
718 shstrtype_t strname
= dynsym
? STR_DYNSTR
: STR_STRTAB
;
719 uint_t symtype
= dynsym
? SHT_DYNSYM
: SHT_SYMTAB
;
721 uintptr_t addr
= fptr
->file_map
->map_pmap
.pr_vaddr
;
723 if (sym
->sym_data_pri
== NULL
|| sym
->sym_symn
== 0 ||
724 sym
->sym_strs
== NULL
)
727 size
= sym
->sym_hdr_pri
.sh_size
;
728 if (gc_pwrite64(pgc
->pgc_fd
, sym
->sym_data_pri
->d_buf
, size
,
729 *pgc
->pgc_doff
) != 0)
732 if (write_shdr(pgc
, symname
, symtype
, 0, addr
, *pgc
->pgc_doff
, size
,
733 index
+ 1, sym
->sym_hdr_pri
.sh_info
, sym
->sym_hdr_pri
.sh_addralign
,
734 sym
->sym_hdr_pri
.sh_entsize
) != 0)
737 *pgc
->pgc_doff
+= roundup(size
, 8);
739 size
= sym
->sym_strhdr
.sh_size
;
740 if (gc_pwrite64(pgc
->pgc_fd
, sym
->sym_strs
, size
, *pgc
->pgc_doff
) != 0)
743 if (write_shdr(pgc
, strname
, SHT_STRTAB
, SHF_STRINGS
, addr
,
744 *pgc
->pgc_doff
, size
, 0, 0, 1, 0) != 0)
747 *pgc
->pgc_doff
+= roundup(size
, 8);
753 dump_sections(pgcore_t
*pgc
)
755 struct ps_prochandle
*P
= pgc
->P
;
760 if (!(pgc
->pgc_content
& (CC_CONTENT_CTF
| CC_CONTENT_SYMTAB
)))
763 fptr
= list_next(&P
->file_head
);
764 for (cnt
= P
->num_files
; cnt
> 0; cnt
--, fptr
= list_next(fptr
)) {
767 Pbuild_file_symtab(P
, fptr
);
769 if ((pgc
->pgc_content
& CC_CONTENT_CTF
) &&
770 Pbuild_file_ctf(P
, fptr
) != NULL
) {
776 * Write the symtab out first so we can correctly
777 * set the sh_link field in the CTF section header.
778 * symindex will be 0 if there is no corresponding
779 * symbol table section.
781 if (fptr
->file_ctf_dyn
) {
782 sym
= &fptr
->file_dynsym
;
785 sym
= &fptr
->file_symtab
;
790 if (sym
->sym_data_pri
!= NULL
&& sym
->sym_symn
!= 0 &&
791 sym
->sym_strs
!= NULL
) {
793 if (dump_symtab(pgc
, fptr
, index
, dynsym
) != 0)
799 * Write the CTF data that we've read out of the
800 * file itself into the core file.
802 if (gc_pwrite64(pgc
->pgc_fd
, fptr
->file_ctf_buf
,
803 fptr
->file_ctf_size
, *pgc
->pgc_doff
) != 0)
806 if (write_shdr(pgc
, STR_CTF
, SHT_PROGBITS
, 0,
807 fptr
->file_map
->map_pmap
.pr_vaddr
, *pgc
->pgc_doff
,
808 fptr
->file_ctf_size
, symindex
, 0, 4, 0) != 0)
812 *pgc
->pgc_doff
+= roundup(fptr
->file_ctf_size
, 8);
815 if ((pgc
->pgc_content
& CC_CONTENT_SYMTAB
) && !hit_symtab
&&
816 fptr
->file_symtab
.sym_data_pri
!= NULL
&&
817 fptr
->file_symtab
.sym_symn
!= 0 &&
818 fptr
->file_symtab
.sym_strs
!= NULL
) {
819 if (dump_symtab(pgc
, fptr
, index
, 0) != 0)
830 dump_map(void *data
, const prmap_t
*pmp
, const char *name
)
832 pgcore_t
*pgc
= data
;
833 struct ps_prochandle
*P
= pgc
->P
;
841 bzero(&phdr
, sizeof (phdr
));
842 phdr
.p_type
= PT_LOAD
;
843 phdr
.p_vaddr
= pmp
->pr_vaddr
;
844 phdr
.p_memsz
= pmp
->pr_size
;
845 if (pmp
->pr_mflags
& MA_READ
)
846 phdr
.p_flags
|= PF_R
;
847 if (pmp
->pr_mflags
& MA_WRITE
)
848 phdr
.p_flags
|= PF_W
;
849 if (pmp
->pr_mflags
& MA_EXEC
)
850 phdr
.p_flags
|= PF_X
;
852 if (pmp
->pr_vaddr
+ pmp
->pr_size
> P
->status
.pr_stkbase
&&
853 pmp
->pr_vaddr
< P
->status
.pr_stkbase
+ P
->status
.pr_stksize
) {
854 if (!(pgc
->pgc_content
& CC_CONTENT_STACK
))
857 } else if ((pmp
->pr_mflags
& MA_ANON
) &&
858 pmp
->pr_vaddr
+ pmp
->pr_size
> P
->status
.pr_brkbase
&&
859 pmp
->pr_vaddr
< P
->status
.pr_brkbase
+ P
->status
.pr_brksize
) {
860 if (!(pgc
->pgc_content
& CC_CONTENT_HEAP
))
863 } else if (pmp
->pr_mflags
& MA_ISM
) {
864 if (pmp
->pr_mflags
& MA_NORESERVE
) {
865 if (!(pgc
->pgc_content
& CC_CONTENT_DISM
))
868 if (!(pgc
->pgc_content
& CC_CONTENT_ISM
))
872 } else if (pmp
->pr_mflags
& MA_SHM
) {
873 if (!(pgc
->pgc_content
& CC_CONTENT_SHM
))
876 } else if (pmp
->pr_mflags
& MA_SHARED
) {
877 if (pmp
->pr_mflags
& MA_ANON
) {
878 if (!(pgc
->pgc_content
& CC_CONTENT_SHANON
))
881 if (!(pgc
->pgc_content
& CC_CONTENT_SHFILE
))
885 } else if (pmp
->pr_mflags
& MA_ANON
) {
886 if (!(pgc
->pgc_content
& CC_CONTENT_ANON
))
889 } else if (phdr
.p_flags
== (PF_R
| PF_X
)) {
890 if (!(pgc
->pgc_content
& CC_CONTENT_TEXT
))
893 } else if (phdr
.p_flags
== PF_R
) {
894 if (!(pgc
->pgc_content
& CC_CONTENT_RODATA
))
898 if (!(pgc
->pgc_content
& CC_CONTENT_DATA
))
903 while (n
< pmp
->pr_size
) {
904 size_t csz
= MIN(pmp
->pr_size
- n
, pgc
->pgc_chunksz
);
907 * If we can't read out part of the victim's address
908 * space for some reason ignore that failure and try to
909 * emit a partial core file without that mapping's data.
910 * As in the kernel, we mark these failures with the
911 * PF_SUNW_FAILURE flag and store the errno where the
912 * mapping would have been.
914 if (Pread(P
, pgc
->pgc_chunk
, csz
, pmp
->pr_vaddr
+ n
) != csz
||
915 gc_pwrite64(pgc
->pgc_fd
, pgc
->pgc_chunk
, csz
,
916 *pgc
->pgc_doff
+ n
) != 0) {
918 (void) gc_pwrite64(pgc
->pgc_fd
, &err
, sizeof (err
),
920 *pgc
->pgc_doff
+= roundup(sizeof (err
), 8);
922 phdr
.p_flags
|= PF_SUNW_FAILURE
;
923 (void) ftruncate64(pgc
->pgc_fd
, *pgc
->pgc_doff
);
930 phdr
.p_offset
= *pgc
->pgc_doff
;
931 phdr
.p_filesz
= pmp
->pr_size
;
932 *pgc
->pgc_doff
+= roundup(phdr
.p_filesz
, 8);
935 if (P
->status
.pr_dmodel
== PR_MODEL_NATIVE
) {
936 if (gc_pwrite64(pgc
->pgc_fd
, &phdr
, sizeof (phdr
),
937 *pgc
->pgc_poff
) != 0)
940 *pgc
->pgc_poff
+= sizeof (phdr
);
945 bzero(&phdr32
, sizeof (phdr32
));
946 phdr32
.p_type
= phdr
.p_type
;
947 phdr32
.p_vaddr
= (Elf32_Addr
)phdr
.p_vaddr
;
948 phdr32
.p_memsz
= (Elf32_Word
)phdr
.p_memsz
;
949 phdr32
.p_flags
= phdr
.p_flags
;
950 phdr32
.p_offset
= (Elf32_Off
)phdr
.p_offset
;
951 phdr32
.p_filesz
= (Elf32_Word
)phdr
.p_filesz
;
953 if (gc_pwrite64(pgc
->pgc_fd
, &phdr32
, sizeof (phdr32
),
954 *pgc
->pgc_poff
) != 0)
957 *pgc
->pgc_poff
+= sizeof (phdr32
);
965 write_shstrtab(struct ps_prochandle
*P
, pgcore_t
*pgc
)
967 off64_t off
= *pgc
->pgc_doff
;
969 shstrtab_t
*s
= &pgc
->pgc_shstrtab
;
972 if (shstrtab_size(s
) == 1)
976 * Preemptively stick the name of the shstrtab in the string table.
978 (void) shstrtab_ndx(&pgc
->pgc_shstrtab
, STR_SHSTRTAB
);
979 size
= shstrtab_size(s
);
982 * Dump all the strings that we used being sure we include the
983 * terminating null character.
985 for (i
= 0; i
< STR_NUM
; i
++) {
986 if ((ndx
= s
->sst_ndx
[i
]) != 0 || i
== STR_NONE
) {
987 const char *str
= shstrtab_data
[i
];
988 size_t len
= strlen(str
) + 1;
989 if (gc_pwrite64(pgc
->pgc_fd
, str
, len
, off
+ ndx
) != 0)
994 if (P
->status
.pr_dmodel
== PR_MODEL_ILP32
) {
997 bzero(&shdr
, sizeof (shdr
));
998 shdr
.sh_name
= shstrtab_ndx(&pgc
->pgc_shstrtab
, STR_SHSTRTAB
);
1000 shdr
.sh_offset
= *pgc
->pgc_doff
;
1001 shdr
.sh_addralign
= 1;
1002 shdr
.sh_flags
= SHF_STRINGS
;
1003 shdr
.sh_type
= SHT_STRTAB
;
1005 if (gc_pwrite64(pgc
->pgc_fd
, &shdr
, sizeof (shdr
),
1006 *pgc
->pgc_soff
) != 0)
1009 *pgc
->pgc_soff
+= sizeof (shdr
);
1014 bzero(&shdr
, sizeof (shdr
));
1015 shdr
.sh_name
= shstrtab_ndx(&pgc
->pgc_shstrtab
, STR_SHSTRTAB
);
1016 shdr
.sh_size
= size
;
1017 shdr
.sh_offset
= *pgc
->pgc_doff
;
1018 shdr
.sh_addralign
= 1;
1019 shdr
.sh_flags
= SHF_STRINGS
;
1020 shdr
.sh_type
= SHT_STRTAB
;
1022 if (gc_pwrite64(pgc
->pgc_fd
, &shdr
, sizeof (shdr
),
1023 *pgc
->pgc_soff
) != 0)
1026 *pgc
->pgc_soff
+= sizeof (shdr
);
1030 *pgc
->pgc_doff
+= roundup(size
, 8);
1036 * Don't explicity stop the process; that's up to the consumer.
1039 Pfgcore(struct ps_prochandle
*P
, int fd
, core_content_t content
)
1041 char plat
[SYS_NMLN
];
1042 char zonename
[ZONENAME_MAX
];
1045 off64_t poff
, soff
, doff
, boff
;
1047 uint_t nphdrs
, nshdrs
;
1049 if (ftruncate64(fd
, 0) != 0)
1052 if (content
== CC_CONTENT_INVALID
) {
1058 * Cache the mappings and other useful data.
1060 (void) Prd_agent(P
);
1065 pgc
.pgc_poff
= &poff
;
1066 pgc
.pgc_soff
= &soff
;
1067 pgc
.pgc_doff
= &doff
;
1068 pgc
.pgc_content
= content
;
1069 pgc
.pgc_chunksz
= PAGESIZE
;
1070 if ((pgc
.pgc_chunk
= malloc(pgc
.pgc_chunksz
)) == NULL
)
1073 shstrtab_init(&pgc
.pgc_shstrtab
);
1076 * There are two PT_NOTE program headers for ancillary data, and
1077 * one for each mapping.
1079 nphdrs
= 2 + P
->map_count
;
1080 nshdrs
= count_sections(&pgc
);
1082 (void) Pplatform(P
, plat
, sizeof (plat
));
1083 platlen
= strlen(plat
) + 1;
1085 (void) Puname(P
, &uts
);
1086 if (Pzonename(P
, zonename
, sizeof (zonename
)) == NULL
)
1090 * The core file contents may required zero section headers, but if we
1091 * overflow the 16 bits allotted to the program header count in the ELF
1092 * header, we'll need that program header at index zero.
1094 if (nshdrs
== 0 && nphdrs
>= PN_XNUM
)
1098 * Set up the ELF header.
1100 if (P
->status
.pr_dmodel
== PR_MODEL_ILP32
) {
1103 bzero(&ehdr
, sizeof (ehdr
));
1104 ehdr
.e_ident
[EI_MAG0
] = ELFMAG0
;
1105 ehdr
.e_ident
[EI_MAG1
] = ELFMAG1
;
1106 ehdr
.e_ident
[EI_MAG2
] = ELFMAG2
;
1107 ehdr
.e_ident
[EI_MAG3
] = ELFMAG3
;
1108 ehdr
.e_type
= ET_CORE
;
1110 ehdr
.e_ident
[EI_CLASS
] = ELFCLASS32
;
1111 #if defined(__sparc)
1112 ehdr
.e_machine
= EM_SPARC
;
1113 ehdr
.e_ident
[EI_DATA
] = ELFDATA2MSB
;
1114 #elif defined(__i386) || defined(__amd64)
1115 ehdr
.e_machine
= EM_386
;
1116 ehdr
.e_ident
[EI_DATA
] = ELFDATA2LSB
;
1118 #error "unknown machine type"
1120 ehdr
.e_ident
[EI_VERSION
] = EV_CURRENT
;
1122 ehdr
.e_version
= EV_CURRENT
;
1123 ehdr
.e_ehsize
= sizeof (ehdr
);
1125 if (nphdrs
>= PN_XNUM
)
1126 ehdr
.e_phnum
= PN_XNUM
;
1128 ehdr
.e_phnum
= (unsigned short)nphdrs
;
1130 ehdr
.e_phentsize
= sizeof (Elf32_Phdr
);
1131 ehdr
.e_phoff
= ehdr
.e_ehsize
;
1134 if (nshdrs
>= SHN_LORESERVE
)
1137 ehdr
.e_shnum
= (unsigned short)nshdrs
;
1139 if (nshdrs
- 1 >= SHN_LORESERVE
)
1140 ehdr
.e_shstrndx
= SHN_XINDEX
;
1142 ehdr
.e_shstrndx
= (unsigned short)(nshdrs
- 1);
1144 ehdr
.e_shentsize
= sizeof (Elf32_Shdr
);
1145 ehdr
.e_shoff
= ehdr
.e_phoff
+ ehdr
.e_phentsize
* nphdrs
;
1148 if (gc_pwrite64(fd
, &ehdr
, sizeof (ehdr
), 0) != 0)
1151 poff
= ehdr
.e_phoff
;
1152 soff
= ehdr
.e_shoff
;
1153 doff
= boff
= ehdr
.e_ehsize
+
1154 ehdr
.e_phentsize
* nphdrs
+
1155 ehdr
.e_shentsize
* nshdrs
;
1161 bzero(&ehdr
, sizeof (ehdr
));
1162 ehdr
.e_ident
[EI_MAG0
] = ELFMAG0
;
1163 ehdr
.e_ident
[EI_MAG1
] = ELFMAG1
;
1164 ehdr
.e_ident
[EI_MAG2
] = ELFMAG2
;
1165 ehdr
.e_ident
[EI_MAG3
] = ELFMAG3
;
1166 ehdr
.e_type
= ET_CORE
;
1168 ehdr
.e_ident
[EI_CLASS
] = ELFCLASS64
;
1169 #if defined(__sparc)
1170 ehdr
.e_machine
= EM_SPARCV9
;
1171 ehdr
.e_ident
[EI_DATA
] = ELFDATA2MSB
;
1172 #elif defined(__i386) || defined(__amd64)
1173 ehdr
.e_machine
= EM_AMD64
;
1174 ehdr
.e_ident
[EI_DATA
] = ELFDATA2LSB
;
1176 #error "unknown machine type"
1178 ehdr
.e_ident
[EI_VERSION
] = EV_CURRENT
;
1180 ehdr
.e_version
= EV_CURRENT
;
1181 ehdr
.e_ehsize
= sizeof (ehdr
);
1183 if (nphdrs
>= PN_XNUM
)
1184 ehdr
.e_phnum
= PN_XNUM
;
1186 ehdr
.e_phnum
= (unsigned short)nphdrs
;
1188 ehdr
.e_phentsize
= sizeof (Elf64_Phdr
);
1189 ehdr
.e_phoff
= ehdr
.e_ehsize
;
1192 if (nshdrs
>= SHN_LORESERVE
)
1195 ehdr
.e_shnum
= (unsigned short)nshdrs
;
1197 if (nshdrs
- 1 >= SHN_LORESERVE
)
1198 ehdr
.e_shstrndx
= SHN_XINDEX
;
1200 ehdr
.e_shstrndx
= (unsigned short)(nshdrs
- 1);
1202 ehdr
.e_shentsize
= sizeof (Elf64_Shdr
);
1203 ehdr
.e_shoff
= ehdr
.e_phoff
+ ehdr
.e_phentsize
* nphdrs
;
1206 if (gc_pwrite64(fd
, &ehdr
, sizeof (ehdr
), 0) != 0)
1209 poff
= ehdr
.e_phoff
;
1210 soff
= ehdr
.e_shoff
;
1211 doff
= boff
= ehdr
.e_ehsize
+
1212 ehdr
.e_phentsize
* nphdrs
+
1213 ehdr
.e_shentsize
* nshdrs
;
1219 * Write the zero indexed section if it exists.
1221 if (nshdrs
> 0 && write_shdr(&pgc
, STR_NONE
, 0, 0, 0, 0,
1222 nshdrs
>= SHN_LORESERVE
? nshdrs
: 0,
1223 nshdrs
- 1 >= SHN_LORESERVE
? nshdrs
- 1 : 0,
1224 nphdrs
>= PN_XNUM
? nphdrs
: 0, 0, 0) != 0)
1228 * Construct the old-style note header and section.
1231 if (P
->status
.pr_dmodel
== PR_MODEL_NATIVE
) {
1232 prpsinfo_t prpsinfo
;
1234 mkprpsinfo(P
, &prpsinfo
);
1235 if (write_note(fd
, NT_PRPSINFO
, &prpsinfo
, sizeof (prpsinfo_t
),
1239 if (write_note(fd
, NT_AUXV
, P
->auxv
,
1240 P
->nauxv
* sizeof (P
->auxv
[0]), &doff
) != 0) {
1247 size_t size
= sizeof (auxv32_t
) * P
->nauxv
;
1250 mkprpsinfo32(P
, &pi32
);
1251 if (write_note(fd
, NT_PRPSINFO
, &pi32
, sizeof (prpsinfo32_t
),
1256 if ((av32
= malloc(size
)) == NULL
)
1259 for (i
= 0; i
< P
->nauxv
; i
++) {
1260 auxv_n_to_32(&P
->auxv
[i
], &av32
[i
]);
1263 if (write_note(fd
, NT_AUXV
, av32
, size
, &doff
) != 0) {
1272 if (write_note(fd
, NT_PLATFORM
, plat
, platlen
, &doff
) != 0)
1275 if (Plwp_iter_all(P
, old_per_lwp
, &pgc
) != 0)
1278 if (P
->status
.pr_dmodel
== PR_MODEL_ILP32
) {
1281 bzero(&phdr
, sizeof (phdr
));
1282 phdr
.p_type
= PT_NOTE
;
1283 phdr
.p_flags
= PF_R
;
1284 phdr
.p_offset
= (Elf32_Off
)boff
;
1285 phdr
.p_filesz
= doff
- boff
;
1288 if (gc_pwrite64(fd
, &phdr
, sizeof (phdr
), poff
) != 0)
1290 poff
+= sizeof (phdr
);
1295 bzero(&phdr
, sizeof (phdr
));
1296 phdr
.p_type
= PT_NOTE
;
1297 phdr
.p_flags
= PF_R
;
1298 phdr
.p_offset
= boff
;
1299 phdr
.p_filesz
= doff
- boff
;
1302 if (gc_pwrite64(fd
, &phdr
, sizeof (phdr
), poff
) != 0)
1304 poff
+= sizeof (phdr
);
1309 * Construct the new-style note header and section.
1312 if (P
->status
.pr_dmodel
== PR_MODEL_NATIVE
) {
1313 if (write_note(fd
, NT_PSINFO
, &P
->psinfo
, sizeof (psinfo_t
),
1317 if (write_note(fd
, NT_PSTATUS
, &P
->status
, sizeof (pstatus_t
),
1321 if (write_note(fd
, NT_AUXV
, P
->auxv
,
1322 P
->nauxv
* sizeof (P
->auxv
[0]), &doff
) != 0) {
1330 size_t size
= sizeof (auxv32_t
) * P
->nauxv
;
1333 psinfo_n_to_32(&P
->psinfo
, &pi32
);
1334 if (write_note(fd
, NT_PSINFO
, &pi32
, sizeof (psinfo32_t
),
1338 pstatus_n_to_32(&P
->status
, &ps32
);
1339 if (write_note(fd
, NT_PSTATUS
, &ps32
, sizeof (pstatus32_t
),
1343 if ((av32
= malloc(size
)) == NULL
)
1346 for (i
= 0; i
< P
->nauxv
; i
++) {
1347 auxv_n_to_32(&P
->auxv
[i
], &av32
[i
]);
1350 if (write_note(fd
, NT_AUXV
, av32
, size
, &doff
) != 0) {
1359 if (write_note(fd
, NT_PLATFORM
, plat
, platlen
, &doff
) != 0 ||
1360 write_note(fd
, NT_UTSNAME
, &uts
, sizeof (uts
), &doff
) != 0 ||
1361 write_note(fd
, NT_CONTENT
, &content
, sizeof (content
), &doff
) != 0)
1366 size_t size
= sizeof (prcred_t
);
1368 if (Pcred(P
, &cred
, 0) != 0)
1371 if (cred
.pr_ngroups
> 0)
1372 size
+= sizeof (gid_t
) * (cred
.pr_ngroups
- 1);
1373 if ((cp
= malloc(size
)) == NULL
)
1376 if (Pcred(P
, cp
, cred
.pr_ngroups
) != 0 ||
1377 write_note(fd
, NT_PRCRED
, cp
, size
, &doff
) != 0) {
1386 prpriv_t
*ppriv
= NULL
;
1387 const priv_impl_info_t
*pinfo
;
1388 size_t pprivsz
, pinfosz
;
1390 if (Ppriv(P
, &ppriv
) == -1)
1392 pprivsz
= PRIV_PRPRIV_SIZE(ppriv
);
1394 if (write_note(fd
, NT_PRPRIV
, ppriv
, pprivsz
, &doff
) != 0) {
1395 Ppriv_free(P
, ppriv
);
1398 Ppriv_free(P
, ppriv
);
1400 if ((pinfo
= getprivimplinfo()) == NULL
)
1402 pinfosz
= PRIV_IMPL_INFO_SIZE(pinfo
);
1404 if (write_note(fd
, NT_PRPRIVINFO
, pinfo
, pinfosz
, &doff
) != 0)
1408 if (write_note(fd
, NT_ZONENAME
, zonename
, strlen(zonename
) + 1,
1415 iter
.fd_doff
= &doff
;
1417 if (Pfdinfo_iter(P
, iter_fd
, &iter
) != 0)
1423 prsecflags_t
*psf
= NULL
;
1425 if (Psecflags(P
, &psf
) != 0)
1428 if (write_note(fd
, NT_SECFLAGS
, psf
,
1429 sizeof (prsecflags_t
), &doff
) != 0) {
1430 Psecflags_free(psf
);
1434 Psecflags_free(psf
);
1437 #if defined(__i386) || defined(__amd64)
1445 * Only dump out non-zero sized LDT notes.
1447 if ((nldt
= Pldt(P
, NULL
, 0)) != 0) {
1448 size
= sizeof (struct ssd
) * nldt
;
1449 if ((ldtp
= malloc(size
)) == NULL
)
1452 if (Pldt(P
, ldtp
, nldt
) == -1 ||
1453 write_note(fd
, NT_LDT
, ldtp
, size
, &doff
) != 0) {
1461 #endif /* __i386 || __amd64 */
1463 if (Plwp_iter_all(P
, new_per_lwp
, &pgc
) != 0)
1466 if (P
->status
.pr_dmodel
== PR_MODEL_ILP32
) {
1469 bzero(&phdr
, sizeof (phdr
));
1470 phdr
.p_type
= PT_NOTE
;
1471 phdr
.p_flags
= PF_R
;
1472 phdr
.p_offset
= (Elf32_Off
)boff
;
1473 phdr
.p_filesz
= doff
- boff
;
1476 if (gc_pwrite64(fd
, &phdr
, sizeof (phdr
), poff
) != 0)
1478 poff
+= sizeof (phdr
);
1483 bzero(&phdr
, sizeof (phdr
));
1484 phdr
.p_type
= PT_NOTE
;
1485 phdr
.p_flags
= PF_R
;
1486 phdr
.p_offset
= boff
;
1487 phdr
.p_filesz
= doff
- boff
;
1490 if (gc_pwrite64(fd
, &phdr
, sizeof (phdr
), poff
) != 0)
1492 poff
+= sizeof (phdr
);
1497 * Construct the headers for each mapping and write out its data
1498 * if the content parameter indicates that it should be present
1501 if (Pmapping_iter(P
, dump_map
, &pgc
) != 0)
1504 if (dump_sections(&pgc
) != 0)
1507 if (write_shstrtab(P
, &pgc
) != 0)
1510 free(pgc
.pgc_chunk
);
1516 * Wipe out anything we may have written if there was an error.
1518 (void) ftruncate64(fd
, 0);
1519 free(pgc
.pgc_chunk
);
1524 static const char *content_str
[] = {
1525 "stack", /* CC_CONTENT_STACK */
1526 "heap", /* CC_CONTENT_HEAP */
1527 "shfile", /* CC_CONTENT_SHFILE */
1528 "shanon", /* CC_CONTENT_SHANON */
1529 "text", /* CC_CONTENT_TEXT */
1530 "data", /* CC_CONTENT_DATA */
1531 "rodata", /* CC_CONTENT_RODATA */
1532 "anon", /* CC_CONTENT_ANON */
1533 "shm", /* CC_CONTENT_SHM */
1534 "ism", /* CC_CONTENT_ISM */
1535 "dism", /* CC_CONTENT_DISM */
1536 "ctf", /* CC_CONTENT_CTF */
1537 "symtab", /* CC_CONTENT_SYMTAB */
1540 static uint_t ncontent_str
= sizeof (content_str
) / sizeof (content_str
[0]);
1542 #define STREQ(a, b, n) (strlen(b) == (n) && strncmp(a, b, n) == 0)
1545 proc_str2content(const char *str
, core_content_t
*cp
)
1547 const char *cur
= str
;
1549 core_content_t mask
, content
= 0;
1552 for (cur
= str
; isalpha(*cur
); cur
++)
1555 if (STREQ(str
, "default", cur
- str
)) {
1556 mask
= CC_CONTENT_DEFAULT
;
1557 } else if (STREQ(str
, "all", cur
- str
)) {
1558 mask
= CC_CONTENT_ALL
;
1559 } else if (STREQ(str
, "none", cur
- str
)) {
1564 while (!STREQ(str
, content_str
[i
], cur
- str
)) {
1567 if (i
>= ncontent_str
)
1571 mask
= (core_content_t
)1 << i
;
1598 popc(core_content_t x
)
1602 for (i
= 0; x
!= 0; i
++)
1609 proc_content2str(core_content_t content
, char *buf
, size_t size
)
1611 int nonecnt
, defcnt
, allcnt
;
1612 core_content_t mask
, bit
;
1618 return ((int)strlcpy(buf
, "none", size
));
1620 if (content
& ~CC_CONTENT_ALL
)
1621 return ((int)strlcpy(buf
, "<invalid>", size
));
1623 nonecnt
= popc(content
);
1624 defcnt
= 1 + popc(content
^ CC_CONTENT_DEFAULT
);
1625 allcnt
= 1 + popc(content
^ CC_CONTENT_ALL
);
1627 if (defcnt
<= nonecnt
&& defcnt
<= allcnt
) {
1628 mask
= content
^ CC_CONTENT_DEFAULT
;
1630 tot
+= (n
= strlcpy(buf
, "default", size
));
1635 } else if (allcnt
< nonecnt
) {
1636 mask
= content
^ CC_CONTENT_ALL
;
1638 tot
+= (n
= strlcpy(buf
, "all", size
));
1649 bit
= mask
^ (mask
& (mask
- 1));
1653 *buf
= (bit
& content
) ? '+' : '-';
1660 index
= popc(bit
- 1);
1661 tot
+= (n
= strlcpy(buf
, content_str
[index
], size
));