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