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