Move stdlib.h to common-defs.h
[deliverable/binutils-gdb.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 #include <string.h>
21 #if HAVE_ERRNO_H
22 #include <errno.h>
23 #endif
24
25 #ifdef IN_PROCESS_AGENT
26 # define PREFIX "ipa: "
27 # define TOOLNAME "GDBserver in-process agent"
28 #else
29 # define PREFIX "gdbserver: "
30 # define TOOLNAME "GDBserver"
31 #endif
32
33 /* Generally useful subroutines used throughout the program. */
34
35 void
36 malloc_failure (long size)
37 {
38 fprintf (stderr,
39 PREFIX "ran out of memory while trying to allocate %lu bytes\n",
40 (unsigned long) size);
41 exit (1);
42 }
43
44 /* Copy a string into a memory buffer.
45 If malloc fails, this will print a message to stderr and exit. */
46
47 char *
48 xstrdup (const char *s)
49 {
50 char *ret = strdup (s);
51 if (ret == NULL)
52 malloc_failure (strlen (s) + 1);
53 return ret;
54 }
55
56 /* Print the system error message for errno, and also mention STRING
57 as the file name for which the error was encountered.
58 Then return to command level. */
59
60 void
61 perror_with_name (const char *string)
62 {
63 const char *err;
64 char *combined;
65
66 err = strerror (errno);
67 if (err == NULL)
68 err = "unknown error";
69
70 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
71 strcpy (combined, string);
72 strcat (combined, ": ");
73 strcat (combined, err);
74
75 error ("%s.", combined);
76 }
77
78 /* Print an error message and return to command level.
79 STRING is the error message, used as a fprintf string,
80 and ARG is passed as an argument to it. */
81
82 void
83 error (const char *string,...)
84 {
85 #ifndef IN_PROCESS_AGENT
86 extern jmp_buf toplevel;
87 #endif
88 va_list args;
89 va_start (args, string);
90 fflush (stdout);
91 vfprintf (stderr, string, args);
92 fprintf (stderr, "\n");
93 #ifndef IN_PROCESS_AGENT
94 longjmp (toplevel, 1);
95 #else
96 exit (1);
97 #endif
98 }
99
100 /* Print an error message and exit reporting failure.
101 This is for a error that we cannot continue from.
102 STRING and ARG are passed to fprintf. */
103
104 /* VARARGS */
105 void
106 fatal (const char *string,...)
107 {
108 va_list args;
109 va_start (args, string);
110 fprintf (stderr, PREFIX);
111 vfprintf (stderr, string, args);
112 fprintf (stderr, "\n");
113 va_end (args);
114 exit (1);
115 }
116
117 /* VARARGS */
118 void
119 warning (const char *string,...)
120 {
121 va_list args;
122 va_start (args, string);
123 fprintf (stderr, PREFIX);
124 vfprintf (stderr, string, args);
125 fprintf (stderr, "\n");
126 va_end (args);
127 }
128
129 /* Report a problem internal to GDBserver, and exit. */
130
131 void
132 internal_error (const char *file, int line, const char *fmt, ...)
133 {
134 va_list args;
135 va_start (args, fmt);
136
137 fprintf (stderr, "\
138 %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line);
139 vfprintf (stderr, fmt, args);
140 fprintf (stderr, "\n");
141 va_end (args);
142 exit (1);
143 }
144
145 /* Convert a CORE_ADDR into a HEX string, like %lx.
146 The result is stored in a circular static buffer, NUMCELLS deep. */
147
148 char *
149 paddress (CORE_ADDR addr)
150 {
151 return phex_nz (addr, sizeof (CORE_ADDR));
152 }
153
154 /* Convert a file descriptor into a printable string. */
155
156 char *
157 pfildes (gdb_fildes_t fd)
158 {
159 #if USE_WIN32API
160 return phex_nz (fd, sizeof (gdb_fildes_t));
161 #else
162 return plongest (fd);
163 #endif
164 }
This page took 0.033245 seconds and 5 git commands to generate.