2 # class for use inside gdb which is debugging the donor process
11 def _string_bytearray(s):
12 # gets us bytes in py2 and py3
13 if not isinstance(s, bytes):
14 s = s.encode('utf-8') # sigh, python 2/3 compat
17 def _string_escape_for_c(s):
19 for c in _string_bytearray(s):
20 if c == ord('\\') or c == ord('"') or c < 32 or c > 126:
31 def _lit_aggregate_uncasted(val_lit_strs):
32 return '{' + ', '.join(['(%s)' % v for v in val_lit_strs]) + ' }'
34 def _lit_string_uncasted(s):
35 b = _string_bytearray(s)
36 return _lit_aggregate_uncasted([_lit_integer(x) for x in b] + [ '0' ])
38 def _lit_array(elemtype, val_lit_strs):
41 (elemtype, len(val_lit_strs), _lit_aggregate_uncasted(val_lit_strs))
44 def _lit_addressof(v):
45 return '&(char[])(%s)' % v
48 if isinstance(v, int):
49 return _lit_integer(v)
51 return v # should already be an integer
54 sys.stderr.write("## EVAL %s\n" % repr(expr))
55 x = gdb.parse_and_eval(expr)
56 sys.stderr.write('## => %s\n' % x)
60 def parse_eval_via_print(expr):
61 # works only with things whose value is an int and where expr is simple
62 sys.stderr.write("## EVAL-VIA-PRINT %s\n" % repr(expr))
63 x = gdb.execute('print %s' % expr, to_string=True)
64 m = re.match('\$\d+ = (\d+)\n$', x) # seriously !
66 sys.stderr.write('## => %s\n' % r)
69 class DonorStructLayout():
70 def __init__(l, typename):
71 x = gdb.lookup_type(typename)
72 l._typename = typename
76 l._posns[f.name] = len(l._template)
77 try: f.type.fields(); blank = '{ }'
78 except TypeError: blank = '0'
79 except AttributeError: blank = '0'
80 l._template.append(blank)
81 sys.stderr.write('## STRUCT %s template %s fields %s\n'
82 % (typename, l._template, l._posns))
84 def substitute(l, values):
85 build = copy.deepcopy(l._template)
86 for (k,v) in values.items():
87 build[ l._posns[k] ] = _make_lit(v)
88 return '((%s)%s)' % (l._typename, _lit_aggregate_uncasted(build))
90 class DonorImplementation():
93 di._saved_errno = None
94 di._result_stream = os.fdopen(3, 'w')
97 # sigh, we have to record the order of the arguments!
98 def _find_fields(di, typename):
100 fields = di._structs[typename]
102 fields = DonorStructLayout(typename)
103 di._structs[typename] = fields
106 def _make(di, typename, values):
107 fields = di._find_fields(typename)
108 return fields.substitute(values)
110 # calling functions (need to cast the function name to the right
111 # type in case maybe gdb doesn't know the type)
113 def _func(di, functype, funcname, realargs):
114 expr = '((%s) %s) %s' % (functype, funcname, realargs)
115 return parse_eval(expr)
117 def _must_func(di, functype, funcname, realargs):
118 retval = di._func(functype, funcname, realargs)
120 errnoval = parse_eval('errno')
121 raise RuntimeError("%s gave errno=%d `%s'" %
122 (funcname, errnoval, os.strerror(errnoval)))
125 # wrappers for the syscalls that do what we want
127 def _sendmsg(di, carrier, control_msg):
128 iov_base = _lit_array('char', [1])
129 iov = di._make('struct iovec', {
130 'iov_base': iov_base,
134 msg = di._make('struct msghdr', {
135 'msg_iov' : _lit_addressof(iov),
137 'msg_control' : _lit_array('char', control_msg),
138 'msg_controllen': len(control_msg),
142 'ssize_t (*)(int, const struct msghdr*, int)',
144 '(%s, %s, 0)' % (carrier, _lit_addressof(msg))
148 return di._must_func(
149 'int (*)(int, int, int)',
151 '(%d, %d, 0)' % (socket.AF_UNIX, socket.SOCK_STREAM)
154 def _connect(di, fd, path):
155 addr = di._make('struct sockaddr_un', {
156 'sun_family' : _lit_integer(socket.AF_UNIX),
157 'sun_path' : _lit_string_uncasted(path),
161 'int (*)(int, const struct sockaddr*, socklen_t)',
163 '(%d, (const struct sockaddr*)%s, sizeof(struct sockaddr_un))'
164 % (fd, _lit_addressof(addr))
168 di._must_func('int (*)(int)', 'close', '(%d)' % fd)
170 def _mkdir(di, path, mode):
172 'int (*)(const char*, mode_t)',
174 '("%s", %d)' % (_string_escape_for_c(path), mode)
177 errnoval = parse_eval('errno')
178 if errnoval != os.errno.EEXIST:
179 raise RuntimeError("mkdir %s failed: `%s'" %
180 (repr(path), os.strerror(errnoval)))
185 # incomprehensibly, gdb.parse_and_eval('errno') can sometimes
187 # gdb.error: Cannot find thread-local variables on this target
188 # even though plain gdb `print errno' works.
189 # OMG. This may be related to:
190 # https://github.com/cloudburst/libheap/issues/24
191 # although I can't find it in the gdb bug db (which is half-broken
194 di._saved_errno = parse_eval_via_print('errno')
196 def _errno_restore(di):
197 to_restore = di._saved_errno
198 di._saved_errno = None
199 if to_restore is not None:
200 parse_eval_via_print('errno = %d' % to_restore)
202 def _result(di, output):
203 sys.stderr.write("#> %s" % output)
204 di._result_stream.write(output)
205 di._result_stream.flush()
209 def donate(di, path, control_msg):
210 # control_msg is an array of integers being the ancillary data
211 # array ("control") for sendmsg, and hence specifies which fds
217 carrier = di._socket()
218 di._connect(carrier, path)
219 di._sendmsg(carrier, control_msg)
223 if carrier is not None:
224 try: di._close(carrier)
225 except Exception: pass
233 val = di._must_func('uid_t (*)(void)', 'geteuid', '()')
237 di._result('%d\n' % val)
242 val = di._mkdir(path, int('0700', 8))
246 di._result('%d\n' % val)
248 def _protocol_read(di):
249 input = sys.stdin.readline()
250 if input == '': return None
251 input = input.rstrip('\n')
252 sys.stderr.write("#< %s\n" % input)
258 cmd = di._protocol_read()
259 if cmd is None: break