blob: d9ecccca47fe510ed991f07835ba8f554b79a237 [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
32#ifdef HAVE_CONFIG_H
33#include "config.h"
34#endif
35
36/* configuration section */
37#ifndef MAX_QUALS
38#define MAX_QUALS 1024 /* maximum number of syscalls, signals, etc. */
39#endif
40#ifndef MAX_PROCS
41#define MAX_PROCS 32 /* maximum number of processes tracable */
42#endif
43#ifndef DEFAULT_STRLEN
44#define DEFAULT_STRLEN 32 /* default maximum # of bytes printed in
45 `printstr', change with `-s' switch */
46#endif
47#ifndef DEFAULT_ACOLUMN
48#define DEFAULT_ACOLUMN 40 /* default alignment column for results */
49#endif
50#ifndef MAX_ARGS
51#define MAX_ARGS 32 /* maximum number of args to a syscall */
52#endif
53#ifndef DEFAULT_SORTBY
54#define DEFAULT_SORTBY "time" /* default sorting method for call profiling */
55#endif
56
57#include <sys/types.h>
58#include <unistd.h>
59#include <stdlib.h>
60#include <stdio.h>
61#include <ctype.h>
62#include <string.h>
63#include <sys/time.h>
64#include <errno.h>
65
66#ifdef STDC_HEADERS
67#include <stddef.h>
68#endif /* STDC_HEADERS */
69
70#if defined(LINUX) && defined(SPARC)
71#define LINUXSPARC
72#endif /* LINUX && SPARC */
73
74#ifdef SVR4
75#include <sys/procfs.h>
76#else /* !SVR4 */
77#if defined(LINUXSPARC) && defined(__GLIBC__)
78#include <sys/ptrace.h>
79#else
80/* Work around awkward prototype in ptrace.h. */
81#define ptrace xptrace
82#include <sys/ptrace.h>
83#undef ptrace
84#ifdef POWERPC
85#define __KERNEL__
86#include <asm/ptrace.h>
87#undef __KERNEL__
88/* TEMP */
89#define UESP PT_R1
90#define EIP PT_NIP
91#define EAX PT_R3
92#define ORIG_EAX PT_ORIG_R3
93#endif
94#ifdef __STDC__
95#ifdef LINUX
96extern long ptrace(int, int, char *, long);
97#else /* !LINUX */
98extern int ptrace(int, int, char *, int, ...);
99#endif /* !LINUX */
100#else /* !__STDC__ */
101extern int ptrace();
102#endif /* !__STDC__ */
103#endif /* !LINUXSPARC */
104#endif /* !SVR4 */
105
106#ifdef LINUX
107#if !defined(LINUXSPARC) || !defined(__GLIBC__)
108#define PTRACE_PEEKUSER PTRACE_PEEKUSR
109#define PTRACE_POKEUSER PTRACE_POKEUSR
110#endif
111#ifdef ALPHA
112#define REG_R0 0
113#define REG_A0 16
114#define REG_A3 19
115#define REG_FP 30
116#define REG_PC 64
117#endif /* ALPHA */
118#endif /* LINUX */
119
120#define SUPPORTED_PERSONALITIES 1
121#define DEFAULT_PERSONALITY 0
122
123#ifdef LINUXSPARC
124#include <linux/a.out.h>
125#include <asm/psr.h>
126#undef SUPPORTED_PERSONALITIES
127#define SUPPORTED_PERSONALITIES 3
128#endif /* LINUXSPARC */
129
130/* Trace Control Block */
131struct tcb {
132 short flags; /* See below for TCB_ values */
133 int pid; /* Process Id of this entry */
134 long scno; /* System call number */
135 int u_nargs; /* System call arguments */
136 long u_arg[MAX_ARGS]; /* System call arguments */
137 int u_error; /* Error code */
138 long u_rval; /* (first) return value */
139 FILE *outf; /* Output file for this process */
140 char *auxstr; /* Auxiliary info from syscall (see RVAL_STR) */
141 struct timeval stime; /* System time usage as of last process wait */
142 struct timeval dtime; /* Delta for system time usage */
143 struct timeval etime; /* Syscall entry time */
144 /* Support for tracing forked processes */
145 struct tcb *parent; /* Parent of this process */
146 int nchildren; /* # of traced children */
147 int waitpid; /* pid(s) this process is waiting for */
148 /* (1st arg of wait4()) */
149 long baddr; /* `Breakpoint' address */
150 long inst[2]; /* Instructions on above */
151 int pfd; /* proc file descriptor */
152#ifdef SVR4
153 prstatus_t status; /* procfs status structure */
154#endif
155};
156
157/* TCB flags */
158#define TCB_STARTUP 00001 /* We have just begun ptracing this process */
159#define TCB_INUSE 00002 /* This table entry is in use */
160#define TCB_INSYSCALL 00004 /* A system call is in progress */
161#define TCB_ATTACHED 00010 /* Process is not our own child */
162#define TCB_EXITING 00020 /* As far as we know, this process is exiting */
163#define TCB_SUSPENDED 00040 /* Process has done a wait(4), that can
164 not be allowed to complete just now */
165#define TCB_BPTSET 00100 /* "Breakpoint" set after fork(2) */
166#define TCB_SIGTRAPPED 00200 /* Process wanted to block SIGTRAP */
167#define TCB_FOLLOWFORK 00400 /* Process should have forks followed */
168#define TCB_REPRINT 01000 /* We should reprint this syscall on exit */
169#ifdef LINUX
170#if defined(ALPHA) || defined(SPARC) || defined(POWERPC)
171#define TCB_WAITEXECVE 02000 /* ignore SIGTRAP after exceve */
172#endif /* ALPHA */
173#endif /* LINUX */
174
175/* qualifier flags */
176#define QUAL_TRACE 0001 /* this system call should be traced */
177#define QUAL_ABBREV 0002 /* abbreviate the structures of this syscall */
178#define QUAL_VERBOSE 0004 /* decode the structures of this syscall */
179#define QUAL_RAW 0010 /* print all args in hex for this syscall */
180#define QUAL_SIGNAL 0020 /* report events with this signal */
181#define QUAL_FAULT 0040 /* report events with this fault */
182#define QUAL_READ 0100 /* dump data read on this file descriptor */
183#define QUAL_WRITE 0200 /* dump data written to this file descriptor */
184
185#define entering(tcp) (!((tcp)->flags & TCB_INSYSCALL))
186#define exiting(tcp) ((tcp)->flags & TCB_INSYSCALL)
187#define syserror(tcp) ((tcp)->u_error != 0)
188#define verbose(tcp) (qual_flags[(tcp)->scno] & QUAL_VERBOSE)
189#define abbrev(tcp) (qual_flags[(tcp)->scno] & QUAL_ABBREV)
190#define waiting_parent(tcp) \
191 (tcp->parent && \
192 (tcp->parent->flags & TCB_SUSPENDED) && \
193 (tcp->parent->waitpid <= 0 || tcp->parent->waitpid == tcp->pid))
194
195struct xlat {
196 int val;
197 char *str;
198};
199
200/* Format of syscall return values */
201#define RVAL_DECIMAL 000 /* decimal format */
202#define RVAL_HEX 001 /* hex format */
203#define RVAL_OCTAL 002 /* octal format */
204#define RVAL_UDECIMAL 003 /* unsigned decimal format */
205#define RVAL_MASK 007 /* mask for these values */
206
207#define RVAL_STR 010 /* Print `auxstr' field after return val */
208#define RVAL_NONE 020 /* Print nothing */
209
210#ifndef offsetof
211#define offsetof(type, member) (((char *) &(((type *) NULL)->member)) - \
212 ((char *) (type *) NULL))
213#endif /* !offsetof */
214
215/* get offset of member within a user struct */
216#define uoff(member) offsetof(struct user, member)
217
218#define TRACE_FILE 001 /* Trace file-related syscalls. */
219#define TRACE_IPC 002 /* Trace IPC-related syscalls. */
220#define TRACE_NETWORK 004 /* Trace network-related syscalls. */
221#define TRACE_PROCESS 010 /* Trace process-related syscalls. */
222#define TRACE_SIGNAL 020 /* Trace signal-related syscalls. */
223
224extern struct tcb tcbtab[];
225extern int qual_flags[];
226extern int debug, followfork, followvfork;
227extern int rflag, tflag, dtime, cflag, xflag, qflag;
228extern int acolumn;
229extern char *outfname;
230extern int nprocs;
231extern int max_strlen;
232extern struct tcb *tcp_last;
233
234#ifdef __STDC__
235#define P(args) args
236#else
237#define P(args) ()
238#endif
239
240extern int set_personality P((int personality));
241extern char *xlookup P((struct xlat *, int));
242extern struct tcb *alloctcb P((int));
243extern void droptcb P((struct tcb *));
244
245extern void set_sortby P((char *));
246extern void set_overhead P((int));
247extern void qualify P((char *));
248extern void newoutf P((struct tcb *));
249extern int trace_syscall P((struct tcb *));
250extern void printxval P((struct xlat *, int, char *));
251extern int printargs P((struct tcb *));
252extern int addflags P((struct xlat *, int));
253extern int printflags P((struct xlat *, int));
254extern int umoven P((struct tcb *, long, int, char *));
255extern int umovestr P((struct tcb *, long, int, char *));
256extern int upeek P((int, long, long *));
257extern void dumpstr P((struct tcb *, long, int));
258extern void string_quote P((char *str));
259extern void printstr P((struct tcb *, long, int));
260extern void printnum P((struct tcb *, long, char *));
261extern void printpath P((struct tcb *, long));
262extern void printpathn P((struct tcb *, long, int));
263extern void printtv P((struct tcb *, long));
264extern void printsock P((struct tcb *, long));
265extern void printrusage P((struct tcb *, long));
266extern int clearbpt P((struct tcb *));
267extern int setbpt P((struct tcb *));
268extern int sigishandled P((struct tcb *, int));
269extern void printcall P((struct tcb *));
270extern void printsignal P((int));
271extern void printleader P((struct tcb *));
272extern void printtrailer P((struct tcb *));
273extern void tabto P((int));
274extern void call_summary P((FILE *));
275extern void fake_execve P((struct tcb *, char *, char *[], char *[]));
276
277extern int internal_fork P((struct tcb *));
278extern int internal_exec P((struct tcb *));
279extern int internal_wait P((struct tcb *));
280extern int internal_exit P((struct tcb *));
281
282extern char *ioctl_lookup P((long));
283extern int ioctl_decode P((struct tcb *, long, long));
284extern int term_ioctl P((struct tcb *, long, long));
285extern int sock_ioctl P((struct tcb *, long, long));
286extern int proc_ioctl P((struct tcb *, int, int));
287extern int stream_ioctl P((struct tcb *, int, int));
288
289extern void tv_tv P((struct timeval *, int, int));
290extern int tv_nz P((struct timeval *));
291extern int tv_cmp P((struct timeval *, struct timeval *));
292extern double tv_float P((struct timeval *));
293extern void tv_add P((struct timeval *, struct timeval *, struct timeval *));
294extern void tv_sub P((struct timeval *, struct timeval *, struct timeval *));
295extern void tv_mul P((struct timeval *, struct timeval *, int));
296extern void tv_div P((struct timeval *, struct timeval *, int));
297
298#ifdef SUNOS4
299extern int fixvfork P((struct tcb *));
300#endif
301#if !(defined(LINUX) && !defined(SPARC))
302extern long getrval2 P((struct tcb *));
303#endif
304#ifdef SVR4
305extern int proc_open P((struct tcb *tcp, int attaching));
306#endif
307
308#define umove(pid, addr, objp) \
309 umoven((pid), (addr), sizeof *(objp), (char *) (objp))
310
311#ifdef __STDC__
312#ifdef __GNUC__
313extern void tprintf(const char *fmt, ...)
314 __attribute__ ((format (printf, 1, 2)));
315#else
316extern void tprintf(const char *fmt, ...);
317#endif
318#else
319extern void tprintf();
320#endif
321
322#ifndef HAVE_STRERROR
323const char *strerror P((int));
324#endif
325#ifndef HAVE_STRSIGNAL
326const char *strsignal P((int));
327#endif
328
329extern int current_personality;
330
331struct sysent {
332 int nargs;
333 int sys_flags;
334 int (*sys_func)();
335 char *sys_name;
336};
337
338extern struct sysent *sysent;
339extern int nsyscalls;
340
341extern char **errnoent;
342extern int nerrnos;
343
344struct ioctlent {
345 char *doth;
346 char *symbol;
347 unsigned long code;
348};
349
350extern struct ioctlent *ioctlent;
351extern int nioctlent;
352
353extern char **signalent;
354extern int nsignals;
355
356extern struct ioctlent *ioctlent;
357extern int nioctlents;
358extern char **signalent;
359extern int nsignals;
360
361extern struct ioctlent ioctlent0[];
362extern int nioctlents0;
363extern char *signalent0[];
364extern int nsignals0;
365
366#if SUPPORTED_PERSONALITIES >= 2
367extern struct ioctlent ioctlent1[];
368extern int nioctlents1;
369extern char *signalent1[];
370extern int nsignals1;
371#endif /* SUPPORTED_PERSONALITIES >= 2 */
372
373#if SUPPORTED_PERSONALITIES >= 3
374extern struct ioctlent ioctlent2[];
375extern int nioctlents2;
376extern char *signalent2[];
377extern int nsignals2;
378#endif /* SUPPORTED_PERSONALITIES >= 3 */