Make exception handling more efficient
authorTom Tromey <tromey@adacore.com>
Wed, 24 Apr 2019 12:50:06 +0000 (06:50 -0600)
committerTom Tromey <tromey@adacore.com>
Thu, 25 Apr 2019 18:59:35 +0000 (12:59 -0600)
This makes exception handling more efficient in a few spots, through
the use of const- and rvalue-references.

I wrote this patch by commenting out the gdb_exception copy
constructor and then examining the resulting error messages one by
one, introducing the use of std::move where appropriate.

gdb/ChangeLog
2019-04-25  Tom Tromey  <tromey@adacore.com>

* xml-support.c (struct gdb_xml_parser) <set_error>: Take an
rvalue reference.
(gdb_xml_start_element_wrapper, gdb_xml_end_element_wrapper)
(gdb_xml_parser::parse): Use std::move.
* python/python-internal.h (gdbpy_convert_exception): Take a const
reference.
* python/py-value.c (valpy_getitem, valpy_nonzero): Use
std::move.
* python/py-utils.c (gdbpy_convert_exception): Take a const
reference.
* python/py-inferior.c (infpy_write_memory, infpy_search_memory):
Use std::move.
* python/py-breakpoint.c (bppy_set_condition, bppy_set_commands):
Use std::move.
* mi/mi-main.c (mi_print_exception): Take a const reference.
* main.c (handle_command_errors): Take a const reference.
* linespec.c (parse_linespec): Use std::move.
* infcall.c (run_inferior_call): Use std::move.
(call_function_by_hand_dummy): Use std::move.
* exec.c (try_open_exec_file): Use std::move.
* exceptions.h (exception_print, exception_fprintf)
(exception_print_same): Update.
* exceptions.c (print_exception, exception_print)
(exception_fprintf, exception_print_same): Change parameters to
const reference.
* event-top.c (gdb_rl_callback_read_char_wrapper): Update.
* common/new-op.c: Use std::move.
* common/common-exceptions.h (struct gdb_exception): Add move
constructor.
(struct gdb_exception_error, struct gdb_exception_quit, struct
gdb_quit_bad_alloc): Change constructor to move constructor.
(throw_exception): Change parameter to rvalue reference.
* common/common-exceptions.c (throw_exception): Take rvalue
reference.
* cli/cli-interp.c (safe_execute_command): Use std::move.
* breakpoint.c (insert_bp_location, location_to_sals): Use
std::move.

20 files changed:
gdb/ChangeLog
gdb/breakpoint.c
gdb/cli/cli-interp.c
gdb/common/common-exceptions.c
gdb/common/common-exceptions.h
gdb/common/new-op.c
gdb/event-top.c
gdb/exceptions.c
gdb/exceptions.h
gdb/exec.c
gdb/infcall.c
gdb/linespec.c
gdb/main.c
gdb/mi/mi-main.c
gdb/python/py-breakpoint.c
gdb/python/py-inferior.c
gdb/python/py-utils.c
gdb/python/py-value.c
gdb/python/python-internal.h
gdb/xml-support.c

index ce3ea81aee3a9320fe4d1acf247cbf4007ce3875..3e28e77cb73980b16108a11a10f9a944aa5a9dd6 100644 (file)
@@ -1,3 +1,43 @@
+2019-04-25  Tom Tromey  <tromey@adacore.com>
+
+       * xml-support.c (struct gdb_xml_parser) <set_error>: Take an
+       rvalue reference.
+       (gdb_xml_start_element_wrapper, gdb_xml_end_element_wrapper)
+       (gdb_xml_parser::parse): Use std::move.
+       * python/python-internal.h (gdbpy_convert_exception): Take a const
+       reference.
+       * python/py-value.c (valpy_getitem, valpy_nonzero): Use
+       std::move.
+       * python/py-utils.c (gdbpy_convert_exception): Take a const
+       reference.
+       * python/py-inferior.c (infpy_write_memory, infpy_search_memory):
+       Use std::move.
+       * python/py-breakpoint.c (bppy_set_condition, bppy_set_commands):
+       Use std::move.
+       * mi/mi-main.c (mi_print_exception): Take a const reference.
+       * main.c (handle_command_errors): Take a const reference.
+       * linespec.c (parse_linespec): Use std::move.
+       * infcall.c (run_inferior_call): Use std::move.
+       (call_function_by_hand_dummy): Use std::move.
+       * exec.c (try_open_exec_file): Use std::move.
+       * exceptions.h (exception_print, exception_fprintf)
+       (exception_print_same): Update.
+       * exceptions.c (print_exception, exception_print)
+       (exception_fprintf, exception_print_same): Change parameters to
+       const reference.
+       * event-top.c (gdb_rl_callback_read_char_wrapper): Update.
+       * common/new-op.c: Use std::move.
+       * common/common-exceptions.h (struct gdb_exception): Add move
+       constructor.
+       (struct gdb_exception_error, struct gdb_exception_quit, struct
+       gdb_quit_bad_alloc): Change constructor to move constructor.
+       (throw_exception): Change parameter to rvalue reference.
+       * common/common-exceptions.c (throw_exception): Take rvalue
+       reference.
+       * cli/cli-interp.c (safe_execute_command): Use std::move.
+       * breakpoint.c (insert_bp_location, location_to_sals): Use
+       std::move.
+
 2019-04-25  Tom Tromey  <tromey@adacore.com>
 
        * guile/scm-exception.c (gdbscm_scm_from_gdb_exception)
index c74fc61ea42245112d1f8f40b3432dd976e45f63..f6d2f36d0a4020d81eb9b4e6ec350f796ebedb9d 100644 (file)
@@ -2545,9 +2545,9 @@ insert_bp_location (struct bp_location *bl,
              if (val)
                bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
            }
-         catch (const gdb_exception &e)
+         catch (gdb_exception &e)
            {
-             bp_excpt = e;
+             bp_excpt = std::move (e);
            }
        }
       else
@@ -2584,9 +2584,9 @@ insert_bp_location (struct bp_location *bl,
                        bp_excpt
                          = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
                    }
-                 catch (const gdb_exception &e)
+                 catch (gdb_exception &e)
                    {
-                     bp_excpt = e;
+                     bp_excpt = std::move (e);
                    }
 
                  if (bp_excpt.reason != 0)
@@ -2608,9 +2608,9 @@ insert_bp_location (struct bp_location *bl,
                  if (val)
                    bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
                }
-             catch (const gdb_exception &e)
+             catch (gdb_exception &e)
                {
-                 bp_excpt = e;
+                 bp_excpt = std::move (e);
                }
            }
          else
@@ -13603,12 +13603,10 @@ location_to_sals (struct breakpoint *b, struct event_location *location,
     {
       sals = b->ops->decode_location (b, location, search_pspace);
     }
-  catch (const gdb_exception_error &e)
+  catch (gdb_exception_error &e)
     {
       int not_found_and_ok = 0;
 
-      exception = e;
-
       /* For pending breakpoints, it's expected that parsing will
         fail until the right shared library is loaded.  User has
         already told to create pending breakpoints and don't need
@@ -13637,6 +13635,8 @@ location_to_sals (struct breakpoint *b, struct event_location *location,
          b->enable_state = bp_disabled;
          throw;
        }
+
+      exception = std::move (e);
     }
 
   if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
index 17639d0c3f3f12d9ac04d37cea76df5e46a38f90..fc4b39a9c2a4c888f48b8129376b62fc8625ef04 100644 (file)
@@ -367,9 +367,9 @@ safe_execute_command (struct ui_out *command_uiout, const char *command,
     {
       execute_command (command, from_tty);
     }
-  catch (const gdb_exception &exception)
+  catch (gdb_exception &exception)
     {
-      e = exception;
+      e = std::move (exception);
     }
 
   /* FIXME: cagney/2005-01-13: This shouldn't be needed.  Instead the
index 0b96cc679da5a3609e8e082dc7ec22275168fb09..9f210250a6f1313d2e90aeba691f307af6d35059 100644 (file)
@@ -180,12 +180,12 @@ throw_exception_sjlj (const struct gdb_exception &exception)
 /* Implementation of throw_exception that uses C++ try/catch.  */
 
 void
-throw_exception (const gdb_exception &exception)
+throw_exception (gdb_exception &&exception)
 {
   if (exception.reason == RETURN_QUIT)
-    throw gdb_exception_quit (exception);
+    throw gdb_exception_quit (std::move (exception));
   else if (exception.reason == RETURN_ERROR)
-    throw gdb_exception_error (exception);
+    throw gdb_exception_error (std::move (exception));
   else
     gdb_assert_not_reached ("invalid return reason");
 }
index d7b25502262e671e1c965699f4072d2a330b6062..ebcaf031354336ae733fe6cc0f28228fa386e3db 100644 (file)
@@ -133,6 +133,10 @@ struct gdb_exception
   {
   }
 
+  /* The move constructor exists so that we can mark it "noexcept",
+     which is a good practice for any sort of exception object.  */
+  explicit gdb_exception (gdb_exception &&other) noexcept = default;
+
   /* The copy constructor exists so that we can mark it "noexcept",
      which is a good practice for any sort of exception object.  */
   gdb_exception (const gdb_exception &other) noexcept
@@ -232,8 +236,8 @@ struct gdb_exception_error : public gdb_exception
   {
   }
 
-  explicit gdb_exception_error (const gdb_exception &ex) noexcept
-    : gdb_exception (ex)
+  explicit gdb_exception_error (gdb_exception &&ex) noexcept
+    : gdb_exception (std::move (ex))
   {
     gdb_assert (ex.reason == RETURN_ERROR);
   }
@@ -247,8 +251,8 @@ struct gdb_exception_quit : public gdb_exception
   {
   }
 
-  explicit gdb_exception_quit (const gdb_exception &ex) noexcept
-    : gdb_exception (ex)
+  explicit gdb_exception_quit (gdb_exception &&ex) noexcept
+    : gdb_exception (std::move (ex))
   {
     gdb_assert (ex.reason == RETURN_QUIT);
   }
@@ -264,8 +268,8 @@ struct gdb_quit_bad_alloc
   : public gdb_exception_quit,
     public std::bad_alloc
 {
-  explicit gdb_quit_bad_alloc (const gdb_exception &ex) noexcept
-    : gdb_exception_quit (ex),
+  explicit gdb_quit_bad_alloc (gdb_exception &&ex) noexcept
+    : gdb_exception_quit (std::move (ex)),
       std::bad_alloc ()
   {
   }
@@ -276,7 +280,7 @@ struct gdb_quit_bad_alloc
 /* Throw an exception (as described by "struct gdb_exception"),
    landing in the inner most containing exception handler established
    using TRY/CATCH.  */
-extern void throw_exception (const gdb_exception &exception)
+extern void throw_exception (gdb_exception &&exception)
      ATTRIBUTE_NORETURN;
 
 /* Throw an exception by executing a LONG JUMP to the inner most
index b230f111ae71924f9b2324ca3c36155d810c5f4c..7c5dba0be6da571adf38a6af4c47807159ad8bf6 100644 (file)
@@ -64,9 +64,9 @@ operator new (std::size_t sz)
        {
          malloc_failure (sz);
        }
-      catch (const gdb_exception &ex)
+      catch (gdb_exception &ex)
        {
-         throw gdb_quit_bad_alloc (ex);
+         throw gdb_quit_bad_alloc (std::move (ex));
        }
     }
   return p;
index bb8ba5cfe5779747f59d95db40280c4faba3a4b1..9fa46c8ad44bbe2e91ef479c54e97a219269cb8d 100644 (file)
@@ -193,7 +193,7 @@ gdb_rl_callback_read_char_wrapper (gdb_client_data client_data)
 
   /* Rethrow using the normal EH mechanism.  */
   if (gdb_expt.reason < 0)
-    throw_exception (gdb_expt);
+    throw_exception (std::move (gdb_expt));
 }
 
 /* GDB's readline callback handler.  Calls the current INPUT_HANDLER,
index 078f3c3bf00c995c052b6624826078958aa532f5..ebdc71d98d40af59b086fceb4bccbc3bc710b3df 100644 (file)
@@ -74,7 +74,7 @@ print_flush (void)
 }
 
 static void
-print_exception (struct ui_file *file, struct gdb_exception e)
+print_exception (struct ui_file *file, const struct gdb_exception &e)
 {
   /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
      as that way the MI's behavior is preserved.  */
@@ -110,7 +110,7 @@ print_exception (struct ui_file *file, struct gdb_exception e)
 }
 
 void
-exception_print (struct ui_file *file, struct gdb_exception e)
+exception_print (struct ui_file *file, const struct gdb_exception &e)
 {
   if (e.reason < 0 && e.message != NULL)
     {
@@ -120,7 +120,7 @@ exception_print (struct ui_file *file, struct gdb_exception e)
 }
 
 void
-exception_fprintf (struct ui_file *file, struct gdb_exception e,
+exception_fprintf (struct ui_file *file, const struct gdb_exception &e,
                   const char *prefix, ...)
 {
   if (e.reason < 0 && e.message != NULL)
@@ -141,7 +141,8 @@ exception_fprintf (struct ui_file *file, struct gdb_exception e,
 /* See exceptions.h.  */
 
 int
-exception_print_same (struct gdb_exception e1, struct gdb_exception e2)
+exception_print_same (const struct gdb_exception &e1,
+                     const struct gdb_exception &e2)
 {
   const char *msg1 = e1.message == nullptr ? "" : e1.what ();
   const char *msg2 = e2.message == nullptr ? "" : e2.what ();
index 083313b9fa90bb4fa609f99fd964e43d93972b69..e169b25c6774e64c73716817e83831593d50512f 100644 (file)
 
 /* If E is an exception, print it's error message on the specified
    stream.  For _fprintf, prefix the message with PREFIX...  */
-extern void exception_print (struct ui_file *file, struct gdb_exception e);
-extern void exception_fprintf (struct ui_file *file, struct gdb_exception e,
+extern void exception_print (struct ui_file *file,
+                            const struct gdb_exception &e);
+extern void exception_fprintf (struct ui_file *file,
+                              const struct gdb_exception &e,
                               const char *prefix,
                               ...) ATTRIBUTE_PRINTF (3, 4);
 
 /* Compare two exception objects for print equality.  */
-extern int exception_print_same (struct gdb_exception e1,
-                                struct gdb_exception e2);
+extern int exception_print_same (const struct gdb_exception &e1,
+                                const struct gdb_exception &e2);
 #endif
index 7ff77f99160ddb43682cb06651b99c39c0d35df8..7de92347f2eee516cb8db2aa6b70a89c7ffd8947 100644 (file)
@@ -167,12 +167,12 @@ try_open_exec_file (const char *exec_file_host, struct inferior *inf,
         exec_file_attach will clear state.  */
       exec_file_attach (exec_file_host, add_flags & SYMFILE_VERBOSE);
     }
-  catch (const gdb_exception_error &err)
+  catch (gdb_exception_error &err)
     {
       if (err.message != NULL)
        warning ("%s", err.what ());
 
-      prev_err = err;
+      prev_err = std::move (err);
     }
 
   if (exec_file_host != NULL)
index 52f9bc907e28cdacecfd5b167a6cb49fb1313429..af60fdc56b2911f1813c41f7b59c990f10b6978c 100644 (file)
@@ -605,9 +605,9 @@ run_inferior_call (struct call_thread_fsm *sm,
         target supports asynchronous execution.  */
       wait_sync_command_done ();
     }
-  catch (const gdb_exception &e)
+  catch (gdb_exception &e)
     {
-      caught_error = e;
+      caught_error = std::move (e);
     }
 
   /* If GDB has the prompt blocked before, then ensure that it remains
@@ -1195,7 +1195,7 @@ When the function is done executing, GDB will silently stop."),
                       e.what (), name);
        case RETURN_QUIT:
        default:
-         throw_exception (e);
+         throw_exception (std::move (e));
        }
     }
 
index 6d26638296ea331ef3f06ba6ad9d2e5ba7902f43..f418e03b774531a8af0b0c16090f378b4fad3e81 100644 (file)
@@ -2613,9 +2613,9 @@ parse_linespec (linespec_parser *parser, const char *arg,
            = symtabs_from_filename (user_filename.get (),
                                     PARSER_STATE (parser)->search_pspace);
        }
-      catch (const gdb_exception_error &ex)
+      catch (gdb_exception_error &ex)
        {
-         file_exception = ex;
+         file_exception = std::move (ex);
        }
 
       if (file_exception.reason >= 0)
@@ -2663,7 +2663,7 @@ parse_linespec (linespec_parser *parser, const char *arg,
       /* The linespec didn't parse.  Re-throw the file exception if
         there was one.  */
       if (file_exception.reason < 0)
-       throw_exception (file_exception);
+       throw_exception (std::move (file_exception));
 
       /* Otherwise, the symbol is not found.  */
       symbol_not_found_error (PARSER_EXPLICIT (parser)->function_name,
index e67efc7bcdf04365da8f8af10dcf0747eb68a5ca..35df1e497f4f49bf8c700796ef17dc02c6b7ad7d 100644 (file)
@@ -339,7 +339,7 @@ captured_command_loop ()
 /* Handle command errors thrown from within catch_command_errors.  */
 
 static int
-handle_command_errors (struct gdb_exception e)
+handle_command_errors (const struct gdb_exception &e)
 {
   if (e.reason < 0)
     {
index 17ca8074719f22b3222f64602ecbf9fabec5774c..2b9883cb99f4df237d69e7f0edbe2ef4452edd7c 100644 (file)
@@ -1875,7 +1875,7 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context)
 /* Print a gdb exception to the MI output stream.  */
 
 static void
-mi_print_exception (const char *token, struct gdb_exception exception)
+mi_print_exception (const char *token, const struct gdb_exception &exception)
 {
   struct mi_interp *mi = (struct mi_interp *) current_interpreter ();
 
index dfc30f70bb28552ff7304eabf2afeeb5528e66f6..fc9543eba0e1720f6584d15d71281f2d3f5c89d4 100644 (file)
@@ -469,9 +469,9 @@ bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure)
     {
       set_breakpoint_condition (self_bp->bp, exp, 0);
     }
-  catch (const gdb_exception &ex)
+  catch (gdb_exception &ex)
     {
-      except = ex;
+      except = std::move (ex);
     }
 
   GDB_PY_SET_HANDLE_EXCEPTION (except);
@@ -540,9 +540,9 @@ bppy_set_commands (PyObject *self, PyObject *newvalue, void *closure)
       counted_command_line lines = read_command_lines_1 (reader, 1, nullptr);
       breakpoint_set_commands (self_bp->bp, std::move (lines));
     }
-  catch (const gdb_exception &ex)
+  catch (gdb_exception &ex)
     {
-      except = ex;
+      except = std::move (ex);
     }
 
   GDB_PY_SET_HANDLE_EXCEPTION (except);
index 1b7e3c2491717e31e7340890895bc48feae3fc18..7e7d518c5577683d62324f7935e0851052a74a7e 100644 (file)
@@ -574,9 +574,9 @@ infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw)
     {
       write_memory_with_notification (addr, buffer, length);
     }
-  catch (const gdb_exception &ex)
+  catch (gdb_exception &ex)
     {
-      except = ex;
+      except = std::move (ex);
     }
 
   GDB_PY_HANDLE_EXCEPTION (except);
@@ -728,9 +728,9 @@ infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
                                    buffer, pattern_size,
                                    &found_addr);
     }
-  catch (const gdb_exception &ex)
+  catch (gdb_exception &ex)
     {
-      except = ex;
+      except = std::move (ex);
     }
 
   GDB_PY_HANDLE_EXCEPTION (except);
index 9fee881778191af46d5cab3174d63508609eae74..e07da034e34175b3622637b7395afc0260f6e5d0 100644 (file)
@@ -229,7 +229,7 @@ gdbpy_err_fetch::type_to_string () const
    This sets the Python error indicator.  */
 
 void
-gdbpy_convert_exception (struct gdb_exception exception)
+gdbpy_convert_exception (const struct gdb_exception &exception)
 {
   PyObject *exc_class;
 
index 3349802f7fa1cffa9d8c6056ce564748971c08fd..512e5d0220c9201d7c14ba580893481dbf17c774 100644 (file)
@@ -1031,9 +1031,9 @@ valpy_getitem (PyObject *self, PyObject *key)
       if (res_val)
        result = value_to_value_object (res_val);
     }
-  catch (const gdb_exception &ex)
+  catch (gdb_exception &ex)
     {
-      except = ex;
+      except = std::move (ex);
     }
 
   GDB_PY_HANDLE_EXCEPTION (except);
@@ -1498,9 +1498,9 @@ valpy_nonzero (PyObject *self)
        /* All other values are True.  */
        nonzero = 1;
     }
-  catch (const gdb_exception &ex)
+  catch (gdb_exception &ex)
     {
-      except = ex;
+      except = std::move (ex);
     }
 
   /* This is not documented in the Python documentation, but if this
index 449926ca8744e4d3ce7d75b4aab0211c2d5959fd..69ff1fe30de195c29b50b1213df72714982608bb 100644 (file)
@@ -729,7 +729,7 @@ extern PyObject *gdbpy_gdb_error;
 extern PyObject *gdbpy_gdb_memory_error;
 extern PyObject *gdbpy_gdberror_exc;
 
-extern void gdbpy_convert_exception (struct gdb_exception)
+extern void gdbpy_convert_exception (const struct gdb_exception &)
     CPYCHECKER_SETS_EXCEPTION;
 
 int get_addr_from_python (PyObject *obj, CORE_ADDR *addr)
index d4cd89c03398208a3ca97538b0744586dc0ff6e0..ae727da03b360d0039c1f4fa70fd31fcb1f8a816 100644 (file)
@@ -113,9 +113,9 @@ struct gdb_xml_parser
   { m_is_xinclude = is_xinclude; }
 
   /* A thrown error, if any.  */
-  void set_error (gdb_exception error)
+  void set_error (gdb_exception &&error)
   {
-    m_error = error;
+    m_error = std::move (error);
 #ifdef HAVE_XML_STOPPARSER
     XML_StopParser (m_expat_parser, XML_FALSE);
 #endif
@@ -387,9 +387,9 @@ gdb_xml_start_element_wrapper (void *data, const XML_Char *name,
     {
       parser->start_element (name, attrs);
     }
-  catch (const gdb_exception &ex)
+  catch (gdb_exception &ex)
     {
-      parser->set_error (ex);
+      parser->set_error (std::move (ex));
     }
 }
 
@@ -459,9 +459,9 @@ gdb_xml_end_element_wrapper (void *data, const XML_Char *name)
     {
       parser->end_element (name);
     }
-  catch (const gdb_exception &ex)
+  catch (gdb_exception &ex)
     {
-      parser->set_error (ex);
+      parser->set_error (std::move (ex));
     }
 }
 
@@ -603,7 +603,7 @@ gdb_xml_parser::parse (const char *buffer)
   else
     {
       gdb_assert (m_error.reason < 0);
-      throw_exception (m_error);
+      throw_exception (std::move (m_error));
     }
 
   if (m_last_line != 0)
This page took 0.066016 seconds and 4 git commands to generate.