xref: /netbsd-src/external/gpl3/gdb/dist/gdbserver/utils.cc (revision 2be465b09aca4bf6e67814eb0e0f409087138d90)
1 /* General utility routines for the remote server for GDB.
2    Copyright (C) 1986-2024 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 
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
27 
28 /* Generally useful subroutines used throughout the program.  */
29 
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 ()
36 {
37 #ifdef DEVELOPMENT
38   abort ();
39 #else
40   exit (1);
41 #endif
42 }
43 
44 void
45 malloc_failure (long size)
46 {
47   fprintf (stderr,
48 	   PREFIX "ran out of memory while trying to allocate %lu bytes\n",
49 	   (unsigned long) size);
50   abort_or_exit ();
51 }
52 
53 /* Print an error message and return to top level.  */
54 
55 void
56 verror (const char *string, va_list args)
57 {
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
66 }
67 
68 void
69 vwarning (const char *string, va_list args)
70 {
71   fprintf (stderr, PREFIX);
72   vfprintf (stderr, string, args);
73   fprintf (stderr, "\n");
74 }
75 
76 /* Report a problem internal to GDBserver, and abort/exit.  */
77 
78 void
79 internal_verror (const char *file, int line, const char *fmt, va_list args)
80 {
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 ();
86 }
87 
88 /* Report a problem internal to GDBserver.  */
89 
90 void
91 internal_vwarning (const char *file, int line, const char *fmt, va_list args)
92 {
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");
97 }
98 
99 /* Convert a CORE_ADDR into a HEX string, like %lx.
100    The result is stored in a circular static buffer, NUMCELLS deep.  */
101 
102 const char *
103 paddress (CORE_ADDR addr)
104 {
105   return phex_nz (addr, sizeof (CORE_ADDR));
106 }
107