1 /*-
2 * Copyright (c) 2011 Mikolaj Golub
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29 #include <sys/param.h>
30 #include <sys/elf.h>
31 #include <sys/sysctl.h>
32 #include <sys/user.h>
33
34 #include <vm/vm.h>
35
36 #include <err.h>
37 #include <errno.h>
38 #include <libprocstat.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include "procstat.h"
45
46 #define PRINT(name, spec, val) \
47 printf("%s %-16s " #spec "\n", prefix, #name, (val))
48 #define PRINT_UNKNOWN(type, val) \
49 printf("%s %16ld %#lx\n", prefix, (long)type, (u_long)(val))
50
51 void
procstat_auxv(struct procstat * procstat,struct kinfo_proc * kipp)52 procstat_auxv(struct procstat *procstat, struct kinfo_proc *kipp)
53 {
54 Elf_Auxinfo *auxv;
55 u_int count, i;
56 static char prefix[256];
57
58 if (!hflag)
59 printf("%5s %-16s %-16s %-16s\n", "PID", "COMM", "AUXV", "VALUE");
60 auxv = procstat_getauxv(procstat, kipp, &count);
61 if (auxv == NULL)
62 return;
63 snprintf(prefix, sizeof(prefix), "%5d %-16s", kipp->ki_pid,
64 kipp->ki_comm);
65 for (i = 0; i < count; i++) {
66 switch(auxv[i].a_type) {
67 case AT_NULL:
68 return;
69 case AT_IGNORE:
70 break;
71 case AT_EXECFD:
72 PRINT(AT_EXECFD, %ld, (long)auxv[i].a_un.a_val);
73 break;
74 case AT_PHDR:
75 PRINT(AT_PHDR, %p, auxv[i].a_un.a_ptr);
76 break;
77 case AT_PHENT:
78 PRINT(AT_PHENT, %ld, (long)auxv[i].a_un.a_val);
79 break;
80 case AT_PHNUM:
81 PRINT(AT_PHNUM, %ld, (long)auxv[i].a_un.a_val);
82 break;
83 case AT_PAGESZ:
84 PRINT(AT_PAGESZ, %ld, (long)auxv[i].a_un.a_val);
85 break;
86 case AT_BASE:
87 PRINT(AT_BASE, %p, auxv[i].a_un.a_ptr);
88 break;
89 case AT_FLAGS:
90 PRINT(AT_FLAGS, %#lx, (u_long)auxv[i].a_un.a_val);
91 break;
92 case AT_ENTRY:
93 PRINT(AT_ENTRY, %p, auxv[i].a_un.a_ptr);
94 break;
95 #ifdef AT_NOTELF
96 case AT_NOTELF:
97 PRINT(AT_NOTELF, %ld, (long)auxv[i].a_un.a_val);
98 break;
99 #endif
100 #ifdef AT_UID
101 case AT_UID:
102 PRINT(AT_UID, %ld, (long)auxv[i].a_un.a_val);
103 break;
104 #endif
105 #ifdef AT_EUID
106 case AT_EUID:
107 PRINT(AT_EUID, %ld, (long)auxv[i].a_un.a_val);
108 break;
109 #endif
110 #ifdef AT_GID
111 case AT_GID:
112 PRINT(AT_GID, %ld, (long)auxv[i].a_un.a_val);
113 break;
114 #endif
115 #ifdef AT_EGID
116 case AT_EGID:
117 PRINT(AT_EGID, %ld, (long)auxv[i].a_un.a_val);
118 break;
119 #endif
120 case AT_EXECPATH:
121 PRINT(AT_EXECPATH, %p, auxv[i].a_un.a_ptr);
122 break;
123 case AT_CANARY:
124 PRINT(AT_CANARY, %p, auxv[i].a_un.a_ptr);
125 break;
126 case AT_CANARYLEN:
127 PRINT(AT_CANARYLEN, %ld, (long)auxv[i].a_un.a_val);
128 break;
129 case AT_OSRELDATE:
130 PRINT(AT_OSRELDATE, %ld, (long)auxv[i].a_un.a_val);
131 break;
132 case AT_NCPUS:
133 PRINT(AT_NCPUS, %ld, (long)auxv[i].a_un.a_val);
134 break;
135 case AT_PAGESIZES:
136 PRINT(AT_PAGESIZES, %p, auxv[i].a_un.a_ptr);
137 break;
138 case AT_PAGESIZESLEN:
139 PRINT(AT_PAGESIZESLEN, %ld, (long)auxv[i].a_un.a_val);
140 break;
141 case AT_STACKPROT:
142 if ((auxv[i].a_un.a_val & VM_PROT_EXECUTE) != 0)
143 PRINT(AT_STACKPROT, %s, "NONEXECUTABLE");
144 else
145 PRINT(AT_STACKPROT, %s, "EXECUTABLE");
146 break;
147 #ifdef AT_TIMEKEEP
148 case AT_TIMEKEEP:
149 PRINT(AT_TIMEKEEP, %p, auxv[i].a_un.a_ptr);
150 break;
151 #endif
152 default:
153 PRINT_UNKNOWN(auxv[i].a_type, auxv[i].a_un.a_val);
154 break;
155 }
156 }
157 printf("\n");
158 procstat_freeauxv(procstat, auxv);
159 }
160
161