Loading...
gen/backtrace.c Libc-1272.200.26 Libc-583
--- Libc/Libc-1272.200.26/gen/backtrace.c
+++ Libc/Libc-583/gen/backtrace.c
@@ -25,71 +25,45 @@
 #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, 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);
+	_thread_stack_pcs((vm_address_t*)buffer, size, &num_frames, 1);
 	while (num_frames >= 1 && buffer[num_frames-1] == NULL) num_frames -= 1;
 	return num_frames;
 }
 
 #if __LP64__
 #define _BACKTRACE_FORMAT "%-4d%-35s 0x%016lx %s + %lu"
-#define _BACKTRACE_FORMAT_SIZE 83 /* %lu can take up to 20, does not include %s, includes NUL */
-#define _BACKTRACE_ADDRESS_LEN 18 /* 0x + 16 (no NUL) */
+#define _BACKTRACE_FORMAT_SIZE 82
 #else
 #define _BACKTRACE_FORMAT "%-4d%-35s 0x%08lx %s + %lu"
-#define _BACKTRACE_FORMAT_SIZE 65 /* %lu can take up to 10, does not include %s, includes NUL */
-#define _BACKTRACE_ADDRESS_LEN 10 /* 0x + 8 (no NUL) */
+#define _BACKTRACE_FORMAT_SIZE 65
 #endif
 
+
 static int _backtrace_snprintf(char* buf, size_t size, int frame, const void* addr, const Dl_info* info) {
-	char symbuf[_BACKTRACE_ADDRESS_LEN + 1];
+	char symbuf[19];
 	const char* image = "???";
-	const char* symbol = "0x0";
-	uintptr_t symbol_offset = 0;
+	const char* symbol = symbuf;
 
 	if (info->dli_fname) {
-		const char *tmp = strrchr(info->dli_fname, '/');
-		if(tmp == NULL)
-			image = info->dli_fname;
-		else
-			image = tmp + 1;
+		image = strrchr(info->dli_fname, '/') + 1;
+		if (image == NULL) image = info->dli_fname;
 	}
 	
 	if (info->dli_sname) {
 		symbol = info->dli_sname;
-		symbol_offset = (uintptr_t)addr - (uintptr_t)info->dli_saddr;
-	} else if(info->dli_fname) {
-		symbol = image;
-		symbol_offset = (uintptr_t)addr - (uintptr_t)info->dli_fbase;
-	} else if(0 < snprintf(symbuf, sizeof(symbuf), "0x%lx", (uintptr_t)info->dli_saddr)) {
-		symbol = symbuf;
-		symbol_offset = (uintptr_t)addr - (uintptr_t)info->dli_saddr;
 	} else {
-		symbol_offset = (uintptr_t)addr;
+		snprintf(symbuf, sizeof(symbuf), "0x%lx", (uintptr_t)info->dli_saddr);
 	}
 
 	return snprintf(buf, size,
@@ -98,7 +72,7 @@
 			image,
 			(uintptr_t)addr,
 			symbol,
-			symbol_offset);
+			(uintptr_t)addr - (uintptr_t)info->dli_saddr) + 1;
 }
 
 char** backtrace_symbols(void* const* buffer, int size) {
@@ -106,7 +80,7 @@
 	size_t total_bytes;
 	char** result;
 	char** ptrs;
-	intptr_t strs, end;
+	intptr_t strs;
 	Dl_info* info = calloc(size, sizeof (Dl_info));
 	
 	if (info == NULL) return NULL;
@@ -120,18 +94,8 @@
 	// Plus each symbol description
 	for (i = 0 ; i < size; ++i) {
 		dladdr(buffer[i], &info[i]);
-		total_bytes += _BACKTRACE_FORMAT_SIZE;
-		if (info[i].dli_sname) {
-			total_bytes += strlen(info[i].dli_sname);
-		} else if(info[i].dli_fname) {
-			const char *tmp = strrchr(info[i].dli_fname, '/');
-			if(tmp == NULL)
-				total_bytes += strlen(info[i].dli_fname);
-	                else
-				total_bytes += strlen(tmp + 1);
-		} else {
-			total_bytes += _BACKTRACE_ADDRESS_LEN;
-		}
+		total_bytes += _BACKTRACE_FORMAT_SIZE + 1;
+		if (info[i].dli_sname) total_bytes += strlen(info[i].dli_sname);
 	}
 	
 	result = (char**)malloc(total_bytes);
@@ -139,24 +103,16 @@
 		free(info);
 		return NULL;
 	}
-	end = (intptr_t)result + total_bytes;
 	
 	// Fill in the array of pointers and append the strings for
 	// each symbol description.
 	
 	ptrs = result;
 	strs = ((intptr_t)result) + sizeof(char*) * size;
-
+	
 	for (i = 0; i < size; ++i) {
-		int chk = _backtrace_snprintf((char*)strs, end - (intptr_t)strs, i, buffer[i], &info[i]);
-
-		if(chk < 0) {
-			free(info);
-			return NULL;
-		}
-
 		ptrs[i] = (char*)strs;
-		strs += chk + 1; // Step over the '\0'
+		strs += _backtrace_snprintf((char*)strs, total_bytes, i, buffer[i], &info[i]);
 	}
 	
 	free(info);
@@ -180,19 +136,7 @@
 		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;
-	};
-}