chiark / gitweb /
fishdescriptor: sort out debugging output
authorIan Jackson <ian.jackson@eu.citrix.com>
Tue, 24 Oct 2017 16:44:11 +0000 (17:44 +0100)
committerIan Jackson <Ian.Jackson@eu.citrix.com>
Tue, 24 Oct 2017 16:44:11 +0000 (17:44 +0100)
Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
fishdescriptor/fishdescriptor
fishdescriptor/py/fishdescriptor/fish.py

index dc17762..20eddfe 100755 (executable)
@@ -117,7 +117,7 @@ def implement_exec(argl):
 def set_donor(pid):
     global donor
     if donor is not None: donor.detach()
-    donor = fishdescriptor.fish.Donor(pid)
+    donor = fishdescriptor.fish.Donor(pid, debug=ov.debug)
 
 def ocb_set_donor(option, opt, value, parser):
     set_donor(value)
@@ -139,6 +139,9 @@ def process_args():
     op.disable_interspersed_args()
     op.add_option('-p','--pid', type='int', action='callback',
                   callback=ocb_set_donor)
+    op.add_option('-D','--debug', action='store_const',
+                  dest='debug', const=sys.stderr)
+    ov.debug = None
 
     args = sys.argv[1:]
     last_nominal = None # None or (nominal,) ie None or (None,) or (int,)
@@ -148,10 +151,8 @@ def process_args():
         if not len(args): break
 
         arg = args.pop(0)
-        print("ARG %s" % arg, file=sys.stderr)
 
         if donor is None:
-            print("SET_DONOR", file=sys.stderr)
             set_donor(int(arg))
         elif arg_matches(r'^(?:(\d+)=)?(\d+)?$'):
             (nominal, there) = m.groups()
index 15ad766..a506938 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):
+    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)