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