blob: a17eb3bb4837aaf3e9bff34075f0ca1f7ba4c806 [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * $Id$
34 */
35
36#include "defs.h"
37
38#include <signal.h>
39#include <time.h>
40#include <errno.h>
41#include <sys/user.h>
42#include <sys/syscall.h>
43#include <sys/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000044
Wichert Akkerman15dea971999-10-06 13:06:34 +000045#if HAVE_ASM_REG_H
Wichert Akkerman00a82ee2001-03-28 20:29:17 +000046#ifdef SPARC
47# define fpq kernel_fpq
48# define fq kernel_fq
49# define fpu kernel_fpu
50#endif
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000051#include <asm/reg.h>
Wichert Akkerman00a82ee2001-03-28 20:29:17 +000052#ifdef SPARC
53# undef fpq
54# undef fq
Roland McGrath761b5d72002-12-15 23:58:31 +000055# undef fpu
Wichert Akkerman00a82ee2001-03-28 20:29:17 +000056#endif
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000057#endif
58
Wichert Akkerman15dea971999-10-06 13:06:34 +000059#ifdef HAVE_SYS_REG_H
60#include <sys/reg.h>
61#ifndef PTRACE_PEEKUSR
62# define PTRACE_PEEKUSR PTRACE_PEEKUSER
63#endif
Wichert Akkermanfaf72222000-02-19 23:59:03 +000064#elif defined(HAVE_LINUX_PTRACE_H)
65#undef PTRACE_SYSCALL
66#include <linux/ptrace.h>
Wichert Akkerman15dea971999-10-06 13:06:34 +000067#endif
68
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000069#if defined(LINUX) && defined(IA64)
70# include <asm/ptrace_offsets.h>
71# include <asm/rse.h>
72#endif
73
Pavel Machekd8ae7e32000-02-01 17:17:25 +000074#define NR_SYSCALL_BASE 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000075#ifdef LINUX
76#ifndef ERESTARTSYS
77#define ERESTARTSYS 512
78#endif
79#ifndef ERESTARTNOINTR
80#define ERESTARTNOINTR 513
81#endif
82#ifndef ERESTARTNOHAND
83#define ERESTARTNOHAND 514 /* restart if no handler.. */
84#endif
85#ifndef ENOIOCTLCMD
86#define ENOIOCTLCMD 515 /* No ioctl command */
87#endif
88#ifndef NSIG
89#define NSIG 32
90#endif
91#ifdef ARM
92#undef NSIG
93#define NSIG 32
Pavel Machekd8ae7e32000-02-01 17:17:25 +000094#undef NR_SYSCALL_BASE
95#define NR_SYSCALL_BASE __NR_SYSCALL_BASE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000096#endif
97#endif /* LINUX */
98
99#include "syscall.h"
100
101/* Define these shorthand notations to simplify the syscallent files. */
102#define TF TRACE_FILE
103#define TI TRACE_IPC
104#define TN TRACE_NETWORK
105#define TP TRACE_PROCESS
106#define TS TRACE_SIGNAL
107
108struct sysent sysent0[] = {
109#include "syscallent.h"
110};
111int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
112
113#if SUPPORTED_PERSONALITIES >= 2
114struct sysent sysent1[] = {
115#include "syscallent1.h"
116};
117int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
118#endif /* SUPPORTED_PERSONALITIES >= 2 */
119
120#if SUPPORTED_PERSONALITIES >= 3
121struct sysent sysent2[] = {
122#include "syscallent2.h"
123};
124int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
125#endif /* SUPPORTED_PERSONALITIES >= 3 */
126
127struct sysent *sysent;
128int nsyscalls;
129
130/* Now undef them since short defines cause wicked namespace pollution. */
131#undef TF
132#undef TI
133#undef TN
134#undef TP
135#undef TS
136
137char *errnoent0[] = {
138#include "errnoent.h"
139};
140int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
141
142#if SUPPORTED_PERSONALITIES >= 2
143char *errnoent1[] = {
144#include "errnoent1.h"
145};
146int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
147#endif /* SUPPORTED_PERSONALITIES >= 2 */
148
149#if SUPPORTED_PERSONALITIES >= 3
150char *errnoent2[] = {
151#include "errnoent2.h"
152};
153int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
154#endif /* SUPPORTED_PERSONALITIES >= 3 */
155
156char **errnoent;
157int nerrnos;
158
159int current_personality;
160
161int
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000162set_personality(personality)
163int personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000164{
165 switch (personality) {
166 case 0:
167 errnoent = errnoent0;
168 nerrnos = nerrnos0;
169 sysent = sysent0;
170 nsyscalls = nsyscalls0;
171 ioctlent = ioctlent0;
172 nioctlents = nioctlents0;
173 signalent = signalent0;
174 nsignals = nsignals0;
175 break;
176
177#if SUPPORTED_PERSONALITIES >= 2
178 case 1:
179 errnoent = errnoent1;
180 nerrnos = nerrnos1;
181 sysent = sysent1;
182 nsyscalls = nsyscalls1;
183 ioctlent = ioctlent1;
184 nioctlents = nioctlents1;
185 signalent = signalent1;
186 nsignals = nsignals1;
187 break;
188#endif /* SUPPORTED_PERSONALITIES >= 2 */
189
190#if SUPPORTED_PERSONALITIES >= 3
191 case 2:
192 errnoent = errnoent2;
193 nerrnos = nerrnos2;
194 sysent = sysent2;
195 nsyscalls = nsyscalls2;
196 ioctlent = ioctlent2;
197 nioctlents = nioctlents2;
198 signalent = signalent2;
199 nsignals = nsignals2;
200 break;
201#endif /* SUPPORTED_PERSONALITIES >= 3 */
202
203 default:
204 return -1;
205 }
206
207 current_personality = personality;
208 return 0;
209}
210
211int qual_flags[MAX_QUALS];
212
213static int call_count[MAX_QUALS];
214static int error_count[MAX_QUALS];
215static struct timeval tv_count[MAX_QUALS];
216static int sorted_count[MAX_QUALS];
217
218static struct timeval shortest = { 1000000, 0 };
219
220static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
221
222static struct qual_options {
223 int bitflag;
224 char *option_name;
225 int (*lookup)();
226 char *argument_name;
227} qual_options[] = {
228 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
229 { QUAL_TRACE, "t", lookup_syscall, "system call" },
230 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
231 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
232 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
233 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
234 { QUAL_RAW, "raw", lookup_syscall, "system call" },
235 { QUAL_RAW, "x", lookup_syscall, "system call" },
236 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
237 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
238 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
239 { QUAL_FAULT, "fault", lookup_fault, "fault" },
240 { QUAL_FAULT, "faults", lookup_fault, "fault" },
241 { QUAL_FAULT, "m", lookup_fault, "fault" },
242 { QUAL_READ, "read", lookup_desc, "descriptor" },
243 { QUAL_READ, "reads", lookup_desc, "descriptor" },
244 { QUAL_READ, "r", lookup_desc, "descriptor" },
245 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
246 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
247 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
248 { 0, NULL, NULL, NULL },
249};
250
251static int
252lookup_syscall(s)
253char *s;
254{
255 int i;
256
257 for (i = 0; i < nsyscalls; i++) {
258 if (strcmp(s, sysent[i].sys_name) == 0)
259 return i;
260 }
261 return -1;
262}
263
264static int
265lookup_signal(s)
266char *s;
267{
268 int i;
269 char buf[32];
270
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000271 if (s && *s && isdigit((unsigned char)*s))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000272 return atoi(s);
273 strcpy(buf, s);
274 s = buf;
275 for (i = 0; s[i]; i++)
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000276 s[i] = toupper((unsigned char)(s[i]));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000277 if (strncmp(s, "SIG", 3) == 0)
278 s += 3;
279 for (i = 0; i <= NSIG; i++) {
Nate Sammonsce780fc1999-03-29 23:23:13 +0000280 if (strcmp(s, signame(i) + 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000281 return i;
282 }
283 return -1;
284}
285
286static int
287lookup_fault(s)
288char *s;
289{
290 return -1;
291}
292
293static int
294lookup_desc(s)
295char *s;
296{
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000297 if (s && *s && isdigit((unsigned char)*s))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000298 return atoi(s);
299 return -1;
300}
301
302static int
303lookup_class(s)
304char *s;
305{
306 if (strcmp(s, "file") == 0)
307 return TRACE_FILE;
308 if (strcmp(s, "ipc") == 0)
309 return TRACE_IPC;
310 if (strcmp(s, "network") == 0)
311 return TRACE_NETWORK;
312 if (strcmp(s, "process") == 0)
313 return TRACE_PROCESS;
314 if (strcmp(s, "signal") == 0)
315 return TRACE_SIGNAL;
316 return -1;
317}
318
319void
320qualify(s)
321char *s;
322{
323 struct qual_options *opt;
324 int not;
325 char *p;
326 int i, n;
327
328 opt = &qual_options[0];
329 for (i = 0; (p = qual_options[i].option_name); i++) {
330 n = strlen(p);
331 if (strncmp(s, p, n) == 0 && s[n] == '=') {
332 opt = &qual_options[i];
333 s += n + 1;
334 break;
335 }
336 }
337 not = 0;
338 if (*s == '!') {
339 not = 1;
340 s++;
341 }
342 if (strcmp(s, "none") == 0) {
343 not = 1 - not;
344 s = "all";
345 }
346 if (strcmp(s, "all") == 0) {
347 for (i = 0; i < MAX_QUALS; i++) {
348 if (not)
349 qual_flags[i] &= ~opt->bitflag;
350 else
351 qual_flags[i] |= opt->bitflag;
352 }
353 return;
354 }
355 for (i = 0; i < MAX_QUALS; i++) {
356 if (not)
357 qual_flags[i] |= opt->bitflag;
358 else
359 qual_flags[i] &= ~opt->bitflag;
360 }
361 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
362 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
363 for (i = 0; i < MAX_QUALS; i++) {
364 if (sysent[i].sys_flags & n) {
365 if (not)
366 qual_flags[i] &= ~opt->bitflag;
367 else
368 qual_flags[i] |= opt->bitflag;
369 }
370 }
371 continue;
372 }
373 if ((n = (*opt->lookup)(p)) < 0) {
374 fprintf(stderr, "strace: invalid %s `%s'\n",
375 opt->argument_name, p);
376 exit(1);
377 }
378 if (not)
379 qual_flags[n] &= ~opt->bitflag;
380 else
381 qual_flags[n] |= opt->bitflag;
382 }
383 return;
384}
385
386static void
387dumpio(tcp)
388struct tcb *tcp;
389{
390 if (syserror(tcp))
391 return;
392 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
393 return;
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000394 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000395 case SYS_read:
396#ifdef SYS_recv
397 case SYS_recv:
398#endif
399#ifdef SYS_recvfrom
400 case SYS_recvfrom:
401#endif
402 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
403 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
404 break;
405 case SYS_write:
406#ifdef SYS_send
407 case SYS_send:
408#endif
409#ifdef SYS_sendto
410 case SYS_sendto:
411#endif
412 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
413 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
414 break;
John Hughes1d08dcf2001-07-10 13:48:44 +0000415#ifdef SYS_readv
416 case SYS_readv:
417 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
418 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
419 break;
420#endif
421#ifdef SYS_writev
422 case SYS_writev:
423
424 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
425 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
426 break;
427#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000428 }
429}
430
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000431#ifndef FREEBSD
Wichert Akkerman8829a551999-06-11 13:18:40 +0000432enum subcall_style { shift_style, deref_style, mask_style, door_style };
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000433#else /* FREEBSD */
434enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
435
436struct subcall {
437 int call;
438 int nsubcalls;
439 int subcalls[5];
440};
441
442const struct subcall subcalls_table[] = {
443 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
John Hughes61eeb552001-03-06 15:51:53 +0000444#ifdef SYS_semconfig
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000445 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
John Hughes61eeb552001-03-06 15:51:53 +0000446#else
447 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
448#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000449 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
450};
451#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000452
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000453#if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000454
455const int socket_map [] = {
456 /* SYS_SOCKET */ 97,
457 /* SYS_BIND */ 104,
458 /* SYS_CONNECT */ 98,
459 /* SYS_LISTEN */ 106,
460 /* SYS_ACCEPT */ 99,
461 /* SYS_GETSOCKNAME */ 150,
462 /* SYS_GETPEERNAME */ 141,
463 /* SYS_SOCKETPAIR */ 135,
464 /* SYS_SEND */ 101,
465 /* SYS_RECV */ 102,
466 /* SYS_SENDTO */ 133,
467 /* SYS_RECVFROM */ 125,
468 /* SYS_SHUTDOWN */ 134,
469 /* SYS_SETSOCKOPT */ 105,
470 /* SYS_GETSOCKOPT */ 118,
471 /* SYS_SENDMSG */ 114,
472 /* SYS_RECVMSG */ 113
473};
474
475void
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000476sparc_socket_decode (tcp)
477struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000478{
479 volatile long addr;
480 volatile int i, n;
481
482 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
483 return;
484 }
485 tcp->scno = socket_map [tcp->u_arg [0]-1];
486 n = tcp->u_nargs = sysent [tcp->scno].nargs;
487 addr = tcp->u_arg [1];
488 for (i = 0; i < n; i++){
489 int arg;
490 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
491 arg = 0;
492 tcp->u_arg [i] = arg;
493 addr += sizeof (arg);
494 }
495}
496
Michal Ludvig0e035502002-09-23 15:41:01 +0000497void
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000498decode_subcall(tcp, subcall, nsubcalls, style)
499struct tcb *tcp;
500int subcall;
501int nsubcalls;
502enum subcall_style style;
503{
Michal Ludvig10a88d02002-10-07 14:31:00 +0000504 long addr, mask, arg;
505 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000506
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000507 switch (style) {
508 case shift_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000509 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
510 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000511 tcp->scno = subcall + tcp->u_arg[0];
512 if (sysent[tcp->scno].nargs != -1)
513 tcp->u_nargs = sysent[tcp->scno].nargs;
514 else
515 tcp->u_nargs--;
516 for (i = 0; i < tcp->u_nargs; i++)
517 tcp->u_arg[i] = tcp->u_arg[i + 1];
518 break;
519 case deref_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000520 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
521 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000522 tcp->scno = subcall + tcp->u_arg[0];
523 addr = tcp->u_arg[1];
524 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
525 if (umove(tcp, addr, &arg) < 0)
526 arg = 0;
527 tcp->u_arg[i] = arg;
528 addr += sizeof(arg);
529 }
530 tcp->u_nargs = sysent[tcp->scno].nargs;
531 break;
532 case mask_style:
533 mask = (tcp->u_arg[0] >> 8) & 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000534 for (i = 0; mask; i++)
535 mask >>= 1;
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000536 if (i >= nsubcalls)
537 return;
538 tcp->u_arg[0] &= 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000539 tcp->scno = subcall + i;
540 if (sysent[tcp->scno].nargs != -1)
541 tcp->u_nargs = sysent[tcp->scno].nargs;
542 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000543 case door_style:
544 /*
545 * Oh, yuck. The call code is the *sixth* argument.
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000546 * (don't you mean the *last* argument? - JH)
Wichert Akkerman8829a551999-06-11 13:18:40 +0000547 */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000548 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
549 return;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000550 tcp->scno = subcall + tcp->u_arg[5];
551 if (sysent[tcp->scno].nargs != -1)
552 tcp->u_nargs = sysent[tcp->scno].nargs;
553 else
554 tcp->u_nargs--;
555 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000556#ifdef FREEBSD
557 case table_style:
558 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
559 if (subcalls_table[i].call == tcp->scno) break;
560 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
561 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
562 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
563 for (i = 0; i < tcp->u_nargs; i++)
564 tcp->u_arg[i] = tcp->u_arg[i + 1];
565 }
566 break;
567#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000568 }
569}
570#endif
571
572struct tcb *tcp_last = NULL;
573
574static int
575internal_syscall(tcp)
576struct tcb *tcp;
577{
578 /*
579 * We must always trace a few critical system calls in order to
580 * correctly support following forks in the presence of tracing
581 * qualifiers.
582 */
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000583 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000584#ifdef SYS_fork
585 case SYS_fork:
586#endif
587#ifdef SYS_vfork
588 case SYS_vfork:
589#endif
John Hughes4e36a812001-04-18 15:11:51 +0000590#ifdef SYS_fork1
591 case SYS_fork1:
592#endif
593#ifdef SYS_forkall
594 case SYS_forkall:
595#endif
596#ifdef SYS_rfork1
597 case SYS_rfork1:
598#endif
599#ifdef SYS_rforkall
600 case SYS_rforkall:
601#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000602 internal_fork(tcp);
603 break;
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000604#ifdef SYS_clone
605 case SYS_clone:
606 internal_clone(tcp);
607 break;
608#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000609#ifdef SYS_clone2
610 case SYS_clone2:
611 internal_clone(tcp);
612 break;
613#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000614#ifdef SYS_execv
615 case SYS_execv:
616#endif
617#ifdef SYS_execve
618 case SYS_execve:
619#endif
John Hughes4e36a812001-04-18 15:11:51 +0000620#ifdef SYS_rexecve
621 case SYS_rexecve:
622#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000623 internal_exec(tcp);
624 break;
625
626#ifdef SYS_wait
627 case SYS_wait:
628#endif
629#ifdef SYS_wait4
630 case SYS_wait4:
631#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000632#ifdef SYS32_wait4
633 case SYS32_wait4:
634#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000635#ifdef SYS_waitpid
636 case SYS_waitpid:
637#endif
638#ifdef SYS_waitsys
639 case SYS_waitsys:
640#endif
641 internal_wait(tcp);
642 break;
643
644#ifdef SYS_exit
645 case SYS_exit:
646#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000647#ifdef SYS32_exit
648 case SYS32_exit:
649#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000650 internal_exit(tcp);
651 break;
652 }
653 return 0;
654}
655
Wichert Akkermanc7926982000-04-10 22:22:31 +0000656
657#ifdef LINUX
658#if defined (I386)
659 static long eax;
660#elif defined (IA64)
661 long r8, r10, psr;
662 long ia32 = 0;
663#elif defined (POWERPC)
664 static long result,flags;
665#elif defined (M68K)
666 static int d0;
667#elif defined (ARM)
668 static int r0;
669#elif defined (ALPHA)
670 static long r0;
671 static long a3;
672#elif defined (SPARC)
Wichert Akkerman00a82ee2001-03-28 20:29:17 +0000673 static struct regs regs;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000674 static unsigned long trap;
675#elif defined(MIPS)
676 static long a3;
677 static long r2;
Michal Ludvig10a88d02002-10-07 14:31:00 +0000678#elif defined(S390) || defined(S390X)
Wichert Akkermanc7926982000-04-10 22:22:31 +0000679 static long gpr2;
680 static long pc;
Michal Ludvig882eda82002-11-11 12:50:47 +0000681 static long syscall_mode;
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000682#elif defined(HPPA)
683 static long r28;
Wichert Akkermanccef6372002-05-01 16:39:22 +0000684#elif defined(SH)
685 static long r0;
Michal Ludvig0e035502002-09-23 15:41:01 +0000686#elif defined(X86_64)
687 static long rax;
Roland McGrath761b5d72002-12-15 23:58:31 +0000688#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000689#endif /* LINUX */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000690#ifdef FREEBSD
691 struct reg regs;
Roland McGrath761b5d72002-12-15 23:58:31 +0000692#endif /* FREEBSD */
Wichert Akkermanc7926982000-04-10 22:22:31 +0000693
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000694int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000695get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000696struct tcb *tcp;
697{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000698 long scno = 0;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000699#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000700 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +0000701#endif /* !PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000702
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000703#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +0000704#if defined(S390) || defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +0000705 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000706 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +0000707 if (syscall_mode != -ENOSYS){
708 /*
709 * Since kernel version 2.5.44 the scno gets passed in gpr2.
710 */
711 scno = syscall_mode;
712 }
713 else {
714 /*
715 * Old style of "passing" the scno via the SVC instruction.
716 */
717
718 long opcode, offset_reg, tmp;
719 void * svc_addr;
720 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
721 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
722 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
723 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
Roland McGrath761b5d72002-12-15 23:58:31 +0000724
Michal Ludvig882eda82002-11-11 12:50:47 +0000725 if (upeek(pid, PT_PSWADDR, &pc) < 0)
726 return -1;
727 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
728 if (errno)
729 return -1;
730
731 /*
732 * We have to check if the SVC got executed directly or via an
733 * EXECUTE instruction. In case of EXECUTE it is necessary to do
734 * instruction decoding to derive the system call number.
735 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
736 * so that this doesn't work if a SVC opcode is part of an EXECUTE
737 * opcode. Since there is no way to find out the opcode size this
738 * is the best we can do...
739 */
740
741 if ((opcode & 0xff00) == 0x0a00) {
742 /* SVC opcode */
743 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +0000744 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000745 else {
746 /* SVC got executed by EXECUTE instruction */
747
748 /*
749 * Do instruction decoding of EXECUTE. If you really want to
750 * understand this, read the Principles of Operations.
751 */
752 svc_addr = (void *) (opcode & 0xfff);
753
754 tmp = 0;
755 offset_reg = (opcode & 0x000f0000) >> 16;
756 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
757 return -1;
758 svc_addr += tmp;
759
760 tmp = 0;
761 offset_reg = (opcode & 0x0000f000) >> 12;
762 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
763 return -1;
764 svc_addr += tmp;
765
766 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
767 if (errno)
768 return -1;
769#if defined(S390X)
770 scno >>= 48;
771#else
772 scno >>= 16;
773#endif
774 tmp = 0;
775 offset_reg = (opcode & 0x00f00000) >> 20;
776 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
777 return -1;
778
779 scno = (scno | tmp) & 0xff;
780 }
781 }
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000782#elif defined (POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000783 if (upeek(pid, 4*PT_R0, &scno) < 0)
784 return -1;
785 if (!(tcp->flags & TCB_INSYSCALL)) {
786 /* Check if we return from execve. */
787 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
788 tcp->flags &= ~TCB_WAITEXECVE;
789 return 0;
790 }
791 }
792#elif defined (I386)
793 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
794 return -1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000795#elif defined (X86_64)
796 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
797 return -1;
798
Roland McGrath761b5d72002-12-15 23:58:31 +0000799 if (!(tcp->flags & TCB_INSYSCALL)) {
800 static int currpers=-1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000801 long val;
802
803 /* Check CS register value. On x86-64 linux it is:
804 * 0x33 for long mode (64 bit)
805 * 0x23 for compatibility mode (32 bit)
Roland McGrath761b5d72002-12-15 23:58:31 +0000806 * It takes only one ptrace and thus doesn't need
Michal Ludvig0e035502002-09-23 15:41:01 +0000807 * to be cached.
808 */
809 if (upeek(pid, 8*CS, &val) < 0)
810 return -1;
811 switch(val)
812 {
813 case 0x23: currpers = 1; break;
814 case 0x33: currpers = 0; break;
815 default:
816 fprintf(stderr, "Unknown value CS=0x%02X while "
817 "detecting personality of process "
818 "PID=%d\n", (int)val, pid);
819 currpers = current_personality;
820 break;
821 }
822#if 0
823 /* This version analyzes the opcode of a syscall instruction.
824 * (int 0x80 on i386 vs. syscall on x86-64)
825 * It works, but is too complicated.
826 */
827 unsigned long val, rip, i;
828
829 if(upeek(pid, 8*RIP, &rip)<0)
830 perror("upeek(RIP)");
Roland McGrath761b5d72002-12-15 23:58:31 +0000831
Michal Ludvig0e035502002-09-23 15:41:01 +0000832 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
833 rip-=2;
834 errno = 0;
835
Roland McGrath761b5d72002-12-15 23:58:31 +0000836 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
837 if (errno)
838 printf("ptrace_peektext failed: %s\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000839 strerror(errno));
840 switch (call & 0xffff)
841 {
842 /* x86-64: syscall = 0x0f 0x05 */
843 case 0x050f: currpers = 0; break;
844 /* i386: int 0x80 = 0xcd 0x80 */
845 case 0x80cd: currpers = 1; break;
846 default:
847 currpers = current_personality;
Roland McGrath761b5d72002-12-15 23:58:31 +0000848 fprintf(stderr,
Michal Ludvig0e035502002-09-23 15:41:01 +0000849 "Unknown syscall opcode (0x%04X) while "
850 "detecting personality of process "
851 "PID=%d\n", (int)call, pid);
852 break;
853 }
854#endif
855 if(currpers != current_personality)
856 {
857 char *names[]={"64 bit", "32 bit"};
858 set_personality(currpers);
Roland McGrath761b5d72002-12-15 23:58:31 +0000859 printf("[ Process PID=%d runs in %s mode. ]\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000860 pid, names[current_personality]);
861 }
Roland McGrath761b5d72002-12-15 23:58:31 +0000862 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000863#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000864# define IA64_PSR_IS ((long)1 << 34)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000865 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000866 ia32 = (psr & IA64_PSR_IS) != 0;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000867 if (!(tcp->flags & TCB_INSYSCALL)) {
868 if (ia32) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000869 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000870 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000871 /* Check if we return from execve. */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000872 } else {
873 if (upeek (pid, PT_R15, &scno) < 0)
874 return -1;
875 }
876 } else {
877 /* syscall in progress */
878 if (upeek (pid, PT_R8, &r8) < 0)
879 return -1;
880 if (upeek (pid, PT_R10, &r10) < 0)
881 return -1;
882 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000883 if (tcp->flags & TCB_WAITEXECVE) {
884 tcp->flags &= ~TCB_WAITEXECVE;
885 return 0;
886 }
887
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000888#elif defined (ARM)
Roland McGrath761b5d72002-12-15 23:58:31 +0000889 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000890 long pc;
891 upeek(pid, 4*15, &pc);
892 umoven(tcp, pc-4, 4, (char *)&scno);
893 scno &= 0x000fffff;
894 }
895#elif defined (M68K)
896 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
897 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +0000898#elif defined (MIPS)
899 if (upeek(pid, REG_A3, &a3) < 0)
900 return -1;
901
902 if(!(tcp->flags & TCB_INSYSCALL)) {
903 if (upeek(pid, REG_V0, &scno) < 0)
904 return -1;
905
906 if (scno < 0 || scno > nsyscalls) {
907 if(a3 == 0 || a3 == -1) {
908 if(debug)
909 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
910 return 0;
911 }
912 }
913 } else {
914 if (upeek(pid, REG_V0, &r2) < 0)
915 return -1;
916 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000917#elif defined (ALPHA)
918 if (upeek(pid, REG_A3, &a3) < 0)
919 return -1;
920
921 if (!(tcp->flags & TCB_INSYSCALL)) {
922 if (upeek(pid, REG_R0, &scno) < 0)
923 return -1;
924
925 /* Check if we return from execve. */
926 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
927 tcp->flags &= ~TCB_WAITEXECVE;
928 return 0;
929 }
930
931 /*
932 * Do some sanity checks to figure out if it's
933 * really a syscall entry
934 */
935 if (scno < 0 || scno > nsyscalls) {
936 if (a3 == 0 || a3 == -1) {
937 if (debug)
938 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
939 return 0;
940 }
941 }
942 }
943 else {
944 if (upeek(pid, REG_R0, &r0) < 0)
945 return -1;
946 }
947#elif defined (SPARC)
948 /* Everything we need is in the current register set. */
949 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
950 return -1;
951
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000952 /* If we are entering, then disassemble the syscall trap. */
953 if (!(tcp->flags & TCB_INSYSCALL)) {
954 /* Retrieve the syscall trap instruction. */
955 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000956 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000957 if (errno)
958 return -1;
959
960 /* Disassemble the trap to see what personality to use. */
961 switch (trap) {
962 case 0x91d02010:
963 /* Linux/SPARC syscall trap. */
964 set_personality(0);
965 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +0000966 case 0x91d0206d:
967 /* Linux/SPARC64 syscall trap. */
968 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
969 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000970 case 0x91d02000:
971 /* SunOS syscall trap. (pers 1) */
972 fprintf(stderr,"syscall: SunOS no support\n");
973 return -1;
974 case 0x91d02008:
975 /* Solaris 2.x syscall trap. (per 2) */
976 set_personality(1);
Roland McGrath761b5d72002-12-15 23:58:31 +0000977 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000978 case 0x91d02009:
979 /* NetBSD/FreeBSD syscall trap. */
980 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
981 return -1;
982 case 0x91d02027:
983 /* Solaris 2.x gettimeofday */
984 set_personality(1);
985 break;
986 default:
987 /* Unknown syscall trap. */
988 if(tcp->flags & TCB_WAITEXECVE) {
989 tcp->flags &= ~TCB_WAITEXECVE;
990 return 0;
991 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000992 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000993 return -1;
994 }
995
996 /* Extract the system call number from the registers. */
997 if (trap == 0x91d02027)
998 scno = 156;
999 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001000 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001001 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001002 scno = regs.r_o0;
1003 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001004 }
1005 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001006#elif defined(HPPA)
1007 if (upeek(pid, PT_GR20, &scno) < 0)
1008 return -1;
1009 if (!(tcp->flags & TCB_INSYSCALL)) {
1010 /* Check if we return from execve. */
1011 if ((tcp->flags & TCB_WAITEXECVE)) {
1012 tcp->flags &= ~TCB_WAITEXECVE;
1013 return 0;
1014 }
1015 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001016#elif defined(SH)
1017 /*
1018 * In the new syscall ABI, the system call number is in R3.
1019 */
1020 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1021 return -1;
1022
1023 if (scno < 0) {
1024 /* Odd as it may seem, a glibc bug has been known to cause
1025 glibc to issue bogus negative syscall numbers. So for
1026 our purposes, make strace print what it *should* have been */
1027 long correct_scno = (scno & 0xff);
1028 if (debug)
1029 fprintf(stderr,
Michal Ludvig53b320f2002-09-23 13:30:09 +00001030 "Detected glibc bug: bogus system call number = %ld, "
1031 "correcting to %ld\n",
Wichert Akkermanccef6372002-05-01 16:39:22 +00001032 scno,
1033 correct_scno);
1034 scno = correct_scno;
1035 }
1036
1037
1038 if (!(tcp->flags & TCB_INSYSCALL)) {
1039 /* Check if we return from execve. */
1040 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1041 tcp->flags &= ~TCB_WAITEXECVE;
1042 return 0;
1043 }
1044 }
1045#endif /* SH */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001046#endif /* LINUX */
1047#ifdef SUNOS4
1048 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1049 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001050#elif defined(SH)
1051 /* new syscall ABI returns result in R0 */
1052 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1053 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001054#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001055#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001056#ifdef HAVE_PR_SYSCALL
John Hughes25299712001-03-06 10:10:06 +00001057 scno = tcp->status.PR_SYSCALL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001058#else /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001059#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001060 scno = tcp->status.PR_WHAT;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001061#else /* FREEBSD */
1062 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1063 perror("pread");
1064 return -1;
1065 }
1066 switch (regs.r_eax) {
1067 case SYS_syscall:
1068 case SYS___syscall:
1069 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1070 break;
1071 default:
1072 scno = regs.r_eax;
1073 break;
1074 }
1075#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001076#endif /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001077#endif /* USE_PROCFS */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001078 if (!(tcp->flags & TCB_INSYSCALL))
1079 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001080 return 1;
1081}
1082
Pavel Machek4dc3b142000-02-01 17:58:41 +00001083
1084int
1085syscall_fixup(tcp)
1086struct tcb *tcp;
1087{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001088#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001089 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001090#else /* USE_PROCFS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001091 int scno = tcp->scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001092
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001093 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001094 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001095 if (
1096 scno == SYS_fork
1097#ifdef SYS_vfork
1098 || scno == SYS_vfork
1099#endif /* SYS_vfork */
John Hughes4e36a812001-04-18 15:11:51 +00001100#ifdef SYS_fork1
1101 || scno == SYS_fork1
1102#endif /* SYS_fork1 */
1103#ifdef SYS_forkall
1104 || scno == SYS_forkall
1105#endif /* SYS_forkall */
1106#ifdef SYS_rfork1
1107 || scno == SYS_rfork1
1108#endif /* SYS_fork1 */
1109#ifdef SYS_rforkall
1110 || scno == SYS_rforkall
1111#endif /* SYS_rforkall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001112 ) {
1113 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001114 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001115 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001116 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001117 }
1118 else {
1119 fprintf(stderr, "syscall: missing entry\n");
1120 tcp->flags |= TCB_INSYSCALL;
1121 }
1122 }
1123 }
1124 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001125 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001126 fprintf(stderr, "syscall: missing exit\n");
1127 tcp->flags &= ~TCB_INSYSCALL;
1128 }
1129 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001130#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001131#ifdef SUNOS4
1132 if (!(tcp->flags & TCB_INSYSCALL)) {
1133 if (scno == 0) {
1134 fprintf(stderr, "syscall: missing entry\n");
1135 tcp->flags |= TCB_INSYSCALL;
1136 }
1137 }
1138 else {
1139 if (scno != 0) {
1140 if (debug) {
1141 /*
1142 * This happens when a signal handler
1143 * for a signal which interrupted a
1144 * a system call makes another system call.
1145 */
1146 fprintf(stderr, "syscall: missing exit\n");
1147 }
1148 tcp->flags &= ~TCB_INSYSCALL;
1149 }
1150 }
1151#endif /* SUNOS4 */
1152#ifdef LINUX
1153#if defined (I386)
1154 if (upeek(pid, 4*EAX, &eax) < 0)
1155 return -1;
1156 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1157 if (debug)
1158 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1159 return 0;
1160 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001161#elif defined (X86_64)
1162 if (upeek(pid, 8*RAX, &rax) < 0)
1163 return -1;
1164 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1165 if (debug)
1166 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1167 return 0;
1168 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001169#elif defined (S390) || defined (S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001170 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1171 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001172 if (syscall_mode != -ENOSYS)
1173 syscall_mode = tcp->scno;
1174 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001175 if (debug)
1176 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1177 return 0;
1178 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001179#elif defined (POWERPC)
1180# define SO_MASK 0x10000000
1181 if (upeek(pid, 4*PT_CCR, &flags) < 0)
1182 return -1;
1183 if (upeek(pid, 4*PT_R3, &result) < 0)
1184 return -1;
1185 if (flags & SO_MASK)
1186 result = -result;
1187#elif defined (M68K)
1188 if (upeek(pid, 4*PT_D0, &d0) < 0)
1189 return -1;
1190 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1191 if (debug)
1192 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1193 return 0;
1194 }
1195#elif defined (ARM)
1196 if (upeek(pid, 4*0, (long *)&r0) < 0)
1197 return -1;
1198 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1199 if (debug)
1200 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
1201 return 0;
1202 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001203#elif defined (HPPA)
1204 if (upeek(pid, PT_GR28, &r28) < 0)
1205 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001206#elif defined(IA64)
1207 if (upeek(pid, PT_R10, &r10) < 0)
1208 return -1;
1209 if (upeek(pid, PT_R8, &r8) < 0)
1210 return -1;
1211 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1212 if (debug)
1213 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1214 return 0;
1215 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001216#endif
1217#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001218 return 1;
1219}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001220
Pavel Machek4dc3b142000-02-01 17:58:41 +00001221int
1222get_error(tcp)
1223struct tcb *tcp;
1224{
1225 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001226#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001227#if defined(S390) || defined(S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001228 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1229 tcp->u_rval = -1;
1230 u_error = -gpr2;
1231 }
1232 else {
1233 tcp->u_rval = gpr2;
1234 u_error = 0;
1235 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001236#else /* !S390 && !S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001237#ifdef I386
1238 if (eax < 0 && -eax < nerrnos) {
1239 tcp->u_rval = -1;
1240 u_error = -eax;
1241 }
1242 else {
1243 tcp->u_rval = eax;
1244 u_error = 0;
1245 }
1246#else /* !I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001247#ifdef X86_64
1248 if (rax < 0 && -rax < nerrnos) {
1249 tcp->u_rval = -1;
1250 u_error = -rax;
1251 }
1252 else {
1253 tcp->u_rval = rax;
1254 u_error = 0;
1255 }
1256#else
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001257#ifdef IA64
1258 if (ia32) {
1259 int err;
1260
1261 err = (int)r8;
1262 if (err < 0 && -err < nerrnos) {
1263 tcp->u_rval = -1;
1264 u_error = -err;
1265 }
1266 else {
1267 tcp->u_rval = err;
1268 u_error = 0;
1269 }
1270 } else {
1271 if (r10) {
1272 tcp->u_rval = -1;
1273 u_error = r8;
1274 } else {
1275 tcp->u_rval = r8;
1276 u_error = 0;
1277 }
1278 }
1279#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001280#ifdef MIPS
1281 if (a3) {
1282 tcp->u_rval = -1;
1283 u_error = r2;
1284 } else {
1285 tcp->u_rval = r2;
1286 u_error = 0;
1287 }
1288#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001289#ifdef POWERPC
1290 if (result && (unsigned) -result < nerrnos) {
1291 tcp->u_rval = -1;
1292 u_error = -result;
1293 }
1294 else {
1295 tcp->u_rval = result;
1296 u_error = 0;
1297 }
1298#else /* !POWERPC */
1299#ifdef M68K
1300 if (d0 && (unsigned) -d0 < nerrnos) {
1301 tcp->u_rval = -1;
1302 u_error = -d0;
1303 }
1304 else {
1305 tcp->u_rval = d0;
1306 u_error = 0;
1307 }
1308#else /* !M68K */
1309#ifdef ARM
1310 if (r0 && (unsigned) -r0 < nerrnos) {
1311 tcp->u_rval = -1;
1312 u_error = -r0;
1313 }
1314 else {
1315 tcp->u_rval = r0;
1316 u_error = 0;
1317 }
1318#else /* !ARM */
1319#ifdef ALPHA
1320 if (a3) {
1321 tcp->u_rval = -1;
1322 u_error = r0;
1323 }
1324 else {
1325 tcp->u_rval = r0;
1326 u_error = 0;
1327 }
1328#else /* !ALPHA */
1329#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001330 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001331 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001332 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001333 }
1334 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001335 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001336 u_error = 0;
1337 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001338#else /* !SPARC */
1339#ifdef HPPA
1340 if (r28 && (unsigned) -r28 < nerrnos) {
1341 tcp->u_rval = -1;
1342 u_error = -r28;
1343 }
1344 else {
1345 tcp->u_rval = r28;
1346 u_error = 0;
1347 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001348#else
1349#ifdef SH
1350 /* interpret R0 as return value or error number */
1351 if (r0 && (unsigned) -r0 < nerrnos) {
1352 tcp->u_rval = -1;
1353 u_error = -r0;
1354 }
1355 else {
1356 tcp->u_rval = r0;
1357 u_error = 0;
1358 }
1359#endif /* SH */
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001360#endif /* HPPA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001361#endif /* SPARC */
1362#endif /* ALPHA */
1363#endif /* ARM */
1364#endif /* M68K */
1365#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001366#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001367#endif /* IA64 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001368#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001369#endif /* I386 */
Michal Ludvig10a88d02002-10-07 14:31:00 +00001370#endif /* S390 || S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001371#endif /* LINUX */
1372#ifdef SUNOS4
1373 /* get error code from user struct */
1374 if (upeek(pid, uoff(u_error), &u_error) < 0)
1375 return -1;
1376 u_error >>= 24; /* u_error is a char */
1377
1378 /* get system call return value */
1379 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1380 return -1;
1381#endif /* SUNOS4 */
1382#ifdef SVR4
1383#ifdef SPARC
1384 /* Judicious guessing goes a long way. */
1385 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1386 tcp->u_rval = -1;
1387 u_error = tcp->status.pr_reg[R_O0];
1388 }
1389 else {
1390 tcp->u_rval = tcp->status.pr_reg[R_O0];
1391 u_error = 0;
1392 }
1393#endif /* SPARC */
1394#ifdef I386
1395 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001396 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001397 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001398 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001399 }
1400 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001401 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001402#ifdef HAVE_LONG_LONG
1403 tcp->u_lrval =
1404 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1405 tcp->status.PR_REG[EAX];
1406#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001407 u_error = 0;
1408 }
1409#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001410#ifdef X86_64
1411 /* Wanna know how to kill an hour single-stepping? */
1412 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1413 tcp->u_rval = -1;
1414 u_error = tcp->status.PR_REG[RAX];
1415 }
1416 else {
1417 tcp->u_rval = tcp->status.PR_REG[RAX];
1418 u_error = 0;
1419 }
1420#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001421#ifdef MIPS
1422 if (tcp->status.pr_reg[CTX_A3]) {
1423 tcp->u_rval = -1;
1424 u_error = tcp->status.pr_reg[CTX_V0];
1425 }
1426 else {
1427 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1428 u_error = 0;
1429 }
1430#endif /* MIPS */
1431#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001432#ifdef FREEBSD
1433 if (regs.r_eflags & PSL_C) {
1434 tcp->u_rval = -1;
1435 u_error = regs.r_eax;
1436 } else {
1437 tcp->u_rval = regs.r_eax;
1438 tcp->u_lrval =
1439 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1440 u_error = 0;
1441 }
Roland McGrath761b5d72002-12-15 23:58:31 +00001442#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001443 tcp->u_error = u_error;
1444 return 1;
1445}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001446
Pavel Machek4dc3b142000-02-01 17:58:41 +00001447int syscall_enter(tcp)
1448struct tcb *tcp;
1449{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001450#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001451 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001452#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001453#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001454#if defined(S390) || defined(S390X)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001455 {
1456 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001457 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1458 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001459 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001460 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001461 for (i = 0; i < tcp->u_nargs; i++) {
Michal Ludvig10a88d02002-10-07 14:31:00 +00001462 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001463 return -1;
1464 }
1465 }
1466#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001467 {
1468 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001469 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1470 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001471 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001472 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001473 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001474 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1475 * for scno somewhere above here!
1476 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001477 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1478 return -1;
1479 }
1480 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001481#elif defined (IA64)
1482 {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001483 if (!ia32) {
1484 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1485 /* be backwards compatible with kernel < 2.4.4... */
1486# ifndef PT_RBS_END
1487# define PT_RBS_END PT_AR_BSP
1488# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001489
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001490 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001491 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001492 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1493 return -1;
1494
1495 sof = (cfm >> 0) & 0x7f;
1496 sol = (cfm >> 7) & 0x7f;
1497 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1498
1499 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1500 && sysent[tcp->scno].nargs != -1)
1501 tcp->u_nargs = sysent[tcp->scno].nargs;
1502 else
1503 tcp->u_nargs = MAX_ARGS;
1504 for (i = 0; i < tcp->u_nargs; ++i) {
1505 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1506 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1507 return -1;
1508 }
1509 } else {
1510 int i;
1511
1512 if (/* EBX = out0 */
1513 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1514 /* ECX = out1 */
1515 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1516 /* EDX = out2 */
1517 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1518 /* ESI = out3 */
1519 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1520 /* EDI = out4 */
1521 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1522 /* EBP = out5 */
1523 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1524 return -1;
1525
1526 for (i = 0; i < 6; ++i)
1527 /* truncate away IVE sign-extension */
1528 tcp->u_arg[i] &= 0xffffffff;
1529
1530 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1531 && sysent[tcp->scno].nargs != -1)
1532 tcp->u_nargs = sysent[tcp->scno].nargs;
1533 else
1534 tcp->u_nargs = 5;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001535 }
1536 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001537#elif defined (MIPS)
1538 {
1539 long sp;
1540 int i, nargs;
1541
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001542 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1543 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001544 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001545 nargs = tcp->u_nargs = MAX_ARGS;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001546 if(nargs > 4) {
1547 if(upeek(pid, REG_SP, &sp) < 0)
1548 return -1;
1549 for(i = 0; i < 4; i++) {
1550 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1551 return -1;
1552 }
1553 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1554 (char *)(tcp->u_arg + 4));
1555 } else {
1556 for(i = 0; i < nargs; i++) {
1557 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1558 return -1;
1559 }
1560 }
1561 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001562#elif defined (POWERPC)
Roland McGrath761b5d72002-12-15 23:58:31 +00001563#ifndef PT_ORIG_R3
1564#define PT_ORIG_R3 34
1565#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001566 {
1567 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001568 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1569 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001570 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001571 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001572 for (i = 0; i < tcp->u_nargs; i++) {
1573 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1574 return -1;
1575 }
1576 }
1577#elif defined (SPARC)
1578 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001579 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001580
1581 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1582 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001583 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001584 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001585 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001586 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001587 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001588#elif defined (HPPA)
1589 {
1590 int i;
1591
1592 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1593 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001594 else
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001595 tcp->u_nargs = MAX_ARGS;
1596 for (i = 0; i < tcp->u_nargs; i++) {
1597 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1598 return -1;
1599 }
1600 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001601#elif defined(SH)
1602 {
Roland McGrath761b5d72002-12-15 23:58:31 +00001603 int i;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001604 static int syscall_regs[] = {
1605 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1606 REG_REG0, REG_REG0+1, REG_REG0+2
1607 };
1608
1609 tcp->u_nargs = sysent[tcp->scno].nargs;
1610 for (i = 0; i < tcp->u_nargs; i++) {
1611 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1612 return -1;
1613 }
1614 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001615#elif defined(X86_64)
1616 {
1617 int i;
1618 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1619 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
1620 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
1621 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001622
Michal Ludvig0e035502002-09-23 15:41:01 +00001623 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1624 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001625 else
Michal Ludvig0e035502002-09-23 15:41:01 +00001626 tcp->u_nargs = MAX_ARGS;
1627 for (i = 0; i < tcp->u_nargs; i++) {
1628 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
1629 return -1;
1630 }
1631 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001632#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001633 {
1634 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001635 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1636 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001637 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001638 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001639 for (i = 0; i < tcp->u_nargs; i++) {
1640 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1641 return -1;
1642 }
1643 }
Roland McGrath761b5d72002-12-15 23:58:31 +00001644#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001645#endif /* LINUX */
1646#ifdef SUNOS4
1647 {
1648 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001649 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1650 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001651 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001652 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001653 for (i = 0; i < tcp->u_nargs; i++) {
1654 struct user *u;
1655
1656 if (upeek(pid, uoff(u_arg[0]) +
1657 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1658 return -1;
1659 }
1660 }
1661#endif /* SUNOS4 */
1662#ifdef SVR4
1663#ifdef MIPS
1664 /*
1665 * SGI is broken: even though it has pr_sysarg, it doesn't
1666 * set them on system call entry. Get a clue.
1667 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001668 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001669 tcp->u_nargs = sysent[tcp->scno].nargs;
1670 else
1671 tcp->u_nargs = tcp->status.pr_nsysarg;
1672 if (tcp->u_nargs > 4) {
1673 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1674 4*sizeof(tcp->u_arg[0]));
1675 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1676 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1677 }
1678 else {
1679 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1680 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1681 }
John Hughes25299712001-03-06 10:10:06 +00001682#elif UNIXWARE >= 2
1683 /*
1684 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1685 */
1686 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1687 tcp->u_nargs = sysent[tcp->scno].nargs;
1688 else
1689 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1690 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1691 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1692#elif defined (HAVE_PR_SYSCALL)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001693 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001694 tcp->u_nargs = sysent[tcp->scno].nargs;
1695 else
1696 tcp->u_nargs = tcp->status.pr_nsysarg;
1697 {
1698 int i;
1699 for (i = 0; i < tcp->u_nargs; i++)
1700 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1701 }
John Hughes25299712001-03-06 10:10:06 +00001702#elif defined (I386)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001703 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001704 tcp->u_nargs = sysent[tcp->scno].nargs;
1705 else
1706 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001707 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001708 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
John Hughes25299712001-03-06 10:10:06 +00001709#else
1710 I DONT KNOW WHAT TO DO
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001711#endif /* !HAVE_PR_SYSCALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001712#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001713#ifdef FREEBSD
1714 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1715 sysent[tcp->scno].nargs > tcp->status.val)
1716 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001717 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001718 tcp->u_nargs = tcp->status.val;
1719 if (tcp->u_nargs < 0)
1720 tcp->u_nargs = 0;
1721 if (tcp->u_nargs > MAX_ARGS)
1722 tcp->u_nargs = MAX_ARGS;
1723 switch(regs.r_eax) {
1724 case SYS___syscall:
1725 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1726 regs.r_esp + sizeof(int) + sizeof(quad_t));
1727 break;
1728 case SYS_syscall:
1729 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1730 regs.r_esp + 2 * sizeof(int));
1731 break;
1732 default:
1733 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1734 regs.r_esp + sizeof(int));
1735 break;
1736 }
1737#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001738 return 1;
1739}
1740
1741int
1742trace_syscall(tcp)
1743struct tcb *tcp;
1744{
1745 int sys_res;
1746 struct timeval tv;
1747 int res;
1748
1749 /* Measure the exit time as early as possible to avoid errors. */
1750 if (dtime && (tcp->flags & TCB_INSYSCALL))
1751 gettimeofday(&tv, NULL);
1752
1753 res = get_scno(tcp);
1754 if (res != 1)
1755 return res;
1756
1757 res = syscall_fixup(tcp);
1758 if (res != 1)
1759 return res;
1760
1761 if (tcp->flags & TCB_INSYSCALL) {
1762 long u_error;
1763 res = get_error(tcp);
1764 if (res != 1)
1765 return res;
1766 u_error = tcp->u_error;
1767
1768
1769 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001770 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1771 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001772 tcp->flags &= ~TCB_INSYSCALL;
1773 return 0;
1774 }
1775
1776 if (tcp->flags & TCB_REPRINT) {
1777 printleader(tcp);
1778 tprintf("<... ");
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001779 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001780 tprintf("syscall_%lu", tcp->scno);
1781 else
1782 tprintf("%s", sysent[tcp->scno].sys_name);
1783 tprintf(" resumed> ");
1784 }
1785
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001786 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001787 call_count[tcp->scno]++;
1788 if (tcp->u_error)
1789 error_count[tcp->scno]++;
1790 tv_sub(&tv, &tv, &tcp->etime);
1791#ifdef LINUX
1792 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1793 static struct timeval one_tick =
1794 { 0, 1000000 / HZ };
1795
1796 if (tv_nz(&tcp->dtime))
1797 tv = tcp->dtime;
1798 else if (tv_cmp(&tv, &one_tick) > 0) {
1799 if (tv_cmp(&shortest, &one_tick) < 0)
1800 tv = shortest;
1801 else
1802 tv = one_tick;
1803 }
1804 }
1805#endif /* LINUX */
1806 if (tv_cmp(&tv, &shortest) < 0)
1807 shortest = tv;
1808 tv_add(&tv_count[tcp->scno],
1809 &tv_count[tcp->scno], &tv);
1810 tcp->flags &= ~TCB_INSYSCALL;
1811 return 0;
1812 }
1813
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001814 if (tcp->scno >= nsyscalls || tcp->scno < 0
Pavel Machek4dc3b142000-02-01 17:58:41 +00001815 || (qual_flags[tcp->scno] & QUAL_RAW))
1816 sys_res = printargs(tcp);
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001817 else {
1818 if (not_failing_only && tcp->u_error)
Roland McGrath761b5d72002-12-15 23:58:31 +00001819 return 0; /* ignore failed syscalls */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001820 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
Roland McGrath761b5d72002-12-15 23:58:31 +00001821 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001822 u_error = tcp->u_error;
1823 tprintf(") ");
1824 tabto(acolumn);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001825 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1826 qual_flags[tcp->scno] & QUAL_RAW) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001827 if (u_error)
1828 tprintf("= -1 (errno %ld)", u_error);
1829 else
1830 tprintf("= %#lx", tcp->u_rval);
1831 }
1832 else if (!(sys_res & RVAL_NONE) && u_error) {
1833 switch (u_error) {
1834#ifdef LINUX
1835 case ERESTARTSYS:
1836 tprintf("= ? ERESTARTSYS (To be restarted)");
1837 break;
1838 case ERESTARTNOINTR:
1839 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1840 break;
1841 case ERESTARTNOHAND:
1842 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1843 break;
1844#endif /* LINUX */
1845 default:
1846 tprintf("= -1 ");
Wichert Akkerman4527dae2002-03-31 19:03:29 +00001847 if (u_error < 0)
1848 tprintf("E??? (errno %ld)", u_error);
Pavel Machek4dc3b142000-02-01 17:58:41 +00001849 else if (u_error < nerrnos)
Roland McGrath761b5d72002-12-15 23:58:31 +00001850 tprintf("%s (%s)", errnoent[u_error],
1851 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00001852 else
Roland McGrath761b5d72002-12-15 23:58:31 +00001853 tprintf("ERRNO_%ld (%s)", u_error,
1854 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00001855 break;
1856 }
1857 }
1858 else {
1859 if (sys_res & RVAL_NONE)
1860 tprintf("= ?");
1861 else {
1862 switch (sys_res & RVAL_MASK) {
1863 case RVAL_HEX:
1864 tprintf("= %#lx", tcp->u_rval);
1865 break;
1866 case RVAL_OCTAL:
1867 tprintf("= %#lo", tcp->u_rval);
1868 break;
1869 case RVAL_UDECIMAL:
1870 tprintf("= %lu", tcp->u_rval);
1871 break;
1872 case RVAL_DECIMAL:
1873 tprintf("= %ld", tcp->u_rval);
1874 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001875#ifdef HAVE_LONG_LONG
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001876 case RVAL_LHEX:
1877 tprintf("= %#llx", tcp->u_lrval);
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001878 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001879 case RVAL_LOCTAL:
1880 tprintf("= %#llo", tcp->u_lrval);
1881 break;
1882 case RVAL_LUDECIMAL:
1883 tprintf("= %llu", tcp->u_lrval);
1884 break;
1885 case RVAL_LDECIMAL:
1886 tprintf("= %lld", tcp->u_lrval);
1887 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001888#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001889 default:
1890 fprintf(stderr,
1891 "invalid rval format\n");
1892 break;
1893 }
1894 }
1895 if ((sys_res & RVAL_STR) && tcp->auxstr)
1896 tprintf(" (%s)", tcp->auxstr);
1897 }
1898 if (dtime) {
1899 tv_sub(&tv, &tv, &tcp->etime);
1900 tprintf(" <%ld.%06ld>",
1901 (long) tv.tv_sec, (long) tv.tv_usec);
1902 }
1903 printtrailer(tcp);
1904
1905 dumpio(tcp);
1906 if (fflush(tcp->outf) == EOF)
1907 return -1;
1908 tcp->flags &= ~TCB_INSYSCALL;
1909 return 0;
1910 }
1911
1912 /* Entering system call */
1913 res = syscall_enter(tcp);
1914 if (res != 1)
1915 return res;
1916
Pavel Machekd8ae7e32000-02-01 17:17:25 +00001917 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001918#ifdef LINUX
Michal Ludvig0e035502002-09-23 15:41:01 +00001919#if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001920 case SYS_socketcall:
1921 decode_subcall(tcp, SYS_socket_subcall,
1922 SYS_socket_nsubcalls, deref_style);
1923 break;
1924 case SYS_ipc:
1925 decode_subcall(tcp, SYS_ipc_subcall,
1926 SYS_ipc_nsubcalls, shift_style);
1927 break;
Michal Ludvig0e035502002-09-23 15:41:01 +00001928#endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001929#ifdef SPARC
1930 case SYS_socketcall:
1931 sparc_socket_decode (tcp);
1932 break;
1933#endif
1934#endif /* LINUX */
1935#ifdef SVR4
1936#ifdef SYS_pgrpsys_subcall
1937 case SYS_pgrpsys:
1938 decode_subcall(tcp, SYS_pgrpsys_subcall,
1939 SYS_pgrpsys_nsubcalls, shift_style);
1940 break;
1941#endif /* SYS_pgrpsys_subcall */
1942#ifdef SYS_sigcall_subcall
1943 case SYS_sigcall:
1944 decode_subcall(tcp, SYS_sigcall_subcall,
1945 SYS_sigcall_nsubcalls, mask_style);
1946 break;
1947#endif /* SYS_sigcall_subcall */
1948 case SYS_msgsys:
1949 decode_subcall(tcp, SYS_msgsys_subcall,
1950 SYS_msgsys_nsubcalls, shift_style);
1951 break;
1952 case SYS_shmsys:
1953 decode_subcall(tcp, SYS_shmsys_subcall,
1954 SYS_shmsys_nsubcalls, shift_style);
1955 break;
1956 case SYS_semsys:
1957 decode_subcall(tcp, SYS_semsys_subcall,
1958 SYS_semsys_nsubcalls, shift_style);
1959 break;
1960#if 0 /* broken */
1961 case SYS_utssys:
1962 decode_subcall(tcp, SYS_utssys_subcall,
1963 SYS_utssys_nsubcalls, shift_style);
1964 break;
1965#endif
1966 case SYS_sysfs:
1967 decode_subcall(tcp, SYS_sysfs_subcall,
1968 SYS_sysfs_nsubcalls, shift_style);
1969 break;
1970 case SYS_spcall:
1971 decode_subcall(tcp, SYS_spcall_subcall,
1972 SYS_spcall_nsubcalls, shift_style);
1973 break;
1974#ifdef SYS_context_subcall
1975 case SYS_context:
1976 decode_subcall(tcp, SYS_context_subcall,
1977 SYS_context_nsubcalls, shift_style);
1978 break;
1979#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00001980#ifdef SYS_door_subcall
1981 case SYS_door:
1982 decode_subcall(tcp, SYS_door_subcall,
1983 SYS_door_nsubcalls, door_style);
1984 break;
1985#endif /* SYS_door_subcall */
John Hughesbdf48f52001-03-06 15:08:09 +00001986#ifdef SYS_kaio_subcall
1987 case SYS_kaio:
1988 decode_subcall(tcp, SYS_kaio_subcall,
1989 SYS_kaio_nsubcalls, shift_style);
1990 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001991#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001992#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001993#ifdef FREEBSD
1994 case SYS_msgsys:
1995 case SYS_shmsys:
1996 case SYS_semsys:
1997 decode_subcall(tcp, 0, 0, table_style);
1998 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001999#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002000#ifdef SUNOS4
2001 case SYS_semsys:
2002 decode_subcall(tcp, SYS_semsys_subcall,
2003 SYS_semsys_nsubcalls, shift_style);
2004 break;
2005 case SYS_msgsys:
2006 decode_subcall(tcp, SYS_msgsys_subcall,
2007 SYS_msgsys_nsubcalls, shift_style);
2008 break;
2009 case SYS_shmsys:
2010 decode_subcall(tcp, SYS_shmsys_subcall,
2011 SYS_shmsys_nsubcalls, shift_style);
2012 break;
2013#endif
2014 }
2015
2016 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002017 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002018 tcp->flags |= TCB_INSYSCALL;
2019 return 0;
2020 }
2021
2022 if (cflag) {
2023 gettimeofday(&tcp->etime, NULL);
2024 tcp->flags |= TCB_INSYSCALL;
2025 return 0;
2026 }
2027
2028 printleader(tcp);
2029 tcp->flags &= ~TCB_REPRINT;
2030 tcp_last = tcp;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002031 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002032 tprintf("syscall_%lu(", tcp->scno);
2033 else
2034 tprintf("%s(", sysent[tcp->scno].sys_name);
Roland McGrath761b5d72002-12-15 23:58:31 +00002035 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002036 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2037 sys_res = printargs(tcp);
2038 else
2039 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2040 if (fflush(tcp->outf) == EOF)
2041 return -1;
2042 tcp->flags |= TCB_INSYSCALL;
2043 /* Measure the entrance time as late as possible to avoid errors. */
2044 if (dtime)
2045 gettimeofday(&tcp->etime, NULL);
2046 return sys_res;
2047}
2048
2049int
2050printargs(tcp)
2051struct tcb *tcp;
2052{
2053 if (entering(tcp)) {
2054 int i;
2055
2056 for (i = 0; i < tcp->u_nargs; i++)
2057 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2058 }
2059 return 0;
2060}
2061
2062long
2063getrval2(tcp)
2064struct tcb *tcp;
2065{
2066 long val = -1;
2067
2068#ifdef LINUX
2069#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002070 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002071 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
2072 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002073 val = regs.r_o1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002074#endif /* SPARC */
2075#endif /* LINUX */
2076
2077#ifdef SUNOS4
2078 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2079 return -1;
2080#endif /* SUNOS4 */
2081
2082#ifdef SVR4
2083#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002084 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002085#endif /* SPARC */
2086#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002087 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002088#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00002089#ifdef X86_64
2090 val = tcp->status.PR_REG[RDX];
2091#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002092#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002093 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002094#endif /* MIPS */
2095#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002096#ifdef FREEBSD
2097 struct reg regs;
2098 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
2099 val = regs.r_edx;
Roland McGrath761b5d72002-12-15 23:58:31 +00002100#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002101 return val;
2102}
2103
2104/*
2105 * Apparently, indirect system calls have already be converted by ptrace(2),
2106 * so if you see "indir" this program has gone astray.
2107 */
2108int
2109sys_indir(tcp)
2110struct tcb *tcp;
2111{
2112 int i, scno, nargs;
2113
2114 if (entering(tcp)) {
2115 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2116 fprintf(stderr, "Bogus syscall: %u\n", scno);
2117 return 0;
2118 }
2119 nargs = sysent[scno].nargs;
2120 tprintf("%s", sysent[scno].sys_name);
2121 for (i = 0; i < nargs; i++)
2122 tprintf(", %#lx", tcp->u_arg[i+1]);
2123 }
2124 return 0;
2125}
2126
2127static int
2128time_cmp(a, b)
2129void *a;
2130void *b;
2131{
2132 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2133}
2134
2135static int
2136syscall_cmp(a, b)
2137void *a;
2138void *b;
2139{
2140 return strcmp(sysent[*((int *) a)].sys_name,
2141 sysent[*((int *) b)].sys_name);
2142}
2143
2144static int
2145count_cmp(a, b)
2146void *a;
2147void *b;
2148{
2149 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2150
2151 return (m < n) ? 1 : (m > n) ? -1 : 0;
2152}
2153
2154static int (*sortfun)();
2155static struct timeval overhead = { -1, -1 };
2156
2157void
2158set_sortby(sortby)
2159char *sortby;
2160{
2161 if (strcmp(sortby, "time") == 0)
2162 sortfun = time_cmp;
2163 else if (strcmp(sortby, "calls") == 0)
2164 sortfun = count_cmp;
2165 else if (strcmp(sortby, "name") == 0)
2166 sortfun = syscall_cmp;
2167 else if (strcmp(sortby, "nothing") == 0)
2168 sortfun = NULL;
2169 else {
2170 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2171 exit(1);
2172 }
2173}
2174
2175void set_overhead(n)
2176int n;
2177{
2178 overhead.tv_sec = n / 1000000;
2179 overhead.tv_usec = n % 1000000;
2180}
2181
2182void
2183call_summary(outf)
2184FILE *outf;
2185{
2186 int i, j;
2187 int call_cum, error_cum;
2188 struct timeval tv_cum, dtv;
2189 double percent;
2190 char *dashes = "-------------------------";
2191 char error_str[16];
2192
2193 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2194 if (overhead.tv_sec == -1) {
2195 tv_mul(&overhead, &shortest, 8);
2196 tv_div(&overhead, &overhead, 10);
2197 }
2198 for (i = 0; i < nsyscalls; i++) {
2199 sorted_count[i] = i;
2200 if (call_count[i] == 0)
2201 continue;
2202 tv_mul(&dtv, &overhead, call_count[i]);
2203 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2204 call_cum += call_count[i];
2205 error_cum += error_count[i];
2206 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2207 }
2208 if (sortfun)
2209 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2210 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2211 "% time", "seconds", "usecs/call",
2212 "calls", "errors", "syscall");
2213 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2214 dashes, dashes, dashes, dashes, dashes, dashes);
2215 for (i = 0; i < nsyscalls; i++) {
2216 j = sorted_count[i];
2217 if (call_count[j] == 0)
2218 continue;
2219 tv_div(&dtv, &tv_count[j], call_count[j]);
2220 if (error_count[j])
2221 sprintf(error_str, "%d", error_count[j]);
2222 else
2223 error_str[0] = '\0';
2224 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2225 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2226 percent, (long) tv_count[j].tv_sec,
2227 (long) tv_count[j].tv_usec,
2228 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2229 call_count[j], error_str, sysent[j].sys_name);
2230 }
2231 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2232 dashes, dashes, dashes, dashes, dashes, dashes);
2233 if (error_cum)
2234 sprintf(error_str, "%d", error_cum);
2235 else
2236 error_str[0] = '\0';
2237 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2238 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2239 call_cum, error_str, "total");
2240}