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