1e93f7393Sniklas /* General utility routines for the remote server for GDB.
2*b725ae77Skettenis Copyright 1986, 1989, 1993, 1995, 1996, 1997, 1999, 2000, 2002, 2003
3*b725ae77Skettenis Free Software Foundation, Inc.
4e93f7393Sniklas
5e93f7393Sniklas This file is part of GDB.
6e93f7393Sniklas
7e93f7393Sniklas This program is free software; you can redistribute it and/or modify
8e93f7393Sniklas it under the terms of the GNU General Public License as published by
9e93f7393Sniklas the Free Software Foundation; either version 2 of the License, or
10e93f7393Sniklas (at your option) any later version.
11e93f7393Sniklas
12e93f7393Sniklas This program is distributed in the hope that it will be useful,
13e93f7393Sniklas but WITHOUT ANY WARRANTY; without even the implied warranty of
14e93f7393Sniklas MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15e93f7393Sniklas GNU General Public License for more details.
16e93f7393Sniklas
17e93f7393Sniklas You should have received a copy of the GNU General Public License
18e93f7393Sniklas along with this program; if not, write to the Free Software
19*b725ae77Skettenis Foundation, Inc., 59 Temple Place - Suite 330,
20*b725ae77Skettenis Boston, MA 02111-1307, USA. */
21e93f7393Sniklas
22e93f7393Sniklas #include "server.h"
23e93f7393Sniklas #include <stdio.h>
24e93f7393Sniklas #include <string.h>
25e93f7393Sniklas
26e93f7393Sniklas /* Generally useful subroutines used throughout the program. */
27e93f7393Sniklas
28e93f7393Sniklas /* Print the system error message for errno, and also mention STRING
29e93f7393Sniklas as the file name for which the error was encountered.
30e93f7393Sniklas Then return to command level. */
31e93f7393Sniklas
32e93f7393Sniklas void
perror_with_name(char * string)33*b725ae77Skettenis perror_with_name (char *string)
34e93f7393Sniklas {
35*b725ae77Skettenis #ifndef STDC_HEADERS
36e93f7393Sniklas extern int errno;
37*b725ae77Skettenis #endif
38*b725ae77Skettenis const char *err;
39e93f7393Sniklas char *combined;
40e93f7393Sniklas
41*b725ae77Skettenis err = strerror (errno);
42*b725ae77Skettenis if (err == NULL)
43e93f7393Sniklas err = "unknown error";
44e93f7393Sniklas
45e93f7393Sniklas combined = (char *) alloca (strlen (err) + strlen (string) + 3);
46e93f7393Sniklas strcpy (combined, string);
47e93f7393Sniklas strcat (combined, ": ");
48e93f7393Sniklas strcat (combined, err);
49e93f7393Sniklas
50e93f7393Sniklas error ("%s.", combined);
51e93f7393Sniklas }
52e93f7393Sniklas
53e93f7393Sniklas /* Print an error message and return to command level.
54e93f7393Sniklas STRING is the error message, used as a fprintf string,
55e93f7393Sniklas and ARG is passed as an argument to it. */
56e93f7393Sniklas
57e93f7393Sniklas void
error(const char * string,...)58*b725ae77Skettenis error (const char *string,...)
59e93f7393Sniklas {
60e93f7393Sniklas extern jmp_buf toplevel;
61e93f7393Sniklas va_list args;
62e93f7393Sniklas va_start (args, string);
63e93f7393Sniklas fflush (stdout);
64e93f7393Sniklas vfprintf (stderr, string, args);
65e93f7393Sniklas fprintf (stderr, "\n");
66e93f7393Sniklas longjmp (toplevel, 1);
67e93f7393Sniklas }
68e93f7393Sniklas
69e93f7393Sniklas /* Print an error message and exit reporting failure.
70e93f7393Sniklas This is for a error that we cannot continue from.
71e93f7393Sniklas STRING and ARG are passed to fprintf. */
72e93f7393Sniklas
73e93f7393Sniklas /* VARARGS */
74*b725ae77Skettenis void
fatal(const char * string,...)75*b725ae77Skettenis fatal (const char *string,...)
76e93f7393Sniklas {
77e93f7393Sniklas va_list args;
78e93f7393Sniklas va_start (args, string);
79e93f7393Sniklas fprintf (stderr, "gdb: ");
80e93f7393Sniklas vfprintf (stderr, string, args);
81e93f7393Sniklas fprintf (stderr, "\n");
82e93f7393Sniklas va_end (args);
83e93f7393Sniklas exit (1);
84e93f7393Sniklas }
85*b725ae77Skettenis
86*b725ae77Skettenis /* VARARGS */
87*b725ae77Skettenis void
warning(const char * string,...)88*b725ae77Skettenis warning (const char *string,...)
89*b725ae77Skettenis {
90*b725ae77Skettenis va_list args;
91*b725ae77Skettenis va_start (args, string);
92*b725ae77Skettenis fprintf (stderr, "gdb: ");
93*b725ae77Skettenis vfprintf (stderr, string, args);
94*b725ae77Skettenis fprintf (stderr, "\n");
95*b725ae77Skettenis va_end (args);
96*b725ae77Skettenis }
97