This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[15/19] create_string_type and string value routines
- From: "Ulrich Weigand" <uweigand at de dot ibm dot com>
- To: gdb-patches at sourceware dot org
- Date: Fri, 5 Jun 2009 23:20:15 +0200 (CEST)
- Subject: [15/19] create_string_type and string value routines
Hello,
deeply buried in various call hierarchies, the create_string_type routine
makes a reference to current_gdbarch:
string_char_type = language_string_char_type (current_language,
current_gdbarch);
This patch gets rid of this reference by pushing the construction of an
appropriate character type up the call chain. As a side effect, the
various routines to create string values are cleaned up and regularized
a bit. In particular:
- value_from_string is very similar to value_typed_string, and can in
fact be replaced by a call to the latter with appropriate arguments.
- value_string needs to acquire a character type argument. Also, the
routine today no longer needs to allocate space in the inferior; the
coerce_to_target machinery takes care of this transparently.
- As a result, value_string and value_typed_string now only differ in
that the former returns a TYPE_CODE_STRING type while the latter
returns a TYPE_CODE_ARRAY C-style string. As value_string now also
takes a type, the name value_typed_string seems misleading; I've renamed
it to value_cstring.
The rest of the patch is simply passing down gdbarch as needed to be
able to construct the proper character type at the value_string call
sites.
Note that once again the patch adds an instance of current_gdbarch to
the python code. This will be eliminated later on.
Bye,
Ulrich
ChangeLog:
* gdbtypes.c (create_string_type): Receive character type as argument.
* gdbtypes.h (create_string_type): Add character type argument.
* dwarf2read.c (read_tag_string_type): Pass character type to
create_string_type.
* value.h (value_string): Add character type argument.
* valops.c (value_string): Add character type argument. Pass it to
create_string_type. Do not allocate space in inferior.
* valarith.c (value_concat): Pass character type to value_string.
* value.h (value_typed_string): Rename to ...
(value_cstring): ... this.
* valops.c (value_typed_string): Rename to ...
(value_cstring): ... this.
* c-lang.c (evaluate_subexp_c): Update.
* python/python-value.c (builtin_type_pychar): New define.
(convert_value_from_python): Call value_cstring instead
of value_from_string.
* value.c (value_from_string): Remove.
* value.h (value_from_string): Remove.
* eval.c (evaluate_subexp_standard): Pass character type to
value_string. Pass expression architecture to value_nsstring
and lookup_child_selector.
* objc-lang.h (lookup_objc_class): Add GDBARCH parameter.
(lookup_child_selector): Likewise.
(value_nsstring): Likewise.
* objc-lang.c (lookup_objc_class): Add GDBARCH parameter.
Pass character type to value_string..
(lookup_child_selector): Likewise.
(value_nsstring): Add GDBARCH parameter, use it instead of
objfile architecture. Pass architecture to lookup_objc_class
and lookup_child_selector. Pass character type to value_string.
(end_msglist): Pass architecture to lookup_objc_class.
* objc-exp.y: Pass architecture to lookup_objc_class.
Index: gdb-head/gdb/dwarf2read.c
===================================================================
--- gdb-head.orig/gdb/dwarf2read.c
+++ gdb-head/gdb/dwarf2read.c
@@ -5061,6 +5061,7 @@ static struct type *
read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
{
struct objfile *objfile = cu->objfile;
+ struct gdbarch *gdbarch = get_objfile_arch (objfile);
struct type *type, *range_type, *index_type, *char_type;
struct attribute *attr;
unsigned int length;
@@ -5086,7 +5087,8 @@ read_tag_string_type (struct die_info *d
index_type = builtin_type_int32;
range_type = create_range_type (NULL, index_type, 1, length);
- type = create_string_type (NULL, range_type);
+ char_type = language_string_char_type (cu->language_defn, gdbarch);
+ type = create_string_type (NULL, char_type, range_type);
return set_die_type (die, type, cu);
}
Index: gdb-head/gdb/gdbtypes.c
===================================================================
--- gdb-head.orig/gdb/gdbtypes.c
+++ gdb-head/gdb/gdbtypes.c
@@ -846,13 +846,10 @@ create_array_type (struct type *result_t
type? */
struct type *
-create_string_type (struct type *result_type,
+create_string_type (struct type *result_type,
+ struct type *string_char_type,
struct type *range_type)
{
- struct type *string_char_type;
-
- string_char_type = language_string_char_type (current_language,
- current_gdbarch);
result_type = create_array_type (result_type,
string_char_type,
range_type);
Index: gdb-head/gdb/gdbtypes.h
===================================================================
--- gdb-head.orig/gdb/gdbtypes.h
+++ gdb-head/gdb/gdbtypes.h
@@ -1177,7 +1177,8 @@ extern struct type *create_range_type (s
extern struct type *create_array_type (struct type *, struct type *,
struct type *);
-extern struct type *create_string_type (struct type *, struct type *);
+extern struct type *create_string_type (struct type *, struct type *,
+ struct type *);
extern struct type *create_set_type (struct type *, struct type *);
Index: gdb-head/gdb/valops.c
===================================================================
--- gdb-head.orig/gdb/valops.c
+++ gdb-head/gdb/valops.c
@@ -1349,7 +1349,7 @@ value_array (int lowbound, int highbound
}
struct value *
-value_typed_string (char *ptr, int len, struct type *char_type)
+value_cstring (char *ptr, int len, struct type *char_type)
{
struct value *val;
int lowbound = current_language->string_lower_bound;
@@ -1376,34 +1376,21 @@ value_typed_string (char *ptr, int len,
string may contain embedded null bytes. */
struct value *
-value_string (char *ptr, int len)
+value_string (char *ptr, int len, struct type *char_type)
{
struct value *val;
int lowbound = current_language->string_lower_bound;
+ int highbound = len / TYPE_LENGTH (char_type);
struct type *rangetype = create_range_type ((struct type *) NULL,
builtin_type_int32,
lowbound,
- len + lowbound - 1);
+ highbound + lowbound - 1);
struct type *stringtype
- = create_string_type ((struct type *) NULL, rangetype);
- CORE_ADDR addr;
-
- if (current_language->c_style_arrays == 0)
- {
- val = allocate_value (stringtype);
- memcpy (value_contents_raw (val), ptr, len);
- return val;
- }
-
+ = create_string_type ((struct type *) NULL, char_type, rangetype);
- /* Allocate space to store the string in the inferior, and then copy
- LEN bytes from PTR in gdb to that address in the inferior. */
-
- addr = allocate_space_in_inferior (len);
- write_memory (addr, (gdb_byte *) ptr, len);
-
- val = value_at_lazy (stringtype, addr);
- return (val);
+ val = allocate_value (stringtype);
+ memcpy (value_contents_raw (val), ptr, len);
+ return val;
}
struct value *
Index: gdb-head/gdb/python/python-value.c
===================================================================
--- gdb-head.orig/gdb/python/python-value.c
+++ gdb-head/gdb/python/python-value.c
@@ -55,6 +55,9 @@ struct value *values_in_python = NULL;
#define builtin_type_pybool \
language_bool_type (current_language, current_gdbarch)
+#define builtin_type_pychar \
+ language_string_char_type (current_language, current_gdbarch)
+
typedef struct {
PyObject_HEAD
struct value *value;
@@ -867,7 +870,7 @@ convert_value_from_python (PyObject *obj
if (s != NULL)
{
old = make_cleanup (xfree, s);
- value = value_from_string (s);
+ value = value_cstring (s, strlen (s), builtin_type_pychar);
do_cleanups (old);
}
}
Index: gdb-head/gdb/value.c
===================================================================
--- gdb-head.orig/gdb/value.c
+++ gdb-head/gdb/value.c
@@ -2025,34 +2025,6 @@ value_from_pointer (struct type *type, C
}
-/* Create a value for a string constant to be stored locally
- (not in the inferior's memory space, but in GDB memory).
- This is analogous to value_from_longest, which also does not
- use inferior memory. String shall NOT contain embedded nulls. */
-
-struct value *
-value_from_string (char *ptr)
-{
- struct value *val;
- int len = strlen (ptr);
- int lowbound = current_language->string_lower_bound;
- struct type *string_char_type;
- struct type *rangetype;
- struct type *stringtype;
-
- rangetype = create_range_type ((struct type *) NULL,
- builtin_type_int32,
- lowbound, len + lowbound - 1);
- string_char_type = language_string_char_type (current_language,
- current_gdbarch);
- stringtype = create_array_type ((struct type *) NULL,
- string_char_type,
- rangetype);
- val = allocate_value (stringtype);
- memcpy (value_contents_raw (val), ptr, len);
- return val;
-}
-
/* Create a value of type TYPE whose contents come from VALADDR, if it
is non-null, and whose memory address (in the inferior) is
ADDRESS. */
Index: gdb-head/gdb/value.h
===================================================================
--- gdb-head.orig/gdb/value.h
+++ gdb-head/gdb/value.h
@@ -339,7 +339,6 @@ extern struct value *value_from_pointer
extern struct value *value_from_double (struct type *type, DOUBLEST num);
extern struct value *value_from_decfloat (struct type *type,
const gdb_byte *decbytes);
-extern struct value *value_from_string (char *string);
extern struct value *value_at (struct type *type, CORE_ADDR addr);
extern struct value *value_at_lazy (struct type *type, CORE_ADDR addr);
@@ -381,9 +380,10 @@ extern struct value *value_mark (void);
extern void value_free_to_mark (struct value *mark);
-extern struct value *value_typed_string (char *ptr, int len,
- struct type *char_type);
-extern struct value *value_string (char *ptr, int len);
+extern struct value *value_cstring (char *ptr, int len,
+ struct type *char_type);
+extern struct value *value_string (char *ptr, int len,
+ struct type *char_type);
extern struct value *value_bitstring (char *ptr, int len);
extern struct value *value_array (int lowbound, int highbound,
Index: gdb-head/gdb/eval.c
===================================================================
--- gdb-head.orig/gdb/eval.c
+++ gdb-head/gdb/eval.c
@@ -789,7 +789,8 @@ evaluate_subexp_standard (struct type *e
(*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
if (noside == EVAL_SKIP)
goto nosideret;
- return value_string (&exp->elts[pc + 2].string, tem);
+ type = language_string_char_type (exp->language_defn, exp->gdbarch);
+ return value_string (&exp->elts[pc + 2].string, tem, type);
case OP_OBJC_NSSTRING: /* Objective C Foundation Class NSString constant. */
tem = longest_to_int (exp->elts[pc + 1].longconst);
@@ -798,7 +799,7 @@ evaluate_subexp_standard (struct type *e
{
goto nosideret;
}
- return (struct value *) value_nsstring (&exp->elts[pc + 2].string, tem + 1);
+ return value_nsstring (exp->gdbarch, &exp->elts[pc + 2].string, tem + 1);
case OP_BITSTRING:
tem = longest_to_int (exp->elts[pc + 1].longconst);
@@ -1010,7 +1011,8 @@ evaluate_subexp_standard (struct type *e
sel[len] = 0; /* Make sure it's terminated. */
selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
- return value_from_longest (selector_type, lookup_child_selector (sel));
+ return value_from_longest (selector_type,
+ lookup_child_selector (exp->gdbarch, sel));
}
case OP_OBJC_MSGCALL:
@@ -1098,16 +1100,20 @@ evaluate_subexp_standard (struct type *e
the verification method than the non-standard, but more
often used, 'NSObject' class. Make sure we check for both. */
- responds_selector = lookup_child_selector ("respondsToSelector:");
+ responds_selector
+ = lookup_child_selector (exp->gdbarch, "respondsToSelector:");
if (responds_selector == 0)
- responds_selector = lookup_child_selector ("respondsTo:");
+ responds_selector
+ = lookup_child_selector (exp->gdbarch, "respondsTo:");
if (responds_selector == 0)
error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
- method_selector = lookup_child_selector ("methodForSelector:");
+ method_selector
+ = lookup_child_selector (exp->gdbarch, "methodForSelector:");
if (method_selector == 0)
- method_selector = lookup_child_selector ("methodFor:");
+ method_selector
+ = lookup_child_selector (exp->gdbarch, "methodFor:");
if (method_selector == 0)
error (_("no 'methodFor:' or 'methodForSelector:' method"));
Index: gdb-head/gdb/objc-exp.y
===================================================================
--- gdb-head.orig/gdb/objc-exp.y
+++ gdb-head/gdb/objc-exp.y
@@ -333,7 +333,8 @@ exp : '[' TYPENAME
{
CORE_ADDR class;
- class = lookup_objc_class (copy_name ($2.stoken));
+ class = lookup_objc_class (parse_gdbarch,
+ copy_name ($2.stoken));
if (class == 0)
error ("%s is not an ObjC Class",
copy_name ($2.stoken));
@@ -1748,7 +1749,7 @@ yylex ()
/* See if it's an ObjC classname. */
if (!sym)
{
- CORE_ADDR Class = lookup_objc_class(tmp);
+ CORE_ADDR Class = lookup_objc_class (parse_gdbarch, tmp);
if (Class)
{
yylval.class.class = Class;
Index: gdb-head/gdb/objc-lang.c
===================================================================
--- gdb-head.orig/gdb/objc-lang.c
+++ gdb-head/gdb/objc-lang.c
@@ -108,8 +108,9 @@ lookup_struct_typedef (char *name, struc
}
CORE_ADDR
-lookup_objc_class (char *classname)
+lookup_objc_class (struct gdbarch *gdbarch, char *classname)
{
+ struct type *char_type = builtin_type (gdbarch)->builtin_char;
struct value * function, *classval;
if (! target_has_execution)
@@ -128,15 +129,16 @@ lookup_objc_class (char *classname)
return 0;
}
- classval = value_string (classname, strlen (classname) + 1);
+ classval = value_string (classname, strlen (classname) + 1, char_type);
classval = value_coerce_array (classval);
return (CORE_ADDR) value_as_long (call_function_by_hand (function,
1, &classval));
}
CORE_ADDR
-lookup_child_selector (char *selname)
+lookup_child_selector (struct gdbarch *gdbarch, char *selname)
{
+ struct type *char_type = builtin_type (gdbarch)->builtin_char;
struct value * function, *selstring;
if (! target_has_execution)
@@ -156,53 +158,50 @@ lookup_child_selector (char *selname)
}
selstring = value_coerce_array (value_string (selname,
- strlen (selname) + 1));
+ strlen (selname) + 1, char_type));
return value_as_long (call_function_by_hand (function, 1, &selstring));
}
struct value *
-value_nsstring (char *ptr, int len)
+value_nsstring (struct gdbarch *gdbarch, char *ptr, int len)
{
+ struct type *char_type = builtin_type (gdbarch)->builtin_char;
struct value *stringValue[3];
struct value *function, *nsstringValue;
struct symbol *sym;
struct type *type;
- struct objfile *objf;
- struct gdbarch *gdbarch;
if (!target_has_execution)
return 0; /* Can't call into inferior to create NSString. */
- stringValue[2] = value_string(ptr, len);
+ stringValue[2] = value_string(ptr, len, char_type);
stringValue[2] = value_coerce_array(stringValue[2]);
/* _NSNewStringFromCString replaces "istr" after Lantern2A. */
if (lookup_minimal_symbol("_NSNewStringFromCString", 0, 0))
{
- function = find_function_in_inferior("_NSNewStringFromCString", &objf);
+ function = find_function_in_inferior("_NSNewStringFromCString", NULL);
nsstringValue = call_function_by_hand(function, 1, &stringValue[2]);
}
else if (lookup_minimal_symbol("istr", 0, 0))
{
- function = find_function_in_inferior("istr", &objf);
+ function = find_function_in_inferior("istr", NULL);
nsstringValue = call_function_by_hand(function, 1, &stringValue[2]);
}
else if (lookup_minimal_symbol("+[NSString stringWithCString:]", 0, 0))
{
function
- = find_function_in_inferior("+[NSString stringWithCString:]", &objf);
- type = builtin_type (get_objfile_arch (objf))->builtin_long;
+ = find_function_in_inferior("+[NSString stringWithCString:]", NULL);
+ type = builtin_type (gdbarch)->builtin_long;
stringValue[0] = value_from_longest
- (type, lookup_objc_class ("NSString"));
+ (type, lookup_objc_class (gdbarch, "NSString"));
stringValue[1] = value_from_longest
- (type, lookup_child_selector ("stringWithCString:"));
+ (type, lookup_child_selector (gdbarch, "stringWithCString:"));
nsstringValue = call_function_by_hand(function, 3, &stringValue[0]);
}
else
error (_("NSString: internal error -- no way to create new NSString"));
- gdbarch = get_objfile_arch (objf);
-
sym = lookup_struct_typedef("NSString", 0, 1);
if (sym == NULL)
sym = lookup_struct_typedef("NXString", 0, 1);
@@ -612,7 +611,7 @@ end_msglist(void)
selname_chain = sel->next;
msglist_len = sel->msglist_len;
msglist_sel = sel->msglist_sel;
- selid = lookup_child_selector(p);
+ selid = lookup_child_selector (parse_gdbarch, p);
if (!selid)
error (_("Can't find selector \"%s\""), p);
write_exp_elt_longcst (selid);
Index: gdb-head/gdb/objc-lang.h
===================================================================
--- gdb-head.orig/gdb/objc-lang.h
+++ gdb-head/gdb/objc-lang.h
@@ -29,8 +29,10 @@ extern int objc_parse (void); /* Define
extern void objc_error (char *); /* Defined in c-exp.y */
-extern CORE_ADDR lookup_objc_class (char *classname);
-extern CORE_ADDR lookup_child_selector (char *methodname);
+extern CORE_ADDR lookup_objc_class (struct gdbarch *gdbarch,
+ char *classname);
+extern CORE_ADDR lookup_child_selector (struct gdbarch *gdbarch,
+ char *methodname);
extern char *objc_demangle (const char *mangled, int options);
@@ -46,7 +48,8 @@ extern char *find_imps (struct symtab *s
char *method, struct symbol **syms,
unsigned int *nsym, unsigned int *ndebug);
-extern struct value *value_nsstring (char *ptr, int len);
+extern struct value *value_nsstring (struct gdbarch *gdbarch,
+ char *ptr, int len);
/* for parsing Objective C */
extern void start_msglist (void);
Index: gdb-head/gdb/valarith.c
===================================================================
--- gdb-head.orig/gdb/valarith.c
+++ gdb-head/gdb/valarith.c
@@ -624,6 +624,7 @@ value_concat (struct value *arg1, struct
char inchar;
struct type *type1 = check_typedef (value_type (arg1));
struct type *type2 = check_typedef (value_type (arg2));
+ struct type *char_type;
/* First figure out if we are dealing with two values to be concatenated
or a repeat count and a value to be repeated. INVAL1 is set to the
@@ -659,6 +660,7 @@ value_concat (struct value *arg1, struct
ptr = (char *) alloca (count * inval2len);
if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
{
+ char_type = type2;
inchar = (char) unpack_long (type2,
value_contents (inval2));
for (idx = 0; idx < count; idx++)
@@ -668,13 +670,14 @@ value_concat (struct value *arg1, struct
}
else
{
+ char_type = TYPE_TARGET_TYPE (type2);
for (idx = 0; idx < count; idx++)
{
memcpy (ptr + (idx * inval2len), value_contents (inval2),
inval2len);
}
}
- outval = value_string (ptr, count * inval2len);
+ outval = value_string (ptr, count * inval2len, char_type);
}
else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
|| TYPE_CODE (type2) == TYPE_CODE_BOOL)
@@ -700,10 +703,12 @@ value_concat (struct value *arg1, struct
ptr = (char *) alloca (inval1len + inval2len);
if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
{
+ char_type = type1;
*ptr = (char) unpack_long (type1, value_contents (inval1));
}
else
{
+ char_type = TYPE_TARGET_TYPE (type1);
memcpy (ptr, value_contents (inval1), inval1len);
}
if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
@@ -715,7 +720,7 @@ value_concat (struct value *arg1, struct
{
memcpy (ptr + inval1len, value_contents (inval2), inval2len);
}
- outval = value_string (ptr, inval1len + inval2len);
+ outval = value_string (ptr, inval1len + inval2len, char_type);
}
else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
|| TYPE_CODE (type1) == TYPE_CODE_BOOL)
Index: gdb-head/gdb/c-lang.c
===================================================================
--- gdb-head.orig/gdb/c-lang.c
+++ gdb-head/gdb/c-lang.c
@@ -951,7 +951,7 @@ evaluate_subexp_c (struct type *expect_t
if ((dest_type & C_CHAR) != 0)
result = allocate_value (type);
else
- result = value_typed_string ("", 0, type);
+ result = value_cstring ("", 0, type);
do_cleanups (cleanup);
return result;
}
@@ -971,9 +971,9 @@ evaluate_subexp_c (struct type *expect_t
/* Write the terminating character. */
for (i = 0; i < TYPE_LENGTH (type); ++i)
obstack_1grow (&output, 0);
- result = value_typed_string (obstack_base (&output),
- obstack_object_size (&output),
- type);
+ result = value_cstring (obstack_base (&output),
+ obstack_object_size (&output),
+ type);
}
do_cleanups (cleanup);
return result;
--
Dr. Ulrich Weigand
GNU Toolchain for Linux on System z and Cell BE
Ulrich.Weigand@de.ibm.com