utils.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* General utility routines for the remote server for GDB.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "server.h"
  15. #ifdef IN_PROCESS_AGENT
  16. # define PREFIX "ipa: "
  17. # define TOOLNAME "GDBserver in-process agent"
  18. #else
  19. # define PREFIX "gdbserver: "
  20. # define TOOLNAME "GDBserver"
  21. #endif
  22. /* Generally useful subroutines used throughout the program. */
  23. void
  24. malloc_failure (long size)
  25. {
  26. fprintf (stderr,
  27. PREFIX "ran out of memory while trying to allocate %lu bytes\n",
  28. (unsigned long) size);
  29. exit (1);
  30. }
  31. /* Print the system error message for errno, and also mention STRING
  32. as the file name for which the error was encountered.
  33. Then return to command level. */
  34. void
  35. perror_with_name (const char *string)
  36. {
  37. const char *err;
  38. char *combined;
  39. err = safe_strerror (errno);
  40. if (err == NULL)
  41. err = "unknown error";
  42. combined = (char *) alloca (strlen (err) + strlen (string) + 3);
  43. strcpy (combined, string);
  44. strcat (combined, ": ");
  45. strcat (combined, err);
  46. error ("%s.", combined);
  47. }
  48. /* Print an error message and return to top level. */
  49. void
  50. verror (const char *string, va_list args)
  51. {
  52. #ifdef IN_PROCESS_AGENT
  53. fflush (stdout);
  54. vfprintf (stderr, string, args);
  55. fprintf (stderr, "\n");
  56. exit (1);
  57. #else
  58. throw_verror (GENERIC_ERROR, string, args);
  59. #endif
  60. }
  61. void
  62. vwarning (const char *string, va_list args)
  63. {
  64. fprintf (stderr, PREFIX);
  65. vfprintf (stderr, string, args);
  66. fprintf (stderr, "\n");
  67. }
  68. /* Report a problem internal to GDBserver, and exit. */
  69. void
  70. internal_verror (const char *file, int line, const char *fmt, va_list args)
  71. {
  72. fprintf (stderr, "\
  73. %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line);
  74. vfprintf (stderr, fmt, args);
  75. fprintf (stderr, "\n");
  76. exit (1);
  77. }
  78. /* Report a problem internal to GDBserver. */
  79. void
  80. internal_vwarning (const char *file, int line, const char *fmt, va_list args)
  81. {
  82. fprintf (stderr, "\
  83. %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line);
  84. vfprintf (stderr, fmt, args);
  85. fprintf (stderr, "\n");
  86. }
  87. /* Convert a CORE_ADDR into a HEX string, like %lx.
  88. The result is stored in a circular static buffer, NUMCELLS deep. */
  89. char *
  90. paddress (CORE_ADDR addr)
  91. {
  92. return phex_nz (addr, sizeof (CORE_ADDR));
  93. }