chiark / gitweb /
fishdescriptor: Provide copyright notices and licence statements
[chiark-utils.git] / fishdescriptor / py / fishdescriptor / indonor.py
index f701434540d9d56b88237228e1fb195e88c4d54a..20bc8071b6d0f7eb9b1223c8269e90e0c0a05ebe 100644 (file)
@@ -1,12 +1,32 @@
 
+# This file is part of chiark-utils, a collection of useful programs
+# used on chiark.greenend.org.uk.
+#
+# This file is:
+#  Copyright 2018 Citrix Systems Ltd
+#
+# This is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3, or (at your option) any later version.
+#
+# This is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, consult the Free Software Foundation's
+# website at www.fsf.org, or the GNU Project website at www.gnu.org.
+
 # class for use inside gdb which is debugging the donor process
 
+from __future__ import print_function
+
 import gdb
 import copy
 import os
 import sys
 import socket
-import re
 
 def _string_bytearray(s):
     # gets us bytes in py2 and py3
@@ -57,15 +77,6 @@ def parse_eval(expr):
     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.lookup_type(typename)
@@ -92,6 +103,7 @@ class DonorImplementation():
         di._structs = { }
         di._saved_errno = None
         di._result_stream = os.fdopen(3, 'w')
+        di._errno_workaround = None
 
     # assembling structs
     # sigh, we have to record the order of the arguments!
@@ -107,6 +119,31 @@ class DonorImplementation():
         fields = di._find_fields(typename)
         return fields.substitute(values)
 
+    # hideous workaround
+
+    def _parse_eval_errno(di, expr_pat):
+        # evaluates  expr_pat % 'errno'
+        if di._errno_workaround is not True:
+            try:
+                x = parse_eval(expr_pat % 'errno')
+                di._errno_workaround = False
+                return x
+            except gdb.error as e:
+                if di._errno_workaround is False:
+                    raise e
+            di._errno_workaround = True
+        # 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 while `print errno = 25'
+        # doesn't.   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).  Also the error is very nonspecific :-/.
+        # This seems to happen on jessie, and is fixed in stretch.
+        # Anyway:
+        return parse_eval(expr_pat % '(*((int (*)(void))__errno_location)())')
+
     # calling functions (need to cast the function name to the right
     # type in case maybe gdb doesn't know the type)
 
@@ -117,7 +154,7 @@ class DonorImplementation():
     def _must_func(di, functype, funcname, realargs):
         retval = di._func(functype, funcname, realargs)
         if retval < 0:
-            errnoval = parse_eval('errno')
+            errnoval = di._parse_eval_errno('%s')
             raise RuntimeError("%s gave errno=%d `%s'" %
                                (funcname, errnoval, os.strerror(errnoval)))
         return retval
@@ -174,7 +211,7 @@ class DonorImplementation():
             '("%s", %d)' % (_string_escape_for_c(path), mode)
         )
         if r < 0:
-            errnoval = parse_eval('errno')
+            errnoval = di._parse_eval_errno('%s')
             if errnoval != os.errno.EEXIST:
                 raise RuntimeError("mkdir %s failed: `%s'" %
                                    (repr(path), os.strerror(errnoval)))
@@ -182,22 +219,13 @@ class DonorImplementation():
         return 1
 
     def _errno_save(di):
-        # 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')
+        di._saved_errno = di._parse_eval_errno('%s')
 
     def _errno_restore(di):
         to_restore = di._saved_errno
         di._saved_errno = None
         if to_restore is not None:
-            parse_eval_via_print('errno = %d' % to_restore)
+            di._parse_eval_errno('%%s = %d' % to_restore)
 
     def _result(di, output):
         sys.stderr.write("#> %s" % output)
@@ -253,6 +281,9 @@ class DonorImplementation():
         return input
 
     def eval_loop(di):
+        if not gdb.selected_inferior().was_attached:
+            print('gdb inferior not attached', file=sys.stderr)
+            sys.exit(0)
         while True:
             di._result('!\n')
             cmd = di._protocol_read()