]> granicus.if.org Git - strace/blob - lseek.c
Fix ILP32 personality preadv/pwritev offset decoding on LP64 architectures
[strace] / lseek.c
1 #include "defs.h"
2
3 #include "xlat/whence_codes.h"
4
5 /* Linux kernel has exactly one version of lseek:
6  * fs/read_write.c::SYSCALL_DEFINE3(lseek, unsigned, fd, off_t, offset, unsigned, origin)
7  * In kernel, off_t is always the same as (kernel's) long
8  * (see include/uapi/asm-generic/posix_types.h),
9  * which means that on x32 we need to use tcp->ext_arg[N] to get offset argument.
10  * Use test/x32_lseek.c to test lseek decoding.
11  */
12 #if defined(LINUX_MIPSN32) || defined(X32)
13 int
14 sys_lseek(struct tcb *tcp)
15 {
16         long long offset;
17         int whence;
18
19         if (entering(tcp)) {
20                 printfd(tcp, tcp->u_arg[0]);
21                 offset = tcp->ext_arg[1];
22                 whence = tcp->u_arg[2];
23                 if (whence == SEEK_SET)
24                         tprintf(", %llu, ", offset);
25                 else
26                         tprintf(", %lld, ", offset);
27                 printxval(whence_codes, whence, "SEEK_???");
28         }
29         return RVAL_LUDECIMAL;
30 }
31 #else
32 int
33 sys_lseek(struct tcb *tcp)
34 {
35         long offset;
36         int whence;
37
38         if (entering(tcp)) {
39                 printfd(tcp, tcp->u_arg[0]);
40                 offset = tcp->u_arg[1];
41                 whence = tcp->u_arg[2];
42                 if (whence == SEEK_SET)
43                         tprintf(", %lu, ", offset);
44                 else
45                         tprintf(", %ld, ", offset);
46                 printxval(whence_codes, whence, "SEEK_???");
47         }
48         return RVAL_UDECIMAL;
49 }
50 #endif
51
52 /* llseek syscall takes explicitly two ulong arguments hi, lo,
53  * rather than one 64-bit argument for which LONG_LONG works
54  * appropriate for the native byte order.
55  *
56  * See kernel's fs/read_write.c::SYSCALL_DEFINE5(llseek, ...)
57  *
58  * hi,lo are "unsigned longs" and combined exactly this way in kernel:
59  * ((loff_t) hi << 32) | lo
60  * Note that for architectures with kernel's long wider than userspace long
61  * (such as x32), combining code will use *kernel's*, i.e. *wide* longs
62  * for hi and lo. We would need to use tcp->ext_arg[N] on x32...
63  * ...however, x32 (and x86_64) does not _have_ llseek syscall as such.
64  */
65 int
66 sys_llseek(struct tcb *tcp)
67 {
68         if (entering(tcp)) {
69                 printfd(tcp, tcp->u_arg[0]);
70                 if (tcp->u_arg[4] == SEEK_SET)
71                         tprintf(", %llu, ",
72                                 ((long long) tcp->u_arg[1]) << 32 |
73                                 (unsigned long long) (unsigned) tcp->u_arg[2]);
74                 else
75                         tprintf(", %lld, ",
76                                 ((long long) tcp->u_arg[1]) << 32 |
77                                 (unsigned long long) (unsigned) tcp->u_arg[2]);
78         }
79         else {
80                 long long off;
81                 if (syserror(tcp) || umove(tcp, tcp->u_arg[3], &off) < 0)
82                         tprintf("%#lx, ", tcp->u_arg[3]);
83                 else
84                         tprintf("[%llu], ", off);
85                 printxval(whence_codes, tcp->u_arg[4], "SEEK_???");
86         }
87         return 0;
88 }