LLDB  mainline
SymbolVendorMacOSX.cpp
Go to the documentation of this file.
1 //===-- SymbolVendorMacOSX.cpp ----------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "SymbolVendorMacOSX.h"
10 
11 #include <string.h>
12 
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/ModuleSpec.h"
16 #include "lldb/Core/Section.h"
17 #include "lldb/Host/Host.h"
18 #include "lldb/Host/XML.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "lldb/Target/Target.h"
23 #include "lldb/Utility/Timer.h"
24 
25 using namespace lldb;
26 using namespace lldb_private;
27 
28 // SymbolVendorMacOSX constructor
29 SymbolVendorMacOSX::SymbolVendorMacOSX(const lldb::ModuleSP &module_sp)
30  : SymbolVendor(module_sp) {}
31 
32 // Destructor
34 
35 static bool UUIDsMatch(Module *module, ObjectFile *ofile,
36  lldb_private::Stream *feedback_strm) {
37  if (module && ofile) {
38  // Make sure the UUIDs match
39  lldb_private::UUID dsym_uuid = ofile->GetUUID();
40  if (!dsym_uuid) {
41  if (feedback_strm) {
42  feedback_strm->PutCString(
43  "warning: failed to get the uuid for object file: '");
44  ofile->GetFileSpec().Dump(feedback_strm);
45  feedback_strm->PutCString("\n");
46  }
47  return false;
48  }
49 
50  if (dsym_uuid == module->GetUUID())
51  return true;
52 
53  // Emit some warning messages since the UUIDs do not match!
54  if (feedback_strm) {
55  feedback_strm->PutCString(
56  "warning: UUID mismatch detected between modules:\n ");
57  module->GetUUID().Dump(feedback_strm);
58  feedback_strm->PutChar(' ');
59  module->GetFileSpec().Dump(feedback_strm);
60  feedback_strm->PutCString("\n ");
61  dsym_uuid.Dump(feedback_strm);
62  feedback_strm->PutChar(' ');
63  ofile->GetFileSpec().Dump(feedback_strm);
64  feedback_strm->EOL();
65  }
66  }
67  return false;
68 }
69 
71  PluginManager::RegisterPlugin(GetPluginNameStatic(),
73 }
74 
76  PluginManager::UnregisterPlugin(CreateInstance);
77 }
78 
80  static ConstString g_name("macosx");
81  return g_name;
82 }
83 
85  return "Symbol vendor for MacOSX that looks for dSYM files that match "
86  "executables.";
87 }
88 
89 // CreateInstance
90 //
91 // Platforms can register a callback to use when creating symbol vendors to
92 // allow for complex debug information file setups, and to also allow for
93 // finding separate debug information files.
95 SymbolVendorMacOSX::CreateInstance(const lldb::ModuleSP &module_sp,
96  lldb_private::Stream *feedback_strm) {
97  if (!module_sp)
98  return NULL;
99 
100  ObjectFile *obj_file = module_sp->GetObjectFile();
101  if (!obj_file)
102  return NULL;
103 
104  static ConstString obj_file_macho("mach-o");
105  ConstString obj_name = obj_file->GetPluginName();
106  if (obj_name != obj_file_macho)
107  return NULL;
108 
109  static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
110  Timer scoped_timer(func_cat,
111  "SymbolVendorMacOSX::CreateInstance (module = %s)",
112  module_sp->GetFileSpec().GetPath().c_str());
113  SymbolVendorMacOSX *symbol_vendor = new SymbolVendorMacOSX(module_sp);
114  if (symbol_vendor) {
115  char path[PATH_MAX];
116  path[0] = '\0';
117 
118  // Try and locate the dSYM file on Mac OS X
119  static Timer::Category func_cat2(
120  "SymbolVendorMacOSX::CreateInstance() locate dSYM");
121  Timer scoped_timer2(
122  func_cat2,
123  "SymbolVendorMacOSX::CreateInstance (module = %s) locate dSYM",
124  module_sp->GetFileSpec().GetPath().c_str());
125 
126  // First check to see if the module has a symbol file in mind already. If
127  // it does, then we MUST use that.
128  FileSpec dsym_fspec(module_sp->GetSymbolFileFileSpec());
129 
130  ObjectFileSP dsym_objfile_sp;
131  if (!dsym_fspec) {
132  // No symbol file was specified in the module, lets try and find one
133  // ourselves.
134  FileSpec file_spec = obj_file->GetFileSpec();
135  if (!file_spec)
136  file_spec = module_sp->GetFileSpec();
137 
138  ModuleSpec module_spec(file_spec, module_sp->GetArchitecture());
139  module_spec.GetUUID() = module_sp->GetUUID();
140  FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths();
141  dsym_fspec =
142  Symbols::LocateExecutableSymbolFile(module_spec, search_paths);
143  if (module_spec.GetSourceMappingList().GetSize())
144  module_sp->GetSourceMappingList().Append(
145  module_spec.GetSourceMappingList(), true);
146  }
147 
148  if (dsym_fspec) {
149  DataBufferSP dsym_file_data_sp;
150  lldb::offset_t dsym_file_data_offset = 0;
151  dsym_objfile_sp =
152  ObjectFile::FindPlugin(module_sp, &dsym_fspec, 0,
153  FileSystem::Instance().GetByteSize(dsym_fspec),
154  dsym_file_data_sp, dsym_file_data_offset);
155  if (UUIDsMatch(module_sp.get(), dsym_objfile_sp.get(), feedback_strm)) {
156  // We need a XML parser if we hope to parse a plist...
157  if (XMLDocument::XMLEnabled()) {
158  char dsym_path[PATH_MAX];
159  if (module_sp->GetSourceMappingList().IsEmpty() &&
160  dsym_fspec.GetPath(dsym_path, sizeof(dsym_path))) {
161  lldb_private::UUID dsym_uuid = dsym_objfile_sp->GetUUID();
162  if (dsym_uuid) {
163  std::string uuid_str = dsym_uuid.GetAsString();
164  if (!uuid_str.empty()) {
165  char *resources = strstr(dsym_path, "/Contents/Resources/");
166  if (resources) {
167  char dsym_uuid_plist_path[PATH_MAX];
168  resources[strlen("/Contents/Resources/")] = '\0';
169  snprintf(dsym_uuid_plist_path, sizeof(dsym_uuid_plist_path),
170  "%s%s.plist", dsym_path, uuid_str.c_str());
171  FileSpec dsym_uuid_plist_spec(dsym_uuid_plist_path);
172  if (FileSystem::Instance().Exists(dsym_uuid_plist_spec)) {
173  ApplePropertyList plist(dsym_uuid_plist_path);
174  if (plist) {
175  std::string DBGBuildSourcePath;
176  std::string DBGSourcePath;
177 
178  // DBGSourcePathRemapping is a dictionary in the plist
179  // with keys which are DBGBuildSourcePath file paths and
180  // values which are DBGSourcePath file paths
181 
182  StructuredData::ObjectSP plist_sp =
183  plist.GetStructuredData();
184  if (plist_sp.get() && plist_sp->GetAsDictionary() &&
185  plist_sp->GetAsDictionary()->HasKey(
186  "DBGSourcePathRemapping") &&
187  plist_sp->GetAsDictionary()
188  ->GetValueForKey("DBGSourcePathRemapping")
189  ->GetAsDictionary()) {
190 
191  // If DBGVersion 1 or DBGVersion missing, ignore DBGSourcePathRemapping.
192  // If DBGVersion 2, strip last two components of path remappings from
193  // entries to fix an issue with a specific set of
194  // DBGSourcePathRemapping entries that lldb worked
195  // with.
196  // If DBGVersion 3, trust & use the source path remappings as-is.
197  //
198 
199  bool new_style_source_remapping_dictionary = false;
200  bool do_truncate_remapping_names = false;
201  std::string original_DBGSourcePath_value =
202  DBGSourcePath;
203  if (plist_sp->GetAsDictionary()->HasKey("DBGVersion")) {
204  std::string version_string =
205  plist_sp->GetAsDictionary()
206  ->GetValueForKey("DBGVersion")
207  ->GetStringValue("");
208  if (!version_string.empty() &&
209  isdigit(version_string[0])) {
210  int version_number = atoi(version_string.c_str());
211  if (version_number > 1) {
212  new_style_source_remapping_dictionary = true;
213  }
214  if (version_number == 2) {
215  do_truncate_remapping_names = true;
216  }
217  }
218  }
219 
220  StructuredData::Dictionary *remappings_dict =
221  plist_sp->GetAsDictionary()
222  ->GetValueForKey("DBGSourcePathRemapping")
223  ->GetAsDictionary();
224  remappings_dict->ForEach(
225  [&module_sp, new_style_source_remapping_dictionary,
226  original_DBGSourcePath_value, do_truncate_remapping_names](
227  ConstString key,
228  StructuredData::Object *object) -> bool {
229  if (object && object->GetAsString()) {
230 
231  // key is DBGBuildSourcePath
232  // object is DBGSourcePath
233  std::string DBGSourcePath =
234  object->GetStringValue();
235  if (!new_style_source_remapping_dictionary &&
236  !original_DBGSourcePath_value.empty()) {
237  DBGSourcePath = original_DBGSourcePath_value;
238  }
239  if (DBGSourcePath[0] == '~') {
240  FileSpec resolved_source_path(
241  DBGSourcePath.c_str());
242  FileSystem::Instance().Resolve(
243  resolved_source_path);
244  DBGSourcePath =
245  resolved_source_path.GetPath();
246  }
247  module_sp->GetSourceMappingList().Append(
248  key, ConstString(DBGSourcePath), true);
249  // With version 2 of DBGSourcePathRemapping, we
250  // can chop off the last two filename parts
251  // from the source remapping and get a more
252  // general source remapping that still works.
253  // Add this as another option in addition to
254  // the full source path remap.
255  if (do_truncate_remapping_names) {
256  FileSpec build_path(key.AsCString());
257  FileSpec source_path(DBGSourcePath.c_str());
258  build_path.RemoveLastPathComponent();
259  build_path.RemoveLastPathComponent();
260  source_path.RemoveLastPathComponent();
261  source_path.RemoveLastPathComponent();
262  module_sp->GetSourceMappingList().Append(
263  ConstString(build_path.GetPath().c_str()),
264  ConstString(source_path.GetPath().c_str()), true);
265  }
266  }
267  return true;
268  });
269  }
270 
271  // If we have a DBGBuildSourcePath + DBGSourcePath pair,
272  // append those to the source path remappings.
273 
274  plist.GetValueAsString("DBGBuildSourcePath",
275  DBGBuildSourcePath);
276  plist.GetValueAsString("DBGSourcePath", DBGSourcePath);
277  if (!DBGBuildSourcePath.empty() &&
278  !DBGSourcePath.empty()) {
279  if (DBGSourcePath[0] == '~') {
280  FileSpec resolved_source_path(DBGSourcePath.c_str());
281  FileSystem::Instance().Resolve(resolved_source_path);
282  DBGSourcePath = resolved_source_path.GetPath();
283  }
284  module_sp->GetSourceMappingList().Append(
285  ConstString(DBGBuildSourcePath),
286  ConstString(DBGSourcePath), true);
287  }
288  }
289  }
290  }
291  }
292  }
293  }
294  }
295 
296  symbol_vendor->AddSymbolFileRepresentation(dsym_objfile_sp);
297  return symbol_vendor;
298  }
299  }
300 
301  // Just create our symbol vendor using the current objfile as this is
302  // either an executable with no dSYM (that we could locate), an executable
303  // with a dSYM that has a UUID that doesn't match.
304  symbol_vendor->AddSymbolFileRepresentation(obj_file->shared_from_this());
305  }
306  return symbol_vendor;
307 }
308 
309 // PluginInterface protocol
311  return GetPluginNameStatic();
312 }
313 
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:224
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
const FileSpec & GetFileSpec() const
Get const accessor for the module file specification.
Definition: Module.h:531
virtual FileSpec & GetFileSpec()
Get accessor to the object file specification.
Definition: ObjectFile.h:269
static bool UUIDsMatch(Module *module, ObjectFile *ofile, lldb_private::Stream *feedback_strm)
A file utility class.
Definition: FileSpec.h:55
A timer class that simplifies common timing metrics.
Definition: Timer.h:23
void ForEach(std::function< bool(ConstString key, Object *object)> const &callback) const
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:58
void Dump(Stream *s) const
Dump this object to a Stream.
Definition: FileSpec.cpp:350
virtual uint32_t GetPluginVersion()
std::string GetAsString(llvm::StringRef separator="-") const
Definition: UUID.cpp:38
uint64_t offset_t
Definition: lldb-types.h:87
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:127
A class that describes an executable image and its associated object and symbol files.
Definition: Module.h:109
SymbolVendorMacOSX(const lldb::ModuleSP &module_sp)
static lldb_private::ConstString GetPluginNameStatic()
ObjectSP GetValueForKey(llvm::StringRef key) const
virtual lldb_private::ConstString GetPluginName()
size_t PutChar(char ch)
Definition: Stream.cpp:103
A uniqued constant string class.
Definition: ConstString.h:38
virtual UUID GetUUID()=0
Gets the UUID for this object file.
Definition: SBAddress.h:15
StructuredData::ObjectSP GetStructuredData()
Definition: XML.cpp:533
std::shared_ptr< Object > ObjectSP
#define PATH_MAX
static lldb_private::SymbolVendor * CreateInstance(const lldb::ModuleSP &module_sp, lldb_private::Stream *feedback_strm)
void Dump(Stream *s) const
Definition: UUID.cpp:53
bool GetValueAsString(const char *key, std::string &value) const
Definition: XML.cpp:423
bool RemoveLastPathComponent()
Removes the last path component by replacing the current path with its parent.
Definition: FileSpec.cpp:474
virtual ConstString GetPluginName()=0
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:376
const lldb_private::UUID & GetUUID()
Get a reference to the UUID value contained in this object.
Definition: Module.cpp:330
static const char * GetPluginDescriptionStatic()