xref: /illumos-gate/usr/src/lib/libproc/common/Pgcore.c (revision b3403853e80914bd0aade9b5b605da4878078173)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
28  * Copyright 2018 Joyent, Inc.
29  * Copyright (c) 2013 by Delphix. All rights reserved.
30  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
31  * Copyright 2021 Oxide Computer Company
32  */
33 
34 #define	_STRUCTURED_PROC	1
35 
36 #include <stdlib.h>
37 #include <ctype.h>
38 #include <string.h>
39 #include <strings.h>
40 #include <errno.h>
41 #include <procfs.h>
42 #include <priv.h>
43 #include <sys/elf.h>
44 #include <sys/machelf.h>
45 #include <sys/sysmacros.h>
46 #include <sys/systeminfo.h>
47 #include <sys/proc.h>
48 #include <sys/utsname.h>
49 
50 #include <sys/old_procfs.h>
51 
52 #include "Pcontrol.h"
53 #include "P32ton.h"
54 #include "proc_fd.h"
55 
56 typedef enum {
57 	STR_NONE,
58 	STR_CTF,
59 	STR_SYMTAB,
60 	STR_DYNSYM,
61 	STR_STRTAB,
62 	STR_DYNSTR,
63 	STR_SHSTRTAB,
64 	STR_NUM
65 } shstrtype_t;
66 
67 static const char *shstrtab_data[] = {
68 	"",
69 	".SUNW_ctf",
70 	".symtab",
71 	".dynsym",
72 	".strtab",
73 	".dynstr",
74 	".shstrtab"
75 };
76 
77 typedef struct shstrtab {
78 	int	sst_ndx[STR_NUM];
79 	int	sst_cur;
80 } shstrtab_t;
81 
82 typedef struct {
83 	struct ps_prochandle *P;
84 	int		pgc_fd;
85 	off64_t		*pgc_poff;
86 	off64_t		*pgc_soff;
87 	off64_t		*pgc_doff;
88 	core_content_t	pgc_content;
89 	void		*pgc_chunk;
90 	size_t		pgc_chunksz;
91 
92 	shstrtab_t	pgc_shstrtab;
93 } pgcore_t;
94 
95 typedef struct {
96 	int		fd_fd;
97 	off64_t		*fd_doff;
98 } fditer_t;
99 
100 static int
101 gc_pwrite64(int fd, const void *buf, size_t len, off64_t off)
102 {
103 	int err;
104 
105 	err = pwrite64(fd, buf, len, off);
106 
107 	if (err < 0)
108 		return (err);
109 
110 	/*
111 	 * We will take a page from ZFS's book here and use the otherwise
112 	 * unused EBADE to mean a short write.  Typically this will actually
113 	 * result from ENOSPC or EDQUOT, but we can't be sure.
114 	 */
115 	if (err < len) {
116 		errno = EBADE;
117 		return (-1);
118 	}
119 
120 	return (0);
121 }
122 
123 static void
124 shstrtab_init(shstrtab_t *s)
125 {
126 	bzero(&s->sst_ndx, sizeof (s->sst_ndx));
127 	s->sst_cur = 1;
128 }
129 
130 static int
131 shstrtab_ndx(shstrtab_t *s, shstrtype_t type)
132 {
133 	int ret;
134 
135 	if ((ret = s->sst_ndx[type]) != 0 || type == STR_NONE)
136 		return (ret);
137 
138 	ret = s->sst_ndx[type] = s->sst_cur;
139 	s->sst_cur += strlen(shstrtab_data[type]) + 1;
140 
141 	return (ret);
142 }
143 
144 static size_t
145 shstrtab_size(const shstrtab_t *s)
146 {
147 	return (s->sst_cur);
148 }
149 
150 int
151 Pgcore(struct ps_prochandle *P, const char *fname, core_content_t content)
152 {
153 	int fd;
154 	int err;
155 	int saved_errno;
156 
157 	if ((fd = creat64(fname, 0666)) < 0)
158 		return (-1);
159 
160 	if ((err = Pfgcore(P, fd, content)) != 0) {
161 		saved_errno = errno;
162 		(void) close(fd);
163 		(void) unlink(fname);
164 		errno = saved_errno;
165 		return (err);
166 	}
167 
168 	return (close(fd));
169 }
170 
171 /*
172  * Since we don't want to use the old-school procfs interfaces, we use the
173  * new-style data structures we already have to construct the old-style
174  * data structures. We include these data structures in core files for
175  * backward compatability.
176  */
177 
178 static void
179 mkprstatus(struct ps_prochandle *P, const lwpstatus_t *lsp,
180     const lwpsinfo_t *lip, prstatus_t *psp)
181 {
182 	bzero(psp, sizeof (*psp));
183 
184 	if (lsp->pr_flags & PR_STOPPED)
185 		psp->pr_flags = 0x0001;
186 	if (lsp->pr_flags & PR_ISTOP)
187 		psp->pr_flags = 0x0002;
188 	if (lsp->pr_flags & PR_DSTOP)
189 		psp->pr_flags = 0x0004;
190 	if (lsp->pr_flags & PR_ASLEEP)
191 		psp->pr_flags = 0x0008;
192 	if (lsp->pr_flags & PR_FORK)
193 		psp->pr_flags = 0x0010;
194 	if (lsp->pr_flags & PR_RLC)
195 		psp->pr_flags = 0x0020;
196 	/*
197 	 * Note that PR_PTRACE (0x0040) from <sys/old_procfs.h> is never set;
198 	 * PR_PCOMPAT corresponds to PR_PTRACE in the newer <sys/procfs.h>.
199 	 */
200 	if (lsp->pr_flags & PR_PCINVAL)
201 		psp->pr_flags = 0x0080;
202 	if (lsp->pr_flags & PR_ISSYS)
203 		psp->pr_flags = 0x0100;
204 	if (lsp->pr_flags & PR_STEP)
205 		psp->pr_flags = 0x0200;
206 	if (lsp->pr_flags & PR_KLC)
207 		psp->pr_flags = 0x0400;
208 	if (lsp->pr_flags & PR_ASYNC)
209 		psp->pr_flags = 0x0800;
210 	if (lsp->pr_flags & PR_PTRACE)
211 		psp->pr_flags = 0x1000;
212 	if (lsp->pr_flags & PR_MSACCT)
213 		psp->pr_flags = 0x2000;
214 	if (lsp->pr_flags & PR_BPTADJ)
215 		psp->pr_flags = 0x4000;
216 	if (lsp->pr_flags & PR_ASLWP)
217 		psp->pr_flags = 0x8000;
218 
219 	psp->pr_why = lsp->pr_why;
220 	psp->pr_what = lsp->pr_what;
221 	psp->pr_info = lsp->pr_info;
222 	psp->pr_cursig = lsp->pr_cursig;
223 	psp->pr_nlwp = P->status.pr_nlwp;
224 	psp->pr_sigpend = P->status.pr_sigpend;
225 	psp->pr_sighold = lsp->pr_lwphold;
226 	psp->pr_altstack = lsp->pr_altstack;
227 	psp->pr_action = lsp->pr_action;
228 	psp->pr_pid = P->status.pr_pid;
229 	psp->pr_ppid = P->status.pr_ppid;
230 	psp->pr_pgrp = P->status.pr_pgid;
231 	psp->pr_sid = P->status.pr_sid;
232 	psp->pr_utime = P->status.pr_utime;
233 	psp->pr_stime = P->status.pr_stime;
234 	psp->pr_cutime = P->status.pr_cutime;
235 	psp->pr_cstime = P->status.pr_cstime;
236 	(void) strncpy(psp->pr_clname, lsp->pr_clname, sizeof (psp->pr_clname));
237 	psp->pr_syscall = lsp->pr_syscall;
238 	psp->pr_nsysarg = lsp->pr_nsysarg;
239 	bcopy(lsp->pr_sysarg, psp->pr_sysarg, sizeof (psp->pr_sysarg));
240 	psp->pr_who = lsp->pr_lwpid;
241 	psp->pr_lwppend = lsp->pr_lwppend;
242 	psp->pr_oldcontext = (ucontext_t *)lsp->pr_oldcontext;
243 	psp->pr_brkbase = (caddr_t)P->status.pr_brkbase;
244 	psp->pr_brksize = P->status.pr_brksize;
245 	psp->pr_stkbase = (caddr_t)P->status.pr_stkbase;
246 	psp->pr_stksize = P->status.pr_stksize;
247 	psp->pr_processor = (short)lip->pr_onpro;
248 	psp->pr_bind = (short)lip->pr_bindpro;
249 	psp->pr_instr = lsp->pr_instr;
250 	bcopy(lsp->pr_reg, psp->pr_reg, sizeof (psp->pr_sysarg));
251 }
252 
253 static void
254 mkprpsinfo(struct ps_prochandle *P, prpsinfo_t *psp)
255 {
256 	bzero(psp, sizeof (*psp));
257 	psp->pr_state = P->psinfo.pr_lwp.pr_state;
258 	psp->pr_sname = P->psinfo.pr_lwp.pr_sname;
259 	psp->pr_zomb = (psp->pr_state == SZOMB);
260 	psp->pr_nice = P->psinfo.pr_lwp.pr_nice;
261 	psp->pr_flag = P->psinfo.pr_lwp.pr_flag;
262 	psp->pr_uid = P->psinfo.pr_uid;
263 	psp->pr_gid = P->psinfo.pr_gid;
264 	psp->pr_pid = P->psinfo.pr_pid;
265 	psp->pr_ppid = P->psinfo.pr_ppid;
266 	psp->pr_pgrp = P->psinfo.pr_pgid;
267 	psp->pr_sid = P->psinfo.pr_sid;
268 	psp->pr_addr = (caddr_t)P->psinfo.pr_addr;
269 	psp->pr_size = P->psinfo.pr_size;
270 	psp->pr_rssize = P->psinfo.pr_rssize;
271 	psp->pr_wchan = (caddr_t)P->psinfo.pr_lwp.pr_wchan;
272 	psp->pr_start = P->psinfo.pr_start;
273 	psp->pr_time = P->psinfo.pr_time;
274 	psp->pr_pri = P->psinfo.pr_lwp.pr_pri;
275 	psp->pr_oldpri = P->psinfo.pr_lwp.pr_oldpri;
276 	psp->pr_cpu = P->psinfo.pr_lwp.pr_cpu;
277 	psp->pr_ottydev = cmpdev(P->psinfo.pr_ttydev);
278 	psp->pr_lttydev = P->psinfo.pr_ttydev;
279 	(void) strncpy(psp->pr_clname, P->psinfo.pr_lwp.pr_clname,
280 	    sizeof (psp->pr_clname));
281 	(void) strncpy(psp->pr_fname, P->psinfo.pr_fname,
282 	    sizeof (psp->pr_fname));
283 	bcopy(&P->psinfo.pr_psargs, &psp->pr_psargs,
284 	    sizeof (psp->pr_psargs));
285 	psp->pr_syscall = P->psinfo.pr_lwp.pr_syscall;
286 	psp->pr_ctime = P->psinfo.pr_ctime;
287 	psp->pr_bysize = psp->pr_size * PAGESIZE;
288 	psp->pr_byrssize = psp->pr_rssize * PAGESIZE;
289 	psp->pr_argc = P->psinfo.pr_argc;
290 	psp->pr_argv = (char **)P->psinfo.pr_argv;
291 	psp->pr_envp = (char **)P->psinfo.pr_envp;
292 	psp->pr_wstat = P->psinfo.pr_wstat;
293 	psp->pr_pctcpu = P->psinfo.pr_pctcpu;
294 	psp->pr_pctmem = P->psinfo.pr_pctmem;
295 	psp->pr_euid = P->psinfo.pr_euid;
296 	psp->pr_egid = P->psinfo.pr_egid;
297 	psp->pr_aslwpid = 0;
298 	psp->pr_dmodel = P->psinfo.pr_dmodel;
299 }
300 
301 #ifdef _LP64
302 
303 static void
304 mkprstatus32(struct ps_prochandle *P, const lwpstatus_t *lsp,
305     const lwpsinfo_t *lip, prstatus32_t *psp)
306 {
307 	bzero(psp, sizeof (*psp));
308 
309 	if (lsp->pr_flags & PR_STOPPED)
310 		psp->pr_flags = 0x0001;
311 	if (lsp->pr_flags & PR_ISTOP)
312 		psp->pr_flags = 0x0002;
313 	if (lsp->pr_flags & PR_DSTOP)
314 		psp->pr_flags = 0x0004;
315 	if (lsp->pr_flags & PR_ASLEEP)
316 		psp->pr_flags = 0x0008;
317 	if (lsp->pr_flags & PR_FORK)
318 		psp->pr_flags = 0x0010;
319 	if (lsp->pr_flags & PR_RLC)
320 		psp->pr_flags = 0x0020;
321 	/*
322 	 * Note that PR_PTRACE (0x0040) from <sys/old_procfs.h> is never set;
323 	 * PR_PCOMPAT corresponds to PR_PTRACE in the newer <sys/procfs.h>.
324 	 */
325 	if (lsp->pr_flags & PR_PCINVAL)
326 		psp->pr_flags = 0x0080;
327 	if (lsp->pr_flags & PR_ISSYS)
328 		psp->pr_flags = 0x0100;
329 	if (lsp->pr_flags & PR_STEP)
330 		psp->pr_flags = 0x0200;
331 	if (lsp->pr_flags & PR_KLC)
332 		psp->pr_flags = 0x0400;
333 	if (lsp->pr_flags & PR_ASYNC)
334 		psp->pr_flags = 0x0800;
335 	if (lsp->pr_flags & PR_PTRACE)
336 		psp->pr_flags = 0x1000;
337 	if (lsp->pr_flags & PR_MSACCT)
338 		psp->pr_flags = 0x2000;
339 	if (lsp->pr_flags & PR_BPTADJ)
340 		psp->pr_flags = 0x4000;
341 	if (lsp->pr_flags & PR_ASLWP)
342 		psp->pr_flags = 0x8000;
343 
344 	psp->pr_why = lsp->pr_why;
345 	psp->pr_what = lsp->pr_what;
346 	siginfo_n_to_32(&lsp->pr_info, &psp->pr_info);
347 	psp->pr_cursig = lsp->pr_cursig;
348 	psp->pr_nlwp = P->status.pr_nlwp;
349 	psp->pr_sigpend = P->status.pr_sigpend;
350 	psp->pr_sighold = lsp->pr_lwphold;
351 	stack_n_to_32(&lsp->pr_altstack, &psp->pr_altstack);
352 	sigaction_n_to_32(&lsp->pr_action, &psp->pr_action);
353 	psp->pr_pid = P->status.pr_pid;
354 	psp->pr_ppid = P->status.pr_ppid;
355 	psp->pr_pgrp = P->status.pr_pgid;
356 	psp->pr_sid = P->status.pr_sid;
357 	timestruc_n_to_32(&P->status.pr_utime, &psp->pr_utime);
358 	timestruc_n_to_32(&P->status.pr_stime, &psp->pr_stime);
359 	timestruc_n_to_32(&P->status.pr_cutime, &psp->pr_cutime);
360 	timestruc_n_to_32(&P->status.pr_cstime, &psp->pr_cstime);
361 	(void) strncpy(psp->pr_clname, lsp->pr_clname, sizeof (psp->pr_clname));
362 	psp->pr_syscall = lsp->pr_syscall;
363 	psp->pr_nsysarg = lsp->pr_nsysarg;
364 	bcopy(lsp->pr_sysarg, psp->pr_sysarg, sizeof (psp->pr_sysarg));
365 	psp->pr_who = lsp->pr_lwpid;
366 	psp->pr_lwppend = lsp->pr_lwppend;
367 	psp->pr_oldcontext = (caddr32_t)lsp->pr_oldcontext;
368 	psp->pr_brkbase = (caddr32_t)P->status.pr_brkbase;
369 	psp->pr_brksize = P->status.pr_brksize;
370 	psp->pr_stkbase = (caddr32_t)P->status.pr_stkbase;
371 	psp->pr_stksize = P->status.pr_stksize;
372 	psp->pr_processor = (short)lip->pr_onpro;
373 	psp->pr_bind = (short)lip->pr_bindpro;
374 	psp->pr_instr = lsp->pr_instr;
375 	bcopy(lsp->pr_reg, psp->pr_reg, sizeof (psp->pr_sysarg));
376 }
377 
378 static void
379 mkprpsinfo32(struct ps_prochandle *P, prpsinfo32_t *psp)
380 {
381 	bzero(psp, sizeof (*psp));
382 	psp->pr_state = P->psinfo.pr_lwp.pr_state;
383 	psp->pr_sname = P->psinfo.pr_lwp.pr_sname;
384 	psp->pr_zomb = (psp->pr_state == SZOMB);
385 	psp->pr_nice = P->psinfo.pr_lwp.pr_nice;
386 	psp->pr_flag = P->psinfo.pr_lwp.pr_flag;
387 	psp->pr_uid = P->psinfo.pr_uid;
388 	psp->pr_gid = P->psinfo.pr_gid;
389 	psp->pr_pid = P->psinfo.pr_pid;
390 	psp->pr_ppid = P->psinfo.pr_ppid;
391 	psp->pr_pgrp = P->psinfo.pr_pgid;
392 	psp->pr_sid = P->psinfo.pr_sid;
393 	psp->pr_addr = (caddr32_t)P->psinfo.pr_addr;
394 	psp->pr_size = P->psinfo.pr_size;
395 	psp->pr_rssize = P->psinfo.pr_rssize;
396 	psp->pr_wchan = (caddr32_t)P->psinfo.pr_lwp.pr_wchan;
397 	timestruc_n_to_32(&P->psinfo.pr_start, &psp->pr_start);
398 	timestruc_n_to_32(&P->psinfo.pr_time, &psp->pr_time);
399 	psp->pr_pri = P->psinfo.pr_lwp.pr_pri;
400 	psp->pr_oldpri = P->psinfo.pr_lwp.pr_oldpri;
401 	psp->pr_cpu = P->psinfo.pr_lwp.pr_cpu;
402 	psp->pr_ottydev = cmpdev(P->psinfo.pr_ttydev);
403 	psp->pr_lttydev = prcmpldev(P->psinfo.pr_ttydev);
404 	(void) strncpy(psp->pr_clname, P->psinfo.pr_lwp.pr_clname,
405 	    sizeof (psp->pr_clname));
406 	(void) strncpy(psp->pr_fname, P->psinfo.pr_fname,
407 	    sizeof (psp->pr_fname));
408 	bcopy(&P->psinfo.pr_psargs, &psp->pr_psargs,
409 	    sizeof (psp->pr_psargs));
410 	psp->pr_syscall = P->psinfo.pr_lwp.pr_syscall;
411 	timestruc_n_to_32(&P->psinfo.pr_ctime, &psp->pr_ctime);
412 	psp->pr_bysize = psp->pr_size * PAGESIZE;
413 	psp->pr_byrssize = psp->pr_rssize * PAGESIZE;
414 	psp->pr_argc = P->psinfo.pr_argc;
415 	psp->pr_argv = (caddr32_t)P->psinfo.pr_argv;
416 	psp->pr_envp = (caddr32_t)P->psinfo.pr_envp;
417 	psp->pr_wstat = P->psinfo.pr_wstat;
418 	psp->pr_pctcpu = P->psinfo.pr_pctcpu;
419 	psp->pr_pctmem = P->psinfo.pr_pctmem;
420 	psp->pr_euid = P->psinfo.pr_euid;
421 	psp->pr_egid = P->psinfo.pr_egid;
422 	psp->pr_aslwpid = 0;
423 	psp->pr_dmodel = P->psinfo.pr_dmodel;
424 }
425 
426 #endif	/* _LP64 */
427 
428 static int
429 write_note(int fd, uint_t type, const void *desc, size_t descsz, off64_t *offp)
430 {
431 	/*
432 	 * Note headers are the same regardless of the data model of the
433 	 * ELF file; we arbitrarily use Elf64_Nhdr here.
434 	 */
435 	struct {
436 		Elf64_Nhdr nhdr;
437 		char name[8];
438 	} n;
439 
440 	bzero(&n, sizeof (n));
441 	bcopy("CORE", n.name, 4);
442 	n.nhdr.n_type = type;
443 	n.nhdr.n_namesz = 5;
444 	n.nhdr.n_descsz = roundup(descsz, 4);
445 
446 	if (gc_pwrite64(fd, &n, sizeof (n), *offp) != 0)
447 		return (-1);
448 
449 	*offp += sizeof (n);
450 
451 	if (gc_pwrite64(fd, desc, n.nhdr.n_descsz, *offp) != 0)
452 		return (-1);
453 
454 	*offp += n.nhdr.n_descsz;
455 
456 	return (0);
457 }
458 
459 static int
460 old_per_lwp(void *data, const lwpstatus_t *lsp, const lwpsinfo_t *lip)
461 {
462 	pgcore_t *pgc = data;
463 	struct ps_prochandle *P = pgc->P;
464 
465 	/*
466 	 * Legacy core files don't contain information about zombie LWPs.
467 	 * We use Plwp_iter_all() so that we get the lwpsinfo_t structure
468 	 * more cheaply.
469 	 */
470 	if (lsp == NULL)
471 		return (0);
472 
473 	if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
474 		prstatus_t prstatus;
475 		mkprstatus(P, lsp, lip, &prstatus);
476 		if (write_note(pgc->pgc_fd, NT_PRSTATUS, &prstatus,
477 		    sizeof (prstatus_t), pgc->pgc_doff) != 0)
478 			return (0);
479 		if (write_note(pgc->pgc_fd, NT_PRFPREG, &lsp->pr_fpreg,
480 		    sizeof (prfpregset_t), pgc->pgc_doff) != 0)
481 			return (1);
482 #ifdef _LP64
483 	} else {
484 		prstatus32_t pr32;
485 		prfpregset32_t pf32;
486 		mkprstatus32(P, lsp, lip, &pr32);
487 		if (write_note(pgc->pgc_fd, NT_PRSTATUS, &pr32,
488 		    sizeof (prstatus32_t), pgc->pgc_doff) != 0)
489 			return (1);
490 		prfpregset_n_to_32(&lsp->pr_fpreg, &pf32);
491 		if (write_note(pgc->pgc_fd, NT_PRFPREG, &pf32,
492 		    sizeof (prfpregset32_t), pgc->pgc_doff) != 0)
493 			return (1);
494 #endif	/* _LP64 */
495 	}
496 
497 #ifdef sparc
498 	{
499 		prxregset_t xregs;
500 		if (Plwp_getxregs(P, lsp->pr_lwpid, &xregs) == 0 &&
501 		    write_note(pgc->pgc_fd, NT_PRXREG, &xregs,
502 		    sizeof (prxregset_t), pgc->pgc_doff) != 0)
503 			return (1);
504 	}
505 #endif	/* sparc */
506 
507 	return (0);
508 }
509 
510 static int
511 new_per_lwp(void *data, const lwpstatus_t *lsp, const lwpsinfo_t *lip)
512 {
513 	pgcore_t *pgc = data;
514 	struct ps_prochandle *P = pgc->P;
515 	prlwpname_t name = { 0, "" };
516 	psinfo_t ps;
517 
518 	/*
519 	 * If lsp is NULL this indicates that this is a zombie LWP in
520 	 * which case we dump only the lwpsinfo_t structure and none of
521 	 * the other ancillary LWP state data.
522 	 */
523 	if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
524 		if (write_note(pgc->pgc_fd, NT_LWPSINFO, lip,
525 		    sizeof (lwpsinfo_t), pgc->pgc_doff) != 0)
526 			return (1);
527 		if (lsp == NULL)
528 			return (0);
529 		if (write_note(pgc->pgc_fd, NT_LWPSTATUS, lsp,
530 		    sizeof (lwpstatus_t), pgc->pgc_doff) != 0)
531 			return (1);
532 #ifdef _LP64
533 	} else {
534 		lwpsinfo32_t li32;
535 		lwpstatus32_t ls32;
536 		lwpsinfo_n_to_32(lip, &li32);
537 		if (write_note(pgc->pgc_fd, NT_LWPSINFO, &li32,
538 		    sizeof (lwpsinfo32_t), pgc->pgc_doff) != 0)
539 			return (1);
540 		if (lsp == NULL)
541 			return (0);
542 		lwpstatus_n_to_32(lsp, &ls32);
543 		if (write_note(pgc->pgc_fd, NT_LWPSTATUS, &ls32,
544 		    sizeof (lwpstatus32_t), pgc->pgc_doff) != 0)
545 			return (1);
546 #endif	/* _LP64 */
547 	}
548 
549 #ifdef sparc
550 	{
551 		prxregset_t xregs;
552 		gwindows_t gwins;
553 		size_t size;
554 
555 		if (Plwp_getxregs(P, lsp->pr_lwpid, &xregs) == 0) {
556 			if (write_note(pgc->pgc_fd, NT_PRXREG, &xregs,
557 			    sizeof (prxregset_t), pgc->pgc_doff) != 0)
558 				return (1);
559 		}
560 
561 		if (Plwp_getgwindows(P, lsp->pr_lwpid, &gwins) == 0 &&
562 		    gwins.wbcnt > 0) {
563 			size = sizeof (gwins) - sizeof (gwins.wbuf) +
564 			    gwins.wbcnt * sizeof (gwins.wbuf[0]);
565 
566 			if (write_note(pgc->pgc_fd, NT_GWINDOWS, &gwins, size,
567 			    pgc->pgc_doff) != 0)
568 				return (1);
569 		}
570 
571 	}
572 #ifdef __sparcv9
573 	if (P->status.pr_dmodel == PR_MODEL_LP64) {
574 		asrset_t asrs;
575 		if (Plwp_getasrs(P, lsp->pr_lwpid, asrs) == 0) {
576 			if (write_note(pgc->pgc_fd, NT_ASRS, &asrs,
577 			    sizeof (asrset_t), pgc->pgc_doff) != 0)
578 				return (1);
579 		}
580 	}
581 #endif	/* __sparcv9 */
582 #endif	/* sparc */
583 
584 	if (Plwp_getname(P, lsp->pr_lwpid, name.pr_lwpname,
585 	    sizeof (name.pr_lwpname)) == 0) {
586 		name.pr_lwpid = lsp->pr_lwpid;
587 		if (write_note(pgc->pgc_fd, NT_LWPNAME, &name,
588 		    sizeof (name), pgc->pgc_doff) != 0)
589 			return (1);
590 	}
591 
592 	if (!(lsp->pr_flags & PR_AGENT))
593 		return (0);
594 
595 	if (Plwp_getspymaster(P, lsp->pr_lwpid, &ps) != 0)
596 		return (0);
597 
598 	if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
599 		if (write_note(pgc->pgc_fd, NT_SPYMASTER, &ps,
600 		    sizeof (psinfo_t), pgc->pgc_doff) != 0)
601 			return (1);
602 #ifdef _LP64
603 	} else {
604 		psinfo32_t ps32;
605 		psinfo_n_to_32(&ps, &ps32);
606 		if (write_note(pgc->pgc_fd, NT_SPYMASTER, &ps32,
607 		    sizeof (psinfo32_t), pgc->pgc_doff) != 0)
608 			return (1);
609 #endif	/* _LP64 */
610 	}
611 
612 
613 	return (0);
614 }
615 
616 static int
617 iter_fd(void *data, const prfdinfo_t *fdinfo)
618 {
619 	fditer_t *iter = data;
620 	prfdinfo_core_t core;
621 	int ret = 0;
622 
623 	if (proc_fdinfo_to_core(fdinfo, &core) != 0)
624 		return (1);
625 
626 	ret = write_note(iter->fd_fd, NT_FDINFO, &core,
627 	    sizeof (core), iter->fd_doff);
628 
629 	if (ret != 0)
630 		return (1);
631 	return (0);
632 }
633 
634 static uint_t
635 count_sections(pgcore_t *pgc)
636 {
637 	struct ps_prochandle *P = pgc->P;
638 	file_info_t *fptr;
639 	uint_t nshdrs = 0;
640 
641 	if (!(pgc->pgc_content & (CC_CONTENT_CTF | CC_CONTENT_SYMTAB)))
642 		return (0);
643 
644 	for (fptr = list_head(&P->file_head); fptr != NULL;
645 	    fptr = list_next(&P->file_head, fptr)) {
646 		int hit_symtab = 0;
647 
648 		Pbuild_file_symtab(P, fptr);
649 
650 		if ((pgc->pgc_content & CC_CONTENT_CTF) &&
651 		    Pbuild_file_ctf(P, fptr) != NULL) {
652 			sym_tbl_t *sym;
653 
654 			nshdrs++;
655 
656 			if (fptr->file_ctf_dyn) {
657 				sym = &fptr->file_dynsym;
658 			} else {
659 				sym = &fptr->file_symtab;
660 				hit_symtab = 1;
661 			}
662 
663 			if (sym->sym_data_pri != NULL && sym->sym_symn != 0 &&
664 			    sym->sym_strs != NULL)
665 				nshdrs += 2;
666 		}
667 
668 		if ((pgc->pgc_content & CC_CONTENT_SYMTAB) && !hit_symtab &&
669 		    fptr->file_symtab.sym_data_pri != NULL &&
670 		    fptr->file_symtab.sym_symn != 0 &&
671 		    fptr->file_symtab.sym_strs != NULL) {
672 			nshdrs += 2;
673 		}
674 	}
675 
676 	return (nshdrs == 0 ? 0 : nshdrs + 2);
677 }
678 
679 static int
680 write_shdr(pgcore_t *pgc, shstrtype_t name, uint_t type, ulong_t flags,
681     uintptr_t addr, ulong_t offset, size_t size, uint_t link, uint_t info,
682     uintptr_t addralign, uintptr_t entsize)
683 {
684 	if (pgc->P->status.pr_dmodel == PR_MODEL_ILP32) {
685 		Elf32_Shdr shdr;
686 
687 		bzero(&shdr, sizeof (shdr));
688 		shdr.sh_name = shstrtab_ndx(&pgc->pgc_shstrtab, name);
689 		shdr.sh_type = type;
690 		shdr.sh_flags = flags;
691 		shdr.sh_addr = (Elf32_Addr)addr;
692 		shdr.sh_offset = offset;
693 		shdr.sh_size = size;
694 		shdr.sh_link = link;
695 		shdr.sh_info = info;
696 		shdr.sh_addralign = addralign;
697 		shdr.sh_entsize = entsize;
698 
699 		if (gc_pwrite64(pgc->pgc_fd, &shdr, sizeof (shdr),
700 		    *pgc->pgc_soff) != 0)
701 			return (-1);
702 
703 		*pgc->pgc_soff += sizeof (shdr);
704 #ifdef _LP64
705 	} else {
706 		Elf64_Shdr shdr;
707 
708 		bzero(&shdr, sizeof (shdr));
709 		shdr.sh_name = shstrtab_ndx(&pgc->pgc_shstrtab, name);
710 		shdr.sh_type = type;
711 		shdr.sh_flags = flags;
712 		shdr.sh_addr = addr;
713 		shdr.sh_offset = offset;
714 		shdr.sh_size = size;
715 		shdr.sh_link = link;
716 		shdr.sh_info = info;
717 		shdr.sh_addralign = addralign;
718 		shdr.sh_entsize = entsize;
719 
720 		if (gc_pwrite64(pgc->pgc_fd, &shdr, sizeof (shdr),
721 		    *pgc->pgc_soff) != 0)
722 			return (-1);
723 
724 		*pgc->pgc_soff += sizeof (shdr);
725 #endif	/* _LP64 */
726 	}
727 
728 	return (0);
729 }
730 
731 static int
732 dump_symtab(pgcore_t *pgc, file_info_t *fptr, uint_t index, int dynsym)
733 {
734 	sym_tbl_t *sym = dynsym ? &fptr->file_dynsym : &fptr->file_symtab;
735 	shstrtype_t symname = dynsym ? STR_DYNSYM : STR_SYMTAB;
736 	shstrtype_t strname = dynsym ? STR_DYNSTR : STR_STRTAB;
737 	uint_t symtype = dynsym ? SHT_DYNSYM : SHT_SYMTAB;
738 	size_t size;
739 	uintptr_t addr = fptr->file_map->map_pmap.pr_vaddr;
740 
741 	if (sym->sym_data_pri == NULL || sym->sym_symn == 0 ||
742 	    sym->sym_strs == NULL)
743 		return (0);
744 
745 	size = sym->sym_hdr_pri.sh_size;
746 	if (gc_pwrite64(pgc->pgc_fd, sym->sym_data_pri->d_buf, size,
747 	    *pgc->pgc_doff) != 0)
748 		return (-1);
749 
750 	if (write_shdr(pgc, symname, symtype, 0, addr, *pgc->pgc_doff, size,
751 	    index + 1, sym->sym_hdr_pri.sh_info, sym->sym_hdr_pri.sh_addralign,
752 	    sym->sym_hdr_pri.sh_entsize) != 0)
753 		return (-1);
754 
755 	*pgc->pgc_doff += roundup(size, 8);
756 
757 	size = sym->sym_strhdr.sh_size;
758 	if (gc_pwrite64(pgc->pgc_fd, sym->sym_strs, size, *pgc->pgc_doff) != 0)
759 		return (-1);
760 
761 	if (write_shdr(pgc, strname, SHT_STRTAB, SHF_STRINGS, addr,
762 	    *pgc->pgc_doff, size, 0, 0, 1, 0) != 0)
763 		return (-1);
764 
765 	*pgc->pgc_doff += roundup(size, 8);
766 
767 	return (0);
768 }
769 
770 static int
771 dump_sections(pgcore_t *pgc)
772 {
773 	struct ps_prochandle *P = pgc->P;
774 	file_info_t *fptr;
775 	uint_t index = 1;
776 
777 	if (!(pgc->pgc_content & (CC_CONTENT_CTF | CC_CONTENT_SYMTAB)))
778 		return (0);
779 
780 	for (fptr = list_head(&P->file_head); fptr != NULL;
781 	    fptr = list_next(&P->file_head, fptr)) {
782 		int hit_symtab = 0;
783 
784 		Pbuild_file_symtab(P, fptr);
785 
786 		if ((pgc->pgc_content & CC_CONTENT_CTF) &&
787 		    Pbuild_file_ctf(P, fptr) != NULL) {
788 			sym_tbl_t *sym;
789 			uint_t dynsym;
790 			uint_t symindex = 0;
791 
792 			/*
793 			 * Write the symtab out first so we can correctly
794 			 * set the sh_link field in the CTF section header.
795 			 * symindex will be 0 if there is no corresponding
796 			 * symbol table section.
797 			 */
798 			if (fptr->file_ctf_dyn) {
799 				sym = &fptr->file_dynsym;
800 				dynsym = 1;
801 			} else {
802 				sym = &fptr->file_symtab;
803 				dynsym = 0;
804 				hit_symtab = 1;
805 			}
806 
807 			if (sym->sym_data_pri != NULL && sym->sym_symn != 0 &&
808 			    sym->sym_strs != NULL) {
809 				symindex = index;
810 				if (dump_symtab(pgc, fptr, index, dynsym) != 0)
811 					return (-1);
812 				index += 2;
813 			}
814 
815 			/*
816 			 * Write the CTF data that we've read out of the
817 			 * file itself into the core file.
818 			 */
819 			if (gc_pwrite64(pgc->pgc_fd, fptr->file_ctf_buf,
820 			    fptr->file_ctf_size, *pgc->pgc_doff) != 0)
821 				return (-1);
822 
823 			if (write_shdr(pgc, STR_CTF, SHT_PROGBITS, 0,
824 			    fptr->file_map->map_pmap.pr_vaddr, *pgc->pgc_doff,
825 			    fptr->file_ctf_size, symindex, 0, 4, 0) != 0)
826 				return (-1);
827 
828 			index++;
829 			*pgc->pgc_doff += roundup(fptr->file_ctf_size, 8);
830 		}
831 
832 		if ((pgc->pgc_content & CC_CONTENT_SYMTAB) && !hit_symtab &&
833 		    fptr->file_symtab.sym_data_pri != NULL &&
834 		    fptr->file_symtab.sym_symn != 0 &&
835 		    fptr->file_symtab.sym_strs != NULL) {
836 			if (dump_symtab(pgc, fptr, index, 0) != 0)
837 				return (-1);
838 			index += 2;
839 		}
840 	}
841 
842 	return (0);
843 }
844 
845 /*ARGSUSED*/
846 static int
847 dump_map(void *data, const prmap_t *pmp, const char *name)
848 {
849 	pgcore_t *pgc = data;
850 	struct ps_prochandle *P = pgc->P;
851 #ifdef _LP64
852 	Elf64_Phdr phdr;
853 #else
854 	Elf32_Phdr phdr;
855 #endif
856 	size_t n;
857 
858 	bzero(&phdr, sizeof (phdr));
859 	phdr.p_type = PT_LOAD;
860 	phdr.p_vaddr = pmp->pr_vaddr;
861 	phdr.p_memsz = pmp->pr_size;
862 	if (pmp->pr_mflags & MA_READ)
863 		phdr.p_flags |= PF_R;
864 	if (pmp->pr_mflags & MA_WRITE)
865 		phdr.p_flags |= PF_W;
866 	if (pmp->pr_mflags & MA_EXEC)
867 		phdr.p_flags |= PF_X;
868 
869 	if (pmp->pr_vaddr + pmp->pr_size > P->status.pr_stkbase &&
870 	    pmp->pr_vaddr < P->status.pr_stkbase + P->status.pr_stksize) {
871 		if (!(pgc->pgc_content & CC_CONTENT_STACK))
872 			goto exclude;
873 
874 	} else if ((pmp->pr_mflags & MA_ANON) &&
875 	    pmp->pr_vaddr + pmp->pr_size > P->status.pr_brkbase &&
876 	    pmp->pr_vaddr < P->status.pr_brkbase + P->status.pr_brksize) {
877 		if (!(pgc->pgc_content & CC_CONTENT_HEAP))
878 			goto exclude;
879 
880 	} else if (pmp->pr_mflags & MA_ISM) {
881 		if (pmp->pr_mflags & MA_NORESERVE) {
882 			if (!(pgc->pgc_content & CC_CONTENT_DISM))
883 				goto exclude;
884 		} else {
885 			if (!(pgc->pgc_content & CC_CONTENT_ISM))
886 				goto exclude;
887 		}
888 
889 	} else if (pmp->pr_mflags & MA_SHM) {
890 		if (!(pgc->pgc_content & CC_CONTENT_SHM))
891 			goto exclude;
892 
893 	} else if (pmp->pr_mflags & MA_SHARED) {
894 		if (pmp->pr_mflags & MA_ANON) {
895 			if (!(pgc->pgc_content & CC_CONTENT_SHANON))
896 				goto exclude;
897 		} else {
898 			if (!(pgc->pgc_content & CC_CONTENT_SHFILE))
899 				goto exclude;
900 		}
901 
902 	} else if (pmp->pr_mflags & MA_ANON) {
903 		if (!(pgc->pgc_content & CC_CONTENT_ANON))
904 			goto exclude;
905 
906 	} else if (phdr.p_flags == (PF_R | PF_X)) {
907 		if (!(pgc->pgc_content & CC_CONTENT_TEXT))
908 			goto exclude;
909 
910 	} else if (phdr.p_flags == PF_R) {
911 		if (!(pgc->pgc_content & CC_CONTENT_RODATA))
912 			goto exclude;
913 
914 	} else {
915 		if (!(pgc->pgc_content & CC_CONTENT_DATA))
916 			goto exclude;
917 	}
918 
919 	n = 0;
920 	while (n < pmp->pr_size) {
921 		size_t csz = MIN(pmp->pr_size - n, pgc->pgc_chunksz);
922 		ssize_t ret;
923 
924 		/*
925 		 * If we happen to have a PROT_NONE mapping, don't try to read
926 		 * from the address space.
927 		 */
928 		if ((pmp->pr_mflags & (MA_READ | MA_WRITE | MA_EXEC)) == 0) {
929 			bzero(pgc->pgc_chunk, csz);
930 			ret = csz;
931 		} else {
932 			ret = Pread(P, pgc->pgc_chunk, csz, pmp->pr_vaddr + n);
933 		}
934 
935 		/*
936 		 * If we can't read out part of the victim's address
937 		 * space for some reason ignore that failure and try to
938 		 * emit a partial core file without that mapping's data.
939 		 * As in the kernel, we mark these failures with the
940 		 * PF_SUNW_FAILURE flag and store the errno where the
941 		 * mapping would have been.
942 		 */
943 		if (ret != csz || gc_pwrite64(pgc->pgc_fd, pgc->pgc_chunk, csz,
944 		    *pgc->pgc_doff + n) != 0) {
945 			int err = errno;
946 			(void) gc_pwrite64(pgc->pgc_fd, &err, sizeof (err),
947 			    *pgc->pgc_doff);
948 			*pgc->pgc_doff += roundup(sizeof (err), 8);
949 
950 			phdr.p_flags |= PF_SUNW_FAILURE;
951 			(void) ftruncate64(pgc->pgc_fd, *pgc->pgc_doff);
952 			goto exclude;
953 		}
954 
955 		n += csz;
956 	}
957 
958 	phdr.p_offset = *pgc->pgc_doff;
959 	phdr.p_filesz = pmp->pr_size;
960 	*pgc->pgc_doff += roundup(phdr.p_filesz, 8);
961 
962 exclude:
963 	if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
964 		if (gc_pwrite64(pgc->pgc_fd, &phdr, sizeof (phdr),
965 		    *pgc->pgc_poff) != 0)
966 			return (1);
967 
968 		*pgc->pgc_poff += sizeof (phdr);
969 #ifdef _LP64
970 	} else {
971 		Elf32_Phdr phdr32;
972 
973 		bzero(&phdr32, sizeof (phdr32));
974 		phdr32.p_type = phdr.p_type;
975 		phdr32.p_vaddr = (Elf32_Addr)phdr.p_vaddr;
976 		phdr32.p_memsz = (Elf32_Word)phdr.p_memsz;
977 		phdr32.p_flags = phdr.p_flags;
978 		phdr32.p_offset = (Elf32_Off)phdr.p_offset;
979 		phdr32.p_filesz = (Elf32_Word)phdr.p_filesz;
980 
981 		if (gc_pwrite64(pgc->pgc_fd, &phdr32, sizeof (phdr32),
982 		    *pgc->pgc_poff) != 0)
983 			return (1);
984 
985 		*pgc->pgc_poff += sizeof (phdr32);
986 #endif	/* _LP64 */
987 	}
988 
989 	return (0);
990 }
991 
992 int
993 write_shstrtab(struct ps_prochandle *P, pgcore_t *pgc)
994 {
995 	off64_t off = *pgc->pgc_doff;
996 	size_t size = 0;
997 	shstrtab_t *s = &pgc->pgc_shstrtab;
998 	int i, ndx;
999 
1000 	if (shstrtab_size(s) == 1)
1001 		return (0);
1002 
1003 	/*
1004 	 * Preemptively stick the name of the shstrtab in the string table.
1005 	 */
1006 	(void) shstrtab_ndx(&pgc->pgc_shstrtab, STR_SHSTRTAB);
1007 	size = shstrtab_size(s);
1008 
1009 	/*
1010 	 * Dump all the strings that we used being sure we include the
1011 	 * terminating null character.
1012 	 */
1013 	for (i = 0; i < STR_NUM; i++) {
1014 		if ((ndx = s->sst_ndx[i]) != 0 || i == STR_NONE) {
1015 			const char *str = shstrtab_data[i];
1016 			size_t len = strlen(str) + 1;
1017 			if (gc_pwrite64(pgc->pgc_fd, str, len, off + ndx) != 0)
1018 				return (1);
1019 		}
1020 	}
1021 
1022 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
1023 		Elf32_Shdr shdr;
1024 
1025 		bzero(&shdr, sizeof (shdr));
1026 		shdr.sh_name = shstrtab_ndx(&pgc->pgc_shstrtab, STR_SHSTRTAB);
1027 		shdr.sh_size = size;
1028 		shdr.sh_offset = *pgc->pgc_doff;
1029 		shdr.sh_addralign = 1;
1030 		shdr.sh_flags = SHF_STRINGS;
1031 		shdr.sh_type = SHT_STRTAB;
1032 
1033 		if (gc_pwrite64(pgc->pgc_fd, &shdr, sizeof (shdr),
1034 		    *pgc->pgc_soff) != 0)
1035 			return (1);
1036 
1037 		*pgc->pgc_soff += sizeof (shdr);
1038 #ifdef _LP64
1039 	} else {
1040 		Elf64_Shdr shdr;
1041 
1042 		bzero(&shdr, sizeof (shdr));
1043 		shdr.sh_name = shstrtab_ndx(&pgc->pgc_shstrtab, STR_SHSTRTAB);
1044 		shdr.sh_size = size;
1045 		shdr.sh_offset = *pgc->pgc_doff;
1046 		shdr.sh_addralign = 1;
1047 		shdr.sh_flags = SHF_STRINGS;
1048 		shdr.sh_type = SHT_STRTAB;
1049 
1050 		if (gc_pwrite64(pgc->pgc_fd, &shdr, sizeof (shdr),
1051 		    *pgc->pgc_soff) != 0)
1052 			return (1);
1053 
1054 		*pgc->pgc_soff += sizeof (shdr);
1055 #endif	/* _LP64 */
1056 	}
1057 
1058 	*pgc->pgc_doff += roundup(size, 8);
1059 
1060 	return (0);
1061 }
1062 
1063 /*
1064  * Don't explicity stop the process; that's up to the consumer.
1065  */
1066 int
1067 Pfgcore(struct ps_prochandle *P, int fd, core_content_t content)
1068 {
1069 	char plat[SYS_NMLN];
1070 	char zonename[ZONENAME_MAX];
1071 	int platlen = -1;
1072 	pgcore_t pgc;
1073 	off64_t poff, soff, doff, boff;
1074 	struct utsname uts;
1075 	uint_t nphdrs, nshdrs;
1076 
1077 	if (ftruncate64(fd, 0) != 0)
1078 		return (-1);
1079 
1080 	if (content == CC_CONTENT_INVALID) {
1081 		errno = EINVAL;
1082 		return (-1);
1083 	}
1084 
1085 	/*
1086 	 * Cache the mappings and other useful data.
1087 	 */
1088 	(void) Prd_agent(P);
1089 	(void) Ppsinfo(P);
1090 
1091 	pgc.P = P;
1092 	pgc.pgc_fd = fd;
1093 	pgc.pgc_poff = &poff;
1094 	pgc.pgc_soff = &soff;
1095 	pgc.pgc_doff = &doff;
1096 	pgc.pgc_content = content;
1097 	pgc.pgc_chunksz = PAGESIZE;
1098 	if ((pgc.pgc_chunk = malloc(pgc.pgc_chunksz)) == NULL)
1099 		return (-1);
1100 
1101 	shstrtab_init(&pgc.pgc_shstrtab);
1102 
1103 	/*
1104 	 * There are two PT_NOTE program headers for ancillary data, and
1105 	 * one for each mapping.
1106 	 */
1107 	nphdrs = 2 + P->map_count;
1108 	nshdrs = count_sections(&pgc);
1109 
1110 	(void) Pplatform(P, plat, sizeof (plat));
1111 	platlen = strlen(plat) + 1;
1112 	Preadauxvec(P);
1113 	(void) Puname(P, &uts);
1114 	if (Pzonename(P, zonename, sizeof (zonename)) == NULL)
1115 		zonename[0] = '\0';
1116 
1117 	/*
1118 	 * The core file contents may required zero section headers, but if we
1119 	 * overflow the 16 bits allotted to the program header count in the ELF
1120 	 * header, we'll need that program header at index zero.
1121 	 */
1122 	if (nshdrs == 0 && nphdrs >= PN_XNUM)
1123 		nshdrs = 1;
1124 
1125 	/*
1126 	 * Set up the ELF header.
1127 	 */
1128 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
1129 		Elf32_Ehdr ehdr;
1130 
1131 		bzero(&ehdr, sizeof (ehdr));
1132 		ehdr.e_ident[EI_MAG0] = ELFMAG0;
1133 		ehdr.e_ident[EI_MAG1] = ELFMAG1;
1134 		ehdr.e_ident[EI_MAG2] = ELFMAG2;
1135 		ehdr.e_ident[EI_MAG3] = ELFMAG3;
1136 		ehdr.e_type = ET_CORE;
1137 
1138 		ehdr.e_ident[EI_CLASS] = ELFCLASS32;
1139 #if defined(__sparc)
1140 		ehdr.e_machine = EM_SPARC;
1141 		ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
1142 #elif defined(__i386) || defined(__amd64)
1143 		ehdr.e_machine = EM_386;
1144 		ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
1145 #else
1146 #error "unknown machine type"
1147 #endif
1148 		ehdr.e_ident[EI_VERSION] = EV_CURRENT;
1149 
1150 		ehdr.e_version = EV_CURRENT;
1151 		ehdr.e_ehsize = sizeof (ehdr);
1152 
1153 		if (nphdrs >= PN_XNUM)
1154 			ehdr.e_phnum = PN_XNUM;
1155 		else
1156 			ehdr.e_phnum = (unsigned short)nphdrs;
1157 
1158 		ehdr.e_phentsize = sizeof (Elf32_Phdr);
1159 		ehdr.e_phoff = ehdr.e_ehsize;
1160 
1161 		if (nshdrs > 0) {
1162 			if (nshdrs >= SHN_LORESERVE)
1163 				ehdr.e_shnum = 0;
1164 			else
1165 				ehdr.e_shnum = (unsigned short)nshdrs;
1166 
1167 			if (nshdrs - 1 >= SHN_LORESERVE)
1168 				ehdr.e_shstrndx = SHN_XINDEX;
1169 			else
1170 				ehdr.e_shstrndx = (unsigned short)(nshdrs - 1);
1171 
1172 			ehdr.e_shentsize = sizeof (Elf32_Shdr);
1173 			ehdr.e_shoff = ehdr.e_phoff + ehdr.e_phentsize * nphdrs;
1174 		}
1175 
1176 		if (gc_pwrite64(fd, &ehdr, sizeof (ehdr), 0) != 0)
1177 			goto err;
1178 
1179 		poff = ehdr.e_phoff;
1180 		soff = ehdr.e_shoff;
1181 		doff = boff = ehdr.e_ehsize +
1182 		    ehdr.e_phentsize * nphdrs +
1183 		    ehdr.e_shentsize * nshdrs;
1184 
1185 #ifdef _LP64
1186 	} else {
1187 		Elf64_Ehdr ehdr;
1188 
1189 		bzero(&ehdr, sizeof (ehdr));
1190 		ehdr.e_ident[EI_MAG0] = ELFMAG0;
1191 		ehdr.e_ident[EI_MAG1] = ELFMAG1;
1192 		ehdr.e_ident[EI_MAG2] = ELFMAG2;
1193 		ehdr.e_ident[EI_MAG3] = ELFMAG3;
1194 		ehdr.e_type = ET_CORE;
1195 
1196 		ehdr.e_ident[EI_CLASS] = ELFCLASS64;
1197 #if defined(__sparc)
1198 		ehdr.e_machine = EM_SPARCV9;
1199 		ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
1200 #elif defined(__i386) || defined(__amd64)
1201 		ehdr.e_machine = EM_AMD64;
1202 		ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
1203 #else
1204 #error "unknown machine type"
1205 #endif
1206 		ehdr.e_ident[EI_VERSION] = EV_CURRENT;
1207 
1208 		ehdr.e_version = EV_CURRENT;
1209 		ehdr.e_ehsize = sizeof (ehdr);
1210 
1211 		if (nphdrs >= PN_XNUM)
1212 			ehdr.e_phnum = PN_XNUM;
1213 		else
1214 			ehdr.e_phnum = (unsigned short)nphdrs;
1215 
1216 		ehdr.e_phentsize = sizeof (Elf64_Phdr);
1217 		ehdr.e_phoff = ehdr.e_ehsize;
1218 
1219 		if (nshdrs > 0) {
1220 			if (nshdrs >= SHN_LORESERVE)
1221 				ehdr.e_shnum = 0;
1222 			else
1223 				ehdr.e_shnum = (unsigned short)nshdrs;
1224 
1225 			if (nshdrs - 1 >= SHN_LORESERVE)
1226 				ehdr.e_shstrndx = SHN_XINDEX;
1227 			else
1228 				ehdr.e_shstrndx = (unsigned short)(nshdrs - 1);
1229 
1230 			ehdr.e_shentsize = sizeof (Elf64_Shdr);
1231 			ehdr.e_shoff = ehdr.e_phoff + ehdr.e_phentsize * nphdrs;
1232 		}
1233 
1234 		if (gc_pwrite64(fd, &ehdr, sizeof (ehdr), 0) != 0)
1235 			goto err;
1236 
1237 		poff = ehdr.e_phoff;
1238 		soff = ehdr.e_shoff;
1239 		doff = boff = ehdr.e_ehsize +
1240 		    ehdr.e_phentsize * nphdrs +
1241 		    ehdr.e_shentsize * nshdrs;
1242 
1243 #endif	/* _LP64 */
1244 	}
1245 
1246 	/*
1247 	 * Write the zero indexed section if it exists.
1248 	 */
1249 	if (nshdrs > 0 && write_shdr(&pgc, STR_NONE, 0, 0, 0, 0,
1250 	    nshdrs >= SHN_LORESERVE ? nshdrs : 0,
1251 	    nshdrs - 1 >= SHN_LORESERVE ? nshdrs - 1 : 0,
1252 	    nphdrs >= PN_XNUM ? nphdrs : 0, 0, 0) != 0)
1253 		goto err;
1254 
1255 	/*
1256 	 * Construct the old-style note header and section.
1257 	 */
1258 
1259 	if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
1260 		prpsinfo_t prpsinfo;
1261 
1262 		mkprpsinfo(P, &prpsinfo);
1263 		if (write_note(fd, NT_PRPSINFO, &prpsinfo, sizeof (prpsinfo_t),
1264 		    &doff) != 0) {
1265 			goto err;
1266 		}
1267 		if (write_note(fd, NT_AUXV, P->auxv,
1268 		    P->nauxv * sizeof (P->auxv[0]), &doff) != 0) {
1269 			goto err;
1270 		}
1271 #ifdef _LP64
1272 	} else {
1273 		prpsinfo32_t pi32;
1274 		auxv32_t *av32;
1275 		size_t size = sizeof (auxv32_t) * P->nauxv;
1276 		int i;
1277 
1278 		mkprpsinfo32(P, &pi32);
1279 		if (write_note(fd, NT_PRPSINFO, &pi32, sizeof (prpsinfo32_t),
1280 		    &doff) != 0) {
1281 			goto err;
1282 		}
1283 
1284 		if ((av32 = malloc(size)) == NULL)
1285 			goto err;
1286 
1287 		for (i = 0; i < P->nauxv; i++) {
1288 			auxv_n_to_32(&P->auxv[i], &av32[i]);
1289 		}
1290 
1291 		if (write_note(fd, NT_AUXV, av32, size, &doff) != 0) {
1292 			free(av32);
1293 			goto err;
1294 		}
1295 
1296 		free(av32);
1297 #endif	/* _LP64 */
1298 	}
1299 
1300 	if (write_note(fd, NT_PLATFORM, plat, platlen, &doff) != 0)
1301 		goto err;
1302 
1303 	if (Plwp_iter_all(P, old_per_lwp, &pgc) != 0)
1304 		goto err;
1305 
1306 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
1307 		Elf32_Phdr phdr;
1308 
1309 		bzero(&phdr, sizeof (phdr));
1310 		phdr.p_type = PT_NOTE;
1311 		phdr.p_flags = PF_R;
1312 		phdr.p_offset = (Elf32_Off)boff;
1313 		phdr.p_filesz = doff - boff;
1314 		boff = doff;
1315 
1316 		if (gc_pwrite64(fd, &phdr, sizeof (phdr), poff) != 0)
1317 			goto err;
1318 		poff += sizeof (phdr);
1319 #ifdef _LP64
1320 	} else {
1321 		Elf64_Phdr phdr;
1322 
1323 		bzero(&phdr, sizeof (phdr));
1324 		phdr.p_type = PT_NOTE;
1325 		phdr.p_flags = PF_R;
1326 		phdr.p_offset = boff;
1327 		phdr.p_filesz = doff - boff;
1328 		boff = doff;
1329 
1330 		if (gc_pwrite64(fd, &phdr, sizeof (phdr), poff) != 0)
1331 			goto err;
1332 		poff += sizeof (phdr);
1333 #endif	/* _LP64 */
1334 	}
1335 
1336 	/*
1337 	 * Construct the new-style note header and section.
1338 	 */
1339 
1340 	if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
1341 		if (write_note(fd, NT_PSINFO, &P->psinfo, sizeof (psinfo_t),
1342 		    &doff) != 0) {
1343 			goto err;
1344 		}
1345 		if (write_note(fd, NT_PSTATUS, &P->status, sizeof (pstatus_t),
1346 		    &doff) != 0) {
1347 			goto err;
1348 		}
1349 		if (write_note(fd, NT_AUXV, P->auxv,
1350 		    P->nauxv * sizeof (P->auxv[0]), &doff) != 0) {
1351 			goto err;
1352 		}
1353 #ifdef _LP64
1354 	} else {
1355 		psinfo32_t pi32;
1356 		pstatus32_t ps32;
1357 		auxv32_t *av32;
1358 		size_t size = sizeof (auxv32_t) * P->nauxv;
1359 		int i;
1360 
1361 		psinfo_n_to_32(&P->psinfo, &pi32);
1362 		if (write_note(fd, NT_PSINFO, &pi32, sizeof (psinfo32_t),
1363 		    &doff) != 0) {
1364 			goto err;
1365 		}
1366 		pstatus_n_to_32(&P->status, &ps32);
1367 		if (write_note(fd, NT_PSTATUS, &ps32, sizeof (pstatus32_t),
1368 		    &doff) != 0) {
1369 			goto err;
1370 		}
1371 		if ((av32 = malloc(size)) == NULL)
1372 			goto err;
1373 
1374 		for (i = 0; i < P->nauxv; i++) {
1375 			auxv_n_to_32(&P->auxv[i], &av32[i]);
1376 		}
1377 
1378 		if (write_note(fd, NT_AUXV, av32, size, &doff) != 0) {
1379 			free(av32);
1380 			goto err;
1381 		}
1382 
1383 		free(av32);
1384 #endif	/* _LP64 */
1385 	}
1386 
1387 	if (write_note(fd, NT_PLATFORM, plat, platlen, &doff) != 0 ||
1388 	    write_note(fd, NT_UTSNAME, &uts, sizeof (uts), &doff) != 0 ||
1389 	    write_note(fd, NT_CONTENT, &content, sizeof (content), &doff) != 0)
1390 		goto err;
1391 
1392 	{
1393 		prcred_t cred, *cp;
1394 		size_t size = sizeof (prcred_t);
1395 
1396 		if (Pcred(P, &cred, 0) != 0)
1397 			goto err;
1398 
1399 		if (cred.pr_ngroups > 0)
1400 			size += sizeof (gid_t) * (cred.pr_ngroups - 1);
1401 		if ((cp = malloc(size)) == NULL)
1402 			goto err;
1403 
1404 		if (Pcred(P, cp, cred.pr_ngroups) != 0 ||
1405 		    write_note(fd, NT_PRCRED, cp, size, &doff) != 0) {
1406 			free(cp);
1407 			goto err;
1408 		}
1409 
1410 		free(cp);
1411 	}
1412 
1413 	{
1414 		prpriv_t *ppriv = NULL;
1415 		const priv_impl_info_t *pinfo;
1416 		size_t pprivsz, pinfosz;
1417 
1418 		if (Ppriv(P, &ppriv) == -1)
1419 			goto err;
1420 		pprivsz = PRIV_PRPRIV_SIZE(ppriv);
1421 
1422 		if (write_note(fd, NT_PRPRIV, ppriv, pprivsz, &doff) != 0) {
1423 			Ppriv_free(P, ppriv);
1424 			goto err;
1425 		}
1426 		Ppriv_free(P, ppriv);
1427 
1428 		if ((pinfo = getprivimplinfo()) == NULL)
1429 			goto err;
1430 		pinfosz = PRIV_IMPL_INFO_SIZE(pinfo);
1431 
1432 		if (write_note(fd, NT_PRPRIVINFO, pinfo, pinfosz, &doff) != 0)
1433 			goto err;
1434 	}
1435 
1436 	if (write_note(fd, NT_ZONENAME, zonename, strlen(zonename) + 1,
1437 	    &doff) != 0)
1438 		goto err;
1439 
1440 	{
1441 		fditer_t iter;
1442 		iter.fd_fd = fd;
1443 		iter.fd_doff = &doff;
1444 
1445 		if (Pfdinfo_iter(P, iter_fd, &iter) != 0)
1446 			goto err;
1447 	}
1448 
1449 
1450 	{
1451 		prsecflags_t *psf = NULL;
1452 
1453 		if (Psecflags(P, &psf) != 0)
1454 			goto err;
1455 
1456 		if (write_note(fd, NT_SECFLAGS, psf,
1457 		    sizeof (prsecflags_t), &doff) != 0) {
1458 			Psecflags_free(psf);
1459 			goto err;
1460 		}
1461 
1462 		Psecflags_free(psf);
1463 	}
1464 
1465 #if defined(__i386) || defined(__amd64)
1466 	/* CSTYLED */
1467 	{
1468 		struct ssd *ldtp;
1469 		size_t size;
1470 		int nldt;
1471 
1472 		/*
1473 		 * Only dump out non-zero sized LDT notes.
1474 		 */
1475 		if ((nldt = Pldt(P, NULL, 0)) != 0) {
1476 			size = sizeof (struct ssd) * nldt;
1477 			if ((ldtp = malloc(size)) == NULL)
1478 				goto err;
1479 
1480 			if (Pldt(P, ldtp, nldt) == -1 ||
1481 			    write_note(fd, NT_LDT, ldtp, size, &doff) != 0) {
1482 				free(ldtp);
1483 				goto err;
1484 			}
1485 
1486 			free(ldtp);
1487 		}
1488 	}
1489 #endif	/* __i386 || __amd64 */
1490 
1491 	if (Plwp_iter_all(P, new_per_lwp, &pgc) != 0)
1492 		goto err;
1493 
1494 	if (P->status.pr_dmodel == PR_MODEL_ILP32) {
1495 		Elf32_Phdr phdr;
1496 
1497 		bzero(&phdr, sizeof (phdr));
1498 		phdr.p_type = PT_NOTE;
1499 		phdr.p_flags = PF_R;
1500 		phdr.p_offset = (Elf32_Off)boff;
1501 		phdr.p_filesz = doff - boff;
1502 		boff = doff;
1503 
1504 		if (gc_pwrite64(fd, &phdr, sizeof (phdr), poff) != 0)
1505 			goto err;
1506 		poff += sizeof (phdr);
1507 #ifdef _LP64
1508 	} else {
1509 		Elf64_Phdr phdr;
1510 
1511 		bzero(&phdr, sizeof (phdr));
1512 		phdr.p_type = PT_NOTE;
1513 		phdr.p_flags = PF_R;
1514 		phdr.p_offset = boff;
1515 		phdr.p_filesz = doff - boff;
1516 		boff = doff;
1517 
1518 		if (gc_pwrite64(fd, &phdr, sizeof (phdr), poff) != 0)
1519 			goto err;
1520 		poff += sizeof (phdr);
1521 #endif	/* _LP64 */
1522 	}
1523 
1524 	/*
1525 	 * Construct the headers for each mapping and write out its data
1526 	 * if the content parameter indicates that it should be present
1527 	 * in the core file.
1528 	 */
1529 	if (Pmapping_iter(P, dump_map, &pgc) != 0)
1530 		goto err;
1531 
1532 	if (dump_sections(&pgc) != 0)
1533 		goto err;
1534 
1535 	if (write_shstrtab(P, &pgc) != 0)
1536 		goto err;
1537 
1538 	free(pgc.pgc_chunk);
1539 
1540 	return (0);
1541 
1542 err:
1543 	/*
1544 	 * Wipe out anything we may have written if there was an error.
1545 	 */
1546 	(void) ftruncate64(fd, 0);
1547 	free(pgc.pgc_chunk);
1548 
1549 	return (-1);
1550 }
1551 
1552 static const char *content_str[] = {
1553 	"stack",	/* CC_CONTENT_STACK */
1554 	"heap",		/* CC_CONTENT_HEAP */
1555 	"shfile",	/* CC_CONTENT_SHFILE */
1556 	"shanon",	/* CC_CONTENT_SHANON */
1557 	"text",		/* CC_CONTENT_TEXT */
1558 	"data",		/* CC_CONTENT_DATA */
1559 	"rodata",	/* CC_CONTENT_RODATA */
1560 	"anon",		/* CC_CONTENT_ANON */
1561 	"shm",		/* CC_CONTENT_SHM */
1562 	"ism",		/* CC_CONTENT_ISM */
1563 	"dism",		/* CC_CONTENT_DISM */
1564 	"ctf",		/* CC_CONTENT_CTF */
1565 	"symtab",	/* CC_CONTENT_SYMTAB */
1566 };
1567 
1568 static uint_t ncontent_str = sizeof (content_str) / sizeof (content_str[0]);
1569 
1570 #define	STREQ(a, b, n)	(strlen(b) == (n) && strncmp(a, b, n) == 0)
1571 
1572 int
1573 proc_str2content(const char *str, core_content_t *cp)
1574 {
1575 	const char *cur = str;
1576 	int add = 1;
1577 	core_content_t mask, content = 0;
1578 
1579 	for (;;) {
1580 		for (cur = str; isalpha(*cur); cur++)
1581 			continue;
1582 
1583 		if (STREQ(str, "default", cur - str)) {
1584 			mask = CC_CONTENT_DEFAULT;
1585 		} else if (STREQ(str, "all", cur - str)) {
1586 			mask = CC_CONTENT_ALL;
1587 		} else if (STREQ(str, "none", cur - str)) {
1588 			mask = 0;
1589 		} else {
1590 			int i = 0;
1591 
1592 			while (!STREQ(str, content_str[i], cur - str)) {
1593 				i++;
1594 
1595 				if (i >= ncontent_str)
1596 					return (-1);
1597 			}
1598 
1599 			mask = (core_content_t)1 << i;
1600 		}
1601 
1602 		if (add)
1603 			content |= mask;
1604 		else
1605 			content &= ~mask;
1606 
1607 		switch (*cur) {
1608 		case '\0':
1609 			*cp = content;
1610 			return (0);
1611 		case '+':
1612 			add = 1;
1613 			break;
1614 		case '-':
1615 			add = 0;
1616 			break;
1617 		default:
1618 			return (-1);
1619 		}
1620 
1621 		str = cur + 1;
1622 	}
1623 }
1624 
1625 static int
1626 popc(core_content_t x)
1627 {
1628 	int i;
1629 
1630 	for (i = 0; x != 0; i++)
1631 		x &= x - 1;
1632 
1633 	return (i);
1634 }
1635 
1636 int
1637 proc_content2str(core_content_t content, char *buf, size_t size)
1638 {
1639 	int nonecnt, defcnt, allcnt;
1640 	core_content_t mask, bit;
1641 	int first;
1642 	uint_t index;
1643 	size_t n, tot = 0;
1644 
1645 	if (content == 0)
1646 		return ((int)strlcpy(buf, "none", size));
1647 
1648 	if (content & ~CC_CONTENT_ALL)
1649 		return ((int)strlcpy(buf, "<invalid>", size));
1650 
1651 	nonecnt = popc(content);
1652 	defcnt = 1 + popc(content ^ CC_CONTENT_DEFAULT);
1653 	allcnt = 1 + popc(content ^ CC_CONTENT_ALL);
1654 
1655 	if (defcnt <= nonecnt && defcnt <= allcnt) {
1656 		mask = content ^ CC_CONTENT_DEFAULT;
1657 		first = 0;
1658 		tot += (n = strlcpy(buf, "default", size));
1659 		if (n > size)
1660 			n = size;
1661 		buf += n;
1662 		size -= n;
1663 	} else if (allcnt < nonecnt) {
1664 		mask = content ^ CC_CONTENT_ALL;
1665 		first = 0;
1666 		tot += (n = strlcpy(buf, "all", size));
1667 		if (n > size)
1668 			n = size;
1669 		buf += n;
1670 		size -= n;
1671 	} else {
1672 		mask = content;
1673 		first = 1;
1674 	}
1675 
1676 	while (mask != 0) {
1677 		bit = mask ^ (mask & (mask - 1));
1678 
1679 		if (!first) {
1680 			if (size > 1) {
1681 				*buf = (bit & content) ? '+' : '-';
1682 				buf++;
1683 				size--;
1684 			}
1685 
1686 			tot++;
1687 		}
1688 		index = popc(bit - 1);
1689 		tot += (n = strlcpy(buf, content_str[index], size));
1690 		if (n > size)
1691 			n = size;
1692 		buf += n;
1693 		size -= n;
1694 
1695 		mask ^= bit;
1696 		first = 0;
1697 	}
1698 
1699 	return ((int)tot);
1700 }
1701