Skip to contentSkip to conversation

strace rework

PATCH In review Thayne Harbaugh 2 participants 6 messages Oct 22, 2007 – Nov 1, 2007 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 strace rework

This is a rework of Stuart Anderson's strace patch. I've fixed target-to-host and host-to-target syscall lookups so that the proper host or target errno is returned.

Makefile.target +1 -1
Index: qemu/Makefile.target
===================================================================
@@ -249,7 +249,7 @@
 endif
 
 ifdef CONFIG_LINUX_USER
-OBJS= main.o syscall.o mmap.o signal.o path.o osdep.o thunk.o \
+OBJS= main.o syscall.o strace.o mmap.o signal.o path.o osdep.o thunk.o \
       elfload.o linuxload.o
 LIBS+= $(AIOLIBS)
 ifdef TARGET_HAS_BFLT
linux-user/syscall.c +33 -2
Index: qemu/linux-user/syscall.c
===================================================================
@@ -249,11 +249,18 @@
 extern int getresgid(gid_t *, gid_t *, gid_t *);
 extern int setgroups(int, gid_t *);
 
+#define ERRNO_TABLE_SIZE 1200
+
+/* target_to_host_errno_table[] is initialized from
+ * host_to_target_errno_table[] in syscall_init(). */
+static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
+};
+
 /*
  * This list is the union of errno values overridden in asm-<arch>/errno.h
  * minus the errnos that are not actually generic to all archs.
  */
-static uint16_t host_to_target_errno_table[1200] = {
+static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
     [EIDRM]		= TARGET_EIDRM,
     [ECHRNG]		= TARGET_ECHRNG,
     [EL2NSYNC]		= TARGET_EL2NSYNC,
@@ -359,7 +366,7 @@
 #ifdef ENOTRECOVERABLE
     [ENOTRECOVERABLE]	= TARGET_ENOTRECOVERABLE,
 #endif
-	};
+};
 
 static inline int host_to_target_errno(int err)
 {
@@ -368,6 +375,13 @@
     return err;
 }
 
+static inline int target_to_host_errno(int err)
+{
+    if (target_to_host_errno_table[err])
+        return target_to_host_errno_table[err];
+    return err;
+}
+
 static inline target_long get_errno(target_long ret)
 {
     if (ret == -1)
@@ -381,6 +395,11 @@
     return (target_ulong)ret >= (target_ulong)(-4096);
 }
 
+char *target_strerror(int err)
+{
+    return strerror(target_to_host_errno(err));
+}
+
 static target_ulong target_brk;
 static target_ulong target_original_brk;
 
@@ -2458,6 +2477,7 @@
     IOCTLEntry *ie;
     const argtype *arg_type;
     int size;
+    int i;
 
 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
@@ -2483,6 +2503,12 @@
                               ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
                 (size << TARGET_IOC_SIZESHIFT);
         }
+
+        /* Build target_to_host_errno_table[] table from
+         * host_to_target_errno_table[]. */
+        for (i=0; i < ERRNO_TABLE_SIZE; i++)
+                target_to_host_errno_table[host_to_target_errno_table[i]] = i;
+
         /* automatic consistency check if same arch */
 #if defined(__i386__) && defined(TARGET_I386)
         if (ie->target_cmd != ie->host_cmd) {
@@ -2582,6 +2608,9 @@
 #ifdef DEBUG
     gemu_log("syscall %d", num);
 #endif
+    if(do_strace)
+        print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
+
     switch(num) {
     case TARGET_NR_exit:
 #ifdef HAVE_GPROF
@@ -5016,5 +5045,7 @@
 #ifdef DEBUG
     gemu_log(" = %ld\n", ret);
 #endif
+    if(do_strace)
+        print_syscall_ret(num, ret);
     return ret;
 }
linux-user/main.c +4 -0
Index: qemu/linux-user/main.c
===================================================================
@@ -1927,6 +1927,10 @@
         putenv("LD_LIBRARY_PATH=/usr/lib/libfakeroot:/usr/lib64/libfakeroot:/usr/lib32/libfakeroot");
     }
 
+    if(getenv("QEMU_STRACE") ){
+      do_strace=1;
+    }
+
     wrk = environ;
     while (*(wrk++))
         environ_count++;
linux-user/qemu.h +6 -0
Index: qemu/linux-user/qemu.h
===================================================================
@@ -138,10 +138,16 @@
 void cpu_loop(CPUState *env);
 void init_paths(const char *prefix);
 const char *path(const char *pathname);
+char *target_strerror(int err);
 
 extern int loglevel;
 extern FILE *logfile;
 
+/* strace.c */
+void print_syscall(int num, long arg1, long arg2, long arg3, long arg4, long arg5, long arg6);
+void print_syscall_ret(int num, long arg1);
+extern int do_strace;
+
 /* signal.c */
 void process_pending_signals(void *cpu_env);
 void signal_init(void);
linux-user/strace.c new file +257 -0

257 lines not on this page · raw message

linux-user/strace.list new file +1515 -0

1,515 lines not on this page · raw message

#1 lore raw
Quoted 3 lines · Thayne Harbaugh

This is a rework of Stuart Anderson's strace patch. I've fixed target-to-host and host-to-target syscall lookups so that the proper host or target errno is returned.

It didn't build for me due to the a missing target_to_host_errno function. Could you also have a look at the compiler warnings from strace.c? I see e.g.:

gcc-3.4 -Wall -O2 -g -fno-strict-aliasing -I. -I.. -I/home/ths/qemu/qemu-work/target-ppc -I/home/ths/qemu/qemu-work -I/home/ths/qemu/qemu-work/linux-user -I/home/ths/qemu/qemu-work/linux-user/ppc -D__powerpc__ -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -I/home/ths/qemu/qemu-work/fpu -DHAS_AUDIO -DHAS_AUDIO_CHOICE -I/home/ths/qemu/qemu-work/slirp -c -o strace.o /home/ths/qemu/qemu-work/linux-user/strace.c
/home/ths/qemu/qemu-work/linux-user/strace.c: In function `print_newselect':
/home/ths/qemu/qemu-work/linux-user/strace.c:111: warning: int format, long int arg (arg 3) /home/ths/qemu/qemu-work/linux-user/strace.c:112: warning: passing arg 2 of print_fdset' makes pointer from integer without a cast /home/ths/qemu/qemu-work/linux-user/strace.c:114: warning: passing arg 2 of print_fdset' makes pointer from integer without a cast /home/ths/qemu/qemu-work/linux-user/strace.c:116: warning: passing arg 2 of print_fdset' makes pointer from integer without a cast /home/ths/qemu/qemu-work/linux-user/strace.c:118: warning: passing arg 1 of print_timeval' makes pointer from integer without a cast /home/ths/qemu/qemu-work/linux-user/strace.c: In function `print_semctl':
/home/ths/qemu/qemu-work/linux-user/strace.c:133: warning: int format, long int arg (arg 3) /home/ths/qemu/qemu-work/linux-user/strace.c:133: warning: int format, long int arg (arg 4) /home/ths/qemu/qemu-work/linux-user/strace.c:135: warning: format argument is not a pointer (arg 2) /home/ths/qemu/qemu-work/linux-user/strace.c: In function `print_execve':
/home/ths/qemu/qemu-work/linux-user/strace.c:142: warning: initialization makes pointer from integer without a cast /home/ths/qemu/qemu-work/linux-user/strace.c:145: warning: format argument is not a pointer (arg 3) /home/ths/qemu/qemu-work/linux-user/strace.c: In function `print_ipc':
/home/ths/qemu/qemu-work/linux-user/strace.c:164: warning: int format, long int arg (arg 3) /home/ths/qemu/qemu-work/linux-user/strace.c:164: warning: int format, long int arg (arg 4) /home/ths/qemu/qemu-work/linux-user/strace.c:164: warning: int format, long int arg (arg 5) /home/ths/qemu/qemu-work/linux-user/strace.c:164: warning: int format, long int arg (arg 6) /home/ths/qemu/qemu-work/linux-user/strace.c: In function `print_syscall_ret_addr':
/home/ths/qemu/qemu-work/linux-user/strace.c:178: warning: unsigned int format, long int arg (arg 2) /home/ths/qemu/qemu-work/linux-user/strace.c: In function `print_syscall_ret_raw':
/home/ths/qemu/qemu-work/linux-user/strace.c:185: warning: unsigned int format, long int arg (arg 2) /home/ths/qemu/qemu-work/linux-user/strace.c: In function `print_syscall_ret_newselect':
/home/ths/qemu/qemu-work/linux-user/strace.c:191: warning: unsigned int format, long int arg (arg 2) /home/ths/qemu/qemu-work/linux-user/strace.c:192: warning: passing arg 2 of print_fdset' makes pointer from integer without a cast /home/ths/qemu/qemu-work/linux-user/strace.c:194: warning: passing arg 2 of print_fdset' makes pointer from integer without a cast /home/ths/qemu/qemu-work/linux-user/strace.c:196: warning: passing arg 2 of print_fdset' makes pointer from integer without a cast /home/ths/qemu/qemu-work/linux-user/strace.c:198: warning: passing arg 1 of print_timeval' makes pointer from integer without a cast /home/ths/qemu/qemu-work/linux-user/strace.c: In function `print_syscall':
/home/ths/qemu/qemu-work/linux-user/strace.c:222: warning: implicit declaration of function getpid' /home/ths/qemu/qemu-work/linux-user/strace.c: In function print_syscall_ret':
/home/ths/qemu/qemu-work/linux-user/strace.c:249: warning: int format, long int arg (arg 2) /home/ths/qemu/qemu-work/linux-user/strace.c:251: warning: int format, long int arg (arg 2) /home/ths/qemu/qemu-work/linux-user/strace.c: At top level:
/home/ths/qemu/qemu-work/linux-user/strace.c:184: warning: 'print_syscall_ret_raw' defined but not used

Thiemo

#2 lore raw
Quoted 7 lines · Thiemo Seufer
Quoted 3 lines · Thayne Harbaugh

This is a rework of Stuart Anderson's strace patch. I've fixed target-to-host and host-to-target syscall lookups so that the proper host or target errno is returned.

It didn't build for me due to the a missing target_to_host_errno function.

This isn't obvious to me. It also doesn't appear that you included this error in the output you sent.

Quoted 2 lines

Could you also have a look at the compiler warnings
from strace.c? I see e.g.:

<miles of warnings deleted>

Thank you for sending me the warnings. I've reworked it so that function arguments are passed correctly, target addresses have access checked and locked and that proper print format codes are used.

Attached is the new version. Please send me feedback.

Thank you.

Makefile.target +1 -1
Index: qemu/Makefile.target
===================================================================
@@ -249,7 +249,7 @@
 endif
 
 ifdef CONFIG_LINUX_USER
-OBJS= main.o syscall.o mmap.o signal.o path.o osdep.o thunk.o \
+OBJS= main.o syscall.o strace.o mmap.o signal.o path.o osdep.o thunk.o \
       elfload.o linuxload.o
 LIBS+= $(AIOLIBS)
 ifdef TARGET_HAS_BFLT
linux-user/syscall.c +33 -2
Index: qemu/linux-user/syscall.c
===================================================================
@@ -249,11 +249,18 @@
 extern int getresgid(gid_t *, gid_t *, gid_t *);
 extern int setgroups(int, gid_t *);
 
+#define ERRNO_TABLE_SIZE 1200
+
+/* target_to_host_errno_table[] is initialized from
+ * host_to_target_errno_table[] in syscall_init(). */
+static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
+};
+
 /*
  * This list is the union of errno values overridden in asm-<arch>/errno.h
  * minus the errnos that are not actually generic to all archs.
  */
-static uint16_t host_to_target_errno_table[1200] = {
+static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
     [EIDRM]		= TARGET_EIDRM,
     [ECHRNG]		= TARGET_ECHRNG,
     [EL2NSYNC]		= TARGET_EL2NSYNC,
@@ -359,7 +366,7 @@
 #ifdef ENOTRECOVERABLE
     [ENOTRECOVERABLE]	= TARGET_ENOTRECOVERABLE,
 #endif
-	};
+};
 
 static inline int host_to_target_errno(int err)
 {
@@ -368,6 +375,13 @@
     return err;
 }
 
+static inline int target_to_host_errno(int err)
+{
+    if (target_to_host_errno_table[err])
+        return target_to_host_errno_table[err];
+    return err;
+}
+
 static inline target_long get_errno(target_long ret)
 {
     if (ret == -1)
@@ -381,6 +395,11 @@
     return (target_ulong)ret >= (target_ulong)(-4096);
 }
 
+char *target_strerror(int err)
+{
+    return strerror(target_to_host_errno(err));
+}
+
 static target_ulong target_brk;
 static target_ulong target_original_brk;
 
@@ -2458,6 +2477,7 @@
     IOCTLEntry *ie;
     const argtype *arg_type;
     int size;
+    int i;
 
 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
@@ -2483,6 +2503,12 @@
                               ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
                 (size << TARGET_IOC_SIZESHIFT);
         }
+
+        /* Build target_to_host_errno_table[] table from
+         * host_to_target_errno_table[]. */
+        for (i=0; i < ERRNO_TABLE_SIZE; i++)
+                target_to_host_errno_table[host_to_target_errno_table[i]] = i;
+
         /* automatic consistency check if same arch */
 #if defined(__i386__) && defined(TARGET_I386)
         if (ie->target_cmd != ie->host_cmd) {
@@ -2582,6 +2608,9 @@
 #ifdef DEBUG
     gemu_log("syscall %d", num);
 #endif
+    if(do_strace)
+        print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
+
     switch(num) {
     case TARGET_NR_exit:
 #ifdef HAVE_GPROF
@@ -5016,5 +5045,7 @@
 #ifdef DEBUG
     gemu_log(" = %ld\n", ret);
 #endif
+    if(do_strace)
+        print_syscall_ret(num, ret);
     return ret;
 }
linux-user/main.c +4 -0
Index: qemu/linux-user/main.c
===================================================================
@@ -1927,6 +1927,10 @@
         putenv("LD_LIBRARY_PATH=/usr/lib/libfakeroot:/usr/lib64/libfakeroot:/usr/lib32/libfakeroot");
     }
 
+    if(getenv("QEMU_STRACE") ){
+      do_strace=1;
+    }
+
     wrk = environ;
     while (*(wrk++))
         environ_count++;
linux-user/qemu.h +8 -0
Index: qemu/linux-user/qemu.h
===================================================================
@@ -138,10 +138,18 @@
 void cpu_loop(CPUState *env);
 void init_paths(const char *prefix);
 const char *path(const char *pathname);
+char *target_strerror(int err);
 
 extern int loglevel;
 extern FILE *logfile;
 
+/* strace.c */
+void print_syscall(int num,
+                   target_long arg1, target_long arg2, target_long arg3,
+                   target_long arg4, target_long arg5, target_long arg6);
+void print_syscall_ret(int num, target_long arg1);
+extern int do_strace;
+
 /* signal.c */
 void process_pending_signals(void *cpu_env);
 void signal_init(void);
linux-user/strace.c new file +299 -0

299 lines not on this page · raw message

linux-user/strace.list new file +1515 -0

1,515 lines not on this page · raw message

#3 lore raw
Quoted 16 lines · Thayne Harbaugh
Quoted 7 lines · Thiemo Seufer
Quoted 3 lines · Thayne Harbaugh

This is a rework of Stuart Anderson's strace patch. I've fixed target-to-host and host-to-target syscall lookups so that the proper host or target errno is returned.

It didn't build for me due to the a missing target_to_host_errno function.

This isn't obvious to me. It also doesn't appear that you included this error in the output you sent.

Quoted 2 lines

Could you also have a look at the compiler warnings
from strace.c? I see e.g.:

Quoted 7 lines

<miles of warnings deleted>

Thank you for sending me the warnings. I've reworked it so that function arguments are passed correctly, target addresses have access checked and locked and that proper print format codes are used.

Attached is the new version. Please send me feedback.

Comitted. Could you also cook up a patch which documents the feature?

Thiemo

#4 lore raw
Quoted 19 lines · Thiemo Seufer
Quoted 18 lines · Thayne Harbaugh
Quoted 7 lines · Thiemo Seufer
Quoted 3 lines · Thayne Harbaugh

This is a rework of Stuart Anderson's strace patch. I've fixed target-to-host and host-to-target syscall lookups so that the proper host or target errno is returned.

It didn't build for me due to the a missing target_to_host_errno function.

This isn't obvious to me. It also doesn't appear that you included this error in the output you sent.

Quoted 2 lines

Could you also have a look at the compiler warnings
from strace.c? I see e.g.:

<miles of warnings deleted>

Quoted 7 lines
Quoted 5 lines

Thank you for sending me the warnings. I've reworked it so that function arguments are passed correctly, target addresses have access checked and locked and that proper print format codes are used.

Attached is the new version. Please send me feedback.

Comitted. Could you also cook up a patch which documents the feature?

Awesome! Thank you.

Yes, I'll be writing piles of documentation from user docs to development code guides. Is there a directory that we can dump things into?

#5 lore raw
Quoted 27 lines · Thayne Harbaugh
Quoted 25 lines · Thiemo Seufer
Quoted 23 lines · Thayne Harbaugh
Quoted 7 lines · Thiemo Seufer
Quoted 3 lines · Thayne Harbaugh

This is a rework of Stuart Anderson's strace patch. I've fixed target-to-host and host-to-target syscall lookups so that the proper host or target errno is returned.

It didn't build for me due to the a missing target_to_host_errno function.

This isn't obvious to me. It also doesn't appear that you included this error in the output you sent.

Quoted 2 lines

Could you also have a look at the compiler warnings
from strace.c? I see e.g.:

<miles of warnings deleted>

Thank you for sending me the warnings. I've reworked it so that function arguments are passed correctly, target addresses have access checked and locked and that proper print format codes are used.

Attached is the new version. Please send me feedback.

Quoted 7 lines
Quoted 1 line

Comitted. Could you also cook up a patch which documents the feature?

Awesome! Thank you.

Yes, I'll be writing piles of documentation from user docs to development code guides. Is there a directory that we can dump things into?

Not yet, but you can send a patch which creates a doc subdirectory. :-)

Thiemo