Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 1 | """ debug utilities """ |
| 2 | |
| 3 | import os |
| 4 | import pexpect |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 5 | import sys |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 6 | |
Klement Sekera | e263685 | 2021-03-16 12:52:12 +0100 | [diff] [blame] | 7 | from sanity_run_vpp import SanityTestCase |
| 8 | from shutil import rmtree |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 9 | from config import available_cpus |
Klement Sekera | e263685 | 2021-03-16 12:52:12 +0100 | [diff] [blame] | 10 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 11 | gdb_path = "/usr/bin/gdb" |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 12 | |
| 13 | |
juraj.linkes | 40dd73b | 2018-09-21 13:55:16 +0200 | [diff] [blame] | 14 | def spawn_gdb(binary_path, core_path): |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 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: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 27 | sys.stderr.write( |
| 28 | "Debugger '%s' does not exist or is not an executable..\n" % gdb_path |
| 29 | ) |
Klement Sekera | e263685 | 2021-03-16 12:52:12 +0100 | [diff] [blame] | 30 | |
| 31 | |
| 32 | def 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 37 | SanityTestCase.assign_cpus(available_cpus[: SanityTestCase.get_cpus_required()]) |
Klement Sekera | e263685 | 2021-03-16 12:52:12 +0100 | [diff] [blame] | 38 | SanityTestCase.setUpConstants() |
| 39 | vpp_cmdline = SanityTestCase.vpp_cmdline |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 40 | print("Hacking cmdline to make VPP interactive.") |
| 41 | vpp_cmdline.insert(vpp_cmdline.index("nodaemon"), "interactive") |
Klement Sekera | e263685 | 2021-03-16 12:52:12 +0100 | [diff] [blame] | 42 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 57 | sys.stderr.write( |
| 58 | "Debugger '%s' does not exist or is not an executable..\n" % gdb_path |
| 59 | ) |