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.
36 #ifdef _LARGEFILE64_SOURCE
37 /* This is the macro everything checks before using foo64 names. */
38 # ifndef _LFS64_LARGEFILE
39 # 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 */
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)
175 #define SUPPORTED_PERSONALITIES 1
176 #define DEFAULT_PERSONALITY 0
179 #include <linux/a.out.h>
181 #undef SUPPORTED_PERSONALITIES
182 #define SUPPORTED_PERSONALITIES 2
183 #endif /* LINUXSPARC */
186 #undef SUPPORTED_PERSONALITIES
187 #define SUPPORTED_PERSONALITIES 2
191 #ifdef HAVE_MP_PROCFS
192 extern int mp_ioctl (int f, int c, void *a, int s);
193 #define IOCTL(f,c,a) mp_ioctl (f, c, a, sizeof *a)
194 #define IOCTL_STATUS(t) \
195 pread (t->pfd_stat, &t->status, sizeof t->status, 0)
196 #define IOCTL_WSTOP(t) \
197 (IOCTL (t->pfd, PCWSTOP, (char *)NULL) < 0 ? -1 : \
199 #define PR_WHY pr_lwp.pr_why
200 #define PR_WHAT pr_lwp.pr_what
201 #define PR_REG pr_lwp.pr_context.uc_mcontext.gregs
202 #define PR_FLAGS pr_lwp.pr_flags
203 #define PR_SYSCALL pr_lwp.pr_syscall
204 #define PR_INFO pr_lwp.pr_info
205 #define PIOCSTIP PCSTOP
206 #define PIOCSET PCSET
207 #define PIOCRESET PCRESET
208 #define PIOCSTRACE PCSTRACE
209 #define PIOCSFAULT PCSFAULT
210 #define PIOCWSTOP PCWSTOP
211 #define PIOCSTOP PCSTOP
212 #define PIOCSENTRY PCSENTRY
213 #define PIOCSEXIT PCSEXIT
214 #define PIOCRUN PCRUN
217 #define IOCTL_STATUS(t) ioctl (t->pfd, PIOCSTATUS, &t->status)
218 #define IOCTL_WSTOP(t) ioctl (t->pfd, PIOCWSTOP, &t->status)
219 #define PR_WHY pr_why
220 #define PR_WHAT pr_what
221 #define PR_REG pr_reg
222 #define PR_FLAGS pr_flags
223 #define PR_SYSCALL pr_syscall
224 #define PR_INFO pr_info
229 #define IOCTL_STATUS(t) ioctl (t->pfd, PIOCSTATUS, &t->status)
230 #define IOCTL_WSTOP(t) ioctl (t->pfd, PIOCWAIT, &t->status)
231 #define PIOCRUN PIOCCONT
232 #define PIOCWSTOP PIOCWAIT
235 #define PR_FLAGS state
236 /* from /usr/src/sys/miscfs/procfs/procfs_vnops.c,
237 status.state = 0 for running, 1 for stopped */
239 #define PR_SYSENTRY S_SCE
240 #define PR_SYSEXIT S_SCX
241 #define PR_SIGNALLED S_SIG
242 #define PR_FAULTED S_CORE
245 /* Trace Control Block */
247 short flags; /* See below for TCB_ values */
248 int pid; /* Process Id of this entry */
249 long scno; /* System call number */
250 int u_nargs; /* System call arguments */
251 long u_arg[MAX_ARGS]; /* System call arguments */
252 int u_error; /* Error code */
253 long u_rval; /* (first) return value */
254 #ifdef HAVE_LONG_LONG
255 long long u_lrval; /* long long return value */
257 FILE *outf; /* Output file for this process */
258 const char *auxstr; /* Auxiliary info from syscall (see RVAL_STR) */
259 struct timeval stime; /* System time usage as of last process wait */
260 struct timeval dtime; /* Delta for system time usage */
261 struct timeval etime; /* Syscall entry time */
262 /* Support for tracing forked processes */
263 struct tcb *parent; /* Parent of this process */
264 int nchildren; /* # of traced children */
265 int waitpid; /* pid(s) this process is waiting for */
266 int nzombies; /* # of formerly traced children now dead */
268 int nclone_threads; /* # of nchildren with CLONE_THREAD */
269 int nclone_detached; /* # of nchildren with CLONE_DETACHED */
270 int nclone_waiting; /* clone threads in wait4 (TCB_SUSPENDED) */
272 /* (1st arg of wait4()) */
273 long baddr; /* `Breakpoint' address */
274 long inst[2]; /* Instructions on above */
275 int pfd; /* proc file descriptor */
277 #ifdef HAVE_MP_PROCFS
282 prstatus_t status; /* procfs status structure */
286 struct procfs_status status;
293 #define TCB_STARTUP 00001 /* We have just begun ptracing this process */
294 #define TCB_INUSE 00002 /* This table entry is in use */
295 #define TCB_INSYSCALL 00004 /* A system call is in progress */
296 #define TCB_ATTACHED 00010 /* Process is not our own child */
297 #define TCB_EXITING 00020 /* As far as we know, this process is exiting */
298 #define TCB_SUSPENDED 00040 /* Process has done a wait(4), that can
299 not be allowed to complete just now */
300 #define TCB_BPTSET 00100 /* "Breakpoint" set after fork(2) */
301 #define TCB_SIGTRAPPED 00200 /* Process wanted to block SIGTRAP */
302 #define TCB_FOLLOWFORK 00400 /* Process should have forks followed */
303 #define TCB_REPRINT 01000 /* We should reprint this syscall on exit */
305 # if defined(ALPHA) || defined(SPARC) || defined(POWERPC) || defined(IA64) || defined(HPPA) || defined(SH) || defined(S390) || defined(S390X)
306 # define TCB_WAITEXECVE 02000 /* ignore SIGTRAP after exceve */
308 # define TCB_CLONE_DETACHED 04000 /* CLONE_DETACHED set in creating syscall */
309 # define TCB_CLONE_THREAD 010000 /* CLONE_THREAD set in creating syscall */
310 # define TCB_GROUP_EXITING 020000 /* TCB_EXITING was exit_group, not _exit */
311 # include <sys/syscall.h>
312 # ifndef __NR_exit_group
313 # /* Hack: Most headers around are too old to have __NR_exit_group. */
315 # define __NR_exit_group 405
317 # define __NR_exit_group 252
319 # define __NR_exit_group 1236
320 # elif defined POWERPC
321 # define __NR_exit_group 234
322 # elif defined S390 || defined S390X
323 # define __NR_exit_group 248
325 # define __NR_exit_group 188
326 # endif /* ALPHA et al */
327 # endif /* !__NR_exit_group */
330 /* qualifier flags */
331 #define QUAL_TRACE 0001 /* this system call should be traced */
332 #define QUAL_ABBREV 0002 /* abbreviate the structures of this syscall */
333 #define QUAL_VERBOSE 0004 /* decode the structures of this syscall */
334 #define QUAL_RAW 0010 /* print all args in hex for this syscall */
335 #define QUAL_SIGNAL 0020 /* report events with this signal */
336 #define QUAL_FAULT 0040 /* report events with this fault */
337 #define QUAL_READ 0100 /* dump data read on this file descriptor */
338 #define QUAL_WRITE 0200 /* dump data written to this file descriptor */
340 #define entering(tcp) (!((tcp)->flags & TCB_INSYSCALL))
341 #define exiting(tcp) ((tcp)->flags & TCB_INSYSCALL)
342 #define syserror(tcp) ((tcp)->u_error != 0)
343 #define verbose(tcp) (qual_flags[(tcp)->scno] & QUAL_VERBOSE)
344 #define abbrev(tcp) (qual_flags[(tcp)->scno] & QUAL_ABBREV)
351 /* Format of syscall return values */
352 #define RVAL_DECIMAL 000 /* decimal format */
353 #define RVAL_HEX 001 /* hex format */
354 #define RVAL_OCTAL 002 /* octal format */
355 #define RVAL_UDECIMAL 003 /* unsigned decimal format */
356 #define RVAL_LDECIMAL 004 /* long decimal format */
357 #define RVAL_LHEX 005 /* long hex format */
358 #define RVAL_LOCTAL 006 /* long octal format */
359 #define RVAL_LUDECIMAL 007 /* long unsigned decimal format */
360 #define RVAL_MASK 007 /* mask for these values */
362 #define RVAL_STR 010 /* Print `auxstr' field after return val */
363 #define RVAL_NONE 020 /* Print nothing */
366 #define offsetof(type, member) (((char *) &(((type *) NULL)->member)) - \
367 ((char *) (type *) NULL))
368 #endif /* !offsetof */
370 /* get offset of member within a user struct */
371 #define uoff(member) offsetof(struct user, member)
373 #define TRACE_FILE 001 /* Trace file-related syscalls. */
374 #define TRACE_IPC 002 /* Trace IPC-related syscalls. */
375 #define TRACE_NETWORK 004 /* Trace network-related syscalls. */
376 #define TRACE_PROCESS 010 /* Trace process-related syscalls. */
377 #define TRACE_SIGNAL 020 /* Trace signal-related syscalls. */
379 extern struct tcb **tcbtab;
380 extern int qual_flags[];
381 extern int debug, followfork, followvfork;
382 extern int rflag, tflag, dtime, cflag, xflag, qflag;
384 extern char *outfname;
385 extern unsigned int nprocs, tcbtabsize;
386 extern int max_strlen;
387 extern struct tcb *tcp_last;
395 extern int set_personality P((int personality));
396 extern char *xlookup P((struct xlat *, int));
397 extern struct tcb *alloctcb P((int));
398 extern struct tcb *pid2tcb P((int));
399 extern void droptcb P((struct tcb *));
401 extern void set_sortby P((char *));
402 extern void set_overhead P((int));
403 extern void qualify P((char *));
404 extern void newoutf P((struct tcb *));
405 extern int get_scno P((struct tcb *));
406 extern int trace_syscall P((struct tcb *));
407 extern void printxval P((struct xlat *, int, char *));
408 extern int printargs P((struct tcb *));
409 extern int addflags P((struct xlat *, int));
410 extern int printflags P((struct xlat *, int));
411 extern int umoven P((struct tcb *, long, int, char *));
412 extern int umovestr P((struct tcb *, long, int, char *));
413 extern int upeek P((int, long, long *));
414 extern void dumpiov P((struct tcb *, int, long));
415 extern void dumpstr P((struct tcb *, long, int));
416 extern void string_quote P((char *str));
417 extern void printstr P((struct tcb *, long, int));
418 extern void printnum P((struct tcb *, long, char *));
419 extern void printpath P((struct tcb *, long));
420 extern void printpathn P((struct tcb *, long, int));
421 extern void printtv P((struct tcb *, long));
422 #ifdef HAVE_SIGINFO_T
423 extern void printsiginfo P((siginfo_t *, int));
425 extern void printsock P((struct tcb *, long, int));
426 extern void print_sock_optmgmt P((struct tcb *, long, int));
427 extern void printrusage P((struct tcb *, long));
428 extern int clearbpt P((struct tcb *));
429 extern int setbpt P((struct tcb *));
430 extern int sigishandled P((struct tcb *, int));
431 extern void printcall P((struct tcb *));
432 extern char *signame P((int));
433 extern void printsignal P((int));
434 extern void printleader P((struct tcb *));
435 extern void printtrailer P((struct tcb *));
436 extern void tabto P((int));
437 extern void call_summary P((FILE *));
438 extern void fake_execve P((struct tcb *, char *, char *[], char *[]));
439 extern void printtv32 P((struct tcb*, long));
440 extern void tprint_iov P((struct tcb *, int, long));
443 extern int internal_clone P((struct tcb *));
445 extern int internal_fork P((struct tcb *));
446 extern int internal_exec P((struct tcb *));
447 extern int internal_wait P((struct tcb *));
448 extern int internal_exit P((struct tcb *));
450 extern char *ioctl_lookup P((long));
451 extern int ioctl_decode P((struct tcb *, long, long));
452 extern int term_ioctl P((struct tcb *, long, long));
453 extern int sock_ioctl P((struct tcb *, long, long));
454 extern int proc_ioctl P((struct tcb *, int, int));
455 extern int stream_ioctl P((struct tcb *, int, int));
457 extern void tv_tv P((struct timeval *, int, int));
458 extern int tv_nz P((struct timeval *));
459 extern int tv_cmp P((struct timeval *, struct timeval *));
460 extern double tv_float P((struct timeval *));
461 extern void tv_add P((struct timeval *, struct timeval *, struct timeval *));
462 extern void tv_sub P((struct timeval *, struct timeval *, struct timeval *));
463 extern void tv_mul P((struct timeval *, struct timeval *, int));
464 extern void tv_div P((struct timeval *, struct timeval *, int));
467 extern int fixvfork P((struct tcb *));
469 #if !(defined(LINUX) && !defined(SPARC))
470 extern long getrval2 P((struct tcb *));
473 extern int proc_open P((struct tcb *tcp, int attaching));
476 #define umove(pid, addr, objp) \
477 umoven((pid), (addr), sizeof *(objp), (char *) (objp))
481 extern void tprintf(const char *fmt, ...)
482 __attribute__ ((format (printf, 1, 2)));
484 extern void tprintf(const char *fmt, ...);
487 extern void tprintf();
490 #ifndef HAVE_STRERROR
491 const char *strerror P((int));
493 #ifndef HAVE_STRSIGNAL
494 const char *strsignal P((int));
497 extern int current_personality;
506 extern struct sysent *sysent;
507 extern int nsyscalls;
509 extern char **errnoent;
518 extern struct ioctlent *ioctlent;
519 extern int nioctlent;
521 extern char **signalent;
524 extern struct ioctlent *ioctlent;
525 extern int nioctlents;
526 extern char **signalent;
529 extern struct ioctlent ioctlent0[];
530 extern int nioctlents0;
531 extern char *signalent0[];
532 extern int nsignals0;
534 #if SUPPORTED_PERSONALITIES >= 2
535 extern struct ioctlent ioctlent1[];
536 extern int nioctlents1;
537 extern char *signalent1[];
538 extern int nsignals1;
539 #endif /* SUPPORTED_PERSONALITIES >= 2 */
541 #if SUPPORTED_PERSONALITIES >= 3
542 extern struct ioctlent ioctlent2[];
543 extern int nioctlents2;
544 extern char *signalent2[];
545 extern int nsignals2;
546 #endif /* SUPPORTED_PERSONALITIES >= 3 */
548 #if defined(FREEBSD) || (defined(LINUX) \
549 && defined(POWERPC) && !defined(__powerpc64__))
550 /* ARRGH! off_t args are aligned on 64 bit boundaries! */
551 #define ALIGN64(tcp,arg) \
554 memmove (&tcp->u_arg[arg], &tcp->u_arg[arg + 1], \
555 (tcp->u_nargs - arg - 1) * sizeof tcp->u_arg[0]); \
558 #define ALIGN64(tcp,arg) do { } while (0)
563 /* _l refers to the lower numbered u_arg,
564 * _h refers to the higher numbered u_arg
567 #if HAVE_LITTLE_ENDIAN_LONG_LONG
568 #define LONG_LONG(_l,_h) \
569 ((long long)((unsigned long long)(unsigned)(_l) | ((unsigned long long)(_h)<<32)))
571 #define LONG_LONG(_l,_h) \
572 ((long long)((unsigned long long)(unsigned)(_h) | ((unsigned long long)(_l)<<32)))
580 extern int not_failing_only;