* utils.c (xmalloc,xcalloc,xstrdup): New fns.
[external/binutils.git] / gdb / gdbserver / utils.c
1 /* General utility routines for the remote server for GDB.
2    Copyright (C) 1986, 1989, 1993, 1995, 1996, 1997, 1999, 2000, 2002, 2003,
3    2007, 2008 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "server.h"
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #if HAVE_ERRNO_H
25 #include <errno.h>
26 #endif
27 #if HAVE_MALLOC_H
28 #include <malloc.h>
29 #endif
30
31 /* Generally useful subroutines used throughout the program.  */
32
33 static void malloc_failure (size_t size) ATTR_NORETURN;
34
35 static void
36 malloc_failure (size_t size)
37 {
38   fprintf (stderr, "gdbserver: ran out of memory while trying to allocate %lu bytes\n",
39            (unsigned long) size);
40   exit (1);
41 }
42
43 /* Allocate memory without fail.
44    If malloc fails, this will print a message to stderr and exit.  */
45
46 void *
47 xmalloc (size_t size)
48 {
49   void *newmem;
50
51   if (size == 0)
52     size = 1;
53   newmem = malloc (size);
54   if (!newmem)
55     malloc_failure (size);
56
57   return newmem;
58 }
59
60 /* Allocate memory without fail and set it to zero.
61    If malloc fails, this will print a message to stderr and exit.  */
62
63 void *
64 xcalloc (size_t nelem, size_t elsize)
65 {
66   void *newmem;
67
68   if (nelem == 0 || elsize == 0)
69     nelem = elsize = 1;
70
71   newmem = calloc (nelem, elsize);
72   if (!newmem)
73     malloc_failure (nelem * elsize);
74
75   return newmem;
76 }
77
78 /* Copy a string into a memory buffer.
79    If malloc fails, this will print a message to stderr and exit.  */
80
81 char *
82 xstrdup (const char *s)
83 {
84   char *ret = strdup (s);
85   if (ret == NULL)
86     malloc_failure (strlen (s) + 1);
87   return ret;
88 }
89
90 /* Print the system error message for errno, and also mention STRING
91    as the file name for which the error was encountered.
92    Then return to command level.  */
93
94 void
95 perror_with_name (char *string)
96 {
97   const char *err;
98   char *combined;
99
100   err = strerror (errno);
101   if (err == NULL)
102     err = "unknown error";
103
104   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
105   strcpy (combined, string);
106   strcat (combined, ": ");
107   strcat (combined, err);
108
109   error ("%s.", combined);
110 }
111
112 /* Print an error message and return to command level.
113    STRING is the error message, used as a fprintf string,
114    and ARG is passed as an argument to it.  */
115
116 void
117 error (const char *string,...)
118 {
119   extern jmp_buf toplevel;
120   va_list args;
121   va_start (args, string);
122   fflush (stdout);
123   vfprintf (stderr, string, args);
124   fprintf (stderr, "\n");
125   longjmp (toplevel, 1);
126 }
127
128 /* Print an error message and exit reporting failure.
129    This is for a error that we cannot continue from.
130    STRING and ARG are passed to fprintf.  */
131
132 /* VARARGS */
133 void
134 fatal (const char *string,...)
135 {
136   va_list args;
137   va_start (args, string);
138   fprintf (stderr, "gdbserver: ");
139   vfprintf (stderr, string, args);
140   fprintf (stderr, "\n");
141   va_end (args);
142   exit (1);
143 }
144
145 /* VARARGS */
146 void
147 warning (const char *string,...)
148 {
149   va_list args;
150   va_start (args, string);
151   fprintf (stderr, "gdbserver: ");
152   vfprintf (stderr, string, args);
153   fprintf (stderr, "\n");
154   va_end (args);
155 }