gdb/python/py-lazy-string.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Python interface to lazy strings.

  2.    Copyright (C) 2010-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "python-internal.h"
  16. #include "charset.h"
  17. #include "value.h"
  18. #include "valprint.h"
  19. #include "language.h"

  20. typedef struct {
  21.   PyObject_HEAD
  22.   /*  Holds the address of the lazy string.  */
  23.   CORE_ADDR address;

  24.   /*  Holds the encoding that will be applied to the string
  25.       when the string is printed by GDB.  If the encoding is set
  26.       to None then GDB will select the most appropriate
  27.       encoding when the sting is printed.  */
  28.   char *encoding;

  29.   /* Holds the length of the string in characters.  If the
  30.      length is -1, then the string will be fetched and encoded up to
  31.      the first null of appropriate width.  */
  32.   long length;

  33.   /*  This attribute holds the type that is represented by the lazy
  34.       string's type.  */
  35.   struct type *type;
  36. } lazy_string_object;

  37. static PyTypeObject lazy_string_object_type
  38.     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("lazy_string_object");

  39. static PyObject *
  40. stpy_get_address (PyObject *self, void *closure)
  41. {
  42.   lazy_string_object *self_string = (lazy_string_object *) self;

  43.   return gdb_py_long_from_ulongest (self_string->address);
  44. }

  45. static PyObject *
  46. stpy_get_encoding (PyObject *self, void *closure)
  47. {
  48.   lazy_string_object *self_string = (lazy_string_object *) self;
  49.   PyObject *result;

  50.   /* An encoding can be set to NULL by the user, so check before
  51.      attempting a Python FromString call.  If NULL return Py_None.  */
  52.   if (self_string->encoding)
  53.     result = PyString_FromString (self_string->encoding);
  54.   else
  55.     {
  56.       result = Py_None;
  57.       Py_INCREF (result);
  58.     }

  59.   return result;
  60. }

  61. static PyObject *
  62. stpy_get_length (PyObject *self, void *closure)
  63. {
  64.   lazy_string_object *self_string = (lazy_string_object *) self;

  65.   return PyLong_FromLong (self_string->length);
  66. }

  67. static PyObject *
  68. stpy_get_type (PyObject *self, void *closure)
  69. {
  70.   lazy_string_object *str_obj = (lazy_string_object *) self;

  71.   return type_to_type_object (str_obj->type);
  72. }

  73. static PyObject *
  74. stpy_convert_to_value  (PyObject *self, PyObject *args)
  75. {
  76.   lazy_string_object *self_string = (lazy_string_object *) self;
  77.   struct value *val = NULL;
  78.   volatile struct gdb_exception except;

  79.   if (self_string->address == 0)
  80.     {
  81.       PyErr_SetString (PyExc_MemoryError,
  82.                        _("Cannot create a value from NULL."));
  83.       return NULL;
  84.     }

  85.   TRY_CATCH (except, RETURN_MASK_ALL)
  86.     {
  87.       val = value_at_lazy (self_string->type, self_string->address);
  88.     }
  89.   GDB_PY_HANDLE_EXCEPTION (except);

  90.   return value_to_value_object (val);
  91. }

  92. static void
  93. stpy_dealloc (PyObject *self)
  94. {
  95.   lazy_string_object *self_string = (lazy_string_object *) self;

  96.   xfree (self_string->encoding);
  97. }

  98. PyObject *
  99. gdbpy_create_lazy_string_object (CORE_ADDR address, long length,
  100.                            const char *encoding, struct type *type)
  101. {
  102.   lazy_string_object *str_obj = NULL;

  103.   if (address == 0 && length != 0)
  104.     {
  105.       PyErr_SetString (PyExc_MemoryError,
  106.                        _("Cannot create a lazy string with address 0x0, " \
  107.                          "and a non-zero length."));
  108.       return NULL;
  109.     }

  110.   if (!type)
  111.     {
  112.       PyErr_SetString (PyExc_RuntimeError,
  113.                        _("A lazy string's type cannot be NULL."));
  114.       return NULL;
  115.     }

  116.   str_obj = PyObject_New (lazy_string_object, &lazy_string_object_type);
  117.   if (!str_obj)
  118.     return NULL;

  119.   str_obj->address = address;
  120.   str_obj->length = length;
  121.   if (encoding == NULL || !strcmp (encoding, ""))
  122.     str_obj->encoding = NULL;
  123.   else
  124.     str_obj->encoding = xstrdup (encoding);
  125.   str_obj->type = type;

  126.   return (PyObject *) str_obj;
  127. }

  128. int
  129. gdbpy_initialize_lazy_string (void)
  130. {
  131.   if (PyType_Ready (&lazy_string_object_type) < 0)
  132.     return -1;

  133.   Py_INCREF (&lazy_string_object_type);
  134.   return 0;
  135. }

  136. /* Determine whether the printer object pointed to by OBJ is a
  137.    Python lazy string.  */
  138. int
  139. gdbpy_is_lazy_string (PyObject *result)
  140. {
  141.   return PyObject_TypeCheck (result, &lazy_string_object_type);
  142. }

  143. /* Extract the parameters from the lazy string object STRING.
  144.    ENCODING will either be set to NULL, or will be allocated with
  145.    xmalloc, in which case the callers is responsible for freeing
  146.    it.  */

  147. void
  148. gdbpy_extract_lazy_string (PyObject *string, CORE_ADDR *addr,
  149.                            struct type **str_type,
  150.                            long *length, char **encoding)
  151. {
  152.   lazy_string_object *lazy;

  153.   gdb_assert (gdbpy_is_lazy_string (string));

  154.   lazy = (lazy_string_object *) string;

  155.   *addr = lazy->address;
  156.   *str_type = lazy->type;
  157.   *length = lazy->length;
  158.   *encoding = lazy->encoding ? xstrdup (lazy->encoding) : NULL;
  159. }



  160. static PyMethodDef lazy_string_object_methods[] = {
  161.   { "value", stpy_convert_to_value, METH_NOARGS,
  162.     "Create a (lazy) value that contains a pointer to the string." },
  163.   {NULL/* Sentinel */
  164. };


  165. static PyGetSetDef lazy_string_object_getset[] = {
  166.   { "address", stpy_get_address, NULL, "Address of the string.", NULL },
  167.   { "encoding", stpy_get_encoding, NULL, "Encoding of the string.", NULL },
  168.   { "length", stpy_get_length, NULL, "Length of the string.", NULL },
  169.   { "type", stpy_get_type, NULL, "Type associated with the string.", NULL },
  170.   { NULL/* Sentinel */
  171. };

  172. static PyTypeObject lazy_string_object_type = {
  173.   PyVarObject_HEAD_INIT (NULL, 0)
  174.   "gdb.LazyString",                  /*tp_name*/
  175.   sizeof (lazy_string_object),          /*tp_basicsize*/
  176.   0,                                  /*tp_itemsize*/
  177.   stpy_dealloc,                   /*tp_dealloc*/
  178.   0,                                  /*tp_print*/
  179.   0,                                  /*tp_getattr*/
  180.   0,                                  /*tp_setattr*/
  181.   0,                                  /*tp_compare*/
  182.   0,                                  /*tp_repr*/
  183.   0,                                  /*tp_as_number*/
  184.   0,                                  /*tp_as_sequence*/
  185.   0,                                  /*tp_as_mapping*/
  186.   0,                                  /*tp_hash */
  187.   0,                                  /*tp_call*/
  188.   0,                                  /*tp_str*/
  189.   0,                                  /*tp_getattro*/
  190.   0,                                  /*tp_setattro*/
  191.   0,                                  /*tp_as_buffer*/
  192.   Py_TPFLAGS_DEFAULT,             /*tp_flags*/
  193.   "GDB lazy string object",          /* tp_doc */
  194.   0,                                  /* tp_traverse */
  195.   0,                                  /* tp_clear */
  196.   0,                                  /* tp_richcompare */
  197.   0,                                  /* tp_weaklistoffset */
  198.   0,                                  /* tp_iter */
  199.   0,                                  /* tp_iternext */
  200.   lazy_string_object_methods,          /* tp_methods */
  201.   0,                                  /* tp_members */
  202.   lazy_string_object_getset          /* tp_getset */
  203. };