blob: e6f90f101cad7bdcf33db6ce2d735a08d21c9cbb [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
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +000087#if defined(SVR4) || defined(FREEBSD)
88#define USE_PROCFS
89#else
90#undef USE_PROCFS
91#endif
92
93#ifdef FREEBSD
94#ifndef I386
95#error "FreeBSD support is only for i386 arch right now."
96#endif
97#include <machine/psl.h>
98#include <machine/reg.h>
99#include <sys/syscall.h>
100#endif
101
102#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000103#include <sys/procfs.h>
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000104#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000105#include <sys/uio.h>
106#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000107#ifdef FREEBSD
108#include <sys/pioctl.h>
109#endif /* FREEBSD */
110#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000111#if defined(LINUXSPARC) && defined(__GLIBC__)
112#include <sys/ptrace.h>
113#else
114/* Work around awkward prototype in ptrace.h. */
115#define ptrace xptrace
116#include <sys/ptrace.h>
117#undef ptrace
118#ifdef POWERPC
119#define __KERNEL__
120#include <asm/ptrace.h>
121#undef __KERNEL__
122/* TEMP */
123#define UESP PT_R1
124#define EIP PT_NIP
125#define EAX PT_R3
126#define ORIG_EAX PT_ORIG_R3
127#endif
128#ifdef __STDC__
129#ifdef LINUX
130extern long ptrace(int, int, char *, long);
131#else /* !LINUX */
132extern int ptrace(int, int, char *, int, ...);
133#endif /* !LINUX */
134#else /* !__STDC__ */
135extern int ptrace();
136#endif /* !__STDC__ */
137#endif /* !LINUXSPARC */
138#endif /* !SVR4 */
139
140#ifdef LINUX
Wichert Akkermanb859bea1999-04-18 22:50:50 +0000141#if !defined(__GLIBC__)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000142#define PTRACE_PEEKUSER PTRACE_PEEKUSR
143#define PTRACE_POKEUSER PTRACE_POKEUSR
144#endif
145#ifdef ALPHA
Wichert Akkermanf90da011999-10-31 21:15:38 +0000146# define REG_R0 0
147# define REG_A0 16
148# define REG_A3 19
149# define REG_FP 30
150# define REG_PC 64
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000151#endif /* ALPHA */
Wichert Akkermanf90da011999-10-31 21:15:38 +0000152#ifdef MIPS
153# define REG_V0 2
154# define REG_A0 4
155# define REG_A3 7
156# define REG_SP 29
157# define REG_EPC 64
158#endif /* MIPS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000159#endif /* LINUX */
160
161#define SUPPORTED_PERSONALITIES 1
162#define DEFAULT_PERSONALITY 0
163
164#ifdef LINUXSPARC
165#include <linux/a.out.h>
166#include <asm/psr.h>
167#undef SUPPORTED_PERSONALITIES
Wichert Akkermanb859bea1999-04-18 22:50:50 +0000168#define SUPPORTED_PERSONALITIES 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000169#endif /* LINUXSPARC */
170
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000171
172#ifdef SVR4
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000173#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000174extern int mp_ioctl (int f, int c, void *a, int s);
175#define IOCTL(f,c,a) mp_ioctl (f, c, a, sizeof *a)
176#define IOCTL_STATUS(t) \
177 pread (t->pfd_stat, &t->status, sizeof t->status, 0)
178#define IOCTL_WSTOP(t) \
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000179 (IOCTL (t->pfd, PCWSTOP, (char *)NULL) < 0 ? -1 : \
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000180 IOCTL_STATUS (t))
181#define PR_WHY pr_lwp.pr_why
182#define PR_WHAT pr_lwp.pr_what
183#define PR_REG pr_lwp.pr_context.uc_mcontext.gregs
184#define PR_FLAGS pr_lwp.pr_flags
185#define PIOCSTIP PCSTOP
186#define PIOCSET PCSET
187#define PIOCRESET PCRESET
188#define PIOCSTRACE PCSTRACE
189#define PIOCSFAULT PCSFAULT
190#define PIOCWSTOP PCWSTOP
191#define PIOCSTOP PCSTOP
192#define PIOCSENTRY PCSENTRY
193#define PIOCSEXIT PCSEXIT
194#define PIOCRUN PCRUN
195#else
196#define IOCTL ioctl
197#define IOCTL_STATUS(t) ioctl (t->pfd, PIOCSTATUS, &t->status)
198#define IOCTL_WSTOP(t) ioctl (t->pfd, PIOCWSTOP, &t->status)
199#define PR_WHY pr_why
200#define PR_WHAT pr_what
201#define PR_REG pr_reg
202#define PR_FLAGS pr_flags
203#endif
204#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000205#ifdef FREEBSD
206#define IOCTL ioctl
207#define IOCTL_STATUS(t) ioctl (t->pfd, PIOCSTATUS, &t->status)
208#define IOCTL_WSTOP(t) ioctl (t->pfd, PIOCWAIT, &t->status)
209#define PIOCRUN PIOCCONT
210#define PIOCWSTOP PIOCWAIT
211#define PR_WHY why
212#define PR_WHAT val
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +0000213#define PR_FLAGS state
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000214/* from /usr/src/sys/miscfs/procfs/procfs_vnops.c,
215 status.state = 0 for running, 1 for stopped */
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +0000216#define PR_ASLEEP 1
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000217#define PR_SYSENTRY S_SCE
218#define PR_SYSEXIT S_SCX
219#define PR_SIGNALLED S_SIG
220#define PR_FAULTED S_CORE
221#endif
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000222
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000223/* Trace Control Block */
224struct tcb {
225 short flags; /* See below for TCB_ values */
226 int pid; /* Process Id of this entry */
227 long scno; /* System call number */
228 int u_nargs; /* System call arguments */
229 long u_arg[MAX_ARGS]; /* System call arguments */
230 int u_error; /* Error code */
231 long u_rval; /* (first) return value */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000232#ifdef HAVE_LONG_LONG
233 long long u_lrval; /* long long return value */
234#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000235 FILE *outf; /* Output file for this process */
Wichert Akkerman43a74822000-06-27 17:33:32 +0000236 const char *auxstr; /* Auxiliary info from syscall (see RVAL_STR) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000237 struct timeval stime; /* System time usage as of last process wait */
238 struct timeval dtime; /* Delta for system time usage */
239 struct timeval etime; /* Syscall entry time */
240 /* Support for tracing forked processes */
241 struct tcb *parent; /* Parent of this process */
242 int nchildren; /* # of traced children */
243 int waitpid; /* pid(s) this process is waiting for */
244 /* (1st arg of wait4()) */
245 long baddr; /* `Breakpoint' address */
246 long inst[2]; /* Instructions on above */
247 int pfd; /* proc file descriptor */
248#ifdef SVR4
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000249#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000250 int pfd_stat;
251 int pfd_as;
252 pstatus_t status;
253#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000254 prstatus_t status; /* procfs status structure */
255#endif
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000256#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000257#ifdef FREEBSD
258 struct procfs_status status;
259 int pfd_reg;
260 int pfd_status;
261#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000262};
263
264/* TCB flags */
265#define TCB_STARTUP 00001 /* We have just begun ptracing this process */
266#define TCB_INUSE 00002 /* This table entry is in use */
267#define TCB_INSYSCALL 00004 /* A system call is in progress */
268#define TCB_ATTACHED 00010 /* Process is not our own child */
269#define TCB_EXITING 00020 /* As far as we know, this process is exiting */
270#define TCB_SUSPENDED 00040 /* Process has done a wait(4), that can
271 not be allowed to complete just now */
272#define TCB_BPTSET 00100 /* "Breakpoint" set after fork(2) */
273#define TCB_SIGTRAPPED 00200 /* Process wanted to block SIGTRAP */
274#define TCB_FOLLOWFORK 00400 /* Process should have forks followed */
275#define TCB_REPRINT 01000 /* We should reprint this syscall on exit */
276#ifdef LINUX
277#if defined(ALPHA) || defined(SPARC) || defined(POWERPC)
278#define TCB_WAITEXECVE 02000 /* ignore SIGTRAP after exceve */
279#endif /* ALPHA */
280#endif /* LINUX */
281
282/* qualifier flags */
283#define QUAL_TRACE 0001 /* this system call should be traced */
284#define QUAL_ABBREV 0002 /* abbreviate the structures of this syscall */
285#define QUAL_VERBOSE 0004 /* decode the structures of this syscall */
286#define QUAL_RAW 0010 /* print all args in hex for this syscall */
287#define QUAL_SIGNAL 0020 /* report events with this signal */
288#define QUAL_FAULT 0040 /* report events with this fault */
289#define QUAL_READ 0100 /* dump data read on this file descriptor */
290#define QUAL_WRITE 0200 /* dump data written to this file descriptor */
291
292#define entering(tcp) (!((tcp)->flags & TCB_INSYSCALL))
293#define exiting(tcp) ((tcp)->flags & TCB_INSYSCALL)
294#define syserror(tcp) ((tcp)->u_error != 0)
295#define verbose(tcp) (qual_flags[(tcp)->scno] & QUAL_VERBOSE)
296#define abbrev(tcp) (qual_flags[(tcp)->scno] & QUAL_ABBREV)
297#define waiting_parent(tcp) \
298 (tcp->parent && \
299 (tcp->parent->flags & TCB_SUSPENDED) && \
300 (tcp->parent->waitpid <= 0 || tcp->parent->waitpid == tcp->pid))
301
302struct xlat {
303 int val;
304 char *str;
305};
306
307/* Format of syscall return values */
308#define RVAL_DECIMAL 000 /* decimal format */
309#define RVAL_HEX 001 /* hex format */
310#define RVAL_OCTAL 002 /* octal format */
311#define RVAL_UDECIMAL 003 /* unsigned decimal format */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000312#define RVAL_LDECIMAL 004 /* long decimal format */
313#define RVAL_LHEX 005 /* long hex format */
314#define RVAL_LOCTAL 006 /* long octal format */
315#define RVAL_LUDECIMAL 007 /* long unsigned decimal format */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000316#define RVAL_MASK 007 /* mask for these values */
317
318#define RVAL_STR 010 /* Print `auxstr' field after return val */
319#define RVAL_NONE 020 /* Print nothing */
320
321#ifndef offsetof
322#define offsetof(type, member) (((char *) &(((type *) NULL)->member)) - \
323 ((char *) (type *) NULL))
324#endif /* !offsetof */
325
326/* get offset of member within a user struct */
327#define uoff(member) offsetof(struct user, member)
328
329#define TRACE_FILE 001 /* Trace file-related syscalls. */
330#define TRACE_IPC 002 /* Trace IPC-related syscalls. */
331#define TRACE_NETWORK 004 /* Trace network-related syscalls. */
332#define TRACE_PROCESS 010 /* Trace process-related syscalls. */
333#define TRACE_SIGNAL 020 /* Trace signal-related syscalls. */
334
335extern struct tcb tcbtab[];
336extern int qual_flags[];
337extern int debug, followfork, followvfork;
338extern int rflag, tflag, dtime, cflag, xflag, qflag;
339extern int acolumn;
340extern char *outfname;
341extern int nprocs;
342extern int max_strlen;
343extern struct tcb *tcp_last;
344
345#ifdef __STDC__
346#define P(args) args
347#else
348#define P(args) ()
349#endif
350
351extern int set_personality P((int personality));
352extern char *xlookup P((struct xlat *, int));
353extern struct tcb *alloctcb P((int));
354extern void droptcb P((struct tcb *));
355
356extern void set_sortby P((char *));
357extern void set_overhead P((int));
358extern void qualify P((char *));
359extern void newoutf P((struct tcb *));
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000360extern int get_scno P((struct tcb *));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000361extern int trace_syscall P((struct tcb *));
362extern void printxval P((struct xlat *, int, char *));
363extern int printargs P((struct tcb *));
364extern int addflags P((struct xlat *, int));
365extern int printflags P((struct xlat *, int));
366extern int umoven P((struct tcb *, long, int, char *));
367extern int umovestr P((struct tcb *, long, int, char *));
368extern int upeek P((int, long, long *));
369extern void dumpstr P((struct tcb *, long, int));
370extern void string_quote P((char *str));
371extern void printstr P((struct tcb *, long, int));
372extern void printnum P((struct tcb *, long, char *));
373extern void printpath P((struct tcb *, long));
374extern void printpathn P((struct tcb *, long, int));
375extern void printtv P((struct tcb *, long));
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000376extern void printsock P((struct tcb *, long, int));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000377extern void printrusage P((struct tcb *, long));
378extern int clearbpt P((struct tcb *));
379extern int setbpt P((struct tcb *));
380extern int sigishandled P((struct tcb *, int));
381extern void printcall P((struct tcb *));
Nate Sammonsce780fc1999-03-29 23:23:13 +0000382extern char *signame P((int));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000383extern void printsignal P((int));
384extern void printleader P((struct tcb *));
385extern void printtrailer P((struct tcb *));
386extern void tabto P((int));
387extern void call_summary P((FILE *));
388extern void fake_execve P((struct tcb *, char *, char *[], char *[]));
Wichert Akkerman221f54f1999-11-18 17:26:45 +0000389extern void printtv32 P((struct tcb*, long));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000390
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000391#ifdef LINUX
392extern int internal_clone P((struct tcb *));
393#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000394extern int internal_fork P((struct tcb *));
395extern int internal_exec P((struct tcb *));
396extern int internal_wait P((struct tcb *));
397extern int internal_exit P((struct tcb *));
398
399extern char *ioctl_lookup P((long));
400extern int ioctl_decode P((struct tcb *, long, long));
401extern int term_ioctl P((struct tcb *, long, long));
402extern int sock_ioctl P((struct tcb *, long, long));
403extern int proc_ioctl P((struct tcb *, int, int));
404extern int stream_ioctl P((struct tcb *, int, int));
405
406extern void tv_tv P((struct timeval *, int, int));
407extern int tv_nz P((struct timeval *));
408extern int tv_cmp P((struct timeval *, struct timeval *));
409extern double tv_float P((struct timeval *));
410extern void tv_add P((struct timeval *, struct timeval *, struct timeval *));
411extern void tv_sub P((struct timeval *, struct timeval *, struct timeval *));
412extern void tv_mul P((struct timeval *, struct timeval *, int));
413extern void tv_div P((struct timeval *, struct timeval *, int));
414
415#ifdef SUNOS4
416extern int fixvfork P((struct tcb *));
417#endif
418#if !(defined(LINUX) && !defined(SPARC))
419extern long getrval2 P((struct tcb *));
420#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000421#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000422extern int proc_open P((struct tcb *tcp, int attaching));
423#endif
424
425#define umove(pid, addr, objp) \
426 umoven((pid), (addr), sizeof *(objp), (char *) (objp))
427
428#ifdef __STDC__
429#ifdef __GNUC__
430extern void tprintf(const char *fmt, ...)
431 __attribute__ ((format (printf, 1, 2)));
432#else
433extern void tprintf(const char *fmt, ...);
434#endif
435#else
436extern void tprintf();
437#endif
438
439#ifndef HAVE_STRERROR
440const char *strerror P((int));
441#endif
442#ifndef HAVE_STRSIGNAL
443const char *strsignal P((int));
444#endif
445
446extern int current_personality;
447
448struct sysent {
449 int nargs;
450 int sys_flags;
451 int (*sys_func)();
452 char *sys_name;
453};
454
455extern struct sysent *sysent;
456extern int nsyscalls;
457
458extern char **errnoent;
459extern int nerrnos;
460
461struct ioctlent {
462 char *doth;
463 char *symbol;
464 unsigned long code;
465};
466
467extern struct ioctlent *ioctlent;
468extern int nioctlent;
469
470extern char **signalent;
471extern int nsignals;
472
473extern struct ioctlent *ioctlent;
474extern int nioctlents;
475extern char **signalent;
476extern int nsignals;
477
478extern struct ioctlent ioctlent0[];
479extern int nioctlents0;
480extern char *signalent0[];
481extern int nsignals0;
482
483#if SUPPORTED_PERSONALITIES >= 2
484extern struct ioctlent ioctlent1[];
485extern int nioctlents1;
486extern char *signalent1[];
487extern int nsignals1;
488#endif /* SUPPORTED_PERSONALITIES >= 2 */
489
490#if SUPPORTED_PERSONALITIES >= 3
491extern struct ioctlent ioctlent2[];
492extern int nioctlents2;
493extern char *signalent2[];
494extern int nsignals2;
495#endif /* SUPPORTED_PERSONALITIES >= 3 */