Remove fatal function and prototype
[external/binutils.git] / gdb / gdbserver / utils.c
1 /* General utility routines for the remote server for GDB.
2    Copyright (C) 1986-2014 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include "server.h"
20
21 #ifdef IN_PROCESS_AGENT
22 #  define PREFIX "ipa: "
23 #  define TOOLNAME "GDBserver in-process agent"
24 #else
25 #  define PREFIX "gdbserver: "
26 #  define TOOLNAME "GDBserver"
27 #endif
28
29 /* Generally useful subroutines used throughout the program.  */
30
31 void
32 malloc_failure (long size)
33 {
34   fprintf (stderr,
35            PREFIX "ran out of memory while trying to allocate %lu bytes\n",
36            (unsigned long) size);
37   exit (1);
38 }
39
40 /* Copy a string into a memory buffer.
41    If malloc fails, this will print a message to stderr and exit.  */
42
43 char *
44 xstrdup (const char *s)
45 {
46   char *ret = strdup (s);
47   if (ret == NULL)
48     malloc_failure (strlen (s) + 1);
49   return ret;
50 }
51
52 /* Print the system error message for errno, and also mention STRING
53    as the file name for which the error was encountered.
54    Then return to command level.  */
55
56 void
57 perror_with_name (const char *string)
58 {
59   const char *err;
60   char *combined;
61
62   err = strerror (errno);
63   if (err == NULL)
64     err = "unknown error";
65
66   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
67   strcpy (combined, string);
68   strcat (combined, ": ");
69   strcat (combined, err);
70
71   error ("%s.", combined);
72 }
73
74 /* Print an error message and return to top level.  */
75
76 void
77 verror (const char *string, va_list args)
78 {
79 #ifndef IN_PROCESS_AGENT
80   extern jmp_buf toplevel;
81 #endif
82
83   fflush (stdout);
84   vfprintf (stderr, string, args);
85   fprintf (stderr, "\n");
86 #ifndef IN_PROCESS_AGENT
87   longjmp (toplevel, 1);
88 #else
89   exit (1);
90 #endif
91 }
92
93 void
94 vwarning (const char *string, va_list args)
95 {
96   fprintf (stderr, PREFIX);
97   vfprintf (stderr, string, args);
98   fprintf (stderr, "\n");
99 }
100
101 /* Report a problem internal to GDBserver, and exit.  */
102
103 void
104 internal_verror (const char *file, int line, const char *fmt, va_list args)
105 {
106   fprintf (stderr,  "\
107 %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line);
108   vfprintf (stderr, fmt, args);
109   fprintf (stderr, "\n");
110   exit (1);
111 }
112
113 /* Convert a CORE_ADDR into a HEX string, like %lx.
114    The result is stored in a circular static buffer, NUMCELLS deep.  */
115
116 char *
117 paddress (CORE_ADDR addr)
118 {
119   return phex_nz (addr, sizeof (CORE_ADDR));
120 }
121
122 /* Convert a file descriptor into a printable string.  */
123
124 char *
125 pfildes (gdb_fildes_t fd)
126 {
127 #if USE_WIN32API
128   return phex_nz (fd, sizeof (gdb_fildes_t));
129 #else
130   return plongest (fd);
131 #endif
132 }