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
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/ReproducerProvider.h"
24 #include "lldb/Utility/StreamString.h"
25 #include "lldb/Utility/Timer.h"
26
27 using namespace lldb;
28 using namespace lldb_private;
29
LLDB_PLUGIN_DEFINE(SymbolVendorMacOSX)30 LLDB_PLUGIN_DEFINE(SymbolVendorMacOSX)
31
32 // SymbolVendorMacOSX constructor
33 SymbolVendorMacOSX::SymbolVendorMacOSX(const lldb::ModuleSP &module_sp)
34 : SymbolVendor(module_sp) {}
35
UUIDsMatch(Module * module,ObjectFile * ofile,lldb_private::Stream * feedback_strm)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
Initialize()71 void SymbolVendorMacOSX::Initialize() {
72 PluginManager::RegisterPlugin(GetPluginNameStatic(),
73 GetPluginDescriptionStatic(), CreateInstance);
74 }
75
Terminate()76 void SymbolVendorMacOSX::Terminate() {
77 PluginManager::UnregisterPlugin(CreateInstance);
78 }
79
GetPluginDescriptionStatic()80 llvm::StringRef SymbolVendorMacOSX::GetPluginDescriptionStatic() {
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.
90 SymbolVendor *
CreateInstance(const lldb::ModuleSP & module_sp,lldb_private::Stream * feedback_strm)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 }
300