Loading...
--- libmalloc/libmalloc-283.100.5/src/malloc_printf.c
+++ libmalloc/libmalloc-409.60.6/src/malloc_printf.c
@@ -48,8 +48,9 @@
 }
 
 #define WRITE_TO_DEBUG_FILE(flags) \
+		(((flags) & MALLOC_REPORT_NOWRITE) == 0 && \
 		((debug_mode == DEBUG_WRITE_ALWAYS) || \
-		(debug_mode == DEBUG_WRITE_ON_CRASH && (flags & MALLOC_REPORT_CRASH)))
+		(debug_mode == DEBUG_WRITE_ON_CRASH && ((flags) & MALLOC_REPORT_CRASH))))
 #define	MALLOC_REPORT_LEVEL_MASK	0x0f
 
 #pragma mark -
@@ -98,7 +99,7 @@
 	if ((b = _simple_salloc()) == NULL) {
 		if (WRITE_TO_DEBUG_FILE(flags)) {
 			if (!(flags & MALLOC_REPORT_NOPREFIX)) {
-				void *self = _os_tsd_get_direct(__TSD_THREAD_SELF);
+				void *self = _pthread_self_direct();
 				_simple_dprintf(malloc_debug_file, "%s(%d,%p) malloc: ", getprogname(), getpid(), self);
 			}
 			write(malloc_debug_file, msg, strlen(msg));
@@ -106,7 +107,7 @@
 		return;
 	}
 	if (!(flags & MALLOC_REPORT_NOPREFIX)) {
-		void *self = _os_tsd_get_direct(__TSD_THREAD_SELF);
+		void *self = _pthread_self_direct();
 		_simple_sprintf(b, "%s(%d,%p) malloc: ", getprogname(), getpid(), self);
 	}
 
@@ -122,6 +123,37 @@
 
 #pragma mark -
 #pragma mark High-Level Reporting Functions
+
+#define MALLOC_BT_DEPTH 50
+
+static void
+_malloc_append_backtrace(_SIMPLE_STRING b)
+{
+	void *btarray[MALLOC_BT_DEPTH];
+
+	int count = backtrace(btarray, MALLOC_BT_DEPTH);
+	if (!count) {
+		return;
+	}
+
+	struct image_offset bt_image_offsets[MALLOC_BT_DEPTH];
+	backtrace_image_offsets(btarray, bt_image_offsets, count);
+
+	uuid_t last_uuid;
+	for (int i = 0; i < count; i++) {
+		// simple de-duping for runs of UUIDs common in backtraces
+		if (i == 0 || uuid_compare(last_uuid, bt_image_offsets[i].uuid) != 0) {
+			uuid_copy(last_uuid, bt_image_offsets[i].uuid);
+
+			uuid_string_t uus;
+			uuid_unparse(bt_image_offsets[i].uuid, uus);
+			_simple_sappend(b, uus);
+		} else {
+			_simple_sappend(b, "last");
+		}
+		_simple_sprintf(b, "+%u,", bt_image_offsets[i].offset);
+	}
+}
 
 MALLOC_NOINLINE void
 malloc_vreport(uint32_t flags, unsigned sleep_time, const char *prefix_msg,
@@ -132,7 +164,7 @@
 	if ((b = _simple_salloc()) == NULL) {
 		if (WRITE_TO_DEBUG_FILE(flags)) {
 			if (!(flags & MALLOC_REPORT_NOPREFIX)) {
-				void *self = _os_tsd_get_direct(__TSD_THREAD_SELF);
+				void *self = _pthread_self_direct();
 				_simple_dprintf(malloc_debug_file, "%s(%d,%p) malloc: ", getprogname(), getpid(), self);
 			}
 			if (prefix_msg) {
@@ -145,13 +177,18 @@
 		}
 	} else {
 		if (!(flags & MALLOC_REPORT_NOPREFIX)) {
-			void *self = _os_tsd_get_direct(__TSD_THREAD_SELF);
+			void *self = _pthread_self_direct();
 			_simple_sprintf(b, "%s(%d,%p) malloc: ", getprogname(), getpid(), self);
 		}
 		if (prefix_msg) {
 			_simple_sprintf(b, prefix_msg, prefix_arg);
 		}
 		_simple_vsprintf(b, fmt, ap);
+
+		if (flags & MALLOC_REPORT_BACKTRACE) {
+			_malloc_append_backtrace(b);
+		}
+
 		if (WRITE_TO_DEBUG_FILE(flags)) {
 			_simple_put(b, malloc_debug_file);
 		}
@@ -206,6 +243,8 @@
 #pragma mark -
 #pragma mark Zone Error Reporing
 
+#ifndef MALLOC_BUILDING_XCTESTS
+
 void
 malloc_zone_error(uint32_t flags, bool is_corruption, const char *fmt, ...)
 {
@@ -220,6 +259,8 @@
 			NULL, NULL, fmt, ap);
 	va_end(ap);
 }
+
+#endif // MALLOC_BUILDING_XCTESTS
 
 #pragma mark -
 #pragma mark Malloc Output API.