Automatic date update in version.in
[binutils-gdb.git] / gdbserver / utils.cc
blob4f6516c9cf292e79436e826b52f86449eea65ab1
1 /* General utility routines for the remote server for GDB.
2 Copyright (C) 1986-2022 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
19 #include "server.h"
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
29 /* Generally useful subroutines used throughout the program. */
31 void
32 malloc_failure (long size)
34 fprintf (stderr,
35 PREFIX "ran out of memory while trying to allocate %lu bytes\n",
36 (unsigned long) size);
37 exit (1);
40 /* Print the system error message for errno, and also mention STRING
41 as the file name for which the error was encountered.
42 Then return to command level. */
44 void
45 perror_with_name (const char *string)
47 const char *err;
48 char *combined;
50 err = safe_strerror (errno);
51 if (err == NULL)
52 err = "unknown error";
54 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
55 strcpy (combined, string);
56 strcat (combined, ": ");
57 strcat (combined, err);
59 error ("%s.", combined);
62 /* Print an error message and return to top level. */
64 void
65 verror (const char *string, va_list args)
67 #ifdef IN_PROCESS_AGENT
68 fflush (stdout);
69 vfprintf (stderr, string, args);
70 fprintf (stderr, "\n");
71 exit (1);
72 #else
73 throw_verror (GENERIC_ERROR, string, args);
74 #endif
77 void
78 vwarning (const char *string, va_list args)
80 fprintf (stderr, PREFIX);
81 vfprintf (stderr, string, args);
82 fprintf (stderr, "\n");
85 /* Report a problem internal to GDBserver, and exit. */
87 void
88 internal_verror (const char *file, int line, const char *fmt, va_list args)
90 fprintf (stderr, "\
91 %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line);
92 vfprintf (stderr, fmt, args);
93 fprintf (stderr, "\n");
94 exit (1);
97 /* Report a problem internal to GDBserver. */
99 void
100 internal_vwarning (const char *file, int line, const char *fmt, va_list args)
102 fprintf (stderr, "\
103 %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line);
104 vfprintf (stderr, fmt, args);
105 fprintf (stderr, "\n");
108 /* Convert a CORE_ADDR into a HEX string, like %lx.
109 The result is stored in a circular static buffer, NUMCELLS deep. */
111 char *
112 paddress (CORE_ADDR addr)
114 return phex_nz (addr, sizeof (CORE_ADDR));