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 /* configuration section */
42 #if defined(linux) && defined(MIPS)
43 #define MAX_QUALS 4999 /* maximum number of syscalls, signals, etc. */
45 #define MAX_QUALS 2048 /* maximum number of syscalls, signals, etc. */
49 #define MAX_PROCS 64 /* maximum number of processes tracable */
51 #ifndef DEFAULT_STRLEN
52 #define DEFAULT_STRLEN 32 /* default maximum # of bytes printed in
53 `printstr', change with `-s' switch */
55 #ifndef DEFAULT_ACOLUMN
56 #define DEFAULT_ACOLUMN 40 /* default alignment column for results */
59 #define MAX_ARGS 32 /* maximum number of args to a syscall */
61 #ifndef DEFAULT_SORTBY
62 #define DEFAULT_SORTBY "time" /* default sorting method for call profiling */
65 #include <sys/types.h>
77 #endif /* STDC_HEADERS */
92 #if defined(SVR4) || defined(FREEBSD)
100 #error "FreeBSD support is only for i386 arch right now."
102 #include <machine/psl.h>
103 #include <machine/reg.h>
104 #include <sys/syscall.h>
108 #include <sys/procfs.h>
109 #ifdef HAVE_MP_PROCFS
113 #include <sys/pioctl.h>
115 #else /* !USE_PROCFS */
116 #if defined(LINUXSPARC) && defined(__GLIBC__)
117 #include <sys/ptrace.h>
119 /* Work around awkward prototype in ptrace.h. */
120 #define ptrace xptrace
121 #include <sys/ptrace.h>
125 #include <asm/ptrace.h>
131 #define ORIG_EAX PT_ORIG_R3
135 extern long ptrace(int, int, char *, long);
137 extern int ptrace(int, int, char *, int, ...);
139 #else /* !__STDC__ */
141 #endif /* !__STDC__ */
142 #endif /* !LINUXSPARC */
146 #if !defined(__GLIBC__)
147 #define PTRACE_PEEKUSER PTRACE_PEEKUSR
148 #define PTRACE_POKEUSER PTRACE_POKEUSR
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)
173 #define SUPPORTED_PERSONALITIES 1
174 #define DEFAULT_PERSONALITY 0
177 #include <linux/a.out.h>
179 #undef SUPPORTED_PERSONALITIES
180 #define SUPPORTED_PERSONALITIES 2
181 #endif /* LINUXSPARC */
184 #ifdef HAVE_MP_PROCFS
185 extern int mp_ioctl (int f, int c, void *a, int s);
186 #define IOCTL(f,c,a) mp_ioctl (f, c, a, sizeof *a)
187 #define IOCTL_STATUS(t) \
188 pread (t->pfd_stat, &t->status, sizeof t->status, 0)
189 #define IOCTL_WSTOP(t) \
190 (IOCTL (t->pfd, PCWSTOP, (char *)NULL) < 0 ? -1 : \
192 #define PR_WHY pr_lwp.pr_why
193 #define PR_WHAT pr_lwp.pr_what
194 #define PR_REG pr_lwp.pr_context.uc_mcontext.gregs
195 #define PR_FLAGS pr_lwp.pr_flags
196 #define PR_SYSCALL pr_lwp.pr_syscall
197 #define PR_INFO pr_lwp.pr_info
198 #define PIOCSTIP PCSTOP
199 #define PIOCSET PCSET
200 #define PIOCRESET PCRESET
201 #define PIOCSTRACE PCSTRACE
202 #define PIOCSFAULT PCSFAULT
203 #define PIOCWSTOP PCWSTOP
204 #define PIOCSTOP PCSTOP
205 #define PIOCSENTRY PCSENTRY
206 #define PIOCSEXIT PCSEXIT
207 #define PIOCRUN PCRUN
210 #define IOCTL_STATUS(t) ioctl (t->pfd, PIOCSTATUS, &t->status)
211 #define IOCTL_WSTOP(t) ioctl (t->pfd, PIOCWSTOP, &t->status)
212 #define PR_WHY pr_why
213 #define PR_WHAT pr_what
214 #define PR_REG pr_reg
215 #define PR_FLAGS pr_flags
216 #define PR_SYSCALL pr_syscall
217 #define PR_INFO pr_info
222 #define IOCTL_STATUS(t) ioctl (t->pfd, PIOCSTATUS, &t->status)
223 #define IOCTL_WSTOP(t) ioctl (t->pfd, PIOCWAIT, &t->status)
224 #define PIOCRUN PIOCCONT
225 #define PIOCWSTOP PIOCWAIT
228 #define PR_FLAGS state
229 /* from /usr/src/sys/miscfs/procfs/procfs_vnops.c,
230 status.state = 0 for running, 1 for stopped */
232 #define PR_SYSENTRY S_SCE
233 #define PR_SYSEXIT S_SCX
234 #define PR_SIGNALLED S_SIG
235 #define PR_FAULTED S_CORE
238 /* Trace Control Block */
240 short flags; /* See below for TCB_ values */
241 int pid; /* Process Id of this entry */
242 long scno; /* System call number */
243 int u_nargs; /* System call arguments */
244 long u_arg[MAX_ARGS]; /* System call arguments */
245 int u_error; /* Error code */
246 long u_rval; /* (first) return value */
247 #ifdef HAVE_LONG_LONG
248 long long u_lrval; /* long long return value */
250 FILE *outf; /* Output file for this process */
251 const char *auxstr; /* Auxiliary info from syscall (see RVAL_STR) */
252 struct timeval stime; /* System time usage as of last process wait */
253 struct timeval dtime; /* Delta for system time usage */
254 struct timeval etime; /* Syscall entry time */
255 /* Support for tracing forked processes */
256 struct tcb *parent; /* Parent of this process */
257 int nchildren; /* # of traced children */
258 int waitpid; /* pid(s) this process is waiting for */
259 /* (1st arg of wait4()) */
260 long baddr; /* `Breakpoint' address */
261 long inst[2]; /* Instructions on above */
262 int pfd; /* proc file descriptor */
264 #ifdef HAVE_MP_PROCFS
269 prstatus_t status; /* procfs status structure */
273 struct procfs_status status;
280 #define TCB_STARTUP 00001 /* We have just begun ptracing this process */
281 #define TCB_INUSE 00002 /* This table entry is in use */
282 #define TCB_INSYSCALL 00004 /* A system call is in progress */
283 #define TCB_ATTACHED 00010 /* Process is not our own child */
284 #define TCB_EXITING 00020 /* As far as we know, this process is exiting */
285 #define TCB_SUSPENDED 00040 /* Process has done a wait(4), that can
286 not be allowed to complete just now */
287 #define TCB_BPTSET 00100 /* "Breakpoint" set after fork(2) */
288 #define TCB_SIGTRAPPED 00200 /* Process wanted to block SIGTRAP */
289 #define TCB_FOLLOWFORK 00400 /* Process should have forks followed */
290 #define TCB_REPRINT 01000 /* We should reprint this syscall on exit */
292 # if defined(ALPHA) || defined(SPARC) || defined(POWERPC) || defined(IA64) || defined(HPPA)
293 # define TCB_WAITEXECVE 02000 /* ignore SIGTRAP after exceve */
297 /* qualifier flags */
298 #define QUAL_TRACE 0001 /* this system call should be traced */
299 #define QUAL_ABBREV 0002 /* abbreviate the structures of this syscall */
300 #define QUAL_VERBOSE 0004 /* decode the structures of this syscall */
301 #define QUAL_RAW 0010 /* print all args in hex for this syscall */
302 #define QUAL_SIGNAL 0020 /* report events with this signal */
303 #define QUAL_FAULT 0040 /* report events with this fault */
304 #define QUAL_READ 0100 /* dump data read on this file descriptor */
305 #define QUAL_WRITE 0200 /* dump data written to this file descriptor */
307 #define entering(tcp) (!((tcp)->flags & TCB_INSYSCALL))
308 #define exiting(tcp) ((tcp)->flags & TCB_INSYSCALL)
309 #define syserror(tcp) ((tcp)->u_error != 0)
310 #define verbose(tcp) (qual_flags[(tcp)->scno] & QUAL_VERBOSE)
311 #define abbrev(tcp) (qual_flags[(tcp)->scno] & QUAL_ABBREV)
312 #define waiting_parent(tcp) \
314 (tcp->parent->flags & TCB_SUSPENDED) && \
315 (tcp->parent->waitpid <= 0 || tcp->parent->waitpid == tcp->pid))
322 /* Format of syscall return values */
323 #define RVAL_DECIMAL 000 /* decimal format */
324 #define RVAL_HEX 001 /* hex format */
325 #define RVAL_OCTAL 002 /* octal format */
326 #define RVAL_UDECIMAL 003 /* unsigned decimal format */
327 #define RVAL_LDECIMAL 004 /* long decimal format */
328 #define RVAL_LHEX 005 /* long hex format */
329 #define RVAL_LOCTAL 006 /* long octal format */
330 #define RVAL_LUDECIMAL 007 /* long unsigned decimal format */
331 #define RVAL_MASK 007 /* mask for these values */
333 #define RVAL_STR 010 /* Print `auxstr' field after return val */
334 #define RVAL_NONE 020 /* Print nothing */
337 #define offsetof(type, member) (((char *) &(((type *) NULL)->member)) - \
338 ((char *) (type *) NULL))
339 #endif /* !offsetof */
341 /* get offset of member within a user struct */
342 #define uoff(member) offsetof(struct user, member)
344 #define TRACE_FILE 001 /* Trace file-related syscalls. */
345 #define TRACE_IPC 002 /* Trace IPC-related syscalls. */
346 #define TRACE_NETWORK 004 /* Trace network-related syscalls. */
347 #define TRACE_PROCESS 010 /* Trace process-related syscalls. */
348 #define TRACE_SIGNAL 020 /* Trace signal-related syscalls. */
350 extern struct tcb tcbtab[];
351 extern int qual_flags[];
352 extern int debug, followfork, followvfork;
353 extern int rflag, tflag, dtime, cflag, xflag, qflag;
355 extern char *outfname;
357 extern int max_strlen;
358 extern struct tcb *tcp_last;
366 extern int set_personality P((int personality));
367 extern char *xlookup P((struct xlat *, int));
368 extern struct tcb *alloctcb P((int));
369 extern void droptcb P((struct tcb *));
371 extern void set_sortby P((char *));
372 extern void set_overhead P((int));
373 extern void qualify P((char *));
374 extern void newoutf P((struct tcb *));
375 extern int get_scno P((struct tcb *));
376 extern int trace_syscall P((struct tcb *));
377 extern void printxval P((struct xlat *, int, char *));
378 extern int printargs P((struct tcb *));
379 extern int addflags P((struct xlat *, int));
380 extern int printflags P((struct xlat *, int));
381 extern int umoven P((struct tcb *, long, int, char *));
382 extern int umovestr P((struct tcb *, long, int, char *));
383 extern int upeek P((int, long, long *));
384 extern void dumpiov P((struct tcb *, int, long));
385 extern void dumpstr P((struct tcb *, long, int));
386 extern void string_quote P((char *str));
387 extern void printstr P((struct tcb *, long, int));
388 extern void printnum P((struct tcb *, long, char *));
389 extern void printpath P((struct tcb *, long));
390 extern void printpathn P((struct tcb *, long, int));
391 extern void printtv P((struct tcb *, long));
392 #ifdef HAVE_SIGINFO_T
393 extern void printsiginfo P((siginfo_t *, int));
395 extern void printsock P((struct tcb *, long, int));
396 extern void printrusage P((struct tcb *, long));
397 extern int clearbpt P((struct tcb *));
398 extern int setbpt P((struct tcb *));
399 extern int sigishandled P((struct tcb *, int));
400 extern void printcall P((struct tcb *));
401 extern char *signame P((int));
402 extern void printsignal P((int));
403 extern void printleader P((struct tcb *));
404 extern void printtrailer P((struct tcb *));
405 extern void tabto P((int));
406 extern void call_summary P((FILE *));
407 extern void fake_execve P((struct tcb *, char *, char *[], char *[]));
408 extern void printtv32 P((struct tcb*, long));
409 extern void tprint_iov P((struct tcb *, int, long));
412 extern int internal_clone P((struct tcb *));
414 extern int internal_fork P((struct tcb *));
415 extern int internal_exec P((struct tcb *));
416 extern int internal_wait P((struct tcb *));
417 extern int internal_exit P((struct tcb *));
419 extern char *ioctl_lookup P((long));
420 extern int ioctl_decode P((struct tcb *, long, long));
421 extern int term_ioctl P((struct tcb *, long, long));
422 extern int sock_ioctl P((struct tcb *, long, long));
423 extern int proc_ioctl P((struct tcb *, int, int));
424 extern int stream_ioctl P((struct tcb *, int, int));
426 extern void tv_tv P((struct timeval *, int, int));
427 extern int tv_nz P((struct timeval *));
428 extern int tv_cmp P((struct timeval *, struct timeval *));
429 extern double tv_float P((struct timeval *));
430 extern void tv_add P((struct timeval *, struct timeval *, struct timeval *));
431 extern void tv_sub P((struct timeval *, struct timeval *, struct timeval *));
432 extern void tv_mul P((struct timeval *, struct timeval *, int));
433 extern void tv_div P((struct timeval *, struct timeval *, int));
436 extern int fixvfork P((struct tcb *));
438 #if !(defined(LINUX) && !defined(SPARC))
439 extern long getrval2 P((struct tcb *));
442 extern int proc_open P((struct tcb *tcp, int attaching));
445 #define umove(pid, addr, objp) \
446 umoven((pid), (addr), sizeof *(objp), (char *) (objp))
450 extern void tprintf(const char *fmt, ...)
451 __attribute__ ((format (printf, 1, 2)));
453 extern void tprintf(const char *fmt, ...);
456 extern void tprintf();
459 #ifndef HAVE_STRERROR
460 const char *strerror P((int));
462 #ifndef HAVE_STRSIGNAL
463 const char *strsignal P((int));
466 extern int current_personality;
475 extern struct sysent *sysent;
476 extern int nsyscalls;
478 extern char **errnoent;
487 extern struct ioctlent *ioctlent;
488 extern int nioctlent;
490 extern char **signalent;
493 extern struct ioctlent *ioctlent;
494 extern int nioctlents;
495 extern char **signalent;
498 extern struct ioctlent ioctlent0[];
499 extern int nioctlents0;
500 extern char *signalent0[];
501 extern int nsignals0;
503 #if SUPPORTED_PERSONALITIES >= 2
504 extern struct ioctlent ioctlent1[];
505 extern int nioctlents1;
506 extern char *signalent1[];
507 extern int nsignals1;
508 #endif /* SUPPORTED_PERSONALITIES >= 2 */
510 #if SUPPORTED_PERSONALITIES >= 3
511 extern struct ioctlent ioctlent2[];
512 extern int nioctlents2;
513 extern char *signalent2[];
514 extern int nsignals2;
515 #endif /* SUPPORTED_PERSONALITIES >= 3 */
518 /* ARRGH! off_t args are aligned on 64 bit boundaries! */
519 #define ALIGN64(tcp,arg) \
522 memmove (&tcp->u_arg[arg], &tcp->u_arg[arg + 1], \
523 (tcp->u_nargs - arg - 1) * sizeof tcp->u_arg[0]); \
526 #define ALIGN64(tcp,arg) do { } while (0)
531 /* _l refers to the lower numbered u_arg,
532 * _h refers to the higher numbered u_arg
535 #if HAVE_LITTLE_ENDIAN_LONG_LONG
536 #define LONG_LONG(_l,_h) \
537 ((long long)((unsigned long long)(unsigned)(_l) | ((unsigned long long)(_h)<<32)))
539 #define LONG_LONG(_l,_h) \
540 ((long long)((unsigned long long)(unsigned)(_h) | ((unsigned long long)(_l)<<32)))