import gdb
import copy
import os
+import sys
+import socket
+import re
+
+def _string_bytearray(s):
+ # gets us bytes in py2 and py3
+ if not isinstance(s, bytes):
+ s = s.encode('utf-8') # sigh, python 2/3 compat
+ return bytearray(s)
def _string_escape_for_c(s):
out = ''
- for c in bytearray(s): # gets us bytes in py2 and py3
+ for c in _string_bytearray(s):
if c == ord('\\') or c == ord('"') or c < 32 or c > 126:
out += '\\x%02x' % c
else:
return '{' + ', '.join(['(%s)' % v for v in val_lit_strs]) + ' }'
def _lit_string_uncasted(s):
- if not isinstance(s, bytes):
- s = s.encode('utf-8') # sigh, python 2/3 compat
- b = bytearray(s)
- return _lit_aggregate_uncasted(map(_lit_integer, b) + [ '0' ])
+ b = _string_bytearray(s)
+ return _lit_aggregate_uncasted([_lit_integer(x) for x in b] + [ '0' ])
def _lit_array(elemtype, val_lit_strs):
return (
'((%s[%d])%s)' %
- (elem_type, len(val_lit_strs), _lit_aggregate_uncasted(val_lit_strs))
+ (elemtype, len(val_lit_strs), _lit_aggregate_uncasted(val_lit_strs))
)
def _lit_addressof(v):
- return '&(%s)' % v
+ return '&(char[])(%s)' % v
def _make_lit(v):
if isinstance(v, int):
else:
return v # should already be an integer
+def parse_eval(expr):
+ sys.stderr.write("## EVAL %s\n" % repr(expr))
+ x = gdb.parse_and_eval(expr)
+ sys.stderr.write('## => %s\n' % x)
+ sys.stderr.flush()
+ return x
+
+def parse_eval_via_print(expr):
+ # works only with things whose value is an int and where expr is simple
+ sys.stderr.write("## EVAL-VIA-PRINT %s\n" % repr(expr))
+ x = gdb.execute('print %s' % expr, to_string=True)
+ m = re.match('\$\d+ = (\d+)\n$', x) # seriously !
+ r = int(m.group(1))
+ sys.stderr.write('## => %s\n' % r)
+ return 4
+
class DonorStructLayout():
def __init__(l, typename):
- x = gdb.parse_and_eval('(%s){ }' % typename)
+ x = gdb.lookup_type(typename)
l._typename = typename
l._template = [ ]
l._posns = { }
- for f in x.type.fields():
+ for f in x.fields():
l._posns[f.name] = len(l._template)
- try: f.type.fields(); blank = '{ }'
+ try: f.type.fields(); blank = '{ }'
+ except TypeError: blank = '0'
except AttributeError: blank = '0'
l._template.append(blank)
+ sys.stderr.write('## STRUCT %s template %s fields %s\n'
+ % (typename, l._template, l._posns))
+
def substitute(l, values):
build = copy.deepcopy(l._template)
for (k,v) in values.items():
# assembling structs
# sigh, we have to record the order of the arguments!
- def _find_fields(typename):
+ def _find_fields(di, typename):
try:
fields = di._structs[typename]
- except AttributeError:
+ except KeyError:
fields = DonorStructLayout(typename)
di._structs[typename] = fields
return fields
- def _make(typename, values):
+ def _make(di, typename, values):
fields = di._find_fields(typename)
return fields.substitute(values)
def _func(di, functype, funcname, realargs):
expr = '((%s) %s) %s' % (functype, funcname, realargs)
- return gdb.parse_and_eval(expr)
+ return parse_eval(expr)
def _must_func(di, functype, funcname, realargs):
retval = di._func(functype, funcname, realargs)
if retval < 0:
- errnoval = gdb.parse_and_eval('errno')
+ errnoval = parse_eval('errno')
raise RuntimeError("%s gave errno=%d `%s'" %
(funcname, errnoval, os.strerror(errnoval)))
+ return retval
# wrappers for the syscalls that do what we want
def _sendmsg(di, carrier, control_msg):
- iov_base = _lit_array('int', map(str,fds))
+ iov_base = _lit_array('char', [1])
iov = di._make('struct iovec', {
'iov_base': iov_base,
- 'iov_len' : len(fds),
+ 'iov_len' : 1,
})
msg = di._make('struct msghdr', {
})
di._must_func(
- 'ssize_t (*)(int, const struct msghdr*, int flags)',
+ 'ssize_t (*)(int, const struct msghdr*, int)',
'sendmsg',
'(%s, %s, 0)' % (carrier, _lit_addressof(msg))
)
'("%s", %d)' % (_string_escape_for_c(path), mode)
)
if r < 0:
- errnoval = gdb.parse_and_eval('errno')
+ errnoval = parse_eval('errno')
if errnoval != os.errno.EEXIST:
raise RuntimeError("mkdir %s failed: `%s'" %
(repr(path), os.strerror(errnoval)))
return 1
def _errno_save(di):
- di._saved_errno = gdb.parse_and_eval('errno')
+ # incomprehensibly, gdb.parse_and_eval('errno') can sometimes
+ # fail with
+ # gdb.error: Cannot find thread-local variables on this target
+ # even though plain gdb `print errno' works.
+ # OMG. This may be related to:
+ # https://github.com/cloudburst/libheap/issues/24
+ # although I can't find it in the gdb bug db (which is half-broken
+ # in my browser)
+ # Anyway:
+ di._saved_errno = parse_eval_via_print('errno')
def _errno_restore(di):
to_restore = di._saved_errno
di._saved_errno = None
if to_restore is not None:
- gdb.parse_and_eval('errno = %d' % to_restore)
+ parse_eval_via_print('errno = %d' % to_restore)
- # main entrypoints
+ def _result(di, output):
+ sys.stderr.write("#> %s" % output)
+ di._result_stream.write(output)
+ di._result_stream.flush()
- def result(di, output):
- di._result_stram.write(output)
- di._result_stram.flush()
+ # main entrypoints
def donate(di, path, control_msg):
# control_msg is an array of integers being the ancillary data
def mkdir(di, path):
try:
di._errno_save()
- val = di._mkdir(path, '0700')
+ val = di._mkdir(path, int('0700', 8))
finally:
di._errno_restore()
di._result('%d\n' % val)
def _protocol_read(di):
- return sys.stdin.readline().rstrip('\n')
+ input = sys.stdin.readline()
+ if input == '': return None
+ input = input.rstrip('\n')
+ sys.stderr.write("#< %s\n" % input)
+ return input
def eval_loop(di):
while True:
di._result('!\n')
cmd = di._protocol_read()
+ if cmd is None: break
eval(cmd)