]> granicus.if.org Git - strace/blob - defs.h
Update AF_*, PF_*, MSG_*, and TCP_* constants
[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
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33 #ifdef _LARGEFILE64_SOURCE
34 /* This is the macro everything checks before using foo64 names.  */
35 # ifndef _LFS64_LARGEFILE
36 #  define _LFS64_LARGEFILE 1
37 # endif
38 #endif
39
40 #ifdef MIPS
41 # include <sgidefs.h>
42 # if _MIPS_SIM == _MIPS_SIM_ABI64
43 #  define LINUX_MIPSN64
44 # elif _MIPS_SIM == _MIPS_SIM_NABI32
45 #  define LINUX_MIPSN32
46 # elif _MIPS_SIM == _MIPS_SIM_ABI32
47 #  define LINUX_MIPSO32
48 # else
49 #  error Unsupported _MIPS_SIM
50 # endif
51 #endif
52
53 #include <features.h>
54 #ifdef HAVE_STDBOOL_H
55 # include <stdbool.h>
56 #endif
57 #include <stdint.h>
58 #include <inttypes.h>
59 #include <sys/types.h>
60 #ifdef STDC_HEADERS
61 # include <stddef.h>
62 #endif
63 #include <unistd.h>
64 #include <stdlib.h>
65 #include <stdio.h>
66 /* Open-coding isprint(ch) et al proved more efficient than calling
67  * generalized libc interface. We don't *want* to do non-ASCII anyway.
68  */
69 /* #include <ctype.h> */
70 #include <string.h>
71 #include <errno.h>
72 #include <signal.h>
73 #include <time.h>
74 #include <sys/time.h>
75 #include <sys/syscall.h>
76
77 #ifndef HAVE_STRERROR
78 const char *strerror(int);
79 #endif
80 #ifndef HAVE_STPCPY
81 /* Some libc have stpcpy, some don't. Sigh...
82  * Roll our private implementation...
83  */
84 #undef stpcpy
85 #define stpcpy strace_stpcpy
86 extern char *stpcpy(char *dst, const char *src);
87 #endif
88
89 #if !defined __GNUC__
90 # define __attribute__(x) /*nothing*/
91 #endif
92
93 #ifndef offsetof
94 # define offsetof(type, member) \
95         (((char *) &(((type *) NULL)->member)) - ((char *) (type *) NULL))
96 #endif
97
98 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
99
100 /* macros */
101 #ifndef MAX
102 # define MAX(a, b)              (((a) > (b)) ? (a) : (b))
103 #endif
104 #ifndef MIN
105 # define MIN(a, b)              (((a) < (b)) ? (a) : (b))
106 #endif
107 #define CLAMP(val, min, max) MIN(MAX(min, val), max)
108
109 /* Glibc has an efficient macro for sigemptyset
110  * (it just does one or two assignments of 0 to internal vector of longs).
111  */
112 #if defined(__GLIBC__) && defined(__sigemptyset) && !defined(sigemptyset)
113 # define sigemptyset __sigemptyset
114 #endif
115
116 /* Configuration section */
117 #ifndef DEFAULT_STRLEN
118 /* default maximum # of bytes printed in `printstr', change with -s switch */
119 # define DEFAULT_STRLEN 32
120 #endif
121 #ifndef DEFAULT_ACOLUMN
122 # define DEFAULT_ACOLUMN        40      /* default alignment column for results */
123 #endif
124 /*
125  * Maximum number of args to a syscall.
126  *
127  * Make sure that all entries in all syscallent.h files have nargs <= MAX_ARGS!
128  * linux/<ARCH>/syscallent*.h:
129  *      all have nargs <= 6 except mips o32 which has nargs <= 7.
130  */
131 #ifndef MAX_ARGS
132 # ifdef LINUX_MIPSO32
133 #  define MAX_ARGS      7
134 # else
135 #  define MAX_ARGS      6
136 # endif
137 #endif
138 /* default sorting method for call profiling */
139 #ifndef DEFAULT_SORTBY
140 # define DEFAULT_SORTBY "time"
141 #endif
142 /*
143  * Experimental code using PTRACE_SEIZE can be enabled here.
144  * This needs Linux kernel 3.4.x or later to work.
145  */
146 #define USE_SEIZE 1
147 /* To force NOMMU build, set to 1 */
148 #define NOMMU_SYSTEM 0
149
150 #if (defined(SPARC) || defined(SPARC64) \
151     || defined(I386) || defined(X32) || defined(X86_64) \
152     || defined(ARM) || defined(AARCH64) \
153     || defined(AVR32) \
154     || defined(OR1K) \
155     || defined(METAG) \
156     || defined(TILE) \
157     || defined(XTENSA) \
158     ) && defined(__GLIBC__)
159 # include <sys/ptrace.h>
160 #else
161 /* Work around awkward prototype in ptrace.h. */
162 # define ptrace xptrace
163 # include <sys/ptrace.h>
164 # undef ptrace
165 # ifdef POWERPC
166 #  define __KERNEL__
167 #  include <asm/ptrace.h>
168 #  undef __KERNEL__
169 # endif
170 extern long ptrace(int, int, char *, long);
171 #endif
172
173 #if defined(TILE)
174 # include <asm/ptrace.h>  /* struct pt_regs */
175 #endif
176
177 #if !HAVE_DECL_PTRACE_SETOPTIONS
178 # define PTRACE_SETOPTIONS      0x4200
179 #endif
180 #if !HAVE_DECL_PTRACE_GETEVENTMSG
181 # define PTRACE_GETEVENTMSG     0x4201
182 #endif
183 #if !HAVE_DECL_PTRACE_GETSIGINFO
184 # define PTRACE_GETSIGINFO      0x4202
185 #endif
186
187 #if !HAVE_DECL_PTRACE_O_TRACESYSGOOD
188 # define PTRACE_O_TRACESYSGOOD  0x00000001
189 #endif
190 #if !HAVE_DECL_PTRACE_O_TRACEFORK
191 # define PTRACE_O_TRACEFORK     0x00000002
192 #endif
193 #if !HAVE_DECL_PTRACE_O_TRACEVFORK
194 # define PTRACE_O_TRACEVFORK    0x00000004
195 #endif
196 #if !HAVE_DECL_PTRACE_O_TRACECLONE
197 # define PTRACE_O_TRACECLONE    0x00000008
198 #endif
199 #if !HAVE_DECL_PTRACE_O_TRACEEXEC
200 # define PTRACE_O_TRACEEXEC     0x00000010
201 #endif
202 #if !HAVE_DECL_PTRACE_O_TRACEEXIT
203 # define PTRACE_O_TRACEEXIT     0x00000040
204 #endif
205
206 #if !HAVE_DECL_PTRACE_EVENT_FORK
207 # define PTRACE_EVENT_FORK      1
208 #endif
209 #if !HAVE_DECL_PTRACE_EVENT_VFORK
210 # define PTRACE_EVENT_VFORK     2
211 #endif
212 #if !HAVE_DECL_PTRACE_EVENT_CLONE
213 # define PTRACE_EVENT_CLONE     3
214 #endif
215 #if !HAVE_DECL_PTRACE_EVENT_EXEC
216 # define PTRACE_EVENT_EXEC      4
217 #endif
218 #if !HAVE_DECL_PTRACE_EVENT_VFORK_DONE
219 # define PTRACE_EVENT_VFORK_DONE        5
220 #endif
221 #if !HAVE_DECL_PTRACE_EVENT_EXIT
222 # define PTRACE_EVENT_EXIT      6
223 #endif
224
225 #if !defined(__GLIBC__)
226 # define PTRACE_PEEKUSER PTRACE_PEEKUSR
227 # define PTRACE_POKEUSER PTRACE_POKEUSR
228 #endif
229
230 #if USE_SEIZE
231 # undef PTRACE_SEIZE
232 # define PTRACE_SEIZE           0x4206
233 # undef PTRACE_INTERRUPT
234 # define PTRACE_INTERRUPT       0x4207
235 # undef PTRACE_LISTEN
236 # define PTRACE_LISTEN          0x4208
237 # undef PTRACE_EVENT_STOP
238 # define PTRACE_EVENT_STOP      128
239 #endif
240
241 #ifdef ALPHA
242 # define REG_R0 0
243 # define REG_A0 16
244 # define REG_A3 19
245 # define REG_FP 30
246 # define REG_PC 64
247 #endif /* ALPHA */
248 #ifdef MIPS
249 # define REG_V0 2
250 # define REG_A0 4
251 # define REG_A3 7
252 # define REG_SP 29
253 # define REG_EPC 64
254 #endif /* MIPS */
255 #ifdef HPPA
256 # define PT_GR20 (20*4)
257 # define PT_GR26 (26*4)
258 # define PT_GR28 (28*4)
259 # define PT_IAOQ0 (106*4)
260 # define PT_IAOQ1 (107*4)
261 #endif /* HPPA */
262 #ifdef SH64
263    /* SH64 Linux - this code assumes the following kernel API for system calls:
264           PC           Offset 0
265           System Call  Offset 16 (actually, (syscall no.) | (0x1n << 16),
266                        where n = no. of parameters.
267           Other regs   Offset 24+
268
269           On entry:    R2-7 = parameters 1-6 (as many as necessary)
270           On return:   R9   = result. */
271
272    /* Offset for peeks of registers */
273 # define REG_OFFSET         (24)
274 # define REG_GENERAL(x)     (8*(x)+REG_OFFSET)
275 # define REG_PC             (0*8)
276 # define REG_SYSCALL        (2*8)
277 #endif /* SH64 */
278 #ifdef AARCH64
279 struct arm_pt_regs {
280         int uregs[18];
281 };
282 # define ARM_cpsr       uregs[16]
283 # define ARM_pc         uregs[15]
284 # define ARM_lr         uregs[14]
285 # define ARM_sp         uregs[13]
286 # define ARM_ip         uregs[12]
287 # define ARM_fp         uregs[11]
288 # define ARM_r10        uregs[10]
289 # define ARM_r9         uregs[9]
290 # define ARM_r8         uregs[8]
291 # define ARM_r7         uregs[7]
292 # define ARM_r6         uregs[6]
293 # define ARM_r5         uregs[5]
294 # define ARM_r4         uregs[4]
295 # define ARM_r3         uregs[3]
296 # define ARM_r2         uregs[2]
297 # define ARM_r1         uregs[1]
298 # define ARM_r0         uregs[0]
299 # define ARM_ORIG_r0    uregs[17]
300 #endif /* AARCH64 */
301
302 #if defined(SPARC) || defined(SPARC64)
303 /* Indexes into the pt_regs.u_reg[] array -- UREG_XX from kernel are all off
304  * by 1 and use Ix instead of Ox.  These work for both 32 and 64 bit Linux. */
305 # define U_REG_G1 0
306 # define U_REG_O0 7
307 # define U_REG_O1 8
308 # define PERSONALITY0_WORDSIZE 4
309 # define PERSONALITY1_WORDSIZE 4
310 # if defined(SPARC64)
311 #  include <asm/psrcompat.h>
312 #  define SUPPORTED_PERSONALITIES 3
313 #  define PERSONALITY2_WORDSIZE 8
314 # else
315 #  include <asm/psr.h>
316 #  define SUPPORTED_PERSONALITIES 2
317 # endif /* SPARC64 */
318 #endif /* SPARC[64] */
319
320 #ifdef X86_64
321 # define SUPPORTED_PERSONALITIES 3
322 # define PERSONALITY0_WORDSIZE 8
323 # define PERSONALITY1_WORDSIZE 4
324 # define PERSONALITY2_WORDSIZE 4
325 #endif
326
327 #ifdef X32
328 # define SUPPORTED_PERSONALITIES 2
329 # define PERSONALITY0_WORDSIZE 4
330 # define PERSONALITY1_WORDSIZE 4
331 #endif
332
333 #ifdef ARM
334 /* one personality */
335 #endif
336
337 #ifdef AARCH64
338 /* The existing ARM personality, then AArch64 */
339 # define SUPPORTED_PERSONALITIES 2
340 # define PERSONALITY0_WORDSIZE 4
341 # define PERSONALITY1_WORDSIZE 8
342 # define DEFAULT_PERSONALITY 1
343 #endif
344
345 #ifdef POWERPC64
346 # define SUPPORTED_PERSONALITIES 2
347 # define PERSONALITY0_WORDSIZE 8
348 # define PERSONALITY1_WORDSIZE 4
349 #endif
350
351 #ifdef TILE
352 # define SUPPORTED_PERSONALITIES 2
353 # define PERSONALITY0_WORDSIZE 8
354 # define PERSONALITY1_WORDSIZE 4
355 # ifdef __tilepro__
356 #  define DEFAULT_PERSONALITY 1
357 # endif
358 #endif
359
360 #ifndef SUPPORTED_PERSONALITIES
361 # define SUPPORTED_PERSONALITIES 1
362 #endif
363 #ifndef DEFAULT_PERSONALITY
364 # define DEFAULT_PERSONALITY 0
365 #endif
366 #ifndef PERSONALITY0_WORDSIZE
367 # define PERSONALITY0_WORDSIZE (int)(sizeof(long))
368 #endif
369
370 #if defined(I386)
371 extern struct user_regs_struct i386_regs;
372 #elif defined(IA64)
373 extern long ia32;
374 #elif defined(SPARC) || defined(SPARC64)
375 extern struct pt_regs sparc_regs;
376 #elif defined(ARM)
377 extern struct pt_regs arm_regs;
378 #elif defined(TILE)
379 extern struct pt_regs tile_regs;
380 #endif
381
382 typedef struct sysent {
383         unsigned nargs;
384         int     sys_flags;
385         int     (*sys_func)();
386         const char *sys_name;
387 } struct_sysent;
388
389 typedef struct ioctlent {
390         const char *doth;
391         const char *symbol;
392         unsigned long code;
393 } struct_ioctlent;
394
395 /* Trace Control Block */
396 struct tcb {
397         int flags;              /* See below for TCB_ values */
398         int pid;                /* Process Id of this entry */
399         int qual_flg;           /* qual_flags[scno] or DEFAULT_QUAL_FLAGS + RAW */
400         int u_error;            /* Error code */
401         long scno;              /* System call number */
402         long u_arg[MAX_ARGS];   /* System call arguments */
403 #if defined(LINUX_MIPSN32) || defined(X32)
404         long long ext_arg[MAX_ARGS];
405         long long u_lrval;      /* long long return value */
406 #endif
407         long u_rval;            /* Return value */
408 #if SUPPORTED_PERSONALITIES > 1
409         int currpers;           /* Personality at the time of scno update */
410 #endif
411         int curcol;             /* Output column for this process */
412         FILE *outf;             /* Output file for this process */
413         const char *auxstr;     /* Auxiliary info from syscall (see RVAL_STR) */
414         const struct_sysent *s_ent; /* sysent[scno] or dummy struct for bad scno */
415         struct timeval stime;   /* System time usage as of last process wait */
416         struct timeval dtime;   /* Delta for system time usage */
417         struct timeval etime;   /* Syscall entry time */
418                                 /* Support for tracing forked processes: */
419         long inst[2];           /* Saved clone args (badly named) */
420 };
421
422 /* TCB flags */
423 #define TCB_INUSE               00001   /* This table entry is in use */
424 /* We have attached to this process, but did not see it stopping yet */
425 #define TCB_STARTUP             00002
426 #define TCB_IGNORE_ONE_SIGSTOP  00004   /* Next SIGSTOP is to be ignored */
427 /*
428  * Are we in system call entry or in syscall exit?
429  *
430  * This bit is set after all syscall entry processing is done.
431  * Therefore, this bit will be set when next ptrace stop occurs,
432  * which should be syscall exit stop. Other stops which are possible
433  * directly after syscall entry (death, ptrace event stop)
434  * are simpler and handled without calling trace_syscall(), therefore
435  * the places where TCB_INSYSCALL can be set but we aren't in syscall stop
436  * are limited to trace(), this condition is never observed in trace_syscall()
437  * and below.
438  * The bit is cleared after all syscall exit processing is done.
439  * User-generated SIGTRAPs and post-execve SIGTRAP make it necessary
440  * to be very careful and NOT set TCB_INSYSCALL bit when they are encountered.
441  * TCB_WAITEXECVE bit is used for this purpose (see below).
442  *
443  * Use entering(tcp) / exiting(tcp) to check this bit to make code more readable.
444  */
445 #define TCB_INSYSCALL   00010
446 #define TCB_ATTACHED    00020   /* It is attached already */
447 /* Are we PROG from "strace PROG [ARGS]" invocation? */
448 #define TCB_STRACE_CHILD 0040
449 #define TCB_BPTSET      00100   /* "Breakpoint" set after fork(2) */
450 #define TCB_REPRINT     00200   /* We should reprint this syscall on exit */
451 #define TCB_FILTERED    00400   /* This system call has been filtered out */
452 /* x86 does not need TCB_WAITEXECVE.
453  * It can detect post-execve SIGTRAP by looking at eax/rax.
454  * See "not a syscall entry (eax = %ld)\n" message.
455  *
456  * Note! On new kernels (about 2.5.46+), we use PTRACE_O_TRACEEXEC, which
457  * suppresses post-execve SIGTRAP. If you are adding a new arch which is
458  * only supported by newer kernels, you most likely don't need to define
459  * TCB_WAITEXECVE!
460  */
461 #if defined(ALPHA) \
462  || defined(SPARC) || defined(SPARC64) \
463  || defined(POWERPC) \
464  || defined(IA64) \
465  || defined(HPPA) \
466  || defined(SH) || defined(SH64) \
467  || defined(S390) || defined(S390X) \
468  || defined(ARM) \
469  || defined(MIPS)
470 /* This tracee has entered into execve syscall. Expect post-execve SIGTRAP
471  * to happen. (When it is detected, tracee is continued and this bit is cleared.)
472  */
473 # define TCB_WAITEXECVE 01000
474 #endif
475
476 /* qualifier flags */
477 #define QUAL_TRACE      0x001   /* this system call should be traced */
478 #define QUAL_ABBREV     0x002   /* abbreviate the structures of this syscall */
479 #define QUAL_VERBOSE    0x004   /* decode the structures of this syscall */
480 #define QUAL_RAW        0x008   /* print all args in hex for this syscall */
481 #define QUAL_SIGNAL     0x010   /* report events with this signal */
482 #define QUAL_READ       0x020   /* dump data read on this file descriptor */
483 #define QUAL_WRITE      0x040   /* dump data written to this file descriptor */
484 typedef uint8_t qualbits_t;
485 #define UNDEFINED_SCNO  0x100   /* Used only in tcp->qual_flg */
486
487 #define DEFAULT_QUAL_FLAGS (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
488
489 #define entering(tcp)   (!((tcp)->flags & TCB_INSYSCALL))
490 #define exiting(tcp)    ((tcp)->flags & TCB_INSYSCALL)
491 #define syserror(tcp)   ((tcp)->u_error != 0)
492 #define verbose(tcp)    ((tcp)->qual_flg & QUAL_VERBOSE)
493 #define abbrev(tcp)     ((tcp)->qual_flg & QUAL_ABBREV)
494 #define filtered(tcp)   ((tcp)->flags & TCB_FILTERED)
495
496 struct xlat {
497         int val;
498         const char *str;
499 };
500
501 extern const struct xlat open_mode_flags[];
502 extern const struct xlat addrfams[];
503 extern const struct xlat struct_user_offsets[];
504 extern const struct xlat open_access_modes[];
505 extern const struct xlat whence_codes[];
506
507 /* Format of syscall return values */
508 #define RVAL_DECIMAL    000     /* decimal format */
509 #define RVAL_HEX        001     /* hex format */
510 #define RVAL_OCTAL      002     /* octal format */
511 #define RVAL_UDECIMAL   003     /* unsigned decimal format */
512 #if defined(LINUX_MIPSN32) || defined(X32)
513 # if 0 /* unused so far */
514 #  define RVAL_LDECIMAL 004     /* long decimal format */
515 #  define RVAL_LHEX     005     /* long hex format */
516 #  define RVAL_LOCTAL   006     /* long octal format */
517 # endif
518 # define RVAL_LUDECIMAL 007     /* long unsigned decimal format */
519 #endif
520 #define RVAL_MASK       007     /* mask for these values */
521
522 #define RVAL_STR        010     /* Print `auxstr' field after return val */
523 #define RVAL_NONE       020     /* Print nothing */
524
525 #define TRACE_FILE      001     /* Trace file-related syscalls. */
526 #define TRACE_IPC       002     /* Trace IPC-related syscalls. */
527 #define TRACE_NETWORK   004     /* Trace network-related syscalls. */
528 #define TRACE_PROCESS   010     /* Trace process-related syscalls. */
529 #define TRACE_SIGNAL    020     /* Trace signal-related syscalls. */
530 #define TRACE_DESC      040     /* Trace file descriptor-related syscalls. */
531 #define TRACE_MEMORY    0100    /* Trace memory mapping-related syscalls. */
532 #define SYSCALL_NEVER_FAILS     0200    /* Syscall is always successful. */
533
534 typedef enum {
535         CFLAG_NONE = 0,
536         CFLAG_ONLY_STATS,
537         CFLAG_BOTH
538 } cflag_t;
539 extern cflag_t cflag;
540 extern bool debug_flag;
541 extern bool Tflag;
542 extern unsigned int qflag;
543 extern bool not_failing_only;
544 extern bool show_fd_path;
545 extern bool hide_log_until_execve;
546 /* are we filtering traces based on paths? */
547 extern const char **paths_selected;
548 #define tracing_paths (paths_selected != NULL)
549 extern bool need_fork_exec_workarounds;
550 extern unsigned xflag;
551 extern unsigned followfork;
552 extern unsigned ptrace_setoptions;
553 extern unsigned max_strlen;
554 extern unsigned os_release;
555 #undef KERNEL_VERSION
556 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
557
558 enum bitness_t { BITNESS_CURRENT = 0, BITNESS_32 };
559
560 void error_msg(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
561 void perror_msg(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
562 void error_msg_and_die(const char *fmt, ...) __attribute__ ((noreturn, format(printf, 1, 2)));
563 void perror_msg_and_die(const char *fmt, ...) __attribute__ ((noreturn, format(printf, 1, 2)));
564 void die_out_of_memory(void) __attribute__ ((noreturn));
565
566 #ifdef USE_CUSTOM_PRINTF
567 /*
568  * Speed-optimized vfprintf implementation.
569  * See comment in vsprintf.c for allowed formats.
570  * Short version: %h[h]u, %zu, %tu are not allowed, use %[l[l]]u.
571  *
572  * It results in strace using about 5% less CPU in user space
573  * (compared to glibc version).
574  * But strace spends a lot of time in kernel space,
575  * so overall it does not appear to be a significant win.
576  * Thus disabled by default.
577  */
578 int strace_vfprintf(FILE *fp, const char *fmt, va_list args);
579 #else
580 # define strace_vfprintf vfprintf
581 #endif
582
583 extern void set_sortby(const char *);
584 extern void set_overhead(int);
585 extern void qualify(const char *);
586 extern int trace_syscall(struct tcb *);
587 extern void count_syscall(struct tcb *, struct timeval *);
588 extern void call_summary(FILE *);
589
590 #if defined(AVR32) \
591  || defined(I386) \
592  || defined(X86_64) || defined(X32) \
593  || defined(AARCH64) \
594  || defined(ARM) \
595  || defined(SPARC) || defined(SPARC64) \
596  || defined(TILE) \
597  || defined(OR1K) \
598  || defined(METAG)
599 extern long get_regs_error;
600 # define clear_regs()  (get_regs_error = -1)
601 extern void get_regs(pid_t pid);
602 #else
603 # define get_regs_error 0
604 # define clear_regs()  ((void)0)
605 # define get_regs(pid) ((void)0)
606 #endif
607 extern int umoven(struct tcb *, long, int, char *);
608 #define umove(pid, addr, objp)  \
609         umoven((pid), (addr), sizeof(*(objp)), (char *) (objp))
610 extern int umovestr(struct tcb *, long, int, char *);
611 extern int upeek(struct tcb *, long, long *);
612 #if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
613 extern long getrval2(struct tcb *);
614 #endif
615 /*
616  * On Linux, "setbpt" is a misnomer: we don't set a breakpoint
617  * (IOW: no poking in user's text segment),
618  * instead we change fork/vfork/clone into clone(CLONE_PTRACE).
619  * On newer kernels, we use PTRACE_O_TRACECLONE/TRACE[V]FORK instead.
620  */
621 extern int setbpt(struct tcb *);
622 extern int clearbpt(struct tcb *);
623
624 extern const char *signame(int);
625 extern int is_restart_error(struct tcb *);
626 extern void pathtrace_select(const char *);
627 extern int pathtrace_match(struct tcb *);
628 extern int getfdpath(struct tcb *, int, char *, unsigned);
629
630 extern const char *xlookup(const struct xlat *, int);
631
632 extern int string_to_uint(const char *str);
633 extern int string_quote(const char *, char *, long, int);
634
635 /* a refers to the lower numbered u_arg,
636  * b refers to the higher numbered u_arg
637  */
638 #if HAVE_LITTLE_ENDIAN_LONG_LONG
639 # define LONG_LONG(a,b) \
640         ((long long)((unsigned long long)(unsigned)(a) | ((unsigned long long)(b)<<32)))
641 #else
642 # define LONG_LONG(a,b) \
643         ((long long)((unsigned long long)(unsigned)(b) | ((unsigned long long)(a)<<32)))
644 #endif
645 extern int printllval(struct tcb *, const char *, int);
646
647 extern void printxval(const struct xlat *, int, const char *);
648 extern int printargs(struct tcb *);
649 extern int printargs_lu(struct tcb *);
650 extern int printargs_ld(struct tcb *);
651 extern void addflags(const struct xlat *, int);
652 extern int printflags(const struct xlat *, int, const char *);
653 extern const char *sprintflags(const char *, const struct xlat *, int);
654 extern void dumpiov(struct tcb *, int, long);
655 extern void dumpstr(struct tcb *, long, int);
656 extern void printstr(struct tcb *, long, long);
657 extern void printnum(struct tcb *, long, const char *);
658 extern void printnum_int(struct tcb *, long, const char *);
659 extern void printpath(struct tcb *, long);
660 extern void printpathn(struct tcb *, long, int);
661 #define TIMESPEC_TEXT_BUFSIZE (sizeof(long)*3 * 2 + sizeof("{%u, %u}"))
662 #define TIMEVAL_TEXT_BUFSIZE  TIMESPEC_TEXT_BUFSIZE
663 extern void printtv_bitness(struct tcb *, long, enum bitness_t, int);
664 #define printtv(tcp, addr)      \
665         printtv_bitness((tcp), (addr), BITNESS_CURRENT, 0)
666 #define printtv_special(tcp, addr)      \
667         printtv_bitness((tcp), (addr), BITNESS_CURRENT, 1)
668 extern char *sprinttv(char *, struct tcb *, long, enum bitness_t, int special);
669 extern void print_timespec(struct tcb *, long);
670 extern void sprint_timespec(char *, struct tcb *, long);
671 #ifdef HAVE_SIGINFO_T
672 extern void printsiginfo(siginfo_t *, int);
673 extern void printsiginfo_at(struct tcb *tcp, long addr);
674 #endif
675 extern void printfd(struct tcb *, int);
676 extern void printsock(struct tcb *, long, int);
677 extern void print_sock_optmgmt(struct tcb *, long, int);
678 extern void printrusage(struct tcb *, long);
679 #ifdef ALPHA
680 extern void printrusage32(struct tcb *, long);
681 #endif
682 extern void printuid(const char *, unsigned long);
683 extern void printcall(struct tcb *);
684 extern void print_sigset(struct tcb *, long, int);
685 extern void printsignal(int);
686 extern void tprint_iov(struct tcb *, unsigned long, unsigned long, int decode_iov);
687 extern void tprint_iov_upto(struct tcb *, unsigned long, unsigned long, int decode_iov, unsigned long);
688 extern void tprint_open_modes(mode_t);
689 extern const char *sprint_open_modes(mode_t);
690 extern void print_loff_t(struct tcb *, long);
691
692 extern const struct_ioctlent *ioctl_lookup(long);
693 extern const struct_ioctlent *ioctl_next_match(const struct_ioctlent *);
694 extern int ioctl_decode(struct tcb *, long, long);
695 extern int term_ioctl(struct tcb *, long, long);
696 extern int sock_ioctl(struct tcb *, long, long);
697 extern int proc_ioctl(struct tcb *, int, int);
698 extern int rtc_ioctl(struct tcb *, long, long);
699 extern int scsi_ioctl(struct tcb *, long, long);
700 extern int block_ioctl(struct tcb *, long, long);
701 extern int mtd_ioctl(struct tcb *, long, long);
702 extern int ubi_ioctl(struct tcb *, long, long);
703 extern int loop_ioctl(struct tcb *, long, long);
704
705 extern int tv_nz(struct timeval *);
706 extern int tv_cmp(struct timeval *, struct timeval *);
707 extern double tv_float(struct timeval *);
708 extern void tv_add(struct timeval *, struct timeval *, struct timeval *);
709 extern void tv_sub(struct timeval *, struct timeval *, struct timeval *);
710 extern void tv_mul(struct timeval *, struct timeval *, int);
711 extern void tv_div(struct timeval *, struct timeval *, int);
712
713 /* Strace log generation machinery.
714  *
715  * printing_tcp: tcb which has incomplete line being printed right now.
716  * NULL if last line has been completed ('\n'-terminated).
717  * printleader(tcp) examines it, finishes incomplete line if needed,
718  * the sets it to tcp.
719  * line_ended() clears printing_tcp and resets ->curcol = 0.
720  * tcp->curcol == 0 check is also used to detect completeness
721  * of last line, since in -ff mode just checking printing_tcp for NULL
722  * is not enough.
723  *
724  * If you change this code, test log generation in both -f and -ff modes
725  * using:
726  * strace -oLOG -f[f] test/threaded_execve
727  * strace -oLOG -f[f] test/sigkill_rain
728  * strace -oLOG -f[f] -p "`pidof web_browser`"
729  */
730 extern struct tcb *printing_tcp;
731 extern void printleader(struct tcb *);
732 extern void line_ended(void);
733 extern void tabto(void);
734 extern void tprintf(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
735 extern void tprints(const char *str);
736
737 #if SUPPORTED_PERSONALITIES > 1
738 extern void set_personality(int personality);
739 extern unsigned current_personality;
740 #else
741 # define set_personality(personality) ((void)0)
742 # define current_personality 0
743 #endif
744
745 #if SUPPORTED_PERSONALITIES == 1
746 # define current_wordsize PERSONALITY0_WORDSIZE
747 #else
748 # if SUPPORTED_PERSONALITIES == 2 && PERSONALITY0_WORDSIZE == PERSONALITY1_WORDSIZE
749 #  define current_wordsize PERSONALITY0_WORDSIZE
750 # else
751 extern unsigned current_wordsize;
752 # endif
753 #endif
754
755 /* In many, many places we play fast and loose and use
756  * tprintf("%d", (int) tcp->u_arg[N]) to print fds, pids etc.
757  * We probably need to use widen_to_long() instead:
758  */
759 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
760 # define widen_to_long(v) (current_wordsize == 4 ? (long)(int32_t)(v) : (long)(v))
761 #else
762 # define widen_to_long(v) ((long)(v))
763 #endif
764
765 extern const struct_sysent sysent0[];
766 extern const char *const errnoent0[];
767 extern const char *const signalent0[];
768 extern const struct_ioctlent ioctlent0[];
769 extern qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
770 #define qual_flags (qual_vec[current_personality])
771 #if SUPPORTED_PERSONALITIES > 1
772 extern const struct_sysent *sysent;
773 extern const char *const *errnoent;
774 extern const char *const *signalent;
775 extern const struct_ioctlent *ioctlent;
776 #else
777 # define sysent     sysent0
778 # define errnoent   errnoent0
779 # define signalent  signalent0
780 # define ioctlent   ioctlent0
781 #endif
782 extern unsigned nsyscalls;
783 extern unsigned nerrnos;
784 extern unsigned nsignals;
785 extern unsigned nioctlents;
786 extern unsigned num_quals;
787
788 /*
789  * If you need non-NULL sysent[scno].sys_func and sysent[scno].sys_name
790  */
791 #define SCNO_IS_VALID(scno) \
792         ((unsigned long)(scno) < nsyscalls && sysent[scno].sys_func)
793
794 /* Only ensures that sysent[scno] isn't out of range */
795 #define SCNO_IN_RANGE(scno) \
796         ((unsigned long)(scno) < nsyscalls)