]> granicus.if.org Git - strace/blob - defs.h
more FreeBSD updates
[strace] / defs.h
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 <sys/time.h>
72 #include <errno.h>
73
74 #ifdef STDC_HEADERS
75 #include <stddef.h>
76 #endif /* STDC_HEADERS */
77
78 #if defined(LINUX)
79 #  if defined(SPARC)
80 #     define LINUXSPARC
81 #  endif
82 #  if defined(ALPHA)
83 #     define LINUX_64BIT
84 #  endif
85 #endif 
86
87 #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
103 #include <sys/procfs.h>
104 #ifdef HAVE_MP_PROCFS
105 #include <sys/uio.h>
106 #endif
107 #ifdef FREEBSD
108 #include <sys/pioctl.h>
109 #endif /* FREEBSD */
110 #else /* !USE_PROCFS */
111 #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
130 extern long ptrace(int, int, char *, long);
131 #else /* !LINUX */
132 extern int ptrace(int, int, char *, int, ...);
133 #endif /* !LINUX */
134 #else /* !__STDC__ */
135 extern int ptrace();
136 #endif /* !__STDC__ */
137 #endif /* !LINUXSPARC */
138 #endif /* !SVR4 */
139
140 #ifdef LINUX
141 #if !defined(__GLIBC__)
142 #define PTRACE_PEEKUSER PTRACE_PEEKUSR
143 #define PTRACE_POKEUSER PTRACE_POKEUSR
144 #endif
145 #ifdef ALPHA
146 #  define REG_R0 0
147 #  define REG_A0 16
148 #  define REG_A3 19
149 #  define REG_FP 30
150 #  define REG_PC 64
151 #endif /* ALPHA */
152 #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 */
159 #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
168 #define SUPPORTED_PERSONALITIES 2
169 #endif /* LINUXSPARC */
170
171
172 #ifdef SVR4
173 #ifdef HAVE_MP_PROCFS
174 extern 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)                                          \
179         (IOCTL (t->pfd, PCWSTOP, (char *)NULL) < 0 ? -1 :               \
180          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
205 #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
213 #define PR_FLAGS        state
214 /* from /usr/src/sys/miscfs/procfs/procfs_vnops.c,
215    status.state = 0 for running, 1 for stopped */
216 #define PR_ASLEEP       1
217 #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
222
223 /* Trace Control Block */
224 struct 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 */
232 #ifdef HAVE_LONG_LONG
233         long long u_lrval;      /* long long return value */
234 #endif
235         FILE *outf;             /* Output file for this process */
236         const char *auxstr;     /* Auxiliary info from syscall (see RVAL_STR) */
237         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
249 #ifdef HAVE_MP_PROCFS
250         int pfd_stat;
251         int pfd_as;
252         pstatus_t status;
253 #else
254         prstatus_t status;      /* procfs status structure */
255 #endif
256 #endif
257 #ifdef FREEBSD
258         struct procfs_status status;
259         int pfd_reg;
260         int pfd_status;
261 #endif
262 };
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
302 struct 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 */
312 #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 */
316 #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
335 extern struct tcb tcbtab[];
336 extern int qual_flags[];
337 extern int debug, followfork, followvfork;
338 extern int rflag, tflag, dtime, cflag, xflag, qflag;
339 extern int acolumn;
340 extern char *outfname;
341 extern int nprocs;
342 extern int max_strlen;
343 extern struct tcb *tcp_last;
344
345 #ifdef __STDC__
346 #define P(args) args
347 #else
348 #define P(args) ()
349 #endif
350
351 extern int set_personality P((int personality));
352 extern char *xlookup P((struct xlat *, int));
353 extern struct tcb *alloctcb P((int));
354 extern void droptcb P((struct tcb *));
355
356 extern void set_sortby P((char *));
357 extern void set_overhead P((int));
358 extern void qualify P((char *));
359 extern void newoutf P((struct tcb *));
360 extern int get_scno P((struct tcb *));
361 extern int trace_syscall P((struct tcb *));
362 extern void printxval P((struct xlat *, int, char *));
363 extern int printargs P((struct tcb *));
364 extern int addflags P((struct xlat *, int));
365 extern int printflags P((struct xlat *, int));
366 extern int umoven P((struct tcb *, long, int, char *));
367 extern int umovestr P((struct tcb *, long, int, char *));
368 extern int upeek P((int, long, long *));
369 extern void dumpstr P((struct tcb *, long, int));
370 extern void string_quote P((char *str));
371 extern void printstr P((struct tcb *, long, int));
372 extern void printnum P((struct tcb *, long, char *));
373 extern void printpath P((struct tcb *, long));
374 extern void printpathn P((struct tcb *, long, int));
375 extern void printtv P((struct tcb *, long));
376 extern void printsock P((struct tcb *, long, int));
377 extern void printrusage P((struct tcb *, long));
378 extern int clearbpt P((struct tcb *));
379 extern int setbpt P((struct tcb *));
380 extern int sigishandled P((struct tcb *, int));
381 extern void printcall P((struct tcb *));
382 extern char *signame P((int));
383 extern void printsignal P((int));
384 extern void printleader P((struct tcb *));
385 extern void printtrailer P((struct tcb *));
386 extern void tabto P((int));
387 extern void call_summary P((FILE *));
388 extern void fake_execve P((struct tcb *, char *, char *[], char *[]));
389 extern void printtv32 P((struct tcb*, long));
390
391 #ifdef LINUX
392 extern int internal_clone P((struct tcb *));
393 #endif
394 extern int internal_fork P((struct tcb *));
395 extern int internal_exec P((struct tcb *));
396 extern int internal_wait P((struct tcb *));
397 extern int internal_exit P((struct tcb *));
398
399 extern char *ioctl_lookup P((long));
400 extern int ioctl_decode P((struct tcb *, long, long));
401 extern int term_ioctl P((struct tcb *, long, long));
402 extern int sock_ioctl P((struct tcb *, long, long));
403 extern int proc_ioctl P((struct tcb *, int, int));
404 extern int stream_ioctl P((struct tcb *, int, int));
405
406 extern void tv_tv P((struct timeval *, int, int));
407 extern int tv_nz P((struct timeval *));
408 extern int tv_cmp P((struct timeval *, struct timeval *));
409 extern double tv_float P((struct timeval *));
410 extern void tv_add P((struct timeval *, struct timeval *, struct timeval *));
411 extern void tv_sub P((struct timeval *, struct timeval *, struct timeval *));
412 extern void tv_mul P((struct timeval *, struct timeval *, int));
413 extern void tv_div P((struct timeval *, struct timeval *, int));
414
415 #ifdef SUNOS4
416 extern int fixvfork P((struct tcb *));
417 #endif
418 #if !(defined(LINUX) && !defined(SPARC))
419 extern long getrval2 P((struct tcb *));
420 #endif
421 #ifdef USE_PROCFS
422 extern 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__
430 extern void tprintf(const char *fmt, ...)
431         __attribute__ ((format (printf, 1, 2)));
432 #else
433 extern void tprintf(const char *fmt, ...);
434 #endif
435 #else
436 extern void tprintf();
437 #endif
438
439 #ifndef HAVE_STRERROR
440 const char *strerror P((int));
441 #endif
442 #ifndef HAVE_STRSIGNAL
443 const char *strsignal P((int));
444 #endif
445
446 extern int current_personality;
447
448 struct sysent {
449         int     nargs;
450         int     sys_flags;
451         int     (*sys_func)();
452         char    *sys_name;
453 };
454
455 extern struct sysent *sysent;
456 extern int nsyscalls;
457
458 extern char **errnoent;
459 extern int nerrnos;
460
461 struct ioctlent {
462         char *doth;
463         char *symbol;
464         unsigned long code;
465 };
466
467 extern struct ioctlent *ioctlent;
468 extern int nioctlent;
469
470 extern char **signalent;
471 extern int nsignals;
472
473 extern struct ioctlent *ioctlent;
474 extern int nioctlents;
475 extern char **signalent;
476 extern int nsignals;
477
478 extern struct ioctlent ioctlent0[];
479 extern int nioctlents0;
480 extern char *signalent0[];
481 extern int nsignals0;
482
483 #if SUPPORTED_PERSONALITIES >= 2
484 extern struct ioctlent ioctlent1[];
485 extern int nioctlents1;
486 extern char *signalent1[];
487 extern int nsignals1;
488 #endif /* SUPPORTED_PERSONALITIES >= 2 */
489
490 #if SUPPORTED_PERSONALITIES >= 3
491 extern struct ioctlent ioctlent2[];
492 extern int nioctlents2;
493 extern char *signalent2[];
494 extern int nsignals2;
495 #endif /* SUPPORTED_PERSONALITIES >= 3 */