defs.h revision 5a826b8427383232940249ca16f543178fbeeba4
1/*
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
32#ifdef linux
33#include <features.h>
34#endif
35
36#ifdef HAVE_CONFIG_H
37#include "config.h"
38#endif
39
40/* configuration section */
41#ifndef MAX_QUALS
42#if defined(linux) && defined(MIPS)
43#define MAX_QUALS	4999	/* maximum number of syscalls, signals, etc. */
44#else
45#define MAX_QUALS	2048	/* maximum number of syscalls, signals, etc. */
46#endif
47#endif
48#ifndef MAX_PROCS
49#define MAX_PROCS	64	/* maximum number of processes tracable */
50#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 <time.h>
72#include <sys/time.h>
73#include <errno.h>
74
75#ifdef STDC_HEADERS
76#include <stddef.h>
77#endif /* STDC_HEADERS */
78
79#if defined(LINUX)
80#  if defined(SPARC)
81#     define LINUXSPARC
82#  endif
83#  if defined(ALPHA)
84#     define LINUX_64BIT
85#  endif
86#endif
87
88#if defined(SVR4) || defined(FREEBSD)
89#define USE_PROCFS
90#else
91#undef USE_PROCFS
92#endif
93
94#ifdef FREEBSD
95#ifndef I386
96#error "FreeBSD support is only for i386 arch right now."
97#endif
98#include <machine/psl.h>
99#include <machine/reg.h>
100#include <sys/syscall.h>
101#endif
102
103#ifdef USE_PROCFS
104#include <sys/procfs.h>
105#ifdef HAVE_MP_PROCFS
106#include <sys/uio.h>
107#endif
108#ifdef FREEBSD
109#include <sys/pioctl.h>
110#endif /* FREEBSD */
111#else /* !USE_PROCFS */
112#if defined(LINUXSPARC) && defined(__GLIBC__)
113#include <sys/ptrace.h>
114#else
115/* Work around awkward prototype in ptrace.h. */
116#define ptrace xptrace
117#include <sys/ptrace.h>
118#undef ptrace
119#ifdef POWERPC
120#define __KERNEL__
121#include <asm/ptrace.h>
122#undef __KERNEL__
123/* TEMP */
124#define UESP	PT_R1
125#define EIP	PT_NIP
126#define EAX	PT_R3
127#define ORIG_EAX PT_ORIG_R3
128#endif
129#ifdef __STDC__
130#ifdef LINUX
131extern long ptrace(int, int, char *, long);
132#else /* !LINUX */
133extern int ptrace(int, int, char *, int, ...);
134#endif /* !LINUX */
135#else /* !__STDC__ */
136extern int ptrace();
137#endif /* !__STDC__ */
138#endif /* !LINUXSPARC */
139#endif /* !SVR4 */
140
141#ifdef LINUX
142#if !defined(__GLIBC__)
143#define	PTRACE_PEEKUSER	PTRACE_PEEKUSR
144#define	PTRACE_POKEUSER	PTRACE_POKEUSR
145#endif
146#ifdef ALPHA
147#  define REG_R0 0
148#  define REG_A0 16
149#  define REG_A3 19
150#  define REG_FP 30
151#  define REG_PC 64
152#endif /* ALPHA */
153#ifdef MIPS
154#  define REG_V0 2
155#  define REG_A0 4
156#  define REG_A3 7
157#  define REG_SP 29
158#  define REG_EPC 64
159#endif /* MIPS */
160#endif /* LINUX */
161
162#define SUPPORTED_PERSONALITIES 1
163#define DEFAULT_PERSONALITY 0
164
165#ifdef LINUXSPARC
166#include <linux/a.out.h>
167#include <asm/psr.h>
168#undef  SUPPORTED_PERSONALITIES
169#define SUPPORTED_PERSONALITIES 2
170#endif /* LINUXSPARC */
171
172
173#ifdef SVR4
174#ifdef HAVE_MP_PROCFS
175extern int mp_ioctl (int f, int c, void *a, int s);
176#define IOCTL(f,c,a)	mp_ioctl (f, c, a, sizeof *a)
177#define IOCTL_STATUS(t) \
178	 pread (t->pfd_stat, &t->status, sizeof t->status, 0)
179#define IOCTL_WSTOP(t)						\
180	(IOCTL (t->pfd, PCWSTOP, (char *)NULL) < 0 ? -1 :		\
181	 IOCTL_STATUS (t))
182#define PR_WHY		pr_lwp.pr_why
183#define PR_WHAT		pr_lwp.pr_what
184#define PR_REG		pr_lwp.pr_context.uc_mcontext.gregs
185#define PR_FLAGS	pr_lwp.pr_flags
186#define PR_SYSCALL	pr_lwp.pr_syscall
187#define PIOCSTIP	PCSTOP
188#define PIOCSET		PCSET
189#define PIOCRESET	PCRESET
190#define PIOCSTRACE	PCSTRACE
191#define PIOCSFAULT	PCSFAULT
192#define PIOCWSTOP	PCWSTOP
193#define PIOCSTOP	PCSTOP
194#define PIOCSENTRY	PCSENTRY
195#define PIOCSEXIT	PCSEXIT
196#define PIOCRUN		PCRUN
197#else
198#define IOCTL		ioctl
199#define IOCTL_STATUS(t)	ioctl (t->pfd, PIOCSTATUS, &t->status)
200#define IOCTL_WSTOP(t)	ioctl (t->pfd, PIOCWSTOP, &t->status)
201#define PR_WHY		pr_why
202#define PR_WHAT		pr_what
203#define PR_REG		pr_reg
204#define PR_FLAGS	pr_flags
205#define PR_SYSCALL	pr_syscall
206#endif
207#endif
208#ifdef FREEBSD
209#define IOCTL		ioctl
210#define IOCTL_STATUS(t)	ioctl (t->pfd, PIOCSTATUS, &t->status)
211#define IOCTL_WSTOP(t)	ioctl (t->pfd, PIOCWAIT, &t->status)
212#define PIOCRUN         PIOCCONT
213#define PIOCWSTOP       PIOCWAIT
214#define PR_WHY		why
215#define PR_WHAT		val
216#define PR_FLAGS	state
217/* from /usr/src/sys/miscfs/procfs/procfs_vnops.c,
218   status.state = 0 for running, 1 for stopped */
219#define PR_ASLEEP	1
220#define PR_SYSENTRY     S_SCE
221#define PR_SYSEXIT      S_SCX
222#define PR_SIGNALLED    S_SIG
223#define PR_FAULTED      S_CORE
224#endif
225
226/* Trace Control Block */
227struct tcb {
228	short flags;		/* See below for TCB_ values */
229	int pid;		/* Process Id of this entry */
230	long scno;		/* System call number */
231	int u_nargs;		/* System call arguments */
232	long u_arg[MAX_ARGS];	/* System call arguments */
233	int u_error;		/* Error code */
234	long u_rval;		/* (first) return value */
235#ifdef HAVE_LONG_LONG
236	long long u_lrval;	/* long long return value */
237#endif
238	FILE *outf;		/* Output file for this process */
239	const char *auxstr;	/* Auxiliary info from syscall (see RVAL_STR) */
240	struct timeval stime;	/* System time usage as of last process wait */
241	struct timeval dtime;	/* Delta for system time usage */
242	struct timeval etime;	/* Syscall entry time */
243				/* Support for tracing forked processes */
244	struct tcb *parent;	/* Parent of this process */
245	int nchildren;		/* # of traced children */
246	int waitpid;		/* pid(s) this process is waiting for */
247				/* (1st arg of wait4()) */
248	long baddr;		/* `Breakpoint' address */
249	long inst[2];		/* Instructions on above */
250	int pfd;		/* proc file descriptor */
251#ifdef SVR4
252#ifdef HAVE_MP_PROCFS
253	int pfd_stat;
254	int pfd_as;
255	pstatus_t status;
256#else
257	prstatus_t status;	/* procfs status structure */
258#endif
259#endif
260#ifdef FREEBSD
261	struct procfs_status status;
262	int pfd_reg;
263	int pfd_status;
264#endif
265};
266
267/* TCB flags */
268#define TCB_STARTUP	00001	/* We have just begun ptracing this process */
269#define TCB_INUSE	00002	/* This table entry is in use */
270#define TCB_INSYSCALL	00004	/* A system call is in progress */
271#define TCB_ATTACHED	00010	/* Process is not our own child */
272#define TCB_EXITING	00020	/* As far as we know, this process is exiting */
273#define TCB_SUSPENDED	00040	/* Process has done a wait(4), that can
274				   not be allowed to complete just now */
275#define TCB_BPTSET	00100	/* "Breakpoint" set after fork(2) */
276#define TCB_SIGTRAPPED	00200	/* Process wanted to block SIGTRAP */
277#define TCB_FOLLOWFORK	00400	/* Process should have forks followed */
278#define TCB_REPRINT	01000	/* We should reprint this syscall on exit */
279#ifdef LINUX
280#if defined(ALPHA) || defined(SPARC) || defined(POWERPC)
281#define TCB_WAITEXECVE	02000	/* ignore SIGTRAP after exceve */
282#endif /* ALPHA */
283#endif /* LINUX */
284
285/* qualifier flags */
286#define QUAL_TRACE	0001	/* this system call should be traced */
287#define QUAL_ABBREV	0002	/* abbreviate the structures of this syscall */
288#define QUAL_VERBOSE	0004	/* decode the structures of this syscall */
289#define QUAL_RAW	0010	/* print all args in hex for this syscall */
290#define QUAL_SIGNAL	0020	/* report events with this signal */
291#define QUAL_FAULT	0040	/* report events with this fault */
292#define QUAL_READ	0100	/* dump data read on this file descriptor */
293#define QUAL_WRITE	0200	/* dump data written to this file descriptor */
294
295#define entering(tcp)	(!((tcp)->flags & TCB_INSYSCALL))
296#define exiting(tcp)	((tcp)->flags & TCB_INSYSCALL)
297#define syserror(tcp)	((tcp)->u_error != 0)
298#define verbose(tcp)	(qual_flags[(tcp)->scno] & QUAL_VERBOSE)
299#define abbrev(tcp)	(qual_flags[(tcp)->scno] & QUAL_ABBREV)
300#define waiting_parent(tcp) \
301		(tcp->parent && \
302		(tcp->parent->flags & TCB_SUSPENDED) && \
303		(tcp->parent->waitpid <= 0 || tcp->parent->waitpid == tcp->pid))
304
305struct xlat {
306	int val;
307	char *str;
308};
309
310/* Format of syscall return values */
311#define RVAL_DECIMAL	000	/* decimal format */
312#define RVAL_HEX	001	/* hex format */
313#define RVAL_OCTAL	002	/* octal format */
314#define RVAL_UDECIMAL	003	/* unsigned decimal format */
315#define RVAL_LDECIMAL	004	/* long decimal format */
316#define RVAL_LHEX	005	/* long hex format */
317#define RVAL_LOCTAL	006	/* long octal format */
318#define RVAL_LUDECIMAL	007	/* long unsigned decimal format */
319#define RVAL_MASK	007	/* mask for these values */
320
321#define RVAL_STR	010	/* Print `auxstr' field after return val */
322#define RVAL_NONE	020	/* Print nothing */
323
324#ifndef offsetof
325#define offsetof(type, member)	(((char *) &(((type *) NULL)->member)) - \
326				 ((char *) (type *) NULL))
327#endif /* !offsetof */
328
329/* get offset of member within a user struct */
330#define uoff(member)	offsetof(struct user, member)
331
332#define TRACE_FILE	001	/* Trace file-related syscalls. */
333#define TRACE_IPC	002	/* Trace IPC-related syscalls. */
334#define TRACE_NETWORK	004	/* Trace network-related syscalls. */
335#define TRACE_PROCESS	010	/* Trace process-related syscalls. */
336#define TRACE_SIGNAL	020	/* Trace signal-related syscalls. */
337
338extern struct tcb tcbtab[];
339extern int qual_flags[];
340extern int debug, followfork, followvfork;
341extern int rflag, tflag, dtime, cflag, xflag, qflag;
342extern int acolumn;
343extern char *outfname;
344extern int nprocs;
345extern int max_strlen;
346extern struct tcb *tcp_last;
347
348#ifdef __STDC__
349#define P(args) args
350#else
351#define P(args) ()
352#endif
353
354extern int set_personality P((int personality));
355extern char *xlookup P((struct xlat *, int));
356extern struct tcb *alloctcb P((int));
357extern void droptcb P((struct tcb *));
358
359extern void set_sortby P((char *));
360extern void set_overhead P((int));
361extern void qualify P((char *));
362extern void newoutf P((struct tcb *));
363extern int get_scno P((struct tcb *));
364extern int trace_syscall P((struct tcb *));
365extern void printxval P((struct xlat *, int, char *));
366extern int printargs P((struct tcb *));
367extern int addflags P((struct xlat *, int));
368extern int printflags P((struct xlat *, int));
369extern int umoven P((struct tcb *, long, int, char *));
370extern int umovestr P((struct tcb *, long, int, char *));
371extern int upeek P((int, long, long *));
372extern void dumpstr P((struct tcb *, long, int));
373extern void string_quote P((char *str));
374extern void printstr P((struct tcb *, long, int));
375extern void printnum P((struct tcb *, long, char *));
376extern void printpath P((struct tcb *, long));
377extern void printpathn P((struct tcb *, long, int));
378extern void printtv P((struct tcb *, long));
379extern void printsock P((struct tcb *, long, int));
380extern void printrusage P((struct tcb *, long));
381extern int clearbpt P((struct tcb *));
382extern int setbpt P((struct tcb *));
383extern int sigishandled P((struct tcb *, int));
384extern void printcall P((struct tcb *));
385extern char *signame P((int));
386extern void printsignal P((int));
387extern void printleader P((struct tcb *));
388extern void printtrailer P((struct tcb *));
389extern void tabto P((int));
390extern void call_summary P((FILE *));
391extern void fake_execve P((struct tcb *, char *, char *[], char *[]));
392extern void printtv32 P((struct tcb*, long));
393
394#ifdef LINUX
395extern int internal_clone P((struct tcb *));
396#endif
397extern int internal_fork P((struct tcb *));
398extern int internal_exec P((struct tcb *));
399extern int internal_wait P((struct tcb *));
400extern int internal_exit P((struct tcb *));
401
402extern char *ioctl_lookup P((long));
403extern int ioctl_decode P((struct tcb *, long, long));
404extern int term_ioctl P((struct tcb *, long, long));
405extern int sock_ioctl P((struct tcb *, long, long));
406extern int proc_ioctl P((struct tcb *, int, int));
407extern int stream_ioctl P((struct tcb *, int, int));
408
409extern void tv_tv P((struct timeval *, int, int));
410extern int tv_nz P((struct timeval *));
411extern int tv_cmp P((struct timeval *, struct timeval *));
412extern double tv_float P((struct timeval *));
413extern void tv_add P((struct timeval *, struct timeval *, struct timeval *));
414extern void tv_sub P((struct timeval *, struct timeval *, struct timeval *));
415extern void tv_mul P((struct timeval *, struct timeval *, int));
416extern void tv_div P((struct timeval *, struct timeval *, int));
417
418#ifdef SUNOS4
419extern int fixvfork P((struct tcb *));
420#endif
421#if !(defined(LINUX) && !defined(SPARC))
422extern long getrval2 P((struct tcb *));
423#endif
424#ifdef USE_PROCFS
425extern int proc_open P((struct tcb *tcp, int attaching));
426#endif
427
428#define umove(pid, addr, objp)	\
429	umoven((pid), (addr), sizeof *(objp), (char *) (objp))
430
431#ifdef __STDC__
432#ifdef __GNUC__
433extern void tprintf(const char *fmt, ...)
434	__attribute__ ((format (printf, 1, 2)));
435#else
436extern void tprintf(const char *fmt, ...);
437#endif
438#else
439extern void tprintf();
440#endif
441
442#ifndef HAVE_STRERROR
443const char *strerror P((int));
444#endif
445#ifndef HAVE_STRSIGNAL
446const char *strsignal P((int));
447#endif
448
449extern int current_personality;
450
451struct sysent {
452	int	nargs;
453	int	sys_flags;
454	int	(*sys_func)();
455	char	*sys_name;
456};
457
458extern struct sysent *sysent;
459extern int nsyscalls;
460
461extern char **errnoent;
462extern int nerrnos;
463
464struct ioctlent {
465	char *doth;
466	char *symbol;
467	unsigned long code;
468};
469
470extern struct ioctlent *ioctlent;
471extern int nioctlent;
472
473extern char **signalent;
474extern int nsignals;
475
476extern struct ioctlent *ioctlent;
477extern int nioctlents;
478extern char **signalent;
479extern int nsignals;
480
481extern struct ioctlent ioctlent0[];
482extern int nioctlents0;
483extern char *signalent0[];
484extern int nsignals0;
485
486#if SUPPORTED_PERSONALITIES >= 2
487extern struct ioctlent ioctlent1[];
488extern int nioctlents1;
489extern char *signalent1[];
490extern int nsignals1;
491#endif /* SUPPORTED_PERSONALITIES >= 2 */
492
493#if SUPPORTED_PERSONALITIES >= 3
494extern struct ioctlent ioctlent2[];
495extern int nioctlents2;
496extern char *signalent2[];
497extern int nsignals2;
498#endif /* SUPPORTED_PERSONALITIES >= 3 */
499
500#if FREEBSD
501/* ARRGH!  off_t args are aligned on 64 bit boundaries! */
502#define ALIGN64(tcp,arg)						\
503do {									\
504	if (arg % 2)							\
505	    memmove (&tcp->u_arg[arg], &tcp->u_arg[arg + 1],		\
506		     (tcp->u_nargs - arg - 1) * sizeof tcp->u_arg[0]);	\
507} while (0)
508#else
509#define ALIGN64(tcp,arg) do { } while (0)
510#endif
511
512#if _LFS64_LARGEFILE || FREEBSD
513
514/* _l refers to the lower numbered u_arg,
515 * _h refers to the higher numbered u_arg
516 */
517
518#if 1
519/* This should work, assuming we can do non-aligned 64 bit fetches.
520 * if not we'll have to figure out how which of the other versions to use.
521 */
522
523#define get64(_l,_h) (*(long long *) &(_l))
524
525#else
526
527#if _LITTLE_ENDIAN
528#define get64(_l,_h) ((long long)((unsigned long long)(_l) | ((unsigned long long)(_h)<<32)))
529#else
530#define get64(_l,_h) ((long long)((unsigned long long)(_h) | ((unsigned long long)(_l)<<32)))
531#endif
532#endif
533#endif
534
535