[PATCH] fix fcntl(F_SETLK) display for 32-bit apps on x86_64

Denys Vlasenko dvlasenk at redhat.com
Tue Nov 25 16:00:35 UTC 2008


Hi,

Before this patch strace on x86_64, when stracing 32-bit app,
shows fcntl(F_SETLK) incorrectly:

# cat -n testfcntl.c
     1  #include <stdio.h>
     2  #include <stdlib.h>
     3  #include <string.h>
     4  #include <unistd.h>
     5  #include <errno.h>
     6  #include <fcntl.h>
     7
     8  int main(int argc, char **argv)
     9  {
    10          int filedes;
    11          struct flock flockstruct;
    12
    13          filedes = open("./testfile.txt", O_RDWR | O_CREAT | O_TRUNC);
    14          if (filedes < 0) {
    15                  perror("Unable to open file");
    16                  return 1;
    17          }
    18          printf("File exists\n");
    19          flockstruct.l_type = F_WRLCK;
    20          flockstruct.l_whence = SEEK_SET;
    21          flockstruct.l_start = 0;
    22          flockstruct.l_len = 0;
    23          if ((fcntl(filedes, F_SETLK, &flockstruct)) == -1) {
    24                  printf("Lock failed!\n");
    25                  return 1;
    26          }
    27          printf("Lock worked\n");
    28          return 0;
    29  }

# i486-linux-uclibc-gcc -Os testfcntl.c

# strace ./a.out
execve("./a.out", ["./a.out"], [/* 55 vars */]) = 0
[ Process PID=17315 runs in 32 bit mode. ]
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
open("./testfile.txt", O_RDWR|O_CREAT|O_TRUNC, 02703611000) = 4
write(1, "File exists\n", 12File exists
)           = 12
fcntl(4, F_SETLK, {type=F_WRLCK, whence=SEEK_SET, start=0, len=4294892232}) = 0
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
write(1, "Lock worked\n", 12Lock worked
)           = 12
_exit(0)                                = ?


Patched strace:

# ./strace ./a.out
execve("./a.out", ["./a.out"], [/* 55 vars */]) = 0
[ Process PID=16942 runs in 32 bit mode. ]
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0
open("./testfile.txt", O_RDWR|O_CREAT|O_TRUNC, 02703611000) = 4
write(1, "File exists\n", 12File exists
)           = 12
fcntl(4, F_SETLK, {type=F_WRLCK, whence=SEEK_SET, start=0, len=0}) = 0
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
write(1, "Lock worked\n", 12Lock worked
)           = 12
_exit(0)                                = ?


Patch is below. Please apply.
--
vda


diff -d -urpN strace.0/desc.c strace.1/desc.c
--- strace.0/desc.c	2008-11-10 23:53:02.000000000 +0100
+++ strace.1/desc.c	2008-11-25 13:50:19.000000000 +0100
@@ -200,8 +200,39 @@ int getlk;
 {
 	struct flock fl;
 
-	if (umove(tcp, addr, &fl) < 0) {
-		tprintf("{...}");
+	if (personality_wordsize[current_personality] == sizeof(fl.l_start))
+	{
+		if (umove(tcp, addr, &fl) < 0)
+		{
+			tprintf("{...}");
+			return;
+		}
+	}
+	else if (personality_wordsize[current_personality] == 4)
+	{
+		/* 32-bit x86 app on x86_64 and similar cases */
+		struct {
+			short int l_type;
+			short int l_whence;
+			int32_t l_start; /* off_t */
+			int32_t l_len; /* off_t */
+			int32_t l_pid; /* pid_t */
+		} fl32;
+		if (umove(tcp, addr, &fl32) < 0)
+		{
+			tprintf("{...}");
+			return;
+		}
+		fl.l_type = fl32.l_type;
+		fl.l_whence = fl32.l_whence;
+		fl.l_start = fl32.l_start;
+		fl.l_len = fl32.l_len;
+		fl.l_pid = fl32.l_pid;
+	}
+	else {
+		/* let people know we have a problem here */
+		tprintf("{ <decode error: unsupported wordsize %d> }",
+				personality_wordsize[current_personality]);
 		return;
 	}
 	tprintf("{type=");






More information about the Strace-devel mailing list