2002-04-20 Daniel Jacobowitz <drow@mvista.com>
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-low.c
index 7964f8232484b42a5fe84c8867dedecb402b17bd..10966e0afe6a88674770b306ed313b7ff8c442da 100644 (file)
 #include <stdlib.h>
 #include <unistd.h>
 
+static CORE_ADDR linux_bp_reinsert;
+
+static void linux_resume (int step, int signal);
+
 #define PTRACE_ARG3_TYPE long
 #define PTRACE_XFER_TYPE long
 
@@ -44,19 +48,20 @@ static int use_regsets_p = 1;
 
 extern int errno;
 
-#ifdef HAVE_LINUX_USRREGS
-extern int num_regs;
-extern int regmap[];
-#endif
-
 static int inferior_pid;
 
+struct inferior_linux_data
+{
+  int pid;
+};
+
 /* Start an inferior process and returns its pid.
    ALLARGS is a vector of program-name and args. */
 
 static int
 linux_create_inferior (char *program, char **allargs)
 {
+  struct inferior_linux_data *tdata;
   int pid;
 
   pid = fork ();
@@ -76,6 +81,10 @@ linux_create_inferior (char *program, char **allargs)
     }
 
   add_inferior (pid);
+  tdata = (struct inferior_linux_data *) malloc (sizeof (*tdata));
+  tdata->pid = pid;
+  set_inferior_target_data (current_inferior, tdata);
+
   /* FIXME remove */
   inferior_pid = pid;
   return 0;
@@ -86,6 +95,8 @@ linux_create_inferior (char *program, char **allargs)
 static int
 linux_attach (int pid)
 {
+  struct inferior_linux_data *tdata;
+
   if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0)
     {
       fprintf (stderr, "Cannot attach to process %d: %s (%d)\n", pid,
@@ -95,6 +106,10 @@ linux_attach (int pid)
       _exit (0177);
     }
 
+  add_inferior (pid);
+  tdata = (struct inferior_linux_data *) malloc (sizeof (*tdata));
+  tdata->pid = pid;
+  set_inferior_target_data (current_inferior, tdata);
   return 0;
 }
 
@@ -117,19 +132,75 @@ linux_thread_alive (int pid)
   return 1;
 }
 
+static int
+linux_wait_for_one_inferior (struct inferior_info *child)
+{
+  struct inferior_linux_data *child_data = inferior_target_data (child);
+  int pid, wstat;
+
+  while (1)
+    {
+      pid = waitpid (child_data->pid, &wstat, 0);
+
+      if (pid != child_data->pid)
+       perror_with_name ("wait");
+
+      /* If this target supports breakpoints, see if we hit one.  */
+      if (the_low_target.stop_pc != NULL
+         && WIFSTOPPED (wstat)
+         && WSTOPSIG (wstat) == SIGTRAP)
+       {
+         CORE_ADDR stop_pc;
+
+         if (linux_bp_reinsert != 0)
+           {
+             reinsert_breakpoint (linux_bp_reinsert);
+             linux_bp_reinsert = 0;
+             linux_resume (0, 0);
+             continue;
+           }
+
+         fetch_inferior_registers (0);
+         stop_pc = (*the_low_target.stop_pc) ();
+
+         if (check_breakpoints (stop_pc) != 0)
+           {
+             if (the_low_target.set_pc != NULL)
+               (*the_low_target.set_pc) (stop_pc);
+
+             if (the_low_target.breakpoint_reinsert_addr == NULL)
+               {
+                 linux_bp_reinsert = stop_pc;
+                 uninsert_breakpoint (stop_pc);
+                 linux_resume (1, 0);
+               }
+             else
+               {
+                 reinsert_breakpoint_by_bp
+                   (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ());
+                 linux_resume (0, 0);
+               }
+
+             continue;
+           }
+       }
+
+      return wstat;
+    }
+  /* NOTREACHED */
+  return 0;
+}
+
 /* Wait for process, returns status */
 
 static unsigned char
 linux_wait (char *status)
 {
-  int pid;
   int w;
 
   enable_async_io ();
-  pid = waitpid (inferior_pid, &w, 0);
+  w = linux_wait_for_one_inferior (current_inferior);
   disable_async_io ();
-  if (pid != inferior_pid)
-    perror_with_name ("wait");
 
   if (WIFEXITED (w))
     {
@@ -175,10 +246,10 @@ register_addr (int regnum)
 {
   int addr;
 
-  if (regnum < 0 || regnum >= num_regs)
+  if (regnum < 0 || regnum >= the_low_target.num_regs)
     error ("Invalid register number %d.", regnum);
 
-  addr = regmap[regnum];
+  addr = the_low_target.regmap[regnum];
   if (addr == -1)
     addr = 0;
 
@@ -192,9 +263,9 @@ fetch_register (int regno)
   CORE_ADDR regaddr;
   register int i;
 
-  if (regno >= num_regs)
+  if (regno >= the_low_target.num_regs)
     return;
-  if (cannot_fetch_register (regno))
+  if ((*the_low_target.cannot_fetch_register) (regno))
     return;
 
   regaddr = register_addr (regno);
@@ -225,7 +296,7 @@ static void
 usr_fetch_inferior_registers (int regno)
 {
   if (regno == -1 || regno == 0)
-    for (regno = 0; regno < num_regs; regno++)
+    for (regno = 0; regno < the_low_target.num_regs; regno++)
       fetch_register (regno);
   else
     fetch_register (regno);
@@ -242,10 +313,10 @@ usr_store_inferior_registers (int regno)
 
   if (regno >= 0)
     {
-      if (regno >= num_regs)
+      if (regno >= the_low_target.num_regs)
        return;
 
-      if (cannot_store_register (regno))
+      if ((*the_low_target.cannot_store_register) (regno) == 1)
        return;
 
       regaddr = register_addr (regno);
@@ -259,20 +330,21 @@ usr_store_inferior_registers (int regno)
                  *(int *) (register_data (regno) + i));
          if (errno != 0)
            {
-             /* Warning, not error, in case we are attached; sometimes the
-                kernel doesn't let us at the registers.  */
-             char *err = strerror (errno);
-             char *msg = alloca (strlen (err) + 128);
-             sprintf (msg, "writing register %d: %s",
-                      regno, err);
-             error (msg);
-             return;
+             if ((*the_low_target.cannot_store_register) (regno) == 0)
+               {
+                 char *err = strerror (errno);
+                 char *msg = alloca (strlen (err) + 128);
+                 sprintf (msg, "writing register %d: %s",
+                          regno, err);
+                 error (msg);
+                 return;
+               }
            }
          regaddr += sizeof (int);
        }
     }
   else
-    for (regno = 0; regno < num_regs; regno++)
+    for (regno = 0; regno < the_low_target.num_regs; regno++)
       store_inferior_registers (regno);
 }
 #endif /* HAVE_LINUX_USRREGS */
@@ -444,7 +516,7 @@ linux_read_memory (CORE_ADDR memaddr, char *myaddr, int len)
    returns the value of errno.  */
 
 static int
-linux_write_memory (CORE_ADDR memaddr, char *myaddr, int len)
+linux_write_memory (CORE_ADDR memaddr, const char *myaddr, int len)
 {
   register int i;
   /* Round starting address down to longword boundary.  */
@@ -486,6 +558,13 @@ linux_write_memory (CORE_ADDR memaddr, char *myaddr, int len)
 
   return 0;
 }
+
+static void
+linux_look_up_symbols (void)
+{
+  /* Don't need to look up any symbols yet.  */
+}
+
 \f
 static struct target_ops linux_target_ops = {
   linux_create_inferior,
@@ -498,11 +577,14 @@ static struct target_ops linux_target_ops = {
   linux_store_registers,
   linux_read_memory,
   linux_write_memory,
+  linux_look_up_symbols,
 };
 
 void
 initialize_low (void)
 {
   set_target_ops (&linux_target_ops);
+  set_breakpoint_data (the_low_target.breakpoint,
+                      the_low_target.breakpoint_len);
   init_registers ();
 }
This page took 0.026446 seconds and 4 git commands to generate.