summaryrefslogtreecommitdiffstats
path: root/meta/lib/oeqa/selftest/cases/gdbserver.py
blob: 3621d9c13eb9206aefc62e6e16d29ce35a4a8fa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#
import os
import time
import tempfile
import shutil
import concurrent.futures

from oeqa.selftest.case import OESelftestTestCase
from oeqa.utils.commands import bitbake, get_bb_var, runqemu, runCmd

class GdbServerTest(OESelftestTestCase):
    def test_gdb_server(self):
        target_arch = self.td["TARGET_ARCH"]
        target_sys = self.td["TARGET_SYS"]
        deploy_dir = get_bb_var("DEPLOY_DIR_IMAGE")

        features = """
IMAGE_GEN_DEBUGFS = "1"
IMAGE_FSTYPES_DEBUGFS = "tar.bz2"
CORE_IMAGE_EXTRA_INSTALL = "gdbserver"
        """
        self.write_config(features)

        gdb_recipe = "gdb-cross-" + target_arch
        gdb_binary = target_sys + "-gdb"

        bitbake("core-image-minimal %s:do_addto_recipe_sysroot" % gdb_recipe)

        native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", gdb_recipe)
        r = runCmd("%s --version" % gdb_binary, native_sysroot=native_sysroot, target_sys=target_sys)
        self.assertEqual(r.status, 0)
        self.assertIn("GNU gdb", r.output)

        with tempfile.TemporaryDirectory(prefix="debugfs-") as debugfs:
            filename = os.path.join(deploy_dir, "core-image-minimal-%s-dbg.tar.bz2" % self.td["MACHINE"])
            shutil.unpack_archive(filename, debugfs)
            filename = os.path.join(deploy_dir, "core-image-minimal-%s.tar.bz2" % self.td["MACHINE"])
            shutil.unpack_archive(filename, debugfs)

            with runqemu("core-image-minimal", runqemuparams="nographic") as qemu:
                status, output = qemu.run_serial("kmod --help")
                self.assertIn("modprobe", output)

                with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
                    def run_gdb():
                        for _ in range(5):
                            time.sleep(2)
                            cmd = "%s --batch -ex 'set sysroot %s' -ex \"target extended-remote %s:9999\" -ex \"info line kmod_help\"" % (gdb_binary, debugfs, qemu.ip)
                            self.logger.warning("starting gdb %s" % cmd)
                            r = runCmd(cmd, native_sysroot=native_sysroot, target_sys=target_sys)
                            self.assertEqual(0, r.status)
                            line_re = r"Line \d+ of \"/usr/src/debug/kmod/.*/tools/kmod.c\" starts at address 0x[0-9A-Fa-f]+ <kmod_help>"
                            self.assertRegex(r.output, line_re)
                            break
                        else:
                            self.fail("Timed out connecting to gdb")
                    future = executor.submit(run_gdb)

                    status, output = qemu.run_serial("gdbserver --once :9999 kmod --help")
                    self.assertEqual(status, 1)
                    # The future either returns None, or raises an exception
                    future.result()