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 <cstring>
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 static bool UUIDsMatch(Module *module, ObjectFile *ofile,
37  lldb_private::Stream *feedback_strm) {
38  if (module && ofile) {
39  // Make sure the UUIDs match
40  lldb_private::UUID dsym_uuid = ofile->GetUUID();
41  if (!dsym_uuid) {
42  if (feedback_strm) {
43  feedback_strm->PutCString(
44  "warning: failed to get the uuid for object file: '");
45  ofile->GetFileSpec().Dump(feedback_strm->AsRawOstream());
46  feedback_strm->PutCString("\n");
47  }
48  return false;
49  }
50 
51  if (dsym_uuid == module->GetUUID())
52  return true;
53 
54  // Emit some warning messages since the UUIDs do not match!
55  if (feedback_strm) {
56  feedback_strm->PutCString(
57  "warning: UUID mismatch detected between modules:\n ");
58  module->GetUUID().Dump(feedback_strm);
59  feedback_strm->PutChar(' ');
60  module->GetFileSpec().Dump(feedback_strm->AsRawOstream());
61  feedback_strm->PutCString("\n ");
62  dsym_uuid.Dump(feedback_strm);
63  feedback_strm->PutChar(' ');
64  ofile->GetFileSpec().Dump(feedback_strm->AsRawOstream());
65  feedback_strm->EOL();
66  }
67  }
68  return false;
69 }
70 
72  PluginManager::RegisterPlugin(GetPluginNameStatic(),
73  GetPluginDescriptionStatic(), CreateInstance);
74 }
75 
77  PluginManager::UnregisterPlugin(CreateInstance);
78 }
79 
81  return "Symbol vendor for MacOSX that looks for dSYM files that match "
82  "executables.";
83 }
84 
85 // CreateInstance
86 //
87 // Platforms can register a callback to use when creating symbol vendors to
88 // allow for complex debug information file setups, and to also allow for
89 // finding separate debug information files.
91 SymbolVendorMacOSX::CreateInstance(const lldb::ModuleSP &module_sp,
92  lldb_private::Stream *feedback_strm) {
93  if (!module_sp)
94  return NULL;
95 
96  ObjectFile *obj_file =
97  llvm::dyn_cast_or_null<ObjectFileMachO>(module_sp->GetObjectFile());
98  if (!obj_file)
99  return NULL;
100 
101  static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
102  Timer scoped_timer(func_cat,
103  "SymbolVendorMacOSX::CreateInstance (module = %s)",
104  module_sp->GetFileSpec().GetPath().c_str());
105  SymbolVendorMacOSX *symbol_vendor = new SymbolVendorMacOSX(module_sp);
106  if (symbol_vendor) {
107  char path[PATH_MAX];
108  path[0] = '\0';
109 
110  // Try and locate the dSYM file on Mac OS X
111  static Timer::Category func_cat2(
112  "SymbolVendorMacOSX::CreateInstance() locate dSYM");
113  Timer scoped_timer2(
114  func_cat2,
115  "SymbolVendorMacOSX::CreateInstance (module = %s) locate dSYM",
116  module_sp->GetFileSpec().GetPath().c_str());
117 
118  // First check to see if the module has a symbol file in mind already. If
119  // it does, then we MUST use that.
120  FileSpec dsym_fspec(module_sp->GetSymbolFileFileSpec());
121 
122  ObjectFileSP dsym_objfile_sp;
123  if (!dsym_fspec) {
124  // No symbol file was specified in the module, lets try and find one
125  // ourselves.
126  FileSpec file_spec = obj_file->GetFileSpec();
127  if (!file_spec)
128  file_spec = module_sp->GetFileSpec();
129 
130  ModuleSpec module_spec(file_spec, module_sp->GetArchitecture());
131  module_spec.GetUUID() = module_sp->GetUUID();
132  FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths();
133  dsym_fspec =
134  Symbols::LocateExecutableSymbolFile(module_spec, search_paths);
135  if (module_spec.GetSourceMappingList().GetSize())
136  module_sp->GetSourceMappingList().Append(
137  module_spec.GetSourceMappingList(), true);
138  }
139 
140  if (dsym_fspec) {
141  // Compute dSYM root.
142  std::string dsym_root = dsym_fspec.GetPath();
143  const size_t pos = dsym_root.find("/Contents/Resources/");
144  dsym_root = pos != std::string::npos ? dsym_root.substr(0, pos) : "";
145 
146  DataBufferSP dsym_file_data_sp;
147  lldb::offset_t dsym_file_data_offset = 0;
148  dsym_objfile_sp =
149  ObjectFile::FindPlugin(module_sp, &dsym_fspec, 0,
150  FileSystem::Instance().GetByteSize(dsym_fspec),
151  dsym_file_data_sp, dsym_file_data_offset);
152  // Important to save the dSYM FileSpec so we don't call
153  // Symbols::LocateExecutableSymbolFile a second time while trying to
154  // add the symbol ObjectFile to this Module.
155  if (dsym_objfile_sp && !module_sp->GetSymbolFileFileSpec()) {
156  module_sp->SetSymbolFileFileSpec(dsym_fspec);
157  }
158  if (UUIDsMatch(module_sp.get(), dsym_objfile_sp.get(), feedback_strm)) {
159  // We need a XML parser if we hope to parse a plist...
160  if (XMLDocument::XMLEnabled()) {
161  if (module_sp->GetSourceMappingList().IsEmpty()) {
162  lldb_private::UUID dsym_uuid = dsym_objfile_sp->GetUUID();
163  if (dsym_uuid) {
164  std::string uuid_str = dsym_uuid.GetAsString();
165  if (!uuid_str.empty() && !dsym_root.empty()) {
166  char dsym_uuid_plist_path[PATH_MAX];
167  snprintf(dsym_uuid_plist_path, sizeof(dsym_uuid_plist_path),
168  "%s/Contents/Resources/%s.plist", dsym_root.c_str(),
169  uuid_str.c_str());
170  FileSpec dsym_uuid_plist_spec(dsym_uuid_plist_path);
171  if (FileSystem::Instance().Exists(dsym_uuid_plist_spec)) {
172  ApplePropertyList plist(dsym_uuid_plist_path);
173  if (plist) {
174  std::string DBGBuildSourcePath;
175  std::string DBGSourcePath;
176 
177  // DBGSourcePathRemapping is a dictionary in the plist
178  // with keys which are DBGBuildSourcePath file paths and
179  // values which are DBGSourcePath file paths
180 
181  StructuredData::ObjectSP plist_sp =
182  plist.GetStructuredData();
183  if (plist_sp.get() && plist_sp->GetAsDictionary() &&
184  plist_sp->GetAsDictionary()->HasKey(
185  "DBGSourcePathRemapping") &&
186  plist_sp->GetAsDictionary()
187  ->GetValueForKey("DBGSourcePathRemapping")
188  ->GetAsDictionary()) {
189 
190  // If DBGVersion 1 or DBGVersion missing, ignore
191  // DBGSourcePathRemapping. If DBGVersion 2, strip last two
192  // components of path remappings from
193  // entries to fix an issue with a
194  // specific set of DBGSourcePathRemapping
195  // entries that lldb worked with.
196  // If DBGVersion 3, trust & use the source path remappings
197  // as-is.
198  //
199 
200  bool new_style_source_remapping_dictionary = false;
201  bool do_truncate_remapping_names = false;
202  std::string original_DBGSourcePath_value = DBGSourcePath;
203  if (plist_sp->GetAsDictionary()->HasKey("DBGVersion")) {
204  std::string version_string =
205  std::string(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,
227  do_truncate_remapping_names](
228  ConstString key,
229  StructuredData::Object *object) -> bool {
230  if (object && object->GetAsString()) {
231 
232  // key is DBGBuildSourcePath
233  // object is DBGSourcePath
234  std::string DBGSourcePath =
235  std::string(object->GetStringValue());
236  if (!new_style_source_remapping_dictionary &&
237  !original_DBGSourcePath_value.empty()) {
238  DBGSourcePath = original_DBGSourcePath_value;
239  }
240  module_sp->GetSourceMappingList().Append(
241  key.GetStringRef(), DBGSourcePath, true);
242  // With version 2 of DBGSourcePathRemapping, we
243  // can chop off the last two filename parts
244  // from the source remapping and get a more
245  // general source remapping that still works.
246  // Add this as another option in addition to
247  // the full source path remap.
248  if (do_truncate_remapping_names) {
249  FileSpec build_path(key.AsCString());
250  FileSpec source_path(DBGSourcePath.c_str());
251  build_path.RemoveLastPathComponent();
252  build_path.RemoveLastPathComponent();
253  source_path.RemoveLastPathComponent();
254  source_path.RemoveLastPathComponent();
255  module_sp->GetSourceMappingList().Append(
256  build_path.GetPath(), source_path.GetPath(),
257  true);
258  }
259  }
260  return true;
261  });
262  }
263 
264  // If we have a DBGBuildSourcePath + DBGSourcePath pair,
265  // append those to the source path remappings.
266 
267  plist.GetValueAsString("DBGBuildSourcePath",
268  DBGBuildSourcePath);
269  plist.GetValueAsString("DBGSourcePath", DBGSourcePath);
270  if (!DBGBuildSourcePath.empty() && !DBGSourcePath.empty()) {
271  module_sp->GetSourceMappingList().Append(
272  DBGBuildSourcePath, DBGSourcePath, true);
273  }
274  }
275  }
276  }
277  }
278  }
279  }
280 
281  symbol_vendor->AddSymbolFileRepresentation(dsym_objfile_sp);
282  if (!dsym_root.empty()) {
283  if (repro::Generator *g =
284  repro::Reproducer::Instance().GetGenerator()) {
285  repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>();
286  fp.RecordInterestingDirectoryRecursive(dsym_root);
287  }
288  }
289  return symbol_vendor;
290  }
291  }
292 
293  // Just create our symbol vendor using the current objfile as this is
294  // either an executable with no dSYM (that we could locate), an executable
295  // with a dSYM that has a UUID that doesn't match.
296  symbol_vendor->AddSymbolFileRepresentation(obj_file->shared_from_this());
297  }
298  return symbol_vendor;
299 }
lldb_private::Timer::Category
Definition: Timer.h:25
lldb_private::UUID
Definition: UUID.h:23
lldb_private::StructuredData::Dictionary
Definition: StructuredData.h:352
lldb_private::ObjectFile::GetFileSpec
virtual FileSpec & GetFileSpec()
Get accessor to the object file specification.
Definition: ObjectFile.h:292
lldb_private::SymbolVendor::AddSymbolFileRepresentation
void AddSymbolFileRepresentation(const lldb::ObjectFileSP &objfile_sp)
Definition: SymbolVendor.cpp:64
ModuleSpec.h
lldb_private::StructuredData::Dictionary::GetValueForKey
ObjectSP GetValueForKey(llvm::StringRef key) const
Definition: StructuredData.h:389
Host.h
SymbolVendorMacOSX::GetPluginDescriptionStatic
static llvm::StringRef GetPluginDescriptionStatic()
Definition: SymbolVendorMacOSX.cpp:80
lldb_private::PathMappingList::GetSize
size_t GetSize() const
Definition: PathMappingList.h:46
Module.h
SymbolVendorMacOSX::Terminate
static void Terminate()
Definition: SymbolVendorMacOSX.cpp:76
lldb_private::Module
Definition: Module.h:85
lldb_private::ConstString::AsCString
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:193
LocateSymbolFile.h
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
lldb_private::Stream
Definition: Stream.h:28
Section.h
lldb_private::SymbolVendor
Definition: SymbolVendor.h:31
lldb_private::UUID::GetAsString
std::string GetAsString(llvm::StringRef separator="-") const
Definition: UUID.cpp:48
lldb_private::StructuredData::Object::GetAsString
String * GetAsString()
Definition: StructuredData.h:130
lldb_private::ApplePropertyList::GetValueAsString
bool GetValueAsString(const char *key, std::string &value) const
Definition: XML.cpp:402
lldb_private::repro::FileProvider
Definition: ReproducerProvider.h:94
Target.h
lldb_private::ModuleSpec::GetUUID
UUID & GetUUID()
Definition: ModuleSpec.h:99
ReproducerProvider.h
lldb_private::ModuleSpec::GetSourceMappingList
PathMappingList & GetSourceMappingList() const
Definition: ModuleSpec.h:125
lldb_private::Module::GetFileSpec
const FileSpec & GetFileSpec() const
Get const accessor for the module file specification.
Definition: Module.h:477
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::ConstString::GetStringRef
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:202
lldb_private::StructuredData::ObjectSP
std::shared_ptr< Object > ObjectSP
Definition: StructuredData.h:59
StreamString.h
lldb_private::ObjectFile::GetUUID
virtual UUID GetUUID()=0
Gets the UUID for this object file.
lldb_private::ConstString
Definition: ConstString.h:40
Timer.h
SymbolVendorMacOSX
Definition: SymbolVendorMacOSX.h:15
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::Stream::PutChar
size_t PutChar(char ch)
Definition: Stream.cpp:104
lldb_private::ApplePropertyList::GetStructuredData
StructuredData::ObjectSP GetStructuredData()
Definition: XML.cpp:509
ObjectFile.h
SymbolVendorMacOSX.h
lldb_private::ModuleSpec
Definition: ModuleSpec.h:27
lldb_private::UUID::Dump
void Dump(Stream *s) const
Definition: UUID.cpp:63
SymbolVendorMacOSX::CreateInstance
static lldb_private::SymbolVendor * CreateInstance(const lldb::ModuleSP &module_sp, lldb_private::Stream *feedback_strm)
Definition: SymbolVendorMacOSX.cpp:91
lldb_private::FileSpec::RemoveLastPathComponent
bool RemoveLastPathComponent()
Removes the last path component by replacing the current path with its parent.
Definition: FileSpec.cpp:445
lldb_private::StructuredData::Dictionary::ForEach
void ForEach(std::function< bool(ConstString key, Object *object)> const &callback) const
Definition: StructuredData.h:370
lldb_private::ApplePropertyList
Definition: XML.h:145
lldb_private::Stream::EOL
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:128
lldb_private::StructuredData::Object::GetAsDictionary
Dictionary * GetAsDictionary()
Definition: StructuredData.h:91
SymbolVendorMacOSX::Initialize
static void Initialize()
Definition: SymbolVendorMacOSX.cpp:71
lldb_private::Stream::AsRawOstream
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:357
XML.h
lldb_private::Timer
Definition: Timer.h:23
if
if(APPLE AND LLVM_ENABLE_LOCAL_SUBMODULE_VISIBILITY) remove_module_flags() endif() macro(add_host_subdirectory group) list(APPEND HOST_SOURCES $
Definition: Host/CMakeLists.txt:1
lldb_private::FileSpec::Dump
void Dump(llvm::raw_ostream &s) const
Dump this object to a Stream.
Definition: FileSpec.cpp:323
PluginManager.h
ObjectFileMachO.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
UUIDsMatch
static bool UUIDsMatch(Module *module, ObjectFile *ofile, lldb_private::Stream *feedback_strm)
Definition: SymbolVendorMacOSX.cpp:36
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb_private::Module::GetUUID
const lldb_private::UUID & GetUUID()
Get a reference to the UUID value contained in this object.
Definition: Module.cpp:341
PATH_MAX
#define PATH_MAX
Definition: windows/PosixApi.h:25
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:347
fp
@ fp
Definition: CompactUnwindInfo.cpp:1248
lldb_private::repro::Generator
The generator is responsible for the logic needed to generate a reproducer.
Definition: Reproducer.h:89
lldb_private::StructuredData::Object
Definition: StructuredData.h:70
lldb
Definition: SBAddress.h:15
lldb_private::ObjectFile
Definition: ObjectFile.h:60