Loading...
gen/backtrace.c Libc-763.13 Libc-1272.200.26
--- Libc/Libc-763.13/gen/backtrace.c
+++ Libc/Libc-1272.200.26/gen/backtrace.c
@@ -25,18 +25,32 @@
 #include <sys/uio.h>
 
 #include <dlfcn.h>
+#include <errno.h>
+#include <mach-o/dyld_priv.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <uuid/uuid.h>
 
 #include "stack_logging.h"
 #include "execinfo.h"
 
+extern void _thread_stack_pcs(vm_address_t *buffer, unsigned max,
+		unsigned *nb, unsigned skip, void *startfp);
+
 int backtrace(void** buffer, int size) {
-	extern void _thread_stack_pcs(vm_address_t *buffer, unsigned max, unsigned *nb, unsigned skip);
 	unsigned int num_frames;
-	_thread_stack_pcs((vm_address_t*)buffer, size, &num_frames, 1);
+	_thread_stack_pcs((vm_address_t*)buffer, size, &num_frames, 1, NULL);
+	while (num_frames >= 1 && buffer[num_frames-1] == NULL) num_frames -= 1;
+	return num_frames;
+}
+
+int
+backtrace_from_fp(void *startfp, void **buffer, int size)
+{
+	unsigned int num_frames;
+	_thread_stack_pcs((vm_address_t*)buffer, size, &num_frames, 1, startfp);
 	while (num_frames >= 1 && buffer[num_frames-1] == NULL) num_frames -= 1;
 	return num_frames;
 }
@@ -84,7 +98,7 @@
 			image,
 			(uintptr_t)addr,
 			symbol,
-			symbol_offset) + 1;
+			symbol_offset);
 }
 
 char** backtrace_symbols(void* const* buffer, int size) {
@@ -110,9 +124,9 @@
 		if (info[i].dli_sname) {
 			total_bytes += strlen(info[i].dli_sname);
 		} else if(info[i].dli_fname) {
-			const char *tmp = strrchr(info->dli_fname, '/');
+			const char *tmp = strrchr(info[i].dli_fname, '/');
 			if(tmp == NULL)
-				total_bytes += strlen(info->dli_fname);
+				total_bytes += strlen(info[i].dli_fname);
 	                else
 				total_bytes += strlen(tmp + 1);
 		} else {
@@ -142,7 +156,7 @@
 		}
 
 		ptrs[i] = (char*)strs;
-		strs += chk;
+		strs += chk + 1; // Step over the '\0'
 	}
 	
 	free(info);
@@ -166,7 +180,19 @@
 		dladdr(buffer[i], &info);
 
 		iov[0].iov_len = _backtrace_snprintf(buf, sizeof(buf), i, buffer[i], &info);
-		
+
 		writev(fd, iov, 2);
 	}
 }
+
+void
+backtrace_image_offsets(void* const* buffer, struct image_offset *imgoffs, int size)
+{
+	struct dyld_image_uuid_offset infos[size];
+	_dyld_images_for_addresses(size, (const void **)buffer, infos);
+
+	for (int i = 0; i < size; i++) {
+		uuid_copy(imgoffs[i].uuid, infos[i].uuid);
+		imgoffs[i].offset = infos[i].offsetInImage;
+	};
+}