Disable breakpoints during startup.

	* fucshia-tdep.c (fuchsia_solib_create_inferior_hook): Call
	disable_breakpoints_before_startup.
	(fuchsia_handle_solib_event): Call enable_breakpoints_after_startup.

TO-108 #done

Change-Id: Id01e6e983a5ea4886470bb4bb9e6e027d9e16fb8
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 06f4eaf..4431e74 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,9 @@
+2017-02-25  Doug Evans  <dje@google.com>
+
+	* fucshia-tdep.c (fuchsia_solib_create_inferior_hook): Call
+	disable_breakpoints_before_startup.
+	(fuchsia_handle_solib_event): Call enable_breakpoints_after_startup.
+
 2017-02-09  Doug Evans  <dje@google.com>
 
 	PR symtab/21126
diff --git a/gdb/fuchsia-tdep.c b/gdb/fuchsia-tdep.c
index 48b38f2..ba20409 100644
--- a/gdb/fuchsia-tdep.c
+++ b/gdb/fuchsia-tdep.c
@@ -600,19 +600,29 @@
   return addr;
 }
 
-/* For Fuchsia we need to reset the PIE exec displacement before we do
-   anything else.  In between the time the inferior starts and when the main
-   executable is loaded the old displacements are wrong and things like
-   re-setting breakpoints can get into worse trouble than normal (typically
-   the PIE addresses are low enough that memory reads/writes fail, but with
-   the previous run's displacement they might errantly work).  A better fix
-   would of course be to not even try to access memory until it exists but
-   that's a wee bit of work.  */
+// Create inferior hook.
 
 static void
 fuchsia_solib_create_inferior_hook (int from_tty)
 {
+  /* For Fuchsia we need to reset the PIE exec displacement before we do
+     anything else.  In between the time the inferior starts and when the main
+     executable is loaded the old displacements are wrong and things like
+     re-setting breakpoints can get into worse trouble than normal (typically
+     the PIE addresses are low enough that memory reads/writes fail, but with
+     the previous run's displacement they might errantly work).  A better fix
+     would of course be to not even try to access memory until it exists but
+     that's a wee bit of work.  */
   svr4_apply_exec_displacement (0);
+
+  /* If the user established breakpoints before starting the inferior, GDB
+     would attempt to insert those now.  This will fail because the executable
+     hasn't been loaded yet: In Fuchsia the executable is loaded by ld.so.
+     To prevent such failures, we disable all user-created breakpoints now;
+     they will be re-enabled in fuchsia_handle_solib_event once the main
+     executable has been loaded.  */
+  disable_breakpoints_before_startup ();
+
   svr4_solib_create_inferior_hook (from_tty);
 }
 
@@ -628,12 +638,17 @@
   if (!info->exec_displacement_known)
     {
       // Presumably this is the first time we've hit the dynamic linker bkpt.
-      // It is now that the main executable has been loaded. To fetch the
+      // It is now that the main executable has been loaded.  To fetch the
       // necessary info (e.g., AT_ENTRY) we need to flush the auxv cache so
       // gdb will refetch it.
       invalidate_auxv_cache_inf (current_inferior ());
       svr4_relocate_main_executable ();
       info->exec_displacement_known = true;
+
+      // Now that the main executable is loaded breakpoints in it can be
+      // enabled (e.g., prologue parsing will now work as memory addresses
+      // have been relocated and are readable).
+      enable_breakpoints_after_startup ();
     }
 
   /* If fuchsia starts using probes, call svr4_handle_solib_event here.  */