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.
33 #ifdef _LARGEFILE64_SOURCE
34 /* This is the macro everything checks before using foo64 names. */
35 # ifndef _LFS64_LARGEFILE
36 # define _LFS64_LARGEFILE 1
48 #include <sys/types.h>
61 #include <sys/syscall.h>
64 const char *strerror(int);
67 /* Some libc have stpcpy, some don't. Sigh...
68 * Roll our private implementation...
71 #define stpcpy strace_stpcpy
72 extern char *stpcpy(char *dst, const char *src);
76 # define __attribute__(x) /*nothing*/
79 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
81 /* Configuration section */
84 # define MAX_QUALS 7000 /* maximum number of syscalls, signals, etc. */
86 # define MAX_QUALS 2048 /* maximum number of syscalls, signals, etc. */
89 #ifndef DEFAULT_STRLEN
90 /* default maximum # of bytes printed in `printstr', change with -s switch */
91 # define DEFAULT_STRLEN 32
93 #ifndef DEFAULT_ACOLUMN
94 # define DEFAULT_ACOLUMN 40 /* default alignment column for results */
97 * Maximum number of args to a syscall.
99 * Make sure that all entries in all syscallent.h files have nargs <= MAX_ARGS!
100 * linux/<ARCH>/syscallent.h: all have nargs <= 6.
105 /* default sorting method for call profiling */
106 #ifndef DEFAULT_SORTBY
107 # define DEFAULT_SORTBY "time"
110 #if defined(SPARC) || defined(SPARC64)
113 #if defined(MIPS) && _MIPS_SIM == _MIPS_SIM_ABI32
114 # define LINUX_MIPSO32
116 #if defined(MIPS) && _MIPS_SIM == _MIPS_SIM_NABI32
117 # define LINUX_MIPSN32
118 # define LINUX_MIPS64
120 #if defined(MIPS) && _MIPS_SIM == _MIPS_SIM_ABI64
121 # define LINUX_MIPSN64
122 # define LINUX_MIPS64
125 #if (defined(LINUXSPARC) || defined(X86_64) || defined(ARM) || defined(AVR32)) && defined(__GLIBC__)
126 # include <sys/ptrace.h>
128 /* Work around awkward prototype in ptrace.h. */
129 # define ptrace xptrace
130 # include <sys/ptrace.h>
134 # include <asm/ptrace.h>
137 extern long ptrace(int, int, char *, long);
140 #if !defined(__GLIBC__)
141 # define PTRACE_PEEKUSER PTRACE_PEEKUSR
142 # define PTRACE_POKEUSER PTRACE_POKEUSR
144 #if defined(X86_64) || defined(I386)
145 /* For struct pt_regs. x86 strace uses PTRACE_GETREGS.
146 * PTRACE_GETREGS returns registers in the layout of this struct.
148 # include <asm/ptrace.h>
165 # define PT_GR20 (20*4)
166 # define PT_GR26 (26*4)
167 # define PT_GR28 (28*4)
168 # define PT_IAOQ0 (106*4)
169 # define PT_IAOQ1 (107*4)
172 /* SH64 Linux - this code assumes the following kernel API for system calls:
174 System Call Offset 16 (actually, (syscall no.) | (0x1n << 16),
175 where n = no. of parameters.
176 Other regs Offset 24+
178 On entry: R2-7 = parameters 1-6 (as many as necessary)
179 On return: R9 = result. */
181 /* Offset for peeks of registers */
182 # define REG_OFFSET (24)
183 # define REG_GENERAL(x) (8*(x)+REG_OFFSET)
184 # define REG_PC (0*8)
185 # define REG_SYSCALL (2*8)
188 #define SUPPORTED_PERSONALITIES 1
189 #define DEFAULT_PERSONALITY 0
192 /* Indexes into the pt_regs.u_reg[] array -- UREG_XX from kernel are all off
193 * by 1 and use Ix instead of Ox. These work for both 32 and 64 bit Linux. */
197 # define PERSONALITY0_WORDSIZE 4
198 # define PERSONALITY1_WORDSIZE 4
199 # undef SUPPORTED_PERSONALITIES
200 # if defined(SPARC64)
201 # include <asm/psrcompat.h>
202 # define SUPPORTED_PERSONALITIES 3
203 # define PERSONALITY2_WORDSIZE 8
205 # include <asm/psr.h>
206 # define SUPPORTED_PERSONALITIES 2
207 # endif /* SPARC64 */
208 #endif /* LINUXSPARC */
211 # undef SUPPORTED_PERSONALITIES
212 # define SUPPORTED_PERSONALITIES 2
213 # define PERSONALITY0_WORDSIZE 8
214 # define PERSONALITY1_WORDSIZE 4
218 # undef SUPPORTED_PERSONALITIES
219 # define SUPPORTED_PERSONALITIES 2
220 # define PERSONALITY0_WORDSIZE 4
221 # define PERSONALITY1_WORDSIZE 4
225 # undef SUPPORTED_PERSONALITIES
226 # define SUPPORTED_PERSONALITIES 2
227 # define PERSONALITY0_WORDSIZE 8
228 # define PERSONALITY1_WORDSIZE 4
231 #ifndef PERSONALITY0_WORDSIZE
232 # define PERSONALITY0_WORDSIZE sizeof(long)
235 #if !HAVE_DECL_PTRACE_SETOPTIONS
236 # define PTRACE_SETOPTIONS 0x4200
238 #if !HAVE_DECL_PTRACE_GETEVENTMSG
239 # define PTRACE_GETEVENTMSG 0x4201
241 #if !HAVE_DECL_PTRACE_GETSIGINFO
242 # define PTRACE_GETSIGINFO 0x4202
245 #if !HAVE_DECL_PTRACE_O_TRACESYSGOOD
246 # define PTRACE_O_TRACESYSGOOD 0x00000001
248 #if !HAVE_DECL_PTRACE_O_TRACEFORK
249 # define PTRACE_O_TRACEFORK 0x00000002
251 #if !HAVE_DECL_PTRACE_O_TRACEVFORK
252 # define PTRACE_O_TRACEVFORK 0x00000004
254 #if !HAVE_DECL_PTRACE_O_TRACECLONE
255 # define PTRACE_O_TRACECLONE 0x00000008
257 #if !HAVE_DECL_PTRACE_O_TRACEEXEC
258 # define PTRACE_O_TRACEEXEC 0x00000010
260 #if !HAVE_DECL_PTRACE_O_TRACEEXIT
261 # define PTRACE_O_TRACEEXIT 0x00000040
264 #if !HAVE_DECL_PTRACE_EVENT_FORK
265 # define PTRACE_EVENT_FORK 1
267 #if !HAVE_DECL_PTRACE_EVENT_VFORK
268 # define PTRACE_EVENT_VFORK 2
270 #if !HAVE_DECL_PTRACE_EVENT_CLONE
271 # define PTRACE_EVENT_CLONE 3
273 #if !HAVE_DECL_PTRACE_EVENT_EXEC
274 # define PTRACE_EVENT_EXEC 4
276 #if !HAVE_DECL_PTRACE_EVENT_VFORK_DONE
277 # define PTRACE_EVENT_VFORK_DONE 5
279 #if !HAVE_DECL_PTRACE_EVENT_EXIT
280 # define PTRACE_EVENT_EXIT 6
283 /* Experimental code using PTRACE_SEIZE can be enabled here: */
284 //# define USE_SEIZE 1
288 # define PTRACE_SEIZE 0x4206
289 # undef PTRACE_INTERRUPT
290 # define PTRACE_INTERRUPT 0x4207
291 # undef PTRACE_LISTEN
292 # define PTRACE_LISTEN 0x4208
293 # undef PTRACE_SEIZE_DEVEL
294 # define PTRACE_SEIZE_DEVEL 0x80000000
295 # undef PTRACE_EVENT_STOP
296 # define PTRACE_EVENT_STOP 7
297 # define PTRACE_EVENT_STOP1 128
301 extern struct pt_regs i386_regs;
307 /* Trace Control Block */
309 int flags; /* See below for TCB_ values */
310 int pid; /* Process Id of this entry */
311 int u_nargs; /* System call argument count */
312 int u_error; /* Error code */
313 long scno; /* System call number */
314 long u_arg[MAX_ARGS]; /* System call arguments */
315 #if defined(LINUX_MIPSN32)
316 long long ext_arg[MAX_ARGS]; /* System call arguments */
318 long u_rval; /* (first) return value */
319 #if SUPPORTED_PERSONALITIES > 1
320 int currpers; /* Personality at the time of scno update */
322 int curcol; /* Output column for this process */
323 FILE *outf; /* Output file for this process */
324 const char *auxstr; /* Auxiliary info from syscall (see RVAL_STR) */
325 struct timeval stime; /* System time usage as of last process wait */
326 struct timeval dtime; /* Delta for system time usage */
327 struct timeval etime; /* Syscall entry time */
328 /* Support for tracing forked processes: */
329 long inst[2]; /* Saved clone args (badly named) */
333 #define TCB_INUSE 00001 /* This table entry is in use */
334 /* We have attached to this process, but did not see it stopping yet */
335 #define TCB_STARTUP 00002
336 #define TCB_IGNORE_ONE_SIGSTOP 00004 /* Next SIGSTOP is to be ignored */
338 * Are we in system call entry or in syscall exit?
340 * This bit is set after all syscall entry processing is done.
341 * Therefore, this bit will be set when next ptrace stop occurs,
342 * which should be syscall exit stop. Other stops which are possible
343 * directly after syscall entry (death, ptrace event stop)
344 * are simpler and handled without calling trace_syscall(), therefore
345 * the places where TCB_INSYSCALL can be set but we aren't in syscall stop
346 * are limited to trace(), this condition is never observed in trace_syscall()
348 * The bit is cleared after all syscall exit processing is done.
349 * User-generated SIGTRAPs and post-execve SIGTRAP make it necessary
350 * to be very careful and NOT set TCB_INSYSCALL bit when they are encountered.
351 * TCB_WAITEXECVE bit is used for this purpose (see below).
353 * Use entering(tcp) / exiting(tcp) to check this bit to make code more readable.
355 #define TCB_INSYSCALL 00010
356 #define TCB_ATTACHED 00020 /* It is attached already */
357 /* Are we PROG from "strace PROG [ARGS]" invocation? */
358 #define TCB_STRACE_CHILD 00040
359 #define TCB_BPTSET 00100 /* "Breakpoint" set after fork(2) */
360 #define TCB_REPRINT 01000 /* We should reprint this syscall on exit */
361 #define TCB_FILTERED 02000 /* This system call has been filtered out */
362 /* x86 does not need TCB_WAITEXECVE.
363 * It can detect SIGTRAP by looking at eax/rax.
364 * See "not a syscall entry (eax = %ld)\n" message
365 * in syscall_fixup_on_sysenter().
367 #if defined(ALPHA) || defined(AVR32) || defined(SPARC) || defined(SPARC64) \
368 || defined(POWERPC) || defined(IA64) || defined(HPPA) \
369 || defined(SH) || defined(SH64) || defined(S390) || defined(S390X) \
370 || defined(ARM) || defined(MIPS) || defined(BFIN) || defined(TILE)
371 /* This tracee has entered into execve syscall. Expect post-execve SIGTRAP
372 * to happen. (When it is detected, tracee is continued and this bit is cleared.)
374 # define TCB_WAITEXECVE 04000
377 /* qualifier flags */
378 #define QUAL_TRACE 0001 /* this system call should be traced */
379 #define QUAL_ABBREV 0002 /* abbreviate the structures of this syscall */
380 #define QUAL_VERBOSE 0004 /* decode the structures of this syscall */
381 #define QUAL_RAW 0010 /* print all args in hex for this syscall */
382 #define QUAL_SIGNAL 0020 /* report events with this signal */
383 #define QUAL_FAULT 0040 /* report events with this fault */
384 #define QUAL_READ 0100 /* dump data read on this file descriptor */
385 #define QUAL_WRITE 0200 /* dump data written to this file descriptor */
387 #define entering(tcp) (!((tcp)->flags & TCB_INSYSCALL))
388 #define exiting(tcp) ((tcp)->flags & TCB_INSYSCALL)
389 #define syserror(tcp) ((tcp)->u_error != 0)
390 #define verbose(tcp) (qual_flags[(tcp)->scno] & QUAL_VERBOSE)
391 #define abbrev(tcp) (qual_flags[(tcp)->scno] & QUAL_ABBREV)
392 #define filtered(tcp) ((tcp)->flags & TCB_FILTERED)
399 extern const struct xlat open_mode_flags[];
400 extern const struct xlat addrfams[];
401 extern const struct xlat struct_user_offsets[];
402 extern const struct xlat open_access_modes[];
404 /* Format of syscall return values */
405 #define RVAL_DECIMAL 000 /* decimal format */
406 #define RVAL_HEX 001 /* hex format */
407 #define RVAL_OCTAL 002 /* octal format */
408 #define RVAL_UDECIMAL 003 /* unsigned decimal format */
409 #define RVAL_MASK 003 /* mask for these values */
411 #define RVAL_STR 010 /* Print `auxstr' field after return val */
412 #define RVAL_NONE 020 /* Print nothing */
415 # define offsetof(type, member) (((char *) &(((type *) NULL)->member)) - \
416 ((char *) (type *) NULL))
419 /* get offset of member within a user struct */
420 #define uoff(member) offsetof(struct user, member)
422 #define TRACE_FILE 001 /* Trace file-related syscalls. */
423 #define TRACE_IPC 002 /* Trace IPC-related syscalls. */
424 #define TRACE_NETWORK 004 /* Trace network-related syscalls. */
425 #define TRACE_PROCESS 010 /* Trace process-related syscalls. */
426 #define TRACE_SIGNAL 020 /* Trace signal-related syscalls. */
427 #define TRACE_DESC 040 /* Trace file descriptor-related syscalls. */
428 #define SYSCALL_NEVER_FAILS 0100 /* Syscall is always successful. */
435 extern cflag_t cflag;
436 extern int *qual_flags;
437 extern bool debug_flag;
440 extern bool not_failing_only;
441 extern bool show_fd_path;
442 extern bool tracing_paths;
443 extern unsigned int xflag;
444 extern unsigned int followfork;
445 extern unsigned int ptrace_setoptions;
446 extern unsigned int max_strlen;
448 enum bitness_t { BITNESS_CURRENT = 0, BITNESS_32 };
450 void error_msg(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
451 void perror_msg(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
452 void error_msg_and_die(const char *fmt, ...) __attribute__ ((noreturn, format(printf, 1, 2)));
453 void perror_msg_and_die(const char *fmt, ...) __attribute__ ((noreturn, format(printf, 1, 2)));
454 void die_out_of_memory(void) __attribute__ ((noreturn));
456 extern const char *xlookup(const struct xlat *, int);
458 extern void set_sortby(const char *);
459 extern void set_overhead(int);
460 extern void qualify(const char *);
461 extern int trace_syscall(struct tcb *);
462 extern void count_syscall(struct tcb *, struct timeval *);
463 extern void printxval(const struct xlat *, int, const char *);
464 extern int printargs(struct tcb *);
465 extern int printargs_lu(struct tcb *);
466 extern int printargs_ld(struct tcb *);
467 extern void addflags(const struct xlat *, int);
468 extern int printflags(const struct xlat *, int, const char *);
469 extern const char *sprintflags(const char *, const struct xlat *, int);
470 extern int umoven(struct tcb *, long, int, char *);
471 extern int umovestr(struct tcb *, long, int, char *);
472 extern int upeek(struct tcb *, long, long *);
473 extern void dumpiov(struct tcb *, int, long);
474 extern void dumpstr(struct tcb *, long, int);
475 extern void printstr(struct tcb *, long, int);
476 extern void printnum(struct tcb *, long, const char *);
477 extern void printnum_int(struct tcb *, long, const char *);
478 extern void printpath(struct tcb *, long);
479 extern void printpathn(struct tcb *, long, int);
480 #define TIMESPEC_TEXT_BUFSIZE (sizeof(long)*3 * 2 + sizeof("{%u, %u}"))
481 #define TIMEVAL_TEXT_BUFSIZE TIMESPEC_TEXT_BUFSIZE
482 extern void printtv_bitness(struct tcb *, long, enum bitness_t, int);
483 extern char *sprinttv(char *, struct tcb *, long, enum bitness_t, int special);
484 extern void print_timespec(struct tcb *, long);
485 extern void sprint_timespec(char *, struct tcb *, long);
486 #ifdef HAVE_SIGINFO_T
487 extern void printsiginfo(siginfo_t *, int);
489 extern const char *getfdpath(struct tcb *, int);
490 extern void printfd(struct tcb *, int);
491 extern void printsock(struct tcb *, long, int);
492 extern void print_sock_optmgmt(struct tcb *, long, int);
493 extern void printrusage(struct tcb *, long);
495 extern void printrusage32(struct tcb *, long);
497 extern void printuid(const char *, unsigned long);
499 * On Linux, "setbpt" is a misnomer: we don't set a breakpoint
500 * (IOW: no poking in user's text segment),
501 * instead we change fork/vfork/clone into clone(CLONE_PTRACE).
502 * On newer kernels, we use PTRACE_O_TRACECLONE/TRACE[V]FORK instead.
504 extern int setbpt(struct tcb *);
505 extern int clearbpt(struct tcb *);
506 extern void printcall(struct tcb *);
507 extern const char *signame(int);
508 extern void print_sigset(struct tcb *, long, int);
509 extern void printsignal(int);
511 extern void call_summary(FILE *);
512 extern void tprint_iov(struct tcb *, unsigned long, unsigned long, int decode_iov);
513 extern void tprint_open_modes(mode_t);
514 extern const char *sprint_open_modes(mode_t);
515 extern int is_restart_error(struct tcb *);
517 extern int pathtrace_select(const char *);
518 extern int pathtrace_match(struct tcb *);
520 extern const struct ioctlent *ioctl_lookup(long);
521 extern const struct ioctlent *ioctl_next_match(const struct ioctlent *);
522 extern int ioctl_decode(struct tcb *, long, long);
523 extern int term_ioctl(struct tcb *, long, long);
524 extern int sock_ioctl(struct tcb *, long, long);
525 extern int proc_ioctl(struct tcb *, int, int);
526 extern int rtc_ioctl(struct tcb *, long, long);
527 extern int scsi_ioctl(struct tcb *, long, long);
528 extern int block_ioctl(struct tcb *, long, long);
530 extern int tv_nz(struct timeval *);
531 extern int tv_cmp(struct timeval *, struct timeval *);
532 extern double tv_float(struct timeval *);
533 extern void tv_add(struct timeval *, struct timeval *, struct timeval *);
534 extern void tv_sub(struct timeval *, struct timeval *, struct timeval *);
535 extern void tv_mul(struct timeval *, struct timeval *, int);
536 extern void tv_div(struct timeval *, struct timeval *, int);
538 #if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
539 extern long getrval2(struct tcb *);
542 /* Strace log generation machinery.
544 * printing_tcp: tcb which has incomplete line being printed right now.
545 * NULL if last line has been completed ('\n'-terminated).
546 * printleader(tcp) examines it, finishes incomplete line if needed,
547 * the sets it to tcp.
548 * line_ended() clears printing_tcp and resets ->curcol = 0.
549 * tcp->curcol == 0 check is also used to detect completeness
550 * of last line, since in -ff mode just checking printing_tcp for NULL
553 * If you change this code, test log generation in both -f and -ff modes
555 * strace -oLOG -f[f] test/threaded_execve
556 * strace -oLOG -f[f] test/sigkill_rain
557 * strace -oLOG -f[f] -p "`pidof web_browser`"
559 extern struct tcb *printing_tcp;
560 extern void printleader(struct tcb *);
561 extern void line_ended(void);
562 extern void tabto(void);
563 extern void tprintf(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
564 extern void tprints(const char *str);
566 #define umove(pid, addr, objp) \
567 umoven((pid), (addr), sizeof *(objp), (char *) (objp))
569 #define printtv(tcp, addr) \
570 printtv_bitness((tcp), (addr), BITNESS_CURRENT, 0)
571 #define printtv_special(tcp, addr) \
572 printtv_bitness((tcp), (addr), BITNESS_CURRENT, 1)
574 #if SUPPORTED_PERSONALITIES > 1
575 extern void set_personality(int personality);
576 extern int current_personality;
577 extern const int personality_wordsize[];
578 # define current_wordsize (personality_wordsize[current_personality])
580 # define set_personality(personality) ((void)0)
581 # define current_personality 0
582 # define current_wordsize PERSONALITY0_WORDSIZE
589 const char *sys_name;
598 extern const struct sysent *sysent;
599 extern unsigned nsyscalls;
600 extern const char *const *errnoent;
601 extern unsigned nerrnos;
602 extern const struct ioctlent *ioctlent;
603 extern unsigned nioctlents;
604 extern const char *const *signalent;
605 extern unsigned nsignals;
607 #define SCNO_IN_RANGE(scno) \
608 ((unsigned long)(scno) < nsyscalls && sysent[scno].sys_func)
612 /* _l refers to the lower numbered u_arg,
613 * _h refers to the higher numbered u_arg
616 #if HAVE_LITTLE_ENDIAN_LONG_LONG
617 #define LONG_LONG(_l,_h) \
618 ((long long)((unsigned long long)(unsigned)(_l) | ((unsigned long long)(_h)<<32)))
620 #define LONG_LONG(_l,_h) \
621 ((long long)((unsigned long long)(unsigned)(_h) | ((unsigned long long)(_l)<<32)))
624 extern int printllval(struct tcb *, const char *, int);