aefb0e1
Index: gdb-6.6/gdb/doc/observer.texi
9231e41
===================================================================
aefb0e1
--- gdb-6.6.orig/gdb/doc/observer.texi
aefb0e1
+++ gdb-6.6/gdb/doc/observer.texi
9231e41
@@ -119,6 +119,10 @@ when @value{GDBN} calls this observer, t
9231e41
 haven't been loaded yet.
0e6ea41
 @end deftypefun
0e6ea41
 
0e6ea41
+@deftypefun void mourn_inferior (struct target_ops *@var{target})
0e6ea41
+@value{GDBN} has just detached from an inferior.
0e6ea41
+@end deftypefun
0e6ea41
+
0e6ea41
 @deftypefun void solib_unloaded (struct so_list *@var{solib})
9231e41
 The shared library specified by @var{solib} has been unloaded.
0e6ea41
 @end deftypefun
aefb0e1
Index: gdb-6.6/gdb/linux-nat.c
9231e41
===================================================================
aefb0e1
--- gdb-6.6.orig/gdb/linux-nat.c
aefb0e1
+++ gdb-6.6/gdb/linux-nat.c
aefb0e1
@@ -803,11 +803,23 @@ iterate_over_lwps (int (*callback) (stru
bc60002
 {
bc60002
   struct lwp_info *lp, *lpnext;
bc60002
 
bc60002
-  for (lp = lwp_list; lp; lp = lpnext)
bc60002
+  if (lwp_list != NULL)
bc60002
     {
bc60002
-      lpnext = lp->next;
bc60002
+      for (lp = lwp_list; lp; lp = lpnext)
bc60002
+        {
bc60002
+          lpnext = lp->next;
bc60002
+          if ((*callback) (lp, data))
bc60002
+	    return lp;
bc60002
+        }
bc60002
+    }
bc60002
+  else
bc60002
+    {
bc60002
+      /* We are calling iterate_over_lwps for a non-threaded program.
bc60002
+         Initialize the lwp list to the inferior's ptid.  */
bc60002
+      lp = add_lwp (BUILD_LWP (GET_PID (inferior_ptid),
bc60002
+			       GET_PID (inferior_ptid)));
bc60002
       if ((*callback) (lp, data))
bc60002
-	return lp;
bc60002
+        return lp;
bc60002
     }
bc60002
 
bc60002
   return NULL;
9231e41
@@ -3262,6 +3274,18 @@ linux_nat_add_target (struct target_ops 
9231e41
   thread_db_init (t);
bc60002
 }
bc60002
 
0e6ea41
+/* Observer function for a mourn inferior event.  This is needed
bc60002
+   because if iterate_over_lwps is called for a non-threaded program
bc60002
+   to handle watchpoints, the lwp list gets initialized but there is
0e6ea41
+   no corresponding clean-up when the inferior is detached.  In
0e6ea41
+   a threaded program, the observer is simply redundant as the
0e6ea41
+   same clean-up gets done in linux_nat_mourn_inferior.  */
bc60002
+static void
0e6ea41
+linux_nat_mourn_inferior_observer (struct target_ops *objfile)
bc60002
+{
bc60002
+  init_lwp_list ();
bc60002
+}
bc60002
+
bc60002
 void
bc60002
 _initialize_linux_nat (void)
bc60002
 {
9231e41
@@ -3276,6 +3300,8 @@ Specify any of the following keywords fo
bc60002
   status   -- list a different bunch of random process info.\n\
9231e41
   all      -- list all available /proc info."));
0e6ea41
 
9231e41
+  observer_attach_mourn_inferior (linux_nat_mourn_inferior_observer);
9231e41
+
9231e41
   /* Save the original signal mask.  */
9231e41
   sigprocmask (SIG_SETMASK, NULL, &normal_mask);
9231e41
 
aefb0e1
Index: gdb-6.6/gdb/target.c
9231e41
===================================================================
aefb0e1
--- gdb-6.6.orig/gdb/target.c
aefb0e1
+++ gdb-6.6/gdb/target.c
aefb0e1
@@ -40,6 +40,7 @@
0e6ea41
 #include "gdb_assert.h"
0e6ea41
 #include "gdbcore.h"
aefb0e1
 #include "exceptions.h"
0e6ea41
+#include "observer.h"
0e6ea41
 
0e6ea41
 static void target_info (char *, int);
0e6ea41
 
aefb0e1
@@ -276,6 +277,13 @@ target_load (char *arg, int from_tty)
0e6ea41
   (*current_target.to_load) (arg, from_tty);
0e6ea41
 }
0e6ea41
 
0e6ea41
+void
0e6ea41
+target_mourn_inferior (void)
0e6ea41
+{
0e6ea41
+  (*current_target.to_mourn_inferior) ();
0e6ea41
+  observer_notify_mourn_inferior (&current_target);
0e6ea41
+}
0e6ea41
+
0e6ea41
 static int
0e6ea41
 nomemory (CORE_ADDR memaddr, char *myaddr, int len, int write,
0e6ea41
 	  struct target_ops *t)
aefb0e1
Index: gdb-6.6/gdb/target.h
9231e41
===================================================================
aefb0e1
--- gdb-6.6.orig/gdb/target.h
aefb0e1
+++ gdb-6.6/gdb/target.h
aefb0e1
@@ -891,8 +891,7 @@ int target_follow_fork (int follow_child
0e6ea41
 
0e6ea41
 /* The inferior process has died.  Do what is right.  */
0e6ea41
 
0e6ea41
-#define	target_mourn_inferior()	\
0e6ea41
-     (*current_target.to_mourn_inferior) ()
0e6ea41
+extern void target_mourn_inferior (void);
0e6ea41
 
0e6ea41
 /* Does target have enough data to do a run or attach command? */
0e6ea41