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