blob: eb7341d6dadb2f23479ff99c6a42a7bf9faf1fc8 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Symbols.cpp ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Host/Symbols.h"
11
12// C Includes
13#include <dirent.h>
Greg Clayton1674b122010-07-21 22:12:05 +000014#include "llvm/Support/MachO.h"
Chris Lattner24943d22010-06-08 16:52:24 +000015
16// C++ Includes
17// Other libraries and framework includes
18#include <CoreFoundation/CoreFoundation.h>
19
20// Project includes
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/ArchSpec.h"
22#include "lldb/Core/DataBuffer.h"
23#include "lldb/Core/DataExtractor.h"
Greg Clayton444fe992012-02-26 05:51:37 +000024#include "lldb/Core/Module.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000025#include "lldb/Core/ModuleSpec.h"
Johnny Chen9262cd52012-08-22 00:18:43 +000026#include "lldb/Core/StreamString.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Core/Timer.h"
28#include "lldb/Core/UUID.h"
Greg Claytoncd548032011-02-01 01:31:41 +000029#include "lldb/Host/Endian.h"
Johnny Chencbf15912012-02-01 01:49:50 +000030#include "lldb/Host/Host.h"
Greg Claytonad400272011-02-01 05:15:02 +000031#include "lldb/Utility/CleanUp.h"
Daniel Dunbara1ebbd22011-10-31 22:50:53 +000032#include "Host/macosx/cfcpp/CFCBundle.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000033#include "Host/macosx/cfcpp/CFCReleaser.h"
Greg Claytonb72d0f02011-04-12 05:54:46 +000034#include "Host/macosx/cfcpp/CFCString.h"
Chris Lattner5bc7b672010-09-08 23:01:14 +000035#include "mach/machine.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000036
Greg Clayton0fa51242011-07-19 03:57:15 +000037
Chris Lattner24943d22010-06-08 16:52:24 +000038using namespace lldb;
39using namespace lldb_private;
Greg Clayton1674b122010-07-21 22:12:05 +000040using namespace llvm::MachO;
Chris Lattner24943d22010-06-08 16:52:24 +000041
Greg Clayton3e4238d2011-11-04 03:34:56 +000042#if !defined (__arm__) // No DebugSymbols on the iOS devices
Chris Lattner24943d22010-06-08 16:52:24 +000043extern "C" {
Greg Clayton54e7afa2010-07-09 20:39:50 +000044
Chris Lattner24943d22010-06-08 16:52:24 +000045CFURLRef DBGCopyFullDSYMURLForUUID (CFUUIDRef uuid, CFURLRef exec_url);
46CFDictionaryRef DBGCopyDSYMPropertyLists (CFURLRef dsym_url);
Greg Clayton54e7afa2010-07-09 20:39:50 +000047
48}
Greg Clayton3e4238d2011-11-04 03:34:56 +000049#endif
Chris Lattner24943d22010-06-08 16:52:24 +000050
51static bool
52SkinnyMachOFileContainsArchAndUUID
53(
54 const FileSpec &file_spec,
55 const ArchSpec *arch,
Greg Clayton0467c782011-02-04 18:53:10 +000056 const lldb_private::UUID *uuid, // the UUID we are looking for
Chris Lattner24943d22010-06-08 16:52:24 +000057 off_t file_offset,
58 DataExtractor& data,
59 uint32_t data_offset,
60 const uint32_t magic
61)
62{
Greg Clayton1674b122010-07-21 22:12:05 +000063 assert(magic == HeaderMagic32 || magic == HeaderMagic32Swapped || magic == HeaderMagic64 || magic == HeaderMagic64Swapped);
64 if (magic == HeaderMagic32 || magic == HeaderMagic64)
Greg Claytoncd548032011-02-01 01:31:41 +000065 data.SetByteOrder (lldb::endian::InlHostByteOrder());
66 else if (lldb::endian::InlHostByteOrder() == eByteOrderBig)
Chris Lattner24943d22010-06-08 16:52:24 +000067 data.SetByteOrder (eByteOrderLittle);
68 else
69 data.SetByteOrder (eByteOrderBig);
70
71 uint32_t i;
72 const uint32_t cputype = data.GetU32(&data_offset); // cpu specifier
73 const uint32_t cpusubtype = data.GetU32(&data_offset); // machine specifier
74 data_offset+=4; // Skip mach file type
75 const uint32_t ncmds = data.GetU32(&data_offset); // number of load commands
76 const uint32_t sizeofcmds = data.GetU32(&data_offset); // the size of all the load commands
77 data_offset+=4; // Skip flags
78
79 // Check the architecture if we have a valid arch pointer
80 if (arch)
81 {
Greg Claytoncf015052010-06-11 03:25:34 +000082 ArchSpec file_arch(eArchTypeMachO, cputype, cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +000083
84 if (file_arch != *arch)
85 return false;
86 }
87
88 // The file exists, and if a valid arch pointer was passed in we know
89 // if already matches, so we can return if we aren't looking for a specific
90 // UUID
91 if (uuid == NULL)
92 return true;
93
Greg Clayton1674b122010-07-21 22:12:05 +000094 if (magic == HeaderMagic64Swapped || magic == HeaderMagic64)
Chris Lattner24943d22010-06-08 16:52:24 +000095 data_offset += 4; // Skip reserved field for in mach_header_64
96
97 // Make sure we have enough data for all the load commands
Greg Clayton1674b122010-07-21 22:12:05 +000098 if (magic == HeaderMagic64Swapped || magic == HeaderMagic64)
Chris Lattner24943d22010-06-08 16:52:24 +000099 {
100 if (data.GetByteSize() < sizeof(struct mach_header_64) + sizeofcmds)
101 {
102 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, sizeof(struct mach_header_64) + sizeofcmds));
103 data.SetData (data_buffer_sp);
104 }
105 }
106 else
107 {
108 if (data.GetByteSize() < sizeof(struct mach_header) + sizeofcmds)
109 {
110 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, sizeof(struct mach_header) + sizeofcmds));
111 data.SetData (data_buffer_sp);
112 }
113 }
114
115 for (i=0; i<ncmds; i++)
116 {
117 const uint32_t cmd_offset = data_offset; // Save this data_offset in case parsing of the segment goes awry!
118 uint32_t cmd = data.GetU32(&data_offset);
119 uint32_t cmd_size = data.GetU32(&data_offset);
Greg Clayton1674b122010-07-21 22:12:05 +0000120 if (cmd == LoadCommandUUID)
Chris Lattner24943d22010-06-08 16:52:24 +0000121 {
Greg Clayton0467c782011-02-04 18:53:10 +0000122 lldb_private::UUID file_uuid (data.GetData(&data_offset, 16), 16);
Johnny Chencbf15912012-02-01 01:49:50 +0000123 if (file_uuid == *uuid)
124 return true;
125
126 // Emit some warning messages since the UUIDs do not match!
127 char path_buf[PATH_MAX];
128 path_buf[0] = '\0';
129 const char *path = file_spec.GetPath(path_buf, PATH_MAX) ? path_buf
130 : file_spec.GetFilename().AsCString();
Johnny Chen9262cd52012-08-22 00:18:43 +0000131 StreamString ss_m_uuid, ss_o_uuid;
132 uuid->Dump(&ss_m_uuid);
133 file_uuid.Dump(&ss_o_uuid);
Johnny Chencbf15912012-02-01 01:49:50 +0000134 Host::SystemLog (Host::eSystemLogWarning,
Johnny Chen9262cd52012-08-22 00:18:43 +0000135 "warning: UUID mismatch detected between binary (%s) and:\n\t'%s' (%s)\n",
136 ss_m_uuid.GetData(), path, ss_o_uuid.GetData());
Johnny Chencbf15912012-02-01 01:49:50 +0000137 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000138 }
139 data_offset = cmd_offset + cmd_size;
140 }
141 return false;
142}
143
144bool
145UniversalMachOFileContainsArchAndUUID
146(
147 const FileSpec &file_spec,
148 const ArchSpec *arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000149 const lldb_private::UUID *uuid,
Chris Lattner24943d22010-06-08 16:52:24 +0000150 off_t file_offset,
151 DataExtractor& data,
152 uint32_t data_offset,
153 const uint32_t magic
154)
155{
Greg Clayton1674b122010-07-21 22:12:05 +0000156 assert(magic == UniversalMagic || magic == UniversalMagicSwapped);
Chris Lattner24943d22010-06-08 16:52:24 +0000157
158 // Universal mach-o files always have their headers encoded as BIG endian
159 data.SetByteOrder(eByteOrderBig);
160
161 uint32_t i;
162 const uint32_t nfat_arch = data.GetU32(&data_offset); // number of structs that follow
163 const uint32_t fat_header_and_arch_size = sizeof(struct fat_header) + nfat_arch * sizeof(struct fat_arch);
164 if (data.GetByteSize() < fat_header_and_arch_size)
165 {
166 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, fat_header_and_arch_size));
167 data.SetData (data_buffer_sp);
168 }
169
170 for (i=0; i<nfat_arch; i++)
171 {
172 cpu_type_t arch_cputype = data.GetU32(&data_offset); // cpu specifier (int)
173 cpu_subtype_t arch_cpusubtype = data.GetU32(&data_offset); // machine specifier (int)
174 uint32_t arch_offset = data.GetU32(&data_offset); // file offset to this object file
175 // uint32_t arch_size = data.GetU32(&data_offset); // size of this object file
176 // uint32_t arch_align = data.GetU32(&data_offset); // alignment as a power of 2
177 data_offset += 8; // Skip size and align as we don't need those
178 // Only process this slice if the cpu type/subtype matches
179 if (arch)
180 {
Greg Claytoncf015052010-06-11 03:25:34 +0000181 ArchSpec fat_arch(eArchTypeMachO, arch_cputype, arch_cpusubtype);
Chris Lattner24943d22010-06-08 16:52:24 +0000182 if (fat_arch != *arch)
183 continue;
184 }
185
186 // Create a buffer with only the arch slice date in it
187 DataExtractor arch_data;
188 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset + arch_offset, 0x1000));
189 arch_data.SetData(data_buffer_sp);
190 uint32_t arch_data_offset = 0;
191 uint32_t arch_magic = arch_data.GetU32(&arch_data_offset);
192
193 switch (arch_magic)
194 {
Greg Clayton1674b122010-07-21 22:12:05 +0000195 case HeaderMagic32:
196 case HeaderMagic32Swapped:
197 case HeaderMagic64:
198 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000199 if (SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset + arch_offset, arch_data, arch_data_offset, arch_magic))
200 return true;
201 break;
202 }
203 }
204 return false;
205}
206
207static bool
208FileAtPathContainsArchAndUUID
209(
210 const FileSpec &file_spec,
211 const ArchSpec *arch,
Greg Clayton0467c782011-02-04 18:53:10 +0000212 const lldb_private::UUID *uuid
Chris Lattner24943d22010-06-08 16:52:24 +0000213)
214{
215 DataExtractor data;
216 off_t file_offset = 0;
217 DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, 0x1000));
218
219 if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0)
220 {
221 data.SetData(data_buffer_sp);
222
223 uint32_t data_offset = 0;
224 uint32_t magic = data.GetU32(&data_offset);
225
226 switch (magic)
227 {
228 // 32 bit mach-o file
Greg Clayton1674b122010-07-21 22:12:05 +0000229 case HeaderMagic32:
230 case HeaderMagic32Swapped:
231 case HeaderMagic64:
232 case HeaderMagic64Swapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000233 return SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic);
234
235 // fat mach-o file
Greg Clayton1674b122010-07-21 22:12:05 +0000236 case UniversalMagic:
237 case UniversalMagicSwapped:
Chris Lattner24943d22010-06-08 16:52:24 +0000238 return UniversalMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic);
239
240 default:
241 break;
242 }
243 }
244 return false;
245}
246
247static FileSpec
248LocateDSYMMachFileInDSYMBundle
249(
250 const FileSpec& dsym_bundle_fspec,
Greg Clayton0467c782011-02-04 18:53:10 +0000251 const lldb_private::UUID *uuid,
Chris Lattner24943d22010-06-08 16:52:24 +0000252 const ArchSpec *arch)
253{
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 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000360 out_dsym_fspec->SetFile(path, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000361
362 if (out_dsym_fspec->GetFileType () == FileSpec::eFileTypeDirectory)
363 {
364 *out_dsym_fspec = LocateDSYMMachFileInDSYMBundle (*out_dsym_fspec, uuid, arch);
365 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);
382 CFDictionaryRef uuid_dict = static_cast<CFDictionaryRef>(::CFDictionaryGetValue (dict.get(), uuid_cfstr.get()));
383 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}