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