blob: efd8fbb47ff67de9e79bb476a4ec78a1600e22de [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
370 if (out_exec_fspec)
371 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000372 CFCReleaser<CFDictionaryRef> dict(::DBGCopyDSYMPropertyLists (dsym_url.get()));
373 bool success = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000374 if (dict.get())
375 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000376 char uuid_cstr_buf[64];
377 const char *uuid_cstr = uuid->GetAsCString (uuid_cstr_buf, sizeof(uuid_cstr_buf));
378 CFCString uuid_cfstr (uuid_cstr);
379 CFDictionaryRef uuid_dict = static_cast<CFDictionaryRef>(::CFDictionaryGetValue (dict.get(), uuid_cfstr.get()));
380 if (uuid_dict)
Chris Lattner24943d22010-06-08 16:52:24 +0000381 {
Greg Claytonb72d0f02011-04-12 05:54:46 +0000382 CFStringRef exec_cf_path = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGSymbolRichExecutable")));
383 if (exec_cf_path && ::CFStringGetFileSystemRepresentation (exec_cf_path, path, sizeof(path)))
384 {
385 ++items_found;
386 out_exec_fspec->SetFile(path, path[0] == '~');
Greg Clayton9ce95382012-02-13 23:10:39 +0000387 if (out_exec_fspec->Exists())
388 success = true;
Greg Claytonb72d0f02011-04-12 05:54:46 +0000389 }
Chris Lattner24943d22010-06-08 16:52:24 +0000390 }
391 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000392
393 if (!success)
Greg Clayton0fa51242011-07-19 03:57:15 +0000394 {
395 // No dictionary, check near the dSYM bundle for an executable that matches...
396 if (::CFURLGetFileSystemRepresentation (dsym_url.get(), true, (UInt8*)path, sizeof(path)-1))
397 {
398 char *dsym_extension_pos = ::strstr (path, ".dSYM");
399 if (dsym_extension_pos)
400 {
401 *dsym_extension_pos = '\0';
402 FileSpec file_spec (path, true);
403 switch (file_spec.GetFileType())
404 {
405 case FileSpec::eFileTypeDirectory: // Bundle directory?
406 {
407 CFCBundle bundle (path);
408 CFCReleaser<CFURLRef> bundle_exe_url (bundle.CopyExecutableURL ());
409 if (bundle_exe_url.get())
410 {
411 if (::CFURLGetFileSystemRepresentation (bundle_exe_url.get(), true, (UInt8*)path, sizeof(path)-1))
412 {
413 FileSpec bundle_exe_file_spec (path, true);
414
415 if (FileAtPathContainsArchAndUUID (bundle_exe_file_spec, arch, uuid))
416 {
417 ++items_found;
418 *out_exec_fspec = bundle_exe_file_spec;
419 }
420 }
421 }
422 }
423 break;
424
425 case FileSpec::eFileTypePipe: // Forget pipes
426 case FileSpec::eFileTypeSocket: // We can't process socket files
427 case FileSpec::eFileTypeInvalid: // File doesn't exist...
428 break;
429
430 case FileSpec::eFileTypeUnknown:
431 case FileSpec::eFileTypeRegular:
432 case FileSpec::eFileTypeSymbolicLink:
433 case FileSpec::eFileTypeOther:
434 if (FileAtPathContainsArchAndUUID (file_spec, arch, uuid))
435 {
436 ++items_found;
437 *out_exec_fspec = file_spec;
438 }
439 break;
440 }
441 }
442 }
443 }
Chris Lattner24943d22010-06-08 16:52:24 +0000444 }
445 }
446 }
447 }
448 }
Greg Clayton3e4238d2011-11-04 03:34:56 +0000449#endif // #if !defined (__arm__)
450
Chris Lattner24943d22010-06-08 16:52:24 +0000451 return items_found;
452}
453
454static bool
Greg Clayton444fe992012-02-26 05:51:37 +0000455LocateDSYMInVincinityOfExecutable (const ModuleSpec &module_spec, FileSpec &dsym_fspec)
Chris Lattner24943d22010-06-08 16:52:24 +0000456{
Greg Clayton444fe992012-02-26 05:51:37 +0000457 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000458 if (exec_fspec)
459 {
460 char path[PATH_MAX];
461 if (exec_fspec->GetPath(path, sizeof(path)))
462 {
463 // Make sure the module isn't already just a dSYM file...
464 if (strcasestr(path, ".dSYM/Contents/Resources/DWARF") == NULL)
465 {
466 size_t obj_file_path_length = strlen(path);
467 strncat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path));
468 strncat(path, exec_fspec->GetFilename().AsCString(), sizeof(path));
469
Greg Clayton537a7a82010-10-20 20:54:39 +0000470 dsym_fspec.SetFile(path, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000471
Greg Clayton444fe992012-02-26 05:51:37 +0000472 if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr()))
Chris Lattner24943d22010-06-08 16:52:24 +0000473 {
474 return true;
475 }
476 else
477 {
478 path[obj_file_path_length] = '\0';
479
480 char *last_dot = strrchr(path, '.');
481 while (last_dot != NULL && last_dot[0])
482 {
483 char *next_slash = strchr(last_dot, '/');
484 if (next_slash != NULL)
485 {
486 *next_slash = '\0';
487 strncat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path));
488 strncat(path, exec_fspec->GetFilename().AsCString(), sizeof(path));
Greg Clayton537a7a82010-10-20 20:54:39 +0000489 dsym_fspec.SetFile(path, false);
Greg Clayton444fe992012-02-26 05:51:37 +0000490 if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr()))
Chris Lattner24943d22010-06-08 16:52:24 +0000491 return true;
492 else
493 {
494 *last_dot = '\0';
495 char *prev_slash = strrchr(path, '/');
496 if (prev_slash != NULL)
497 *prev_slash = '\0';
498 else
499 break;
500 }
501 }
502 else
503 {
504 break;
505 }
506 }
507 }
508 }
509 }
510 }
511 dsym_fspec.Clear();
512 return false;
513}
514
515FileSpec
Greg Clayton444fe992012-02-26 05:51:37 +0000516Symbols::LocateExecutableObjectFile (const ModuleSpec &module_spec)
Chris Lattner24943d22010-06-08 16:52:24 +0000517{
Greg Clayton444fe992012-02-26 05:51:37 +0000518 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
519 const ArchSpec *arch = module_spec.GetArchitecturePtr();
520 const UUID *uuid = module_spec.GetUUIDPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000521 Timer scoped_timer (__PRETTY_FUNCTION__,
522 "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)",
523 exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>",
Greg Clayton940b1032011-02-23 00:35:02 +0000524 arch ? arch->GetArchitectureName() : "<NULL>",
Chris Lattner24943d22010-06-08 16:52:24 +0000525 uuid);
526
527 FileSpec objfile_fspec;
Greg Clayton444fe992012-02-26 05:51:37 +0000528 if (exec_fspec && FileAtPathContainsArchAndUUID (exec_fspec, arch, uuid))
529 objfile_fspec = exec_fspec;
Chris Lattner24943d22010-06-08 16:52:24 +0000530 else
Greg Clayton444fe992012-02-26 05:51:37 +0000531 LocateMacOSXFilesUsingDebugSymbols (module_spec, &objfile_fspec, NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000532 return objfile_fspec;
533}
534
535FileSpec
Greg Clayton444fe992012-02-26 05:51:37 +0000536Symbols::LocateExecutableSymbolFile (const ModuleSpec &module_spec)
Chris Lattner24943d22010-06-08 16:52:24 +0000537{
Greg Clayton444fe992012-02-26 05:51:37 +0000538 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
539 const ArchSpec *arch = module_spec.GetArchitecturePtr();
540 const UUID *uuid = module_spec.GetUUIDPtr();
541
Chris Lattner24943d22010-06-08 16:52:24 +0000542 Timer scoped_timer (__PRETTY_FUNCTION__,
543 "LocateExecutableSymbolFile (file = %s, arch = %s, uuid = %p)",
544 exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>",
Greg Clayton940b1032011-02-23 00:35:02 +0000545 arch ? arch->GetArchitectureName() : "<NULL>",
Chris Lattner24943d22010-06-08 16:52:24 +0000546 uuid);
547
548 FileSpec symbol_fspec;
549 // First try and find the dSYM in the same directory as the executable or in
550 // an appropriate parent directory
Greg Clayton444fe992012-02-26 05:51:37 +0000551 if (LocateDSYMInVincinityOfExecutable (module_spec, symbol_fspec) == false)
Chris Lattner24943d22010-06-08 16:52:24 +0000552 {
553 // We failed to easily find the dSYM above, so use DebugSymbols
Greg Clayton444fe992012-02-26 05:51:37 +0000554 LocateMacOSXFilesUsingDebugSymbols (module_spec, NULL, &symbol_fspec);
Chris Lattner24943d22010-06-08 16:52:24 +0000555 }
556 return symbol_fspec;
557}