Merge remote-tracking branch 'strace/master' into cp

Conflicts:
	configure.ac
	file.c
	linux/dummy.h
	mem.c
	net.c
	process.c
	signal.c
	syscall.c

Change-Id: Ib7acdaefedb621e2dd0c292b80781d0d6e1f7e5c
diff --git a/Android.mk b/Android.mk
new file mode 100644
index 0000000..8794c56
--- /dev/null
+++ b/Android.mk
@@ -0,0 +1,208 @@
+#
+# Copyright (C) 2006 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+# -------------------------------------------------------------------------
+
+include $(CLEAR_VARS)
+
+strace_version := $(shell grep Version $(LOCAL_PATH)/strace.spec | cut -d " " -f 2)
+
+LOCAL_SRC_FILES := \
+    aio.c \
+    bjm.c \
+    block.c \
+    count.c \
+    desc.c \
+    fanotify.c \
+    file.c \
+    inotify.c \
+    io.c \
+    ioctl.c \
+    ioprio.c \
+    ipc.c \
+    kexec.c \
+    keyctl.c \
+    loop.c \
+    mem.c \
+    mtd.c \
+    net.c \
+    pathtrace.c \
+    process.c \
+    ptp.c \
+    quota.c \
+    reboot.c \
+    resource.c \
+    scsi.c \
+    signal.c \
+    sock.c \
+    strace.c \
+    stream.c \
+    syscall.c \
+    system.c \
+    term.c \
+    time.c \
+    util.c \
+    vsprintf.c \
+
+LOCAL_SHARED_LIBRARIES :=
+
+LOCAL_CFLAGS := \
+    -DGETGROUPS_T=gid_t \
+    -DHAVE_ASM_SIGCONTEXT_H=1 \
+    -DHAVE_DECL_PTRACE_EVENT_FORK=1 \
+    -DHAVE_DECL_PTRACE_EVENT_VFORK=1 \
+    -DHAVE_DECL_PTRACE_EVENT_CLONE=1 \
+    -DHAVE_DECL_PTRACE_EVENT_EXEC=1 \
+    -DHAVE_DECL_PTRACE_EVENT_VFORK_DONE=1 \
+    -DHAVE_DECL_PTRACE_EVENT_EXIT=1 \
+    -DHAVE_DECL_PTRACE_GETEVENTMSG=1 \
+    -DHAVE_DECL_PTRACE_GETSIGINFO=1 \
+    -DHAVE_DECL_PTRACE_O_TRACECLONE=1 \
+    -DHAVE_DECL_PTRACE_O_TRACEEXEC=1 \
+    -DHAVE_DECL_PTRACE_O_TRACEEXIT=1 \
+    -DHAVE_DECL_PTRACE_O_TRACEFORK=1 \
+    -DHAVE_DECL_PTRACE_O_TRACESYSGOOD=1 \
+    -DHAVE_DECL_PTRACE_O_TRACEVFORK=1 \
+    -DHAVE_DECL_PTRACE_SETOPTIONS=1 \
+    -UHAVE_DECL_IO_CMD_PWRITE \
+    -UHAVE_DECL_IO_CMD_PWRITEV \
+    -UHAVE_DECL_LO_FLAGS_AUTOCLEAR \
+    -UHAVE_DECL_LO_FLAGS_PARTSCAN \
+    -DHAVE_DECL_SYS_ERRLIST=1 \
+    -DHAVE_ELF_H=1 \
+    -DHAVE_FORK=1 \
+    -DHAVE_IF_INDEXTONAME=1 \
+    -DHAVE_INET_NTOP=1 \
+    -DHAVE_LINUX_CAPABILITY_H=1 \
+    -DHAVE_LINUX_ICMP_H=1 \
+    -DHAVE_LINUX_IF_PACKET_H=1 \
+    -DHAVE_LINUX_IN6_H=1 \
+    -DHAVE_LINUX_NETLINK_H=1 \
+    -DHAVE_LITTLE_ENDIAN_LONG_LONG=1 \
+    -DHAVE_LONG_LONG=1 \
+    -DHAVE_NETINET_TCP_H=1 \
+    -DHAVE_NETINET_UDP_H=1 \
+    -DHAVE_POLL_H=1 \
+    -DHAVE_PRCTL=1 \
+    -DHAVE_SENDMSG=1 \
+    -DHAVE_SIGACTION=1 \
+    -DHAVE_SIGINFO_T=1 \
+    -DHAVE_SIG_ATOMIC_T=1 \
+    -DHAVE_STAT64=1 \
+    -DHAVE_STATFS64=1 \
+    -DHAVE_STDBOOL_H=1 \
+    -DHAVE_STRERROR=1 \
+    -DHAVE_STRUCT_FLOCK64=1 \
+    -DHAVE_STRUCT_MSGHDR_MSG_CONTROL=1 \
+    -DHAVE_STRUCT_SIGCONTEXT \
+    -DHAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID=1 \
+    -DHAVE_STRUCT_STAT_ST_BLKSIZE=1 \
+    -DHAVE_STRUCT_STAT_ST_BLOCKS=1 \
+    -DHAVE_STRUCT_STAT_ST_RDEV=1 \
+    -DHAVE_STRUCT_USER_DESC=1 \
+    -DHAVE_SYS_EPOLL_H=1 \
+    -DHAVE_SYS_IOCTL_H=1 \
+    -DHAVE_SYS_POLL_H=1 \
+    -DHAVE_SYS_REG_H=1 \
+    -DHAVE_SYS_VFS_H=1 \
+    -DMAJOR_IN_SYSMACROS \
+    -DPACKAGE_NAME='"strace"' \
+    -DVERSION='"$(strace_version)"' \
+    -DSIZEOF_OFF_T=SIZEOF_LONG \
+    -DSIZEOF_LONG_LONG=8 \
+    -DSTDC_HEADERS=1 \
+    -DSTRACE_KNOWS_ONLY_EABI=1 \
+    -D_LFS64_LARGEFILE=1 \
+
+LOCAL_CFLAGS += -D_GNU_SOURCE=1 -D_POSIX_SOURCE=1
+
+LOCAL_CFLAGS_32 += -DSIZEOF_LONG=4 -DSIZEOF_RLIM_T=4
+LOCAL_CFLAGS_64 += -DSIZEOF_LONG=8 -DSIZEOF_RLIM_T=8
+
+LOCAL_CFLAGS_arm += -DARM=1
+LOCAL_CFLAGS_arm += -DHAVE_STRUCT___OLD_KERNEL_STAT=1
+
+LOCAL_CFLAGS_arm64 += -DAARCH64=1
+
+LOCAL_CFLAGS_mips += -DMIPS=1
+LOCAL_CFLAGS_mips += -DHAVE_ASM_SYSMIPS_H=1
+
+LOCAL_CFLAGS_x86 += -DI386=1
+LOCAL_CFLAGS_x86 += -DHAVE_STRUCT___OLD_KERNEL_STAT=1
+
+LOCAL_CFLAGS_x86_64 += -DX86_64=1 -UHAVE_STAT64
+
+LOCAL_CFLAGS += \
+    -Wall \
+    -Wno-missing-field-initializers \
+    -Wno-unused-parameter \
+    -Wno-sign-compare \
+
+LOCAL_C_INCLUDES := \
+    $(LOCAL_PATH)/linux
+
+LOCAL_C_INCLUDES_arm := $(LOCAL_PATH)/linux/arm
+LOCAL_C_INCLUDES_arm64 := $(LOCAL_PATH)/linux/aarch64
+LOCAL_C_INCLUDES_mips := $(LOCAL_PATH)/linux/mips
+LOCAL_C_INCLUDES_x86 := $(LOCAL_PATH)/linux/i386
+LOCAL_C_INCLUDES_x86_64 := $(LOCAL_PATH)/linux/x86_64
+
+LOCAL_MODULE := strace
+
+LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
+
+LOCAL_MODULE_TAGS := debug
+
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+
+include $(BUILD_EXECUTABLE)
+
+
+# -------------------------------------------------------------------------
+
+.PHONY: update-ioctls
+update-ioctls:
+	# Build the generated .h files needed by ioctlsort from the current bionic uapi headers.
+	cd external/strace; ./linux/ioctlent.sh ../../bionic/libc/kernel/uapi/
+	# Build the ioctlsort tool.
+	ONE_SHOT_MAKEFILE=external/strace/Android.mk make -f build/core/main.mk $(TARGET_OUT_EXECUTABLES)/ioctlsort
+	# Remove the generated .h files now we've built ioctlsort.
+	rm external/strace/ioctls.h external/strace/ioctldefs.h
+	# Run the ioctlsort tool on the target to generate the one file we do want to check in.
+	adb sync
+	adb shell ioctlsort | tr -d '\r' > external/strace/linux/ioctlent.h
+	# Rebuild strace with the new "ioctlent.h".
+	ONE_SHOT_MAKEFILE=external/strace/Android.mk make -f build/core/main.mk
+
+# We don't build ioctlsort unless really necessary, because we don't check
+# in the temporary files needed to build it. This tool is only necessary
+# when updating strace's list of ioctls.
+ifneq (,$(filter ioctlsort,$(MAKECMDGOALS)))
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := linux/ioctlsort.c
+LOCAL_CFLAGS += -include asm/types.h -include linux/ashmem.h
+LOCAL_CFLAGS += -Wno-unused-parameter
+LOCAL_MODULE := ioctlsort
+LOCAL_MODULE_TAGS := optional
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+include $(BUILD_EXECUTABLE)
+endif
+
+# -------------------------------------------------------------------------
+
+include $(LOCAL_PATH)/test/Android.mk
diff --git a/test/Android.mk b/test/Android.mk
new file mode 100644
index 0000000..40f2943
--- /dev/null
+++ b/test/Android.mk
@@ -0,0 +1,102 @@
+#
+# Copyright (C) 2014 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+define declare-strace-test-target
+  include $(CLEAR_VARS)
+  LOCAL_SRC_FILES := $(1)
+  LOCAL_CFLAGS := -Wno-unused-parameter -Wno-error=return-type
+  LOCAL_MODULE := strace-$(basename $(1))-test
+  LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
+  LOCAL_MODULE_TAGS := tests
+  LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+  include $(BUILD_EXECUTABLE)
+endef
+
+src_files := \
+    childthread.c \
+    clone.c \
+    fork.c \
+    leaderkill.c \
+    mmap_offset_decode.c \
+    mtd.c \
+    select.c \
+    sfd.c \
+    sig.c \
+    sigkill_rain.c \
+    sigreturn.c \
+    skodic.c \
+    threaded_execve.c \
+    ubi.c \
+    vfork.c \
+    wait_must_be_interruptible.c \
+
+$(foreach file, $(src_files), $(eval $(call declare-strace-test-target,$(file))))
+
+# Simple sanity tests meant to be run manually on device. Tests expect that
+# strace will report "exit with 0" at the end of the programs. Some tests
+# document what string should be expected in the output and for them additional
+# checks is made (*-expected output vars)).
+#
+# Failure should be inspected manually. Usually they require a special test
+# setup that can't be easily automated.
+#
+# adb sync is requreired before running "mm run-strace-tests".
+# logs are pulled automatically from the device to the root of the tree
+# (strace-log-*)
+
+childthread-expected-output := 'write(1, "OK\\n",'
+clone-expected-output := 'write(1, "original\\n",'
+fork-expected-output := 'write(1, "parent\\n",'
+leaderkill-expected-output := 'write(1, "OK\\n",'
+mmap_offset_decode-expected-output := ''
+mtd-expected-output := ''
+select-expected-output := ''
+sfd-expected-output := ''
+sig-expected-output := 'write(2, "qwerty\\n",'
+sigkill_rain-expected-output := ''
+sigreturn-expected-output := 'RT_1 RT_3 RT_31 RT_32'
+skodic-expected-output := ''
+threaded_execve-expected-output := ''
+ubi-expected-output := ''
+vfork-expected-output := 'write(1, "parent\\n",'
+wait_must_be_interruptible-expected-output := 'write(1, "Good: wait seems to be correctly"'
+
+run-strace-%-test: TEST_TMP_DIR := /data/local/tmp
+run-strace-%-test:
+	@printf >&2 "\n$*: RUNNING...\n" ; \
+	adb shell rm -f $(TEST_TMP_DIR)/strace-log-$* ; \
+	timeout -s 9 10 adb shell strace -f -o$(TEST_TMP_DIR)/strace-log-$* strace-$*-test > /dev/null ; \
+	adb pull $(TEST_TMP_DIR)/strace-log-$* 2> /dev/null ; \
+	if adb shell cat $(TEST_TMP_DIR)/strace-log-$* | grep "exited with 0" > /dev/null ; \
+	then \
+		if [ -n $($*-expected-output) ] ; then \
+			if adb shell cat $(TEST_TMP_DIR)/strace-log-$* | grep $($*-expected-output) > /dev/null ; \
+				then printf >&2 "$*: PASSED\n" ; \
+				else printf >&2 "$*: FAILED\n" ; \
+			fi ; \
+		else \
+			printf >&2 "$*: PASSED\n" ; \
+		fi ; \
+	else \
+		printf >&2 "$*: FAILED\n" ; \
+	fi
+
+adb-sync:
+	adb sync
+
+run-strace-tests: adb-sync $(foreach file, $(src_files), run-strace-$(basename $(file))-test)