blob: 3621d9c13eb9206aefc62e6e16d29ce35a4a8fa1 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6import os
7import time
8import tempfile
9import shutil
10import concurrent.futures
11
12from oeqa.selftest.case import OESelftestTestCase
13from oeqa.utils.commands import bitbake, get_bb_var, runqemu, runCmd
14
15class GdbServerTest(OESelftestTestCase):
16 def test_gdb_server(self):
17 target_arch = self.td["TARGET_ARCH"]
18 target_sys = self.td["TARGET_SYS"]
19 deploy_dir = get_bb_var("DEPLOY_DIR_IMAGE")
20
21 features = """
22IMAGE_GEN_DEBUGFS = "1"
23IMAGE_FSTYPES_DEBUGFS = "tar.bz2"
24CORE_IMAGE_EXTRA_INSTALL = "gdbserver"
25 """
26 self.write_config(features)
27
28 gdb_recipe = "gdb-cross-" + target_arch
29 gdb_binary = target_sys + "-gdb"
30
31 bitbake("core-image-minimal %s:do_addto_recipe_sysroot" % gdb_recipe)
32
33 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", gdb_recipe)
34 r = runCmd("%s --version" % gdb_binary, native_sysroot=native_sysroot, target_sys=target_sys)
35 self.assertEqual(r.status, 0)
36 self.assertIn("GNU gdb", r.output)
37
38 with tempfile.TemporaryDirectory(prefix="debugfs-") as debugfs:
39 filename = os.path.join(deploy_dir, "core-image-minimal-%s-dbg.tar.bz2" % self.td["MACHINE"])
40 shutil.unpack_archive(filename, debugfs)
41 filename = os.path.join(deploy_dir, "core-image-minimal-%s.tar.bz2" % self.td["MACHINE"])
42 shutil.unpack_archive(filename, debugfs)
43
44 with runqemu("core-image-minimal", runqemuparams="nographic") as qemu:
45 status, output = qemu.run_serial("kmod --help")
46 self.assertIn("modprobe", output)
47
48 with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
49 def run_gdb():
50 for _ in range(5):
51 time.sleep(2)
52 cmd = "%s --batch -ex 'set sysroot %s' -ex \"target extended-remote %s:9999\" -ex \"info line kmod_help\"" % (gdb_binary, debugfs, qemu.ip)
53 self.logger.warning("starting gdb %s" % cmd)
54 r = runCmd(cmd, native_sysroot=native_sysroot, target_sys=target_sys)
55 self.assertEqual(0, r.status)
56 line_re = r"Line \d+ of \"/usr/src/debug/kmod/.*/tools/kmod.c\" starts at address 0x[0-9A-Fa-f]+ <kmod_help>"
57 self.assertRegex(r.output, line_re)
58 break
59 else:
60 self.fail("Timed out connecting to gdb")
61 future = executor.submit(run_gdb)
62
63 status, output = qemu.run_serial("gdbserver --once :9999 kmod --help")
64 self.assertEqual(status, 1)
65 # The future either returns None, or raises an exception
66 future.result()