LLDB  mainline
SymbolVendorMacOSX.cpp
Go to the documentation of this file.
1 //===-- SymbolVendorMacOSX.cpp --------------------------------------------===//
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 
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/ModuleSpec.h"
17 #include "lldb/Core/Section.h"
18 #include "lldb/Host/Host.h"
19 #include "lldb/Host/XML.h"
21 #include "lldb/Symbol/ObjectFile.h"
22 #include "lldb/Target/Target.h"
25 #include "lldb/Utility/Timer.h"
26 
27 using namespace lldb;
28 using namespace lldb_private;
29 
31 
32 // SymbolVendorMacOSX constructor
33 SymbolVendorMacOSX::SymbolVendorMacOSX(const lldb::ModuleSP &module_sp)
34  : SymbolVendor(module_sp) {}
35 
36 // Destructor
38 
39 static bool UUIDsMatch(Module *module, ObjectFile *ofile,
40  lldb_private::Stream *feedback_strm) {
41  if (module && ofile) {
42  // Make sure the UUIDs match
43  lldb_private::UUID dsym_uuid = ofile->GetUUID();
44  if (!dsym_uuid) {
45  if (feedback_strm) {
46  feedback_strm->PutCString(
47  "warning: failed to get the uuid for object file: '");
48  ofile->GetFileSpec().Dump(feedback_strm->AsRawOstream());
49  feedback_strm->PutCString("\n");
50  }
51  return false;
52  }
53 
54  if (dsym_uuid == module->GetUUID())
55  return true;
56 
57  // Emit some warning messages since the UUIDs do not match!
58  if (feedback_strm) {
59  feedback_strm->PutCString(
60  "warning: UUID mismatch detected between modules:\n ");
61  module->GetUUID().Dump(feedback_strm);
62  feedback_strm->PutChar(' ');
63  module->GetFileSpec().Dump(feedback_strm->AsRawOstream());
64  feedback_strm->PutCString("\n ");
65  dsym_uuid.Dump(feedback_strm);
66  feedback_strm->PutChar(' ');
67  ofile->GetFileSpec().Dump(feedback_strm->AsRawOstream());
68  feedback_strm->EOL();
69  }
70  }
71  return false;
72 }
73 
75  PluginManager::RegisterPlugin(GetPluginNameStatic(),
76  GetPluginDescriptionStatic(), CreateInstance);
77 }
78 
80  PluginManager::UnregisterPlugin(CreateInstance);
81 }
82 
84  static ConstString g_name("macosx");
85  return g_name;
86 }
87 
89  return "Symbol vendor for MacOSX that looks for dSYM files that match "
90  "executables.";
91 }
92 
93 // CreateInstance
94 //
95 // Platforms can register a callback to use when creating symbol vendors to
96 // allow for complex debug information file setups, and to also allow for
97 // finding separate debug information files.
99 SymbolVendorMacOSX::CreateInstance(const lldb::ModuleSP &module_sp,
100  lldb_private::Stream *feedback_strm) {
101  if (!module_sp)
102  return NULL;
103 
104  ObjectFile *obj_file =
105  llvm::dyn_cast_or_null<ObjectFileMachO>(module_sp->GetObjectFile());
106  if (!obj_file)
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  // Compute dSYM root.
150  std::string dsym_root = dsym_fspec.GetPath();
151  const size_t pos = dsym_root.find("/Contents/Resources/");
152  dsym_root = pos != std::string::npos ? dsym_root.substr(0, pos) : "";
153 
154  DataBufferSP dsym_file_data_sp;
155  lldb::offset_t dsym_file_data_offset = 0;
156  dsym_objfile_sp =
157  ObjectFile::FindPlugin(module_sp, &dsym_fspec, 0,
158  FileSystem::Instance().GetByteSize(dsym_fspec),
159  dsym_file_data_sp, dsym_file_data_offset);
160  if (UUIDsMatch(module_sp.get(), dsym_objfile_sp.get(), feedback_strm)) {
161  // We need a XML parser if we hope to parse a plist...
162  if (XMLDocument::XMLEnabled()) {
163  if (module_sp->GetSourceMappingList().IsEmpty()) {
164  lldb_private::UUID dsym_uuid = dsym_objfile_sp->GetUUID();
165  if (dsym_uuid) {
166  std::string uuid_str = dsym_uuid.GetAsString();
167  if (!uuid_str.empty() && !dsym_root.empty()) {
168  char dsym_uuid_plist_path[PATH_MAX];
169  snprintf(dsym_uuid_plist_path, sizeof(dsym_uuid_plist_path),
170  "%s/Contents/Resources/%s.plist", dsym_root.c_str(),
171  uuid_str.c_str());
172  FileSpec dsym_uuid_plist_spec(dsym_uuid_plist_path);
173  if (FileSystem::Instance().Exists(dsym_uuid_plist_spec)) {
174  ApplePropertyList plist(dsym_uuid_plist_path);
175  if (plist) {
176  std::string DBGBuildSourcePath;
177  std::string DBGSourcePath;
178 
179  // DBGSourcePathRemapping is a dictionary in the plist
180  // with keys which are DBGBuildSourcePath file paths and
181  // values which are DBGSourcePath file paths
182 
183  StructuredData::ObjectSP plist_sp =
184  plist.GetStructuredData();
185  if (plist_sp.get() && plist_sp->GetAsDictionary() &&
186  plist_sp->GetAsDictionary()->HasKey(
187  "DBGSourcePathRemapping") &&
188  plist_sp->GetAsDictionary()
189  ->GetValueForKey("DBGSourcePathRemapping")
190  ->GetAsDictionary()) {
191 
192  // If DBGVersion 1 or DBGVersion missing, ignore
193  // DBGSourcePathRemapping. If DBGVersion 2, strip last two
194  // components of path remappings from
195  // entries to fix an issue with a
196  // specific set of DBGSourcePathRemapping
197  // entries that lldb worked with.
198  // If DBGVersion 3, trust & use the source path remappings
199  // as-is.
200  //
201 
202  bool new_style_source_remapping_dictionary = false;
203  bool do_truncate_remapping_names = false;
204  std::string original_DBGSourcePath_value = DBGSourcePath;
205  if (plist_sp->GetAsDictionary()->HasKey("DBGVersion")) {
206  std::string version_string =
207  std::string(plist_sp->GetAsDictionary()
208  ->GetValueForKey("DBGVersion")
209  ->GetStringValue(""));
210  if (!version_string.empty() &&
211  isdigit(version_string[0])) {
212  int version_number = atoi(version_string.c_str());
213  if (version_number > 1) {
214  new_style_source_remapping_dictionary = true;
215  }
216  if (version_number == 2) {
217  do_truncate_remapping_names = true;
218  }
219  }
220  }
221 
222  StructuredData::Dictionary *remappings_dict =
223  plist_sp->GetAsDictionary()
224  ->GetValueForKey("DBGSourcePathRemapping")
225  ->GetAsDictionary();
226  remappings_dict->ForEach(
227  [&module_sp, new_style_source_remapping_dictionary,
228  original_DBGSourcePath_value,
229  do_truncate_remapping_names](
230  ConstString key,
231  StructuredData::Object *object) -> bool {
232  if (object && object->GetAsString()) {
233 
234  // key is DBGBuildSourcePath
235  // object is DBGSourcePath
236  std::string DBGSourcePath =
237  std::string(object->GetStringValue());
238  if (!new_style_source_remapping_dictionary &&
239  !original_DBGSourcePath_value.empty()) {
240  DBGSourcePath = original_DBGSourcePath_value;
241  }
242  if (DBGSourcePath[0] == '~') {
243  FileSpec resolved_source_path(
244  DBGSourcePath.c_str());
245  FileSystem::Instance().Resolve(
246  resolved_source_path);
247  DBGSourcePath = resolved_source_path.GetPath();
248  }
249  module_sp->GetSourceMappingList().Append(
250  key, ConstString(DBGSourcePath), true);
251  // With version 2 of DBGSourcePathRemapping, we
252  // can chop off the last two filename parts
253  // from the source remapping and get a more
254  // general source remapping that still works.
255  // Add this as another option in addition to
256  // the full source path remap.
257  if (do_truncate_remapping_names) {
258  FileSpec build_path(key.AsCString());
259  FileSpec source_path(DBGSourcePath.c_str());
260  build_path.RemoveLastPathComponent();
261  build_path.RemoveLastPathComponent();
262  source_path.RemoveLastPathComponent();
263  source_path.RemoveLastPathComponent();
264  module_sp->GetSourceMappingList().Append(
265  ConstString(build_path.GetPath().c_str()),
266  ConstString(source_path.GetPath().c_str()),
267  true);
268  }
269  }
270  return true;
271  });
272  }
273 
274  // If we have a DBGBuildSourcePath + DBGSourcePath pair,
275  // append those to the source path remappings.
276 
277  plist.GetValueAsString("DBGBuildSourcePath",
278  DBGBuildSourcePath);
279  plist.GetValueAsString("DBGSourcePath", DBGSourcePath);
280  if (!DBGBuildSourcePath.empty() && !DBGSourcePath.empty()) {
281  if (DBGSourcePath[0] == '~') {
282  FileSpec resolved_source_path(DBGSourcePath.c_str());
283  FileSystem::Instance().Resolve(resolved_source_path);
284  DBGSourcePath = resolved_source_path.GetPath();
285  }
286  module_sp->GetSourceMappingList().Append(
287  ConstString(DBGBuildSourcePath),
288  ConstString(DBGSourcePath), true);
289  }
290  }
291  }
292  }
293  }
294  }
295  }
296 
297  symbol_vendor->AddSymbolFileRepresentation(dsym_objfile_sp);
298  if (!dsym_root.empty()) {
299  if (repro::Generator *g =
300  repro::Reproducer::Instance().GetGenerator()) {
301  repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>();
302  fp.recordInterestingDirectory(dsym_root);
303  }
304  }
305  return symbol_vendor;
306  }
307  }
308 
309  // Just create our symbol vendor using the current objfile as this is
310  // either an executable with no dSYM (that we could locate), an executable
311  // with a dSYM that has a UUID that doesn't match.
312  symbol_vendor->AddSymbolFileRepresentation(obj_file->shared_from_this());
313  }
314  return symbol_vendor;
315 }
316 
317 // PluginInterface protocol
319  return GetPluginNameStatic();
320 }
321 
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:357
A class that represents a running process on the host machine.
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:223
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:465
virtual FileSpec & GetFileSpec()
Get accessor to the object file specification.
Definition: ObjectFile.h:279
void recordInterestingDirectory(const llvm::Twine &dir)
Definition: Reproducer.cpp:302
static bool UUIDsMatch(Module *module, ObjectFile *ofile, lldb_private::Stream *feedback_strm)
A file utility class.
Definition: FileSpec.h:56
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
uint32_t GetPluginVersion() override
std::string GetAsString(llvm::StringRef separator="-") const
Definition: UUID.cpp:38
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
uint64_t offset_t
Definition: lldb-types.h:87
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:128
A class that describes an executable image and its associated object and symbol files.
Definition: Module.h:75
llvm::StringRef GetStringValue(const char *fail_value=nullptr)
static lldb_private::ConstString GetPluginNameStatic()
ObjectSP GetValueForKey(llvm::StringRef key) const
size_t PutChar(char ch)
Definition: Stream.cpp:104
void Dump(llvm::raw_ostream &s) const
Dump this object to a Stream.
Definition: FileSpec.cpp:324
A uniqued constant string class.
Definition: ConstString.h:40
The generator is responsible for the logic needed to generate a reproducer.
Definition: Reproducer.h:293
virtual UUID GetUUID()=0
Gets the UUID for this object file.
Definition: SBAddress.h:15
StructuredData::ObjectSP GetStructuredData()
Definition: XML.cpp:534
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:424
bool RemoveLastPathComponent()
Removes the last path component by replacing the current path with its parent.
Definition: FileSpec.cpp:446
#define NULL
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:348
lldb_private::ConstString GetPluginName() override
const lldb_private::UUID & GetUUID()
Get a reference to the UUID value contained in this object.
Definition: Module.cpp:347
static const char * GetPluginDescriptionStatic()