blob: 4dbed044b225ed27de838e701b6d097f022bfc05 [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
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>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
17 *
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.
28 *
29 * $Id$
30 */
31
Wichert Akkermanb859bea1999-04-18 22:50:50 +000032#ifdef linux
33#include <features.h>
34#endif
35
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000036#ifdef HAVE_CONFIG_H
37#include "config.h"
38#endif
39
40/* configuration section */
41#ifndef MAX_QUALS
Wichert Akkermanfd89ced2000-04-13 17:06:09 +000042#if defined(linux) && defined(MIPS)
43#define MAX_QUALS 4999 /* maximum number of syscalls, signals, etc. */
44#else
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000045#define MAX_QUALS 2048 /* maximum number of syscalls, signals, etc. */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000046#endif
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +000047#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000048#ifndef MAX_PROCS
Wichert Akkermanfaf72222000-02-19 23:59:03 +000049#define MAX_PROCS 64 /* maximum number of processes tracable */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000050#endif
51#ifndef DEFAULT_STRLEN
52#define DEFAULT_STRLEN 32 /* default maximum # of bytes printed in
53 `printstr', change with `-s' switch */
54#endif
55#ifndef DEFAULT_ACOLUMN
56#define DEFAULT_ACOLUMN 40 /* default alignment column for results */
57#endif
58#ifndef MAX_ARGS
59#define MAX_ARGS 32 /* maximum number of args to a syscall */
60#endif
61#ifndef DEFAULT_SORTBY
62#define DEFAULT_SORTBY "time" /* default sorting method for call profiling */
63#endif
64
65#include <sys/types.h>
66#include <unistd.h>
67#include <stdlib.h>
68#include <stdio.h>
69#include <ctype.h>
70#include <string.h>
71#include <sys/time.h>
72#include <errno.h>
73
74#ifdef STDC_HEADERS
75#include <stddef.h>
76#endif /* STDC_HEADERS */
77
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +000078#if defined(LINUX)
79# if defined(SPARC)
80# define LINUXSPARC
81# endif
82# if defined(ALPHA)
83# define LINUX_64BIT
84# endif
85#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000086
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +000087#if defined(SVR4) || defined(FREEBSD)
88#define USE_PROCFS
89#else
90#undef USE_PROCFS
91#endif
92
93#ifdef FREEBSD
94#ifndef I386
95#error "FreeBSD support is only for i386 arch right now."
96#endif
97#include <machine/psl.h>
98#include <machine/reg.h>
99#include <sys/syscall.h>
100#endif
101
102#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000103#include <sys/procfs.h>
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000104#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000105#include <sys/uio.h>
106#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000107#ifdef FREEBSD
108#include <sys/pioctl.h>
109#endif /* FREEBSD */
110#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000111#if defined(LINUXSPARC) && defined(__GLIBC__)
112#include <sys/ptrace.h>
113#else
114/* Work around awkward prototype in ptrace.h. */
115#define ptrace xptrace
116#include <sys/ptrace.h>
117#undef ptrace
118#ifdef POWERPC
119#define __KERNEL__
120#include <asm/ptrace.h>
121#undef __KERNEL__
122/* TEMP */
123#define UESP PT_R1
124#define EIP PT_NIP
125#define EAX PT_R3
126#define ORIG_EAX PT_ORIG_R3
127#endif
128#ifdef __STDC__
129#ifdef LINUX
130extern long ptrace(int, int, char *, long);
131#else /* !LINUX */
132extern int ptrace(int, int, char *, int, ...);
133#endif /* !LINUX */
134#else /* !__STDC__ */
135extern int ptrace();
136#endif /* !__STDC__ */
137#endif /* !LINUXSPARC */
138#endif /* !SVR4 */
139
140#ifdef LINUX
Wichert Akkermanb859bea1999-04-18 22:50:50 +0000141#if !defined(__GLIBC__)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000142#define PTRACE_PEEKUSER PTRACE_PEEKUSR
143#define PTRACE_POKEUSER PTRACE_POKEUSR
144#endif
145#ifdef ALPHA
Wichert Akkermanf90da011999-10-31 21:15:38 +0000146# define REG_R0 0
147# define REG_A0 16
148# define REG_A3 19
149# define REG_FP 30
150# define REG_PC 64
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000151#endif /* ALPHA */
Wichert Akkermanf90da011999-10-31 21:15:38 +0000152#ifdef MIPS
153# define REG_V0 2
154# define REG_A0 4
155# define REG_A3 7
156# define REG_SP 29
157# define REG_EPC 64
158#endif /* MIPS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000159#endif /* LINUX */
160
161#define SUPPORTED_PERSONALITIES 1
162#define DEFAULT_PERSONALITY 0
163
164#ifdef LINUXSPARC
165#include <linux/a.out.h>
166#include <asm/psr.h>
167#undef SUPPORTED_PERSONALITIES
Wichert Akkermanb859bea1999-04-18 22:50:50 +0000168#define SUPPORTED_PERSONALITIES 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000169#endif /* LINUXSPARC */
170
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000171
172#ifdef SVR4
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000173#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000174extern int mp_ioctl (int f, int c, void *a, int s);
175#define IOCTL(f,c,a) mp_ioctl (f, c, a, sizeof *a)
176#define IOCTL_STATUS(t) \
177 pread (t->pfd_stat, &t->status, sizeof t->status, 0)
178#define IOCTL_WSTOP(t) \
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000179 (IOCTL (t->pfd, PCWSTOP, (char *)NULL) < 0 ? -1 : \
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000180 IOCTL_STATUS (t))
181#define PR_WHY pr_lwp.pr_why
182#define PR_WHAT pr_lwp.pr_what
183#define PR_REG pr_lwp.pr_context.uc_mcontext.gregs
184#define PR_FLAGS pr_lwp.pr_flags
185#define PIOCSTIP PCSTOP
186#define PIOCSET PCSET
187#define PIOCRESET PCRESET
188#define PIOCSTRACE PCSTRACE
189#define PIOCSFAULT PCSFAULT
190#define PIOCWSTOP PCWSTOP
191#define PIOCSTOP PCSTOP
192#define PIOCSENTRY PCSENTRY
193#define PIOCSEXIT PCSEXIT
194#define PIOCRUN PCRUN
195#else
196#define IOCTL ioctl
197#define IOCTL_STATUS(t) ioctl (t->pfd, PIOCSTATUS, &t->status)
198#define IOCTL_WSTOP(t) ioctl (t->pfd, PIOCWSTOP, &t->status)
199#define PR_WHY pr_why
200#define PR_WHAT pr_what
201#define PR_REG pr_reg
202#define PR_FLAGS pr_flags
203#endif
204#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000205#ifdef FREEBSD
206#define IOCTL ioctl
207#define IOCTL_STATUS(t) ioctl (t->pfd, PIOCSTATUS, &t->status)
208#define IOCTL_WSTOP(t) ioctl (t->pfd, PIOCWAIT, &t->status)
209#define PIOCRUN PIOCCONT
210#define PIOCWSTOP PIOCWAIT
211#define PR_WHY why
212#define PR_WHAT val
213#define PR_FLAGS flags
214/* from /usr/src/sys/miscfs/procfs/procfs_vnops.c,
215 status.state = 0 for running, 1 for stopped */
216#define PR_SYSENTRY S_SCE
217#define PR_SYSEXIT S_SCX
218#define PR_SIGNALLED S_SIG
219#define PR_FAULTED S_CORE
220#endif
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000221
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000222/* Trace Control Block */
223struct tcb {
224 short flags; /* See below for TCB_ values */
225 int pid; /* Process Id of this entry */
226 long scno; /* System call number */
227 int u_nargs; /* System call arguments */
228 long u_arg[MAX_ARGS]; /* System call arguments */
229 int u_error; /* Error code */
230 long u_rval; /* (first) return value */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000231#ifdef HAVE_LONG_LONG
232 long long u_lrval; /* long long return value */
233#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000234 FILE *outf; /* Output file for this process */
Wichert Akkerman43a74822000-06-27 17:33:32 +0000235 const char *auxstr; /* Auxiliary info from syscall (see RVAL_STR) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000236 struct timeval stime; /* System time usage as of last process wait */
237 struct timeval dtime; /* Delta for system time usage */
238 struct timeval etime; /* Syscall entry time */
239 /* Support for tracing forked processes */
240 struct tcb *parent; /* Parent of this process */
241 int nchildren; /* # of traced children */
242 int waitpid; /* pid(s) this process is waiting for */
243 /* (1st arg of wait4()) */
244 long baddr; /* `Breakpoint' address */
245 long inst[2]; /* Instructions on above */
246 int pfd; /* proc file descriptor */
247#ifdef SVR4
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000248#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000249 int pfd_stat;
250 int pfd_as;
251 pstatus_t status;
252#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000253 prstatus_t status; /* procfs status structure */
254#endif
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000255#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000256#ifdef FREEBSD
257 struct procfs_status status;
258 int pfd_reg;
259 int pfd_status;
260#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000261};
262
263/* TCB flags */
264#define TCB_STARTUP 00001 /* We have just begun ptracing this process */
265#define TCB_INUSE 00002 /* This table entry is in use */
266#define TCB_INSYSCALL 00004 /* A system call is in progress */
267#define TCB_ATTACHED 00010 /* Process is not our own child */
268#define TCB_EXITING 00020 /* As far as we know, this process is exiting */
269#define TCB_SUSPENDED 00040 /* Process has done a wait(4), that can
270 not be allowed to complete just now */
271#define TCB_BPTSET 00100 /* "Breakpoint" set after fork(2) */
272#define TCB_SIGTRAPPED 00200 /* Process wanted to block SIGTRAP */
273#define TCB_FOLLOWFORK 00400 /* Process should have forks followed */
274#define TCB_REPRINT 01000 /* We should reprint this syscall on exit */
275#ifdef LINUX
276#if defined(ALPHA) || defined(SPARC) || defined(POWERPC)
277#define TCB_WAITEXECVE 02000 /* ignore SIGTRAP after exceve */
278#endif /* ALPHA */
279#endif /* LINUX */
280
281/* qualifier flags */
282#define QUAL_TRACE 0001 /* this system call should be traced */
283#define QUAL_ABBREV 0002 /* abbreviate the structures of this syscall */
284#define QUAL_VERBOSE 0004 /* decode the structures of this syscall */
285#define QUAL_RAW 0010 /* print all args in hex for this syscall */
286#define QUAL_SIGNAL 0020 /* report events with this signal */
287#define QUAL_FAULT 0040 /* report events with this fault */
288#define QUAL_READ 0100 /* dump data read on this file descriptor */
289#define QUAL_WRITE 0200 /* dump data written to this file descriptor */
290
291#define entering(tcp) (!((tcp)->flags & TCB_INSYSCALL))
292#define exiting(tcp) ((tcp)->flags & TCB_INSYSCALL)
293#define syserror(tcp) ((tcp)->u_error != 0)
294#define verbose(tcp) (qual_flags[(tcp)->scno] & QUAL_VERBOSE)
295#define abbrev(tcp) (qual_flags[(tcp)->scno] & QUAL_ABBREV)
296#define waiting_parent(tcp) \
297 (tcp->parent && \
298 (tcp->parent->flags & TCB_SUSPENDED) && \
299 (tcp->parent->waitpid <= 0 || tcp->parent->waitpid == tcp->pid))
300
301struct xlat {
302 int val;
303 char *str;
304};
305
306/* Format of syscall return values */
307#define RVAL_DECIMAL 000 /* decimal format */
308#define RVAL_HEX 001 /* hex format */
309#define RVAL_OCTAL 002 /* octal format */
310#define RVAL_UDECIMAL 003 /* unsigned decimal format */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000311#define RVAL_LDECIMAL 004 /* long decimal format */
312#define RVAL_LHEX 005 /* long hex format */
313#define RVAL_LOCTAL 006 /* long octal format */
314#define RVAL_LUDECIMAL 007 /* long unsigned decimal format */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000315#define RVAL_MASK 007 /* mask for these values */
316
317#define RVAL_STR 010 /* Print `auxstr' field after return val */
318#define RVAL_NONE 020 /* Print nothing */
319
320#ifndef offsetof
321#define offsetof(type, member) (((char *) &(((type *) NULL)->member)) - \
322 ((char *) (type *) NULL))
323#endif /* !offsetof */
324
325/* get offset of member within a user struct */
326#define uoff(member) offsetof(struct user, member)
327
328#define TRACE_FILE 001 /* Trace file-related syscalls. */
329#define TRACE_IPC 002 /* Trace IPC-related syscalls. */
330#define TRACE_NETWORK 004 /* Trace network-related syscalls. */
331#define TRACE_PROCESS 010 /* Trace process-related syscalls. */
332#define TRACE_SIGNAL 020 /* Trace signal-related syscalls. */
333
334extern struct tcb tcbtab[];
335extern int qual_flags[];
336extern int debug, followfork, followvfork;
337extern int rflag, tflag, dtime, cflag, xflag, qflag;
338extern int acolumn;
339extern char *outfname;
340extern int nprocs;
341extern int max_strlen;
342extern struct tcb *tcp_last;
343
344#ifdef __STDC__
345#define P(args) args
346#else
347#define P(args) ()
348#endif
349
350extern int set_personality P((int personality));
351extern char *xlookup P((struct xlat *, int));
352extern struct tcb *alloctcb P((int));
353extern void droptcb P((struct tcb *));
354
355extern void set_sortby P((char *));
356extern void set_overhead P((int));
357extern void qualify P((char *));
358extern void newoutf P((struct tcb *));
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000359extern int get_scno P((struct tcb *));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000360extern int trace_syscall P((struct tcb *));
361extern void printxval P((struct xlat *, int, char *));
362extern int printargs P((struct tcb *));
363extern int addflags P((struct xlat *, int));
364extern int printflags P((struct xlat *, int));
365extern int umoven P((struct tcb *, long, int, char *));
366extern int umovestr P((struct tcb *, long, int, char *));
367extern int upeek P((int, long, long *));
368extern void dumpstr P((struct tcb *, long, int));
369extern void string_quote P((char *str));
370extern void printstr P((struct tcb *, long, int));
371extern void printnum P((struct tcb *, long, char *));
372extern void printpath P((struct tcb *, long));
373extern void printpathn P((struct tcb *, long, int));
374extern void printtv P((struct tcb *, long));
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000375extern void printsock P((struct tcb *, long, int));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000376extern void printrusage P((struct tcb *, long));
377extern int clearbpt P((struct tcb *));
378extern int setbpt P((struct tcb *));
379extern int sigishandled P((struct tcb *, int));
380extern void printcall P((struct tcb *));
Nate Sammonsce780fc1999-03-29 23:23:13 +0000381extern char *signame P((int));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000382extern void printsignal P((int));
383extern void printleader P((struct tcb *));
384extern void printtrailer P((struct tcb *));
385extern void tabto P((int));
386extern void call_summary P((FILE *));
387extern void fake_execve P((struct tcb *, char *, char *[], char *[]));
Wichert Akkerman221f54f1999-11-18 17:26:45 +0000388extern void printtv32 P((struct tcb*, long));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000389
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000390#ifdef LINUX
391extern int internal_clone P((struct tcb *));
392#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000393extern int internal_fork P((struct tcb *));
394extern int internal_exec P((struct tcb *));
395extern int internal_wait P((struct tcb *));
396extern int internal_exit P((struct tcb *));
397
398extern char *ioctl_lookup P((long));
399extern int ioctl_decode P((struct tcb *, long, long));
400extern int term_ioctl P((struct tcb *, long, long));
401extern int sock_ioctl P((struct tcb *, long, long));
402extern int proc_ioctl P((struct tcb *, int, int));
403extern int stream_ioctl P((struct tcb *, int, int));
404
405extern void tv_tv P((struct timeval *, int, int));
406extern int tv_nz P((struct timeval *));
407extern int tv_cmp P((struct timeval *, struct timeval *));
408extern double tv_float P((struct timeval *));
409extern void tv_add P((struct timeval *, struct timeval *, struct timeval *));
410extern void tv_sub P((struct timeval *, struct timeval *, struct timeval *));
411extern void tv_mul P((struct timeval *, struct timeval *, int));
412extern void tv_div P((struct timeval *, struct timeval *, int));
413
414#ifdef SUNOS4
415extern int fixvfork P((struct tcb *));
416#endif
417#if !(defined(LINUX) && !defined(SPARC))
418extern long getrval2 P((struct tcb *));
419#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000420#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000421extern int proc_open P((struct tcb *tcp, int attaching));
422#endif
423
424#define umove(pid, addr, objp) \
425 umoven((pid), (addr), sizeof *(objp), (char *) (objp))
426
427#ifdef __STDC__
428#ifdef __GNUC__
429extern void tprintf(const char *fmt, ...)
430 __attribute__ ((format (printf, 1, 2)));
431#else
432extern void tprintf(const char *fmt, ...);
433#endif
434#else
435extern void tprintf();
436#endif
437
438#ifndef HAVE_STRERROR
439const char *strerror P((int));
440#endif
441#ifndef HAVE_STRSIGNAL
442const char *strsignal P((int));
443#endif
444
445extern int current_personality;
446
447struct sysent {
448 int nargs;
449 int sys_flags;
450 int (*sys_func)();
451 char *sys_name;
452};
453
454extern struct sysent *sysent;
455extern int nsyscalls;
456
457extern char **errnoent;
458extern int nerrnos;
459
460struct ioctlent {
461 char *doth;
462 char *symbol;
463 unsigned long code;
464};
465
466extern struct ioctlent *ioctlent;
467extern int nioctlent;
468
469extern char **signalent;
470extern int nsignals;
471
472extern struct ioctlent *ioctlent;
473extern int nioctlents;
474extern char **signalent;
475extern int nsignals;
476
477extern struct ioctlent ioctlent0[];
478extern int nioctlents0;
479extern char *signalent0[];
480extern int nsignals0;
481
482#if SUPPORTED_PERSONALITIES >= 2
483extern struct ioctlent ioctlent1[];
484extern int nioctlents1;
485extern char *signalent1[];
486extern int nsignals1;
487#endif /* SUPPORTED_PERSONALITIES >= 2 */
488
489#if SUPPORTED_PERSONALITIES >= 3
490extern struct ioctlent ioctlent2[];
491extern int nioctlents2;
492extern char *signalent2[];
493extern int nsignals2;
494#endif /* SUPPORTED_PERSONALITIES >= 3 */