blob: a9022aae4f72a8b2781a6374fb30d2cfd3259024 [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * $Id$
34 */
35
36#include "defs.h"
37
38#include <signal.h>
39#include <time.h>
40#include <errno.h>
41#include <sys/user.h>
42#include <sys/syscall.h>
43#include <sys/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000044
Wichert Akkerman15dea971999-10-06 13:06:34 +000045#if HAVE_ASM_REG_H
Wichert Akkerman00a82ee2001-03-28 20:29:17 +000046#ifdef SPARC
47# define fpq kernel_fpq
48# define fq kernel_fq
49# define fpu kernel_fpu
50#endif
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000051#include <asm/reg.h>
Wichert Akkerman00a82ee2001-03-28 20:29:17 +000052#ifdef SPARC
53# undef fpq
54# undef fq
Roland McGrath761b5d72002-12-15 23:58:31 +000055# undef fpu
Wichert Akkerman00a82ee2001-03-28 20:29:17 +000056#endif
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000057#endif
58
Wichert Akkerman15dea971999-10-06 13:06:34 +000059#ifdef HAVE_SYS_REG_H
60#include <sys/reg.h>
61#ifndef PTRACE_PEEKUSR
62# define PTRACE_PEEKUSR PTRACE_PEEKUSER
63#endif
Wichert Akkermanfaf72222000-02-19 23:59:03 +000064#elif defined(HAVE_LINUX_PTRACE_H)
65#undef PTRACE_SYSCALL
66#include <linux/ptrace.h>
Wichert Akkerman15dea971999-10-06 13:06:34 +000067#endif
68
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000069#if defined(LINUX) && defined(IA64)
70# include <asm/ptrace_offsets.h>
71# include <asm/rse.h>
72#endif
73
Pavel Machekd8ae7e32000-02-01 17:17:25 +000074#define NR_SYSCALL_BASE 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000075#ifdef LINUX
76#ifndef ERESTARTSYS
77#define ERESTARTSYS 512
78#endif
79#ifndef ERESTARTNOINTR
80#define ERESTARTNOINTR 513
81#endif
82#ifndef ERESTARTNOHAND
83#define ERESTARTNOHAND 514 /* restart if no handler.. */
84#endif
85#ifndef ENOIOCTLCMD
86#define ENOIOCTLCMD 515 /* No ioctl command */
87#endif
88#ifndef NSIG
89#define NSIG 32
90#endif
91#ifdef ARM
92#undef NSIG
93#define NSIG 32
Pavel Machekd8ae7e32000-02-01 17:17:25 +000094#undef NR_SYSCALL_BASE
95#define NR_SYSCALL_BASE __NR_SYSCALL_BASE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000096#endif
97#endif /* LINUX */
98
99#include "syscall.h"
100
101/* Define these shorthand notations to simplify the syscallent files. */
102#define TF TRACE_FILE
103#define TI TRACE_IPC
104#define TN TRACE_NETWORK
105#define TP TRACE_PROCESS
106#define TS TRACE_SIGNAL
107
108struct sysent sysent0[] = {
109#include "syscallent.h"
110};
111int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
112
113#if SUPPORTED_PERSONALITIES >= 2
114struct sysent sysent1[] = {
115#include "syscallent1.h"
116};
117int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
118#endif /* SUPPORTED_PERSONALITIES >= 2 */
119
120#if SUPPORTED_PERSONALITIES >= 3
121struct sysent sysent2[] = {
122#include "syscallent2.h"
123};
124int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
125#endif /* SUPPORTED_PERSONALITIES >= 3 */
126
127struct sysent *sysent;
128int nsyscalls;
129
130/* Now undef them since short defines cause wicked namespace pollution. */
131#undef TF
132#undef TI
133#undef TN
134#undef TP
135#undef TS
136
137char *errnoent0[] = {
138#include "errnoent.h"
139};
140int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
141
142#if SUPPORTED_PERSONALITIES >= 2
143char *errnoent1[] = {
144#include "errnoent1.h"
145};
146int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
147#endif /* SUPPORTED_PERSONALITIES >= 2 */
148
149#if SUPPORTED_PERSONALITIES >= 3
150char *errnoent2[] = {
151#include "errnoent2.h"
152};
153int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
154#endif /* SUPPORTED_PERSONALITIES >= 3 */
155
156char **errnoent;
157int nerrnos;
158
159int current_personality;
160
161int
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000162set_personality(personality)
163int personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000164{
165 switch (personality) {
166 case 0:
167 errnoent = errnoent0;
168 nerrnos = nerrnos0;
169 sysent = sysent0;
170 nsyscalls = nsyscalls0;
171 ioctlent = ioctlent0;
172 nioctlents = nioctlents0;
173 signalent = signalent0;
174 nsignals = nsignals0;
175 break;
176
177#if SUPPORTED_PERSONALITIES >= 2
178 case 1:
179 errnoent = errnoent1;
180 nerrnos = nerrnos1;
181 sysent = sysent1;
182 nsyscalls = nsyscalls1;
183 ioctlent = ioctlent1;
184 nioctlents = nioctlents1;
185 signalent = signalent1;
186 nsignals = nsignals1;
187 break;
188#endif /* SUPPORTED_PERSONALITIES >= 2 */
189
190#if SUPPORTED_PERSONALITIES >= 3
191 case 2:
192 errnoent = errnoent2;
193 nerrnos = nerrnos2;
194 sysent = sysent2;
195 nsyscalls = nsyscalls2;
196 ioctlent = ioctlent2;
197 nioctlents = nioctlents2;
198 signalent = signalent2;
199 nsignals = nsignals2;
200 break;
201#endif /* SUPPORTED_PERSONALITIES >= 3 */
202
203 default:
204 return -1;
205 }
206
207 current_personality = personality;
208 return 0;
209}
210
211int qual_flags[MAX_QUALS];
212
213static int call_count[MAX_QUALS];
214static int error_count[MAX_QUALS];
215static struct timeval tv_count[MAX_QUALS];
216static int sorted_count[MAX_QUALS];
217
218static struct timeval shortest = { 1000000, 0 };
219
Roland McGrath9797ceb2002-12-30 10:23:00 +0000220static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000221
222static struct qual_options {
223 int bitflag;
224 char *option_name;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000225 int (*qualify)();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000226 char *argument_name;
227} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000228 { QUAL_TRACE, "trace", qual_syscall, "system call" },
229 { QUAL_TRACE, "t", qual_syscall, "system call" },
230 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
231 { QUAL_ABBREV, "a", qual_syscall, "system call" },
232 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
233 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
234 { QUAL_RAW, "raw", qual_syscall, "system call" },
235 { QUAL_RAW, "x", qual_syscall, "system call" },
236 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
237 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
238 { QUAL_SIGNAL, "s", qual_signal, "signal" },
239 { QUAL_FAULT, "fault", qual_fault, "fault" },
240 { QUAL_FAULT, "faults", qual_fault, "fault" },
241 { QUAL_FAULT, "m", qual_fault, "fault" },
242 { QUAL_READ, "read", qual_desc, "descriptor" },
243 { QUAL_READ, "reads", qual_desc, "descriptor" },
244 { QUAL_READ, "r", qual_desc, "descriptor" },
245 { QUAL_WRITE, "write", qual_desc, "descriptor" },
246 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
247 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000248 { 0, NULL, NULL, NULL },
249};
250
Roland McGrath9797ceb2002-12-30 10:23:00 +0000251static void
252qualify_one(n, opt, not)
253 int n;
254 struct qual_options *opt;
255 int not;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000256{
Roland McGrath9797ceb2002-12-30 10:23:00 +0000257 if (not)
258 qual_flags[n] &= ~opt->bitflag;
259 else
260 qual_flags[n] |= opt->bitflag;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000261}
262
263static int
Roland McGrath9797ceb2002-12-30 10:23:00 +0000264qual_syscall(s, opt, not)
265 char *s;
266 struct qual_options *opt;
267 int not;
268{
269 int i;
270 int any = 0;
271
272 for (i = 0; i < nsyscalls; i++) {
273 if (strcmp(s, sysent[i].sys_name) == 0) {
274 qualify_one(i, opt, not);
275 any = 1;
276 }
277 }
278 return !any;
279}
280
281static int
282qual_signal(s, opt, not)
283 char *s;
284 struct qual_options *opt;
285 int not;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000286{
287 int i;
288 char buf[32];
289
Roland McGrath9797ceb2002-12-30 10:23:00 +0000290 if (s && *s && isdigit((unsigned char)*s)) {
291 qualify_one(atoi(s), opt, not);
292 return 1;
293 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000294 strcpy(buf, s);
295 s = buf;
296 for (i = 0; s[i]; i++)
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000297 s[i] = toupper((unsigned char)(s[i]));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000298 if (strncmp(s, "SIG", 3) == 0)
299 s += 3;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000300 for (i = 0; i <= NSIG; i++)
301 if (strcmp(s, signame(i) + 3) == 0) {
302 qualify_one(atoi(s), opt, not);
303 return 1;
304 }
305 return 0;
306}
307
308static int
309qual_fault(s, opt, not)
310 char *s;
311 struct qual_options *opt;
312 int not;
313{
314 return -1;
315}
316
317static int
318qual_desc(s, opt, not)
319 char *s;
320 struct qual_options *opt;
321 int not;
322{
323 if (s && *s && isdigit((unsigned char)*s)) {
324 qualify_one(atoi(s), opt, not);
Roland McGrath2b619022003-04-10 18:58:20 +0000325 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000326 }
327 return -1;
328}
329
330static int
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000331lookup_class(s)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000332 char *s;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000333{
334 if (strcmp(s, "file") == 0)
335 return TRACE_FILE;
336 if (strcmp(s, "ipc") == 0)
337 return TRACE_IPC;
338 if (strcmp(s, "network") == 0)
339 return TRACE_NETWORK;
340 if (strcmp(s, "process") == 0)
341 return TRACE_PROCESS;
342 if (strcmp(s, "signal") == 0)
343 return TRACE_SIGNAL;
344 return -1;
345}
346
347void
348qualify(s)
349char *s;
350{
351 struct qual_options *opt;
352 int not;
353 char *p;
354 int i, n;
355
356 opt = &qual_options[0];
357 for (i = 0; (p = qual_options[i].option_name); i++) {
358 n = strlen(p);
359 if (strncmp(s, p, n) == 0 && s[n] == '=') {
360 opt = &qual_options[i];
361 s += n + 1;
362 break;
363 }
364 }
365 not = 0;
366 if (*s == '!') {
367 not = 1;
368 s++;
369 }
370 if (strcmp(s, "none") == 0) {
371 not = 1 - not;
372 s = "all";
373 }
374 if (strcmp(s, "all") == 0) {
375 for (i = 0; i < MAX_QUALS; i++) {
376 if (not)
377 qual_flags[i] &= ~opt->bitflag;
378 else
379 qual_flags[i] |= opt->bitflag;
380 }
381 return;
382 }
383 for (i = 0; i < MAX_QUALS; i++) {
384 if (not)
385 qual_flags[i] |= opt->bitflag;
386 else
387 qual_flags[i] &= ~opt->bitflag;
388 }
389 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
390 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
391 for (i = 0; i < MAX_QUALS; i++) {
392 if (sysent[i].sys_flags & n) {
393 if (not)
394 qual_flags[i] &= ~opt->bitflag;
395 else
396 qual_flags[i] |= opt->bitflag;
397 }
398 }
399 continue;
400 }
Roland McGrath9797ceb2002-12-30 10:23:00 +0000401 if (opt->qualify(p, opt, not)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000402 fprintf(stderr, "strace: invalid %s `%s'\n",
403 opt->argument_name, p);
404 exit(1);
405 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000406 }
407 return;
408}
409
410static void
411dumpio(tcp)
412struct tcb *tcp;
413{
414 if (syserror(tcp))
415 return;
416 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
417 return;
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000418 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000419 case SYS_read:
420#ifdef SYS_recv
421 case SYS_recv:
422#endif
423#ifdef SYS_recvfrom
424 case SYS_recvfrom:
425#endif
426 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
427 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
428 break;
429 case SYS_write:
430#ifdef SYS_send
431 case SYS_send:
432#endif
433#ifdef SYS_sendto
434 case SYS_sendto:
435#endif
436 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
437 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
438 break;
John Hughes1d08dcf2001-07-10 13:48:44 +0000439#ifdef SYS_readv
440 case SYS_readv:
441 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
442 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
443 break;
444#endif
445#ifdef SYS_writev
446 case SYS_writev:
447
448 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
449 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
450 break;
451#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000452 }
453}
454
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000455#ifndef FREEBSD
Wichert Akkerman8829a551999-06-11 13:18:40 +0000456enum subcall_style { shift_style, deref_style, mask_style, door_style };
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000457#else /* FREEBSD */
458enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
459
460struct subcall {
461 int call;
462 int nsubcalls;
463 int subcalls[5];
464};
465
466const struct subcall subcalls_table[] = {
467 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
John Hughes61eeb552001-03-06 15:51:53 +0000468#ifdef SYS_semconfig
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000469 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
John Hughes61eeb552001-03-06 15:51:53 +0000470#else
471 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
472#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000473 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
474};
475#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000476
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000477#if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000478
479const int socket_map [] = {
480 /* SYS_SOCKET */ 97,
481 /* SYS_BIND */ 104,
482 /* SYS_CONNECT */ 98,
483 /* SYS_LISTEN */ 106,
484 /* SYS_ACCEPT */ 99,
485 /* SYS_GETSOCKNAME */ 150,
486 /* SYS_GETPEERNAME */ 141,
487 /* SYS_SOCKETPAIR */ 135,
488 /* SYS_SEND */ 101,
489 /* SYS_RECV */ 102,
490 /* SYS_SENDTO */ 133,
491 /* SYS_RECVFROM */ 125,
492 /* SYS_SHUTDOWN */ 134,
493 /* SYS_SETSOCKOPT */ 105,
494 /* SYS_GETSOCKOPT */ 118,
495 /* SYS_SENDMSG */ 114,
496 /* SYS_RECVMSG */ 113
497};
498
499void
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000500sparc_socket_decode (tcp)
501struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000502{
503 volatile long addr;
504 volatile int i, n;
505
506 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
507 return;
508 }
509 tcp->scno = socket_map [tcp->u_arg [0]-1];
510 n = tcp->u_nargs = sysent [tcp->scno].nargs;
511 addr = tcp->u_arg [1];
512 for (i = 0; i < n; i++){
513 int arg;
514 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
515 arg = 0;
516 tcp->u_arg [i] = arg;
517 addr += sizeof (arg);
518 }
519}
520
Michal Ludvig0e035502002-09-23 15:41:01 +0000521void
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000522decode_subcall(tcp, subcall, nsubcalls, style)
523struct tcb *tcp;
524int subcall;
525int nsubcalls;
526enum subcall_style style;
527{
Michal Ludvig10a88d02002-10-07 14:31:00 +0000528 long addr, mask, arg;
529 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000530
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000531 switch (style) {
532 case shift_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000533 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
534 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000535 tcp->scno = subcall + tcp->u_arg[0];
536 if (sysent[tcp->scno].nargs != -1)
537 tcp->u_nargs = sysent[tcp->scno].nargs;
538 else
539 tcp->u_nargs--;
540 for (i = 0; i < tcp->u_nargs; i++)
541 tcp->u_arg[i] = tcp->u_arg[i + 1];
542 break;
543 case deref_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000544 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
545 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000546 tcp->scno = subcall + tcp->u_arg[0];
547 addr = tcp->u_arg[1];
548 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
549 if (umove(tcp, addr, &arg) < 0)
550 arg = 0;
551 tcp->u_arg[i] = arg;
552 addr += sizeof(arg);
553 }
554 tcp->u_nargs = sysent[tcp->scno].nargs;
555 break;
556 case mask_style:
557 mask = (tcp->u_arg[0] >> 8) & 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000558 for (i = 0; mask; i++)
559 mask >>= 1;
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000560 if (i >= nsubcalls)
561 return;
562 tcp->u_arg[0] &= 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000563 tcp->scno = subcall + i;
564 if (sysent[tcp->scno].nargs != -1)
565 tcp->u_nargs = sysent[tcp->scno].nargs;
566 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000567 case door_style:
568 /*
569 * Oh, yuck. The call code is the *sixth* argument.
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000570 * (don't you mean the *last* argument? - JH)
Wichert Akkerman8829a551999-06-11 13:18:40 +0000571 */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000572 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
573 return;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000574 tcp->scno = subcall + tcp->u_arg[5];
575 if (sysent[tcp->scno].nargs != -1)
576 tcp->u_nargs = sysent[tcp->scno].nargs;
577 else
578 tcp->u_nargs--;
579 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000580#ifdef FREEBSD
581 case table_style:
582 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
583 if (subcalls_table[i].call == tcp->scno) break;
584 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
585 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
586 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
587 for (i = 0; i < tcp->u_nargs; i++)
588 tcp->u_arg[i] = tcp->u_arg[i + 1];
589 }
590 break;
591#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000592 }
593}
594#endif
595
596struct tcb *tcp_last = NULL;
597
598static int
599internal_syscall(tcp)
600struct tcb *tcp;
601{
602 /*
603 * We must always trace a few critical system calls in order to
604 * correctly support following forks in the presence of tracing
605 * qualifiers.
606 */
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000607 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000608#ifdef SYS_fork
609 case SYS_fork:
610#endif
611#ifdef SYS_vfork
612 case SYS_vfork:
613#endif
John Hughes4e36a812001-04-18 15:11:51 +0000614#ifdef SYS_fork1
615 case SYS_fork1:
616#endif
617#ifdef SYS_forkall
618 case SYS_forkall:
619#endif
620#ifdef SYS_rfork1
621 case SYS_rfork1:
622#endif
623#ifdef SYS_rforkall
624 case SYS_rforkall:
625#endif
Roland McGrathf3a0e1b2003-02-20 02:45:22 +0000626#ifdef SYS_rfork
627 case SYS_rfork:
628#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000629 internal_fork(tcp);
630 break;
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000631#ifdef SYS_clone
632 case SYS_clone:
633 internal_clone(tcp);
634 break;
635#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000636#ifdef SYS_clone2
637 case SYS_clone2:
638 internal_clone(tcp);
639 break;
640#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000641#ifdef SYS_execv
642 case SYS_execv:
643#endif
644#ifdef SYS_execve
645 case SYS_execve:
646#endif
John Hughes4e36a812001-04-18 15:11:51 +0000647#ifdef SYS_rexecve
648 case SYS_rexecve:
649#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000650 internal_exec(tcp);
651 break;
652
653#ifdef SYS_wait
654 case SYS_wait:
655#endif
656#ifdef SYS_wait4
657 case SYS_wait4:
658#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000659#ifdef SYS32_wait4
660 case SYS32_wait4:
661#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000662#ifdef SYS_waitpid
663 case SYS_waitpid:
664#endif
665#ifdef SYS_waitsys
666 case SYS_waitsys:
667#endif
668 internal_wait(tcp);
669 break;
670
671#ifdef SYS_exit
672 case SYS_exit:
673#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000674#ifdef SYS32_exit
675 case SYS32_exit:
676#endif
Roland McGrath923f7502003-01-09 06:53:27 +0000677#ifdef __NR_exit_group
678 case __NR_exit_group:
679#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000680 internal_exit(tcp);
681 break;
682 }
683 return 0;
684}
685
Wichert Akkermanc7926982000-04-10 22:22:31 +0000686
687#ifdef LINUX
688#if defined (I386)
689 static long eax;
690#elif defined (IA64)
691 long r8, r10, psr;
692 long ia32 = 0;
693#elif defined (POWERPC)
694 static long result,flags;
695#elif defined (M68K)
696 static int d0;
697#elif defined (ARM)
Roland McGrath0f87c492003-06-03 23:29:04 +0000698 static struct pt_regs regs;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000699#elif defined (ALPHA)
700 static long r0;
701 static long a3;
702#elif defined (SPARC)
Wichert Akkerman00a82ee2001-03-28 20:29:17 +0000703 static struct regs regs;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000704 static unsigned long trap;
705#elif defined(MIPS)
706 static long a3;
707 static long r2;
Michal Ludvig10a88d02002-10-07 14:31:00 +0000708#elif defined(S390) || defined(S390X)
Wichert Akkermanc7926982000-04-10 22:22:31 +0000709 static long gpr2;
710 static long pc;
Michal Ludvig882eda82002-11-11 12:50:47 +0000711 static long syscall_mode;
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000712#elif defined(HPPA)
713 static long r28;
Wichert Akkermanccef6372002-05-01 16:39:22 +0000714#elif defined(SH)
715 static long r0;
Roland McGrathf5a47772003-06-26 22:40:42 +0000716#elif defined(SH64)
Roland McGrath0f87c492003-06-03 23:29:04 +0000717 static long r9;
Michal Ludvig0e035502002-09-23 15:41:01 +0000718#elif defined(X86_64)
719 static long rax;
Roland McGrath761b5d72002-12-15 23:58:31 +0000720#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000721#endif /* LINUX */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000722#ifdef FREEBSD
723 struct reg regs;
Roland McGrath761b5d72002-12-15 23:58:31 +0000724#endif /* FREEBSD */
Wichert Akkermanc7926982000-04-10 22:22:31 +0000725
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000726int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000727get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000728struct tcb *tcp;
729{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000730 long scno = 0;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000731#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000732 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +0000733#endif /* !PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000734
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000735#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +0000736#if defined(S390) || defined(S390X)
Roland McGrath96dc5142003-01-20 10:23:04 +0000737 if (tcp->flags & TCB_WAITEXECVE) {
738 /*
739 * When the execve system call completes successfully, the
740 * new process still has -ENOSYS (old style) or __NR_execve
741 * (new style) in gpr2. We cannot recover the scno again
742 * by disassembly, because the image that executed the
743 * syscall is gone now. Fortunately, we don't want it. We
744 * leave the flag set so that syscall_fixup can fake the
745 * result.
746 */
747 if (tcp->flags & TCB_INSYSCALL)
748 return 1;
749 /*
750 * This is the SIGTRAP after execve. We cannot try to read
751 * the system call here either.
752 */
753 tcp->flags &= ~TCB_WAITEXECVE;
754 return 0;
755 }
Roland McGrath2f924ca2003-06-26 22:23:28 +0000756
757 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
758 return -1;
759
760 if (syscall_mode != -ENOSYS) {
761 /*
762 * Since kernel version 2.5.44 the scno gets passed in gpr2.
763 */
764 scno = syscall_mode;
765 } else {
Michal Ludvig882eda82002-11-11 12:50:47 +0000766 /*
767 * Old style of "passing" the scno via the SVC instruction.
768 */
769
770 long opcode, offset_reg, tmp;
771 void * svc_addr;
772 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
773 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
774 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
775 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
Roland McGrath761b5d72002-12-15 23:58:31 +0000776
Michal Ludvig882eda82002-11-11 12:50:47 +0000777 if (upeek(pid, PT_PSWADDR, &pc) < 0)
778 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000779 errno = 0;
Michal Ludvig882eda82002-11-11 12:50:47 +0000780 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +0000781 if (errno) {
782 perror("peektext(pc-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +0000783 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000784 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000785
786 /*
787 * We have to check if the SVC got executed directly or via an
788 * EXECUTE instruction. In case of EXECUTE it is necessary to do
789 * instruction decoding to derive the system call number.
790 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
791 * so that this doesn't work if a SVC opcode is part of an EXECUTE
792 * opcode. Since there is no way to find out the opcode size this
793 * is the best we can do...
794 */
795
796 if ((opcode & 0xff00) == 0x0a00) {
797 /* SVC opcode */
798 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +0000799 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000800 else {
801 /* SVC got executed by EXECUTE instruction */
802
803 /*
804 * Do instruction decoding of EXECUTE. If you really want to
805 * understand this, read the Principles of Operations.
806 */
807 svc_addr = (void *) (opcode & 0xfff);
808
809 tmp = 0;
810 offset_reg = (opcode & 0x000f0000) >> 16;
811 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
812 return -1;
813 svc_addr += tmp;
814
815 tmp = 0;
816 offset_reg = (opcode & 0x0000f000) >> 12;
817 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
818 return -1;
819 svc_addr += tmp;
820
821 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
822 if (errno)
823 return -1;
824#if defined(S390X)
825 scno >>= 48;
826#else
827 scno >>= 16;
828#endif
829 tmp = 0;
830 offset_reg = (opcode & 0x00f00000) >> 20;
831 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
832 return -1;
833
834 scno = (scno | tmp) & 0xff;
835 }
836 }
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000837#elif defined (POWERPC)
Roland McGratheb285352003-01-14 09:59:00 +0000838 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000839 return -1;
840 if (!(tcp->flags & TCB_INSYSCALL)) {
841 /* Check if we return from execve. */
842 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
843 tcp->flags &= ~TCB_WAITEXECVE;
844 return 0;
845 }
846 }
847#elif defined (I386)
848 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
849 return -1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000850#elif defined (X86_64)
851 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
852 return -1;
853
Roland McGrath761b5d72002-12-15 23:58:31 +0000854 if (!(tcp->flags & TCB_INSYSCALL)) {
855 static int currpers=-1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000856 long val;
857
858 /* Check CS register value. On x86-64 linux it is:
859 * 0x33 for long mode (64 bit)
860 * 0x23 for compatibility mode (32 bit)
Roland McGrath761b5d72002-12-15 23:58:31 +0000861 * It takes only one ptrace and thus doesn't need
Michal Ludvig0e035502002-09-23 15:41:01 +0000862 * to be cached.
863 */
864 if (upeek(pid, 8*CS, &val) < 0)
865 return -1;
866 switch(val)
867 {
868 case 0x23: currpers = 1; break;
869 case 0x33: currpers = 0; break;
870 default:
871 fprintf(stderr, "Unknown value CS=0x%02X while "
872 "detecting personality of process "
873 "PID=%d\n", (int)val, pid);
874 currpers = current_personality;
875 break;
876 }
877#if 0
878 /* This version analyzes the opcode of a syscall instruction.
879 * (int 0x80 on i386 vs. syscall on x86-64)
880 * It works, but is too complicated.
881 */
882 unsigned long val, rip, i;
883
884 if(upeek(pid, 8*RIP, &rip)<0)
885 perror("upeek(RIP)");
Roland McGrath761b5d72002-12-15 23:58:31 +0000886
Michal Ludvig0e035502002-09-23 15:41:01 +0000887 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
888 rip-=2;
889 errno = 0;
890
Roland McGrath761b5d72002-12-15 23:58:31 +0000891 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
892 if (errno)
893 printf("ptrace_peektext failed: %s\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000894 strerror(errno));
895 switch (call & 0xffff)
896 {
897 /* x86-64: syscall = 0x0f 0x05 */
898 case 0x050f: currpers = 0; break;
899 /* i386: int 0x80 = 0xcd 0x80 */
900 case 0x80cd: currpers = 1; break;
901 default:
902 currpers = current_personality;
Roland McGrath761b5d72002-12-15 23:58:31 +0000903 fprintf(stderr,
Michal Ludvig0e035502002-09-23 15:41:01 +0000904 "Unknown syscall opcode (0x%04X) while "
905 "detecting personality of process "
906 "PID=%d\n", (int)call, pid);
907 break;
908 }
909#endif
910 if(currpers != current_personality)
911 {
912 char *names[]={"64 bit", "32 bit"};
913 set_personality(currpers);
Roland McGrath761b5d72002-12-15 23:58:31 +0000914 printf("[ Process PID=%d runs in %s mode. ]\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000915 pid, names[current_personality]);
916 }
Roland McGrath761b5d72002-12-15 23:58:31 +0000917 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000918#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000919# define IA64_PSR_IS ((long)1 << 34)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000920 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000921 ia32 = (psr & IA64_PSR_IS) != 0;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000922 if (!(tcp->flags & TCB_INSYSCALL)) {
923 if (ia32) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000924 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000925 return -1;
926 } else {
927 if (upeek (pid, PT_R15, &scno) < 0)
928 return -1;
929 }
Roland McGrathba954762003-03-05 06:29:06 +0000930 /* Check if we return from execve. */
931 if (tcp->flags & TCB_WAITEXECVE) {
932 tcp->flags &= ~TCB_WAITEXECVE;
933 return 0;
934 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000935 } else {
936 /* syscall in progress */
937 if (upeek (pid, PT_R8, &r8) < 0)
938 return -1;
939 if (upeek (pid, PT_R10, &r10) < 0)
940 return -1;
941 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000942#elif defined (ARM)
Roland McGrath0f87c492003-06-03 23:29:04 +0000943 /*
944 * Read complete register set in one go.
945 */
946 if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)&regs) == -1)
947 return -1;
948
949 /*
950 * We only need to grab the syscall number on syscall entry.
951 */
952 if (regs.ARM_ip == 0) {
953 /*
954 * Note: we only deal with only 32-bit CPUs here.
955 */
956 if (regs.ARM_cpsr & 0x20) {
957 /*
958 * Get the Thumb-mode system call number
959 */
960 scno = regs.ARM_r7;
961 } else {
962 /*
963 * Get the ARM-mode system call number
964 */
965 errno = 0;
966 scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
967 if (errno)
968 return -1;
969
970 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
971 tcp->flags &= ~TCB_WAITEXECVE;
972 return 0;
973 }
974
975 if ((scno & 0x0ff00000) != 0x0f900000) {
976 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
977 scno);
978 return -1;
979 }
980
981 /*
982 * Fixup the syscall number
983 */
984 scno &= 0x000fffff;
985 }
986
987 if (tcp->flags & TCB_INSYSCALL) {
988 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
989 tcp->flags &= ~TCB_INSYSCALL;
990 }
991 } else {
992 if (!(tcp->flags & TCB_INSYSCALL)) {
993 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
994 tcp->flags |= TCB_INSYSCALL;
995 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000996 }
997#elif defined (M68K)
998 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
999 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001000#elif defined (MIPS)
1001 if (upeek(pid, REG_A3, &a3) < 0)
1002 return -1;
1003
1004 if(!(tcp->flags & TCB_INSYSCALL)) {
1005 if (upeek(pid, REG_V0, &scno) < 0)
1006 return -1;
1007
1008 if (scno < 0 || scno > nsyscalls) {
1009 if(a3 == 0 || a3 == -1) {
1010 if(debug)
1011 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
1012 return 0;
1013 }
1014 }
1015 } else {
1016 if (upeek(pid, REG_V0, &r2) < 0)
1017 return -1;
1018 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001019#elif defined (ALPHA)
1020 if (upeek(pid, REG_A3, &a3) < 0)
1021 return -1;
1022
1023 if (!(tcp->flags & TCB_INSYSCALL)) {
1024 if (upeek(pid, REG_R0, &scno) < 0)
1025 return -1;
1026
1027 /* Check if we return from execve. */
1028 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1029 tcp->flags &= ~TCB_WAITEXECVE;
1030 return 0;
1031 }
1032
1033 /*
1034 * Do some sanity checks to figure out if it's
1035 * really a syscall entry
1036 */
1037 if (scno < 0 || scno > nsyscalls) {
1038 if (a3 == 0 || a3 == -1) {
1039 if (debug)
1040 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
1041 return 0;
1042 }
1043 }
1044 }
1045 else {
1046 if (upeek(pid, REG_R0, &r0) < 0)
1047 return -1;
1048 }
1049#elif defined (SPARC)
1050 /* Everything we need is in the current register set. */
1051 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
1052 return -1;
1053
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001054 /* If we are entering, then disassemble the syscall trap. */
1055 if (!(tcp->flags & TCB_INSYSCALL)) {
1056 /* Retrieve the syscall trap instruction. */
1057 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001058 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001059 if (errno)
1060 return -1;
1061
1062 /* Disassemble the trap to see what personality to use. */
1063 switch (trap) {
1064 case 0x91d02010:
1065 /* Linux/SPARC syscall trap. */
1066 set_personality(0);
1067 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +00001068 case 0x91d0206d:
1069 /* Linux/SPARC64 syscall trap. */
1070 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
1071 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001072 case 0x91d02000:
1073 /* SunOS syscall trap. (pers 1) */
1074 fprintf(stderr,"syscall: SunOS no support\n");
1075 return -1;
1076 case 0x91d02008:
1077 /* Solaris 2.x syscall trap. (per 2) */
1078 set_personality(1);
Roland McGrath761b5d72002-12-15 23:58:31 +00001079 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001080 case 0x91d02009:
1081 /* NetBSD/FreeBSD syscall trap. */
1082 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1083 return -1;
1084 case 0x91d02027:
1085 /* Solaris 2.x gettimeofday */
1086 set_personality(1);
1087 break;
1088 default:
1089 /* Unknown syscall trap. */
1090 if(tcp->flags & TCB_WAITEXECVE) {
1091 tcp->flags &= ~TCB_WAITEXECVE;
1092 return 0;
1093 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001094 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001095 return -1;
1096 }
1097
1098 /* Extract the system call number from the registers. */
1099 if (trap == 0x91d02027)
1100 scno = 156;
1101 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001102 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001103 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001104 scno = regs.r_o0;
1105 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001106 }
1107 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001108#elif defined(HPPA)
1109 if (upeek(pid, PT_GR20, &scno) < 0)
1110 return -1;
1111 if (!(tcp->flags & TCB_INSYSCALL)) {
1112 /* Check if we return from execve. */
1113 if ((tcp->flags & TCB_WAITEXECVE)) {
1114 tcp->flags &= ~TCB_WAITEXECVE;
1115 return 0;
1116 }
1117 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001118#elif defined(SH)
1119 /*
1120 * In the new syscall ABI, the system call number is in R3.
1121 */
1122 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1123 return -1;
1124
1125 if (scno < 0) {
1126 /* Odd as it may seem, a glibc bug has been known to cause
1127 glibc to issue bogus negative syscall numbers. So for
1128 our purposes, make strace print what it *should* have been */
1129 long correct_scno = (scno & 0xff);
1130 if (debug)
1131 fprintf(stderr,
Michal Ludvig53b320f2002-09-23 13:30:09 +00001132 "Detected glibc bug: bogus system call number = %ld, "
1133 "correcting to %ld\n",
Wichert Akkermanccef6372002-05-01 16:39:22 +00001134 scno,
1135 correct_scno);
1136 scno = correct_scno;
1137 }
1138
1139
1140 if (!(tcp->flags & TCB_INSYSCALL)) {
1141 /* Check if we return from execve. */
1142 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1143 tcp->flags &= ~TCB_WAITEXECVE;
1144 return 0;
1145 }
1146 }
Roland McGrathf5a47772003-06-26 22:40:42 +00001147#elif defined(SH64)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001148 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1149 return -1;
1150 scno &= 0xFFFF;
1151
1152 if (!(tcp->flags & TCB_INSYSCALL)) {
1153 /* Check if we return from execve. */
1154 if (tcp->flags & TCB_WAITEXECVE) {
1155 tcp->flags &= ~TCB_WAITEXECVE;
1156 return 0;
1157 }
1158 }
Roland McGrathf5a47772003-06-26 22:40:42 +00001159#endif /* SH64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001160#endif /* LINUX */
1161#ifdef SUNOS4
1162 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1163 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001164#elif defined(SH)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001165 /* new syscall ABI returns result in R0 */
1166 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1167 return -1;
Roland McGrathf5a47772003-06-26 22:40:42 +00001168#elif defined(SH64)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001169 /* ABI defines result returned in r9 */
1170 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1171 return -1;
1172
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001173#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001174#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001175#ifdef HAVE_PR_SYSCALL
John Hughes25299712001-03-06 10:10:06 +00001176 scno = tcp->status.PR_SYSCALL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001177#else /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001178#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001179 scno = tcp->status.PR_WHAT;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001180#else /* FREEBSD */
1181 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1182 perror("pread");
1183 return -1;
1184 }
1185 switch (regs.r_eax) {
1186 case SYS_syscall:
1187 case SYS___syscall:
1188 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1189 break;
1190 default:
1191 scno = regs.r_eax;
1192 break;
1193 }
1194#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001195#endif /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001196#endif /* USE_PROCFS */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001197 if (!(tcp->flags & TCB_INSYSCALL))
1198 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001199 return 1;
1200}
1201
Pavel Machek4dc3b142000-02-01 17:58:41 +00001202
1203int
1204syscall_fixup(tcp)
1205struct tcb *tcp;
1206{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001207#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001208 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001209#else /* USE_PROCFS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001210 int scno = tcp->scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001211
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001212 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001213 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001214 if (
1215 scno == SYS_fork
1216#ifdef SYS_vfork
1217 || scno == SYS_vfork
1218#endif /* SYS_vfork */
John Hughes4e36a812001-04-18 15:11:51 +00001219#ifdef SYS_fork1
1220 || scno == SYS_fork1
1221#endif /* SYS_fork1 */
1222#ifdef SYS_forkall
1223 || scno == SYS_forkall
1224#endif /* SYS_forkall */
1225#ifdef SYS_rfork1
1226 || scno == SYS_rfork1
1227#endif /* SYS_fork1 */
1228#ifdef SYS_rforkall
1229 || scno == SYS_rforkall
1230#endif /* SYS_rforkall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001231 ) {
1232 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001233 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001234 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001235 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001236 }
1237 else {
1238 fprintf(stderr, "syscall: missing entry\n");
1239 tcp->flags |= TCB_INSYSCALL;
1240 }
1241 }
1242 }
1243 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001244 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001245 fprintf(stderr, "syscall: missing exit\n");
1246 tcp->flags &= ~TCB_INSYSCALL;
1247 }
1248 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001249#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001250#ifdef SUNOS4
1251 if (!(tcp->flags & TCB_INSYSCALL)) {
1252 if (scno == 0) {
1253 fprintf(stderr, "syscall: missing entry\n");
1254 tcp->flags |= TCB_INSYSCALL;
1255 }
1256 }
1257 else {
1258 if (scno != 0) {
1259 if (debug) {
1260 /*
1261 * This happens when a signal handler
1262 * for a signal which interrupted a
1263 * a system call makes another system call.
1264 */
1265 fprintf(stderr, "syscall: missing exit\n");
1266 }
1267 tcp->flags &= ~TCB_INSYSCALL;
1268 }
1269 }
1270#endif /* SUNOS4 */
1271#ifdef LINUX
1272#if defined (I386)
1273 if (upeek(pid, 4*EAX, &eax) < 0)
1274 return -1;
1275 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1276 if (debug)
1277 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1278 return 0;
1279 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001280#elif defined (X86_64)
1281 if (upeek(pid, 8*RAX, &rax) < 0)
1282 return -1;
1283 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1284 if (debug)
1285 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1286 return 0;
1287 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001288#elif defined (S390) || defined (S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001289 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1290 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001291 if (syscall_mode != -ENOSYS)
1292 syscall_mode = tcp->scno;
1293 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001294 if (debug)
1295 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1296 return 0;
1297 }
Roland McGrath96dc5142003-01-20 10:23:04 +00001298 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1299 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1300 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1301 /*
1302 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1303 * flag set for the post-execve SIGTRAP to see and reset.
1304 */
1305 gpr2 = 0;
1306 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001307#elif defined (POWERPC)
1308# define SO_MASK 0x10000000
Roland McGratheb285352003-01-14 09:59:00 +00001309 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001310 return -1;
Roland McGratheb285352003-01-14 09:59:00 +00001311 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001312 return -1;
1313 if (flags & SO_MASK)
1314 result = -result;
1315#elif defined (M68K)
1316 if (upeek(pid, 4*PT_D0, &d0) < 0)
1317 return -1;
1318 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1319 if (debug)
1320 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1321 return 0;
1322 }
1323#elif defined (ARM)
Roland McGrath0f87c492003-06-03 23:29:04 +00001324 /*
1325 * Nothing required
1326 */
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001327#elif defined (HPPA)
1328 if (upeek(pid, PT_GR28, &r28) < 0)
1329 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001330#elif defined(IA64)
1331 if (upeek(pid, PT_R10, &r10) < 0)
1332 return -1;
1333 if (upeek(pid, PT_R8, &r8) < 0)
1334 return -1;
1335 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1336 if (debug)
1337 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1338 return 0;
1339 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001340#endif
1341#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001342 return 1;
1343}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001344
Pavel Machek4dc3b142000-02-01 17:58:41 +00001345int
1346get_error(tcp)
1347struct tcb *tcp;
1348{
1349 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001350#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001351#if defined(S390) || defined(S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001352 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1353 tcp->u_rval = -1;
1354 u_error = -gpr2;
1355 }
1356 else {
1357 tcp->u_rval = gpr2;
1358 u_error = 0;
1359 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001360#else /* !S390 && !S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001361#ifdef I386
1362 if (eax < 0 && -eax < nerrnos) {
1363 tcp->u_rval = -1;
1364 u_error = -eax;
1365 }
1366 else {
1367 tcp->u_rval = eax;
1368 u_error = 0;
1369 }
1370#else /* !I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001371#ifdef X86_64
1372 if (rax < 0 && -rax < nerrnos) {
1373 tcp->u_rval = -1;
1374 u_error = -rax;
1375 }
1376 else {
1377 tcp->u_rval = rax;
1378 u_error = 0;
1379 }
1380#else
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001381#ifdef IA64
1382 if (ia32) {
1383 int err;
1384
1385 err = (int)r8;
1386 if (err < 0 && -err < nerrnos) {
1387 tcp->u_rval = -1;
1388 u_error = -err;
1389 }
1390 else {
1391 tcp->u_rval = err;
1392 u_error = 0;
1393 }
1394 } else {
1395 if (r10) {
1396 tcp->u_rval = -1;
1397 u_error = r8;
1398 } else {
1399 tcp->u_rval = r8;
1400 u_error = 0;
1401 }
1402 }
1403#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001404#ifdef MIPS
1405 if (a3) {
1406 tcp->u_rval = -1;
1407 u_error = r2;
1408 } else {
1409 tcp->u_rval = r2;
1410 u_error = 0;
1411 }
1412#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001413#ifdef POWERPC
1414 if (result && (unsigned) -result < nerrnos) {
1415 tcp->u_rval = -1;
1416 u_error = -result;
1417 }
1418 else {
1419 tcp->u_rval = result;
1420 u_error = 0;
1421 }
1422#else /* !POWERPC */
1423#ifdef M68K
1424 if (d0 && (unsigned) -d0 < nerrnos) {
1425 tcp->u_rval = -1;
1426 u_error = -d0;
1427 }
1428 else {
1429 tcp->u_rval = d0;
1430 u_error = 0;
1431 }
1432#else /* !M68K */
1433#ifdef ARM
Roland McGrath0f87c492003-06-03 23:29:04 +00001434 if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001435 tcp->u_rval = -1;
Roland McGrath0f87c492003-06-03 23:29:04 +00001436 u_error = -regs.ARM_r0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001437 }
1438 else {
Roland McGrath0f87c492003-06-03 23:29:04 +00001439 tcp->u_rval = regs.ARM_r0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001440 u_error = 0;
1441 }
1442#else /* !ARM */
1443#ifdef ALPHA
1444 if (a3) {
1445 tcp->u_rval = -1;
1446 u_error = r0;
1447 }
1448 else {
1449 tcp->u_rval = r0;
1450 u_error = 0;
1451 }
1452#else /* !ALPHA */
1453#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001454 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001455 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001456 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001457 }
1458 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001459 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001460 u_error = 0;
1461 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001462#else /* !SPARC */
1463#ifdef HPPA
1464 if (r28 && (unsigned) -r28 < nerrnos) {
1465 tcp->u_rval = -1;
1466 u_error = -r28;
1467 }
1468 else {
1469 tcp->u_rval = r28;
1470 u_error = 0;
1471 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001472#else
1473#ifdef SH
1474 /* interpret R0 as return value or error number */
1475 if (r0 && (unsigned) -r0 < nerrnos) {
1476 tcp->u_rval = -1;
1477 u_error = -r0;
1478 }
1479 else {
1480 tcp->u_rval = r0;
1481 u_error = 0;
1482 }
Roland McGrathe1e584b2003-06-02 19:18:58 +00001483#else
Roland McGrathf5a47772003-06-26 22:40:42 +00001484#ifdef SH64
Roland McGrathe1e584b2003-06-02 19:18:58 +00001485 /* interpret result as return value or error number */
1486 if (r9 && (unsigned) -r9 < nerrnos) {
1487 tcp->u_rval = -1;
1488 u_error = -r9;
1489 }
1490 else {
1491 tcp->u_rval = r9;
1492 u_error = 0;
1493 }
Roland McGrathf5a47772003-06-26 22:40:42 +00001494#endif /* SH64 */
Wichert Akkermanccef6372002-05-01 16:39:22 +00001495#endif /* SH */
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001496#endif /* HPPA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001497#endif /* SPARC */
1498#endif /* ALPHA */
1499#endif /* ARM */
1500#endif /* M68K */
1501#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001502#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001503#endif /* IA64 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001504#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001505#endif /* I386 */
Michal Ludvig10a88d02002-10-07 14:31:00 +00001506#endif /* S390 || S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001507#endif /* LINUX */
1508#ifdef SUNOS4
1509 /* get error code from user struct */
1510 if (upeek(pid, uoff(u_error), &u_error) < 0)
1511 return -1;
1512 u_error >>= 24; /* u_error is a char */
1513
1514 /* get system call return value */
1515 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1516 return -1;
1517#endif /* SUNOS4 */
1518#ifdef SVR4
1519#ifdef SPARC
1520 /* Judicious guessing goes a long way. */
1521 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1522 tcp->u_rval = -1;
1523 u_error = tcp->status.pr_reg[R_O0];
1524 }
1525 else {
1526 tcp->u_rval = tcp->status.pr_reg[R_O0];
1527 u_error = 0;
1528 }
1529#endif /* SPARC */
1530#ifdef I386
1531 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001532 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001533 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001534 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001535 }
1536 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001537 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001538#ifdef HAVE_LONG_LONG
1539 tcp->u_lrval =
1540 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1541 tcp->status.PR_REG[EAX];
1542#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001543 u_error = 0;
1544 }
1545#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001546#ifdef X86_64
1547 /* Wanna know how to kill an hour single-stepping? */
1548 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1549 tcp->u_rval = -1;
1550 u_error = tcp->status.PR_REG[RAX];
1551 }
1552 else {
1553 tcp->u_rval = tcp->status.PR_REG[RAX];
1554 u_error = 0;
1555 }
1556#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001557#ifdef MIPS
1558 if (tcp->status.pr_reg[CTX_A3]) {
1559 tcp->u_rval = -1;
1560 u_error = tcp->status.pr_reg[CTX_V0];
1561 }
1562 else {
1563 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1564 u_error = 0;
1565 }
1566#endif /* MIPS */
1567#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001568#ifdef FREEBSD
1569 if (regs.r_eflags & PSL_C) {
1570 tcp->u_rval = -1;
1571 u_error = regs.r_eax;
1572 } else {
1573 tcp->u_rval = regs.r_eax;
1574 tcp->u_lrval =
1575 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1576 u_error = 0;
1577 }
Roland McGrath761b5d72002-12-15 23:58:31 +00001578#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001579 tcp->u_error = u_error;
1580 return 1;
1581}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001582
Roland McGrathb69f81b2002-12-21 23:25:18 +00001583int
1584force_result(tcp, error, rval)
1585 struct tcb *tcp;
1586 int error;
1587 long rval;
1588{
1589#ifdef LINUX
1590#if defined(S390) || defined(S390X)
1591 gpr2 = error ? -error : rval;
Roland McGrathd38809b2003-01-10 07:50:21 +00001592 if (upeek(tcp->pid, PT_GPR2, &gpr2) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001593 return -1;
1594 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1595 return -1;
1596#else /* !S390 && !S390X */
1597#ifdef I386
1598 eax = error ? -error : rval;
1599 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1600 return -1;
1601#else /* !I386 */
1602#ifdef X86_64
1603 rax = error ? -error : rval;
1604 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 4), rax) < 0)
1605 return -1;
1606#else
1607#ifdef IA64
1608 if (ia32) {
1609 r8 = error ? -error : rval;
1610 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1611 return -1;
1612 }
1613 else {
1614 if (error) {
1615 r8 = error;
1616 r10 = -1;
1617 }
1618 else {
1619 r8 = rval;
1620 r10 = 0;
1621 }
1622 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1623 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1624 return -1;
1625 }
1626#else /* !IA64 */
1627#ifdef MIPS
1628 if (error) {
1629 r2 = error;
1630 a3 = -1;
1631 }
1632 else {
1633 r2 = rval;
1634 a3 = 0;
1635 }
1636 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1637 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1638 return -1;
1639#else
1640#ifdef POWERPC
Roland McGratheb285352003-01-14 09:59:00 +00001641 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001642 return -1;
1643 if (error) {
1644 flags |= SO_MASK;
1645 result = error;
1646 }
1647 else {
1648 flags &= ~SO_MASK;
1649 result = rval;
1650 }
Roland McGratheb285352003-01-14 09:59:00 +00001651 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1652 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001653 return -1;
1654#else /* !POWERPC */
1655#ifdef M68K
1656 d0 = error ? -error : rval;
1657 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1658 return -1;
1659#else /* !M68K */
1660#ifdef ARM
Roland McGrath7c051d22003-06-26 22:29:32 +00001661 regs.ARM_r0 = error ? -error : rval;
1662 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), regs.ARM_r0) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001663 return -1;
1664#else /* !ARM */
1665#ifdef ALPHA
1666 if (error) {
1667 a3 = -1;
1668 r0 = error;
1669 }
1670 else {
1671 a3 = 0;
1672 r0 = rval;
1673 }
1674 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1675 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1676 return -1;
1677#else /* !ALPHA */
1678#ifdef SPARC
1679 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0)
1680 return -1;
1681 if (error) {
1682 regs.r_psr |= PSR_C;
1683 regs.r_o0 = error;
1684 }
1685 else {
1686 regs.r_psr &= ~PSR_C;
1687 regs.r_o0 = rval;
1688 }
1689 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)&regs, 0) < 0)
1690 return -1;
1691#else /* !SPARC */
1692#ifdef HPPA
1693 r28 = error ? -error : rval;
1694 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1695 return -1;
1696#else
1697#ifdef SH
1698 r0 = error ? -error : rval;
1699 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1700 return -1;
Roland McGrathe1e584b2003-06-02 19:18:58 +00001701#else
Roland McGrathf5a47772003-06-26 22:40:42 +00001702#ifdef SH64
Roland McGrathe1e584b2003-06-02 19:18:58 +00001703 r9 = error ? -error : rval;
1704 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1705 return -1;
Roland McGrathf5a47772003-06-26 22:40:42 +00001706#endif /* SH64 */
Roland McGrathb69f81b2002-12-21 23:25:18 +00001707#endif /* SH */
1708#endif /* HPPA */
1709#endif /* SPARC */
1710#endif /* ALPHA */
1711#endif /* ARM */
1712#endif /* M68K */
1713#endif /* POWERPC */
1714#endif /* MIPS */
1715#endif /* IA64 */
1716#endif /* X86_64 */
1717#endif /* I386 */
1718#endif /* S390 || S390X */
1719#endif /* LINUX */
1720#ifdef SUNOS4
1721 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1722 error << 24) < 0 ||
1723 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1724 return -1;
1725#endif /* SUNOS4 */
1726#ifdef SVR4
1727 /* XXX no clue */
1728 return -1;
1729#endif /* SVR4 */
1730#ifdef FREEBSD
1731 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1732 perror("pread");
1733 return -1;
1734 }
1735 if (error) {
1736 regs.r_eflags |= PSL_C;
1737 regs.r_eax = error;
1738 }
1739 else {
1740 regs.r_eflags &= ~PSL_C;
1741 regs.r_eax = rval;
1742 }
1743 if (pwrite(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1744 perror("pwrite");
1745 return -1;
1746 }
1747#endif /* FREEBSD */
1748
1749 /* All branches reach here on success (only). */
1750 tcp->u_error = error;
1751 tcp->u_rval = rval;
1752 return 0;
1753}
1754
Pavel Machek4dc3b142000-02-01 17:58:41 +00001755int syscall_enter(tcp)
1756struct tcb *tcp;
1757{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001758#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001759 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001760#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001761#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001762#if defined(S390) || defined(S390X)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001763 {
1764 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001765 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1766 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001767 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001768 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001769 for (i = 0; i < tcp->u_nargs; i++) {
Michal Ludvig10a88d02002-10-07 14:31:00 +00001770 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001771 return -1;
1772 }
1773 }
1774#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001775 {
1776 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001777 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1778 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001779 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001780 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001781 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001782 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1783 * for scno somewhere above here!
1784 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001785 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1786 return -1;
1787 }
1788 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001789#elif defined (IA64)
1790 {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001791 if (!ia32) {
1792 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1793 /* be backwards compatible with kernel < 2.4.4... */
1794# ifndef PT_RBS_END
1795# define PT_RBS_END PT_AR_BSP
1796# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001797
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001798 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001799 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001800 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1801 return -1;
1802
1803 sof = (cfm >> 0) & 0x7f;
1804 sol = (cfm >> 7) & 0x7f;
1805 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1806
1807 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1808 && sysent[tcp->scno].nargs != -1)
1809 tcp->u_nargs = sysent[tcp->scno].nargs;
1810 else
1811 tcp->u_nargs = MAX_ARGS;
1812 for (i = 0; i < tcp->u_nargs; ++i) {
1813 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1814 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1815 return -1;
1816 }
1817 } else {
1818 int i;
1819
1820 if (/* EBX = out0 */
1821 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1822 /* ECX = out1 */
1823 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1824 /* EDX = out2 */
1825 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1826 /* ESI = out3 */
1827 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1828 /* EDI = out4 */
1829 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1830 /* EBP = out5 */
1831 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1832 return -1;
1833
1834 for (i = 0; i < 6; ++i)
1835 /* truncate away IVE sign-extension */
1836 tcp->u_arg[i] &= 0xffffffff;
1837
1838 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1839 && sysent[tcp->scno].nargs != -1)
1840 tcp->u_nargs = sysent[tcp->scno].nargs;
1841 else
1842 tcp->u_nargs = 5;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001843 }
1844 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001845#elif defined (MIPS)
1846 {
1847 long sp;
1848 int i, nargs;
1849
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001850 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1851 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001852 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001853 nargs = tcp->u_nargs = MAX_ARGS;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001854 if(nargs > 4) {
1855 if(upeek(pid, REG_SP, &sp) < 0)
1856 return -1;
1857 for(i = 0; i < 4; i++) {
1858 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1859 return -1;
1860 }
1861 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1862 (char *)(tcp->u_arg + 4));
1863 } else {
1864 for(i = 0; i < nargs; i++) {
1865 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1866 return -1;
1867 }
1868 }
1869 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001870#elif defined (POWERPC)
Roland McGrath761b5d72002-12-15 23:58:31 +00001871#ifndef PT_ORIG_R3
1872#define PT_ORIG_R3 34
1873#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001874 {
1875 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001876 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1877 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001878 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001879 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001880 for (i = 0; i < tcp->u_nargs; i++) {
Roland McGratheb285352003-01-14 09:59:00 +00001881 if (upeek(pid, (i==0) ?
1882 (sizeof(unsigned long)*PT_ORIG_R3) :
1883 ((i+PT_R3)*sizeof(unsigned long)),
1884 &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001885 return -1;
1886 }
1887 }
1888#elif defined (SPARC)
1889 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001890 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001891
1892 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1893 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001894 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001895 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001896 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001897 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001898 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001899#elif defined (HPPA)
1900 {
1901 int i;
1902
1903 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1904 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001905 else
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001906 tcp->u_nargs = MAX_ARGS;
1907 for (i = 0; i < tcp->u_nargs; i++) {
1908 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1909 return -1;
1910 }
1911 }
Roland McGrath0f87c492003-06-03 23:29:04 +00001912#elif defined(ARM)
1913 {
1914 int i;
1915
1916 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1917 tcp->u_nargs = sysent[tcp->scno].nargs;
1918 else
1919 tcp->u_nargs = MAX_ARGS;
1920 for (i = 0; i < tcp->u_nargs; i++)
1921 tcp->u_arg[i] = regs.uregs[i];
1922 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001923#elif defined(SH)
1924 {
Roland McGrath761b5d72002-12-15 23:58:31 +00001925 int i;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001926 static int syscall_regs[] = {
1927 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1928 REG_REG0, REG_REG0+1, REG_REG0+2
1929 };
1930
1931 tcp->u_nargs = sysent[tcp->scno].nargs;
1932 for (i = 0; i < tcp->u_nargs; i++) {
1933 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1934 return -1;
1935 }
1936 }
Roland McGrathf5a47772003-06-26 22:40:42 +00001937#elif defined(SH64)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001938 {
1939 int i;
1940 /* Registers used by SH5 Linux system calls for parameters */
1941 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
1942
1943 /*
1944 * TODO: should also check that the number of arguments encoded
1945 * in the trap number matches the number strace expects.
1946 */
1947 /*
1948 assert(sysent[tcp->scno].nargs <
1949 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
1950 */
1951
1952 tcp->u_nargs = sysent[tcp->scno].nargs;
1953 for (i = 0; i < tcp->u_nargs; i++) {
1954 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1955 return -1;
1956 }
1957 }
1958
Michal Ludvig0e035502002-09-23 15:41:01 +00001959#elif defined(X86_64)
1960 {
1961 int i;
1962 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1963 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
1964 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
1965 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001966
Michal Ludvig0e035502002-09-23 15:41:01 +00001967 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1968 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001969 else
Michal Ludvig0e035502002-09-23 15:41:01 +00001970 tcp->u_nargs = MAX_ARGS;
1971 for (i = 0; i < tcp->u_nargs; i++) {
1972 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
1973 return -1;
1974 }
1975 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001976#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001977 {
1978 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001979 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1980 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001981 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001982 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001983 for (i = 0; i < tcp->u_nargs; i++) {
1984 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1985 return -1;
1986 }
1987 }
Roland McGrath761b5d72002-12-15 23:58:31 +00001988#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001989#endif /* LINUX */
1990#ifdef SUNOS4
1991 {
1992 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001993 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1994 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001995 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001996 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001997 for (i = 0; i < tcp->u_nargs; i++) {
1998 struct user *u;
1999
2000 if (upeek(pid, uoff(u_arg[0]) +
2001 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2002 return -1;
2003 }
2004 }
2005#endif /* SUNOS4 */
2006#ifdef SVR4
2007#ifdef MIPS
2008 /*
2009 * SGI is broken: even though it has pr_sysarg, it doesn't
2010 * set them on system call entry. Get a clue.
2011 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002012 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002013 tcp->u_nargs = sysent[tcp->scno].nargs;
2014 else
2015 tcp->u_nargs = tcp->status.pr_nsysarg;
2016 if (tcp->u_nargs > 4) {
2017 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2018 4*sizeof(tcp->u_arg[0]));
2019 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2020 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2021 }
2022 else {
2023 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2024 tcp->u_nargs*sizeof(tcp->u_arg[0]));
2025 }
John Hughes25299712001-03-06 10:10:06 +00002026#elif UNIXWARE >= 2
2027 /*
2028 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2029 */
2030 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2031 tcp->u_nargs = sysent[tcp->scno].nargs;
2032 else
2033 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2034 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2035 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2036#elif defined (HAVE_PR_SYSCALL)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002037 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002038 tcp->u_nargs = sysent[tcp->scno].nargs;
2039 else
2040 tcp->u_nargs = tcp->status.pr_nsysarg;
2041 {
2042 int i;
2043 for (i = 0; i < tcp->u_nargs; i++)
2044 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2045 }
John Hughes25299712001-03-06 10:10:06 +00002046#elif defined (I386)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002047 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002048 tcp->u_nargs = sysent[tcp->scno].nargs;
2049 else
2050 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002051 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002052 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
John Hughes25299712001-03-06 10:10:06 +00002053#else
2054 I DONT KNOW WHAT TO DO
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002055#endif /* !HAVE_PR_SYSCALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002056#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002057#ifdef FREEBSD
2058 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2059 sysent[tcp->scno].nargs > tcp->status.val)
2060 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00002061 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002062 tcp->u_nargs = tcp->status.val;
2063 if (tcp->u_nargs < 0)
2064 tcp->u_nargs = 0;
2065 if (tcp->u_nargs > MAX_ARGS)
2066 tcp->u_nargs = MAX_ARGS;
2067 switch(regs.r_eax) {
2068 case SYS___syscall:
2069 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2070 regs.r_esp + sizeof(int) + sizeof(quad_t));
2071 break;
2072 case SYS_syscall:
2073 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2074 regs.r_esp + 2 * sizeof(int));
2075 break;
2076 default:
2077 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2078 regs.r_esp + sizeof(int));
2079 break;
2080 }
2081#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00002082 return 1;
2083}
2084
2085int
2086trace_syscall(tcp)
2087struct tcb *tcp;
2088{
2089 int sys_res;
2090 struct timeval tv;
2091 int res;
2092
2093 /* Measure the exit time as early as possible to avoid errors. */
2094 if (dtime && (tcp->flags & TCB_INSYSCALL))
2095 gettimeofday(&tv, NULL);
2096
2097 res = get_scno(tcp);
2098 if (res != 1)
2099 return res;
2100
2101 res = syscall_fixup(tcp);
2102 if (res != 1)
2103 return res;
2104
2105 if (tcp->flags & TCB_INSYSCALL) {
2106 long u_error;
2107 res = get_error(tcp);
2108 if (res != 1)
2109 return res;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002110
2111 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002112 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2113 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002114 tcp->flags &= ~TCB_INSYSCALL;
2115 return 0;
2116 }
2117
2118 if (tcp->flags & TCB_REPRINT) {
2119 printleader(tcp);
2120 tprintf("<... ");
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002121 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002122 tprintf("syscall_%lu", tcp->scno);
2123 else
2124 tprintf("%s", sysent[tcp->scno].sys_name);
2125 tprintf(" resumed> ");
2126 }
2127
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002128 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002129 call_count[tcp->scno]++;
2130 if (tcp->u_error)
2131 error_count[tcp->scno]++;
2132 tv_sub(&tv, &tv, &tcp->etime);
2133#ifdef LINUX
2134 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2135 static struct timeval one_tick =
2136 { 0, 1000000 / HZ };
2137
2138 if (tv_nz(&tcp->dtime))
2139 tv = tcp->dtime;
2140 else if (tv_cmp(&tv, &one_tick) > 0) {
2141 if (tv_cmp(&shortest, &one_tick) < 0)
2142 tv = shortest;
2143 else
2144 tv = one_tick;
2145 }
2146 }
2147#endif /* LINUX */
2148 if (tv_cmp(&tv, &shortest) < 0)
2149 shortest = tv;
2150 tv_add(&tv_count[tcp->scno],
2151 &tv_count[tcp->scno], &tv);
2152 tcp->flags &= ~TCB_INSYSCALL;
2153 return 0;
2154 }
2155
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002156 if (tcp->scno >= nsyscalls || tcp->scno < 0
Pavel Machek4dc3b142000-02-01 17:58:41 +00002157 || (qual_flags[tcp->scno] & QUAL_RAW))
2158 sys_res = printargs(tcp);
Michal Ludvig17f8fb32002-11-06 13:17:21 +00002159 else {
2160 if (not_failing_only && tcp->u_error)
Roland McGrath761b5d72002-12-15 23:58:31 +00002161 return 0; /* ignore failed syscalls */
Pavel Machek4dc3b142000-02-01 17:58:41 +00002162 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
Roland McGrath761b5d72002-12-15 23:58:31 +00002163 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00002164 u_error = tcp->u_error;
2165 tprintf(") ");
2166 tabto(acolumn);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002167 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2168 qual_flags[tcp->scno] & QUAL_RAW) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002169 if (u_error)
2170 tprintf("= -1 (errno %ld)", u_error);
2171 else
2172 tprintf("= %#lx", tcp->u_rval);
2173 }
2174 else if (!(sys_res & RVAL_NONE) && u_error) {
2175 switch (u_error) {
2176#ifdef LINUX
2177 case ERESTARTSYS:
2178 tprintf("= ? ERESTARTSYS (To be restarted)");
2179 break;
2180 case ERESTARTNOINTR:
2181 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2182 break;
2183 case ERESTARTNOHAND:
2184 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2185 break;
2186#endif /* LINUX */
2187 default:
2188 tprintf("= -1 ");
Wichert Akkerman4527dae2002-03-31 19:03:29 +00002189 if (u_error < 0)
2190 tprintf("E??? (errno %ld)", u_error);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002191 else if (u_error < nerrnos)
Roland McGrath761b5d72002-12-15 23:58:31 +00002192 tprintf("%s (%s)", errnoent[u_error],
2193 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00002194 else
Roland McGrath761b5d72002-12-15 23:58:31 +00002195 tprintf("ERRNO_%ld (%s)", u_error,
2196 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00002197 break;
2198 }
2199 }
2200 else {
2201 if (sys_res & RVAL_NONE)
2202 tprintf("= ?");
2203 else {
2204 switch (sys_res & RVAL_MASK) {
2205 case RVAL_HEX:
2206 tprintf("= %#lx", tcp->u_rval);
2207 break;
2208 case RVAL_OCTAL:
2209 tprintf("= %#lo", tcp->u_rval);
2210 break;
2211 case RVAL_UDECIMAL:
2212 tprintf("= %lu", tcp->u_rval);
2213 break;
2214 case RVAL_DECIMAL:
2215 tprintf("= %ld", tcp->u_rval);
2216 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002217#ifdef HAVE_LONG_LONG
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002218 case RVAL_LHEX:
2219 tprintf("= %#llx", tcp->u_lrval);
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002220 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002221 case RVAL_LOCTAL:
2222 tprintf("= %#llo", tcp->u_lrval);
2223 break;
2224 case RVAL_LUDECIMAL:
2225 tprintf("= %llu", tcp->u_lrval);
2226 break;
2227 case RVAL_LDECIMAL:
2228 tprintf("= %lld", tcp->u_lrval);
2229 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002230#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00002231 default:
2232 fprintf(stderr,
2233 "invalid rval format\n");
2234 break;
2235 }
2236 }
2237 if ((sys_res & RVAL_STR) && tcp->auxstr)
2238 tprintf(" (%s)", tcp->auxstr);
2239 }
2240 if (dtime) {
2241 tv_sub(&tv, &tv, &tcp->etime);
2242 tprintf(" <%ld.%06ld>",
2243 (long) tv.tv_sec, (long) tv.tv_usec);
2244 }
2245 printtrailer(tcp);
2246
2247 dumpio(tcp);
2248 if (fflush(tcp->outf) == EOF)
2249 return -1;
2250 tcp->flags &= ~TCB_INSYSCALL;
2251 return 0;
2252 }
2253
2254 /* Entering system call */
2255 res = syscall_enter(tcp);
2256 if (res != 1)
2257 return res;
2258
Pavel Machekd8ae7e32000-02-01 17:17:25 +00002259 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002260#ifdef LINUX
Michal Ludvig0e035502002-09-23 15:41:01 +00002261#if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002262 case SYS_socketcall:
2263 decode_subcall(tcp, SYS_socket_subcall,
2264 SYS_socket_nsubcalls, deref_style);
2265 break;
2266 case SYS_ipc:
2267 decode_subcall(tcp, SYS_ipc_subcall,
2268 SYS_ipc_nsubcalls, shift_style);
2269 break;
Michal Ludvig0e035502002-09-23 15:41:01 +00002270#endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002271#ifdef SPARC
2272 case SYS_socketcall:
2273 sparc_socket_decode (tcp);
2274 break;
2275#endif
2276#endif /* LINUX */
2277#ifdef SVR4
2278#ifdef SYS_pgrpsys_subcall
2279 case SYS_pgrpsys:
2280 decode_subcall(tcp, SYS_pgrpsys_subcall,
2281 SYS_pgrpsys_nsubcalls, shift_style);
2282 break;
2283#endif /* SYS_pgrpsys_subcall */
2284#ifdef SYS_sigcall_subcall
2285 case SYS_sigcall:
2286 decode_subcall(tcp, SYS_sigcall_subcall,
2287 SYS_sigcall_nsubcalls, mask_style);
2288 break;
2289#endif /* SYS_sigcall_subcall */
2290 case SYS_msgsys:
2291 decode_subcall(tcp, SYS_msgsys_subcall,
2292 SYS_msgsys_nsubcalls, shift_style);
2293 break;
2294 case SYS_shmsys:
2295 decode_subcall(tcp, SYS_shmsys_subcall,
2296 SYS_shmsys_nsubcalls, shift_style);
2297 break;
2298 case SYS_semsys:
2299 decode_subcall(tcp, SYS_semsys_subcall,
2300 SYS_semsys_nsubcalls, shift_style);
2301 break;
2302#if 0 /* broken */
2303 case SYS_utssys:
2304 decode_subcall(tcp, SYS_utssys_subcall,
2305 SYS_utssys_nsubcalls, shift_style);
2306 break;
2307#endif
2308 case SYS_sysfs:
2309 decode_subcall(tcp, SYS_sysfs_subcall,
2310 SYS_sysfs_nsubcalls, shift_style);
2311 break;
2312 case SYS_spcall:
2313 decode_subcall(tcp, SYS_spcall_subcall,
2314 SYS_spcall_nsubcalls, shift_style);
2315 break;
2316#ifdef SYS_context_subcall
2317 case SYS_context:
2318 decode_subcall(tcp, SYS_context_subcall,
2319 SYS_context_nsubcalls, shift_style);
2320 break;
2321#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00002322#ifdef SYS_door_subcall
2323 case SYS_door:
2324 decode_subcall(tcp, SYS_door_subcall,
2325 SYS_door_nsubcalls, door_style);
2326 break;
2327#endif /* SYS_door_subcall */
John Hughesbdf48f52001-03-06 15:08:09 +00002328#ifdef SYS_kaio_subcall
2329 case SYS_kaio:
2330 decode_subcall(tcp, SYS_kaio_subcall,
2331 SYS_kaio_nsubcalls, shift_style);
2332 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002333#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002334#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002335#ifdef FREEBSD
2336 case SYS_msgsys:
2337 case SYS_shmsys:
2338 case SYS_semsys:
2339 decode_subcall(tcp, 0, 0, table_style);
2340 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002341#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002342#ifdef SUNOS4
2343 case SYS_semsys:
2344 decode_subcall(tcp, SYS_semsys_subcall,
2345 SYS_semsys_nsubcalls, shift_style);
2346 break;
2347 case SYS_msgsys:
2348 decode_subcall(tcp, SYS_msgsys_subcall,
2349 SYS_msgsys_nsubcalls, shift_style);
2350 break;
2351 case SYS_shmsys:
2352 decode_subcall(tcp, SYS_shmsys_subcall,
2353 SYS_shmsys_nsubcalls, shift_style);
2354 break;
2355#endif
2356 }
2357
2358 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002359 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002360 tcp->flags |= TCB_INSYSCALL;
2361 return 0;
2362 }
2363
2364 if (cflag) {
2365 gettimeofday(&tcp->etime, NULL);
2366 tcp->flags |= TCB_INSYSCALL;
2367 return 0;
2368 }
2369
2370 printleader(tcp);
2371 tcp->flags &= ~TCB_REPRINT;
2372 tcp_last = tcp;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002373 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002374 tprintf("syscall_%lu(", tcp->scno);
2375 else
2376 tprintf("%s(", sysent[tcp->scno].sys_name);
Roland McGrath761b5d72002-12-15 23:58:31 +00002377 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002378 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2379 sys_res = printargs(tcp);
2380 else
2381 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2382 if (fflush(tcp->outf) == EOF)
2383 return -1;
2384 tcp->flags |= TCB_INSYSCALL;
2385 /* Measure the entrance time as late as possible to avoid errors. */
2386 if (dtime)
2387 gettimeofday(&tcp->etime, NULL);
2388 return sys_res;
2389}
2390
2391int
2392printargs(tcp)
2393struct tcb *tcp;
2394{
2395 if (entering(tcp)) {
2396 int i;
2397
2398 for (i = 0; i < tcp->u_nargs; i++)
2399 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2400 }
2401 return 0;
2402}
2403
2404long
2405getrval2(tcp)
2406struct tcb *tcp;
2407{
2408 long val = -1;
2409
2410#ifdef LINUX
2411#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002412 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002413 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
2414 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002415 val = regs.r_o1;
Roland McGrath6b1d43e2003-03-31 01:05:01 +00002416#elif defined(SH)
2417 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2418 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002419#endif /* SPARC */
2420#endif /* LINUX */
2421
2422#ifdef SUNOS4
2423 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2424 return -1;
2425#endif /* SUNOS4 */
2426
2427#ifdef SVR4
2428#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002429 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002430#endif /* SPARC */
2431#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002432 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002433#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00002434#ifdef X86_64
2435 val = tcp->status.PR_REG[RDX];
2436#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002437#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002438 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002439#endif /* MIPS */
2440#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002441#ifdef FREEBSD
2442 struct reg regs;
2443 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
2444 val = regs.r_edx;
Roland McGrath761b5d72002-12-15 23:58:31 +00002445#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002446 return val;
2447}
2448
2449/*
2450 * Apparently, indirect system calls have already be converted by ptrace(2),
2451 * so if you see "indir" this program has gone astray.
2452 */
2453int
2454sys_indir(tcp)
2455struct tcb *tcp;
2456{
2457 int i, scno, nargs;
2458
2459 if (entering(tcp)) {
2460 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2461 fprintf(stderr, "Bogus syscall: %u\n", scno);
2462 return 0;
2463 }
2464 nargs = sysent[scno].nargs;
2465 tprintf("%s", sysent[scno].sys_name);
2466 for (i = 0; i < nargs; i++)
2467 tprintf(", %#lx", tcp->u_arg[i+1]);
2468 }
2469 return 0;
2470}
2471
2472static int
2473time_cmp(a, b)
2474void *a;
2475void *b;
2476{
2477 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2478}
2479
2480static int
2481syscall_cmp(a, b)
2482void *a;
2483void *b;
2484{
2485 return strcmp(sysent[*((int *) a)].sys_name,
2486 sysent[*((int *) b)].sys_name);
2487}
2488
2489static int
2490count_cmp(a, b)
2491void *a;
2492void *b;
2493{
2494 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2495
2496 return (m < n) ? 1 : (m > n) ? -1 : 0;
2497}
2498
2499static int (*sortfun)();
2500static struct timeval overhead = { -1, -1 };
2501
2502void
2503set_sortby(sortby)
2504char *sortby;
2505{
2506 if (strcmp(sortby, "time") == 0)
2507 sortfun = time_cmp;
2508 else if (strcmp(sortby, "calls") == 0)
2509 sortfun = count_cmp;
2510 else if (strcmp(sortby, "name") == 0)
2511 sortfun = syscall_cmp;
2512 else if (strcmp(sortby, "nothing") == 0)
2513 sortfun = NULL;
2514 else {
2515 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2516 exit(1);
2517 }
2518}
2519
2520void set_overhead(n)
2521int n;
2522{
2523 overhead.tv_sec = n / 1000000;
2524 overhead.tv_usec = n % 1000000;
2525}
2526
2527void
2528call_summary(outf)
2529FILE *outf;
2530{
2531 int i, j;
2532 int call_cum, error_cum;
2533 struct timeval tv_cum, dtv;
2534 double percent;
2535 char *dashes = "-------------------------";
2536 char error_str[16];
2537
2538 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2539 if (overhead.tv_sec == -1) {
2540 tv_mul(&overhead, &shortest, 8);
2541 tv_div(&overhead, &overhead, 10);
2542 }
2543 for (i = 0; i < nsyscalls; i++) {
2544 sorted_count[i] = i;
2545 if (call_count[i] == 0)
2546 continue;
2547 tv_mul(&dtv, &overhead, call_count[i]);
2548 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2549 call_cum += call_count[i];
2550 error_cum += error_count[i];
2551 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2552 }
2553 if (sortfun)
2554 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2555 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2556 "% time", "seconds", "usecs/call",
2557 "calls", "errors", "syscall");
2558 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2559 dashes, dashes, dashes, dashes, dashes, dashes);
2560 for (i = 0; i < nsyscalls; i++) {
2561 j = sorted_count[i];
2562 if (call_count[j] == 0)
2563 continue;
2564 tv_div(&dtv, &tv_count[j], call_count[j]);
2565 if (error_count[j])
2566 sprintf(error_str, "%d", error_count[j]);
2567 else
2568 error_str[0] = '\0';
2569 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2570 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2571 percent, (long) tv_count[j].tv_sec,
2572 (long) tv_count[j].tv_usec,
2573 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2574 call_count[j], error_str, sysent[j].sys_name);
2575 }
2576 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2577 dashes, dashes, dashes, dashes, dashes, dashes);
2578 if (error_cum)
2579 sprintf(error_str, "%d", error_cum);
2580 else
2581 error_str[0] = '\0';
2582 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2583 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2584 call_cum, error_str, "total");
2585}