]> granicus.if.org Git - strace/blob - io.c
Merge Harald Böhme's solaris patches
[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  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  *      $Id$
31  */
32
33 #include "defs.h"
34
35 #include <fcntl.h>
36 #include <sys/uio.h>
37
38 int
39 sys_read(tcp)
40 struct tcb *tcp;
41 {
42         if (entering(tcp)) {
43                 tprintf("%ld, ", tcp->u_arg[0]);
44         } else {
45                 if (syserror(tcp))
46                         tprintf("%#lx", tcp->u_arg[1]);
47                 else
48                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
49                 tprintf(", %lu", tcp->u_arg[2]);
50         }
51         return 0;
52 }
53
54 int
55 sys_write(tcp)
56 struct tcb *tcp;
57 {
58         if (entering(tcp)) {
59                 tprintf("%ld, ", tcp->u_arg[0]);
60                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
61                 tprintf(", %lu", tcp->u_arg[2]);
62         }
63         return 0;
64 }
65
66 void
67 tprint_iov(tcp, len, addr)
68 struct tcb * tcp;
69 int len;
70 char * addr;
71 {
72         struct iovec *iov;
73         int i;
74
75
76         if (!len) {
77                 tprintf("[]");
78                 return;
79         }
80           
81         if ((iov = (struct iovec *) malloc(len * sizeof *iov)) == NULL) {
82                 fprintf(stderr, "No memory");
83                 return;
84         }
85         if (umoven(tcp, (int) addr,
86                    len * sizeof *iov, (char *) iov) < 0) {
87                 tprintf("%#lx", tcp->u_arg[1]);
88         } else {
89                 tprintf("[");
90                 for (i = 0; i < len; i++) {
91                         if (i)
92                                 tprintf(", ");
93                         tprintf("{");
94                         printstr(tcp, (long) iov[i].iov_base,
95                                 iov[i].iov_len);
96                         tprintf(", %lu}", (unsigned long)iov[i].iov_len);
97                 }
98                 tprintf("]");
99         }
100         free((char *) iov);
101 }
102
103 int
104 sys_readv(tcp)
105 struct tcb *tcp;
106 {
107         if (entering(tcp)) {
108                 tprintf("%ld, ", tcp->u_arg[0]);
109         } else {
110                 if (syserror(tcp)) {
111                         tprintf("%#lx, %lu",
112                                         tcp->u_arg[1], tcp->u_arg[2]);
113                         return 0;
114                 }
115                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
116                 tprintf(", %lu", tcp->u_arg[2]);
117         }
118         return 0;
119 }
120
121 int
122 sys_writev(tcp)
123 struct tcb *tcp;
124 {
125         if (entering(tcp)) {
126                 tprintf("%ld, ", tcp->u_arg[0]);
127                 tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
128                 tprintf(", %lu", tcp->u_arg[2]);
129         }
130         return 0;
131 }
132
133 #if defined(SVR4) || defined(FREEBSD)
134
135 int
136 sys_pread(tcp)
137 struct tcb *tcp;
138 {
139         if (entering(tcp)) {
140                 tprintf("%ld, ", tcp->u_arg[0]);
141         } else {
142                 if (syserror(tcp))
143                         tprintf("%#lx", tcp->u_arg[1]);
144                 else
145                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
146 #if UNIXWARE
147                 /* off_t is signed int */
148                 tprintf(", %lu, %ld", tcp->u_arg[2], tcp->u_arg[3]);
149 #else
150 #ifndef FREEBSD
151                 tprintf(", %lu, %llu", tcp->u_arg[2],
152                                 (((unsigned long long) tcp->u_arg[4]) << 32
153                                  | tcp->u_arg[3]));
154 #else
155                 tprintf(", %lu, %llu", tcp->u_arg[2], 
156                                 (((off_t) tcp->u_arg[3]) << 32) +  tcp->u_arg[4]);
157 #endif
158 #endif
159         }
160         return 0;
161 }
162
163 int
164 sys_pwrite(tcp)
165 struct tcb *tcp;
166 {
167         if (entering(tcp)) {
168                 tprintf("%ld, ", tcp->u_arg[0]);
169                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
170 #if UNIXWARE
171                 /* off_t is signed int */
172                 tprintf(", %lu, %ld", tcp->u_arg[2], tcp->u_arg[3]);
173 #else
174 #ifndef FREEBSD
175                 tprintf(", %lu, %llu", tcp->u_arg[2],
176                                 (((unsigned long long) tcp->u_arg[4]) << 32
177                                  | tcp->u_arg[3]));
178 #else
179                 tprintf(", %lu, %llu", tcp->u_arg[2],
180                                 (((off_t) tcp->u_arg[3]) << 32) + tcp->u_arg[4]);
181 #endif
182 #endif
183         }
184         return 0;
185 }
186 #endif /* SVR4 || FREEBSD */
187
188 #ifdef FREEBSD
189 #include <sys/types.h>
190 #include <sys/socket.h>
191
192 int
193 sys_sendfile(tcp)
194 struct tcb *tcp;
195 {
196         if (entering(tcp)) {
197                 tprintf("%ld, %ld, %llu, %lu", tcp->u_arg[0], tcp->u_arg[1],
198                         (((unsigned long long) tcp->u_arg[3]) << 32 |
199                          tcp->u_arg[2]), tcp->u_arg[4]);
200         } else {
201                 off_t offset;
202
203                 if (!tcp->u_arg[5])
204                         tprintf(", NULL");
205                 else {
206                         struct sf_hdtr hdtr;
207
208                         if (umove(tcp, tcp->u_arg[5], &hdtr) < 0)
209                                 tprintf(", %#lx", tcp->u_arg[5]);
210                         else {
211                                 tprintf(", { ");
212                                 tprint_iov(tcp, hdtr.hdr_cnt, hdtr.headers);
213                                 tprintf(", %u, ", hdtr.hdr_cnt);
214                                 tprint_iov(tcp, hdtr.trl_cnt, hdtr.trailers);
215                                 tprintf(", %u }", hdtr.hdr_cnt);
216                         }
217                 }
218                 if (!tcp->u_arg[6])
219                         tprintf(", NULL");
220                 else if (umove(tcp, tcp->u_arg[6], &offset) < 0)
221                         tprintf(", %#lx", tcp->u_arg[6]);
222                 else
223                         tprintf(", [%llu]", offset);
224                 tprintf(", %lu", tcp->u_arg[7]);
225         }
226         return 0;
227 }
228 #endif /* FREEBSD */
229
230 #ifdef LINUX
231 int
232 sys_pread(tcp)
233 struct tcb *tcp;
234 {
235         if (entering(tcp)) {
236                 tprintf("%ld, ", tcp->u_arg[0]);
237         } else {
238                 if (syserror(tcp))
239                         tprintf("%#lx", tcp->u_arg[1]);
240                 else
241                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
242                 tprintf(", %lu, %llu", tcp->u_arg[2],
243                         *(unsigned long long *)&tcp->u_arg[3]);
244         }
245         return 0;
246 }
247
248 int
249 sys_pwrite(tcp)
250 struct tcb *tcp;
251 {
252         if (entering(tcp)) {
253                 tprintf("%ld, ", tcp->u_arg[0]);
254                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
255                 tprintf(", %lu, %llu", tcp->u_arg[2],
256                         *(unsigned long long *)&tcp->u_arg[3]);
257         }
258         return 0;
259 }
260
261 int
262 sys_sendfile(tcp)
263 struct tcb *tcp;
264 {
265         if (entering(tcp)) {
266                 off_t offset;
267
268                 tprintf("%ld, %ld, ", tcp->u_arg[0], tcp->u_arg[1]);
269                 if (!tcp->u_arg[2])
270                         tprintf("NULL");
271                 else if (umove(tcp, tcp->u_arg[2], &offset) < 0)
272                         tprintf("%#lx", tcp->u_arg[2]);
273                 else
274                         tprintf("[%lu]", offset);
275                 tprintf(", %lu", tcp->u_arg[3]);
276         }
277         return 0;
278 }
279
280 #endif /* LINUX */
281
282 #if _LFS64_LARGEFILE
283 int
284 sys_pread64(tcp)
285 struct tcb *tcp;
286 {
287         if (entering(tcp)) {
288                 tprintf("%ld, ", tcp->u_arg[0]);
289         } else {
290                 if (syserror(tcp))
291                         tprintf("%#lx", tcp->u_arg[1]);
292                 else
293                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
294                 tprintf(", %lu, %#llx", tcp->u_arg[2], get64(tcp->u_arg[3], tcp->u_arg[4]));
295         }
296         return 0;
297 }
298
299 int
300 sys_pwrite64(tcp)
301 struct tcb *tcp;
302 {
303         if (entering(tcp)) {
304                 tprintf("%ld, ", tcp->u_arg[0]);
305                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
306                 tprintf(", %lu, %#llx", tcp->u_arg[2], get64(tcp->u_arg[3], tcp->u_arg[4]));
307         }
308         return 0;
309 }
310 #endif
311  
312 int
313 sys_ioctl(tcp)
314 struct tcb *tcp;
315 {
316         char *symbol;
317
318         if (entering(tcp)) {
319                 tprintf("%ld, ", tcp->u_arg[0]);
320                 symbol = ioctl_lookup(tcp->u_arg[1]);
321                 if (symbol)
322                         tprintf("%s", symbol);
323                 else
324                         tprintf("%#lx", tcp->u_arg[1]);
325                 ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]);
326         }
327         else {
328                 if (ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]) == 0)
329                         tprintf(", %#lx", tcp->u_arg[2]);
330         }
331         return 0;
332 }