1# 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, get_bb_vars , 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 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) 36 image = 'core-image-minimal' 37 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image) 38 39 with tempfile.TemporaryDirectory(prefix="debugfs-") as debugfs: 40 filename = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], "%s-dbg.tar.bz2" % bb_vars['IMAGE_LINK_NAME']) 41 shutil.unpack_archive(filename, debugfs) 42 filename = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], "%s.tar.bz2" % bb_vars['IMAGE_LINK_NAME']) 43 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 \".*\" 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() 68