]> git.saurik.com Git - apple/libc.git/blobdiff - gen/backtrace.c
Libc-1439.100.3.tar.gz
[apple/libc.git] / gen / backtrace.c
index 77192838fd76192228b8bbef6ea29feb962ae5da..f3b74249ef95878aa475f58f0c8a429f46035d90 100644 (file)
  * @APPLE_LICENSE_HEADER_END@
  */
 
+#include <TargetConditionals.h>
 #include <mach/vm_types.h>
 #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,
@@ -55,6 +50,13 @@ backtrace_from_fp(void *startfp, void **buffer, int size)
        return num_frames;
 }
 
+#if !TARGET_OS_DRIVERKIT
+
+#include <dlfcn.h>
+#include <stdio.h>
+#include <string.h>
+#include "stack_logging.h"
+
 #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 */
@@ -64,41 +66,76 @@ backtrace_from_fp(void *startfp, void **buffer, int size)
 #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) */
 #endif
+#define _BACKTRACE_IMAGE_LEN 35
+#define _BACKTRACE_UUID_LEN 36
 
 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 addrbuf[_BACKTRACE_ADDRESS_LEN + 1];
+       char imagebuf[_BACKTRACE_IMAGE_LEN + 1];
+       uuid_string_t uuidbuf;
        const char* image = "???";
        const char* symbol = "0x0";
        uintptr_t symbol_offset = 0;
 
        if (info->dli_fname) {
                const char *tmp = strrchr(info->dli_fname, '/');
-               if(tmp == NULL)
-                       image = info->dli_fname;
-               else
-                       image = tmp + 1;
+               if (tmp == NULL) {
+                       strlcpy(imagebuf, info->dli_fname, sizeof(imagebuf));
+               } else {
+                       strlcpy(imagebuf, tmp + 1, sizeof(imagebuf));
+               }
+               image = imagebuf;
        }
-       
+
        if (info->dli_sname) {
-               symbol = info->dli_sname;
-               symbol_offset = (uintptr_t)addr - (uintptr_t)info->dli_saddr;
-       } else if(info->dli_fname) {
+               uuid_t uuid;
+               if (strcmp(info->dli_sname, "<redacted>") == 0 &&
+                               _dyld_get_image_uuid(info->dli_fbase, uuid)) {
+                       /*
+                        * dyld returns <redacted> when the symbol name has been elided in
+                        * the shared cache.  To enable symbolication later, we provide the
+                        * UUID and UUID-offset instead.
+                        */
+                       uuid_unparse(uuid, uuidbuf);
+                       symbol = uuidbuf;
+                       symbol_offset = (uintptr_t)addr - (uintptr_t)info->dli_fbase;
+               } else {
+                       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;
+       } else if (0 < snprintf(addrbuf, sizeof(addrbuf), "0x%lx",
+                       (uintptr_t)info->dli_saddr)) {
+               symbol = addrbuf;
                symbol_offset = (uintptr_t)addr - (uintptr_t)info->dli_saddr;
        } else {
                symbol_offset = (uintptr_t)addr;
        }
 
-       return snprintf(buf, size,
-                       _BACKTRACE_FORMAT,
-                       frame,
-                       image,
-                       (uintptr_t)addr,
-                       symbol,
-                       symbol_offset);
+       return snprintf(buf, size, _BACKTRACE_FORMAT, frame, image,
+                       (uintptr_t)addr, symbol, symbol_offset);
+}
+
+static size_t symbol_length(Dl_info *info)
+{
+       if (info->dli_sname) {
+               if (strcmp(info->dli_sname, "<redacted>") == 0) {
+                       return _BACKTRACE_UUID_LEN;
+               } else {
+                       return strlen(info->dli_sname);
+               }
+       } else if (info->dli_fname) {
+               const char *tmp = strrchr(info->dli_fname, '/');
+               if (tmp == NULL) {
+                       return strlen(info->dli_fname);
+               } else {
+                       return strlen(tmp + 1);
+               }
+       } else {
+               return _BACKTRACE_ADDRESS_LEN;
+       }
 }
 
 char** backtrace_symbols(void* const* buffer, int size) {
@@ -108,60 +145,51 @@ char** backtrace_symbols(void* const* buffer, int size) {
        char** ptrs;
        intptr_t strs, end;
        Dl_info* info = calloc(size, sizeof (Dl_info));
-       
+
        if (info == NULL) return NULL;
-       
+
        // Compute the total size for the block that is returned.
        // The block will contain size number of pointers to the
        // symbol descriptions.
 
        total_bytes = sizeof(char*) * size;
-       
+
        // 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 += symbol_length(&info[i]);
        }
-       
+
        result = (char**)malloc(total_bytes);
        if (result == NULL) {
-               free(info);
-               return NULL;
+               goto error;
        }
        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;
+               if (chk < 0) {
+                       goto error;
                }
 
                ptrs[i] = (char*)strs;
                strs += chk + 1; // Step over the '\0'
        }
-       
+
        free(info);
-       
        return result;
+
+error:
+       free(info);
+       free(result);
+       return NULL;
 }
 
 void backtrace_symbols_fd(void* const* buffer, int size, int fd) {
@@ -185,6 +213,8 @@ void backtrace_symbols_fd(void* const* buffer, int size, int fd) {
        }
 }
 
+#endif // !TARGET_OS_DRIVERKIT
+
 void
 backtrace_image_offsets(void* const* buffer, struct image_offset *imgoffs, int size)
 {