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