Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Symbols.cpp ---------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Host/Symbols.h" |
| 11 | |
| 12 | // C Includes |
| 13 | #include <dirent.h> |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 14 | #include "llvm/Support/MachO.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | |
| 16 | // C++ Includes |
| 17 | // Other libraries and framework includes |
| 18 | #include <CoreFoundation/CoreFoundation.h> |
| 19 | |
| 20 | // Project includes |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Core/ArchSpec.h" |
| 22 | #include "lldb/Core/DataBuffer.h" |
| 23 | #include "lldb/Core/DataExtractor.h" |
| 24 | #include "lldb/Core/Timer.h" |
| 25 | #include "lldb/Core/UUID.h" |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 26 | #include "lldb/Host/Endian.h" |
Johnny Chen | cbf1591 | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 27 | #include "lldb/Host/Host.h" |
Greg Clayton | ad40027 | 2011-02-01 05:15:02 +0000 | [diff] [blame] | 28 | #include "lldb/Utility/CleanUp.h" |
Daniel Dunbar | a1ebbd2 | 2011-10-31 22:50:53 +0000 | [diff] [blame] | 29 | #include "Host/macosx/cfcpp/CFCBundle.h" |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 30 | #include "Host/macosx/cfcpp/CFCReleaser.h" |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 31 | #include "Host/macosx/cfcpp/CFCString.h" |
Chris Lattner | 5bc7b67 | 2010-09-08 23:01:14 +0000 | [diff] [blame] | 32 | #include "mach/machine.h" |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 33 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | using namespace lldb; |
| 36 | using namespace lldb_private; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 37 | using namespace llvm::MachO; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | |
Greg Clayton | 3e4238d | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 39 | #if !defined (__arm__) // No DebugSymbols on the iOS devices |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | extern "C" { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | CFURLRef DBGCopyFullDSYMURLForUUID (CFUUIDRef uuid, CFURLRef exec_url); |
| 43 | CFDictionaryRef DBGCopyDSYMPropertyLists (CFURLRef dsym_url); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 44 | |
| 45 | } |
Greg Clayton | 3e4238d | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 46 | #endif |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | |
| 48 | static bool |
| 49 | SkinnyMachOFileContainsArchAndUUID |
| 50 | ( |
| 51 | const FileSpec &file_spec, |
| 52 | const ArchSpec *arch, |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 53 | const lldb_private::UUID *uuid, // the UUID we are looking for |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | off_t file_offset, |
| 55 | DataExtractor& data, |
| 56 | uint32_t data_offset, |
| 57 | const uint32_t magic |
| 58 | ) |
| 59 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 60 | assert(magic == HeaderMagic32 || magic == HeaderMagic32Swapped || magic == HeaderMagic64 || magic == HeaderMagic64Swapped); |
| 61 | if (magic == HeaderMagic32 || magic == HeaderMagic64) |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 62 | data.SetByteOrder (lldb::endian::InlHostByteOrder()); |
| 63 | else if (lldb::endian::InlHostByteOrder() == eByteOrderBig) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 64 | data.SetByteOrder (eByteOrderLittle); |
| 65 | else |
| 66 | data.SetByteOrder (eByteOrderBig); |
| 67 | |
| 68 | uint32_t i; |
| 69 | const uint32_t cputype = data.GetU32(&data_offset); // cpu specifier |
| 70 | const uint32_t cpusubtype = data.GetU32(&data_offset); // machine specifier |
| 71 | data_offset+=4; // Skip mach file type |
| 72 | const uint32_t ncmds = data.GetU32(&data_offset); // number of load commands |
| 73 | const uint32_t sizeofcmds = data.GetU32(&data_offset); // the size of all the load commands |
| 74 | data_offset+=4; // Skip flags |
| 75 | |
| 76 | // Check the architecture if we have a valid arch pointer |
| 77 | if (arch) |
| 78 | { |
Greg Clayton | cf01505 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 79 | ArchSpec file_arch(eArchTypeMachO, cputype, cpusubtype); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 80 | |
| 81 | if (file_arch != *arch) |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | // The file exists, and if a valid arch pointer was passed in we know |
| 86 | // if already matches, so we can return if we aren't looking for a specific |
| 87 | // UUID |
| 88 | if (uuid == NULL) |
| 89 | return true; |
| 90 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 91 | if (magic == HeaderMagic64Swapped || magic == HeaderMagic64) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | data_offset += 4; // Skip reserved field for in mach_header_64 |
| 93 | |
| 94 | // Make sure we have enough data for all the load commands |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 95 | if (magic == HeaderMagic64Swapped || magic == HeaderMagic64) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 96 | { |
| 97 | if (data.GetByteSize() < sizeof(struct mach_header_64) + sizeofcmds) |
| 98 | { |
| 99 | DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, sizeof(struct mach_header_64) + sizeofcmds)); |
| 100 | data.SetData (data_buffer_sp); |
| 101 | } |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | if (data.GetByteSize() < sizeof(struct mach_header) + sizeofcmds) |
| 106 | { |
| 107 | DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, sizeof(struct mach_header) + sizeofcmds)); |
| 108 | data.SetData (data_buffer_sp); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | for (i=0; i<ncmds; i++) |
| 113 | { |
| 114 | const uint32_t cmd_offset = data_offset; // Save this data_offset in case parsing of the segment goes awry! |
| 115 | uint32_t cmd = data.GetU32(&data_offset); |
| 116 | uint32_t cmd_size = data.GetU32(&data_offset); |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 117 | if (cmd == LoadCommandUUID) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 118 | { |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 119 | lldb_private::UUID file_uuid (data.GetData(&data_offset, 16), 16); |
Johnny Chen | cbf1591 | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 120 | if (file_uuid == *uuid) |
| 121 | return true; |
| 122 | |
| 123 | // Emit some warning messages since the UUIDs do not match! |
| 124 | char path_buf[PATH_MAX]; |
| 125 | path_buf[0] = '\0'; |
| 126 | const char *path = file_spec.GetPath(path_buf, PATH_MAX) ? path_buf |
| 127 | : file_spec.GetFilename().AsCString(); |
| 128 | Host::SystemLog (Host::eSystemLogWarning, |
| 129 | "warning: UUID mismatch detected between binary and:\n\t'%s'\n", |
| 130 | path); |
| 131 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 132 | } |
| 133 | data_offset = cmd_offset + cmd_size; |
| 134 | } |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | bool |
| 139 | UniversalMachOFileContainsArchAndUUID |
| 140 | ( |
| 141 | const FileSpec &file_spec, |
| 142 | const ArchSpec *arch, |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 143 | const lldb_private::UUID *uuid, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | off_t file_offset, |
| 145 | DataExtractor& data, |
| 146 | uint32_t data_offset, |
| 147 | const uint32_t magic |
| 148 | ) |
| 149 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 150 | assert(magic == UniversalMagic || magic == UniversalMagicSwapped); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | |
| 152 | // Universal mach-o files always have their headers encoded as BIG endian |
| 153 | data.SetByteOrder(eByteOrderBig); |
| 154 | |
| 155 | uint32_t i; |
| 156 | const uint32_t nfat_arch = data.GetU32(&data_offset); // number of structs that follow |
| 157 | const uint32_t fat_header_and_arch_size = sizeof(struct fat_header) + nfat_arch * sizeof(struct fat_arch); |
| 158 | if (data.GetByteSize() < fat_header_and_arch_size) |
| 159 | { |
| 160 | DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, fat_header_and_arch_size)); |
| 161 | data.SetData (data_buffer_sp); |
| 162 | } |
| 163 | |
| 164 | for (i=0; i<nfat_arch; i++) |
| 165 | { |
| 166 | cpu_type_t arch_cputype = data.GetU32(&data_offset); // cpu specifier (int) |
| 167 | cpu_subtype_t arch_cpusubtype = data.GetU32(&data_offset); // machine specifier (int) |
| 168 | uint32_t arch_offset = data.GetU32(&data_offset); // file offset to this object file |
| 169 | // uint32_t arch_size = data.GetU32(&data_offset); // size of this object file |
| 170 | // uint32_t arch_align = data.GetU32(&data_offset); // alignment as a power of 2 |
| 171 | data_offset += 8; // Skip size and align as we don't need those |
| 172 | // Only process this slice if the cpu type/subtype matches |
| 173 | if (arch) |
| 174 | { |
Greg Clayton | cf01505 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 175 | ArchSpec fat_arch(eArchTypeMachO, arch_cputype, arch_cpusubtype); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 176 | if (fat_arch != *arch) |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | // Create a buffer with only the arch slice date in it |
| 181 | DataExtractor arch_data; |
| 182 | DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset + arch_offset, 0x1000)); |
| 183 | arch_data.SetData(data_buffer_sp); |
| 184 | uint32_t arch_data_offset = 0; |
| 185 | uint32_t arch_magic = arch_data.GetU32(&arch_data_offset); |
| 186 | |
| 187 | switch (arch_magic) |
| 188 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 189 | case HeaderMagic32: |
| 190 | case HeaderMagic32Swapped: |
| 191 | case HeaderMagic64: |
| 192 | case HeaderMagic64Swapped: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 193 | if (SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset + arch_offset, arch_data, arch_data_offset, arch_magic)) |
| 194 | return true; |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | static bool |
| 202 | FileAtPathContainsArchAndUUID |
| 203 | ( |
| 204 | const FileSpec &file_spec, |
| 205 | const ArchSpec *arch, |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 206 | const lldb_private::UUID *uuid |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 207 | ) |
| 208 | { |
| 209 | DataExtractor data; |
| 210 | off_t file_offset = 0; |
| 211 | DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, 0x1000)); |
| 212 | |
| 213 | if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) |
| 214 | { |
| 215 | data.SetData(data_buffer_sp); |
| 216 | |
| 217 | uint32_t data_offset = 0; |
| 218 | uint32_t magic = data.GetU32(&data_offset); |
| 219 | |
| 220 | switch (magic) |
| 221 | { |
| 222 | // 32 bit mach-o file |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 223 | case HeaderMagic32: |
| 224 | case HeaderMagic32Swapped: |
| 225 | case HeaderMagic64: |
| 226 | case HeaderMagic64Swapped: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 227 | return SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic); |
| 228 | |
| 229 | // fat mach-o file |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 230 | case UniversalMagic: |
| 231 | case UniversalMagicSwapped: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 232 | return UniversalMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic); |
| 233 | |
| 234 | default: |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | static FileSpec |
| 242 | LocateDSYMMachFileInDSYMBundle |
| 243 | ( |
| 244 | const FileSpec& dsym_bundle_fspec, |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 245 | const lldb_private::UUID *uuid, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 246 | const ArchSpec *arch) |
| 247 | { |
| 248 | char path[PATH_MAX]; |
| 249 | |
| 250 | FileSpec dsym_fspec; |
| 251 | |
| 252 | if (dsym_bundle_fspec.GetPath(path, sizeof(path))) |
| 253 | { |
| 254 | ::strncat (path, "/Contents/Resources/DWARF", sizeof(path) - strlen(path) - 1); |
| 255 | |
Greg Clayton | 52fd984 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 256 | lldb_utility::CleanUp <DIR *, int> dirp (opendir(path), NULL, closedir); |
Greg Clayton | ad40027 | 2011-02-01 05:15:02 +0000 | [diff] [blame] | 257 | if (dirp.is_valid()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 258 | { |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 259 | dsym_fspec.GetDirectory().SetCString(path); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 260 | struct dirent* dp; |
Greg Clayton | ad40027 | 2011-02-01 05:15:02 +0000 | [diff] [blame] | 261 | while ((dp = readdir(dirp.get())) != NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 262 | { |
| 263 | // Only search directories |
| 264 | if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN) |
| 265 | { |
| 266 | if (dp->d_namlen == 1 && dp->d_name[0] == '.') |
| 267 | continue; |
| 268 | |
| 269 | if (dp->d_namlen == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.') |
| 270 | continue; |
| 271 | } |
| 272 | |
| 273 | if (dp->d_type == DT_REG || dp->d_type == DT_UNKNOWN) |
| 274 | { |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 275 | dsym_fspec.GetFilename().SetCString(dp->d_name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 276 | if (FileAtPathContainsArchAndUUID (dsym_fspec, arch, uuid)) |
| 277 | return dsym_fspec; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | dsym_fspec.Clear(); |
| 283 | return dsym_fspec; |
| 284 | } |
| 285 | |
| 286 | static int |
| 287 | LocateMacOSXFilesUsingDebugSymbols |
| 288 | ( |
| 289 | const FileSpec *exec_fspec, // An executable path that may or may not be correct if UUID is specified |
| 290 | const ArchSpec* arch, // Limit the search to files with this architecture if non-NULL |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 291 | const lldb_private::UUID *uuid, // Match the UUID value if non-NULL, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 292 | FileSpec *out_exec_fspec, // If non-NULL, try and find the executable |
| 293 | FileSpec *out_dsym_fspec // If non-NULL try and find the debug symbol file |
| 294 | ) |
| 295 | { |
| 296 | int items_found = 0; |
| 297 | |
| 298 | if (out_exec_fspec) |
| 299 | out_exec_fspec->Clear(); |
| 300 | |
| 301 | if (out_dsym_fspec) |
| 302 | out_dsym_fspec->Clear(); |
| 303 | |
Greg Clayton | 3e4238d | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 304 | #if !defined (__arm__) // No DebugSymbols on the iOS devices |
| 305 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 306 | if (uuid && uuid->IsValid()) |
| 307 | { |
| 308 | // Try and locate the dSYM file using DebugSymbols first |
| 309 | const UInt8 *module_uuid = (const UInt8 *)uuid->GetBytes(); |
| 310 | if (module_uuid != NULL) |
| 311 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 312 | CFCReleaser<CFUUIDRef> module_uuid_ref(::CFUUIDCreateWithBytes (NULL, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 313 | module_uuid[0], |
| 314 | module_uuid[1], |
| 315 | module_uuid[2], |
| 316 | module_uuid[3], |
| 317 | module_uuid[4], |
| 318 | module_uuid[5], |
| 319 | module_uuid[6], |
| 320 | module_uuid[7], |
| 321 | module_uuid[8], |
| 322 | module_uuid[9], |
| 323 | module_uuid[10], |
| 324 | module_uuid[11], |
| 325 | module_uuid[12], |
| 326 | module_uuid[13], |
| 327 | module_uuid[14], |
| 328 | module_uuid[15])); |
| 329 | |
| 330 | if (module_uuid_ref.get()) |
| 331 | { |
| 332 | CFCReleaser<CFURLRef> exec_url; |
| 333 | |
| 334 | if (exec_fspec) |
| 335 | { |
| 336 | char exec_cf_path[PATH_MAX]; |
| 337 | if (exec_fspec->GetPath(exec_cf_path, sizeof(exec_cf_path))) |
| 338 | exec_url.reset(::CFURLCreateFromFileSystemRepresentation (NULL, |
| 339 | (const UInt8 *)exec_cf_path, |
| 340 | strlen(exec_cf_path), |
| 341 | FALSE)); |
| 342 | } |
| 343 | |
| 344 | CFCReleaser<CFURLRef> dsym_url (::DBGCopyFullDSYMURLForUUID(module_uuid_ref.get(), exec_url.get())); |
| 345 | char path[PATH_MAX]; |
| 346 | |
| 347 | if (dsym_url.get()) |
| 348 | { |
| 349 | if (out_dsym_fspec) |
| 350 | { |
| 351 | if (::CFURLGetFileSystemRepresentation (dsym_url.get(), true, (UInt8*)path, sizeof(path)-1)) |
| 352 | { |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 353 | out_dsym_fspec->SetFile(path, false); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 354 | |
| 355 | if (out_dsym_fspec->GetFileType () == FileSpec::eFileTypeDirectory) |
| 356 | { |
| 357 | *out_dsym_fspec = LocateDSYMMachFileInDSYMBundle (*out_dsym_fspec, uuid, arch); |
| 358 | if (*out_dsym_fspec) |
| 359 | ++items_found; |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | ++items_found; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if (out_exec_fspec) |
| 369 | { |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame^] | 370 | CFCReleaser<CFDictionaryRef> dict(::DBGCopyDSYMPropertyLists (dsym_url.get())); |
| 371 | bool success = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 372 | if (dict.get()) |
| 373 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 374 | char uuid_cstr_buf[64]; |
| 375 | const char *uuid_cstr = uuid->GetAsCString (uuid_cstr_buf, sizeof(uuid_cstr_buf)); |
| 376 | CFCString uuid_cfstr (uuid_cstr); |
| 377 | CFDictionaryRef uuid_dict = static_cast<CFDictionaryRef>(::CFDictionaryGetValue (dict.get(), uuid_cfstr.get())); |
| 378 | if (uuid_dict) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 379 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 380 | CFStringRef exec_cf_path = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGSymbolRichExecutable"))); |
| 381 | if (exec_cf_path && ::CFStringGetFileSystemRepresentation (exec_cf_path, path, sizeof(path))) |
| 382 | { |
| 383 | ++items_found; |
| 384 | out_exec_fspec->SetFile(path, path[0] == '~'); |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame^] | 385 | if (out_exec_fspec->Exists()) |
| 386 | success = true; |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 387 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame^] | 390 | |
| 391 | if (!success) |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 392 | { |
| 393 | // No dictionary, check near the dSYM bundle for an executable that matches... |
| 394 | if (::CFURLGetFileSystemRepresentation (dsym_url.get(), true, (UInt8*)path, sizeof(path)-1)) |
| 395 | { |
| 396 | char *dsym_extension_pos = ::strstr (path, ".dSYM"); |
| 397 | if (dsym_extension_pos) |
| 398 | { |
| 399 | *dsym_extension_pos = '\0'; |
| 400 | FileSpec file_spec (path, true); |
| 401 | switch (file_spec.GetFileType()) |
| 402 | { |
| 403 | case FileSpec::eFileTypeDirectory: // Bundle directory? |
| 404 | { |
| 405 | CFCBundle bundle (path); |
| 406 | CFCReleaser<CFURLRef> bundle_exe_url (bundle.CopyExecutableURL ()); |
| 407 | if (bundle_exe_url.get()) |
| 408 | { |
| 409 | if (::CFURLGetFileSystemRepresentation (bundle_exe_url.get(), true, (UInt8*)path, sizeof(path)-1)) |
| 410 | { |
| 411 | FileSpec bundle_exe_file_spec (path, true); |
| 412 | |
| 413 | if (FileAtPathContainsArchAndUUID (bundle_exe_file_spec, arch, uuid)) |
| 414 | { |
| 415 | ++items_found; |
| 416 | *out_exec_fspec = bundle_exe_file_spec; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | break; |
| 422 | |
| 423 | case FileSpec::eFileTypePipe: // Forget pipes |
| 424 | case FileSpec::eFileTypeSocket: // We can't process socket files |
| 425 | case FileSpec::eFileTypeInvalid: // File doesn't exist... |
| 426 | break; |
| 427 | |
| 428 | case FileSpec::eFileTypeUnknown: |
| 429 | case FileSpec::eFileTypeRegular: |
| 430 | case FileSpec::eFileTypeSymbolicLink: |
| 431 | case FileSpec::eFileTypeOther: |
| 432 | if (FileAtPathContainsArchAndUUID (file_spec, arch, uuid)) |
| 433 | { |
| 434 | ++items_found; |
| 435 | *out_exec_fspec = file_spec; |
| 436 | } |
| 437 | break; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | } |
Greg Clayton | 3e4238d | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 447 | #endif // #if !defined (__arm__) |
| 448 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 449 | return items_found; |
| 450 | } |
| 451 | |
| 452 | static bool |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 453 | LocateDSYMInVincinityOfExecutable (const FileSpec *exec_fspec, const ArchSpec* arch, const lldb_private::UUID *uuid, FileSpec &dsym_fspec) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 454 | { |
| 455 | if (exec_fspec) |
| 456 | { |
| 457 | char path[PATH_MAX]; |
| 458 | if (exec_fspec->GetPath(path, sizeof(path))) |
| 459 | { |
| 460 | // Make sure the module isn't already just a dSYM file... |
| 461 | if (strcasestr(path, ".dSYM/Contents/Resources/DWARF") == NULL) |
| 462 | { |
| 463 | size_t obj_file_path_length = strlen(path); |
| 464 | strncat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path)); |
| 465 | strncat(path, exec_fspec->GetFilename().AsCString(), sizeof(path)); |
| 466 | |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 467 | dsym_fspec.SetFile(path, false); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 468 | |
Greg Clayton | db2dc2b | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 469 | if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, arch, uuid)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 470 | { |
| 471 | return true; |
| 472 | } |
| 473 | else |
| 474 | { |
| 475 | path[obj_file_path_length] = '\0'; |
| 476 | |
| 477 | char *last_dot = strrchr(path, '.'); |
| 478 | while (last_dot != NULL && last_dot[0]) |
| 479 | { |
| 480 | char *next_slash = strchr(last_dot, '/'); |
| 481 | if (next_slash != NULL) |
| 482 | { |
| 483 | *next_slash = '\0'; |
| 484 | strncat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path)); |
| 485 | strncat(path, exec_fspec->GetFilename().AsCString(), sizeof(path)); |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 486 | dsym_fspec.SetFile(path, false); |
Greg Clayton | db2dc2b | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 487 | if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, arch, uuid)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 488 | return true; |
| 489 | else |
| 490 | { |
| 491 | *last_dot = '\0'; |
| 492 | char *prev_slash = strrchr(path, '/'); |
| 493 | if (prev_slash != NULL) |
| 494 | *prev_slash = '\0'; |
| 495 | else |
| 496 | break; |
| 497 | } |
| 498 | } |
| 499 | else |
| 500 | { |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | dsym_fspec.Clear(); |
| 509 | return false; |
| 510 | } |
| 511 | |
| 512 | FileSpec |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 513 | Symbols::LocateExecutableObjectFile (const FileSpec *exec_fspec, const ArchSpec* arch, const lldb_private::UUID *uuid) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 514 | { |
| 515 | Timer scoped_timer (__PRETTY_FUNCTION__, |
| 516 | "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)", |
| 517 | exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>", |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 518 | arch ? arch->GetArchitectureName() : "<NULL>", |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 519 | uuid); |
| 520 | |
| 521 | FileSpec objfile_fspec; |
| 522 | if (exec_fspec && FileAtPathContainsArchAndUUID (*exec_fspec, arch, uuid)) |
| 523 | objfile_fspec = *exec_fspec; |
| 524 | else |
| 525 | LocateMacOSXFilesUsingDebugSymbols (exec_fspec, arch, uuid, &objfile_fspec, NULL); |
| 526 | return objfile_fspec; |
| 527 | } |
| 528 | |
| 529 | FileSpec |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 530 | Symbols::LocateExecutableSymbolFile (const FileSpec *exec_fspec, const ArchSpec* arch, const lldb_private::UUID *uuid) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 531 | { |
| 532 | Timer scoped_timer (__PRETTY_FUNCTION__, |
| 533 | "LocateExecutableSymbolFile (file = %s, arch = %s, uuid = %p)", |
| 534 | exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>", |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 535 | arch ? arch->GetArchitectureName() : "<NULL>", |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 536 | uuid); |
| 537 | |
| 538 | FileSpec symbol_fspec; |
| 539 | // First try and find the dSYM in the same directory as the executable or in |
| 540 | // an appropriate parent directory |
| 541 | if (LocateDSYMInVincinityOfExecutable (exec_fspec, arch, uuid, symbol_fspec) == false) |
| 542 | { |
| 543 | // We failed to easily find the dSYM above, so use DebugSymbols |
| 544 | LocateMacOSXFilesUsingDebugSymbols (exec_fspec, arch, uuid, NULL, &symbol_fspec); |
| 545 | } |
| 546 | return symbol_fspec; |
| 547 | } |