]> granicus.if.org Git - strace/blob - lseek.c
x32: fix decoding of i386 personality lseek syscall
[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 SYS_FUNC(lseek)
14 {
15         long long offset;
16         int whence;
17
18         printfd(tcp, tcp->u_arg[0]);
19 # ifdef X32
20         /* tcp->ext_arg is not initialized for i386 personality */
21         if (current_personality == 1)
22                 offset = tcp->u_arg[1];
23         else
24 # endif
25         offset = tcp->ext_arg[1];
26         whence = tcp->u_arg[2];
27         if (whence == SEEK_SET)
28                 tprintf(", %llu, ", offset);
29         else
30                 tprintf(", %lld, ", offset);
31         printxval(whence_codes, whence, "SEEK_???");
32
33         return RVAL_DECODED | RVAL_LUDECIMAL;
34 }
35 #else
36 SYS_FUNC(lseek)
37 {
38         long offset;
39         int whence;
40
41         printfd(tcp, tcp->u_arg[0]);
42         offset = tcp->u_arg[1];
43         whence = tcp->u_arg[2];
44         if (whence == SEEK_SET)
45                 tprintf(", %lu, ", offset);
46         else
47                 tprintf(", %ld, ", offset);
48         printxval(whence_codes, whence, "SEEK_???");
49
50         return RVAL_DECODED | RVAL_UDECIMAL;
51 }
52 #endif
53
54 /* llseek syscall takes explicitly two ulong arguments hi, lo,
55  * rather than one 64-bit argument for which LONG_LONG works
56  * appropriate for the native byte order.
57  *
58  * See kernel's fs/read_write.c::SYSCALL_DEFINE5(llseek, ...)
59  *
60  * hi,lo are "unsigned longs" and combined exactly this way in kernel:
61  * ((loff_t) hi << 32) | lo
62  * Note that for architectures with kernel's long wider than userspace long
63  * (such as x32), combining code will use *kernel's*, i.e. *wide* longs
64  * for hi and lo. We would need to use tcp->ext_arg[N] on x32...
65  * ...however, x32 (and x86_64) does not _have_ llseek syscall as such.
66  */
67 SYS_FUNC(llseek)
68 {
69         if (entering(tcp)) {
70                 printfd(tcp, tcp->u_arg[0]);
71                 if (tcp->u_arg[4] == SEEK_SET)
72                         tprintf(", %llu, ",
73                                 ((long long) tcp->u_arg[1]) << 32 |
74                                 (unsigned long long) (unsigned) tcp->u_arg[2]);
75                 else
76                         tprintf(", %lld, ",
77                                 ((long long) tcp->u_arg[1]) << 32 |
78                                 (unsigned long long) (unsigned) tcp->u_arg[2]);
79         } else {
80                 printnum_int64(tcp, tcp->u_arg[3], "%" PRIu64);
81                 tprints(", ");
82                 printxval(whence_codes, tcp->u_arg[4], "SEEK_???");
83         }
84         return 0;
85 }