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 */
267 int nclone_threads; /* # of nchildren with CLONE_THREAD */
268 int nclone_detached; /* # of nchildren with CLONE_DETACHED */
269 int nclone_waiting; /* clone threads in wait4 (TCB_SUSPENDED) */
271 /* (1st arg of wait4()) */
272 long baddr; /* `Breakpoint' address */
273 long inst[2]; /* Instructions on above */
274 int pfd; /* proc file descriptor */
276 #ifdef HAVE_MP_PROCFS
281 prstatus_t status; /* procfs status structure */
285 struct procfs_status status;
292 #define TCB_STARTUP 00001 /* We have just begun ptracing this process */
293 #define TCB_INUSE 00002 /* This table entry is in use */
294 #define TCB_INSYSCALL 00004 /* A system call is in progress */
295 #define TCB_ATTACHED 00010 /* Process is not our own child */
296 #define TCB_EXITING 00020 /* As far as we know, this process is exiting */
297 #define TCB_SUSPENDED 00040 /* Process has done a wait(4), that can
298 not be allowed to complete just now */
299 #define TCB_BPTSET 00100 /* "Breakpoint" set after fork(2) */
300 #define TCB_SIGTRAPPED 00200 /* Process wanted to block SIGTRAP */
301 #define TCB_FOLLOWFORK 00400 /* Process should have forks followed */
302 #define TCB_REPRINT 01000 /* We should reprint this syscall on exit */
304 # if defined(ALPHA) || defined(SPARC) || defined(POWERPC) || defined(IA64) || defined(HPPA) || defined(SH)
305 # define TCB_WAITEXECVE 02000 /* ignore SIGTRAP after exceve */
307 # define TCB_CLONE_DETACHED 04000 /* CLONE_DETACHED set in creating syscall */
308 # define TCB_CLONE_THREAD 010000 /* CLONE_THREAD set in creating syscall */
309 # define TCB_GROUP_EXITING 020000 /* TCB_EXITING was exit_group, not _exit */
310 # include <sys/syscall.h>
311 # ifndef __NR_exit_group
312 # /* Hack: Most headers around are too old to have __NR_exit_group. */
314 # define __NR_exit_group 405
316 # define __NR_exit_group 252
318 # define __NR_exit_group 1236
319 # elif defined POWERPC
320 # define __NR_exit_group 234
321 # elif defined S390 || defined S390X
322 # define __NR_exit_group 248
324 # define __NR_exit_group 188
325 # endif /* ALPHA et al */
326 # endif /* !__NR_exit_group */
329 /* qualifier flags */
330 #define QUAL_TRACE 0001 /* this system call should be traced */
331 #define QUAL_ABBREV 0002 /* abbreviate the structures of this syscall */
332 #define QUAL_VERBOSE 0004 /* decode the structures of this syscall */
333 #define QUAL_RAW 0010 /* print all args in hex for this syscall */
334 #define QUAL_SIGNAL 0020 /* report events with this signal */
335 #define QUAL_FAULT 0040 /* report events with this fault */
336 #define QUAL_READ 0100 /* dump data read on this file descriptor */
337 #define QUAL_WRITE 0200 /* dump data written to this file descriptor */
339 #define entering(tcp) (!((tcp)->flags & TCB_INSYSCALL))
340 #define exiting(tcp) ((tcp)->flags & TCB_INSYSCALL)
341 #define syserror(tcp) ((tcp)->u_error != 0)
342 #define verbose(tcp) (qual_flags[(tcp)->scno] & QUAL_VERBOSE)
343 #define abbrev(tcp) (qual_flags[(tcp)->scno] & QUAL_ABBREV)
350 /* Format of syscall return values */
351 #define RVAL_DECIMAL 000 /* decimal format */
352 #define RVAL_HEX 001 /* hex format */
353 #define RVAL_OCTAL 002 /* octal format */
354 #define RVAL_UDECIMAL 003 /* unsigned decimal format */
355 #define RVAL_LDECIMAL 004 /* long decimal format */
356 #define RVAL_LHEX 005 /* long hex format */
357 #define RVAL_LOCTAL 006 /* long octal format */
358 #define RVAL_LUDECIMAL 007 /* long unsigned decimal format */
359 #define RVAL_MASK 007 /* mask for these values */
361 #define RVAL_STR 010 /* Print `auxstr' field after return val */
362 #define RVAL_NONE 020 /* Print nothing */
365 #define offsetof(type, member) (((char *) &(((type *) NULL)->member)) - \
366 ((char *) (type *) NULL))
367 #endif /* !offsetof */
369 /* get offset of member within a user struct */
370 #define uoff(member) offsetof(struct user, member)
372 #define TRACE_FILE 001 /* Trace file-related syscalls. */
373 #define TRACE_IPC 002 /* Trace IPC-related syscalls. */
374 #define TRACE_NETWORK 004 /* Trace network-related syscalls. */
375 #define TRACE_PROCESS 010 /* Trace process-related syscalls. */
376 #define TRACE_SIGNAL 020 /* Trace signal-related syscalls. */
378 extern struct tcb **tcbtab;
379 extern int qual_flags[];
380 extern int debug, followfork, followvfork;
381 extern int rflag, tflag, dtime, cflag, xflag, qflag;
383 extern char *outfname;
384 extern unsigned int nprocs, tcbtabsize;
385 extern int max_strlen;
386 extern struct tcb *tcp_last;
394 extern int set_personality P((int personality));
395 extern char *xlookup P((struct xlat *, int));
396 extern struct tcb *alloctcb P((int));
397 extern struct tcb *pid2tcb P((int));
398 extern void droptcb P((struct tcb *));
400 extern void set_sortby P((char *));
401 extern void set_overhead P((int));
402 extern void qualify P((char *));
403 extern void newoutf P((struct tcb *));
404 extern int get_scno P((struct tcb *));
405 extern int trace_syscall P((struct tcb *));
406 extern void printxval P((struct xlat *, int, char *));
407 extern int printargs P((struct tcb *));
408 extern int addflags P((struct xlat *, int));
409 extern int printflags P((struct xlat *, int));
410 extern int umoven P((struct tcb *, long, int, char *));
411 extern int umovestr P((struct tcb *, long, int, char *));
412 extern int upeek P((int, long, long *));
413 extern void dumpiov P((struct tcb *, int, long));
414 extern void dumpstr P((struct tcb *, long, int));
415 extern void string_quote P((char *str));
416 extern void printstr P((struct tcb *, long, int));
417 extern void printnum P((struct tcb *, long, char *));
418 extern void printpath P((struct tcb *, long));
419 extern void printpathn P((struct tcb *, long, int));
420 extern void printtv P((struct tcb *, long));
421 #ifdef HAVE_SIGINFO_T
422 extern void printsiginfo P((siginfo_t *, int));
424 extern void printsock P((struct tcb *, long, int));
425 extern void print_sock_optmgmt P((struct tcb *, long, int));
426 extern void printrusage P((struct tcb *, long));
427 extern int clearbpt P((struct tcb *));
428 extern int setbpt P((struct tcb *));
429 extern int sigishandled P((struct tcb *, int));
430 extern void printcall P((struct tcb *));
431 extern char *signame P((int));
432 extern void printsignal P((int));
433 extern void printleader P((struct tcb *));
434 extern void printtrailer P((struct tcb *));
435 extern void tabto P((int));
436 extern void call_summary P((FILE *));
437 extern void fake_execve P((struct tcb *, char *, char *[], char *[]));
438 extern void printtv32 P((struct tcb*, long));
439 extern void tprint_iov P((struct tcb *, int, long));
442 extern int internal_clone P((struct tcb *));
444 extern int internal_fork P((struct tcb *));
445 extern int internal_exec P((struct tcb *));
446 extern int internal_wait P((struct tcb *));
447 extern int internal_exit P((struct tcb *));
449 extern char *ioctl_lookup P((long));
450 extern int ioctl_decode P((struct tcb *, long, long));
451 extern int term_ioctl P((struct tcb *, long, long));
452 extern int sock_ioctl P((struct tcb *, long, long));
453 extern int proc_ioctl P((struct tcb *, int, int));
454 extern int stream_ioctl P((struct tcb *, int, int));
456 extern void tv_tv P((struct timeval *, int, int));
457 extern int tv_nz P((struct timeval *));
458 extern int tv_cmp P((struct timeval *, struct timeval *));
459 extern double tv_float P((struct timeval *));
460 extern void tv_add P((struct timeval *, struct timeval *, struct timeval *));
461 extern void tv_sub P((struct timeval *, struct timeval *, struct timeval *));
462 extern void tv_mul P((struct timeval *, struct timeval *, int));
463 extern void tv_div P((struct timeval *, struct timeval *, int));
466 extern int fixvfork P((struct tcb *));
468 #if !(defined(LINUX) && !defined(SPARC))
469 extern long getrval2 P((struct tcb *));
472 extern int proc_open P((struct tcb *tcp, int attaching));
475 #define umove(pid, addr, objp) \
476 umoven((pid), (addr), sizeof *(objp), (char *) (objp))
480 extern void tprintf(const char *fmt, ...)
481 __attribute__ ((format (printf, 1, 2)));
483 extern void tprintf(const char *fmt, ...);
486 extern void tprintf();
489 #ifndef HAVE_STRERROR
490 const char *strerror P((int));
492 #ifndef HAVE_STRSIGNAL
493 const char *strsignal P((int));
496 extern int current_personality;
505 extern struct sysent *sysent;
506 extern int nsyscalls;
508 extern char **errnoent;
517 extern struct ioctlent *ioctlent;
518 extern int nioctlent;
520 extern char **signalent;
523 extern struct ioctlent *ioctlent;
524 extern int nioctlents;
525 extern char **signalent;
528 extern struct ioctlent ioctlent0[];
529 extern int nioctlents0;
530 extern char *signalent0[];
531 extern int nsignals0;
533 #if SUPPORTED_PERSONALITIES >= 2
534 extern struct ioctlent ioctlent1[];
535 extern int nioctlents1;
536 extern char *signalent1[];
537 extern int nsignals1;
538 #endif /* SUPPORTED_PERSONALITIES >= 2 */
540 #if SUPPORTED_PERSONALITIES >= 3
541 extern struct ioctlent ioctlent2[];
542 extern int nioctlents2;
543 extern char *signalent2[];
544 extern int nsignals2;
545 #endif /* SUPPORTED_PERSONALITIES >= 3 */
547 #if defined(FREEBSD) || (defined(LINUX) \
548 && defined(POWERPC) && !defined(__powerpc64__))
549 /* ARRGH! off_t args are aligned on 64 bit boundaries! */
550 #define ALIGN64(tcp,arg) \
553 memmove (&tcp->u_arg[arg], &tcp->u_arg[arg + 1], \
554 (tcp->u_nargs - arg - 1) * sizeof tcp->u_arg[0]); \
557 #define ALIGN64(tcp,arg) do { } while (0)
562 /* _l refers to the lower numbered u_arg,
563 * _h refers to the higher numbered u_arg
566 #if HAVE_LITTLE_ENDIAN_LONG_LONG
567 #define LONG_LONG(_l,_h) \
568 ((long long)((unsigned long long)(unsigned)(_l) | ((unsigned long long)(_h)<<32)))
570 #define LONG_LONG(_l,_h) \
571 ((long long)((unsigned long long)(unsigned)(_h) | ((unsigned long long)(_l)<<32)))
579 extern int not_failing_only;