]> granicus.if.org Git - strace/blob - io.c
Add file descriptor argument to decode_netlink
[strace] / io.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, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * Copyright (c) 1999-2017 The strace developers.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "defs.h"
33 #include <fcntl.h>
34 #include <sys/uio.h>
35
36 SYS_FUNC(read)
37 {
38         if (entering(tcp)) {
39                 printfd(tcp, tcp->u_arg[0]);
40                 tprints(", ");
41         } else {
42                 if (syserror(tcp))
43                         printaddr(tcp->u_arg[1]);
44                 else
45                         printstrn(tcp, tcp->u_arg[1], tcp->u_rval);
46                 tprintf(", %" PRI_klu, tcp->u_arg[2]);
47         }
48         return 0;
49 }
50
51 SYS_FUNC(write)
52 {
53         printfd(tcp, tcp->u_arg[0]);
54         tprints(", ");
55         printstrn(tcp, tcp->u_arg[1], tcp->u_arg[2]);
56         tprintf(", %" PRI_klu, tcp->u_arg[2]);
57
58         return RVAL_DECODED;
59 }
60
61 struct print_iovec_config {
62         enum iov_decode decode_iov;
63         kernel_ulong_t data_size;
64 };
65
66 static bool
67 print_iovec(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
68 {
69         const kernel_ulong_t *iov;
70         kernel_ulong_t iov_buf[2], len;
71         struct print_iovec_config *c = data;
72
73         if (elem_size < sizeof(iov_buf)) {
74                 iov_buf[0] = ((unsigned int *) elem_buf)[0];
75                 iov_buf[1] = ((unsigned int *) elem_buf)[1];
76                 iov = iov_buf;
77         } else {
78                 iov = elem_buf;
79         }
80
81         tprints("{iov_base=");
82
83         len = iov[1];
84
85         switch (c->decode_iov) {
86                 case IOV_DECODE_STR:
87                         if (len > c->data_size)
88                                 len = c->data_size;
89                         if (c->data_size != (kernel_ulong_t) -1)
90                                 c->data_size -= len;
91                         printstrn(tcp, iov[0], len);
92                         break;
93                 case IOV_DECODE_NETLINK:
94                         if (len > c->data_size)
95                                 len = c->data_size;
96                         if (c->data_size != (kernel_ulong_t) -1)
97                                 c->data_size -= len;
98                         /* assume that the descriptor is 1st syscall argument */
99                         decode_netlink(tcp, tcp->u_arg[0], iov[0], len);
100                         break;
101                 default:
102                         printaddr(iov[0]);
103                         break;
104         }
105
106         tprintf(", iov_len=%" PRI_klu "}", iov[1]);
107
108         return true;
109 }
110
111 /*
112  * data_size limits the cumulative size of printed data.
113  * Example: recvmsg returing a short read.
114  */
115 void
116 tprint_iov_upto(struct tcb *const tcp, const kernel_ulong_t len,
117                 const kernel_ulong_t addr, const enum iov_decode decode_iov,
118                 const kernel_ulong_t data_size)
119 {
120         kernel_ulong_t iov[2];
121         struct print_iovec_config config =
122                 { .decode_iov = decode_iov, .data_size = data_size };
123
124         print_array(tcp, addr, len, iov, current_wordsize * 2,
125                     umoven_or_printaddr_ignore_syserror, print_iovec, &config);
126 }
127
128 SYS_FUNC(readv)
129 {
130         if (entering(tcp)) {
131                 printfd(tcp, tcp->u_arg[0]);
132                 tprints(", ");
133         } else {
134                 tprint_iov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
135                                 syserror(tcp) ? IOV_DECODE_ADDR :
136                                 IOV_DECODE_STR, tcp->u_rval);
137                 tprintf(", %" PRI_klu, tcp->u_arg[2]);
138         }
139         return 0;
140 }
141
142 SYS_FUNC(writev)
143 {
144         printfd(tcp, tcp->u_arg[0]);
145         tprints(", ");
146         tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], IOV_DECODE_STR);
147         tprintf(", %" PRI_klu, tcp->u_arg[2]);
148
149         return RVAL_DECODED;
150 }
151
152 SYS_FUNC(pread)
153 {
154         if (entering(tcp)) {
155                 printfd(tcp, tcp->u_arg[0]);
156                 tprints(", ");
157         } else {
158                 if (syserror(tcp))
159                         printaddr(tcp->u_arg[1]);
160                 else
161                         printstrn(tcp, tcp->u_arg[1], tcp->u_rval);
162                 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
163                 printllval(tcp, "%lld", 3);
164         }
165         return 0;
166 }
167
168 SYS_FUNC(pwrite)
169 {
170         printfd(tcp, tcp->u_arg[0]);
171         tprints(", ");
172         printstrn(tcp, tcp->u_arg[1], tcp->u_arg[2]);
173         tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
174         printllval(tcp, "%lld", 3);
175
176         return RVAL_DECODED;
177 }
178
179 static void
180 print_lld_from_low_high_val(struct tcb *tcp, int arg)
181 {
182 #if SIZEOF_KERNEL_LONG_T > 4
183 # ifndef current_klongsize
184         if (current_klongsize < SIZEOF_KERNEL_LONG_T) {
185                 tprintf("%" PRI_kld, (tcp->u_arg[arg + 1] << 32)
186                                | tcp->u_arg[arg]);
187         } else
188 # endif /* !current_klongsize */
189         {
190                 tprintf("%" PRI_kld, tcp->u_arg[arg]);
191         }
192 #else /* SIZEOF_KERNEL_LONG_T == 4 */
193         tprintf("%lld",
194                   ((long long) tcp->u_arg[arg + 1] << 32)
195                 | ((long long) tcp->u_arg[arg]));
196 #endif
197 }
198
199 #include "xlat/rwf_flags.h"
200
201 static int
202 do_preadv(struct tcb *tcp, const int flags_arg)
203 {
204         if (entering(tcp)) {
205                 printfd(tcp, tcp->u_arg[0]);
206                 tprints(", ");
207         } else {
208                 kernel_ulong_t len =
209                         truncate_kulong_to_current_wordsize(tcp->u_arg[2]);
210
211                 tprint_iov_upto(tcp, len, tcp->u_arg[1],
212                                 syserror(tcp) ? IOV_DECODE_ADDR :
213                                 IOV_DECODE_STR, tcp->u_rval);
214                 tprintf(", %" PRI_klu ", ", len);
215                 print_lld_from_low_high_val(tcp, 3);
216                 if (flags_arg >= 0) {
217                         tprints(", ");
218                         printflags(rwf_flags, tcp->u_arg[flags_arg], "RWF_???");
219                 }
220         }
221         return 0;
222 }
223
224 SYS_FUNC(preadv)
225 {
226         return do_preadv(tcp, -1);
227 }
228
229 static int
230 do_pwritev(struct tcb *tcp, const int flags_arg)
231 {
232         kernel_ulong_t len =
233                 truncate_kulong_to_current_wordsize(tcp->u_arg[2]);
234
235         printfd(tcp, tcp->u_arg[0]);
236         tprints(", ");
237         tprint_iov(tcp, len, tcp->u_arg[1], IOV_DECODE_STR);
238         tprintf(", %" PRI_klu ", ", len);
239         print_lld_from_low_high_val(tcp, 3);
240         if (flags_arg >= 0) {
241                 tprints(", ");
242                 printflags(rwf_flags, tcp->u_arg[flags_arg], "RWF_???");
243         }
244
245         return RVAL_DECODED;
246 }
247
248 SYS_FUNC(pwritev)
249 {
250         return do_pwritev(tcp, -1);
251 }
252
253 /*
254  * x32 is the only architecture where preadv2 takes 5 arguments
255  * instead of 6, see preadv64v2 in kernel sources.
256  * Likewise, x32 is the only architecture where pwritev2 takes 5 arguments
257  * instead of 6, see pwritev64v2 in kernel sources.
258  */
259
260 #if defined X86_64
261 # define PREADV2_PWRITEV2_FLAGS_ARG_NO (current_personality == 2 ? 4 : 5)
262 #elif defined X32
263 # define PREADV2_PWRITEV2_FLAGS_ARG_NO (current_personality == 0 ? 4 : 5)
264 #else
265 # define PREADV2_PWRITEV2_FLAGS_ARG_NO 5
266 #endif
267
268 SYS_FUNC(preadv2)
269 {
270         return do_preadv(tcp, PREADV2_PWRITEV2_FLAGS_ARG_NO);
271 }
272
273 SYS_FUNC(pwritev2)
274 {
275         return do_pwritev(tcp, PREADV2_PWRITEV2_FLAGS_ARG_NO);
276 }
277
278 #include "xlat/splice_flags.h"
279
280 SYS_FUNC(tee)
281 {
282         /* int fd_in */
283         printfd(tcp, tcp->u_arg[0]);
284         tprints(", ");
285         /* int fd_out */
286         printfd(tcp, tcp->u_arg[1]);
287         tprints(", ");
288         /* size_t len */
289         tprintf("%" PRI_klu ", ", tcp->u_arg[2]);
290         /* unsigned int flags */
291         printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
292
293         return RVAL_DECODED;
294 }
295
296 SYS_FUNC(splice)
297 {
298         /* int fd_in */
299         printfd(tcp, tcp->u_arg[0]);
300         tprints(", ");
301         /* loff_t *off_in */
302         printnum_int64(tcp, tcp->u_arg[1], "%" PRId64);
303         tprints(", ");
304         /* int fd_out */
305         printfd(tcp, tcp->u_arg[2]);
306         tprints(", ");
307         /* loff_t *off_out */
308         printnum_int64(tcp, tcp->u_arg[3], "%" PRId64);
309         tprints(", ");
310         /* size_t len */
311         tprintf("%" PRI_klu ", ", tcp->u_arg[4]);
312         /* unsigned int flags */
313         printflags(splice_flags, tcp->u_arg[5], "SPLICE_F_???");
314
315         return RVAL_DECODED;
316 }
317
318 SYS_FUNC(vmsplice)
319 {
320         /* int fd */
321         printfd(tcp, tcp->u_arg[0]);
322         tprints(", ");
323         /* const struct iovec *iov, unsigned long nr_segs */
324         tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], IOV_DECODE_STR);
325         tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
326         /* unsigned int flags */
327         printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
328
329         return RVAL_DECODED;
330 }