blob: 9da97ae78037bfd13434e93cf235c62fbbcb66fe [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
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050013from oeqa.utils.commands import bitbake, get_bb_var, get_bb_vars , runqemu, runCmd
Patrick Williams92b42cb2022-09-03 06:53:57 -050014
15class GdbServerTest(OESelftestTestCase):
16 def test_gdb_server(self):
17 target_arch = self.td["TARGET_ARCH"]
18 target_sys = self.td["TARGET_SYS"]
Patrick Williams92b42cb2022-09-03 06:53:57 -050019
20 features = """
21IMAGE_GEN_DEBUGFS = "1"
22IMAGE_FSTYPES_DEBUGFS = "tar.bz2"
23CORE_IMAGE_EXTRA_INSTALL = "gdbserver"
24 """
25 self.write_config(features)
26
27 gdb_recipe = "gdb-cross-" + target_arch
28 gdb_binary = target_sys + "-gdb"
29
30 bitbake("core-image-minimal %s:do_addto_recipe_sysroot" % gdb_recipe)
31
32 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", gdb_recipe)
33 r = runCmd("%s --version" % gdb_binary, native_sysroot=native_sysroot, target_sys=target_sys)
34 self.assertEqual(r.status, 0)
35 self.assertIn("GNU gdb", r.output)
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050036 image = 'core-image-minimal'
37 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image)
Patrick Williams92b42cb2022-09-03 06:53:57 -050038
39 with tempfile.TemporaryDirectory(prefix="debugfs-") as debugfs:
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050040 filename = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], "%s-dbg.tar.bz2" % bb_vars['IMAGE_LINK_NAME'])
Patrick Williams92b42cb2022-09-03 06:53:57 -050041 shutil.unpack_archive(filename, debugfs)
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050042 filename = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], "%s.tar.bz2" % bb_vars['IMAGE_LINK_NAME'])
Patrick Williams92b42cb2022-09-03 06:53:57 -050043 shutil.unpack_archive(filename, debugfs)
44
45 with runqemu("core-image-minimal", runqemuparams="nographic") as qemu:
46 status, output = qemu.run_serial("kmod --help")
47 self.assertIn("modprobe", output)
48
49 with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
50 def run_gdb():
51 for _ in range(5):
52 time.sleep(2)
53 cmd = "%s --batch -ex 'set sysroot %s' -ex \"target extended-remote %s:9999\" -ex \"info line kmod_help\"" % (gdb_binary, debugfs, qemu.ip)
54 self.logger.warning("starting gdb %s" % cmd)
55 r = runCmd(cmd, native_sysroot=native_sysroot, target_sys=target_sys)
56 self.assertEqual(0, r.status)
57 line_re = r"Line \d+ of \"/usr/src/debug/kmod/.*/tools/kmod.c\" starts at address 0x[0-9A-Fa-f]+ <kmod_help>"
58 self.assertRegex(r.output, line_re)
59 break
60 else:
61 self.fail("Timed out connecting to gdb")
62 future = executor.submit(run_gdb)
63
64 status, output = qemu.run_serial("gdbserver --once :9999 kmod --help")
65 self.assertEqual(status, 1)
66 # The future either returns None, or raises an exception
67 future.result()