From 03dd37c302866305fa6d48988f558c4eba2bbabe Mon Sep 17 00:00:00 2001 From: Andrew Cagney Date: Wed, 17 Oct 2001 20:35:31 +0000 Subject: [PATCH] Functions to convert to/from CORE_ADDR and a string. Use in Insight. --- gdb/ChangeLog | 10 ++++++++++ gdb/defs.h | 6 ++++++ gdb/utils.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index aea9dc1b715..1cc56a5a6b7 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,13 @@ +2001-10-16 Andrew Cagney + + Based on code by John Moore : + + * utils.c (core_addr_to_string): New function for conversion of + CORE_ADDR to string. + (string_to_core_addr): New function to convert from string to + CORE_ADDR. + * defs.h: Added extern statements for the above. + 2001-10-17 Jason Molenda (jason-cl@molenda.com) * symtab.c (lookup_block_symbol): Break out of linear search diff --git a/gdb/defs.h b/gdb/defs.h index a0d6d03e8d2..390600ff6ba 100644 --- a/gdb/defs.h +++ b/gdb/defs.h @@ -690,6 +690,12 @@ extern char *paddr_d (LONGEST addr); extern char *phex (ULONGEST l, int sizeof_l); extern char *phex_nz (ULONGEST l, int sizeof_l); +/* Like paddr() only print/scan raw CORE_ADDR. The output from + core_addr_to_string() can be passed direct to + string_to_core_addr(). */ +extern const char *core_addr_to_string (const CORE_ADDR addr); +extern CORE_ADDR string_to_core_addr (const char *my_string); + extern void fprintf_symbol_filtered (struct ui_file *, char *, enum language, int); diff --git a/gdb/utils.c b/gdb/utils.c index ba73d8d5734..d32957f7019 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -2490,3 +2490,47 @@ address_to_host_pointer (CORE_ADDR addr) ADDRESS_TO_POINTER (builtin_type_void_data_ptr, &ptr, addr); return ptr; } + +/* Convert a CORE_ADDR into a string. */ +const char * +core_addr_to_string (const CORE_ADDR addr) +{ + char *str = get_cell (); + strcpy (str, "0x"); + strcat (str, phex_nz (addr, sizeof (addr))); + return str; +} + +/* Convert a string back into a CORE_ADDR. */ +CORE_ADDR +string_to_core_addr (const char *my_string) +{ + CORE_ADDR addr = 0; + if (my_string[0] == '0' && tolower (my_string[1]) == 'x') + { + /* Assume that it is in decimal. */ + int i; + for (i = 2; my_string[i] != '\0'; i++) + { + if (isdigit (my_string[i])) + addr = (my_string[i] - '0') + (addr * 16); + else if (isxdigit (my_string[i])) + addr = (tolower (my_string[i]) - 'a' + 0xa) + (addr * 16); + else + internal_error (__FILE__, __LINE__, "invalid hex"); + } + } + else + { + /* Assume that it is in decimal. */ + int i; + for (i = 0; my_string[i] != '\0'; i++) + { + if (isdigit (my_string[i])) + addr = (my_string[i] - '0') + (addr * 10); + else + internal_error (__FILE__, __LINE__, "invalid decimal"); + } + } + return addr; +} -- 2.30.2