]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.bin/gcore/elfcore.c
MFC 303002: Include process IDs in core dumps.
[FreeBSD/stable/10.git] / usr.bin / gcore / elfcore.c
1 /*-
2  * Copyright (c) 2007 Sandvine Incorporated
3  * Copyright (c) 1998 John D. Polstra
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/endian.h>
32 #include <sys/param.h>
33 #include <sys/procfs.h>
34 #include <sys/ptrace.h>
35 #include <sys/queue.h>
36 #include <sys/linker_set.h>
37 #include <sys/sbuf.h>
38 #include <sys/sysctl.h>
39 #include <sys/user.h>
40 #include <sys/wait.h>
41 #include <machine/elf.h>
42 #include <vm/vm_param.h>
43 #include <vm/vm.h>
44 #include <vm/pmap.h>
45 #include <vm/vm_map.h>
46 #include <assert.h>
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <stdbool.h>
51 #include <stdint.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <libutil.h>
57
58 #include "extern.h"
59
60 /*
61  * Code for generating ELF core dumps.
62  */
63
64 typedef void (*segment_callback)(vm_map_entry_t, void *);
65
66 /* Closure for cb_put_phdr(). */
67 struct phdr_closure {
68         Elf_Phdr *phdr;         /* Program header to fill in */
69         Elf_Off offset;         /* Offset of segment in core file */
70 };
71
72 /* Closure for cb_size_segment(). */
73 struct sseg_closure {
74         int count;              /* Count of writable segments. */
75         size_t size;            /* Total size of all writable segments. */
76 };
77
78 #ifdef ELFCORE_COMPAT_32
79 typedef struct fpreg32 elfcore_fpregset_t;
80 typedef struct reg32   elfcore_gregset_t;
81 typedef struct prpsinfo32 elfcore_prpsinfo_t;
82 typedef struct prstatus32 elfcore_prstatus_t;
83 static void elf_convert_gregset(elfcore_gregset_t *rd, struct reg *rs);
84 static void elf_convert_fpregset(elfcore_fpregset_t *rd, struct fpreg *rs);
85 #else
86 typedef fpregset_t elfcore_fpregset_t;
87 typedef gregset_t  elfcore_gregset_t;
88 typedef prpsinfo_t elfcore_prpsinfo_t;
89 typedef prstatus_t elfcore_prstatus_t;
90 #define elf_convert_gregset(d,s)        *d = *s
91 #define elf_convert_fpregset(d,s)       *d = *s
92 #endif
93
94 typedef void* (*notefunc_t)(void *, size_t *);
95
96 static void cb_put_phdr(vm_map_entry_t, void *);
97 static void cb_size_segment(vm_map_entry_t, void *);
98 static void each_writable_segment(vm_map_entry_t, segment_callback,
99     void *closure);
100 static void elf_detach(void);   /* atexit() handler. */
101 static void *elf_note_fpregset(void *, size_t *);
102 static void *elf_note_prpsinfo(void *, size_t *);
103 static void *elf_note_prstatus(void *, size_t *);
104 static void *elf_note_thrmisc(void *, size_t *);
105 #if defined(__i386__) || defined(__amd64__)
106 static void *elf_note_x86_xstate(void *, size_t *);
107 #endif
108 static void *elf_note_procstat_auxv(void *, size_t *);
109 static void *elf_note_procstat_files(void *, size_t *);
110 static void *elf_note_procstat_groups(void *, size_t *);
111 static void *elf_note_procstat_osrel(void *, size_t *);
112 static void *elf_note_procstat_proc(void *, size_t *);
113 static void *elf_note_procstat_psstrings(void *, size_t *);
114 static void *elf_note_procstat_rlimit(void *, size_t *);
115 static void *elf_note_procstat_umask(void *, size_t *);
116 static void *elf_note_procstat_vmmap(void *, size_t *);
117 static void elf_puthdr(pid_t, vm_map_entry_t, void *, size_t, size_t, size_t,
118     int);
119 static void elf_putnote(int, notefunc_t, void *, struct sbuf *);
120 static void elf_putnotes(pid_t, struct sbuf *, size_t *);
121 static void freemap(vm_map_entry_t);
122 static vm_map_entry_t readmap(pid_t);
123 static void *procstat_sysctl(void *, int, size_t, size_t *sizep);
124
125 static pid_t g_pid;             /* Pid being dumped, global for elf_detach */
126 static int g_status;            /* proc status after ptrace attach */
127
128 static int
129 elf_ident(int efd, pid_t pid __unused, char *binfile __unused)
130 {
131         Elf_Ehdr hdr;
132         int cnt;
133         uint16_t machine;
134
135         cnt = read(efd, &hdr, sizeof(hdr));
136         if (cnt != sizeof(hdr))
137                 return (0);
138         if (!IS_ELF(hdr))
139                 return (0);
140         switch (hdr.e_ident[EI_DATA]) {
141         case ELFDATA2LSB:
142                 machine = le16toh(hdr.e_machine);
143                 break;
144         case ELFDATA2MSB:
145                 machine = be16toh(hdr.e_machine);
146                 break;
147         default:
148                 return (0);
149         }
150         if (!ELF_MACHINE_OK(machine))
151                 return (0);
152
153         /* Looks good. */
154         return (1);
155 }
156
157 static void
158 elf_detach(void)
159 {
160         int sig;
161
162         if (g_pid != 0) {
163                 /*
164                  * Forward any pending signals. SIGSTOP is generated by ptrace
165                  * itself, so ignore it.
166                  */
167                 sig = WIFSTOPPED(g_status) ? WSTOPSIG(g_status) : 0;
168                 if (sig == SIGSTOP)
169                         sig = 0;
170                 ptrace(PT_DETACH, g_pid, (caddr_t)1, sig);
171         }
172 }
173
174 /*
175  * Write an ELF coredump for the given pid to the given fd.
176  */
177 static void
178 elf_coredump(int efd __unused, int fd, pid_t pid)
179 {
180         vm_map_entry_t map;
181         struct sseg_closure seginfo;
182         struct sbuf *sb;
183         void *hdr;
184         size_t hdrsize, notesz, segoff;
185         ssize_t n, old_len;
186         Elf_Phdr *php;
187         int i;
188
189         /* Attach to process to dump. */
190         g_pid = pid;
191         if (atexit(elf_detach) != 0)
192                 err(1, "atexit");
193         errno = 0;
194         ptrace(PT_ATTACH, pid, NULL, 0);
195         if (errno)
196                 err(1, "PT_ATTACH");
197         if (waitpid(pid, &g_status, 0) == -1)
198                 err(1, "waitpid");
199
200         /* Get the program's memory map. */
201         map = readmap(pid);
202
203         /* Size the program segments. */
204         seginfo.count = 0;
205         seginfo.size = 0;
206         each_writable_segment(map, cb_size_segment, &seginfo);
207
208         /*
209          * Build the header and the notes using sbuf and write to the file.
210          */
211         sb = sbuf_new_auto();
212         hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
213         /* Start header + notes section. */
214         sbuf_start_section(sb, NULL);
215         /* Make empty header subsection. */
216         sbuf_start_section(sb, &old_len);
217         sbuf_putc(sb, 0);
218         sbuf_end_section(sb, old_len, hdrsize, 0);
219         /* Put notes. */
220         elf_putnotes(pid, sb, &notesz);
221         /* Align up to a page boundary for the program segments. */
222         sbuf_end_section(sb, -1, PAGE_SIZE, 0);
223         if (sbuf_finish(sb) != 0)
224                 err(1, "sbuf_finish");
225         hdr = sbuf_data(sb);
226         segoff = sbuf_len(sb);
227         /* Fill in the header. */
228         elf_puthdr(pid, map, hdr, hdrsize, notesz, segoff, seginfo.count);
229
230         n = write(fd, hdr, segoff);
231         if (n == -1)
232                 err(1, "write");
233         if (n < segoff)
234               errx(1, "short write");
235
236         /* Write the contents of all of the writable segments. */
237         php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
238         for (i = 0;  i < seginfo.count;  i++) {
239                 struct ptrace_io_desc iorequest;
240                 uintmax_t nleft = php->p_filesz;
241
242                 iorequest.piod_op = PIOD_READ_D;
243                 iorequest.piod_offs = (caddr_t)(uintptr_t)php->p_vaddr;
244                 while (nleft > 0) {
245                         char buf[8*1024];
246                         size_t nwant;
247                         ssize_t ngot;
248
249                         if (nleft > sizeof(buf))
250                                 nwant = sizeof buf;
251                         else
252                                 nwant = nleft;
253                         iorequest.piod_addr = buf;
254                         iorequest.piod_len = nwant;
255                         ptrace(PT_IO, pid, (caddr_t)&iorequest, 0);
256                         ngot = iorequest.piod_len;
257                         if ((size_t)ngot < nwant)
258                                 errx(1, "short read wanted %zu, got %zd",
259                                     nwant, ngot);
260                         ngot = write(fd, buf, nwant);
261                         if (ngot == -1)
262                                 err(1, "write of segment %d failed", i);
263                         if ((size_t)ngot != nwant)
264                                 errx(1, "short write");
265                         nleft -= nwant;
266                         iorequest.piod_offs += ngot;
267                 }
268                 php++;
269         }
270         sbuf_delete(sb);
271         freemap(map);
272 }
273
274 /*
275  * A callback for each_writable_segment() to write out the segment's
276  * program header entry.
277  */
278 static void
279 cb_put_phdr(vm_map_entry_t entry, void *closure)
280 {
281         struct phdr_closure *phc = (struct phdr_closure *)closure;
282         Elf_Phdr *phdr = phc->phdr;
283
284         phc->offset = round_page(phc->offset);
285
286         phdr->p_type = PT_LOAD;
287         phdr->p_offset = phc->offset;
288         phdr->p_vaddr = entry->start;
289         phdr->p_paddr = 0;
290         phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
291         phdr->p_align = PAGE_SIZE;
292         phdr->p_flags = 0;
293         if (entry->protection & VM_PROT_READ)
294                 phdr->p_flags |= PF_R;
295         if (entry->protection & VM_PROT_WRITE)
296                 phdr->p_flags |= PF_W;
297         if (entry->protection & VM_PROT_EXECUTE)
298                 phdr->p_flags |= PF_X;
299
300         phc->offset += phdr->p_filesz;
301         phc->phdr++;
302 }
303
304 /*
305  * A callback for each_writable_segment() to gather information about
306  * the number of segments and their total size.
307  */
308 static void
309 cb_size_segment(vm_map_entry_t entry, void *closure)
310 {
311         struct sseg_closure *ssc = (struct sseg_closure *)closure;
312
313         ssc->count++;
314         ssc->size += entry->end - entry->start;
315 }
316
317 /*
318  * For each segment in the given memory map, call the given function
319  * with a pointer to the map entry and some arbitrary caller-supplied
320  * data.
321  */
322 static void
323 each_writable_segment(vm_map_entry_t map, segment_callback func, void *closure)
324 {
325         vm_map_entry_t entry;
326
327         for (entry = map;  entry != NULL;  entry = entry->next)
328                 (*func)(entry, closure);
329 }
330
331 static void
332 elf_putnotes(pid_t pid, struct sbuf *sb, size_t *sizep)
333 {
334         lwpid_t *tids;
335         size_t threads, old_len;
336         ssize_t size;
337         int i;
338
339         errno = 0;
340         threads = ptrace(PT_GETNUMLWPS, pid, NULL, 0);
341         if (errno)
342                 err(1, "PT_GETNUMLWPS");
343         tids = malloc(threads * sizeof(*tids));
344         if (tids == NULL)
345                 errx(1, "out of memory");
346         errno = 0;
347         ptrace(PT_GETLWPLIST, pid, (void *)tids, threads);
348         if (errno)
349                 err(1, "PT_GETLWPLIST");
350
351         sbuf_start_section(sb, &old_len);
352         elf_putnote(NT_PRPSINFO, elf_note_prpsinfo, &pid, sb);
353
354         for (i = 0; i < threads; ++i) {
355                 elf_putnote(NT_PRSTATUS, elf_note_prstatus, tids + i, sb);
356                 elf_putnote(NT_FPREGSET, elf_note_fpregset, tids + i, sb);
357                 elf_putnote(NT_THRMISC, elf_note_thrmisc, tids + i, sb);
358 #if defined(__i386__) || defined(__amd64__)
359                 elf_putnote(NT_X86_XSTATE, elf_note_x86_xstate, tids + i, sb);
360 #endif
361         }
362
363 #ifndef ELFCORE_COMPAT_32
364         elf_putnote(NT_PROCSTAT_PROC, elf_note_procstat_proc, &pid, sb);
365         elf_putnote(NT_PROCSTAT_FILES, elf_note_procstat_files, &pid, sb);
366         elf_putnote(NT_PROCSTAT_VMMAP, elf_note_procstat_vmmap, &pid, sb);
367         elf_putnote(NT_PROCSTAT_GROUPS, elf_note_procstat_groups, &pid, sb);
368         elf_putnote(NT_PROCSTAT_UMASK, elf_note_procstat_umask, &pid, sb);
369         elf_putnote(NT_PROCSTAT_RLIMIT, elf_note_procstat_rlimit, &pid, sb);
370         elf_putnote(NT_PROCSTAT_OSREL, elf_note_procstat_osrel, &pid, sb);
371         elf_putnote(NT_PROCSTAT_PSSTRINGS, elf_note_procstat_psstrings, &pid,
372             sb);
373         elf_putnote(NT_PROCSTAT_AUXV, elf_note_procstat_auxv, &pid, sb);
374 #endif
375
376         size = sbuf_end_section(sb, old_len, 1, 0);
377         if (size == -1)
378                 err(1, "sbuf_end_section");
379         free(tids);
380         *sizep = size;
381 }
382
383 /*
384  * Emit one note section to sbuf.
385  */
386 static void
387 elf_putnote(int type, notefunc_t notefunc, void *arg, struct sbuf *sb)
388 {
389         Elf_Note note;
390         size_t descsz;
391         ssize_t old_len;
392         void *desc;
393
394         desc = notefunc(arg, &descsz);
395         note.n_namesz = 8; /* strlen("FreeBSD") + 1 */
396         note.n_descsz = descsz;
397         note.n_type = type;
398
399         sbuf_bcat(sb, &note, sizeof(note));
400         sbuf_start_section(sb, &old_len);
401         sbuf_bcat(sb, "FreeBSD", note.n_namesz);
402         sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
403         if (descsz == 0)
404                 return;
405         sbuf_start_section(sb, &old_len);
406         sbuf_bcat(sb, desc, descsz);
407         sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
408         free(desc);
409 }
410
411 /*
412  * Generate the ELF coredump header.
413  */
414 static void
415 elf_puthdr(pid_t pid, vm_map_entry_t map, void *hdr, size_t hdrsize,
416     size_t notesz, size_t segoff, int numsegs)
417 {
418         Elf_Ehdr *ehdr;
419         Elf_Phdr *phdr;
420         struct phdr_closure phc;
421
422         ehdr = (Elf_Ehdr *)hdr;
423         phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr));
424
425         ehdr->e_ident[EI_MAG0] = ELFMAG0;
426         ehdr->e_ident[EI_MAG1] = ELFMAG1;
427         ehdr->e_ident[EI_MAG2] = ELFMAG2;
428         ehdr->e_ident[EI_MAG3] = ELFMAG3;
429         ehdr->e_ident[EI_CLASS] = ELF_CLASS;
430         ehdr->e_ident[EI_DATA] = ELF_DATA;
431         ehdr->e_ident[EI_VERSION] = EV_CURRENT;
432         ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
433         ehdr->e_ident[EI_ABIVERSION] = 0;
434         ehdr->e_ident[EI_PAD] = 0;
435         ehdr->e_type = ET_CORE;
436         ehdr->e_machine = ELF_ARCH;
437         ehdr->e_version = EV_CURRENT;
438         ehdr->e_entry = 0;
439         ehdr->e_phoff = sizeof(Elf_Ehdr);
440         ehdr->e_flags = 0;
441         ehdr->e_ehsize = sizeof(Elf_Ehdr);
442         ehdr->e_phentsize = sizeof(Elf_Phdr);
443         ehdr->e_phnum = numsegs + 1;
444         ehdr->e_shentsize = sizeof(Elf_Shdr);
445         ehdr->e_shnum = 0;
446         ehdr->e_shstrndx = SHN_UNDEF;
447
448         /*
449          * Fill in the program header entries.
450          */
451
452         /* The note segement. */
453         phdr->p_type = PT_NOTE;
454         phdr->p_offset = hdrsize;
455         phdr->p_vaddr = 0;
456         phdr->p_paddr = 0;
457         phdr->p_filesz = notesz;
458         phdr->p_memsz = 0;
459         phdr->p_flags = PF_R;
460         phdr->p_align = sizeof(Elf32_Size);
461         phdr++;
462
463         /* All the writable segments from the program. */
464         phc.phdr = phdr;
465         phc.offset = segoff;
466         each_writable_segment(map, cb_put_phdr, &phc);
467 }
468
469 /*
470  * Free the memory map.
471  */
472 static void
473 freemap(vm_map_entry_t map)
474 {
475
476         while (map != NULL) {
477                 vm_map_entry_t next = map->next;
478                 free(map);
479                 map = next;
480         }
481 }
482
483 /*
484  * Read the process's memory map using kinfo_getvmmap(), and return a list of
485  * VM map entries.  Only the non-device read/writable segments are
486  * returned.  The map entries in the list aren't fully filled in; only
487  * the items we need are present.
488  */
489 static vm_map_entry_t
490 readmap(pid_t pid)
491 {
492         vm_map_entry_t ent, *linkp, map;
493         struct kinfo_vmentry *vmentl, *kve;
494         int i, nitems;
495
496         vmentl = kinfo_getvmmap(pid, &nitems);
497         if (vmentl == NULL)
498                 err(1, "cannot retrieve mappings for %u process", pid);
499
500         map = NULL;
501         linkp = &map;
502         for (i = 0; i < nitems; i++) {
503                 kve = &vmentl[i];
504
505                 /*
506                  * Ignore 'malformed' segments or ones representing memory
507                  * mapping with MAP_NOCORE on.
508                  * If the 'full' support is disabled, just dump the most
509                  * meaningful data segments.
510                  */
511                 if ((kve->kve_protection & KVME_PROT_READ) == 0 ||
512                     (kve->kve_flags & KVME_FLAG_NOCOREDUMP) != 0 ||
513                     kve->kve_type == KVME_TYPE_DEAD ||
514                     kve->kve_type == KVME_TYPE_UNKNOWN ||
515                     ((pflags & PFLAGS_FULL) == 0 &&
516                     kve->kve_type != KVME_TYPE_DEFAULT &&
517                     kve->kve_type != KVME_TYPE_VNODE &&
518                     kve->kve_type != KVME_TYPE_SWAP &&
519                     kve->kve_type != KVME_TYPE_PHYS))
520                         continue;
521
522                 ent = calloc(1, sizeof(*ent));
523                 if (ent == NULL)
524                         errx(1, "out of memory");
525                 ent->start = (vm_offset_t)kve->kve_start;
526                 ent->end = (vm_offset_t)kve->kve_end;
527                 ent->protection = VM_PROT_READ | VM_PROT_WRITE;
528                 if ((kve->kve_protection & KVME_PROT_EXEC) != 0)
529                         ent->protection |= VM_PROT_EXECUTE;
530
531                 *linkp = ent;
532                 linkp = &ent->next;
533         }
534         free(vmentl);
535         return (map);
536 }
537
538 /*
539  * Miscellaneous note out functions.
540  */
541
542 static void *
543 elf_note_prpsinfo(void *arg, size_t *sizep)
544 {
545         char *cp, *end;
546         pid_t pid;
547         elfcore_prpsinfo_t *psinfo;
548         struct kinfo_proc kip;
549         size_t len;
550         int name[4];
551
552         pid = *(pid_t *)arg;
553         psinfo = calloc(1, sizeof(*psinfo));
554         if (psinfo == NULL)
555                 errx(1, "out of memory");
556         psinfo->pr_version = PRPSINFO_VERSION;
557         psinfo->pr_psinfosz = sizeof(*psinfo);
558
559         name[0] = CTL_KERN;
560         name[1] = KERN_PROC;
561         name[2] = KERN_PROC_PID;
562         name[3] = pid;
563         len = sizeof(kip);
564         if (sysctl(name, 4, &kip, &len, NULL, 0) == -1)
565                 err(1, "kern.proc.pid.%u", pid);
566         if (kip.ki_pid != pid)
567                 err(1, "kern.proc.pid.%u", pid);
568         strlcpy(psinfo->pr_fname, kip.ki_comm, sizeof(psinfo->pr_fname));
569         name[2] = KERN_PROC_ARGS;
570         len = sizeof(psinfo->pr_psargs) - 1;
571         if (sysctl(name, 4, psinfo->pr_psargs, &len, NULL, 0) == 0 && len > 0) {
572                 cp = psinfo->pr_psargs;
573                 end = cp + len - 1;
574                 for (;;) {
575                         cp = memchr(cp, '\0', end - cp);
576                         if (cp == NULL)
577                                 break;
578                         *cp = ' ';
579                 }
580         } else
581                 strlcpy(psinfo->pr_psargs, kip.ki_comm,
582                     sizeof(psinfo->pr_psargs));
583         psinfo->pr_pid = pid;
584
585         *sizep = sizeof(*psinfo);
586         return (psinfo);
587 }
588
589 static void *
590 elf_note_prstatus(void *arg, size_t *sizep)
591 {
592         lwpid_t tid;
593         elfcore_prstatus_t *status;
594         struct reg greg;
595
596         tid = *(lwpid_t *)arg;
597         status = calloc(1, sizeof(*status));
598         if (status == NULL)
599                 errx(1, "out of memory");
600         status->pr_version = PRSTATUS_VERSION;
601         status->pr_statussz = sizeof(*status);
602         status->pr_gregsetsz = sizeof(elfcore_gregset_t);
603         status->pr_fpregsetsz = sizeof(elfcore_fpregset_t);
604         status->pr_osreldate = __FreeBSD_version;
605         status->pr_pid = tid;
606         ptrace(PT_GETREGS, tid, (void *)&greg, 0);
607         elf_convert_gregset(&status->pr_reg, &greg);
608
609         *sizep = sizeof(*status);
610         return (status);
611 }
612
613 static void *
614 elf_note_fpregset(void *arg, size_t *sizep)
615 {
616         lwpid_t tid;
617         elfcore_fpregset_t *fpregset;
618         fpregset_t fpreg;
619
620         tid = *(lwpid_t *)arg;
621         fpregset = calloc(1, sizeof(*fpregset));
622         if (fpregset == NULL)
623                 errx(1, "out of memory");
624         ptrace(PT_GETFPREGS, tid, (void *)&fpreg, 0);
625         elf_convert_fpregset(fpregset, &fpreg);
626
627         *sizep = sizeof(*fpregset);
628         return (fpregset);
629 }
630
631 static void *
632 elf_note_thrmisc(void *arg, size_t *sizep)
633 {
634         lwpid_t tid;
635         struct ptrace_lwpinfo lwpinfo;
636         thrmisc_t *thrmisc;
637
638         tid = *(lwpid_t *)arg;
639         thrmisc = calloc(1, sizeof(*thrmisc));
640         if (thrmisc == NULL)
641                 errx(1, "out of memory");
642         ptrace(PT_LWPINFO, tid, (void *)&lwpinfo,
643             sizeof(lwpinfo));
644         memset(&thrmisc->_pad, 0, sizeof(thrmisc->_pad));
645         strcpy(thrmisc->pr_tname, lwpinfo.pl_tdname);
646
647         *sizep = sizeof(*thrmisc);
648         return (thrmisc);
649 }
650
651 #if defined(__i386__) || defined(__amd64__)
652 static void *
653 elf_note_x86_xstate(void *arg, size_t *sizep)
654 {
655         lwpid_t tid;
656         char *xstate;
657         static bool xsave_checked = false;
658         static struct ptrace_xstate_info info;
659
660         tid = *(lwpid_t *)arg;
661         if (!xsave_checked) {
662                 if (ptrace(PT_GETXSTATE_INFO, tid, (void *)&info,
663                     sizeof(info)) != 0)
664                         info.xsave_len = 0;
665                 xsave_checked = true;
666         }
667         if (info.xsave_len == 0) {
668                 *sizep = 0;
669                 return (NULL);
670         }
671         xstate = calloc(1, info.xsave_len);
672         ptrace(PT_GETXSTATE, tid, xstate, 0);
673         *(uint64_t *)(xstate + X86_XSTATE_XCR0_OFFSET) = info.xsave_mask;
674         *sizep = info.xsave_len;
675         return (xstate);
676 }
677 #endif
678
679 static void *
680 procstat_sysctl(void *arg, int what, size_t structsz, size_t *sizep)
681 {
682         size_t len, oldlen;
683         pid_t pid;
684         int name[4], structsize;
685         void *buf, *p;
686
687         pid = *(pid_t *)arg;
688         structsize = structsz;
689         name[0] = CTL_KERN;
690         name[1] = KERN_PROC;
691         name[2] = what;
692         name[3] = pid;
693         len = 0;
694         if (sysctl(name, 4, NULL, &len, NULL, 0) == -1)
695                 err(1, "kern.proc.%d.%u", what, pid);
696         buf = calloc(1, sizeof(structsize) + len * 4 / 3);
697         if (buf == NULL)
698                 errx(1, "out of memory");
699         bcopy(&structsize, buf, sizeof(structsize));
700         p = (char *)buf + sizeof(structsize);
701         if (sysctl(name, 4, p, &len, NULL, 0) == -1)
702                 err(1, "kern.proc.%d.%u", what, pid);
703
704         *sizep = sizeof(structsize) + len;
705         return (buf);
706 }
707
708 static void *
709 elf_note_procstat_proc(void *arg, size_t *sizep)
710 {
711
712         return (procstat_sysctl(arg, KERN_PROC_PID | KERN_PROC_INC_THREAD,
713             sizeof(struct kinfo_proc), sizep));
714 }
715
716 static void *
717 elf_note_procstat_files(void *arg, size_t *sizep)
718 {
719
720         return (procstat_sysctl(arg, KERN_PROC_FILEDESC,
721             sizeof(struct kinfo_file), sizep));
722 }
723
724 static void *
725 elf_note_procstat_vmmap(void *arg, size_t *sizep)
726 {
727
728         return (procstat_sysctl(arg, KERN_PROC_VMMAP,
729             sizeof(struct kinfo_vmentry), sizep));
730 }
731
732 static void *
733 elf_note_procstat_groups(void *arg, size_t *sizep)
734 {
735
736         return (procstat_sysctl(arg, KERN_PROC_GROUPS, sizeof(gid_t), sizep));
737 }
738
739 static void *
740 elf_note_procstat_umask(void *arg, size_t *sizep)
741 {
742
743         return (procstat_sysctl(arg, KERN_PROC_UMASK, sizeof(u_short), sizep));
744 }
745
746 static void *
747 elf_note_procstat_osrel(void *arg, size_t *sizep)
748 {
749
750         return (procstat_sysctl(arg, KERN_PROC_OSREL, sizeof(int), sizep));
751 }
752
753 static void *
754 elf_note_procstat_psstrings(void *arg, size_t *sizep)
755 {
756
757         return (procstat_sysctl(arg, KERN_PROC_PS_STRINGS,
758             sizeof(vm_offset_t), sizep));
759 }
760
761 static void *
762 elf_note_procstat_auxv(void *arg, size_t *sizep)
763 {
764
765         return (procstat_sysctl(arg, KERN_PROC_AUXV,
766             sizeof(Elf_Auxinfo), sizep));
767 }
768
769 static void *
770 elf_note_procstat_rlimit(void *arg, size_t *sizep)
771 {
772         pid_t pid;
773         size_t len;
774         int i, name[5], structsize;
775         void *buf, *p;
776
777         pid = *(pid_t *)arg;
778         structsize = sizeof(struct rlimit) * RLIM_NLIMITS;
779         buf = calloc(1, sizeof(structsize) + structsize);
780         if (buf == NULL)
781                 errx(1, "out of memory");
782         bcopy(&structsize, buf, sizeof(structsize));
783         p = (char *)buf + sizeof(structsize);
784         name[0] = CTL_KERN;
785         name[1] = KERN_PROC;
786         name[2] = KERN_PROC_RLIMIT;
787         name[3] = pid;
788         len = sizeof(struct rlimit);
789         for (i = 0; i < RLIM_NLIMITS; i++) {
790                 name[4] = i;
791                 if (sysctl(name, 5, p, &len, NULL, 0) == -1)
792                         err(1, "kern.proc.rlimit.%u", pid);
793                 if (len != sizeof(struct rlimit))
794                         errx(1, "kern.proc.rlimit.%u: short read", pid);
795                 p += len;
796         }
797
798         *sizep = sizeof(structsize) + structsize;
799         return (buf);
800 }
801
802 struct dumpers __elfN(dump) = { elf_ident, elf_coredump };
803 TEXT_SET(dumpset, __elfN(dump));