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)
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 #undef SUPPORTED_PERSONALITIES
198 #define SUPPORTED_PERSONALITIES 2
199 #endif /* LINUXSPARC */
202 #undef SUPPORTED_PERSONALITIES
203 #define SUPPORTED_PERSONALITIES 2
207 #ifdef HAVE_MP_PROCFS
208 extern int mp_ioctl (int f, int c, void *a, int s);
209 #define IOCTL(f,c,a) mp_ioctl (f, c, a, sizeof *a)
210 #define IOCTL_STATUS(t) \
211 pread (t->pfd_stat, &t->status, sizeof t->status, 0)
212 #define IOCTL_WSTOP(t) \
213 (IOCTL (t->pfd, PCWSTOP, (char *)NULL) < 0 ? -1 : \
215 #define PR_WHY pr_lwp.pr_why
216 #define PR_WHAT pr_lwp.pr_what
217 #define PR_REG pr_lwp.pr_context.uc_mcontext.gregs
218 #define PR_FLAGS pr_lwp.pr_flags
219 #define PR_SYSCALL pr_lwp.pr_syscall
220 #define PR_INFO pr_lwp.pr_info
221 #define PIOCSTIP PCSTOP
222 #define PIOCSET PCSET
223 #define PIOCRESET PCRESET
224 #define PIOCSTRACE PCSTRACE
225 #define PIOCSFAULT PCSFAULT
226 #define PIOCWSTOP PCWSTOP
227 #define PIOCSTOP PCSTOP
228 #define PIOCSENTRY PCSENTRY
229 #define PIOCSEXIT PCSEXIT
230 #define PIOCRUN PCRUN
233 #define IOCTL_STATUS(t) ioctl (t->pfd, PIOCSTATUS, &t->status)
234 #define IOCTL_WSTOP(t) ioctl (t->pfd, PIOCWSTOP, &t->status)
235 #define PR_WHY pr_why
236 #define PR_WHAT pr_what
237 #define PR_REG pr_reg
238 #define PR_FLAGS pr_flags
239 #define PR_SYSCALL pr_syscall
240 #define PR_INFO pr_info
245 #define IOCTL_STATUS(t) ioctl (t->pfd, PIOCSTATUS, &t->status)
246 #define IOCTL_WSTOP(t) ioctl (t->pfd, PIOCWAIT, &t->status)
247 #define PIOCRUN PIOCCONT
248 #define PIOCWSTOP PIOCWAIT
251 #define PR_FLAGS state
252 /* from /usr/src/sys/miscfs/procfs/procfs_vnops.c,
253 status.state = 0 for running, 1 for stopped */
255 #define PR_SYSENTRY S_SCE
256 #define PR_SYSEXIT S_SCX
257 #define PR_SIGNALLED S_SIG
258 #define PR_FAULTED S_CORE
261 /* Trace Control Block */
263 short flags; /* See below for TCB_ values */
264 int pid; /* Process Id of this entry */
265 long scno; /* System call number */
266 int u_nargs; /* System call arguments */
267 long u_arg[MAX_ARGS]; /* System call arguments */
268 int u_error; /* Error code */
269 long u_rval; /* (first) return value */
270 #ifdef HAVE_LONG_LONG
271 long long u_lrval; /* long long return value */
273 FILE *outf; /* Output file for this process */
274 const char *auxstr; /* Auxiliary info from syscall (see RVAL_STR) */
275 struct timeval stime; /* System time usage as of last process wait */
276 struct timeval dtime; /* Delta for system time usage */
277 struct timeval etime; /* Syscall entry time */
278 /* Support for tracing forked processes */
279 struct tcb *parent; /* Parent of this process */
280 int nchildren; /* # of traced children */
281 int waitpid; /* pid(s) this process is waiting for */
282 int nzombies; /* # of formerly traced children now dead */
284 int nclone_threads; /* # of nchildren with CLONE_THREAD */
285 int nclone_detached; /* # of nchildren with CLONE_DETACHED */
286 int nclone_waiting; /* clone threads in wait4 (TCB_SUSPENDED) */
288 /* (1st arg of wait4()) */
289 long baddr; /* `Breakpoint' address */
290 long inst[2]; /* Instructions on above */
291 int pfd; /* proc file descriptor */
293 #ifdef HAVE_MP_PROCFS
298 prstatus_t status; /* procfs status structure */
302 struct procfs_status status;
309 #define TCB_STARTUP 00001 /* We have just begun ptracing this process */
310 #define TCB_INUSE 00002 /* This table entry is in use */
311 #define TCB_INSYSCALL 00004 /* A system call is in progress */
312 #define TCB_ATTACHED 00010 /* Process is not our own child */
313 #define TCB_EXITING 00020 /* As far as we know, this process is exiting */
314 #define TCB_SUSPENDED 00040 /* Process has done a wait(4), that can
315 not be allowed to complete just now */
316 #define TCB_BPTSET 00100 /* "Breakpoint" set after fork(2) */
317 #define TCB_SIGTRAPPED 00200 /* Process wanted to block SIGTRAP */
318 #define TCB_FOLLOWFORK 00400 /* Process should have forks followed */
319 #define TCB_REPRINT 01000 /* We should reprint this syscall on exit */
321 # if defined(ALPHA) || defined(SPARC) || defined(POWERPC) || defined(IA64) || defined(HPPA) || defined(SH) || defined(SH64) || defined(S390) || defined(S390X) || defined(ARM)
322 # define TCB_WAITEXECVE 02000 /* ignore SIGTRAP after exceve */
324 # define TCB_CLONE_DETACHED 04000 /* CLONE_DETACHED set in creating syscall */
325 # define TCB_CLONE_THREAD 010000 /* CLONE_THREAD set in creating syscall */
326 # define TCB_GROUP_EXITING 020000 /* TCB_EXITING was exit_group, not _exit */
327 # include <sys/syscall.h>
328 # ifndef __NR_exit_group
329 # /* Hack: Most headers around are too old to have __NR_exit_group. */
331 # define __NR_exit_group 405
333 # define __NR_exit_group 252
335 # define __NR_exit_group 1236
336 # elif defined POWERPC
337 # define __NR_exit_group 234
338 # elif defined S390 || defined S390X
339 # define __NR_exit_group 248
341 # define __NR_exit_group 188
342 # endif /* ALPHA et al */
343 # endif /* !__NR_exit_group */
346 /* qualifier flags */
347 #define QUAL_TRACE 0001 /* this system call should be traced */
348 #define QUAL_ABBREV 0002 /* abbreviate the structures of this syscall */
349 #define QUAL_VERBOSE 0004 /* decode the structures of this syscall */
350 #define QUAL_RAW 0010 /* print all args in hex for this syscall */
351 #define QUAL_SIGNAL 0020 /* report events with this signal */
352 #define QUAL_FAULT 0040 /* report events with this fault */
353 #define QUAL_READ 0100 /* dump data read on this file descriptor */
354 #define QUAL_WRITE 0200 /* dump data written to this file descriptor */
356 #define entering(tcp) (!((tcp)->flags & TCB_INSYSCALL))
357 #define exiting(tcp) ((tcp)->flags & TCB_INSYSCALL)
358 #define syserror(tcp) ((tcp)->u_error != 0)
359 #define verbose(tcp) (qual_flags[(tcp)->scno] & QUAL_VERBOSE)
360 #define abbrev(tcp) (qual_flags[(tcp)->scno] & QUAL_ABBREV)
367 /* Format of syscall return values */
368 #define RVAL_DECIMAL 000 /* decimal format */
369 #define RVAL_HEX 001 /* hex format */
370 #define RVAL_OCTAL 002 /* octal format */
371 #define RVAL_UDECIMAL 003 /* unsigned decimal format */
372 #define RVAL_LDECIMAL 004 /* long decimal format */
373 #define RVAL_LHEX 005 /* long hex format */
374 #define RVAL_LOCTAL 006 /* long octal format */
375 #define RVAL_LUDECIMAL 007 /* long unsigned decimal format */
376 #define RVAL_MASK 007 /* mask for these values */
378 #define RVAL_STR 010 /* Print `auxstr' field after return val */
379 #define RVAL_NONE 020 /* Print nothing */
382 #define offsetof(type, member) (((char *) &(((type *) NULL)->member)) - \
383 ((char *) (type *) NULL))
384 #endif /* !offsetof */
386 /* get offset of member within a user struct */
387 #define uoff(member) offsetof(struct user, member)
389 #define TRACE_FILE 001 /* Trace file-related syscalls. */
390 #define TRACE_IPC 002 /* Trace IPC-related syscalls. */
391 #define TRACE_NETWORK 004 /* Trace network-related syscalls. */
392 #define TRACE_PROCESS 010 /* Trace process-related syscalls. */
393 #define TRACE_SIGNAL 020 /* Trace signal-related syscalls. */
395 extern struct tcb **tcbtab;
396 extern int qual_flags[];
397 extern int debug, followfork, followvfork;
398 extern int rflag, tflag, dtime, cflag, xflag, qflag;
400 extern char *outfname;
401 extern unsigned int nprocs, tcbtabsize;
402 extern int max_strlen;
403 extern struct tcb *tcp_last;
411 extern int set_personality P((int personality));
412 extern char *xlookup P((struct xlat *, int));
413 extern struct tcb *alloctcb P((int));
414 extern struct tcb *pid2tcb P((int));
415 extern void droptcb P((struct tcb *));
416 extern int expand_tcbtab P((void));
418 extern void set_sortby P((char *));
419 extern void set_overhead P((int));
420 extern void qualify P((char *));
421 extern void newoutf P((struct tcb *));
422 extern int get_scno P((struct tcb *));
423 extern int trace_syscall P((struct tcb *));
424 extern void printxval P((struct xlat *, int, char *));
425 extern int printargs P((struct tcb *));
426 extern int addflags P((struct xlat *, int));
427 extern int printflags P((struct xlat *, int));
428 extern int umoven P((struct tcb *, long, int, char *));
429 extern int umovestr P((struct tcb *, long, int, char *));
430 extern int upeek P((int, long, long *));
431 extern void dumpiov P((struct tcb *, int, long));
432 extern void dumpstr P((struct tcb *, long, int));
433 extern void string_quote P((char *str));
434 extern void printstr P((struct tcb *, long, int));
435 extern void printnum P((struct tcb *, long, char *));
436 extern void printpath P((struct tcb *, long));
437 extern void printpathn P((struct tcb *, long, int));
438 extern void printtv P((struct tcb *, long));
439 #ifdef HAVE_SIGINFO_T
440 extern void printsiginfo P((siginfo_t *, int));
442 extern void printsock P((struct tcb *, long, int));
443 extern void print_sock_optmgmt P((struct tcb *, long, int));
444 extern void printrusage P((struct tcb *, long));
445 extern void printuid P((const char *, unsigned long));
446 extern int clearbpt P((struct tcb *));
447 extern int setbpt P((struct tcb *));
448 extern int sigishandled P((struct tcb *, int));
449 extern void printcall P((struct tcb *));
450 extern char *signame P((int));
451 extern void printsignal P((int));
452 extern void printleader P((struct tcb *));
453 extern void printtrailer P((struct tcb *));
454 extern void tabto P((int));
455 extern void call_summary P((FILE *));
456 extern void fake_execve P((struct tcb *, char *, char *[], char *[]));
457 extern void printtv32 P((struct tcb*, long));
458 extern void tprint_iov P((struct tcb *, int, long));
461 extern int internal_clone P((struct tcb *));
463 extern int internal_fork P((struct tcb *));
464 extern int internal_exec P((struct tcb *));
465 extern int internal_wait P((struct tcb *));
466 extern int internal_exit P((struct tcb *));
468 extern struct ioctlent *ioctl_lookup P((long));
469 extern struct ioctlent *ioctl_next_match P((struct ioctlent *));
470 extern int ioctl_decode P((struct tcb *, long, long));
471 extern int term_ioctl P((struct tcb *, long, long));
472 extern int sock_ioctl P((struct tcb *, long, long));
473 extern int proc_ioctl P((struct tcb *, int, int));
474 extern int stream_ioctl P((struct tcb *, int, int));
476 extern void tv_tv P((struct timeval *, int, int));
477 extern int tv_nz P((struct timeval *));
478 extern int tv_cmp P((struct timeval *, struct timeval *));
479 extern double tv_float P((struct timeval *));
480 extern void tv_add P((struct timeval *, struct timeval *, struct timeval *));
481 extern void tv_sub P((struct timeval *, struct timeval *, struct timeval *));
482 extern void tv_mul P((struct timeval *, struct timeval *, int));
483 extern void tv_div P((struct timeval *, struct timeval *, int));
486 extern int fixvfork P((struct tcb *));
488 #if !(defined(LINUX) && !defined(SPARC) && !defined(IA64))
489 extern long getrval2 P((struct tcb *));
492 extern int proc_open P((struct tcb *tcp, int attaching));
495 #define umove(pid, addr, objp) \
496 umoven((pid), (addr), sizeof *(objp), (char *) (objp))
500 extern void tprintf(const char *fmt, ...)
501 __attribute__ ((format (printf, 1, 2)));
503 extern void tprintf(const char *fmt, ...);
506 extern void tprintf();
509 #ifndef HAVE_STRERROR
510 const char *strerror P((int));
512 #ifndef HAVE_STRSIGNAL
513 const char *strsignal P((int));
516 extern int current_personality;
525 extern struct sysent *sysent;
526 extern int nsyscalls;
528 extern char **errnoent;
537 extern struct ioctlent *ioctlent;
538 extern int nioctlent;
540 extern char **signalent;
543 extern struct ioctlent *ioctlent;
544 extern int nioctlents;
545 extern char **signalent;
548 extern struct ioctlent ioctlent0[];
549 extern int nioctlents0;
550 extern char *signalent0[];
551 extern int nsignals0;
553 #if SUPPORTED_PERSONALITIES >= 2
554 extern struct ioctlent ioctlent1[];
555 extern int nioctlents1;
556 extern char *signalent1[];
557 extern int nsignals1;
558 #endif /* SUPPORTED_PERSONALITIES >= 2 */
560 #if SUPPORTED_PERSONALITIES >= 3
561 extern struct ioctlent ioctlent2[];
562 extern int nioctlents2;
563 extern char *signalent2[];
564 extern int nsignals2;
565 #endif /* SUPPORTED_PERSONALITIES >= 3 */
567 #if defined(FREEBSD) || (defined(LINUX) \
568 && defined(POWERPC) && !defined(__powerpc64__)) \
569 || (defined (LINUX) && defined (MIPS) && !defined(__mips64))
570 /* ARRGH! off_t args are aligned on 64 bit boundaries! */
571 #define ALIGN64(tcp,arg) \
574 memmove (&tcp->u_arg[arg], &tcp->u_arg[arg + 1], \
575 (tcp->u_nargs - arg - 1) * sizeof tcp->u_arg[0]); \
578 #define ALIGN64(tcp,arg) do { } while (0)
583 /* _l refers to the lower numbered u_arg,
584 * _h refers to the higher numbered u_arg
587 #if HAVE_LITTLE_ENDIAN_LONG_LONG
588 #define LONG_LONG(_l,_h) \
589 ((long long)((unsigned long long)(unsigned)(_l) | ((unsigned long long)(_h)<<32)))
591 #define LONG_LONG(_l,_h) \
592 ((long long)((unsigned long long)(unsigned)(_h) | ((unsigned long long)(_l)<<32)))
600 extern int not_failing_only;