Avoid quadratic behavior when computing the value of a register.
[deliverable/binutils-gdb.git] / gdb / infcall.c
index 0dfd4ed7e90c86df3ee483c27234aa418aaff44b..c9d98cf65eb2173808a66f48b2ef52fdf5233dd9 100644 (file)
@@ -98,6 +98,30 @@ Unwinding of stack if a signal is received while in a call dummy is %s.\n"),
                    value);
 }
 
+/* This boolean tells what gdb should do if a std::terminate call is
+   made while in a function called from gdb (call dummy).
+   As the confines of a single dummy stack prohibit out-of-frame
+   handlers from handling a raised exception, and as out-of-frame
+   handlers are common in C++, this can lead to no handler being found
+   by the unwinder, and a std::terminate call.  This is a false positive.
+   If set, gdb unwinds the stack and restores the context to what it
+   was before the call.
+
+   The default is to unwind the frame if a std::terminate call is
+   made.  */
+
+static int unwind_on_terminating_exception_p = 1;
+
+static void
+show_unwind_on_terminating_exception_p (struct ui_file *file, int from_tty,
+                                       struct cmd_list_element *c,
+                                       const char *value)
+
+{
+  fprintf_filtered (file, _("\
+Unwind stack if a C++ exception is unhandled while in a call dummy is %s.\n"),
+                   value);
+}
 
 /* Perform the standard coercions that are specified
    for arguments to be passed to C or Ada functions.
@@ -118,7 +142,7 @@ value_arg_coerce (struct gdbarch *gdbarch, struct value *arg,
 
   /* Perform any Ada-specific coercion first.  */
   if (current_language->la_language == language_ada)
-    arg = ada_convert_actual (arg, type, sp);
+    arg = ada_convert_actual (arg, type, gdbarch, sp);
 
   /* Force the value to the target if we will need its address.  At
      this point, we could allocate arguments on the stack instead of
@@ -207,6 +231,7 @@ CORE_ADDR
 find_function_addr (struct value *function, struct type **retval_type)
 {
   struct type *ftype = check_typedef (value_type (function));
+  struct gdbarch *gdbarch = get_type_arch (ftype);
   enum type_code code = TYPE_CODE (ftype);
   struct type *value_type = NULL;
   CORE_ADDR funaddr;
@@ -217,7 +242,7 @@ find_function_addr (struct value *function, struct type **retval_type)
   /* Determine address to call.  */
   if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
     {
-      funaddr = VALUE_ADDRESS (function);
+      funaddr = value_address (function);
       value_type = TYPE_TARGET_TYPE (ftype);
     }
   else if (code == TYPE_CODE_PTR)
@@ -227,8 +252,7 @@ find_function_addr (struct value *function, struct type **retval_type)
       if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
          || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
        {
-         funaddr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
-                                                       funaddr,
+         funaddr = gdbarch_convert_from_func_ptr_addr (gdbarch, funaddr,
                                                        &current_target);
          value_type = TYPE_TARGET_TYPE (ftype);
        }
@@ -249,8 +273,7 @@ find_function_addr (struct value *function, struct type **retval_type)
              CORE_ADDR nfunaddr;
              funaddr = value_as_address (value_addr (function));
              nfunaddr = funaddr;
-             funaddr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
-                                                           funaddr,
+             funaddr = gdbarch_convert_from_func_ptr_addr (gdbarch, funaddr,
                                                            &current_target);
              if (funaddr != nfunaddr)
                found_descriptor = 1;
@@ -265,7 +288,7 @@ find_function_addr (struct value *function, struct type **retval_type)
 
   if (retval_type != NULL)
     *retval_type = value_type;
-  return funaddr + gdbarch_deprecated_function_start_offset (current_gdbarch);
+  return funaddr + gdbarch_deprecated_function_start_offset (gdbarch);
 }
 
 /* For CALL_DUMMY_ON_STACK, push a breakpoint sequence that the called
@@ -416,6 +439,8 @@ call_function_by_hand (struct value *function, int nargs, struct value **args)
   struct cleanup *args_cleanup;
   struct frame_info *frame;
   struct gdbarch *gdbarch;
+  struct breakpoint *terminate_bp = NULL;
+  struct minimal_symbol *tm;
   ptid_t call_thread_ptid;
   struct gdb_exception e;
   const char *name;
@@ -536,11 +561,12 @@ call_function_by_hand (struct value *function, int nargs, struct value **args)
 
       /* Tell the target specific argument pushing routine not to
         expect a value.  */
-      target_values_type = builtin_type_void;
+      target_values_type = builtin_type (gdbarch)->builtin_void;
     }
   else
     {
-      struct_return = using_struct_return (value_type (function), values_type);
+      struct_return = using_struct_return (gdbarch,
+                                          value_type (function), values_type);
       target_values_type = values_type;
     }
 
@@ -566,11 +592,6 @@ call_function_by_hand (struct value *function, int nargs, struct value **args)
 
        real_pc = funaddr;
        dummy_addr = entry_point_address ();
-       /* Make certain that the address points at real code, and not a
-          function descriptor.  */
-       dummy_addr = gdbarch_convert_from_func_ptr_addr (gdbarch,
-                                                        dummy_addr,
-                                                        &current_target);
        /* A call dummy always consists of just a single breakpoint, so
           its address is the same as the address of the dummy.  */
        bp_addr = dummy_addr;
@@ -588,14 +609,16 @@ call_function_by_hand (struct value *function, int nargs, struct value **args)
        sym = lookup_minimal_symbol ("__CALL_DUMMY_ADDRESS", NULL, NULL);
        real_pc = funaddr;
        if (sym)
-         dummy_addr = SYMBOL_VALUE_ADDRESS (sym);
+         {
+           dummy_addr = SYMBOL_VALUE_ADDRESS (sym);
+           /* Make certain that the address points at real code, and not
+              a function descriptor.  */
+           dummy_addr = gdbarch_convert_from_func_ptr_addr (gdbarch,
+                                                            dummy_addr,
+                                                            &current_target);
+         }
        else
          dummy_addr = entry_point_address ();
-       /* Make certain that the address points at real code, and not
-          a function descriptor.  */
-       dummy_addr = gdbarch_convert_from_func_ptr_addr (gdbarch,
-                                                        dummy_addr,
-                                                        &current_target);
        /* A call dummy always consists of just a single breakpoint,
           so it's address is the same as the address of the dummy.  */
        bp_addr = dummy_addr;
@@ -712,10 +735,31 @@ call_function_by_hand (struct value *function, int nargs, struct value **args)
     /* Sanity.  The exact same SP value is returned by
        PUSH_DUMMY_CALL, saved as the dummy-frame TOS, and used by
        dummy_id to form the frame ID's stack address.  */
-    bpt = set_momentary_breakpoint (sal, dummy_id, bp_call_dummy);
+    bpt = set_momentary_breakpoint (gdbarch, sal, dummy_id, bp_call_dummy);
     bpt->disposition = disp_del;
   }
 
+  /* Create a breakpoint in std::terminate.
+     If a C++ exception is raised in the dummy-frame, and the
+     exception handler is (normally, and expected to be) out-of-frame,
+     the default C++ handler will (wrongly) be called in an inferior
+     function call.  This is wrong, as an exception can be  normally
+     and legally handled out-of-frame.  The confines of the dummy frame
+     prevent the unwinder from finding the correct handler (or any
+     handler, unless it is in-frame).  The default handler calls
+     std::terminate.  This will kill the inferior.  Assert that
+     terminate should never be called in an inferior function
+     call.  Place a momentary breakpoint in the std::terminate function
+     and if triggered in the call, rewind.  */
+  if (unwind_on_terminating_exception_p)
+     {
+       struct minimal_symbol *tm = lookup_minimal_symbol  ("std::terminate()",
+                                                          NULL, NULL);
+       if (tm != NULL)
+          terminate_bp = set_momentary_breakpoint_at_pc
+            (gdbarch, SYMBOL_VALUE_ADDRESS (tm),  bp_breakpoint);
+     }
+
   /* Everything's ready, push all the info needed to restore the
      caller (and identify the dummy-frame) onto the dummy-frame
      stack.  */
@@ -726,6 +770,10 @@ call_function_by_hand (struct value *function, int nargs, struct value **args)
      or discard it.  */
   discard_cleanups (inf_status_cleanup);
 
+  /* Register a clean-up for unwind_on_terminating_exception_breakpoint.  */
+  if (terminate_bp)
+    make_cleanup_delete_breakpoint (terminate_bp);
+
   /* - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP -
      If you're looking to implement asynchronous dummy-frames, then
      just below is the place to chop this function in two..  */
@@ -881,6 +929,38 @@ When the function is done executing, GDB will silently stop."),
 
       if (!stop_stack_dummy)
        {
+
+         /* Check if unwind on terminating exception behaviour is on.  */
+         if (unwind_on_terminating_exception_p)
+           {
+             /* Check that the breakpoint is our special std::terminate
+                breakpoint.  If it is, we do not want to kill the inferior
+                in an inferior function call. Rewind, and warn the
+                user.  */
+
+             if (terminate_bp != NULL
+                 && (inferior_thread()->stop_bpstat->breakpoint_at->address
+                     == terminate_bp->loc->address))
+               {
+                 /* We must get back to the frame we were before the
+                    dummy call.  */
+                 dummy_frame_pop (dummy_id);
+
+                 /* We also need to restore inferior status to that before the
+                    dummy call.  */
+                 restore_inferior_status (inf_status);
+
+                 error (_("\
+The program being debugged entered a std::terminate call, most likely\n\
+caused by an unhandled C++ exception.  GDB blocked this call in order\n\
+to prevent the program from being terminated, and has restored the\n\
+context to its original state before the call.\n\
+To change this behaviour use \"set unwind-on-terminating-exception off\".\n\
+Evaluation of the expression containing the function (%s)\n\
+will be abandoned."),
+                        name);
+               }
+           }
          /* We hit a breakpoint inside the FUNCTION.
             Keep the dummy frame, the user may want to examine its state.
             Discard inferior status, we're not at the same point
@@ -989,4 +1069,19 @@ The default is to stop in the frame where the signal was received."),
                           NULL,
                           show_unwind_on_signal_p,
                           &setlist, &showlist);
+
+  add_setshow_boolean_cmd ("unwind-on-terminating-exception", no_class,
+                          &unwind_on_terminating_exception_p, _("\
+Set unwinding of stack if std::terminate is called while in call dummy."), _("\
+Show unwinding of stack if std::terminate() is called while in a call dummy."), _("\
+The unwind on terminating exception flag lets the user determine\n\
+what gdb should do if a std::terminate() call is made from the\n\
+default exception handler.  If set, gdb unwinds the stack and restores\n\
+the context to what it was before the call.  If unset, gdb allows the\n\
+std::terminate call to proceed.\n\
+The default is to unwind the frame."),
+                          NULL,
+                          show_unwind_on_terminating_exception_p,
+                          &setlist, &showlist);
+
 }
This page took 0.027773 seconds and 4 git commands to generate.