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> |
Jason Molenda | f256975 | 2012-10-30 21:26:30 +0000 | [diff] [blame] | 14 | #include <pwd.h> |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 15 | #include "llvm/Support/MachO.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | |
| 17 | // C++ Includes |
| 18 | // Other libraries and framework includes |
| 19 | #include <CoreFoundation/CoreFoundation.h> |
| 20 | |
| 21 | // Project includes |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Core/ArchSpec.h" |
| 23 | #include "lldb/Core/DataBuffer.h" |
| 24 | #include "lldb/Core/DataExtractor.h" |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 25 | #include "lldb/Core/Module.h" |
Greg Clayton | 49ce896 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 26 | #include "lldb/Core/ModuleSpec.h" |
Johnny Chen | 9262cd5 | 2012-08-22 00:18:43 +0000 | [diff] [blame] | 27 | #include "lldb/Core/StreamString.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | #include "lldb/Core/Timer.h" |
| 29 | #include "lldb/Core/UUID.h" |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 30 | #include "lldb/Host/Endian.h" |
Johnny Chen | cbf1591 | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 31 | #include "lldb/Host/Host.h" |
Greg Clayton | ad40027 | 2011-02-01 05:15:02 +0000 | [diff] [blame] | 32 | #include "lldb/Utility/CleanUp.h" |
Daniel Dunbar | a1ebbd2 | 2011-10-31 22:50:53 +0000 | [diff] [blame] | 33 | #include "Host/macosx/cfcpp/CFCBundle.h" |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 34 | #include "Host/macosx/cfcpp/CFCData.h" |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 35 | #include "Host/macosx/cfcpp/CFCReleaser.h" |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 36 | #include "Host/macosx/cfcpp/CFCString.h" |
Chris Lattner | 5bc7b67 | 2010-09-08 23:01:14 +0000 | [diff] [blame] | 37 | #include "mach/machine.h" |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 38 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | using namespace lldb; |
| 41 | using namespace lldb_private; |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 42 | using namespace llvm::MachO; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | |
Greg Clayton | 3e4238d | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 44 | #if !defined (__arm__) // No DebugSymbols on the iOS devices |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 45 | extern "C" { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | CFURLRef DBGCopyFullDSYMURLForUUID (CFUUIDRef uuid, CFURLRef exec_url); |
| 48 | CFDictionaryRef DBGCopyDSYMPropertyLists (CFURLRef dsym_url); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 49 | |
| 50 | } |
Greg Clayton | 3e4238d | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 51 | #endif |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | |
| 53 | static bool |
| 54 | SkinnyMachOFileContainsArchAndUUID |
| 55 | ( |
| 56 | const FileSpec &file_spec, |
| 57 | const ArchSpec *arch, |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 58 | const lldb_private::UUID *uuid, // the UUID we are looking for |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | off_t file_offset, |
| 60 | DataExtractor& data, |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 61 | lldb::offset_t data_offset, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | const uint32_t magic |
| 63 | ) |
| 64 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 65 | assert(magic == HeaderMagic32 || magic == HeaderMagic32Swapped || magic == HeaderMagic64 || magic == HeaderMagic64Swapped); |
| 66 | if (magic == HeaderMagic32 || magic == HeaderMagic64) |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 67 | data.SetByteOrder (lldb::endian::InlHostByteOrder()); |
| 68 | else if (lldb::endian::InlHostByteOrder() == eByteOrderBig) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 69 | data.SetByteOrder (eByteOrderLittle); |
| 70 | else |
| 71 | data.SetByteOrder (eByteOrderBig); |
| 72 | |
| 73 | uint32_t i; |
| 74 | const uint32_t cputype = data.GetU32(&data_offset); // cpu specifier |
| 75 | const uint32_t cpusubtype = data.GetU32(&data_offset); // machine specifier |
| 76 | data_offset+=4; // Skip mach file type |
| 77 | const uint32_t ncmds = data.GetU32(&data_offset); // number of load commands |
| 78 | const uint32_t sizeofcmds = data.GetU32(&data_offset); // the size of all the load commands |
| 79 | data_offset+=4; // Skip flags |
| 80 | |
| 81 | // Check the architecture if we have a valid arch pointer |
| 82 | if (arch) |
| 83 | { |
Greg Clayton | cf01505 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 84 | ArchSpec file_arch(eArchTypeMachO, cputype, cpusubtype); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 85 | |
Sean Callanan | 878f13c | 2012-12-14 23:43:03 +0000 | [diff] [blame] | 86 | if (!file_arch.IsCompatibleMatch(*arch)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 87 | return false; |
| 88 | } |
| 89 | |
| 90 | // The file exists, and if a valid arch pointer was passed in we know |
| 91 | // if already matches, so we can return if we aren't looking for a specific |
| 92 | // UUID |
| 93 | if (uuid == NULL) |
| 94 | return true; |
| 95 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 96 | if (magic == HeaderMagic64Swapped || magic == HeaderMagic64) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 97 | data_offset += 4; // Skip reserved field for in mach_header_64 |
| 98 | |
| 99 | // Make sure we have enough data for all the load commands |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 100 | if (magic == HeaderMagic64Swapped || magic == HeaderMagic64) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 101 | { |
| 102 | if (data.GetByteSize() < sizeof(struct mach_header_64) + sizeofcmds) |
| 103 | { |
| 104 | DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, sizeof(struct mach_header_64) + sizeofcmds)); |
| 105 | data.SetData (data_buffer_sp); |
| 106 | } |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | if (data.GetByteSize() < sizeof(struct mach_header) + sizeofcmds) |
| 111 | { |
| 112 | DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, sizeof(struct mach_header) + sizeofcmds)); |
| 113 | data.SetData (data_buffer_sp); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | for (i=0; i<ncmds; i++) |
| 118 | { |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 119 | const lldb::offset_t cmd_offset = data_offset; // Save this data_offset in case parsing of the segment goes awry! |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 120 | uint32_t cmd = data.GetU32(&data_offset); |
| 121 | uint32_t cmd_size = data.GetU32(&data_offset); |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 122 | if (cmd == LoadCommandUUID) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 123 | { |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 124 | lldb_private::UUID file_uuid (data.GetData(&data_offset, 16), 16); |
Johnny Chen | cbf1591 | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 125 | if (file_uuid == *uuid) |
| 126 | return true; |
Johnny Chen | cbf1591 | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 127 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 128 | } |
| 129 | data_offset = cmd_offset + cmd_size; |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | bool |
| 135 | UniversalMachOFileContainsArchAndUUID |
| 136 | ( |
| 137 | const FileSpec &file_spec, |
| 138 | const ArchSpec *arch, |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 139 | const lldb_private::UUID *uuid, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 140 | off_t file_offset, |
| 141 | DataExtractor& data, |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 142 | lldb::offset_t data_offset, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | const uint32_t magic |
| 144 | ) |
| 145 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 146 | assert(magic == UniversalMagic || magic == UniversalMagicSwapped); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | |
| 148 | // Universal mach-o files always have their headers encoded as BIG endian |
| 149 | data.SetByteOrder(eByteOrderBig); |
| 150 | |
| 151 | uint32_t i; |
| 152 | const uint32_t nfat_arch = data.GetU32(&data_offset); // number of structs that follow |
| 153 | const uint32_t fat_header_and_arch_size = sizeof(struct fat_header) + nfat_arch * sizeof(struct fat_arch); |
| 154 | if (data.GetByteSize() < fat_header_and_arch_size) |
| 155 | { |
| 156 | DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, fat_header_and_arch_size)); |
| 157 | data.SetData (data_buffer_sp); |
| 158 | } |
| 159 | |
| 160 | for (i=0; i<nfat_arch; i++) |
| 161 | { |
| 162 | cpu_type_t arch_cputype = data.GetU32(&data_offset); // cpu specifier (int) |
| 163 | cpu_subtype_t arch_cpusubtype = data.GetU32(&data_offset); // machine specifier (int) |
| 164 | uint32_t arch_offset = data.GetU32(&data_offset); // file offset to this object file |
| 165 | // uint32_t arch_size = data.GetU32(&data_offset); // size of this object file |
| 166 | // uint32_t arch_align = data.GetU32(&data_offset); // alignment as a power of 2 |
| 167 | data_offset += 8; // Skip size and align as we don't need those |
| 168 | // Only process this slice if the cpu type/subtype matches |
| 169 | if (arch) |
| 170 | { |
Greg Clayton | cf01505 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 171 | ArchSpec fat_arch(eArchTypeMachO, arch_cputype, arch_cpusubtype); |
Sean Callanan | 64246cd | 2012-12-21 21:32:25 +0000 | [diff] [blame] | 172 | if (!fat_arch.IsExactMatch(*arch)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 173 | continue; |
| 174 | } |
| 175 | |
| 176 | // Create a buffer with only the arch slice date in it |
| 177 | DataExtractor arch_data; |
| 178 | DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset + arch_offset, 0x1000)); |
| 179 | arch_data.SetData(data_buffer_sp); |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 180 | lldb::offset_t arch_data_offset = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 181 | uint32_t arch_magic = arch_data.GetU32(&arch_data_offset); |
| 182 | |
| 183 | switch (arch_magic) |
| 184 | { |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 185 | case HeaderMagic32: |
| 186 | case HeaderMagic32Swapped: |
| 187 | case HeaderMagic64: |
| 188 | case HeaderMagic64Swapped: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 189 | if (SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset + arch_offset, arch_data, arch_data_offset, arch_magic)) |
| 190 | return true; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | static bool |
| 198 | FileAtPathContainsArchAndUUID |
| 199 | ( |
| 200 | const FileSpec &file_spec, |
| 201 | const ArchSpec *arch, |
Greg Clayton | 0467c78 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 202 | const lldb_private::UUID *uuid |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 203 | ) |
| 204 | { |
| 205 | DataExtractor data; |
| 206 | off_t file_offset = 0; |
| 207 | DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, 0x1000)); |
| 208 | |
| 209 | if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) |
| 210 | { |
| 211 | data.SetData(data_buffer_sp); |
| 212 | |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 213 | lldb::offset_t data_offset = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 214 | uint32_t magic = data.GetU32(&data_offset); |
| 215 | |
| 216 | switch (magic) |
| 217 | { |
| 218 | // 32 bit mach-o file |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 219 | case HeaderMagic32: |
| 220 | case HeaderMagic32Swapped: |
| 221 | case HeaderMagic64: |
| 222 | case HeaderMagic64Swapped: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 223 | return SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic); |
| 224 | |
| 225 | // fat mach-o file |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 226 | case UniversalMagic: |
| 227 | case UniversalMagicSwapped: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | return UniversalMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic); |
| 229 | |
| 230 | default: |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | return false; |
| 235 | } |
| 236 | |
Greg Clayton | 95b765e | 2012-09-12 02:03:59 +0000 | [diff] [blame] | 237 | FileSpec |
| 238 | Symbols::FindSymbolFileInBundle (const FileSpec& dsym_bundle_fspec, |
| 239 | const lldb_private::UUID *uuid, |
| 240 | const ArchSpec *arch) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 241 | { |
| 242 | char path[PATH_MAX]; |
| 243 | |
| 244 | FileSpec dsym_fspec; |
| 245 | |
| 246 | if (dsym_bundle_fspec.GetPath(path, sizeof(path))) |
| 247 | { |
| 248 | ::strncat (path, "/Contents/Resources/DWARF", sizeof(path) - strlen(path) - 1); |
| 249 | |
Greg Clayton | 52fd984 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 250 | lldb_utility::CleanUp <DIR *, int> dirp (opendir(path), NULL, closedir); |
Greg Clayton | ad40027 | 2011-02-01 05:15:02 +0000 | [diff] [blame] | 251 | if (dirp.is_valid()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | { |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 253 | dsym_fspec.GetDirectory().SetCString(path); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 254 | struct dirent* dp; |
Greg Clayton | ad40027 | 2011-02-01 05:15:02 +0000 | [diff] [blame] | 255 | while ((dp = readdir(dirp.get())) != NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 256 | { |
| 257 | // Only search directories |
| 258 | if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN) |
| 259 | { |
| 260 | if (dp->d_namlen == 1 && dp->d_name[0] == '.') |
| 261 | continue; |
| 262 | |
| 263 | if (dp->d_namlen == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.') |
| 264 | continue; |
| 265 | } |
| 266 | |
| 267 | if (dp->d_type == DT_REG || dp->d_type == DT_UNKNOWN) |
| 268 | { |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 269 | dsym_fspec.GetFilename().SetCString(dp->d_name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 270 | if (FileAtPathContainsArchAndUUID (dsym_fspec, arch, uuid)) |
| 271 | return dsym_fspec; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | dsym_fspec.Clear(); |
| 277 | return dsym_fspec; |
| 278 | } |
| 279 | |
| 280 | static int |
| 281 | LocateMacOSXFilesUsingDebugSymbols |
| 282 | ( |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 283 | const ModuleSpec &module_spec, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 284 | FileSpec *out_exec_fspec, // If non-NULL, try and find the executable |
| 285 | FileSpec *out_dsym_fspec // If non-NULL try and find the debug symbol file |
| 286 | ) |
| 287 | { |
| 288 | int items_found = 0; |
| 289 | |
| 290 | if (out_exec_fspec) |
| 291 | out_exec_fspec->Clear(); |
| 292 | |
| 293 | if (out_dsym_fspec) |
| 294 | out_dsym_fspec->Clear(); |
| 295 | |
Greg Clayton | 3e4238d | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 296 | #if !defined (__arm__) // No DebugSymbols on the iOS devices |
| 297 | |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 298 | const UUID *uuid = module_spec.GetUUIDPtr(); |
| 299 | const ArchSpec *arch = module_spec.GetArchitecturePtr(); |
| 300 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 301 | if (uuid && uuid->IsValid()) |
| 302 | { |
| 303 | // Try and locate the dSYM file using DebugSymbols first |
| 304 | const UInt8 *module_uuid = (const UInt8 *)uuid->GetBytes(); |
| 305 | if (module_uuid != NULL) |
| 306 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 307 | CFCReleaser<CFUUIDRef> module_uuid_ref(::CFUUIDCreateWithBytes (NULL, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 308 | module_uuid[0], |
| 309 | module_uuid[1], |
| 310 | module_uuid[2], |
| 311 | module_uuid[3], |
| 312 | module_uuid[4], |
| 313 | module_uuid[5], |
| 314 | module_uuid[6], |
| 315 | module_uuid[7], |
| 316 | module_uuid[8], |
| 317 | module_uuid[9], |
| 318 | module_uuid[10], |
| 319 | module_uuid[11], |
| 320 | module_uuid[12], |
| 321 | module_uuid[13], |
| 322 | module_uuid[14], |
| 323 | module_uuid[15])); |
| 324 | |
| 325 | if (module_uuid_ref.get()) |
| 326 | { |
| 327 | CFCReleaser<CFURLRef> exec_url; |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 328 | const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 329 | if (exec_fspec) |
| 330 | { |
| 331 | char exec_cf_path[PATH_MAX]; |
| 332 | if (exec_fspec->GetPath(exec_cf_path, sizeof(exec_cf_path))) |
| 333 | exec_url.reset(::CFURLCreateFromFileSystemRepresentation (NULL, |
| 334 | (const UInt8 *)exec_cf_path, |
| 335 | strlen(exec_cf_path), |
| 336 | FALSE)); |
| 337 | } |
| 338 | |
| 339 | CFCReleaser<CFURLRef> dsym_url (::DBGCopyFullDSYMURLForUUID(module_uuid_ref.get(), exec_url.get())); |
| 340 | char path[PATH_MAX]; |
| 341 | |
| 342 | if (dsym_url.get()) |
| 343 | { |
| 344 | if (out_dsym_fspec) |
| 345 | { |
| 346 | if (::CFURLGetFileSystemRepresentation (dsym_url.get(), true, (UInt8*)path, sizeof(path)-1)) |
| 347 | { |
Jason Molenda | 1b09dfe | 2012-10-05 04:57:34 +0000 | [diff] [blame] | 348 | out_dsym_fspec->SetFile(path, path[0] == '~'); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 349 | |
| 350 | if (out_dsym_fspec->GetFileType () == FileSpec::eFileTypeDirectory) |
| 351 | { |
Greg Clayton | 95b765e | 2012-09-12 02:03:59 +0000 | [diff] [blame] | 352 | *out_dsym_fspec = Symbols::FindSymbolFileInBundle (*out_dsym_fspec, uuid, arch); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 353 | if (*out_dsym_fspec) |
| 354 | ++items_found; |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | ++items_found; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
Greg Clayton | 964deba | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 363 | CFCReleaser<CFDictionaryRef> dict(::DBGCopyDSYMPropertyLists (dsym_url.get())); |
| 364 | CFDictionaryRef uuid_dict = NULL; |
| 365 | if (dict.get()) |
| 366 | { |
| 367 | char uuid_cstr_buf[64]; |
| 368 | const char *uuid_cstr = uuid->GetAsCString (uuid_cstr_buf, sizeof(uuid_cstr_buf)); |
| 369 | CFCString uuid_cfstr (uuid_cstr); |
Jason Molenda | 63e5cf6 | 2012-10-02 22:23:42 +0000 | [diff] [blame] | 370 | uuid_dict = static_cast<CFDictionaryRef>(::CFDictionaryGetValue (dict.get(), uuid_cfstr.get())); |
Greg Clayton | 964deba | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 371 | if (uuid_dict) |
| 372 | { |
| 373 | |
| 374 | CFStringRef actual_src_cfpath = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGSourcePath"))); |
| 375 | if (actual_src_cfpath) |
| 376 | { |
| 377 | CFStringRef build_src_cfpath = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGBuildSourcePath"))); |
| 378 | if (build_src_cfpath) |
| 379 | { |
| 380 | char actual_src_path[PATH_MAX]; |
| 381 | char build_src_path[PATH_MAX]; |
| 382 | ::CFStringGetFileSystemRepresentation (actual_src_cfpath, actual_src_path, sizeof(actual_src_path)); |
| 383 | ::CFStringGetFileSystemRepresentation (build_src_cfpath, build_src_path, sizeof(build_src_path)); |
Greg Clayton | 4d8c543 | 2012-07-13 01:20:25 +0000 | [diff] [blame] | 384 | if (actual_src_path[0] == '~') |
| 385 | { |
| 386 | FileSpec resolved_source_path(actual_src_path, true); |
| 387 | resolved_source_path.GetPath(actual_src_path, sizeof(actual_src_path)); |
| 388 | } |
Greg Clayton | 964deba | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 389 | module_spec.GetSourceMappingList().Append (ConstString(build_src_path), ConstString(actual_src_path), true); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 395 | if (out_exec_fspec) |
| 396 | { |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 397 | bool success = false; |
Greg Clayton | 964deba | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 398 | if (uuid_dict) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 399 | { |
Greg Clayton | 964deba | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 400 | CFStringRef exec_cf_path = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGSymbolRichExecutable"))); |
| 401 | if (exec_cf_path && ::CFStringGetFileSystemRepresentation (exec_cf_path, path, sizeof(path))) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 402 | { |
Greg Clayton | 964deba | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 403 | ++items_found; |
| 404 | out_exec_fspec->SetFile(path, path[0] == '~'); |
| 405 | if (out_exec_fspec->Exists()) |
| 406 | success = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 409 | |
| 410 | if (!success) |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 411 | { |
| 412 | // No dictionary, check near the dSYM bundle for an executable that matches... |
| 413 | if (::CFURLGetFileSystemRepresentation (dsym_url.get(), true, (UInt8*)path, sizeof(path)-1)) |
| 414 | { |
| 415 | char *dsym_extension_pos = ::strstr (path, ".dSYM"); |
| 416 | if (dsym_extension_pos) |
| 417 | { |
| 418 | *dsym_extension_pos = '\0'; |
| 419 | FileSpec file_spec (path, true); |
| 420 | switch (file_spec.GetFileType()) |
| 421 | { |
| 422 | case FileSpec::eFileTypeDirectory: // Bundle directory? |
| 423 | { |
| 424 | CFCBundle bundle (path); |
| 425 | CFCReleaser<CFURLRef> bundle_exe_url (bundle.CopyExecutableURL ()); |
| 426 | if (bundle_exe_url.get()) |
| 427 | { |
| 428 | if (::CFURLGetFileSystemRepresentation (bundle_exe_url.get(), true, (UInt8*)path, sizeof(path)-1)) |
| 429 | { |
| 430 | FileSpec bundle_exe_file_spec (path, true); |
| 431 | |
| 432 | if (FileAtPathContainsArchAndUUID (bundle_exe_file_spec, arch, uuid)) |
| 433 | { |
| 434 | ++items_found; |
| 435 | *out_exec_fspec = bundle_exe_file_spec; |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | break; |
| 441 | |
| 442 | case FileSpec::eFileTypePipe: // Forget pipes |
| 443 | case FileSpec::eFileTypeSocket: // We can't process socket files |
| 444 | case FileSpec::eFileTypeInvalid: // File doesn't exist... |
| 445 | break; |
| 446 | |
| 447 | case FileSpec::eFileTypeUnknown: |
| 448 | case FileSpec::eFileTypeRegular: |
| 449 | case FileSpec::eFileTypeSymbolicLink: |
| 450 | case FileSpec::eFileTypeOther: |
| 451 | if (FileAtPathContainsArchAndUUID (file_spec, arch, uuid)) |
| 452 | { |
| 453 | ++items_found; |
| 454 | *out_exec_fspec = file_spec; |
| 455 | } |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | } |
Greg Clayton | 3e4238d | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 466 | #endif // #if !defined (__arm__) |
| 467 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 468 | return items_found; |
| 469 | } |
| 470 | |
| 471 | static bool |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 472 | LocateDSYMInVincinityOfExecutable (const ModuleSpec &module_spec, FileSpec &dsym_fspec) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 473 | { |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 474 | const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 475 | if (exec_fspec) |
| 476 | { |
| 477 | char path[PATH_MAX]; |
| 478 | if (exec_fspec->GetPath(path, sizeof(path))) |
| 479 | { |
| 480 | // Make sure the module isn't already just a dSYM file... |
| 481 | if (strcasestr(path, ".dSYM/Contents/Resources/DWARF") == NULL) |
| 482 | { |
| 483 | size_t obj_file_path_length = strlen(path); |
Filipe Cabecinhas | 78c180a | 2012-05-06 17:56:42 +0000 | [diff] [blame] | 484 | strlcat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path)); |
| 485 | strlcat(path, exec_fspec->GetFilename().AsCString(), sizeof(path)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 486 | |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 487 | dsym_fspec.SetFile(path, false); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 488 | |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 489 | 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] | 490 | { |
| 491 | return true; |
| 492 | } |
| 493 | else |
| 494 | { |
| 495 | path[obj_file_path_length] = '\0'; |
| 496 | |
| 497 | char *last_dot = strrchr(path, '.'); |
| 498 | while (last_dot != NULL && last_dot[0]) |
| 499 | { |
| 500 | char *next_slash = strchr(last_dot, '/'); |
| 501 | if (next_slash != NULL) |
| 502 | { |
| 503 | *next_slash = '\0'; |
Filipe Cabecinhas | 78c180a | 2012-05-06 17:56:42 +0000 | [diff] [blame] | 504 | strlcat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path)); |
| 505 | strlcat(path, exec_fspec->GetFilename().AsCString(), sizeof(path)); |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 506 | dsym_fspec.SetFile(path, false); |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 507 | 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] | 508 | return true; |
| 509 | else |
| 510 | { |
| 511 | *last_dot = '\0'; |
| 512 | char *prev_slash = strrchr(path, '/'); |
| 513 | if (prev_slash != NULL) |
| 514 | *prev_slash = '\0'; |
| 515 | else |
| 516 | break; |
| 517 | } |
| 518 | } |
| 519 | else |
| 520 | { |
| 521 | break; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | dsym_fspec.Clear(); |
| 529 | return false; |
| 530 | } |
| 531 | |
| 532 | FileSpec |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 533 | Symbols::LocateExecutableObjectFile (const ModuleSpec &module_spec) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 534 | { |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 535 | const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); |
| 536 | const ArchSpec *arch = module_spec.GetArchitecturePtr(); |
| 537 | const UUID *uuid = module_spec.GetUUIDPtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 538 | Timer scoped_timer (__PRETTY_FUNCTION__, |
| 539 | "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)", |
| 540 | exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>", |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 541 | arch ? arch->GetArchitectureName() : "<NULL>", |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 542 | uuid); |
| 543 | |
| 544 | FileSpec objfile_fspec; |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 545 | if (exec_fspec && FileAtPathContainsArchAndUUID (exec_fspec, arch, uuid)) |
| 546 | objfile_fspec = exec_fspec; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 547 | else |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 548 | LocateMacOSXFilesUsingDebugSymbols (module_spec, &objfile_fspec, NULL); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 549 | return objfile_fspec; |
| 550 | } |
| 551 | |
| 552 | FileSpec |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 553 | Symbols::LocateExecutableSymbolFile (const ModuleSpec &module_spec) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 554 | { |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 555 | const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); |
| 556 | const ArchSpec *arch = module_spec.GetArchitecturePtr(); |
| 557 | const UUID *uuid = module_spec.GetUUIDPtr(); |
| 558 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 559 | Timer scoped_timer (__PRETTY_FUNCTION__, |
| 560 | "LocateExecutableSymbolFile (file = %s, arch = %s, uuid = %p)", |
| 561 | exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>", |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 562 | arch ? arch->GetArchitectureName() : "<NULL>", |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 563 | uuid); |
| 564 | |
| 565 | FileSpec symbol_fspec; |
| 566 | // First try and find the dSYM in the same directory as the executable or in |
| 567 | // an appropriate parent directory |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 568 | if (LocateDSYMInVincinityOfExecutable (module_spec, symbol_fspec) == false) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 569 | { |
| 570 | // We failed to easily find the dSYM above, so use DebugSymbols |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 571 | LocateMacOSXFilesUsingDebugSymbols (module_spec, NULL, &symbol_fspec); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 572 | } |
| 573 | return symbol_fspec; |
| 574 | } |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 575 | |
| 576 | |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 577 | static bool |
| 578 | GetModuleSpecInfoFromUUIDDictionary (CFDictionaryRef uuid_dict, ModuleSpec &module_spec) |
| 579 | { |
| 580 | bool success = false; |
| 581 | if (uuid_dict != NULL && CFGetTypeID (uuid_dict) == CFDictionaryGetTypeID ()) |
| 582 | { |
| 583 | std::string str; |
| 584 | CFStringRef cf_str; |
| 585 | |
| 586 | cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGSymbolRichExecutable")); |
| 587 | if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ()) |
| 588 | { |
| 589 | if (CFCString::FileSystemRepresentation(cf_str, str)) |
| 590 | module_spec.GetFileSpec().SetFile (str.c_str(), true); |
| 591 | } |
| 592 | |
| 593 | cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGDSYMPath")); |
| 594 | if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ()) |
| 595 | { |
| 596 | if (CFCString::FileSystemRepresentation(cf_str, str)) |
| 597 | { |
| 598 | module_spec.GetSymbolFileSpec().SetFile (str.c_str(), true); |
| 599 | success = true; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGArchitecture")); |
| 604 | if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ()) |
| 605 | { |
| 606 | if (CFCString::FileSystemRepresentation(cf_str, str)) |
| 607 | module_spec.GetArchitecture().SetTriple(str.c_str()); |
| 608 | } |
| 609 | |
| 610 | std::string DBGBuildSourcePath; |
| 611 | std::string DBGSourcePath; |
| 612 | |
| 613 | cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGBuildSourcePath")); |
| 614 | if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ()) |
| 615 | { |
| 616 | CFCString::FileSystemRepresentation(cf_str, DBGBuildSourcePath); |
| 617 | } |
| 618 | |
| 619 | cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGSourcePath")); |
| 620 | if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ()) |
| 621 | { |
| 622 | CFCString::FileSystemRepresentation(cf_str, DBGSourcePath); |
| 623 | } |
| 624 | |
| 625 | if (!DBGBuildSourcePath.empty() && !DBGSourcePath.empty()) |
| 626 | { |
| 627 | module_spec.GetSourceMappingList().Append (ConstString(DBGBuildSourcePath.c_str()), ConstString(DBGSourcePath.c_str()), true); |
| 628 | } |
| 629 | } |
| 630 | return success; |
| 631 | } |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 632 | |
| 633 | |
| 634 | bool |
Jason Molenda | d6d45ce | 2012-10-09 01:17:11 +0000 | [diff] [blame] | 635 | Symbols::DownloadObjectAndSymbolFile (ModuleSpec &module_spec, bool force_lookup) |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 636 | { |
| 637 | bool success = false; |
| 638 | const UUID *uuid_ptr = module_spec.GetUUIDPtr(); |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 639 | const FileSpec *file_spec_ptr = module_spec.GetFileSpecPtr(); |
Jason Molenda | d6d45ce | 2012-10-09 01:17:11 +0000 | [diff] [blame] | 640 | |
| 641 | // It's expensive to check for the DBGShellCommands defaults setting, only do it once per |
| 642 | // lldb run and cache the result. |
| 643 | static bool g_have_checked_for_dbgshell_command = false; |
| 644 | static const char *g_dbgshell_command = NULL; |
| 645 | if (g_have_checked_for_dbgshell_command == false) |
| 646 | { |
| 647 | g_have_checked_for_dbgshell_command = true; |
| 648 | CFTypeRef defaults_setting = CFPreferencesCopyAppValue (CFSTR ("DBGShellCommands"), CFSTR ("com.apple.DebugSymbols")); |
| 649 | if (defaults_setting && CFGetTypeID (defaults_setting) == CFStringGetTypeID()) |
| 650 | { |
| 651 | char cstr_buf[PATH_MAX]; |
| 652 | if (CFStringGetCString ((CFStringRef) defaults_setting, cstr_buf, sizeof (cstr_buf), kCFStringEncodingUTF8)) |
| 653 | { |
| 654 | g_dbgshell_command = strdup (cstr_buf); // this malloc'ed memory will never be freed |
| 655 | } |
| 656 | } |
| 657 | if (defaults_setting) |
| 658 | { |
| 659 | CFRelease (defaults_setting); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | // When g_dbgshell_command is NULL, the user has not enabled the use of an external program |
| 664 | // to find the symbols, don't run it for them. |
| 665 | if (force_lookup == false && g_dbgshell_command == NULL) |
| 666 | { |
| 667 | return false; |
| 668 | } |
| 669 | |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 670 | if (uuid_ptr || (file_spec_ptr && file_spec_ptr->Exists())) |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 671 | { |
| 672 | static bool g_located_dsym_for_uuid_exe = false; |
| 673 | static bool g_dsym_for_uuid_exe_exists = false; |
| 674 | static char g_dsym_for_uuid_exe_path[PATH_MAX]; |
| 675 | if (!g_located_dsym_for_uuid_exe) |
| 676 | { |
| 677 | g_located_dsym_for_uuid_exe = true; |
| 678 | const char *dsym_for_uuid_exe_path_cstr = getenv("LLDB_APPLE_DSYMFORUUID_EXECUTABLE"); |
| 679 | FileSpec dsym_for_uuid_exe_spec; |
| 680 | if (dsym_for_uuid_exe_path_cstr) |
| 681 | { |
| 682 | dsym_for_uuid_exe_spec.SetFile(dsym_for_uuid_exe_path_cstr, true); |
| 683 | g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists(); |
| 684 | } |
| 685 | |
| 686 | if (!g_dsym_for_uuid_exe_exists) |
| 687 | { |
Jason Molenda | f256975 | 2012-10-30 21:26:30 +0000 | [diff] [blame] | 688 | dsym_for_uuid_exe_spec.SetFile("/usr/local/bin/dsymForUUID", false); |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 689 | g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists(); |
| 690 | if (!g_dsym_for_uuid_exe_exists) |
| 691 | { |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 692 | long bufsize; |
Jason Molenda | f256975 | 2012-10-30 21:26:30 +0000 | [diff] [blame] | 693 | if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) != -1) |
| 694 | { |
| 695 | char buffer[bufsize]; |
| 696 | struct passwd pwd; |
| 697 | struct passwd *tilde_rc = NULL; |
| 698 | // we are a library so we need to use the reentrant version of getpwnam() |
| 699 | if (getpwnam_r ("rc", &pwd, buffer, bufsize, &tilde_rc) == 0 |
| 700 | && tilde_rc |
| 701 | && tilde_rc->pw_dir) |
| 702 | { |
| 703 | std::string dsymforuuid_path(tilde_rc->pw_dir); |
| 704 | dsymforuuid_path += "/bin/dsymForUUID"; |
| 705 | dsym_for_uuid_exe_spec.SetFile(dsymforuuid_path.c_str(), false); |
| 706 | g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists(); |
| 707 | } |
| 708 | } |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 709 | } |
| 710 | } |
Jason Molenda | d6d45ce | 2012-10-09 01:17:11 +0000 | [diff] [blame] | 711 | if (!g_dsym_for_uuid_exe_exists && g_dbgshell_command != NULL) |
| 712 | { |
| 713 | dsym_for_uuid_exe_spec.SetFile(g_dbgshell_command, true); |
| 714 | g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists(); |
| 715 | } |
| 716 | |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 717 | if (g_dsym_for_uuid_exe_exists) |
| 718 | dsym_for_uuid_exe_spec.GetPath (g_dsym_for_uuid_exe_path, sizeof(g_dsym_for_uuid_exe_path)); |
| 719 | } |
| 720 | if (g_dsym_for_uuid_exe_exists) |
| 721 | { |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 722 | char uuid_cstr_buffer[64]; |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 723 | char file_path[PATH_MAX]; |
| 724 | uuid_cstr_buffer[0] = '\0'; |
| 725 | file_path[0] = '\0'; |
| 726 | const char *uuid_cstr = NULL; |
| 727 | |
| 728 | if (uuid_ptr) |
| 729 | uuid_cstr = uuid_ptr->GetAsCString(uuid_cstr_buffer, sizeof(uuid_cstr_buffer)); |
| 730 | |
| 731 | if (file_spec_ptr) |
| 732 | file_spec_ptr->GetPath(file_path, sizeof(file_path)); |
| 733 | |
| 734 | StreamString command; |
| 735 | if (uuid_cstr) |
Jason Molenda | 63e5cf6 | 2012-10-02 22:23:42 +0000 | [diff] [blame] | 736 | command.Printf("%s --ignoreNegativeCache --copyExecutable %s", g_dsym_for_uuid_exe_path, uuid_cstr); |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 737 | else if (file_path && file_path[0]) |
Jason Molenda | 63e5cf6 | 2012-10-02 22:23:42 +0000 | [diff] [blame] | 738 | command.Printf("%s --ignoreNegativeCache --copyExecutable %s", g_dsym_for_uuid_exe_path, file_path); |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 739 | |
| 740 | if (!command.GetString().empty()) |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 741 | { |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 742 | int exit_status = -1; |
| 743 | int signo = -1; |
| 744 | std::string command_output; |
| 745 | Error error = Host::RunShellCommand (command.GetData(), |
| 746 | NULL, // current working directory |
| 747 | &exit_status, // Exit status |
| 748 | &signo, // Signal int * |
| 749 | &command_output, // Command output |
| 750 | 30, // Large timeout to allow for long dsym download times |
| 751 | NULL); // Don't run in a shell (we don't need shell expansion) |
| 752 | if (error.Success() && exit_status == 0 && !command_output.empty()) |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 753 | { |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 754 | CFCData data (CFDataCreateWithBytesNoCopy (NULL, |
| 755 | (const UInt8 *)command_output.data(), |
| 756 | command_output.size(), |
| 757 | kCFAllocatorNull)); |
| 758 | |
| 759 | CFCReleaser<CFDictionaryRef> plist((CFDictionaryRef)::CFPropertyListCreateFromXMLData (NULL, data.get(), kCFPropertyListImmutable, NULL)); |
| 760 | |
Sean Callanan | 50972ae | 2013-02-08 23:17:17 +0000 | [diff] [blame] | 761 | if (plist.get() && CFGetTypeID (plist.get()) == CFDictionaryGetTypeID ()) |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 762 | { |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 763 | if (uuid_cstr) |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 764 | { |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 765 | CFCString uuid_cfstr(uuid_cstr); |
| 766 | CFDictionaryRef uuid_dict = (CFDictionaryRef)CFDictionaryGetValue (plist.get(), uuid_cfstr.get()); |
| 767 | success = GetModuleSpecInfoFromUUIDDictionary (uuid_dict, module_spec); |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 768 | } |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 769 | else |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 770 | { |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 771 | const CFIndex num_values = ::CFDictionaryGetCount(plist.get()); |
| 772 | if (num_values > 0) |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 773 | { |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 774 | std::vector<CFStringRef> keys (num_values, NULL); |
| 775 | std::vector<CFDictionaryRef> values (num_values, NULL); |
| 776 | ::CFDictionaryGetKeysAndValues(plist.get(), NULL, (const void **)&values[0]); |
| 777 | if (num_values == 1) |
| 778 | { |
| 779 | return GetModuleSpecInfoFromUUIDDictionary (values[0], module_spec); |
| 780 | } |
| 781 | else |
| 782 | { |
| 783 | for (CFIndex i=0; i<num_values; ++i) |
| 784 | { |
| 785 | ModuleSpec curr_module_spec; |
| 786 | if (GetModuleSpecInfoFromUUIDDictionary (values[i], curr_module_spec)) |
| 787 | { |
Sean Callanan | 40e278c | 2012-12-13 22:07:14 +0000 | [diff] [blame] | 788 | if (module_spec.GetArchitecture().IsCompatibleMatch(curr_module_spec.GetArchitecture())) |
Greg Clayton | 437b5bc | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 789 | { |
| 790 | module_spec = curr_module_spec; |
| 791 | return true; |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | } |
Greg Clayton | b924eb6 | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | return success; |
| 804 | } |
| 805 | |