xref: /netbsd-src/external/gpl3/gdb/dist/gdbserver/utils.cc (revision 2be465b09aca4bf6e67814eb0e0f409087138d90)
18dffb485Schristos /* General utility routines for the remote server for GDB.
2*2be465b0Schristos    Copyright (C) 1986-2024 Free Software Foundation, Inc.
38dffb485Schristos 
48dffb485Schristos    This file is part of GDB.
58dffb485Schristos 
68dffb485Schristos    This program is free software; you can redistribute it and/or modify
78dffb485Schristos    it under the terms of the GNU General Public License as published by
88dffb485Schristos    the Free Software Foundation; either version 3 of the License, or
98dffb485Schristos    (at your option) any later version.
108dffb485Schristos 
118dffb485Schristos    This program is distributed in the hope that it will be useful,
128dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
138dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
148dffb485Schristos    GNU General Public License for more details.
158dffb485Schristos 
168dffb485Schristos    You should have received a copy of the GNU General Public License
178dffb485Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
188dffb485Schristos 
198dffb485Schristos 
208dffb485Schristos #ifdef IN_PROCESS_AGENT
218dffb485Schristos #  define PREFIX "ipa: "
228dffb485Schristos #  define TOOLNAME "GDBserver in-process agent"
238dffb485Schristos #else
248dffb485Schristos #  define PREFIX "gdbserver: "
258dffb485Schristos #  define TOOLNAME "GDBserver"
268dffb485Schristos #endif
278dffb485Schristos 
288dffb485Schristos /* Generally useful subroutines used throughout the program.  */
298dffb485Schristos 
304b169a6bSchristos /* If in release mode, just exit.  This avoids potentially littering
314b169a6bSchristos    the filesystem of small embedded targets with core files.  If in
324b169a6bSchristos    development mode however, abort, producing core files to help with
334b169a6bSchristos    debugging GDBserver.  */
344b169a6bSchristos static void ATTRIBUTE_NORETURN
354b169a6bSchristos abort_or_exit ()
364b169a6bSchristos {
374b169a6bSchristos #ifdef DEVELOPMENT
384b169a6bSchristos   abort ();
394b169a6bSchristos #else
404b169a6bSchristos   exit (1);
414b169a6bSchristos #endif
424b169a6bSchristos }
434b169a6bSchristos 
448dffb485Schristos void
458dffb485Schristos malloc_failure (long size)
468dffb485Schristos {
478dffb485Schristos   fprintf (stderr,
488dffb485Schristos 	   PREFIX "ran out of memory while trying to allocate %lu bytes\n",
498dffb485Schristos 	   (unsigned long) size);
504b169a6bSchristos   abort_or_exit ();
518dffb485Schristos }
528dffb485Schristos 
538dffb485Schristos /* Print an error message and return to top level.  */
548dffb485Schristos 
558dffb485Schristos void
568dffb485Schristos verror (const char *string, va_list args)
578dffb485Schristos {
588dffb485Schristos #ifdef IN_PROCESS_AGENT
598dffb485Schristos   fflush (stdout);
608dffb485Schristos   vfprintf (stderr, string, args);
618dffb485Schristos   fprintf (stderr, "\n");
628dffb485Schristos   exit (1);
638dffb485Schristos #else
648dffb485Schristos   throw_verror (GENERIC_ERROR, string, args);
658dffb485Schristos #endif
668dffb485Schristos }
678dffb485Schristos 
688dffb485Schristos void
698dffb485Schristos vwarning (const char *string, va_list args)
708dffb485Schristos {
718dffb485Schristos   fprintf (stderr, PREFIX);
728dffb485Schristos   vfprintf (stderr, string, args);
738dffb485Schristos   fprintf (stderr, "\n");
748dffb485Schristos }
758dffb485Schristos 
764b169a6bSchristos /* Report a problem internal to GDBserver, and abort/exit.  */
778dffb485Schristos 
788dffb485Schristos void
798dffb485Schristos internal_verror (const char *file, int line, const char *fmt, va_list args)
808dffb485Schristos {
818dffb485Schristos   fprintf (stderr,  "\
828dffb485Schristos %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line);
838dffb485Schristos   vfprintf (stderr, fmt, args);
848dffb485Schristos   fprintf (stderr, "\n");
854b169a6bSchristos   abort_or_exit ();
868dffb485Schristos }
878dffb485Schristos 
888dffb485Schristos /* Report a problem internal to GDBserver.  */
898dffb485Schristos 
908dffb485Schristos void
918dffb485Schristos internal_vwarning (const char *file, int line, const char *fmt, va_list args)
928dffb485Schristos {
938dffb485Schristos   fprintf (stderr,  "\
948dffb485Schristos %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line);
958dffb485Schristos   vfprintf (stderr, fmt, args);
968dffb485Schristos   fprintf (stderr, "\n");
978dffb485Schristos }
988dffb485Schristos 
998dffb485Schristos /* Convert a CORE_ADDR into a HEX string, like %lx.
1008dffb485Schristos    The result is stored in a circular static buffer, NUMCELLS deep.  */
1018dffb485Schristos 
102*2be465b0Schristos const char *
1038dffb485Schristos paddress (CORE_ADDR addr)
1048dffb485Schristos {
1058dffb485Schristos   return phex_nz (addr, sizeof (CORE_ADDR));
1068dffb485Schristos }
107