Merge "Fix double-free in dso__set_basename()."
diff --git a/perf-3.12.0/tools/lib/lk/debugfs.c b/perf-3.12.0/tools/lib/lk/debugfs.c
index 7c43479..a778add 100644
--- a/perf-3.12.0/tools/lib/lk/debugfs.c
+++ b/perf-3.12.0/tools/lib/lk/debugfs.c
@@ -76,6 +76,9 @@
 /* mount the debugfs somewhere if it's not mounted */
 char *debugfs_mount(const char *mountpoint)
 {
+#ifdef __APPLE__
+	goto out;
+#else
 	/* see if it's already mounted */
 	if (debugfs_find_mountpoint())
 		goto out;
@@ -95,6 +98,7 @@
 	/* save the mountpoint */
 	debugfs_found = true;
 	strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
+#endif
 out:
 	return debugfs_mountpoint;
 }
diff --git a/perf-3.12.0/tools/perf/Android.mk b/perf-3.12.0/tools/perf/Android.mk
index 9d35950..2eb8136 100644
--- a/perf-3.12.0/tools/perf/Android.mk
+++ b/perf-3.12.0/tools/perf/Android.mk
@@ -18,7 +18,7 @@
 supported_platforms := none
 else
 # TODO Clang is having issues with elfutils - only compile on linux-x86 for now
-supported_platforms := linux-x86
+supported_platforms := linux-x86 darwin-x86
 endif
 
 cur_platform := $(filter $(HOST_OS)-$(HOST_ARCH),$(supported_platforms))
@@ -165,8 +165,13 @@
 
 LOCAL_CFLAGS += $(common_compiler_flags)
 
+# mainly for darwin
+LOCAL_CFLAGS += -include $(LOCAL_PATH)/host-$(HOST_OS)-fixup/AndroidFixup.h
+
 LOCAL_C_INCLUDES := $(common_perf_headers) $(common_elfutil_headers)
 
+LOCAL_C_INCLUDES += $(LOCAL_PATH)/host-$(HOST_OS)-fixup
+
 LOCAL_MODULE := libperf
 
 LOCAL_MODULE_TAGS := eng
@@ -191,11 +196,11 @@
 	builtin-mem.c \
 	builtin-probe.c \
 	builtin-record.c \
+	builtin-report.c \
 	builtin-sched.c \
 	builtin-script.c \
 	builtin-stat.c \
 	builtin-timechart.c \
-	builtin-report.c \
 	builtin-top.c
 
 include $(CLEAR_VARS)
@@ -239,15 +244,26 @@
 # predefined macros
 LOCAL_CFLAGS += $(common_predefined_macros)
 
-# available in host libc
+# available on linux-x86 but not darwin-x86
+ifeq ($(strip $(HOST_OS)),linux)
 LOCAL_CFLAGS += -DHAVE_ON_EXIT
+endif
 
 LOCAL_CFLAGS += $(common_compiler_flags)
 
+# mainly for darwin
+LOCAL_CFLAGS += -include $(LOCAL_PATH)/host-$(HOST_OS)-fixup/AndroidFixup.h
+
 LOCAL_C_INCLUDES := $(common_perf_headers) $(common_elfutil_headers)
 
+LOCAL_C_INCLUDES += $(LOCAL_PATH)/host-$(HOST_OS)-fixup
+
 # for pthread_* and clock_gettime
-LOCAL_LDLIBS := -lpthread -lrt -ldl
+LOCAL_LDLIBS := -lpthread -ldl
+
+ifeq ($(strip $(HOST_OS)),linux)
+LOCAL_LDLIBS += -lrt
+endif
 
 LOCAL_MODULE := perfhost
 
diff --git a/perf-3.12.0/tools/perf/builtin-record.c b/perf-3.12.0/tools/perf/builtin-record.c
index d046514..9e3f90e 100644
--- a/perf-3.12.0/tools/perf/builtin-record.c
+++ b/perf-3.12.0/tools/perf/builtin-record.c
@@ -345,6 +345,7 @@
 
 static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
 {
+#ifndef __APPLE__
 	struct stat st;
 	int flags;
 	int err, output, feat;
@@ -598,6 +599,9 @@
 out_delete_session:
 	perf_session__delete(session);
 	return err;
+#else
+	return -1;
+#endif
 }
 
 #define BRANCH_OPT(n, m) \
diff --git a/perf-3.12.0/tools/perf/builtin-sched.c b/perf-3.12.0/tools/perf/builtin-sched.c
index d8c51b2..0ad5b4d 100644
--- a/perf-3.12.0/tools/perf/builtin-sched.c
+++ b/perf-3.12.0/tools/perf/builtin-sched.c
@@ -173,11 +173,16 @@
 
 static u64 get_nsecs(void)
 {
+#ifndef __APPLE__
 	struct timespec ts;
 
 	clock_gettime(CLOCK_MONOTONIC, &ts);
 
 	return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
+#else
+	perror("get_nsecs not supported on MacOS");
+	return 0;
+#endif
 }
 
 static void burn_nsecs(struct perf_sched *sched, u64 nsecs)
@@ -461,6 +466,7 @@
 
 static void *thread_func(void *ctx)
 {
+#ifndef __APPLE__
 	struct sched_thread_parms *parms = ctx;
 	struct task_desc *this_task = parms->task;
 	struct perf_sched *sched = parms->sched;
@@ -502,6 +508,10 @@
 	BUG_ON(ret);
 
 	goto again;
+#else
+	perror("thread_func not supported on MacOS");
+	return NULL;
+#endif
 }
 
 static void create_tasks(struct perf_sched *sched)
diff --git a/perf-3.12.0/tools/perf/builtin-stat.c b/perf-3.12.0/tools/perf/builtin-stat.c
index 5098f14..2893ada 100644
--- a/perf-3.12.0/tools/perf/builtin-stat.c
+++ b/perf-3.12.0/tools/perf/builtin-stat.c
@@ -358,6 +358,7 @@
 
 static void print_interval(void)
 {
+#ifndef __APPLE__
 	static int num_print_interval;
 	struct perf_evsel *counter;
 	struct perf_stat *ps;
@@ -418,6 +419,9 @@
 	}
 
 	fflush(output);
+#else
+	perror("print_interval not supported on MacOS");
+#endif
 }
 
 static void handle_initial_delay(void)
@@ -436,6 +440,7 @@
 
 static int __run_perf_stat(int argc, const char **argv)
 {
+#ifndef __APPLE__
 	char msg[512];
 	unsigned long long t0, t1;
 	struct perf_evsel *counter;
@@ -543,6 +548,9 @@
 	}
 
 	return WEXITSTATUS(status);
+#else
+	return -1;
+#endif
 }
 
 static int run_perf_stat(int argc __maybe_unused, const char **argv)
@@ -1385,6 +1393,7 @@
 
 int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
 {
+#ifndef __APPLE__
 	bool append_file = false;
 	int output_fd = 0;
 	const char *output_name	= NULL;
@@ -1594,4 +1603,8 @@
 out:
 	perf_evlist__delete(evsel_list);
 	return status;
+#else
+	perror("cmd_stat not supported on MacOS");
+	return -1;
+#endif
 }
diff --git a/perf-3.12.0/tools/perf/builtin-top.c b/perf-3.12.0/tools/perf/builtin-top.c
index 5a11f13..a3718d3 100644
--- a/perf-3.12.0/tools/perf/builtin-top.c
+++ b/perf-3.12.0/tools/perf/builtin-top.c
@@ -929,6 +929,7 @@
 
 static int __cmd_top(struct perf_top *top)
 {
+#ifndef __APPLE__
 	struct perf_record_opts *opts = &top->record_opts;
 	pthread_t thread;
 	int ret;
@@ -1015,6 +1016,9 @@
 	top->session = NULL;
 
 	return ret;
+#else
+	return -1;
+#endif
 }
 
 static int
diff --git a/perf-3.12.0/tools/perf/host-darwin-fixup/AndroidFixup.h b/perf-3.12.0/tools/perf/host-darwin-fixup/AndroidFixup.h
new file mode 100644
index 0000000..c904086
--- /dev/null
+++ b/perf-3.12.0/tools/perf/host-darwin-fixup/AndroidFixup.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2013, 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.
+ */
+
+#ifndef ANDROID_FIXUP_H
+#define ANDROID_FIXUP_H
+
+#include <unistd.h>
+
+typedef __uint8_t  __u8;
+typedef __uint16_t __u16;
+typedef __uint32_t __u32;
+typedef __uint64_t __u64;
+typedef __int64_t  __s64;
+
+typedef off_t	loff_t;
+
+#define strndup(str, size) strdup(str)
+
+#define __maybe_unused
+
+#endif /* ANDROID_FIXUP_H */
diff --git a/perf-3.12.0/tools/perf/host-darwin-fixup/asm/types.h b/perf-3.12.0/tools/perf/host-darwin-fixup/asm/types.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/perf-3.12.0/tools/perf/host-darwin-fixup/asm/types.h
diff --git a/perf-3.12.0/tools/perf/host-darwin-fixup/asm/unistd.h b/perf-3.12.0/tools/perf/host-darwin-fixup/asm/unistd.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/perf-3.12.0/tools/perf/host-darwin-fixup/asm/unistd.h
diff --git a/perf-3.12.0/tools/perf/host-darwin-fixup/byteswap.h b/perf-3.12.0/tools/perf/host-darwin-fixup/byteswap.h
new file mode 100644
index 0000000..a8d27d4
--- /dev/null
+++ b/perf-3.12.0/tools/perf/host-darwin-fixup/byteswap.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2013, 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.
+ */
+
+#ifndef ELFUTILS_BYTESWAP_H
+#define ELFUTILS_BYTESWAP_H
+
+static inline unsigned short bswap_16(unsigned short val)
+{
+        return ((val & 0xff) << 8) | ((val >> 8) & 0xff);
+}
+
+static inline unsigned long bswap_32(unsigned long val)
+{
+        return bswap_16((unsigned short)val) << 16 |
+               bswap_16((unsigned short)(val >> 16));
+}
+
+static inline unsigned long long bswap_64(unsigned long long val)
+{
+        return ((((unsigned long long)bswap_32(val)) << 32) |
+                (((unsigned long long)bswap_32(val >> 32)) & 0xffffffffULL));
+}
+
+#endif /* ELFUTILS_BYTESWAP_H */
diff --git a/perf-3.12.0/tools/perf/host-darwin-fixup/linux/ioctl.h b/perf-3.12.0/tools/perf/host-darwin-fixup/linux/ioctl.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/perf-3.12.0/tools/perf/host-darwin-fixup/linux/ioctl.h
diff --git a/perf-3.12.0/tools/perf/host-darwin-fixup/linux/stddef.h b/perf-3.12.0/tools/perf/host-darwin-fixup/linux/stddef.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/perf-3.12.0/tools/perf/host-darwin-fixup/linux/stddef.h
diff --git a/perf-3.12.0/tools/perf/host-darwin-fixup/linux/unistd.h b/perf-3.12.0/tools/perf/host-darwin-fixup/linux/unistd.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/perf-3.12.0/tools/perf/host-darwin-fixup/linux/unistd.h
diff --git a/perf-3.12.0/tools/perf/host-darwin-fixup/mntent.h b/perf-3.12.0/tools/perf/host-darwin-fixup/mntent.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/perf-3.12.0/tools/perf/host-darwin-fixup/mntent.h
diff --git a/perf-3.12.0/tools/perf/host-darwin-fixup/sys/prctl.h b/perf-3.12.0/tools/perf/host-darwin-fixup/sys/prctl.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/perf-3.12.0/tools/perf/host-darwin-fixup/sys/prctl.h
diff --git a/perf-3.12.0/tools/perf/host-darwin-fixup/sys/statfs.h b/perf-3.12.0/tools/perf/host-darwin-fixup/sys/statfs.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/perf-3.12.0/tools/perf/host-darwin-fixup/sys/statfs.h
diff --git a/perf-3.12.0/tools/perf/host-darwin-fixup/sys/vfs.h b/perf-3.12.0/tools/perf/host-darwin-fixup/sys/vfs.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/perf-3.12.0/tools/perf/host-darwin-fixup/sys/vfs.h
diff --git a/perf-3.12.0/tools/perf/host-linux-fixup/AndroidFixup.h b/perf-3.12.0/tools/perf/host-linux-fixup/AndroidFixup.h
new file mode 100644
index 0000000..ea4d5a1
--- /dev/null
+++ b/perf-3.12.0/tools/perf/host-linux-fixup/AndroidFixup.h
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2013, 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.
+ */
+
+#ifndef ANDROID_FIXUP_H
+#define ANDROID_FIXUP_H
+
+/* intentionally blank */
+
+#endif /* ANDROID_FIXUP_H */
diff --git a/perf-3.12.0/tools/perf/perf.h b/perf-3.12.0/tools/perf/perf.h
index cf20187..a70e8c2 100644
--- a/perf-3.12.0/tools/perf/perf.h
+++ b/perf-3.12.0/tools/perf/perf.h
@@ -131,10 +131,15 @@
 
 static inline unsigned long long rdclock(void)
 {
+#ifdef __APPLE__
+	perror("no clock_gettime");
+	return 0;
+#else
 	struct timespec ts;
 
 	clock_gettime(CLOCK_MONOTONIC, &ts);
 	return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
+#endif
 }
 
 /*
diff --git a/perf-3.12.0/tools/perf/util/callchain.c b/perf-3.12.0/tools/perf/util/callchain.c
index 482f680..9f4d009 100644
--- a/perf-3.12.0/tools/perf/util/callchain.c
+++ b/perf-3.12.0/tools/perf/util/callchain.c
@@ -19,7 +19,11 @@
 #include "util.h"
 #include "callchain.h"
 
+#ifdef __APPLE__
+struct callchain_cursor callchain_cursor;
+#else
 __thread struct callchain_cursor callchain_cursor;
+#endif
 
 #define chain_for_each_child(child, parent)	\
 	list_for_each_entry(child, &parent->children, siblings)
diff --git a/perf-3.12.0/tools/perf/util/callchain.h b/perf-3.12.0/tools/perf/util/callchain.h
index 9e99060..fe1eb71 100644
--- a/perf-3.12.0/tools/perf/util/callchain.h
+++ b/perf-3.12.0/tools/perf/util/callchain.h
@@ -82,7 +82,11 @@
 	struct callchain_cursor_node	*curr;
 };
 
+#ifdef __APPLE__
+extern struct callchain_cursor callchain_cursor;
+#else
 extern __thread struct callchain_cursor callchain_cursor;
+#endif
 
 static inline void callchain_init(struct callchain_root *root)
 {
diff --git a/perf-3.12.0/tools/perf/util/include/linux/bitops.h b/perf-3.12.0/tools/perf/util/include/linux/bitops.h
index 45cf10a..e6da726 100644
--- a/perf-3.12.0/tools/perf/util/include/linux/bitops.h
+++ b/perf-3.12.0/tools/perf/util/include/linux/bitops.h
@@ -5,6 +5,10 @@
 #include <linux/compiler.h>
 #include <asm/hweight.h>
 
+#ifdef __APPLE__
+#define __SIZEOF_LONG__ 4
+#endif
+
 #ifndef __WORDSIZE
 #define __WORDSIZE (__SIZEOF_LONG__ * 8)
 #endif
diff --git a/perf-3.12.0/tools/perf/util/sysfs.c b/perf-3.12.0/tools/perf/util/sysfs.c
index f71e9ea..eace246 100644
--- a/perf-3.12.0/tools/perf/util/sysfs.c
+++ b/perf-3.12.0/tools/perf/util/sysfs.c
@@ -12,12 +12,14 @@
 
 static int sysfs_valid_mountpoint(const char *sysfs)
 {
+#ifndef __APPLE__
 	struct statfs st_fs;
 
 	if (statfs(sysfs, &st_fs) < 0)
 		return -ENOENT;
 	else if (st_fs.f_type != (long) SYSFS_MAGIC)
 		return -ENOENT;
+#endif
 
 	return 0;
 }