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