Commit | Line | Data |
---|---|---|
c906108c | 1 | /* General utility routines for the remote server for GDB. |
ecd75fc8 | 2 | Copyright (C) 1986-2014 Free Software Foundation, Inc. |
c906108c | 3 | |
c5aa993b | 4 | This file is part of GDB. |
c906108c | 5 | |
c5aa993b JM |
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 | |
a9762ec7 | 8 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 9 | (at your option) any later version. |
c906108c | 10 | |
c5aa993b JM |
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. | |
c906108c | 15 | |
c5aa993b | 16 | You should have received a copy of the GNU General Public License |
a9762ec7 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
18 | |
19 | #include "server.h" | |
c906108c | 20 | |
fa593d66 PA |
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 | ||
c906108c SS |
29 | /* Generally useful subroutines used throughout the program. */ |
30 | ||
d26e3629 KY |
31 | void |
32 | malloc_failure (long size) | |
bca929d3 | 33 | { |
493e2a69 MS |
34 | fprintf (stderr, |
35 | PREFIX "ran out of memory while trying to allocate %lu bytes\n", | |
bca929d3 DE |
36 | (unsigned long) size); |
37 | exit (1); | |
38 | } | |
39 | ||
bca929d3 DE |
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 | ||
c906108c SS |
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 | |
54363045 | 57 | perror_with_name (const char *string) |
c906108c | 58 | { |
5c44784c | 59 | const char *err; |
c906108c SS |
60 | char *combined; |
61 | ||
43d5792c DJ |
62 | err = strerror (errno); |
63 | if (err == NULL) | |
c906108c SS |
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 | ||
ef87c8bb | 74 | /* Print an error message and return to top level. */ |
c906108c | 75 | |
0729219d | 76 | void |
ef87c8bb | 77 | verror (const char *string, va_list args) |
c906108c | 78 | { |
fa593d66 | 79 | #ifndef IN_PROCESS_AGENT |
c906108c | 80 | extern jmp_buf toplevel; |
fa593d66 | 81 | #endif |
ef87c8bb | 82 | |
c906108c | 83 | fflush (stdout); |
c906108c | 84 | vfprintf (stderr, string, args); |
c906108c | 85 | fprintf (stderr, "\n"); |
fa593d66 | 86 | #ifndef IN_PROCESS_AGENT |
c5aa993b | 87 | longjmp (toplevel, 1); |
fa593d66 PA |
88 | #else |
89 | exit (1); | |
90 | #endif | |
c906108c SS |
91 | } |
92 | ||
93 | /* Print an error message and exit reporting failure. | |
94 | This is for a error that we cannot continue from. | |
95 | STRING and ARG are passed to fprintf. */ | |
96 | ||
97 | /* VARARGS */ | |
0729219d | 98 | void |
0a30fbc4 | 99 | fatal (const char *string,...) |
c906108c SS |
100 | { |
101 | va_list args; | |
c906108c | 102 | va_start (args, string); |
fa593d66 | 103 | fprintf (stderr, PREFIX); |
c906108c SS |
104 | vfprintf (stderr, string, args); |
105 | fprintf (stderr, "\n"); | |
106 | va_end (args); | |
107 | exit (1); | |
108 | } | |
0a30fbc4 | 109 | |
ef87c8bb GB |
110 | /* Print a warning message. */ |
111 | ||
0a30fbc4 | 112 | void |
ef87c8bb | 113 | vwarning (const char *string, va_list args) |
0a30fbc4 | 114 | { |
fa593d66 | 115 | fprintf (stderr, PREFIX); |
0a30fbc4 DJ |
116 | vfprintf (stderr, string, args); |
117 | fprintf (stderr, "\n"); | |
0a30fbc4 | 118 | } |
aa5ca48f | 119 | |
e92d13d5 PA |
120 | /* Report a problem internal to GDBserver, and exit. */ |
121 | ||
122 | void | |
ef87c8bb | 123 | internal_verror (const char *file, int line, const char *fmt, va_list args) |
e92d13d5 | 124 | { |
e92d13d5 | 125 | fprintf (stderr, "\ |
fa593d66 | 126 | %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line); |
e92d13d5 PA |
127 | vfprintf (stderr, fmt, args); |
128 | fprintf (stderr, "\n"); | |
e92d13d5 PA |
129 | exit (1); |
130 | } | |
131 | ||
5c3216e2 OS |
132 | /* Convert a CORE_ADDR into a HEX string, like %lx. |
133 | The result is stored in a circular static buffer, NUMCELLS deep. */ | |
134 | ||
135 | char * | |
136 | paddress (CORE_ADDR addr) | |
137 | { | |
138 | return phex_nz (addr, sizeof (CORE_ADDR)); | |
139 | } | |
ec48365d PA |
140 | |
141 | /* Convert a file descriptor into a printable string. */ | |
142 | ||
143 | char * | |
144 | pfildes (gdb_fildes_t fd) | |
145 | { | |
146 | #if USE_WIN32API | |
147 | return phex_nz (fd, sizeof (gdb_fildes_t)); | |
148 | #else | |
149 | return plongest (fd); | |
150 | #endif | |
151 | } |