]>
Commit | Line | Data |
---|---|---|
224c7076 | 1 | /* |
34e8f829 | 2 | * Copyright (c) 2007, 2008 Apple Inc. All rights reserved. |
224c7076 A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
507116e3 | 24 | #include <TargetConditionals.h> |
224c7076 A |
25 | #include <mach/vm_types.h> |
26 | #include <sys/uio.h> | |
70ad1dc8 A |
27 | #include <errno.h> |
28 | #include <mach-o/dyld_priv.h> | |
34e8f829 | 29 | #include <stdint.h> |
224c7076 | 30 | #include <stdlib.h> |
70ad1dc8 | 31 | #include <uuid/uuid.h> |
224c7076 A |
32 | #include "execinfo.h" |
33 | ||
70ad1dc8 A |
34 | extern void _thread_stack_pcs(vm_address_t *buffer, unsigned max, |
35 | unsigned *nb, unsigned skip, void *startfp); | |
36 | ||
224c7076 | 37 | int backtrace(void** buffer, int size) { |
224c7076 | 38 | unsigned int num_frames; |
70ad1dc8 A |
39 | _thread_stack_pcs((vm_address_t*)buffer, size, &num_frames, 1, NULL); |
40 | while (num_frames >= 1 && buffer[num_frames-1] == NULL) num_frames -= 1; | |
41 | return num_frames; | |
42 | } | |
43 | ||
44 | int | |
45 | backtrace_from_fp(void *startfp, void **buffer, int size) | |
46 | { | |
47 | unsigned int num_frames; | |
48 | _thread_stack_pcs((vm_address_t*)buffer, size, &num_frames, 1, startfp); | |
224c7076 A |
49 | while (num_frames >= 1 && buffer[num_frames-1] == NULL) num_frames -= 1; |
50 | return num_frames; | |
51 | } | |
52 | ||
507116e3 A |
53 | #if !TARGET_OS_DRIVERKIT |
54 | ||
55 | #include <dlfcn.h> | |
56 | #include <stdio.h> | |
57 | #include <string.h> | |
58 | #include "stack_logging.h" | |
59 | ||
224c7076 | 60 | #if __LP64__ |
34e8f829 | 61 | #define _BACKTRACE_FORMAT "%-4d%-35s 0x%016lx %s + %lu" |
1f2f436a A |
62 | #define _BACKTRACE_FORMAT_SIZE 83 /* %lu can take up to 20, does not include %s, includes NUL */ |
63 | #define _BACKTRACE_ADDRESS_LEN 18 /* 0x + 16 (no NUL) */ | |
224c7076 | 64 | #else |
34e8f829 | 65 | #define _BACKTRACE_FORMAT "%-4d%-35s 0x%08lx %s + %lu" |
1f2f436a A |
66 | #define _BACKTRACE_FORMAT_SIZE 65 /* %lu can take up to 10, does not include %s, includes NUL */ |
67 | #define _BACKTRACE_ADDRESS_LEN 10 /* 0x + 8 (no NUL) */ | |
224c7076 | 68 | #endif |
507116e3 A |
69 | #define _BACKTRACE_IMAGE_LEN 35 |
70 | #define _BACKTRACE_UUID_LEN 36 | |
224c7076 | 71 | |
224c7076 | 72 | static int _backtrace_snprintf(char* buf, size_t size, int frame, const void* addr, const Dl_info* info) { |
507116e3 A |
73 | char addrbuf[_BACKTRACE_ADDRESS_LEN + 1]; |
74 | char imagebuf[_BACKTRACE_IMAGE_LEN + 1]; | |
75 | uuid_string_t uuidbuf; | |
224c7076 | 76 | const char* image = "???"; |
1f2f436a A |
77 | const char* symbol = "0x0"; |
78 | uintptr_t symbol_offset = 0; | |
224c7076 A |
79 | |
80 | if (info->dli_fname) { | |
1f2f436a | 81 | const char *tmp = strrchr(info->dli_fname, '/'); |
507116e3 A |
82 | if (tmp == NULL) { |
83 | strlcpy(imagebuf, info->dli_fname, sizeof(imagebuf)); | |
84 | } else { | |
85 | strlcpy(imagebuf, tmp + 1, sizeof(imagebuf)); | |
86 | } | |
87 | image = imagebuf; | |
224c7076 | 88 | } |
507116e3 | 89 | |
224c7076 | 90 | if (info->dli_sname) { |
507116e3 A |
91 | uuid_t uuid; |
92 | if (strcmp(info->dli_sname, "<redacted>") == 0 && | |
93 | _dyld_get_image_uuid(info->dli_fbase, uuid)) { | |
94 | /* | |
95 | * dyld returns <redacted> when the symbol name has been elided in | |
96 | * the shared cache. To enable symbolication later, we provide the | |
97 | * UUID and UUID-offset instead. | |
98 | */ | |
99 | uuid_unparse(uuid, uuidbuf); | |
100 | symbol = uuidbuf; | |
101 | symbol_offset = (uintptr_t)addr - (uintptr_t)info->dli_fbase; | |
102 | } else { | |
103 | symbol = info->dli_sname; | |
104 | symbol_offset = (uintptr_t)addr - (uintptr_t)info->dli_saddr; | |
105 | } | |
106 | } else if (info->dli_fname) { | |
1f2f436a A |
107 | symbol = image; |
108 | symbol_offset = (uintptr_t)addr - (uintptr_t)info->dli_fbase; | |
507116e3 A |
109 | } else if (0 < snprintf(addrbuf, sizeof(addrbuf), "0x%lx", |
110 | (uintptr_t)info->dli_saddr)) { | |
111 | symbol = addrbuf; | |
1f2f436a | 112 | symbol_offset = (uintptr_t)addr - (uintptr_t)info->dli_saddr; |
224c7076 | 113 | } else { |
1f2f436a | 114 | symbol_offset = (uintptr_t)addr; |
224c7076 A |
115 | } |
116 | ||
507116e3 A |
117 | return snprintf(buf, size, _BACKTRACE_FORMAT, frame, image, |
118 | (uintptr_t)addr, symbol, symbol_offset); | |
119 | } | |
120 | ||
121 | static size_t symbol_length(Dl_info *info) | |
122 | { | |
123 | if (info->dli_sname) { | |
124 | if (strcmp(info->dli_sname, "<redacted>") == 0) { | |
125 | return _BACKTRACE_UUID_LEN; | |
126 | } else { | |
127 | return strlen(info->dli_sname); | |
128 | } | |
129 | } else if (info->dli_fname) { | |
130 | const char *tmp = strrchr(info->dli_fname, '/'); | |
131 | if (tmp == NULL) { | |
132 | return strlen(info->dli_fname); | |
133 | } else { | |
134 | return strlen(tmp + 1); | |
135 | } | |
136 | } else { | |
137 | return _BACKTRACE_ADDRESS_LEN; | |
138 | } | |
224c7076 A |
139 | } |
140 | ||
141 | char** backtrace_symbols(void* const* buffer, int size) { | |
142 | int i; | |
143 | size_t total_bytes; | |
144 | char** result; | |
145 | char** ptrs; | |
1f2f436a | 146 | intptr_t strs, end; |
224c7076 | 147 | Dl_info* info = calloc(size, sizeof (Dl_info)); |
507116e3 | 148 | |
224c7076 | 149 | if (info == NULL) return NULL; |
507116e3 | 150 | |
224c7076 A |
151 | // Compute the total size for the block that is returned. |
152 | // The block will contain size number of pointers to the | |
153 | // symbol descriptions. | |
154 | ||
155 | total_bytes = sizeof(char*) * size; | |
507116e3 | 156 | |
224c7076 A |
157 | // Plus each symbol description |
158 | for (i = 0 ; i < size; ++i) { | |
159 | dladdr(buffer[i], &info[i]); | |
1f2f436a | 160 | total_bytes += _BACKTRACE_FORMAT_SIZE; |
507116e3 | 161 | total_bytes += symbol_length(&info[i]); |
224c7076 | 162 | } |
507116e3 | 163 | |
224c7076 A |
164 | result = (char**)malloc(total_bytes); |
165 | if (result == NULL) { | |
507116e3 | 166 | goto error; |
224c7076 | 167 | } |
1f2f436a | 168 | end = (intptr_t)result + total_bytes; |
507116e3 | 169 | |
224c7076 A |
170 | // Fill in the array of pointers and append the strings for |
171 | // each symbol description. | |
507116e3 | 172 | |
224c7076 A |
173 | ptrs = result; |
174 | strs = ((intptr_t)result) + sizeof(char*) * size; | |
1f2f436a | 175 | |
224c7076 | 176 | for (i = 0; i < size; ++i) { |
1f2f436a | 177 | int chk = _backtrace_snprintf((char*)strs, end - (intptr_t)strs, i, buffer[i], &info[i]); |
507116e3 A |
178 | if (chk < 0) { |
179 | goto error; | |
1f2f436a A |
180 | } |
181 | ||
224c7076 | 182 | ptrs[i] = (char*)strs; |
6465356a | 183 | strs += chk + 1; // Step over the '\0' |
224c7076 | 184 | } |
507116e3 | 185 | |
224c7076 | 186 | free(info); |
224c7076 | 187 | return result; |
507116e3 A |
188 | |
189 | error: | |
190 | free(info); | |
191 | free(result); | |
192 | return NULL; | |
224c7076 A |
193 | } |
194 | ||
195 | void backtrace_symbols_fd(void* const* buffer, int size, int fd) { | |
196 | int i; | |
197 | char buf[BUFSIZ]; | |
198 | Dl_info info; | |
199 | struct iovec iov[2]; | |
200 | ||
201 | iov[0].iov_base = buf; | |
202 | ||
203 | iov[1].iov_base = "\n"; | |
204 | iov[1].iov_len = 1; | |
205 | ||
206 | for (i = 0; i < size; ++i) { | |
207 | memset(&info, 0, sizeof(info)); | |
208 | dladdr(buffer[i], &info); | |
209 | ||
210 | iov[0].iov_len = _backtrace_snprintf(buf, sizeof(buf), i, buffer[i], &info); | |
70ad1dc8 | 211 | |
224c7076 A |
212 | writev(fd, iov, 2); |
213 | } | |
214 | } | |
70ad1dc8 | 215 | |
507116e3 A |
216 | #endif // !TARGET_OS_DRIVERKIT |
217 | ||
70ad1dc8 A |
218 | void |
219 | backtrace_image_offsets(void* const* buffer, struct image_offset *imgoffs, int size) | |
220 | { | |
221 | struct dyld_image_uuid_offset infos[size]; | |
222 | _dyld_images_for_addresses(size, (const void **)buffer, infos); | |
223 | ||
224 | for (int i = 0; i < size; i++) { | |
225 | uuid_copy(imgoffs[i].uuid, infos[i].uuid); | |
226 | imgoffs[i].offset = infos[i].offsetInImage; | |
227 | }; | |
228 | } |