Skip to content

Commit 74f2a9a

Browse files
committed
[lldb/Host] Improve error messages on unowned read files
When trying to read a core file that is not owned by the user running lldb and that doesn't have read permission on the file, lldb shows a misleading error message: ``` Unable to find process plug-in for core file ``` This is due to the fact that currently, lldb doesn't check the file ownership. And when trying to to open and read a core file, the syscall fails, which prevents a process to be created. Since lldb already have a portable `open` syscall interface, lets take advantage of that and delegate the error handling to the syscall itself. This way, no matter if the file exists or if the user has proper ownership, lldb will always try to open the file, and behave accordingly to the error code returned. rdar://42630030 https://reviews.llvm.org/D78712 Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent 1af8d34 commit 74f2a9a

File tree

2 files changed

+39
-60
lines changed

2 files changed

+39
-60
lines changed

lldb/source/Commands/CommandObjectTarget.cpp

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,13 @@ class CommandObjectTargetCreate : public CommandObjectParsed {
271271
FileSpec remote_file(m_remote_file.GetOptionValue().GetCurrentValue());
272272

273273
if (core_file) {
274-
if (!FileSystem::Instance().Exists(core_file)) {
275-
result.AppendErrorWithFormat("core file '%s' doesn't exist",
276-
core_file.GetPath().c_str());
277-
result.SetStatus(eReturnStatusFailed);
278-
return false;
279-
}
280-
if (!FileSystem::Instance().Readable(core_file)) {
281-
result.AppendErrorWithFormat("core file '%s' is not readable",
282-
core_file.GetPath().c_str());
274+
auto file = FileSystem::Instance().Open(
275+
core_file, lldb_private::File::eOpenOptionRead);
276+
277+
if (!file) {
278+
result.AppendErrorWithFormatv("Cannot open '{0}': {1}.",
279+
core_file.GetPath(),
280+
llvm::toString(file.takeError()));
283281
result.SetStatus(eReturnStatusFailed);
284282
return false;
285283
}
@@ -288,18 +286,13 @@ class CommandObjectTargetCreate : public CommandObjectParsed {
288286
if (argc == 1 || core_file || remote_file) {
289287
FileSpec symfile(m_symbol_file.GetOptionValue().GetCurrentValue());
290288
if (symfile) {
291-
if (FileSystem::Instance().Exists(symfile)) {
292-
if (!FileSystem::Instance().Readable(symfile)) {
293-
result.AppendErrorWithFormat("symbol file '%s' is not readable",
294-
symfile.GetPath().c_str());
295-
result.SetStatus(eReturnStatusFailed);
296-
return false;
297-
}
298-
} else {
299-
char symfile_path[PATH_MAX];
300-
symfile.GetPath(symfile_path, sizeof(symfile_path));
301-
result.AppendErrorWithFormat("invalid symbol file path '%s'",
302-
symfile_path);
289+
auto file = FileSystem::Instance().Open(
290+
symfile, lldb_private::File::eOpenOptionRead);
291+
292+
if (!file) {
293+
result.AppendErrorWithFormatv("Cannot open '{0}': {1}.",
294+
symfile.GetPath(),
295+
llvm::toString(file.takeError()));
303296
result.SetStatus(eReturnStatusFailed);
304297
return false;
305298
}
@@ -401,48 +394,34 @@ class CommandObjectTargetCreate : public CommandObjectParsed {
401394
if (module_sp)
402395
module_sp->SetPlatformFileSpec(remote_file);
403396
}
397+
404398
if (core_file) {
405-
char core_path[PATH_MAX];
406-
core_file.GetPath(core_path, sizeof(core_path));
407-
if (FileSystem::Instance().Exists(core_file)) {
408-
if (!FileSystem::Instance().Readable(core_file)) {
409-
result.AppendMessageWithFormat(
410-
"Core file '%s' is not readable.\n", core_path);
411-
result.SetStatus(eReturnStatusFailed);
412-
return false;
413-
}
414-
FileSpec core_file_dir;
415-
core_file_dir.GetDirectory() = core_file.GetDirectory();
416-
target_sp->AppendExecutableSearchPaths(core_file_dir);
399+
FileSpec core_file_dir;
400+
core_file_dir.GetDirectory() = core_file.GetDirectory();
401+
target_sp->AppendExecutableSearchPaths(core_file_dir);
417402

418-
ProcessSP process_sp(target_sp->CreateProcess(
419-
GetDebugger().GetListener(), llvm::StringRef(), &core_file));
403+
ProcessSP process_sp(target_sp->CreateProcess(
404+
GetDebugger().GetListener(), llvm::StringRef(), &core_file));
420405

421-
if (process_sp) {
422-
// Seems weird that we Launch a core file, but that is what we
423-
// do!
424-
error = process_sp->LoadCore();
406+
if (process_sp) {
407+
// Seems weird that we Launch a core file, but that is what we
408+
// do!
409+
error = process_sp->LoadCore();
425410

426-
if (error.Fail()) {
427-
result.AppendError(
428-
error.AsCString("can't find plug-in for core file"));
429-
result.SetStatus(eReturnStatusFailed);
430-
return false;
431-
} else {
432-
result.AppendMessageWithFormat(
433-
"Core file '%s' (%s) was loaded.\n", core_path,
434-
target_sp->GetArchitecture().GetArchitectureName());
435-
result.SetStatus(eReturnStatusSuccessFinishNoResult);
436-
}
437-
} else {
438-
result.AppendErrorWithFormat(
439-
"Unable to find process plug-in for core file '%s'\n",
440-
core_path);
411+
if (error.Fail()) {
412+
result.AppendError(
413+
error.AsCString("can't find plug-in for core file"));
441414
result.SetStatus(eReturnStatusFailed);
415+
return false;
416+
} else {
417+
result.AppendMessageWithFormatv("Core file '{0}' ({1}) was loaded.\n", core_file.GetPath(),
418+
target_sp->GetArchitecture().GetArchitectureName());
419+
result.SetStatus(eReturnStatusSuccessFinishNoResult);
442420
}
443421
} else {
444-
result.AppendErrorWithFormat("Core file '%s' does not exist\n",
445-
core_path);
422+
result.AppendErrorWithFormatv(
423+
"Unable to find process plug-in for core file '{0}'\n",
424+
core_file.GetPath());
446425
result.SetStatus(eReturnStatusFailed);
447426
}
448427
} else {

lldb/test/API/commands/target/basic/TestTargetCommand.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def test_target_create_multiple_args(self):
326326
@no_debug_info_test
327327
def test_target_create_nonexistent_core_file(self):
328328
self.expect("target create -c doesntexist", error=True,
329-
substrs=["core file 'doesntexist' doesn't exist"])
329+
substrs=["Cannot open 'doesntexist': No such file or directory"])
330330

331331
# Write only files don't seem to be supported on Windows.
332332
@skipIfWindows
@@ -335,12 +335,12 @@ def test_target_create_unreadable_core_file(self):
335335
tf = tempfile.NamedTemporaryFile()
336336
os.chmod(tf.name, stat.S_IWRITE)
337337
self.expect("target create -c '" + tf.name + "'", error=True,
338-
substrs=["core file '", "' is not readable"])
338+
substrs=["Cannot open '", "': Permission denied"])
339339

340340
@no_debug_info_test
341341
def test_target_create_nonexistent_sym_file(self):
342342
self.expect("target create -s doesntexist doesntexisteither", error=True,
343-
substrs=["invalid symbol file path 'doesntexist'"])
343+
substrs=["Cannot open '", "': No such file or directory"])
344344

345345
@skipIfWindows
346346
@no_debug_info_test
@@ -357,7 +357,7 @@ def test_target_create_unreadable_sym_file(self):
357357
tf = tempfile.NamedTemporaryFile()
358358
os.chmod(tf.name, stat.S_IWRITE)
359359
self.expect("target create -s '" + tf.name + "' no_exe", error=True,
360-
substrs=["symbol file '", "' is not readable"])
360+
substrs=["Cannot open '", "': Permission denied"])
361361

362362
@no_debug_info_test
363363
def test_target_delete_all(self):

0 commit comments

Comments
 (0)