(_bfd_link_section_stabs): Do not skip N_EXCL stabs.
[deliverable/binutils-gdb.git] / gdb / windows-nat.c
index c6f8f66fd16bcd109f88c2e009cf997a09096e1f..8b26916e2b77b206dcfb15d1f059ae5e0e6826f4 100644 (file)
@@ -27,7 +27,6 @@
 /* We assume we're being built with and will be used for cygwin.  */
 
 #include "defs.h"
-#include "tm.h"                        /* required for SSE registers */
 #include "frame.h"             /* required by inferior.h */
 #include "inferior.h"
 #include "target.h"
@@ -36,7 +35,6 @@
 #include "completer.h"
 #include "regcache.h"
 #include "top.h"
-#include "i386-tdep.h"
 #include <signal.h>
 #include <sys/types.h>
 #include <fcntl.h>
 #include "gdbcmd.h"
 #include <sys/param.h>
 #include <unistd.h>
+#include "exec.h"
+
+#include "i386-tdep.h"
+#include "i387-tdep.h"
 
 /* The ui's event loop. */
 extern int (*ui_loop_hook) (int signo);
@@ -70,16 +72,12 @@ enum
 #include <sys/procfs.h>
 #include <psapi.h>
 
-#ifdef HAVE_SSE_REGS
 #define CONTEXT_DEBUGGER_DR CONTEXT_DEBUGGER | CONTEXT_DEBUG_REGISTERS \
        | CONTEXT_EXTENDED_REGISTERS
-#else
-#define CONTEXT_DEBUGGER_DR CONTEXT_DEBUGGER | CONTEXT_DEBUG_REGISTERS
-#endif
 
 static unsigned dr[8];
-static int debug_registers_changed = 0;
-static int debug_registers_used = 0;
+static int debug_registers_changed;
+static int debug_registers_used;
 
 /* The string sent by cygwin when it processes a signal.
    FIXME: This should be in a cygwin include file. */
@@ -110,6 +108,7 @@ typedef struct thread_info_struct
     HANDLE h;
     char *name;
     int suspend_count;
+    int reload_context;
     CONTEXT context;
     STACKFRAME sf;
   }
@@ -187,7 +186,6 @@ static const int mappings[] =
   context_offset (FloatSave.DataSelector),
   context_offset (FloatSave.DataOffset),
   context_offset (FloatSave.ErrorSelector)
-#ifdef HAVE_SSE_REGS
   /* XMM0-7 */ ,
   context_offset (ExtendedRegisters[10*16]),
   context_offset (ExtendedRegisters[11*16]),
@@ -199,7 +197,6 @@ static const int mappings[] =
   context_offset (ExtendedRegisters[17*16]),
   /* MXCSR */
   context_offset (ExtendedRegisters[24])
-#endif
 };
 
 #undef context_offset
@@ -232,7 +229,6 @@ check (BOOL ok, const char *file, int line)
                     GetLastError ());
 }
 
-
 /* Find a thread record given a thread id.
    If get_context then also retrieve the context for this
    thread. */
@@ -250,19 +246,7 @@ thread_rec (DWORD id, int get_context)
              th->suspend_count = SuspendThread (th->h) + 1;
            else if (get_context < 0)
              th->suspend_count = -1;
-
-           th->context.ContextFlags = CONTEXT_DEBUGGER_DR;
-           GetThreadContext (th->h, &th->context);
-           if (id == current_event.dwThreadId)
-             {
-               /* Copy dr values from that thread.  */
-               dr[0] = th->context.Dr0;
-               dr[1] = th->context.Dr1;
-               dr[2] = th->context.Dr2;
-               dr[3] = th->context.Dr3;
-               dr[6] = th->context.Dr6;
-               dr[7] = th->context.Dr7;
-             }
+           th->reload_context = 1;
          }
        return th;
       }
@@ -352,12 +336,34 @@ do_child_fetch_inferior_registers (int r)
 {
   char *context_offset = ((char *) &current_thread->context) + mappings[r];
   long l;
-  if (r == FCS_REGNUM)
+
+  if (!current_thread)
+    return;    /* Windows sometimes uses a non-existent thread id in its
+                  events */
+
+  if (current_thread->reload_context)
+    {
+      thread_info *th = current_thread;
+      th->context.ContextFlags = CONTEXT_DEBUGGER_DR;
+      GetThreadContext (th->h, &th->context);
+      /* Copy dr values from that thread.  */
+      dr[0] = th->context.Dr0;
+      dr[1] = th->context.Dr1;
+      dr[2] = th->context.Dr2;
+      dr[3] = th->context.Dr3;
+      dr[6] = th->context.Dr6;
+      dr[7] = th->context.Dr7;
+      current_thread->reload_context = 0;
+    }
+
+#define I387_ST0_REGNUM I386_ST0_REGNUM
+
+  if (r == I387_FISEG_REGNUM)
     {
       l = *((long *) context_offset) & 0xffff;
       supply_register (r, (char *) &l);
     }
-  else if (r == FOP_REGNUM)
+  else if (r == I387_FOP_REGNUM)
     {
       l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
       supply_register (r, (char *) &l);
@@ -369,19 +375,26 @@ do_child_fetch_inferior_registers (int r)
       for (r = 0; r < NUM_REGS; r++)
        do_child_fetch_inferior_registers (r);
     }
+
+#undef I387_ST0_REGNUM
 }
 
 static void
 child_fetch_inferior_registers (int r)
 {
   current_thread = thread_rec (PIDGET (inferior_ptid), TRUE);
-  do_child_fetch_inferior_registers (r);
+  /* Check if current_thread exists.  Windows sometimes uses a non-existent
+     thread id in its events */
+  if (current_thread)
+    do_child_fetch_inferior_registers (r);
 }
 
 static void
 do_child_store_inferior_registers (int r)
 {
-  if (r >= 0)
+  if (!current_thread)
+    /* Windows sometimes uses a non-existent thread id in its events */;
+  else if (r >= 0)
     regcache_collect (r, ((char *) &current_thread->context) + mappings[r]);
   else
     {
@@ -395,7 +408,10 @@ static void
 child_store_inferior_registers (int r)
 {
   current_thread = thread_rec (PIDGET (inferior_ptid), TRUE);
-  do_child_store_inferior_registers (r);
+  /* Check if current_thread exists.  Windows sometimes uses a non-existent
+     thread id in its events */
+  if (current_thread)
+    do_child_store_inferior_registers (r);
 }
 
 static int psapi_loaded = 0;
@@ -637,7 +653,7 @@ get_image_name (HANDLE h, void *address, int unicode)
 
   /* See if we could read the address of a string, and that the
      address isn't null. */
-  if (!ReadProcessMemory (h, address,  &address_ptr, sizeof (address_ptr), &done) 
+  if (!ReadProcessMemory (h, address,  &address_ptr, sizeof (address_ptr), &done)
       || done != sizeof (address_ptr) || !address_ptr)
     return NULL;
 
@@ -802,7 +818,7 @@ get_relocated_section_addrs (bfd *abfd, CORE_ADDR text_load)
 static struct objfile *
 solib_symbols_add (char *name, int from_tty, CORE_ADDR load_addr)
 {
-  struct section_addr_info *section_addrs_ptr = NULL;
+  struct section_addr_info *addrs = NULL;
   static struct objfile *result = NULL;
   bfd *abfd = NULL;
 
@@ -825,33 +841,28 @@ solib_symbols_add (char *name, int from_tty, CORE_ADDR load_addr)
     {
       if (bfd_check_format (abfd, bfd_object))
        {
-         section_addrs_ptr = get_relocated_section_addrs (abfd, load_addr);
+         addrs = get_relocated_section_addrs (abfd, load_addr);
        }
 
       bfd_close (abfd);
     }
 
-  if (section_addrs_ptr)
+  if (addrs)
     {
-      result = safe_symbol_file_add (name, from_tty, section_addrs_ptr,
-                                    0, OBJF_SHARED);
-
-      free_section_addr_info (section_addrs_ptr);
+      result = safe_symbol_file_add (name, from_tty, addrs, 0, OBJF_SHARED);
+      free_section_addr_info (addrs);
     }
-
   else
     {
       /* Fallback on handling just the .text section. */
-      struct section_addr_info *section_addrs;
       struct cleanup *my_cleanups;
 
-      section_addrs = alloc_section_addr_info (1);
-      my_cleanups = make_cleanup (xfree, section_addrs);
-      section_addrs.other[0].name = ".text";
-      section_addrs.other[0].addr = load_addr;
+      addrs = alloc_section_addr_info (1);
+      my_cleanups = make_cleanup (xfree, addrs);
+      addrs->other[0].name = ".text";
+      addrs->other[0].addr = load_addr;
 
-      result = safe_symbol_file_add (name, from_tty, &section_addrs,
-                                    0, OBJF_SHARED);
+      result = safe_symbol_file_add (name, from_tty, addrs, 0, OBJF_SHARED);
       do_cleanups (my_cleanups);
     }
 
@@ -1183,7 +1194,7 @@ child_continue (DWORD continue_status, int id)
          th->suspend_count = 0;
          if (debug_registers_changed)
            {
-             /* Only change the value of the debug reisters */
+             /* Only change the value of the debug registers */
              th->context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
              th->context.Dr0 = dr[0];
              th->context.Dr1 = dr[1];
@@ -1201,6 +1212,19 @@ child_continue (DWORD continue_status, int id)
   return res;
 }
 
+/* Called in pathological case where Windows fails to send a
+   CREATE_PROCESS_DEBUG_EVENT after an attach.  */
+DWORD
+fake_create_process (void)
+{
+  current_process_handle = OpenProcess (PROCESS_ALL_ACCESS, FALSE,
+                                       current_event.dwProcessId);
+  main_thread_id = current_event.dwThreadId;
+  current_thread = child_add_thread (main_thread_id,
+                                    current_event.u.CreateThread.hThread);
+  return main_thread_id;
+}
+
 /* Get the next event from the child.  Return 1 if the event requires
    handling by WFI (or whatever).
  */
@@ -1209,7 +1233,7 @@ get_child_debug_event (int pid, struct target_waitstatus *ourstatus)
 {
   BOOL debug_event;
   DWORD continue_status, event_code;
-  thread_info *th = NULL;
+  thread_info *th;
   static thread_info dummy_thread_info;
   int retval = 0;
 
@@ -1223,6 +1247,7 @@ get_child_debug_event (int pid, struct target_waitstatus *ourstatus)
 
   event_code = current_event.dwDebugEventCode;
   ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
+  th = NULL;
 
   switch (event_code)
     {
@@ -1232,7 +1257,17 @@ get_child_debug_event (int pid, struct target_waitstatus *ourstatus)
                     (unsigned) current_event.dwThreadId,
                     "CREATE_THREAD_DEBUG_EVENT"));
       if (saw_create != 1)
-       break;
+       {
+         if (!saw_create && attach_flag)
+           {
+             /* Kludge around a Windows bug where first event is a create
+                thread event.  Caused when attached process does not have
+                a main thread. */
+             retval = ourstatus->value.related_pid = fake_create_process ();
+             saw_create++;
+           }
+         break;
+       }
       /* Record the existence of this thread */
       th = child_add_thread (current_event.dwThreadId,
                             current_event.u.CreateThread.hThread);
@@ -1248,10 +1283,11 @@ get_child_debug_event (int pid, struct target_waitstatus *ourstatus)
                     (unsigned) current_event.dwProcessId,
                     (unsigned) current_event.dwThreadId,
                     "EXIT_THREAD_DEBUG_EVENT"));
-      if (saw_create != 1)
-       break;
-      child_delete_thread (current_event.dwThreadId);
-      th = &dummy_thread_info;
+      if (current_event.dwThreadId != main_thread_id)
+       {
+         child_delete_thread (current_event.dwThreadId);
+         th = &dummy_thread_info;
+       }
       break;
 
     case CREATE_PROCESS_DEBUG_EVENT:
@@ -1267,12 +1303,10 @@ get_child_debug_event (int pid, struct target_waitstatus *ourstatus)
        }
 
       current_process_handle = current_event.u.CreateProcessInfo.hProcess;
+      if (main_thread_id)
+       child_delete_thread (main_thread_id);
       main_thread_id = current_event.dwThreadId;
       /* Add the main thread */
-#if 0
-      th = child_add_thread (current_event.dwProcessId,
-                            current_event.u.CreateProcessInfo.hProcess);
-#endif
       th = child_add_thread (main_thread_id,
                             current_event.u.CreateProcessInfo.hThread);
       retval = ourstatus->value.related_pid = current_event.dwThreadId;
@@ -1357,8 +1391,8 @@ get_child_debug_event (int pid, struct target_waitstatus *ourstatus)
     CHECK (child_continue (continue_status, -1));
   else
     {
-      current_thread = th ? : thread_rec (current_event.dwThreadId, TRUE);
       inferior_ptid = pid_to_ptid (retval);
+      current_thread = th ?: thread_rec (current_event.dwThreadId, TRUE);
     }
 
 out:
@@ -1573,10 +1607,9 @@ child_attach (char *args, int from_tty)
     }
 
   if (has_detach_ability ())
-    {
-      attach_flag = 1;
-      DebugSetProcessKillOnExit (FALSE);
-    }
+    DebugSetProcessKillOnExit (FALSE);
+
+  attach_flag = 1;
 
   if (from_tty)
     {
@@ -1635,7 +1668,6 @@ child_files_info (struct target_ops *ignore)
       attach_flag ? "attached" : "child", target_pid_to_str (inferior_ptid));
 }
 
-/* ARGSUSED */
 static void
 child_open (char *arg, int from_tty)
 {
@@ -1699,6 +1731,8 @@ child_create_inferior (char *exec_file, char *allargs, char **env)
   if (new_console)
     flags |= CREATE_NEW_CONSOLE;
 
+  attach_flag = 0;
+
   args = alloca (strlen (toexec) + strlen (allargs) + 2);
   strcpy (args, toexec);
   strcat (args, " ");
@@ -1900,7 +1934,8 @@ child_kill_inferior (void)
   CHECK (CloseHandle (current_process_handle));
 
   /* this may fail in an attached process so don't check. */
-  (void) CloseHandle (current_thread->h);
+  if (current_thread && current_thread->h)
+    (void) CloseHandle (current_thread->h);
   target_mourn_inferior ();    /* or just child_mourn_inferior? */
 }
 
@@ -2152,7 +2187,6 @@ cygwin_get_dr6 (void)
   return dr[6];
 }
 
-
 /* Determine if the thread referenced by "pid" is alive
    by "polling" it.  If WaitForSingleObject returns WAIT_OBJECT_0
    it means that the pid has died.  Otherwise it is assumed to be alive. */
@@ -2204,66 +2238,65 @@ core_dll_symbols_add (char *dll_name, DWORD base_addr)
       }
   }
 
-  register_loaded_dll (dll_name, base_addr + 0x1000);
-  solib_symbols_add (dll_name, 0, (CORE_ADDR) base_addr + 0x1000);
+    register_loaded_dll (dll_name, base_addr + 0x1000);
+    solib_symbols_add (dll_name, 0, (CORE_ADDR) base_addr + 0x1000);
 
-out:
-  return 1;
-}
+  out:
+    return 1;
+  }
 
-typedef struct
-{
-  struct target_ops *target;
-  bfd_vma addr;
-}
-map_code_section_args;
+  typedef struct
+  {
+    struct target_ops *target;
+    bfd_vma addr;
+  } map_code_section_args;
 
-static void
-map_single_dll_code_section (bfd * abfd, asection * sect, void *obj)
-{
-  int old;
-  int update_coreops;
-  struct section_table *new_target_sect_ptr;
+  static void
+  map_single_dll_code_section (bfd * abfd, asection * sect, void *obj)
+  {
+    int old;
+    int update_coreops;
+    struct section_table *new_target_sect_ptr;
 
-  map_code_section_args *args = (map_code_section_args *) obj;
-  struct target_ops *target = args->target;
-  if (sect->flags & SEC_CODE)
-    {
-      update_coreops = core_ops.to_sections == target->to_sections;
+    map_code_section_args *args = (map_code_section_args *) obj;
+    struct target_ops *target = args->target;
+    if (sect->flags & SEC_CODE)
+      {
+       update_coreops = core_ops.to_sections == target->to_sections;
 
-      if (target->to_sections)
-       {
-         old = target->to_sections_end - target->to_sections;
-         target->to_sections = (struct section_table *)
-           xrealloc ((char *) target->to_sections,
-                     (sizeof (struct section_table)) * (1 + old));
-       }
-      else
-       {
-         old = 0;
-         target->to_sections = (struct section_table *)
-           xmalloc ((sizeof (struct section_table)));
-       }
-      target->to_sections_end = target->to_sections + (1 + old);
+       if (target->to_sections)
+         {
+           old = target->to_sections_end - target->to_sections;
+           target->to_sections = (struct section_table *)
+             xrealloc ((char *) target->to_sections,
+                       (sizeof (struct section_table)) * (1 + old));
+         }
+       else
+         {
+           old = 0;
+           target->to_sections = (struct section_table *)
+             xmalloc ((sizeof (struct section_table)));
+         }
+       target->to_sections_end = target->to_sections + (1 + old);
 
-      /* Update the to_sections field in the core_ops structure
-        if needed.  */
-      if (update_coreops)
-       {
-         core_ops.to_sections = target->to_sections;
-         core_ops.to_sections_end = target->to_sections_end;
-       }
-      new_target_sect_ptr = target->to_sections + old;
-      new_target_sect_ptr->addr = args->addr + bfd_section_vma (abfd, sect);
-      new_target_sect_ptr->endaddr = args->addr + bfd_section_vma (abfd, sect) +
-       bfd_section_size (abfd, sect);;
-      new_target_sect_ptr->the_bfd_section = sect;
-      new_target_sect_ptr->bfd = abfd;
-    }
-}
+       /* Update the to_sections field in the core_ops structure
+          if needed.  */
+       if (update_coreops)
+         {
+           core_ops.to_sections = target->to_sections;
+           core_ops.to_sections_end = target->to_sections_end;
+         }
+       new_target_sect_ptr = target->to_sections + old;
+       new_target_sect_ptr->addr = args->addr + bfd_section_vma (abfd, sect);
+       new_target_sect_ptr->endaddr = args->addr + bfd_section_vma (abfd, sect) +
+         bfd_section_size (abfd, sect);;
+       new_target_sect_ptr->the_bfd_section = sect;
+       new_target_sect_ptr->bfd = abfd;
+      }
+  }
 
-static int
-dll_code_sections_add (const char *dll_name, int base_addr, struct target_ops *target)
+  static int
+  dll_code_sections_add (const char *dll_name, int base_addr, struct target_ops *target)
 {
   bfd *dll_bfd;
   map_code_section_args map_args;
This page took 0.029811 seconds and 4 git commands to generate.