blob: a911a78b47fe532e6debb00df4730c98a1c86663 [file] [log] [blame]
Klement Sekera909a6a12017-08-08 04:33:53 +02001""" debug utilities """
2
3import os
4import pexpect
juraj.linkes40dd73b2018-09-21 13:55:16 +02005import sys
Klement Sekera909a6a12017-08-08 04:33:53 +02006
Klement Sekerae2636852021-03-16 12:52:12 +01007from sanity_run_vpp import SanityTestCase
8from shutil import rmtree
Klement Sekera1dc146e2021-07-01 14:55:46 +02009from cpu_config import available_cpus
Klement Sekerae2636852021-03-16 12:52:12 +010010
Klement Sekera909a6a12017-08-08 04:33:53 +020011gdb_path = '/usr/bin/gdb'
12
13
juraj.linkes40dd73b2018-09-21 13:55:16 +020014def spawn_gdb(binary_path, core_path):
Klement Sekera909a6a12017-08-08 04:33:53 +020015 if os.path.isfile(gdb_path) and os.access(gdb_path, os.X_OK):
16 # automatically attach gdb
17 gdb_cmdline = "%s %s %s" % (gdb_path, binary_path, core_path)
18 gdb = pexpect.spawn(gdb_cmdline)
19 gdb.interact()
20 try:
21 gdb.terminate(True)
22 except:
23 pass
24 if gdb.isalive():
25 raise Exception("GDB refused to die...")
26 else:
juraj.linkes40dd73b2018-09-21 13:55:16 +020027 sys.stderr.write("Debugger '%s' does not exist or is not "
28 "an executable..\n" % gdb_path)
Klement Sekerae2636852021-03-16 12:52:12 +010029
30
31def start_vpp_in_gdb():
32 # here we use SanityTestCase as a dummy to inherit functionality,
33 # but any test case class could be used ...
34 SanityTestCase.set_debug_flags("attach")
35 SanityTestCase.tempdir = SanityTestCase.get_tempdir()
36 if os.path.exists(SanityTestCase.tempdir):
37 if os.getenv("VPP_IN_GDB_NO_RMDIR", "0") in ["1", "y", "yes"]:
38 raise FileExistsError(
39 "Temporary directory exists and removal denied.")
40 print("Removing existing temp dir '%s'." % SanityTestCase.tempdir)
41 rmtree(SanityTestCase.tempdir)
42 print("Creating temp dir '%s'." % SanityTestCase.tempdir)
43 os.mkdir(SanityTestCase.tempdir)
Klement Sekera1dc146e2021-07-01 14:55:46 +020044 SanityTestCase.assign_cpus(
45 available_cpus[:SanityTestCase.get_cpus_required()])
Klement Sekerae2636852021-03-16 12:52:12 +010046 SanityTestCase.setUpConstants()
47 vpp_cmdline = SanityTestCase.vpp_cmdline
48 if os.getenv("VPP_IN_GDB_CMDLINE", "y").lower() in ["1", "y", "yes"]:
49 print("Hacking cmdline to make VPP interactive.")
50 vpp_cmdline.insert(vpp_cmdline.index("nodaemon"), "interactive")
51 print("VPP cmdline is %s" % " ".join(vpp_cmdline))
52 print("Running GDB.")
53
54 if os.path.isfile(gdb_path) and os.access(gdb_path, os.X_OK):
55 gdb_cmdline = "%s --args %s " % (gdb_path, " ".join(vpp_cmdline))
56 print("GDB cmdline is %s" % gdb_cmdline)
57 gdb = pexpect.spawn(gdb_cmdline)
58 gdb.interact()
59 try:
60 gdb.terminate(True)
61 except:
62 pass
63 if gdb.isalive():
64 raise Exception("GDB refused to die...")
65 else:
66 sys.stderr.write("Debugger '%s' does not exist or is not "
67 "an executable..\n" % gdb_path)