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