Skip to content

Commit 2dea3b4

Browse files
authored
[lldb][PlatformDarwin][NFCI] Factor out dSYM script auto-loading into helper function (llvm#182002)
Depends on: * llvm#182001 (only second commit is relevant for this review) This patch factors out the logic to load dSYM scripting resources into a helper function. In the process we eliminate some redundant copying of `FileSpec` and pass it to the helper by `const-ref` instead (specifically the `symfile_spec`).
1 parent 9355a17 commit 2dea3b4

1 file changed

Lines changed: 97 additions & 91 deletions

File tree

lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp

Lines changed: 97 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,97 @@ PlatformDarwin::PutFile(const lldb_private::FileSpec &source,
199199
return PlatformPOSIX::PutFile(source, destination, uid, gid);
200200
}
201201

202+
static FileSpecList LoadExecutableScriptingResourceFromDSYM(
203+
Stream &feedback_stream, FileSpec module_spec, const Target &target,
204+
const FileSpec &symfile_spec) {
205+
FileSpecList file_list;
206+
while (module_spec.GetFilename()) {
207+
std::string module_basename(module_spec.GetFilename().GetCString());
208+
std::string original_module_basename(module_basename);
209+
210+
bool was_keyword = false;
211+
212+
// FIXME: for Python, don't allow certain characters in imported module
213+
// filenames. Theoretically, different scripting languages may have
214+
// different sets of forbidden tokens in filenames, and that should
215+
// be dealt with by each ScriptInterpreter. For now, just replace dots
216+
// with underscores. In order to support anything other than Python
217+
// this will need to be reworked.
218+
llvm::replace(module_basename, '.', '_');
219+
llvm::replace(module_basename, ' ', '_');
220+
llvm::replace(module_basename, '-', '_');
221+
ScriptInterpreter *script_interpreter =
222+
target.GetDebugger().GetScriptInterpreter();
223+
if (script_interpreter &&
224+
script_interpreter->IsReservedWord(module_basename.c_str())) {
225+
module_basename.insert(module_basename.begin(), '_');
226+
was_keyword = true;
227+
}
228+
229+
StreamString path_string;
230+
StreamString original_path_string;
231+
// for OSX we are going to be in
232+
// .dSYM/Contents/Resources/DWARF/<basename> let us go to
233+
// .dSYM/Contents/Resources/Python/<basename>.py and see if the
234+
// file exists
235+
path_string.Printf("%s/../Python/%s.py",
236+
symfile_spec.GetDirectory().GetCString(),
237+
module_basename.c_str());
238+
original_path_string.Printf("%s/../Python/%s.py",
239+
symfile_spec.GetDirectory().GetCString(),
240+
original_module_basename.c_str());
241+
FileSpec script_fspec(path_string.GetString());
242+
FileSystem::Instance().Resolve(script_fspec);
243+
FileSpec orig_script_fspec(original_path_string.GetString());
244+
FileSystem::Instance().Resolve(orig_script_fspec);
245+
246+
// if we did some replacements of reserved characters, and a
247+
// file with the untampered name exists, then warn the user
248+
// that the file as-is shall not be loaded
249+
if (module_basename != original_module_basename &&
250+
FileSystem::Instance().Exists(orig_script_fspec)) {
251+
const char *reason_for_complaint = was_keyword
252+
? "conflicts with a keyword"
253+
: "contains reserved characters";
254+
if (FileSystem::Instance().Exists(script_fspec))
255+
feedback_stream.Printf(
256+
"warning: the symbol file '%s' contains a debug "
257+
"script. However, its name"
258+
" '%s' %s and as such cannot be loaded. LLDB will"
259+
" load '%s' instead. Consider removing the file with "
260+
"the malformed name to"
261+
" eliminate this warning.\n",
262+
symfile_spec.GetPath().c_str(), original_path_string.GetData(),
263+
reason_for_complaint, path_string.GetData());
264+
else
265+
feedback_stream.Printf(
266+
"warning: the symbol file '%s' contains a debug "
267+
"script. However, its name"
268+
" %s and as such cannot be loaded. If you intend"
269+
" to have this script loaded, please rename '%s' to "
270+
"'%s' and retry.\n",
271+
symfile_spec.GetPath().c_str(), reason_for_complaint,
272+
original_path_string.GetData(), path_string.GetData());
273+
}
274+
275+
if (FileSystem::Instance().Exists(script_fspec)) {
276+
file_list.Append(script_fspec);
277+
break;
278+
}
279+
280+
// If we didn't find the python file, then keep stripping the
281+
// extensions and try again
282+
ConstString filename_no_extension(
283+
module_spec.GetFileNameStrippingExtension());
284+
if (module_spec.GetFilename() == filename_no_extension)
285+
break;
286+
287+
module_spec.SetFilename(filename_no_extension);
288+
}
289+
290+
return file_list;
291+
}
292+
202293
FileSpecList PlatformDarwin::LocateExecutableScriptingResources(
203294
Target *target, Module &module, Stream &feedback_stream) {
204295
if (!target)
@@ -215,7 +306,7 @@ FileSpecList PlatformDarwin::LocateExecutableScriptingResources(
215306
// extensions (".exe", ".app", ".dSYM", ".framework") which should be
216307
// stripped while leaving "this.binary.file" as-is.
217308

218-
FileSpec module_spec = module.GetFileSpec();
309+
const FileSpec &module_spec = module.GetFileSpec();
219310

220311
if (!module_spec)
221312
return {};
@@ -228,100 +319,15 @@ FileSpecList PlatformDarwin::LocateExecutableScriptingResources(
228319
if (!objfile)
229320
return {};
230321

231-
FileSpecList file_list;
232-
FileSpec symfile_spec(objfile->GetFileSpec());
322+
const FileSpec &symfile_spec = objfile->GetFileSpec();
233323
if (symfile_spec &&
234324
llvm::StringRef(symfile_spec.GetPath())
235325
.contains_insensitive(".dSYM/Contents/Resources/DWARF") &&
236-
FileSystem::Instance().Exists(symfile_spec)) {
237-
while (module_spec.GetFilename()) {
238-
std::string module_basename(module_spec.GetFilename().GetCString());
239-
std::string original_module_basename(module_basename);
240-
241-
bool was_keyword = false;
242-
243-
// FIXME: for Python, we cannot allow certain characters in
244-
// module
245-
// filenames we import. Theoretically, different scripting
246-
// languages may have different sets of forbidden tokens in
247-
// filenames, and that should be dealt with by each
248-
// ScriptInterpreter. For now, we just replace dots with
249-
// underscores, but if we ever support anything other than
250-
// Python we will need to rework this
251-
llvm::replace(module_basename, '.', '_');
252-
llvm::replace(module_basename, ' ', '_');
253-
llvm::replace(module_basename, '-', '_');
254-
ScriptInterpreter *script_interpreter =
255-
target->GetDebugger().GetScriptInterpreter();
256-
if (script_interpreter &&
257-
script_interpreter->IsReservedWord(module_basename.c_str())) {
258-
module_basename.insert(module_basename.begin(), '_');
259-
was_keyword = true;
260-
}
261-
262-
StreamString path_string;
263-
StreamString original_path_string;
264-
// for OSX we are going to be in
265-
// .dSYM/Contents/Resources/DWARF/<basename> let us go to
266-
// .dSYM/Contents/Resources/Python/<basename>.py and see if the
267-
// file exists
268-
path_string.Printf("%s/../Python/%s.py",
269-
symfile_spec.GetDirectory().GetCString(),
270-
module_basename.c_str());
271-
original_path_string.Printf("%s/../Python/%s.py",
272-
symfile_spec.GetDirectory().GetCString(),
273-
original_module_basename.c_str());
274-
FileSpec script_fspec(path_string.GetString());
275-
FileSystem::Instance().Resolve(script_fspec);
276-
FileSpec orig_script_fspec(original_path_string.GetString());
277-
FileSystem::Instance().Resolve(orig_script_fspec);
278-
279-
// if we did some replacements of reserved characters, and a
280-
// file with the untampered name exists, then warn the user
281-
// that the file as-is shall not be loaded
282-
if (module_basename != original_module_basename &&
283-
FileSystem::Instance().Exists(orig_script_fspec)) {
284-
const char *reason_for_complaint = was_keyword
285-
? "conflicts with a keyword"
286-
: "contains reserved characters";
287-
if (FileSystem::Instance().Exists(script_fspec))
288-
feedback_stream.Printf(
289-
"warning: the symbol file '%s' contains a debug "
290-
"script. However, its name"
291-
" '%s' %s and as such cannot be loaded. LLDB will"
292-
" load '%s' instead. Consider removing the file with "
293-
"the malformed name to"
294-
" eliminate this warning.\n",
295-
symfile_spec.GetPath().c_str(), original_path_string.GetData(),
296-
reason_for_complaint, path_string.GetData());
297-
else
298-
feedback_stream.Printf(
299-
"warning: the symbol file '%s' contains a debug "
300-
"script. However, its name"
301-
" %s and as such cannot be loaded. If you intend"
302-
" to have this script loaded, please rename '%s' to "
303-
"'%s' and retry.\n",
304-
symfile_spec.GetPath().c_str(), reason_for_complaint,
305-
original_path_string.GetData(), path_string.GetData());
306-
}
307-
308-
if (FileSystem::Instance().Exists(script_fspec)) {
309-
file_list.Append(script_fspec);
310-
break;
311-
}
326+
FileSystem::Instance().Exists(symfile_spec))
327+
return LoadExecutableScriptingResourceFromDSYM(feedback_stream, module_spec,
328+
*target, symfile_spec);
312329

313-
// If we didn't find the python file, then keep stripping the
314-
// extensions and try again
315-
ConstString filename_no_extension(
316-
module_spec.GetFileNameStrippingExtension());
317-
if (module_spec.GetFilename() == filename_no_extension)
318-
break;
319-
320-
module_spec.SetFilename(filename_no_extension);
321-
}
322-
}
323-
324-
return file_list;
330+
return {};
325331
}
326332

327333
Status PlatformDarwin::ResolveSymbolFile(Target &target,

0 commit comments

Comments
 (0)