chiark / gitweb /
fishdescriptor: sort out debugging output
[chiark-utils.git] / fishdescriptor / py / fishdescriptor / fish.py
index 718456a28536516e2eaaa472f10e9cd60d87a509..a506938964f71671c757b57decdceacb9039151a 100644 (file)
@@ -4,18 +4,27 @@ import socket
 import subprocess
 import os
 import pwd
+import struct
+import tempfile
+import shutil
+import sys
 
 def _shuffle_fd3():
     os.dup2(1,3)
     os.dup2(2,1)
 
 class Donor():
-    def __init__(d, pid):
-        d._pid = pid
+    def __init__(d, pid, debug=None):
+        d.pid = pid
+        if debug is None:
+            d._stderr = tempfile.TemporaryFile(mode='w+')
+        else:
+            d._stderr = None
         d._sp = subprocess.Popen(
             preexec_fn = _shuffle_fd3,
             stdin = subprocess.PIPE,
             stdout = subprocess.PIPE,
+            stderr = d._stderr,
             close_fds = False,
             args = ['gdb', '-p', str(pid), '-batch', '-ex',
                     'python import fishdescriptor.indonor as id;'+
@@ -24,12 +33,20 @@ class Donor():
         )            
 
     def _eval_integer(d, expr):
-        l = d._sp.stdout.readline()
-        if l != b'!\n': raise RuntimeError("indonor said %s" % repr(l))
-        d._sp.stdin.write(expr.encode('utf-8') + b'\n')
-        d._sp.stdin.flush()
-        l = d._sp.stdout.readline().rstrip(b'\n')
-        return int(l)
+        try:
+            l = d._sp.stdout.readline()
+            if l != b'!\n': raise RuntimeError("indonor said %s" % repr(l))
+            d._sp.stdin.write(expr.encode('utf-8') + b'\n')
+            d._sp.stdin.flush()
+            l = d._sp.stdout.readline().rstrip(b'\n')
+            return int(l)
+        except Exception as e:
+            if d._stderr is not None:
+                d._stderr.seek(0)
+                shutil.copyfileobj(d._stderr, sys.stderr)
+                d._stderr.seek(0)
+                d._stderr.truncate()
+            raise e
 
     def _eval_success(d, expr):
         r = d._eval_integer(expr)
@@ -96,9 +113,9 @@ class Donor():
         sockdir = d._sock_dir(euid)
         d.mkdir(sockdir)
 
-        sockname = '%s/%s,%d' % (sockdir, os.uname().nodename, d._pid)
+        sockname = '%s/%s,%d' % (sockdir, os.uname().nodename, d.pid)
 
-        target_root = '/proc/%d/root' % d._pid
+        target_root = '/proc/%d/root' % d.pid
         if not d._exists(target_root):
             target_root = ''
 
@@ -119,11 +136,13 @@ class Donor():
             (s2, dummy) = s.accept()
             (msg, ancil, flags, sender) = s2.recvmsg(1, ancil_len)
 
-            got_fds = [ ]
+            got_fds = None
+            unpack_fmt = '%di' % len(fds)
 
             for clvl, ctype, cdata in ancil:
                 if clvl == socket.SOL_SOCKET and ctype == socket.SCM_RIGHTS:
-                    got_fds += cdata # need to trim any surplus, and unpack
+                    assert(got_fds is None)
+                    got_fds = struct.unpack_from(unpack_fmt, cdata)
 
         finally:
             if s is not None: s.close()
@@ -131,3 +150,8 @@ class Donor():
 
             try: os.remove(our_sockname)
             except FileNotFoundError: pass
+
+        return list(got_fds)
+
+    def detach(d):
+        d._sp.stdin.close()