]> granicus.if.org Git - strace/blob - syscall.c
poll: change address argument type from long to kernel_ureg_t
[strace] / syscall.c
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  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7  *                     Linux for s390 port by D.J. Barrow
8  *                    <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include "defs.h"
35 #include "native_defs.h"
36 #include <sys/param.h>
37 #include <signal.h>
38
39 /* for struct iovec */
40 #include <sys/uio.h>
41
42 #include "regs.h"
43 #include "ptrace.h"
44
45 #if defined(SPARC64)
46 # undef PTRACE_GETREGS
47 # define PTRACE_GETREGS PTRACE_GETREGS64
48 # undef PTRACE_SETREGS
49 # define PTRACE_SETREGS PTRACE_SETREGS64
50 #endif
51
52 #if defined SPARC64
53 # include <asm/psrcompat.h>
54 #elif defined SPARC
55 # include <asm/psr.h>
56 #endif
57
58 #ifdef IA64
59 # include <asm/rse.h>
60 #endif
61
62 #ifndef NT_PRSTATUS
63 # define NT_PRSTATUS 1
64 #endif
65
66 #ifndef NSIG
67 # warning: NSIG is not defined, using 32
68 # define NSIG 32
69 #endif
70
71 #include "syscall.h"
72
73 /* Define these shorthand notations to simplify the syscallent files. */
74 #define TD TRACE_DESC
75 #define TF TRACE_FILE
76 #define TI TRACE_IPC
77 #define TN TRACE_NETWORK
78 #define TP TRACE_PROCESS
79 #define TS TRACE_SIGNAL
80 #define TM TRACE_MEMORY
81 #define NF SYSCALL_NEVER_FAILS
82 #define MA MAX_ARGS
83 #define SI STACKTRACE_INVALIDATE_CACHE
84 #define SE STACKTRACE_CAPTURE_ON_ENTER
85 #define CST COMPAT_SYSCALL_TYPES
86
87 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
88
89 const struct_sysent sysent0[] = {
90 #include "syscallent.h"
91 };
92
93 #if SUPPORTED_PERSONALITIES > 1
94 # include PERSONALITY1_INCLUDE_FUNCS
95 static const struct_sysent sysent1[] = {
96 # include "syscallent1.h"
97 };
98 #endif
99
100 #if SUPPORTED_PERSONALITIES > 2
101 # include PERSONALITY2_INCLUDE_FUNCS
102 static const struct_sysent sysent2[] = {
103 # include "syscallent2.h"
104 };
105 #endif
106
107 /* Now undef them since short defines cause wicked namespace pollution. */
108 #undef SEN
109 #undef TD
110 #undef TF
111 #undef TI
112 #undef TN
113 #undef TP
114 #undef TS
115 #undef TM
116 #undef NF
117 #undef MA
118 #undef SI
119 #undef SE
120 #undef CST
121
122 /*
123  * `ioctlent[012].h' files are automatically generated by the auxiliary
124  * program `ioctlsort', such that the list is sorted by the `code' field.
125  * This has the side-effect of resolving the _IO.. macros into
126  * plain integers, eliminating the need to include here everything
127  * in "/usr/include".
128  */
129
130 const char *const errnoent0[] = {
131 #include "errnoent.h"
132 };
133 const char *const signalent0[] = {
134 #include "signalent.h"
135 };
136 const struct_ioctlent ioctlent0[] = {
137 #include "ioctlent0.h"
138 };
139
140 #if SUPPORTED_PERSONALITIES > 1
141 static const char *const errnoent1[] = {
142 # include "errnoent1.h"
143 };
144 static const char *const signalent1[] = {
145 # include "signalent1.h"
146 };
147 static const struct_ioctlent ioctlent1[] = {
148 # include "ioctlent1.h"
149 };
150 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
151 static const struct_printers printers0 = {
152 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
153 };
154 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
155 static const struct_printers printers1 = {
156 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
157 };
158 #endif
159
160 #if SUPPORTED_PERSONALITIES > 2
161 static const char *const errnoent2[] = {
162 # include "errnoent2.h"
163 };
164 static const char *const signalent2[] = {
165 # include "signalent2.h"
166 };
167 static const struct_ioctlent ioctlent2[] = {
168 # include "ioctlent2.h"
169 };
170 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
171 static const struct_printers printers2 = {
172 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
173 };
174 #endif
175
176 enum {
177         nsyscalls0 = ARRAY_SIZE(sysent0)
178 #if SUPPORTED_PERSONALITIES > 1
179         , nsyscalls1 = ARRAY_SIZE(sysent1)
180 # if SUPPORTED_PERSONALITIES > 2
181         , nsyscalls2 = ARRAY_SIZE(sysent2)
182 # endif
183 #endif
184 };
185
186 enum {
187         nerrnos0 = ARRAY_SIZE(errnoent0)
188 #if SUPPORTED_PERSONALITIES > 1
189         , nerrnos1 = ARRAY_SIZE(errnoent1)
190 # if SUPPORTED_PERSONALITIES > 2
191         , nerrnos2 = ARRAY_SIZE(errnoent2)
192 # endif
193 #endif
194 };
195
196 enum {
197         nsignals0 = ARRAY_SIZE(signalent0)
198 #if SUPPORTED_PERSONALITIES > 1
199         , nsignals1 = ARRAY_SIZE(signalent1)
200 # if SUPPORTED_PERSONALITIES > 2
201         , nsignals2 = ARRAY_SIZE(signalent2)
202 # endif
203 #endif
204 };
205
206 enum {
207         nioctlents0 = ARRAY_SIZE(ioctlent0)
208 #if SUPPORTED_PERSONALITIES > 1
209         , nioctlents1 = ARRAY_SIZE(ioctlent1)
210 # if SUPPORTED_PERSONALITIES > 2
211         , nioctlents2 = ARRAY_SIZE(ioctlent2)
212 # endif
213 #endif
214 };
215
216 #if SUPPORTED_PERSONALITIES > 1
217 const struct_sysent *sysent = sysent0;
218 const char *const *errnoent = errnoent0;
219 const char *const *signalent = signalent0;
220 const struct_ioctlent *ioctlent = ioctlent0;
221 const struct_printers *printers = &printers0;
222 #endif
223
224 unsigned nsyscalls = nsyscalls0;
225 unsigned nerrnos = nerrnos0;
226 unsigned nsignals = nsignals0;
227 unsigned nioctlents = nioctlents0;
228
229 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
230         nsyscalls0,
231 #if SUPPORTED_PERSONALITIES > 1
232         nsyscalls1,
233 #endif
234 #if SUPPORTED_PERSONALITIES > 2
235         nsyscalls2,
236 #endif
237 };
238 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
239         sysent0,
240 #if SUPPORTED_PERSONALITIES > 1
241         sysent1,
242 #endif
243 #if SUPPORTED_PERSONALITIES > 2
244         sysent2,
245 #endif
246 };
247
248 #if SUPPORTED_PERSONALITIES > 1
249 unsigned current_personality;
250
251 # ifndef current_wordsize
252 unsigned current_wordsize;
253 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
254         PERSONALITY0_WORDSIZE,
255         PERSONALITY1_WORDSIZE,
256 # if SUPPORTED_PERSONALITIES > 2
257         PERSONALITY2_WORDSIZE,
258 # endif
259 };
260 # endif
261
262 # ifndef current_klongsize
263 unsigned current_klongsize;
264 static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
265         PERSONALITY0_KLONGSIZE,
266         PERSONALITY1_KLONGSIZE,
267 #  if SUPPORTED_PERSONALITIES > 2
268         PERSONALITY2_KLONGSIZE,
269 #  endif
270 };
271 # endif
272
273 void
274 set_personality(int personality)
275 {
276         nsyscalls = nsyscall_vec[personality];
277         sysent = sysent_vec[personality];
278
279         switch (personality) {
280         case 0:
281                 errnoent = errnoent0;
282                 nerrnos = nerrnos0;
283                 ioctlent = ioctlent0;
284                 nioctlents = nioctlents0;
285                 signalent = signalent0;
286                 nsignals = nsignals0;
287                 printers = &printers0;
288                 break;
289
290         case 1:
291                 errnoent = errnoent1;
292                 nerrnos = nerrnos1;
293                 ioctlent = ioctlent1;
294                 nioctlents = nioctlents1;
295                 signalent = signalent1;
296                 nsignals = nsignals1;
297                 printers = &printers1;
298                 break;
299
300 # if SUPPORTED_PERSONALITIES > 2
301         case 2:
302                 errnoent = errnoent2;
303                 nerrnos = nerrnos2;
304                 ioctlent = ioctlent2;
305                 nioctlents = nioctlents2;
306                 signalent = signalent2;
307                 nsignals = nsignals2;
308                 printers = &printers2;
309                 break;
310 # endif
311         }
312
313         current_personality = personality;
314 # ifndef current_wordsize
315         current_wordsize = personality_wordsize[personality];
316 # endif
317 # ifndef current_klongsize
318         current_klongsize = personality_klongsize[personality];
319 # endif
320 }
321
322 static void
323 update_personality(struct tcb *tcp, unsigned int personality)
324 {
325         if (personality == current_personality)
326                 return;
327         set_personality(personality);
328
329         if (personality == tcp->currpers)
330                 return;
331         tcp->currpers = personality;
332
333 # undef PERSONALITY_NAMES
334 # if defined POWERPC64
335 #  define PERSONALITY_NAMES {"64 bit", "32 bit"}
336 # elif defined X86_64
337 #  define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
338 # elif defined X32
339 #  define PERSONALITY_NAMES {"x32", "32 bit"}
340 # elif defined AARCH64
341 #  define PERSONALITY_NAMES {"64 bit", "32 bit"}
342 # elif defined TILE
343 #  define PERSONALITY_NAMES {"64-bit", "32-bit"}
344 # endif
345 # ifdef PERSONALITY_NAMES
346         if (!qflag) {
347                 static const char *const names[] = PERSONALITY_NAMES;
348                 error_msg("[ Process PID=%d runs in %s mode. ]",
349                           tcp->pid, names[personality]);
350         }
351 # endif
352 }
353 #endif
354
355 #ifdef SYS_socket_subcall
356 static void
357 decode_socket_subcall(struct tcb *tcp)
358 {
359         const int call = tcp->u_arg[0];
360
361         if (call < 1 || call >= SYS_socket_nsubcalls)
362                 return;
363
364         const kernel_scno_t scno = SYS_socket_subcall + call;
365         const unsigned int nargs = sysent[scno].nargs;
366         uint64_t buf[nargs];
367
368         if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
369                 return;
370
371         tcp->scno = scno;
372         tcp->qual_flg = qual_flags(scno);
373         tcp->s_ent = &sysent[scno];
374
375         unsigned int i;
376         for (i = 0; i < nargs; ++i)
377                 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
378                                 ? ((uint32_t *) (void *) buf)[i] : buf[i];
379 }
380 #endif
381
382 #ifdef SYS_ipc_subcall
383 static void
384 decode_ipc_subcall(struct tcb *tcp)
385 {
386         unsigned int call = tcp->u_arg[0];
387         const unsigned int version = call >> 16;
388
389         if (version) {
390 # if defined S390 || defined S390X
391                 return;
392 # else
393 #  ifdef SPARC64
394                 if (current_wordsize == 8)
395                         return;
396 #  endif
397                 set_tcb_priv_ulong(tcp, version);
398                 call &= 0xffff;
399 # endif
400         }
401
402         switch (call) {
403                 case  1: case  2: case  3: case  4:
404                 case 11: case 12: case 13: case 14:
405                 case 21: case 22: case 23: case 24:
406                         break;
407                 default:
408                         return;
409         }
410
411         tcp->scno = SYS_ipc_subcall + call;
412         tcp->qual_flg = qual_flags(tcp->scno);
413         tcp->s_ent = &sysent[tcp->scno];
414
415         const unsigned int n = tcp->s_ent->nargs;
416         unsigned int i;
417         for (i = 0; i < n; i++)
418                 tcp->u_arg[i] = tcp->u_arg[i + 1];
419 }
420 #endif
421
422 #ifdef LINUX_MIPSO32
423 static void
424 decode_mips_subcall(struct tcb *tcp)
425 {
426         if (!scno_is_valid(tcp->u_arg[0]))
427                 return;
428         tcp->scno = tcp->u_arg[0];
429         tcp->qual_flg = qual_flags(tcp->scno);
430         tcp->s_ent = &sysent[tcp->scno];
431         memmove(&tcp->u_arg[0], &tcp->u_arg[1],
432                 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
433         /*
434          * Fetching the last arg of 7-arg syscalls (fadvise64_64
435          * and sync_file_range) requires additional code,
436          * see linux/mips/get_syscall_args.c
437          */
438         if (tcp->s_ent->nargs == MAX_ARGS) {
439                 if (umoven(tcp,
440                            mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
441                            sizeof(tcp->u_arg[0]),
442                            &tcp->u_arg[MAX_ARGS - 1]) < 0)
443                 tcp->u_arg[MAX_ARGS - 1] = 0;
444         }
445 }
446 #endif /* LINUX_MIPSO32 */
447
448 static void
449 dumpio(struct tcb *tcp)
450 {
451         if (syserror(tcp))
452                 return;
453
454         int fd = tcp->u_arg[0];
455         if (fd < 0)
456                 return;
457
458         if (is_number_in_set(fd, &read_set)) {
459                 switch (tcp->s_ent->sen) {
460                 case SEN_read:
461                 case SEN_pread:
462                 case SEN_recv:
463                 case SEN_recvfrom:
464                 case SEN_mq_timedreceive:
465                         dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
466                         return;
467                 case SEN_readv:
468                 case SEN_preadv:
469                 case SEN_preadv2:
470                         dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
471                                      tcp->u_rval);
472                         return;
473                 case SEN_recvmsg:
474                         dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
475                         return;
476                 case SEN_recvmmsg:
477                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
478                         return;
479                 }
480         }
481         if (is_number_in_set(fd, &write_set)) {
482                 switch (tcp->s_ent->sen) {
483                 case SEN_write:
484                 case SEN_pwrite:
485                 case SEN_send:
486                 case SEN_sendto:
487                 case SEN_mq_timedsend:
488                         dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
489                         break;
490                 case SEN_writev:
491                 case SEN_pwritev:
492                 case SEN_pwritev2:
493                 case SEN_vmsplice:
494                         dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
495                         break;
496                 case SEN_sendmsg:
497                         dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1UL);
498                         break;
499                 case SEN_sendmmsg:
500                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
501                         break;
502                 }
503         }
504 }
505
506 /*
507  * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
508  * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
509  */
510 static kernel_scno_t
511 shuffle_scno(kernel_scno_t scno)
512 {
513 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
514         if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
515                 return scno;
516
517         /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
518         if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
519                 return 0x000ffff0;
520         if (scno == 0x000ffff0)
521                 return ARM_FIRST_SHUFFLED_SYSCALL;
522
523 # define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
524         /*
525          * Is it ARM specific syscall?
526          * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
527          * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
528          */
529         if (scno >= 0x000f0000 &&
530             scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
531                 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
532         }
533         if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
534                 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
535         }
536 #endif /* ARM || AARCH64 */
537
538         return scno;
539 }
540
541 const char *
542 err_name(unsigned long err)
543 {
544         if ((err < nerrnos) && errnoent[err])
545                 return errnoent[err];
546
547         return NULL;
548 }
549
550 static long get_regs_error;
551
552 void
553 clear_regs(void)
554 {
555         get_regs_error = -1;
556 }
557
558 static int get_syscall_args(struct tcb *);
559 static int get_syscall_result(struct tcb *);
560 static int arch_get_scno(struct tcb *tcp);
561 static int arch_set_scno(struct tcb *, kernel_scno_t);
562 static void get_error(struct tcb *, const bool);
563 static int arch_set_error(struct tcb *);
564
565 struct fault_opts *fault_vec[SUPPORTED_PERSONALITIES];
566
567 static struct fault_opts *
568 tcb_fault_opts(struct tcb *tcp)
569 {
570         return (scno_in_range(tcp->scno) && tcp->fault_vec[current_personality])
571                ? &tcp->fault_vec[current_personality][tcp->scno] : NULL;
572 }
573
574
575 static long
576 inject_syscall_fault_entering(struct tcb *tcp)
577 {
578         if (!tcp->fault_vec[current_personality]) {
579                 tcp->fault_vec[current_personality] =
580                         xcalloc(nsyscalls, sizeof(**fault_vec));
581                 memcpy(tcp->fault_vec[current_personality],
582                        fault_vec[current_personality],
583                        nsyscalls * sizeof(**fault_vec));
584         }
585
586         struct fault_opts *opts = tcb_fault_opts(tcp);
587
588         if (!opts || opts->first == 0)
589                 return 0;
590
591         --opts->first;
592
593         if (opts->first != 0)
594                 return 0;
595
596         opts->first = opts->step;
597
598         if (!arch_set_scno(tcp, -1))
599                 tcp->flags |= TCB_FAULT_INJ;
600
601         return 0;
602 }
603
604 static long
605 update_syscall_fault_exiting(struct tcb *tcp)
606 {
607         struct fault_opts *opts = tcb_fault_opts(tcp);
608
609         if (opts && opts->err && tcp->u_error != opts->err) {
610                 unsigned long u_error = tcp->u_error;
611                 tcp->u_error = opts->err;
612                 if (arch_set_error(tcp))
613                         tcp->u_error = u_error;
614         }
615
616         return 0;
617 }
618
619 static int
620 trace_syscall_entering(struct tcb *tcp)
621 {
622         int res, scno_good;
623
624         scno_good = res = get_scno(tcp);
625         if (res == 0)
626                 return res;
627         if (res == 1)
628                 res = get_syscall_args(tcp);
629
630         if (res != 1) {
631                 printleader(tcp);
632                 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
633                 /*
634                  * " <unavailable>" will be added later by the code which
635                  * detects ptrace errors.
636                  */
637                 goto ret;
638         }
639
640 #ifdef LINUX_MIPSO32
641         if (SEN_syscall == tcp->s_ent->sen)
642                 decode_mips_subcall(tcp);
643 #endif
644
645 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
646         switch (tcp->s_ent->sen) {
647 # ifdef SYS_socket_subcall
648                 case SEN_socketcall:
649                         decode_socket_subcall(tcp);
650                         break;
651 # endif
652 # ifdef SYS_ipc_subcall
653                 case SEN_ipc:
654                         decode_ipc_subcall(tcp);
655                         break;
656 # endif
657         }
658 #endif
659
660         /* Restrain from fault injection while the trace executes strace code. */
661         if (hide_log(tcp)) {
662                 tcp->qual_flg &= ~QUAL_FAULT;
663         }
664
665         switch (tcp->s_ent->sen) {
666                 case SEN_execve:
667                 case SEN_execveat:
668 #if defined SPARC || defined SPARC64
669                 case SEN_execv:
670 #endif
671                         tcp->flags &= ~TCB_HIDE_LOG;
672                         break;
673         }
674
675         if (!(tcp->qual_flg & QUAL_TRACE)
676          || (tracing_paths && !pathtrace_match(tcp))
677         ) {
678                 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
679                 tcp->sys_func_rval = 0;
680                 return 0;
681         }
682
683         tcp->flags &= ~TCB_FILTERED;
684
685         if (hide_log(tcp)) {
686                 res = 0;
687                 goto ret;
688         }
689
690         if (tcp->qual_flg & QUAL_FAULT)
691                 inject_syscall_fault_entering(tcp);
692
693         if (cflag == CFLAG_ONLY_STATS) {
694                 res = 0;
695                 goto ret;
696         }
697
698 #ifdef USE_LIBUNWIND
699         if (stack_trace_enabled) {
700                 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
701                         unwind_capture_stacktrace(tcp);
702         }
703 #endif
704
705         printleader(tcp);
706         tprintf("%s(", tcp->s_ent->sys_name);
707         if (tcp->qual_flg & QUAL_RAW)
708                 res = printargs(tcp);
709         else
710                 res = tcp->s_ent->sys_func(tcp);
711
712         fflush(tcp->outf);
713  ret:
714         tcp->flags |= TCB_INSYSCALL;
715         tcp->sys_func_rval = res;
716         /* Measure the entrance time as late as possible to avoid errors. */
717         if (Tflag || cflag)
718                 gettimeofday(&tcp->etime, NULL);
719         return res;
720 }
721
722 static bool
723 syscall_fault_injected(struct tcb *tcp)
724 {
725         return tcp->flags & TCB_FAULT_INJ;
726 }
727
728 static int
729 trace_syscall_exiting(struct tcb *tcp)
730 {
731         int sys_res;
732         struct timeval tv;
733         int res;
734         unsigned long u_error;
735         const char *u_error_str;
736
737         /* Measure the exit time as early as possible to avoid errors. */
738         if (Tflag || cflag)
739                 gettimeofday(&tv, NULL);
740
741 #ifdef USE_LIBUNWIND
742         if (stack_trace_enabled) {
743                 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
744                         unwind_cache_invalidate(tcp);
745         }
746 #endif
747
748 #if SUPPORTED_PERSONALITIES > 1
749         update_personality(tcp, tcp->currpers);
750 #endif
751         res = (get_regs_error ? -1 : get_syscall_result(tcp));
752         if (filtered(tcp) || hide_log(tcp))
753                 goto ret;
754
755         if (syserror(tcp) && syscall_fault_injected(tcp))
756                 update_syscall_fault_exiting(tcp);
757
758         if (cflag) {
759                 count_syscall(tcp, &tv);
760                 if (cflag == CFLAG_ONLY_STATS) {
761                         goto ret;
762                 }
763         }
764
765         /* If not in -ff mode, and printing_tcp != tcp,
766          * then the log currently does not end with output
767          * of _our syscall entry_, but with something else.
768          * We need to say which syscall's return is this.
769          *
770          * Forced reprinting via TCB_REPRINT is used only by
771          * "strace -ff -oLOG test/threaded_execve" corner case.
772          * It's the only case when -ff mode needs reprinting.
773          */
774         if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
775                 tcp->flags &= ~TCB_REPRINT;
776                 printleader(tcp);
777                 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
778         }
779         printing_tcp = tcp;
780
781         tcp->s_prev_ent = NULL;
782         if (res != 1) {
783                 /* There was error in one of prior ptrace ops */
784                 tprints(") ");
785                 tabto();
786                 tprints("= ? <unavailable>\n");
787                 line_ended();
788                 tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
789                 tcp->sys_func_rval = 0;
790                 free_tcb_priv_data(tcp);
791                 return res;
792         }
793         tcp->s_prev_ent = tcp->s_ent;
794
795         sys_res = 0;
796         if (tcp->qual_flg & QUAL_RAW) {
797                 /* sys_res = printargs(tcp); - but it's nop on sysexit */
798         } else {
799         /* FIXME: not_failing_only (IOW, option -z) is broken:
800          * failure of syscall is known only after syscall return.
801          * Thus we end up with something like this on, say, ENOENT:
802          *     open("doesnt_exist", O_RDONLY <unfinished ...>
803          *     {next syscall decode}
804          * whereas the intended result is that open(...) line
805          * is not shown at all.
806          */
807                 if (not_failing_only && tcp->u_error)
808                         goto ret;       /* ignore failed syscalls */
809                 if (tcp->sys_func_rval & RVAL_DECODED)
810                         sys_res = tcp->sys_func_rval;
811                 else
812                         sys_res = tcp->s_ent->sys_func(tcp);
813         }
814
815         tprints(") ");
816         tabto();
817         u_error = tcp->u_error;
818
819         if (tcp->qual_flg & QUAL_RAW) {
820                 if (u_error) {
821                         tprintf("= -1 (errno %lu)", u_error);
822                         if (syscall_fault_injected(tcp))
823                                 tprints(" (INJECTED)");
824                 } else {
825                         tprintf("= %#lx", tcp->u_rval);
826                 }
827         }
828         else if (!(sys_res & RVAL_NONE) && u_error) {
829                 switch (u_error) {
830                 /* Blocked signals do not interrupt any syscalls.
831                  * In this case syscalls don't return ERESTARTfoo codes.
832                  *
833                  * Deadly signals set to SIG_DFL interrupt syscalls
834                  * and kill the process regardless of which of the codes below
835                  * is returned by the interrupted syscall.
836                  * In some cases, kernel forces a kernel-generated deadly
837                  * signal to be unblocked and set to SIG_DFL (and thus cause
838                  * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
839                  * or SIGILL. (The alternative is to leave process spinning
840                  * forever on the faulty instruction - not useful).
841                  *
842                  * SIG_IGNed signals and non-deadly signals set to SIG_DFL
843                  * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
844                  * but kernel will always restart them.
845                  */
846                 case ERESTARTSYS:
847                         /* Most common type of signal-interrupted syscall exit code.
848                          * The system call will be restarted with the same arguments
849                          * if SA_RESTART is set; otherwise, it will fail with EINTR.
850                          */
851                         tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
852                         break;
853                 case ERESTARTNOINTR:
854                         /* Rare. For example, fork() returns this if interrupted.
855                          * SA_RESTART is ignored (assumed set): the restart is unconditional.
856                          */
857                         tprints("= ? ERESTARTNOINTR (To be restarted)");
858                         break;
859                 case ERESTARTNOHAND:
860                         /* pause(), rt_sigsuspend() etc use this code.
861                          * SA_RESTART is ignored (assumed not set):
862                          * syscall won't restart (will return EINTR instead)
863                          * even after signal with SA_RESTART set. However,
864                          * after SIG_IGN or SIG_DFL signal it will restart
865                          * (thus the name "restart only if has no handler").
866                          */
867                         tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
868                         break;
869                 case ERESTART_RESTARTBLOCK:
870                         /* Syscalls like nanosleep(), poll() which can't be
871                          * restarted with their original arguments use this
872                          * code. Kernel will execute restart_syscall() instead,
873                          * which changes arguments before restarting syscall.
874                          * SA_RESTART is ignored (assumed not set) similarly
875                          * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
876                          * since restart data is saved in "restart block"
877                          * in task struct, and if signal handler uses a syscall
878                          * which in turn saves another such restart block,
879                          * old data is lost and restart becomes impossible)
880                          */
881                         tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
882                         break;
883                 default:
884                         u_error_str = err_name(u_error);
885                         if (u_error_str)
886                                 tprintf("= -1 %s (%s)",
887                                         u_error_str, strerror(u_error));
888                         else
889                                 tprintf("= -1 %lu (%s)",
890                                         u_error, strerror(u_error));
891                         break;
892                 }
893                 if (syscall_fault_injected(tcp))
894                         tprintf(" (INJECTED)");
895                 if ((sys_res & RVAL_STR) && tcp->auxstr)
896                         tprintf(" (%s)", tcp->auxstr);
897         }
898         else {
899                 if (sys_res & RVAL_NONE)
900                         tprints("= ?");
901                 else {
902                         switch (sys_res & RVAL_MASK) {
903                         case RVAL_HEX:
904 #if SUPPORTED_PERSONALITIES > 1
905                                 if (current_wordsize < sizeof(long))
906                                         tprintf("= %#x",
907                                                 (unsigned int) tcp->u_rval);
908                                 else
909 #endif
910                                         tprintf("= %#lx", tcp->u_rval);
911                                 break;
912                         case RVAL_OCTAL:
913                                 tprints("= ");
914                                 print_numeric_long_umask(tcp->u_rval);
915                                 break;
916                         case RVAL_UDECIMAL:
917 #if SUPPORTED_PERSONALITIES > 1
918                                 if (current_wordsize < sizeof(long))
919                                         tprintf("= %u",
920                                                 (unsigned int) tcp->u_rval);
921                                 else
922 #endif
923                                         tprintf("= %lu", tcp->u_rval);
924                                 break;
925                         case RVAL_DECIMAL:
926                                 tprintf("= %ld", tcp->u_rval);
927                                 break;
928                         case RVAL_FD:
929                                 if (show_fd_path) {
930                                         tprints("= ");
931                                         printfd(tcp, tcp->u_rval);
932                                 }
933                                 else
934                                         tprintf("= %ld", tcp->u_rval);
935                                 break;
936 #if HAVE_STRUCT_TCB_EXT_ARG
937                         /*
938                         case RVAL_LHEX:
939                                 tprintf("= %#llx", tcp->u_lrval);
940                                 break;
941                         case RVAL_LOCTAL:
942                                 tprintf("= %#llo", tcp->u_lrval);
943                                 break;
944                         */
945                         case RVAL_LUDECIMAL:
946                                 tprintf("= %llu", tcp->u_lrval);
947                                 break;
948                         /*
949                         case RVAL_LDECIMAL:
950                                 tprintf("= %lld", tcp->u_lrval);
951                                 break;
952                         */
953 #endif /* HAVE_STRUCT_TCB_EXT_ARG */
954                         default:
955                                 error_msg("invalid rval format");
956                                 break;
957                         }
958                 }
959                 if ((sys_res & RVAL_STR) && tcp->auxstr)
960                         tprintf(" (%s)", tcp->auxstr);
961         }
962         if (Tflag) {
963                 tv_sub(&tv, &tv, &tcp->etime);
964                 tprintf(" <%ld.%06ld>",
965                         (long) tv.tv_sec, (long) tv.tv_usec);
966         }
967         tprints("\n");
968         dumpio(tcp);
969         line_ended();
970
971 #ifdef USE_LIBUNWIND
972         if (stack_trace_enabled)
973                 unwind_print_stacktrace(tcp);
974 #endif
975
976  ret:
977         tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
978         tcp->sys_func_rval = 0;
979         free_tcb_priv_data(tcp);
980         return 0;
981 }
982
983 int
984 trace_syscall(struct tcb *tcp)
985 {
986         return exiting(tcp) ?
987                 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
988 }
989
990 bool
991 is_erestart(struct tcb *tcp)
992 {
993         switch (tcp->u_error) {
994                 case ERESTARTSYS:
995                 case ERESTARTNOINTR:
996                 case ERESTARTNOHAND:
997                 case ERESTART_RESTARTBLOCK:
998                         return true;
999                 default:
1000                         return false;
1001         }
1002 }
1003
1004 static unsigned long saved_u_error;
1005
1006 void
1007 temporarily_clear_syserror(struct tcb *tcp)
1008 {
1009         saved_u_error = tcp->u_error;
1010         tcp->u_error = 0;
1011 }
1012
1013 void
1014 restore_cleared_syserror(struct tcb *tcp)
1015 {
1016         tcp->u_error = saved_u_error;
1017 }
1018
1019 /*
1020  * Check the syscall return value register value for whether it is
1021  * a negated errno code indicating an error, or a success return value.
1022  */
1023 static inline bool
1024 is_negated_errno(kernel_ulong_t val)
1025 {
1026         /* Linux kernel defines MAX_ERRNO to 4095. */
1027         kernel_ulong_t max = -(kernel_long_t) 4095;
1028
1029 #ifndef current_klongsize
1030         if (current_klongsize < sizeof(val)) {
1031                 val = (uint32_t) val;
1032                 max = (uint32_t) max;
1033         }
1034 #endif /* !current_klongsize */
1035
1036         return val >= max;
1037 }
1038
1039 #include "arch_regs.c"
1040
1041 #ifdef HAVE_GETRVAL2
1042 # include "arch_getrval2.c"
1043 #endif
1044
1045 void
1046 print_pc(struct tcb *tcp)
1047 {
1048 #if defined ARCH_PC_REG
1049 # define ARCH_GET_PC 0
1050 #elif defined ARCH_PC_PEEK_ADDR
1051         long pc;
1052 # define ARCH_PC_REG pc
1053 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1054 #else
1055 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1056 #endif
1057         if (get_regs_error || ARCH_GET_PC)
1058                 tprints(current_wordsize == 4 ? "[????????] "
1059                                               : "[????????????????] ");
1060         else
1061                 tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
1062                         (unsigned long) ARCH_PC_REG);
1063 }
1064
1065 #include "getregs_old.h"
1066
1067 #undef ptrace_getregset_or_getregs
1068 #undef ptrace_setregset_or_setregs
1069 #ifdef ARCH_REGS_FOR_GETREGSET
1070
1071 # define ptrace_getregset_or_getregs ptrace_getregset
1072 static long
1073 ptrace_getregset(pid_t pid)
1074 {
1075 # ifdef ARCH_IOVEC_FOR_GETREGSET
1076         /* variable iovec */
1077         ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1078         return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1079                       &ARCH_IOVEC_FOR_GETREGSET);
1080 # else
1081         /* constant iovec */
1082         static struct iovec io = {
1083                 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1084                 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1085         };
1086         return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1087
1088 # endif
1089 }
1090
1091 # ifndef HAVE_GETREGS_OLD
1092 #  define ptrace_setregset_or_setregs ptrace_setregset
1093 static int
1094 ptrace_setregset(pid_t pid)
1095 {
1096 #  ifdef ARCH_IOVEC_FOR_GETREGSET
1097         /* variable iovec */
1098         return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1099                       &ARCH_IOVEC_FOR_GETREGSET);
1100 #  else
1101         /* constant iovec */
1102         static struct iovec io = {
1103                 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1104                 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1105         };
1106         return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1107 #  endif
1108 }
1109 # endif /* !HAVE_GETREGS_OLD */
1110
1111 #elif defined ARCH_REGS_FOR_GETREGS
1112
1113 # define ptrace_getregset_or_getregs ptrace_getregs
1114 static long
1115 ptrace_getregs(pid_t pid)
1116 {
1117 # if defined SPARC || defined SPARC64
1118         /* SPARC systems have the meaning of data and addr reversed */
1119         return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1120 # else
1121         return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1122 # endif
1123 }
1124
1125 # ifndef HAVE_GETREGS_OLD
1126 #  define ptrace_setregset_or_setregs ptrace_setregs
1127 static int
1128 ptrace_setregs(pid_t pid)
1129 {
1130 #  if defined SPARC || defined SPARC64
1131         /* SPARC systems have the meaning of data and addr reversed */
1132         return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1133 #  else
1134         return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1135 #  endif
1136 }
1137 # endif /* !HAVE_GETREGS_OLD */
1138
1139 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1140
1141 void
1142 get_regs(pid_t pid)
1143 {
1144 #undef USE_GET_SYSCALL_RESULT_REGS
1145 #ifdef ptrace_getregset_or_getregs
1146
1147 # ifdef HAVE_GETREGS_OLD
1148         /*
1149          * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1150          * fallback to getregs_old.
1151          */
1152         static int use_getregs_old;
1153         if (use_getregs_old < 0) {
1154                 get_regs_error = ptrace_getregset_or_getregs(pid);
1155                 return;
1156         } else if (use_getregs_old == 0) {
1157                 get_regs_error = ptrace_getregset_or_getregs(pid);
1158                 if (get_regs_error >= 0) {
1159                         use_getregs_old = -1;
1160                         return;
1161                 }
1162                 if (errno == EPERM || errno == ESRCH)
1163                         return;
1164                 use_getregs_old = 1;
1165         }
1166         get_regs_error = getregs_old(pid);
1167 # else /* !HAVE_GETREGS_OLD */
1168         /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1169         get_regs_error = ptrace_getregset_or_getregs(pid);
1170 # endif /* !HAVE_GETREGS_OLD */
1171
1172 #else /* !ptrace_getregset_or_getregs */
1173
1174 # define USE_GET_SYSCALL_RESULT_REGS 1
1175 # warning get_regs is not implemented for this architecture yet
1176         get_regs_error = 0;
1177
1178 #endif /* !ptrace_getregset_or_getregs */
1179 }
1180
1181 #ifdef ptrace_setregset_or_setregs
1182 static int
1183 set_regs(pid_t pid)
1184 {
1185         return ptrace_setregset_or_setregs(pid);
1186 }
1187 #endif /* ptrace_setregset_or_setregs */
1188
1189 struct sysent_buf {
1190         struct tcb *tcp;
1191         struct_sysent ent;
1192         char buf[sizeof("syscall_%lu") + sizeof(long) * 3];
1193 };
1194
1195 static void
1196 free_sysent_buf(void *ptr)
1197 {
1198         struct sysent_buf *s = ptr;
1199         s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1200         free(ptr);
1201 }
1202
1203 /*
1204  * Returns:
1205  * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1206  * 1: ok, continue in trace_syscall_entering().
1207  * other: error, trace_syscall_entering() should print error indicator
1208  *    ("????" etc) and bail out.
1209  */
1210 int
1211 get_scno(struct tcb *tcp)
1212 {
1213         if (get_regs_error)
1214                 return -1;
1215
1216         int rc = arch_get_scno(tcp);
1217         if (rc != 1)
1218                 return rc;
1219
1220         if (scno_is_valid(tcp->scno)) {
1221                 tcp->s_ent = &sysent[tcp->scno];
1222                 tcp->qual_flg = qual_flags(tcp->scno);
1223         } else {
1224                 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1225
1226                 s->tcp = tcp;
1227                 s->ent.nargs = MAX_ARGS;
1228                 s->ent.sen = SEN_printargs;
1229                 s->ent.sys_func = printargs;
1230                 s->ent.sys_name = s->buf;
1231                 sprintf(s->buf, "syscall_%lu", shuffle_scno(tcp->scno));
1232
1233                 tcp->s_ent = &s->ent;
1234                 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1235
1236                 set_tcb_priv_data(tcp, s, free_sysent_buf);
1237
1238                 if (debug_flag)
1239                         error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
1240         }
1241         return 1;
1242 }
1243
1244 #ifdef USE_GET_SYSCALL_RESULT_REGS
1245 static int get_syscall_result_regs(struct tcb *);
1246 #endif
1247
1248 /* Returns:
1249  * 1: ok, continue in trace_syscall_exiting().
1250  * -1: error, trace_syscall_exiting() should print error indicator
1251  *    ("????" etc) and bail out.
1252  */
1253 static int
1254 get_syscall_result(struct tcb *tcp)
1255 {
1256 #ifdef USE_GET_SYSCALL_RESULT_REGS
1257         if (get_syscall_result_regs(tcp))
1258                 return -1;
1259 #endif
1260         tcp->u_error = 0;
1261         get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1262
1263         return 1;
1264 }
1265
1266 #include "get_scno.c"
1267 #include "set_scno.c"
1268 #include "get_syscall_args.c"
1269 #ifdef USE_GET_SYSCALL_RESULT_REGS
1270 # include "get_syscall_result.c"
1271 #endif
1272 #include "get_error.c"
1273 #include "set_error.c"
1274 #ifdef HAVE_GETREGS_OLD
1275 # include "getregs_old.c"
1276 #endif
1277
1278 const char *
1279 syscall_name(kernel_scno_t scno)
1280 {
1281 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1282         if (current_personality == X32_PERSONALITY_NUMBER)
1283                 scno &= ~__X32_SYSCALL_BIT;
1284 #endif
1285         return scno_is_valid(scno) ? sysent[scno].sys_name: NULL;
1286 }