1 /* General utility routines for the remote server for GDB. 2 Copyright (C) 1986-2023 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 #include "server.h" 20 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 29 /* Generally useful subroutines used throughout the program. */ 30 31 /* If in release mode, just exit. This avoids potentially littering 32 the filesystem of small embedded targets with core files. If in 33 development mode however, abort, producing core files to help with 34 debugging GDBserver. */ 35 static void ATTRIBUTE_NORETURN 36 abort_or_exit () 37 { 38 #ifdef DEVELOPMENT 39 abort (); 40 #else 41 exit (1); 42 #endif 43 } 44 45 void 46 malloc_failure (long size) 47 { 48 fprintf (stderr, 49 PREFIX "ran out of memory while trying to allocate %lu bytes\n", 50 (unsigned long) size); 51 abort_or_exit (); 52 } 53 54 /* Print the system error message for errno, and also mention STRING 55 as the file name for which the error was encountered. 56 Then return to command level. */ 57 58 void 59 perror_with_name (const char *string) 60 { 61 const char *err; 62 char *combined; 63 64 err = safe_strerror (errno); 65 if (err == NULL) 66 err = "unknown error"; 67 68 combined = (char *) alloca (strlen (err) + strlen (string) + 3); 69 strcpy (combined, string); 70 strcat (combined, ": "); 71 strcat (combined, err); 72 73 error ("%s.", combined); 74 } 75 76 /* Print an error message and return to top level. */ 77 78 void 79 verror (const char *string, va_list args) 80 { 81 #ifdef IN_PROCESS_AGENT 82 fflush (stdout); 83 vfprintf (stderr, string, args); 84 fprintf (stderr, "\n"); 85 exit (1); 86 #else 87 throw_verror (GENERIC_ERROR, string, args); 88 #endif 89 } 90 91 void 92 vwarning (const char *string, va_list args) 93 { 94 fprintf (stderr, PREFIX); 95 vfprintf (stderr, string, args); 96 fprintf (stderr, "\n"); 97 } 98 99 /* Report a problem internal to GDBserver, and abort/exit. */ 100 101 void 102 internal_verror (const char *file, int line, const char *fmt, va_list args) 103 { 104 fprintf (stderr, "\ 105 %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line); 106 vfprintf (stderr, fmt, args); 107 fprintf (stderr, "\n"); 108 abort_or_exit (); 109 } 110 111 /* Report a problem internal to GDBserver. */ 112 113 void 114 internal_vwarning (const char *file, int line, const char *fmt, va_list args) 115 { 116 fprintf (stderr, "\ 117 %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line); 118 vfprintf (stderr, fmt, args); 119 fprintf (stderr, "\n"); 120 } 121 122 /* Convert a CORE_ADDR into a HEX string, like %lx. 123 The result is stored in a circular static buffer, NUMCELLS deep. */ 124 125 char * 126 paddress (CORE_ADDR addr) 127 { 128 return phex_nz (addr, sizeof (CORE_ADDR)); 129 } 130