chiark / gitweb /
fishdescriptor: sort out debugging output
[chiark-utils.git] / fishdescriptor / py / fishdescriptor / fish.py
index 763684bd4db460414dfcffbf91a4a2537c814d7f..a506938964f71671c757b57decdceacb9039151a 100644 (file)
@@ -5,18 +5,26 @@ 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;'+
@@ -25,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)
@@ -97,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 = ''
 
@@ -136,3 +152,6 @@ class Donor():
             except FileNotFoundError: pass
 
         return list(got_fds)
+
+    def detach(d):
+        d._sp.stdin.close()