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 "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h" 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/Host/Host.h" 19 #include "lldb/Host/XML.h" 20 #include "lldb/Symbol/LocateSymbolFile.h" 21 #include "lldb/Symbol/ObjectFile.h" 22 #include "lldb/Target/Target.h" 23 #include "lldb/Utility/StreamString.h" 24 #include "lldb/Utility/Timer.h" 25 26 using namespace lldb; 27 using namespace lldb_private; 28 29 // SymbolVendorMacOSX constructor 30 SymbolVendorMacOSX::SymbolVendorMacOSX(const lldb::ModuleSP &module_sp) 31 : SymbolVendor(module_sp) {} 32 33 // Destructor 34 SymbolVendorMacOSX::~SymbolVendorMacOSX() {} 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 71 void SymbolVendorMacOSX::Initialize() { 72 PluginManager::RegisterPlugin(GetPluginNameStatic(), 73 GetPluginDescriptionStatic(), CreateInstance); 74 } 75 76 void SymbolVendorMacOSX::Terminate() { 77 PluginManager::UnregisterPlugin(CreateInstance); 78 } 79 80 lldb_private::ConstString SymbolVendorMacOSX::GetPluginNameStatic() { 81 static ConstString g_name("macosx"); 82 return g_name; 83 } 84 85 const char *SymbolVendorMacOSX::GetPluginDescriptionStatic() { 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. 95 SymbolVendor * 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 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 if (UUIDsMatch(module_sp.get(), dsym_objfile_sp.get(), feedback_strm)) { 153 // We need a XML parser if we hope to parse a plist... 154 if (XMLDocument::XMLEnabled()) { 155 char dsym_path[PATH_MAX]; 156 if (module_sp->GetSourceMappingList().IsEmpty() && 157 dsym_fspec.GetPath(dsym_path, sizeof(dsym_path))) { 158 lldb_private::UUID dsym_uuid = dsym_objfile_sp->GetUUID(); 159 if (dsym_uuid) { 160 std::string uuid_str = dsym_uuid.GetAsString(); 161 if (!uuid_str.empty()) { 162 char *resources = strstr(dsym_path, "/Contents/Resources/"); 163 if (resources) { 164 char dsym_uuid_plist_path[PATH_MAX]; 165 resources[strlen("/Contents/Resources/")] = '\0'; 166 snprintf(dsym_uuid_plist_path, sizeof(dsym_uuid_plist_path), 167 "%s%s.plist", dsym_path, uuid_str.c_str()); 168 FileSpec dsym_uuid_plist_spec(dsym_uuid_plist_path); 169 if (FileSystem::Instance().Exists(dsym_uuid_plist_spec)) { 170 ApplePropertyList plist(dsym_uuid_plist_path); 171 if (plist) { 172 std::string DBGBuildSourcePath; 173 std::string DBGSourcePath; 174 175 // DBGSourcePathRemapping is a dictionary in the plist 176 // with keys which are DBGBuildSourcePath file paths and 177 // values which are DBGSourcePath file paths 178 179 StructuredData::ObjectSP plist_sp = 180 plist.GetStructuredData(); 181 if (plist_sp.get() && plist_sp->GetAsDictionary() && 182 plist_sp->GetAsDictionary()->HasKey( 183 "DBGSourcePathRemapping") && 184 plist_sp->GetAsDictionary() 185 ->GetValueForKey("DBGSourcePathRemapping") 186 ->GetAsDictionary()) { 187 188 // If DBGVersion 1 or DBGVersion missing, ignore DBGSourcePathRemapping. 189 // If DBGVersion 2, strip last two components of path remappings from 190 // entries to fix an issue with a specific set of 191 // DBGSourcePathRemapping entries that lldb worked 192 // with. 193 // If DBGVersion 3, trust & use the source path remappings as-is. 194 // 195 196 bool new_style_source_remapping_dictionary = false; 197 bool do_truncate_remapping_names = false; 198 std::string original_DBGSourcePath_value = 199 DBGSourcePath; 200 if (plist_sp->GetAsDictionary()->HasKey("DBGVersion")) { 201 std::string version_string = 202 plist_sp->GetAsDictionary() 203 ->GetValueForKey("DBGVersion") 204 ->GetStringValue(""); 205 if (!version_string.empty() && 206 isdigit(version_string[0])) { 207 int version_number = atoi(version_string.c_str()); 208 if (version_number > 1) { 209 new_style_source_remapping_dictionary = true; 210 } 211 if (version_number == 2) { 212 do_truncate_remapping_names = true; 213 } 214 } 215 } 216 217 StructuredData::Dictionary *remappings_dict = 218 plist_sp->GetAsDictionary() 219 ->GetValueForKey("DBGSourcePathRemapping") 220 ->GetAsDictionary(); 221 remappings_dict->ForEach( 222 [&module_sp, new_style_source_remapping_dictionary, 223 original_DBGSourcePath_value, do_truncate_remapping_names]( 224 ConstString key, 225 StructuredData::Object *object) -> bool { 226 if (object && object->GetAsString()) { 227 228 // key is DBGBuildSourcePath 229 // object is DBGSourcePath 230 std::string DBGSourcePath = 231 object->GetStringValue(); 232 if (!new_style_source_remapping_dictionary && 233 !original_DBGSourcePath_value.empty()) { 234 DBGSourcePath = original_DBGSourcePath_value; 235 } 236 if (DBGSourcePath[0] == '~') { 237 FileSpec resolved_source_path( 238 DBGSourcePath.c_str()); 239 FileSystem::Instance().Resolve( 240 resolved_source_path); 241 DBGSourcePath = 242 resolved_source_path.GetPath(); 243 } 244 module_sp->GetSourceMappingList().Append( 245 key, ConstString(DBGSourcePath), true); 246 // With version 2 of DBGSourcePathRemapping, we 247 // can chop off the last two filename parts 248 // from the source remapping and get a more 249 // general source remapping that still works. 250 // Add this as another option in addition to 251 // the full source path remap. 252 if (do_truncate_remapping_names) { 253 FileSpec build_path(key.AsCString()); 254 FileSpec source_path(DBGSourcePath.c_str()); 255 build_path.RemoveLastPathComponent(); 256 build_path.RemoveLastPathComponent(); 257 source_path.RemoveLastPathComponent(); 258 source_path.RemoveLastPathComponent(); 259 module_sp->GetSourceMappingList().Append( 260 ConstString(build_path.GetPath().c_str()), 261 ConstString(source_path.GetPath().c_str()), true); 262 } 263 } 264 return true; 265 }); 266 } 267 268 // If we have a DBGBuildSourcePath + DBGSourcePath pair, 269 // append those to the source path remappings. 270 271 plist.GetValueAsString("DBGBuildSourcePath", 272 DBGBuildSourcePath); 273 plist.GetValueAsString("DBGSourcePath", DBGSourcePath); 274 if (!DBGBuildSourcePath.empty() && 275 !DBGSourcePath.empty()) { 276 if (DBGSourcePath[0] == '~') { 277 FileSpec resolved_source_path(DBGSourcePath.c_str()); 278 FileSystem::Instance().Resolve(resolved_source_path); 279 DBGSourcePath = resolved_source_path.GetPath(); 280 } 281 module_sp->GetSourceMappingList().Append( 282 ConstString(DBGBuildSourcePath), 283 ConstString(DBGSourcePath), true); 284 } 285 } 286 } 287 } 288 } 289 } 290 } 291 } 292 293 symbol_vendor->AddSymbolFileRepresentation(dsym_objfile_sp); 294 return symbol_vendor; 295 } 296 } 297 298 // Just create our symbol vendor using the current objfile as this is 299 // either an executable with no dSYM (that we could locate), an executable 300 // with a dSYM that has a UUID that doesn't match. 301 symbol_vendor->AddSymbolFileRepresentation(obj_file->shared_from_this()); 302 } 303 return symbol_vendor; 304 } 305 306 // PluginInterface protocol 307 ConstString SymbolVendorMacOSX::GetPluginName() { 308 return GetPluginNameStatic(); 309 } 310 311 uint32_t SymbolVendorMacOSX::GetPluginVersion() { return 1; } 312