blob: 39ee93c32b6c7fe45710b0f828e0cdbfc299bc32 [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 McGrathe1e584b2003-06-02 19:18:58 +0000716#elif defined(SHMEDIA)
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)
Michal Ludvig882eda82002-11-11 12:50:47 +0000737 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000738 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000739 if (syscall_mode != -ENOSYS) {
Michal Ludvig882eda82002-11-11 12:50:47 +0000740 /*
741 * Since kernel version 2.5.44 the scno gets passed in gpr2.
742 */
743 scno = syscall_mode;
744 }
Roland McGrath96dc5142003-01-20 10:23:04 +0000745 if (tcp->flags & TCB_WAITEXECVE) {
746 /*
747 * When the execve system call completes successfully, the
748 * new process still has -ENOSYS (old style) or __NR_execve
749 * (new style) in gpr2. We cannot recover the scno again
750 * by disassembly, because the image that executed the
751 * syscall is gone now. Fortunately, we don't want it. We
752 * leave the flag set so that syscall_fixup can fake the
753 * result.
754 */
755 if (tcp->flags & TCB_INSYSCALL)
756 return 1;
757 /*
758 * This is the SIGTRAP after execve. We cannot try to read
759 * the system call here either.
760 */
761 tcp->flags &= ~TCB_WAITEXECVE;
762 return 0;
763 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000764 else {
765 /*
766 * Old style of "passing" the scno via the SVC instruction.
767 */
768
769 long opcode, offset_reg, tmp;
770 void * svc_addr;
771 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
772 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
773 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
774 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
Roland McGrath761b5d72002-12-15 23:58:31 +0000775
Michal Ludvig882eda82002-11-11 12:50:47 +0000776 if (upeek(pid, PT_PSWADDR, &pc) < 0)
777 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000778 errno = 0;
Michal Ludvig882eda82002-11-11 12:50:47 +0000779 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +0000780 if (errno) {
781 perror("peektext(pc-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +0000782 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000783 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000784
785 /*
786 * We have to check if the SVC got executed directly or via an
787 * EXECUTE instruction. In case of EXECUTE it is necessary to do
788 * instruction decoding to derive the system call number.
789 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
790 * so that this doesn't work if a SVC opcode is part of an EXECUTE
791 * opcode. Since there is no way to find out the opcode size this
792 * is the best we can do...
793 */
794
795 if ((opcode & 0xff00) == 0x0a00) {
796 /* SVC opcode */
797 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +0000798 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000799 else {
800 /* SVC got executed by EXECUTE instruction */
801
802 /*
803 * Do instruction decoding of EXECUTE. If you really want to
804 * understand this, read the Principles of Operations.
805 */
806 svc_addr = (void *) (opcode & 0xfff);
807
808 tmp = 0;
809 offset_reg = (opcode & 0x000f0000) >> 16;
810 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
811 return -1;
812 svc_addr += tmp;
813
814 tmp = 0;
815 offset_reg = (opcode & 0x0000f000) >> 12;
816 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
817 return -1;
818 svc_addr += tmp;
819
820 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
821 if (errno)
822 return -1;
823#if defined(S390X)
824 scno >>= 48;
825#else
826 scno >>= 16;
827#endif
828 tmp = 0;
829 offset_reg = (opcode & 0x00f00000) >> 20;
830 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
831 return -1;
832
833 scno = (scno | tmp) & 0xff;
834 }
835 }
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000836#elif defined (POWERPC)
Roland McGratheb285352003-01-14 09:59:00 +0000837 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000838 return -1;
839 if (!(tcp->flags & TCB_INSYSCALL)) {
840 /* Check if we return from execve. */
841 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
842 tcp->flags &= ~TCB_WAITEXECVE;
843 return 0;
844 }
845 }
846#elif defined (I386)
847 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
848 return -1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000849#elif defined (X86_64)
850 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
851 return -1;
852
Roland McGrath761b5d72002-12-15 23:58:31 +0000853 if (!(tcp->flags & TCB_INSYSCALL)) {
854 static int currpers=-1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000855 long val;
856
857 /* Check CS register value. On x86-64 linux it is:
858 * 0x33 for long mode (64 bit)
859 * 0x23 for compatibility mode (32 bit)
Roland McGrath761b5d72002-12-15 23:58:31 +0000860 * It takes only one ptrace and thus doesn't need
Michal Ludvig0e035502002-09-23 15:41:01 +0000861 * to be cached.
862 */
863 if (upeek(pid, 8*CS, &val) < 0)
864 return -1;
865 switch(val)
866 {
867 case 0x23: currpers = 1; break;
868 case 0x33: currpers = 0; break;
869 default:
870 fprintf(stderr, "Unknown value CS=0x%02X while "
871 "detecting personality of process "
872 "PID=%d\n", (int)val, pid);
873 currpers = current_personality;
874 break;
875 }
876#if 0
877 /* This version analyzes the opcode of a syscall instruction.
878 * (int 0x80 on i386 vs. syscall on x86-64)
879 * It works, but is too complicated.
880 */
881 unsigned long val, rip, i;
882
883 if(upeek(pid, 8*RIP, &rip)<0)
884 perror("upeek(RIP)");
Roland McGrath761b5d72002-12-15 23:58:31 +0000885
Michal Ludvig0e035502002-09-23 15:41:01 +0000886 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
887 rip-=2;
888 errno = 0;
889
Roland McGrath761b5d72002-12-15 23:58:31 +0000890 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
891 if (errno)
892 printf("ptrace_peektext failed: %s\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000893 strerror(errno));
894 switch (call & 0xffff)
895 {
896 /* x86-64: syscall = 0x0f 0x05 */
897 case 0x050f: currpers = 0; break;
898 /* i386: int 0x80 = 0xcd 0x80 */
899 case 0x80cd: currpers = 1; break;
900 default:
901 currpers = current_personality;
Roland McGrath761b5d72002-12-15 23:58:31 +0000902 fprintf(stderr,
Michal Ludvig0e035502002-09-23 15:41:01 +0000903 "Unknown syscall opcode (0x%04X) while "
904 "detecting personality of process "
905 "PID=%d\n", (int)call, pid);
906 break;
907 }
908#endif
909 if(currpers != current_personality)
910 {
911 char *names[]={"64 bit", "32 bit"};
912 set_personality(currpers);
Roland McGrath761b5d72002-12-15 23:58:31 +0000913 printf("[ Process PID=%d runs in %s mode. ]\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000914 pid, names[current_personality]);
915 }
Roland McGrath761b5d72002-12-15 23:58:31 +0000916 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000917#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000918# define IA64_PSR_IS ((long)1 << 34)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000919 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000920 ia32 = (psr & IA64_PSR_IS) != 0;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000921 if (!(tcp->flags & TCB_INSYSCALL)) {
922 if (ia32) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000923 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000924 return -1;
925 } else {
926 if (upeek (pid, PT_R15, &scno) < 0)
927 return -1;
928 }
Roland McGrathba954762003-03-05 06:29:06 +0000929 /* Check if we return from execve. */
930 if (tcp->flags & TCB_WAITEXECVE) {
931 tcp->flags &= ~TCB_WAITEXECVE;
932 return 0;
933 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000934 } else {
935 /* syscall in progress */
936 if (upeek (pid, PT_R8, &r8) < 0)
937 return -1;
938 if (upeek (pid, PT_R10, &r10) < 0)
939 return -1;
940 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000941#elif defined (ARM)
Roland McGrath0f87c492003-06-03 23:29:04 +0000942 /*
943 * Read complete register set in one go.
944 */
945 if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)&regs) == -1)
946 return -1;
947
948 /*
949 * We only need to grab the syscall number on syscall entry.
950 */
951 if (regs.ARM_ip == 0) {
952 /*
953 * Note: we only deal with only 32-bit CPUs here.
954 */
955 if (regs.ARM_cpsr & 0x20) {
956 /*
957 * Get the Thumb-mode system call number
958 */
959 scno = regs.ARM_r7;
960 } else {
961 /*
962 * Get the ARM-mode system call number
963 */
964 errno = 0;
965 scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
966 if (errno)
967 return -1;
968
969 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
970 tcp->flags &= ~TCB_WAITEXECVE;
971 return 0;
972 }
973
974 if ((scno & 0x0ff00000) != 0x0f900000) {
975 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
976 scno);
977 return -1;
978 }
979
980 /*
981 * Fixup the syscall number
982 */
983 scno &= 0x000fffff;
984 }
985
986 if (tcp->flags & TCB_INSYSCALL) {
987 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
988 tcp->flags &= ~TCB_INSYSCALL;
989 }
990 } else {
991 if (!(tcp->flags & TCB_INSYSCALL)) {
992 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
993 tcp->flags |= TCB_INSYSCALL;
994 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000995 }
996#elif defined (M68K)
997 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
998 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +0000999#elif defined (MIPS)
1000 if (upeek(pid, REG_A3, &a3) < 0)
1001 return -1;
1002
1003 if(!(tcp->flags & TCB_INSYSCALL)) {
1004 if (upeek(pid, REG_V0, &scno) < 0)
1005 return -1;
1006
1007 if (scno < 0 || scno > nsyscalls) {
1008 if(a3 == 0 || a3 == -1) {
1009 if(debug)
1010 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
1011 return 0;
1012 }
1013 }
1014 } else {
1015 if (upeek(pid, REG_V0, &r2) < 0)
1016 return -1;
1017 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001018#elif defined (ALPHA)
1019 if (upeek(pid, REG_A3, &a3) < 0)
1020 return -1;
1021
1022 if (!(tcp->flags & TCB_INSYSCALL)) {
1023 if (upeek(pid, REG_R0, &scno) < 0)
1024 return -1;
1025
1026 /* Check if we return from execve. */
1027 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1028 tcp->flags &= ~TCB_WAITEXECVE;
1029 return 0;
1030 }
1031
1032 /*
1033 * Do some sanity checks to figure out if it's
1034 * really a syscall entry
1035 */
1036 if (scno < 0 || scno > nsyscalls) {
1037 if (a3 == 0 || a3 == -1) {
1038 if (debug)
1039 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
1040 return 0;
1041 }
1042 }
1043 }
1044 else {
1045 if (upeek(pid, REG_R0, &r0) < 0)
1046 return -1;
1047 }
1048#elif defined (SPARC)
1049 /* Everything we need is in the current register set. */
1050 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
1051 return -1;
1052
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001053 /* If we are entering, then disassemble the syscall trap. */
1054 if (!(tcp->flags & TCB_INSYSCALL)) {
1055 /* Retrieve the syscall trap instruction. */
1056 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001057 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001058 if (errno)
1059 return -1;
1060
1061 /* Disassemble the trap to see what personality to use. */
1062 switch (trap) {
1063 case 0x91d02010:
1064 /* Linux/SPARC syscall trap. */
1065 set_personality(0);
1066 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +00001067 case 0x91d0206d:
1068 /* Linux/SPARC64 syscall trap. */
1069 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
1070 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001071 case 0x91d02000:
1072 /* SunOS syscall trap. (pers 1) */
1073 fprintf(stderr,"syscall: SunOS no support\n");
1074 return -1;
1075 case 0x91d02008:
1076 /* Solaris 2.x syscall trap. (per 2) */
1077 set_personality(1);
Roland McGrath761b5d72002-12-15 23:58:31 +00001078 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001079 case 0x91d02009:
1080 /* NetBSD/FreeBSD syscall trap. */
1081 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1082 return -1;
1083 case 0x91d02027:
1084 /* Solaris 2.x gettimeofday */
1085 set_personality(1);
1086 break;
1087 default:
1088 /* Unknown syscall trap. */
1089 if(tcp->flags & TCB_WAITEXECVE) {
1090 tcp->flags &= ~TCB_WAITEXECVE;
1091 return 0;
1092 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001093 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001094 return -1;
1095 }
1096
1097 /* Extract the system call number from the registers. */
1098 if (trap == 0x91d02027)
1099 scno = 156;
1100 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001101 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001102 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001103 scno = regs.r_o0;
1104 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001105 }
1106 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001107#elif defined(HPPA)
1108 if (upeek(pid, PT_GR20, &scno) < 0)
1109 return -1;
1110 if (!(tcp->flags & TCB_INSYSCALL)) {
1111 /* Check if we return from execve. */
1112 if ((tcp->flags & TCB_WAITEXECVE)) {
1113 tcp->flags &= ~TCB_WAITEXECVE;
1114 return 0;
1115 }
1116 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001117#elif defined(SH)
1118 /*
1119 * In the new syscall ABI, the system call number is in R3.
1120 */
1121 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1122 return -1;
1123
1124 if (scno < 0) {
1125 /* Odd as it may seem, a glibc bug has been known to cause
1126 glibc to issue bogus negative syscall numbers. So for
1127 our purposes, make strace print what it *should* have been */
1128 long correct_scno = (scno & 0xff);
1129 if (debug)
1130 fprintf(stderr,
Michal Ludvig53b320f2002-09-23 13:30:09 +00001131 "Detected glibc bug: bogus system call number = %ld, "
1132 "correcting to %ld\n",
Wichert Akkermanccef6372002-05-01 16:39:22 +00001133 scno,
1134 correct_scno);
1135 scno = correct_scno;
1136 }
1137
1138
1139 if (!(tcp->flags & TCB_INSYSCALL)) {
1140 /* Check if we return from execve. */
1141 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1142 tcp->flags &= ~TCB_WAITEXECVE;
1143 return 0;
1144 }
1145 }
Roland McGrathe1e584b2003-06-02 19:18:58 +00001146#elif defined(SHMEDIA)
1147 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1148 return -1;
1149 scno &= 0xFFFF;
1150
1151 if (!(tcp->flags & TCB_INSYSCALL)) {
1152 /* Check if we return from execve. */
1153 if (tcp->flags & TCB_WAITEXECVE) {
1154 tcp->flags &= ~TCB_WAITEXECVE;
1155 return 0;
1156 }
1157 }
1158#endif /* SHMEDIA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001159#endif /* LINUX */
1160#ifdef SUNOS4
1161 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1162 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001163#elif defined(SH)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001164 /* new syscall ABI returns result in R0 */
1165 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1166 return -1;
1167#elif defined(SHMEDIA)
1168 /* ABI defines result returned in r9 */
1169 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1170 return -1;
1171
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001172#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001173#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001174#ifdef HAVE_PR_SYSCALL
John Hughes25299712001-03-06 10:10:06 +00001175 scno = tcp->status.PR_SYSCALL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001176#else /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001177#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001178 scno = tcp->status.PR_WHAT;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001179#else /* FREEBSD */
1180 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1181 perror("pread");
1182 return -1;
1183 }
1184 switch (regs.r_eax) {
1185 case SYS_syscall:
1186 case SYS___syscall:
1187 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1188 break;
1189 default:
1190 scno = regs.r_eax;
1191 break;
1192 }
1193#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001194#endif /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001195#endif /* USE_PROCFS */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001196 if (!(tcp->flags & TCB_INSYSCALL))
1197 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001198 return 1;
1199}
1200
Pavel Machek4dc3b142000-02-01 17:58:41 +00001201
1202int
1203syscall_fixup(tcp)
1204struct tcb *tcp;
1205{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001206#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001207 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001208#else /* USE_PROCFS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001209 int scno = tcp->scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001210
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001211 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001212 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001213 if (
1214 scno == SYS_fork
1215#ifdef SYS_vfork
1216 || scno == SYS_vfork
1217#endif /* SYS_vfork */
John Hughes4e36a812001-04-18 15:11:51 +00001218#ifdef SYS_fork1
1219 || scno == SYS_fork1
1220#endif /* SYS_fork1 */
1221#ifdef SYS_forkall
1222 || scno == SYS_forkall
1223#endif /* SYS_forkall */
1224#ifdef SYS_rfork1
1225 || scno == SYS_rfork1
1226#endif /* SYS_fork1 */
1227#ifdef SYS_rforkall
1228 || scno == SYS_rforkall
1229#endif /* SYS_rforkall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001230 ) {
1231 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001232 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001233 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001234 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001235 }
1236 else {
1237 fprintf(stderr, "syscall: missing entry\n");
1238 tcp->flags |= TCB_INSYSCALL;
1239 }
1240 }
1241 }
1242 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001243 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001244 fprintf(stderr, "syscall: missing exit\n");
1245 tcp->flags &= ~TCB_INSYSCALL;
1246 }
1247 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001248#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001249#ifdef SUNOS4
1250 if (!(tcp->flags & TCB_INSYSCALL)) {
1251 if (scno == 0) {
1252 fprintf(stderr, "syscall: missing entry\n");
1253 tcp->flags |= TCB_INSYSCALL;
1254 }
1255 }
1256 else {
1257 if (scno != 0) {
1258 if (debug) {
1259 /*
1260 * This happens when a signal handler
1261 * for a signal which interrupted a
1262 * a system call makes another system call.
1263 */
1264 fprintf(stderr, "syscall: missing exit\n");
1265 }
1266 tcp->flags &= ~TCB_INSYSCALL;
1267 }
1268 }
1269#endif /* SUNOS4 */
1270#ifdef LINUX
1271#if defined (I386)
1272 if (upeek(pid, 4*EAX, &eax) < 0)
1273 return -1;
1274 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1275 if (debug)
1276 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1277 return 0;
1278 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001279#elif defined (X86_64)
1280 if (upeek(pid, 8*RAX, &rax) < 0)
1281 return -1;
1282 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1283 if (debug)
1284 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1285 return 0;
1286 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001287#elif defined (S390) || defined (S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001288 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1289 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001290 if (syscall_mode != -ENOSYS)
1291 syscall_mode = tcp->scno;
1292 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001293 if (debug)
1294 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1295 return 0;
1296 }
Roland McGrath96dc5142003-01-20 10:23:04 +00001297 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1298 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1299 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1300 /*
1301 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1302 * flag set for the post-execve SIGTRAP to see and reset.
1303 */
1304 gpr2 = 0;
1305 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001306#elif defined (POWERPC)
1307# define SO_MASK 0x10000000
Roland McGratheb285352003-01-14 09:59:00 +00001308 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001309 return -1;
Roland McGratheb285352003-01-14 09:59:00 +00001310 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001311 return -1;
1312 if (flags & SO_MASK)
1313 result = -result;
1314#elif defined (M68K)
1315 if (upeek(pid, 4*PT_D0, &d0) < 0)
1316 return -1;
1317 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1318 if (debug)
1319 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1320 return 0;
1321 }
1322#elif defined (ARM)
Roland McGrath0f87c492003-06-03 23:29:04 +00001323 /*
1324 * Nothing required
1325 */
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001326#elif defined (HPPA)
1327 if (upeek(pid, PT_GR28, &r28) < 0)
1328 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001329#elif defined(IA64)
1330 if (upeek(pid, PT_R10, &r10) < 0)
1331 return -1;
1332 if (upeek(pid, PT_R8, &r8) < 0)
1333 return -1;
1334 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1335 if (debug)
1336 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1337 return 0;
1338 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001339#endif
1340#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001341 return 1;
1342}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001343
Pavel Machek4dc3b142000-02-01 17:58:41 +00001344int
1345get_error(tcp)
1346struct tcb *tcp;
1347{
1348 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001349#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001350#if defined(S390) || defined(S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001351 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1352 tcp->u_rval = -1;
1353 u_error = -gpr2;
1354 }
1355 else {
1356 tcp->u_rval = gpr2;
1357 u_error = 0;
1358 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001359#else /* !S390 && !S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001360#ifdef I386
1361 if (eax < 0 && -eax < nerrnos) {
1362 tcp->u_rval = -1;
1363 u_error = -eax;
1364 }
1365 else {
1366 tcp->u_rval = eax;
1367 u_error = 0;
1368 }
1369#else /* !I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001370#ifdef X86_64
1371 if (rax < 0 && -rax < nerrnos) {
1372 tcp->u_rval = -1;
1373 u_error = -rax;
1374 }
1375 else {
1376 tcp->u_rval = rax;
1377 u_error = 0;
1378 }
1379#else
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001380#ifdef IA64
1381 if (ia32) {
1382 int err;
1383
1384 err = (int)r8;
1385 if (err < 0 && -err < nerrnos) {
1386 tcp->u_rval = -1;
1387 u_error = -err;
1388 }
1389 else {
1390 tcp->u_rval = err;
1391 u_error = 0;
1392 }
1393 } else {
1394 if (r10) {
1395 tcp->u_rval = -1;
1396 u_error = r8;
1397 } else {
1398 tcp->u_rval = r8;
1399 u_error = 0;
1400 }
1401 }
1402#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001403#ifdef MIPS
1404 if (a3) {
1405 tcp->u_rval = -1;
1406 u_error = r2;
1407 } else {
1408 tcp->u_rval = r2;
1409 u_error = 0;
1410 }
1411#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001412#ifdef POWERPC
1413 if (result && (unsigned) -result < nerrnos) {
1414 tcp->u_rval = -1;
1415 u_error = -result;
1416 }
1417 else {
1418 tcp->u_rval = result;
1419 u_error = 0;
1420 }
1421#else /* !POWERPC */
1422#ifdef M68K
1423 if (d0 && (unsigned) -d0 < nerrnos) {
1424 tcp->u_rval = -1;
1425 u_error = -d0;
1426 }
1427 else {
1428 tcp->u_rval = d0;
1429 u_error = 0;
1430 }
1431#else /* !M68K */
1432#ifdef ARM
Roland McGrath0f87c492003-06-03 23:29:04 +00001433 if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001434 tcp->u_rval = -1;
Roland McGrath0f87c492003-06-03 23:29:04 +00001435 u_error = -regs.ARM_r0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001436 }
1437 else {
Roland McGrath0f87c492003-06-03 23:29:04 +00001438 tcp->u_rval = regs.ARM_r0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001439 u_error = 0;
1440 }
1441#else /* !ARM */
1442#ifdef ALPHA
1443 if (a3) {
1444 tcp->u_rval = -1;
1445 u_error = r0;
1446 }
1447 else {
1448 tcp->u_rval = r0;
1449 u_error = 0;
1450 }
1451#else /* !ALPHA */
1452#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001453 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001454 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001455 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001456 }
1457 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001458 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001459 u_error = 0;
1460 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001461#else /* !SPARC */
1462#ifdef HPPA
1463 if (r28 && (unsigned) -r28 < nerrnos) {
1464 tcp->u_rval = -1;
1465 u_error = -r28;
1466 }
1467 else {
1468 tcp->u_rval = r28;
1469 u_error = 0;
1470 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001471#else
1472#ifdef SH
1473 /* interpret R0 as return value or error number */
1474 if (r0 && (unsigned) -r0 < nerrnos) {
1475 tcp->u_rval = -1;
1476 u_error = -r0;
1477 }
1478 else {
1479 tcp->u_rval = r0;
1480 u_error = 0;
1481 }
Roland McGrathe1e584b2003-06-02 19:18:58 +00001482#else
1483#ifdef SHMEDIA
1484 /* interpret result as return value or error number */
1485 if (r9 && (unsigned) -r9 < nerrnos) {
1486 tcp->u_rval = -1;
1487 u_error = -r9;
1488 }
1489 else {
1490 tcp->u_rval = r9;
1491 u_error = 0;
1492 }
1493#endif /* SHMEDIA */
Wichert Akkermanccef6372002-05-01 16:39:22 +00001494#endif /* SH */
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001495#endif /* HPPA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001496#endif /* SPARC */
1497#endif /* ALPHA */
1498#endif /* ARM */
1499#endif /* M68K */
1500#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001501#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001502#endif /* IA64 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001503#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001504#endif /* I386 */
Michal Ludvig10a88d02002-10-07 14:31:00 +00001505#endif /* S390 || S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001506#endif /* LINUX */
1507#ifdef SUNOS4
1508 /* get error code from user struct */
1509 if (upeek(pid, uoff(u_error), &u_error) < 0)
1510 return -1;
1511 u_error >>= 24; /* u_error is a char */
1512
1513 /* get system call return value */
1514 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1515 return -1;
1516#endif /* SUNOS4 */
1517#ifdef SVR4
1518#ifdef SPARC
1519 /* Judicious guessing goes a long way. */
1520 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1521 tcp->u_rval = -1;
1522 u_error = tcp->status.pr_reg[R_O0];
1523 }
1524 else {
1525 tcp->u_rval = tcp->status.pr_reg[R_O0];
1526 u_error = 0;
1527 }
1528#endif /* SPARC */
1529#ifdef I386
1530 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001531 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001532 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001533 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001534 }
1535 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001536 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001537#ifdef HAVE_LONG_LONG
1538 tcp->u_lrval =
1539 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1540 tcp->status.PR_REG[EAX];
1541#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001542 u_error = 0;
1543 }
1544#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001545#ifdef X86_64
1546 /* Wanna know how to kill an hour single-stepping? */
1547 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1548 tcp->u_rval = -1;
1549 u_error = tcp->status.PR_REG[RAX];
1550 }
1551 else {
1552 tcp->u_rval = tcp->status.PR_REG[RAX];
1553 u_error = 0;
1554 }
1555#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001556#ifdef MIPS
1557 if (tcp->status.pr_reg[CTX_A3]) {
1558 tcp->u_rval = -1;
1559 u_error = tcp->status.pr_reg[CTX_V0];
1560 }
1561 else {
1562 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1563 u_error = 0;
1564 }
1565#endif /* MIPS */
1566#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001567#ifdef FREEBSD
1568 if (regs.r_eflags & PSL_C) {
1569 tcp->u_rval = -1;
1570 u_error = regs.r_eax;
1571 } else {
1572 tcp->u_rval = regs.r_eax;
1573 tcp->u_lrval =
1574 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1575 u_error = 0;
1576 }
Roland McGrath761b5d72002-12-15 23:58:31 +00001577#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001578 tcp->u_error = u_error;
1579 return 1;
1580}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001581
Roland McGrathb69f81b2002-12-21 23:25:18 +00001582int
1583force_result(tcp, error, rval)
1584 struct tcb *tcp;
1585 int error;
1586 long rval;
1587{
1588#ifdef LINUX
1589#if defined(S390) || defined(S390X)
1590 gpr2 = error ? -error : rval;
Roland McGrathd38809b2003-01-10 07:50:21 +00001591 if (upeek(tcp->pid, PT_GPR2, &gpr2) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001592 return -1;
1593 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1594 return -1;
1595#else /* !S390 && !S390X */
1596#ifdef I386
1597 eax = error ? -error : rval;
1598 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1599 return -1;
1600#else /* !I386 */
1601#ifdef X86_64
1602 rax = error ? -error : rval;
1603 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 4), rax) < 0)
1604 return -1;
1605#else
1606#ifdef IA64
1607 if (ia32) {
1608 r8 = error ? -error : rval;
1609 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1610 return -1;
1611 }
1612 else {
1613 if (error) {
1614 r8 = error;
1615 r10 = -1;
1616 }
1617 else {
1618 r8 = rval;
1619 r10 = 0;
1620 }
1621 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1622 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1623 return -1;
1624 }
1625#else /* !IA64 */
1626#ifdef MIPS
1627 if (error) {
1628 r2 = error;
1629 a3 = -1;
1630 }
1631 else {
1632 r2 = rval;
1633 a3 = 0;
1634 }
1635 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1636 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1637 return -1;
1638#else
1639#ifdef POWERPC
Roland McGratheb285352003-01-14 09:59:00 +00001640 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001641 return -1;
1642 if (error) {
1643 flags |= SO_MASK;
1644 result = error;
1645 }
1646 else {
1647 flags &= ~SO_MASK;
1648 result = rval;
1649 }
Roland McGratheb285352003-01-14 09:59:00 +00001650 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1651 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001652 return -1;
1653#else /* !POWERPC */
1654#ifdef M68K
1655 d0 = error ? -error : rval;
1656 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1657 return -1;
1658#else /* !M68K */
1659#ifdef ARM
1660 r0 = error ? -error : rval;
1661 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), r0) < 0)
1662 return -1;
1663#else /* !ARM */
1664#ifdef ALPHA
1665 if (error) {
1666 a3 = -1;
1667 r0 = error;
1668 }
1669 else {
1670 a3 = 0;
1671 r0 = rval;
1672 }
1673 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1674 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1675 return -1;
1676#else /* !ALPHA */
1677#ifdef SPARC
1678 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0)
1679 return -1;
1680 if (error) {
1681 regs.r_psr |= PSR_C;
1682 regs.r_o0 = error;
1683 }
1684 else {
1685 regs.r_psr &= ~PSR_C;
1686 regs.r_o0 = rval;
1687 }
1688 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)&regs, 0) < 0)
1689 return -1;
1690#else /* !SPARC */
1691#ifdef HPPA
1692 r28 = error ? -error : rval;
1693 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1694 return -1;
1695#else
1696#ifdef SH
1697 r0 = error ? -error : rval;
1698 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1699 return -1;
Roland McGrathe1e584b2003-06-02 19:18:58 +00001700#else
1701#ifdef SHMEDIA
1702 r9 = error ? -error : rval;
1703 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1704 return -1;
1705#endif /* SHMEDIA */
Roland McGrathb69f81b2002-12-21 23:25:18 +00001706#endif /* SH */
1707#endif /* HPPA */
1708#endif /* SPARC */
1709#endif /* ALPHA */
1710#endif /* ARM */
1711#endif /* M68K */
1712#endif /* POWERPC */
1713#endif /* MIPS */
1714#endif /* IA64 */
1715#endif /* X86_64 */
1716#endif /* I386 */
1717#endif /* S390 || S390X */
1718#endif /* LINUX */
1719#ifdef SUNOS4
1720 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1721 error << 24) < 0 ||
1722 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1723 return -1;
1724#endif /* SUNOS4 */
1725#ifdef SVR4
1726 /* XXX no clue */
1727 return -1;
1728#endif /* SVR4 */
1729#ifdef FREEBSD
1730 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1731 perror("pread");
1732 return -1;
1733 }
1734 if (error) {
1735 regs.r_eflags |= PSL_C;
1736 regs.r_eax = error;
1737 }
1738 else {
1739 regs.r_eflags &= ~PSL_C;
1740 regs.r_eax = rval;
1741 }
1742 if (pwrite(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1743 perror("pwrite");
1744 return -1;
1745 }
1746#endif /* FREEBSD */
1747
1748 /* All branches reach here on success (only). */
1749 tcp->u_error = error;
1750 tcp->u_rval = rval;
1751 return 0;
1752}
1753
Pavel Machek4dc3b142000-02-01 17:58:41 +00001754int syscall_enter(tcp)
1755struct tcb *tcp;
1756{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001757#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001758 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001759#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001760#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001761#if defined(S390) || defined(S390X)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001762 {
1763 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001764 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1765 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001766 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001767 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001768 for (i = 0; i < tcp->u_nargs; i++) {
Michal Ludvig10a88d02002-10-07 14:31:00 +00001769 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001770 return -1;
1771 }
1772 }
1773#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001774 {
1775 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001776 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1777 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001778 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001779 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001780 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001781 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1782 * for scno somewhere above here!
1783 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001784 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1785 return -1;
1786 }
1787 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001788#elif defined (IA64)
1789 {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001790 if (!ia32) {
1791 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1792 /* be backwards compatible with kernel < 2.4.4... */
1793# ifndef PT_RBS_END
1794# define PT_RBS_END PT_AR_BSP
1795# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001796
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001797 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001798 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001799 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1800 return -1;
1801
1802 sof = (cfm >> 0) & 0x7f;
1803 sol = (cfm >> 7) & 0x7f;
1804 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1805
1806 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1807 && sysent[tcp->scno].nargs != -1)
1808 tcp->u_nargs = sysent[tcp->scno].nargs;
1809 else
1810 tcp->u_nargs = MAX_ARGS;
1811 for (i = 0; i < tcp->u_nargs; ++i) {
1812 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1813 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1814 return -1;
1815 }
1816 } else {
1817 int i;
1818
1819 if (/* EBX = out0 */
1820 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1821 /* ECX = out1 */
1822 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1823 /* EDX = out2 */
1824 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1825 /* ESI = out3 */
1826 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1827 /* EDI = out4 */
1828 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1829 /* EBP = out5 */
1830 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1831 return -1;
1832
1833 for (i = 0; i < 6; ++i)
1834 /* truncate away IVE sign-extension */
1835 tcp->u_arg[i] &= 0xffffffff;
1836
1837 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1838 && sysent[tcp->scno].nargs != -1)
1839 tcp->u_nargs = sysent[tcp->scno].nargs;
1840 else
1841 tcp->u_nargs = 5;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001842 }
1843 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001844#elif defined (MIPS)
1845 {
1846 long sp;
1847 int i, nargs;
1848
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001849 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1850 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001851 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001852 nargs = tcp->u_nargs = MAX_ARGS;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001853 if(nargs > 4) {
1854 if(upeek(pid, REG_SP, &sp) < 0)
1855 return -1;
1856 for(i = 0; i < 4; i++) {
1857 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1858 return -1;
1859 }
1860 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1861 (char *)(tcp->u_arg + 4));
1862 } else {
1863 for(i = 0; i < nargs; i++) {
1864 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1865 return -1;
1866 }
1867 }
1868 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001869#elif defined (POWERPC)
Roland McGrath761b5d72002-12-15 23:58:31 +00001870#ifndef PT_ORIG_R3
1871#define PT_ORIG_R3 34
1872#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001873 {
1874 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001875 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1876 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001877 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001878 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001879 for (i = 0; i < tcp->u_nargs; i++) {
Roland McGratheb285352003-01-14 09:59:00 +00001880 if (upeek(pid, (i==0) ?
1881 (sizeof(unsigned long)*PT_ORIG_R3) :
1882 ((i+PT_R3)*sizeof(unsigned long)),
1883 &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001884 return -1;
1885 }
1886 }
1887#elif defined (SPARC)
1888 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001889 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001890
1891 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1892 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001893 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001894 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001895 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001896 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001897 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001898#elif defined (HPPA)
1899 {
1900 int i;
1901
1902 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1903 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001904 else
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001905 tcp->u_nargs = MAX_ARGS;
1906 for (i = 0; i < tcp->u_nargs; i++) {
1907 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1908 return -1;
1909 }
1910 }
Roland McGrath0f87c492003-06-03 23:29:04 +00001911#elif defined(ARM)
1912 {
1913 int i;
1914
1915 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1916 tcp->u_nargs = sysent[tcp->scno].nargs;
1917 else
1918 tcp->u_nargs = MAX_ARGS;
1919 for (i = 0; i < tcp->u_nargs; i++)
1920 tcp->u_arg[i] = regs.uregs[i];
1921 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001922#elif defined(SH)
1923 {
Roland McGrath761b5d72002-12-15 23:58:31 +00001924 int i;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001925 static int syscall_regs[] = {
1926 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1927 REG_REG0, REG_REG0+1, REG_REG0+2
1928 };
1929
1930 tcp->u_nargs = sysent[tcp->scno].nargs;
1931 for (i = 0; i < tcp->u_nargs; i++) {
1932 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1933 return -1;
1934 }
1935 }
Roland McGrathe1e584b2003-06-02 19:18:58 +00001936#elif defined(SHMEDIA)
1937 {
1938 int i;
1939 /* Registers used by SH5 Linux system calls for parameters */
1940 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
1941
1942 /*
1943 * TODO: should also check that the number of arguments encoded
1944 * in the trap number matches the number strace expects.
1945 */
1946 /*
1947 assert(sysent[tcp->scno].nargs <
1948 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
1949 */
1950
1951 tcp->u_nargs = sysent[tcp->scno].nargs;
1952 for (i = 0; i < tcp->u_nargs; i++) {
1953 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1954 return -1;
1955 }
1956 }
1957
Michal Ludvig0e035502002-09-23 15:41:01 +00001958#elif defined(X86_64)
1959 {
1960 int i;
1961 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1962 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
1963 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
1964 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001965
Michal Ludvig0e035502002-09-23 15:41:01 +00001966 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1967 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001968 else
Michal Ludvig0e035502002-09-23 15:41:01 +00001969 tcp->u_nargs = MAX_ARGS;
1970 for (i = 0; i < tcp->u_nargs; i++) {
1971 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
1972 return -1;
1973 }
1974 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001975#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001976 {
1977 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001978 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1979 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001980 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001981 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001982 for (i = 0; i < tcp->u_nargs; i++) {
1983 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1984 return -1;
1985 }
1986 }
Roland McGrath761b5d72002-12-15 23:58:31 +00001987#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001988#endif /* LINUX */
1989#ifdef SUNOS4
1990 {
1991 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001992 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1993 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001994 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001995 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001996 for (i = 0; i < tcp->u_nargs; i++) {
1997 struct user *u;
1998
1999 if (upeek(pid, uoff(u_arg[0]) +
2000 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2001 return -1;
2002 }
2003 }
2004#endif /* SUNOS4 */
2005#ifdef SVR4
2006#ifdef MIPS
2007 /*
2008 * SGI is broken: even though it has pr_sysarg, it doesn't
2009 * set them on system call entry. Get a clue.
2010 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002011 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002012 tcp->u_nargs = sysent[tcp->scno].nargs;
2013 else
2014 tcp->u_nargs = tcp->status.pr_nsysarg;
2015 if (tcp->u_nargs > 4) {
2016 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2017 4*sizeof(tcp->u_arg[0]));
2018 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2019 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2020 }
2021 else {
2022 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2023 tcp->u_nargs*sizeof(tcp->u_arg[0]));
2024 }
John Hughes25299712001-03-06 10:10:06 +00002025#elif UNIXWARE >= 2
2026 /*
2027 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2028 */
2029 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2030 tcp->u_nargs = sysent[tcp->scno].nargs;
2031 else
2032 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2033 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2034 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2035#elif defined (HAVE_PR_SYSCALL)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002036 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002037 tcp->u_nargs = sysent[tcp->scno].nargs;
2038 else
2039 tcp->u_nargs = tcp->status.pr_nsysarg;
2040 {
2041 int i;
2042 for (i = 0; i < tcp->u_nargs; i++)
2043 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2044 }
John Hughes25299712001-03-06 10:10:06 +00002045#elif defined (I386)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002046 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002047 tcp->u_nargs = sysent[tcp->scno].nargs;
2048 else
2049 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002050 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002051 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
John Hughes25299712001-03-06 10:10:06 +00002052#else
2053 I DONT KNOW WHAT TO DO
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002054#endif /* !HAVE_PR_SYSCALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002055#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002056#ifdef FREEBSD
2057 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2058 sysent[tcp->scno].nargs > tcp->status.val)
2059 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00002060 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002061 tcp->u_nargs = tcp->status.val;
2062 if (tcp->u_nargs < 0)
2063 tcp->u_nargs = 0;
2064 if (tcp->u_nargs > MAX_ARGS)
2065 tcp->u_nargs = MAX_ARGS;
2066 switch(regs.r_eax) {
2067 case SYS___syscall:
2068 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2069 regs.r_esp + sizeof(int) + sizeof(quad_t));
2070 break;
2071 case SYS_syscall:
2072 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2073 regs.r_esp + 2 * sizeof(int));
2074 break;
2075 default:
2076 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2077 regs.r_esp + sizeof(int));
2078 break;
2079 }
2080#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00002081 return 1;
2082}
2083
2084int
2085trace_syscall(tcp)
2086struct tcb *tcp;
2087{
2088 int sys_res;
2089 struct timeval tv;
2090 int res;
2091
2092 /* Measure the exit time as early as possible to avoid errors. */
2093 if (dtime && (tcp->flags & TCB_INSYSCALL))
2094 gettimeofday(&tv, NULL);
2095
2096 res = get_scno(tcp);
2097 if (res != 1)
2098 return res;
2099
2100 res = syscall_fixup(tcp);
2101 if (res != 1)
2102 return res;
2103
2104 if (tcp->flags & TCB_INSYSCALL) {
2105 long u_error;
2106 res = get_error(tcp);
2107 if (res != 1)
2108 return res;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002109
2110 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002111 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2112 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002113 tcp->flags &= ~TCB_INSYSCALL;
2114 return 0;
2115 }
2116
2117 if (tcp->flags & TCB_REPRINT) {
2118 printleader(tcp);
2119 tprintf("<... ");
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002120 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002121 tprintf("syscall_%lu", tcp->scno);
2122 else
2123 tprintf("%s", sysent[tcp->scno].sys_name);
2124 tprintf(" resumed> ");
2125 }
2126
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002127 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002128 call_count[tcp->scno]++;
2129 if (tcp->u_error)
2130 error_count[tcp->scno]++;
2131 tv_sub(&tv, &tv, &tcp->etime);
2132#ifdef LINUX
2133 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2134 static struct timeval one_tick =
2135 { 0, 1000000 / HZ };
2136
2137 if (tv_nz(&tcp->dtime))
2138 tv = tcp->dtime;
2139 else if (tv_cmp(&tv, &one_tick) > 0) {
2140 if (tv_cmp(&shortest, &one_tick) < 0)
2141 tv = shortest;
2142 else
2143 tv = one_tick;
2144 }
2145 }
2146#endif /* LINUX */
2147 if (tv_cmp(&tv, &shortest) < 0)
2148 shortest = tv;
2149 tv_add(&tv_count[tcp->scno],
2150 &tv_count[tcp->scno], &tv);
2151 tcp->flags &= ~TCB_INSYSCALL;
2152 return 0;
2153 }
2154
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002155 if (tcp->scno >= nsyscalls || tcp->scno < 0
Pavel Machek4dc3b142000-02-01 17:58:41 +00002156 || (qual_flags[tcp->scno] & QUAL_RAW))
2157 sys_res = printargs(tcp);
Michal Ludvig17f8fb32002-11-06 13:17:21 +00002158 else {
2159 if (not_failing_only && tcp->u_error)
Roland McGrath761b5d72002-12-15 23:58:31 +00002160 return 0; /* ignore failed syscalls */
Pavel Machek4dc3b142000-02-01 17:58:41 +00002161 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
Roland McGrath761b5d72002-12-15 23:58:31 +00002162 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00002163 u_error = tcp->u_error;
2164 tprintf(") ");
2165 tabto(acolumn);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002166 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2167 qual_flags[tcp->scno] & QUAL_RAW) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002168 if (u_error)
2169 tprintf("= -1 (errno %ld)", u_error);
2170 else
2171 tprintf("= %#lx", tcp->u_rval);
2172 }
2173 else if (!(sys_res & RVAL_NONE) && u_error) {
2174 switch (u_error) {
2175#ifdef LINUX
2176 case ERESTARTSYS:
2177 tprintf("= ? ERESTARTSYS (To be restarted)");
2178 break;
2179 case ERESTARTNOINTR:
2180 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2181 break;
2182 case ERESTARTNOHAND:
2183 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2184 break;
2185#endif /* LINUX */
2186 default:
2187 tprintf("= -1 ");
Wichert Akkerman4527dae2002-03-31 19:03:29 +00002188 if (u_error < 0)
2189 tprintf("E??? (errno %ld)", u_error);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002190 else if (u_error < nerrnos)
Roland McGrath761b5d72002-12-15 23:58:31 +00002191 tprintf("%s (%s)", errnoent[u_error],
2192 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00002193 else
Roland McGrath761b5d72002-12-15 23:58:31 +00002194 tprintf("ERRNO_%ld (%s)", u_error,
2195 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00002196 break;
2197 }
2198 }
2199 else {
2200 if (sys_res & RVAL_NONE)
2201 tprintf("= ?");
2202 else {
2203 switch (sys_res & RVAL_MASK) {
2204 case RVAL_HEX:
2205 tprintf("= %#lx", tcp->u_rval);
2206 break;
2207 case RVAL_OCTAL:
2208 tprintf("= %#lo", tcp->u_rval);
2209 break;
2210 case RVAL_UDECIMAL:
2211 tprintf("= %lu", tcp->u_rval);
2212 break;
2213 case RVAL_DECIMAL:
2214 tprintf("= %ld", tcp->u_rval);
2215 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002216#ifdef HAVE_LONG_LONG
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002217 case RVAL_LHEX:
2218 tprintf("= %#llx", tcp->u_lrval);
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002219 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002220 case RVAL_LOCTAL:
2221 tprintf("= %#llo", tcp->u_lrval);
2222 break;
2223 case RVAL_LUDECIMAL:
2224 tprintf("= %llu", tcp->u_lrval);
2225 break;
2226 case RVAL_LDECIMAL:
2227 tprintf("= %lld", tcp->u_lrval);
2228 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002229#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00002230 default:
2231 fprintf(stderr,
2232 "invalid rval format\n");
2233 break;
2234 }
2235 }
2236 if ((sys_res & RVAL_STR) && tcp->auxstr)
2237 tprintf(" (%s)", tcp->auxstr);
2238 }
2239 if (dtime) {
2240 tv_sub(&tv, &tv, &tcp->etime);
2241 tprintf(" <%ld.%06ld>",
2242 (long) tv.tv_sec, (long) tv.tv_usec);
2243 }
2244 printtrailer(tcp);
2245
2246 dumpio(tcp);
2247 if (fflush(tcp->outf) == EOF)
2248 return -1;
2249 tcp->flags &= ~TCB_INSYSCALL;
2250 return 0;
2251 }
2252
2253 /* Entering system call */
2254 res = syscall_enter(tcp);
2255 if (res != 1)
2256 return res;
2257
Pavel Machekd8ae7e32000-02-01 17:17:25 +00002258 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002259#ifdef LINUX
Michal Ludvig0e035502002-09-23 15:41:01 +00002260#if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002261 case SYS_socketcall:
2262 decode_subcall(tcp, SYS_socket_subcall,
2263 SYS_socket_nsubcalls, deref_style);
2264 break;
2265 case SYS_ipc:
2266 decode_subcall(tcp, SYS_ipc_subcall,
2267 SYS_ipc_nsubcalls, shift_style);
2268 break;
Michal Ludvig0e035502002-09-23 15:41:01 +00002269#endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002270#ifdef SPARC
2271 case SYS_socketcall:
2272 sparc_socket_decode (tcp);
2273 break;
2274#endif
2275#endif /* LINUX */
2276#ifdef SVR4
2277#ifdef SYS_pgrpsys_subcall
2278 case SYS_pgrpsys:
2279 decode_subcall(tcp, SYS_pgrpsys_subcall,
2280 SYS_pgrpsys_nsubcalls, shift_style);
2281 break;
2282#endif /* SYS_pgrpsys_subcall */
2283#ifdef SYS_sigcall_subcall
2284 case SYS_sigcall:
2285 decode_subcall(tcp, SYS_sigcall_subcall,
2286 SYS_sigcall_nsubcalls, mask_style);
2287 break;
2288#endif /* SYS_sigcall_subcall */
2289 case SYS_msgsys:
2290 decode_subcall(tcp, SYS_msgsys_subcall,
2291 SYS_msgsys_nsubcalls, shift_style);
2292 break;
2293 case SYS_shmsys:
2294 decode_subcall(tcp, SYS_shmsys_subcall,
2295 SYS_shmsys_nsubcalls, shift_style);
2296 break;
2297 case SYS_semsys:
2298 decode_subcall(tcp, SYS_semsys_subcall,
2299 SYS_semsys_nsubcalls, shift_style);
2300 break;
2301#if 0 /* broken */
2302 case SYS_utssys:
2303 decode_subcall(tcp, SYS_utssys_subcall,
2304 SYS_utssys_nsubcalls, shift_style);
2305 break;
2306#endif
2307 case SYS_sysfs:
2308 decode_subcall(tcp, SYS_sysfs_subcall,
2309 SYS_sysfs_nsubcalls, shift_style);
2310 break;
2311 case SYS_spcall:
2312 decode_subcall(tcp, SYS_spcall_subcall,
2313 SYS_spcall_nsubcalls, shift_style);
2314 break;
2315#ifdef SYS_context_subcall
2316 case SYS_context:
2317 decode_subcall(tcp, SYS_context_subcall,
2318 SYS_context_nsubcalls, shift_style);
2319 break;
2320#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00002321#ifdef SYS_door_subcall
2322 case SYS_door:
2323 decode_subcall(tcp, SYS_door_subcall,
2324 SYS_door_nsubcalls, door_style);
2325 break;
2326#endif /* SYS_door_subcall */
John Hughesbdf48f52001-03-06 15:08:09 +00002327#ifdef SYS_kaio_subcall
2328 case SYS_kaio:
2329 decode_subcall(tcp, SYS_kaio_subcall,
2330 SYS_kaio_nsubcalls, shift_style);
2331 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002332#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002333#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002334#ifdef FREEBSD
2335 case SYS_msgsys:
2336 case SYS_shmsys:
2337 case SYS_semsys:
2338 decode_subcall(tcp, 0, 0, table_style);
2339 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002340#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002341#ifdef SUNOS4
2342 case SYS_semsys:
2343 decode_subcall(tcp, SYS_semsys_subcall,
2344 SYS_semsys_nsubcalls, shift_style);
2345 break;
2346 case SYS_msgsys:
2347 decode_subcall(tcp, SYS_msgsys_subcall,
2348 SYS_msgsys_nsubcalls, shift_style);
2349 break;
2350 case SYS_shmsys:
2351 decode_subcall(tcp, SYS_shmsys_subcall,
2352 SYS_shmsys_nsubcalls, shift_style);
2353 break;
2354#endif
2355 }
2356
2357 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002358 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002359 tcp->flags |= TCB_INSYSCALL;
2360 return 0;
2361 }
2362
2363 if (cflag) {
2364 gettimeofday(&tcp->etime, NULL);
2365 tcp->flags |= TCB_INSYSCALL;
2366 return 0;
2367 }
2368
2369 printleader(tcp);
2370 tcp->flags &= ~TCB_REPRINT;
2371 tcp_last = tcp;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002372 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002373 tprintf("syscall_%lu(", tcp->scno);
2374 else
2375 tprintf("%s(", sysent[tcp->scno].sys_name);
Roland McGrath761b5d72002-12-15 23:58:31 +00002376 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002377 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2378 sys_res = printargs(tcp);
2379 else
2380 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2381 if (fflush(tcp->outf) == EOF)
2382 return -1;
2383 tcp->flags |= TCB_INSYSCALL;
2384 /* Measure the entrance time as late as possible to avoid errors. */
2385 if (dtime)
2386 gettimeofday(&tcp->etime, NULL);
2387 return sys_res;
2388}
2389
2390int
2391printargs(tcp)
2392struct tcb *tcp;
2393{
2394 if (entering(tcp)) {
2395 int i;
2396
2397 for (i = 0; i < tcp->u_nargs; i++)
2398 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2399 }
2400 return 0;
2401}
2402
2403long
2404getrval2(tcp)
2405struct tcb *tcp;
2406{
2407 long val = -1;
2408
2409#ifdef LINUX
2410#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002411 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002412 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
2413 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002414 val = regs.r_o1;
Roland McGrath6b1d43e2003-03-31 01:05:01 +00002415#elif defined(SH)
2416 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2417 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002418#endif /* SPARC */
2419#endif /* LINUX */
2420
2421#ifdef SUNOS4
2422 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2423 return -1;
2424#endif /* SUNOS4 */
2425
2426#ifdef SVR4
2427#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002428 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002429#endif /* SPARC */
2430#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002431 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002432#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00002433#ifdef X86_64
2434 val = tcp->status.PR_REG[RDX];
2435#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002436#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002437 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002438#endif /* MIPS */
2439#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002440#ifdef FREEBSD
2441 struct reg regs;
2442 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
2443 val = regs.r_edx;
Roland McGrath761b5d72002-12-15 23:58:31 +00002444#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002445 return val;
2446}
2447
2448/*
2449 * Apparently, indirect system calls have already be converted by ptrace(2),
2450 * so if you see "indir" this program has gone astray.
2451 */
2452int
2453sys_indir(tcp)
2454struct tcb *tcp;
2455{
2456 int i, scno, nargs;
2457
2458 if (entering(tcp)) {
2459 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2460 fprintf(stderr, "Bogus syscall: %u\n", scno);
2461 return 0;
2462 }
2463 nargs = sysent[scno].nargs;
2464 tprintf("%s", sysent[scno].sys_name);
2465 for (i = 0; i < nargs; i++)
2466 tprintf(", %#lx", tcp->u_arg[i+1]);
2467 }
2468 return 0;
2469}
2470
2471static int
2472time_cmp(a, b)
2473void *a;
2474void *b;
2475{
2476 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2477}
2478
2479static int
2480syscall_cmp(a, b)
2481void *a;
2482void *b;
2483{
2484 return strcmp(sysent[*((int *) a)].sys_name,
2485 sysent[*((int *) b)].sys_name);
2486}
2487
2488static int
2489count_cmp(a, b)
2490void *a;
2491void *b;
2492{
2493 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2494
2495 return (m < n) ? 1 : (m > n) ? -1 : 0;
2496}
2497
2498static int (*sortfun)();
2499static struct timeval overhead = { -1, -1 };
2500
2501void
2502set_sortby(sortby)
2503char *sortby;
2504{
2505 if (strcmp(sortby, "time") == 0)
2506 sortfun = time_cmp;
2507 else if (strcmp(sortby, "calls") == 0)
2508 sortfun = count_cmp;
2509 else if (strcmp(sortby, "name") == 0)
2510 sortfun = syscall_cmp;
2511 else if (strcmp(sortby, "nothing") == 0)
2512 sortfun = NULL;
2513 else {
2514 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2515 exit(1);
2516 }
2517}
2518
2519void set_overhead(n)
2520int n;
2521{
2522 overhead.tv_sec = n / 1000000;
2523 overhead.tv_usec = n % 1000000;
2524}
2525
2526void
2527call_summary(outf)
2528FILE *outf;
2529{
2530 int i, j;
2531 int call_cum, error_cum;
2532 struct timeval tv_cum, dtv;
2533 double percent;
2534 char *dashes = "-------------------------";
2535 char error_str[16];
2536
2537 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2538 if (overhead.tv_sec == -1) {
2539 tv_mul(&overhead, &shortest, 8);
2540 tv_div(&overhead, &overhead, 10);
2541 }
2542 for (i = 0; i < nsyscalls; i++) {
2543 sorted_count[i] = i;
2544 if (call_count[i] == 0)
2545 continue;
2546 tv_mul(&dtv, &overhead, call_count[i]);
2547 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2548 call_cum += call_count[i];
2549 error_cum += error_count[i];
2550 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2551 }
2552 if (sortfun)
2553 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2554 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2555 "% time", "seconds", "usecs/call",
2556 "calls", "errors", "syscall");
2557 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2558 dashes, dashes, dashes, dashes, dashes, dashes);
2559 for (i = 0; i < nsyscalls; i++) {
2560 j = sorted_count[i];
2561 if (call_count[j] == 0)
2562 continue;
2563 tv_div(&dtv, &tv_count[j], call_count[j]);
2564 if (error_count[j])
2565 sprintf(error_str, "%d", error_count[j]);
2566 else
2567 error_str[0] = '\0';
2568 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2569 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2570 percent, (long) tv_count[j].tv_sec,
2571 (long) tv_count[j].tv_usec,
2572 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2573 call_count[j], error_str, sysent[j].sys_name);
2574 }
2575 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2576 dashes, dashes, dashes, dashes, dashes, dashes);
2577 if (error_cum)
2578 sprintf(error_str, "%d", error_cum);
2579 else
2580 error_str[0] = '\0';
2581 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2582 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2583 call_cum, error_str, "total");
2584}