]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-lazy-string.c
*** empty log message ***
[thirdparty/binutils-gdb.git] / gdb / python / py-lazy-string.c
CommitLineData
be759fcf
PM
1/* Python interface to lazy strings.
2
7b6bb8da 3 Copyright (C) 2010, 2011 Free Software Foundation, Inc.
be759fcf
PM
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "python-internal.h"
22#include "charset.h"
23#include "value.h"
24#include "exceptions.h"
25#include "valprint.h"
26#include "language.h"
09ca9e2e 27#include "gdb_assert.h"
be759fcf
PM
28
29typedef struct {
30 PyObject_HEAD
31 /* Holds the address of the lazy string. */
32 CORE_ADDR address;
33
34 /* Holds the encoding that will be applied to the string
35 when the string is printed by GDB. If the encoding is set
36 to None then GDB will select the most appropriate
37 encoding when the sting is printed. */
38 char *encoding;
39
40 /* Holds the length of the string in characters. If the
41 length is -1, then the string will be fetched and encoded up to
42 the first null of appropriate width. */
43 long length;
44
45 /* This attribute holds the type that is represented by the lazy
46 string's type. */
47 struct type *type;
48} lazy_string_object;
49
50static PyTypeObject lazy_string_object_type;
51
52static PyObject *
53stpy_get_address (PyObject *self, void *closure)
54{
55 lazy_string_object *self_string = (lazy_string_object *) self;
d59b6f6c 56
74aedc46 57 return gdb_py_long_from_ulongest (self_string->address);
be759fcf
PM
58}
59
60static PyObject *
61stpy_get_encoding (PyObject *self, void *closure)
62{
63 lazy_string_object *self_string = (lazy_string_object *) self;
64 PyObject *result;
65
66 /* An encoding can be set to NULL by the user, so check before
67 attempting a Python FromString call. If NULL return Py_None. */
68 if (self_string->encoding)
69 result = PyString_FromString (self_string->encoding);
70 else
71 {
72 result = Py_None;
73 Py_INCREF (result);
74 }
75
76 return result;
77}
78
79static PyObject *
80stpy_get_length (PyObject *self, void *closure)
81{
82 lazy_string_object *self_string = (lazy_string_object *) self;
d59b6f6c 83
be759fcf
PM
84 return PyLong_FromLong (self_string->length);
85}
86
87PyObject *
88stpy_get_type (PyObject *self, void *closure)
89{
90 lazy_string_object *str_obj = (lazy_string_object *) self;
d59b6f6c 91
be759fcf
PM
92 return type_to_type_object (str_obj->type);
93}
94
95static PyObject *
96stpy_convert_to_value (PyObject *self, PyObject *args)
97{
98 lazy_string_object *self_string = (lazy_string_object *) self;
99 struct value *val;
100
fff5cc64
PM
101 if (self_string->address == 0)
102 {
103 PyErr_SetString (PyExc_MemoryError,
044c0f87 104 _("Cannot create a value from NULL."));
fff5cc64
PM
105 return NULL;
106 }
107
be759fcf
PM
108 val = value_at_lazy (self_string->type, self_string->address);
109 return value_to_value_object (val);
110}
111
112static void
113stpy_dealloc (PyObject *self)
114{
115 lazy_string_object *self_string = (lazy_string_object *) self;
d59b6f6c 116
be759fcf
PM
117 xfree (self_string->encoding);
118}
119
120PyObject *
121gdbpy_create_lazy_string_object (CORE_ADDR address, long length,
122 const char *encoding, struct type *type)
123{
124 lazy_string_object *str_obj = NULL;
125
fff5cc64 126 if (address == 0 && length != 0)
be759fcf
PM
127 {
128 PyErr_SetString (PyExc_MemoryError,
fff5cc64
PM
129 _("Cannot create a lazy string with address 0x0, " \
130 "and a non-zero length."));
be759fcf
PM
131 return NULL;
132 }
133
134 if (!type)
135 {
136 PyErr_SetString (PyExc_RuntimeError,
044c0f87 137 _("A lazy string's type cannot be NULL."));
be759fcf
PM
138 return NULL;
139 }
140
141 str_obj = PyObject_New (lazy_string_object, &lazy_string_object_type);
142 if (!str_obj)
143 return NULL;
144
145 str_obj->address = address;
146 str_obj->length = length;
147 if (encoding == NULL || !strcmp (encoding, ""))
148 str_obj->encoding = NULL;
149 else
150 str_obj->encoding = xstrdup (encoding);
151 str_obj->type = type;
152
153 return (PyObject *) str_obj;
154}
155
156void
157gdbpy_initialize_lazy_string (void)
158{
159 if (PyType_Ready (&lazy_string_object_type) < 0)
160 return;
161
162 Py_INCREF (&lazy_string_object_type);
163}
164
165/* Determine whether the printer object pointed to by OBJ is a
166 Python lazy string. */
167int
168gdbpy_is_lazy_string (PyObject *result)
169{
170 return PyObject_TypeCheck (result, &lazy_string_object_type);
171}
172
09ca9e2e
TT
173/* Extract the parameters from the lazy string object STRING.
174 ENCODING will either be set to NULL, or will be allocated with
175 xmalloc, in which case the callers is responsible for freeing
176 it. */
be759fcf 177
09ca9e2e
TT
178void
179gdbpy_extract_lazy_string (PyObject *string, CORE_ADDR *addr,
180 struct type **str_type,
181 long *length, char **encoding)
182{
183 lazy_string_object *lazy;
be759fcf 184
09ca9e2e 185 gdb_assert (gdbpy_is_lazy_string (string));
be759fcf 186
09ca9e2e 187 lazy = (lazy_string_object *) string;
be759fcf 188
09ca9e2e
TT
189 *addr = lazy->address;
190 *str_type = lazy->type;
191 *length = lazy->length;
192 *encoding = lazy->encoding ? xstrdup (lazy->encoding) : NULL;
be759fcf
PM
193}
194
195\f
196
197static PyMethodDef lazy_string_object_methods[] = {
198 { "value", stpy_convert_to_value, METH_NOARGS,
199 "Create a (lazy) value that contains a pointer to the string." },
200 {NULL} /* Sentinel */
201};
202
203
204static PyGetSetDef lazy_string_object_getset[] = {
205 { "address", stpy_get_address, NULL, "Address of the string.", NULL },
206 { "encoding", stpy_get_encoding, NULL, "Encoding of the string.", NULL },
207 { "length", stpy_get_length, NULL, "Length of the string.", NULL },
208 { "type", stpy_get_type, NULL, "Type associated with the string.", NULL },
209 { NULL } /* Sentinel */
210};
211
212static PyTypeObject lazy_string_object_type = {
213 PyObject_HEAD_INIT (NULL)
214 0, /*ob_size*/
215 "gdb.LazyString", /*tp_name*/
216 sizeof (lazy_string_object), /*tp_basicsize*/
217 0, /*tp_itemsize*/
218 stpy_dealloc, /*tp_dealloc*/
219 0, /*tp_print*/
220 0, /*tp_getattr*/
221 0, /*tp_setattr*/
222 0, /*tp_compare*/
223 0, /*tp_repr*/
224 0, /*tp_as_number*/
225 0, /*tp_as_sequence*/
226 0, /*tp_as_mapping*/
227 0, /*tp_hash */
228 0, /*tp_call*/
229 0, /*tp_str*/
230 0, /*tp_getattro*/
231 0, /*tp_setattro*/
232 0, /*tp_as_buffer*/
233 Py_TPFLAGS_DEFAULT, /*tp_flags*/
234 "GDB lazy string object", /* tp_doc */
235 0, /* tp_traverse */
236 0, /* tp_clear */
237 0, /* tp_richcompare */
238 0, /* tp_weaklistoffset */
239 0, /* tp_iter */
240 0, /* tp_iternext */
241 lazy_string_object_methods, /* tp_methods */
242 0, /* tp_members */
243 lazy_string_object_getset /* tp_getset */
244};