blob: dcd3504448c5f321003b2cf573e2a2399ea39e48 [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
55# undef fpu
56#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
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000074#ifndef SYS_ERRLIST_DECLARED
75extern int sys_nerr;
76extern char *sys_errlist[];
77#endif /* SYS_ERRLIST_DECLARED */
78
Pavel Machekd8ae7e32000-02-01 17:17:25 +000079#define NR_SYSCALL_BASE 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000080#ifdef LINUX
81#ifndef ERESTARTSYS
82#define ERESTARTSYS 512
83#endif
84#ifndef ERESTARTNOINTR
85#define ERESTARTNOINTR 513
86#endif
87#ifndef ERESTARTNOHAND
88#define ERESTARTNOHAND 514 /* restart if no handler.. */
89#endif
90#ifndef ENOIOCTLCMD
91#define ENOIOCTLCMD 515 /* No ioctl command */
92#endif
93#ifndef NSIG
94#define NSIG 32
95#endif
96#ifdef ARM
97#undef NSIG
98#define NSIG 32
Pavel Machekd8ae7e32000-02-01 17:17:25 +000099#undef NR_SYSCALL_BASE
100#define NR_SYSCALL_BASE __NR_SYSCALL_BASE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000101#endif
102#endif /* LINUX */
103
104#include "syscall.h"
105
106/* Define these shorthand notations to simplify the syscallent files. */
107#define TF TRACE_FILE
108#define TI TRACE_IPC
109#define TN TRACE_NETWORK
110#define TP TRACE_PROCESS
111#define TS TRACE_SIGNAL
112
113struct sysent sysent0[] = {
114#include "syscallent.h"
115};
116int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
117
118#if SUPPORTED_PERSONALITIES >= 2
119struct sysent sysent1[] = {
120#include "syscallent1.h"
121};
122int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
123#endif /* SUPPORTED_PERSONALITIES >= 2 */
124
125#if SUPPORTED_PERSONALITIES >= 3
126struct sysent sysent2[] = {
127#include "syscallent2.h"
128};
129int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
130#endif /* SUPPORTED_PERSONALITIES >= 3 */
131
132struct sysent *sysent;
133int nsyscalls;
134
135/* Now undef them since short defines cause wicked namespace pollution. */
136#undef TF
137#undef TI
138#undef TN
139#undef TP
140#undef TS
141
142char *errnoent0[] = {
143#include "errnoent.h"
144};
145int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
146
147#if SUPPORTED_PERSONALITIES >= 2
148char *errnoent1[] = {
149#include "errnoent1.h"
150};
151int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
152#endif /* SUPPORTED_PERSONALITIES >= 2 */
153
154#if SUPPORTED_PERSONALITIES >= 3
155char *errnoent2[] = {
156#include "errnoent2.h"
157};
158int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
159#endif /* SUPPORTED_PERSONALITIES >= 3 */
160
161char **errnoent;
162int nerrnos;
163
164int current_personality;
165
166int
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000167set_personality(personality)
168int personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000169{
170 switch (personality) {
171 case 0:
172 errnoent = errnoent0;
173 nerrnos = nerrnos0;
174 sysent = sysent0;
175 nsyscalls = nsyscalls0;
176 ioctlent = ioctlent0;
177 nioctlents = nioctlents0;
178 signalent = signalent0;
179 nsignals = nsignals0;
180 break;
181
182#if SUPPORTED_PERSONALITIES >= 2
183 case 1:
184 errnoent = errnoent1;
185 nerrnos = nerrnos1;
186 sysent = sysent1;
187 nsyscalls = nsyscalls1;
188 ioctlent = ioctlent1;
189 nioctlents = nioctlents1;
190 signalent = signalent1;
191 nsignals = nsignals1;
192 break;
193#endif /* SUPPORTED_PERSONALITIES >= 2 */
194
195#if SUPPORTED_PERSONALITIES >= 3
196 case 2:
197 errnoent = errnoent2;
198 nerrnos = nerrnos2;
199 sysent = sysent2;
200 nsyscalls = nsyscalls2;
201 ioctlent = ioctlent2;
202 nioctlents = nioctlents2;
203 signalent = signalent2;
204 nsignals = nsignals2;
205 break;
206#endif /* SUPPORTED_PERSONALITIES >= 3 */
207
208 default:
209 return -1;
210 }
211
212 current_personality = personality;
213 return 0;
214}
215
216int qual_flags[MAX_QUALS];
217
218static int call_count[MAX_QUALS];
219static int error_count[MAX_QUALS];
220static struct timeval tv_count[MAX_QUALS];
221static int sorted_count[MAX_QUALS];
222
223static struct timeval shortest = { 1000000, 0 };
224
225static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
226
227static struct qual_options {
228 int bitflag;
229 char *option_name;
230 int (*lookup)();
231 char *argument_name;
232} qual_options[] = {
233 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
234 { QUAL_TRACE, "t", lookup_syscall, "system call" },
235 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
236 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
237 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
238 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
239 { QUAL_RAW, "raw", lookup_syscall, "system call" },
240 { QUAL_RAW, "x", lookup_syscall, "system call" },
241 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
242 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
243 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
244 { QUAL_FAULT, "fault", lookup_fault, "fault" },
245 { QUAL_FAULT, "faults", lookup_fault, "fault" },
246 { QUAL_FAULT, "m", lookup_fault, "fault" },
247 { QUAL_READ, "read", lookup_desc, "descriptor" },
248 { QUAL_READ, "reads", lookup_desc, "descriptor" },
249 { QUAL_READ, "r", lookup_desc, "descriptor" },
250 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
251 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
252 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
253 { 0, NULL, NULL, NULL },
254};
255
256static int
257lookup_syscall(s)
258char *s;
259{
260 int i;
261
262 for (i = 0; i < nsyscalls; i++) {
263 if (strcmp(s, sysent[i].sys_name) == 0)
264 return i;
265 }
266 return -1;
267}
268
269static int
270lookup_signal(s)
271char *s;
272{
273 int i;
274 char buf[32];
275
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000276 if (s && *s && isdigit((unsigned char)*s))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000277 return atoi(s);
278 strcpy(buf, s);
279 s = buf;
280 for (i = 0; s[i]; i++)
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000281 s[i] = toupper((unsigned char)(s[i]));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000282 if (strncmp(s, "SIG", 3) == 0)
283 s += 3;
284 for (i = 0; i <= NSIG; i++) {
Nate Sammonsce780fc1999-03-29 23:23:13 +0000285 if (strcmp(s, signame(i) + 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000286 return i;
287 }
288 return -1;
289}
290
291static int
292lookup_fault(s)
293char *s;
294{
295 return -1;
296}
297
298static int
299lookup_desc(s)
300char *s;
301{
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000302 if (s && *s && isdigit((unsigned char)*s))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000303 return atoi(s);
304 return -1;
305}
306
307static int
308lookup_class(s)
309char *s;
310{
311 if (strcmp(s, "file") == 0)
312 return TRACE_FILE;
313 if (strcmp(s, "ipc") == 0)
314 return TRACE_IPC;
315 if (strcmp(s, "network") == 0)
316 return TRACE_NETWORK;
317 if (strcmp(s, "process") == 0)
318 return TRACE_PROCESS;
319 if (strcmp(s, "signal") == 0)
320 return TRACE_SIGNAL;
321 return -1;
322}
323
324void
325qualify(s)
326char *s;
327{
328 struct qual_options *opt;
329 int not;
330 char *p;
331 int i, n;
332
333 opt = &qual_options[0];
334 for (i = 0; (p = qual_options[i].option_name); i++) {
335 n = strlen(p);
336 if (strncmp(s, p, n) == 0 && s[n] == '=') {
337 opt = &qual_options[i];
338 s += n + 1;
339 break;
340 }
341 }
342 not = 0;
343 if (*s == '!') {
344 not = 1;
345 s++;
346 }
347 if (strcmp(s, "none") == 0) {
348 not = 1 - not;
349 s = "all";
350 }
351 if (strcmp(s, "all") == 0) {
352 for (i = 0; i < MAX_QUALS; i++) {
353 if (not)
354 qual_flags[i] &= ~opt->bitflag;
355 else
356 qual_flags[i] |= opt->bitflag;
357 }
358 return;
359 }
360 for (i = 0; i < MAX_QUALS; i++) {
361 if (not)
362 qual_flags[i] |= opt->bitflag;
363 else
364 qual_flags[i] &= ~opt->bitflag;
365 }
366 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
367 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
368 for (i = 0; i < MAX_QUALS; i++) {
369 if (sysent[i].sys_flags & n) {
370 if (not)
371 qual_flags[i] &= ~opt->bitflag;
372 else
373 qual_flags[i] |= opt->bitflag;
374 }
375 }
376 continue;
377 }
378 if ((n = (*opt->lookup)(p)) < 0) {
379 fprintf(stderr, "strace: invalid %s `%s'\n",
380 opt->argument_name, p);
381 exit(1);
382 }
383 if (not)
384 qual_flags[n] &= ~opt->bitflag;
385 else
386 qual_flags[n] |= opt->bitflag;
387 }
388 return;
389}
390
391static void
392dumpio(tcp)
393struct tcb *tcp;
394{
395 if (syserror(tcp))
396 return;
397 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
398 return;
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000399 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000400 case SYS_read:
401#ifdef SYS_recv
402 case SYS_recv:
403#endif
404#ifdef SYS_recvfrom
405 case SYS_recvfrom:
406#endif
407 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
408 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
409 break;
410 case SYS_write:
411#ifdef SYS_send
412 case SYS_send:
413#endif
414#ifdef SYS_sendto
415 case SYS_sendto:
416#endif
417 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
418 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
419 break;
John Hughes1d08dcf2001-07-10 13:48:44 +0000420#ifdef SYS_readv
421 case SYS_readv:
422 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
423 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
424 break;
425#endif
426#ifdef SYS_writev
427 case SYS_writev:
428
429 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
430 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
431 break;
432#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000433 }
434}
435
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000436#ifndef FREEBSD
Wichert Akkerman8829a551999-06-11 13:18:40 +0000437enum subcall_style { shift_style, deref_style, mask_style, door_style };
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000438#else /* FREEBSD */
439enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
440
441struct subcall {
442 int call;
443 int nsubcalls;
444 int subcalls[5];
445};
446
447const struct subcall subcalls_table[] = {
448 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
John Hughes61eeb552001-03-06 15:51:53 +0000449#ifdef SYS_semconfig
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000450 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
John Hughes61eeb552001-03-06 15:51:53 +0000451#else
452 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
453#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000454 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
455};
456#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000457
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000458#if !(defined(LINUX) && ( defined(ALPHA) || defined(IA64) || defined(MIPS) ))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000459
460const int socket_map [] = {
461 /* SYS_SOCKET */ 97,
462 /* SYS_BIND */ 104,
463 /* SYS_CONNECT */ 98,
464 /* SYS_LISTEN */ 106,
465 /* SYS_ACCEPT */ 99,
466 /* SYS_GETSOCKNAME */ 150,
467 /* SYS_GETPEERNAME */ 141,
468 /* SYS_SOCKETPAIR */ 135,
469 /* SYS_SEND */ 101,
470 /* SYS_RECV */ 102,
471 /* SYS_SENDTO */ 133,
472 /* SYS_RECVFROM */ 125,
473 /* SYS_SHUTDOWN */ 134,
474 /* SYS_SETSOCKOPT */ 105,
475 /* SYS_GETSOCKOPT */ 118,
476 /* SYS_SENDMSG */ 114,
477 /* SYS_RECVMSG */ 113
478};
479
480void
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000481sparc_socket_decode (tcp)
482struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000483{
484 volatile long addr;
485 volatile int i, n;
486
487 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
488 return;
489 }
490 tcp->scno = socket_map [tcp->u_arg [0]-1];
491 n = tcp->u_nargs = sysent [tcp->scno].nargs;
492 addr = tcp->u_arg [1];
493 for (i = 0; i < n; i++){
494 int arg;
495 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
496 arg = 0;
497 tcp->u_arg [i] = arg;
498 addr += sizeof (arg);
499 }
500}
501
502static void
503decode_subcall(tcp, subcall, nsubcalls, style)
504struct tcb *tcp;
505int subcall;
506int nsubcalls;
507enum subcall_style style;
508{
509 int i, addr, mask, arg;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000510
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000511 switch (style) {
512 case shift_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000513 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
514 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000515 tcp->scno = subcall + tcp->u_arg[0];
516 if (sysent[tcp->scno].nargs != -1)
517 tcp->u_nargs = sysent[tcp->scno].nargs;
518 else
519 tcp->u_nargs--;
520 for (i = 0; i < tcp->u_nargs; i++)
521 tcp->u_arg[i] = tcp->u_arg[i + 1];
522 break;
523 case deref_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000524 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
525 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000526 tcp->scno = subcall + tcp->u_arg[0];
527 addr = tcp->u_arg[1];
528 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
529 if (umove(tcp, addr, &arg) < 0)
530 arg = 0;
531 tcp->u_arg[i] = arg;
532 addr += sizeof(arg);
533 }
534 tcp->u_nargs = sysent[tcp->scno].nargs;
535 break;
536 case mask_style:
537 mask = (tcp->u_arg[0] >> 8) & 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000538 for (i = 0; mask; i++)
539 mask >>= 1;
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000540 if (i >= nsubcalls)
541 return;
542 tcp->u_arg[0] &= 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000543 tcp->scno = subcall + i;
544 if (sysent[tcp->scno].nargs != -1)
545 tcp->u_nargs = sysent[tcp->scno].nargs;
546 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000547 case door_style:
548 /*
549 * Oh, yuck. The call code is the *sixth* argument.
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000550 * (don't you mean the *last* argument? - JH)
Wichert Akkerman8829a551999-06-11 13:18:40 +0000551 */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000552 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
553 return;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000554 tcp->scno = subcall + tcp->u_arg[5];
555 if (sysent[tcp->scno].nargs != -1)
556 tcp->u_nargs = sysent[tcp->scno].nargs;
557 else
558 tcp->u_nargs--;
559 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000560#ifdef FREEBSD
561 case table_style:
562 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
563 if (subcalls_table[i].call == tcp->scno) break;
564 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
565 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
566 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
567 for (i = 0; i < tcp->u_nargs; i++)
568 tcp->u_arg[i] = tcp->u_arg[i + 1];
569 }
570 break;
571#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000572 }
573}
574#endif
575
576struct tcb *tcp_last = NULL;
577
578static int
579internal_syscall(tcp)
580struct tcb *tcp;
581{
582 /*
583 * We must always trace a few critical system calls in order to
584 * correctly support following forks in the presence of tracing
585 * qualifiers.
586 */
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000587 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000588#ifdef SYS_fork
589 case SYS_fork:
590#endif
591#ifdef SYS_vfork
592 case SYS_vfork:
593#endif
John Hughes4e36a812001-04-18 15:11:51 +0000594#ifdef SYS_fork1
595 case SYS_fork1:
596#endif
597#ifdef SYS_forkall
598 case SYS_forkall:
599#endif
600#ifdef SYS_rfork1
601 case SYS_rfork1:
602#endif
603#ifdef SYS_rforkall
604 case SYS_rforkall:
605#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000606 internal_fork(tcp);
607 break;
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000608#ifdef SYS_clone
609 case SYS_clone:
610 internal_clone(tcp);
611 break;
612#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000613#ifdef SYS_execv
614 case SYS_execv:
615#endif
616#ifdef SYS_execve
617 case SYS_execve:
618#endif
John Hughes4e36a812001-04-18 15:11:51 +0000619#ifdef SYS_rexecve
620 case SYS_rexecve:
621#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000622 internal_exec(tcp);
623 break;
624
625#ifdef SYS_wait
626 case SYS_wait:
627#endif
628#ifdef SYS_wait4
629 case SYS_wait4:
630#endif
631#ifdef SYS_waitpid
632 case SYS_waitpid:
633#endif
634#ifdef SYS_waitsys
635 case SYS_waitsys:
636#endif
637 internal_wait(tcp);
638 break;
639
640#ifdef SYS_exit
641 case SYS_exit:
642#endif
643 internal_exit(tcp);
644 break;
645 }
646 return 0;
647}
648
Wichert Akkermanc7926982000-04-10 22:22:31 +0000649
650#ifdef LINUX
651#if defined (I386)
652 static long eax;
653#elif defined (IA64)
654 long r8, r10, psr;
655 long ia32 = 0;
656#elif defined (POWERPC)
657 static long result,flags;
658#elif defined (M68K)
659 static int d0;
660#elif defined (ARM)
661 static int r0;
662#elif defined (ALPHA)
663 static long r0;
664 static long a3;
665#elif defined (SPARC)
Wichert Akkerman00a82ee2001-03-28 20:29:17 +0000666 static struct regs regs;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000667 static unsigned long trap;
668#elif defined(MIPS)
669 static long a3;
670 static long r2;
671#elif defined(S390)
672 static long gpr2;
673 static long pc;
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000674#elif defined(HPPA)
675 static long r28;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000676#endif
677#endif /* LINUX */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000678#ifdef FREEBSD
679 struct reg regs;
680#endif /* FREEBSD */
Wichert Akkermanc7926982000-04-10 22:22:31 +0000681
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000682int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000683get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000684struct tcb *tcp;
685{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000686 long scno = 0;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000687#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000688 int pid = tcp->pid;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000689#endif /* !PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000690
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000691#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000692#if defined(S390)
693 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
694 return -1;
695 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
696 if (errno)
697 return -1;
698 scno&=0xFF;
699#elif defined (POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000700 if (upeek(pid, 4*PT_R0, &scno) < 0)
701 return -1;
702 if (!(tcp->flags & TCB_INSYSCALL)) {
703 /* Check if we return from execve. */
704 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
705 tcp->flags &= ~TCB_WAITEXECVE;
706 return 0;
707 }
708 }
709#elif defined (I386)
710 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
711 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000712#elif defined(IA64)
713#define IA64_PSR_IS ((long)1 << 34)
714 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
715 ia32 = (psr & IA64_PSR_IS);
716 if (!(tcp->flags & TCB_INSYSCALL)) {
717 if (ia32) {
718 if (upeek(pid, PT_R8, &scno) < 0)
719 return -1;
720 } else {
721 if (upeek (pid, PT_R15, &scno) < 0)
722 return -1;
723 }
724 } else {
725 /* syscall in progress */
726 if (upeek (pid, PT_R8, &r8) < 0)
727 return -1;
728 if (upeek (pid, PT_R10, &r10) < 0)
729 return -1;
730 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000731#elif defined (ARM)
732 {
733 long pc;
734 upeek(pid, 4*15, &pc);
735 umoven(tcp, pc-4, 4, (char *)&scno);
736 scno &= 0x000fffff;
737 }
738#elif defined (M68K)
739 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
740 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +0000741#elif defined (MIPS)
742 if (upeek(pid, REG_A3, &a3) < 0)
743 return -1;
744
745 if(!(tcp->flags & TCB_INSYSCALL)) {
746 if (upeek(pid, REG_V0, &scno) < 0)
747 return -1;
748
749 if (scno < 0 || scno > nsyscalls) {
750 if(a3 == 0 || a3 == -1) {
751 if(debug)
752 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
753 return 0;
754 }
755 }
756 } else {
757 if (upeek(pid, REG_V0, &r2) < 0)
758 return -1;
759 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000760#elif defined (ALPHA)
761 if (upeek(pid, REG_A3, &a3) < 0)
762 return -1;
763
764 if (!(tcp->flags & TCB_INSYSCALL)) {
765 if (upeek(pid, REG_R0, &scno) < 0)
766 return -1;
767
768 /* Check if we return from execve. */
769 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
770 tcp->flags &= ~TCB_WAITEXECVE;
771 return 0;
772 }
773
774 /*
775 * Do some sanity checks to figure out if it's
776 * really a syscall entry
777 */
778 if (scno < 0 || scno > nsyscalls) {
779 if (a3 == 0 || a3 == -1) {
780 if (debug)
781 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
782 return 0;
783 }
784 }
785 }
786 else {
787 if (upeek(pid, REG_R0, &r0) < 0)
788 return -1;
789 }
790#elif defined (SPARC)
791 /* Everything we need is in the current register set. */
792 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
793 return -1;
794
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000795 /* If we are entering, then disassemble the syscall trap. */
796 if (!(tcp->flags & TCB_INSYSCALL)) {
797 /* Retrieve the syscall trap instruction. */
798 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000799 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000800 if (errno)
801 return -1;
802
803 /* Disassemble the trap to see what personality to use. */
804 switch (trap) {
805 case 0x91d02010:
806 /* Linux/SPARC syscall trap. */
807 set_personality(0);
808 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +0000809 case 0x91d0206d:
810 /* Linux/SPARC64 syscall trap. */
811 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
812 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000813 case 0x91d02000:
814 /* SunOS syscall trap. (pers 1) */
815 fprintf(stderr,"syscall: SunOS no support\n");
816 return -1;
817 case 0x91d02008:
818 /* Solaris 2.x syscall trap. (per 2) */
819 set_personality(1);
820 break;
821 case 0x91d02009:
822 /* NetBSD/FreeBSD syscall trap. */
823 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
824 return -1;
825 case 0x91d02027:
826 /* Solaris 2.x gettimeofday */
827 set_personality(1);
828 break;
829 default:
830 /* Unknown syscall trap. */
831 if(tcp->flags & TCB_WAITEXECVE) {
832 tcp->flags &= ~TCB_WAITEXECVE;
833 return 0;
834 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000835 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000836 return -1;
837 }
838
839 /* Extract the system call number from the registers. */
840 if (trap == 0x91d02027)
841 scno = 156;
842 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000843 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000844 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000845 scno = regs.r_o0;
846 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000847 }
848 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000849#elif defined(HPPA)
850 if (upeek(pid, PT_GR20, &scno) < 0)
851 return -1;
852 if (!(tcp->flags & TCB_INSYSCALL)) {
853 /* Check if we return from execve. */
854 if ((tcp->flags & TCB_WAITEXECVE)) {
855 tcp->flags &= ~TCB_WAITEXECVE;
856 return 0;
857 }
858 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000859#endif
860#endif /* LINUX */
861#ifdef SUNOS4
862 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
863 return -1;
864#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000865#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000866#ifdef HAVE_PR_SYSCALL
John Hughes25299712001-03-06 10:10:06 +0000867 scno = tcp->status.PR_SYSCALL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000868#else /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000869#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000870 scno = tcp->status.PR_WHAT;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000871#else /* FREEBSD */
872 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
873 perror("pread");
874 return -1;
875 }
876 switch (regs.r_eax) {
877 case SYS_syscall:
878 case SYS___syscall:
879 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
880 break;
881 default:
882 scno = regs.r_eax;
883 break;
884 }
885#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000886#endif /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000887#endif /* USE_PROCFS */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +0000888 if (!(tcp->flags & TCB_INSYSCALL))
889 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +0000890 return 1;
891}
892
Pavel Machek4dc3b142000-02-01 17:58:41 +0000893
894int
895syscall_fixup(tcp)
896struct tcb *tcp;
897{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000898#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +0000899 int pid = tcp->pid;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000900#else /* USE_PROCFS */
901 int scno = tcp->scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +0000902
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000903 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000904 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000905 if (
906 scno == SYS_fork
907#ifdef SYS_vfork
908 || scno == SYS_vfork
909#endif /* SYS_vfork */
John Hughes4e36a812001-04-18 15:11:51 +0000910#ifdef SYS_fork1
911 || scno == SYS_fork1
912#endif /* SYS_fork1 */
913#ifdef SYS_forkall
914 || scno == SYS_forkall
915#endif /* SYS_forkall */
916#ifdef SYS_rfork1
917 || scno == SYS_rfork1
918#endif /* SYS_fork1 */
919#ifdef SYS_rforkall
920 || scno == SYS_rforkall
921#endif /* SYS_rforkall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000922 ) {
923 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000924 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000925 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000926 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000927 }
928 else {
929 fprintf(stderr, "syscall: missing entry\n");
930 tcp->flags |= TCB_INSYSCALL;
931 }
932 }
933 }
934 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000935 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000936 fprintf(stderr, "syscall: missing exit\n");
937 tcp->flags &= ~TCB_INSYSCALL;
938 }
939 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000940#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000941#ifdef SUNOS4
942 if (!(tcp->flags & TCB_INSYSCALL)) {
943 if (scno == 0) {
944 fprintf(stderr, "syscall: missing entry\n");
945 tcp->flags |= TCB_INSYSCALL;
946 }
947 }
948 else {
949 if (scno != 0) {
950 if (debug) {
951 /*
952 * This happens when a signal handler
953 * for a signal which interrupted a
954 * a system call makes another system call.
955 */
956 fprintf(stderr, "syscall: missing exit\n");
957 }
958 tcp->flags &= ~TCB_INSYSCALL;
959 }
960 }
961#endif /* SUNOS4 */
962#ifdef LINUX
963#if defined (I386)
964 if (upeek(pid, 4*EAX, &eax) < 0)
965 return -1;
966 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
967 if (debug)
968 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
969 return 0;
970 }
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000971#elif defined (S390)
972 if (upeek(pid, PT_GPR2, &gpr2) < 0)
973 return -1;
974 if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
975 if (debug)
976 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
977 return 0;
978 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000979#elif defined (POWERPC)
980# define SO_MASK 0x10000000
981 if (upeek(pid, 4*PT_CCR, &flags) < 0)
982 return -1;
983 if (upeek(pid, 4*PT_R3, &result) < 0)
984 return -1;
985 if (flags & SO_MASK)
986 result = -result;
987#elif defined (M68K)
988 if (upeek(pid, 4*PT_D0, &d0) < 0)
989 return -1;
990 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
991 if (debug)
992 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
993 return 0;
994 }
995#elif defined (ARM)
996 if (upeek(pid, 4*0, (long *)&r0) < 0)
997 return -1;
998 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
999 if (debug)
1000 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
1001 return 0;
1002 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001003#elif defined (HPPA)
1004 if (upeek(pid, PT_GR28, &r28) < 0)
1005 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001006#endif
1007#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001008 return 1;
1009}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001010
Pavel Machek4dc3b142000-02-01 17:58:41 +00001011int
1012get_error(tcp)
1013struct tcb *tcp;
1014{
1015 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001016#ifdef LINUX
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001017#ifdef S390
1018 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1019 tcp->u_rval = -1;
1020 u_error = -gpr2;
1021 }
1022 else {
1023 tcp->u_rval = gpr2;
1024 u_error = 0;
1025 }
1026#else /* !S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001027#ifdef I386
1028 if (eax < 0 && -eax < nerrnos) {
1029 tcp->u_rval = -1;
1030 u_error = -eax;
1031 }
1032 else {
1033 tcp->u_rval = eax;
1034 u_error = 0;
1035 }
1036#else /* !I386 */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001037#ifdef IA64
1038 if (ia32) {
1039 int err;
1040
1041 err = (int)r8;
1042 if (err < 0 && -err < nerrnos) {
1043 tcp->u_rval = -1;
1044 u_error = -err;
1045 }
1046 else {
1047 tcp->u_rval = err;
1048 u_error = 0;
1049 }
1050 } else {
1051 if (r10) {
1052 tcp->u_rval = -1;
1053 u_error = r8;
1054 } else {
1055 tcp->u_rval = r8;
1056 u_error = 0;
1057 }
1058 }
1059#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001060#ifdef MIPS
1061 if (a3) {
1062 tcp->u_rval = -1;
1063 u_error = r2;
1064 } else {
1065 tcp->u_rval = r2;
1066 u_error = 0;
1067 }
1068#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001069#ifdef POWERPC
1070 if (result && (unsigned) -result < nerrnos) {
1071 tcp->u_rval = -1;
1072 u_error = -result;
1073 }
1074 else {
1075 tcp->u_rval = result;
1076 u_error = 0;
1077 }
1078#else /* !POWERPC */
1079#ifdef M68K
1080 if (d0 && (unsigned) -d0 < nerrnos) {
1081 tcp->u_rval = -1;
1082 u_error = -d0;
1083 }
1084 else {
1085 tcp->u_rval = d0;
1086 u_error = 0;
1087 }
1088#else /* !M68K */
1089#ifdef ARM
1090 if (r0 && (unsigned) -r0 < nerrnos) {
1091 tcp->u_rval = -1;
1092 u_error = -r0;
1093 }
1094 else {
1095 tcp->u_rval = r0;
1096 u_error = 0;
1097 }
1098#else /* !ARM */
1099#ifdef ALPHA
1100 if (a3) {
1101 tcp->u_rval = -1;
1102 u_error = r0;
1103 }
1104 else {
1105 tcp->u_rval = r0;
1106 u_error = 0;
1107 }
1108#else /* !ALPHA */
1109#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001110 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001111 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001112 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001113 }
1114 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001115 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001116 u_error = 0;
1117 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001118#else /* !SPARC */
1119#ifdef HPPA
1120 if (r28 && (unsigned) -r28 < nerrnos) {
1121 tcp->u_rval = -1;
1122 u_error = -r28;
1123 }
1124 else {
1125 tcp->u_rval = r28;
1126 u_error = 0;
1127 }
1128#endif /* HPPA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001129#endif /* SPARC */
1130#endif /* ALPHA */
1131#endif /* ARM */
1132#endif /* M68K */
1133#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001134#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001135#endif /* IA64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001136#endif /* I386 */
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001137#endif /* S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001138#endif /* LINUX */
1139#ifdef SUNOS4
1140 /* get error code from user struct */
1141 if (upeek(pid, uoff(u_error), &u_error) < 0)
1142 return -1;
1143 u_error >>= 24; /* u_error is a char */
1144
1145 /* get system call return value */
1146 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1147 return -1;
1148#endif /* SUNOS4 */
1149#ifdef SVR4
1150#ifdef SPARC
1151 /* Judicious guessing goes a long way. */
1152 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1153 tcp->u_rval = -1;
1154 u_error = tcp->status.pr_reg[R_O0];
1155 }
1156 else {
1157 tcp->u_rval = tcp->status.pr_reg[R_O0];
1158 u_error = 0;
1159 }
1160#endif /* SPARC */
1161#ifdef I386
1162 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001163 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001164 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001165 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001166 }
1167 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001168 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001169#ifdef HAVE_LONG_LONG
1170 tcp->u_lrval =
1171 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1172 tcp->status.PR_REG[EAX];
1173#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001174 u_error = 0;
1175 }
1176#endif /* I386 */
1177#ifdef MIPS
1178 if (tcp->status.pr_reg[CTX_A3]) {
1179 tcp->u_rval = -1;
1180 u_error = tcp->status.pr_reg[CTX_V0];
1181 }
1182 else {
1183 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1184 u_error = 0;
1185 }
1186#endif /* MIPS */
1187#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001188#ifdef FREEBSD
1189 if (regs.r_eflags & PSL_C) {
1190 tcp->u_rval = -1;
1191 u_error = regs.r_eax;
1192 } else {
1193 tcp->u_rval = regs.r_eax;
1194 tcp->u_lrval =
1195 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1196 u_error = 0;
1197 }
1198#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001199 tcp->u_error = u_error;
1200 return 1;
1201}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001202
Pavel Machek4dc3b142000-02-01 17:58:41 +00001203int syscall_enter(tcp)
1204struct tcb *tcp;
1205{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001206#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001207 int pid = tcp->pid;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001208#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001209#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001210#if defined(S390)
1211 {
1212 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001213 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1214 tcp->u_nargs = sysent[tcp->scno].nargs;
1215 else
1216 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001217 for (i = 0; i < tcp->u_nargs; i++) {
1218 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1219 return -1;
1220 }
1221 }
1222#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001223 {
1224 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001225 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1226 tcp->u_nargs = sysent[tcp->scno].nargs;
1227 else
1228 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001229 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001230 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1231 * for scno somewhere above here!
1232 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001233 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1234 return -1;
1235 }
1236 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001237#elif defined (IA64)
1238 {
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001239 unsigned long *bsp, cfm, i;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001240
1241 if (upeek(pid, PT_AR_BSP, (long *) &bsp) < 0)
1242 return -1;
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001243 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1244 return -1;
1245
1246 bsp = ia64_rse_skip_regs(bsp, -(cfm & 0x7f));
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001247
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001248 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1249 tcp->u_nargs = sysent[tcp->scno].nargs;
1250 else
1251 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001252 for (i = 0; i < tcp->u_nargs; ++i) {
1253 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(bsp, i), sizeof(long),
1254 (char *) &tcp->u_arg[i])
1255 < 0)
1256 return -1;
1257 }
1258 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001259#elif defined (MIPS)
1260 {
1261 long sp;
1262 int i, nargs;
1263
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001264 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1265 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1266 else
1267 nargs = tcp->u_nargs = MAX_ARGS;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001268 if(nargs > 4) {
1269 if(upeek(pid, REG_SP, &sp) < 0)
1270 return -1;
1271 for(i = 0; i < 4; i++) {
1272 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1273 return -1;
1274 }
1275 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1276 (char *)(tcp->u_arg + 4));
1277 } else {
1278 for(i = 0; i < nargs; i++) {
1279 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1280 return -1;
1281 }
1282 }
1283 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001284#elif defined (POWERPC)
1285 {
1286 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001287 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1288 tcp->u_nargs = sysent[tcp->scno].nargs;
1289 else
1290 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001291 for (i = 0; i < tcp->u_nargs; i++) {
1292 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1293 return -1;
1294 }
1295 }
1296#elif defined (SPARC)
1297 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001298 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001299
1300 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1301 tcp->u_nargs = sysent[tcp->scno].nargs;
1302 else
1303 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001304 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001305 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001306 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001307#elif defined (HPPA)
1308 {
1309 int i;
1310
1311 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1312 tcp->u_nargs = sysent[tcp->scno].nargs;
1313 else
1314 tcp->u_nargs = MAX_ARGS;
1315 for (i = 0; i < tcp->u_nargs; i++) {
1316 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1317 return -1;
1318 }
1319 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001320#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001321 {
1322 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001323 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1324 tcp->u_nargs = sysent[tcp->scno].nargs;
1325 else
1326 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001327 for (i = 0; i < tcp->u_nargs; i++) {
1328 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1329 return -1;
1330 }
1331 }
1332#endif
1333#endif /* LINUX */
1334#ifdef SUNOS4
1335 {
1336 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001337 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1338 tcp->u_nargs = sysent[tcp->scno].nargs;
1339 else
1340 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001341 for (i = 0; i < tcp->u_nargs; i++) {
1342 struct user *u;
1343
1344 if (upeek(pid, uoff(u_arg[0]) +
1345 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1346 return -1;
1347 }
1348 }
1349#endif /* SUNOS4 */
1350#ifdef SVR4
1351#ifdef MIPS
1352 /*
1353 * SGI is broken: even though it has pr_sysarg, it doesn't
1354 * set them on system call entry. Get a clue.
1355 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001356 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001357 tcp->u_nargs = sysent[tcp->scno].nargs;
1358 else
1359 tcp->u_nargs = tcp->status.pr_nsysarg;
1360 if (tcp->u_nargs > 4) {
1361 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1362 4*sizeof(tcp->u_arg[0]));
1363 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1364 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1365 }
1366 else {
1367 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1368 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1369 }
John Hughes25299712001-03-06 10:10:06 +00001370#elif UNIXWARE >= 2
1371 /*
1372 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1373 */
1374 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1375 tcp->u_nargs = sysent[tcp->scno].nargs;
1376 else
1377 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1378 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1379 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1380#elif defined (HAVE_PR_SYSCALL)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001381 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001382 tcp->u_nargs = sysent[tcp->scno].nargs;
1383 else
1384 tcp->u_nargs = tcp->status.pr_nsysarg;
1385 {
1386 int i;
1387 for (i = 0; i < tcp->u_nargs; i++)
1388 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1389 }
John Hughes25299712001-03-06 10:10:06 +00001390#elif defined (I386)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001391 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001392 tcp->u_nargs = sysent[tcp->scno].nargs;
1393 else
1394 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001395 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001396 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
John Hughes25299712001-03-06 10:10:06 +00001397#else
1398 I DONT KNOW WHAT TO DO
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001399#endif /* !HAVE_PR_SYSCALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001400#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001401#ifdef FREEBSD
1402 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1403 sysent[tcp->scno].nargs > tcp->status.val)
1404 tcp->u_nargs = sysent[tcp->scno].nargs;
1405 else
1406 tcp->u_nargs = tcp->status.val;
1407 if (tcp->u_nargs < 0)
1408 tcp->u_nargs = 0;
1409 if (tcp->u_nargs > MAX_ARGS)
1410 tcp->u_nargs = MAX_ARGS;
1411 switch(regs.r_eax) {
1412 case SYS___syscall:
1413 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1414 regs.r_esp + sizeof(int) + sizeof(quad_t));
1415 break;
1416 case SYS_syscall:
1417 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1418 regs.r_esp + 2 * sizeof(int));
1419 break;
1420 default:
1421 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1422 regs.r_esp + sizeof(int));
1423 break;
1424 }
1425#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001426 return 1;
1427}
1428
1429int
1430trace_syscall(tcp)
1431struct tcb *tcp;
1432{
1433 int sys_res;
1434 struct timeval tv;
1435 int res;
1436
1437 /* Measure the exit time as early as possible to avoid errors. */
1438 if (dtime && (tcp->flags & TCB_INSYSCALL))
1439 gettimeofday(&tv, NULL);
1440
1441 res = get_scno(tcp);
1442 if (res != 1)
1443 return res;
1444
1445 res = syscall_fixup(tcp);
1446 if (res != 1)
1447 return res;
1448
1449 if (tcp->flags & TCB_INSYSCALL) {
1450 long u_error;
1451 res = get_error(tcp);
1452 if (res != 1)
1453 return res;
1454 u_error = tcp->u_error;
1455
1456
1457 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001458 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1459 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001460 tcp->flags &= ~TCB_INSYSCALL;
1461 return 0;
1462 }
1463
1464 if (tcp->flags & TCB_REPRINT) {
1465 printleader(tcp);
1466 tprintf("<... ");
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001467 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001468 tprintf("syscall_%lu", tcp->scno);
1469 else
1470 tprintf("%s", sysent[tcp->scno].sys_name);
1471 tprintf(" resumed> ");
1472 }
1473
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001474 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001475 call_count[tcp->scno]++;
1476 if (tcp->u_error)
1477 error_count[tcp->scno]++;
1478 tv_sub(&tv, &tv, &tcp->etime);
1479#ifdef LINUX
1480 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1481 static struct timeval one_tick =
1482 { 0, 1000000 / HZ };
1483
1484 if (tv_nz(&tcp->dtime))
1485 tv = tcp->dtime;
1486 else if (tv_cmp(&tv, &one_tick) > 0) {
1487 if (tv_cmp(&shortest, &one_tick) < 0)
1488 tv = shortest;
1489 else
1490 tv = one_tick;
1491 }
1492 }
1493#endif /* LINUX */
1494 if (tv_cmp(&tv, &shortest) < 0)
1495 shortest = tv;
1496 tv_add(&tv_count[tcp->scno],
1497 &tv_count[tcp->scno], &tv);
1498 tcp->flags &= ~TCB_INSYSCALL;
1499 return 0;
1500 }
1501
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001502 if (tcp->scno >= nsyscalls || tcp->scno < 0
Pavel Machek4dc3b142000-02-01 17:58:41 +00001503 || (qual_flags[tcp->scno] & QUAL_RAW))
1504 sys_res = printargs(tcp);
1505 else
1506 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1507 u_error = tcp->u_error;
1508 tprintf(") ");
1509 tabto(acolumn);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001510 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1511 qual_flags[tcp->scno] & QUAL_RAW) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001512 if (u_error)
1513 tprintf("= -1 (errno %ld)", u_error);
1514 else
1515 tprintf("= %#lx", tcp->u_rval);
1516 }
1517 else if (!(sys_res & RVAL_NONE) && u_error) {
1518 switch (u_error) {
1519#ifdef LINUX
1520 case ERESTARTSYS:
1521 tprintf("= ? ERESTARTSYS (To be restarted)");
1522 break;
1523 case ERESTARTNOINTR:
1524 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1525 break;
1526 case ERESTARTNOHAND:
1527 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1528 break;
1529#endif /* LINUX */
1530 default:
1531 tprintf("= -1 ");
1532 if (u_error < nerrnos && u_error < sys_nerr)
1533 tprintf("%s (%s)", errnoent[u_error],
1534 sys_errlist[u_error]);
1535 else if (u_error < nerrnos)
1536 tprintf("%s (errno %ld)",
1537 errnoent[u_error], u_error);
1538 else if (u_error < sys_nerr)
1539 tprintf("ERRNO_%ld (%s)", u_error,
1540 sys_errlist[u_error]);
1541 else
1542 tprintf("E??? (errno %ld)", u_error);
1543 break;
1544 }
1545 }
1546 else {
1547 if (sys_res & RVAL_NONE)
1548 tprintf("= ?");
1549 else {
1550 switch (sys_res & RVAL_MASK) {
1551 case RVAL_HEX:
1552 tprintf("= %#lx", tcp->u_rval);
1553 break;
1554 case RVAL_OCTAL:
1555 tprintf("= %#lo", tcp->u_rval);
1556 break;
1557 case RVAL_UDECIMAL:
1558 tprintf("= %lu", tcp->u_rval);
1559 break;
1560 case RVAL_DECIMAL:
1561 tprintf("= %ld", tcp->u_rval);
1562 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001563#ifdef HAVE_LONG_LONG
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001564 case RVAL_LHEX:
1565 tprintf("= %#llx", tcp->u_lrval);
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001566 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001567 case RVAL_LOCTAL:
1568 tprintf("= %#llo", tcp->u_lrval);
1569 break;
1570 case RVAL_LUDECIMAL:
1571 tprintf("= %llu", tcp->u_lrval);
1572 break;
1573 case RVAL_LDECIMAL:
1574 tprintf("= %lld", tcp->u_lrval);
1575 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001576#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001577 default:
1578 fprintf(stderr,
1579 "invalid rval format\n");
1580 break;
1581 }
1582 }
1583 if ((sys_res & RVAL_STR) && tcp->auxstr)
1584 tprintf(" (%s)", tcp->auxstr);
1585 }
1586 if (dtime) {
1587 tv_sub(&tv, &tv, &tcp->etime);
1588 tprintf(" <%ld.%06ld>",
1589 (long) tv.tv_sec, (long) tv.tv_usec);
1590 }
1591 printtrailer(tcp);
1592
1593 dumpio(tcp);
1594 if (fflush(tcp->outf) == EOF)
1595 return -1;
1596 tcp->flags &= ~TCB_INSYSCALL;
1597 return 0;
1598 }
1599
1600 /* Entering system call */
1601 res = syscall_enter(tcp);
1602 if (res != 1)
1603 return res;
1604
Pavel Machekd8ae7e32000-02-01 17:17:25 +00001605 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001606#ifdef LINUX
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001607#if !defined (ALPHA) && !defined(IA64) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001608 case SYS_socketcall:
1609 decode_subcall(tcp, SYS_socket_subcall,
1610 SYS_socket_nsubcalls, deref_style);
1611 break;
1612 case SYS_ipc:
1613 decode_subcall(tcp, SYS_ipc_subcall,
1614 SYS_ipc_nsubcalls, shift_style);
1615 break;
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001616#endif /* !ALPHA && !IA64 && !MIPS && !SPARC */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001617#ifdef SPARC
1618 case SYS_socketcall:
1619 sparc_socket_decode (tcp);
1620 break;
1621#endif
1622#endif /* LINUX */
1623#ifdef SVR4
1624#ifdef SYS_pgrpsys_subcall
1625 case SYS_pgrpsys:
1626 decode_subcall(tcp, SYS_pgrpsys_subcall,
1627 SYS_pgrpsys_nsubcalls, shift_style);
1628 break;
1629#endif /* SYS_pgrpsys_subcall */
1630#ifdef SYS_sigcall_subcall
1631 case SYS_sigcall:
1632 decode_subcall(tcp, SYS_sigcall_subcall,
1633 SYS_sigcall_nsubcalls, mask_style);
1634 break;
1635#endif /* SYS_sigcall_subcall */
1636 case SYS_msgsys:
1637 decode_subcall(tcp, SYS_msgsys_subcall,
1638 SYS_msgsys_nsubcalls, shift_style);
1639 break;
1640 case SYS_shmsys:
1641 decode_subcall(tcp, SYS_shmsys_subcall,
1642 SYS_shmsys_nsubcalls, shift_style);
1643 break;
1644 case SYS_semsys:
1645 decode_subcall(tcp, SYS_semsys_subcall,
1646 SYS_semsys_nsubcalls, shift_style);
1647 break;
1648#if 0 /* broken */
1649 case SYS_utssys:
1650 decode_subcall(tcp, SYS_utssys_subcall,
1651 SYS_utssys_nsubcalls, shift_style);
1652 break;
1653#endif
1654 case SYS_sysfs:
1655 decode_subcall(tcp, SYS_sysfs_subcall,
1656 SYS_sysfs_nsubcalls, shift_style);
1657 break;
1658 case SYS_spcall:
1659 decode_subcall(tcp, SYS_spcall_subcall,
1660 SYS_spcall_nsubcalls, shift_style);
1661 break;
1662#ifdef SYS_context_subcall
1663 case SYS_context:
1664 decode_subcall(tcp, SYS_context_subcall,
1665 SYS_context_nsubcalls, shift_style);
1666 break;
1667#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00001668#ifdef SYS_door_subcall
1669 case SYS_door:
1670 decode_subcall(tcp, SYS_door_subcall,
1671 SYS_door_nsubcalls, door_style);
1672 break;
1673#endif /* SYS_door_subcall */
John Hughesbdf48f52001-03-06 15:08:09 +00001674#ifdef SYS_kaio_subcall
1675 case SYS_kaio:
1676 decode_subcall(tcp, SYS_kaio_subcall,
1677 SYS_kaio_nsubcalls, shift_style);
1678 break;
1679#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001680#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001681#ifdef FREEBSD
1682 case SYS_msgsys:
1683 case SYS_shmsys:
1684 case SYS_semsys:
1685 decode_subcall(tcp, 0, 0, table_style);
1686 break;
1687#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001688#ifdef SUNOS4
1689 case SYS_semsys:
1690 decode_subcall(tcp, SYS_semsys_subcall,
1691 SYS_semsys_nsubcalls, shift_style);
1692 break;
1693 case SYS_msgsys:
1694 decode_subcall(tcp, SYS_msgsys_subcall,
1695 SYS_msgsys_nsubcalls, shift_style);
1696 break;
1697 case SYS_shmsys:
1698 decode_subcall(tcp, SYS_shmsys_subcall,
1699 SYS_shmsys_nsubcalls, shift_style);
1700 break;
1701#endif
1702 }
1703
1704 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001705 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001706 tcp->flags |= TCB_INSYSCALL;
1707 return 0;
1708 }
1709
1710 if (cflag) {
1711 gettimeofday(&tcp->etime, NULL);
1712 tcp->flags |= TCB_INSYSCALL;
1713 return 0;
1714 }
1715
1716 printleader(tcp);
1717 tcp->flags &= ~TCB_REPRINT;
1718 tcp_last = tcp;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001719 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001720 tprintf("syscall_%lu(", tcp->scno);
1721 else
1722 tprintf("%s(", sysent[tcp->scno].sys_name);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001723 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001724 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1725 sys_res = printargs(tcp);
1726 else
1727 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1728 if (fflush(tcp->outf) == EOF)
1729 return -1;
1730 tcp->flags |= TCB_INSYSCALL;
1731 /* Measure the entrance time as late as possible to avoid errors. */
1732 if (dtime)
1733 gettimeofday(&tcp->etime, NULL);
1734 return sys_res;
1735}
1736
1737int
1738printargs(tcp)
1739struct tcb *tcp;
1740{
1741 if (entering(tcp)) {
1742 int i;
1743
1744 for (i = 0; i < tcp->u_nargs; i++)
1745 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1746 }
1747 return 0;
1748}
1749
1750long
1751getrval2(tcp)
1752struct tcb *tcp;
1753{
1754 long val = -1;
1755
1756#ifdef LINUX
1757#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001758 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001759 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
1760 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001761 val = regs.r_o1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001762#endif /* SPARC */
1763#endif /* LINUX */
1764
1765#ifdef SUNOS4
1766 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1767 return -1;
1768#endif /* SUNOS4 */
1769
1770#ifdef SVR4
1771#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001772 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001773#endif /* SPARC */
1774#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001775 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001776#endif /* I386 */
1777#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001778 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001779#endif /* MIPS */
1780#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001781#ifdef FREEBSD
1782 struct reg regs;
1783 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
1784 val = regs.r_edx;
1785#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001786 return val;
1787}
1788
1789/*
1790 * Apparently, indirect system calls have already be converted by ptrace(2),
1791 * so if you see "indir" this program has gone astray.
1792 */
1793int
1794sys_indir(tcp)
1795struct tcb *tcp;
1796{
1797 int i, scno, nargs;
1798
1799 if (entering(tcp)) {
1800 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1801 fprintf(stderr, "Bogus syscall: %u\n", scno);
1802 return 0;
1803 }
1804 nargs = sysent[scno].nargs;
1805 tprintf("%s", sysent[scno].sys_name);
1806 for (i = 0; i < nargs; i++)
1807 tprintf(", %#lx", tcp->u_arg[i+1]);
1808 }
1809 return 0;
1810}
1811
1812static int
1813time_cmp(a, b)
1814void *a;
1815void *b;
1816{
1817 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1818}
1819
1820static int
1821syscall_cmp(a, b)
1822void *a;
1823void *b;
1824{
1825 return strcmp(sysent[*((int *) a)].sys_name,
1826 sysent[*((int *) b)].sys_name);
1827}
1828
1829static int
1830count_cmp(a, b)
1831void *a;
1832void *b;
1833{
1834 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1835
1836 return (m < n) ? 1 : (m > n) ? -1 : 0;
1837}
1838
1839static int (*sortfun)();
1840static struct timeval overhead = { -1, -1 };
1841
1842void
1843set_sortby(sortby)
1844char *sortby;
1845{
1846 if (strcmp(sortby, "time") == 0)
1847 sortfun = time_cmp;
1848 else if (strcmp(sortby, "calls") == 0)
1849 sortfun = count_cmp;
1850 else if (strcmp(sortby, "name") == 0)
1851 sortfun = syscall_cmp;
1852 else if (strcmp(sortby, "nothing") == 0)
1853 sortfun = NULL;
1854 else {
1855 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1856 exit(1);
1857 }
1858}
1859
1860void set_overhead(n)
1861int n;
1862{
1863 overhead.tv_sec = n / 1000000;
1864 overhead.tv_usec = n % 1000000;
1865}
1866
1867void
1868call_summary(outf)
1869FILE *outf;
1870{
1871 int i, j;
1872 int call_cum, error_cum;
1873 struct timeval tv_cum, dtv;
1874 double percent;
1875 char *dashes = "-------------------------";
1876 char error_str[16];
1877
1878 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1879 if (overhead.tv_sec == -1) {
1880 tv_mul(&overhead, &shortest, 8);
1881 tv_div(&overhead, &overhead, 10);
1882 }
1883 for (i = 0; i < nsyscalls; i++) {
1884 sorted_count[i] = i;
1885 if (call_count[i] == 0)
1886 continue;
1887 tv_mul(&dtv, &overhead, call_count[i]);
1888 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1889 call_cum += call_count[i];
1890 error_cum += error_count[i];
1891 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1892 }
1893 if (sortfun)
1894 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1895 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1896 "% time", "seconds", "usecs/call",
1897 "calls", "errors", "syscall");
1898 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1899 dashes, dashes, dashes, dashes, dashes, dashes);
1900 for (i = 0; i < nsyscalls; i++) {
1901 j = sorted_count[i];
1902 if (call_count[j] == 0)
1903 continue;
1904 tv_div(&dtv, &tv_count[j], call_count[j]);
1905 if (error_count[j])
1906 sprintf(error_str, "%d", error_count[j]);
1907 else
1908 error_str[0] = '\0';
1909 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1910 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1911 percent, (long) tv_count[j].tv_sec,
1912 (long) tv_count[j].tv_usec,
1913 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1914 call_count[j], error_str, sysent[j].sys_name);
1915 }
1916 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1917 dashes, dashes, dashes, dashes, dashes, dashes);
1918 if (error_cum)
1919 sprintf(error_str, "%d", error_cum);
1920 else
1921 error_str[0] = '\0';
1922 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1923 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1924 call_cum, error_str, "total");
1925}