blob: e79f082615d210c004b2c071bf2397615170b0f7 [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
9
Klement Sekera909a6a12017-08-08 04:33:53 +020010gdb_path = '/usr/bin/gdb'
11
12
juraj.linkes40dd73b2018-09-21 13:55:16 +020013def spawn_gdb(binary_path, core_path):
Klement Sekera909a6a12017-08-08 04:33:53 +020014 if os.path.isfile(gdb_path) and os.access(gdb_path, os.X_OK):
15 # automatically attach gdb
16 gdb_cmdline = "%s %s %s" % (gdb_path, binary_path, core_path)
17 gdb = pexpect.spawn(gdb_cmdline)
18 gdb.interact()
19 try:
20 gdb.terminate(True)
21 except:
22 pass
23 if gdb.isalive():
24 raise Exception("GDB refused to die...")
25 else:
juraj.linkes40dd73b2018-09-21 13:55:16 +020026 sys.stderr.write("Debugger '%s' does not exist or is not "
27 "an executable..\n" % gdb_path)
Klement Sekerae2636852021-03-16 12:52:12 +010028
29
30def start_vpp_in_gdb():
31 # here we use SanityTestCase as a dummy to inherit functionality,
32 # but any test case class could be used ...
33 SanityTestCase.set_debug_flags("attach")
34 SanityTestCase.tempdir = SanityTestCase.get_tempdir()
35 if os.path.exists(SanityTestCase.tempdir):
36 if os.getenv("VPP_IN_GDB_NO_RMDIR", "0") in ["1", "y", "yes"]:
37 raise FileExistsError(
38 "Temporary directory exists and removal denied.")
39 print("Removing existing temp dir '%s'." % SanityTestCase.tempdir)
40 rmtree(SanityTestCase.tempdir)
41 print("Creating temp dir '%s'." % SanityTestCase.tempdir)
42 os.mkdir(SanityTestCase.tempdir)
43 SanityTestCase.setUpConstants()
44 vpp_cmdline = SanityTestCase.vpp_cmdline
45 if os.getenv("VPP_IN_GDB_CMDLINE", "y").lower() in ["1", "y", "yes"]:
46 print("Hacking cmdline to make VPP interactive.")
47 vpp_cmdline.insert(vpp_cmdline.index("nodaemon"), "interactive")
48 print("VPP cmdline is %s" % " ".join(vpp_cmdline))
49 print("Running GDB.")
50
51 if os.path.isfile(gdb_path) and os.access(gdb_path, os.X_OK):
52 gdb_cmdline = "%s --args %s " % (gdb_path, " ".join(vpp_cmdline))
53 print("GDB cmdline is %s" % gdb_cmdline)
54 gdb = pexpect.spawn(gdb_cmdline)
55 gdb.interact()
56 try:
57 gdb.terminate(True)
58 except:
59 pass
60 if gdb.isalive():
61 raise Exception("GDB refused to die...")
62 else:
63 sys.stderr.write("Debugger '%s' does not exist or is not "
64 "an executable..\n" % gdb_path)