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