System notice: In light of the Debian OpenSSL security issue we've regenerated the server keys. See this thread for instructions and the new key fingerprints.

Commit 76c4268be10ff1591223c5b78da5931a96647ae8

Export values to Python.

Commit diff

gdb/Makefile.in

 
261261# python sub directory definitons
262262#
263263SUBDIR_PYTHON_OBS = \
264 python.o
264 python.o \
265 python-value.o
265266SUBDIR_PYTHON_SRCS = \
266 python/python.c
267 python/python.c \
268 python/value.c
267269SUBDIR_PYTHON_DEPS =
268270SUBDIR_PYTHON_LDFLAGS=
269271SUBDIR_PYTHON_CFLAGS=
29842984value.o: value.c $(defs_h) $(gdb_string_h) $(symtab_h) $(gdbtypes_h) \
29852985 $(value_h) $(gdbcore_h) $(command_h) $(gdbcmd_h) $(target_h) \
29862986 $(language_h) $(demangle_h) $(doublest_h) \
2987 $(gdb_assert_h) $(regcache_h) $(block_h) $(dfp_h)
2987 $(gdb_assert_h) $(regcache_h) $(block_h) $(dfp_h) $(python_h)
29882988varobj.o: varobj.c $(defs_h) $(exceptions_h) $(value_h) $(expression_h) \
29892989 $(frame_h) $(language_h) $(wrapper_h) $(gdbcmd_h) $(block_h) \
29902990 $(gdb_assert_h) $(gdb_string_h) $(varobj_h) $(vec_h) $(gdbthread_h) \
34133413 $(command_h) $(libiberty_h) $(cli_decode_h) $(charset_h) $(top_h) \
34143414 $(exceptions_h) $(python_internal_h) $(version_h)
34153415 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) $(srcdir)/python/python.c
3416python-value.o: $(srcdir)/python/value.c $(defs_h) $(exceptions_h) \
3417 $(python_internal_h) $(value_h)
3418 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
3419 $(srcdir)/python/value.c -o python-value.o
34163420
34173421### end of the gdb Makefile.in.
toggle raw diff

gdb/python/python-internal.h

 
2727#endif
2828
2929extern PyObject *gdb_module;
30extern PyTypeObject value_object_type;
31
32PyObject *gdbpy_make_value_from_int (PyObject *self, PyObject *args);
33PyObject *gdbpy_get_value_from_history (PyObject *self, PyObject *args);
34
35PyObject *value_to_value_object (struct value *v);
36PyObject *gdb_owned_value_to_value_object (struct value *v);
37
38struct value *value_object_to_value (PyObject *self);
39
40void gdbpy_initialize_values (void);
3041
3142/* Use this after a TRY_EXCEPT to throw the appropriate Python
3243 exception. FIXME: throw different errors depending on
toggle raw diff

gdb/python/python.c

 
4444 if (!initialized)
4545 {
4646 static PyMethodDef GdbMethods[] = {
47 { "make_value_from_int", gdbpy_make_value_from_int, METH_VARARGS,
48 "Make a value from int" },
49 { "get_value_from_history", gdbpy_get_value_from_history, METH_VARARGS,
50 "Get a value from history" },
4751 { "execute", execute_gdb_command, METH_VARARGS,
4852 "Execute a gdb command" },
4953 { "show", get_show_variable, METH_VARARGS,
6464 PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", host_name);
6565 PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG", target_name);
6666
67 gdbpy_initialize_values ();
68
6769 PyRun_SimpleString ("import gdb");
6870
6971 initialized = 1;
toggle raw diff

gdb/python/python.h

 
2424#include "value.h"
2525#include "python-internal.h"
2626
27extern struct value *values_in_python;
28
2729void eval_python_from_control_command (struct command_line *);
2830
2931#endif /* GDB_PYTHON_H */
toggle raw diff

gdb/python/value.c

 
1/* Python interface to values.
2
3 Copyright (C) 2008 Free Software Foundation, Inc.
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 "value.h"
22#include "exceptions.h"
23#include "python-internal.h"
24
25typedef struct {
26 PyObject_HEAD
27 struct value *value;
28 int owned_by_gdb;
29} value_object;
30
31/* List of all values which are currently exposed to Python. It is maintained
32 so that when an objfile is discarded, preserve_values can copy the values'
33 types if needed. */
34struct value *values_in_python;
35
36static void valpy_dealloc (PyObject *obj);
37static PyObject *valpy_dereference (PyObject *self, PyObject *args);
38static PyObject *valpy_get_element (PyObject *self, PyObject *args);
39static PyObject *valpy_str (PyObject *self);
40static PyObject *valpy_increment (PyObject *self, PyObject *args);
41static PyObject *valpy_equal_p (PyObject *self, PyObject *args);
42static PyObject * valpy_lazy_p (PyObject *self, PyObject *args);
43static PyObject * valpy_fetch_lazy (PyObject *self, PyObject *args);
44static PyObject * valpy_common_print (PyObject *self, PyObject *args);
45
46static PyMethodDef value_object_methods[] = {
47 { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
48 { "get_element", valpy_get_element, METH_VARARGS,
49 "Obtains element inside value." },
50 { "increment", valpy_increment, METH_VARARGS,
51 "Increment value by the given amount." },
52 { "equals", valpy_equal_p, METH_VARARGS, "Compare values." },
53 { "is_lazy", valpy_lazy_p, METH_NOARGS,
54 "Returns True if the value is lazy, False if not." },
55 { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS,
56 "Fetches value from inferior memory." },
57 { "common_print", valpy_common_print, METH_VARARGS, "Prints value." },
58 {NULL} /* Sentinel */
59};
60
61PyTypeObject value_object_type = {
62 PyObject_HEAD_INIT (NULL)
63 0, /*ob_size*/
64 "gdb.Value", /*tp_name*/
65 sizeof (value_object), /*tp_basicsize*/
66 0, /*tp_itemsize*/
67 valpy_dealloc, /*tp_dealloc*/
68 0, /*tp_print*/
69 0, /*tp_getattr*/
70 0, /*tp_setattr*/
71 0, /*tp_compare*/
72 0, /*tp_repr*/
73 0, /*tp_as_number*/
74 0, /*tp_as_sequence*/
75 0, /*tp_as_mapping*/
76 0, /*tp_hash */
77 0, /*tp_call*/
78 valpy_str, /*tp_str*/
79 0, /*tp_getattro*/
80 0, /*tp_setattro*/
81 0, /*tp_as_buffer*/
82 Py_TPFLAGS_DEFAULT, /*tp_flags*/
83 "GDB value object", /* tp_doc */
84 0, /* tp_traverse */
85 0, /* tp_clear */
86 0, /* tp_richcompare */
87 0, /* tp_weaklistoffset */
88 0, /* tp_iter */
89 0, /* tp_iternext */
90 value_object_methods /* tp_methods */
91};
92
93/* Mantra used to return value object wrapping value struct. */
94#define VALPY_RETURN_VALUE(valpy_obj, value_st) \
95 do { \
96 valpy_obj = PyObject_New (value_object, &value_object_type); \
97 if (valpy_obj != NULL) \
98 { \
99 valpy_obj->value = value_st; \
100 release_value (value_st); \
101 value_prepend_to_list (&values_in_python, value_st); \
102 } \
103 return (PyObject *) valpy_obj; \
104 } while (0)
105
106
107static void
108valpy_dealloc (PyObject *obj)
109{
110 value_object *self = (value_object *) obj;
111
112 value_remove_from_list (&values_in_python, self->value);
113
114 if (!self->owned_by_gdb)
115 value_free (self->value);
116 self->ob_type->tp_free (self);
117}
118
119static PyObject *
120valpy_dereference (PyObject *self, PyObject *args)
121{
122 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
123 value_object *result;
124 volatile struct gdb_exception except;
125
126 TRY_CATCH (except, RETURN_MASK_ALL)
127 {
128 res_val = value_ind (((value_object *) self)->value);
129 }
130 GDB_PY_HANDLE_EXCEPTION (except);
131
132 VALPY_RETURN_VALUE (result, res_val);
133}
134
135/* Takes value and string name of element inside that value. */
136static PyObject *
137valpy_get_element (PyObject *self, PyObject *args)
138{
139 value_object *self_value = (value_object *) self;
140 char *field;
141 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
142 value_object *result;
143 volatile struct gdb_exception except;
144
145 if (!PyArg_ParseTuple (args, "s", &field))
146 return NULL;
147
148 TRY_CATCH (except, RETURN_MASK_ALL)
149 {
150 res_val = value_struct_elt (&self_value->value, NULL, field, 0, NULL);
151 }
152 GDB_PY_HANDLE_EXCEPTION (except);
153
154 VALPY_RETURN_VALUE (result, res_val);
155}
156
157/* FIXME: copy paste from varobj.c */
158static char *
159value_get_print_value (struct value *value)
160{
161 long dummy;
162 struct ui_file *stb;
163 struct cleanup *old_chain;
164 char *thevalue;
165
166 if (value == NULL)
167 return NULL;
168
169 stb = mem_fileopen ();
170 old_chain = make_cleanup_ui_file_delete (stb);
171
172 common_val_print (value, stb, 0, 1, 0, 0);
173 thevalue = ui_file_xstrdup (stb, &dummy);
174
175 do_cleanups (old_chain);
176 return thevalue;
177}
178
179static PyObject *
180valpy_str (PyObject *self)
181{
182 char *s;
183 PyObject *result;
184
185 s = value_get_print_value (((value_object *) self)->value);
186 result = PyString_FromString (s);
187 xfree (s);
188
189 return result;
190}
191
192static PyObject *
193valpy_increment (PyObject *self, PyObject *args)
194{
195 int i;
196 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
197 struct value *inc;
198 value_object *result;
199 volatile struct gdb_exception except;
200
201 if (!PyArg_ParseTuple (args, "i", &i))
202 return NULL;
203
204 TRY_CATCH (except, RETURN_MASK_ALL)
205 {
206 /* FIXME: clear everything. */
207 inc = value_from_longest (builtin_type_int, i);
208 res_val = value_add (((value_object *) self)->value, inc);
209 }
210 GDB_PY_HANDLE_EXCEPTION (except);
211
212 VALPY_RETURN_VALUE (result, res_val);
213}
214
215static PyObject *
216valpy_equal_p (PyObject *self, PyObject *args)
217{
218 int equalp = 0; /* Initialize to appease gcc warning. */
219 value_object *other;
220 volatile struct gdb_exception except;
221
222 if (!PyArg_ParseTuple (args, "O!", &value_object_type, &other))
223 return NULL;
224
225 TRY_CATCH (except, RETURN_MASK_ALL)
226 {
227 equalp = value_equal (((value_object *) self)->value, other->value);
228 }
229 GDB_PY_HANDLE_EXCEPTION (except);
230
231 if (equalp == 1)
232 Py_RETURN_TRUE;
233
234 Py_RETURN_FALSE;
235}
236
237static PyObject *
238valpy_lazy_p (PyObject *self, PyObject *args)
239{
240 if (value_lazy (((value_object *) self)->value))
241 Py_RETURN_TRUE;
242
243 Py_RETURN_FALSE;
244}
245
246static PyObject *
247valpy_fetch_lazy (PyObject *self, PyObject *args)
248{
249 volatile struct gdb_exception except;
250
251 TRY_CATCH (except, RETURN_MASK_ALL)
252 {
253 value_fetch_lazy (((value_object *) self)->value);
254 }
255 GDB_PY_HANDLE_EXCEPTION (except);
256
257 Py_RETURN_NONE;
258}
259
260static PyObject *
261valpy_common_print (PyObject *self, PyObject *args)
262{
263 value_object *self_value = (value_object *) self;
264 int ret = 0, format, recurse, pretty;
265 PyObject *format_obj, *deref_obj;
266 struct ui_stream *stb;
267 struct cleanup *chain;
268 volatile struct gdb_exception except;
269
270 if (!PyArg_ParseTuple (args, "OO!ii", &format_obj, &PyBool_Type, &deref_obj,
271 &recurse, &pretty))
272 return NULL;
273
274 if (format_obj == Py_None)
275 format = 0;
276 else
277 {
278 if (!PyString_Check (format_obj) || PyString_Size (format_obj) != 1)
279 {
280 PyErr_SetString (PyExc_TypeError, "Argument 1 must be char or None.");
281 return NULL;
282 }
283
284 format = *PyString_AsString (format_obj);
285 }
286
287 stb = ui_out_stream_new (uiout);
288 chain = make_cleanup_ui_out_stream_delete (stb);
289
290 TRY_CATCH (except, RETURN_MASK_ALL)
291 {
292 ret = common_val_print (self_value->value, stb->stream, format,
293 deref_obj == Py_True, recurse, pretty);
294 /* FIXME: should I really use ui_out_field_stream? */
295 ui_out_field_stream (uiout, "value", stb);
296 }
297 GDB_PY_HANDLE_EXCEPTION (except);
298
299 do_cleanups (chain);
300
301 return PyInt_FromLong (ret);
302}
303
304/* A value owned by GDB is in the all_values chain, so it will be freed
305 automatically when not needed anymore (i.e., before the current command
306 completes). */
307PyObject *
308gdb_owned_value_to_value_object (struct value *v)
309{
310 value_object *result = PyObject_New (value_object, &value_object_type);
311 if (result != NULL)
312 {
313 result->value = v;
314 result->owned_by_gdb = 1;
315 /* FIXME: should we do it? What is it? */
316 /* I don't think it is needed, since a GDB owned value has a very short
317 lifetime. The purpose of the list is explained in the comment above
318 its declaration. -- bauermann */
319 value_prepend_to_list (&values_in_python, v);
320 }
321 return (PyObject *)result;
322}
323
324/* Returns an object for a value which is released from the all_values chain,
325 so its lifetime is not bound to the execution of a command. */
326PyObject *
327value_to_value_object (struct value *v)
328{
329 value_object *val_obj;
330
331 VALPY_RETURN_VALUE (val_obj, v);
332}
333
334struct value *
335value_object_to_value (PyObject *self)
336{
337 value_object *real = (value_object *)self;
338 return real->value;
339}
340
341PyObject *
342gdbpy_make_value_from_int (PyObject *self, PyObject *args)
343{
344 int i;
345 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
346 value_object *result;
347 volatile struct gdb_exception except;
348
349 if (!PyArg_ParseTuple (args, "i", &i))
350 return NULL;
351
352 TRY_CATCH (except, RETURN_MASK_ALL)
353 {
354 res_val = value_from_longest (builtin_type_int, i);
355 }
356 GDB_PY_HANDLE_EXCEPTION (except);
357
358 VALPY_RETURN_VALUE (result, res_val);
359}
360
361PyObject *
362gdbpy_get_value_from_history (PyObject *self, PyObject *args)
363{
364 int i;
365 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
366 value_object *result;
367 volatile struct gdb_exception except;
368
369 if (!PyArg_ParseTuple (args, "i", &i))
370 return NULL;
371
372 TRY_CATCH (except, RETURN_MASK_ALL)
373 {
374 res_val = access_value_history (i);
375 }
376 GDB_PY_HANDLE_EXCEPTION (except);
377
378 VALPY_RETURN_VALUE (result, res_val);
379}
380
381void
382gdbpy_initialize_values (void)
383{
384 value_object_type.tp_new = PyType_GenericNew;
385 if (PyType_Ready (&value_object_type) < 0)
386 return;
387
388 Py_INCREF (&value_object_type);
389 PyModule_AddObject (gdb_module, "Value", (PyObject *) &value_object_type);
390
391 values_in_python = NULL;
392}
toggle raw diff

gdb/value.c

 
3636#include "block.h"
3737#include "dfp.h"
3838
39#ifdef HAVE_PYTHON
40#include "python/python.h"
41#endif
42
3943/* Prototypes for exported functions. */
4044
4145void _initialize_values (void);
133133
134134 /* Values are stored in a chain, so that they can be deleted easily
135135 over calls to the inferior. Values assigned to internal
136 variables or put into the value history are taken off this
137 list. */
136 variables, put into the value history or exposed to Python are
137 taken off this list. */
138138 struct value *next;
139139
140140 /* Register number if the value is from a register. */
260260 type, range_type));
261261}
262262
263/* Needed if another module needs to maintain its on list of values. */
264void
265value_prepend_to_list (struct value **head, struct value *val)
266{
267 val->next = *head;
268 *head = val;
269}
270
271/* Needed if another module needs to maintain its on list of values. */
272void
273value_remove_from_list (struct value **head, struct value *val)
274{
275 struct value *prev;
276
277 if (*head == val)
278 *head = (*head)->next;
279 else
280 for (prev = *head; prev->next; prev = prev->next)
281 if (prev->next == val)
282 {
283 prev->next = val->next;
284 break;
285 }
286}
287
263288/* Accessor methods. */
264289
265290struct value *
944944 htab_t copied_types;
945945 struct value_history_chunk *cur;
946946 struct internalvar *var;
947 struct value *val;
947948 int i;
948949
949950 /* Create the hash table. We allocate on the objfile's obstack, since
959959 for (var = internalvars; var; var = var->next)
960960 preserve_one_value (var->value, objfile, copied_types);
961961
962#ifdef HAVE_PYTHON
963 for (val = values_in_python; val; val = val->next)
964 preserve_one_value (val, objfile, copied_types);
965#endif
966
962967 htab_delete (copied_types);
963968}
964969
toggle raw diff

gdb/value.h

 
3939
4040struct value;
4141
42/* Needed if another module needs to maintain its on list of values. */
43
44void value_prepend_to_list (struct value **head, struct value *val);
45void value_remove_from_list (struct value **head, struct value *val);
46
4247/* Values are stored in a chain, so that they can be deleted easily
43 over calls to the inferior. Values assigned to internal variables
44 or put into the value history are taken off this list. */
48 over calls to the inferior. Values assigned to internal variables,
49 put into the value history or exposed to Python are taken off this
50 list. */
4551
4652struct value *value_next (struct value *);
4753
toggle raw diff