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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #ifdef _LARGEFILE64_SOURCE
41 /* This is the macro everything checks before using foo64 names. */
42 # ifndef _LFS64_LARGEFILE
43 # define _LFS64_LARGEFILE 1
47 /* configuration section */
49 #if defined(LINUX) && defined(MIPS)
50 #define MAX_QUALS 5000 /* maximum number of syscalls, signals, etc. */
52 #define MAX_QUALS 2048 /* maximum number of syscalls, signals, etc. */
55 #ifndef DEFAULT_STRLEN
56 #define DEFAULT_STRLEN 32 /* default maximum # of bytes printed in
57 `printstr', change with `-s' switch */
59 #ifndef DEFAULT_ACOLUMN
60 #define DEFAULT_ACOLUMN 40 /* default alignment column for results */
63 #define MAX_ARGS 32 /* maximum number of args to a syscall */
65 #ifndef DEFAULT_SORTBY
66 #define DEFAULT_SORTBY "time" /* default sorting method for call profiling */
69 #include <sys/types.h>
81 #endif /* STDC_HEADERS */
88 # if defined(SPARC) || defined(SPARC64)
99 #if defined(SVR4) || defined(FREEBSD)
107 #error "FreeBSD support is only for i386 arch right now."
109 #include <machine/psl.h>
110 #include <machine/reg.h>
111 #include <sys/syscall.h>
115 #include <sys/procfs.h>
116 #ifdef HAVE_MP_PROCFS
120 #include <sys/pioctl.h>
122 #else /* !USE_PROCFS */
123 #if (defined(LINUXSPARC) || defined (LINUX_X86_64)) && defined(__GLIBC__)
124 #include <sys/ptrace.h>
126 /* Work around awkward prototype in ptrace.h. */
127 #define ptrace xptrace
128 #include <sys/ptrace.h>
132 #include <asm/ptrace.h>
137 extern long ptrace(int, int, char *, long);
139 extern int ptrace(int, int, char *, int, ...);
141 #else /* !__STDC__ */
143 #endif /* !__STDC__ */
144 #endif /* !LINUXSPARC */
148 #if !defined(__GLIBC__)
149 #define PTRACE_PEEKUSER PTRACE_PEEKUSR
150 #define PTRACE_POKEUSER PTRACE_POKEUSR
167 # define PT_GR20 (20*4)
168 # define PT_GR26 (26*4)
169 # define PT_GR28 (28*4)
170 # define PT_IAOQ0 (106*4)
171 # define PT_IAOQ1 (107*4)
174 /* SH64 Linux - this code assumes the following kernel API for system calls:
176 System Call Offset 16 (actually, (syscall no.) | (0x1n << 16),
177 where n = no. of parameters.
178 Other regs Offset 24+
180 On entry: R2-7 = parameters 1-6 (as many as necessary)
181 On return: R9 = result. */
183 /* Offset for peeks of registers */
184 # define REG_OFFSET (24)
185 # define REG_GENERAL(x) (8*(x)+REG_OFFSET)
186 # define REG_PC (0*8)
187 # define REG_SYSCALL (2*8)
191 #define SUPPORTED_PERSONALITIES 1
192 #define DEFAULT_PERSONALITY 0
195 #include <linux/a.out.h>
197 #define PERSONALITY0_WORDSIZE 4
198 #define PERSONALITY1_WORDSIZE 4
199 #undef SUPPORTED_PERSONALITIES
201 #define SUPPORTED_PERSONALITIES 3
202 #define PERSONALITY2_WORDSIZE 8
204 #define SUPPORTED_PERSONALITIES 2
206 #endif /* LINUXSPARC */
209 #undef SUPPORTED_PERSONALITIES
210 #define SUPPORTED_PERSONALITIES 2
211 #define PERSONALITY0_WORDSIZE 8
212 #define PERSONALITY1_WORDSIZE 4
216 #ifdef HAVE_MP_PROCFS
217 extern int mp_ioctl (int f, int c, void *a, int s);
218 #define IOCTL(f,c,a) mp_ioctl (f, c, a, sizeof *a)
219 #define IOCTL_STATUS(t) \
220 pread (t->pfd_stat, &t->status, sizeof t->status, 0)
221 #define IOCTL_WSTOP(t) \
222 (IOCTL (t->pfd, PCWSTOP, (char *)NULL) < 0 ? -1 : \
224 #define PR_WHY pr_lwp.pr_why
225 #define PR_WHAT pr_lwp.pr_what
226 #define PR_REG pr_lwp.pr_context.uc_mcontext.gregs
227 #define PR_FLAGS pr_lwp.pr_flags
228 #define PR_SYSCALL pr_lwp.pr_syscall
229 #define PR_INFO pr_lwp.pr_info
230 #define PIOCSTIP PCSTOP
231 #define PIOCSET PCSET
232 #define PIOCRESET PCRESET
233 #define PIOCSTRACE PCSTRACE
234 #define PIOCSFAULT PCSFAULT
235 #define PIOCWSTOP PCWSTOP
236 #define PIOCSTOP PCSTOP
237 #define PIOCSENTRY PCSENTRY
238 #define PIOCSEXIT PCSEXIT
239 #define PIOCRUN PCRUN
242 #define IOCTL_STATUS(t) ioctl (t->pfd, PIOCSTATUS, &t->status)
243 #define IOCTL_WSTOP(t) ioctl (t->pfd, PIOCWSTOP, &t->status)
244 #define PR_WHY pr_why
245 #define PR_WHAT pr_what
246 #define PR_REG pr_reg
247 #define PR_FLAGS pr_flags
248 #define PR_SYSCALL pr_syscall
249 #define PR_INFO pr_info
254 #define IOCTL_STATUS(t) ioctl (t->pfd, PIOCSTATUS, &t->status)
255 #define IOCTL_WSTOP(t) ioctl (t->pfd, PIOCWAIT, &t->status)
256 #define PIOCRUN PIOCCONT
257 #define PIOCWSTOP PIOCWAIT
260 #define PR_FLAGS state
261 /* from /usr/src/sys/miscfs/procfs/procfs_vnops.c,
262 status.state = 0 for running, 1 for stopped */
264 #define PR_SYSENTRY S_SCE
265 #define PR_SYSEXIT S_SCX
266 #define PR_SIGNALLED S_SIG
267 #define PR_FAULTED S_CORE
270 /* Trace Control Block */
272 short flags; /* See below for TCB_ values */
273 int pid; /* Process Id of this entry */
274 long scno; /* System call number */
275 int u_nargs; /* System call arguments */
276 long u_arg[MAX_ARGS]; /* System call arguments */
277 int u_error; /* Error code */
278 long u_rval; /* (first) return value */
279 #ifdef HAVE_LONG_LONG
280 long long u_lrval; /* long long return value */
282 FILE *outf; /* Output file for this process */
283 const char *auxstr; /* Auxiliary info from syscall (see RVAL_STR) */
284 struct timeval stime; /* System time usage as of last process wait */
285 struct timeval dtime; /* Delta for system time usage */
286 struct timeval etime; /* Syscall entry time */
287 /* Support for tracing forked processes */
288 struct tcb *parent; /* Parent of this process */
289 int nchildren; /* # of traced children */
290 int waitpid; /* pid(s) this process is waiting for */
291 int nzombies; /* # of formerly traced children now dead */
293 int nclone_threads; /* # of nchildren with CLONE_THREAD */
294 int nclone_detached; /* # of nchildren with CLONE_DETACHED */
295 int nclone_waiting; /* clone threads in wait4 (TCB_SUSPENDED) */
297 /* (1st arg of wait4()) */
298 long baddr; /* `Breakpoint' address */
299 long inst[2]; /* Instructions on above */
300 int pfd; /* proc file descriptor */
302 #ifdef HAVE_MP_PROCFS
307 prstatus_t status; /* procfs status structure */
311 struct procfs_status status;
318 #define TCB_STARTUP 00001 /* We have just begun ptracing this process */
319 #define TCB_INUSE 00002 /* This table entry is in use */
320 #define TCB_INSYSCALL 00004 /* A system call is in progress */
321 #define TCB_ATTACHED 00010 /* Process is not our own child */
322 #define TCB_EXITING 00020 /* As far as we know, this process is exiting */
323 #define TCB_SUSPENDED 00040 /* Process has done a wait(4), that can
324 not be allowed to complete just now */
325 #define TCB_BPTSET 00100 /* "Breakpoint" set after fork(2) */
326 #define TCB_SIGTRAPPED 00200 /* Process wanted to block SIGTRAP */
327 #define TCB_FOLLOWFORK 00400 /* Process should have forks followed */
328 #define TCB_REPRINT 01000 /* We should reprint this syscall on exit */
330 # if defined(ALPHA) || defined(SPARC) || defined(SPARC64) || defined(POWERPC) || defined(IA64) || defined(HPPA) || defined(SH) || defined(SH64) || defined(S390) || defined(S390X) || defined(ARM)
331 # define TCB_WAITEXECVE 02000 /* ignore SIGTRAP after exceve */
333 # define TCB_CLONE_DETACHED 04000 /* CLONE_DETACHED set in creating syscall */
334 # define TCB_CLONE_THREAD 010000 /* CLONE_THREAD set in creating syscall */
335 # define TCB_GROUP_EXITING 020000 /* TCB_EXITING was exit_group, not _exit */
336 # include <sys/syscall.h>
337 # ifndef __NR_exit_group
338 # /* Hack: Most headers around are too old to have __NR_exit_group. */
340 # define __NR_exit_group 405
342 # define __NR_exit_group 252
343 # elif defined X86_64
344 # define __NR_exit_group 231
346 # define __NR_exit_group 1236
347 # elif defined POWERPC
348 # define __NR_exit_group 234
349 # elif defined S390 || defined S390X
350 # define __NR_exit_group 248
351 # elif defined SPARC || defined SPARC64
352 # define __NR_exit_group 188
354 # define __NR_exit_group 247
355 # endif /* ALPHA et al */
356 # endif /* !__NR_exit_group */
359 /* qualifier flags */
360 #define QUAL_TRACE 0001 /* this system call should be traced */
361 #define QUAL_ABBREV 0002 /* abbreviate the structures of this syscall */
362 #define QUAL_VERBOSE 0004 /* decode the structures of this syscall */
363 #define QUAL_RAW 0010 /* print all args in hex for this syscall */
364 #define QUAL_SIGNAL 0020 /* report events with this signal */
365 #define QUAL_FAULT 0040 /* report events with this fault */
366 #define QUAL_READ 0100 /* dump data read on this file descriptor */
367 #define QUAL_WRITE 0200 /* dump data written to this file descriptor */
369 #define entering(tcp) (!((tcp)->flags & TCB_INSYSCALL))
370 #define exiting(tcp) ((tcp)->flags & TCB_INSYSCALL)
371 #define syserror(tcp) ((tcp)->u_error != 0)
372 #define verbose(tcp) (qual_flags[(tcp)->scno] & QUAL_VERBOSE)
373 #define abbrev(tcp) (qual_flags[(tcp)->scno] & QUAL_ABBREV)
380 /* Format of syscall return values */
381 #define RVAL_DECIMAL 000 /* decimal format */
382 #define RVAL_HEX 001 /* hex format */
383 #define RVAL_OCTAL 002 /* octal format */
384 #define RVAL_UDECIMAL 003 /* unsigned decimal format */
385 #define RVAL_LDECIMAL 004 /* long decimal format */
386 #define RVAL_LHEX 005 /* long hex format */
387 #define RVAL_LOCTAL 006 /* long octal format */
388 #define RVAL_LUDECIMAL 007 /* long unsigned decimal format */
389 #define RVAL_MASK 007 /* mask for these values */
391 #define RVAL_STR 010 /* Print `auxstr' field after return val */
392 #define RVAL_NONE 020 /* Print nothing */
395 #define offsetof(type, member) (((char *) &(((type *) NULL)->member)) - \
396 ((char *) (type *) NULL))
397 #endif /* !offsetof */
399 /* get offset of member within a user struct */
400 #define uoff(member) offsetof(struct user, member)
402 #define TRACE_FILE 001 /* Trace file-related syscalls. */
403 #define TRACE_IPC 002 /* Trace IPC-related syscalls. */
404 #define TRACE_NETWORK 004 /* Trace network-related syscalls. */
405 #define TRACE_PROCESS 010 /* Trace process-related syscalls. */
406 #define TRACE_SIGNAL 020 /* Trace signal-related syscalls. */
407 #define TRACE_DESC 040 /* Trace file descriptor-related syscalls. */
409 extern struct tcb **tcbtab;
410 extern int *qual_flags;
411 extern int debug, followfork, followvfork;
412 extern int dtime, cflag, xflag, qflag;
414 extern unsigned int nprocs, tcbtabsize;
415 extern int max_strlen;
416 extern struct tcb *tcp_last;
424 enum bitness_t { BITNESS_CURRENT = 0, BITNESS_32 };
426 extern int set_personality P((int personality));
427 extern char *xlookup P((const struct xlat *, int));
428 extern struct tcb *alloctcb P((int));
429 extern struct tcb *pid2tcb P((int));
430 extern void droptcb P((struct tcb *));
431 extern int expand_tcbtab P((void));
433 extern void set_sortby P((char *));
434 extern void set_overhead P((int));
435 extern void qualify P((char *));
436 extern void newoutf P((struct tcb *));
437 extern int get_scno P((struct tcb *));
438 extern long known_scno P((struct tcb *));
439 extern int trace_syscall P((struct tcb *));
440 extern void printxval P((const struct xlat *, int, const char *));
441 extern int printargs P((struct tcb *));
442 extern int addflags P((const struct xlat *, int));
443 extern int printflags P((const struct xlat *, int, const char *));
444 extern int umoven P((struct tcb *, long, int, char *));
445 extern int umovestr P((struct tcb *, long, int, char *));
446 extern int upeek P((int, long, long *));
447 extern void dumpiov P((struct tcb *, int, long));
448 extern void dumpstr P((struct tcb *, long, int));
449 extern void printstr P((struct tcb *, long, int));
450 extern void printnum P((struct tcb *, long, char *));
451 extern void printnum_int P((struct tcb *, long, char *));
452 extern void printpath P((struct tcb *, long));
453 extern void printpathn P((struct tcb *, long, int));
454 extern void printtv_bitness P((struct tcb *, long, enum bitness_t));
455 extern void sprinttv P((struct tcb *, long, enum bitness_t, char *));
456 #ifdef HAVE_SIGINFO_T
457 extern void printsiginfo P((siginfo_t *, int));
459 extern void printsock P((struct tcb *, long, int));
460 extern void print_sock_optmgmt P((struct tcb *, long, int));
461 extern void printrusage P((struct tcb *, long));
462 extern void printuid P((const char *, unsigned long));
463 extern int clearbpt P((struct tcb *));
464 extern int setbpt P((struct tcb *));
465 extern int sigishandled P((struct tcb *, int));
466 extern void printcall P((struct tcb *));
467 extern const char *signame P((int));
468 extern void print_sigset P((struct tcb *, long, int));
469 extern void printsignal P((int));
470 extern void printleader P((struct tcb *));
471 extern void printtrailer P((struct tcb *));
472 extern void tabto P((int));
473 extern void call_summary P((FILE *));
474 extern void tprint_iov P((struct tcb *, unsigned long, unsigned long));
477 extern int internal_clone P((struct tcb *));
479 extern int internal_fork P((struct tcb *));
480 extern int internal_exec P((struct tcb *));
481 extern int internal_wait P((struct tcb *, int));
482 extern int internal_exit P((struct tcb *));
484 extern const struct ioctlent *ioctl_lookup P((long));
485 extern const struct ioctlent *ioctl_next_match P((const struct ioctlent *));
486 extern int ioctl_decode P((struct tcb *, long, long));
487 extern int term_ioctl P((struct tcb *, long, long));
488 extern int sock_ioctl P((struct tcb *, long, long));
489 extern int proc_ioctl P((struct tcb *, int, int));
490 extern int stream_ioctl P((struct tcb *, int, int));
492 extern int rtc_ioctl P((struct tcb *, long, long));
495 extern void tv_tv P((struct timeval *, int, int));
496 extern int tv_nz P((struct timeval *));
497 extern int tv_cmp P((struct timeval *, struct timeval *));
498 extern double tv_float P((struct timeval *));
499 extern void tv_add P((struct timeval *, struct timeval *, struct timeval *));
500 extern void tv_sub P((struct timeval *, struct timeval *, struct timeval *));
501 extern void tv_mul P((struct timeval *, struct timeval *, int));
502 extern void tv_div P((struct timeval *, struct timeval *, int));
505 extern int fixvfork P((struct tcb *));
507 #if !(defined(LINUX) && !defined(SPARC) && !defined(SPARC64) && !defined(IA64))
508 extern long getrval2 P((struct tcb *));
511 extern int proc_open P((struct tcb *tcp, int attaching));
514 #define umove(pid, addr, objp) \
515 umoven((pid), (addr), sizeof *(objp), (char *) (objp))
517 #define printtv(tcp, addr) \
518 printtv_bitness((tcp), (addr), BITNESS_CURRENT)
522 extern void tprintf(const char *fmt, ...)
523 __attribute__ ((format (printf, 1, 2)));
525 extern void tprintf(const char *fmt, ...);
528 extern void tprintf();
531 #ifndef HAVE_STRERROR
532 const char *strerror P((int));
534 #ifndef HAVE_STRSIGNAL
535 const char *strsignal P((int));
538 extern int current_personality;
539 extern const int personality_wordsize[];
545 const char *sys_name;
546 long native_scno; /* Match against SYS_* constants. */
549 extern const struct sysent *sysent;
550 extern int nsyscalls;
552 extern const char *const *errnoent;
561 extern const struct ioctlent *ioctlent;
562 extern int nioctlents;
564 extern const char *const *signalent;
567 extern const struct ioctlent ioctlent0[];
568 extern const int nioctlents0;
569 extern const char *const signalent0[];
570 extern const int nsignals0;
572 #if SUPPORTED_PERSONALITIES >= 2
573 extern const struct ioctlent ioctlent1[];
574 extern const int nioctlents1;
575 extern const char *const signalent1[];
576 extern const int nsignals1;
577 #endif /* SUPPORTED_PERSONALITIES >= 2 */
579 #if SUPPORTED_PERSONALITIES >= 3
580 extern const struct ioctlent ioctlent2[];
581 extern const int nioctlents2;
582 extern const char *const signalent2[];
583 extern const int nsignals2;
584 #endif /* SUPPORTED_PERSONALITIES >= 3 */
586 #if defined(FREEBSD) || (defined(LINUX) \
587 && defined(POWERPC) && !defined(__powerpc64__)) \
588 || (defined (LINUX) && defined (MIPS) && !defined(__mips64))
589 /* ARRGH! off_t args are aligned on 64 bit boundaries! */
590 #define ALIGN64(tcp,arg) \
593 memmove (&tcp->u_arg[arg], &tcp->u_arg[arg + 1], \
594 (tcp->u_nargs - arg - 1) * sizeof tcp->u_arg[0]); \
597 #define ALIGN64(tcp,arg) do { } while (0)
602 /* _l refers to the lower numbered u_arg,
603 * _h refers to the higher numbered u_arg
606 #if HAVE_LITTLE_ENDIAN_LONG_LONG
607 #define LONG_LONG(_l,_h) \
608 ((long long)((unsigned long long)(unsigned)(_l) | ((unsigned long long)(_h)<<32)))
610 #define LONG_LONG(_l,_h) \
611 ((long long)((unsigned long long)(unsigned)(_h) | ((unsigned long long)(_l)<<32)))
619 extern int not_failing_only;