nat: nat44-ed add session timing out indicator in api
[vpp.git] / test / debug.py
1 """ debug utilities """
2
3 import os
4 import pexpect
5 import sys
6
7 from sanity_run_vpp import SanityTestCase
8 from shutil import rmtree
9 from cpu_config import available_cpus
10
11 gdb_path = '/usr/bin/gdb'
12
13
14 def spawn_gdb(binary_path, core_path):
15     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:
27         sys.stderr.write("Debugger '%s' does not exist or is not "
28                          "an executable..\n" % gdb_path)
29
30
31 def 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)
44     SanityTestCase.assign_cpus(
45         available_cpus[:SanityTestCase.get_cpus_required()])
46     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)