blob: a8fa1c0dbc3ce2c06d83d305f4ac30eb00c2dc9a [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Clayton1674b122010-07-21 22:12:05 +000014#include "llvm/Support/MachO.h"
Chris Lattner24943d22010-06-08 16:52:24 +000015
16// C++ Includes
17// Other libraries and framework includes
18#include <CoreFoundation/CoreFoundation.h>
19
20// Project includes
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/ArchSpec.h"
22#include "lldb/Core/DataBuffer.h"
23#include "lldb/Core/DataExtractor.h"
Greg Clayton444fe992012-02-26 05:51:37 +000024#include "lldb/Core/Module.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000025#include "lldb/Core/ModuleSpec.h"
Johnny Chen9262cd52012-08-22 00:18:43 +000026#include "lldb/Core/StreamString.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Core/Timer.h"
28#include "lldb/Core/UUID.h"
Greg Claytoncd548032011-02-01 01:31:41 +000029#include "lldb/Host/Endian.h"
Johnny Chencbf15912012-02-01 01:49:50 +000030#include "lldb/Host/Host.h"
Greg Claytonad400272011-02-01 05:15:02 +000031#include "lldb/Utility/CleanUp.h"
Daniel Dunbara1ebbd22011-10-31 22:50:53 +000032#include "Host/macosx/cfcpp/CFCBundle.h"
Greg Claytonb924eb62012-09-27 03:13:55 +000033#include "Host/macosx/cfcpp/CFCData.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000034#include "Host/macosx/cfcpp/CFCReleaser.h"
Greg Claytonb72d0f02011-04-12 05:54:46 +000035#include "Host/macosx/cfcpp/CFCString.h"
Chris Lattner5bc7b672010-09-08 23:01:14 +000036#include "mach/machine.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000037
Greg Clayton0fa51242011-07-19 03:57:15 +000038
Chris Lattner24943d22010-06-08 16:52:24 +000039using namespace lldb;
40using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000041using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000042
Greg Clayton3e4238d2011-11-04 03:34:56 +000043#if !defined (__arm__) // No DebugSymbols on the iOS devices
Chris Lattner24943d22010-06-08 16:52:24 +000044extern "C" {
Greg Clayton54e7afa2010-07-09 20:39:50 +000045
Chris Lattner24943d22010-06-08 16:52:24 +000046CFURLRef DBGCopyFullDSYMURLForUUID (CFUUIDRef uuid, CFURLRef exec_url);
47CFDictionaryRef DBGCopyDSYMPropertyLists (CFURLRef dsym_url);
Greg Clayton54e7afa2010-07-09 20:39:50 +000048
49}
Greg Clayton3e4238d2011-11-04 03:34:56 +000050#endif
Chris Lattner24943d22010-06-08 16:52:24 +000051
52static bool
53SkinnyMachOFileContainsArchAndUUID
54(
55 const FileSpec &file_spec,
56 const ArchSpec *arch,
Greg Clayton0467c782011-02-04 18:53:10 +000057 const lldb_private::UUID *uuid, // the UUID we are looking for
Chris Lattner24943d22010-06-08 16:52:24 +000058 off_t file_offset,
59 DataExtractor& data,
60 uint32_t data_offset,
61 const uint32_t magic
62)
63{
Greg Clayton1674b122010-07-21 22:12:05 +000064 assert(magic == HeaderMagic32 || magic == HeaderMagic32Swapped || magic == HeaderMagic64 || magic == HeaderMagic64Swapped);
65 if (magic == HeaderMagic32 || magic == HeaderMagic64)
Greg Claytoncd548032011-02-01 01:31:41 +000066 data.SetByteOrder (lldb::endian::InlHostByteOrder());
67 else if (lldb::endian::InlHostByteOrder() == eByteOrderBig)
Chris Lattner24943d22010-06-08 16:52:24 +000068 data.SetByteOrder (eByteOrderLittle);
69 else
70 data.SetByteOrder (eByteOrderBig);
71
72 uint32_t i;
73 const uint32_t cputype = data.GetU32(&data_offset); // cpu specifier
74 const uint32_t cpusubtype = data.GetU32(&data_offset); // machine specifier
75 data_offset+=4; // Skip mach file type
76 const uint32_t ncmds = data.GetU32(&data_offset); // number of load commands
77 const uint32_t sizeofcmds = data.GetU32(&data_offset); // the size of all the load commands
78 data_offset+=4; // Skip flags
79
80 // Check the architecture if we have a valid arch pointer
81 if (arch)
82 {
Greg Claytoncf015052010-06-11 03:25:34 +000083 ArchSpec file_arch(eArchTypeMachO, cputype, cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +000084
85 if (file_arch != *arch)
86 return false;
87 }
88
89 // The file exists, and if a valid arch pointer was passed in we know
90 // if already matches, so we can return if we aren't looking for a specific
91 // UUID
92 if (uuid == NULL)
93 return true;
94
Greg Clayton1674b122010-07-21 22:12:05 +000095 if (magic == HeaderMagic64Swapped || magic == HeaderMagic64)
Chris Lattner24943d22010-06-08 16:52:24 +000096 data_offset += 4; // Skip reserved field for in mach_header_64
97
98 // Make sure we have enough data for all the load commands
Greg Clayton1674b122010-07-21 22:12:05 +000099 if (magic == HeaderMagic64Swapped || magic == HeaderMagic64)
Chris Lattner24943d22010-06-08 16:52:24 +0000100 {
101 if (data.GetByteSize() < sizeof(struct mach_header_64) + sizeofcmds)
102 {
103 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, sizeof(struct mach_header_64) + sizeofcmds));
104 data.SetData (data_buffer_sp);
105 }
106 }
107 else
108 {
109 if (data.GetByteSize() < sizeof(struct mach_header) + sizeofcmds)
110 {
111 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, sizeof(struct mach_header) + sizeofcmds));
112 data.SetData (data_buffer_sp);
113 }
114 }
115
116 for (i=0; i<ncmds; i++)
117 {
118 const uint32_t cmd_offset = data_offset; // Save this data_offset in case parsing of the segment goes awry!
119 uint32_t cmd = data.GetU32(&data_offset);
120 uint32_t cmd_size = data.GetU32(&data_offset);
Greg Clayton1674b122010-07-21 22:12:05 +0000121 if (cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +0000122 {
Greg Clayton0467c782011-02-04 18:53:10 +0000123 lldb_private::UUID file_uuid (data.GetData(&data_offset, 16), 16);
Johnny Chencbf15912012-02-01 01:49:50 +0000124 if (file_uuid == *uuid)
125 return true;
126
127 // Emit some warning messages since the UUIDs do not match!
128 char path_buf[PATH_MAX];
129 path_buf[0] = '\0';
130 const char *path = file_spec.GetPath(path_buf, PATH_MAX) ? path_buf
131 : file_spec.GetFilename().AsCString();
Johnny Chen9262cd52012-08-22 00:18:43 +0000132 StreamString ss_m_uuid, ss_o_uuid;
133 uuid->Dump(&ss_m_uuid);
134 file_uuid.Dump(&ss_o_uuid);
Johnny Chencbf15912012-02-01 01:49:50 +0000135 Host::SystemLog (Host::eSystemLogWarning,
Johnny Chen9262cd52012-08-22 00:18:43 +0000136 "warning: UUID mismatch detected between binary (%s) and:\n\t'%s' (%s)\n",
137 ss_m_uuid.GetData(), path, ss_o_uuid.GetData());
Johnny Chencbf15912012-02-01 01:49:50 +0000138 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000139 }
140 data_offset = cmd_offset + cmd_size;
141 }
142 return false;
143}
144
145bool
146UniversalMachOFileContainsArchAndUUID
147(
148 const FileSpec &file_spec,
149 const ArchSpec *arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000150 const lldb_private::UUID *uuid,
Chris Lattner24943d22010-06-08 16:52:24 +0000151 off_t file_offset,
152 DataExtractor& data,
153 uint32_t data_offset,
154 const uint32_t magic
155)
156{
Greg Clayton1674b122010-07-21 22:12:05 +0000157 assert(magic == UniversalMagic || magic == UniversalMagicSwapped);
Chris Lattner24943d22010-06-08 16:52:24 +0000158
159 // Universal mach-o files always have their headers encoded as BIG endian
160 data.SetByteOrder(eByteOrderBig);
161
162 uint32_t i;
163 const uint32_t nfat_arch = data.GetU32(&data_offset); // number of structs that follow
164 const uint32_t fat_header_and_arch_size = sizeof(struct fat_header) + nfat_arch * sizeof(struct fat_arch);
165 if (data.GetByteSize() < fat_header_and_arch_size)
166 {
167 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, fat_header_and_arch_size));
168 data.SetData (data_buffer_sp);
169 }
170
171 for (i=0; i<nfat_arch; i++)
172 {
173 cpu_type_t arch_cputype = data.GetU32(&data_offset); // cpu specifier (int)
174 cpu_subtype_t arch_cpusubtype = data.GetU32(&data_offset); // machine specifier (int)
175 uint32_t arch_offset = data.GetU32(&data_offset); // file offset to this object file
176 // uint32_t arch_size = data.GetU32(&data_offset); // size of this object file
177 // uint32_t arch_align = data.GetU32(&data_offset); // alignment as a power of 2
178 data_offset += 8; // Skip size and align as we don't need those
179 // Only process this slice if the cpu type/subtype matches
180 if (arch)
181 {
Greg Claytoncf015052010-06-11 03:25:34 +0000182 ArchSpec fat_arch(eArchTypeMachO, arch_cputype, arch_cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +0000183 if (fat_arch != *arch)
184 continue;
185 }
186
187 // Create a buffer with only the arch slice date in it
188 DataExtractor arch_data;
189 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset + arch_offset, 0x1000));
190 arch_data.SetData(data_buffer_sp);
191 uint32_t arch_data_offset = 0;
192 uint32_t arch_magic = arch_data.GetU32(&arch_data_offset);
193
194 switch (arch_magic)
195 {
Greg Clayton1674b122010-07-21 22:12:05 +0000196 case HeaderMagic32:
197 case HeaderMagic32Swapped:
198 case HeaderMagic64:
199 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000200 if (SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset + arch_offset, arch_data, arch_data_offset, arch_magic))
201 return true;
202 break;
203 }
204 }
205 return false;
206}
207
208static bool
209FileAtPathContainsArchAndUUID
210(
211 const FileSpec &file_spec,
212 const ArchSpec *arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000213 const lldb_private::UUID *uuid
Chris Lattner24943d22010-06-08 16:52:24 +0000214)
215{
216 DataExtractor data;
217 off_t file_offset = 0;
218 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, 0x1000));
219
220 if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0)
221 {
222 data.SetData(data_buffer_sp);
223
224 uint32_t data_offset = 0;
225 uint32_t magic = data.GetU32(&data_offset);
226
227 switch (magic)
228 {
229 // 32 bit mach-o file
Greg Clayton1674b122010-07-21 22:12:05 +0000230 case HeaderMagic32:
231 case HeaderMagic32Swapped:
232 case HeaderMagic64:
233 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000234 return SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic);
235
236 // fat mach-o file
Greg Clayton1674b122010-07-21 22:12:05 +0000237 case UniversalMagic:
238 case UniversalMagicSwapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000239 return UniversalMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic);
240
241 default:
242 break;
243 }
244 }
245 return false;
246}
247
Greg Clayton95b765e2012-09-12 02:03:59 +0000248FileSpec
249Symbols::FindSymbolFileInBundle (const FileSpec& dsym_bundle_fspec,
250 const lldb_private::UUID *uuid,
251 const ArchSpec *arch)
Chris Lattner24943d22010-06-08 16:52:24 +0000252{
253 char path[PATH_MAX];
254
255 FileSpec dsym_fspec;
256
257 if (dsym_bundle_fspec.GetPath(path, sizeof(path)))
258 {
259 ::strncat (path, "/Contents/Resources/DWARF", sizeof(path) - strlen(path) - 1);
260
Greg Clayton52fd9842011-02-02 02:24:04 +0000261 lldb_utility::CleanUp <DIR *, int> dirp (opendir(path), NULL, closedir);
Greg Claytonad400272011-02-01 05:15:02 +0000262 if (dirp.is_valid())
Chris Lattner24943d22010-06-08 16:52:24 +0000263 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000264 dsym_fspec.GetDirectory().SetCString(path);
Chris Lattner24943d22010-06-08 16:52:24 +0000265 struct dirent* dp;
Greg Claytonad400272011-02-01 05:15:02 +0000266 while ((dp = readdir(dirp.get())) != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000267 {
268 // Only search directories
269 if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN)
270 {
271 if (dp->d_namlen == 1 && dp->d_name[0] == '.')
272 continue;
273
274 if (dp->d_namlen == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.')
275 continue;
276 }
277
278 if (dp->d_type == DT_REG || dp->d_type == DT_UNKNOWN)
279 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000280 dsym_fspec.GetFilename().SetCString(dp->d_name);
Chris Lattner24943d22010-06-08 16:52:24 +0000281 if (FileAtPathContainsArchAndUUID (dsym_fspec, arch, uuid))
282 return dsym_fspec;
283 }
284 }
285 }
286 }
287 dsym_fspec.Clear();
288 return dsym_fspec;
289}
290
291static int
292LocateMacOSXFilesUsingDebugSymbols
293(
Greg Clayton444fe992012-02-26 05:51:37 +0000294 const ModuleSpec &module_spec,
Chris Lattner24943d22010-06-08 16:52:24 +0000295 FileSpec *out_exec_fspec, // If non-NULL, try and find the executable
296 FileSpec *out_dsym_fspec // If non-NULL try and find the debug symbol file
297)
298{
299 int items_found = 0;
300
301 if (out_exec_fspec)
302 out_exec_fspec->Clear();
303
304 if (out_dsym_fspec)
305 out_dsym_fspec->Clear();
306
Greg Clayton3e4238d2011-11-04 03:34:56 +0000307#if !defined (__arm__) // No DebugSymbols on the iOS devices
308
Greg Clayton444fe992012-02-26 05:51:37 +0000309 const UUID *uuid = module_spec.GetUUIDPtr();
310 const ArchSpec *arch = module_spec.GetArchitecturePtr();
311
Chris Lattner24943d22010-06-08 16:52:24 +0000312 if (uuid && uuid->IsValid())
313 {
314 // Try and locate the dSYM file using DebugSymbols first
315 const UInt8 *module_uuid = (const UInt8 *)uuid->GetBytes();
316 if (module_uuid != NULL)
317 {
Greg Clayton0fa51242011-07-19 03:57:15 +0000318 CFCReleaser<CFUUIDRef> module_uuid_ref(::CFUUIDCreateWithBytes (NULL,
Chris Lattner24943d22010-06-08 16:52:24 +0000319 module_uuid[0],
320 module_uuid[1],
321 module_uuid[2],
322 module_uuid[3],
323 module_uuid[4],
324 module_uuid[5],
325 module_uuid[6],
326 module_uuid[7],
327 module_uuid[8],
328 module_uuid[9],
329 module_uuid[10],
330 module_uuid[11],
331 module_uuid[12],
332 module_uuid[13],
333 module_uuid[14],
334 module_uuid[15]));
335
336 if (module_uuid_ref.get())
337 {
338 CFCReleaser<CFURLRef> exec_url;
Greg Clayton444fe992012-02-26 05:51:37 +0000339 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000340 if (exec_fspec)
341 {
342 char exec_cf_path[PATH_MAX];
343 if (exec_fspec->GetPath(exec_cf_path, sizeof(exec_cf_path)))
344 exec_url.reset(::CFURLCreateFromFileSystemRepresentation (NULL,
345 (const UInt8 *)exec_cf_path,
346 strlen(exec_cf_path),
347 FALSE));
348 }
349
350 CFCReleaser<CFURLRef> dsym_url (::DBGCopyFullDSYMURLForUUID(module_uuid_ref.get(), exec_url.get()));
351 char path[PATH_MAX];
352
353 if (dsym_url.get())
354 {
355 if (out_dsym_fspec)
356 {
357 if (::CFURLGetFileSystemRepresentation (dsym_url.get(), true, (UInt8*)path, sizeof(path)-1))
358 {
Jason Molenda1b09dfe2012-10-05 04:57:34 +0000359 out_dsym_fspec->SetFile(path, path[0] == '~');
Chris Lattner24943d22010-06-08 16:52:24 +0000360
361 if (out_dsym_fspec->GetFileType () == FileSpec::eFileTypeDirectory)
362 {
Greg Clayton95b765e2012-09-12 02:03:59 +0000363 *out_dsym_fspec = Symbols::FindSymbolFileInBundle (*out_dsym_fspec, uuid, arch);
Chris Lattner24943d22010-06-08 16:52:24 +0000364 if (*out_dsym_fspec)
365 ++items_found;
366 }
367 else
368 {
369 ++items_found;
370 }
371 }
372 }
373
Greg Clayton964deba2012-03-15 21:01:31 +0000374 CFCReleaser<CFDictionaryRef> dict(::DBGCopyDSYMPropertyLists (dsym_url.get()));
375 CFDictionaryRef uuid_dict = NULL;
376 if (dict.get())
377 {
378 char uuid_cstr_buf[64];
379 const char *uuid_cstr = uuid->GetAsCString (uuid_cstr_buf, sizeof(uuid_cstr_buf));
380 CFCString uuid_cfstr (uuid_cstr);
Jason Molenda63e5cf62012-10-02 22:23:42 +0000381 uuid_dict = static_cast<CFDictionaryRef>(::CFDictionaryGetValue (dict.get(), uuid_cfstr.get()));
Greg Clayton964deba2012-03-15 21:01:31 +0000382 if (uuid_dict)
383 {
384
385 CFStringRef actual_src_cfpath = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGSourcePath")));
386 if (actual_src_cfpath)
387 {
388 CFStringRef build_src_cfpath = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGBuildSourcePath")));
389 if (build_src_cfpath)
390 {
391 char actual_src_path[PATH_MAX];
392 char build_src_path[PATH_MAX];
393 ::CFStringGetFileSystemRepresentation (actual_src_cfpath, actual_src_path, sizeof(actual_src_path));
394 ::CFStringGetFileSystemRepresentation (build_src_cfpath, build_src_path, sizeof(build_src_path));
Greg Clayton4d8c5432012-07-13 01:20:25 +0000395 if (actual_src_path[0] == '~')
396 {
397 FileSpec resolved_source_path(actual_src_path, true);
398 resolved_source_path.GetPath(actual_src_path, sizeof(actual_src_path));
399 }
Greg Clayton964deba2012-03-15 21:01:31 +0000400 module_spec.GetSourceMappingList().Append (ConstString(build_src_path), ConstString(actual_src_path), true);
401 }
402 }
403 }
404 }
405
Chris Lattner24943d22010-06-08 16:52:24 +0000406 if (out_exec_fspec)
407 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000408 bool success = false;
Greg Clayton964deba2012-03-15 21:01:31 +0000409 if (uuid_dict)
Chris Lattner24943d22010-06-08 16:52:24 +0000410 {
Greg Clayton964deba2012-03-15 21:01:31 +0000411 CFStringRef exec_cf_path = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGSymbolRichExecutable")));
412 if (exec_cf_path && ::CFStringGetFileSystemRepresentation (exec_cf_path, path, sizeof(path)))
Chris Lattner24943d22010-06-08 16:52:24 +0000413 {
Greg Clayton964deba2012-03-15 21:01:31 +0000414 ++items_found;
415 out_exec_fspec->SetFile(path, path[0] == '~');
416 if (out_exec_fspec->Exists())
417 success = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000418 }
419 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000420
421 if (!success)
Greg Clayton0fa51242011-07-19 03:57:15 +0000422 {
423 // No dictionary, check near the dSYM bundle for an executable that matches...
424 if (::CFURLGetFileSystemRepresentation (dsym_url.get(), true, (UInt8*)path, sizeof(path)-1))
425 {
426 char *dsym_extension_pos = ::strstr (path, ".dSYM");
427 if (dsym_extension_pos)
428 {
429 *dsym_extension_pos = '\0';
430 FileSpec file_spec (path, true);
431 switch (file_spec.GetFileType())
432 {
433 case FileSpec::eFileTypeDirectory: // Bundle directory?
434 {
435 CFCBundle bundle (path);
436 CFCReleaser<CFURLRef> bundle_exe_url (bundle.CopyExecutableURL ());
437 if (bundle_exe_url.get())
438 {
439 if (::CFURLGetFileSystemRepresentation (bundle_exe_url.get(), true, (UInt8*)path, sizeof(path)-1))
440 {
441 FileSpec bundle_exe_file_spec (path, true);
442
443 if (FileAtPathContainsArchAndUUID (bundle_exe_file_spec, arch, uuid))
444 {
445 ++items_found;
446 *out_exec_fspec = bundle_exe_file_spec;
447 }
448 }
449 }
450 }
451 break;
452
453 case FileSpec::eFileTypePipe: // Forget pipes
454 case FileSpec::eFileTypeSocket: // We can't process socket files
455 case FileSpec::eFileTypeInvalid: // File doesn't exist...
456 break;
457
458 case FileSpec::eFileTypeUnknown:
459 case FileSpec::eFileTypeRegular:
460 case FileSpec::eFileTypeSymbolicLink:
461 case FileSpec::eFileTypeOther:
462 if (FileAtPathContainsArchAndUUID (file_spec, arch, uuid))
463 {
464 ++items_found;
465 *out_exec_fspec = file_spec;
466 }
467 break;
468 }
469 }
470 }
471 }
Chris Lattner24943d22010-06-08 16:52:24 +0000472 }
473 }
474 }
475 }
476 }
Greg Clayton3e4238d2011-11-04 03:34:56 +0000477#endif // #if !defined (__arm__)
478
Chris Lattner24943d22010-06-08 16:52:24 +0000479 return items_found;
480}
481
482static bool
Greg Clayton444fe992012-02-26 05:51:37 +0000483LocateDSYMInVincinityOfExecutable (const ModuleSpec &module_spec, FileSpec &dsym_fspec)
Chris Lattner24943d22010-06-08 16:52:24 +0000484{
Greg Clayton444fe992012-02-26 05:51:37 +0000485 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000486 if (exec_fspec)
487 {
488 char path[PATH_MAX];
489 if (exec_fspec->GetPath(path, sizeof(path)))
490 {
491 // Make sure the module isn't already just a dSYM file...
492 if (strcasestr(path, ".dSYM/Contents/Resources/DWARF") == NULL)
493 {
494 size_t obj_file_path_length = strlen(path);
Filipe Cabecinhas78c180a2012-05-06 17:56:42 +0000495 strlcat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path));
496 strlcat(path, exec_fspec->GetFilename().AsCString(), sizeof(path));
Chris Lattner24943d22010-06-08 16:52:24 +0000497
Greg Clayton537a7a82010-10-20 20:54:39 +0000498 dsym_fspec.SetFile(path, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000499
Greg Clayton444fe992012-02-26 05:51:37 +0000500 if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr()))
Chris Lattner24943d22010-06-08 16:52:24 +0000501 {
502 return true;
503 }
504 else
505 {
506 path[obj_file_path_length] = '\0';
507
508 char *last_dot = strrchr(path, '.');
509 while (last_dot != NULL && last_dot[0])
510 {
511 char *next_slash = strchr(last_dot, '/');
512 if (next_slash != NULL)
513 {
514 *next_slash = '\0';
Filipe Cabecinhas78c180a2012-05-06 17:56:42 +0000515 strlcat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path));
516 strlcat(path, exec_fspec->GetFilename().AsCString(), sizeof(path));
Greg Clayton537a7a82010-10-20 20:54:39 +0000517 dsym_fspec.SetFile(path, false);
Greg Clayton444fe992012-02-26 05:51:37 +0000518 if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr()))
Chris Lattner24943d22010-06-08 16:52:24 +0000519 return true;
520 else
521 {
522 *last_dot = '\0';
523 char *prev_slash = strrchr(path, '/');
524 if (prev_slash != NULL)
525 *prev_slash = '\0';
526 else
527 break;
528 }
529 }
530 else
531 {
532 break;
533 }
534 }
535 }
536 }
537 }
538 }
539 dsym_fspec.Clear();
540 return false;
541}
542
543FileSpec
Greg Clayton444fe992012-02-26 05:51:37 +0000544Symbols::LocateExecutableObjectFile (const ModuleSpec &module_spec)
Chris Lattner24943d22010-06-08 16:52:24 +0000545{
Greg Clayton444fe992012-02-26 05:51:37 +0000546 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
547 const ArchSpec *arch = module_spec.GetArchitecturePtr();
548 const UUID *uuid = module_spec.GetUUIDPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000549 Timer scoped_timer (__PRETTY_FUNCTION__,
550 "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)",
551 exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>",
Greg Clayton940b1032011-02-23 00:35:02 +0000552 arch ? arch->GetArchitectureName() : "<NULL>",
Chris Lattner24943d22010-06-08 16:52:24 +0000553 uuid);
554
555 FileSpec objfile_fspec;
Greg Clayton444fe992012-02-26 05:51:37 +0000556 if (exec_fspec && FileAtPathContainsArchAndUUID (exec_fspec, arch, uuid))
557 objfile_fspec = exec_fspec;
Chris Lattner24943d22010-06-08 16:52:24 +0000558 else
Greg Clayton444fe992012-02-26 05:51:37 +0000559 LocateMacOSXFilesUsingDebugSymbols (module_spec, &objfile_fspec, NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000560 return objfile_fspec;
561}
562
563FileSpec
Greg Clayton444fe992012-02-26 05:51:37 +0000564Symbols::LocateExecutableSymbolFile (const ModuleSpec &module_spec)
Chris Lattner24943d22010-06-08 16:52:24 +0000565{
Greg Clayton444fe992012-02-26 05:51:37 +0000566 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
567 const ArchSpec *arch = module_spec.GetArchitecturePtr();
568 const UUID *uuid = module_spec.GetUUIDPtr();
569
Chris Lattner24943d22010-06-08 16:52:24 +0000570 Timer scoped_timer (__PRETTY_FUNCTION__,
571 "LocateExecutableSymbolFile (file = %s, arch = %s, uuid = %p)",
572 exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>",
Greg Clayton940b1032011-02-23 00:35:02 +0000573 arch ? arch->GetArchitectureName() : "<NULL>",
Chris Lattner24943d22010-06-08 16:52:24 +0000574 uuid);
575
576 FileSpec symbol_fspec;
577 // First try and find the dSYM in the same directory as the executable or in
578 // an appropriate parent directory
Greg Clayton444fe992012-02-26 05:51:37 +0000579 if (LocateDSYMInVincinityOfExecutable (module_spec, symbol_fspec) == false)
Chris Lattner24943d22010-06-08 16:52:24 +0000580 {
581 // We failed to easily find the dSYM above, so use DebugSymbols
Greg Clayton444fe992012-02-26 05:51:37 +0000582 LocateMacOSXFilesUsingDebugSymbols (module_spec, NULL, &symbol_fspec);
Chris Lattner24943d22010-06-08 16:52:24 +0000583 }
584 return symbol_fspec;
585}
Greg Claytonb924eb62012-09-27 03:13:55 +0000586
587
Greg Clayton437b5bc2012-09-27 22:26:11 +0000588static bool
589GetModuleSpecInfoFromUUIDDictionary (CFDictionaryRef uuid_dict, ModuleSpec &module_spec)
590{
591 bool success = false;
592 if (uuid_dict != NULL && CFGetTypeID (uuid_dict) == CFDictionaryGetTypeID ())
593 {
594 std::string str;
595 CFStringRef cf_str;
596
597 cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGSymbolRichExecutable"));
598 if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ())
599 {
600 if (CFCString::FileSystemRepresentation(cf_str, str))
601 module_spec.GetFileSpec().SetFile (str.c_str(), true);
602 }
603
604 cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGDSYMPath"));
605 if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ())
606 {
607 if (CFCString::FileSystemRepresentation(cf_str, str))
608 {
609 module_spec.GetSymbolFileSpec().SetFile (str.c_str(), true);
610 success = true;
611 }
612 }
613
614 cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGArchitecture"));
615 if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ())
616 {
617 if (CFCString::FileSystemRepresentation(cf_str, str))
618 module_spec.GetArchitecture().SetTriple(str.c_str());
619 }
620
621 std::string DBGBuildSourcePath;
622 std::string DBGSourcePath;
623
624 cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGBuildSourcePath"));
625 if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ())
626 {
627 CFCString::FileSystemRepresentation(cf_str, DBGBuildSourcePath);
628 }
629
630 cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGSourcePath"));
631 if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ())
632 {
633 CFCString::FileSystemRepresentation(cf_str, DBGSourcePath);
634 }
635
636 if (!DBGBuildSourcePath.empty() && !DBGSourcePath.empty())
637 {
638 module_spec.GetSourceMappingList().Append (ConstString(DBGBuildSourcePath.c_str()), ConstString(DBGSourcePath.c_str()), true);
639 }
640 }
641 return success;
642}
Greg Claytonb924eb62012-09-27 03:13:55 +0000643
644
645bool
Jason Molendad6d45ce2012-10-09 01:17:11 +0000646Symbols::DownloadObjectAndSymbolFile (ModuleSpec &module_spec, bool force_lookup)
Greg Claytonb924eb62012-09-27 03:13:55 +0000647{
648 bool success = false;
649 const UUID *uuid_ptr = module_spec.GetUUIDPtr();
Greg Clayton437b5bc2012-09-27 22:26:11 +0000650 const FileSpec *file_spec_ptr = module_spec.GetFileSpecPtr();
Jason Molendad6d45ce2012-10-09 01:17:11 +0000651
652 // It's expensive to check for the DBGShellCommands defaults setting, only do it once per
653 // lldb run and cache the result.
654 static bool g_have_checked_for_dbgshell_command = false;
655 static const char *g_dbgshell_command = NULL;
656 if (g_have_checked_for_dbgshell_command == false)
657 {
658 g_have_checked_for_dbgshell_command = true;
659 CFTypeRef defaults_setting = CFPreferencesCopyAppValue (CFSTR ("DBGShellCommands"), CFSTR ("com.apple.DebugSymbols"));
660 if (defaults_setting && CFGetTypeID (defaults_setting) == CFStringGetTypeID())
661 {
662 char cstr_buf[PATH_MAX];
663 if (CFStringGetCString ((CFStringRef) defaults_setting, cstr_buf, sizeof (cstr_buf), kCFStringEncodingUTF8))
664 {
665 g_dbgshell_command = strdup (cstr_buf); // this malloc'ed memory will never be freed
666 }
667 }
668 if (defaults_setting)
669 {
670 CFRelease (defaults_setting);
671 }
672 }
673
674 // When g_dbgshell_command is NULL, the user has not enabled the use of an external program
675 // to find the symbols, don't run it for them.
676 if (force_lookup == false && g_dbgshell_command == NULL)
677 {
678 return false;
679 }
680
Greg Clayton437b5bc2012-09-27 22:26:11 +0000681 if (uuid_ptr || (file_spec_ptr && file_spec_ptr->Exists()))
Greg Claytonb924eb62012-09-27 03:13:55 +0000682 {
683 static bool g_located_dsym_for_uuid_exe = false;
684 static bool g_dsym_for_uuid_exe_exists = false;
685 static char g_dsym_for_uuid_exe_path[PATH_MAX];
686 if (!g_located_dsym_for_uuid_exe)
687 {
688 g_located_dsym_for_uuid_exe = true;
689 const char *dsym_for_uuid_exe_path_cstr = getenv("LLDB_APPLE_DSYMFORUUID_EXECUTABLE");
690 FileSpec dsym_for_uuid_exe_spec;
691 if (dsym_for_uuid_exe_path_cstr)
692 {
693 dsym_for_uuid_exe_spec.SetFile(dsym_for_uuid_exe_path_cstr, true);
694 g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists();
695 }
696
697 if (!g_dsym_for_uuid_exe_exists)
698 {
699 dsym_for_uuid_exe_spec.SetFile("~rc/bin/dsymForUUID", true);
700 g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists();
701 if (!g_dsym_for_uuid_exe_exists)
702 {
703 dsym_for_uuid_exe_spec.SetFile("/usr/local/bin/dsymForUUID", false);
Jason Molendad6d45ce2012-10-09 01:17:11 +0000704 g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists();
Greg Claytonb924eb62012-09-27 03:13:55 +0000705 }
706 }
Jason Molendad6d45ce2012-10-09 01:17:11 +0000707 if (!g_dsym_for_uuid_exe_exists && g_dbgshell_command != NULL)
708 {
709 dsym_for_uuid_exe_spec.SetFile(g_dbgshell_command, true);
710 g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists();
711 }
712
Greg Claytonb924eb62012-09-27 03:13:55 +0000713 if (g_dsym_for_uuid_exe_exists)
714 dsym_for_uuid_exe_spec.GetPath (g_dsym_for_uuid_exe_path, sizeof(g_dsym_for_uuid_exe_path));
715 }
716 if (g_dsym_for_uuid_exe_exists)
717 {
Greg Claytonb924eb62012-09-27 03:13:55 +0000718 char uuid_cstr_buffer[64];
Greg Clayton437b5bc2012-09-27 22:26:11 +0000719 char file_path[PATH_MAX];
720 uuid_cstr_buffer[0] = '\0';
721 file_path[0] = '\0';
722 const char *uuid_cstr = NULL;
723
724 if (uuid_ptr)
725 uuid_cstr = uuid_ptr->GetAsCString(uuid_cstr_buffer, sizeof(uuid_cstr_buffer));
726
727 if (file_spec_ptr)
728 file_spec_ptr->GetPath(file_path, sizeof(file_path));
729
730 StreamString command;
731 if (uuid_cstr)
Jason Molenda63e5cf62012-10-02 22:23:42 +0000732 command.Printf("%s --ignoreNegativeCache --copyExecutable %s", g_dsym_for_uuid_exe_path, uuid_cstr);
Greg Clayton437b5bc2012-09-27 22:26:11 +0000733 else if (file_path && file_path[0])
Jason Molenda63e5cf62012-10-02 22:23:42 +0000734 command.Printf("%s --ignoreNegativeCache --copyExecutable %s", g_dsym_for_uuid_exe_path, file_path);
Greg Clayton437b5bc2012-09-27 22:26:11 +0000735
736 if (!command.GetString().empty())
Greg Claytonb924eb62012-09-27 03:13:55 +0000737 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000738 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 Claytonb924eb62012-09-27 03:13:55 +0000749 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000750 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
757 if (CFGetTypeID (plist.get()) == CFDictionaryGetTypeID ())
Greg Claytonb924eb62012-09-27 03:13:55 +0000758 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000759 if (uuid_cstr)
Greg Claytonb924eb62012-09-27 03:13:55 +0000760 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000761 CFCString uuid_cfstr(uuid_cstr);
762 CFDictionaryRef uuid_dict = (CFDictionaryRef)CFDictionaryGetValue (plist.get(), uuid_cfstr.get());
763 success = GetModuleSpecInfoFromUUIDDictionary (uuid_dict, module_spec);
Greg Claytonb924eb62012-09-27 03:13:55 +0000764 }
Greg Clayton437b5bc2012-09-27 22:26:11 +0000765 else
Greg Claytonb924eb62012-09-27 03:13:55 +0000766 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000767 const CFIndex num_values = ::CFDictionaryGetCount(plist.get());
768 if (num_values > 0)
Greg Claytonb924eb62012-09-27 03:13:55 +0000769 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000770 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 {
784 if (module_spec.GetArchitecture() == curr_module_spec.GetArchitecture())
785 {
786 module_spec = curr_module_spec;
787 return true;
788 }
789 }
790 }
791 }
Greg Claytonb924eb62012-09-27 03:13:55 +0000792 }
793 }
794 }
795 }
796 }
797 }
798 }
799 return success;
800}
801