blob: c49592eddc7861aaaf34335db6fc123325ae27c0 [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
Greg Clayton95b765e2012-09-12 02:03:59 +0000247FileSpec
248Symbols::FindSymbolFileInBundle (const FileSpec& dsym_bundle_fspec,
249 const lldb_private::UUID *uuid,
250 const ArchSpec *arch)
Chris Lattner24943d22010-06-08 16:52:24 +0000251{
252 char path[PATH_MAX];
253
254 FileSpec dsym_fspec;
255
256 if (dsym_bundle_fspec.GetPath(path, sizeof(path)))
257 {
258 ::strncat (path, "/Contents/Resources/DWARF", sizeof(path) - strlen(path) - 1);
259
Greg Clayton52fd9842011-02-02 02:24:04 +0000260 lldb_utility::CleanUp <DIR *, int> dirp (opendir(path), NULL, closedir);
Greg Claytonad400272011-02-01 05:15:02 +0000261 if (dirp.is_valid())
Chris Lattner24943d22010-06-08 16:52:24 +0000262 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000263 dsym_fspec.GetDirectory().SetCString(path);
Chris Lattner24943d22010-06-08 16:52:24 +0000264 struct dirent* dp;
Greg Claytonad400272011-02-01 05:15:02 +0000265 while ((dp = readdir(dirp.get())) != NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000266 {
267 // Only search directories
268 if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN)
269 {
270 if (dp->d_namlen == 1 && dp->d_name[0] == '.')
271 continue;
272
273 if (dp->d_namlen == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.')
274 continue;
275 }
276
277 if (dp->d_type == DT_REG || dp->d_type == DT_UNKNOWN)
278 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000279 dsym_fspec.GetFilename().SetCString(dp->d_name);
Chris Lattner24943d22010-06-08 16:52:24 +0000280 if (FileAtPathContainsArchAndUUID (dsym_fspec, arch, uuid))
281 return dsym_fspec;
282 }
283 }
284 }
285 }
286 dsym_fspec.Clear();
287 return dsym_fspec;
288}
289
290static int
291LocateMacOSXFilesUsingDebugSymbols
292(
Greg Clayton444fe992012-02-26 05:51:37 +0000293 const ModuleSpec &module_spec,
Chris Lattner24943d22010-06-08 16:52:24 +0000294 FileSpec *out_exec_fspec, // If non-NULL, try and find the executable
295 FileSpec *out_dsym_fspec // If non-NULL try and find the debug symbol file
296)
297{
298 int items_found = 0;
299
300 if (out_exec_fspec)
301 out_exec_fspec->Clear();
302
303 if (out_dsym_fspec)
304 out_dsym_fspec->Clear();
305
Greg Clayton3e4238d2011-11-04 03:34:56 +0000306#if !defined (__arm__) // No DebugSymbols on the iOS devices
307
Greg Clayton444fe992012-02-26 05:51:37 +0000308 const UUID *uuid = module_spec.GetUUIDPtr();
309 const ArchSpec *arch = module_spec.GetArchitecturePtr();
310
Chris Lattner24943d22010-06-08 16:52:24 +0000311 if (uuid && uuid->IsValid())
312 {
313 // Try and locate the dSYM file using DebugSymbols first
314 const UInt8 *module_uuid = (const UInt8 *)uuid->GetBytes();
315 if (module_uuid != NULL)
316 {
Greg Clayton0fa51242011-07-19 03:57:15 +0000317 CFCReleaser<CFUUIDRef> module_uuid_ref(::CFUUIDCreateWithBytes (NULL,
Chris Lattner24943d22010-06-08 16:52:24 +0000318 module_uuid[0],
319 module_uuid[1],
320 module_uuid[2],
321 module_uuid[3],
322 module_uuid[4],
323 module_uuid[5],
324 module_uuid[6],
325 module_uuid[7],
326 module_uuid[8],
327 module_uuid[9],
328 module_uuid[10],
329 module_uuid[11],
330 module_uuid[12],
331 module_uuid[13],
332 module_uuid[14],
333 module_uuid[15]));
334
335 if (module_uuid_ref.get())
336 {
337 CFCReleaser<CFURLRef> exec_url;
Greg Clayton444fe992012-02-26 05:51:37 +0000338 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000339 if (exec_fspec)
340 {
341 char exec_cf_path[PATH_MAX];
342 if (exec_fspec->GetPath(exec_cf_path, sizeof(exec_cf_path)))
343 exec_url.reset(::CFURLCreateFromFileSystemRepresentation (NULL,
344 (const UInt8 *)exec_cf_path,
345 strlen(exec_cf_path),
346 FALSE));
347 }
348
349 CFCReleaser<CFURLRef> dsym_url (::DBGCopyFullDSYMURLForUUID(module_uuid_ref.get(), exec_url.get()));
350 char path[PATH_MAX];
351
352 if (dsym_url.get())
353 {
354 if (out_dsym_fspec)
355 {
356 if (::CFURLGetFileSystemRepresentation (dsym_url.get(), true, (UInt8*)path, sizeof(path)-1))
357 {
Greg Clayton537a7a82010-10-20 20:54:39 +0000358 out_dsym_fspec->SetFile(path, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000359
360 if (out_dsym_fspec->GetFileType () == FileSpec::eFileTypeDirectory)
361 {
Greg Clayton95b765e2012-09-12 02:03:59 +0000362 *out_dsym_fspec = Symbols::FindSymbolFileInBundle (*out_dsym_fspec, uuid, arch);
Chris Lattner24943d22010-06-08 16:52:24 +0000363 if (*out_dsym_fspec)
364 ++items_found;
365 }
366 else
367 {
368 ++items_found;
369 }
370 }
371 }
372
Greg Clayton964deba2012-03-15 21:01:31 +0000373 CFCReleaser<CFDictionaryRef> dict(::DBGCopyDSYMPropertyLists (dsym_url.get()));
374 CFDictionaryRef uuid_dict = NULL;
375 if (dict.get())
376 {
377 char uuid_cstr_buf[64];
378 const char *uuid_cstr = uuid->GetAsCString (uuid_cstr_buf, sizeof(uuid_cstr_buf));
379 CFCString uuid_cfstr (uuid_cstr);
380 CFDictionaryRef uuid_dict = static_cast<CFDictionaryRef>(::CFDictionaryGetValue (dict.get(), uuid_cfstr.get()));
381 if (uuid_dict)
382 {
383
384 CFStringRef actual_src_cfpath = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGSourcePath")));
385 if (actual_src_cfpath)
386 {
387 CFStringRef build_src_cfpath = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGBuildSourcePath")));
388 if (build_src_cfpath)
389 {
390 char actual_src_path[PATH_MAX];
391 char build_src_path[PATH_MAX];
392 ::CFStringGetFileSystemRepresentation (actual_src_cfpath, actual_src_path, sizeof(actual_src_path));
393 ::CFStringGetFileSystemRepresentation (build_src_cfpath, build_src_path, sizeof(build_src_path));
Greg Clayton4d8c5432012-07-13 01:20:25 +0000394 if (actual_src_path[0] == '~')
395 {
396 FileSpec resolved_source_path(actual_src_path, true);
397 resolved_source_path.GetPath(actual_src_path, sizeof(actual_src_path));
398 }
Greg Clayton964deba2012-03-15 21:01:31 +0000399 module_spec.GetSourceMappingList().Append (ConstString(build_src_path), ConstString(actual_src_path), true);
400 }
401 }
402 }
403 }
404
Chris Lattner24943d22010-06-08 16:52:24 +0000405 if (out_exec_fspec)
406 {
Greg Clayton9ce95382012-02-13 23:10:39 +0000407 bool success = false;
Greg Clayton964deba2012-03-15 21:01:31 +0000408 if (uuid_dict)
Chris Lattner24943d22010-06-08 16:52:24 +0000409 {
Greg Clayton964deba2012-03-15 21:01:31 +0000410 CFStringRef exec_cf_path = static_cast<CFStringRef>(::CFDictionaryGetValue (uuid_dict, CFSTR("DBGSymbolRichExecutable")));
411 if (exec_cf_path && ::CFStringGetFileSystemRepresentation (exec_cf_path, path, sizeof(path)))
Chris Lattner24943d22010-06-08 16:52:24 +0000412 {
Greg Clayton964deba2012-03-15 21:01:31 +0000413 ++items_found;
414 out_exec_fspec->SetFile(path, path[0] == '~');
415 if (out_exec_fspec->Exists())
416 success = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000417 }
418 }
Greg Clayton9ce95382012-02-13 23:10:39 +0000419
420 if (!success)
Greg Clayton0fa51242011-07-19 03:57:15 +0000421 {
422 // No dictionary, check near the dSYM bundle for an executable that matches...
423 if (::CFURLGetFileSystemRepresentation (dsym_url.get(), true, (UInt8*)path, sizeof(path)-1))
424 {
425 char *dsym_extension_pos = ::strstr (path, ".dSYM");
426 if (dsym_extension_pos)
427 {
428 *dsym_extension_pos = '\0';
429 FileSpec file_spec (path, true);
430 switch (file_spec.GetFileType())
431 {
432 case FileSpec::eFileTypeDirectory: // Bundle directory?
433 {
434 CFCBundle bundle (path);
435 CFCReleaser<CFURLRef> bundle_exe_url (bundle.CopyExecutableURL ());
436 if (bundle_exe_url.get())
437 {
438 if (::CFURLGetFileSystemRepresentation (bundle_exe_url.get(), true, (UInt8*)path, sizeof(path)-1))
439 {
440 FileSpec bundle_exe_file_spec (path, true);
441
442 if (FileAtPathContainsArchAndUUID (bundle_exe_file_spec, arch, uuid))
443 {
444 ++items_found;
445 *out_exec_fspec = bundle_exe_file_spec;
446 }
447 }
448 }
449 }
450 break;
451
452 case FileSpec::eFileTypePipe: // Forget pipes
453 case FileSpec::eFileTypeSocket: // We can't process socket files
454 case FileSpec::eFileTypeInvalid: // File doesn't exist...
455 break;
456
457 case FileSpec::eFileTypeUnknown:
458 case FileSpec::eFileTypeRegular:
459 case FileSpec::eFileTypeSymbolicLink:
460 case FileSpec::eFileTypeOther:
461 if (FileAtPathContainsArchAndUUID (file_spec, arch, uuid))
462 {
463 ++items_found;
464 *out_exec_fspec = file_spec;
465 }
466 break;
467 }
468 }
469 }
470 }
Chris Lattner24943d22010-06-08 16:52:24 +0000471 }
472 }
473 }
474 }
475 }
Greg Clayton3e4238d2011-11-04 03:34:56 +0000476#endif // #if !defined (__arm__)
477
Chris Lattner24943d22010-06-08 16:52:24 +0000478 return items_found;
479}
480
481static bool
Greg Clayton444fe992012-02-26 05:51:37 +0000482LocateDSYMInVincinityOfExecutable (const ModuleSpec &module_spec, FileSpec &dsym_fspec)
Chris Lattner24943d22010-06-08 16:52:24 +0000483{
Greg Clayton444fe992012-02-26 05:51:37 +0000484 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000485 if (exec_fspec)
486 {
487 char path[PATH_MAX];
488 if (exec_fspec->GetPath(path, sizeof(path)))
489 {
490 // Make sure the module isn't already just a dSYM file...
491 if (strcasestr(path, ".dSYM/Contents/Resources/DWARF") == NULL)
492 {
493 size_t obj_file_path_length = strlen(path);
Filipe Cabecinhas78c180a2012-05-06 17:56:42 +0000494 strlcat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path));
495 strlcat(path, exec_fspec->GetFilename().AsCString(), sizeof(path));
Chris Lattner24943d22010-06-08 16:52:24 +0000496
Greg Clayton537a7a82010-10-20 20:54:39 +0000497 dsym_fspec.SetFile(path, false);
Chris Lattner24943d22010-06-08 16:52:24 +0000498
Greg Clayton444fe992012-02-26 05:51:37 +0000499 if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr()))
Chris Lattner24943d22010-06-08 16:52:24 +0000500 {
501 return true;
502 }
503 else
504 {
505 path[obj_file_path_length] = '\0';
506
507 char *last_dot = strrchr(path, '.');
508 while (last_dot != NULL && last_dot[0])
509 {
510 char *next_slash = strchr(last_dot, '/');
511 if (next_slash != NULL)
512 {
513 *next_slash = '\0';
Filipe Cabecinhas78c180a2012-05-06 17:56:42 +0000514 strlcat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path));
515 strlcat(path, exec_fspec->GetFilename().AsCString(), sizeof(path));
Greg Clayton537a7a82010-10-20 20:54:39 +0000516 dsym_fspec.SetFile(path, false);
Greg Clayton444fe992012-02-26 05:51:37 +0000517 if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr()))
Chris Lattner24943d22010-06-08 16:52:24 +0000518 return true;
519 else
520 {
521 *last_dot = '\0';
522 char *prev_slash = strrchr(path, '/');
523 if (prev_slash != NULL)
524 *prev_slash = '\0';
525 else
526 break;
527 }
528 }
529 else
530 {
531 break;
532 }
533 }
534 }
535 }
536 }
537 }
538 dsym_fspec.Clear();
539 return false;
540}
541
542FileSpec
Greg Clayton444fe992012-02-26 05:51:37 +0000543Symbols::LocateExecutableObjectFile (const ModuleSpec &module_spec)
Chris Lattner24943d22010-06-08 16:52:24 +0000544{
Greg Clayton444fe992012-02-26 05:51:37 +0000545 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
546 const ArchSpec *arch = module_spec.GetArchitecturePtr();
547 const UUID *uuid = module_spec.GetUUIDPtr();
Chris Lattner24943d22010-06-08 16:52:24 +0000548 Timer scoped_timer (__PRETTY_FUNCTION__,
549 "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)",
550 exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>",
Greg Clayton940b1032011-02-23 00:35:02 +0000551 arch ? arch->GetArchitectureName() : "<NULL>",
Chris Lattner24943d22010-06-08 16:52:24 +0000552 uuid);
553
554 FileSpec objfile_fspec;
Greg Clayton444fe992012-02-26 05:51:37 +0000555 if (exec_fspec && FileAtPathContainsArchAndUUID (exec_fspec, arch, uuid))
556 objfile_fspec = exec_fspec;
Chris Lattner24943d22010-06-08 16:52:24 +0000557 else
Greg Clayton444fe992012-02-26 05:51:37 +0000558 LocateMacOSXFilesUsingDebugSymbols (module_spec, &objfile_fspec, NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000559 return objfile_fspec;
560}
561
562FileSpec
Greg Clayton444fe992012-02-26 05:51:37 +0000563Symbols::LocateExecutableSymbolFile (const ModuleSpec &module_spec)
Chris Lattner24943d22010-06-08 16:52:24 +0000564{
Greg Clayton444fe992012-02-26 05:51:37 +0000565 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
566 const ArchSpec *arch = module_spec.GetArchitecturePtr();
567 const UUID *uuid = module_spec.GetUUIDPtr();
568
Chris Lattner24943d22010-06-08 16:52:24 +0000569 Timer scoped_timer (__PRETTY_FUNCTION__,
570 "LocateExecutableSymbolFile (file = %s, arch = %s, uuid = %p)",
571 exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>",
Greg Clayton940b1032011-02-23 00:35:02 +0000572 arch ? arch->GetArchitectureName() : "<NULL>",
Chris Lattner24943d22010-06-08 16:52:24 +0000573 uuid);
574
575 FileSpec symbol_fspec;
576 // First try and find the dSYM in the same directory as the executable or in
577 // an appropriate parent directory
Greg Clayton444fe992012-02-26 05:51:37 +0000578 if (LocateDSYMInVincinityOfExecutable (module_spec, symbol_fspec) == false)
Chris Lattner24943d22010-06-08 16:52:24 +0000579 {
580 // We failed to easily find the dSYM above, so use DebugSymbols
Greg Clayton444fe992012-02-26 05:51:37 +0000581 LocateMacOSXFilesUsingDebugSymbols (module_spec, NULL, &symbol_fspec);
Chris Lattner24943d22010-06-08 16:52:24 +0000582 }
583 return symbol_fspec;
584}