Refactor LDT decoding

* configure.ac (AC_CHECK_TYPES): Remove struct user_desc.
* ldt.c: New file.
* Makefile.am (strace_SOURCES): Add ldt.c.
* mem.c: Do not include <asm/ldt.h>.
(print_ldt_entry): Remove.
(sys_modify_ldt, sys_set_thread_area, sys_get_thread_area): Move...
* ldt.c: ... here.
* process.c: Do not include <asm/ldt.h>.
(sys_clone) [I386 || X86_64 || X32]: Use print_user_desc.
diff --git a/Makefile.am b/Makefile.am
index 03d310b..a789b8c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -29,6 +29,7 @@
 	ipc.c		\
 	kexec.c		\
 	keyctl.c	\
+	ldt.c		\
 	loop.c		\
 	mem.c		\
 	mtd.c		\
diff --git a/configure.ac b/configure.ac
index 11d9be8..0969173 100644
--- a/configure.ac
+++ b/configure.ac
@@ -263,8 +263,6 @@
 AC_CHECK_TYPES([struct pt_all_user_regs, struct ia64_fpreg, struct ptrace_peeksiginfo_args],,,
 	       [#include <sys/ptrace.h>])
 
-AC_CHECK_TYPES([struct user_desc],,, [#include <asm/ldt.h>])
-
 AC_CHECK_MEMBERS([struct utsname.domainname],,, [#include <sys/utsname.h>])
 
 AC_CHECK_MEMBERS([struct sigevent._sigev_un._pad,
diff --git a/ldt.c b/ldt.c
new file mode 100644
index 0000000..0376d2b
--- /dev/null
+++ b/ldt.c
@@ -0,0 +1,110 @@
+#include "defs.h"
+
+#if defined I386 || defined X86_64 || defined X32
+
+# include <asm/ldt.h>
+
+void
+print_user_desc(struct tcb *tcp, long addr)
+{
+	struct user_desc desc;
+
+	if (umove(tcp, addr, &desc) < 0) {
+		tprintf("%lx", addr);
+		return;
+	}
+
+	if (!verbose(tcp)) {
+		tprintf("{entry_number:%d, ...}", desc.entry_number);
+		return;
+	}
+
+	tprintf("{entry_number:%d, "
+		"base_addr:%#08x, "
+		"limit:%d, "
+		"seg_32bit:%d, "
+		"contents:%d, "
+		"read_exec_only:%d, "
+		"limit_in_pages:%d, "
+		"seg_not_present:%d, "
+		"useable:%d}",
+		desc.entry_number,
+		desc.base_addr,
+		desc.limit,
+		desc.seg_32bit,
+		desc.contents,
+		desc.read_exec_only,
+		desc.limit_in_pages,
+		desc.seg_not_present,
+		desc.useable);
+}
+
+int
+sys_modify_ldt(struct tcb *tcp)
+{
+	if (entering(tcp)) {
+		tprintf("%ld, ", tcp->u_arg[0]);
+		if (tcp->u_arg[1] == 0
+		    || tcp->u_arg[2] != sizeof(struct user_desc)) {
+			tprintf("%lx", tcp->u_arg[1]);
+		} else {
+			print_user_desc(tcp, tcp->u_arg[1]);
+		}
+		tprintf(", %lu", tcp->u_arg[2]);
+	}
+	return 0;
+}
+
+int
+sys_set_thread_area(struct tcb *tcp)
+{
+	if (entering(tcp)) {
+		print_user_desc(tcp, tcp->u_arg[0]);
+	} else {
+		struct user_desc desc;
+
+		if (syserror(tcp) || umove(tcp, tcp->u_arg[0], &desc) < 0) {
+			/* returned entry_number is not available */
+		} else {
+			static char outstr[32];
+
+			sprintf(outstr, "entry_number:%d", desc.entry_number);
+			tcp->auxstr = outstr;
+			return RVAL_STR;
+		}
+	}
+	return 0;
+}
+
+int
+sys_get_thread_area(struct tcb *tcp)
+{
+	if (exiting(tcp)) {
+		if (syserror(tcp))
+			tprintf("%lx", tcp->u_arg[0]);
+		else
+			print_user_desc(tcp, tcp->u_arg[0]);
+	}
+	return 0;
+}
+
+#endif /* I386 || X86_64 || X32 */
+
+#if defined(M68K) || defined(MIPS)
+int
+sys_set_thread_area(struct tcb *tcp)
+{
+	if (entering(tcp))
+		tprintf("%#lx", tcp->u_arg[0]);
+	return 0;
+
+}
+#endif
+
+#if defined(M68K)
+int
+sys_get_thread_area(struct tcb *tcp)
+{
+	return RVAL_HEX;
+}
+#endif
diff --git a/mem.c b/mem.c
index 4b6bc18..ce5ce51 100644
--- a/mem.c
+++ b/mem.c
@@ -33,12 +33,6 @@
 #include "defs.h"
 #include <asm/mman.h>
 #include <sys/mman.h>
-#if defined(I386) || defined(X86_64) || defined(X32)
-# include <asm/ldt.h>
-# ifdef HAVE_STRUCT_USER_DESC
-#  define modify_ldt_ldt_s user_desc
-# endif
-#endif /* I386 || X86_64 || X32 */
 
 static unsigned long
 get_pagesize()
@@ -543,119 +537,6 @@
 }
 #endif
 
-#if defined(I386) || defined(X86_64) || defined(X32)
-void
-print_ldt_entry(struct modify_ldt_ldt_s *ldt_entry)
-{
-	tprintf("base_addr:%#08lx, "
-		"limit:%d, "
-		"seg_32bit:%d, "
-		"contents:%d, "
-		"read_exec_only:%d, "
-		"limit_in_pages:%d, "
-		"seg_not_present:%d, "
-		"useable:%d}",
-		(long) ldt_entry->base_addr,
-		ldt_entry->limit,
-		ldt_entry->seg_32bit,
-		ldt_entry->contents,
-		ldt_entry->read_exec_only,
-		ldt_entry->limit_in_pages,
-		ldt_entry->seg_not_present,
-		ldt_entry->useable);
-}
-
-int
-sys_modify_ldt(struct tcb *tcp)
-{
-	if (entering(tcp)) {
-		struct modify_ldt_ldt_s copy;
-		tprintf("%ld", tcp->u_arg[0]);
-		if (tcp->u_arg[1] == 0
-				|| tcp->u_arg[2] != sizeof(struct modify_ldt_ldt_s)
-				|| umove(tcp, tcp->u_arg[1], &copy) == -1)
-			tprintf(", %lx", tcp->u_arg[1]);
-		else {
-			tprintf(", {entry_number:%d, ", copy.entry_number);
-			if (!verbose(tcp))
-				tprints("...}");
-			else {
-				print_ldt_entry(&copy);
-			}
-		}
-		tprintf(", %lu", tcp->u_arg[2]);
-	}
-	return 0;
-}
-
-int
-sys_set_thread_area(struct tcb *tcp)
-{
-	struct modify_ldt_ldt_s copy;
-	if (entering(tcp)) {
-		if (umove(tcp, tcp->u_arg[0], &copy) != -1) {
-			if (copy.entry_number == -1)
-				tprintf("{entry_number:%d -> ",
-					copy.entry_number);
-			else
-				tprints("{entry_number:");
-		}
-	} else {
-		if (umove(tcp, tcp->u_arg[0], &copy) != -1) {
-			tprintf("%d, ", copy.entry_number);
-			if (!verbose(tcp))
-				tprints("...}");
-			else {
-				print_ldt_entry(&copy);
-			}
-		} else {
-			tprintf("%lx", tcp->u_arg[0]);
-		}
-	}
-	return 0;
-
-}
-
-int
-sys_get_thread_area(struct tcb *tcp)
-{
-	struct modify_ldt_ldt_s copy;
-	if (exiting(tcp)) {
-		if (umove(tcp, tcp->u_arg[0], &copy) != -1) {
-			tprintf("{entry_number:%d, ", copy.entry_number);
-			if (!verbose(tcp))
-				tprints("...}");
-			else {
-				print_ldt_entry(&copy);
-			}
-		} else {
-			tprintf("%lx", tcp->u_arg[0]);
-		}
-	}
-	return 0;
-
-}
-#endif /* I386 || X86_64 || X32 */
-
-#if defined(M68K) || defined(MIPS)
-int
-sys_set_thread_area(struct tcb *tcp)
-{
-	if (entering(tcp))
-		tprintf("%#lx", tcp->u_arg[0]);
-	return 0;
-
-}
-#endif
-
-#if defined(M68K)
-int
-sys_get_thread_area(struct tcb *tcp)
-{
-	return RVAL_HEX;
-}
-#endif
-
 int
 sys_remap_file_pages(struct tcb *tcp)
 {
diff --git a/process.c b/process.c
index 963d3b1..af203ea 100644
--- a/process.c
+++ b/process.c
@@ -492,11 +492,7 @@
 };
 
 #if defined I386 || defined X86_64 || defined X32
-# include <asm/ldt.h>
-#  ifdef HAVE_STRUCT_USER_DESC
-#   define modify_ldt_ldt_s user_desc
-#  endif
-extern void print_ldt_entry();
+extern void print_user_desc(struct tcb *, long);
 #endif /* I386 || X86_64 || X32 */
 
 #if defined IA64
@@ -561,17 +557,12 @@
 			if (current_personality == 1)
 # endif
 			{
-				struct modify_ldt_ldt_s copy;
-				if (umove(tcp, tcp->u_arg[ARG_TLS], &copy) != -1) {
-					tprintf(", {entry_number:%d, ",
-						copy.entry_number);
-					if (!verbose(tcp))
-						tprints("...}");
-					else
-						print_ldt_entry(&copy);
-				}
+				tprints(", tls=");
+				print_user_desc(tcp, tcp->u_arg[ARG_TLS]);
 			}
+# ifndef I386
 			else
+# endif
 #endif /* I386 || X86_64 || X32 */
 				tprintf(", tls=%#lx", tcp->u_arg[ARG_TLS]);
 		}