Automatic date update in version.in
[binutils-gdb.git] / gdbserver / utils.cc
blob302300770e84cf4c8924bdd1281eb6db16b3b020
1 /* General utility routines for the remote server for GDB.
2 Copyright (C) 1986-2024 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/>. */
20 #ifdef IN_PROCESS_AGENT
21 # define PREFIX "ipa: "
22 # define TOOLNAME "GDBserver in-process agent"
23 #else
24 # define PREFIX "gdbserver: "
25 # define TOOLNAME "GDBserver"
26 #endif
28 /* Generally useful subroutines used throughout the program. */
30 /* If in release mode, just exit. This avoids potentially littering
31 the filesystem of small embedded targets with core files. If in
32 development mode however, abort, producing core files to help with
33 debugging GDBserver. */
34 static void ATTRIBUTE_NORETURN
35 abort_or_exit ()
37 #ifdef DEVELOPMENT
38 abort ();
39 #else
40 exit (1);
41 #endif
44 void
45 malloc_failure (long size)
47 fprintf (stderr,
48 PREFIX "ran out of memory while trying to allocate %lu bytes\n",
49 (unsigned long) size);
50 abort_or_exit ();
53 /* Print an error message and return to top level. */
55 void
56 verror (const char *string, va_list args)
58 #ifdef IN_PROCESS_AGENT
59 fflush (stdout);
60 vfprintf (stderr, string, args);
61 fprintf (stderr, "\n");
62 exit (1);
63 #else
64 throw_verror (GENERIC_ERROR, string, args);
65 #endif
68 void
69 vwarning (const char *string, va_list args)
71 fprintf (stderr, PREFIX);
72 vfprintf (stderr, string, args);
73 fprintf (stderr, "\n");
76 /* Report a problem internal to GDBserver, and abort/exit. */
78 void
79 internal_verror (const char *file, int line, const char *fmt, va_list args)
81 fprintf (stderr, "\
82 %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line);
83 vfprintf (stderr, fmt, args);
84 fprintf (stderr, "\n");
85 abort_or_exit ();
88 /* Report a problem internal to GDBserver. */
90 void
91 internal_vwarning (const char *file, int line, const char *fmt, va_list args)
93 fprintf (stderr, "\
94 %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line);
95 vfprintf (stderr, fmt, args);
96 fprintf (stderr, "\n");
99 /* Convert a CORE_ADDR into a HEX string, like %lx.
100 The result is stored in a circular static buffer, NUMCELLS deep. */
102 const char *
103 paddress (CORE_ADDR addr)
105 return phex_nz (addr, sizeof (CORE_ADDR));