blob: bc93771fc60be0153501d1862778730719e89328 [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"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Core/Timer.h"
26#include "lldb/Core/UUID.h"
Greg Claytoncd548032011-02-01 01:31:41 +000027#include "lldb/Host/Endian.h"
Johnny Chencbf15912012-02-01 01:49:50 +000028#include "lldb/Host/Host.h"
Greg Claytonad400272011-02-01 05:15:02 +000029#include "lldb/Utility/CleanUp.h"
Daniel Dunbara1ebbd22011-10-31 22:50:53 +000030#include "Host/macosx/cfcpp/CFCBundle.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000031#include "Host/macosx/cfcpp/CFCReleaser.h"
Greg Claytonb72d0f02011-04-12 05:54:46 +000032#include "Host/macosx/cfcpp/CFCString.h"
Chris Lattner5bc7b672010-09-08 23:01:14 +000033#include "mach/machine.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000034
Greg Clayton0fa51242011-07-19 03:57:15 +000035
Chris Lattner24943d22010-06-08 16:52:24 +000036using namespace lldb;
37using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000038using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000039
Greg Clayton3e4238d2011-11-04 03:34:56 +000040#if !defined (__arm__) // No DebugSymbols on the iOS devices
Chris Lattner24943d22010-06-08 16:52:24 +000041extern "C" {
Greg Clayton54e7afa2010-07-09 20:39:50 +000042
Chris Lattner24943d22010-06-08 16:52:24 +000043CFURLRef DBGCopyFullDSYMURLForUUID (CFUUIDRef uuid, CFURLRef exec_url);
44CFDictionaryRef DBGCopyDSYMPropertyLists (CFURLRef dsym_url);
Greg Clayton54e7afa2010-07-09 20:39:50 +000045
46}
Greg Clayton3e4238d2011-11-04 03:34:56 +000047#endif
Chris Lattner24943d22010-06-08 16:52:24 +000048
49static bool
50SkinnyMachOFileContainsArchAndUUID
51(
52 const FileSpec &file_spec,
53 const ArchSpec *arch,
Greg Clayton0467c782011-02-04 18:53:10 +000054 const lldb_private::UUID *uuid, // the UUID we are looking for
Chris Lattner24943d22010-06-08 16:52:24 +000055 off_t file_offset,
56 DataExtractor& data,
57 uint32_t data_offset,
58 const uint32_t magic
59)
60{
Greg Clayton1674b122010-07-21 22:12:05 +000061 assert(magic == HeaderMagic32 || magic == HeaderMagic32Swapped || magic == HeaderMagic64 || magic == HeaderMagic64Swapped);
62 if (magic == HeaderMagic32 || magic == HeaderMagic64)
Greg Claytoncd548032011-02-01 01:31:41 +000063 data.SetByteOrder (lldb::endian::InlHostByteOrder());
64 else if (lldb::endian::InlHostByteOrder() == eByteOrderBig)
Chris Lattner24943d22010-06-08 16:52:24 +000065 data.SetByteOrder (eByteOrderLittle);
66 else
67 data.SetByteOrder (eByteOrderBig);
68
69 uint32_t i;
70 const uint32_t cputype = data.GetU32(&data_offset); // cpu specifier
71 const uint32_t cpusubtype = data.GetU32(&data_offset); // machine specifier
72 data_offset+=4; // Skip mach file type
73 const uint32_t ncmds = data.GetU32(&data_offset); // number of load commands
74 const uint32_t sizeofcmds = data.GetU32(&data_offset); // the size of all the load commands
75 data_offset+=4; // Skip flags
76
77 // Check the architecture if we have a valid arch pointer
78 if (arch)
79 {
Greg Claytoncf015052010-06-11 03:25:34 +000080 ArchSpec file_arch(eArchTypeMachO, cputype, cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +000081
82 if (file_arch != *arch)
83 return false;
84 }
85
86 // The file exists, and if a valid arch pointer was passed in we know
87 // if already matches, so we can return if we aren't looking for a specific
88 // UUID
89 if (uuid == NULL)
90 return true;
91
Greg Clayton1674b122010-07-21 22:12:05 +000092 if (magic == HeaderMagic64Swapped || magic == HeaderMagic64)
Chris Lattner24943d22010-06-08 16:52:24 +000093 data_offset += 4; // Skip reserved field for in mach_header_64
94
95 // Make sure we have enough data for all the load commands
Greg Clayton1674b122010-07-21 22:12:05 +000096 if (magic == HeaderMagic64Swapped || magic == HeaderMagic64)
Chris Lattner24943d22010-06-08 16:52:24 +000097 {
98 if (data.GetByteSize() < sizeof(struct mach_header_64) + sizeofcmds)
99 {
100 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, sizeof(struct mach_header_64) + sizeofcmds));
101 data.SetData (data_buffer_sp);
102 }
103 }
104 else
105 {
106 if (data.GetByteSize() < sizeof(struct mach_header) + sizeofcmds)
107 {
108 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, sizeof(struct mach_header) + sizeofcmds));
109 data.SetData (data_buffer_sp);
110 }
111 }
112
113 for (i=0; i<ncmds; i++)
114 {
115 const uint32_t cmd_offset = data_offset; // Save this data_offset in case parsing of the segment goes awry!
116 uint32_t cmd = data.GetU32(&data_offset);
117 uint32_t cmd_size = data.GetU32(&data_offset);
Greg Clayton1674b122010-07-21 22:12:05 +0000118 if (cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +0000119 {
Greg Clayton0467c782011-02-04 18:53:10 +0000120 lldb_private::UUID file_uuid (data.GetData(&data_offset, 16), 16);
Johnny Chencbf15912012-02-01 01:49:50 +0000121 if (file_uuid == *uuid)
122 return true;
123
124 // Emit some warning messages since the UUIDs do not match!
125 char path_buf[PATH_MAX];
126 path_buf[0] = '\0';
127 const char *path = file_spec.GetPath(path_buf, PATH_MAX) ? path_buf
128 : file_spec.GetFilename().AsCString();
129 Host::SystemLog (Host::eSystemLogWarning,
130 "warning: UUID mismatch detected between binary and:\n\t'%s'\n",
131 path);
132 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000133 }
134 data_offset = cmd_offset + cmd_size;
135 }
136 return false;
137}
138
139bool
140UniversalMachOFileContainsArchAndUUID
141(
142 const FileSpec &file_spec,
143 const ArchSpec *arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000144 const lldb_private::UUID *uuid,
Chris Lattner24943d22010-06-08 16:52:24 +0000145 off_t file_offset,
146 DataExtractor& data,
147 uint32_t data_offset,
148 const uint32_t magic
149)
150{
Greg Clayton1674b122010-07-21 22:12:05 +0000151 assert(magic == UniversalMagic || magic == UniversalMagicSwapped);
Chris Lattner24943d22010-06-08 16:52:24 +0000152
153 // Universal mach-o files always have their headers encoded as BIG endian
154 data.SetByteOrder(eByteOrderBig);
155
156 uint32_t i;
157 const uint32_t nfat_arch = data.GetU32(&data_offset); // number of structs that follow
158 const uint32_t fat_header_and_arch_size = sizeof(struct fat_header) + nfat_arch * sizeof(struct fat_arch);
159 if (data.GetByteSize() < fat_header_and_arch_size)
160 {
161 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, fat_header_and_arch_size));
162 data.SetData (data_buffer_sp);
163 }
164
165 for (i=0; i<nfat_arch; i++)
166 {
167 cpu_type_t arch_cputype = data.GetU32(&data_offset); // cpu specifier (int)
168 cpu_subtype_t arch_cpusubtype = data.GetU32(&data_offset); // machine specifier (int)
169 uint32_t arch_offset = data.GetU32(&data_offset); // file offset to this object file
170 // uint32_t arch_size = data.GetU32(&data_offset); // size of this object file
171 // uint32_t arch_align = data.GetU32(&data_offset); // alignment as a power of 2
172 data_offset += 8; // Skip size and align as we don't need those
173 // Only process this slice if the cpu type/subtype matches
174 if (arch)
175 {
Greg Claytoncf015052010-06-11 03:25:34 +0000176 ArchSpec fat_arch(eArchTypeMachO, arch_cputype, arch_cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +0000177 if (fat_arch != *arch)
178 continue;
179 }
180
181 // Create a buffer with only the arch slice date in it
182 DataExtractor arch_data;
183 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset + arch_offset, 0x1000));
184 arch_data.SetData(data_buffer_sp);
185 uint32_t arch_data_offset = 0;
186 uint32_t arch_magic = arch_data.GetU32(&arch_data_offset);
187
188 switch (arch_magic)
189 {
Greg Clayton1674b122010-07-21 22:12:05 +0000190 case HeaderMagic32:
191 case HeaderMagic32Swapped:
192 case HeaderMagic64:
193 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000194 if (SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset + arch_offset, arch_data, arch_data_offset, arch_magic))
195 return true;
196 break;
197 }
198 }
199 return false;
200}
201
202static bool
203FileAtPathContainsArchAndUUID
204(
205 const FileSpec &file_spec,
206 const ArchSpec *arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000207 const lldb_private::UUID *uuid
Chris Lattner24943d22010-06-08 16:52:24 +0000208)
209{
210 DataExtractor data;
211 off_t file_offset = 0;
212 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, 0x1000));
213
214 if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0)
215 {
216 data.SetData(data_buffer_sp);
217
218 uint32_t data_offset = 0;
219 uint32_t magic = data.GetU32(&data_offset);
220
221 switch (magic)
222 {
223 // 32 bit mach-o file
Greg Clayton1674b122010-07-21 22:12:05 +0000224 case HeaderMagic32:
225 case HeaderMagic32Swapped:
226 case HeaderMagic64:
227 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000228 return SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic);
229
230 // fat mach-o file
Greg Clayton1674b122010-07-21 22:12:05 +0000231 case UniversalMagic:
232 case UniversalMagicSwapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000233 return UniversalMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic);
234
235 default:
236 break;
237 }
238 }
239 return false;
240}
241
242static FileSpec
243LocateDSYMMachFileInDSYMBundle
244(
245 const FileSpec& dsym_bundle_fspec,
Greg Clayton0467c782011-02-04 18:53:10 +0000246 const lldb_private::UUID *uuid,
Chris Lattner24943d22010-06-08 16:52:24 +0000247 const ArchSpec *arch)
248{
249 char path[PATH_MAX];
250
251 FileSpec dsym_fspec;
252
253 if (dsym_bundle_fspec.GetPath(path, sizeof(path)))
254 {
255 ::strncat (path, "/Contents/Resources/DWARF", sizeof(path) - strlen(path) - 1);
256
Greg Clayton52fd9842011-02-02 02:24:04 +0000257 lldb_utility::CleanUp <DIR *, int> dirp (opendir(path), NULL, closedir);
Greg Claytonad400272011-02-01 05:15:02 +0000258 if (dirp.is_valid())
Chris Lattner24943d22010-06-08 16:52:24 +0000259 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000260 dsym_fspec.GetDirectory().SetCString(path);
Chris Lattner24943d22010-06-08 16:52:24 +0000261 struct dirent* dp;
Greg Claytonad400272011-02-01 05:15:02 +0000262 while ((dp = readdir(dirp.get())) != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000263 {
264 // Only search directories
265 if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN)
266 {
267 if (dp->d_namlen == 1 && dp->d_name[0] == '.')
268 continue;
269
270 if (dp->d_namlen == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.')
271 continue;
272 }
273
274 if (dp->d_type == DT_REG || dp->d_type == DT_UNKNOWN)
275 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000276 dsym_fspec.GetFilename().SetCString(dp->d_name);
Chris Lattner24943d22010-06-08 16:52:24 +0000277 if (FileAtPathContainsArchAndUUID (dsym_fspec, arch, uuid))
278 return dsym_fspec;
279 }
280 }
281 }
282 }
283 dsym_fspec.Clear();
284 return dsym_fspec;
285}
286
287static int
288LocateMacOSXFilesUsingDebugSymbols
289(
Greg Clayton444fe992012-02-26 05:51:37 +0000290 const ModuleSpec &module_spec,
Chris Lattner24943d22010-06-08 16:52:24 +0000291 FileSpec *out_exec_fspec, // If non-NULL, try and find the executable
292 FileSpec *out_dsym_fspec // If non-NULL try and find the debug symbol file
293)
294{
295 int items_found = 0;
296
297 if (out_exec_fspec)
298 out_exec_fspec->Clear();
299
300 if (out_dsym_fspec)
301 out_dsym_fspec->Clear();
302
Greg Clayton3e4238d2011-11-04 03:34:56 +0000303#if !defined (__arm__) // No DebugSymbols on the iOS devices
304
Greg Clayton444fe992012-02-26 05:51:37 +0000305 const UUID *uuid = module_spec.GetUUIDPtr();
306 const ArchSpec *arch = module_spec.GetArchitecturePtr();
307
Chris Lattner24943d22010-06-08 16:52:24 +0000308 if (uuid && uuid->IsValid())
309 {
310 // Try and locate the dSYM file using DebugSymbols first
311 const UInt8 *module_uuid = (const UInt8 *)uuid->GetBytes();
312 if (module_uuid != NULL)
313 {
Greg Clayton0fa51242011-07-19 03:57:15 +0000314 CFCReleaser<CFUUIDRef> module_uuid_ref(::CFUUIDCreateWithBytes (NULL,
Chris Lattner24943d22010-06-08 16:52:24 +0000315 module_uuid[0],
316 module_uuid[1],
317 module_uuid[2],
318 module_uuid[3],
319 module_uuid[4],
320 module_uuid[5],
321 module_uuid[6],
322 module_uuid[7],
323 module_uuid[8],
324 module_uuid[9],
325 module_uuid[10],
326 module_uuid[11],
327 module_uuid[12],
328 module_uuid[13],
329 module_uuid[14],
330 module_uuid[15]));
331
332 if (module_uuid_ref.get())
333 {
334 CFCReleaser<CFURLRef> exec_url;
Greg Clayton444fe992012-02-26 05:51:37 +0000335 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000336 if (exec_fspec)
337 {
338 char exec_cf_path[PATH_MAX];
339 if (exec_fspec->GetPath(exec_cf_path, sizeof(exec_cf_path)))
340 exec_url.reset(::CFURLCreateFromFileSystemRepresentation (NULL,
341 (const UInt8 *)exec_cf_path,
342 strlen(exec_cf_path),
343 FALSE));
344 }
345
346 CFCReleaser<CFURLRef> dsym_url (::DBGCopyFullDSYMURLForUUID(module_uuid_ref.get(), exec_url.get()));
347 char path[PATH_MAX];
348
349 if (dsym_url.get())
350 {
351 if (out_dsym_fspec)
352 {
353 if (::CFURLGetFileSystemRepresentation (dsym_url.get(), true, (UInt8*)path, sizeof(path)-1))
354 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000355 out_dsym_fspec->SetFile(path, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000356
357 if (out_dsym_fspec->GetFileType () == FileSpec::eFileTypeDirectory)
358 {
359 *out_dsym_fspec = LocateDSYMMachFileInDSYMBundle (*out_dsym_fspec, uuid, arch);
360 if (*out_dsym_fspec)
361 ++items_found;
362 }
363 else
364 {
365 ++items_found;
366 }
367 }
368 }
369
Greg Clayton964deba2012-03-15 21:01:31 +0000370 CFCReleaser<CFDictionaryRef> dict(::DBGCopyDSYMPropertyLists (dsym_url.get()));
371 CFDictionaryRef uuid_dict = NULL;
372 if (dict.get())
373 {
374 char uuid_cstr_buf[64];
375 const char *uuid_cstr = uuid->GetAsCString (uuid_cstr_buf, sizeof(uuid_cstr_buf));
376 CFCString uuid_cfstr (uuid_cstr);
377 CFDictionaryRef uuid_dict = static_cast<CFDictionaryRef>(::CFDictionaryGetValue (dict.get(), uuid_cfstr.get()));
378 if (uuid_dict)
379 {
380
381 CFStringRef actual_src_cfpath = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGSourcePath")));
382 if (actual_src_cfpath)
383 {
384 CFStringRef build_src_cfpath = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGBuildSourcePath")));
385 if (build_src_cfpath)
386 {
387 char actual_src_path[PATH_MAX];
388 char build_src_path[PATH_MAX];
389 ::CFStringGetFileSystemRepresentation (actual_src_cfpath, actual_src_path, sizeof(actual_src_path));
390 ::CFStringGetFileSystemRepresentation (build_src_cfpath, build_src_path, sizeof(build_src_path));
391 module_spec.GetSourceMappingList().Append (ConstString(build_src_path), ConstString(actual_src_path), true);
392 }
393 }
394 }
395 }
396
Chris Lattner24943d22010-06-08 16:52:24 +0000397 if (out_exec_fspec)
398 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000399 bool success = false;
Greg Clayton964deba2012-03-15 21:01:31 +0000400 if (uuid_dict)
Chris Lattner24943d22010-06-08 16:52:24 +0000401 {
Greg Clayton964deba2012-03-15 21:01:31 +0000402 CFStringRef exec_cf_path = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGSymbolRichExecutable")));
403 if (exec_cf_path && ::CFStringGetFileSystemRepresentation (exec_cf_path, path, sizeof(path)))
Chris Lattner24943d22010-06-08 16:52:24 +0000404 {
Greg Clayton964deba2012-03-15 21:01:31 +0000405 ++items_found;
406 out_exec_fspec->SetFile(path, path[0] == '~');
407 if (out_exec_fspec->Exists())
408 success = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000409 }
410 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000411
412 if (!success)
Greg Clayton0fa51242011-07-19 03:57:15 +0000413 {
414 // No dictionary, check near the dSYM bundle for an executable that matches...
415 if (::CFURLGetFileSystemRepresentation (dsym_url.get(), true, (UInt8*)path, sizeof(path)-1))
416 {
417 char *dsym_extension_pos = ::strstr (path, ".dSYM");
418 if (dsym_extension_pos)
419 {
420 *dsym_extension_pos = '\0';
421 FileSpec file_spec (path, true);
422 switch (file_spec.GetFileType())
423 {
424 case FileSpec::eFileTypeDirectory: // Bundle directory?
425 {
426 CFCBundle bundle (path);
427 CFCReleaser<CFURLRef> bundle_exe_url (bundle.CopyExecutableURL ());
428 if (bundle_exe_url.get())
429 {
430 if (::CFURLGetFileSystemRepresentation (bundle_exe_url.get(), true, (UInt8*)path, sizeof(path)-1))
431 {
432 FileSpec bundle_exe_file_spec (path, true);
433
434 if (FileAtPathContainsArchAndUUID (bundle_exe_file_spec, arch, uuid))
435 {
436 ++items_found;
437 *out_exec_fspec = bundle_exe_file_spec;
438 }
439 }
440 }
441 }
442 break;
443
444 case FileSpec::eFileTypePipe: // Forget pipes
445 case FileSpec::eFileTypeSocket: // We can't process socket files
446 case FileSpec::eFileTypeInvalid: // File doesn't exist...
447 break;
448
449 case FileSpec::eFileTypeUnknown:
450 case FileSpec::eFileTypeRegular:
451 case FileSpec::eFileTypeSymbolicLink:
452 case FileSpec::eFileTypeOther:
453 if (FileAtPathContainsArchAndUUID (file_spec, arch, uuid))
454 {
455 ++items_found;
456 *out_exec_fspec = file_spec;
457 }
458 break;
459 }
460 }
461 }
462 }
Chris Lattner24943d22010-06-08 16:52:24 +0000463 }
464 }
465 }
466 }
467 }
Greg Clayton3e4238d2011-11-04 03:34:56 +0000468#endif // #if !defined (__arm__)
469
Chris Lattner24943d22010-06-08 16:52:24 +0000470 return items_found;
471}
472
473static bool
Greg Clayton444fe992012-02-26 05:51:37 +0000474LocateDSYMInVincinityOfExecutable (const ModuleSpec &module_spec, FileSpec &dsym_fspec)
Chris Lattner24943d22010-06-08 16:52:24 +0000475{
Greg Clayton444fe992012-02-26 05:51:37 +0000476 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000477 if (exec_fspec)
478 {
479 char path[PATH_MAX];
480 if (exec_fspec->GetPath(path, sizeof(path)))
481 {
482 // Make sure the module isn't already just a dSYM file...
483 if (strcasestr(path, ".dSYM/Contents/Resources/DWARF") == NULL)
484 {
485 size_t obj_file_path_length = strlen(path);
Filipe Cabecinhas78c180a2012-05-06 17:56:42 +0000486 strlcat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path));
487 strlcat(path, exec_fspec->GetFilename().AsCString(), sizeof(path));
Chris Lattner24943d22010-06-08 16:52:24 +0000488
Greg Clayton537a7a82010-10-20 20:54:39 +0000489 dsym_fspec.SetFile(path, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000490
Greg Clayton444fe992012-02-26 05:51:37 +0000491 if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr()))
Chris Lattner24943d22010-06-08 16:52:24 +0000492 {
493 return true;
494 }
495 else
496 {
497 path[obj_file_path_length] = '\0';
498
499 char *last_dot = strrchr(path, '.');
500 while (last_dot != NULL && last_dot[0])
501 {
502 char *next_slash = strchr(last_dot, '/');
503 if (next_slash != NULL)
504 {
505 *next_slash = '\0';
Filipe Cabecinhas78c180a2012-05-06 17:56:42 +0000506 strlcat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path));
507 strlcat(path, exec_fspec->GetFilename().AsCString(), sizeof(path));
Greg Clayton537a7a82010-10-20 20:54:39 +0000508 dsym_fspec.SetFile(path, false);
Greg Clayton444fe992012-02-26 05:51:37 +0000509 if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr()))
Chris Lattner24943d22010-06-08 16:52:24 +0000510 return true;
511 else
512 {
513 *last_dot = '\0';
514 char *prev_slash = strrchr(path, '/');
515 if (prev_slash != NULL)
516 *prev_slash = '\0';
517 else
518 break;
519 }
520 }
521 else
522 {
523 break;
524 }
525 }
526 }
527 }
528 }
529 }
530 dsym_fspec.Clear();
531 return false;
532}
533
534FileSpec
Greg Clayton444fe992012-02-26 05:51:37 +0000535Symbols::LocateExecutableObjectFile (const ModuleSpec &module_spec)
Chris Lattner24943d22010-06-08 16:52:24 +0000536{
Greg Clayton444fe992012-02-26 05:51:37 +0000537 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
538 const ArchSpec *arch = module_spec.GetArchitecturePtr();
539 const UUID *uuid = module_spec.GetUUIDPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000540 Timer scoped_timer (__PRETTY_FUNCTION__,
541 "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)",
542 exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>",
Greg Clayton940b1032011-02-23 00:35:02 +0000543 arch ? arch->GetArchitectureName() : "<NULL>",
Chris Lattner24943d22010-06-08 16:52:24 +0000544 uuid);
545
546 FileSpec objfile_fspec;
Greg Clayton444fe992012-02-26 05:51:37 +0000547 if (exec_fspec && FileAtPathContainsArchAndUUID (exec_fspec, arch, uuid))
548 objfile_fspec = exec_fspec;
Chris Lattner24943d22010-06-08 16:52:24 +0000549 else
Greg Clayton444fe992012-02-26 05:51:37 +0000550 LocateMacOSXFilesUsingDebugSymbols (module_spec, &objfile_fspec, NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000551 return objfile_fspec;
552}
553
554FileSpec
Greg Clayton444fe992012-02-26 05:51:37 +0000555Symbols::LocateExecutableSymbolFile (const ModuleSpec &module_spec)
Chris Lattner24943d22010-06-08 16:52:24 +0000556{
Greg Clayton444fe992012-02-26 05:51:37 +0000557 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
558 const ArchSpec *arch = module_spec.GetArchitecturePtr();
559 const UUID *uuid = module_spec.GetUUIDPtr();
560
Chris Lattner24943d22010-06-08 16:52:24 +0000561 Timer scoped_timer (__PRETTY_FUNCTION__,
562 "LocateExecutableSymbolFile (file = %s, arch = %s, uuid = %p)",
563 exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>",
Greg Clayton940b1032011-02-23 00:35:02 +0000564 arch ? arch->GetArchitectureName() : "<NULL>",
Chris Lattner24943d22010-06-08 16:52:24 +0000565 uuid);
566
567 FileSpec symbol_fspec;
568 // First try and find the dSYM in the same directory as the executable or in
569 // an appropriate parent directory
Greg Clayton444fe992012-02-26 05:51:37 +0000570 if (LocateDSYMInVincinityOfExecutable (module_spec, symbol_fspec) == false)
Chris Lattner24943d22010-06-08 16:52:24 +0000571 {
572 // We failed to easily find the dSYM above, so use DebugSymbols
Greg Clayton444fe992012-02-26 05:51:37 +0000573 LocateMacOSXFilesUsingDebugSymbols (module_spec, NULL, &symbol_fspec);
Chris Lattner24943d22010-06-08 16:52:24 +0000574 }
575 return symbol_fspec;
576}