| 1 |
|
| 2 |
import sys |
| 3 |
|
| 4 |
if sys.version_info < (3, 10): |
| 5 |
print( |
| 6 |
f'A minimum Python version of at least 3.10 is required, ' |
| 7 |
f'{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro} was found', |
| 8 |
file=sys.stderr |
| 9 |
) |
| 10 |
sys.exit(1) |
| 11 |
|
| 12 |
import argparse |
| 13 |
import re |
| 14 |
import shutil |
| 15 |
import subprocess |
| 16 |
import shlex |
| 17 |
import tempfile |
| 18 |
|
| 19 |
|
| 20 |
class CoreDumper(): |
| 21 |
def __init__(self): |
| 22 |
self.build_directory = None |
| 23 |
self.app = None |
| 24 |
self.coredumpctl_matches = "" |
| 25 |
self.extra_flatpak_args = "" |
| 26 |
self.gdb_arguments = "" |
| 27 |
|
| 28 |
def clean_args(self): |
| 29 |
if not self.build_directory and not self.app: |
| 30 |
print("Either `--build-directory` or `APP` must be specified.", |
| 31 |
file=sys.stderr) |
| 32 |
return False |
| 33 |
|
| 34 |
return True |
| 35 |
|
| 36 |
def run(self): |
| 37 |
if not shutil.which("coredumpctl"): |
| 38 |
print("'coredumpctl' not present on the system, can't run.", |
| 39 |
file=sys.stderr) |
| 40 |
sys.exit(1) |
| 41 |
|
| 42 |
|
| 43 |
flatpak_command = ["flatpak", "build" if self.build_directory else "run", "--filesystem=home", |
| 44 |
f"--filesystem={tempfile.gettempdir()}"] + shlex.split(self.extra_flatpak_args) |
| 45 |
if not self.build_directory: |
| 46 |
flatpak_command.extend(["--command=gdb", |
| 47 |
"--devel", self.app]) |
| 48 |
else: |
| 49 |
flatpak_command.extend([self.build_directory, "gdb"]) |
| 50 |
|
| 51 |
with tempfile.NamedTemporaryFile() as coredump: |
| 52 |
dumpres = subprocess.run( |
| 53 |
["coredumpctl", "dump"] + shlex.split(self.coredumpctl_matches), |
| 54 |
stdout=coredump, stderr=subprocess.PIPE, text=True |
| 55 |
) |
| 56 |
if dumpres.returncode != 0: |
| 57 |
print("Failed to retrieve coredump via coredumpctl.", file=sys.stderr) |
| 58 |
if dumpres.stderr: |
| 59 |
print(f"Reason:\n{dumpres.stderr}", file=sys.stderr) |
| 60 |
|
| 61 |
sys.exit(dumpres.returncode) |
| 62 |
|
| 63 |
matches = re.findall(r".*Executable: (.*)", dumpres.stderr) |
| 64 |
if len(matches) != 1: |
| 65 |
print(f"Could not determine executable from coredumpctl output " |
| 66 |
f"(found {len(matches)} 'Executable:' line(s)).", |
| 67 |
file=sys.stderr) |
| 68 |
sys.exit(1) |
| 69 |
executable = matches[0] |
| 70 |
if not executable.startswith(("/newroot/", "/app/")): |
| 71 |
print(f"Executable {executable} doesn't seem to be a flatpaked application.", |
| 72 |
file=sys.stderr) |
| 73 |
executable = executable.replace("/newroot", "", 1) |
| 74 |
flatpak_command.extend([executable, coredump.name]) |
| 75 |
flatpak_command.extend(shlex.split(self.gdb_arguments)) |
| 76 |
|
| 77 |
print(f"Running: `{shlex.join(flatpak_command)}`") |
| 78 |
sys.exit(subprocess.run(flatpak_command).returncode) |
| 79 |
|
| 80 |
|
| 81 |
if __name__ == "__main__": |
| 82 |
parser = argparse.ArgumentParser(description= |
| 83 |
"Debug in gdb an application that crashed inside flatpak." |
| 84 |
" It uses (and thus requires) coredumpctl to retrieve the coredump file.") |
| 85 |
parser.add_argument("-b", "--build-directory", default=None, |
| 86 |
help="The build directory to use. It allows you to retrieve a coredump" |
| 87 |
" for applications being built") |
| 88 |
parser.add_argument("--extra-flatpak-args", default="", |
| 89 |
help="Extra argument to pass to flatpak") |
| 90 |
parser.add_argument("app", nargs="?", |
| 91 |
help="The flatpak application to use. eg. `org.gnome.Epiphany//3.28`.") |
| 92 |
parser.add_argument("-m", "--coredumpctl-matches", default="", nargs="?", |
| 93 |
help="Coredumpctl matches, see `man coredumpctl` for more information.") |
| 94 |
parser.add_argument("--gdb-arguments", default="", |
| 95 |
help="Arguments to pass to gdb.") |
| 96 |
|
| 97 |
coredumper = CoreDumper() |
| 98 |
options = parser.parse_args(namespace=coredumper) |
| 99 |
if not coredumper.clean_args(): |
| 100 |
parser.print_help(sys.stderr) |
| 101 |
sys.exit(1) |
| 102 |
|
| 103 |
coredumper.run() |