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