blob: 9b02bca41234ecee21fa459b89992e8488752b9a [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) \
161 (IOCTL (t->pfd, PCWSTOP, NULL) < 0 ? -1 : \
162 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 */
197 FILE *outf; /* Output file for this process */
198 char *auxstr; /* Auxiliary info from syscall (see RVAL_STR) */
199 struct timeval stime; /* System time usage as of last process wait */
200 struct timeval dtime; /* Delta for system time usage */
201 struct timeval etime; /* Syscall entry time */
202 /* Support for tracing forked processes */
203 struct tcb *parent; /* Parent of this process */
204 int nchildren; /* # of traced children */
205 int waitpid; /* pid(s) this process is waiting for */
206 /* (1st arg of wait4()) */
207 long baddr; /* `Breakpoint' address */
208 long inst[2]; /* Instructions on above */
209 int pfd; /* proc file descriptor */
210#ifdef SVR4
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000211#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000212 int pfd_stat;
213 int pfd_as;
214 pstatus_t status;
215#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000216 prstatus_t status; /* procfs status structure */
217#endif
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000218#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000219};
220
221/* TCB flags */
222#define TCB_STARTUP 00001 /* We have just begun ptracing this process */
223#define TCB_INUSE 00002 /* This table entry is in use */
224#define TCB_INSYSCALL 00004 /* A system call is in progress */
225#define TCB_ATTACHED 00010 /* Process is not our own child */
226#define TCB_EXITING 00020 /* As far as we know, this process is exiting */
227#define TCB_SUSPENDED 00040 /* Process has done a wait(4), that can
228 not be allowed to complete just now */
229#define TCB_BPTSET 00100 /* "Breakpoint" set after fork(2) */
230#define TCB_SIGTRAPPED 00200 /* Process wanted to block SIGTRAP */
231#define TCB_FOLLOWFORK 00400 /* Process should have forks followed */
232#define TCB_REPRINT 01000 /* We should reprint this syscall on exit */
233#ifdef LINUX
234#if defined(ALPHA) || defined(SPARC) || defined(POWERPC)
235#define TCB_WAITEXECVE 02000 /* ignore SIGTRAP after exceve */
236#endif /* ALPHA */
237#endif /* LINUX */
238
239/* qualifier flags */
240#define QUAL_TRACE 0001 /* this system call should be traced */
241#define QUAL_ABBREV 0002 /* abbreviate the structures of this syscall */
242#define QUAL_VERBOSE 0004 /* decode the structures of this syscall */
243#define QUAL_RAW 0010 /* print all args in hex for this syscall */
244#define QUAL_SIGNAL 0020 /* report events with this signal */
245#define QUAL_FAULT 0040 /* report events with this fault */
246#define QUAL_READ 0100 /* dump data read on this file descriptor */
247#define QUAL_WRITE 0200 /* dump data written to this file descriptor */
248
249#define entering(tcp) (!((tcp)->flags & TCB_INSYSCALL))
250#define exiting(tcp) ((tcp)->flags & TCB_INSYSCALL)
251#define syserror(tcp) ((tcp)->u_error != 0)
252#define verbose(tcp) (qual_flags[(tcp)->scno] & QUAL_VERBOSE)
253#define abbrev(tcp) (qual_flags[(tcp)->scno] & QUAL_ABBREV)
254#define waiting_parent(tcp) \
255 (tcp->parent && \
256 (tcp->parent->flags & TCB_SUSPENDED) && \
257 (tcp->parent->waitpid <= 0 || tcp->parent->waitpid == tcp->pid))
258
259struct xlat {
260 int val;
261 char *str;
262};
263
264/* Format of syscall return values */
265#define RVAL_DECIMAL 000 /* decimal format */
266#define RVAL_HEX 001 /* hex format */
267#define RVAL_OCTAL 002 /* octal format */
268#define RVAL_UDECIMAL 003 /* unsigned decimal format */
269#define RVAL_MASK 007 /* mask for these values */
270
271#define RVAL_STR 010 /* Print `auxstr' field after return val */
272#define RVAL_NONE 020 /* Print nothing */
273
274#ifndef offsetof
275#define offsetof(type, member) (((char *) &(((type *) NULL)->member)) - \
276 ((char *) (type *) NULL))
277#endif /* !offsetof */
278
279/* get offset of member within a user struct */
280#define uoff(member) offsetof(struct user, member)
281
282#define TRACE_FILE 001 /* Trace file-related syscalls. */
283#define TRACE_IPC 002 /* Trace IPC-related syscalls. */
284#define TRACE_NETWORK 004 /* Trace network-related syscalls. */
285#define TRACE_PROCESS 010 /* Trace process-related syscalls. */
286#define TRACE_SIGNAL 020 /* Trace signal-related syscalls. */
287
288extern struct tcb tcbtab[];
289extern int qual_flags[];
290extern int debug, followfork, followvfork;
291extern int rflag, tflag, dtime, cflag, xflag, qflag;
292extern int acolumn;
293extern char *outfname;
294extern int nprocs;
295extern int max_strlen;
296extern struct tcb *tcp_last;
297
298#ifdef __STDC__
299#define P(args) args
300#else
301#define P(args) ()
302#endif
303
304extern int set_personality P((int personality));
305extern char *xlookup P((struct xlat *, int));
306extern struct tcb *alloctcb P((int));
307extern void droptcb P((struct tcb *));
308
309extern void set_sortby P((char *));
310extern void set_overhead P((int));
311extern void qualify P((char *));
312extern void newoutf P((struct tcb *));
313extern int trace_syscall P((struct tcb *));
314extern void printxval P((struct xlat *, int, char *));
315extern int printargs P((struct tcb *));
316extern int addflags P((struct xlat *, int));
317extern int printflags P((struct xlat *, int));
318extern int umoven P((struct tcb *, long, int, char *));
319extern int umovestr P((struct tcb *, long, int, char *));
320extern int upeek P((int, long, long *));
321extern void dumpstr P((struct tcb *, long, int));
322extern void string_quote P((char *str));
323extern void printstr P((struct tcb *, long, int));
324extern void printnum P((struct tcb *, long, char *));
325extern void printpath P((struct tcb *, long));
326extern void printpathn P((struct tcb *, long, int));
327extern void printtv P((struct tcb *, long));
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000328extern void printsock P((struct tcb *, long, int));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000329extern void printrusage P((struct tcb *, long));
330extern int clearbpt P((struct tcb *));
331extern int setbpt P((struct tcb *));
332extern int sigishandled P((struct tcb *, int));
333extern void printcall P((struct tcb *));
Nate Sammonsce780fc1999-03-29 23:23:13 +0000334extern char *signame P((int));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000335extern void printsignal P((int));
336extern void printleader P((struct tcb *));
337extern void printtrailer P((struct tcb *));
338extern void tabto P((int));
339extern void call_summary P((FILE *));
340extern void fake_execve P((struct tcb *, char *, char *[], char *[]));
Wichert Akkerman221f54f1999-11-18 17:26:45 +0000341extern void printtv32 P((struct tcb*, long));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000342
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000343#ifdef LINUX
344extern int internal_clone P((struct tcb *));
345#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000346extern int internal_fork P((struct tcb *));
347extern int internal_exec P((struct tcb *));
348extern int internal_wait P((struct tcb *));
349extern int internal_exit P((struct tcb *));
350
351extern char *ioctl_lookup P((long));
352extern int ioctl_decode P((struct tcb *, long, long));
353extern int term_ioctl P((struct tcb *, long, long));
354extern int sock_ioctl P((struct tcb *, long, long));
355extern int proc_ioctl P((struct tcb *, int, int));
356extern int stream_ioctl P((struct tcb *, int, int));
357
358extern void tv_tv P((struct timeval *, int, int));
359extern int tv_nz P((struct timeval *));
360extern int tv_cmp P((struct timeval *, struct timeval *));
361extern double tv_float P((struct timeval *));
362extern void tv_add P((struct timeval *, struct timeval *, struct timeval *));
363extern void tv_sub P((struct timeval *, struct timeval *, struct timeval *));
364extern void tv_mul P((struct timeval *, struct timeval *, int));
365extern void tv_div P((struct timeval *, struct timeval *, int));
366
367#ifdef SUNOS4
368extern int fixvfork P((struct tcb *));
369#endif
370#if !(defined(LINUX) && !defined(SPARC))
371extern long getrval2 P((struct tcb *));
372#endif
373#ifdef SVR4
374extern int proc_open P((struct tcb *tcp, int attaching));
375#endif
376
377#define umove(pid, addr, objp) \
378 umoven((pid), (addr), sizeof *(objp), (char *) (objp))
379
380#ifdef __STDC__
381#ifdef __GNUC__
382extern void tprintf(const char *fmt, ...)
383 __attribute__ ((format (printf, 1, 2)));
384#else
385extern void tprintf(const char *fmt, ...);
386#endif
387#else
388extern void tprintf();
389#endif
390
391#ifndef HAVE_STRERROR
392const char *strerror P((int));
393#endif
394#ifndef HAVE_STRSIGNAL
395const char *strsignal P((int));
396#endif
397
398extern int current_personality;
399
400struct sysent {
401 int nargs;
402 int sys_flags;
403 int (*sys_func)();
404 char *sys_name;
405};
406
407extern struct sysent *sysent;
408extern int nsyscalls;
409
410extern char **errnoent;
411extern int nerrnos;
412
413struct ioctlent {
414 char *doth;
415 char *symbol;
416 unsigned long code;
417};
418
419extern struct ioctlent *ioctlent;
420extern int nioctlent;
421
422extern char **signalent;
423extern int nsignals;
424
425extern struct ioctlent *ioctlent;
426extern int nioctlents;
427extern char **signalent;
428extern int nsignals;
429
430extern struct ioctlent ioctlent0[];
431extern int nioctlents0;
432extern char *signalent0[];
433extern int nsignals0;
434
435#if SUPPORTED_PERSONALITIES >= 2
436extern struct ioctlent ioctlent1[];
437extern int nioctlents1;
438extern char *signalent1[];
439extern int nsignals1;
440#endif /* SUPPORTED_PERSONALITIES >= 2 */
441
442#if SUPPORTED_PERSONALITIES >= 3
443extern struct ioctlent ioctlent2[];
444extern int nioctlents2;
445extern char *signalent2[];
446extern int nsignals2;
447#endif /* SUPPORTED_PERSONALITIES >= 3 */