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