1 //===-- SymbolVendorMacOSX.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 "SymbolVendorMacOSX.h"
11 
12 #include <string.h>
13 
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/ModuleSpec.h"
16 #include "lldb/Core/PluginManager.h"
17 #include "lldb/Core/Section.h"
18 #include "lldb/Core/StreamString.h"
19 #include "lldb/Core/Timer.h"
20 #include "lldb/Host/Host.h"
21 #include "lldb/Host/Symbols.h"
22 #include "lldb/Host/XML.h"
23 #include "lldb/Symbol/ObjectFile.h"
24 
25 using namespace lldb;
26 using namespace lldb_private;
27 
28 //----------------------------------------------------------------------
29 // SymbolVendorMacOSX constructor
30 //----------------------------------------------------------------------
31 SymbolVendorMacOSX::SymbolVendorMacOSX(const lldb::ModuleSP &module_sp) :
32     SymbolVendor (module_sp)
33 {
34 }
35 
36 //----------------------------------------------------------------------
37 // Destructor
38 //----------------------------------------------------------------------
39 SymbolVendorMacOSX::~SymbolVendorMacOSX()
40 {
41 }
42 
43 
44 static bool
45 UUIDsMatch(Module *module, ObjectFile *ofile, lldb_private::Stream *feedback_strm)
46 {
47     if (module && ofile)
48     {
49         // Make sure the UUIDs match
50         lldb_private::UUID dsym_uuid;
51 
52         if (!ofile->GetUUID(&dsym_uuid))
53         {
54             if (feedback_strm)
55             {
56                 feedback_strm->PutCString("warning: failed to get the uuid for object file: '");
57                 ofile->GetFileSpec().Dump(feedback_strm);
58                 feedback_strm->PutCString("\n");
59             }
60             return false;
61         }
62 
63         if (dsym_uuid == module->GetUUID())
64             return true;
65 
66         // Emit some warning messages since the UUIDs do not match!
67         if (feedback_strm)
68         {
69             feedback_strm->PutCString("warning: UUID mismatch detected between modules:\n    ");
70             module->GetUUID().Dump(feedback_strm);
71             feedback_strm->PutChar(' ');
72             module->GetFileSpec().Dump(feedback_strm);
73             feedback_strm->PutCString("\n    ");
74             dsym_uuid.Dump(feedback_strm);
75             feedback_strm->PutChar(' ');
76             ofile->GetFileSpec().Dump(feedback_strm);
77             feedback_strm->EOL();
78         }
79     }
80     return false;
81 }
82 
83 void
84 SymbolVendorMacOSX::Initialize()
85 {
86     PluginManager::RegisterPlugin (GetPluginNameStatic(),
87                                    GetPluginDescriptionStatic(),
88                                    CreateInstance);
89 }
90 
91 void
92 SymbolVendorMacOSX::Terminate()
93 {
94     PluginManager::UnregisterPlugin (CreateInstance);
95 }
96 
97 
98 lldb_private::ConstString
99 SymbolVendorMacOSX::GetPluginNameStatic()
100 {
101     static ConstString g_name("macosx");
102     return g_name;
103 }
104 
105 const char *
106 SymbolVendorMacOSX::GetPluginDescriptionStatic()
107 {
108     return "Symbol vendor for MacOSX that looks for dSYM files that match executables.";
109 }
110 
111 
112 
113 //----------------------------------------------------------------------
114 // CreateInstance
115 //
116 // Platforms can register a callback to use when creating symbol
117 // vendors to allow for complex debug information file setups, and to
118 // also allow for finding separate debug information files.
119 //----------------------------------------------------------------------
120 SymbolVendor*
121 SymbolVendorMacOSX::CreateInstance (const lldb::ModuleSP &module_sp, lldb_private::Stream *feedback_strm)
122 {
123     if (!module_sp)
124         return NULL;
125 
126     ObjectFile * obj_file = module_sp->GetObjectFile();
127     if (!obj_file)
128         return NULL;
129 
130     static ConstString obj_file_macho("mach-o");
131     ConstString obj_name = obj_file->GetPluginName();
132     if (obj_name != obj_file_macho)
133         return NULL;
134 
135     Timer scoped_timer (__PRETTY_FUNCTION__,
136                         "SymbolVendorMacOSX::CreateInstance (module = %s)",
137                         module_sp->GetFileSpec().GetPath().c_str());
138     SymbolVendorMacOSX* symbol_vendor = new SymbolVendorMacOSX(module_sp);
139     if (symbol_vendor)
140     {
141         char path[PATH_MAX];
142         path[0] = '\0';
143 
144         // Try and locate the dSYM file on Mac OS X
145         Timer scoped_timer2 ("SymbolVendorMacOSX::CreateInstance () locate dSYM",
146                              "SymbolVendorMacOSX::CreateInstance (module = %s) locate dSYM",
147                              module_sp->GetFileSpec().GetPath().c_str());
148 
149         // First check to see if the module has a symbol file in mind already.
150         // If it does, then we MUST use that.
151         FileSpec dsym_fspec (module_sp->GetSymbolFileFileSpec());
152 
153         ObjectFileSP dsym_objfile_sp;
154         if (!dsym_fspec)
155         {
156             // No symbol file was specified in the module, lets try and find
157             // one ourselves.
158             FileSpec file_spec = obj_file->GetFileSpec();
159             if (!file_spec)
160                 file_spec = module_sp->GetFileSpec();
161 
162             ModuleSpec module_spec(file_spec, module_sp->GetArchitecture());
163             module_spec.GetUUID() = module_sp->GetUUID();
164             dsym_fspec = Symbols::LocateExecutableSymbolFile (module_spec);
165             if (module_spec.GetSourceMappingList().GetSize())
166                 module_sp->GetSourceMappingList().Append (module_spec.GetSourceMappingList (), true);
167         }
168 
169         if (dsym_fspec)
170         {
171             DataBufferSP dsym_file_data_sp;
172             lldb::offset_t dsym_file_data_offset = 0;
173             dsym_objfile_sp = ObjectFile::FindPlugin(module_sp, &dsym_fspec, 0, dsym_fspec.GetByteSize(), dsym_file_data_sp, dsym_file_data_offset);
174             if (UUIDsMatch(module_sp.get(), dsym_objfile_sp.get(), feedback_strm))
175             {
176                 // We need a XML parser if we hope to parse a plist...
177                 if (XMLDocument::XMLEnabled())
178                 {
179                     char dsym_path[PATH_MAX];
180                     if (module_sp->GetSourceMappingList().IsEmpty() && dsym_fspec.GetPath(dsym_path, sizeof(dsym_path)))
181                     {
182                         lldb_private::UUID dsym_uuid;
183                         if (dsym_objfile_sp->GetUUID(&dsym_uuid))
184                         {
185                             std::string uuid_str = dsym_uuid.GetAsString ();
186                             if (!uuid_str.empty())
187                             {
188                                 char *resources = strstr (dsym_path, "/Contents/Resources/");
189                                 if (resources)
190                                 {
191                                     char dsym_uuid_plist_path[PATH_MAX];
192                                     resources[strlen("/Contents/Resources/")] = '\0';
193                                     snprintf(dsym_uuid_plist_path, sizeof(dsym_uuid_plist_path), "%s%s.plist", dsym_path, uuid_str.c_str());
194                                     FileSpec dsym_uuid_plist_spec(dsym_uuid_plist_path, false);
195                                     if (dsym_uuid_plist_spec.Exists())
196                                     {
197                                         ApplePropertyList plist(dsym_uuid_plist_path);
198                                         if (plist)
199                                         {
200                                             std::string DBGBuildSourcePath;
201                                             std::string DBGSourcePath;
202 
203                                             plist.GetValueAsString("DBGBuildSourcePath", DBGBuildSourcePath);
204                                             plist.GetValueAsString("DBGSourcePath", DBGSourcePath);
205                                             if (!DBGBuildSourcePath.empty() && !DBGSourcePath.empty())
206                                             {
207                                                 if (DBGSourcePath[0] == '~')
208                                                 {
209                                                     FileSpec resolved_source_path(DBGSourcePath.c_str(), true);
210                                                     DBGSourcePath = resolved_source_path.GetPath();
211                                                 }
212                                                 module_sp->GetSourceMappingList().Append (ConstString(DBGBuildSourcePath), ConstString(DBGSourcePath), true);
213                                             }
214                                         }
215                                     }
216                                 }
217                             }
218                         }
219                     }
220                 }
221 
222                 symbol_vendor->AddSymbolFileRepresentation(dsym_objfile_sp);
223                 return symbol_vendor;
224             }
225         }
226 
227         // Just create our symbol vendor using the current objfile as this is either
228         // an executable with no dSYM (that we could locate), an executable with
229         // a dSYM that has a UUID that doesn't match.
230         symbol_vendor->AddSymbolFileRepresentation(obj_file->shared_from_this());
231     }
232     return symbol_vendor;
233 }
234 
235 
236 
237 //------------------------------------------------------------------
238 // PluginInterface protocol
239 //------------------------------------------------------------------
240 ConstString
241 SymbolVendorMacOSX::GetPluginName()
242 {
243     return GetPluginNameStatic();
244 }
245 
246 uint32_t
247 SymbolVendorMacOSX::GetPluginVersion()
248 {
249     return 1;
250 }
251 
252