blob: 1f98de012d4e33c115e9703c2e0026d38439077b [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 Akkerman7b3346b2001-10-09 23:47:38 +0000458#if !(defined(LINUX) && ( defined(ALPHA) || 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
Michal Ludvig0e035502002-09-23 15:41:01 +0000502void
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000503decode_subcall(tcp, subcall, nsubcalls, style)
504struct tcb *tcp;
505int subcall;
506int nsubcalls;
507enum subcall_style style;
508{
Michal Ludvig10a88d02002-10-07 14:31:00 +0000509 long addr, mask, arg;
510 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000511
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000512 switch (style) {
513 case shift_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000514 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
515 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000516 tcp->scno = subcall + tcp->u_arg[0];
517 if (sysent[tcp->scno].nargs != -1)
518 tcp->u_nargs = sysent[tcp->scno].nargs;
519 else
520 tcp->u_nargs--;
521 for (i = 0; i < tcp->u_nargs; i++)
522 tcp->u_arg[i] = tcp->u_arg[i + 1];
523 break;
524 case deref_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000525 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
526 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000527 tcp->scno = subcall + tcp->u_arg[0];
528 addr = tcp->u_arg[1];
529 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
530 if (umove(tcp, addr, &arg) < 0)
531 arg = 0;
532 tcp->u_arg[i] = arg;
533 addr += sizeof(arg);
534 }
535 tcp->u_nargs = sysent[tcp->scno].nargs;
536 break;
537 case mask_style:
538 mask = (tcp->u_arg[0] >> 8) & 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000539 for (i = 0; mask; i++)
540 mask >>= 1;
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000541 if (i >= nsubcalls)
542 return;
543 tcp->u_arg[0] &= 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000544 tcp->scno = subcall + i;
545 if (sysent[tcp->scno].nargs != -1)
546 tcp->u_nargs = sysent[tcp->scno].nargs;
547 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000548 case door_style:
549 /*
550 * Oh, yuck. The call code is the *sixth* argument.
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000551 * (don't you mean the *last* argument? - JH)
Wichert Akkerman8829a551999-06-11 13:18:40 +0000552 */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000553 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
554 return;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000555 tcp->scno = subcall + tcp->u_arg[5];
556 if (sysent[tcp->scno].nargs != -1)
557 tcp->u_nargs = sysent[tcp->scno].nargs;
558 else
559 tcp->u_nargs--;
560 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000561#ifdef FREEBSD
562 case table_style:
563 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
564 if (subcalls_table[i].call == tcp->scno) break;
565 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
566 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
567 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
568 for (i = 0; i < tcp->u_nargs; i++)
569 tcp->u_arg[i] = tcp->u_arg[i + 1];
570 }
571 break;
572#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000573 }
574}
575#endif
576
577struct tcb *tcp_last = NULL;
578
579static int
580internal_syscall(tcp)
581struct tcb *tcp;
582{
583 /*
584 * We must always trace a few critical system calls in order to
585 * correctly support following forks in the presence of tracing
586 * qualifiers.
587 */
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000588 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000589#ifdef SYS_fork
590 case SYS_fork:
591#endif
592#ifdef SYS_vfork
593 case SYS_vfork:
594#endif
John Hughes4e36a812001-04-18 15:11:51 +0000595#ifdef SYS_fork1
596 case SYS_fork1:
597#endif
598#ifdef SYS_forkall
599 case SYS_forkall:
600#endif
601#ifdef SYS_rfork1
602 case SYS_rfork1:
603#endif
604#ifdef SYS_rforkall
605 case SYS_rforkall:
606#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000607 internal_fork(tcp);
608 break;
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000609#ifdef SYS_clone
610 case SYS_clone:
611 internal_clone(tcp);
612 break;
613#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000614#ifdef SYS_clone2
615 case SYS_clone2:
616 internal_clone(tcp);
617 break;
618#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000619#ifdef SYS_execv
620 case SYS_execv:
621#endif
622#ifdef SYS_execve
623 case SYS_execve:
624#endif
John Hughes4e36a812001-04-18 15:11:51 +0000625#ifdef SYS_rexecve
626 case SYS_rexecve:
627#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000628 internal_exec(tcp);
629 break;
630
631#ifdef SYS_wait
632 case SYS_wait:
633#endif
634#ifdef SYS_wait4
635 case SYS_wait4:
636#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000637#ifdef SYS32_wait4
638 case SYS32_wait4:
639#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000640#ifdef SYS_waitpid
641 case SYS_waitpid:
642#endif
643#ifdef SYS_waitsys
644 case SYS_waitsys:
645#endif
646 internal_wait(tcp);
647 break;
648
649#ifdef SYS_exit
650 case SYS_exit:
651#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000652#ifdef SYS32_exit
653 case SYS32_exit:
654#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000655 internal_exit(tcp);
656 break;
657 }
658 return 0;
659}
660
Wichert Akkermanc7926982000-04-10 22:22:31 +0000661
662#ifdef LINUX
663#if defined (I386)
664 static long eax;
665#elif defined (IA64)
666 long r8, r10, psr;
667 long ia32 = 0;
668#elif defined (POWERPC)
669 static long result,flags;
670#elif defined (M68K)
671 static int d0;
672#elif defined (ARM)
673 static int r0;
674#elif defined (ALPHA)
675 static long r0;
676 static long a3;
677#elif defined (SPARC)
Wichert Akkerman00a82ee2001-03-28 20:29:17 +0000678 static struct regs regs;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000679 static unsigned long trap;
680#elif defined(MIPS)
681 static long a3;
682 static long r2;
Michal Ludvig10a88d02002-10-07 14:31:00 +0000683#elif defined(S390) || defined(S390X)
Wichert Akkermanc7926982000-04-10 22:22:31 +0000684 static long gpr2;
685 static long pc;
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000686#elif defined(HPPA)
687 static long r28;
Wichert Akkermanccef6372002-05-01 16:39:22 +0000688#elif defined(SH)
689 static long r0;
Michal Ludvig0e035502002-09-23 15:41:01 +0000690#elif defined(X86_64)
691 static long rax;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000692#endif
693#endif /* LINUX */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000694#ifdef FREEBSD
695 struct reg regs;
696#endif /* FREEBSD */
Wichert Akkermanc7926982000-04-10 22:22:31 +0000697
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000698int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000699get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000700struct tcb *tcp;
701{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000702 long scno = 0;
Michal Ludvig0e035502002-09-23 15:41:01 +0000703 static int currpers=-1;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000704#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000705 int pid = tcp->pid;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000706#endif /* !PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000707
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000708#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +0000709#if defined(S390) || defined(S390X)
710 if (upeek(pid,PT_PSWADDR,&pc) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000711 return -1;
Michal Ludvig10a88d02002-10-07 14:31:00 +0000712 scno = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)),0);
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000713 if (errno)
714 return -1;
715 scno&=0xFF;
716#elif defined (POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000717 if (upeek(pid, 4*PT_R0, &scno) < 0)
718 return -1;
719 if (!(tcp->flags & TCB_INSYSCALL)) {
720 /* Check if we return from execve. */
721 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
722 tcp->flags &= ~TCB_WAITEXECVE;
723 return 0;
724 }
725 }
726#elif defined (I386)
727 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
728 return -1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000729#elif defined (X86_64)
730 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
731 return -1;
732
733 if (!(tcp->flags & TCB_INSYSCALL)) {
734 long val;
735
736 /* Check CS register value. On x86-64 linux it is:
737 * 0x33 for long mode (64 bit)
738 * 0x23 for compatibility mode (32 bit)
739 * It takes only one ptrace and thus doesn't need
740 * to be cached.
741 */
742 if (upeek(pid, 8*CS, &val) < 0)
743 return -1;
744 switch(val)
745 {
746 case 0x23: currpers = 1; break;
747 case 0x33: currpers = 0; break;
748 default:
749 fprintf(stderr, "Unknown value CS=0x%02X while "
750 "detecting personality of process "
751 "PID=%d\n", (int)val, pid);
752 currpers = current_personality;
753 break;
754 }
755#if 0
756 /* This version analyzes the opcode of a syscall instruction.
757 * (int 0x80 on i386 vs. syscall on x86-64)
758 * It works, but is too complicated.
759 */
760 unsigned long val, rip, i;
761
762 if(upeek(pid, 8*RIP, &rip)<0)
763 perror("upeek(RIP)");
764
765 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
766 rip-=2;
767 errno = 0;
768
769 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
770 if (errno)
771 printf("ptrace_peektext failed: %s\n",
772 strerror(errno));
773 switch (call & 0xffff)
774 {
775 /* x86-64: syscall = 0x0f 0x05 */
776 case 0x050f: currpers = 0; break;
777 /* i386: int 0x80 = 0xcd 0x80 */
778 case 0x80cd: currpers = 1; break;
779 default:
780 currpers = current_personality;
781 fprintf(stderr,
782 "Unknown syscall opcode (0x%04X) while "
783 "detecting personality of process "
784 "PID=%d\n", (int)call, pid);
785 break;
786 }
787#endif
788 if(currpers != current_personality)
789 {
790 char *names[]={"64 bit", "32 bit"};
791 set_personality(currpers);
792 printf("[ Process PID=%d runs in %s mode. ]\n",
793 pid, names[current_personality]);
794 }
795 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000796#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000797# define IA64_PSR_IS ((long)1 << 34)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000798 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000799 ia32 = (psr & IA64_PSR_IS) != 0;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000800 if (!(tcp->flags & TCB_INSYSCALL)) {
801 if (ia32) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000802 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000803 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000804 /* Check if we return from execve. */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000805 } else {
806 if (upeek (pid, PT_R15, &scno) < 0)
807 return -1;
808 }
809 } else {
810 /* syscall in progress */
811 if (upeek (pid, PT_R8, &r8) < 0)
812 return -1;
813 if (upeek (pid, PT_R10, &r10) < 0)
814 return -1;
815 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000816 if (tcp->flags & TCB_WAITEXECVE) {
817 tcp->flags &= ~TCB_WAITEXECVE;
818 return 0;
819 }
820
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000821#elif defined (ARM)
822 {
823 long pc;
824 upeek(pid, 4*15, &pc);
825 umoven(tcp, pc-4, 4, (char *)&scno);
826 scno &= 0x000fffff;
827 }
828#elif defined (M68K)
829 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
830 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +0000831#elif defined (MIPS)
832 if (upeek(pid, REG_A3, &a3) < 0)
833 return -1;
834
835 if(!(tcp->flags & TCB_INSYSCALL)) {
836 if (upeek(pid, REG_V0, &scno) < 0)
837 return -1;
838
839 if (scno < 0 || scno > nsyscalls) {
840 if(a3 == 0 || a3 == -1) {
841 if(debug)
842 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
843 return 0;
844 }
845 }
846 } else {
847 if (upeek(pid, REG_V0, &r2) < 0)
848 return -1;
849 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000850#elif defined (ALPHA)
851 if (upeek(pid, REG_A3, &a3) < 0)
852 return -1;
853
854 if (!(tcp->flags & TCB_INSYSCALL)) {
855 if (upeek(pid, REG_R0, &scno) < 0)
856 return -1;
857
858 /* Check if we return from execve. */
859 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
860 tcp->flags &= ~TCB_WAITEXECVE;
861 return 0;
862 }
863
864 /*
865 * Do some sanity checks to figure out if it's
866 * really a syscall entry
867 */
868 if (scno < 0 || scno > nsyscalls) {
869 if (a3 == 0 || a3 == -1) {
870 if (debug)
871 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
872 return 0;
873 }
874 }
875 }
876 else {
877 if (upeek(pid, REG_R0, &r0) < 0)
878 return -1;
879 }
880#elif defined (SPARC)
881 /* Everything we need is in the current register set. */
882 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
883 return -1;
884
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000885 /* If we are entering, then disassemble the syscall trap. */
886 if (!(tcp->flags & TCB_INSYSCALL)) {
887 /* Retrieve the syscall trap instruction. */
888 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000889 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000890 if (errno)
891 return -1;
892
893 /* Disassemble the trap to see what personality to use. */
894 switch (trap) {
895 case 0x91d02010:
896 /* Linux/SPARC syscall trap. */
897 set_personality(0);
898 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +0000899 case 0x91d0206d:
900 /* Linux/SPARC64 syscall trap. */
901 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
902 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000903 case 0x91d02000:
904 /* SunOS syscall trap. (pers 1) */
905 fprintf(stderr,"syscall: SunOS no support\n");
906 return -1;
907 case 0x91d02008:
908 /* Solaris 2.x syscall trap. (per 2) */
909 set_personality(1);
910 break;
911 case 0x91d02009:
912 /* NetBSD/FreeBSD syscall trap. */
913 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
914 return -1;
915 case 0x91d02027:
916 /* Solaris 2.x gettimeofday */
917 set_personality(1);
918 break;
919 default:
920 /* Unknown syscall trap. */
921 if(tcp->flags & TCB_WAITEXECVE) {
922 tcp->flags &= ~TCB_WAITEXECVE;
923 return 0;
924 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000925 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000926 return -1;
927 }
928
929 /* Extract the system call number from the registers. */
930 if (trap == 0x91d02027)
931 scno = 156;
932 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000933 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000934 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000935 scno = regs.r_o0;
936 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000937 }
938 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000939#elif defined(HPPA)
940 if (upeek(pid, PT_GR20, &scno) < 0)
941 return -1;
942 if (!(tcp->flags & TCB_INSYSCALL)) {
943 /* Check if we return from execve. */
944 if ((tcp->flags & TCB_WAITEXECVE)) {
945 tcp->flags &= ~TCB_WAITEXECVE;
946 return 0;
947 }
948 }
Wichert Akkermanccef6372002-05-01 16:39:22 +0000949#elif defined(SH)
950 /*
951 * In the new syscall ABI, the system call number is in R3.
952 */
953 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
954 return -1;
955
956 if (scno < 0) {
957 /* Odd as it may seem, a glibc bug has been known to cause
958 glibc to issue bogus negative syscall numbers. So for
959 our purposes, make strace print what it *should* have been */
960 long correct_scno = (scno & 0xff);
961 if (debug)
962 fprintf(stderr,
Michal Ludvig53b320f2002-09-23 13:30:09 +0000963 "Detected glibc bug: bogus system call number = %ld, "
964 "correcting to %ld\n",
Wichert Akkermanccef6372002-05-01 16:39:22 +0000965 scno,
966 correct_scno);
967 scno = correct_scno;
968 }
969
970
971 if (!(tcp->flags & TCB_INSYSCALL)) {
972 /* Check if we return from execve. */
973 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
974 tcp->flags &= ~TCB_WAITEXECVE;
975 return 0;
976 }
977 }
978#endif /* SH */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000979#endif /* LINUX */
980#ifdef SUNOS4
981 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
982 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +0000983#elif defined(SH)
984 /* new syscall ABI returns result in R0 */
985 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
986 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000987#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000988#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000989#ifdef HAVE_PR_SYSCALL
John Hughes25299712001-03-06 10:10:06 +0000990 scno = tcp->status.PR_SYSCALL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000991#else /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000992#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000993 scno = tcp->status.PR_WHAT;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000994#else /* FREEBSD */
995 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
996 perror("pread");
997 return -1;
998 }
999 switch (regs.r_eax) {
1000 case SYS_syscall:
1001 case SYS___syscall:
1002 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1003 break;
1004 default:
1005 scno = regs.r_eax;
1006 break;
1007 }
1008#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001009#endif /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001010#endif /* USE_PROCFS */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001011 if (!(tcp->flags & TCB_INSYSCALL))
1012 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001013 return 1;
1014}
1015
Pavel Machek4dc3b142000-02-01 17:58:41 +00001016
1017int
1018syscall_fixup(tcp)
1019struct tcb *tcp;
1020{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001021#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001022 int pid = tcp->pid;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001023#else /* USE_PROCFS */
1024 int scno = tcp->scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001025
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001026 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001027 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001028 if (
1029 scno == SYS_fork
1030#ifdef SYS_vfork
1031 || scno == SYS_vfork
1032#endif /* SYS_vfork */
John Hughes4e36a812001-04-18 15:11:51 +00001033#ifdef SYS_fork1
1034 || scno == SYS_fork1
1035#endif /* SYS_fork1 */
1036#ifdef SYS_forkall
1037 || scno == SYS_forkall
1038#endif /* SYS_forkall */
1039#ifdef SYS_rfork1
1040 || scno == SYS_rfork1
1041#endif /* SYS_fork1 */
1042#ifdef SYS_rforkall
1043 || scno == SYS_rforkall
1044#endif /* SYS_rforkall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001045 ) {
1046 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001047 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001048 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001049 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001050 }
1051 else {
1052 fprintf(stderr, "syscall: missing entry\n");
1053 tcp->flags |= TCB_INSYSCALL;
1054 }
1055 }
1056 }
1057 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001058 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001059 fprintf(stderr, "syscall: missing exit\n");
1060 tcp->flags &= ~TCB_INSYSCALL;
1061 }
1062 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001063#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001064#ifdef SUNOS4
1065 if (!(tcp->flags & TCB_INSYSCALL)) {
1066 if (scno == 0) {
1067 fprintf(stderr, "syscall: missing entry\n");
1068 tcp->flags |= TCB_INSYSCALL;
1069 }
1070 }
1071 else {
1072 if (scno != 0) {
1073 if (debug) {
1074 /*
1075 * This happens when a signal handler
1076 * for a signal which interrupted a
1077 * a system call makes another system call.
1078 */
1079 fprintf(stderr, "syscall: missing exit\n");
1080 }
1081 tcp->flags &= ~TCB_INSYSCALL;
1082 }
1083 }
1084#endif /* SUNOS4 */
1085#ifdef LINUX
1086#if defined (I386)
1087 if (upeek(pid, 4*EAX, &eax) < 0)
1088 return -1;
1089 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1090 if (debug)
1091 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1092 return 0;
1093 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001094#elif defined (X86_64)
1095 if (upeek(pid, 8*RAX, &rax) < 0)
1096 return -1;
1097 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1098 if (debug)
1099 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1100 return 0;
1101 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001102#elif defined (S390) || defined (S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001103 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1104 return -1;
1105 if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1106 if (debug)
1107 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1108 return 0;
1109 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001110#elif defined (POWERPC)
1111# define SO_MASK 0x10000000
1112 if (upeek(pid, 4*PT_CCR, &flags) < 0)
1113 return -1;
1114 if (upeek(pid, 4*PT_R3, &result) < 0)
1115 return -1;
1116 if (flags & SO_MASK)
1117 result = -result;
1118#elif defined (M68K)
1119 if (upeek(pid, 4*PT_D0, &d0) < 0)
1120 return -1;
1121 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1122 if (debug)
1123 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1124 return 0;
1125 }
1126#elif defined (ARM)
1127 if (upeek(pid, 4*0, (long *)&r0) < 0)
1128 return -1;
1129 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1130 if (debug)
1131 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
1132 return 0;
1133 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001134#elif defined (HPPA)
1135 if (upeek(pid, PT_GR28, &r28) < 0)
1136 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001137#elif defined(IA64)
1138 if (upeek(pid, PT_R10, &r10) < 0)
1139 return -1;
1140 if (upeek(pid, PT_R8, &r8) < 0)
1141 return -1;
1142 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1143 if (debug)
1144 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1145 return 0;
1146 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001147#endif
1148#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001149 return 1;
1150}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001151
Pavel Machek4dc3b142000-02-01 17:58:41 +00001152int
1153get_error(tcp)
1154struct tcb *tcp;
1155{
1156 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001157#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001158#if defined(S390) || defined(S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001159 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1160 tcp->u_rval = -1;
1161 u_error = -gpr2;
1162 }
1163 else {
1164 tcp->u_rval = gpr2;
1165 u_error = 0;
1166 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001167#else /* !S390 && !S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001168#ifdef I386
1169 if (eax < 0 && -eax < nerrnos) {
1170 tcp->u_rval = -1;
1171 u_error = -eax;
1172 }
1173 else {
1174 tcp->u_rval = eax;
1175 u_error = 0;
1176 }
1177#else /* !I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001178#ifdef X86_64
1179 if (rax < 0 && -rax < nerrnos) {
1180 tcp->u_rval = -1;
1181 u_error = -rax;
1182 }
1183 else {
1184 tcp->u_rval = rax;
1185 u_error = 0;
1186 }
1187#else
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001188#ifdef IA64
1189 if (ia32) {
1190 int err;
1191
1192 err = (int)r8;
1193 if (err < 0 && -err < nerrnos) {
1194 tcp->u_rval = -1;
1195 u_error = -err;
1196 }
1197 else {
1198 tcp->u_rval = err;
1199 u_error = 0;
1200 }
1201 } else {
1202 if (r10) {
1203 tcp->u_rval = -1;
1204 u_error = r8;
1205 } else {
1206 tcp->u_rval = r8;
1207 u_error = 0;
1208 }
1209 }
1210#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001211#ifdef MIPS
1212 if (a3) {
1213 tcp->u_rval = -1;
1214 u_error = r2;
1215 } else {
1216 tcp->u_rval = r2;
1217 u_error = 0;
1218 }
1219#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001220#ifdef POWERPC
1221 if (result && (unsigned) -result < nerrnos) {
1222 tcp->u_rval = -1;
1223 u_error = -result;
1224 }
1225 else {
1226 tcp->u_rval = result;
1227 u_error = 0;
1228 }
1229#else /* !POWERPC */
1230#ifdef M68K
1231 if (d0 && (unsigned) -d0 < nerrnos) {
1232 tcp->u_rval = -1;
1233 u_error = -d0;
1234 }
1235 else {
1236 tcp->u_rval = d0;
1237 u_error = 0;
1238 }
1239#else /* !M68K */
1240#ifdef ARM
1241 if (r0 && (unsigned) -r0 < nerrnos) {
1242 tcp->u_rval = -1;
1243 u_error = -r0;
1244 }
1245 else {
1246 tcp->u_rval = r0;
1247 u_error = 0;
1248 }
1249#else /* !ARM */
1250#ifdef ALPHA
1251 if (a3) {
1252 tcp->u_rval = -1;
1253 u_error = r0;
1254 }
1255 else {
1256 tcp->u_rval = r0;
1257 u_error = 0;
1258 }
1259#else /* !ALPHA */
1260#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001261 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001262 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001263 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001264 }
1265 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001266 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001267 u_error = 0;
1268 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001269#else /* !SPARC */
1270#ifdef HPPA
1271 if (r28 && (unsigned) -r28 < nerrnos) {
1272 tcp->u_rval = -1;
1273 u_error = -r28;
1274 }
1275 else {
1276 tcp->u_rval = r28;
1277 u_error = 0;
1278 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001279#else
1280#ifdef SH
1281 /* interpret R0 as return value or error number */
1282 if (r0 && (unsigned) -r0 < nerrnos) {
1283 tcp->u_rval = -1;
1284 u_error = -r0;
1285 }
1286 else {
1287 tcp->u_rval = r0;
1288 u_error = 0;
1289 }
1290#endif /* SH */
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001291#endif /* HPPA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001292#endif /* SPARC */
1293#endif /* ALPHA */
1294#endif /* ARM */
1295#endif /* M68K */
1296#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001297#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001298#endif /* IA64 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001299#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001300#endif /* I386 */
Michal Ludvig10a88d02002-10-07 14:31:00 +00001301#endif /* S390 || S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001302#endif /* LINUX */
1303#ifdef SUNOS4
1304 /* get error code from user struct */
1305 if (upeek(pid, uoff(u_error), &u_error) < 0)
1306 return -1;
1307 u_error >>= 24; /* u_error is a char */
1308
1309 /* get system call return value */
1310 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1311 return -1;
1312#endif /* SUNOS4 */
1313#ifdef SVR4
1314#ifdef SPARC
1315 /* Judicious guessing goes a long way. */
1316 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1317 tcp->u_rval = -1;
1318 u_error = tcp->status.pr_reg[R_O0];
1319 }
1320 else {
1321 tcp->u_rval = tcp->status.pr_reg[R_O0];
1322 u_error = 0;
1323 }
1324#endif /* SPARC */
1325#ifdef I386
1326 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001327 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001328 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001329 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001330 }
1331 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001332 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001333#ifdef HAVE_LONG_LONG
1334 tcp->u_lrval =
1335 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1336 tcp->status.PR_REG[EAX];
1337#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001338 u_error = 0;
1339 }
1340#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001341#ifdef X86_64
1342 /* Wanna know how to kill an hour single-stepping? */
1343 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1344 tcp->u_rval = -1;
1345 u_error = tcp->status.PR_REG[RAX];
1346 }
1347 else {
1348 tcp->u_rval = tcp->status.PR_REG[RAX];
1349 u_error = 0;
1350 }
1351#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001352#ifdef MIPS
1353 if (tcp->status.pr_reg[CTX_A3]) {
1354 tcp->u_rval = -1;
1355 u_error = tcp->status.pr_reg[CTX_V0];
1356 }
1357 else {
1358 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1359 u_error = 0;
1360 }
1361#endif /* MIPS */
1362#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001363#ifdef FREEBSD
1364 if (regs.r_eflags & PSL_C) {
1365 tcp->u_rval = -1;
1366 u_error = regs.r_eax;
1367 } else {
1368 tcp->u_rval = regs.r_eax;
1369 tcp->u_lrval =
1370 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1371 u_error = 0;
1372 }
1373#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001374 tcp->u_error = u_error;
1375 return 1;
1376}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001377
Pavel Machek4dc3b142000-02-01 17:58:41 +00001378int syscall_enter(tcp)
1379struct tcb *tcp;
1380{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001381#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001382 int pid = tcp->pid;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001383#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001384#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001385#if defined(S390) || defined(S390X)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001386 {
1387 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001388 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1389 tcp->u_nargs = sysent[tcp->scno].nargs;
1390 else
1391 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001392 for (i = 0; i < tcp->u_nargs; i++) {
Michal Ludvig10a88d02002-10-07 14:31:00 +00001393 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001394 return -1;
1395 }
1396 }
1397#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001398 {
1399 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001400 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1401 tcp->u_nargs = sysent[tcp->scno].nargs;
1402 else
1403 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001404 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001405 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1406 * for scno somewhere above here!
1407 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001408 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1409 return -1;
1410 }
1411 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001412#elif defined (IA64)
1413 {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001414 if (!ia32) {
1415 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1416 /* be backwards compatible with kernel < 2.4.4... */
1417# ifndef PT_RBS_END
1418# define PT_RBS_END PT_AR_BSP
1419# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001420
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001421 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001422 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001423 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1424 return -1;
1425
1426 sof = (cfm >> 0) & 0x7f;
1427 sol = (cfm >> 7) & 0x7f;
1428 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1429
1430 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1431 && sysent[tcp->scno].nargs != -1)
1432 tcp->u_nargs = sysent[tcp->scno].nargs;
1433 else
1434 tcp->u_nargs = MAX_ARGS;
1435 for (i = 0; i < tcp->u_nargs; ++i) {
1436 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1437 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1438 return -1;
1439 }
1440 } else {
1441 int i;
1442
1443 if (/* EBX = out0 */
1444 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1445 /* ECX = out1 */
1446 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1447 /* EDX = out2 */
1448 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1449 /* ESI = out3 */
1450 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1451 /* EDI = out4 */
1452 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1453 /* EBP = out5 */
1454 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1455 return -1;
1456
1457 for (i = 0; i < 6; ++i)
1458 /* truncate away IVE sign-extension */
1459 tcp->u_arg[i] &= 0xffffffff;
1460
1461 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1462 && sysent[tcp->scno].nargs != -1)
1463 tcp->u_nargs = sysent[tcp->scno].nargs;
1464 else
1465 tcp->u_nargs = 5;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001466 }
1467 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001468#elif defined (MIPS)
1469 {
1470 long sp;
1471 int i, nargs;
1472
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001473 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1474 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1475 else
1476 nargs = tcp->u_nargs = MAX_ARGS;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001477 if(nargs > 4) {
1478 if(upeek(pid, REG_SP, &sp) < 0)
1479 return -1;
1480 for(i = 0; i < 4; i++) {
1481 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1482 return -1;
1483 }
1484 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1485 (char *)(tcp->u_arg + 4));
1486 } else {
1487 for(i = 0; i < nargs; i++) {
1488 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1489 return -1;
1490 }
1491 }
1492 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001493#elif defined (POWERPC)
1494 {
1495 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001496 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1497 tcp->u_nargs = sysent[tcp->scno].nargs;
1498 else
1499 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001500 for (i = 0; i < tcp->u_nargs; i++) {
1501 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1502 return -1;
1503 }
1504 }
1505#elif defined (SPARC)
1506 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001507 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001508
1509 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1510 tcp->u_nargs = sysent[tcp->scno].nargs;
1511 else
1512 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001513 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001514 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001515 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001516#elif defined (HPPA)
1517 {
1518 int i;
1519
1520 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1521 tcp->u_nargs = sysent[tcp->scno].nargs;
1522 else
1523 tcp->u_nargs = MAX_ARGS;
1524 for (i = 0; i < tcp->u_nargs; i++) {
1525 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1526 return -1;
1527 }
1528 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001529#elif defined(SH)
1530 {
1531 int i;
1532 static int syscall_regs[] = {
1533 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1534 REG_REG0, REG_REG0+1, REG_REG0+2
1535 };
1536
1537 tcp->u_nargs = sysent[tcp->scno].nargs;
1538 for (i = 0; i < tcp->u_nargs; i++) {
1539 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1540 return -1;
1541 }
1542 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001543#elif defined(X86_64)
1544 {
1545 int i;
1546 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1547 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
1548 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
1549 };
1550
1551 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1552 tcp->u_nargs = sysent[tcp->scno].nargs;
1553 else
1554 tcp->u_nargs = MAX_ARGS;
1555 for (i = 0; i < tcp->u_nargs; i++) {
1556 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
1557 return -1;
1558 }
1559 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001560#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001561 {
1562 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001563 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1564 tcp->u_nargs = sysent[tcp->scno].nargs;
1565 else
1566 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001567 for (i = 0; i < tcp->u_nargs; i++) {
1568 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1569 return -1;
1570 }
1571 }
1572#endif
1573#endif /* LINUX */
1574#ifdef SUNOS4
1575 {
1576 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001577 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1578 tcp->u_nargs = sysent[tcp->scno].nargs;
1579 else
1580 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001581 for (i = 0; i < tcp->u_nargs; i++) {
1582 struct user *u;
1583
1584 if (upeek(pid, uoff(u_arg[0]) +
1585 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1586 return -1;
1587 }
1588 }
1589#endif /* SUNOS4 */
1590#ifdef SVR4
1591#ifdef MIPS
1592 /*
1593 * SGI is broken: even though it has pr_sysarg, it doesn't
1594 * set them on system call entry. Get a clue.
1595 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001596 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001597 tcp->u_nargs = sysent[tcp->scno].nargs;
1598 else
1599 tcp->u_nargs = tcp->status.pr_nsysarg;
1600 if (tcp->u_nargs > 4) {
1601 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1602 4*sizeof(tcp->u_arg[0]));
1603 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1604 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1605 }
1606 else {
1607 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1608 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1609 }
John Hughes25299712001-03-06 10:10:06 +00001610#elif UNIXWARE >= 2
1611 /*
1612 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1613 */
1614 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1615 tcp->u_nargs = sysent[tcp->scno].nargs;
1616 else
1617 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1618 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1619 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1620#elif defined (HAVE_PR_SYSCALL)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001621 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001622 tcp->u_nargs = sysent[tcp->scno].nargs;
1623 else
1624 tcp->u_nargs = tcp->status.pr_nsysarg;
1625 {
1626 int i;
1627 for (i = 0; i < tcp->u_nargs; i++)
1628 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1629 }
John Hughes25299712001-03-06 10:10:06 +00001630#elif defined (I386)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001631 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001632 tcp->u_nargs = sysent[tcp->scno].nargs;
1633 else
1634 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001635 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001636 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
John Hughes25299712001-03-06 10:10:06 +00001637#else
1638 I DONT KNOW WHAT TO DO
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001639#endif /* !HAVE_PR_SYSCALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001640#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001641#ifdef FREEBSD
1642 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1643 sysent[tcp->scno].nargs > tcp->status.val)
1644 tcp->u_nargs = sysent[tcp->scno].nargs;
1645 else
1646 tcp->u_nargs = tcp->status.val;
1647 if (tcp->u_nargs < 0)
1648 tcp->u_nargs = 0;
1649 if (tcp->u_nargs > MAX_ARGS)
1650 tcp->u_nargs = MAX_ARGS;
1651 switch(regs.r_eax) {
1652 case SYS___syscall:
1653 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1654 regs.r_esp + sizeof(int) + sizeof(quad_t));
1655 break;
1656 case SYS_syscall:
1657 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1658 regs.r_esp + 2 * sizeof(int));
1659 break;
1660 default:
1661 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1662 regs.r_esp + sizeof(int));
1663 break;
1664 }
1665#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001666 return 1;
1667}
1668
1669int
1670trace_syscall(tcp)
1671struct tcb *tcp;
1672{
1673 int sys_res;
1674 struct timeval tv;
1675 int res;
1676
1677 /* Measure the exit time as early as possible to avoid errors. */
1678 if (dtime && (tcp->flags & TCB_INSYSCALL))
1679 gettimeofday(&tv, NULL);
1680
1681 res = get_scno(tcp);
1682 if (res != 1)
1683 return res;
1684
1685 res = syscall_fixup(tcp);
1686 if (res != 1)
1687 return res;
1688
1689 if (tcp->flags & TCB_INSYSCALL) {
1690 long u_error;
1691 res = get_error(tcp);
1692 if (res != 1)
1693 return res;
1694 u_error = tcp->u_error;
1695
1696
1697 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001698 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1699 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001700 tcp->flags &= ~TCB_INSYSCALL;
1701 return 0;
1702 }
1703
1704 if (tcp->flags & TCB_REPRINT) {
1705 printleader(tcp);
1706 tprintf("<... ");
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001707 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001708 tprintf("syscall_%lu", tcp->scno);
1709 else
1710 tprintf("%s", sysent[tcp->scno].sys_name);
1711 tprintf(" resumed> ");
1712 }
1713
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001714 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001715 call_count[tcp->scno]++;
1716 if (tcp->u_error)
1717 error_count[tcp->scno]++;
1718 tv_sub(&tv, &tv, &tcp->etime);
1719#ifdef LINUX
1720 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1721 static struct timeval one_tick =
1722 { 0, 1000000 / HZ };
1723
1724 if (tv_nz(&tcp->dtime))
1725 tv = tcp->dtime;
1726 else if (tv_cmp(&tv, &one_tick) > 0) {
1727 if (tv_cmp(&shortest, &one_tick) < 0)
1728 tv = shortest;
1729 else
1730 tv = one_tick;
1731 }
1732 }
1733#endif /* LINUX */
1734 if (tv_cmp(&tv, &shortest) < 0)
1735 shortest = tv;
1736 tv_add(&tv_count[tcp->scno],
1737 &tv_count[tcp->scno], &tv);
1738 tcp->flags &= ~TCB_INSYSCALL;
1739 return 0;
1740 }
1741
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001742 if (tcp->scno >= nsyscalls || tcp->scno < 0
Pavel Machek4dc3b142000-02-01 17:58:41 +00001743 || (qual_flags[tcp->scno] & QUAL_RAW))
1744 sys_res = printargs(tcp);
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001745 else {
1746 if (not_failing_only && tcp->u_error)
1747 return; /* ignore failed syscalls */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001748 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001749 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001750 u_error = tcp->u_error;
1751 tprintf(") ");
1752 tabto(acolumn);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001753 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1754 qual_flags[tcp->scno] & QUAL_RAW) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001755 if (u_error)
1756 tprintf("= -1 (errno %ld)", u_error);
1757 else
1758 tprintf("= %#lx", tcp->u_rval);
1759 }
1760 else if (!(sys_res & RVAL_NONE) && u_error) {
1761 switch (u_error) {
1762#ifdef LINUX
1763 case ERESTARTSYS:
1764 tprintf("= ? ERESTARTSYS (To be restarted)");
1765 break;
1766 case ERESTARTNOINTR:
1767 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1768 break;
1769 case ERESTARTNOHAND:
1770 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1771 break;
1772#endif /* LINUX */
1773 default:
1774 tprintf("= -1 ");
Wichert Akkerman4527dae2002-03-31 19:03:29 +00001775 if (u_error < 0)
1776 tprintf("E??? (errno %ld)", u_error);
1777 else if (u_error < nerrnos && u_error < sys_nerr)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001778 tprintf("%s (%s)", errnoent[u_error],
1779 sys_errlist[u_error]);
1780 else if (u_error < nerrnos)
1781 tprintf("%s (errno %ld)",
1782 errnoent[u_error], u_error);
1783 else if (u_error < sys_nerr)
1784 tprintf("ERRNO_%ld (%s)", u_error,
1785 sys_errlist[u_error]);
1786 else
1787 tprintf("E??? (errno %ld)", u_error);
1788 break;
1789 }
1790 }
1791 else {
1792 if (sys_res & RVAL_NONE)
1793 tprintf("= ?");
1794 else {
1795 switch (sys_res & RVAL_MASK) {
1796 case RVAL_HEX:
1797 tprintf("= %#lx", tcp->u_rval);
1798 break;
1799 case RVAL_OCTAL:
1800 tprintf("= %#lo", tcp->u_rval);
1801 break;
1802 case RVAL_UDECIMAL:
1803 tprintf("= %lu", tcp->u_rval);
1804 break;
1805 case RVAL_DECIMAL:
1806 tprintf("= %ld", tcp->u_rval);
1807 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001808#ifdef HAVE_LONG_LONG
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001809 case RVAL_LHEX:
1810 tprintf("= %#llx", tcp->u_lrval);
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001811 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001812 case RVAL_LOCTAL:
1813 tprintf("= %#llo", tcp->u_lrval);
1814 break;
1815 case RVAL_LUDECIMAL:
1816 tprintf("= %llu", tcp->u_lrval);
1817 break;
1818 case RVAL_LDECIMAL:
1819 tprintf("= %lld", tcp->u_lrval);
1820 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001821#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001822 default:
1823 fprintf(stderr,
1824 "invalid rval format\n");
1825 break;
1826 }
1827 }
1828 if ((sys_res & RVAL_STR) && tcp->auxstr)
1829 tprintf(" (%s)", tcp->auxstr);
1830 }
1831 if (dtime) {
1832 tv_sub(&tv, &tv, &tcp->etime);
1833 tprintf(" <%ld.%06ld>",
1834 (long) tv.tv_sec, (long) tv.tv_usec);
1835 }
1836 printtrailer(tcp);
1837
1838 dumpio(tcp);
1839 if (fflush(tcp->outf) == EOF)
1840 return -1;
1841 tcp->flags &= ~TCB_INSYSCALL;
1842 return 0;
1843 }
1844
1845 /* Entering system call */
1846 res = syscall_enter(tcp);
1847 if (res != 1)
1848 return res;
1849
Pavel Machekd8ae7e32000-02-01 17:17:25 +00001850 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001851#ifdef LINUX
Michal Ludvig0e035502002-09-23 15:41:01 +00001852#if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001853 case SYS_socketcall:
1854 decode_subcall(tcp, SYS_socket_subcall,
1855 SYS_socket_nsubcalls, deref_style);
1856 break;
1857 case SYS_ipc:
1858 decode_subcall(tcp, SYS_ipc_subcall,
1859 SYS_ipc_nsubcalls, shift_style);
1860 break;
Michal Ludvig0e035502002-09-23 15:41:01 +00001861#endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001862#ifdef SPARC
1863 case SYS_socketcall:
1864 sparc_socket_decode (tcp);
1865 break;
1866#endif
1867#endif /* LINUX */
1868#ifdef SVR4
1869#ifdef SYS_pgrpsys_subcall
1870 case SYS_pgrpsys:
1871 decode_subcall(tcp, SYS_pgrpsys_subcall,
1872 SYS_pgrpsys_nsubcalls, shift_style);
1873 break;
1874#endif /* SYS_pgrpsys_subcall */
1875#ifdef SYS_sigcall_subcall
1876 case SYS_sigcall:
1877 decode_subcall(tcp, SYS_sigcall_subcall,
1878 SYS_sigcall_nsubcalls, mask_style);
1879 break;
1880#endif /* SYS_sigcall_subcall */
1881 case SYS_msgsys:
1882 decode_subcall(tcp, SYS_msgsys_subcall,
1883 SYS_msgsys_nsubcalls, shift_style);
1884 break;
1885 case SYS_shmsys:
1886 decode_subcall(tcp, SYS_shmsys_subcall,
1887 SYS_shmsys_nsubcalls, shift_style);
1888 break;
1889 case SYS_semsys:
1890 decode_subcall(tcp, SYS_semsys_subcall,
1891 SYS_semsys_nsubcalls, shift_style);
1892 break;
1893#if 0 /* broken */
1894 case SYS_utssys:
1895 decode_subcall(tcp, SYS_utssys_subcall,
1896 SYS_utssys_nsubcalls, shift_style);
1897 break;
1898#endif
1899 case SYS_sysfs:
1900 decode_subcall(tcp, SYS_sysfs_subcall,
1901 SYS_sysfs_nsubcalls, shift_style);
1902 break;
1903 case SYS_spcall:
1904 decode_subcall(tcp, SYS_spcall_subcall,
1905 SYS_spcall_nsubcalls, shift_style);
1906 break;
1907#ifdef SYS_context_subcall
1908 case SYS_context:
1909 decode_subcall(tcp, SYS_context_subcall,
1910 SYS_context_nsubcalls, shift_style);
1911 break;
1912#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00001913#ifdef SYS_door_subcall
1914 case SYS_door:
1915 decode_subcall(tcp, SYS_door_subcall,
1916 SYS_door_nsubcalls, door_style);
1917 break;
1918#endif /* SYS_door_subcall */
John Hughesbdf48f52001-03-06 15:08:09 +00001919#ifdef SYS_kaio_subcall
1920 case SYS_kaio:
1921 decode_subcall(tcp, SYS_kaio_subcall,
1922 SYS_kaio_nsubcalls, shift_style);
1923 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001924#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001925#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001926#ifdef FREEBSD
1927 case SYS_msgsys:
1928 case SYS_shmsys:
1929 case SYS_semsys:
1930 decode_subcall(tcp, 0, 0, table_style);
1931 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001932#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001933#ifdef SUNOS4
1934 case SYS_semsys:
1935 decode_subcall(tcp, SYS_semsys_subcall,
1936 SYS_semsys_nsubcalls, shift_style);
1937 break;
1938 case SYS_msgsys:
1939 decode_subcall(tcp, SYS_msgsys_subcall,
1940 SYS_msgsys_nsubcalls, shift_style);
1941 break;
1942 case SYS_shmsys:
1943 decode_subcall(tcp, SYS_shmsys_subcall,
1944 SYS_shmsys_nsubcalls, shift_style);
1945 break;
1946#endif
1947 }
1948
1949 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001950 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001951 tcp->flags |= TCB_INSYSCALL;
1952 return 0;
1953 }
1954
1955 if (cflag) {
1956 gettimeofday(&tcp->etime, NULL);
1957 tcp->flags |= TCB_INSYSCALL;
1958 return 0;
1959 }
1960
1961 printleader(tcp);
1962 tcp->flags &= ~TCB_REPRINT;
1963 tcp_last = tcp;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001964 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001965 tprintf("syscall_%lu(", tcp->scno);
1966 else
1967 tprintf("%s(", sysent[tcp->scno].sys_name);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001968 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001969 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1970 sys_res = printargs(tcp);
1971 else
1972 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1973 if (fflush(tcp->outf) == EOF)
1974 return -1;
1975 tcp->flags |= TCB_INSYSCALL;
1976 /* Measure the entrance time as late as possible to avoid errors. */
1977 if (dtime)
1978 gettimeofday(&tcp->etime, NULL);
1979 return sys_res;
1980}
1981
1982int
1983printargs(tcp)
1984struct tcb *tcp;
1985{
1986 if (entering(tcp)) {
1987 int i;
1988
1989 for (i = 0; i < tcp->u_nargs; i++)
1990 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1991 }
1992 return 0;
1993}
1994
1995long
1996getrval2(tcp)
1997struct tcb *tcp;
1998{
1999 long val = -1;
2000
2001#ifdef LINUX
2002#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002003 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002004 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
2005 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002006 val = regs.r_o1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002007#endif /* SPARC */
2008#endif /* LINUX */
2009
2010#ifdef SUNOS4
2011 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2012 return -1;
2013#endif /* SUNOS4 */
2014
2015#ifdef SVR4
2016#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002017 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002018#endif /* SPARC */
2019#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002020 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002021#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00002022#ifdef X86_64
2023 val = tcp->status.PR_REG[RDX];
2024#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002025#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002026 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002027#endif /* MIPS */
2028#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002029#ifdef FREEBSD
2030 struct reg regs;
2031 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
2032 val = regs.r_edx;
2033#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002034 return val;
2035}
2036
2037/*
2038 * Apparently, indirect system calls have already be converted by ptrace(2),
2039 * so if you see "indir" this program has gone astray.
2040 */
2041int
2042sys_indir(tcp)
2043struct tcb *tcp;
2044{
2045 int i, scno, nargs;
2046
2047 if (entering(tcp)) {
2048 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2049 fprintf(stderr, "Bogus syscall: %u\n", scno);
2050 return 0;
2051 }
2052 nargs = sysent[scno].nargs;
2053 tprintf("%s", sysent[scno].sys_name);
2054 for (i = 0; i < nargs; i++)
2055 tprintf(", %#lx", tcp->u_arg[i+1]);
2056 }
2057 return 0;
2058}
2059
2060static int
2061time_cmp(a, b)
2062void *a;
2063void *b;
2064{
2065 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2066}
2067
2068static int
2069syscall_cmp(a, b)
2070void *a;
2071void *b;
2072{
2073 return strcmp(sysent[*((int *) a)].sys_name,
2074 sysent[*((int *) b)].sys_name);
2075}
2076
2077static int
2078count_cmp(a, b)
2079void *a;
2080void *b;
2081{
2082 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2083
2084 return (m < n) ? 1 : (m > n) ? -1 : 0;
2085}
2086
2087static int (*sortfun)();
2088static struct timeval overhead = { -1, -1 };
2089
2090void
2091set_sortby(sortby)
2092char *sortby;
2093{
2094 if (strcmp(sortby, "time") == 0)
2095 sortfun = time_cmp;
2096 else if (strcmp(sortby, "calls") == 0)
2097 sortfun = count_cmp;
2098 else if (strcmp(sortby, "name") == 0)
2099 sortfun = syscall_cmp;
2100 else if (strcmp(sortby, "nothing") == 0)
2101 sortfun = NULL;
2102 else {
2103 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2104 exit(1);
2105 }
2106}
2107
2108void set_overhead(n)
2109int n;
2110{
2111 overhead.tv_sec = n / 1000000;
2112 overhead.tv_usec = n % 1000000;
2113}
2114
2115void
2116call_summary(outf)
2117FILE *outf;
2118{
2119 int i, j;
2120 int call_cum, error_cum;
2121 struct timeval tv_cum, dtv;
2122 double percent;
2123 char *dashes = "-------------------------";
2124 char error_str[16];
2125
2126 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2127 if (overhead.tv_sec == -1) {
2128 tv_mul(&overhead, &shortest, 8);
2129 tv_div(&overhead, &overhead, 10);
2130 }
2131 for (i = 0; i < nsyscalls; i++) {
2132 sorted_count[i] = i;
2133 if (call_count[i] == 0)
2134 continue;
2135 tv_mul(&dtv, &overhead, call_count[i]);
2136 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2137 call_cum += call_count[i];
2138 error_cum += error_count[i];
2139 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2140 }
2141 if (sortfun)
2142 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2143 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2144 "% time", "seconds", "usecs/call",
2145 "calls", "errors", "syscall");
2146 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2147 dashes, dashes, dashes, dashes, dashes, dashes);
2148 for (i = 0; i < nsyscalls; i++) {
2149 j = sorted_count[i];
2150 if (call_count[j] == 0)
2151 continue;
2152 tv_div(&dtv, &tv_count[j], call_count[j]);
2153 if (error_count[j])
2154 sprintf(error_str, "%d", error_count[j]);
2155 else
2156 error_str[0] = '\0';
2157 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2158 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2159 percent, (long) tv_count[j].tv_sec,
2160 (long) tv_count[j].tv_usec,
2161 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2162 call_count[j], error_str, sysent[j].sys_name);
2163 }
2164 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2165 dashes, dashes, dashes, dashes, dashes, dashes);
2166 if (error_cum)
2167 sprintf(error_str, "%d", error_cum);
2168 else
2169 error_str[0] = '\0';
2170 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2171 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2172 call_cum, error_str, "total");
2173}