]> granicus.if.org Git - strace/blob - stream.c
Ensure that SWAP_FLAG_* constants are defined
[strace] / stream.c
1 /*
2  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
3  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  *      $Id$
29  */
30
31 #include "defs.h"
32 #include <sys/syscall.h>
33
34 #ifdef HAVE_POLL_H
35 #include <poll.h>
36 #endif
37 #ifdef HAVE_SYS_POLL_H
38 #include <sys/poll.h>
39 #endif
40 #ifdef HAVE_STROPTS_H
41 #include <stropts.h>
42 #endif
43 #ifdef HAVE_SYS_CONF_H
44 #include <sys/conf.h>
45 #endif
46
47 #ifndef HAVE_STROPTS_H
48 #define RS_HIPRI 1
49 struct strbuf {
50         int     maxlen;                 /* no. of bytes in buffer */
51         int     len;                    /* no. of bytes returned */
52         const char *buf;                /* pointer to data */
53 };
54 #define MORECTL 1
55 #define MOREDATA 2
56 #endif /* !HAVE_STROPTS_H */
57
58 static const struct xlat msgflags[] = {
59         { RS_HIPRI,     "RS_HIPRI"      },
60         { 0,            NULL            },
61 };
62
63 static void
64 printstrbuf(struct tcb *tcp, struct strbuf *sbp, int getting)
65 {
66         if (sbp->maxlen == -1 && getting)
67                 tprints("{maxlen=-1}");
68         else {
69                 tprints("{");
70                 if (getting)
71                         tprintf("maxlen=%d, ", sbp->maxlen);
72                 tprintf("len=%d, buf=", sbp->len);
73                 printstr(tcp, (unsigned long) sbp->buf, sbp->len);
74                 tprints("}");
75         }
76 }
77
78 static void
79 printstrbufarg(struct tcb *tcp, int arg, int getting)
80 {
81         struct strbuf buf;
82
83         if (arg == 0)
84                 tprints("NULL");
85         else if (umove(tcp, arg, &buf) < 0)
86                 tprints("{...}");
87         else
88                 printstrbuf(tcp, &buf, getting);
89         tprints(", ");
90 }
91
92 int
93 sys_putmsg(struct tcb *tcp)
94 {
95         int i;
96
97         if (entering(tcp)) {
98                 /* fd */
99                 tprintf("%ld, ", tcp->u_arg[0]);
100                 /* control and data */
101                 for (i = 1; i < 3; i++)
102                         printstrbufarg(tcp, tcp->u_arg[i], 0);
103                 /* flags */
104                 printflags(msgflags, tcp->u_arg[3], "RS_???");
105         }
106         return 0;
107 }
108
109 #if defined(SPARC) || defined(SPARC64)
110 int
111 sys_getmsg(struct tcb *tcp)
112 {
113         int i, flags;
114
115         if (entering(tcp)) {
116                 /* fd */
117                 tprintf("%lu, ", tcp->u_arg[0]);
118         } else {
119                 if (syserror(tcp)) {
120                         tprintf("%#lx, %#lx, %#lx",
121                                 tcp->u_arg[1], tcp->u_arg[2], tcp->u_arg[3]);
122                         return 0;
123                 }
124                 /* control and data */
125                 for (i = 1; i < 3; i++)
126                         printstrbufarg(tcp, tcp->u_arg[i], 1);
127                 /* pointer to flags */
128                 if (tcp->u_arg[3] == 0)
129                         tprints("NULL");
130                 else if (umove(tcp, tcp->u_arg[3], &flags) < 0)
131                         tprints("[?]");
132                 else {
133                         tprints("[");
134                         printflags(msgflags, flags, "RS_???");
135                         tprints("]");
136                 }
137                 /* decode return value */
138                 switch (tcp->u_rval) {
139                 case MORECTL:
140                         tcp->auxstr = "MORECTL";
141                         break;
142                 case MORECTL|MOREDATA:
143                         tcp->auxstr = "MORECTL|MOREDATA";
144                         break;
145                 case MOREDATA:
146                         tcp->auxstr = "MORECTL";
147                         break;
148                 default:
149                         tcp->auxstr = NULL;
150                         break;
151                 }
152         }
153         return RVAL_HEX | RVAL_STR;
154 }
155 #endif
156
157 #if defined SYS_putpmsg || defined SYS_getpmsg
158 static const struct xlat pmsgflags[] = {
159 #ifdef MSG_HIPRI
160         { MSG_HIPRI,    "MSG_HIPRI"     },
161 #endif
162 #ifdef MSG_AND
163         { MSG_ANY,      "MSG_ANY"       },
164 #endif
165 #ifdef MSG_BAND
166         { MSG_BAND,     "MSG_BAND"      },
167 #endif
168         { 0,            NULL            },
169 };
170 #endif
171
172 #ifdef SYS_putpmsg
173 int
174 sys_putpmsg(struct tcb *tcp)
175 {
176         int i;
177
178         if (entering(tcp)) {
179                 /* fd */
180                 tprintf("%ld, ", tcp->u_arg[0]);
181                 /* control and data */
182                 for (i = 1; i < 3; i++)
183                         printstrbufarg(tcp, tcp->u_arg[i], 0);
184                 /* band */
185                 tprintf("%ld, ", tcp->u_arg[3]);
186                 /* flags */
187                 printflags(pmsgflags, tcp->u_arg[4], "MSG_???");
188         }
189         return 0;
190 }
191 #endif /* SYS_putpmsg */
192
193 #ifdef SYS_getpmsg
194 int
195 sys_getpmsg(struct tcb *tcp)
196 {
197         int i, flags;
198
199         if (entering(tcp)) {
200                 /* fd */
201                 tprintf("%lu, ", tcp->u_arg[0]);
202         } else {
203                 if (syserror(tcp)) {
204                         tprintf("%#lx, %#lx, %#lx, %#lx", tcp->u_arg[1],
205                                 tcp->u_arg[2], tcp->u_arg[3], tcp->u_arg[4]);
206                         return 0;
207                 }
208                 /* control and data */
209                 for (i = 1; i < 3; i++)
210                         printstrbufarg(tcp, tcp->u_arg[i], 1);
211                 /* pointer to band */
212                 printnum(tcp, tcp->u_arg[3], "%d");
213                 tprints(", ");
214                 /* pointer to flags */
215                 if (tcp->u_arg[4] == 0)
216                         tprints("NULL");
217                 else if (umove(tcp, tcp->u_arg[4], &flags) < 0)
218                         tprints("[?]");
219                 else {
220                         tprints("[");
221                         printflags(pmsgflags, flags, "MSG_???");
222                         tprints("]");
223                 }
224                 /* decode return value */
225                 switch (tcp->u_rval) {
226                 case MORECTL:
227                         tcp->auxstr = "MORECTL";
228                         break;
229                 case MORECTL|MOREDATA:
230                         tcp->auxstr = "MORECTL|MOREDATA";
231                         break;
232                 case MOREDATA:
233                         tcp->auxstr = "MORECTL";
234                         break;
235                 default:
236                         tcp->auxstr = NULL;
237                         break;
238                 }
239         }
240         return RVAL_HEX | RVAL_STR;
241 }
242 #endif /* SYS_getpmsg */
243
244 #ifdef HAVE_SYS_POLL_H
245
246 static const struct xlat pollflags[] = {
247 #ifdef POLLIN
248         { POLLIN,       "POLLIN"        },
249         { POLLPRI,      "POLLPRI"       },
250         { POLLOUT,      "POLLOUT"       },
251 #ifdef POLLRDNORM
252         { POLLRDNORM,   "POLLRDNORM"    },
253 #endif
254 #ifdef POLLWRNORM
255         { POLLWRNORM,   "POLLWRNORM"    },
256 #endif
257 #ifdef POLLRDBAND
258         { POLLRDBAND,   "POLLRDBAND"    },
259 #endif
260 #ifdef POLLWRBAND
261         { POLLWRBAND,   "POLLWRBAND"    },
262 #endif
263         { POLLERR,      "POLLERR"       },
264         { POLLHUP,      "POLLHUP"       },
265         { POLLNVAL,     "POLLNVAL"      },
266 #endif
267         { 0,            NULL            },
268 };
269
270 static int
271 decode_poll(struct tcb *tcp, long pts)
272 {
273         struct pollfd fds;
274         unsigned nfds;
275         unsigned long size, start, cur, end, abbrev_end;
276         int failed = 0;
277
278         if (entering(tcp)) {
279                 nfds = tcp->u_arg[1];
280                 size = sizeof(fds) * nfds;
281                 start = tcp->u_arg[0];
282                 end = start + size;
283                 if (nfds == 0 || size / sizeof(fds) != nfds || end < start) {
284                         tprintf("%#lx, %d, ",
285                                 tcp->u_arg[0], nfds);
286                         return 0;
287                 }
288                 if (abbrev(tcp)) {
289                         abbrev_end = start + max_strlen * sizeof(fds);
290                         if (abbrev_end < start)
291                                 abbrev_end = end;
292                 } else {
293                         abbrev_end = end;
294                 }
295                 tprints("[");
296                 for (cur = start; cur < end; cur += sizeof(fds)) {
297                         if (cur > start)
298                                 tprints(", ");
299                         if (cur >= abbrev_end) {
300                                 tprints("...");
301                                 break;
302                         }
303                         if (umoven(tcp, cur, sizeof fds, (char *) &fds) < 0) {
304                                 tprints("?");
305                                 failed = 1;
306                                 break;
307                         }
308                         if (fds.fd < 0) {
309                                 tprintf("{fd=%d}", fds.fd);
310                                 continue;
311                         }
312                         tprints("{fd=");
313                         printfd(tcp, fds.fd);
314                         tprints(", events=");
315                         printflags(pollflags, fds.events, "POLL???");
316                         tprints("}");
317                 }
318                 tprints("]");
319                 if (failed)
320                         tprintf(" %#lx", start);
321                 tprintf(", %d, ", nfds);
322                 return 0;
323         } else {
324                 static char outstr[1024];
325                 char *outptr;
326 #define end_outstr (outstr + sizeof(outstr))
327                 const char *flagstr;
328
329                 if (syserror(tcp))
330                         return 0;
331                 if (tcp->u_rval == 0) {
332                         tcp->auxstr = "Timeout";
333                         return RVAL_STR;
334                 }
335
336                 nfds = tcp->u_arg[1];
337                 size = sizeof(fds) * nfds;
338                 start = tcp->u_arg[0];
339                 end = start + size;
340                 if (nfds == 0 || size / sizeof(fds) != nfds || end < start)
341                         return 0;
342                 if (abbrev(tcp)) {
343                         abbrev_end = start + max_strlen * sizeof(fds);
344                         if (abbrev_end < start)
345                                 abbrev_end = end;
346                 } else {
347                         abbrev_end = end;
348                 }
349
350                 outptr = outstr;
351
352                 for (cur = start; cur < end; cur += sizeof(fds)) {
353                         if (umoven(tcp, cur, sizeof fds, (char *) &fds) < 0) {
354                                 if (outptr < end_outstr - 2)
355                                         *outptr++ = '?';
356                                 failed = 1;
357                                 break;
358                         }
359                         if (!fds.revents)
360                                 continue;
361                         if (outptr == outstr) {
362                                 *outptr++ = '[';
363                         } else {
364                                 if (outptr < end_outstr - 3)
365                                         outptr = stpcpy(outptr, ", ");
366                         }
367                         if (cur >= abbrev_end) {
368                                 if (outptr < end_outstr - 4)
369                                         outptr = stpcpy(outptr, "...");
370                                 break;
371                         }
372                         if (outptr < end_outstr - (sizeof("{fd=%d, revents=") + sizeof(int)*3) + 1)
373                                 outptr += sprintf(outptr, "{fd=%d, revents=", fds.fd);
374                         flagstr = sprintflags("", pollflags, fds.revents);
375                         if (outptr < end_outstr - (strlen(flagstr) + 2)) {
376                                 outptr = stpcpy(outptr, flagstr);
377                                 *outptr++ = '}';
378                         }
379                 }
380                 if (failed)
381                         return 0;
382
383                 if (outptr != outstr /* && outptr < end_outstr - 1 (always true)*/)
384                         *outptr++ = ']';
385
386                 *outptr = '\0';
387                 if (pts) {
388                         if (outptr < end_outstr - (10 + TIMESPEC_TEXT_BUFSIZE)) {
389                                 outptr = stpcpy(outptr, outptr == outstr ? "left " : ", left ");
390                                 sprint_timespec(outptr, tcp, pts);
391                         }
392                 }
393
394                 if (outptr == outstr)
395                         return 0;
396
397                 tcp->auxstr = outstr;
398                 return RVAL_STR;
399 #undef end_outstr
400         }
401 }
402
403 int
404 sys_poll(struct tcb *tcp)
405 {
406         int rc = decode_poll(tcp, 0);
407         if (entering(tcp)) {
408 #ifdef INFTIM
409                 if (tcp->u_arg[2] == INFTIM)
410                         tprints("INFTIM");
411                 else
412 #endif
413                         tprintf("%ld", tcp->u_arg[2]);
414         }
415         return rc;
416 }
417
418 int
419 sys_ppoll(struct tcb *tcp)
420 {
421         int rc = decode_poll(tcp, tcp->u_arg[2]);
422         if (entering(tcp)) {
423                 print_timespec(tcp, tcp->u_arg[2]);
424                 tprints(", ");
425                 print_sigset(tcp, tcp->u_arg[3], 0);
426                 tprintf(", %lu", tcp->u_arg[4]);
427         }
428         return rc;
429 }
430
431 #else /* !HAVE_SYS_POLL_H */
432 int
433 sys_poll(struct tcb *tcp)
434 {
435         return 0;
436 }
437 #endif