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, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
31 /* Generally useful subroutines used throughout the program. */
33 static void malloc_failure (size_t size
) ATTR_NORETURN
;
36 malloc_failure (size_t size
)
38 fprintf (stderr
, "gdbserver: ran out of memory while trying to allocate %lu bytes\n",
39 (unsigned long) size
);
43 /* Allocate memory without fail.
44 If malloc fails, this will print a message to stderr and exit. */
53 newmem
= malloc (size
);
55 malloc_failure (size
);
60 /* Allocate memory without fail and set it to zero.
61 If malloc fails, this will print a message to stderr and exit. */
64 xcalloc (size_t nelem
, size_t elsize
)
68 if (nelem
== 0 || elsize
== 0)
71 newmem
= calloc (nelem
, elsize
);
73 malloc_failure (nelem
* elsize
);
78 /* Copy a string into a memory buffer.
79 If malloc fails, this will print a message to stderr and exit. */
82 xstrdup (const char *s
)
84 char *ret
= strdup (s
);
86 malloc_failure (strlen (s
) + 1);
90 /* Free a standard argv vector. */
93 freeargv (char **vector
)
99 for (scan
= vector
; *scan
!= NULL
; scan
++)
107 /* Print the system error message for errno, and also mention STRING
108 as the file name for which the error was encountered.
109 Then return to command level. */
112 perror_with_name (const char *string
)
117 err
= strerror (errno
);
119 err
= "unknown error";
121 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
122 strcpy (combined
, string
);
123 strcat (combined
, ": ");
124 strcat (combined
, err
);
126 error ("%s.", combined
);
129 /* Print an error message and return to command level.
130 STRING is the error message, used as a fprintf string,
131 and ARG is passed as an argument to it. */
134 error (const char *string
,...)
136 extern jmp_buf toplevel
;
138 va_start (args
, string
);
140 vfprintf (stderr
, string
, args
);
141 fprintf (stderr
, "\n");
142 longjmp (toplevel
, 1);
145 /* Print an error message and exit reporting failure.
146 This is for a error that we cannot continue from.
147 STRING and ARG are passed to fprintf. */
151 fatal (const char *string
,...)
154 va_start (args
, string
);
155 fprintf (stderr
, "gdbserver: ");
156 vfprintf (stderr
, string
, args
);
157 fprintf (stderr
, "\n");
164 warning (const char *string
,...)
167 va_start (args
, string
);
168 fprintf (stderr
, "gdbserver: ");
169 vfprintf (stderr
, string
, args
);
170 fprintf (stderr
, "\n");
174 /* Temporary storage using circular buffer. */
178 /* Return the next entry in the circular buffer. */
183 static char buf
[NUMCELLS
][CELLSIZE
];
185 if (++cell
>= NUMCELLS
)
190 /* Stdarg wrapper around vsnprintf.
191 SIZE is the size of the buffer pointed to by STR. */
194 xsnprintf (char *str
, size_t size
, const char *format
, ...)
199 va_start (args
, format
);
200 ret
= vsnprintf (str
, size
, format
, args
);
206 /* Convert a CORE_ADDR into a HEX string, like %lx.
207 The result is stored in a circular static buffer, NUMCELLS deep. */
210 paddress (CORE_ADDR addr
)
212 char *str
= get_cell ();
213 xsnprintf (str
, CELLSIZE
, "%lx", (long) addr
);