blob: d77fd87f3e5a6764479a5eeff49672f8920cefb0 [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 Sekerab23ffd72021-05-31 16:08:53 +02009from config import available_cpus
Klement Sekerae2636852021-03-16 12:52:12 +010010
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020011gdb_path = "/usr/bin/gdb"
Klement Sekera909a6a12017-08-08 04:33:53 +020012
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:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020027 sys.stderr.write(
28 "Debugger '%s' does not exist or is not an executable..\n" % gdb_path
29 )
Klement Sekerae2636852021-03-16 12:52:12 +010030
31
32def start_vpp_in_gdb():
33 # here we use SanityTestCase as a dummy to inherit functionality,
34 # but any test case class could be used ...
35 SanityTestCase.set_debug_flags("attach")
36 SanityTestCase.tempdir = SanityTestCase.get_tempdir()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020037 SanityTestCase.assign_cpus(available_cpus[: SanityTestCase.get_cpus_required()])
Klement Sekerae2636852021-03-16 12:52:12 +010038 SanityTestCase.setUpConstants()
39 vpp_cmdline = SanityTestCase.vpp_cmdline
Klement Sekerab23ffd72021-05-31 16:08:53 +020040 print("Hacking cmdline to make VPP interactive.")
41 vpp_cmdline.insert(vpp_cmdline.index("nodaemon"), "interactive")
Klement Sekerae2636852021-03-16 12:52:12 +010042 print("VPP cmdline is %s" % " ".join(vpp_cmdline))
43 print("Running GDB.")
44
45 if os.path.isfile(gdb_path) and os.access(gdb_path, os.X_OK):
46 gdb_cmdline = "%s --args %s " % (gdb_path, " ".join(vpp_cmdline))
47 print("GDB cmdline is %s" % gdb_cmdline)
48 gdb = pexpect.spawn(gdb_cmdline)
49 gdb.interact()
50 try:
51 gdb.terminate(True)
52 except:
53 pass
54 if gdb.isalive():
55 raise Exception("GDB refused to die...")
56 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020057 sys.stderr.write(
58 "Debugger '%s' does not exist or is not an executable..\n" % gdb_path
59 )