blob: b102a4bcd9ce179d1dace98db5a3378454af378a [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,
61 uint32_t data_offset,
62 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 {
119 const uint32_t cmd_offset = data_offset; // Save this data_offset in case parsing of the segment goes awry!
120 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;
127
128 // Emit some warning messages since the UUIDs do not match!
129 char path_buf[PATH_MAX];
130 path_buf[0] = '\0';
131 const char *path = file_spec.GetPath(path_buf, PATH_MAX) ? path_buf
132 : file_spec.GetFilename().AsCString();
Johnny Chen9262cd52012-08-22 00:18:43 +0000133 StreamString ss_m_uuid, ss_o_uuid;
134 uuid->Dump(&ss_m_uuid);
135 file_uuid.Dump(&ss_o_uuid);
Johnny Chencbf15912012-02-01 01:49:50 +0000136 Host::SystemLog (Host::eSystemLogWarning,
Johnny Chen9262cd52012-08-22 00:18:43 +0000137 "warning: UUID mismatch detected between binary (%s) and:\n\t'%s' (%s)\n",
138 ss_m_uuid.GetData(), path, ss_o_uuid.GetData());
Johnny Chencbf15912012-02-01 01:49:50 +0000139 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000140 }
141 data_offset = cmd_offset + cmd_size;
142 }
143 return false;
144}
145
146bool
147UniversalMachOFileContainsArchAndUUID
148(
149 const FileSpec &file_spec,
150 const ArchSpec *arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000151 const lldb_private::UUID *uuid,
Chris Lattner24943d22010-06-08 16:52:24 +0000152 off_t file_offset,
153 DataExtractor& data,
154 uint32_t data_offset,
155 const uint32_t magic
156)
157{
Greg Clayton1674b122010-07-21 22:12:05 +0000158 assert(magic == UniversalMagic || magic == UniversalMagicSwapped);
Chris Lattner24943d22010-06-08 16:52:24 +0000159
160 // Universal mach-o files always have their headers encoded as BIG endian
161 data.SetByteOrder(eByteOrderBig);
162
163 uint32_t i;
164 const uint32_t nfat_arch = data.GetU32(&data_offset); // number of structs that follow
165 const uint32_t fat_header_and_arch_size = sizeof(struct fat_header) + nfat_arch * sizeof(struct fat_arch);
166 if (data.GetByteSize() < fat_header_and_arch_size)
167 {
168 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, fat_header_and_arch_size));
169 data.SetData (data_buffer_sp);
170 }
171
172 for (i=0; i<nfat_arch; i++)
173 {
174 cpu_type_t arch_cputype = data.GetU32(&data_offset); // cpu specifier (int)
175 cpu_subtype_t arch_cpusubtype = data.GetU32(&data_offset); // machine specifier (int)
176 uint32_t arch_offset = data.GetU32(&data_offset); // file offset to this object file
177 // uint32_t arch_size = data.GetU32(&data_offset); // size of this object file
178 // uint32_t arch_align = data.GetU32(&data_offset); // alignment as a power of 2
179 data_offset += 8; // Skip size and align as we don't need those
180 // Only process this slice if the cpu type/subtype matches
181 if (arch)
182 {
Greg Claytoncf015052010-06-11 03:25:34 +0000183 ArchSpec fat_arch(eArchTypeMachO, arch_cputype, arch_cpusubtype);
Sean Callanan878f13c2012-12-14 23:43:03 +0000184 if (!fat_arch.IsCompatibleMatch(*arch))
Chris Lattner24943d22010-06-08 16:52:24 +0000185 continue;
186 }
187
188 // Create a buffer with only the arch slice date in it
189 DataExtractor arch_data;
190 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset + arch_offset, 0x1000));
191 arch_data.SetData(data_buffer_sp);
192 uint32_t arch_data_offset = 0;
193 uint32_t arch_magic = arch_data.GetU32(&arch_data_offset);
194
195 switch (arch_magic)
196 {
Greg Clayton1674b122010-07-21 22:12:05 +0000197 case HeaderMagic32:
198 case HeaderMagic32Swapped:
199 case HeaderMagic64:
200 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000201 if (SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset + arch_offset, arch_data, arch_data_offset, arch_magic))
202 return true;
203 break;
204 }
205 }
206 return false;
207}
208
209static bool
210FileAtPathContainsArchAndUUID
211(
212 const FileSpec &file_spec,
213 const ArchSpec *arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000214 const lldb_private::UUID *uuid
Chris Lattner24943d22010-06-08 16:52:24 +0000215)
216{
217 DataExtractor data;
218 off_t file_offset = 0;
219 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, 0x1000));
220
221 if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0)
222 {
223 data.SetData(data_buffer_sp);
224
225 uint32_t data_offset = 0;
226 uint32_t magic = data.GetU32(&data_offset);
227
228 switch (magic)
229 {
230 // 32 bit mach-o file
Greg Clayton1674b122010-07-21 22:12:05 +0000231 case HeaderMagic32:
232 case HeaderMagic32Swapped:
233 case HeaderMagic64:
234 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000235 return SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic);
236
237 // fat mach-o file
Greg Clayton1674b122010-07-21 22:12:05 +0000238 case UniversalMagic:
239 case UniversalMagicSwapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000240 return UniversalMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic);
241
242 default:
243 break;
244 }
245 }
246 return false;
247}
248
Greg Clayton95b765e2012-09-12 02:03:59 +0000249FileSpec
250Symbols::FindSymbolFileInBundle (const FileSpec& dsym_bundle_fspec,
251 const lldb_private::UUID *uuid,
252 const ArchSpec *arch)
Chris Lattner24943d22010-06-08 16:52:24 +0000253{
254 char path[PATH_MAX];
255
256 FileSpec dsym_fspec;
257
258 if (dsym_bundle_fspec.GetPath(path, sizeof(path)))
259 {
260 ::strncat (path, "/Contents/Resources/DWARF", sizeof(path) - strlen(path) - 1);
261
Greg Clayton52fd9842011-02-02 02:24:04 +0000262 lldb_utility::CleanUp <DIR *, int> dirp (opendir(path), NULL, closedir);
Greg Claytonad400272011-02-01 05:15:02 +0000263 if (dirp.is_valid())
Chris Lattner24943d22010-06-08 16:52:24 +0000264 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000265 dsym_fspec.GetDirectory().SetCString(path);
Chris Lattner24943d22010-06-08 16:52:24 +0000266 struct dirent* dp;
Greg Claytonad400272011-02-01 05:15:02 +0000267 while ((dp = readdir(dirp.get())) != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000268 {
269 // Only search directories
270 if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN)
271 {
272 if (dp->d_namlen == 1 && dp->d_name[0] == '.')
273 continue;
274
275 if (dp->d_namlen == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.')
276 continue;
277 }
278
279 if (dp->d_type == DT_REG || dp->d_type == DT_UNKNOWN)
280 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000281 dsym_fspec.GetFilename().SetCString(dp->d_name);
Chris Lattner24943d22010-06-08 16:52:24 +0000282 if (FileAtPathContainsArchAndUUID (dsym_fspec, arch, uuid))
283 return dsym_fspec;
284 }
285 }
286 }
287 }
288 dsym_fspec.Clear();
289 return dsym_fspec;
290}
291
292static int
293LocateMacOSXFilesUsingDebugSymbols
294(
Greg Clayton444fe992012-02-26 05:51:37 +0000295 const ModuleSpec &module_spec,
Chris Lattner24943d22010-06-08 16:52:24 +0000296 FileSpec *out_exec_fspec, // If non-NULL, try and find the executable
297 FileSpec *out_dsym_fspec // If non-NULL try and find the debug symbol file
298)
299{
300 int items_found = 0;
301
302 if (out_exec_fspec)
303 out_exec_fspec->Clear();
304
305 if (out_dsym_fspec)
306 out_dsym_fspec->Clear();
307
Greg Clayton3e4238d2011-11-04 03:34:56 +0000308#if !defined (__arm__) // No DebugSymbols on the iOS devices
309
Greg Clayton444fe992012-02-26 05:51:37 +0000310 const UUID *uuid = module_spec.GetUUIDPtr();
311 const ArchSpec *arch = module_spec.GetArchitecturePtr();
312
Chris Lattner24943d22010-06-08 16:52:24 +0000313 if (uuid && uuid->IsValid())
314 {
315 // Try and locate the dSYM file using DebugSymbols first
316 const UInt8 *module_uuid = (const UInt8 *)uuid->GetBytes();
317 if (module_uuid != NULL)
318 {
Greg Clayton0fa51242011-07-19 03:57:15 +0000319 CFCReleaser<CFUUIDRef> module_uuid_ref(::CFUUIDCreateWithBytes (NULL,
Chris Lattner24943d22010-06-08 16:52:24 +0000320 module_uuid[0],
321 module_uuid[1],
322 module_uuid[2],
323 module_uuid[3],
324 module_uuid[4],
325 module_uuid[5],
326 module_uuid[6],
327 module_uuid[7],
328 module_uuid[8],
329 module_uuid[9],
330 module_uuid[10],
331 module_uuid[11],
332 module_uuid[12],
333 module_uuid[13],
334 module_uuid[14],
335 module_uuid[15]));
336
337 if (module_uuid_ref.get())
338 {
339 CFCReleaser<CFURLRef> exec_url;
Greg Clayton444fe992012-02-26 05:51:37 +0000340 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000341 if (exec_fspec)
342 {
343 char exec_cf_path[PATH_MAX];
344 if (exec_fspec->GetPath(exec_cf_path, sizeof(exec_cf_path)))
345 exec_url.reset(::CFURLCreateFromFileSystemRepresentation (NULL,
346 (const UInt8 *)exec_cf_path,
347 strlen(exec_cf_path),
348 FALSE));
349 }
350
351 CFCReleaser<CFURLRef> dsym_url (::DBGCopyFullDSYMURLForUUID(module_uuid_ref.get(), exec_url.get()));
352 char path[PATH_MAX];
353
354 if (dsym_url.get())
355 {
356 if (out_dsym_fspec)
357 {
358 if (::CFURLGetFileSystemRepresentation (dsym_url.get(), true, (UInt8*)path, sizeof(path)-1))
359 {
Jason Molenda1b09dfe2012-10-05 04:57:34 +0000360 out_dsym_fspec->SetFile(path, path[0] == '~');
Chris Lattner24943d22010-06-08 16:52:24 +0000361
362 if (out_dsym_fspec->GetFileType () == FileSpec::eFileTypeDirectory)
363 {
Greg Clayton95b765e2012-09-12 02:03:59 +0000364 *out_dsym_fspec = Symbols::FindSymbolFileInBundle (*out_dsym_fspec, uuid, arch);
Chris Lattner24943d22010-06-08 16:52:24 +0000365 if (*out_dsym_fspec)
366 ++items_found;
367 }
368 else
369 {
370 ++items_found;
371 }
372 }
373 }
374
Greg Clayton964deba2012-03-15 21:01:31 +0000375 CFCReleaser<CFDictionaryRef> dict(::DBGCopyDSYMPropertyLists (dsym_url.get()));
376 CFDictionaryRef uuid_dict = NULL;
377 if (dict.get())
378 {
379 char uuid_cstr_buf[64];
380 const char *uuid_cstr = uuid->GetAsCString (uuid_cstr_buf, sizeof(uuid_cstr_buf));
381 CFCString uuid_cfstr (uuid_cstr);
Jason Molenda63e5cf62012-10-02 22:23:42 +0000382 uuid_dict = static_cast<CFDictionaryRef>(::CFDictionaryGetValue (dict.get(), uuid_cfstr.get()));
Greg Clayton964deba2012-03-15 21:01:31 +0000383 if (uuid_dict)
384 {
385
386 CFStringRef actual_src_cfpath = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGSourcePath")));
387 if (actual_src_cfpath)
388 {
389 CFStringRef build_src_cfpath = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGBuildSourcePath")));
390 if (build_src_cfpath)
391 {
392 char actual_src_path[PATH_MAX];
393 char build_src_path[PATH_MAX];
394 ::CFStringGetFileSystemRepresentation (actual_src_cfpath, actual_src_path, sizeof(actual_src_path));
395 ::CFStringGetFileSystemRepresentation (build_src_cfpath, build_src_path, sizeof(build_src_path));
Greg Clayton4d8c5432012-07-13 01:20:25 +0000396 if (actual_src_path[0] == '~')
397 {
398 FileSpec resolved_source_path(actual_src_path, true);
399 resolved_source_path.GetPath(actual_src_path, sizeof(actual_src_path));
400 }
Greg Clayton964deba2012-03-15 21:01:31 +0000401 module_spec.GetSourceMappingList().Append (ConstString(build_src_path), ConstString(actual_src_path), true);
402 }
403 }
404 }
405 }
406
Chris Lattner24943d22010-06-08 16:52:24 +0000407 if (out_exec_fspec)
408 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000409 bool success = false;
Greg Clayton964deba2012-03-15 21:01:31 +0000410 if (uuid_dict)
Chris Lattner24943d22010-06-08 16:52:24 +0000411 {
Greg Clayton964deba2012-03-15 21:01:31 +0000412 CFStringRef exec_cf_path = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGSymbolRichExecutable")));
413 if (exec_cf_path && ::CFStringGetFileSystemRepresentation (exec_cf_path, path, sizeof(path)))
Chris Lattner24943d22010-06-08 16:52:24 +0000414 {
Greg Clayton964deba2012-03-15 21:01:31 +0000415 ++items_found;
416 out_exec_fspec->SetFile(path, path[0] == '~');
417 if (out_exec_fspec->Exists())
418 success = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000419 }
420 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000421
422 if (!success)
Greg Clayton0fa51242011-07-19 03:57:15 +0000423 {
424 // No dictionary, check near the dSYM bundle for an executable that matches...
425 if (::CFURLGetFileSystemRepresentation (dsym_url.get(), true, (UInt8*)path, sizeof(path)-1))
426 {
427 char *dsym_extension_pos = ::strstr (path, ".dSYM");
428 if (dsym_extension_pos)
429 {
430 *dsym_extension_pos = '\0';
431 FileSpec file_spec (path, true);
432 switch (file_spec.GetFileType())
433 {
434 case FileSpec::eFileTypeDirectory: // Bundle directory?
435 {
436 CFCBundle bundle (path);
437 CFCReleaser<CFURLRef> bundle_exe_url (bundle.CopyExecutableURL ());
438 if (bundle_exe_url.get())
439 {
440 if (::CFURLGetFileSystemRepresentation (bundle_exe_url.get(), true, (UInt8*)path, sizeof(path)-1))
441 {
442 FileSpec bundle_exe_file_spec (path, true);
443
444 if (FileAtPathContainsArchAndUUID (bundle_exe_file_spec, arch, uuid))
445 {
446 ++items_found;
447 *out_exec_fspec = bundle_exe_file_spec;
448 }
449 }
450 }
451 }
452 break;
453
454 case FileSpec::eFileTypePipe: // Forget pipes
455 case FileSpec::eFileTypeSocket: // We can't process socket files
456 case FileSpec::eFileTypeInvalid: // File doesn't exist...
457 break;
458
459 case FileSpec::eFileTypeUnknown:
460 case FileSpec::eFileTypeRegular:
461 case FileSpec::eFileTypeSymbolicLink:
462 case FileSpec::eFileTypeOther:
463 if (FileAtPathContainsArchAndUUID (file_spec, arch, uuid))
464 {
465 ++items_found;
466 *out_exec_fspec = file_spec;
467 }
468 break;
469 }
470 }
471 }
472 }
Chris Lattner24943d22010-06-08 16:52:24 +0000473 }
474 }
475 }
476 }
477 }
Greg Clayton3e4238d2011-11-04 03:34:56 +0000478#endif // #if !defined (__arm__)
479
Chris Lattner24943d22010-06-08 16:52:24 +0000480 return items_found;
481}
482
483static bool
Greg Clayton444fe992012-02-26 05:51:37 +0000484LocateDSYMInVincinityOfExecutable (const ModuleSpec &module_spec, FileSpec &dsym_fspec)
Chris Lattner24943d22010-06-08 16:52:24 +0000485{
Greg Clayton444fe992012-02-26 05:51:37 +0000486 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000487 if (exec_fspec)
488 {
489 char path[PATH_MAX];
490 if (exec_fspec->GetPath(path, sizeof(path)))
491 {
492 // Make sure the module isn't already just a dSYM file...
493 if (strcasestr(path, ".dSYM/Contents/Resources/DWARF") == NULL)
494 {
495 size_t obj_file_path_length = strlen(path);
Filipe Cabecinhas78c180a2012-05-06 17:56:42 +0000496 strlcat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path));
497 strlcat(path, exec_fspec->GetFilename().AsCString(), sizeof(path));
Chris Lattner24943d22010-06-08 16:52:24 +0000498
Greg Clayton537a7a82010-10-20 20:54:39 +0000499 dsym_fspec.SetFile(path, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000500
Greg Clayton444fe992012-02-26 05:51:37 +0000501 if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr()))
Chris Lattner24943d22010-06-08 16:52:24 +0000502 {
503 return true;
504 }
505 else
506 {
507 path[obj_file_path_length] = '\0';
508
509 char *last_dot = strrchr(path, '.');
510 while (last_dot != NULL && last_dot[0])
511 {
512 char *next_slash = strchr(last_dot, '/');
513 if (next_slash != NULL)
514 {
515 *next_slash = '\0';
Filipe Cabecinhas78c180a2012-05-06 17:56:42 +0000516 strlcat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path));
517 strlcat(path, exec_fspec->GetFilename().AsCString(), sizeof(path));
Greg Clayton537a7a82010-10-20 20:54:39 +0000518 dsym_fspec.SetFile(path, false);
Greg Clayton444fe992012-02-26 05:51:37 +0000519 if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr()))
Chris Lattner24943d22010-06-08 16:52:24 +0000520 return true;
521 else
522 {
523 *last_dot = '\0';
524 char *prev_slash = strrchr(path, '/');
525 if (prev_slash != NULL)
526 *prev_slash = '\0';
527 else
528 break;
529 }
530 }
531 else
532 {
533 break;
534 }
535 }
536 }
537 }
538 }
539 }
540 dsym_fspec.Clear();
541 return false;
542}
543
544FileSpec
Greg Clayton444fe992012-02-26 05:51:37 +0000545Symbols::LocateExecutableObjectFile (const ModuleSpec &module_spec)
Chris Lattner24943d22010-06-08 16:52:24 +0000546{
Greg Clayton444fe992012-02-26 05:51:37 +0000547 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
548 const ArchSpec *arch = module_spec.GetArchitecturePtr();
549 const UUID *uuid = module_spec.GetUUIDPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000550 Timer scoped_timer (__PRETTY_FUNCTION__,
551 "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)",
552 exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>",
Greg Clayton940b1032011-02-23 00:35:02 +0000553 arch ? arch->GetArchitectureName() : "<NULL>",
Chris Lattner24943d22010-06-08 16:52:24 +0000554 uuid);
555
556 FileSpec objfile_fspec;
Greg Clayton444fe992012-02-26 05:51:37 +0000557 if (exec_fspec && FileAtPathContainsArchAndUUID (exec_fspec, arch, uuid))
558 objfile_fspec = exec_fspec;
Chris Lattner24943d22010-06-08 16:52:24 +0000559 else
Greg Clayton444fe992012-02-26 05:51:37 +0000560 LocateMacOSXFilesUsingDebugSymbols (module_spec, &objfile_fspec, NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000561 return objfile_fspec;
562}
563
564FileSpec
Greg Clayton444fe992012-02-26 05:51:37 +0000565Symbols::LocateExecutableSymbolFile (const ModuleSpec &module_spec)
Chris Lattner24943d22010-06-08 16:52:24 +0000566{
Greg Clayton444fe992012-02-26 05:51:37 +0000567 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
568 const ArchSpec *arch = module_spec.GetArchitecturePtr();
569 const UUID *uuid = module_spec.GetUUIDPtr();
570
Chris Lattner24943d22010-06-08 16:52:24 +0000571 Timer scoped_timer (__PRETTY_FUNCTION__,
572 "LocateExecutableSymbolFile (file = %s, arch = %s, uuid = %p)",
573 exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>",
Greg Clayton940b1032011-02-23 00:35:02 +0000574 arch ? arch->GetArchitectureName() : "<NULL>",
Chris Lattner24943d22010-06-08 16:52:24 +0000575 uuid);
576
577 FileSpec symbol_fspec;
578 // First try and find the dSYM in the same directory as the executable or in
579 // an appropriate parent directory
Greg Clayton444fe992012-02-26 05:51:37 +0000580 if (LocateDSYMInVincinityOfExecutable (module_spec, symbol_fspec) == false)
Chris Lattner24943d22010-06-08 16:52:24 +0000581 {
582 // We failed to easily find the dSYM above, so use DebugSymbols
Greg Clayton444fe992012-02-26 05:51:37 +0000583 LocateMacOSXFilesUsingDebugSymbols (module_spec, NULL, &symbol_fspec);
Chris Lattner24943d22010-06-08 16:52:24 +0000584 }
585 return symbol_fspec;
586}
Greg Claytonb924eb62012-09-27 03:13:55 +0000587
588
Greg Clayton437b5bc2012-09-27 22:26:11 +0000589static bool
590GetModuleSpecInfoFromUUIDDictionary (CFDictionaryRef uuid_dict, ModuleSpec &module_spec)
591{
592 bool success = false;
593 if (uuid_dict != NULL && CFGetTypeID (uuid_dict) == CFDictionaryGetTypeID ())
594 {
595 std::string str;
596 CFStringRef cf_str;
597
598 cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGSymbolRichExecutable"));
599 if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ())
600 {
601 if (CFCString::FileSystemRepresentation(cf_str, str))
602 module_spec.GetFileSpec().SetFile (str.c_str(), true);
603 }
604
605 cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGDSYMPath"));
606 if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ())
607 {
608 if (CFCString::FileSystemRepresentation(cf_str, str))
609 {
610 module_spec.GetSymbolFileSpec().SetFile (str.c_str(), true);
611 success = true;
612 }
613 }
614
615 cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGArchitecture"));
616 if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ())
617 {
618 if (CFCString::FileSystemRepresentation(cf_str, str))
619 module_spec.GetArchitecture().SetTriple(str.c_str());
620 }
621
622 std::string DBGBuildSourcePath;
623 std::string DBGSourcePath;
624
625 cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGBuildSourcePath"));
626 if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ())
627 {
628 CFCString::FileSystemRepresentation(cf_str, DBGBuildSourcePath);
629 }
630
631 cf_str = (CFStringRef)CFDictionaryGetValue ((CFDictionaryRef) uuid_dict, CFSTR("DBGSourcePath"));
632 if (cf_str && CFGetTypeID (cf_str) == CFStringGetTypeID ())
633 {
634 CFCString::FileSystemRepresentation(cf_str, DBGSourcePath);
635 }
636
637 if (!DBGBuildSourcePath.empty() && !DBGSourcePath.empty())
638 {
639 module_spec.GetSourceMappingList().Append (ConstString(DBGBuildSourcePath.c_str()), ConstString(DBGSourcePath.c_str()), true);
640 }
641 }
642 return success;
643}
Greg Claytonb924eb62012-09-27 03:13:55 +0000644
645
646bool
Jason Molendad6d45ce2012-10-09 01:17:11 +0000647Symbols::DownloadObjectAndSymbolFile (ModuleSpec &module_spec, bool force_lookup)
Greg Claytonb924eb62012-09-27 03:13:55 +0000648{
649 bool success = false;
650 const UUID *uuid_ptr = module_spec.GetUUIDPtr();
Greg Clayton437b5bc2012-09-27 22:26:11 +0000651 const FileSpec *file_spec_ptr = module_spec.GetFileSpecPtr();
Jason Molendad6d45ce2012-10-09 01:17:11 +0000652
653 // It's expensive to check for the DBGShellCommands defaults setting, only do it once per
654 // lldb run and cache the result.
655 static bool g_have_checked_for_dbgshell_command = false;
656 static const char *g_dbgshell_command = NULL;
657 if (g_have_checked_for_dbgshell_command == false)
658 {
659 g_have_checked_for_dbgshell_command = true;
660 CFTypeRef defaults_setting = CFPreferencesCopyAppValue (CFSTR ("DBGShellCommands"), CFSTR ("com.apple.DebugSymbols"));
661 if (defaults_setting && CFGetTypeID (defaults_setting) == CFStringGetTypeID())
662 {
663 char cstr_buf[PATH_MAX];
664 if (CFStringGetCString ((CFStringRef) defaults_setting, cstr_buf, sizeof (cstr_buf), kCFStringEncodingUTF8))
665 {
666 g_dbgshell_command = strdup (cstr_buf); // this malloc'ed memory will never be freed
667 }
668 }
669 if (defaults_setting)
670 {
671 CFRelease (defaults_setting);
672 }
673 }
674
675 // When g_dbgshell_command is NULL, the user has not enabled the use of an external program
676 // to find the symbols, don't run it for them.
677 if (force_lookup == false && g_dbgshell_command == NULL)
678 {
679 return false;
680 }
681
Greg Clayton437b5bc2012-09-27 22:26:11 +0000682 if (uuid_ptr || (file_spec_ptr && file_spec_ptr->Exists()))
Greg Claytonb924eb62012-09-27 03:13:55 +0000683 {
684 static bool g_located_dsym_for_uuid_exe = false;
685 static bool g_dsym_for_uuid_exe_exists = false;
686 static char g_dsym_for_uuid_exe_path[PATH_MAX];
687 if (!g_located_dsym_for_uuid_exe)
688 {
689 g_located_dsym_for_uuid_exe = true;
690 const char *dsym_for_uuid_exe_path_cstr = getenv("LLDB_APPLE_DSYMFORUUID_EXECUTABLE");
691 FileSpec dsym_for_uuid_exe_spec;
692 if (dsym_for_uuid_exe_path_cstr)
693 {
694 dsym_for_uuid_exe_spec.SetFile(dsym_for_uuid_exe_path_cstr, true);
695 g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists();
696 }
697
698 if (!g_dsym_for_uuid_exe_exists)
699 {
Jason Molendaf2569752012-10-30 21:26:30 +0000700 dsym_for_uuid_exe_spec.SetFile("/usr/local/bin/dsymForUUID", false);
Greg Claytonb924eb62012-09-27 03:13:55 +0000701 g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists();
702 if (!g_dsym_for_uuid_exe_exists)
703 {
Jason Molendaf2569752012-10-30 21:26:30 +0000704 int bufsize;
705 if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) != -1)
706 {
707 char buffer[bufsize];
708 struct passwd pwd;
709 struct passwd *tilde_rc = NULL;
710 // we are a library so we need to use the reentrant version of getpwnam()
711 if (getpwnam_r ("rc", &pwd, buffer, bufsize, &tilde_rc) == 0
712 && tilde_rc
713 && tilde_rc->pw_dir)
714 {
715 std::string dsymforuuid_path(tilde_rc->pw_dir);
716 dsymforuuid_path += "/bin/dsymForUUID";
717 dsym_for_uuid_exe_spec.SetFile(dsymforuuid_path.c_str(), false);
718 g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists();
719 }
720 }
Greg Claytonb924eb62012-09-27 03:13:55 +0000721 }
722 }
Jason Molendad6d45ce2012-10-09 01:17:11 +0000723 if (!g_dsym_for_uuid_exe_exists && g_dbgshell_command != NULL)
724 {
725 dsym_for_uuid_exe_spec.SetFile(g_dbgshell_command, true);
726 g_dsym_for_uuid_exe_exists = dsym_for_uuid_exe_spec.Exists();
727 }
728
Greg Claytonb924eb62012-09-27 03:13:55 +0000729 if (g_dsym_for_uuid_exe_exists)
730 dsym_for_uuid_exe_spec.GetPath (g_dsym_for_uuid_exe_path, sizeof(g_dsym_for_uuid_exe_path));
731 }
732 if (g_dsym_for_uuid_exe_exists)
733 {
Greg Claytonb924eb62012-09-27 03:13:55 +0000734 char uuid_cstr_buffer[64];
Greg Clayton437b5bc2012-09-27 22:26:11 +0000735 char file_path[PATH_MAX];
736 uuid_cstr_buffer[0] = '\0';
737 file_path[0] = '\0';
738 const char *uuid_cstr = NULL;
739
740 if (uuid_ptr)
741 uuid_cstr = uuid_ptr->GetAsCString(uuid_cstr_buffer, sizeof(uuid_cstr_buffer));
742
743 if (file_spec_ptr)
744 file_spec_ptr->GetPath(file_path, sizeof(file_path));
745
746 StreamString command;
747 if (uuid_cstr)
Jason Molenda63e5cf62012-10-02 22:23:42 +0000748 command.Printf("%s --ignoreNegativeCache --copyExecutable %s", g_dsym_for_uuid_exe_path, uuid_cstr);
Greg Clayton437b5bc2012-09-27 22:26:11 +0000749 else if (file_path && file_path[0])
Jason Molenda63e5cf62012-10-02 22:23:42 +0000750 command.Printf("%s --ignoreNegativeCache --copyExecutable %s", g_dsym_for_uuid_exe_path, file_path);
Greg Clayton437b5bc2012-09-27 22:26:11 +0000751
752 if (!command.GetString().empty())
Greg Claytonb924eb62012-09-27 03:13:55 +0000753 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000754 int exit_status = -1;
755 int signo = -1;
756 std::string command_output;
757 Error error = Host::RunShellCommand (command.GetData(),
758 NULL, // current working directory
759 &exit_status, // Exit status
760 &signo, // Signal int *
761 &command_output, // Command output
762 30, // Large timeout to allow for long dsym download times
763 NULL); // Don't run in a shell (we don't need shell expansion)
764 if (error.Success() && exit_status == 0 && !command_output.empty())
Greg Claytonb924eb62012-09-27 03:13:55 +0000765 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000766 CFCData data (CFDataCreateWithBytesNoCopy (NULL,
767 (const UInt8 *)command_output.data(),
768 command_output.size(),
769 kCFAllocatorNull));
770
771 CFCReleaser<CFDictionaryRef> plist((CFDictionaryRef)::CFPropertyListCreateFromXMLData (NULL, data.get(), kCFPropertyListImmutable, NULL));
772
773 if (CFGetTypeID (plist.get()) == CFDictionaryGetTypeID ())
Greg Claytonb924eb62012-09-27 03:13:55 +0000774 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000775 if (uuid_cstr)
Greg Claytonb924eb62012-09-27 03:13:55 +0000776 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000777 CFCString uuid_cfstr(uuid_cstr);
778 CFDictionaryRef uuid_dict = (CFDictionaryRef)CFDictionaryGetValue (plist.get(), uuid_cfstr.get());
779 success = GetModuleSpecInfoFromUUIDDictionary (uuid_dict, module_spec);
Greg Claytonb924eb62012-09-27 03:13:55 +0000780 }
Greg Clayton437b5bc2012-09-27 22:26:11 +0000781 else
Greg Claytonb924eb62012-09-27 03:13:55 +0000782 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000783 const CFIndex num_values = ::CFDictionaryGetCount(plist.get());
784 if (num_values > 0)
Greg Claytonb924eb62012-09-27 03:13:55 +0000785 {
Greg Clayton437b5bc2012-09-27 22:26:11 +0000786 std::vector<CFStringRef> keys (num_values, NULL);
787 std::vector<CFDictionaryRef> values (num_values, NULL);
788 ::CFDictionaryGetKeysAndValues(plist.get(), NULL, (const void **)&values[0]);
789 if (num_values == 1)
790 {
791 return GetModuleSpecInfoFromUUIDDictionary (values[0], module_spec);
792 }
793 else
794 {
795 for (CFIndex i=0; i<num_values; ++i)
796 {
797 ModuleSpec curr_module_spec;
798 if (GetModuleSpecInfoFromUUIDDictionary (values[i], curr_module_spec))
799 {
Sean Callanan40e278c2012-12-13 22:07:14 +0000800 if (module_spec.GetArchitecture().IsCompatibleMatch(curr_module_spec.GetArchitecture()))
Greg Clayton437b5bc2012-09-27 22:26:11 +0000801 {
802 module_spec = curr_module_spec;
803 return true;
804 }
805 }
806 }
807 }
Greg Claytonb924eb62012-09-27 03:13:55 +0000808 }
809 }
810 }
811 }
812 }
813 }
814 }
815 return success;
816}
817