blob: db1931d1aa2e24500b21c0c6cd7ab4073d44917f [file] [log] [blame]
Dmitry V. Levin811bda62015-07-30 16:49:42 +00001/*
2 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
3 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
Elliott Hughesb7556142018-02-20 17:03:16 -08004 * Copyright (c) 1999-2018 The strace developers.
Dmitry V. Levin811bda62015-07-30 16:49:42 +00005 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "defs.h"
31#include <poll.h>
Elliott Hughesb7556142018-02-20 17:03:16 -080032#include "xstring.h"
Dmitry V. Levin811bda62015-07-30 16:49:42 +000033
34#include "xlat/pollflags.h"
35
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000036static bool
37print_pollfd(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000038{
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000039 const struct pollfd *fds = elem_buf;
40
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000041 tprints("{fd=");
42 printfd(tcp, fds->fd);
43 if (fds->fd >= 0) {
44 tprints(", events=");
Dmitry V. Levin952d89b2016-05-16 22:03:51 +000045 printflags(pollflags, (unsigned short) fds->events, "POLL???");
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000046 }
47 tprints("}");
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000048
49 return true;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000050}
51
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000052static void
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000053decode_poll_entering(struct tcb *tcp)
Dmitry V. Levin811bda62015-07-30 16:49:42 +000054{
Elliott Hughesd35df492017-02-15 15:19:05 -080055 const kernel_ulong_t addr = tcp->u_arg[0];
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000056 const unsigned int nfds = tcp->u_arg[1];
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000057 struct pollfd fds;
Dmitry V. Levin811bda62015-07-30 16:49:42 +000058
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000059 print_array(tcp, addr, nfds, &fds, sizeof(fds),
Elliott Hughes03a418e2018-06-15 13:11:40 -070060 tfetch_mem, print_pollfd, 0);
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000061 tprintf(", %u, ", nfds);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000062}
63
64static int
Elliott Hughesd35df492017-02-15 15:19:05 -080065decode_poll_exiting(struct tcb *const tcp, const kernel_ulong_t pts)
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000066{
67 struct pollfd fds;
68 const unsigned int nfds = tcp->u_arg[1];
69 const unsigned long size = sizeof(fds) * nfds;
Elliott Hughesd35df492017-02-15 15:19:05 -080070 const kernel_ulong_t start = tcp->u_arg[0];
71 const kernel_ulong_t end = start + size;
72 kernel_ulong_t cur;
73 const unsigned int max_printed =
74 abbrev(tcp) ? max_strlen : -1U;
75 unsigned int printed;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000076
77 static char outstr[1024];
78 char *outptr;
79#define end_outstr (outstr + sizeof(outstr))
80
81 if (syserror(tcp))
82 return 0;
83 if (tcp->u_rval == 0) {
84 tcp->auxstr = "Timeout";
85 return RVAL_STR;
86 }
87
88 if (!verbose(tcp) || !start || !nfds ||
89 size / sizeof(fds) != nfds || end < start)
90 return 0;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000091
92 outptr = outstr;
93
Dmitry V. Levin9f612ff2016-02-16 00:44:16 +000094 for (printed = 0, cur = start; cur < end; cur += sizeof(fds)) {
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000095 if (umove(tcp, cur, &fds) < 0) {
96 if (outptr == outstr)
97 *outptr++ = '[';
98 else
99 outptr = stpcpy(outptr, ", ");
Elliott Hughesb7556142018-02-20 17:03:16 -0800100 outptr = xappendstr(outstr, outptr, "%#" PRI_klx, cur);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000101 break;
102 }
103 if (!fds.revents)
104 continue;
105 if (outptr == outstr)
106 *outptr++ = '[';
107 else
108 outptr = stpcpy(outptr, ", ");
Dmitry V. Levin9f612ff2016-02-16 00:44:16 +0000109 if (printed >= max_printed) {
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000110 outptr = stpcpy(outptr, "...");
111 break;
112 }
113
114 static const char fmt[] = "{fd=%d, revents=";
115 char fdstr[sizeof(fmt) + sizeof(int) * 3];
Elliott Hughesb7556142018-02-20 17:03:16 -0800116 xsprintf(fdstr, fmt, fds.fd);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000117
Dmitry V. Levin6274ecc2016-05-15 14:23:06 +0000118 const char *flagstr = sprintflags("", pollflags,
119 (unsigned short) fds.revents);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000120
Dmitry V. Levin8ef98092016-02-16 00:04:37 +0000121 if (outptr + strlen(fdstr) + strlen(flagstr) + 1 >=
122 end_outstr - (2 + 2 * sizeof(long) + sizeof(", ], ..."))) {
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000123 outptr = stpcpy(outptr, "...");
124 break;
125 }
126 outptr = stpcpy(outptr, fdstr);
127 outptr = stpcpy(outptr, flagstr);
128 *outptr++ = '}';
Dmitry V. Levin9f612ff2016-02-16 00:44:16 +0000129 ++printed;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000130 }
131
132 if (outptr != outstr)
133 *outptr++ = ']';
134
135 *outptr = '\0';
136 if (pts) {
Dmitry V. Levin2950de32015-09-18 17:44:16 +0000137 const char *str = sprint_timespec(tcp, pts);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000138
Dmitry V. Levin2950de32015-09-18 17:44:16 +0000139 if (outptr + sizeof(", left ") + strlen(str) < end_outstr) {
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000140 outptr = stpcpy(outptr, outptr == outstr ? "left " : ", left ");
Dmitry V. Levin2950de32015-09-18 17:44:16 +0000141 outptr = stpcpy(outptr, str);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000142 } else {
143 outptr = stpcpy(outptr, ", ...");
144 }
145 }
146
147 if (outptr == outstr)
148 return 0;
149
150 tcp->auxstr = outstr;
151 return RVAL_STR;
152#undef end_outstr
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000153}
154
155SYS_FUNC(poll)
156{
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000157 if (entering(tcp)) {
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +0000158 decode_poll_entering(tcp);
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700159 tprintf("%d", (int) tcp->u_arg[2]);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000160
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +0000161 return 0;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000162 } else {
163 return decode_poll_exiting(tcp, 0);
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000164 }
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000165}
166
167SYS_FUNC(ppoll)
168{
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000169 if (entering(tcp)) {
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +0000170 decode_poll_entering(tcp);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000171
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000172 print_timespec(tcp, tcp->u_arg[2]);
173 tprints(", ");
Elliott Hughesd35df492017-02-15 15:19:05 -0800174 /* NB: kernel requires arg[4] == NSIG_BYTES */
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000175 print_sigset_addr_len(tcp, tcp->u_arg[3], tcp->u_arg[4]);
Elliott Hughesd35df492017-02-15 15:19:05 -0800176 tprintf(", %" PRI_klu, tcp->u_arg[4]);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000177
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +0000178 return 0;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000179 } else {
180 return decode_poll_exiting(tcp, tcp->u_arg[2]);
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000181 }
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000182}