1 /* General utility routines for the remote server for GDB.
2 Copyright (C) 1986, 1989, 1993, 1995-1997, 1999-2000, 2002-2003,
3 2007-2012 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #ifdef IN_PROCESS_AGENT
29 # define PREFIX "ipa: "
30 # define TOOLNAME "GDBserver in-process agent"
32 # define PREFIX "gdbserver: "
33 # define TOOLNAME "GDBserver"
36 /* Generally useful subroutines used throughout the program. */
39 malloc_failure (long size
)
42 PREFIX
"ran out of memory while trying to allocate %lu bytes\n",
43 (unsigned long) size
);
47 /* Copy a string into a memory buffer.
48 If malloc fails, this will print a message to stderr and exit. */
51 xstrdup (const char *s
)
53 char *ret
= strdup (s
);
55 malloc_failure (strlen (s
) + 1);
59 #ifndef IN_PROCESS_AGENT
61 /* Free a standard argv vector. */
64 freeargv (char **vector
)
70 for (scan
= vector
; *scan
!= NULL
; scan
++)
80 /* Print the system error message for errno, and also mention STRING
81 as the file name for which the error was encountered.
82 Then return to command level. */
85 perror_with_name (const char *string
)
90 err
= strerror (errno
);
92 err
= "unknown error";
94 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
95 strcpy (combined
, string
);
96 strcat (combined
, ": ");
97 strcat (combined
, err
);
99 error ("%s.", combined
);
102 /* Print an error message and return to command level.
103 STRING is the error message, used as a fprintf string,
104 and ARG is passed as an argument to it. */
107 error (const char *string
,...)
109 #ifndef IN_PROCESS_AGENT
110 extern jmp_buf toplevel
;
113 va_start (args
, string
);
115 vfprintf (stderr
, string
, args
);
116 fprintf (stderr
, "\n");
117 #ifndef IN_PROCESS_AGENT
118 longjmp (toplevel
, 1);
124 /* Print an error message and exit reporting failure.
125 This is for a error that we cannot continue from.
126 STRING and ARG are passed to fprintf. */
130 fatal (const char *string
,...)
133 va_start (args
, string
);
134 fprintf (stderr
, PREFIX
);
135 vfprintf (stderr
, string
, args
);
136 fprintf (stderr
, "\n");
143 warning (const char *string
,...)
146 va_start (args
, string
);
147 fprintf (stderr
, PREFIX
);
148 vfprintf (stderr
, string
, args
);
149 fprintf (stderr
, "\n");
153 /* Report a problem internal to GDBserver, and exit. */
156 internal_error (const char *file
, int line
, const char *fmt
, ...)
159 va_start (args
, fmt
);
162 %s:%d: A problem internal to " TOOLNAME
" has been detected.\n", file
, line
);
163 vfprintf (stderr
, fmt
, args
);
164 fprintf (stderr
, "\n");
169 /* Temporary storage using circular buffer. */
173 /* Return the next entry in the circular buffer. */
178 static char buf
[NUMCELLS
][CELLSIZE
];
180 if (++cell
>= NUMCELLS
)
186 decimal2str (char *sign
, ULONGEST addr
)
188 /* Steal code from valprint.c:print_decimal(). Should this worry
189 about the real size of addr as the above does? */
190 unsigned long temp
[3];
191 char *str
= get_cell ();
197 temp
[i
] = addr
% (1000 * 1000 * 1000);
198 addr
/= (1000 * 1000 * 1000);
201 while (addr
!= 0 && i
< (sizeof (temp
) / sizeof (temp
[0])));
206 xsnprintf (str
, CELLSIZE
, "%s%0*lu", sign
, width
, temp
[0]);
209 xsnprintf (str
, CELLSIZE
, "%s%0*lu%09lu", sign
, width
,
213 xsnprintf (str
, CELLSIZE
, "%s%0*lu%09lu%09lu", sign
, width
,
214 temp
[2], temp
[1], temp
[0]);
217 internal_error (__FILE__
, __LINE__
,
218 "failed internal consistency check");
224 /* %u for ULONGEST. The result is stored in a circular static buffer,
228 pulongest (ULONGEST u
)
230 return decimal2str ("", u
);
233 /* %d for LONGEST. The result is stored in a circular static buffer,
240 return decimal2str ("-", -l
);
242 return decimal2str ("", l
);
245 /* Eliminate warning from compiler on 32-bit systems. */
246 static int thirty_two
= 32;
248 /* Convert a ULONGEST into a HEX string, like %lx. The result is
249 stored in a circular static buffer, NUMCELLS deep. */
252 phex_nz (ULONGEST l
, int sizeof_l
)
260 unsigned long high
= (unsigned long) (l
>> thirty_two
);
263 xsnprintf (str
, CELLSIZE
, "%lx",
264 (unsigned long) (l
& 0xffffffff));
266 xsnprintf (str
, CELLSIZE
, "%lx%08lx", high
,
267 (unsigned long) (l
& 0xffffffff));
272 xsnprintf (str
, CELLSIZE
, "%lx", (unsigned long) l
);
276 xsnprintf (str
, CELLSIZE
, "%x", (unsigned short) (l
& 0xffff));
279 str
= phex_nz (l
, sizeof (l
));
286 /* Convert a CORE_ADDR into a HEX string, like %lx.
287 The result is stored in a circular static buffer, NUMCELLS deep. */
290 paddress (CORE_ADDR addr
)
292 return phex_nz (addr
, sizeof (CORE_ADDR
));
295 /* Convert a file descriptor into a printable string. */
298 pfildes (gdb_fildes_t fd
)
301 return phex_nz (fd
, sizeof (gdb_fildes_t
));
303 return plongest (fd
);
This page took 0.035711 seconds and 4 git commands to generate.