Chris Lattner | 30fdc8d | 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 | 055c57b | 2012-10-30 21:26:30 +0000 | [diff] [blame] | 14 | #include <pwd.h> |
Greg Clayton | e1a916a | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 15 | #include "llvm/Support/MachO.h" |
Chris Lattner | 30fdc8d | 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 | 30fdc8d | 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 | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 25 | #include "lldb/Core/Module.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 26 | #include "lldb/Core/ModuleSpec.h" |
Johnny Chen | 82e5a26 | 2012-08-22 00:18:43 +0000 | [diff] [blame] | 27 | #include "lldb/Core/StreamString.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | #include "lldb/Core/Timer.h" |
| 29 | #include "lldb/Core/UUID.h" |
Greg Clayton | 7fb56d0 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 30 | #include "lldb/Host/Endian.h" |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 31 | #include "lldb/Host/Host.h" |
Greg Clayton | 293d593 | 2011-02-01 05:15:02 +0000 | [diff] [blame] | 32 | #include "lldb/Utility/CleanUp.h" |
Daniel Dunbar | 5ae8732 | 2011-10-31 22:50:53 +0000 | [diff] [blame] | 33 | #include "Host/macosx/cfcpp/CFCBundle.h" |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 34 | #include "Host/macosx/cfcpp/CFCData.h" |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 35 | #include "Host/macosx/cfcpp/CFCReleaser.h" |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 36 | #include "Host/macosx/cfcpp/CFCString.h" |
Chris Lattner | 311adf3 | 2010-09-08 23:01:14 +0000 | [diff] [blame] | 37 | #include "mach/machine.h" |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 38 | |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | using namespace lldb; |
| 41 | using namespace lldb_private; |
Greg Clayton | e1a916a | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 42 | using namespace llvm::MachO; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | |
Greg Clayton | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 44 | #if !defined (__arm__) // No DebugSymbols on the iOS devices |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 45 | extern "C" { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 30fdc8d | 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 | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 49 | |
| 50 | } |
Greg Clayton | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 51 | #endif |
Chris Lattner | 30fdc8d | 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 | 6083026 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 58 | const lldb_private::UUID *uuid, // the UUID we are looking for |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | off_t file_offset, |
| 60 | DataExtractor& data, |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 61 | lldb::offset_t data_offset, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | const uint32_t magic |
| 63 | ) |
| 64 | { |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame^] | 65 | assert(magic == MH_MAGIC || magic == MH_CIGAM || magic == MH_MAGIC_64 || magic == MH_CIGAM_64); |
| 66 | if (magic == MH_MAGIC || magic == MH_MAGIC_64) |
Greg Clayton | 7fb56d0 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 67 | data.SetByteOrder (lldb::endian::InlHostByteOrder()); |
| 68 | else if (lldb::endian::InlHostByteOrder() == eByteOrderBig) |
Chris Lattner | 30fdc8d | 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 | 41f9232 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 84 | ArchSpec file_arch(eArchTypeMachO, cputype, cpusubtype); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 85 | |
Sean Callanan | 7038627 | 2012-12-14 23:43:03 +0000 | [diff] [blame] | 86 | if (!file_arch.IsCompatibleMatch(*arch)) |
Chris Lattner | 30fdc8d | 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 | |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame^] | 96 | if (magic == MH_CIGAM_64 || magic == MH_MAGIC_64) |
Chris Lattner | 30fdc8d | 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 |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame^] | 100 | if (magic == MH_CIGAM_64 || magic == MH_MAGIC_64) |
Chris Lattner | 30fdc8d | 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 | c7bece56 | 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 | 30fdc8d | 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); |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame^] | 122 | if (cmd == LC_UUID) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 123 | { |
Greg Clayton | 6083026 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 124 | lldb_private::UUID file_uuid (data.GetData(&data_offset, 16), 16); |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 125 | if (file_uuid == *uuid) |
| 126 | return true; |
Johnny Chen | fdc80a5c | 2012-02-01 01:49:50 +0000 | [diff] [blame] | 127 | return false; |
Chris Lattner | 30fdc8d | 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 | 6083026 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 139 | const lldb_private::UUID *uuid, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 140 | off_t file_offset, |
| 141 | DataExtractor& data, |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 142 | lldb::offset_t data_offset, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | const uint32_t magic |
| 144 | ) |
| 145 | { |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame^] | 146 | assert(magic == FAT_MAGIC || magic == FAT_CIGAM); |
Chris Lattner | 30fdc8d | 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 | 41f9232 | 2010-06-11 03:25:34 +0000 | [diff] [blame] | 171 | ArchSpec fat_arch(eArchTypeMachO, arch_cputype, arch_cpusubtype); |
Sean Callanan | bab7cc4 | 2012-12-21 21:32:25 +0000 | [diff] [blame] | 172 | if (!fat_arch.IsExactMatch(*arch)) |
Chris Lattner | 30fdc8d | 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 | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 180 | lldb::offset_t arch_data_offset = 0; |
Chris Lattner | 30fdc8d | 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 | { |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame^] | 185 | case MH_MAGIC: |
| 186 | case MH_CIGAM: |
| 187 | case MH_MAGIC_64: |
| 188 | case MH_CIGAM_64: |
Chris Lattner | 30fdc8d | 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 | 6083026 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 202 | const lldb_private::UUID *uuid |
Chris Lattner | 30fdc8d | 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 | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 213 | lldb::offset_t data_offset = 0; |
Chris Lattner | 30fdc8d | 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 |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame^] | 219 | case MH_MAGIC: |
| 220 | case MH_CIGAM: |
| 221 | case MH_MAGIC_64: |
| 222 | case MH_CIGAM_64: |
Chris Lattner | 30fdc8d | 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 |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame^] | 226 | case FAT_MAGIC: |
| 227 | case FAT_CIGAM: |
Chris Lattner | 30fdc8d | 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 | 103f028 | 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 | 30fdc8d | 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 | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 250 | lldb_utility::CleanUp <DIR *, int> dirp (opendir(path), NULL, closedir); |
Greg Clayton | 293d593 | 2011-02-01 05:15:02 +0000 | [diff] [blame] | 251 | if (dirp.is_valid()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | { |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 253 | dsym_fspec.GetDirectory().SetCString(path); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 254 | struct dirent* dp; |
Greg Clayton | 293d593 | 2011-02-01 05:15:02 +0000 | [diff] [blame] | 255 | while ((dp = readdir(dirp.get())) != NULL) |
Chris Lattner | 30fdc8d | 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 | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 269 | dsym_fspec.GetFilename().SetCString(dp->d_name); |
Chris Lattner | 30fdc8d | 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 | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 283 | const ModuleSpec &module_spec, |
Chris Lattner | 30fdc8d | 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 | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 296 | #if !defined (__arm__) // No DebugSymbols on the iOS devices |
| 297 | |
Greg Clayton | b9a01b3 | 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 | 30fdc8d | 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 | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 307 | CFCReleaser<CFUUIDRef> module_uuid_ref(::CFUUIDCreateWithBytes (NULL, |
Chris Lattner | 30fdc8d | 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 | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 328 | const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); |
Chris Lattner | 30fdc8d | 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 | a8805d3 | 2012-10-05 04:57:34 +0000 | [diff] [blame] | 348 | out_dsym_fspec->SetFile(path, path[0] == '~'); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 349 | |
| 350 | if (out_dsym_fspec->GetFileType () == FileSpec::eFileTypeDirectory) |
| 351 | { |
Greg Clayton | 103f028 | 2012-09-12 02:03:59 +0000 | [diff] [blame] | 352 | *out_dsym_fspec = Symbols::FindSymbolFileInBundle (*out_dsym_fspec, uuid, arch); |
Chris Lattner | 30fdc8d | 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 | d804d28 | 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 | { |
Jason Molenda | c16b4af | 2013-05-03 23:56:12 +0000 | [diff] [blame] | 367 | CFCString uuid_cfstr (uuid->GetAsString().c_str()); |
Jason Molenda | 743e4396 | 2012-10-02 22:23:42 +0000 | [diff] [blame] | 368 | uuid_dict = static_cast<CFDictionaryRef>(::CFDictionaryGetValue (dict.get(), uuid_cfstr.get())); |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 369 | if (uuid_dict) |
| 370 | { |
| 371 | |
| 372 | CFStringRef actual_src_cfpath = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGSourcePath"))); |
| 373 | if (actual_src_cfpath) |
| 374 | { |
| 375 | CFStringRef build_src_cfpath = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGBuildSourcePath"))); |
| 376 | if (build_src_cfpath) |
| 377 | { |
| 378 | char actual_src_path[PATH_MAX]; |
| 379 | char build_src_path[PATH_MAX]; |
| 380 | ::CFStringGetFileSystemRepresentation (actual_src_cfpath, actual_src_path, sizeof(actual_src_path)); |
| 381 | ::CFStringGetFileSystemRepresentation (build_src_cfpath, build_src_path, sizeof(build_src_path)); |
Greg Clayton | 96816e4 | 2012-07-13 01:20:25 +0000 | [diff] [blame] | 382 | if (actual_src_path[0] == '~') |
| 383 | { |
| 384 | FileSpec resolved_source_path(actual_src_path, true); |
| 385 | resolved_source_path.GetPath(actual_src_path, sizeof(actual_src_path)); |
| 386 | } |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 387 | module_spec.GetSourceMappingList().Append (ConstString(build_src_path), ConstString(actual_src_path), true); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 393 | if (out_exec_fspec) |
| 394 | { |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 395 | bool success = false; |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 396 | if (uuid_dict) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 397 | { |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 398 | CFStringRef exec_cf_path = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGSymbolRichExecutable"))); |
| 399 | if (exec_cf_path && ::CFStringGetFileSystemRepresentation (exec_cf_path, path, sizeof(path))) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 400 | { |
Greg Clayton | d804d28 | 2012-03-15 21:01:31 +0000 | [diff] [blame] | 401 | ++items_found; |
| 402 | out_exec_fspec->SetFile(path, path[0] == '~'); |
| 403 | if (out_exec_fspec->Exists()) |
| 404 | success = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 405 | } |
| 406 | } |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 407 | |
| 408 | if (!success) |
Greg Clayton | a63d08c | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 409 | { |
| 410 | // No dictionary, check near the dSYM bundle for an executable that matches... |
| 411 | if (::CFURLGetFileSystemRepresentation (dsym_url.get(), true, (UInt8*)path, sizeof(path)-1)) |
| 412 | { |
| 413 | char *dsym_extension_pos = ::strstr (path, ".dSYM"); |
| 414 | if (dsym_extension_pos) |
| 415 | { |
| 416 | *dsym_extension_pos = '\0'; |
| 417 | FileSpec file_spec (path, true); |
| 418 | switch (file_spec.GetFileType()) |
| 419 | { |
| 420 | case FileSpec::eFileTypeDirectory: // Bundle directory? |
| 421 | { |
| 422 | CFCBundle bundle (path); |
| 423 | CFCReleaser<CFURLRef> bundle_exe_url (bundle.CopyExecutableURL ()); |
| 424 | if (bundle_exe_url.get()) |
| 425 | { |
| 426 | if (::CFURLGetFileSystemRepresentation (bundle_exe_url.get(), true, (UInt8*)path, sizeof(path)-1)) |
| 427 | { |
| 428 | FileSpec bundle_exe_file_spec (path, true); |
| 429 | |
| 430 | if (FileAtPathContainsArchAndUUID (bundle_exe_file_spec, arch, uuid)) |
| 431 | { |
| 432 | ++items_found; |
| 433 | *out_exec_fspec = bundle_exe_file_spec; |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | break; |
| 439 | |
| 440 | case FileSpec::eFileTypePipe: // Forget pipes |
| 441 | case FileSpec::eFileTypeSocket: // We can't process socket files |
| 442 | case FileSpec::eFileTypeInvalid: // File doesn't exist... |
| 443 | break; |
| 444 | |
| 445 | case FileSpec::eFileTypeUnknown: |
| 446 | case FileSpec::eFileTypeRegular: |
| 447 | case FileSpec::eFileTypeSymbolicLink: |
| 448 | case FileSpec::eFileTypeOther: |
| 449 | if (FileAtPathContainsArchAndUUID (file_spec, arch, uuid)) |
| 450 | { |
| 451 | ++items_found; |
| 452 | *out_exec_fspec = file_spec; |
| 453 | } |
| 454 | break; |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | } |
Greg Clayton | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 464 | #endif // #if !defined (__arm__) |
| 465 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 466 | return items_found; |
| 467 | } |
| 468 | |
| 469 | static bool |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 470 | LocateDSYMInVincinityOfExecutable (const ModuleSpec &module_spec, FileSpec &dsym_fspec) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 471 | { |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 472 | const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 473 | if (exec_fspec) |
| 474 | { |
| 475 | char path[PATH_MAX]; |
| 476 | if (exec_fspec->GetPath(path, sizeof(path))) |
| 477 | { |
| 478 | // Make sure the module isn't already just a dSYM file... |
| 479 | if (strcasestr(path, ".dSYM/Contents/Resources/DWARF") == NULL) |
| 480 | { |
| 481 | size_t obj_file_path_length = strlen(path); |
Filipe Cabecinhas | 75bc95e | 2012-05-06 17:56:42 +0000 | [diff] [blame] | 482 | strlcat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path)); |
| 483 | strlcat(path, exec_fspec->GetFilename().AsCString(), sizeof(path)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 484 | |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 485 | dsym_fspec.SetFile(path, false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 486 | |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 487 | if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr())) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 488 | { |
| 489 | return true; |
| 490 | } |
| 491 | else |
| 492 | { |
| 493 | path[obj_file_path_length] = '\0'; |
| 494 | |
| 495 | char *last_dot = strrchr(path, '.'); |
| 496 | while (last_dot != NULL && last_dot[0]) |
| 497 | { |
| 498 | char *next_slash = strchr(last_dot, '/'); |
| 499 | if (next_slash != NULL) |
| 500 | { |
| 501 | *next_slash = '\0'; |
Filipe Cabecinhas | 75bc95e | 2012-05-06 17:56:42 +0000 | [diff] [blame] | 502 | strlcat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path)); |
| 503 | strlcat(path, exec_fspec->GetFilename().AsCString(), sizeof(path)); |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 504 | dsym_fspec.SetFile(path, false); |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 505 | if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr())) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 506 | return true; |
| 507 | else |
| 508 | { |
| 509 | *last_dot = '\0'; |
| 510 | char *prev_slash = strrchr(path, '/'); |
| 511 | if (prev_slash != NULL) |
| 512 | *prev_slash = '\0'; |
| 513 | else |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | else |
| 518 | { |
| 519 | break; |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | dsym_fspec.Clear(); |
| 527 | return false; |
| 528 | } |
| 529 | |
| 530 | FileSpec |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 531 | Symbols::LocateExecutableObjectFile (const ModuleSpec &module_spec) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 532 | { |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 533 | const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); |
| 534 | const ArchSpec *arch = module_spec.GetArchitecturePtr(); |
| 535 | const UUID *uuid = module_spec.GetUUIDPtr(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 536 | Timer scoped_timer (__PRETTY_FUNCTION__, |
| 537 | "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)", |
| 538 | exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>", |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 539 | arch ? arch->GetArchitectureName() : "<NULL>", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 540 | uuid); |
| 541 | |
| 542 | FileSpec objfile_fspec; |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 543 | if (exec_fspec && FileAtPathContainsArchAndUUID (exec_fspec, arch, uuid)) |
| 544 | objfile_fspec = exec_fspec; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 545 | else |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 546 | LocateMacOSXFilesUsingDebugSymbols (module_spec, &objfile_fspec, NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 547 | return objfile_fspec; |
| 548 | } |
| 549 | |
| 550 | FileSpec |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 551 | Symbols::LocateExecutableSymbolFile (const ModuleSpec &module_spec) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 552 | { |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 553 | const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); |
| 554 | const ArchSpec *arch = module_spec.GetArchitecturePtr(); |
| 555 | const UUID *uuid = module_spec.GetUUIDPtr(); |
| 556 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 557 | Timer scoped_timer (__PRETTY_FUNCTION__, |
| 558 | "LocateExecutableSymbolFile (file = %s, arch = %s, uuid = %p)", |
| 559 | exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>", |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 560 | arch ? arch->GetArchitectureName() : "<NULL>", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 561 | uuid); |
| 562 | |
| 563 | FileSpec symbol_fspec; |
| 564 | // First try and find the dSYM in the same directory as the executable or in |
| 565 | // an appropriate parent directory |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 566 | if (LocateDSYMInVincinityOfExecutable (module_spec, symbol_fspec) == false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 567 | { |
| 568 | // We failed to easily find the dSYM above, so use DebugSymbols |
Greg Clayton | b9a01b3 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 569 | LocateMacOSXFilesUsingDebugSymbols (module_spec, NULL, &symbol_fspec); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 570 | } |
| 571 | return symbol_fspec; |
| 572 | } |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 573 | |
| 574 | |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 575 | static bool |
| 576 | GetModuleSpecInfoFromUUIDDictionary (CFDictionaryRef uuid_dict, ModuleSpec &module_spec) |
| 577 | { |
| 578 | bool success = false; |
| 579 | if (uuid_dict != NULL && CFGetTypeID (uuid_dict) == CFDictionaryGetTypeID ()) |
| 580 | { |
| 581 | std::string str; |
| 582 | CFStringRef cf_str; |
| 583 | |
| 584 | cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGSymbolRichExecutable")); |
| 585 | if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ()) |
| 586 | { |
| 587 | if (CFCString::FileSystemRepresentation(cf_str, str)) |
| 588 | module_spec.GetFileSpec().SetFile (str.c_str(), true); |
| 589 | } |
| 590 | |
| 591 | cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGDSYMPath")); |
| 592 | if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ()) |
| 593 | { |
| 594 | if (CFCString::FileSystemRepresentation(cf_str, str)) |
| 595 | { |
| 596 | module_spec.GetSymbolFileSpec().SetFile (str.c_str(), true); |
| 597 | success = true; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGArchitecture")); |
| 602 | if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ()) |
| 603 | { |
| 604 | if (CFCString::FileSystemRepresentation(cf_str, str)) |
| 605 | module_spec.GetArchitecture().SetTriple(str.c_str()); |
| 606 | } |
| 607 | |
| 608 | std::string DBGBuildSourcePath; |
| 609 | std::string DBGSourcePath; |
| 610 | |
| 611 | cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGBuildSourcePath")); |
| 612 | if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ()) |
| 613 | { |
| 614 | CFCString::FileSystemRepresentation(cf_str, DBGBuildSourcePath); |
| 615 | } |
| 616 | |
| 617 | cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGSourcePath")); |
| 618 | if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ()) |
| 619 | { |
| 620 | CFCString::FileSystemRepresentation(cf_str, DBGSourcePath); |
| 621 | } |
| 622 | |
| 623 | if (!DBGBuildSourcePath.empty() && !DBGSourcePath.empty()) |
| 624 | { |
| 625 | module_spec.GetSourceMappingList().Append (ConstString(DBGBuildSourcePath.c_str()), ConstString(DBGSourcePath.c_str()), true); |
| 626 | } |
| 627 | } |
| 628 | return success; |
| 629 | } |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 630 | |
| 631 | |
| 632 | bool |
Jason Molenda | bb860bd | 2012-10-09 01:17:11 +0000 | [diff] [blame] | 633 | Symbols::DownloadObjectAndSymbolFile (ModuleSpec &module_spec, bool force_lookup) |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 634 | { |
| 635 | bool success = false; |
| 636 | const UUID *uuid_ptr = module_spec.GetUUIDPtr(); |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 637 | const FileSpec *file_spec_ptr = module_spec.GetFileSpecPtr(); |
Jason Molenda | bb860bd | 2012-10-09 01:17:11 +0000 | [diff] [blame] | 638 | |
| 639 | // It's expensive to check for the DBGShellCommands defaults setting, only do it once per |
| 640 | // lldb run and cache the result. |
| 641 | static bool g_have_checked_for_dbgshell_command = false; |
| 642 | static const char *g_dbgshell_command = NULL; |
| 643 | if (g_have_checked_for_dbgshell_command == false) |
| 644 | { |
| 645 | g_have_checked_for_dbgshell_command = true; |
| 646 | CFTypeRef defaults_setting = CFPreferencesCopyAppValue (CFSTR ("DBGShellCommands"), CFSTR ("com.apple.DebugSymbols")); |
| 647 | if (defaults_setting && CFGetTypeID (defaults_setting) == CFStringGetTypeID()) |
| 648 | { |
| 649 | char cstr_buf[PATH_MAX]; |
| 650 | if (CFStringGetCString ((CFStringRef) defaults_setting, cstr_buf, sizeof (cstr_buf), kCFStringEncodingUTF8)) |
| 651 | { |
| 652 | g_dbgshell_command = strdup (cstr_buf); // this malloc'ed memory will never be freed |
| 653 | } |
| 654 | } |
| 655 | if (defaults_setting) |
| 656 | { |
| 657 | CFRelease (defaults_setting); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | // When g_dbgshell_command is NULL, the user has not enabled the use of an external program |
| 662 | // to find the symbols, don't run it for them. |
| 663 | if (force_lookup == false && g_dbgshell_command == NULL) |
| 664 | { |
| 665 | return false; |
| 666 | } |
| 667 | |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 668 | if (uuid_ptr || (file_spec_ptr && file_spec_ptr->Exists())) |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 669 | { |
| 670 | static bool g_located_dsym_for_uuid_exe = false; |
| 671 | static bool g_dsym_for_uuid_exe_exists = false; |
| 672 | static char g_dsym_for_uuid_exe_path[PATH_MAX]; |
| 673 | if (!g_located_dsym_for_uuid_exe) |
| 674 | { |
| 675 | g_located_dsym_for_uuid_exe = true; |
| 676 | const char *dsym_for_uuid_exe_path_cstr = getenv("LLDB_APPLE_DSYMFORUUID_EXECUTABLE"); |
| 677 | FileSpec dsym_for_uuid_exe_spec; |
| 678 | if (dsym_for_uuid_exe_path_cstr) |
| 679 | { |
| 680 | dsym_for_uuid_exe_spec.SetFile(dsym_for_uuid_exe_path_cstr, true); |
| 681 | g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists(); |
| 682 | } |
| 683 | |
| 684 | if (!g_dsym_for_uuid_exe_exists) |
| 685 | { |
Jason Molenda | 055c57b | 2012-10-30 21:26:30 +0000 | [diff] [blame] | 686 | dsym_for_uuid_exe_spec.SetFile("/usr/local/bin/dsymForUUID", false); |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 687 | g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists(); |
| 688 | if (!g_dsym_for_uuid_exe_exists) |
| 689 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 690 | long bufsize; |
Jason Molenda | 055c57b | 2012-10-30 21:26:30 +0000 | [diff] [blame] | 691 | if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) != -1) |
| 692 | { |
| 693 | char buffer[bufsize]; |
| 694 | struct passwd pwd; |
| 695 | struct passwd *tilde_rc = NULL; |
| 696 | // we are a library so we need to use the reentrant version of getpwnam() |
| 697 | if (getpwnam_r ("rc", &pwd, buffer, bufsize, &tilde_rc) == 0 |
| 698 | && tilde_rc |
| 699 | && tilde_rc->pw_dir) |
| 700 | { |
| 701 | std::string dsymforuuid_path(tilde_rc->pw_dir); |
| 702 | dsymforuuid_path += "/bin/dsymForUUID"; |
| 703 | dsym_for_uuid_exe_spec.SetFile(dsymforuuid_path.c_str(), false); |
| 704 | g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists(); |
| 705 | } |
| 706 | } |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
Jason Molenda | bb860bd | 2012-10-09 01:17:11 +0000 | [diff] [blame] | 709 | if (!g_dsym_for_uuid_exe_exists && g_dbgshell_command != NULL) |
| 710 | { |
| 711 | dsym_for_uuid_exe_spec.SetFile(g_dbgshell_command, true); |
| 712 | g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists(); |
| 713 | } |
| 714 | |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 715 | if (g_dsym_for_uuid_exe_exists) |
| 716 | dsym_for_uuid_exe_spec.GetPath (g_dsym_for_uuid_exe_path, sizeof(g_dsym_for_uuid_exe_path)); |
| 717 | } |
| 718 | if (g_dsym_for_uuid_exe_exists) |
| 719 | { |
Jason Molenda | c16b4af | 2013-05-03 23:56:12 +0000 | [diff] [blame] | 720 | std::string uuid_str; |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 721 | char file_path[PATH_MAX]; |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 722 | file_path[0] = '\0'; |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 723 | |
| 724 | if (uuid_ptr) |
Jason Molenda | c16b4af | 2013-05-03 23:56:12 +0000 | [diff] [blame] | 725 | uuid_str = uuid_ptr->GetAsString(); |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 726 | |
| 727 | if (file_spec_ptr) |
| 728 | file_spec_ptr->GetPath(file_path, sizeof(file_path)); |
| 729 | |
| 730 | StreamString command; |
Jason Molenda | c16b4af | 2013-05-03 23:56:12 +0000 | [diff] [blame] | 731 | if (!uuid_str.empty()) |
| 732 | command.Printf("%s --ignoreNegativeCache --copyExecutable %s", g_dsym_for_uuid_exe_path, uuid_str.c_str()); |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 733 | else if (file_path && file_path[0]) |
Jason Molenda | 743e4396 | 2012-10-02 22:23:42 +0000 | [diff] [blame] | 734 | command.Printf("%s --ignoreNegativeCache --copyExecutable %s", g_dsym_for_uuid_exe_path, file_path); |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 735 | |
| 736 | if (!command.GetString().empty()) |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 737 | { |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 738 | int exit_status = -1; |
| 739 | int signo = -1; |
| 740 | std::string command_output; |
| 741 | Error error = Host::RunShellCommand (command.GetData(), |
| 742 | NULL, // current working directory |
| 743 | &exit_status, // Exit status |
| 744 | &signo, // Signal int * |
| 745 | &command_output, // Command output |
| 746 | 30, // Large timeout to allow for long dsym download times |
| 747 | NULL); // Don't run in a shell (we don't need shell expansion) |
| 748 | if (error.Success() && exit_status == 0 && !command_output.empty()) |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 749 | { |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 750 | CFCData data (CFDataCreateWithBytesNoCopy (NULL, |
| 751 | (const UInt8 *)command_output.data(), |
| 752 | command_output.size(), |
| 753 | kCFAllocatorNull)); |
| 754 | |
| 755 | CFCReleaser<CFDictionaryRef> plist((CFDictionaryRef)::CFPropertyListCreateFromXMLData (NULL, data.get(), kCFPropertyListImmutable, NULL)); |
| 756 | |
Sean Callanan | 29214ab | 2013-02-08 23:17:17 +0000 | [diff] [blame] | 757 | if (plist.get() && CFGetTypeID (plist.get()) == CFDictionaryGetTypeID ()) |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 758 | { |
Jason Molenda | c16b4af | 2013-05-03 23:56:12 +0000 | [diff] [blame] | 759 | if (!uuid_str.empty()) |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 760 | { |
Jason Molenda | c16b4af | 2013-05-03 23:56:12 +0000 | [diff] [blame] | 761 | CFCString uuid_cfstr(uuid_str.c_str()); |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 762 | CFDictionaryRef uuid_dict = (CFDictionaryRef)CFDictionaryGetValue (plist.get(), uuid_cfstr.get()); |
| 763 | success = GetModuleSpecInfoFromUUIDDictionary (uuid_dict, module_spec); |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 764 | } |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 765 | else |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 766 | { |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 767 | const CFIndex num_values = ::CFDictionaryGetCount(plist.get()); |
| 768 | if (num_values > 0) |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 769 | { |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 770 | std::vector<CFStringRef> keys (num_values, NULL); |
| 771 | std::vector<CFDictionaryRef> values (num_values, NULL); |
| 772 | ::CFDictionaryGetKeysAndValues(plist.get(), NULL, (const void **)&values[0]); |
| 773 | if (num_values == 1) |
| 774 | { |
| 775 | return GetModuleSpecInfoFromUUIDDictionary (values[0], module_spec); |
| 776 | } |
| 777 | else |
| 778 | { |
| 779 | for (CFIndex i=0; i<num_values; ++i) |
| 780 | { |
| 781 | ModuleSpec curr_module_spec; |
| 782 | if (GetModuleSpecInfoFromUUIDDictionary (values[i], curr_module_spec)) |
| 783 | { |
Sean Callanan | bf4b7be | 2012-12-13 22:07:14 +0000 | [diff] [blame] | 784 | if (module_spec.GetArchitecture().IsCompatibleMatch(curr_module_spec.GetArchitecture())) |
Greg Clayton | b5f0fea | 2012-09-27 22:26:11 +0000 | [diff] [blame] | 785 | { |
| 786 | module_spec = curr_module_spec; |
| 787 | return true; |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | } |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | return success; |
| 800 | } |
| 801 | |