blob: b2e0ca1d80c062a7b0e5c9c9bddcb758fb2cff11 [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>
Jason Molendaf2569752012-10-30 21:26:30 +000014#include <pwd.h>
Greg Clayton1674b122010-07-21 22:12:05 +000015#include "llvm/Support/MachO.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016
17// C++ Includes
18// Other libraries and framework includes
19#include <CoreFoundation/CoreFoundation.h>
20
21// Project includes
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Core/ArchSpec.h"
23#include "lldb/Core/DataBuffer.h"
24#include "lldb/Core/DataExtractor.h"
Greg Clayton444fe992012-02-26 05:51:37 +000025#include "lldb/Core/Module.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000026#include "lldb/Core/ModuleSpec.h"
Johnny Chen9262cd52012-08-22 00:18:43 +000027#include "lldb/Core/StreamString.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Core/Timer.h"
29#include "lldb/Core/UUID.h"
Greg Claytoncd548032011-02-01 01:31:41 +000030#include "lldb/Host/Endian.h"
Johnny Chencbf15912012-02-01 01:49:50 +000031#include "lldb/Host/Host.h"
Greg Claytonad400272011-02-01 05:15:02 +000032#include "lldb/Utility/CleanUp.h"
Daniel Dunbara1ebbd22011-10-31 22:50:53 +000033#include "Host/macosx/cfcpp/CFCBundle.h"
Greg Claytonb924eb62012-09-27 03:13:55 +000034#include "Host/macosx/cfcpp/CFCData.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000035#include "Host/macosx/cfcpp/CFCReleaser.h"
Greg Claytonb72d0f02011-04-12 05:54:46 +000036#include "Host/macosx/cfcpp/CFCString.h"
Chris Lattner5bc7b672010-09-08 23:01:14 +000037#include "mach/machine.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000038
Greg Clayton0fa51242011-07-19 03:57:15 +000039
Chris Lattner24943d22010-06-08 16:52:24 +000040using namespace lldb;
41using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000042using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000043
Greg Clayton3e4238d2011-11-04 03:34:56 +000044#if !defined (__arm__) // No DebugSymbols on the iOS devices
Chris Lattner24943d22010-06-08 16:52:24 +000045extern "C" {
Greg Clayton54e7afa2010-07-09 20:39:50 +000046
Chris Lattner24943d22010-06-08 16:52:24 +000047CFURLRef DBGCopyFullDSYMURLForUUID (CFUUIDRef uuid, CFURLRef exec_url);
48CFDictionaryRef DBGCopyDSYMPropertyLists (CFURLRef dsym_url);
Greg Clayton54e7afa2010-07-09 20:39:50 +000049
50}
Greg Clayton3e4238d2011-11-04 03:34:56 +000051#endif
Chris Lattner24943d22010-06-08 16:52:24 +000052
53static bool
54SkinnyMachOFileContainsArchAndUUID
55(
56 const FileSpec &file_spec,
57 const ArchSpec *arch,
Greg Clayton0467c782011-02-04 18:53:10 +000058 const lldb_private::UUID *uuid, // the UUID we are looking for
Chris Lattner24943d22010-06-08 16:52:24 +000059 off_t file_offset,
60 DataExtractor& data,
Greg Clayton36da2aa2013-01-25 18:06:21 +000061 lldb::offset_t data_offset,
Chris Lattner24943d22010-06-08 16:52:24 +000062 const uint32_t magic
63)
64{
Greg Clayton1674b122010-07-21 22:12:05 +000065 assert(magic == HeaderMagic32 || magic == HeaderMagic32Swapped || magic == HeaderMagic64 || magic == HeaderMagic64Swapped);
66 if (magic == HeaderMagic32 || magic == HeaderMagic64)
Greg Claytoncd548032011-02-01 01:31:41 +000067 data.SetByteOrder (lldb::endian::InlHostByteOrder());
68 else if (lldb::endian::InlHostByteOrder() == eByteOrderBig)
Chris Lattner24943d22010-06-08 16:52:24 +000069 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 Claytoncf015052010-06-11 03:25:34 +000084 ArchSpec file_arch(eArchTypeMachO, cputype, cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +000085
Sean Callanan878f13c2012-12-14 23:43:03 +000086 if (!file_arch.IsCompatibleMatch(*arch))
Chris Lattner24943d22010-06-08 16:52:24 +000087 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 Clayton1674b122010-07-21 22:12:05 +000096 if (magic == HeaderMagic64Swapped || magic == HeaderMagic64)
Chris Lattner24943d22010-06-08 16:52:24 +000097 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 Clayton1674b122010-07-21 22:12:05 +0000100 if (magic == HeaderMagic64Swapped || magic == HeaderMagic64)
Chris Lattner24943d22010-06-08 16:52:24 +0000101 {
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 Clayton36da2aa2013-01-25 18:06:21 +0000119 const lldb::offset_t cmd_offset = data_offset; // Save this data_offset in case parsing of the segment goes awry!
Chris Lattner24943d22010-06-08 16:52:24 +0000120 uint32_t cmd = data.GetU32(&data_offset);
121 uint32_t cmd_size = data.GetU32(&data_offset);
Greg Clayton1674b122010-07-21 22:12:05 +0000122 if (cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +0000123 {
Greg Clayton0467c782011-02-04 18:53:10 +0000124 lldb_private::UUID file_uuid (data.GetData(&data_offset, 16), 16);
Johnny Chencbf15912012-02-01 01:49:50 +0000125 if (file_uuid == *uuid)
126 return true;
Johnny Chencbf15912012-02-01 01:49:50 +0000127 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000128 }
129 data_offset = cmd_offset + cmd_size;
130 }
131 return false;
132}
133
134bool
135UniversalMachOFileContainsArchAndUUID
136(
137 const FileSpec &file_spec,
138 const ArchSpec *arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000139 const lldb_private::UUID *uuid,
Chris Lattner24943d22010-06-08 16:52:24 +0000140 off_t file_offset,
141 DataExtractor& data,
Greg Clayton36da2aa2013-01-25 18:06:21 +0000142 lldb::offset_t data_offset,
Chris Lattner24943d22010-06-08 16:52:24 +0000143 const uint32_t magic
144)
145{
Greg Clayton1674b122010-07-21 22:12:05 +0000146 assert(magic == UniversalMagic || magic == UniversalMagicSwapped);
Chris Lattner24943d22010-06-08 16:52:24 +0000147
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 Claytoncf015052010-06-11 03:25:34 +0000171 ArchSpec fat_arch(eArchTypeMachO, arch_cputype, arch_cpusubtype);
Sean Callanan64246cd2012-12-21 21:32:25 +0000172 if (!fat_arch.IsExactMatch(*arch))
Chris Lattner24943d22010-06-08 16:52:24 +0000173 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 Clayton36da2aa2013-01-25 18:06:21 +0000180 lldb::offset_t arch_data_offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000181 uint32_t arch_magic = arch_data.GetU32(&arch_data_offset);
182
183 switch (arch_magic)
184 {
Greg Clayton1674b122010-07-21 22:12:05 +0000185 case HeaderMagic32:
186 case HeaderMagic32Swapped:
187 case HeaderMagic64:
188 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000189 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
197static bool
198FileAtPathContainsArchAndUUID
199(
200 const FileSpec &file_spec,
201 const ArchSpec *arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000202 const lldb_private::UUID *uuid
Chris Lattner24943d22010-06-08 16:52:24 +0000203)
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 Clayton36da2aa2013-01-25 18:06:21 +0000213 lldb::offset_t data_offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000214 uint32_t magic = data.GetU32(&data_offset);
215
216 switch (magic)
217 {
218 // 32 bit mach-o file
Greg Clayton1674b122010-07-21 22:12:05 +0000219 case HeaderMagic32:
220 case HeaderMagic32Swapped:
221 case HeaderMagic64:
222 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000223 return SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic);
224
225 // fat mach-o file
Greg Clayton1674b122010-07-21 22:12:05 +0000226 case UniversalMagic:
227 case UniversalMagicSwapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000228 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 Clayton95b765e2012-09-12 02:03:59 +0000237FileSpec
238Symbols::FindSymbolFileInBundle (const FileSpec& dsym_bundle_fspec,
239 const lldb_private::UUID *uuid,
240 const ArchSpec *arch)
Chris Lattner24943d22010-06-08 16:52:24 +0000241{
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 Clayton52fd9842011-02-02 02:24:04 +0000250 lldb_utility::CleanUp <DIR *, int> dirp (opendir(path), NULL, closedir);
Greg Claytonad400272011-02-01 05:15:02 +0000251 if (dirp.is_valid())
Chris Lattner24943d22010-06-08 16:52:24 +0000252 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000253 dsym_fspec.GetDirectory().SetCString(path);
Chris Lattner24943d22010-06-08 16:52:24 +0000254 struct dirent* dp;
Greg Claytonad400272011-02-01 05:15:02 +0000255 while ((dp = readdir(dirp.get())) != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000256 {
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 Clayton537a7a82010-10-20 20:54:39 +0000269 dsym_fspec.GetFilename().SetCString(dp->d_name);
Chris Lattner24943d22010-06-08 16:52:24 +0000270 if (FileAtPathContainsArchAndUUID (dsym_fspec, arch, uuid))
271 return dsym_fspec;
272 }
273 }
274 }
275 }
276 dsym_fspec.Clear();
277 return dsym_fspec;
278}
279
280static int
281LocateMacOSXFilesUsingDebugSymbols
282(
Greg Clayton444fe992012-02-26 05:51:37 +0000283 const ModuleSpec &module_spec,
Chris Lattner24943d22010-06-08 16:52:24 +0000284 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 Clayton3e4238d2011-11-04 03:34:56 +0000296#if !defined (__arm__) // No DebugSymbols on the iOS devices
297
Greg Clayton444fe992012-02-26 05:51:37 +0000298 const UUID *uuid = module_spec.GetUUIDPtr();
299 const ArchSpec *arch = module_spec.GetArchitecturePtr();
300
Chris Lattner24943d22010-06-08 16:52:24 +0000301 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 Clayton0fa51242011-07-19 03:57:15 +0000307 CFCReleaser<CFUUIDRef> module_uuid_ref(::CFUUIDCreateWithBytes (NULL,
Chris Lattner24943d22010-06-08 16:52:24 +0000308 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 Clayton444fe992012-02-26 05:51:37 +0000328 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000329 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 Molenda1b09dfe2012-10-05 04:57:34 +0000348 out_dsym_fspec->SetFile(path, path[0] == '~');
Chris Lattner24943d22010-06-08 16:52:24 +0000349
350 if (out_dsym_fspec->GetFileType () == FileSpec::eFileTypeDirectory)
351 {
Greg Clayton95b765e2012-09-12 02:03:59 +0000352 *out_dsym_fspec = Symbols::FindSymbolFileInBundle (*out_dsym_fspec, uuid, arch);
Chris Lattner24943d22010-06-08 16:52:24 +0000353 if (*out_dsym_fspec)
354 ++items_found;
355 }
356 else
357 {
358 ++items_found;
359 }
360 }
361 }
362
Greg Clayton964deba2012-03-15 21:01:31 +0000363 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 Molenda63e5cf62012-10-02 22:23:42 +0000370 uuid_dict = static_cast<CFDictionaryRef>(::CFDictionaryGetValue (dict.get(), uuid_cfstr.get()));
Greg Clayton964deba2012-03-15 21:01:31 +0000371 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 Clayton4d8c5432012-07-13 01:20:25 +0000384 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 Clayton964deba2012-03-15 21:01:31 +0000389 module_spec.GetSourceMappingList().Append (ConstString(build_src_path), ConstString(actual_src_path), true);
390 }
391 }
392 }
393 }
394
Chris Lattner24943d22010-06-08 16:52:24 +0000395 if (out_exec_fspec)
396 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000397 bool success = false;
Greg Clayton964deba2012-03-15 21:01:31 +0000398 if (uuid_dict)
Chris Lattner24943d22010-06-08 16:52:24 +0000399 {
Greg Clayton964deba2012-03-15 21:01:31 +0000400 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 Lattner24943d22010-06-08 16:52:24 +0000402 {
Greg Clayton964deba2012-03-15 21:01:31 +0000403 ++items_found;
404 out_exec_fspec->SetFile(path, path[0] == '~');
405 if (out_exec_fspec->Exists())
406 success = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000407 }
408 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000409
410 if (!success)
Greg Clayton0fa51242011-07-19 03:57:15 +0000411 {
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 Lattner24943d22010-06-08 16:52:24 +0000461 }
462 }
463 }
464 }
465 }
Greg Clayton3e4238d2011-11-04 03:34:56 +0000466#endif // #if !defined (__arm__)
467
Chris Lattner24943d22010-06-08 16:52:24 +0000468 return items_found;
469}
470
471static bool
Greg Clayton444fe992012-02-26 05:51:37 +0000472LocateDSYMInVincinityOfExecutable (const ModuleSpec &module_spec, FileSpec &dsym_fspec)
Chris Lattner24943d22010-06-08 16:52:24 +0000473{
Greg Clayton444fe992012-02-26 05:51:37 +0000474 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000475 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 Cabecinhas78c180a2012-05-06 17:56:42 +0000484 strlcat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path));
485 strlcat(path, exec_fspec->GetFilename().AsCString(), sizeof(path));
Chris Lattner24943d22010-06-08 16:52:24 +0000486
Greg Clayton537a7a82010-10-20 20:54:39 +0000487 dsym_fspec.SetFile(path, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000488
Greg Clayton444fe992012-02-26 05:51:37 +0000489 if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr()))
Chris Lattner24943d22010-06-08 16:52:24 +0000490 {
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 Cabecinhas78c180a2012-05-06 17:56:42 +0000504 strlcat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path));
505 strlcat(path, exec_fspec->GetFilename().AsCString(), sizeof(path));
Greg Clayton537a7a82010-10-20 20:54:39 +0000506 dsym_fspec.SetFile(path, false);
Greg Clayton444fe992012-02-26 05:51:37 +0000507 if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr()))
Chris Lattner24943d22010-06-08 16:52:24 +0000508 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
532FileSpec
Greg Clayton444fe992012-02-26 05:51:37 +0000533Symbols::LocateExecutableObjectFile (const ModuleSpec &module_spec)
Chris Lattner24943d22010-06-08 16:52:24 +0000534{
Greg Clayton444fe992012-02-26 05:51:37 +0000535 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
536 const ArchSpec *arch = module_spec.GetArchitecturePtr();
537 const UUID *uuid = module_spec.GetUUIDPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000538 Timer scoped_timer (__PRETTY_FUNCTION__,
539 "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)",
540 exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>",
Greg Clayton940b1032011-02-23 00:35:02 +0000541 arch ? arch->GetArchitectureName() : "<NULL>",
Chris Lattner24943d22010-06-08 16:52:24 +0000542 uuid);
543
544 FileSpec objfile_fspec;
Greg Clayton444fe992012-02-26 05:51:37 +0000545 if (exec_fspec && FileAtPathContainsArchAndUUID (exec_fspec, arch, uuid))
546 objfile_fspec = exec_fspec;
Chris Lattner24943d22010-06-08 16:52:24 +0000547 else
Greg Clayton444fe992012-02-26 05:51:37 +0000548 LocateMacOSXFilesUsingDebugSymbols (module_spec, &objfile_fspec, NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000549 return objfile_fspec;
550}
551
552FileSpec
Greg Clayton444fe992012-02-26 05:51:37 +0000553Symbols::LocateExecutableSymbolFile (const ModuleSpec &module_spec)
Chris Lattner24943d22010-06-08 16:52:24 +0000554{
Greg Clayton444fe992012-02-26 05:51:37 +0000555 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
556 const ArchSpec *arch = module_spec.GetArchitecturePtr();
557 const UUID *uuid = module_spec.GetUUIDPtr();
558
Chris Lattner24943d22010-06-08 16:52:24 +0000559 Timer scoped_timer (__PRETTY_FUNCTION__,
560 "LocateExecutableSymbolFile (file = %s, arch = %s, uuid = %p)",
561 exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>",
Greg Clayton940b1032011-02-23 00:35:02 +0000562 arch ? arch->GetArchitectureName() : "<NULL>",
Chris Lattner24943d22010-06-08 16:52:24 +0000563 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 Clayton444fe992012-02-26 05:51:37 +0000568 if (LocateDSYMInVincinityOfExecutable (module_spec, symbol_fspec) == false)
Chris Lattner24943d22010-06-08 16:52:24 +0000569 {
570 // We failed to easily find the dSYM above, so use DebugSymbols
Greg Clayton444fe992012-02-26 05:51:37 +0000571 LocateMacOSXFilesUsingDebugSymbols (module_spec, NULL, &symbol_fspec);
Chris Lattner24943d22010-06-08 16:52:24 +0000572 }
573 return symbol_fspec;
574}
Greg Claytonb924eb62012-09-27 03:13:55 +0000575
576
Greg Clayton437b5bc2012-09-27 22:26:11 +0000577static bool
578GetModuleSpecInfoFromUUIDDictionary (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 Claytonb924eb62012-09-27 03:13:55 +0000632
633
634bool
Jason Molendad6d45ce2012-10-09 01:17:11 +0000635Symbols::DownloadObjectAndSymbolFile (ModuleSpec &module_spec, bool force_lookup)
Greg Claytonb924eb62012-09-27 03:13:55 +0000636{
637 bool success = false;
638 const UUID *uuid_ptr = module_spec.GetUUIDPtr();
Greg Clayton437b5bc2012-09-27 22:26:11 +0000639 const FileSpec *file_spec_ptr = module_spec.GetFileSpecPtr();
Jason Molendad6d45ce2012-10-09 01:17:11 +0000640
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 Clayton437b5bc2012-09-27 22:26:11 +0000670 if (uuid_ptr || (file_spec_ptr && file_spec_ptr->Exists()))
Greg Claytonb924eb62012-09-27 03:13:55 +0000671 {
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 Molendaf2569752012-10-30 21:26:30 +0000688 dsym_for_uuid_exe_spec.SetFile("/usr/local/bin/dsymForUUID", false);
Greg Claytonb924eb62012-09-27 03:13:55 +0000689 g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists();
690 if (!g_dsym_for_uuid_exe_exists)
691 {
Greg Clayton36da2aa2013-01-25 18:06:21 +0000692 long bufsize;
Jason Molendaf2569752012-10-30 21:26:30 +0000693 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 Claytonb924eb62012-09-27 03:13:55 +0000709 }
710 }
Jason Molendad6d45ce2012-10-09 01:17:11 +0000711 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 Claytonb924eb62012-09-27 03:13:55 +0000717 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 Claytonb924eb62012-09-27 03:13:55 +0000722 char uuid_cstr_buffer[64];
Greg Clayton437b5bc2012-09-27 22:26:11 +0000723 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 Molenda63e5cf62012-10-02 22:23:42 +0000736 command.Printf("%s --ignoreNegativeCache --copyExecutable %s", g_dsym_for_uuid_exe_path, uuid_cstr);
Greg Clayton437b5bc2012-09-27 22:26:11 +0000737 else if (file_path && file_path[0])
Jason Molenda63e5cf62012-10-02 22:23:42 +0000738 command.Printf("%s --ignoreNegativeCache --copyExecutable %s", g_dsym_for_uuid_exe_path, file_path);
Greg Clayton437b5bc2012-09-27 22:26:11 +0000739
740 if (!command.GetString().empty())
Greg Claytonb924eb62012-09-27 03:13:55 +0000741 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000742 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 Claytonb924eb62012-09-27 03:13:55 +0000753 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000754 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 Callanan50972ae2013-02-08 23:17:17 +0000761 if (plist.get() && CFGetTypeID (plist.get()) == CFDictionaryGetTypeID ())
Greg Claytonb924eb62012-09-27 03:13:55 +0000762 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000763 if (uuid_cstr)
Greg Claytonb924eb62012-09-27 03:13:55 +0000764 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000765 CFCString uuid_cfstr(uuid_cstr);
766 CFDictionaryRef uuid_dict = (CFDictionaryRef)CFDictionaryGetValue (plist.get(), uuid_cfstr.get());
767 success = GetModuleSpecInfoFromUUIDDictionary (uuid_dict, module_spec);
Greg Claytonb924eb62012-09-27 03:13:55 +0000768 }
Greg Clayton437b5bc2012-09-27 22:26:11 +0000769 else
Greg Claytonb924eb62012-09-27 03:13:55 +0000770 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000771 const CFIndex num_values = ::CFDictionaryGetCount(plist.get());
772 if (num_values > 0)
Greg Claytonb924eb62012-09-27 03:13:55 +0000773 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000774 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 Callanan40e278c2012-12-13 22:07:14 +0000788 if (module_spec.GetArchitecture().IsCompatibleMatch(curr_module_spec.GetArchitecture()))
Greg Clayton437b5bc2012-09-27 22:26:11 +0000789 {
790 module_spec = curr_module_spec;
791 return true;
792 }
793 }
794 }
795 }
Greg Claytonb924eb62012-09-27 03:13:55 +0000796 }
797 }
798 }
799 }
800 }
801 }
802 }
803 return success;
804}
805