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  static ConstString g_name("macosx");
82  return g_name;
83 }
84 
86  return "Symbol vendor for MacOSX that looks for dSYM files that match "
87  "executables.";
88 }
89 
90 // CreateInstance
91 //
92 // Platforms can register a callback to use when creating symbol vendors to
93 // allow for complex debug information file setups, and to also allow for
94 // finding separate debug information files.
96 SymbolVendorMacOSX::CreateInstance(const lldb::ModuleSP &module_sp,
97  lldb_private::Stream *feedback_strm) {
98  if (!module_sp)
99  return NULL;
100 
101  ObjectFile *obj_file =
102  llvm::dyn_cast_or_null<ObjectFileMachO>(module_sp->GetObjectFile());
103  if (!obj_file)
104  return NULL;
105 
106  static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
107  Timer scoped_timer(func_cat,
108  "SymbolVendorMacOSX::CreateInstance (module = %s)",
109  module_sp->GetFileSpec().GetPath().c_str());
110  SymbolVendorMacOSX *symbol_vendor = new SymbolVendorMacOSX(module_sp);
111  if (symbol_vendor) {
112  char path[PATH_MAX];
113  path[0] = '\0';
114 
115  // Try and locate the dSYM file on Mac OS X
116  static Timer::Category func_cat2(
117  "SymbolVendorMacOSX::CreateInstance() locate dSYM");
118  Timer scoped_timer2(
119  func_cat2,
120  "SymbolVendorMacOSX::CreateInstance (module = %s) locate dSYM",
121  module_sp->GetFileSpec().GetPath().c_str());
122 
123  // First check to see if the module has a symbol file in mind already. If
124  // it does, then we MUST use that.
125  FileSpec dsym_fspec(module_sp->GetSymbolFileFileSpec());
126 
127  ObjectFileSP dsym_objfile_sp;
128  if (!dsym_fspec) {
129  // No symbol file was specified in the module, lets try and find one
130  // ourselves.
131  FileSpec file_spec = obj_file->GetFileSpec();
132  if (!file_spec)
133  file_spec = module_sp->GetFileSpec();
134 
135  ModuleSpec module_spec(file_spec, module_sp->GetArchitecture());
136  module_spec.GetUUID() = module_sp->GetUUID();
137  FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths();
138  dsym_fspec =
139  Symbols::LocateExecutableSymbolFile(module_spec, search_paths);
140  if (module_spec.GetSourceMappingList().GetSize())
141  module_sp->GetSourceMappingList().Append(
142  module_spec.GetSourceMappingList(), true);
143  }
144 
145  if (dsym_fspec) {
146  // Compute dSYM root.
147  std::string dsym_root = dsym_fspec.GetPath();
148  const size_t pos = dsym_root.find("/Contents/Resources/");
149  dsym_root = pos != std::string::npos ? dsym_root.substr(0, pos) : "";
150 
151  DataBufferSP dsym_file_data_sp;
152  lldb::offset_t dsym_file_data_offset = 0;
153  dsym_objfile_sp =
154  ObjectFile::FindPlugin(module_sp, &dsym_fspec, 0,
155  FileSystem::Instance().GetByteSize(dsym_fspec),
156  dsym_file_data_sp, dsym_file_data_offset);
157  if (UUIDsMatch(module_sp.get(), dsym_objfile_sp.get(), feedback_strm)) {
158  // We need a XML parser if we hope to parse a plist...
159  if (XMLDocument::XMLEnabled()) {
160  if (module_sp->GetSourceMappingList().IsEmpty()) {
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() && !dsym_root.empty()) {
165  char dsym_uuid_plist_path[PATH_MAX];
166  snprintf(dsym_uuid_plist_path, sizeof(dsym_uuid_plist_path),
167  "%s/Contents/Resources/%s.plist", dsym_root.c_str(),
168  uuid_str.c_str());
169  FileSpec dsym_uuid_plist_spec(dsym_uuid_plist_path);
170  if (FileSystem::Instance().Exists(dsym_uuid_plist_spec)) {
171  ApplePropertyList plist(dsym_uuid_plist_path);
172  if (plist) {
173  std::string DBGBuildSourcePath;
174  std::string DBGSourcePath;
175 
176  // DBGSourcePathRemapping is a dictionary in the plist
177  // with keys which are DBGBuildSourcePath file paths and
178  // values which are DBGSourcePath file paths
179 
180  StructuredData::ObjectSP plist_sp =
181  plist.GetStructuredData();
182  if (plist_sp.get() && plist_sp->GetAsDictionary() &&
183  plist_sp->GetAsDictionary()->HasKey(
184  "DBGSourcePathRemapping") &&
185  plist_sp->GetAsDictionary()
186  ->GetValueForKey("DBGSourcePathRemapping")
187  ->GetAsDictionary()) {
188 
189  // If DBGVersion 1 or DBGVersion missing, ignore
190  // DBGSourcePathRemapping. If DBGVersion 2, strip last two
191  // components of path remappings from
192  // entries to fix an issue with a
193  // specific set of DBGSourcePathRemapping
194  // entries that lldb worked with.
195  // If DBGVersion 3, trust & use the source path remappings
196  // 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 = DBGSourcePath;
202  if (plist_sp->GetAsDictionary()->HasKey("DBGVersion")) {
203  std::string version_string =
204  std::string(plist_sp->GetAsDictionary()
205  ->GetValueForKey("DBGVersion")
206  ->GetStringValue(""));
207  if (!version_string.empty() &&
208  isdigit(version_string[0])) {
209  int version_number = atoi(version_string.c_str());
210  if (version_number > 1) {
211  new_style_source_remapping_dictionary = true;
212  }
213  if (version_number == 2) {
214  do_truncate_remapping_names = true;
215  }
216  }
217  }
218 
219  StructuredData::Dictionary *remappings_dict =
220  plist_sp->GetAsDictionary()
221  ->GetValueForKey("DBGSourcePathRemapping")
222  ->GetAsDictionary();
223  remappings_dict->ForEach(
224  [&module_sp, new_style_source_remapping_dictionary,
225  original_DBGSourcePath_value,
226  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  std::string(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 = resolved_source_path.GetPath();
245  }
246  module_sp->GetSourceMappingList().Append(
247  key, ConstString(DBGSourcePath), true);
248  // With version 2 of DBGSourcePathRemapping, we
249  // can chop off the last two filename parts
250  // from the source remapping and get a more
251  // general source remapping that still works.
252  // Add this as another option in addition to
253  // the full source path remap.
254  if (do_truncate_remapping_names) {
255  FileSpec build_path(key.AsCString());
256  FileSpec source_path(DBGSourcePath.c_str());
257  build_path.RemoveLastPathComponent();
258  build_path.RemoveLastPathComponent();
259  source_path.RemoveLastPathComponent();
260  source_path.RemoveLastPathComponent();
261  module_sp->GetSourceMappingList().Append(
262  ConstString(build_path.GetPath().c_str()),
263  ConstString(source_path.GetPath().c_str()),
264  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() && !DBGSourcePath.empty()) {
278  if (DBGSourcePath[0] == '~') {
279  FileSpec resolved_source_path(DBGSourcePath.c_str());
280  FileSystem::Instance().Resolve(resolved_source_path);
281  DBGSourcePath = resolved_source_path.GetPath();
282  }
283  module_sp->GetSourceMappingList().Append(
284  ConstString(DBGBuildSourcePath),
285  ConstString(DBGSourcePath), true);
286  }
287  }
288  }
289  }
290  }
291  }
292  }
293 
294  symbol_vendor->AddSymbolFileRepresentation(dsym_objfile_sp);
295  if (!dsym_root.empty()) {
296  if (repro::Generator *g =
297  repro::Reproducer::Instance().GetGenerator()) {
298  repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>();
299  fp.RecordInterestingDirectoryRecursive(dsym_root);
300  }
301  }
302  return symbol_vendor;
303  }
304  }
305 
306  // Just create our symbol vendor using the current objfile as this is
307  // either an executable with no dSYM (that we could locate), an executable
308  // with a dSYM that has a UUID that doesn't match.
309  symbol_vendor->AddSymbolFileRepresentation(obj_file->shared_from_this());
310  }
311  return symbol_vendor;
312 }
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:290
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:379
Host.h
lldb_private::PathMappingList::GetSize
size_t GetSize() const
Definition: PathMappingList.h:47
Module.h
SymbolVendorMacOSX::Terminate
static void Terminate()
Definition: SymbolVendorMacOSX.cpp:76
lldb_private::Module
Definition: Module.h:84
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::ApplePropertyList::GetValueAsString
bool GetValueAsString(const char *key, std::string &value) const
Definition: XML.cpp:399
lldb_private::repro::FileProvider
Definition: ReproducerProvider.h:111
Target.h
lldb_private::ModuleSpec::GetUUID
UUID & GetUUID()
Definition: ModuleSpec.h:104
ReproducerProvider.h
lldb_private::ModuleSpec::GetSourceMappingList
PathMappingList & GetSourceMappingList() const
Definition: ModuleSpec.h:130
lldb_private::Module::GetFileSpec
const FileSpec & GetFileSpec() const
Get const accessor for the module file specification.
Definition: Module.h:476
lldb_private::FileSpec
Definition: FileSpec.h:56
SymbolVendorMacOSX::GetPluginDescriptionStatic
static const char * GetPluginDescriptionStatic()
Definition: SymbolVendorMacOSX.cpp:85
object
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
if
if(CMAKE_SYSTEM_NAME MATCHES "Windows") add_definitions(-DEXPORT_LIBLLDB) endif() get_property(LLDB_ALL_PLUGINS GLOBAL PROPERTY LLDB_PLUGINS) if(LLDB_BUILD_FRAMEWORK) set(option_install_prefix INSTALL_PREFIX $
Definition: API/CMakeLists.txt:1
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:506
ObjectFile.h
SymbolVendorMacOSX.h
lldb_private::ModuleSpec
Definition: ModuleSpec.h:26
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:96
lldb_private::FileSpec::RemoveLastPathComponent
bool RemoveLastPathComponent()
Removes the last path component by replacing the current path with its parent.
Definition: FileSpec.cpp:446
lldb_private::StructuredData::Dictionary::ForEach
void ForEach(std::function< bool(ConstString key, Object *object)> const &callback) const
Definition: StructuredData.h:360
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
lldb_private::FileSpec::Dump
void Dump(llvm::raw_ostream &s) const
Dump this object to a Stream.
Definition: FileSpec.cpp:324
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:345
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:348
fp
@ fp
Definition: CompactUnwindInfo.cpp:1247
lldb_private::repro::Generator
The generator is responsible for the logic needed to generate a reproducer.
Definition: Reproducer.h:91
SymbolVendorMacOSX::GetPluginNameStatic
static lldb_private::ConstString GetPluginNameStatic()
Definition: SymbolVendorMacOSX.cpp:80
lldb_private::StructuredData::Object
Definition: StructuredData.h:70
lldb
Definition: SBAddress.h:15
lldb_private::ObjectFile
Definition: ObjectFile.h:58