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