]> granicus.if.org Git - strace/blob - stream.c
Update AF_*, PF_*, MSG_*, and TCP_* constants
[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
29 #include "defs.h"
30 #ifdef HAVE_POLL_H
31 # include <poll.h>
32 #endif
33 #ifdef HAVE_SYS_POLL_H
34 # include <sys/poll.h>
35 #endif
36 #ifdef HAVE_SYS_CONF_H
37 # include <sys/conf.h>
38 #endif
39
40 /* Who has STREAMS syscalls?
41  * Linux hasn't. Solaris has (had?).
42  * Just in case I miss something, retain in for Sparc...
43  */
44 #if defined(SPARC) || defined(SPARC64)
45
46 # ifdef HAVE_STROPTS_H
47 #  include <stropts.h>
48 # else
49 #  define RS_HIPRI 1
50 struct strbuf {
51         int     maxlen;                 /* no. of bytes in buffer */
52         int     len;                    /* no. of bytes returned */
53         const char *buf;                /* pointer to data */
54 };
55 #  define MORECTL 1
56 #  define MOREDATA 2
57 # endif
58
59 static const struct xlat msgflags[] = {
60         { RS_HIPRI,     "RS_HIPRI"      },
61         { 0,            NULL            },
62 };
63
64 static void
65 printstrbuf(struct tcb *tcp, struct strbuf *sbp, int getting)
66 {
67         if (sbp->maxlen == -1 && getting)
68                 tprints("{maxlen=-1}");
69         else {
70                 tprints("{");
71                 if (getting)
72                         tprintf("maxlen=%d, ", sbp->maxlen);
73                 tprintf("len=%d, buf=", sbp->len);
74                 printstr(tcp, (unsigned long) sbp->buf, sbp->len);
75                 tprints("}");
76         }
77 }
78
79 static void
80 printstrbufarg(struct tcb *tcp, long arg, int getting)
81 {
82         struct strbuf buf;
83
84         if (arg == 0)
85                 tprints("NULL");
86         else if (umove(tcp, arg, &buf) < 0)
87                 tprints("{...}");
88         else
89                 printstrbuf(tcp, &buf, getting);
90         tprints(", ");
91 }
92
93 int
94 sys_putmsg(struct tcb *tcp)
95 {
96         int i;
97
98         if (entering(tcp)) {
99                 /* fd */
100                 tprintf("%ld, ", tcp->u_arg[0]);
101                 /* control and data */
102                 for (i = 1; i < 3; i++)
103                         printstrbufarg(tcp, tcp->u_arg[i], 0);
104                 /* flags */
105                 printflags(msgflags, tcp->u_arg[3], "RS_???");
106         }
107         return 0;
108 }
109
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
156 # if defined SYS_putpmsg || defined SYS_getpmsg
157 static const struct xlat pmsgflags[] = {
158 #  ifdef MSG_HIPRI
159         { MSG_HIPRI,    "MSG_HIPRI"     },
160 #  endif
161 #  ifdef MSG_AND
162         { MSG_ANY,      "MSG_ANY"       },
163 #  endif
164 #  ifdef MSG_BAND
165         { MSG_BAND,     "MSG_BAND"      },
166 #  endif
167         { 0,            NULL            },
168 };
169 #  ifdef SYS_putpmsg
170 int
171 sys_putpmsg(struct tcb *tcp)
172 {
173         int i;
174
175         if (entering(tcp)) {
176                 /* fd */
177                 tprintf("%ld, ", tcp->u_arg[0]);
178                 /* control and data */
179                 for (i = 1; i < 3; i++)
180                         printstrbufarg(tcp, tcp->u_arg[i], 0);
181                 /* band */
182                 tprintf("%ld, ", tcp->u_arg[3]);
183                 /* flags */
184                 printflags(pmsgflags, tcp->u_arg[4], "MSG_???");
185         }
186         return 0;
187 }
188 #  endif
189 #  ifdef SYS_getpmsg
190 int
191 sys_getpmsg(struct tcb *tcp)
192 {
193         int i, flags;
194
195         if (entering(tcp)) {
196                 /* fd */
197                 tprintf("%lu, ", tcp->u_arg[0]);
198         } else {
199                 if (syserror(tcp)) {
200                         tprintf("%#lx, %#lx, %#lx, %#lx", tcp->u_arg[1],
201                                 tcp->u_arg[2], tcp->u_arg[3], tcp->u_arg[4]);
202                         return 0;
203                 }
204                 /* control and data */
205                 for (i = 1; i < 3; i++)
206                         printstrbufarg(tcp, tcp->u_arg[i], 1);
207                 /* pointer to band */
208                 printnum(tcp, tcp->u_arg[3], "%d");
209                 tprints(", ");
210                 /* pointer to flags */
211                 if (tcp->u_arg[4] == 0)
212                         tprints("NULL");
213                 else if (umove(tcp, tcp->u_arg[4], &flags) < 0)
214                         tprints("[?]");
215                 else {
216                         tprints("[");
217                         printflags(pmsgflags, flags, "MSG_???");
218                         tprints("]");
219                 }
220                 /* decode return value */
221                 switch (tcp->u_rval) {
222                 case MORECTL:
223                         tcp->auxstr = "MORECTL";
224                         break;
225                 case MORECTL|MOREDATA:
226                         tcp->auxstr = "MORECTL|MOREDATA";
227                         break;
228                 case MOREDATA:
229                         tcp->auxstr = "MORECTL";
230                         break;
231                 default:
232                         tcp->auxstr = NULL;
233                         break;
234                 }
235         }
236         return RVAL_HEX | RVAL_STR;
237 }
238 #  endif
239 # endif /* getpmsg/putpmsg */
240
241 #endif /* STREAMS syscalls support */
242
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