blob: 5b04dc913610f67c125591797bd7b68ed8e03c48 [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);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000325 }
326 return -1;
327}
328
329static int
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000330lookup_class(s)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000331 char *s;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000332{
333 if (strcmp(s, "file") == 0)
334 return TRACE_FILE;
335 if (strcmp(s, "ipc") == 0)
336 return TRACE_IPC;
337 if (strcmp(s, "network") == 0)
338 return TRACE_NETWORK;
339 if (strcmp(s, "process") == 0)
340 return TRACE_PROCESS;
341 if (strcmp(s, "signal") == 0)
342 return TRACE_SIGNAL;
343 return -1;
344}
345
346void
347qualify(s)
348char *s;
349{
350 struct qual_options *opt;
351 int not;
352 char *p;
353 int i, n;
354
355 opt = &qual_options[0];
356 for (i = 0; (p = qual_options[i].option_name); i++) {
357 n = strlen(p);
358 if (strncmp(s, p, n) == 0 && s[n] == '=') {
359 opt = &qual_options[i];
360 s += n + 1;
361 break;
362 }
363 }
364 not = 0;
365 if (*s == '!') {
366 not = 1;
367 s++;
368 }
369 if (strcmp(s, "none") == 0) {
370 not = 1 - not;
371 s = "all";
372 }
373 if (strcmp(s, "all") == 0) {
374 for (i = 0; i < MAX_QUALS; i++) {
375 if (not)
376 qual_flags[i] &= ~opt->bitflag;
377 else
378 qual_flags[i] |= opt->bitflag;
379 }
380 return;
381 }
382 for (i = 0; i < MAX_QUALS; i++) {
383 if (not)
384 qual_flags[i] |= opt->bitflag;
385 else
386 qual_flags[i] &= ~opt->bitflag;
387 }
388 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
389 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
390 for (i = 0; i < MAX_QUALS; i++) {
391 if (sysent[i].sys_flags & n) {
392 if (not)
393 qual_flags[i] &= ~opt->bitflag;
394 else
395 qual_flags[i] |= opt->bitflag;
396 }
397 }
398 continue;
399 }
Roland McGrath9797ceb2002-12-30 10:23:00 +0000400 if (opt->qualify(p, opt, not)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000401 fprintf(stderr, "strace: invalid %s `%s'\n",
402 opt->argument_name, p);
403 exit(1);
404 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000405 }
406 return;
407}
408
409static void
410dumpio(tcp)
411struct tcb *tcp;
412{
413 if (syserror(tcp))
414 return;
415 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
416 return;
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000417 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000418 case SYS_read:
419#ifdef SYS_recv
420 case SYS_recv:
421#endif
422#ifdef SYS_recvfrom
423 case SYS_recvfrom:
424#endif
425 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
426 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
427 break;
428 case SYS_write:
429#ifdef SYS_send
430 case SYS_send:
431#endif
432#ifdef SYS_sendto
433 case SYS_sendto:
434#endif
435 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
436 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
437 break;
John Hughes1d08dcf2001-07-10 13:48:44 +0000438#ifdef SYS_readv
439 case SYS_readv:
440 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
441 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
442 break;
443#endif
444#ifdef SYS_writev
445 case SYS_writev:
446
447 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
448 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
449 break;
450#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000451 }
452}
453
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000454#ifndef FREEBSD
Wichert Akkerman8829a551999-06-11 13:18:40 +0000455enum subcall_style { shift_style, deref_style, mask_style, door_style };
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000456#else /* FREEBSD */
457enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
458
459struct subcall {
460 int call;
461 int nsubcalls;
462 int subcalls[5];
463};
464
465const struct subcall subcalls_table[] = {
466 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
John Hughes61eeb552001-03-06 15:51:53 +0000467#ifdef SYS_semconfig
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000468 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
John Hughes61eeb552001-03-06 15:51:53 +0000469#else
470 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
471#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000472 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
473};
474#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000475
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000476#if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000477
478const int socket_map [] = {
479 /* SYS_SOCKET */ 97,
480 /* SYS_BIND */ 104,
481 /* SYS_CONNECT */ 98,
482 /* SYS_LISTEN */ 106,
483 /* SYS_ACCEPT */ 99,
484 /* SYS_GETSOCKNAME */ 150,
485 /* SYS_GETPEERNAME */ 141,
486 /* SYS_SOCKETPAIR */ 135,
487 /* SYS_SEND */ 101,
488 /* SYS_RECV */ 102,
489 /* SYS_SENDTO */ 133,
490 /* SYS_RECVFROM */ 125,
491 /* SYS_SHUTDOWN */ 134,
492 /* SYS_SETSOCKOPT */ 105,
493 /* SYS_GETSOCKOPT */ 118,
494 /* SYS_SENDMSG */ 114,
495 /* SYS_RECVMSG */ 113
496};
497
498void
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000499sparc_socket_decode (tcp)
500struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000501{
502 volatile long addr;
503 volatile int i, n;
504
505 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
506 return;
507 }
508 tcp->scno = socket_map [tcp->u_arg [0]-1];
509 n = tcp->u_nargs = sysent [tcp->scno].nargs;
510 addr = tcp->u_arg [1];
511 for (i = 0; i < n; i++){
512 int arg;
513 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
514 arg = 0;
515 tcp->u_arg [i] = arg;
516 addr += sizeof (arg);
517 }
518}
519
Michal Ludvig0e035502002-09-23 15:41:01 +0000520void
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000521decode_subcall(tcp, subcall, nsubcalls, style)
522struct tcb *tcp;
523int subcall;
524int nsubcalls;
525enum subcall_style style;
526{
Michal Ludvig10a88d02002-10-07 14:31:00 +0000527 long addr, mask, arg;
528 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000529
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000530 switch (style) {
531 case shift_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000532 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
533 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000534 tcp->scno = subcall + tcp->u_arg[0];
535 if (sysent[tcp->scno].nargs != -1)
536 tcp->u_nargs = sysent[tcp->scno].nargs;
537 else
538 tcp->u_nargs--;
539 for (i = 0; i < tcp->u_nargs; i++)
540 tcp->u_arg[i] = tcp->u_arg[i + 1];
541 break;
542 case deref_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000543 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
544 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000545 tcp->scno = subcall + tcp->u_arg[0];
546 addr = tcp->u_arg[1];
547 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
548 if (umove(tcp, addr, &arg) < 0)
549 arg = 0;
550 tcp->u_arg[i] = arg;
551 addr += sizeof(arg);
552 }
553 tcp->u_nargs = sysent[tcp->scno].nargs;
554 break;
555 case mask_style:
556 mask = (tcp->u_arg[0] >> 8) & 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000557 for (i = 0; mask; i++)
558 mask >>= 1;
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000559 if (i >= nsubcalls)
560 return;
561 tcp->u_arg[0] &= 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000562 tcp->scno = subcall + i;
563 if (sysent[tcp->scno].nargs != -1)
564 tcp->u_nargs = sysent[tcp->scno].nargs;
565 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000566 case door_style:
567 /*
568 * Oh, yuck. The call code is the *sixth* argument.
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000569 * (don't you mean the *last* argument? - JH)
Wichert Akkerman8829a551999-06-11 13:18:40 +0000570 */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000571 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
572 return;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000573 tcp->scno = subcall + tcp->u_arg[5];
574 if (sysent[tcp->scno].nargs != -1)
575 tcp->u_nargs = sysent[tcp->scno].nargs;
576 else
577 tcp->u_nargs--;
578 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000579#ifdef FREEBSD
580 case table_style:
581 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
582 if (subcalls_table[i].call == tcp->scno) break;
583 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
584 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
585 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
586 for (i = 0; i < tcp->u_nargs; i++)
587 tcp->u_arg[i] = tcp->u_arg[i + 1];
588 }
589 break;
590#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000591 }
592}
593#endif
594
595struct tcb *tcp_last = NULL;
596
597static int
598internal_syscall(tcp)
599struct tcb *tcp;
600{
601 /*
602 * We must always trace a few critical system calls in order to
603 * correctly support following forks in the presence of tracing
604 * qualifiers.
605 */
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000606 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000607#ifdef SYS_fork
608 case SYS_fork:
609#endif
610#ifdef SYS_vfork
611 case SYS_vfork:
612#endif
John Hughes4e36a812001-04-18 15:11:51 +0000613#ifdef SYS_fork1
614 case SYS_fork1:
615#endif
616#ifdef SYS_forkall
617 case SYS_forkall:
618#endif
619#ifdef SYS_rfork1
620 case SYS_rfork1:
621#endif
622#ifdef SYS_rforkall
623 case SYS_rforkall:
624#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000625 internal_fork(tcp);
626 break;
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000627#ifdef SYS_clone
628 case SYS_clone:
629 internal_clone(tcp);
630 break;
631#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000632#ifdef SYS_clone2
633 case SYS_clone2:
634 internal_clone(tcp);
635 break;
636#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000637#ifdef SYS_execv
638 case SYS_execv:
639#endif
640#ifdef SYS_execve
641 case SYS_execve:
642#endif
John Hughes4e36a812001-04-18 15:11:51 +0000643#ifdef SYS_rexecve
644 case SYS_rexecve:
645#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000646 internal_exec(tcp);
647 break;
648
649#ifdef SYS_wait
650 case SYS_wait:
651#endif
652#ifdef SYS_wait4
653 case SYS_wait4:
654#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000655#ifdef SYS32_wait4
656 case SYS32_wait4:
657#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000658#ifdef SYS_waitpid
659 case SYS_waitpid:
660#endif
661#ifdef SYS_waitsys
662 case SYS_waitsys:
663#endif
664 internal_wait(tcp);
665 break;
666
667#ifdef SYS_exit
668 case SYS_exit:
669#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000670#ifdef SYS32_exit
671 case SYS32_exit:
672#endif
Roland McGrath923f7502003-01-09 06:53:27 +0000673#ifdef __NR_exit_group
674 case __NR_exit_group:
675#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000676 internal_exit(tcp);
677 break;
678 }
679 return 0;
680}
681
Wichert Akkermanc7926982000-04-10 22:22:31 +0000682
683#ifdef LINUX
684#if defined (I386)
685 static long eax;
686#elif defined (IA64)
687 long r8, r10, psr;
688 long ia32 = 0;
689#elif defined (POWERPC)
690 static long result,flags;
691#elif defined (M68K)
692 static int d0;
693#elif defined (ARM)
694 static int r0;
695#elif defined (ALPHA)
696 static long r0;
697 static long a3;
698#elif defined (SPARC)
Wichert Akkerman00a82ee2001-03-28 20:29:17 +0000699 static struct regs regs;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000700 static unsigned long trap;
701#elif defined(MIPS)
702 static long a3;
703 static long r2;
Michal Ludvig10a88d02002-10-07 14:31:00 +0000704#elif defined(S390) || defined(S390X)
Wichert Akkermanc7926982000-04-10 22:22:31 +0000705 static long gpr2;
706 static long pc;
Michal Ludvig882eda82002-11-11 12:50:47 +0000707 static long syscall_mode;
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000708#elif defined(HPPA)
709 static long r28;
Wichert Akkermanccef6372002-05-01 16:39:22 +0000710#elif defined(SH)
711 static long r0;
Michal Ludvig0e035502002-09-23 15:41:01 +0000712#elif defined(X86_64)
713 static long rax;
Roland McGrath761b5d72002-12-15 23:58:31 +0000714#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000715#endif /* LINUX */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000716#ifdef FREEBSD
717 struct reg regs;
Roland McGrath761b5d72002-12-15 23:58:31 +0000718#endif /* FREEBSD */
Wichert Akkermanc7926982000-04-10 22:22:31 +0000719
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000720int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000721get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000722struct tcb *tcp;
723{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000724 long scno = 0;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000725#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000726 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +0000727#endif /* !PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000728
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000729#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +0000730#if defined(S390) || defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +0000731 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000732 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000733 if (syscall_mode != -ENOSYS) {
Michal Ludvig882eda82002-11-11 12:50:47 +0000734 /*
735 * Since kernel version 2.5.44 the scno gets passed in gpr2.
736 */
737 scno = syscall_mode;
738 }
Roland McGrath96dc5142003-01-20 10:23:04 +0000739 if (tcp->flags & TCB_WAITEXECVE) {
740 /*
741 * When the execve system call completes successfully, the
742 * new process still has -ENOSYS (old style) or __NR_execve
743 * (new style) in gpr2. We cannot recover the scno again
744 * by disassembly, because the image that executed the
745 * syscall is gone now. Fortunately, we don't want it. We
746 * leave the flag set so that syscall_fixup can fake the
747 * result.
748 */
749 if (tcp->flags & TCB_INSYSCALL)
750 return 1;
751 /*
752 * This is the SIGTRAP after execve. We cannot try to read
753 * the system call here either.
754 */
755 tcp->flags &= ~TCB_WAITEXECVE;
756 return 0;
757 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000758 else {
759 /*
760 * Old style of "passing" the scno via the SVC instruction.
761 */
762
763 long opcode, offset_reg, tmp;
764 void * svc_addr;
765 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
766 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
767 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
768 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
Roland McGrath761b5d72002-12-15 23:58:31 +0000769
Michal Ludvig882eda82002-11-11 12:50:47 +0000770 if (upeek(pid, PT_PSWADDR, &pc) < 0)
771 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000772 errno = 0;
Michal Ludvig882eda82002-11-11 12:50:47 +0000773 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +0000774 if (errno) {
775 perror("peektext(pc-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +0000776 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000777 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000778
779 /*
780 * We have to check if the SVC got executed directly or via an
781 * EXECUTE instruction. In case of EXECUTE it is necessary to do
782 * instruction decoding to derive the system call number.
783 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
784 * so that this doesn't work if a SVC opcode is part of an EXECUTE
785 * opcode. Since there is no way to find out the opcode size this
786 * is the best we can do...
787 */
788
789 if ((opcode & 0xff00) == 0x0a00) {
790 /* SVC opcode */
791 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +0000792 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000793 else {
794 /* SVC got executed by EXECUTE instruction */
795
796 /*
797 * Do instruction decoding of EXECUTE. If you really want to
798 * understand this, read the Principles of Operations.
799 */
800 svc_addr = (void *) (opcode & 0xfff);
801
802 tmp = 0;
803 offset_reg = (opcode & 0x000f0000) >> 16;
804 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
805 return -1;
806 svc_addr += tmp;
807
808 tmp = 0;
809 offset_reg = (opcode & 0x0000f000) >> 12;
810 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
811 return -1;
812 svc_addr += tmp;
813
814 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
815 if (errno)
816 return -1;
817#if defined(S390X)
818 scno >>= 48;
819#else
820 scno >>= 16;
821#endif
822 tmp = 0;
823 offset_reg = (opcode & 0x00f00000) >> 20;
824 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
825 return -1;
826
827 scno = (scno | tmp) & 0xff;
828 }
829 }
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000830#elif defined (POWERPC)
Roland McGratheb285352003-01-14 09:59:00 +0000831 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000832 return -1;
833 if (!(tcp->flags & TCB_INSYSCALL)) {
834 /* Check if we return from execve. */
835 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
836 tcp->flags &= ~TCB_WAITEXECVE;
837 return 0;
838 }
839 }
840#elif defined (I386)
841 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
842 return -1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000843#elif defined (X86_64)
844 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
845 return -1;
846
Roland McGrath761b5d72002-12-15 23:58:31 +0000847 if (!(tcp->flags & TCB_INSYSCALL)) {
848 static int currpers=-1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000849 long val;
850
851 /* Check CS register value. On x86-64 linux it is:
852 * 0x33 for long mode (64 bit)
853 * 0x23 for compatibility mode (32 bit)
Roland McGrath761b5d72002-12-15 23:58:31 +0000854 * It takes only one ptrace and thus doesn't need
Michal Ludvig0e035502002-09-23 15:41:01 +0000855 * to be cached.
856 */
857 if (upeek(pid, 8*CS, &val) < 0)
858 return -1;
859 switch(val)
860 {
861 case 0x23: currpers = 1; break;
862 case 0x33: currpers = 0; break;
863 default:
864 fprintf(stderr, "Unknown value CS=0x%02X while "
865 "detecting personality of process "
866 "PID=%d\n", (int)val, pid);
867 currpers = current_personality;
868 break;
869 }
870#if 0
871 /* This version analyzes the opcode of a syscall instruction.
872 * (int 0x80 on i386 vs. syscall on x86-64)
873 * It works, but is too complicated.
874 */
875 unsigned long val, rip, i;
876
877 if(upeek(pid, 8*RIP, &rip)<0)
878 perror("upeek(RIP)");
Roland McGrath761b5d72002-12-15 23:58:31 +0000879
Michal Ludvig0e035502002-09-23 15:41:01 +0000880 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
881 rip-=2;
882 errno = 0;
883
Roland McGrath761b5d72002-12-15 23:58:31 +0000884 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
885 if (errno)
886 printf("ptrace_peektext failed: %s\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000887 strerror(errno));
888 switch (call & 0xffff)
889 {
890 /* x86-64: syscall = 0x0f 0x05 */
891 case 0x050f: currpers = 0; break;
892 /* i386: int 0x80 = 0xcd 0x80 */
893 case 0x80cd: currpers = 1; break;
894 default:
895 currpers = current_personality;
Roland McGrath761b5d72002-12-15 23:58:31 +0000896 fprintf(stderr,
Michal Ludvig0e035502002-09-23 15:41:01 +0000897 "Unknown syscall opcode (0x%04X) while "
898 "detecting personality of process "
899 "PID=%d\n", (int)call, pid);
900 break;
901 }
902#endif
903 if(currpers != current_personality)
904 {
905 char *names[]={"64 bit", "32 bit"};
906 set_personality(currpers);
Roland McGrath761b5d72002-12-15 23:58:31 +0000907 printf("[ Process PID=%d runs in %s mode. ]\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000908 pid, names[current_personality]);
909 }
Roland McGrath761b5d72002-12-15 23:58:31 +0000910 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000911#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000912# define IA64_PSR_IS ((long)1 << 34)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000913 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000914 ia32 = (psr & IA64_PSR_IS) != 0;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000915 if (!(tcp->flags & TCB_INSYSCALL)) {
916 if (ia32) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000917 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000918 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000919 /* Check if we return from execve. */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000920 } else {
921 if (upeek (pid, PT_R15, &scno) < 0)
922 return -1;
923 }
924 } else {
925 /* syscall in progress */
926 if (upeek (pid, PT_R8, &r8) < 0)
927 return -1;
928 if (upeek (pid, PT_R10, &r10) < 0)
929 return -1;
930 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000931 if (tcp->flags & TCB_WAITEXECVE) {
932 tcp->flags &= ~TCB_WAITEXECVE;
933 return 0;
934 }
935
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000936#elif defined (ARM)
Roland McGrath761b5d72002-12-15 23:58:31 +0000937 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000938 long pc;
939 upeek(pid, 4*15, &pc);
940 umoven(tcp, pc-4, 4, (char *)&scno);
941 scno &= 0x000fffff;
942 }
943#elif defined (M68K)
944 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
945 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +0000946#elif defined (MIPS)
947 if (upeek(pid, REG_A3, &a3) < 0)
948 return -1;
949
950 if(!(tcp->flags & TCB_INSYSCALL)) {
951 if (upeek(pid, REG_V0, &scno) < 0)
952 return -1;
953
954 if (scno < 0 || scno > nsyscalls) {
955 if(a3 == 0 || a3 == -1) {
956 if(debug)
957 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
958 return 0;
959 }
960 }
961 } else {
962 if (upeek(pid, REG_V0, &r2) < 0)
963 return -1;
964 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000965#elif defined (ALPHA)
966 if (upeek(pid, REG_A3, &a3) < 0)
967 return -1;
968
969 if (!(tcp->flags & TCB_INSYSCALL)) {
970 if (upeek(pid, REG_R0, &scno) < 0)
971 return -1;
972
973 /* Check if we return from execve. */
974 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
975 tcp->flags &= ~TCB_WAITEXECVE;
976 return 0;
977 }
978
979 /*
980 * Do some sanity checks to figure out if it's
981 * really a syscall entry
982 */
983 if (scno < 0 || scno > nsyscalls) {
984 if (a3 == 0 || a3 == -1) {
985 if (debug)
986 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
987 return 0;
988 }
989 }
990 }
991 else {
992 if (upeek(pid, REG_R0, &r0) < 0)
993 return -1;
994 }
995#elif defined (SPARC)
996 /* Everything we need is in the current register set. */
997 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
998 return -1;
999
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001000 /* If we are entering, then disassemble the syscall trap. */
1001 if (!(tcp->flags & TCB_INSYSCALL)) {
1002 /* Retrieve the syscall trap instruction. */
1003 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001004 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001005 if (errno)
1006 return -1;
1007
1008 /* Disassemble the trap to see what personality to use. */
1009 switch (trap) {
1010 case 0x91d02010:
1011 /* Linux/SPARC syscall trap. */
1012 set_personality(0);
1013 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +00001014 case 0x91d0206d:
1015 /* Linux/SPARC64 syscall trap. */
1016 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
1017 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001018 case 0x91d02000:
1019 /* SunOS syscall trap. (pers 1) */
1020 fprintf(stderr,"syscall: SunOS no support\n");
1021 return -1;
1022 case 0x91d02008:
1023 /* Solaris 2.x syscall trap. (per 2) */
1024 set_personality(1);
Roland McGrath761b5d72002-12-15 23:58:31 +00001025 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001026 case 0x91d02009:
1027 /* NetBSD/FreeBSD syscall trap. */
1028 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1029 return -1;
1030 case 0x91d02027:
1031 /* Solaris 2.x gettimeofday */
1032 set_personality(1);
1033 break;
1034 default:
1035 /* Unknown syscall trap. */
1036 if(tcp->flags & TCB_WAITEXECVE) {
1037 tcp->flags &= ~TCB_WAITEXECVE;
1038 return 0;
1039 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001040 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001041 return -1;
1042 }
1043
1044 /* Extract the system call number from the registers. */
1045 if (trap == 0x91d02027)
1046 scno = 156;
1047 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001048 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001049 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001050 scno = regs.r_o0;
1051 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001052 }
1053 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001054#elif defined(HPPA)
1055 if (upeek(pid, PT_GR20, &scno) < 0)
1056 return -1;
1057 if (!(tcp->flags & TCB_INSYSCALL)) {
1058 /* Check if we return from execve. */
1059 if ((tcp->flags & TCB_WAITEXECVE)) {
1060 tcp->flags &= ~TCB_WAITEXECVE;
1061 return 0;
1062 }
1063 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001064#elif defined(SH)
1065 /*
1066 * In the new syscall ABI, the system call number is in R3.
1067 */
1068 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1069 return -1;
1070
1071 if (scno < 0) {
1072 /* Odd as it may seem, a glibc bug has been known to cause
1073 glibc to issue bogus negative syscall numbers. So for
1074 our purposes, make strace print what it *should* have been */
1075 long correct_scno = (scno & 0xff);
1076 if (debug)
1077 fprintf(stderr,
Michal Ludvig53b320f2002-09-23 13:30:09 +00001078 "Detected glibc bug: bogus system call number = %ld, "
1079 "correcting to %ld\n",
Wichert Akkermanccef6372002-05-01 16:39:22 +00001080 scno,
1081 correct_scno);
1082 scno = correct_scno;
1083 }
1084
1085
1086 if (!(tcp->flags & TCB_INSYSCALL)) {
1087 /* Check if we return from execve. */
1088 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1089 tcp->flags &= ~TCB_WAITEXECVE;
1090 return 0;
1091 }
1092 }
1093#endif /* SH */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001094#endif /* LINUX */
1095#ifdef SUNOS4
1096 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1097 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001098#elif defined(SH)
1099 /* new syscall ABI returns result in R0 */
1100 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1101 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001102#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001103#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001104#ifdef HAVE_PR_SYSCALL
John Hughes25299712001-03-06 10:10:06 +00001105 scno = tcp->status.PR_SYSCALL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001106#else /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001107#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001108 scno = tcp->status.PR_WHAT;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001109#else /* FREEBSD */
1110 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1111 perror("pread");
1112 return -1;
1113 }
1114 switch (regs.r_eax) {
1115 case SYS_syscall:
1116 case SYS___syscall:
1117 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1118 break;
1119 default:
1120 scno = regs.r_eax;
1121 break;
1122 }
1123#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001124#endif /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001125#endif /* USE_PROCFS */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001126 if (!(tcp->flags & TCB_INSYSCALL))
1127 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001128 return 1;
1129}
1130
Pavel Machek4dc3b142000-02-01 17:58:41 +00001131
1132int
1133syscall_fixup(tcp)
1134struct tcb *tcp;
1135{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001136#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001137 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001138#else /* USE_PROCFS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001139 int scno = tcp->scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001140
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001141 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001142 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001143 if (
1144 scno == SYS_fork
1145#ifdef SYS_vfork
1146 || scno == SYS_vfork
1147#endif /* SYS_vfork */
John Hughes4e36a812001-04-18 15:11:51 +00001148#ifdef SYS_fork1
1149 || scno == SYS_fork1
1150#endif /* SYS_fork1 */
1151#ifdef SYS_forkall
1152 || scno == SYS_forkall
1153#endif /* SYS_forkall */
1154#ifdef SYS_rfork1
1155 || scno == SYS_rfork1
1156#endif /* SYS_fork1 */
1157#ifdef SYS_rforkall
1158 || scno == SYS_rforkall
1159#endif /* SYS_rforkall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001160 ) {
1161 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001162 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001163 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001164 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001165 }
1166 else {
1167 fprintf(stderr, "syscall: missing entry\n");
1168 tcp->flags |= TCB_INSYSCALL;
1169 }
1170 }
1171 }
1172 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001173 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001174 fprintf(stderr, "syscall: missing exit\n");
1175 tcp->flags &= ~TCB_INSYSCALL;
1176 }
1177 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001178#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001179#ifdef SUNOS4
1180 if (!(tcp->flags & TCB_INSYSCALL)) {
1181 if (scno == 0) {
1182 fprintf(stderr, "syscall: missing entry\n");
1183 tcp->flags |= TCB_INSYSCALL;
1184 }
1185 }
1186 else {
1187 if (scno != 0) {
1188 if (debug) {
1189 /*
1190 * This happens when a signal handler
1191 * for a signal which interrupted a
1192 * a system call makes another system call.
1193 */
1194 fprintf(stderr, "syscall: missing exit\n");
1195 }
1196 tcp->flags &= ~TCB_INSYSCALL;
1197 }
1198 }
1199#endif /* SUNOS4 */
1200#ifdef LINUX
1201#if defined (I386)
1202 if (upeek(pid, 4*EAX, &eax) < 0)
1203 return -1;
1204 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1205 if (debug)
1206 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1207 return 0;
1208 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001209#elif defined (X86_64)
1210 if (upeek(pid, 8*RAX, &rax) < 0)
1211 return -1;
1212 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1213 if (debug)
1214 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1215 return 0;
1216 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001217#elif defined (S390) || defined (S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001218 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1219 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001220 if (syscall_mode != -ENOSYS)
1221 syscall_mode = tcp->scno;
1222 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001223 if (debug)
1224 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1225 return 0;
1226 }
Roland McGrath96dc5142003-01-20 10:23:04 +00001227 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1228 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1229 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1230 /*
1231 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1232 * flag set for the post-execve SIGTRAP to see and reset.
1233 */
1234 gpr2 = 0;
1235 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001236#elif defined (POWERPC)
1237# define SO_MASK 0x10000000
Roland McGratheb285352003-01-14 09:59:00 +00001238 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001239 return -1;
Roland McGratheb285352003-01-14 09:59:00 +00001240 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001241 return -1;
1242 if (flags & SO_MASK)
1243 result = -result;
1244#elif defined (M68K)
1245 if (upeek(pid, 4*PT_D0, &d0) < 0)
1246 return -1;
1247 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1248 if (debug)
1249 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1250 return 0;
1251 }
1252#elif defined (ARM)
1253 if (upeek(pid, 4*0, (long *)&r0) < 0)
1254 return -1;
1255 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1256 if (debug)
1257 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
1258 return 0;
1259 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001260#elif defined (HPPA)
1261 if (upeek(pid, PT_GR28, &r28) < 0)
1262 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001263#elif defined(IA64)
1264 if (upeek(pid, PT_R10, &r10) < 0)
1265 return -1;
1266 if (upeek(pid, PT_R8, &r8) < 0)
1267 return -1;
1268 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1269 if (debug)
1270 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1271 return 0;
1272 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001273#endif
1274#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001275 return 1;
1276}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001277
Pavel Machek4dc3b142000-02-01 17:58:41 +00001278int
1279get_error(tcp)
1280struct tcb *tcp;
1281{
1282 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001283#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001284#if defined(S390) || defined(S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001285 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1286 tcp->u_rval = -1;
1287 u_error = -gpr2;
1288 }
1289 else {
1290 tcp->u_rval = gpr2;
1291 u_error = 0;
1292 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001293#else /* !S390 && !S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001294#ifdef I386
1295 if (eax < 0 && -eax < nerrnos) {
1296 tcp->u_rval = -1;
1297 u_error = -eax;
1298 }
1299 else {
1300 tcp->u_rval = eax;
1301 u_error = 0;
1302 }
1303#else /* !I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001304#ifdef X86_64
1305 if (rax < 0 && -rax < nerrnos) {
1306 tcp->u_rval = -1;
1307 u_error = -rax;
1308 }
1309 else {
1310 tcp->u_rval = rax;
1311 u_error = 0;
1312 }
1313#else
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001314#ifdef IA64
1315 if (ia32) {
1316 int err;
1317
1318 err = (int)r8;
1319 if (err < 0 && -err < nerrnos) {
1320 tcp->u_rval = -1;
1321 u_error = -err;
1322 }
1323 else {
1324 tcp->u_rval = err;
1325 u_error = 0;
1326 }
1327 } else {
1328 if (r10) {
1329 tcp->u_rval = -1;
1330 u_error = r8;
1331 } else {
1332 tcp->u_rval = r8;
1333 u_error = 0;
1334 }
1335 }
1336#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001337#ifdef MIPS
1338 if (a3) {
1339 tcp->u_rval = -1;
1340 u_error = r2;
1341 } else {
1342 tcp->u_rval = r2;
1343 u_error = 0;
1344 }
1345#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001346#ifdef POWERPC
1347 if (result && (unsigned) -result < nerrnos) {
1348 tcp->u_rval = -1;
1349 u_error = -result;
1350 }
1351 else {
1352 tcp->u_rval = result;
1353 u_error = 0;
1354 }
1355#else /* !POWERPC */
1356#ifdef M68K
1357 if (d0 && (unsigned) -d0 < nerrnos) {
1358 tcp->u_rval = -1;
1359 u_error = -d0;
1360 }
1361 else {
1362 tcp->u_rval = d0;
1363 u_error = 0;
1364 }
1365#else /* !M68K */
1366#ifdef ARM
1367 if (r0 && (unsigned) -r0 < nerrnos) {
1368 tcp->u_rval = -1;
1369 u_error = -r0;
1370 }
1371 else {
1372 tcp->u_rval = r0;
1373 u_error = 0;
1374 }
1375#else /* !ARM */
1376#ifdef ALPHA
1377 if (a3) {
1378 tcp->u_rval = -1;
1379 u_error = r0;
1380 }
1381 else {
1382 tcp->u_rval = r0;
1383 u_error = 0;
1384 }
1385#else /* !ALPHA */
1386#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001387 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001388 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001389 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001390 }
1391 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001392 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001393 u_error = 0;
1394 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001395#else /* !SPARC */
1396#ifdef HPPA
1397 if (r28 && (unsigned) -r28 < nerrnos) {
1398 tcp->u_rval = -1;
1399 u_error = -r28;
1400 }
1401 else {
1402 tcp->u_rval = r28;
1403 u_error = 0;
1404 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001405#else
1406#ifdef SH
1407 /* interpret R0 as return value or error number */
1408 if (r0 && (unsigned) -r0 < nerrnos) {
1409 tcp->u_rval = -1;
1410 u_error = -r0;
1411 }
1412 else {
1413 tcp->u_rval = r0;
1414 u_error = 0;
1415 }
1416#endif /* SH */
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001417#endif /* HPPA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001418#endif /* SPARC */
1419#endif /* ALPHA */
1420#endif /* ARM */
1421#endif /* M68K */
1422#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001423#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001424#endif /* IA64 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001425#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001426#endif /* I386 */
Michal Ludvig10a88d02002-10-07 14:31:00 +00001427#endif /* S390 || S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001428#endif /* LINUX */
1429#ifdef SUNOS4
1430 /* get error code from user struct */
1431 if (upeek(pid, uoff(u_error), &u_error) < 0)
1432 return -1;
1433 u_error >>= 24; /* u_error is a char */
1434
1435 /* get system call return value */
1436 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1437 return -1;
1438#endif /* SUNOS4 */
1439#ifdef SVR4
1440#ifdef SPARC
1441 /* Judicious guessing goes a long way. */
1442 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1443 tcp->u_rval = -1;
1444 u_error = tcp->status.pr_reg[R_O0];
1445 }
1446 else {
1447 tcp->u_rval = tcp->status.pr_reg[R_O0];
1448 u_error = 0;
1449 }
1450#endif /* SPARC */
1451#ifdef I386
1452 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001453 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001454 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001455 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001456 }
1457 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001458 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001459#ifdef HAVE_LONG_LONG
1460 tcp->u_lrval =
1461 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1462 tcp->status.PR_REG[EAX];
1463#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001464 u_error = 0;
1465 }
1466#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001467#ifdef X86_64
1468 /* Wanna know how to kill an hour single-stepping? */
1469 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1470 tcp->u_rval = -1;
1471 u_error = tcp->status.PR_REG[RAX];
1472 }
1473 else {
1474 tcp->u_rval = tcp->status.PR_REG[RAX];
1475 u_error = 0;
1476 }
1477#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001478#ifdef MIPS
1479 if (tcp->status.pr_reg[CTX_A3]) {
1480 tcp->u_rval = -1;
1481 u_error = tcp->status.pr_reg[CTX_V0];
1482 }
1483 else {
1484 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1485 u_error = 0;
1486 }
1487#endif /* MIPS */
1488#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001489#ifdef FREEBSD
1490 if (regs.r_eflags & PSL_C) {
1491 tcp->u_rval = -1;
1492 u_error = regs.r_eax;
1493 } else {
1494 tcp->u_rval = regs.r_eax;
1495 tcp->u_lrval =
1496 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1497 u_error = 0;
1498 }
Roland McGrath761b5d72002-12-15 23:58:31 +00001499#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001500 tcp->u_error = u_error;
1501 return 1;
1502}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001503
Roland McGrathb69f81b2002-12-21 23:25:18 +00001504int
1505force_result(tcp, error, rval)
1506 struct tcb *tcp;
1507 int error;
1508 long rval;
1509{
1510#ifdef LINUX
1511#if defined(S390) || defined(S390X)
1512 gpr2 = error ? -error : rval;
Roland McGrathd38809b2003-01-10 07:50:21 +00001513 if (upeek(tcp->pid, PT_GPR2, &gpr2) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001514 return -1;
1515 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1516 return -1;
1517#else /* !S390 && !S390X */
1518#ifdef I386
1519 eax = error ? -error : rval;
1520 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1521 return -1;
1522#else /* !I386 */
1523#ifdef X86_64
1524 rax = error ? -error : rval;
1525 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 4), rax) < 0)
1526 return -1;
1527#else
1528#ifdef IA64
1529 if (ia32) {
1530 r8 = error ? -error : rval;
1531 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1532 return -1;
1533 }
1534 else {
1535 if (error) {
1536 r8 = error;
1537 r10 = -1;
1538 }
1539 else {
1540 r8 = rval;
1541 r10 = 0;
1542 }
1543 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1544 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1545 return -1;
1546 }
1547#else /* !IA64 */
1548#ifdef MIPS
1549 if (error) {
1550 r2 = error;
1551 a3 = -1;
1552 }
1553 else {
1554 r2 = rval;
1555 a3 = 0;
1556 }
1557 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1558 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1559 return -1;
1560#else
1561#ifdef POWERPC
Roland McGratheb285352003-01-14 09:59:00 +00001562 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001563 return -1;
1564 if (error) {
1565 flags |= SO_MASK;
1566 result = error;
1567 }
1568 else {
1569 flags &= ~SO_MASK;
1570 result = rval;
1571 }
Roland McGratheb285352003-01-14 09:59:00 +00001572 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1573 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001574 return -1;
1575#else /* !POWERPC */
1576#ifdef M68K
1577 d0 = error ? -error : rval;
1578 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1579 return -1;
1580#else /* !M68K */
1581#ifdef ARM
1582 r0 = error ? -error : rval;
1583 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), r0) < 0)
1584 return -1;
1585#else /* !ARM */
1586#ifdef ALPHA
1587 if (error) {
1588 a3 = -1;
1589 r0 = error;
1590 }
1591 else {
1592 a3 = 0;
1593 r0 = rval;
1594 }
1595 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1596 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1597 return -1;
1598#else /* !ALPHA */
1599#ifdef SPARC
1600 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0)
1601 return -1;
1602 if (error) {
1603 regs.r_psr |= PSR_C;
1604 regs.r_o0 = error;
1605 }
1606 else {
1607 regs.r_psr &= ~PSR_C;
1608 regs.r_o0 = rval;
1609 }
1610 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)&regs, 0) < 0)
1611 return -1;
1612#else /* !SPARC */
1613#ifdef HPPA
1614 r28 = error ? -error : rval;
1615 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1616 return -1;
1617#else
1618#ifdef SH
1619 r0 = error ? -error : rval;
1620 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1621 return -1;
1622#endif /* SH */
1623#endif /* HPPA */
1624#endif /* SPARC */
1625#endif /* ALPHA */
1626#endif /* ARM */
1627#endif /* M68K */
1628#endif /* POWERPC */
1629#endif /* MIPS */
1630#endif /* IA64 */
1631#endif /* X86_64 */
1632#endif /* I386 */
1633#endif /* S390 || S390X */
1634#endif /* LINUX */
1635#ifdef SUNOS4
1636 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1637 error << 24) < 0 ||
1638 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1639 return -1;
1640#endif /* SUNOS4 */
1641#ifdef SVR4
1642 /* XXX no clue */
1643 return -1;
1644#endif /* SVR4 */
1645#ifdef FREEBSD
1646 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1647 perror("pread");
1648 return -1;
1649 }
1650 if (error) {
1651 regs.r_eflags |= PSL_C;
1652 regs.r_eax = error;
1653 }
1654 else {
1655 regs.r_eflags &= ~PSL_C;
1656 regs.r_eax = rval;
1657 }
1658 if (pwrite(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1659 perror("pwrite");
1660 return -1;
1661 }
1662#endif /* FREEBSD */
1663
1664 /* All branches reach here on success (only). */
1665 tcp->u_error = error;
1666 tcp->u_rval = rval;
1667 return 0;
1668}
1669
Pavel Machek4dc3b142000-02-01 17:58:41 +00001670int syscall_enter(tcp)
1671struct tcb *tcp;
1672{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001673#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001674 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001675#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001676#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001677#if defined(S390) || defined(S390X)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001678 {
1679 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001680 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1681 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001682 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001683 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001684 for (i = 0; i < tcp->u_nargs; i++) {
Michal Ludvig10a88d02002-10-07 14:31:00 +00001685 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001686 return -1;
1687 }
1688 }
1689#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001690 {
1691 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001692 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1693 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001694 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001695 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001696 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001697 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1698 * for scno somewhere above here!
1699 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001700 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1701 return -1;
1702 }
1703 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001704#elif defined (IA64)
1705 {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001706 if (!ia32) {
1707 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1708 /* be backwards compatible with kernel < 2.4.4... */
1709# ifndef PT_RBS_END
1710# define PT_RBS_END PT_AR_BSP
1711# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001712
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001713 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001714 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001715 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1716 return -1;
1717
1718 sof = (cfm >> 0) & 0x7f;
1719 sol = (cfm >> 7) & 0x7f;
1720 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1721
1722 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1723 && sysent[tcp->scno].nargs != -1)
1724 tcp->u_nargs = sysent[tcp->scno].nargs;
1725 else
1726 tcp->u_nargs = MAX_ARGS;
1727 for (i = 0; i < tcp->u_nargs; ++i) {
1728 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1729 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1730 return -1;
1731 }
1732 } else {
1733 int i;
1734
1735 if (/* EBX = out0 */
1736 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1737 /* ECX = out1 */
1738 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1739 /* EDX = out2 */
1740 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1741 /* ESI = out3 */
1742 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1743 /* EDI = out4 */
1744 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1745 /* EBP = out5 */
1746 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1747 return -1;
1748
1749 for (i = 0; i < 6; ++i)
1750 /* truncate away IVE sign-extension */
1751 tcp->u_arg[i] &= 0xffffffff;
1752
1753 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1754 && sysent[tcp->scno].nargs != -1)
1755 tcp->u_nargs = sysent[tcp->scno].nargs;
1756 else
1757 tcp->u_nargs = 5;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001758 }
1759 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001760#elif defined (MIPS)
1761 {
1762 long sp;
1763 int i, nargs;
1764
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001765 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1766 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001767 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001768 nargs = tcp->u_nargs = MAX_ARGS;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001769 if(nargs > 4) {
1770 if(upeek(pid, REG_SP, &sp) < 0)
1771 return -1;
1772 for(i = 0; i < 4; i++) {
1773 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1774 return -1;
1775 }
1776 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1777 (char *)(tcp->u_arg + 4));
1778 } else {
1779 for(i = 0; i < nargs; i++) {
1780 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1781 return -1;
1782 }
1783 }
1784 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001785#elif defined (POWERPC)
Roland McGrath761b5d72002-12-15 23:58:31 +00001786#ifndef PT_ORIG_R3
1787#define PT_ORIG_R3 34
1788#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001789 {
1790 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001791 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1792 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001793 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001794 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001795 for (i = 0; i < tcp->u_nargs; i++) {
Roland McGratheb285352003-01-14 09:59:00 +00001796 if (upeek(pid, (i==0) ?
1797 (sizeof(unsigned long)*PT_ORIG_R3) :
1798 ((i+PT_R3)*sizeof(unsigned long)),
1799 &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001800 return -1;
1801 }
1802 }
1803#elif defined (SPARC)
1804 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001805 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001806
1807 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1808 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001809 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001810 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001811 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001812 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001813 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001814#elif defined (HPPA)
1815 {
1816 int i;
1817
1818 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1819 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001820 else
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001821 tcp->u_nargs = MAX_ARGS;
1822 for (i = 0; i < tcp->u_nargs; i++) {
1823 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1824 return -1;
1825 }
1826 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001827#elif defined(SH)
1828 {
Roland McGrath761b5d72002-12-15 23:58:31 +00001829 int i;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001830 static int syscall_regs[] = {
1831 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1832 REG_REG0, REG_REG0+1, REG_REG0+2
1833 };
1834
1835 tcp->u_nargs = sysent[tcp->scno].nargs;
1836 for (i = 0; i < tcp->u_nargs; i++) {
1837 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1838 return -1;
1839 }
1840 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001841#elif defined(X86_64)
1842 {
1843 int i;
1844 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1845 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
1846 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
1847 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001848
Michal Ludvig0e035502002-09-23 15:41:01 +00001849 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1850 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001851 else
Michal Ludvig0e035502002-09-23 15:41:01 +00001852 tcp->u_nargs = MAX_ARGS;
1853 for (i = 0; i < tcp->u_nargs; i++) {
1854 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
1855 return -1;
1856 }
1857 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001858#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001859 {
1860 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001861 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1862 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001863 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001864 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001865 for (i = 0; i < tcp->u_nargs; i++) {
1866 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1867 return -1;
1868 }
1869 }
Roland McGrath761b5d72002-12-15 23:58:31 +00001870#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001871#endif /* LINUX */
1872#ifdef SUNOS4
1873 {
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++) {
1880 struct user *u;
1881
1882 if (upeek(pid, uoff(u_arg[0]) +
1883 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1884 return -1;
1885 }
1886 }
1887#endif /* SUNOS4 */
1888#ifdef SVR4
1889#ifdef MIPS
1890 /*
1891 * SGI is broken: even though it has pr_sysarg, it doesn't
1892 * set them on system call entry. Get a clue.
1893 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001894 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001895 tcp->u_nargs = sysent[tcp->scno].nargs;
1896 else
1897 tcp->u_nargs = tcp->status.pr_nsysarg;
1898 if (tcp->u_nargs > 4) {
1899 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1900 4*sizeof(tcp->u_arg[0]));
1901 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1902 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1903 }
1904 else {
1905 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1906 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1907 }
John Hughes25299712001-03-06 10:10:06 +00001908#elif UNIXWARE >= 2
1909 /*
1910 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1911 */
1912 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1913 tcp->u_nargs = sysent[tcp->scno].nargs;
1914 else
1915 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1916 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1917 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1918#elif defined (HAVE_PR_SYSCALL)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001919 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001920 tcp->u_nargs = sysent[tcp->scno].nargs;
1921 else
1922 tcp->u_nargs = tcp->status.pr_nsysarg;
1923 {
1924 int i;
1925 for (i = 0; i < tcp->u_nargs; i++)
1926 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1927 }
John Hughes25299712001-03-06 10:10:06 +00001928#elif defined (I386)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001929 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001930 tcp->u_nargs = sysent[tcp->scno].nargs;
1931 else
1932 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001933 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001934 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
John Hughes25299712001-03-06 10:10:06 +00001935#else
1936 I DONT KNOW WHAT TO DO
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001937#endif /* !HAVE_PR_SYSCALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001938#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001939#ifdef FREEBSD
1940 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1941 sysent[tcp->scno].nargs > tcp->status.val)
1942 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001943 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001944 tcp->u_nargs = tcp->status.val;
1945 if (tcp->u_nargs < 0)
1946 tcp->u_nargs = 0;
1947 if (tcp->u_nargs > MAX_ARGS)
1948 tcp->u_nargs = MAX_ARGS;
1949 switch(regs.r_eax) {
1950 case SYS___syscall:
1951 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1952 regs.r_esp + sizeof(int) + sizeof(quad_t));
1953 break;
1954 case SYS_syscall:
1955 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1956 regs.r_esp + 2 * sizeof(int));
1957 break;
1958 default:
1959 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1960 regs.r_esp + sizeof(int));
1961 break;
1962 }
1963#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001964 return 1;
1965}
1966
1967int
1968trace_syscall(tcp)
1969struct tcb *tcp;
1970{
1971 int sys_res;
1972 struct timeval tv;
1973 int res;
1974
1975 /* Measure the exit time as early as possible to avoid errors. */
1976 if (dtime && (tcp->flags & TCB_INSYSCALL))
1977 gettimeofday(&tv, NULL);
1978
1979 res = get_scno(tcp);
1980 if (res != 1)
1981 return res;
1982
1983 res = syscall_fixup(tcp);
1984 if (res != 1)
1985 return res;
1986
1987 if (tcp->flags & TCB_INSYSCALL) {
1988 long u_error;
1989 res = get_error(tcp);
1990 if (res != 1)
1991 return res;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001992
1993 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001994 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1995 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001996 tcp->flags &= ~TCB_INSYSCALL;
1997 return 0;
1998 }
1999
2000 if (tcp->flags & TCB_REPRINT) {
2001 printleader(tcp);
2002 tprintf("<... ");
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002003 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002004 tprintf("syscall_%lu", tcp->scno);
2005 else
2006 tprintf("%s", sysent[tcp->scno].sys_name);
2007 tprintf(" resumed> ");
2008 }
2009
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002010 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002011 call_count[tcp->scno]++;
2012 if (tcp->u_error)
2013 error_count[tcp->scno]++;
2014 tv_sub(&tv, &tv, &tcp->etime);
2015#ifdef LINUX
2016 if (tv_cmp(&tv, &tcp->dtime) > 0) {
2017 static struct timeval one_tick =
2018 { 0, 1000000 / HZ };
2019
2020 if (tv_nz(&tcp->dtime))
2021 tv = tcp->dtime;
2022 else if (tv_cmp(&tv, &one_tick) > 0) {
2023 if (tv_cmp(&shortest, &one_tick) < 0)
2024 tv = shortest;
2025 else
2026 tv = one_tick;
2027 }
2028 }
2029#endif /* LINUX */
2030 if (tv_cmp(&tv, &shortest) < 0)
2031 shortest = tv;
2032 tv_add(&tv_count[tcp->scno],
2033 &tv_count[tcp->scno], &tv);
2034 tcp->flags &= ~TCB_INSYSCALL;
2035 return 0;
2036 }
2037
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002038 if (tcp->scno >= nsyscalls || tcp->scno < 0
Pavel Machek4dc3b142000-02-01 17:58:41 +00002039 || (qual_flags[tcp->scno] & QUAL_RAW))
2040 sys_res = printargs(tcp);
Michal Ludvig17f8fb32002-11-06 13:17:21 +00002041 else {
2042 if (not_failing_only && tcp->u_error)
Roland McGrath761b5d72002-12-15 23:58:31 +00002043 return 0; /* ignore failed syscalls */
Pavel Machek4dc3b142000-02-01 17:58:41 +00002044 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
Roland McGrath761b5d72002-12-15 23:58:31 +00002045 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00002046 u_error = tcp->u_error;
2047 tprintf(") ");
2048 tabto(acolumn);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002049 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2050 qual_flags[tcp->scno] & QUAL_RAW) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002051 if (u_error)
2052 tprintf("= -1 (errno %ld)", u_error);
2053 else
2054 tprintf("= %#lx", tcp->u_rval);
2055 }
2056 else if (!(sys_res & RVAL_NONE) && u_error) {
2057 switch (u_error) {
2058#ifdef LINUX
2059 case ERESTARTSYS:
2060 tprintf("= ? ERESTARTSYS (To be restarted)");
2061 break;
2062 case ERESTARTNOINTR:
2063 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2064 break;
2065 case ERESTARTNOHAND:
2066 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2067 break;
2068#endif /* LINUX */
2069 default:
2070 tprintf("= -1 ");
Wichert Akkerman4527dae2002-03-31 19:03:29 +00002071 if (u_error < 0)
2072 tprintf("E??? (errno %ld)", u_error);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002073 else if (u_error < nerrnos)
Roland McGrath761b5d72002-12-15 23:58:31 +00002074 tprintf("%s (%s)", errnoent[u_error],
2075 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00002076 else
Roland McGrath761b5d72002-12-15 23:58:31 +00002077 tprintf("ERRNO_%ld (%s)", u_error,
2078 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00002079 break;
2080 }
2081 }
2082 else {
2083 if (sys_res & RVAL_NONE)
2084 tprintf("= ?");
2085 else {
2086 switch (sys_res & RVAL_MASK) {
2087 case RVAL_HEX:
2088 tprintf("= %#lx", tcp->u_rval);
2089 break;
2090 case RVAL_OCTAL:
2091 tprintf("= %#lo", tcp->u_rval);
2092 break;
2093 case RVAL_UDECIMAL:
2094 tprintf("= %lu", tcp->u_rval);
2095 break;
2096 case RVAL_DECIMAL:
2097 tprintf("= %ld", tcp->u_rval);
2098 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002099#ifdef HAVE_LONG_LONG
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002100 case RVAL_LHEX:
2101 tprintf("= %#llx", tcp->u_lrval);
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002102 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002103 case RVAL_LOCTAL:
2104 tprintf("= %#llo", tcp->u_lrval);
2105 break;
2106 case RVAL_LUDECIMAL:
2107 tprintf("= %llu", tcp->u_lrval);
2108 break;
2109 case RVAL_LDECIMAL:
2110 tprintf("= %lld", tcp->u_lrval);
2111 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002112#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00002113 default:
2114 fprintf(stderr,
2115 "invalid rval format\n");
2116 break;
2117 }
2118 }
2119 if ((sys_res & RVAL_STR) && tcp->auxstr)
2120 tprintf(" (%s)", tcp->auxstr);
2121 }
2122 if (dtime) {
2123 tv_sub(&tv, &tv, &tcp->etime);
2124 tprintf(" <%ld.%06ld>",
2125 (long) tv.tv_sec, (long) tv.tv_usec);
2126 }
2127 printtrailer(tcp);
2128
2129 dumpio(tcp);
2130 if (fflush(tcp->outf) == EOF)
2131 return -1;
2132 tcp->flags &= ~TCB_INSYSCALL;
2133 return 0;
2134 }
2135
2136 /* Entering system call */
2137 res = syscall_enter(tcp);
2138 if (res != 1)
2139 return res;
2140
Pavel Machekd8ae7e32000-02-01 17:17:25 +00002141 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002142#ifdef LINUX
Michal Ludvig0e035502002-09-23 15:41:01 +00002143#if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002144 case SYS_socketcall:
2145 decode_subcall(tcp, SYS_socket_subcall,
2146 SYS_socket_nsubcalls, deref_style);
2147 break;
2148 case SYS_ipc:
2149 decode_subcall(tcp, SYS_ipc_subcall,
2150 SYS_ipc_nsubcalls, shift_style);
2151 break;
Michal Ludvig0e035502002-09-23 15:41:01 +00002152#endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002153#ifdef SPARC
2154 case SYS_socketcall:
2155 sparc_socket_decode (tcp);
2156 break;
2157#endif
2158#endif /* LINUX */
2159#ifdef SVR4
2160#ifdef SYS_pgrpsys_subcall
2161 case SYS_pgrpsys:
2162 decode_subcall(tcp, SYS_pgrpsys_subcall,
2163 SYS_pgrpsys_nsubcalls, shift_style);
2164 break;
2165#endif /* SYS_pgrpsys_subcall */
2166#ifdef SYS_sigcall_subcall
2167 case SYS_sigcall:
2168 decode_subcall(tcp, SYS_sigcall_subcall,
2169 SYS_sigcall_nsubcalls, mask_style);
2170 break;
2171#endif /* SYS_sigcall_subcall */
2172 case SYS_msgsys:
2173 decode_subcall(tcp, SYS_msgsys_subcall,
2174 SYS_msgsys_nsubcalls, shift_style);
2175 break;
2176 case SYS_shmsys:
2177 decode_subcall(tcp, SYS_shmsys_subcall,
2178 SYS_shmsys_nsubcalls, shift_style);
2179 break;
2180 case SYS_semsys:
2181 decode_subcall(tcp, SYS_semsys_subcall,
2182 SYS_semsys_nsubcalls, shift_style);
2183 break;
2184#if 0 /* broken */
2185 case SYS_utssys:
2186 decode_subcall(tcp, SYS_utssys_subcall,
2187 SYS_utssys_nsubcalls, shift_style);
2188 break;
2189#endif
2190 case SYS_sysfs:
2191 decode_subcall(tcp, SYS_sysfs_subcall,
2192 SYS_sysfs_nsubcalls, shift_style);
2193 break;
2194 case SYS_spcall:
2195 decode_subcall(tcp, SYS_spcall_subcall,
2196 SYS_spcall_nsubcalls, shift_style);
2197 break;
2198#ifdef SYS_context_subcall
2199 case SYS_context:
2200 decode_subcall(tcp, SYS_context_subcall,
2201 SYS_context_nsubcalls, shift_style);
2202 break;
2203#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00002204#ifdef SYS_door_subcall
2205 case SYS_door:
2206 decode_subcall(tcp, SYS_door_subcall,
2207 SYS_door_nsubcalls, door_style);
2208 break;
2209#endif /* SYS_door_subcall */
John Hughesbdf48f52001-03-06 15:08:09 +00002210#ifdef SYS_kaio_subcall
2211 case SYS_kaio:
2212 decode_subcall(tcp, SYS_kaio_subcall,
2213 SYS_kaio_nsubcalls, shift_style);
2214 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002215#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002216#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002217#ifdef FREEBSD
2218 case SYS_msgsys:
2219 case SYS_shmsys:
2220 case SYS_semsys:
2221 decode_subcall(tcp, 0, 0, table_style);
2222 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002223#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002224#ifdef SUNOS4
2225 case SYS_semsys:
2226 decode_subcall(tcp, SYS_semsys_subcall,
2227 SYS_semsys_nsubcalls, shift_style);
2228 break;
2229 case SYS_msgsys:
2230 decode_subcall(tcp, SYS_msgsys_subcall,
2231 SYS_msgsys_nsubcalls, shift_style);
2232 break;
2233 case SYS_shmsys:
2234 decode_subcall(tcp, SYS_shmsys_subcall,
2235 SYS_shmsys_nsubcalls, shift_style);
2236 break;
2237#endif
2238 }
2239
2240 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002241 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002242 tcp->flags |= TCB_INSYSCALL;
2243 return 0;
2244 }
2245
2246 if (cflag) {
2247 gettimeofday(&tcp->etime, NULL);
2248 tcp->flags |= TCB_INSYSCALL;
2249 return 0;
2250 }
2251
2252 printleader(tcp);
2253 tcp->flags &= ~TCB_REPRINT;
2254 tcp_last = tcp;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002255 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002256 tprintf("syscall_%lu(", tcp->scno);
2257 else
2258 tprintf("%s(", sysent[tcp->scno].sys_name);
Roland McGrath761b5d72002-12-15 23:58:31 +00002259 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002260 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2261 sys_res = printargs(tcp);
2262 else
2263 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2264 if (fflush(tcp->outf) == EOF)
2265 return -1;
2266 tcp->flags |= TCB_INSYSCALL;
2267 /* Measure the entrance time as late as possible to avoid errors. */
2268 if (dtime)
2269 gettimeofday(&tcp->etime, NULL);
2270 return sys_res;
2271}
2272
2273int
2274printargs(tcp)
2275struct tcb *tcp;
2276{
2277 if (entering(tcp)) {
2278 int i;
2279
2280 for (i = 0; i < tcp->u_nargs; i++)
2281 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2282 }
2283 return 0;
2284}
2285
2286long
2287getrval2(tcp)
2288struct tcb *tcp;
2289{
2290 long val = -1;
2291
2292#ifdef LINUX
2293#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002294 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002295 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
2296 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002297 val = regs.r_o1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002298#endif /* SPARC */
2299#endif /* LINUX */
2300
2301#ifdef SUNOS4
2302 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2303 return -1;
2304#endif /* SUNOS4 */
2305
2306#ifdef SVR4
2307#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002308 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002309#endif /* SPARC */
2310#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002311 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002312#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00002313#ifdef X86_64
2314 val = tcp->status.PR_REG[RDX];
2315#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002316#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002317 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002318#endif /* MIPS */
2319#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002320#ifdef FREEBSD
2321 struct reg regs;
2322 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
2323 val = regs.r_edx;
Roland McGrath761b5d72002-12-15 23:58:31 +00002324#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002325 return val;
2326}
2327
2328/*
2329 * Apparently, indirect system calls have already be converted by ptrace(2),
2330 * so if you see "indir" this program has gone astray.
2331 */
2332int
2333sys_indir(tcp)
2334struct tcb *tcp;
2335{
2336 int i, scno, nargs;
2337
2338 if (entering(tcp)) {
2339 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2340 fprintf(stderr, "Bogus syscall: %u\n", scno);
2341 return 0;
2342 }
2343 nargs = sysent[scno].nargs;
2344 tprintf("%s", sysent[scno].sys_name);
2345 for (i = 0; i < nargs; i++)
2346 tprintf(", %#lx", tcp->u_arg[i+1]);
2347 }
2348 return 0;
2349}
2350
2351static int
2352time_cmp(a, b)
2353void *a;
2354void *b;
2355{
2356 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2357}
2358
2359static int
2360syscall_cmp(a, b)
2361void *a;
2362void *b;
2363{
2364 return strcmp(sysent[*((int *) a)].sys_name,
2365 sysent[*((int *) b)].sys_name);
2366}
2367
2368static int
2369count_cmp(a, b)
2370void *a;
2371void *b;
2372{
2373 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2374
2375 return (m < n) ? 1 : (m > n) ? -1 : 0;
2376}
2377
2378static int (*sortfun)();
2379static struct timeval overhead = { -1, -1 };
2380
2381void
2382set_sortby(sortby)
2383char *sortby;
2384{
2385 if (strcmp(sortby, "time") == 0)
2386 sortfun = time_cmp;
2387 else if (strcmp(sortby, "calls") == 0)
2388 sortfun = count_cmp;
2389 else if (strcmp(sortby, "name") == 0)
2390 sortfun = syscall_cmp;
2391 else if (strcmp(sortby, "nothing") == 0)
2392 sortfun = NULL;
2393 else {
2394 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2395 exit(1);
2396 }
2397}
2398
2399void set_overhead(n)
2400int n;
2401{
2402 overhead.tv_sec = n / 1000000;
2403 overhead.tv_usec = n % 1000000;
2404}
2405
2406void
2407call_summary(outf)
2408FILE *outf;
2409{
2410 int i, j;
2411 int call_cum, error_cum;
2412 struct timeval tv_cum, dtv;
2413 double percent;
2414 char *dashes = "-------------------------";
2415 char error_str[16];
2416
2417 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2418 if (overhead.tv_sec == -1) {
2419 tv_mul(&overhead, &shortest, 8);
2420 tv_div(&overhead, &overhead, 10);
2421 }
2422 for (i = 0; i < nsyscalls; i++) {
2423 sorted_count[i] = i;
2424 if (call_count[i] == 0)
2425 continue;
2426 tv_mul(&dtv, &overhead, call_count[i]);
2427 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2428 call_cum += call_count[i];
2429 error_cum += error_count[i];
2430 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2431 }
2432 if (sortfun)
2433 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2434 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2435 "% time", "seconds", "usecs/call",
2436 "calls", "errors", "syscall");
2437 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2438 dashes, dashes, dashes, dashes, dashes, dashes);
2439 for (i = 0; i < nsyscalls; i++) {
2440 j = sorted_count[i];
2441 if (call_count[j] == 0)
2442 continue;
2443 tv_div(&dtv, &tv_count[j], call_count[j]);
2444 if (error_count[j])
2445 sprintf(error_str, "%d", error_count[j]);
2446 else
2447 error_str[0] = '\0';
2448 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2449 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2450 percent, (long) tv_count[j].tv_sec,
2451 (long) tv_count[j].tv_usec,
2452 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2453 call_count[j], error_str, sysent[j].sys_name);
2454 }
2455 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2456 dashes, dashes, dashes, dashes, dashes, dashes);
2457 if (error_cum)
2458 sprintf(error_str, "%d", error_cum);
2459 else
2460 error_str[0] = '\0';
2461 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2462 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2463 call_cum, error_str, "total");
2464}