tester/gdb: mi_parser, fix on Python3 and remove globals

- Fix mi_parser on Python3. Python3 does not support the __cmp__
  operator and rich comparision operators are required, See PEP 207.
- Remove the global variables and make a class containing them. Update
  the gdb class to use the mi_parser session class. Removing the globals
  means the global lock in the gdb module can be removed.
This commit is contained in:
Chris Johns
2018-11-26 09:56:50 +11:00
parent f632bd8b60
commit 71cede05ae
3 changed files with 61 additions and 50 deletions

View File

@@ -51,15 +51,11 @@ from rtemstoolkit import path
import console import console
import pygdb import pygdb
#
# The MI parser needs a global lock. It has global objects.
#
mi_lock = threading.Lock()
class gdb(object): class gdb(object):
'''RTEMS Testing GDB base.''' '''RTEMS Testing GDB base.'''
def __init__(self, bsp_arch, bsp, trace = False, mi_trace = False): def __init__(self, bsp_arch, bsp, trace = False, mi_trace = False):
self.session = pygdb.mi_parser.session()
self.trace = trace self.trace = trace
self.mi_trace = mi_trace self.mi_trace = mi_trace
self.lock_trace = False self.lock_trace = False
@@ -90,12 +86,6 @@ class gdb(object):
print('|] UNLOCK:%s [|' % (msg)) print('|] UNLOCK:%s [|' % (msg))
self.lock.release() self.lock.release()
def _mi_lock(self):
mi_lock.acquire()
def _mi_unlock(self):
mi_lock.release()
def _put(self, text): def _put(self, text):
if self.trace: if self.trace:
print(')))', text) print(')))', text)
@@ -208,15 +198,12 @@ class gdb(object):
cleanup = self._cleanup) cleanup = self._cleanup)
finally: finally:
self._unlock('_open') self._unlock('_open')
try: self.gdb_console('gdb: %s' % (' '.join(cmds)))
self.gdb_console('gdb: %s' % (' '.join(cmds))) ec, proc = self.process.open(cmds, timeout = (timeout, self._timeout))
ec, proc = self.process.open(cmds, timeout = (timeout, self._timeout)) if self.trace:
if self.trace: print('gdb done', ec)
print('gdb done', ec) if ec > 0:
if ec > 0: raise error.general('gdb exec: %s: %s' % (cmds[0], os.strerror(ec)))
raise error.general('gdb exec: %s: %s' % (cmds[0], os.strerror(ec)))
except:
raise
self._lock('_open') self._lock('_open')
try: try:
self.process = None self.process = None
@@ -248,13 +235,9 @@ class gdb(object):
def gdb_parse(self, lines): def gdb_parse(self, lines):
try: try:
self._mi_lock() if self.mi_trace:
try: print('mi-data:', lines)
if self.mi_trace: rec = self.session.process(lines)
print('mi-data:', lines)
rec = pygdb.mi_parser.process(lines)
finally:
self._mi_unlock()
if self.mi_trace: if self.mi_trace:
print('mi-rec:', rec) print('mi-rec:', rec)
if rec.record_type == 'result': if rec.record_type == 'result':
@@ -302,7 +285,7 @@ class gdb(object):
self.output_buffer = self.output_buffer[last_lf + 1:] self.output_buffer = self.output_buffer[last_lf + 1:]
except: except:
if self.trace: if self.trace:
print('/// console output') print('/// exception: console output')
for line in lines.splitlines(): for line in lines.splitlines():
self.output(line) self.output(line)

View File

@@ -18,5 +18,4 @@
all = ['mi_parser'] all = ['mi_parser']
from . import mi_parser from . import mi_parser
scan = mi_parser.scan session = mi_parser.session
process = mi_parser.process

View File

@@ -35,28 +35,48 @@ import pprint
from . import spark from . import spark
def __private(): def _private():
class Token: class Token(object):
def __init__(self, type, value=None): def __init__(self, type, value=None):
self.type = type self.type = type
self.value = value self.value = value
def __cmp__(self, o): def __lt__(self, o):
return cmp(self.type, o) return self.type < o
def __gt__(self, o):
return self.type > o
def __le__(self, o):
return self.type <= o
def __ge__(self, o):
return self.type >= o
def __eq__(self, o):
return self.type == o
def __ne__(self, o):
return self.type != o
def __repr__(self): def __repr__(self):
return self.value or self.type return self.value or self.type
class AST: class AST(object):
def __init__(self, type): def __init__(self, type):
self.type = type self.type = type
self._kids = [] self._kids = []
def __getitem__(self, i): def __getitem__(self, i):
return self._kids[i] return self._kids[i]
def __setitem__(self, i, k):
self._kids[i] = k
def __len__(self): def __len__(self):
return len(self._kids) return len(self._kids)
def __setslice__(self, low, high, seq): def __lt__(self, o):
self._kids[low:high] = seq return self.type < o
def __cmp__(self, o): def __gt__(self, o):
return cmp(self.type, o) return self.type > o
def __le__(self, o):
return self.type <= o
def __ge__(self, o):
return self.type >= o
def __eq__(self, o):
return self.type == o
def __ne__(self, o):
return self.type != o
class GdbMiScannerBase(spark.GenericScanner): class GdbMiScannerBase(spark.GenericScanner):
def tokenize(self, input): def tokenize(self, input):
@@ -300,7 +320,7 @@ def __private():
#def default(self, node): #def default(self, node):
#print 'default: ' + node.type #print 'default: ' + node.type
class GdbDynamicObject: class GdbDynamicObject(object):
def __init__(self, dict_): def __init__(self, dict_):
self.graft(dict_) self.graft(dict_)
@@ -355,20 +375,29 @@ def __private():
return (GdbMiScanner(), GdbMiParser(), GdbMiInterpreter, GdbMiRecord) return (GdbMiScanner(), GdbMiParser(), GdbMiInterpreter, GdbMiRecord)
(__the_scanner, __the_parser, __the_interpreter, __the_output) = __private()
def scan(input): class session(object):
return __the_scanner.tokenize(input) def __init__(self):
(self.the_scanner,
self.the_parser,
self.the_interpreter,
self.the_output) = _private()
def parse(tokens): def scan(self, input):
return __the_parser.parse(tokens) return self.the_scanner.tokenize(input)
def process(input): def parse(self, tokens):
tokens = scan(input) return self.the_parser.parse(tokens)
ast = parse(tokens)
__the_interpreter(ast)
return __the_output(ast.value)
def process(self, input):
tokens = self.scan(input)
ast = self.parse(tokens)
self.the_interpreter(ast)
return self.the_output(ast.value)
#
# Not updated with the session class
#
if __name__ == '__main__': if __name__ == '__main__':
def main(): def main():
def print_tokens(tokens): def print_tokens(tokens):