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
10
11#include <cstring>
12
14#include "lldb/Core/Module.h"
17#include "lldb/Core/Section.h"
18#include "lldb/Host/Host.h"
19#include "lldb/Host/XML.h"
21#include "lldb/Target/Target.h"
23#include "lldb/Utility/Timer.h"
24
25using namespace lldb;
26using namespace lldb_private;
27
29
30// SymbolVendorMacOSX constructor
32 : SymbolVendor(module_sp) {}
33
34static bool UUIDsMatch(Module *module, ObjectFile *ofile,
35 lldb_private::Stream *feedback_strm) {
36 if (module && ofile) {
37 // Make sure the UUIDs match
38 lldb_private::UUID dsym_uuid = ofile->GetUUID();
39 if (!dsym_uuid) {
40 if (feedback_strm) {
41 feedback_strm->PutCString(
42 "warning: failed to get the uuid for object file: '");
43 ofile->GetFileSpec().Dump(feedback_strm->AsRawOstream());
44 feedback_strm->PutCString("\n");
45 }
46 return false;
47 }
48
49 if (dsym_uuid == module->GetUUID())
50 return true;
51
52 // Emit some warning messages since the UUIDs do not match!
53 if (feedback_strm) {
54 feedback_strm->PutCString(
55 "warning: UUID mismatch detected between modules:\n ");
56 module->GetUUID().Dump(*feedback_strm);
57 feedback_strm->PutChar(' ');
58 module->GetFileSpec().Dump(feedback_strm->AsRawOstream());
59 feedback_strm->PutCString("\n ");
60 dsym_uuid.Dump(*feedback_strm);
61 feedback_strm->PutChar(' ');
62 ofile->GetFileSpec().Dump(feedback_strm->AsRawOstream());
63 feedback_strm->EOL();
64 }
65 }
66 return false;
67}
68
72}
73
76}
77
79 return "Symbol vendor for MacOSX that looks for dSYM files that match "
80 "executables.";
81}
82
83// CreateInstance
84//
85// Platforms can register a callback to use when creating symbol vendors to
86// allow for complex debug information file setups, and to also allow for
87// finding separate debug information files.
90 lldb_private::Stream *feedback_strm) {
91 if (!module_sp)
92 return NULL;
93
94 ObjectFile *obj_file =
95 llvm::dyn_cast_or_null<ObjectFileMachO>(module_sp->GetObjectFile());
96 if (!obj_file)
97 return NULL;
98
99 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
100 Timer scoped_timer(func_cat,
101 "SymbolVendorMacOSX::CreateInstance (module = %s)",
102 module_sp->GetFileSpec().GetPath().c_str());
103 SymbolVendorMacOSX *symbol_vendor = new SymbolVendorMacOSX(module_sp);
104 if (symbol_vendor) {
105 char path[PATH_MAX];
106 path[0] = '\0';
107
108 // Try and locate the dSYM file on Mac OS X
109 static Timer::Category func_cat2(
110 "SymbolVendorMacOSX::CreateInstance() locate dSYM");
111 Timer scoped_timer2(
112 func_cat2,
113 "SymbolVendorMacOSX::CreateInstance (module = %s) locate dSYM",
114 module_sp->GetFileSpec().GetPath().c_str());
115
116 // First check to see if the module has a symbol file in mind already. If
117 // it does, then we MUST use that.
118 FileSpec dsym_fspec(module_sp->GetSymbolFileFileSpec());
119
120 ObjectFileSP dsym_objfile_sp;
121 if (!dsym_fspec) {
122 // No symbol file was specified in the module, lets try and find one
123 // ourselves.
124 FileSpec file_spec = obj_file->GetFileSpec();
125 if (!file_spec)
126 file_spec = module_sp->GetFileSpec();
127
128 ModuleSpec module_spec(file_spec, module_sp->GetArchitecture());
129 module_spec.GetUUID() = module_sp->GetUUID();
131 dsym_fspec =
132 PluginManager::LocateExecutableSymbolFile(module_spec, search_paths);
133 if (module_spec.GetSourceMappingList().GetSize())
134 module_sp->GetSourceMappingList().Append(
135 module_spec.GetSourceMappingList(), true);
136 }
137
138 if (dsym_fspec) {
139 // Compute dSYM root.
140 std::string dsym_root = dsym_fspec.GetPath();
141 const size_t pos = dsym_root.find("/Contents/Resources/");
142 dsym_root = pos != std::string::npos ? dsym_root.substr(0, pos) : "";
143
144 DataBufferSP dsym_file_data_sp;
145 lldb::offset_t dsym_file_data_offset = 0;
146 dsym_objfile_sp =
147 ObjectFile::FindPlugin(module_sp, &dsym_fspec, 0,
148 FileSystem::Instance().GetByteSize(dsym_fspec),
149 dsym_file_data_sp, dsym_file_data_offset);
150 // Important to save the dSYM FileSpec so we don't call
151 // PluginManager::LocateExecutableSymbolFile a second time while trying to
152 // add the symbol ObjectFile to this Module.
153 if (dsym_objfile_sp && !module_sp->GetSymbolFileFileSpec()) {
154 module_sp->SetSymbolFileFileSpec(dsym_fspec);
155 }
156 if (UUIDsMatch(module_sp.get(), dsym_objfile_sp.get(), feedback_strm)) {
157 // We need a XML parser if we hope to parse a plist...
159 if (module_sp->GetSourceMappingList().IsEmpty()) {
160 lldb_private::UUID dsym_uuid = dsym_objfile_sp->GetUUID();
161 if (dsym_uuid) {
162 std::string uuid_str = dsym_uuid.GetAsString();
163 if (!uuid_str.empty() && !dsym_root.empty()) {
164 char dsym_uuid_plist_path[PATH_MAX];
165 snprintf(dsym_uuid_plist_path, sizeof(dsym_uuid_plist_path),
166 "%s/Contents/Resources/%s.plist", dsym_root.c_str(),
167 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
189 // DBGSourcePathRemapping. If DBGVersion 2, strip last two
190 // components of path remappings from
191 // entries to fix an issue with a
192 // specific set of DBGSourcePathRemapping
193 // entries that lldb worked with.
194 // If DBGVersion 3, trust & use the source path remappings
195 // as-is.
196 //
197
198 bool new_style_source_remapping_dictionary = false;
199 bool do_truncate_remapping_names = false;
200 std::string original_DBGSourcePath_value = DBGSourcePath;
201 if (plist_sp->GetAsDictionary()->HasKey("DBGVersion")) {
202 std::string version_string =
203 std::string(plist_sp->GetAsDictionary()
204 ->GetValueForKey("DBGVersion")
205 ->GetStringValue(""));
206 if (!version_string.empty() &&
207 isdigit(version_string[0])) {
208 int version_number = atoi(version_string.c_str());
209 if (version_number > 1) {
210 new_style_source_remapping_dictionary = true;
211 }
212 if (version_number == 2) {
213 do_truncate_remapping_names = true;
214 }
215 }
216 }
217
218 StructuredData::Dictionary *remappings_dict =
219 plist_sp->GetAsDictionary()
220 ->GetValueForKey("DBGSourcePathRemapping")
221 ->GetAsDictionary();
222 remappings_dict->ForEach(
223 [&module_sp, new_style_source_remapping_dictionary,
224 original_DBGSourcePath_value,
225 do_truncate_remapping_names](
226 llvm::StringRef key,
227 StructuredData::Object *object) -> bool {
228 if (object && object->GetAsString()) {
229
230 // key is DBGBuildSourcePath
231 // object is DBGSourcePath
232 std::string DBGSourcePath =
233 std::string(object->GetStringValue());
234 if (!new_style_source_remapping_dictionary &&
235 !original_DBGSourcePath_value.empty()) {
236 DBGSourcePath = original_DBGSourcePath_value;
237 }
238 module_sp->GetSourceMappingList().Append(
239 key, DBGSourcePath, true);
240 // With version 2 of DBGSourcePathRemapping, we
241 // can chop off the last two filename parts
242 // from the source remapping and get a more
243 // general source remapping that still works.
244 // Add this as another option in addition to
245 // the full source path remap.
246 if (do_truncate_remapping_names) {
247 FileSpec build_path(key);
248 FileSpec source_path(DBGSourcePath.c_str());
249 build_path.RemoveLastPathComponent();
250 build_path.RemoveLastPathComponent();
251 source_path.RemoveLastPathComponent();
252 source_path.RemoveLastPathComponent();
253 module_sp->GetSourceMappingList().Append(
254 build_path.GetPath(), source_path.GetPath(),
255 true);
256 }
257 }
258 return true;
259 });
260 }
261
262 // If we have a DBGBuildSourcePath + DBGSourcePath pair,
263 // append those to the source path remappings.
264
265 plist.GetValueAsString("DBGBuildSourcePath",
266 DBGBuildSourcePath);
267 plist.GetValueAsString("DBGSourcePath", DBGSourcePath);
268 if (!DBGBuildSourcePath.empty() && !DBGSourcePath.empty()) {
269 module_sp->GetSourceMappingList().Append(
270 DBGBuildSourcePath, DBGSourcePath, true);
271 }
272 }
273 }
274 }
275 }
276 }
277 }
278
279 symbol_vendor->AddSymbolFileRepresentation(dsym_objfile_sp);
280 return symbol_vendor;
281 }
282 }
283
284 // Just create our symbol vendor using the current objfile as this is
285 // either an executable with no dSYM (that we could locate), an executable
286 // with a dSYM that has a UUID that doesn't match.
287 symbol_vendor->AddSymbolFileRepresentation(obj_file->shared_from_this());
288 }
289 return symbol_vendor;
290}
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
static bool UUIDsMatch(Module *module, ObjectFile *ofile, lldb_private::Stream *feedback_strm)
static lldb_private::SymbolVendor * CreateInstance(const lldb::ModuleSP &module_sp, lldb_private::Stream *feedback_strm)
static llvm::StringRef GetPluginDescriptionStatic()
static llvm::StringRef GetPluginNameStatic()
bool GetValueAsString(const char *key, std::string &value) const
Definition: XML.cpp:404
StructuredData::ObjectSP GetStructuredData()
Definition: XML.cpp:511
A file collection class.
Definition: FileSpecList.h:24
A file utility class.
Definition: FileSpec.h:57
bool RemoveLastPathComponent()
Removes the last path component by replacing the current path with its parent.
Definition: FileSpec.cpp:461
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:370
void Dump(llvm::raw_ostream &s) const
Dump this object to a Stream.
Definition: FileSpec.cpp:328
static FileSystem & Instance()
PathMappingList & GetSourceMappingList() const
Definition: ModuleSpec.h:125
A class that describes an executable image and its associated object and symbol files.
Definition: Module.h:88
const lldb_private::UUID & GetUUID()
Get a reference to the UUID value contained in this object.
Definition: Module.cpp:340
const FileSpec & GetFileSpec() const
Get const accessor for the module file specification.
Definition: Module.h:497
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:44
static lldb::ObjectFileSP FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file_spec, lldb::offset_t file_offset, lldb::offset_t file_size, lldb::DataBufferSP &data_sp, lldb::offset_t &data_offset)
Find a ObjectFile plug-in that can parse file_spec.
Definition: ObjectFile.cpp:53
virtual FileSpec & GetFileSpec()
Get accessor to the object file specification.
Definition: ObjectFile.h:274
virtual UUID GetUUID()=0
Gets the UUID for this object file.
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static FileSpec LocateExecutableSymbolFile(const ModuleSpec &module_spec, const FileSpecList &default_search_paths)
static bool UnregisterPlugin(ABICreateInstance create_callback)
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:357
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
size_t PutChar(char ch)
Definition: Stream.cpp:104
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:128
ObjectSP GetValueForKey(llvm::StringRef key) const
void ForEach(std::function< bool(llvm::StringRef key, Object *object)> const &callback) const
std::shared_ptr< Object > ObjectSP
void AddSymbolFileRepresentation(const lldb::ObjectFileSP &objfile_sp)
static FileSpecList GetDefaultDebugFileSearchPaths()
Definition: Target.cpp:2607
A timer class that simplifies common timing metrics.
Definition: Timer.h:23
void Dump(Stream &s) const
Definition: UUID.cpp:64
std::string GetAsString(llvm::StringRef separator="-") const
Definition: UUID.cpp:49
static bool XMLEnabled()
Definition: XML.cpp:83
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
uint64_t offset_t
Definition: lldb-types.h:83
std::shared_ptr< lldb_private::ObjectFile > ObjectFileSP
Definition: lldb-forward.h:363
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
Definition: lldb-forward.h:324
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:361
#define PATH_MAX