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