Skip to content

Commit

Permalink
fixup! Fallback to the first executable map if the given executable i…
Browse files Browse the repository at this point in the history
…s invalid

Only fall back to first map when an executable wasn't explicitly provided
  • Loading branch information
godlygeek committed Jul 23, 2024
1 parent 595d8bd commit 0e46e16
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
41 changes: 23 additions & 18 deletions src/pystack/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,26 @@ def process_core(parser: argparse.ArgumentParser, args: argparse.Namespace) -> N
if args.executable is None:
corefile_analyzer = CoreFileAnalyzer(corefile)
executable = pathlib.Path(corefile_analyzer.extract_executable())
if not executable.exists() or not is_elf(executable):
first_map = next(
(
map
for map in corefile_analyzer.extract_maps()
if map.path is not None
),
None,
)
if (
first_map is not None
and first_map.path is not None
and first_map.path.exists()
and is_elf(first_map.path)
):
executable = first_map.path
LOGGER.info(
"Setting executable automatically to the first map in the core: %s",
executable,
)
if not executable.exists():
raise errors.DetectedExecutableNotFound(
f"Detected executable doesn't exist: {executable}"
Expand Down Expand Up @@ -356,25 +376,10 @@ def process_core(parser: argparse.ArgumentParser, args: argparse.Namespace) -> N
print(format_psinfo_information(corefile_analyzer.extract_ps_info()))
print(format_failureinfo_information(corefile_analyzer.extract_failure_info()))

if args.executable is not None and not is_elf(executable):
first_map = next(
(map for map in corefile_analyzer.extract_maps() if map.path is not None),
None,
if not is_elf(executable):
raise errors.InvalidExecutable(
f"The provided executable ({executable}) doesn't have an executable format"
)
if (
first_map is not None
and first_map.path is not None
and is_elf(first_map.path)
):
executable = first_map.path
LOGGER.info(
"Setting executable automatically to the first map in the core: %s",
executable,
)
else:
raise errors.InvalidExecutable(
f"The provided executable ({executable}) doesn't have an executable format"
)

if args.native_mode != NativeReportingMode.OFF:
for module in corefile_analyzer.missing_modules():
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ def test_process_core_default_without_executable_and_executable_does_not_exist(c
"extracted_executable"
)
# THEN
path_exists_mock.side_effect = [True, False]
path_exists_mock.side_effect = [True, False, False, False]

with pytest.raises(SystemExit):
main()
Expand Down Expand Up @@ -1304,8 +1304,7 @@ def test_core_file_missing_build_ids_are_logged(caplog, native):

def test_executable_is_not_elf_uses_the_first_map():
# GIVEN

argv = ["pystack", "core", "corefile", "executable"]
argv = ["pystack", "core", "corefile"]

# WHEN
real_executable = Path("/foo/bar/executable")
Expand Down

0 comments on commit 0e46e16

Please sign in to comment.