]> granicus.if.org Git - strace/blob - wait.c
xlat: add BPF_F_TEST_STATE_FREQ to bpf_prog_flags
[strace] / wait.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-1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * Copyright (c) 2002-2004 Roland McGrath <roland@redhat.com>
7  * Copyright (c) 2004 Ulrich Drepper <drepper@redhat.com>
8  * Copyright (c) 2009-2013 Denys Vlasenko <dvlasenk@redhat.com>
9  * Copyright (c) 2014-2015 Dmitry V. Levin <ldv@altlinux.org>
10  * Copyright (c) 2014-2018 The strace developers.
11  * All rights reserved.
12  *
13  * SPDX-License-Identifier: LGPL-2.1-or-later
14  */
15
16 #include "defs.h"
17 #include "ptrace.h"
18
19 #include "wait.h"
20
21 #include "xlat/wait4_options.h"
22 #include "xlat/ptrace_events.h"
23
24 static int
25 printstatus(int status)
26 {
27         int exited = 0;
28
29         /*
30          * Here is a tricky presentation problem.  This solution
31          * is still not entirely satisfactory but since there
32          * are no wait status constructors it will have to do.
33          */
34         if (WIFSTOPPED(status)) {
35                 int sig = WSTOPSIG(status);
36                 tprintf("[{WIFSTOPPED(s) && WSTOPSIG(s) == %s%s}",
37                         sprintsigname(sig & 0x7f),
38                         sig & 0x80 ? " | 0x80" : "");
39                 status &= ~W_STOPCODE(sig);
40         } else if (WIFSIGNALED(status)) {
41                 tprintf("[{WIFSIGNALED(s) && WTERMSIG(s) == %s%s}",
42                         sprintsigname(WTERMSIG(status)),
43                         WCOREDUMP(status) ? " && WCOREDUMP(s)" : "");
44                 status &= ~(W_EXITCODE(0, WTERMSIG(status)) | WCOREFLAG);
45         } else if (WIFEXITED(status)) {
46                 tprintf("[{WIFEXITED(s) && WEXITSTATUS(s) == %d}",
47                         WEXITSTATUS(status));
48                 exited = 1;
49                 status &= ~W_EXITCODE(WEXITSTATUS(status), 0);
50         }
51 #ifdef WIFCONTINUED
52         else if (WIFCONTINUED(status)) {
53                 tprints("[{WIFCONTINUED(s)}");
54                 status &= ~W_CONTINUED;
55         }
56 #endif
57         else {
58                 tprintf("[%#x]", status);
59                 return 0;
60         }
61
62         if (status) {
63                 unsigned int event = (unsigned int) status >> 16;
64                 if (event) {
65                         tprints(" | ");
66                         printxval(ptrace_events, event, "PTRACE_EVENT_???");
67                         tprints(" << 16");
68                         status &= 0xffff;
69                 }
70                 if (status)
71                         tprintf(" | %#x", status);
72         }
73         tprints("]");
74
75         return exited;
76 }
77
78 static int
79 printwaitn(struct tcb *const tcp,
80            void (*const print_rusage)(struct tcb *, kernel_ulong_t))
81 {
82         if (entering(tcp)) {
83                 /* On Linux, kernel-side pid_t is typedef'ed to int
84                  * on all arches. Also, glibc-2.8 truncates wait3 and wait4
85                  * pid argument to int on 64bit arches, producing,
86                  * for example, wait4(4294967295, ...) instead of -1
87                  * in strace. We have to use int here, not long.
88                  */
89                 int pid = tcp->u_arg[0];
90                 tprintf("%d, ", pid);
91         } else {
92                 int status;
93
94                 /* status */
95                 if (tcp->u_rval == 0)
96                         printaddr(tcp->u_arg[1]);
97                 else if (!umove_or_printaddr(tcp, tcp->u_arg[1], &status))
98                         printstatus(status);
99                 /* options */
100                 tprints(", ");
101                 printflags(wait4_options, tcp->u_arg[2], "W???");
102                 if (print_rusage) {
103                         /* usage */
104                         tprints(", ");
105                         if (tcp->u_rval > 0)
106                                 print_rusage(tcp, tcp->u_arg[3]);
107                         else
108                                 printaddr(tcp->u_arg[3]);
109                 }
110         }
111         return 0;
112 }
113
114 SYS_FUNC(waitpid)
115 {
116         return printwaitn(tcp, NULL);
117 }
118
119 SYS_FUNC(wait4)
120 {
121         return printwaitn(tcp, printrusage);
122 }
123
124 #ifdef ALPHA
125 SYS_FUNC(osf_wait4)
126 {
127         return printwaitn(tcp, printrusage32);
128 }
129 #endif
130
131 #include "xlat/waitid_types.h"
132
133 SYS_FUNC(waitid)
134 {
135         if (entering(tcp)) {
136                 printxval(waitid_types, tcp->u_arg[0], "P_???");
137                 int pid = tcp->u_arg[1];
138                 tprintf(", %d, ", pid);
139         } else {
140                 /* siginfo */
141                 printsiginfo_at(tcp, tcp->u_arg[2]);
142                 /* options */
143                 tprints(", ");
144                 printflags(wait4_options, tcp->u_arg[3], "W???");
145                 /* usage */
146                 tprints(", ");
147                 printrusage(tcp, tcp->u_arg[4]);
148         }
149         return 0;
150 }