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