From b6cb8e7dead0c48df80c28f95ee7543799672d11 Mon Sep 17 00:00:00 2001 From: Thiago Jung Bauermann Date: Thu, 5 Feb 2009 21:16:09 +0000 Subject: [PATCH] gdb/ 2009-02-05 Thiago Jung Bauermann Tom Tromey * python/python-utils.c (target_string_to_unicode): New function. * python/python-internal.h (target_string_to_unicode): New prototype. * python/python-value.c (valpy_string): New function. (value_object_methods): Add `string' entry. gdb/doc/ 2009-02-05 Tom Tromey * gdb.texinfo (Values From Inferior): Document Value.string. --- gdb/ChangeLog | 8 ++++++++ gdb/doc/ChangeLog | 4 ++++ gdb/doc/gdb.texinfo | 27 +++++++++++++++++++++++++++ gdb/python/python-internal.h | 1 + gdb/python/python-utils.c | 13 +++++++++++++ gdb/python/python-value.c | 33 +++++++++++++++++++++++++++++++++ 6 files changed, 86 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 7b020b1..e910679 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,11 @@ +2009-02-05 Thiago Jung Bauermann + Tom Tromey + + * python/python-utils.c (target_string_to_unicode): New function. + * python/python-internal.h (target_string_to_unicode): New prototype. + * python/python-value.c (valpy_string): New function. + (value_object_methods): Add `string' entry. + 2009-02-05 Pedro Alves * target.h (target_tid_to_str): Delete. diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog index a23fa1f..0f5363c 100644 --- a/gdb/doc/ChangeLog +++ b/gdb/doc/ChangeLog @@ -1,5 +1,9 @@ 2009-02-05 Tom Tromey + * gdb.texinfo (Values From Inferior): Document Value.string. + +2009-02-05 Tom Tromey + * gdb.texinfo (Basic Python): Document execute's from_tty argument. diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 99e5248..a2ed0b8 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -18219,6 +18219,33 @@ The result @code{bar} will be a @code{gdb.Value} object holding the value pointed to by @code{foo}. @end defmethod +@defmethod Value string @r{[}encoding @r{[}errors@r{]}@r{]} +If this @code{gdb.Value} represents a string, then this method +converts the contents to a Python string. Otherwise, this method will +throw an exception. + +Strings are recognized in a language-specific way; whether a given +@code{gdb.Value} represents a string is determined by the current +language. + +For C-like languages, a value is a string if it is a pointer to or an +array of characters or ints. The string is assumed to be terminated +by a zero of the appropriate width. + +If the optional @var{encoding} argument is given, it must be a string +naming the encoding of the string in the @code{gdb.Value}, such as +@code{"ascii"}, @code{"iso-8859-6"} or @code{"utf-8"}. It accepts +the same encodings as the corresponding argument to Python's +@code{string.decode} method, and the Python codec machinery will be used +to convert the string. If @var{encoding} is not given, or if +@var{encoding} is the empty string, then either the @code{target-charset} +(@pxref{Character Sets}) will be used, or a language-specific encoding +will be used, if the current language is able to supply one. + +The optional @var{errors} argument is the same as the corresponding +argument to Python's @code{string.decode} method. +@end defmethod + @node Interpreters @chapter Command Interpreters @cindex command interpreters diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 672d8a4..1457928 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -91,6 +91,7 @@ PyObject *python_string_to_unicode (PyObject *obj); char *unicode_to_target_string (PyObject *unicode_str); char *python_string_to_target_string (PyObject *obj); char *python_string_to_host_string (PyObject *obj); +PyObject *target_string_to_unicode (const gdb_byte *str, int length); int gdbpy_is_string (PyObject *obj); #endif /* GDB_PYTHON_INTERNAL_H */ diff --git a/gdb/python/python-utils.c b/gdb/python/python-utils.c index 6a95939..b0ec7b3 100644 --- a/gdb/python/python-utils.c +++ b/gdb/python/python-utils.c @@ -160,6 +160,19 @@ python_string_to_host_string (PyObject *obj) return unicode_to_encoded_string (str, host_charset ()); } +/* Converts a target string of LENGTH bytes in the target's charset to a + Python Unicode string. If LENGTH is -1, convert until a null byte is found. + + Returns NULL on error, with a python exception set. */ +PyObject * +target_string_to_unicode (const gdb_byte *str, int length) +{ + if (length == -1) + length = strlen (str); + + return PyUnicode_Decode (str, length, target_charset (), NULL); +} + /* Return true if OBJ is a Python string or unicode object, false otherwise. */ diff --git a/gdb/python/python-value.c b/gdb/python/python-value.c index 420d26f..bc077b6 100644 --- a/gdb/python/python-value.c +++ b/gdb/python/python-value.c @@ -143,6 +143,37 @@ valpy_address (PyObject *self, PyObject *args) return value_to_value_object (res_val); } +/* Return Unicode string with value contents (assumed to be encoded in the + target's charset). */ +static PyObject * +valpy_string (PyObject *self, PyObject *args) +{ + int length, ret = 0; + gdb_byte *buffer; + struct value *value = ((value_object *) self)->value; + volatile struct gdb_exception except; + PyObject *unicode; + const char *encoding = NULL; + const char *errors = NULL; + const char *user_encoding = NULL; + const char *la_encoding = NULL; + + if (!PyArg_ParseTuple (args, "|ss", &user_encoding, &errors)) + return NULL; + + TRY_CATCH (except, RETURN_MASK_ALL) + { + LA_GET_STRING (value, &buffer, &length, &la_encoding); + } + GDB_PY_HANDLE_EXCEPTION (except); + + encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding; + unicode = PyUnicode_Decode (buffer, length, encoding, errors); + xfree (buffer); + + return unicode; +} + static Py_ssize_t valpy_length (PyObject *self) { @@ -794,6 +825,8 @@ gdbpy_initialize_values (void) static PyMethodDef value_object_methods[] = { { "address", valpy_address, METH_NOARGS, "Return the address of the value." }, { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." }, + { "string", valpy_string, METH_VARARGS, + "Return Unicode string representation of the value." }, {NULL} /* Sentinel */ }; -- 2.7.4