* source.c: Make global variables current_source_symtab and
[platform/upstream/binutils.git] / gdb / breakpoint.c
index 8548e43..caa8f1c 100644 (file)
 #include "annotate.h"
 #include "symfile.h"
 #include "objfiles.h"
+#include "source.h"
 #include "linespec.h"
 #include "completer.h"
 #include "gdb.h"
 #include "ui-out.h"
+#include "cli/cli-script.h"
 
 #include "gdb-events.h"
 
@@ -217,6 +219,9 @@ extern int addressprint;    /* Print machine addresses? */
 /* Are we executing breakpoint commands?  */
 static int executing_breakpoint_commands;
 
+/* Are overlay event breakpoints enabled? */
+static int overlay_events_enabled;
+
 /* Walk the following statement or block through all breakpoints.
    ALL_BREAKPOINTS_SAFE does so even if the statment deletes the current
    breakpoint.  */
@@ -550,6 +555,7 @@ condition_command (char *arg, int from_tty)
            error ("Junk at end of expression");
        }
       breakpoints_changed ();
+      breakpoint_modify_event (b->number);
       return;
     }
 
@@ -589,6 +595,7 @@ commands_command (char *arg, int from_tty)
       free_command_lines (&b->commands);
       b->commands = l;
       breakpoints_changed ();
+      breakpoint_modify_event (b->number);
       return;
     }
   error ("No breakpoint number %d.", bnum);
@@ -710,17 +717,39 @@ insert_breakpoints (void)
   int return_val = 0;  /* return success code. */
   int val = 0;
   int disabled_breaks = 0;
+  int hw_breakpoint_error = 0;
+#ifdef ONE_PROCESS_WRITETEXT
+  int process_warning = 0;
+#endif
 
   static char message1[] = "Error inserting catchpoint %d:\n";
   static char message[sizeof (message1) + 30];
 
+  struct ui_file *tmp_error_stream = mem_fileopen ();
+  make_cleanup_ui_file_delete (tmp_error_stream);
+
+  /* Explicitly mark the warning -- this will only be printed if
+     there was an error.  */
+  fprintf_unfiltered (tmp_error_stream, "Warning:\n");
 
   ALL_BREAKPOINTS_SAFE (b, temp)
   {
     if (b->enable_state == bp_permanent)
       /* Permanent breakpoints cannot be inserted or removed.  */
       continue;
-    else if (b->type != bp_watchpoint
+    if ((b->type == bp_watchpoint
+        || b->type == bp_hardware_watchpoint
+        || b->type == bp_read_watchpoint
+        || b->type == bp_access_watchpoint) && (!b->val))
+      {
+       struct value *val;
+       val = evaluate_expression (b->exp);
+       release_value (val);
+       if (VALUE_LAZY (val))
+         value_fetch_lazy (val);
+       b->val = val;
+      } 
+    if (b->type != bp_watchpoint
        && b->type != bp_hardware_watchpoint
        && b->type != bp_read_watchpoint
        && b->type != bp_access_watchpoint
@@ -735,33 +764,66 @@ insert_breakpoints (void)
        && !b->inserted
        && !b->duplicate)
       {
-       if (b->type == bp_hardware_breakpoint)
-         val = target_insert_hw_breakpoint (b->address, b->shadow_contents);
+       /* "Normal" instruction breakpoint: either the standard
+          trap-instruction bp (bp_breakpoint), or a
+          bp_hardware_breakpoint.  */
+
+       /* First check to see if we have to handle an overlay.  */
+       if (overlay_debugging == ovly_off
+           || b->section == NULL
+           || !(section_is_overlay (b->section)))
+         {
+           /* No overlay handling: just set the breakpoint.  */
+
+           if (b->type == bp_hardware_breakpoint)
+             val = target_insert_hw_breakpoint (b->address, 
+                                                b->shadow_contents);
+           else
+             val = target_insert_breakpoint (b->address, b->shadow_contents);
+         }
        else
          {
-           /* Check to see if breakpoint is in an overlay section;
-              if so, we should set the breakpoint at the LMA address.
-              Only if the section is currently mapped should we ALSO
-              set a break at the VMA address. */
-           if (overlay_debugging && b->section 
-               && section_is_overlay (b->section))
+           /* This breakpoint is in an overlay section.  
+              Shall we set a breakpoint at the LMA?  */
+           if (!overlay_events_enabled)
              {
-               CORE_ADDR addr;
-
-               addr = overlay_unmapped_address (b->address, b->section);
-               val = target_insert_breakpoint (addr, b->shadow_contents);
-               /* This would be the time to check val, to see if the
-                  breakpoint write to the load address succeeded.  
-                  However, this might be an ordinary occurrance, eg. if 
-                  the unmapped overlay is in ROM.  */
-               val = 0;        /* in case unmapped address failed */
-               if (section_is_mapped (b->section))
+               /* Yes -- overlay event support is not active, 
+                  so we must try to set a breakpoint at the LMA.
+                  This will not work for a hardware breakpoint.  */
+               if (b->type == bp_hardware_breakpoint)
+                 warning ("hardware breakpoint %d not supported in overlay!\n",
+                          b->number);
+               else
+                 {
+                   CORE_ADDR addr = overlay_unmapped_address (b->address, 
+                                                              b->section);
+                   /* Set a software (trap) breakpoint at the LMA.  */
+                   val = target_insert_breakpoint (addr, b->shadow_contents);
+                   if (val != 0)
+                     fprintf_unfiltered (tmp_error_stream, 
+                                         "Overlay breakpoint %d failed: in ROM?", 
+                                         b->number);
+                 }
+             }
+           /* Shall we set a breakpoint at the VMA? */
+           if (section_is_mapped (b->section))
+             {
+               /* Yes.  This overlay section is mapped into memory.  */
+               if (b->type == bp_hardware_breakpoint)
+                 val = target_insert_hw_breakpoint (b->address, 
+                                                    b->shadow_contents);
+               else
                  val = target_insert_breakpoint (b->address,
                                                  b->shadow_contents);
              }
-           else                /* ordinary (non-overlay) address */
-             val = target_insert_breakpoint (b->address, b->shadow_contents);
+           else
+             {
+               /* No.  This breakpoint will not be inserted.  
+                  No error, but do not mark the bp as 'inserted'.  */
+               continue;
+             }
          }
+
        if (val)
          {
            /* Can't set the breakpoint.  */
@@ -773,22 +835,41 @@ insert_breakpoints (void)
                b->enable_state = bp_shlib_disabled;
                if (!disabled_breaks)
                  {
-                   target_terminal_ours_for_output ();
-                   warning ("Cannot insert breakpoint %d:", b->number);
-                   warning ("Temporarily disabling shared library breakpoints:");
+                   fprintf_unfiltered (tmp_error_stream, 
+                                       "Cannot insert breakpoint %d.\n", 
+                                       b->number);
+                   fprintf_unfiltered (tmp_error_stream, 
+                                       "Temporarily disabling shared library breakpoints:\n");
                  }
                disabled_breaks = 1;
-               warning ("breakpoint #%d ", b->number);
+               fprintf_unfiltered (tmp_error_stream, 
+                                   "breakpoint #%d\n", b->number);
              }
            else
 #endif
              {
-               target_terminal_ours_for_output ();
-               warning ("Cannot insert breakpoint %d:", b->number);
 #ifdef ONE_PROCESS_WRITETEXT
-               warning ("The same program may be running in another process.");
+               process_warning = 1;
 #endif
-               memory_error (val, b->address);    /* which bombs us out */
+               if (b->type == bp_hardware_breakpoint)
+                 {
+                   hw_breakpoint_error = 1;
+                   fprintf_unfiltered (tmp_error_stream, 
+                                       "Cannot insert hardware breakpoint %d.\n",
+                                       b->number);
+                 }
+               else
+                 {
+                   fprintf_unfiltered (tmp_error_stream, 
+                                       "Cannot insert breakpoint %d.\n", 
+                                       b->number);
+                   fprintf_filtered (tmp_error_stream, 
+                                     "Error accessing memory address ");
+                   print_address_numeric (b->address, 1, tmp_error_stream);
+                   fprintf_filtered (tmp_error_stream, ": %s.\n",
+                                     safe_strerror (val));
+                 }
+
              }
          }
        else
@@ -815,9 +896,14 @@ insert_breakpoints (void)
        if (val)
          {
            /* Couldn't set breakpoint for some reason */
-           target_terminal_ours_for_output ();
-           warning ("Cannot insert catchpoint %d; disabling it.",
-                    b->number);
+           fprintf_unfiltered (tmp_error_stream, 
+                               "Cannot insert catchpoint %d; disabling it.\n",
+                               b->number);
+           fprintf_filtered (tmp_error_stream, 
+                             "Error accessing memory address ");
+           print_address_numeric (b->address, 1, tmp_error_stream);
+           fprintf_filtered (tmp_error_stream, ": %s.\n",
+                             safe_strerror (val));
            b->enable_state = bp_disabled;
          }
        else
@@ -839,9 +925,9 @@ insert_breakpoints (void)
            if (val == -1)
              {
                /* something went wrong */
-               target_terminal_ours_for_output ();
-               warning ("Cannot insert catchpoint %d; disabling it.",
-                        b->number);
+               fprintf_unfiltered (tmp_error_stream, 
+                                   "Cannot insert catchpoint %d; disabling it.\n",
+                                   b->number);
                b->enable_state = bp_disabled;
              }
          }
@@ -866,7 +952,7 @@ insert_breakpoints (void)
        /* Save the current frame and level so we can restore it after
           evaluating the watchpoint expression on its own frame.  */
        saved_frame = selected_frame;
-       saved_level = selected_frame_level;
+       saved_level = frame_relative_level (selected_frame);
 
        /* Determine if the watchpoint is within scope.  */
        if (b->exp_valid_block == NULL)
@@ -874,16 +960,10 @@ insert_breakpoints (void)
        else
          {
            struct frame_info *fi;
-
-           /* There might be no current frame at this moment if we are
-              resuming from a step over a breakpoint.
-              Set up current frame before trying to find the watchpoint
-              frame.  */
-           get_current_frame ();
-           fi = find_frame_addr_in_frame_chain (b->watchpoint_frame);
+           fi = frame_find_by_id (b->watchpoint_frame);
            within_current_scope = (fi != NULL);
            if (within_current_scope)
-             select_frame (fi, -1);
+             select_frame (fi);
          }
 
        if (within_current_scope)
@@ -924,7 +1004,7 @@ insert_breakpoints (void)
 
                        addr = VALUE_ADDRESS (v) + VALUE_OFFSET (v);
                        len = TYPE_LENGTH (VALUE_TYPE (v));
-                       type   = hw_write;
+                       type = hw_write;
                        if (b->type == bp_read_watchpoint)
                          type = hw_read;
                        else if (b->type == bp_access_watchpoint)
@@ -950,8 +1030,10 @@ insert_breakpoints (void)
            if (!b->inserted)
              {
                remove_breakpoint (b, mark_uninserted);
-               warning ("Could not insert hardware watchpoint %d.",
-                        b->number);
+               hw_breakpoint_error = 1;
+               fprintf_unfiltered (tmp_error_stream,
+                                   "Could not insert hardware watchpoint %d.\n", 
+                                   b->number);
                val = -1;
              }               
          }
@@ -967,8 +1049,8 @@ insert_breakpoints (void)
 
        /* Restore the frame and level.  */
        if ((saved_frame != selected_frame) ||
-           (saved_level != selected_frame_level))
-         select_frame (saved_frame, saved_level);
+           (saved_level != frame_relative_level (selected_frame)))
+         select_frame (saved_frame);
 
        if (val)
          return_val = val;     /* remember failure */
@@ -998,8 +1080,8 @@ insert_breakpoints (void)
          }
        if (val < 0)
          {
-           target_terminal_ours_for_output ();
-           warning ("Cannot insert catchpoint %d.", b->number);
+           fprintf_unfiltered (tmp_error_stream, 
+                               "Cannot insert catchpoint %d.", b->number);
          }
        else
          b->inserted = 1;
@@ -1008,11 +1090,28 @@ insert_breakpoints (void)
          return_val = val;     /* remember failure */
       }
   }
-
+  
+  if (return_val) 
+    {
+      /* If a hardware breakpoint or watchpoint was inserted, add a
+         message about possibly exhausted resources.  */
+      if (hw_breakpoint_error)  
+       {
+         fprintf_unfiltered (tmp_error_stream, 
+                             "Could not insert hardware breakpoints:\n\
+You may have requested too many hardware breakpoints/watchpoints.\n");
+       }
+#ifdef ONE_PROCESS_WRITETEXT
+      if (process_warning)
+       fprintf_unfiltered (tmp_error_stream,
+                           "The same program may be running in another process.");
+#endif
+      target_terminal_ours_for_output ();
+      error_stream (tmp_error_stream);
+    }
   return return_val;
 }
 
-
 int
 remove_breakpoints (void)
 {
@@ -1266,32 +1365,61 @@ remove_breakpoint (struct breakpoint *b, insertion_state_t is)
       && b->type != bp_catch_catch
       && b->type != bp_catch_throw)
     {
-      if (b->type == bp_hardware_breakpoint)
-       val = target_remove_hw_breakpoint (b->address, b->shadow_contents);
+      /* "Normal" instruction breakpoint: either the standard
+        trap-instruction bp (bp_breakpoint), or a
+        bp_hardware_breakpoint.  */
+
+      /* First check to see if we have to handle an overlay.  */
+      if (overlay_debugging == ovly_off
+         || b->section == NULL
+         || !(section_is_overlay (b->section)))
+       {
+         /* No overlay handling: just remove the breakpoint.  */
+
+         if (b->type == bp_hardware_breakpoint)
+           val = target_remove_hw_breakpoint (b->address, 
+                                              b->shadow_contents);
+         else
+           val = target_remove_breakpoint (b->address, b->shadow_contents);
+       }
       else
        {
-         /* Check to see if breakpoint is in an overlay section;
-            if so, we should remove the breakpoint at the LMA address.
-            If that is not equal to the raw address, then we should 
-            presumably remove the breakpoint there as well.  */
-         if (overlay_debugging && b->section 
-             && section_is_overlay (b->section))
+         /* This breakpoint is in an overlay section.  
+            Did we set a breakpoint at the LMA?  */
+         if (!overlay_events_enabled)
+             {
+               /* Yes -- overlay event support is not active, so we
+                  should have set a breakpoint at the LMA.  Remove it.  
+               */
+               CORE_ADDR addr = overlay_unmapped_address (b->address, 
+                                                          b->section);
+               /* Ignore any failures: if the LMA is in ROM, we will
+                  have already warned when we failed to insert it.  */
+               if (b->type != bp_hardware_breakpoint)
+                 target_remove_hw_breakpoint (addr, b->shadow_contents);
+               else
+                 target_remove_breakpoint (addr, b->shadow_contents);
+             }
+         /* Did we set a breakpoint at the VMA? 
+            If so, we will have marked the breakpoint 'inserted'.  */
+         if (b->inserted)
            {
-             CORE_ADDR addr;
-
-             addr = overlay_unmapped_address (b->address, b->section);
-             val = target_remove_breakpoint (addr, b->shadow_contents);
-             /* This would be the time to check val, to see if the
-                shadow breakpoint write to the load address succeeded.  
-                However, this might be an ordinary occurrance, eg. if 
-                the unmapped overlay is in ROM.  */
-             val = 0;          /* in case unmapped address failed */
-             if (section_is_mapped (b->section))
+             /* Yes -- remove it.  Previously we did not bother to
+                remove the breakpoint if the section had been
+                unmapped, but let's not rely on that being safe.  We
+                don't know what the overlay manager might do.  */
+             if (b->type == bp_hardware_breakpoint)
+               val = target_remove_hw_breakpoint (b->address, 
+                                                  b->shadow_contents);
+             else
                val = target_remove_breakpoint (b->address,
                                                b->shadow_contents);
            }
-         else                  /* ordinary (non-overlay) address */
-           val = target_remove_breakpoint (b->address, b->shadow_contents);
+         else
+           {
+             /* No -- not inserted, so no need to remove.  No error.  */
+             val = 0;
+           }
        }
       if (val)
        return val;
@@ -1460,6 +1588,14 @@ breakpoint_init_inferior (enum inf_context context)
        /* Likewise for watchpoints on local expressions.  */
        if (b->exp_valid_block != NULL)
          delete_breakpoint (b);
+       if (context == inf_starting) 
+         {
+           /* Reset val field to force reread of starting value
+              in insert_breakpoints.  */
+           if (b->val)
+             value_free (b->val);
+           b->val = NULL;
+         }
        break;
       default:
        /* Likewise for exception catchpoints in dynamic-linked
@@ -1658,6 +1794,7 @@ bpstat_clear (bpstat *bsp)
       q = p->next;
       if (p->old_val != NULL)
        value_free (p->old_val);
+      free_command_lines (&p->commands);
       xfree (p);
       p = q;
     }
@@ -1770,7 +1907,7 @@ bpstat_clear_actions (bpstat bs)
 {
   for (; bs != NULL; bs = bs->next)
     {
-      bs->commands = NULL;
+      free_command_lines (&bs->commands);
       if (bs->old_val != NULL)
        {
          value_free (bs->old_val);
@@ -1839,11 +1976,9 @@ top:
           to look at, so start over.  */
        goto top;
       else
-       bs->commands = NULL;
+       free_command_lines (&bs->commands);
     }
-
-  executing_breakpoint_commands = 0;
-  discard_cleanups (old_chain);
+  do_cleanups (old_chain);
 }
 
 /* This is the normal print function for a bpstat.  In the future,
@@ -2256,7 +2391,7 @@ watchpoint_check (PTR p)
          any chance of handling watchpoints on local variables, we'll need
          the frame chain (so we can determine if we're in scope).  */
       reinit_frame_cache ();
-      fr = find_frame_addr_in_frame_chain (b->watchpoint_frame);
+      fr = frame_find_by_id (b->watchpoint_frame);
       within_current_scope = (fr != NULL);
       /* in_function_epilogue_p() returns a non-zero value if we're still
         in the function but the stack frame has already been invalidated.
@@ -2270,7 +2405,7 @@ watchpoint_check (PTR p)
        /* If we end up stopping, the current frame will get selected
           in normal_stop.  So this call to select_frame won't affect
           the user.  */
-       select_frame (fr, -1);
+       select_frame (fr);
     }
 
   if (within_current_scope)
@@ -2330,7 +2465,7 @@ which its expression is valid.\n");
 
 /* Get a bpstat associated with having just stopped at address *PC
    and frame address CORE_ADDRESS.  Update *PC to point at the
-   breakpoint (if we hit a breakpoint).  NOT_A_BREAKPOINT is nonzero
+   breakpoint (if we hit a breakpoint).  NOT_A_SW_BREAKPOINT is nonzero
    if this is known to not be a real breakpoint (it could still be a
    watchpoint, though).  */
 
@@ -2349,7 +2484,7 @@ which its expression is valid.\n");
    commands, FIXME??? fields.  */
 
 bpstat
-bpstat_stop_status (CORE_ADDR *pc, int not_a_breakpoint)
+bpstat_stop_status (CORE_ADDR *pc, int not_a_sw_breakpoint)
 {
   register struct breakpoint *b, *temp;
   CORE_ADDR bp_addr;
@@ -2363,14 +2498,13 @@ bpstat_stop_status (CORE_ADDR *pc, int not_a_breakpoint)
   "Error evaluating expression for watchpoint %d\n";
   char message[sizeof (message1) + 30 /* slop */ ];
 
-  /* Get the address where the breakpoint would have been.  
-     The "not_a_breakpoint" argument is meant to distinguish 
-     between a breakpoint trap event and a trace/singlestep
-     trap event.  For a trace/singlestep trap event, we would
-     not want to subtract DECR_PC_AFTER_BREAK from the PC. */
+  /* Get the address where the breakpoint would have been.  The
+     "not_a_sw_breakpoint" argument is meant to distinguish between a
+     breakpoint trap event and a trace/singlestep trap event.  For a
+     trace/singlestep trap event, we would not want to subtract
+     DECR_PC_AFTER_BREAK from the PC. */
 
-  bp_addr = *pc - (not_a_breakpoint && !SOFTWARE_SINGLE_STEP_P () ? 
-                   0 : DECR_PC_AFTER_BREAK);
+  bp_addr = *pc - (not_a_sw_breakpoint ? 0 : DECR_PC_AFTER_BREAK);
 
   ALL_BREAKPOINTS_SAFE (b, temp)
   {
@@ -2398,9 +2532,15 @@ bpstat_stop_status (CORE_ADDR *pc, int not_a_breakpoint)
          continue;
       }
 
-    if (b->type == bp_hardware_breakpoint
-       && b->address != (*pc - DECR_PC_AFTER_HW_BREAK))
-      continue;
+    if (b->type == bp_hardware_breakpoint)
+      {
+       if (b->address != (*pc - DECR_PC_AFTER_HW_BREAK))
+         continue;
+       if (overlay_debugging           /* unmapped overlay section */
+           && section_is_overlay (b->section) 
+           && !section_is_mapped (b->section))
+         continue;
+      }
 
     /* Is this a catchpoint of a load or unload?  If so, did we
        get a load or unload of the specified library?  If not,
@@ -2595,7 +2735,7 @@ bpstat_stop_status (CORE_ADDR *pc, int not_a_breakpoint)
          {
            /* Need to select the frame, with all that implies
               so that the conditions will have the right context.  */
-           select_frame (get_current_frame (), 0);
+           select_frame (get_current_frame ());
            value_is_zero
              = catch_errors (breakpoint_cond_eval, (b->cond),
                              "Error in testing breakpoint condition:\n",
@@ -2620,7 +2760,7 @@ bpstat_stop_status (CORE_ADDR *pc, int not_a_breakpoint)
            /* We will stop here */
            if (b->disposition == disp_disable)
              b->enable_state = bp_disabled;
-           bs->commands = b->commands;
+           bs->commands = copy_command_lines (b->commands);
            if (b->silent)
              bs->print = 0;
            if (bs->commands &&
@@ -2780,7 +2920,7 @@ bpstat_what (bpstat bs)
 
   /* step_resume entries: a step resume breakpoint overrides another
      breakpoint of signal handling (see comment in wait_for_inferior
-     at first IN_SIGTRAMP where we set the step_resume breakpoint).  */
+     at first PC_IN_SIGTRAMP where we set the step_resume breakpoint).  */
   /* We handle the through_sigtramp_breakpoint the same way; having both
      one of those and a step_resume_breakpoint is probably very rare (?).  */
 
@@ -3824,9 +3964,15 @@ create_overlay_event_breakpoint (char *func_name)
   b->addr_string = xstrdup (func_name);
 
   if (overlay_debugging == ovly_auto)
-    b->enable_state = bp_enabled;
+    {
+      b->enable_state = bp_enabled;
+      overlay_events_enabled = 1;
+    }
   else 
-    b->enable_state = bp_disabled;
+    {
+      b->enable_state = bp_disabled;
+      overlay_events_enabled = 0;
+    }
 }
 
 void
@@ -3839,6 +3985,7 @@ enable_overlay_breakpoints (void)
     {
       b->enable_state = bp_enabled;
       check_duplicates (b);
+      overlay_events_enabled = 1;
     }
 }
 
@@ -3852,6 +3999,7 @@ disable_overlay_breakpoints (void)
     {
       b->enable_state = bp_disabled;
       check_duplicates (b);
+      overlay_events_enabled = 0;
     }
 }
 
@@ -4471,8 +4619,12 @@ parse_breakpoint_sals (char **address,
          current_source_symtab (which is decode_line_1's default).  This
          should produce the results we want almost all of the time while
          leaving default_breakpoint_* alone.  */
+        
+      struct symtab_and_line cursal = 
+                       get_current_or_default_source_symtab_and_line ();
+                       
       if (default_breakpoint_valid
-         && (!current_source_symtab
+         && (!cursal.symtab
              || (strchr ("+-", (*address)[0]) != NULL)))
        *sals = decode_line_1 (address, 1, default_breakpoint_symtab,
                               default_breakpoint_line, addr_string);
@@ -5243,10 +5395,12 @@ watch_command_1 (char *arg, int accessflag, int from_tty)
   if (frame)
     {
       prev_frame = get_prev_frame (frame);
-      b->watchpoint_frame = frame->frame;
+      get_frame_id (frame, &b->watchpoint_frame);
     }
   else
-    b->watchpoint_frame = (CORE_ADDR) 0;
+    {
+      memset (&b->watchpoint_frame, 0, sizeof (b->watchpoint_frame));
+    }
 
   /* If the expression is "local", then set up a "watchpoint scope"
      breakpoint at the point where we've left the scope of the watchpoint
@@ -5256,16 +5410,8 @@ watch_command_1 (char *arg, int accessflag, int from_tty)
       if (prev_frame)
        {
          struct breakpoint *scope_breakpoint;
-         struct symtab_and_line scope_sal;
-
-         INIT_SAL (&scope_sal);        /* initialize to zeroes */
-         scope_sal.pc = get_frame_pc (prev_frame);
-         scope_sal.section = find_pc_overlay (scope_sal.pc);
-
-         scope_breakpoint = set_raw_breakpoint (scope_sal,
-                                                bp_watchpoint_scope);
-         set_breakpoint_count (breakpoint_count + 1);
-         scope_breakpoint->number = breakpoint_count;
+         scope_breakpoint = create_internal_breakpoint (get_frame_pc (prev_frame),
+                                                        bp_watchpoint_scope);
 
          scope_breakpoint->enable_state = bp_enabled;
 
@@ -5621,7 +5767,7 @@ get_catch_sals (int this_level_only)
      but it's better than a core dump.  */
   if (selected_frame == NULL)
     error ("No selected frame.");
-  block = get_frame_block (selected_frame);
+  block = get_frame_block (selected_frame, 0);
   pc = selected_frame->pc;
 
   sals.nelts = 0;
@@ -6364,15 +6510,15 @@ tcatch_command (char *arg, int from_tty)
   catch_command_1 (arg, 1, from_tty);
 }
 
+/* Delete breakpoints by address or line.  */
 
 static void
 clear_command (char *arg, int from_tty)
 {
-  register struct breakpoint *b, *b1;
+  struct breakpoint *b, *tmp, *prev, *found;
   int default_match;
   struct symtabs_and_lines sals;
   struct symtab_and_line sal;
-  register struct breakpoint *found;
   int i;
 
   if (arg)
@@ -6384,6 +6530,7 @@ clear_command (char *arg, int from_tty)
     {
       sals.sals = (struct symtab_and_line *)
        xmalloc (sizeof (struct symtab_and_line));
+      make_cleanup (xfree, sals.sals);
       INIT_SAL (&sal);         /* initialize to zeroes */
       sal.line = default_breakpoint_line;
       sal.symtab = default_breakpoint_symtab;
@@ -6398,13 +6545,11 @@ clear_command (char *arg, int from_tty)
     }
 
   /* For each line spec given, delete bps which correspond
-     to it.  We do this in two loops: the first loop looks at
-     the initial bp(s) in the chain which should be deleted,
-     the second goes down the rest of the chain looking ahead
-     one so it can take those bps off the chain without messing
-     up the chain. */
-
+     to it.  Do it in two passes, solely to preserve the current
+     behavior that from_tty is forced true if we delete more than
+     one breakpoint.  */
 
+  found = NULL;
   for (i = 0; i < sals.nelts; i++)
     {
       /* If exact pc given, clear bpts at that pc.
@@ -6420,81 +6565,75 @@ clear_command (char *arg, int from_tty)
          1              0             <can't happen> */
 
       sal = sals.sals[i];
-      found = (struct breakpoint *) 0;
-
-
-      while (breakpoint_chain
-      /* Why don't we check here that this is not
-         a watchpoint, etc., as we do below?
-         I can't make it fail, but don't know
-         what's stopping the failure: a watchpoint
-         of the same address as "sal.pc" should
-         wind up being deleted. */
-
-            && (((sal.pc && (breakpoint_chain->address == sal.pc)) 
-                 && (!overlay_debugging 
-                     || breakpoint_chain->section == sal.section))
-                || ((default_match || (0 == sal.pc))
-                    && breakpoint_chain->source_file != NULL
-                    && sal.symtab != NULL
-             && STREQ (breakpoint_chain->source_file, sal.symtab->filename)
-                    && breakpoint_chain->line_number == sal.line)))
+      prev = NULL;
 
+      /* Find all matching breakpoints, remove them from the
+        breakpoint chain, and add them to the 'found' chain.  */
+      ALL_BREAKPOINTS_SAFE (b, tmp)
        {
-         b1 = breakpoint_chain;
-         breakpoint_chain = b1->next;
-         b1->next = found;
-         found = b1;
-       }
-
-      ALL_BREAKPOINTS (b)
-       while (b->next
-              && b->next->type != bp_none
-              && b->next->type != bp_watchpoint
-              && b->next->type != bp_hardware_watchpoint
-              && b->next->type != bp_read_watchpoint
-              && b->next->type != bp_access_watchpoint
-              && (((sal.pc && (b->next->address == sal.pc)) 
-                   && (!overlay_debugging || b->next->section == sal.section))
-                  || ((default_match || (0 == sal.pc))
-                      && b->next->source_file != NULL
-                      && sal.symtab != NULL
-                      && STREQ (b->next->source_file, sal.symtab->filename)
-                      && b->next->line_number == sal.line)))
-
-
-       {
-         b1 = b->next;
-         b->next = b1->next;
-         b1->next = found;
-         found = b1;
-       }
-
-      if (found == 0)
-       {
-         if (arg)
-           error ("No breakpoint at %s.", arg);
+         /* Are we going to delete b? */
+         if (b->type != bp_none
+             && b->type != bp_watchpoint
+             && b->type != bp_hardware_watchpoint
+             && b->type != bp_read_watchpoint
+             && b->type != bp_access_watchpoint
+             /* Not if b is a watchpoint of any sort... */
+             && (((sal.pc && (b->address == sal.pc)) 
+                  && (!section_is_overlay (b->section)
+                      || b->section == sal.section))
+                 /* Yes, if sal.pc matches b (modulo overlays).  */
+                 || ((default_match || (0 == sal.pc))
+                     && b->source_file != NULL
+                     && sal.symtab != NULL
+                     && STREQ (b->source_file, sal.symtab->filename)
+                     && b->line_number == sal.line)))
+           /* Yes, if sal source file and line matches b.  */
+           {
+             /* Remove it from breakpoint_chain...  */
+             if (b == breakpoint_chain)
+               {
+                 /* b is at the head of the list */
+                 breakpoint_chain = b->next;
+               }
+             else
+               {
+                 prev->next = b->next;
+               }
+             /* And add it to 'found' chain.  */
+             b->next = found;
+             found = b;
+           }
          else
-           error ("No breakpoint at this line.");
+           {
+             /* Keep b, and keep a pointer to it.  */
+             prev = b;
+           }
        }
+    }
+  /* Now go thru the 'found' chain and delete them.  */
+  if (found == 0)
+    {
+      if (arg)
+       error ("No breakpoint at %s.", arg);
+      else
+       error ("No breakpoint at this line.");
+    }
 
-      if (found->next)
-       from_tty = 1;           /* Always report if deleted more than one */
-      if (from_tty)
-       printf_unfiltered ("Deleted breakpoint%s ", found->next ? "s" : "");
-      breakpoints_changed ();
-      while (found)
-       {
-         if (from_tty)
-           printf_unfiltered ("%d ", found->number);
-         b1 = found->next;
-         delete_breakpoint (found);
-         found = b1;
-       }
+  if (found->next)
+    from_tty = 1;              /* Always report if deleted more than one */
+  if (from_tty)
+    printf_unfiltered ("Deleted breakpoint%s ", found->next ? "s" : "");
+  breakpoints_changed ();
+  while (found)
+    {
       if (from_tty)
-       putchar_unfiltered ('\n');
+       printf_unfiltered ("%d ", found->number);
+      tmp = found->next;
+      delete_breakpoint (found);
+      found = tmp;
     }
-  xfree (sals.sals);
+  if (from_tty)
+    putchar_unfiltered ('\n');
 }
 \f
 /* Delete breakpoint in BS if they are `delete' breakpoints and
@@ -6621,11 +6760,32 @@ delete_breakpoint (struct breakpoint *bpt)
          else
            val = target_insert_breakpoint (b->address, b->shadow_contents);
 
+         /* If there was an error in the insert, print a message, then stop execution.  */
          if (val != 0)
            {
+             struct ui_file *tmp_error_stream = mem_fileopen ();
+             make_cleanup_ui_file_delete (tmp_error_stream);
+            
+
+             if (b->type == bp_hardware_breakpoint)
+               {
+                 fprintf_unfiltered (tmp_error_stream, 
+                                       "Cannot insert hardware breakpoint %d.\n"
+                                     "You may have requested too many hardware breakpoints.\n",
+                                       b->number);
+                 }
+               else
+                 {
+                   fprintf_unfiltered (tmp_error_stream, "Cannot insert breakpoint %d.\n", b->number);
+                   fprintf_filtered (tmp_error_stream, "Error accessing memory address ");
+                   print_address_numeric (b->address, 1, tmp_error_stream);
+                   fprintf_filtered (tmp_error_stream, ": %s.\n",
+                                     safe_strerror (val));
+                 }
+             
+             fprintf_unfiltered (tmp_error_stream,"The same program may be running in another process.");
              target_terminal_ours_for_output ();
-             warning ("Cannot insert breakpoint %d:", b->number);
-             memory_error (val, b->address);   /* which bombs us out */
+             error_stream(tmp_error_stream); 
            }
          else
            b->inserted = 1;
@@ -6661,14 +6821,8 @@ delete_breakpoint (struct breakpoint *bpt)
     if (bs->breakpoint_at == bpt)
       {
        bs->breakpoint_at = NULL;
-
-       /* we'd call bpstat_clear_actions, but that free's stuff and due
-          to the multiple pointers pointing to one item with no
-          reference counts found anywhere through out the bpstat's (how
-          do you spell fragile?), we don't want to free things twice --
-          better a memory leak than a corrupt malloc pool! */
-       bs->commands = NULL;
        bs->old_val = NULL;
+       /* bs->commands will be freed later.  */
       }
   /* On the chance that someone will soon try again to delete this same
      bp, we mark it as deleted before freeing its storage. */
@@ -6700,6 +6854,8 @@ delete_command (char *arg, int from_tty)
 {
   struct breakpoint *b, *temp;
 
+  dont_repeat ();
+
   if (arg == 0)
     {
       int breaks_to_delete = 0;
@@ -7004,18 +7160,20 @@ set_ignore_count (int bptnum, int count, int from_tty)
     if (b->number == bptnum)
     {
       b->ignore_count = count;
-      if (!from_tty)
-       return;
-      else if (count == 0)
-       printf_filtered ("Will stop next time breakpoint %d is reached.",
-                        bptnum);
-      else if (count == 1)
-       printf_filtered ("Will ignore next crossing of breakpoint %d.",
-                        bptnum);
-      else
-       printf_filtered ("Will ignore next %d crossings of breakpoint %d.",
-                        count, bptnum);
+      if (from_tty)
+       {
+         if (count == 0)
+           printf_filtered ("Will stop next time breakpoint %d is reached.",
+                            bptnum);
+         else if (count == 1)
+           printf_filtered ("Will ignore next crossing of breakpoint %d.",
+                            bptnum);
+         else
+           printf_filtered ("Will ignore next %d crossings of breakpoint %d.",
+                            count, bptnum);
+       }
       breakpoints_changed ();
+      breakpoint_modify_event (b->number);
       return;
     }
 
@@ -7052,8 +7210,8 @@ ignore_command (char *args, int from_tty)
   set_ignore_count (num,
                    longest_to_int (value_as_long (parse_and_eval (p))),
                    from_tty);
-  printf_filtered ("\n");
-  breakpoints_changed ();
+  if (from_tty)
+    printf_filtered ("\n");
 }
 \f
 /* Call FUNCTION on each of the breakpoints
@@ -7195,12 +7353,7 @@ do_enable_breakpoint (struct breakpoint *bpt, enum bpdisp disposition)
       if (bpt->exp_valid_block != NULL)
        {
          struct frame_info *fr =
-
-         /* Ensure that we have the current frame.  Else, this
-            next query may pessimistically be answered as, "No,
-            not within current scope". */
-         get_current_frame ();
-         fr = find_frame_addr_in_frame_chain (bpt->watchpoint_frame);
+         fr = frame_find_by_id (bpt->watchpoint_frame);
          if (fr == NULL)
            {
              printf_filtered ("\
@@ -7211,8 +7364,8 @@ is valid is not currently in scope.\n", bpt->number);
            }
 
          save_selected_frame = selected_frame;
-         save_selected_frame_level = selected_frame_level;
-         select_frame (fr, -1);
+         save_selected_frame_level = frame_relative_level (selected_frame);
+         select_frame (fr);
        }
 
       value_free (bpt->val);
@@ -7247,7 +7400,7 @@ have been allocated for other watchpoints.\n", bpt->number);
        }
 
       if (save_selected_frame_level >= 0)
-       select_frame (save_selected_frame, save_selected_frame_level);
+       select_frame (save_selected_frame);
       value_free_to_mark (mark);
     }
   if (modify_breakpoint_hook)