OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gdb-7.2/] [gdb/] [gdbserver/] [utils.c] - Blame information for rev 866

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 330 jeremybenn
/* General utility routines for the remote server for GDB.
2
   Copyright (C) 1986, 1989, 1993, 1995, 1996, 1997, 1999, 2000, 2002, 2003,
3
   2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
 
5
   This file is part of GDB.
6
 
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.
11
 
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.
16
 
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/>.  */
19
 
20
#include "server.h"
21
#include <stdio.h>
22
#include <string.h>
23
#include <stdlib.h>
24
#if HAVE_ERRNO_H
25
#include <errno.h>
26
#endif
27
#if HAVE_MALLOC_H
28
#include <malloc.h>
29
#endif
30
 
31
#ifdef IN_PROCESS_AGENT
32
#  define PREFIX "ipa: "
33
#  define TOOLNAME "GDBserver in-process agent"
34
#else
35
#  define PREFIX "gdbserver: "
36
#  define TOOLNAME "GDBserver"
37
#endif
38
 
39
/* Generally useful subroutines used throughout the program.  */
40
 
41
static void malloc_failure (size_t size) ATTR_NORETURN;
42
 
43
static void
44
malloc_failure (size_t size)
45
{
46
  fprintf (stderr, PREFIX "ran out of memory while trying to allocate %lu bytes\n",
47
           (unsigned long) size);
48
  exit (1);
49
}
50
 
51
/* Allocate memory without fail.
52
   If malloc fails, this will print a message to stderr and exit.  */
53
 
54
void *
55
xmalloc (size_t size)
56
{
57
  void *newmem;
58
 
59
  if (size == 0)
60
    size = 1;
61
  newmem = malloc (size);
62
  if (!newmem)
63
    malloc_failure (size);
64
 
65
  return newmem;
66
}
67
 
68
/* Reallocate memory without fail.  This works like xmalloc. */
69
 
70
void *
71
xrealloc (void *ptr, size_t size)
72
{
73
  void *val;
74
 
75
  if (size == 0)
76
    size = 1;
77
 
78
  if (ptr != NULL)
79
    val = realloc (ptr, size);  /* OK: realloc */
80
  else
81
    val = malloc (size);        /* OK: malloc */
82
  if (val == NULL)
83
    malloc_failure (size);
84
 
85
  return val;
86
}
87
 
88
/* Allocate memory without fail and set it to zero.
89
   If malloc fails, this will print a message to stderr and exit.  */
90
 
91
void *
92
xcalloc (size_t nelem, size_t elsize)
93
{
94
  void *newmem;
95
 
96
  if (nelem == 0 || elsize == 0)
97
    nelem = elsize = 1;
98
 
99
  newmem = calloc (nelem, elsize);
100
  if (!newmem)
101
    malloc_failure (nelem * elsize);
102
 
103
  return newmem;
104
}
105
 
106
/* Copy a string into a memory buffer.
107
   If malloc fails, this will print a message to stderr and exit.  */
108
 
109
char *
110
xstrdup (const char *s)
111
{
112
  char *ret = strdup (s);
113
  if (ret == NULL)
114
    malloc_failure (strlen (s) + 1);
115
  return ret;
116
}
117
 
118
#ifndef IN_PROCESS_AGENT
119
 
120
/* Free a standard argv vector.  */
121
 
122
void
123
freeargv (char **vector)
124
{
125
  char **scan;
126
 
127
  if (vector != NULL)
128
    {
129
      for (scan = vector; *scan != NULL; scan++)
130
        {
131
          free (*scan);
132
        }
133
      free (vector);
134
    }
135
}
136
 
137
#endif
138
 
139
/* Print the system error message for errno, and also mention STRING
140
   as the file name for which the error was encountered.
141
   Then return to command level.  */
142
 
143
void
144
perror_with_name (const char *string)
145
{
146
  const char *err;
147
  char *combined;
148
 
149
  err = strerror (errno);
150
  if (err == NULL)
151
    err = "unknown error";
152
 
153
  combined = (char *) alloca (strlen (err) + strlen (string) + 3);
154
  strcpy (combined, string);
155
  strcat (combined, ": ");
156
  strcat (combined, err);
157
 
158
  error ("%s.", combined);
159
}
160
 
161
/* Print an error message and return to command level.
162
   STRING is the error message, used as a fprintf string,
163
   and ARG is passed as an argument to it.  */
164
 
165
void
166
error (const char *string,...)
167
{
168
#ifndef IN_PROCESS_AGENT
169
  extern jmp_buf toplevel;
170
#endif
171
  va_list args;
172
  va_start (args, string);
173
  fflush (stdout);
174
  vfprintf (stderr, string, args);
175
  fprintf (stderr, "\n");
176
#ifndef IN_PROCESS_AGENT
177
  longjmp (toplevel, 1);
178
#else
179
  exit (1);
180
#endif
181
}
182
 
183
/* Print an error message and exit reporting failure.
184
   This is for a error that we cannot continue from.
185
   STRING and ARG are passed to fprintf.  */
186
 
187
/* VARARGS */
188
void
189
fatal (const char *string,...)
190
{
191
  va_list args;
192
  va_start (args, string);
193
  fprintf (stderr, PREFIX);
194
  vfprintf (stderr, string, args);
195
  fprintf (stderr, "\n");
196
  va_end (args);
197
  exit (1);
198
}
199
 
200
/* VARARGS */
201
void
202
warning (const char *string,...)
203
{
204
  va_list args;
205
  va_start (args, string);
206
  fprintf (stderr, PREFIX);
207
  vfprintf (stderr, string, args);
208
  fprintf (stderr, "\n");
209
  va_end (args);
210
}
211
 
212
/* Report a problem internal to GDBserver, and exit.  */
213
 
214
void
215
internal_error (const char *file, int line, const char *fmt, ...)
216
{
217
  va_list args;
218
  va_start (args, fmt);
219
 
220
  fprintf (stderr,  "\
221
%s:%d: A problem internal to " TOOLNAME " has been detected.\n", file, line);
222
  vfprintf (stderr, fmt, args);
223
  fprintf (stderr, "\n");
224
  va_end (args);
225
  exit (1);
226
}
227
 
228
/* Temporary storage using circular buffer.  */
229
#define NUMCELLS 10
230
#define CELLSIZE 50
231
 
232
/* Return the next entry in the circular buffer.  */
233
 
234
static char *
235
get_cell (void)
236
{
237
  static char buf[NUMCELLS][CELLSIZE];
238
  static int cell = 0;
239
  if (++cell >= NUMCELLS)
240
    cell = 0;
241
  return buf[cell];
242
}
243
 
244
/* Stdarg wrapper around vsnprintf.
245
   SIZE is the size of the buffer pointed to by STR.  */
246
 
247
static int
248
xsnprintf (char *str, size_t size, const char *format, ...)
249
{
250
  va_list args;
251
  int ret;
252
 
253
  va_start (args, format);
254
  ret = vsnprintf (str, size, format, args);
255
  va_end (args);
256
 
257
  return ret;
258
}
259
 
260
static char *
261
decimal2str (char *sign, ULONGEST addr, int width)
262
{
263
  /* Steal code from valprint.c:print_decimal().  Should this worry
264
     about the real size of addr as the above does? */
265
  unsigned long temp[3];
266
  char *str = get_cell ();
267
 
268
  int i = 0;
269
  do
270
    {
271
      temp[i] = addr % (1000 * 1000 * 1000);
272
      addr /= (1000 * 1000 * 1000);
273
      i++;
274
      width -= 9;
275
    }
276
  while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
277
 
278
  width = 9;
279
  if (width < 0)
280
    width = 0;
281
 
282
  switch (i)
283
    {
284
    case 1:
285
      xsnprintf (str, CELLSIZE, "%s%0*lu", sign, width, temp[0]);
286
      break;
287
    case 2:
288
      xsnprintf (str, CELLSIZE, "%s%0*lu%09lu", sign, width,
289
                 temp[1], temp[0]);
290
      break;
291
    case 3:
292
      xsnprintf (str, CELLSIZE, "%s%0*lu%09lu%09lu", sign, width,
293
                 temp[2], temp[1], temp[0]);
294
      break;
295
    default:
296
      internal_error (__FILE__, __LINE__,
297
                      "failed internal consistency check");
298
    }
299
 
300
  return str;
301
}
302
 
303
/* %u for ULONGEST.  The result is stored in a circular static buffer,
304
   NUMCELLS deep.  */
305
 
306
char *
307
pulongest (ULONGEST u)
308
{
309
  return decimal2str ("", u, 0);
310
}
311
 
312
/* %d for LONGEST.  The result is stored in a circular static buffer,
313
   NUMCELLS deep.  */
314
 
315
char *
316
plongest (LONGEST l)
317
{
318
  if (l < 0)
319
    return decimal2str ("-", -l, 0);
320
  else
321
    return decimal2str ("", l, 0);
322
}
323
 
324
/* Eliminate warning from compiler on 32-bit systems.  */
325
static int thirty_two = 32;
326
 
327
/* Convert a ULONGEST into a HEX string, like %lx.  The result is
328
   stored in a circular static buffer, NUMCELLS deep.  */
329
 
330
char *
331
phex_nz (ULONGEST l, int sizeof_l)
332
{
333
  char *str;
334
 
335
  switch (sizeof_l)
336
    {
337
    case 8:
338
      {
339
        unsigned long high = (unsigned long) (l >> thirty_two);
340
        str = get_cell ();
341
        if (high == 0)
342
          xsnprintf (str, CELLSIZE, "%lx",
343
                     (unsigned long) (l & 0xffffffff));
344
        else
345
          xsnprintf (str, CELLSIZE, "%lx%08lx", high,
346
                     (unsigned long) (l & 0xffffffff));
347
        break;
348
      }
349
    case 4:
350
      str = get_cell ();
351
      xsnprintf (str, CELLSIZE, "%lx", (unsigned long) l);
352
      break;
353
    case 2:
354
      str = get_cell ();
355
      xsnprintf (str, CELLSIZE, "%x", (unsigned short) (l & 0xffff));
356
      break;
357
    default:
358
      str = phex_nz (l, sizeof (l));
359
      break;
360
    }
361
 
362
  return str;
363
}
364
 
365
/* Convert a CORE_ADDR into a HEX string, like %lx.
366
   The result is stored in a circular static buffer, NUMCELLS deep.  */
367
 
368
char *
369
paddress (CORE_ADDR addr)
370
{
371
  return phex_nz (addr, sizeof (CORE_ADDR));
372
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.