aboutsummaryrefslogtreecommitdiff
path: root/gdb/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'gdb/utils.c')
-rw-r--r--gdb/utils.c248
1 files changed, 188 insertions, 60 deletions
diff --git a/gdb/utils.c b/gdb/utils.c
index cc1573d..24fd9ea 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -35,8 +35,10 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* Prototypes for local functions */
#if !defined (NO_MALLOC_CHECK)
+
static void
malloc_botch PARAMS ((void));
+
#endif /* NO_MALLOC_CHECK */
static void
@@ -91,7 +93,7 @@ int sevenbit_strings = 0;
/* String to be printed before error messages, if any. */
char *error_pre_print;
-char *warning_pre_print;
+char *warning_pre_print = "\nwarning: ";
/* Add a new cleanup to the cleanup_chain,
and return the previous chain pointer
@@ -274,7 +276,7 @@ fatal (va_alist)
va_start (args);
string = va_arg (args, char *);
- fprintf (stderr, "gdb: ");
+ fprintf (stderr, "\ngdb: ");
vfprintf (stderr, string, args);
fprintf (stderr, "\n");
va_end (args);
@@ -296,7 +298,7 @@ fatal_dump_core (va_alist)
string = va_arg (args, char *);
/* "internal error" is always correct, since GDB should never dump
core, no matter what the input. */
- fprintf (stderr, "gdb internal error: ");
+ fprintf (stderr, "\ngdb internal error: ");
vfprintf (stderr, string, args);
fprintf (stderr, "\n");
va_end (args);
@@ -307,62 +309,6 @@ fatal_dump_core (va_alist)
exit (1);
}
-
-/* Memory management stuff (malloc friends). */
-
-#if defined (NO_MALLOC_CHECK)
-void
-init_malloc ()
-{}
-#else /* Have mcheck(). */
-static void
-malloc_botch ()
-{
- fatal_dump_core ("Memory corruption");
-}
-
-void
-init_malloc ()
-{
- extern PTR (*__morecore) PARAMS ((long));
-
- mcheck (malloc_botch);
- mtrace ();
-}
-#endif /* Have mcheck(). */
-
-/* Like malloc but get error if no storage available. */
-
-PTR
-xmalloc (size)
- long size;
-{
- register char *val;
-
- /* Protect against gdb wanting to allocate zero bytes. */
- if (size == 0)
- return NULL;
-
- val = (char *) malloc (size);
- if (!val)
- fatal ("virtual memory exhausted.", 0);
- return val;
-}
-
-/* Like realloc but get error if no storage available. */
-
-PTR
-xrealloc (ptr, size)
- char *ptr;
- long size;
-{
- register char *val =
- ptr ? (char *) realloc (ptr, size) : (char*) malloc (size);
- if (!val)
- fatal ("virtual memory exhausted.", 0);
- return val;
-}
-
/* Print the system error message for errno, and also mention STRING
as the file name for which the error was encountered.
Then return to command level. */
@@ -456,6 +402,168 @@ request_quit (signo)
if (immediate_quit)
quit ();
}
+
+
+/* Memory management stuff (malloc friends). */
+
+#if defined (NO_MMALLOC)
+
+PTR
+mmalloc (md, size)
+ PTR md;
+ long size;
+{
+ return (malloc (size));
+}
+
+PTR
+mrealloc (md, ptr, size)
+ PTR md;
+ PTR ptr;
+ long size;
+{
+ return (realloc (ptr, size));
+}
+
+void
+mfree (md, ptr)
+ PTR md;
+ PTR ptr;
+{
+ free (ptr);
+}
+
+#endif /* NO_MMALLOC */
+
+#if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
+
+void
+init_malloc (md)
+ PTR md;
+{
+}
+
+#else /* have mmalloc and want corruption checking */
+
+static void
+malloc_botch ()
+{
+ fatal_dump_core ("Memory corruption");
+}
+
+/* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
+ by MD, to detect memory corruption. Note that MD may be NULL to specify
+ the default heap that grows via sbrk.
+
+ Note that for freshly created regions, we must call mmcheck prior to any
+ mallocs in the region. Otherwise, any region which was allocated prior to
+ installing the checking hooks, which is later reallocated or freed, will
+ fail the checks! The mmcheck function only allows initial hooks to be
+ installed before the first mmalloc. However, anytime after we have called
+ mmcheck the first time to install the checking hooks, we can call it again
+ to update the function pointer to the memory corruption handler.
+
+ Returns zero on failure, non-zero on success. */
+
+void
+init_malloc (md)
+ PTR md;
+{
+ if (!mmcheck (md, malloc_botch))
+ {
+ warning ("internal error: failed to install memory consistency checks");
+ }
+
+ (void) mmtrace ();
+}
+
+#endif /* Have mmalloc and want corruption checking */
+
+/* Called when a memory allocation fails, with the number of bytes of
+ memory requested in SIZE. */
+
+NORETURN void
+nomem (size)
+ long size;
+{
+ if (size > 0)
+ {
+ fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
+ }
+ else
+ {
+ fatal ("virtual memory exhausted.");
+ }
+}
+
+/* Like mmalloc but get error if no storage available, and protect against
+ the caller wanting to allocate zero bytes. Whether to return NULL for
+ a zero byte request, or translate the request into a request for one
+ byte of zero'd storage, is a religious issue. */
+
+PTR
+xmmalloc (md, size)
+ PTR md;
+ long size;
+{
+ register PTR val;
+
+ if (size == 0)
+ {
+ val = NULL;
+ }
+ else if ((val = mmalloc (md, size)) == NULL)
+ {
+ nomem (size);
+ }
+ return (val);
+}
+
+/* Like mrealloc but get error if no storage available. */
+
+PTR
+xmrealloc (md, ptr, size)
+ PTR md;
+ PTR ptr;
+ long size;
+{
+ register PTR val;
+
+ if (ptr != NULL)
+ {
+ val = mrealloc (md, ptr, size);
+ }
+ else
+ {
+ val = mmalloc (md, size);
+ }
+ if (val == NULL)
+ {
+ nomem (size);
+ }
+ return (val);
+}
+
+/* Like malloc but get error if no storage available, and protect against
+ the caller wanting to allocate zero bytes. */
+
+PTR
+xmalloc (size)
+ long size;
+{
+ return (xmmalloc ((void *) NULL, size));
+}
+
+/* Like mrealloc but get error if no storage available. */
+
+PTR
+xrealloc (ptr, size)
+ PTR ptr;
+ long size;
+{
+ return (xmrealloc ((void *) NULL, ptr, size));
+}
+
/* My replacement for the read system call.
Used like `read' but keeps going if `read' returns too soon. */
@@ -497,6 +605,18 @@ savestring (ptr, size)
return p;
}
+char *
+msavestring (md, ptr, size)
+ void *md;
+ const char *ptr;
+ int size;
+{
+ register char *p = (char *) xmmalloc (md, size + 1);
+ bcopy (ptr, p, size);
+ p[size] = 0;
+ 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? */
@@ -507,6 +627,14 @@ strsave (ptr)
return savestring (ptr, strlen (ptr));
}
+char *
+mstrsave (md, ptr)
+ void *md;
+ const char *ptr;
+{
+ return (msavestring (md, ptr, strlen (ptr)));
+}
+
void
print_spaces (n, file)
register int n;
@@ -1101,7 +1229,7 @@ n_spaces (n)
{
if (spaces)
free (spaces);
- spaces = (char *) malloc (n+1);
+ spaces = (char *) xmalloc (n+1);
for (t = spaces+n; t != spaces;)
*--t = ' ';
spaces[n] = '\0';