blob: c1d1dff24952b6be91feeb39232a9e3ccf0be834 [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
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>
Elliott Hughesb7556142018-02-20 17:03:16 -080010 * Copyright (c) 2014-2018 The strace developers.
Dmitry V. Levin38a34c92015-12-17 17:56:48 +000011 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +000036#include "defs.h"
Elliott Hughesb7556142018-02-20 17:03:16 -080037#include "ptrace.h"
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +000038
39#include <sys/wait.h>
40
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +000041#include "xlat/wait4_options.h"
42
43#if !defined WCOREFLAG && defined WCOREFLG
44# define WCOREFLAG WCOREFLG
45#endif
46#ifndef WCOREFLAG
47# define WCOREFLAG 0x80
48#endif
49#ifndef WCOREDUMP
50# define WCOREDUMP(status) ((status) & 0200)
51#endif
52#ifndef W_STOPCODE
53# define W_STOPCODE(sig) ((sig) << 8 | 0x7f)
54#endif
55#ifndef W_EXITCODE
56# define W_EXITCODE(ret, sig) ((ret) << 8 | (sig))
57#endif
Dmitry V. Levind8b34042015-02-12 22:43:02 +000058#ifndef W_CONTINUED
59# define W_CONTINUED 0xffff
60#endif
61
62#include "xlat/ptrace_events.h"
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +000063
64static int
65printstatus(int status)
66{
67 int exited = 0;
68
69 /*
70 * Here is a tricky presentation problem. This solution
71 * is still not entirely satisfactory but since there
72 * are no wait status constructors it will have to do.
73 */
74 if (WIFSTOPPED(status)) {
Dmitry V. Levind8b34042015-02-12 22:43:02 +000075 int sig = WSTOPSIG(status);
76 tprintf("[{WIFSTOPPED(s) && WSTOPSIG(s) == %s%s}",
77 signame(sig & 0x7f),
78 sig & 0x80 ? " | 0x80" : "");
79 status &= ~W_STOPCODE(sig);
Elliott Hughesdc75b012017-07-05 13:54:44 -070080 } else if (WIFSIGNALED(status)) {
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +000081 tprintf("[{WIFSIGNALED(s) && WTERMSIG(s) == %s%s}",
82 signame(WTERMSIG(status)),
83 WCOREDUMP(status) ? " && WCOREDUMP(s)" : "");
84 status &= ~(W_EXITCODE(0, WTERMSIG(status)) | WCOREFLAG);
Elliott Hughesdc75b012017-07-05 13:54:44 -070085 } else if (WIFEXITED(status)) {
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +000086 tprintf("[{WIFEXITED(s) && WEXITSTATUS(s) == %d}",
87 WEXITSTATUS(status));
88 exited = 1;
89 status &= ~W_EXITCODE(WEXITSTATUS(status), 0);
90 }
Dmitry V. Levind8b34042015-02-12 22:43:02 +000091#ifdef WIFCONTINUED
92 else if (WIFCONTINUED(status)) {
93 tprints("[{WIFCONTINUED(s)}");
94 status &= ~W_CONTINUED;
95 }
96#endif
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +000097 else {
98 tprintf("[%#x]", status);
99 return 0;
100 }
101
Dmitry V. Levind8b34042015-02-12 22:43:02 +0000102 if (status) {
103 unsigned int event = (unsigned int) status >> 16;
104 if (event) {
105 tprints(" | ");
106 printxval(ptrace_events, event, "PTRACE_EVENT_???");
107 tprints(" << 16");
108 status &= 0xffff;
109 }
110 if (status)
111 tprintf(" | %#x", status);
112 }
113 tprints("]");
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000114
115 return exited;
116}
117
118static int
Elliott Hughesd35df492017-02-15 15:19:05 -0800119printwaitn(struct tcb *const tcp,
120 void (*const print_rusage)(struct tcb *, kernel_ulong_t))
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000121{
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000122 if (entering(tcp)) {
123 /* On Linux, kernel-side pid_t is typedef'ed to int
124 * on all arches. Also, glibc-2.8 truncates wait3 and wait4
125 * pid argument to int on 64bit arches, producing,
126 * for example, wait4(4294967295, ...) instead of -1
127 * in strace. We have to use int here, not long.
128 */
129 int pid = tcp->u_arg[0];
130 tprintf("%d, ", pid);
131 } else {
Dmitry V. Levinaf618fa2016-02-11 02:39:04 +0000132 int status;
133
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000134 /* status */
Dmitry V. Levin90cfe8f2015-07-15 00:36:20 +0000135 if (tcp->u_rval == 0)
136 printaddr(tcp->u_arg[1]);
137 else if (!umove_or_printaddr(tcp, tcp->u_arg[1], &status))
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000138 printstatus(status);
139 /* options */
140 tprints(", ");
141 printflags(wait4_options, tcp->u_arg[2], "W???");
Dmitry V. Levinaf618fa2016-02-11 02:39:04 +0000142 if (print_rusage) {
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000143 /* usage */
Dmitry V. Levinaf618fa2016-02-11 02:39:04 +0000144 tprints(", ");
145 if (tcp->u_rval > 0)
146 print_rusage(tcp, tcp->u_arg[3]);
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000147 else
Dmitry V. Levin90cfe8f2015-07-15 00:36:20 +0000148 printaddr(tcp->u_arg[3]);
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000149 }
150 }
151 return 0;
152}
153
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000154SYS_FUNC(waitpid)
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000155{
Dmitry V. Levinaf618fa2016-02-11 02:39:04 +0000156 return printwaitn(tcp, NULL);
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000157}
158
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000159SYS_FUNC(wait4)
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000160{
Dmitry V. Levinaf618fa2016-02-11 02:39:04 +0000161 return printwaitn(tcp, printrusage);
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000162}
163
164#ifdef ALPHA
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000165SYS_FUNC(osf_wait4)
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000166{
Dmitry V. Levinaf618fa2016-02-11 02:39:04 +0000167 return printwaitn(tcp, printrusage32);
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000168}
169#endif
170
171#include "xlat/waitid_types.h"
172
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000173SYS_FUNC(waitid)
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000174{
175 if (entering(tcp)) {
176 printxval(waitid_types, tcp->u_arg[0], "P_???");
Dmitry V. Levin591906c2016-02-11 02:42:36 +0000177 int pid = tcp->u_arg[1];
178 tprintf(", %d, ", pid);
Dmitry V. Levin90cfe8f2015-07-15 00:36:20 +0000179 } else {
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000180 /* siginfo */
Dmitry V. Levine2fb0bb2015-09-15 21:51:15 +0000181 printsiginfo_at(tcp, tcp->u_arg[2]);
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000182 /* options */
183 tprints(", ");
184 printflags(wait4_options, tcp->u_arg[3], "W???");
Dmitry V. Levind3f17c62016-02-11 02:20:18 +0000185 /* usage */
186 tprints(", ");
187 printrusage(tcp, tcp->u_arg[4]);
Dmitry V. Levin7ccc1442014-12-11 19:25:02 +0000188 }
189 return 0;
190}