* top.c (init_main): Make `set history file name' complete on file
[deliverable/binutils-gdb.git] / gdb / utils.c
index 46d0eb1e6f4fa6b3085df3d3f0251680a1b8e3ad..df6046a93e66d3b0f4ec939551de63a8f40c8a28 100644 (file)
@@ -1,5 +1,6 @@
 /* General utility routines for GDB, the GNU debugger.
-   Copyright 1986, 1989, 1990-1992, 1995, 1996, 1998, 2000
+   Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
+   1997, 1998, 1999, 2000, 2001
    Free Software Foundation, Inc.
 
    This file is part of GDB.
@@ -40,7 +41,7 @@
 #undef reg
 #endif
 
-#include "signals.h"
+#include <signal.h>
 #include "gdbcmd.h"
 #include "serial.h"
 #include "bfd.h"
@@ -215,14 +216,17 @@ make_cleanup_bfd_close (bfd *abfd)
 static void
 do_close_cleanup (void *arg)
 {
-  close ((int) arg);
+  int *fd = arg;
+  close (*fd);
+  xfree (fd);
 }
 
 struct cleanup *
 make_cleanup_close (int fd)
 {
-  /* int into void*. Outch!! */
-  return make_cleanup (do_close_cleanup, (void *) fd);
+  int *saved_fd = xmalloc (sizeof (fd));
+  *saved_fd = fd;
+  return make_cleanup (do_close_cleanup, saved_fd);
 }
 
 static void
@@ -295,7 +299,7 @@ do_my_cleanups (register struct cleanup **pmy_chain,
     {
       *pmy_chain = ptr->next;  /* Do this first incase recursion */
       (*ptr->function) (ptr->arg);
-      free (ptr);
+      xfree (ptr);
     }
 }
 
@@ -328,7 +332,7 @@ discard_my_cleanups (register struct cleanup **pmy_chain,
   while ((ptr = *pmy_chain) != old_chain)
     {
       *pmy_chain = ptr->next;
-      free (ptr);
+      xfree (ptr);
     }
 }
 
@@ -386,10 +390,11 @@ free_current_contents (void *ptr)
 {
   void **location = ptr;
   if (location == NULL)
-    internal_error ("free_current_contents: NULL pointer");
+    internal_error (__FILE__, __LINE__,
+                   "free_current_contents: NULL pointer");
   if (*location != NULL)
     {
-      free (*location);
+      xfree (*location);
       *location = NULL;
     }
 }
@@ -449,7 +454,7 @@ do_all_continuations (void)
        (continuation_ptr->continuation_hook) (continuation_ptr->arg_list);
        saved_continuation = continuation_ptr;
        continuation_ptr = continuation_ptr->next;
-       free (saved_continuation);
+       xfree (saved_continuation);
      }
 }
 
@@ -464,7 +469,7 @@ discard_all_continuations (void)
     {
       continuation_ptr = cmd_continuation;
       cmd_continuation = continuation_ptr->next;
-      free (continuation_ptr);
+      xfree (continuation_ptr);
     }
 }
 
@@ -511,7 +516,7 @@ do_all_intermediate_continuations (void)
        (continuation_ptr->continuation_hook) (continuation_ptr->arg_list);
        saved_continuation = continuation_ptr;
        continuation_ptr = continuation_ptr->next;
-       free (saved_continuation);
+       xfree (saved_continuation);
      }
 }
 
@@ -526,7 +531,7 @@ discard_all_intermediate_continuations (void)
     {
       continuation_ptr = intermediate_continuation;
       intermediate_continuation = continuation_ptr->next;
-      free (continuation_ptr);
+      xfree (continuation_ptr);
     }
 }
 
@@ -621,7 +626,7 @@ verror (const char *string, va_list args)
   vfprintf_filtered (gdb_lasterr, string, args);
   /* Retrieve the last error and print it to gdb_stderr */
   err_string = error_last_message ();
-  err_string_cleanup = make_cleanup (free, err_string);
+  err_string_cleanup = make_cleanup (xfree, err_string);
   fputs_filtered (err_string, gdb_stderr);
   fprintf_filtered (gdb_stderr, "\n");
   do_cleanups (err_string_cleanup);
@@ -642,7 +647,7 @@ error_stream (struct ui_file *stream)
 {
   long size;
   char *msg = ui_file_xstrdup (stream, &size);
-  make_cleanup (free, msg);
+  make_cleanup (xfree, msg);
   error ("%s", msg);
 }
 
@@ -667,7 +672,8 @@ error_init (void)
    want to continue, dump core, or just exit. */
 
 NORETURN void
-internal_verror (const char *fmt, va_list ap)
+internal_verror (const char *file, int line,
+                const char *fmt, va_list ap)
 {
   static char msg[] = "Internal GDB error: recursive internal error.\n";
   static int dejavu = 0;
@@ -683,7 +689,7 @@ internal_verror (const char *fmt, va_list ap)
     case 1:
       dejavu = 2;
       fputs_unfiltered (msg, gdb_stderr);
-      abort ();
+      internal_error (__FILE__, __LINE__, "failed internal consistency check");
     default:
       dejavu = 3;
       write (STDERR_FILENO, msg, sizeof (msg));
@@ -692,14 +698,14 @@ internal_verror (const char *fmt, va_list ap)
 
   /* Try to get the message out */
   target_terminal_ours ();
-  fputs_unfiltered ("gdb-internal-error: ", gdb_stderr);
+  fprintf_unfiltered (gdb_stderr, "%s:%d: gdb-internal-error: ", file, line);
   vfprintf_unfiltered (gdb_stderr, fmt, ap);
   fputs_unfiltered ("\n", gdb_stderr);
 
   /* Default (no case) is to quit GDB.  When in batch mode this
      lessens the likelhood of GDB going into an infinate loop. */
   continue_p = query ("\
-An internal GDB error was detected.  This may make make further\n\
+An internal GDB error was detected.  This may make further\n\
 debugging unreliable.  Continue this debugging session? ");
 
   /* Default (no case) is to not dump core.  Lessen the chance of GDB
@@ -712,13 +718,13 @@ Create a core file containing the current state of GDB? ");
       if (dump_core_p)
        {
          if (fork () == 0)
-           abort ();
+           internal_error (__FILE__, __LINE__, "failed internal consistency check");
        }
     }
   else
     {
       if (dump_core_p)
-       abort ();
+       internal_error (__FILE__, __LINE__, "failed internal consistency check");
       else
        exit (1);
     }
@@ -728,12 +734,12 @@ Create a core file containing the current state of GDB? ");
 }
 
 NORETURN void
-internal_error (char *string, ...)
+internal_error (const char *file, int line, const char *string, ...)
 {
   va_list ap;
   va_start (ap, string);
 
-  internal_verror (string, ap);
+  internal_verror (file, line, string, ap);
   va_end (ap);
 }
 
@@ -932,7 +938,7 @@ mrealloc (PTR md, PTR ptr, size_t size)
 void
 mfree (PTR md, PTR ptr)
 {
-  free (ptr);
+  xfree (ptr);
 }
 
 #endif /* USE_MMALLOC */
@@ -950,7 +956,7 @@ static void
 malloc_botch (void)
 {
   fprintf_unfiltered (gdb_stderr, "Memory corruption\n");
-  abort ();
+  internal_error (__FILE__, __LINE__, "failed internal consistency check");
 }
 
 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
@@ -999,11 +1005,13 @@ nomem (long size)
 {
   if (size > 0)
     {
-      internal_error ("virtual memory exhausted: can't allocate %ld bytes.", size);
+      internal_error (__FILE__, __LINE__,
+                     "virtual memory exhausted: can't allocate %ld bytes.", size);
     }
   else
     {
-      internal_error ("virtual memory exhausted.");
+      internal_error (__FILE__, __LINE__,
+                     "virtual memory exhausted.");
     }
 }
 
@@ -1077,6 +1085,16 @@ xrealloc (PTR ptr, size_t size)
 {
   return (xmrealloc ((PTR) NULL, ptr, size));
 }
+
+/* Free up space allocated by one of xmalloc(), xcalloc(), or
+   xrealloc().  */
+
+void
+xfree (void *ptr)
+{
+  if (ptr != NULL)
+    free (ptr);
+}
 \f
 
 /* Like asprintf/vasprintf but get an internal_error if the call
@@ -1098,13 +1116,15 @@ xvasprintf (char **ret, const char *format, va_list ap)
   /* NULL could be returned due to a memory allocation problem; a
      badly format string; or something else. */
   if ((*ret) == NULL)
-    internal_error ("%s:%d: vasprintf returned NULL buffer (errno %d)",
-                   __FILE__, __LINE__, errno);
+    internal_error (__FILE__, __LINE__,
+                   "vasprintf returned NULL buffer (errno %d)",
+                   errno);
   /* A negative status with a non-NULL buffer shouldn't never
      happen. But to be sure. */
   if (status < 0)
-    internal_error ("%s:%d: vasprintf call failed (errno %d)",
-                   __FILE__, __LINE__, errno);
+    internal_error (__FILE__, __LINE__,
+                   "vasprintf call failed (errno %d)",
+                   errno);
 }
 
 
@@ -1135,7 +1155,7 @@ myread (int desc, char *addr, int len)
    Uses malloc to get the space.  Returns the address of the copy.  */
 
 char *
-savestring (const char *ptr, int size)
+savestring (const char *ptr, size_t size)
 {
   register char *p = (char *) xmalloc (size + 1);
   memcpy (p, ptr, size);
@@ -1144,7 +1164,7 @@ savestring (const char *ptr, int size)
 }
 
 char *
-msavestring (void *md, const char *ptr, int size)
+msavestring (void *md, const char *ptr, size_t size)
 {
   register char *p = (char *) xmmalloc (md, size + 1);
   memcpy (p, ptr, size);
@@ -1152,15 +1172,6 @@ msavestring (void *md, const char *ptr, int size)
   return p;
 }
 
-/* The "const" is so it compiles under DGUX (which prototypes strsave
-   in <string.h>.  FIXME: This should be named "xstrsave", shouldn't it?
-   Doesn't real strsave return NULL if out of memory?  */
-char *
-strsave (const char *ptr)
-{
-  return savestring (ptr, strlen (ptr));
-}
-
 char *
 mstrsave (void *md, const char *ptr)
 {
@@ -1636,7 +1647,7 @@ prompt_for_continue (void)
          else
            async_request_quit (0);
        }
-      free (ignore);
+      xfree (ignore);
     }
   immediate_quit--;
 
@@ -1682,7 +1693,7 @@ wrap_here (char *indent)
 {
   /* This should have been allocated, but be paranoid anyway. */
   if (!wrap_buffer)
-    abort ();
+    internal_error (__FILE__, __LINE__, "failed internal consistency check");
 
   if (wrap_buffer[0])
     {
@@ -1856,6 +1867,15 @@ putchar_unfiltered (int c)
   return c;
 }
 
+/* Write character C to gdb_stdout using GDB's paging mechanism and return C.
+   May return nonlocally.  */
+
+int
+putchar_filtered (int c)
+{
+  return fputc_filtered (c, gdb_stdout);
+}
+
 int
 fputc_unfiltered (int c, struct ui_file *stream)
 {
@@ -1982,7 +2002,7 @@ vfprintf_maybe_filtered (struct ui_file *stream, const char *format,
   struct cleanup *old_cleanups;
 
   xvasprintf (&linebuffer, format, args);
-  old_cleanups = make_cleanup (free, linebuffer);
+  old_cleanups = make_cleanup (xfree, linebuffer);
   fputs_maybe_filtered (linebuffer, stream, filter);
   do_cleanups (old_cleanups);
 }
@@ -2001,7 +2021,7 @@ vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args)
   struct cleanup *old_cleanups;
 
   xvasprintf (&linebuffer, format, args);
-  old_cleanups = make_cleanup (free, linebuffer);
+  old_cleanups = make_cleanup (xfree, linebuffer);
   fputs_unfiltered (linebuffer, stream);
   do_cleanups (old_cleanups);
 }
@@ -2112,7 +2132,7 @@ n_spaces (int n)
   if (n > max_spaces)
     {
       if (spaces)
-       free (spaces);
+       xfree (spaces);
       spaces = (char *) xmalloc (n + 1);
       for (t = spaces + n; t != spaces;)
        *--t = ' ';
@@ -2170,7 +2190,7 @@ fprintf_symbol_filtered (struct ui_file *stream, char *name, enum language lang,
          fputs_filtered (demangled ? demangled : name, stream);
          if (demangled != NULL)
            {
-             free (demangled);
+             xfree (demangled);
            }
        }
     }
@@ -2783,7 +2803,7 @@ decimal2str (char *paddr_str, char *sign, ULONGEST addr)
               sign, temp[2], temp[1], temp[0]);
       break;
     default:
-      abort ();
+      internal_error (__FILE__, __LINE__, "failed internal consistency check");
     }
 }
 
@@ -2869,7 +2889,8 @@ CORE_ADDR
 host_pointer_to_address (void *ptr)
 {
   if (sizeof (ptr) != TYPE_LENGTH (builtin_type_ptr))
-    internal_error ("core_addr_to_void_ptr: bad cast");
+    internal_error (__FILE__, __LINE__,
+                   "core_addr_to_void_ptr: bad cast");
   return POINTER_TO_ADDRESS (builtin_type_ptr, &ptr);
 }
 
@@ -2878,7 +2899,8 @@ address_to_host_pointer (CORE_ADDR addr)
 {
   void *ptr;
   if (sizeof (ptr) != TYPE_LENGTH (builtin_type_ptr))
-    internal_error ("core_addr_to_void_ptr: bad cast");
+    internal_error (__FILE__, __LINE__,
+                   "core_addr_to_void_ptr: bad cast");
   ADDRESS_TO_POINTER (builtin_type_ptr, &ptr, addr);
   return ptr;
 }
This page took 0.028428 seconds and 4 git commands to generate.