1 //==========================================================================
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
44 // Contributors: gthomas, dmoseley
46 // Purpose: Monitor utilities.
50 //####DESCRIPTIONEND####
52 //=========================================================================
61 #include <bsp/hex-utils.h>
66 #if USE_CYGMON_PROTOTYPES
67 /* Use common prototypes */
68 /* Some of the composed board.h files compose these
69 prototypes redundently, but if they dont,
70 these are the common definitions */
71 #include "fmt_util.h" /* Interface to string formatting utilities */
72 #include "generic-stub.h" /* from libstub */
73 #endif /* USE_CYGMON_PROTOTYPES */
75 volatile int switch_to_stub_flag = 0;
77 /* Input routine for the line editor. */
83 /* We have to drop the '+' characters on the floor
84 because gdb will send a '+' as the first character
85 when connecting to the target. If we waste time
86 echoing that, slow hw might get a uart overrun. */
87 while ((i = xgetchar ()) == '+');
92 switch_to_stub_flag = 1;
99 static char tohex_array[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
100 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
102 #define tohex(X) (tohex_array[(X) & 15])
105 #define fromhex __hex
112 if (a >= '0' && a <= '9')
114 else if (a >= 'a' && a <= 'f')
115 number = a - 'a' + 10;
116 else if (a >= 'A' && a <= 'F')
117 number = a - 'A' + 10;
119 xprintf ("Invalid hex digit %c", a);
126 static unsigned long long
127 str2ull (char *str, int base)
129 unsigned long long l = 0;
131 if (str[0] == '0' && str[1] == 'x')
142 l = (l * base) + fromhex(*str++);
149 /* Converts a string to a long, base is the assumed base of the string */
152 str2int (char *str, int base)
154 return str2ull(str, base);
157 /* Converts a string to a double, input is a raw integer string
158 * of the given assumed base. */
161 str2double (char *str, int base)
165 switch (sizeof(double))
167 case sizeof(unsigned int):
168 *((unsigned int *)&d) = str2ull(str, base);
170 #if __LONG_MAX__ != __INT_MAX__
171 case sizeof(unsigned long):
172 *((unsigned long *)&d) = str2ull(str, base);
175 #if __LONG_LONG_MAX__ != __LONG_MAX__
176 case sizeof(unsigned long long):
177 *((unsigned long long *)&d) = str2ull(str, base);
189 str2intlen (char *str, int base, int len)
191 target_register_t number = 0;
193 while ((len--) > 0 && *str != '\0')
194 number = number * base + fromhex (*(str++));
200 hex2bytes (char *str, char *dest, int maxsize)
205 for (i = 0; i < maxsize; i++)
209 // Don't try and convert 0x prefix
210 if ((str[0] == '0') && (str[1] == 'x'))
213 ptr = str + strlen(str) - 1;
214 while (maxsize >= 0 && ptr >= str)
216 dest [maxsize] = fromhex(*ptr);
220 dest [maxsize--] |= fromhex(*ptr) * 16;
228 /* Converts an unsigned long long to an ASCII string, adding leading
229 zeroes to pad space up to numdigs. */
230 static int use_dots = 1;
232 #define MAX_NUM_DIGS 51
235 ull2str (unsigned long long number, int base, int numdigs)
237 static char string[MAX_NUM_DIGS+1];
239 char *ptr = string + MAX_NUM_DIGS;
241 dots = (use_dots && base == 16);
244 *(ptr--) = tohex (number % base);
246 number = number / base;
250 if (dots && (i % 4) == 0)
252 *(ptr--) = tohex (number % base);
254 number = number / base;
265 if (dots && (i % 4) == 0)
276 int2str (target_register_t number, int base, int numdigs)
278 return ull2str((unsigned long long)number, base, numdigs);
285 switch(sizeof(double))
287 case sizeof(unsigned int):
288 return ull2str(*((unsigned int *)&d), 16, sizeof(double) * 2);
290 #if __LONG_MAX__ != __INT_MAX__
291 case sizeof(unsigned long):
292 return ull2str(*((unsigned long *)&d), 16, sizeof(double) * 2);
295 #if __LONG_LONG_MAX__ != __LONG_MAX__
296 case sizeof(unsigned long long):
297 return ull2str(*((unsigned long long *)&d), 16, sizeof(double) * 2);
301 return "....fixme...";
307 strdup(const char *str)
309 char *x = malloc (strlen (str) + 1);
320 return get_register(REG_PC);
324 #if defined(HAVE_BSP) && !defined(__ECOS__)
326 get_register_type(regnames_t which)
330 for (i = 0; regtab[i].registername != NULL; i++)
331 if (regtab[i].registernumber == which)
332 return regtab[i].registertype;
337 char *get_register_str (regnames_t which, int detail, int valid)
339 #ifdef SPECIAL_REG_OUTPUT
342 if ((res = SPECIAL_REG_OUTPUT (which, detail)) != NULL)
349 switch (sizeof (target_register_t))
351 case 1: return "...";
352 case 2: return ".....";
353 case 4: return ".........";
354 case 8: return ".................";
355 default: return ".........";
360 return int2str (get_register (which), 16, sizeof (target_register_t) * 2);
366 store_register (regnames_t which, char *string)
368 #ifdef SPECIAL_REG_STORE
369 if (SPECIAL_REG_STORE(which, string))
372 put_register (which, str2int (string, 16));