Skip to content

Commit

Permalink
Add script for debugging
Browse files Browse the repository at this point in the history
1. Setup handle SIGALRM ignore first and run qtest in gdb shell
2. Analyze core dump file
  • Loading branch information
kickasson committed Feb 23, 2020
1 parent e88bdd4 commit 5be37ab
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
8 changes: 5 additions & 3 deletions qtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -629,9 +629,11 @@ static bool do_show(int argc, char *argv[])
/* Signal handlers */
static void sigsegvhandler(int sig)
{
trigger_exception(
"Segmentation fault occurred. You dereferenced a NULL or invalid "
"pointer");
report(1,
"Segmentation fault occurred. You dereferenced a NULL or invalid "
"pointer");
/* Raising a SIGABRT signal to produce a core dump for debugging. */
abort();
}

static void sigalrmhandler(int sig)
Expand Down
83 changes: 83 additions & 0 deletions scripts/debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env python3

import argparse
import shutil
import subprocess
import pathlib
import os

class Debugger:
def __init__(self, command, common_opts):
self.command = command
self.common_opts = common_opts

def __call__(self, argv, **kwargs):
g = subprocess.Popen([self.command] + self.common_opts + argv, **kwargs)

while g.returncode != 0:
try:
g.communicate()
except KeyboardInterrupt:
pass

return g

def debug(self, program):
return self([
"-ex", "handle SIGALRM ignore",
"-ex", "run",
program
])

def analyze(self, program, core_file):
return self([
program,
core_file,
"-ex", "backtrace"
])


def main(argv):
common = [
"-quiet",
f"-cd={ROOT}"
]

gdb = Debugger(GDB_PATH, common)

if argv.debug:
gdb.debug(QTEST)

elif argv.analyze:
if not os.path.exists(CORE_DUMP):
print("ERROR: core dump file is not exist")
exit(1)

gdb.analyze(QTEST, CORE_DUMP)

else:
parser.print_help()

if __name__ == "__main__":
ROOT = str(pathlib.Path(__file__).resolve().parents[1])
GDB_PATH = shutil.which("gdb")
QTEST = ROOT + "/qtest"
CORE_DUMP = ROOT + "/core"

parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument("-d", "--debug", dest="debug", action="store_true",
help="Enter gdb shell")
group.add_argument("-a", "--analyze", dest="analyze", action="store_true",
help="Analyze the core dump file")
args = parser.parse_args()

if not GDB_PATH:
print("ERROR: gdb is not installed")
exit(1)

if not os.path.exists(QTEST):
print("ERROR: qtest is not exist")
exit(1)

main(args)

0 comments on commit 5be37ab

Please sign in to comment.