gdb/gdbserver/utils.c - gdb

Functions defined

Macros defined

Source code

  1. /* General utility routines for the remote server for GDB.
  2.    Copyright (C) 1986-2015 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. /* Copy a string into a memory buffer.
  32.    If malloc fails, this will print a message to stderr and exit.  */

  33. char *
  34. xstrdup (const char *s)
  35. {
  36.   char *ret = strdup (s);
  37.   if (ret == NULL)
  38.     malloc_failure (strlen (s) + 1);
  39.   return ret;
  40. }

  41. /* Print the system error message for errno, and also mention STRING
  42.    as the file name for which the error was encountered.
  43.    Then return to command level.  */

  44. void
  45. perror_with_name (const char *string)
  46. {
  47.   const char *err;
  48.   char *combined;

  49.   err = strerror (errno);
  50.   if (err == NULL)
  51.     err = "unknown error";

  52.   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
  53.   strcpy (combined, string);
  54.   strcat (combined, ": ");
  55.   strcat (combined, err);

  56.   error ("%s.", combined);
  57. }

  58. /* Print an error message and return to top level.  */

  59. void
  60. verror (const char *string, va_list args)
  61. {
  62. #ifdef IN_PROCESS_AGENT
  63.   fflush (stdout);
  64.   vfprintf (stderr, string, args);
  65.   fprintf (stderr, "\n");
  66.   exit (1);
  67. #else
  68.   throw_verror (GENERIC_ERROR, string, args);
  69. #endif
  70. }

  71. void
  72. vwarning (const char *string, va_list args)
  73. {
  74.   fprintf (stderr, PREFIX);
  75.   vfprintf (stderr, string, args);
  76.   fprintf (stderr, "\n");
  77. }

  78. /* Report a problem internal to GDBserver, and exit.  */

  79. void
  80. internal_verror (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.   exit (1);
  87. }

  88. /* Report a problem internal to GDBserver.  */

  89. void
  90. internal_vwarning (const char *file, int line, const char *fmt, va_list args)
  91. {
  92.   fprintf (stderr"\
  93. %s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line);
  94.   vfprintf (stderr, fmt, args);
  95.   fprintf (stderr, "\n");
  96. }

  97. /* Convert a CORE_ADDR into a HEX string, like %lx.
  98.    The result is stored in a circular static buffer, NUMCELLS deep.  */

  99. char *
  100. paddress (CORE_ADDR addr)
  101. {
  102.   return phex_nz (addr, sizeof (CORE_ADDR));
  103. }

  104. /* Convert a file descriptor into a printable string.  */

  105. char *
  106. pfildes (gdb_fildes_t fd)
  107. {
  108. #if USE_WIN32API
  109.   return phex_nz (fd, sizeof (gdb_fildes_t));
  110. #else
  111.   return plongest (fd);
  112. #endif
  113. }

  114. /* See common/common-exceptions.h.  */

  115. void
  116. prepare_to_throw_exception (void)
  117. {
  118.   /* No client-specific actions required.  */
  119. }