This is the mail archive of the archer@sourceware.org mailing list for the Archer project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Value.string method


El miÃ, 19-11-2008 a las 18:26 -0200, Thiago Jung Bauermann escribiÃ:
> I just pushed the patch below to the python branch. It provides a string
> method to gdb.Value

... except that it doesn't. :-)

This is the patch that uses the GDB machinery I created in that patch to
actually provide the string method. Just pushed.
-- 
[]'s
Thiago Jung Bauermann
IBM Linux Technology Center


commit 591a91b7b55a4aa5d9e08b44307d8d619d6b2f03
Author: Thiago Jung Bauermann <bauerman@br.ibm.com>
Date:   Wed Nov 19 18:35:31 2008 -0200

    	Add gdb.Value.string method.
    	* 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 entry for valpy_string.

diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index a03d889..88f438d 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -123,6 +123,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);
 
 /* Note that these are declared here, and not in python.h with the
diff --git a/gdb/python/python-utils.c b/gdb/python/python-utils.c
index c6cece7..86ae1b5 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 9d88e58..96a8c53 100644
--- a/gdb/python/python-value.c
+++ b/gdb/python/python-value.c
@@ -153,6 +153,39 @@ valpy_type (PyObject *self, PyObject *args)
   return type_to_type_object (NULL, value_type (value));
 }
 
+/* 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;
+  gdb_byte *buffer;
+  struct value *value = ((value_object *) self)->value;
+  volatile struct gdb_exception except;
+  PyObject *unicode;
+
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      ret = LA_GET_STRING (value, &buffer, &length);
+    }
+  GDB_PY_HANDLE_EXCEPTION (except);
+
+  if (ret != 0)
+    {
+      /* We may have read a partial string before the error happened, but
+         we will ignore it and throw an exception anyway.  */
+      PyErr_SetString (PyExc_RuntimeError, safe_strerror (ret));
+      xfree (buffer);
+
+      return NULL;
+    }
+
+  unicode = target_string_to_unicode (buffer, length);
+  xfree (buffer);
+
+  return unicode;
+}
+
 /* Cast a value to a given type.  */
 static PyObject *
 valpy_cast (PyObject *self, PyObject *args)
@@ -867,6 +900,8 @@ static PyMethodDef value_object_methods[] = {
   { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." },
   { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
   { "type", valpy_type, METH_NOARGS, "Return type of the value." },
+  { "string", valpy_string, METH_NOARGS,
+    "Return Unicode string representation of the value." },
   {NULL}  /* Sentinel */
 };
 



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]