Deleted Added
full compact
amd64-linux32.c (286937) amd64-linux32.c (286938)
1/*
2 * Copyright 1997 Sean Eric Fagan
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Sean Eric Fagan
15 * 4. Neither the name of the author may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#ifndef lint
33static const char rcsid[] =
1/*
2 * Copyright 1997 Sean Eric Fagan
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Sean Eric Fagan
15 * 4. Neither the name of the author may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#ifndef lint
33static const char rcsid[] =
34 "$FreeBSD: head/usr.bin/truss/amd64-linux32.c 286937 2015-08-19 19:59:42Z jhb $";
34 "$FreeBSD: head/usr.bin/truss/amd64-linux32.c 286938 2015-08-19 20:02:03Z jhb $";
35#endif /* not lint */
36
37/*
38 * Linux/i386-specific system call handling. Given how much of this code
39 * is taken from the freebsd equivalent, I can probably put even more of
40 * it in support routines that can be used by any personality support.
41 */
42
43#include <sys/types.h>
44#include <sys/ptrace.h>
45
46#include <machine/reg.h>
47#include <machine/psl.h>
48
49#include <errno.h>
50#include <fcntl.h>
51#include <signal.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <time.h>
56#include <unistd.h>
57
58#include "truss.h"
59#include "syscall.h"
60#include "extern.h"
61
62#include "linux32_syscalls.h"
63
64static int nsyscalls = nitems(linux32_syscallnames);
65
66/*
67 * This is what this particular file uses to keep track of a system call.
68 * It is probably not quite sufficient -- I can probably use the same
69 * structure for the various syscall personalities, and I also probably
70 * need to nest system calls (for signal handlers).
71 *
72 * 'struct syscall' describes the system call; it may be NULL, however,
73 * if we don't know about this particular system call yet.
74 */
75struct linux_syscall {
76 struct syscall *sc;
77 const char *name;
78 int number;
79 unsigned long args[5];
80 int nargs; /* number of arguments -- *not* number of words! */
81 char **s_args; /* the printable arguments */
82};
83
84static struct linux_syscall *
85alloc_fsc(void)
86{
87
88 return (malloc(sizeof(struct linux_syscall)));
89}
90
91/* Clear up and free parts of the fsc structure. */
92static void
93free_fsc(struct linux_syscall *fsc)
94{
95 int i;
96
97 if (fsc->s_args) {
98 for (i = 0; i < fsc->nargs; i++)
99 free(fsc->s_args[i]);
100 free(fsc->s_args);
101 }
102 free(fsc);
103}
104
105/*
106 * Called when a process has entered a system call. nargs is the
107 * number of words, not number of arguments (a necessary distinction
108 * in some cases). Note that if the STOPEVENT() code in i386/i386/trap.c
109 * is ever changed these functions need to keep up.
110 */
111
112void
113amd64_linux32_syscall_entry(struct trussinfo *trussinfo, int nargs)
114{
115 struct reg regs;
116 struct linux_syscall *fsc;
117 struct syscall *sc;
118 lwpid_t tid;
119 int i, syscall_num;
120
121 tid = trussinfo->curthread->tid;
122
123 if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
124 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
125 return;
126 }
127
128 syscall_num = regs.r_rax;
129
130 fsc = alloc_fsc();
131 if (fsc == NULL)
132 return;
133 fsc->number = syscall_num;
134 fsc->name = (syscall_num < 0 || syscall_num >= nsyscalls) ?
135 NULL : linux32_syscallnames[syscall_num];
136 if (!fsc->name) {
137 fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n",
138 syscall_num);
139 }
140
141 if (fsc->name && (trussinfo->flags & FOLLOWFORKS) &&
142 (strcmp(fsc->name, "linux_fork") == 0 ||
143 strcmp(fsc->name, "linux_vfork") == 0))
144 trussinfo->curthread->in_fork = 1;
145
146 if (nargs == 0)
147 return;
148
149 /*
150 * Linux passes syscall arguments in registers, not
151 * on the stack. Fortunately, we've got access to the
152 * register set. Note that we don't bother checking the
153 * number of arguments. And what does linux do for syscalls
154 * that have more than five arguments?
155 */
156
157 fsc->args[0] = regs.r_rbx;
158 fsc->args[1] = regs.r_rcx;
159 fsc->args[2] = regs.r_rdx;
160 fsc->args[3] = regs.r_rsi;
161 fsc->args[4] = regs.r_rdi;
162
163 sc = get_syscall(fsc->name);
164 if (sc)
165 fsc->nargs = sc->nargs;
166 else {
167#if DEBUG
168 fprintf(trussinfo->outfile, "unknown syscall %s -- setting "
169 "args to %d\n", fsc->name, nargs);
170#endif
171 fsc->nargs = nargs;
172 }
173
174 fsc->s_args = calloc(1, (1 + fsc->nargs) * sizeof(char *));
175 fsc->sc = sc;
176
177 /*
178 * At this point, we set up the system call arguments.
179 * We ignore any OUT ones, however -- those are arguments that
180 * are set by the system call, and so are probably meaningless
181 * now. This doesn't currently support arguments that are
182 * passed in *and* out, however.
183 */
184
185 if (fsc->name) {
186#if DEBUG
187 fprintf(stderr, "syscall %s(", fsc->name);
188#endif
189 for (i = 0; i < fsc->nargs; i++) {
190#if DEBUG
191 fprintf(stderr, "0x%x%s", sc ?
192 fsc->args[sc->args[i].offset] : fsc->args[i],
193 i < (fsc->nargs - 1) ? "," : "");
194#endif
195 if (sc && !(sc->args[i].type & OUT)) {
196 fsc->s_args[i] = print_arg(&sc->args[i],
197 fsc->args, 0, trussinfo);
198 }
199 }
200#if DEBUG
201 fprintf(stderr, ")\n");
202#endif
203 }
204
205#if DEBUG
206 fprintf(trussinfo->outfile, "\n");
207#endif
208
209 if (fsc->name != NULL && (strcmp(fsc->name, "linux_execve") == 0 ||
210 strcmp(fsc->name, "exit") == 0)) {
211 /*
212 * XXX
213 * This could be done in a more general
214 * manner but it still wouldn't be very pretty.
215 */
216 if (strcmp(fsc->name, "linux_execve") == 0) {
217 if ((trussinfo->flags & EXECVEARGS) == 0) {
218 if (fsc->s_args[1]) {
219 free(fsc->s_args[1]);
220 fsc->s_args[1] = NULL;
221 }
222 }
223 if ((trussinfo->flags & EXECVEENVS) == 0) {
224 if (fsc->s_args[2]) {
225 free(fsc->s_args[2]);
226 fsc->s_args[2] = NULL;
227 }
228 }
229 }
230 }
231 trussinfo->curthread->fsc = fsc;
232}
233
234/*
235 * Linux syscalls return negative errno's, we do positive and map them
236 */
237static const int bsd_to_linux_errno[] = {
238 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9,
239 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
240 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
241 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
242 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
243 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
244 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
245 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9,
246 -6,
247};
248
249long
250amd64_linux32_syscall_exit(struct trussinfo *trussinfo,
251 int syscall_num __unused)
252{
253 struct reg regs;
254 struct linux_syscall *fsc;
255 struct syscall *sc;
256 lwpid_t tid;
257 long retval;
258 int errorp, i;
259
260 if (trussinfo->curthread->fsc == NULL)
261 return (-1);
262
263 tid = trussinfo->curthread->tid;
264
265 if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
266 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
267 return (-1);
268 }
269
270 retval = regs.r_rax;
271 errorp = !!(regs.r_rflags & PSL_C);
272
273 /*
274 * This code, while simpler than the initial versions I used, could
275 * stand some significant cleaning.
276 */
277
278 fsc = trussinfo->curthread->fsc;
279 sc = fsc->sc;
280 if (!sc) {
281 for (i = 0; i < fsc->nargs; i++)
282 asprintf(&fsc->s_args[i], "0x%lx", fsc->args[i]);
283 } else {
284 /*
285 * Here, we only look for arguments that have OUT masked in --
286 * otherwise, they were handled in the syscall_entry function.
287 */
288 for (i = 0; i < sc->nargs; i++) {
289 char *temp;
35#endif /* not lint */
36
37/*
38 * Linux/i386-specific system call handling. Given how much of this code
39 * is taken from the freebsd equivalent, I can probably put even more of
40 * it in support routines that can be used by any personality support.
41 */
42
43#include <sys/types.h>
44#include <sys/ptrace.h>
45
46#include <machine/reg.h>
47#include <machine/psl.h>
48
49#include <errno.h>
50#include <fcntl.h>
51#include <signal.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <time.h>
56#include <unistd.h>
57
58#include "truss.h"
59#include "syscall.h"
60#include "extern.h"
61
62#include "linux32_syscalls.h"
63
64static int nsyscalls = nitems(linux32_syscallnames);
65
66/*
67 * This is what this particular file uses to keep track of a system call.
68 * It is probably not quite sufficient -- I can probably use the same
69 * structure for the various syscall personalities, and I also probably
70 * need to nest system calls (for signal handlers).
71 *
72 * 'struct syscall' describes the system call; it may be NULL, however,
73 * if we don't know about this particular system call yet.
74 */
75struct linux_syscall {
76 struct syscall *sc;
77 const char *name;
78 int number;
79 unsigned long args[5];
80 int nargs; /* number of arguments -- *not* number of words! */
81 char **s_args; /* the printable arguments */
82};
83
84static struct linux_syscall *
85alloc_fsc(void)
86{
87
88 return (malloc(sizeof(struct linux_syscall)));
89}
90
91/* Clear up and free parts of the fsc structure. */
92static void
93free_fsc(struct linux_syscall *fsc)
94{
95 int i;
96
97 if (fsc->s_args) {
98 for (i = 0; i < fsc->nargs; i++)
99 free(fsc->s_args[i]);
100 free(fsc->s_args);
101 }
102 free(fsc);
103}
104
105/*
106 * Called when a process has entered a system call. nargs is the
107 * number of words, not number of arguments (a necessary distinction
108 * in some cases). Note that if the STOPEVENT() code in i386/i386/trap.c
109 * is ever changed these functions need to keep up.
110 */
111
112void
113amd64_linux32_syscall_entry(struct trussinfo *trussinfo, int nargs)
114{
115 struct reg regs;
116 struct linux_syscall *fsc;
117 struct syscall *sc;
118 lwpid_t tid;
119 int i, syscall_num;
120
121 tid = trussinfo->curthread->tid;
122
123 if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
124 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
125 return;
126 }
127
128 syscall_num = regs.r_rax;
129
130 fsc = alloc_fsc();
131 if (fsc == NULL)
132 return;
133 fsc->number = syscall_num;
134 fsc->name = (syscall_num < 0 || syscall_num >= nsyscalls) ?
135 NULL : linux32_syscallnames[syscall_num];
136 if (!fsc->name) {
137 fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n",
138 syscall_num);
139 }
140
141 if (fsc->name && (trussinfo->flags & FOLLOWFORKS) &&
142 (strcmp(fsc->name, "linux_fork") == 0 ||
143 strcmp(fsc->name, "linux_vfork") == 0))
144 trussinfo->curthread->in_fork = 1;
145
146 if (nargs == 0)
147 return;
148
149 /*
150 * Linux passes syscall arguments in registers, not
151 * on the stack. Fortunately, we've got access to the
152 * register set. Note that we don't bother checking the
153 * number of arguments. And what does linux do for syscalls
154 * that have more than five arguments?
155 */
156
157 fsc->args[0] = regs.r_rbx;
158 fsc->args[1] = regs.r_rcx;
159 fsc->args[2] = regs.r_rdx;
160 fsc->args[3] = regs.r_rsi;
161 fsc->args[4] = regs.r_rdi;
162
163 sc = get_syscall(fsc->name);
164 if (sc)
165 fsc->nargs = sc->nargs;
166 else {
167#if DEBUG
168 fprintf(trussinfo->outfile, "unknown syscall %s -- setting "
169 "args to %d\n", fsc->name, nargs);
170#endif
171 fsc->nargs = nargs;
172 }
173
174 fsc->s_args = calloc(1, (1 + fsc->nargs) * sizeof(char *));
175 fsc->sc = sc;
176
177 /*
178 * At this point, we set up the system call arguments.
179 * We ignore any OUT ones, however -- those are arguments that
180 * are set by the system call, and so are probably meaningless
181 * now. This doesn't currently support arguments that are
182 * passed in *and* out, however.
183 */
184
185 if (fsc->name) {
186#if DEBUG
187 fprintf(stderr, "syscall %s(", fsc->name);
188#endif
189 for (i = 0; i < fsc->nargs; i++) {
190#if DEBUG
191 fprintf(stderr, "0x%x%s", sc ?
192 fsc->args[sc->args[i].offset] : fsc->args[i],
193 i < (fsc->nargs - 1) ? "," : "");
194#endif
195 if (sc && !(sc->args[i].type & OUT)) {
196 fsc->s_args[i] = print_arg(&sc->args[i],
197 fsc->args, 0, trussinfo);
198 }
199 }
200#if DEBUG
201 fprintf(stderr, ")\n");
202#endif
203 }
204
205#if DEBUG
206 fprintf(trussinfo->outfile, "\n");
207#endif
208
209 if (fsc->name != NULL && (strcmp(fsc->name, "linux_execve") == 0 ||
210 strcmp(fsc->name, "exit") == 0)) {
211 /*
212 * XXX
213 * This could be done in a more general
214 * manner but it still wouldn't be very pretty.
215 */
216 if (strcmp(fsc->name, "linux_execve") == 0) {
217 if ((trussinfo->flags & EXECVEARGS) == 0) {
218 if (fsc->s_args[1]) {
219 free(fsc->s_args[1]);
220 fsc->s_args[1] = NULL;
221 }
222 }
223 if ((trussinfo->flags & EXECVEENVS) == 0) {
224 if (fsc->s_args[2]) {
225 free(fsc->s_args[2]);
226 fsc->s_args[2] = NULL;
227 }
228 }
229 }
230 }
231 trussinfo->curthread->fsc = fsc;
232}
233
234/*
235 * Linux syscalls return negative errno's, we do positive and map them
236 */
237static const int bsd_to_linux_errno[] = {
238 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9,
239 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
240 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
241 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
242 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
243 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
244 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
245 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9,
246 -6,
247};
248
249long
250amd64_linux32_syscall_exit(struct trussinfo *trussinfo,
251 int syscall_num __unused)
252{
253 struct reg regs;
254 struct linux_syscall *fsc;
255 struct syscall *sc;
256 lwpid_t tid;
257 long retval;
258 int errorp, i;
259
260 if (trussinfo->curthread->fsc == NULL)
261 return (-1);
262
263 tid = trussinfo->curthread->tid;
264
265 if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
266 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
267 return (-1);
268 }
269
270 retval = regs.r_rax;
271 errorp = !!(regs.r_rflags & PSL_C);
272
273 /*
274 * This code, while simpler than the initial versions I used, could
275 * stand some significant cleaning.
276 */
277
278 fsc = trussinfo->curthread->fsc;
279 sc = fsc->sc;
280 if (!sc) {
281 for (i = 0; i < fsc->nargs; i++)
282 asprintf(&fsc->s_args[i], "0x%lx", fsc->args[i]);
283 } else {
284 /*
285 * Here, we only look for arguments that have OUT masked in --
286 * otherwise, they were handled in the syscall_entry function.
287 */
288 for (i = 0; i < sc->nargs; i++) {
289 char *temp;
290
290 if (sc->args[i].type & OUT) {
291 /*
292 * If an error occurred, then don't bother
293 * getting the data; it may not be valid.
294 */
295 if (errorp) {
296 asprintf(&temp, "0x%lx",
297 fsc->args[sc->args[i].offset]);
298 } else {
299 temp = print_arg(&sc->args[i],
300 fsc->args, retval, trussinfo);
301 }
302 fsc->s_args[i] = temp;
303 }
304 }
305 }
306
307 /*
308 * It would probably be a good idea to merge the error handling,
309 * but that complicates things considerably.
310 */
311 if (errorp) {
312 for (i = 0; (size_t)i < nitems(bsd_to_linux_errno); i++) {
313 if (retval == bsd_to_linux_errno[i])
314 break;
315 }
316 }
317
318 if (fsc->name != NULL && (strcmp(fsc->name, "linux_execve") == 0 ||
319 strcmp(fsc->name, "exit") == 0))
320 trussinfo->curthread->in_syscall = 1;
321
322 print_syscall_ret(trussinfo, fsc->name, fsc->nargs, fsc->s_args, errorp,
323 errorp ? i : retval, fsc->sc);
324 free_fsc(fsc);
325
326 return (retval);
327}
291 if (sc->args[i].type & OUT) {
292 /*
293 * If an error occurred, then don't bother
294 * getting the data; it may not be valid.
295 */
296 if (errorp) {
297 asprintf(&temp, "0x%lx",
298 fsc->args[sc->args[i].offset]);
299 } else {
300 temp = print_arg(&sc->args[i],
301 fsc->args, retval, trussinfo);
302 }
303 fsc->s_args[i] = temp;
304 }
305 }
306 }
307
308 /*
309 * It would probably be a good idea to merge the error handling,
310 * but that complicates things considerably.
311 */
312 if (errorp) {
313 for (i = 0; (size_t)i < nitems(bsd_to_linux_errno); i++) {
314 if (retval == bsd_to_linux_errno[i])
315 break;
316 }
317 }
318
319 if (fsc->name != NULL && (strcmp(fsc->name, "linux_execve") == 0 ||
320 strcmp(fsc->name, "exit") == 0))
321 trussinfo->curthread->in_syscall = 1;
322
323 print_syscall_ret(trussinfo, fsc->name, fsc->nargs, fsc->s_args, errorp,
324 errorp ? i : retval, fsc->sc);
325 free_fsc(fsc);
326
327 return (retval);
328}