1999-01-19 Fernando Nasser <fnasser@totem.to.cygnus.com>
[deliverable/binutils-gdb.git] / gdb / monitor.c
index 60cfc143e71a61a84530176f8c31fd58c0c73b87..a8e4a6be26d9d5e780728afff15dd818186bdec8 100644 (file)
@@ -71,6 +71,8 @@ static void monitor_command PARAMS ((char *args, int fromtty));
 static void monitor_fetch_register PARAMS ((int regno));
 static void monitor_store_register PARAMS ((int regno));
 
+static int monitor_printable_string PARAMS ((char *newstr, char *oldstr));
+static void monitor_error PARAMS ((char *format, CORE_ADDR memaddr, int len, char *string, int final_char));
 static void monitor_detach PARAMS ((char *args, int from_tty));
 static void monitor_resume PARAMS ((int pid, int step, enum target_signal sig));
 static void monitor_interrupt PARAMS ((int signo));
@@ -122,11 +124,7 @@ static void (*ofunc)();            /* Old SIGINT signal handler */
 #if ! defined(EXTRA_RDEBUG)
 #define EXTRA_RDEBUG 0
 #endif
-#if EXTRA_RDEBUG
-#define RDEBUG(stuff) { if (remote_debug) printf stuff ; }
-#else
-#define RDEBUG(stuff) {}
-#endif
+#define RDEBUG(stuff) { if (EXTRA_RDEBUG && remote_debug) printf stuff ; }
 
 /* Descriptor for I/O to remote machine.  Initialize it to NULL so
    that monitor_open knows that we don't have a file open when the
@@ -149,6 +147,69 @@ static DCACHE *remote_dcache;
 static int first_time=0;       /* is this the first time we're executing after 
                                        gaving created the child proccess? */
 
+/* Convert a string into a printable representation, Return # byte in the
+   new string.  */
+
+static int
+monitor_printable_string (newstr, oldstr)
+     char *newstr;
+     char *oldstr;
+{
+  char *save = newstr;
+  int ch;
+
+  while ((ch = *oldstr++) != '\0')
+    {
+      switch (ch)
+        {
+       default:
+         if (isprint (ch))
+           *newstr++ = ch;
+
+         else
+           {
+             sprintf (newstr, "\\x%02x", ch & 0xff);
+             newstr += 4;
+           }
+         break;
+
+       case '\\': *newstr++ = '\\'; *newstr++ = '\\';  break;
+       case '\b': *newstr++ = '\\'; *newstr++ = 'b';   break;
+       case '\f': *newstr++ = '\\'; *newstr++ = 't';   break;
+       case '\n': *newstr++ = '\\'; *newstr++ = 'n';   break;
+       case '\r': *newstr++ = '\\'; *newstr++ = 'r';   break;
+       case '\t': *newstr++ = '\\'; *newstr++ = 't';   break;
+       case '\v': *newstr++ = '\\'; *newstr++ = 'v';   break;
+        }
+    }
+
+  *newstr++ = '\0';
+  return newstr - save;
+}
+
+/* Print monitor errors with a string, converting the string to printable
+   representation.  */
+
+static void
+monitor_error (format, memaddr, len, string, final_char)
+     char *format;
+     CORE_ADDR memaddr;
+     int len;
+     char *string;
+     int final_char;
+{
+  int real_len = (len == 0 && string != (char *)0) ? strlen (string) : len;
+  char *safe_string = alloca ((real_len * 4) + 1);
+  char *p, *q;
+  int ch;
+  int safe_len = monitor_printable_string (safe_string, string);
+
+  if (final_char)
+    error (format, (int)memaddr, p - safe_string, safe_string, final_char);
+  else
+    error (format, (int)memaddr, p - safe_string, safe_string);
+}
+
 /* Convert hex digit A to a number.  */
 
 static int
@@ -258,17 +319,22 @@ monitor_printf_noecho (va_alist)
 
   monitor_vsprintf (sndbuf, pattern, args);
 
+  len = strlen (sndbuf);
+  if (len + 1 > sizeof sndbuf)
+    abort ();
+
 #if 0
   if (remote_debug > 0)
     puts_debug ("sent -->", sndbuf, "<--");
 #endif
-  RDEBUG(("sent[%s]\n",sndbuf)) ;
+  if (EXTRA_RDEBUG
+      && remote_debug)
+    {
+      char *safe_string = (char *) alloca ((strlen (sndbuf) * 4) + 1);
+      monitor_printable_string (safe_string, sndbuf);
+      printf ("sent[%s]\n", safe_string);
+    }
   
-  len = strlen (sndbuf);
-
-  if (len + 1 > sizeof sndbuf)
-    abort ();
-
   monitor_write (sndbuf, len);
 }
 
@@ -297,14 +363,21 @@ monitor_printf (va_alist)
 
   monitor_vsprintf (sndbuf, pattern, args);
 
+  len = strlen (sndbuf);
+  if (len + 1 > sizeof sndbuf)
+    abort ();
+
 #if 0
   if (remote_debug > 0)
     puts_debug ("sent -->", sndbuf, "<--");
 #endif
-  RDEBUG(("sent[%s]\n",sndbuf)) 
-  len = strlen (sndbuf);
-  if (len + 1 > sizeof sndbuf)
-    abort ();
+  if (EXTRA_RDEBUG
+      && remote_debug)
+    {
+      char *safe_string = (char *) alloca ((len * 4) + 1);
+      monitor_printable_string (safe_string, sndbuf);
+      printf ("sent[%s]\n", safe_string);
+    }
 
   monitor_write (sndbuf, len);
 
@@ -325,7 +398,8 @@ monitor_write (buf, buflen)
      int buflen;
 {
   if (SERIAL_WRITE(monitor_desc, buf, buflen))
-    fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno));
+    fprintf_unfiltered (gdb_stderr, "SERIAL_WRITE failed: %s\n",
+                       safe_strerror (errno));
 }
 
 
@@ -449,7 +523,14 @@ monitor_expect (string, buf, buflen)
   int obuflen = buflen;
   int c;
   extern struct target_ops *targ_ops;
-  RDEBUG(("MON Expecting '%s'\n",string)) ;
+
+  if (EXTRA_RDEBUG
+      && remote_debug)
+    {
+      char *safe_string = (char *) alloca ((strlen (string) * 4) + 1);
+      monitor_printable_string (safe_string, string);
+      printf ("MON Expecting '%s'\n", safe_string);
+    }
 
   immediate_quit = 1;
   while (1)
@@ -847,6 +928,8 @@ parse_register_dump (buf, len)
         points to the start of the register value.  */
       struct re_registers register_strings;
 
+      memset (&register_strings, 0, sizeof (struct re_registers));
+
       if (re_search (&register_pattern, buf, len, 0, len,
                     &register_strings) == -1)
        break;
@@ -1620,8 +1703,8 @@ monitor_read_memory_single (memaddr, myaddr, len)
       if ((c == '0') && ((c = readchar (timeout)) == 'x'))
        ;
       else
-       error ("monitor_read_memory_single (0x%x):  bad response from monitor: %.*s%c.",
-              memaddr, i, membuf, c);
+       monitor_error ("monitor_read_memory_single (0x%x):  bad response from monitor: %.*s%c.",
+                      memaddr, i, membuf, c);
     }
   for (i = 0; i < len * 2; i++)
     {
@@ -1635,8 +1718,8 @@ monitor_read_memory_single (memaddr, myaddr, len)
          if (c == ' ')
            continue;
 
-         error ("monitor_read_memory_single (0x%x):  bad response from monitor: %.*s%c.",
-                memaddr, i, membuf, c);
+         monitor_error ("monitor_read_memory_single (0x%x):  bad response from monitor: %.*s%c.",
+                        memaddr, i, membuf, c);
        }
 
       membuf[i] = c;
@@ -1665,8 +1748,8 @@ monitor_read_memory_single (memaddr, myaddr, len)
   val = strtoul (membuf, &p, 16);
 
   if (val == 0 && membuf == p)
-    error ("monitor_read_memory_single (0x%x):  bad value from monitor: %s.",
-          memaddr, membuf);
+    monitor_error ("monitor_read_memory_single (0x%x):  bad value from monitor: %s.",
+                  memaddr, 0, membuf, 0);
 
   /* supply register stores in target byte order, so swap here */
 
@@ -1692,6 +1775,12 @@ monitor_read_memory (memaddr, myaddr, len)
   int i;
   CORE_ADDR dumpaddr;
 
+  if (len <= 0)
+    {
+      RDEBUG (("Zero length call to monitor_read_memory\n"));
+      return 0;
+    }
+
   if (remote_debug) printf("MON read block ta(%08x) ha(%08x) %d\n",
          (unsigned long) memaddr , (unsigned long)myaddr, len);
 
@@ -1734,8 +1823,8 @@ monitor_read_memory (memaddr, myaddr, len)
       resp_len = monitor_expect (current_monitor->getmem.term, buf, sizeof buf); /* get response */
 
       if (resp_len <= 0)
-       error ("monitor_read_memory (0x%x):  excessive response from monitor: %.*s.",
-              memaddr, resp_len, buf);
+       monitor_error ("monitor_read_memory (0x%x):  excessive response from monitor: %.*s.",
+                      memaddr, resp_len, buf, 0);
 
       if (current_monitor->getmem.term_cmd)
        {
@@ -1759,20 +1848,21 @@ monitor_read_memory (memaddr, myaddr, len)
       struct re_registers resp_strings;
       RDEBUG(("MON getmem.resp_delim %s\n",current_monitor->getmem.resp_delim)) ;
 
+      memset (&resp_strings, 0, sizeof (struct re_registers));
       tmp = strlen (p);
       retval = re_search (&getmem_resp_delim_pattern, p, tmp, 0, tmp,
                          &resp_strings);
 
       if (retval < 0)
-       error ("monitor_read_memory (0x%x):  bad response from monitor: %.*s.",
-              memaddr, resp_len, buf);
+       monitor_error ("monitor_read_memory (0x%x):  bad response from monitor: %.*s.",
+                      memaddr, resp_len, buf, 0);
 
       p += resp_strings.end[0];
 #if 0
       p = strstr (p, current_monitor->getmem.resp_delim);
       if (!p)
-       error ("monitor_read_memory (0x%x):  bad response from monitor: %.*s.",
-              memaddr, resp_len, buf);
+       monitor_error ("monitor_read_memory (0x%x):  bad response from monitor: %.*s.",
+                      memaddr, resp_len, buf, 0);
       p += strlen (current_monitor->getmem.resp_delim);
 #endif
     }
@@ -1816,15 +1906,16 @@ monitor_read_memory (memaddr, myaddr, len)
            break;
 
          if (*p == '\000' || *p == '\n' || *p == '\r')
-           error ("monitor_read_memory (0x%x):  badly terminated response from monitor: %.*s", memaddr, resp_len, buf);
+           monitor_error ("monitor_read_memory (0x%x):  badly terminated response from monitor: %.*s",
+                          memaddr, resp_len, buf, 0);
          p++;
        }
 
       val = strtoul (p, &p1, 16);
 
       if (val == 0 && p == p1)
-       error ("monitor_read_memory (0x%x):  bad value from monitor: %.*s.", memaddr,
-              resp_len, buf);
+       monitor_error ("monitor_read_memory (0x%x):  bad value from monitor: %.*s.",
+                      memaddr, resp_len, buf, 0);
 
       *myaddr++ = val;
 
@@ -1955,7 +2046,8 @@ monitor_remove_breakpoint (addr, shadow)
          return 0;
        }
     }
-  fprintf_unfiltered (stderr, "Can't find breakpoint associated with 0x%x\n", addr);
+  fprintf_unfiltered (gdb_stderr,
+                     "Can't find breakpoint associated with 0x%x\n", addr);
   return 1;
 }
 
@@ -2110,10 +2202,14 @@ static void init_base_monitor_ops(void)
   monitor_ops.to_doc =   NULL;         
   monitor_ops.to_open =   NULL;                
   monitor_ops.to_close =   monitor_close;
-  monitor_ops.to_attach =   NULL;              
+  monitor_ops.to_attach =   NULL;
+  monitor_ops.to_post_attach = NULL;
+  monitor_ops.to_require_attach = NULL;                
   monitor_ops.to_detach =   monitor_detach;    
+  monitor_ops.to_require_detach = NULL; 
   monitor_ops.to_resume =   monitor_resume;    
-  monitor_ops.to_wait  =   monitor_wait;       
+  monitor_ops.to_wait  =   monitor_wait;
+  monitor_ops.to_post_wait = NULL;     
   monitor_ops.to_fetch_registers  =   monitor_fetch_registers;
   monitor_ops.to_store_registers  =   monitor_store_registers;
   monitor_ops.to_prepare_to_store =   monitor_prepare_to_store;
@@ -2130,11 +2226,30 @@ static void init_base_monitor_ops(void)
   monitor_ops.to_load  =   monitor_load;       
   monitor_ops.to_lookup_symbol =   0;          
   monitor_ops.to_create_inferior =   monitor_create_inferior;
+  monitor_ops.to_post_startup_inferior = NULL;
+  monitor_ops.to_acknowledge_created_inferior = NULL;
+  monitor_ops.to_clone_and_follow_inferior  = NULL;
+  monitor_ops.to_post_follow_inferior_by_clone = NULL;
+  monitor_ops.to_insert_fork_catchpoint = NULL;
+  monitor_ops.to_remove_fork_catchpoint = NULL;
+  monitor_ops.to_insert_vfork_catchpoint = NULL;
+  monitor_ops.to_remove_vfork_catchpoint = NULL;
+  monitor_ops.to_has_forked = NULL;
+  monitor_ops.to_has_vforked = NULL;
+  monitor_ops.to_can_follow_vfork_prior_to_exec = NULL;
+  monitor_ops.to_post_follow_vfork = NULL;
+  monitor_ops.to_insert_exec_catchpoint = NULL;
+  monitor_ops.to_remove_exec_catchpoint = NULL;
+  monitor_ops.to_has_execd = NULL;
+  monitor_ops.to_reported_exec_events_per_exec_call = NULL;
+  monitor_ops.to_has_exited = NULL;
   monitor_ops.to_mourn_inferior =   monitor_mourn_inferior;    
   monitor_ops.to_can_run  =   0;                               
   monitor_ops.to_notice_signals =   0;                                 
   monitor_ops.to_thread_alive  =   0;                          
-  monitor_ops.to_stop  =   monitor_stop;                       
+  monitor_ops.to_stop  =   monitor_stop;
+  monitor_ops.to_pid_to_exec_file = NULL;      
+  monitor_ops.to_core_file_to_sym_file = NULL;
   monitor_ops.to_stratum =   process_stratum;          
   monitor_ops.DONT_USE =   0;                          
   monitor_ops.to_has_all_memory =   1;                 
@@ -2153,6 +2268,9 @@ void
 init_monitor_ops (ops)
      struct target_ops *ops;
 {
+  if (monitor_ops.to_magic != OPS_MAGIC)
+    init_base_monitor_ops ();
+
   memcpy (ops, &monitor_ops, sizeof monitor_ops);
 }
 
@@ -2172,3 +2290,5 @@ When enabled, a hashmark \'#\' is displayed.",
   add_com ("monitor", class_obscure, monitor_command,
           "Send a command to the debug monitor."); 
 }
+
+
This page took 0.027305 seconds and 4 git commands to generate.