Commit a5dea54b03caeef499c852f0397b61ea98caf0ec

Implement Python function objects.

Commit diff

gdb/Makefile.in

 
259259#
260260SUBDIR_PYTHON_OBS = \
261261 python.o \
262 python-function.o \
262263 python-hooks.o \
263264 python-utils.o \
264265 python-value.o
265266SUBDIR_PYTHON_SRCS = \
266267 python/python.c \
268 python/python-function.c \
267269 python/python-hooks.c \
268270 python/python-utils.c \
269271 python/python-value.c
21262126 $(value_h) $(expression_h) $(target_h) $(frame_h) $(language_h) \
21272127 $(f_lang_h) $(cp_abi_h) $(infcall_h) $(objc_lang_h) $(block_h) \
21282128 $(parser_defs_h) $(cp_support_h) $(gdb_assert_h) $(exceptions_h) \
2129 $(uiout_h) $(regcache_h)
2129 $(uiout_h) $(regcache_h) $(python_h)
21302130event-loop.o: event-loop.c $(defs_h) $(event_loop_h) $(event_top_h) \
21312131 $(gdb_string_h) $(exceptions_h) $(gdb_assert_h) $(gdb_select_h)
21322132event-top.o: event-top.c $(defs_h) $(top_h) $(inferior_h) $(target_h) \
30013001 $(value_h) $(gdbcore_h) $(command_h) $(gdbcmd_h) $(target_h) \
30023002 $(language_h) $(demangle_h) $(doublest_h) \
30033003 $(gdb_assert_h) $(regcache_h) $(block_h) $(dfp_h) $(objfiles_h) \
3004 $(python_h)
3004 $(python_h) $(cli_decode_h)
30053005varobj.o: varobj.c $(defs_h) $(exceptions_h) $(value_h) $(expression_h) \
30063006 $(frame_h) $(language_h) $(wrapper_h) $(gdbcmd_h) $(block_h) \
30073007 $(gdb_assert_h) $(gdb_string_h) $(varobj_h) $(vec_h) $(gdbthread_h) \
34313431 $(exceptions_h) $(python_internal_h) $(version_h) $(cli_script_h) \
34323432 $(ui_out_h) $(target_h) $(gdbthread_h)
34333433 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) $(srcdir)/python/python.c
3434python-function.o: $(srcdir)/python/python-function.c $(defs_h) $(python_h) \
3435 $(python_internal_h) $(frame_h) $(exceptions_h) $(symtab_h) \
3436 $(charset_h) $(stack_h) $(value_h)
3437 $(CC) -c $(INTERNAL_CFLAGS) $(PYTHON_CFLAGS) \
3438 $(srcdir)/python/python-function.c -o python-function.o
34343439python-hooks.o: $(srcdir)/python/python-hooks.c $(defs_h) $(cli_decode_h) \
34353440 $(charset_h) $(gdb_events_h) $(python_h) $(python_internal_h) \
34363441 $(observer_h)
toggle raw diff

gdb/eval.c

 
3838#include "ui-out.h"
3939#include "exceptions.h"
4040#include "regcache.h"
41#include "python/python.h"
4142
4243#include "gdb_assert.h"
4344
45#include <ctype.h>
46
4447/* This is defined in valops.c */
4548extern int overload_resolution;
4649
12961296 else
12971297 error (_("Expression of type other than \"Function returning ...\" used as function"));
12981298 }
1299 if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_INTERNAL_FUNCTION)
1300 return call_internal_function (argvec[0], nargs, argvec + 1);
1301
12991302 return call_function_by_hand (argvec[0], nargs, argvec + 1);
13001303 /* pai: FIXME save value from call_function_by_hand, then adjust pc by adjust_fn_pc if +ve */
13011304
toggle raw diff

gdb/expression.h

 
433433extern void dump_raw_expression (struct expression *, struct ui_file *, char *);
434434extern void dump_prefix_expression (struct expression *, struct ui_file *);
435435
436
436437#endif /* !defined (EXPRESSION_H) */
toggle raw diff

gdb/gdbtypes.h

 
133133
134134 TYPE_CODE_NAMESPACE, /* C++ namespace. */
135135
136 TYPE_CODE_DECFLOAT /* Decimal floating point. */
136 TYPE_CODE_DECFLOAT, /* Decimal floating point. */
137
138 /* Internal function type. */
139 TYPE_CODE_INTERNAL_FUNCTION
137140 };
138141
139142/* For now allow source to use TYPE_CODE_CLASS for C++ classes, as an
toggle raw diff

gdb/parse.c

 
305305 strings with embedded null bytes, as is required for some languages.
306306
307307 Don't be fooled by the fact that the string is null byte terminated,
308 this is strictly for the convenience of debugging gdb itself. Gdb
308 this is strictly for the convenience of debugging gdb itself.
309309 Gdb does not depend up the string being null terminated, since the
310310 actual length is recorded in expression elements at each end of the
311311 string. The null byte is taken into consideration when computing how
toggle raw diff

gdb/python/python-function.c

 
1/* gdb commands implemented in Python
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
21#include "defs.h"
22#include "value.h"
23#include "exceptions.h"
24#include "python-internal.h"
25#include "charset.h"
26#include "gdbcmd.h"
27#include "cli/cli-decode.h"
28#include "completer.h"
29#include "expression.h"
30
31static PyTypeObject fnpy_object_type =
32{
33 PyObject_HEAD_INIT (NULL)
34 0, /*ob_size*/
35 "gdb.Function", /*tp_name*/
36 sizeof (PyObject), /*tp_basicsize*/
37 0, /*tp_itemsize*/
38 0, /*tp_dealloc*/
39 0, /*tp_print*/
40 0, /*tp_getattr*/
41 0, /*tp_setattr*/
42 0, /*tp_compare*/
43 0, /*tp_repr*/
44 0, /*tp_as_number*/
45 0, /*tp_as_sequence*/
46 0, /*tp_as_mapping*/
47 0, /*tp_hash */
48 0, /*tp_call*/
49 0, /*tp_str*/
50 0, /*tp_getattro*/
51 0, /*tp_setattro*/
52 0, /*tp_as_buffer*/
53 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
54 "GDB function object", /* tp_doc */
55 0, /* tp_traverse */
56 0, /* tp_clear */
57 0, /* tp_richcompare */
58 0, /* tp_weaklistoffset */
59 0, /* tp_iter */
60 0, /* tp_iternext */
61 0 /* tp_methods */
62};
63
64
65
66static PyObject *
67convert_values_to_python (int argc, struct value **argv)
68{
69 int i;
70 PyObject *result = PyTuple_New (argc);
71 for (i = 0; i < argc; ++i)
72 {
73 PyObject *elt = value_to_value_object (argv[i]);
74 if (! elt)
75 {
76 Py_DECREF (result);
77 error ("could not convert value to Python object");
78 }
79 PyTuple_SetItem (result, i, elt);
80 }
81 return result;
82}
83
84/* Call a Python function object's invoke method. */
85
86static struct value *
87fnpy_call (void *cookie, int argc, struct value **argv)
88{
89 int i;
90 struct value *value;
91 PyObject *result, *callable, *args;
92 volatile struct gdb_exception except;
93
94 args = convert_values_to_python (argc, argv);
95
96 callable = PyObject_GetAttrString ((PyObject *) cookie, "invoke");
97 if (! callable)
98 {
99 Py_DECREF (args);
100 error ("no method named 'invoke' in object");
101 }
102
103 result = PyObject_Call (callable, args, NULL);
104 Py_DECREF (callable);
105 Py_DECREF (args);
106
107 if (!result)
108 return NULL;
109
110 TRY_CATCH (except, RETURN_MASK_ALL)
111 {
112 value = convert_value_from_python (result);
113 }
114 if (except.reason < 0)
115 {
116 Py_DECREF (result);
117 gdbpy_print_stack ();
118 throw_exception (except);
119 }
120
121 Py_DECREF (result);
122
123 return value;
124}
125
126/* Called when destroying a struct internal_function. */
127
128static void
129fnpy_destroy (void *cookie)
130{
131 Py_DECREF ((PyObject *) cookie);
132}
133
134/* Initializer for a Function object. It takes one argument, the name
135 of the function. */
136
137static int
138fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
139{
140 char *name, *docstring = NULL;
141 if (! PyArg_ParseTuple (args, "s", &name))
142 return -1;
143 Py_INCREF (self);
144
145 if (PyObject_HasAttrString (self, "__doc__"))
146 {
147 PyObject *ds_obj = PyObject_GetAttrString (self, "__doc__");
148 if (ds_obj && PyString_Check (ds_obj))
149 docstring = PyString_AsString (ds_obj);
150 }
151 if (! docstring)
152 docstring = "This command is not documented.";
153
154 add_internal_function (name, docstring, fnpy_call, self, fnpy_destroy);
155 return 0;
156}
157
158/* Initialize internal function support. */
159
160void
161gdbpy_initialize_functions (void)
162{
163 fnpy_object_type.tp_new = PyType_GenericNew;
164 fnpy_object_type.tp_init = fnpy_init;
165 if (PyType_Ready (&fnpy_object_type) < 0)
166 return;
167 Py_INCREF (&fnpy_object_type);
168 PyModule_AddObject (gdb_module, "Function", (PyObject *) &fnpy_object_type);
169}
toggle raw diff

gdb/python/python-internal.h

 
5858PyObject *gdbpy_get_hook_function (const char *);
5959
6060void gdbpy_initialize_values (void);
61void gdbpy_initialize_functions (void);
6162
6263struct cleanup *make_cleanup_py_decref (PyObject *py);
6364
toggle raw diff

gdb/python/python.c

 
410410 PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG", (char*) target_name);
411411
412412 gdbpy_initialize_values ();
413 gdbpy_initialize_functions ();
413414
414415 PyRun_SimpleString ("import gdb");
415416
toggle raw diff

gdb/python/python.h

 
2626
2727void eval_python_from_control_command (struct command_line *);
2828
29extern struct value *value_of_python (char *exp, int length);
30
2931#endif /* GDB_PYTHON_H */
toggle raw diff

gdb/valprint.c

 
259259 return 0;
260260 }
261261
262 if (TYPE_CODE (value_type (val)) == TYPE_CODE_INTERNAL_FUNCTION)
263 {
264 fprintf_filtered (stream, _("<internal function %s>"),
265 value_internal_function_name (val));
266 return 0;
267 }
268
262269 return 1;
263270}
264271
toggle raw diff

gdb/value.c

 
3636#include "block.h"
3737#include "dfp.h"
3838#include "objfiles.h"
39#include "cli/cli-decode.h"
3940
4041#include "python/python.h"
4142
4444
4545void _initialize_values (void);
4646
47/* Definition of a user function. */
48struct internal_function
49{
50 /* The name of the function. It is a bit odd to have this in the
51 function itself -- the user might use a differently-named
52 convenience variable to hold the function. */
53 char *name;
54
55 /* The handler. */
56 internal_function_fn handler;
57
58 /* User data for the handler. */
59 void *cookie;
60
61 /* Function called to destroy the cookie when the function object is
62 destroyed. */
63 void (*destroyer) (void *cookie);
64};
65
66static struct cmd_list_element *functionlist;
67
4768struct value
4869{
4970 /* Type of value; either not an lval, or one of the various
227227static struct value_history_chunk *value_history_chain;
228228
229229static int value_history_count; /* Abs number of last entry stored */
230
231/* The type of internal functions. */
232
233static struct type *internal_fn_type;
230234
231235/* List of all value objects currently allocated
232236 (except for those released by calls to release_value)
267267 return val;
268268}
269269
270/* Deallocate a value and run destructors if needed. */
271
272void
273value_free (struct value *value)
274{
275 xfree (value);
276}
277
270278/* Allocate a value that has the correct length
271279 for COUNT repetitions of type TYPE. */
272280
808808 the return value is NULL. */
809809
810810struct internalvar *
811lookup_only_internalvar (char *name)
811lookup_only_internalvar (const char *name)
812812{
813813 struct internalvar *var;
814814
824824 NAME should not normally include a dollar sign. */
825825
826826struct internalvar *
827create_internalvar (char *name)
827create_internalvar (const char *name)
828828{
829829 struct internalvar *var;
830830 var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
845845 one is created, with a void value. */
846846
847847struct internalvar *
848lookup_internalvar (char *name)
848lookup_internalvar (const char *name)
849849{
850850 struct internalvar *var;
851851
932932
933933 /* Begin code which must not call error(). If var->value points to
934934 something free'd, an error() obviously leaves a dangling pointer.
935 But we also get a danling pointer if var->value points to
935 But we also get a dangling pointer if var->value points to
936936 something in the value chain (i.e., before release_value is
937937 called), because after the error free_all_values will get called before
938938 long. */
949949 return var->name;
950950}
951951
952static struct value *
953value_create_internal_function (const char *name,
954 internal_function_fn handler,
955 void *cookie,
956 void (*destroyer) (void *))
957{
958 struct value *result = allocate_value (internal_fn_type);
959 gdb_byte *addr = value_contents_writeable (result);
960 struct internal_function **fnp = (struct internal_function **) addr;
961 /* The internal_function object is leaked here -- to make it truly
962 deletable, we would have to reference count it and add special
963 code to value_free and value_copy. The setup here is a bit odd
964 in general. It would be better to have a special case in
965 help_command. */
966 struct internal_function *ifn = XNEW (struct internal_function);
967 ifn->name = xstrdup (name);
968 ifn->handler = handler;
969 ifn->cookie = cookie;
970 ifn->destroyer = destroyer;
971 *fnp = ifn;
972 return result;
973}
974
975char *
976value_internal_function_name (struct value *val)
977{
978 gdb_byte *addr = value_contents_writeable (val);
979 struct internal_function *ifn = * (struct internal_function **) addr;
980 return ifn->name;
981}
982
983struct value *
984call_internal_function (struct value *func, int argc, struct value **argv)
985{
986 gdb_byte *addr = value_contents_writeable (func);
987 struct internal_function *ifn = * (struct internal_function **) addr;
988 return (*ifn->handler) (ifn->cookie, argc, argv);
989}
990
991/* The 'function' command. This does nothing -- it is just a
992 placeholder to let "help function NAME" work. This is also used as
993 the implementation of the sub-command that is created when
994 registering an internal function. */
995static void
996function_command (char *command, int from_tty)
997{
998 /* Do nothing. */
999}
1000
1001/* Clean up if an internal function's command is destroyed. */
1002static void
1003function_destroyer (struct cmd_list_element *self, void *ignore)
1004{
1005 xfree (self->name);
1006 xfree (self->doc);
1007}
1008
1009/* Add a new internal function. NAME is the name of the function; DOC
1010 is a documentation string describing the function. HANDLER is
1011 called when the function is invoked. COOKIE is an arbitrary
1012 pointer which is passed to HANDLER and is intended for "user data".
1013 DESTROYER is invoked when the function is destroyed. */
1014void
1015add_internal_function (const char *name, const char *doc,
1016 internal_function_fn handler,
1017 void *cookie, void (*destroyer) (void *))
1018{
1019 struct cmd_list_element *cmd;
1020 struct internalvar *var = lookup_internalvar (name);
1021 struct value *fnval = value_create_internal_function (name, handler, cookie,
1022 destroyer);
1023 release_value (fnval);
1024 set_internalvar (var, fnval);
1025
1026 cmd = add_cmd (xstrdup (name), no_class, function_command, (char *) doc,
1027 &functionlist);
1028 cmd->destroyer = function_destroyer;
1029}
1030
9521031/* Update VALUE before discarding OBJFILE. COPIED_TYPES is used to
9531032 prevent cycles / duplicates. */
9541033
19241924Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
19251925exist or does not contain a value. The EXPRESSION is not evaluated if the\n\
19261926VARIABLE is already initialized."));
1927
1928 add_prefix_cmd ("function", no_class, function_command, _("\
1929Placeholder command for showing help on convenience functions."),
1930 &functionlist, "function ", 0, &cmdlist);
1931
1932 internal_fn_type = alloc_type (NULL);
1933 TYPE_CODE (internal_fn_type) = TYPE_CODE_INTERNAL_FUNCTION;
1934 TYPE_LENGTH (internal_fn_type) = sizeof (struct internal_function *);
1935 TYPE_NAME (internal_fn_type) = "<internal function>";
19271936}
toggle raw diff

gdb/value.h

 
457457 int bitpos, int bitsize,
458458 struct value *newvalue);
459459
460extern struct internalvar *lookup_only_internalvar (char *name);
460extern struct internalvar *lookup_only_internalvar (const char *name);
461461
462extern struct internalvar *create_internalvar (char *name);
462extern struct internalvar *create_internalvar (const char *name);
463463
464extern struct internalvar *lookup_internalvar (char *name);
464extern struct internalvar *lookup_internalvar (const char *name);
465465
466466extern int value_equal (struct value *arg1, struct value *arg2);
467467
491491
492492extern int destructor_name_p (const char *name, const struct type *type);
493493
494#define value_free(val) xfree (val)
494extern void value_free (struct value *value);
495495
496496extern void free_all_values (void);
497497
576576extern struct value *value_of_local (const char *name, int complain);
577577
578578extern struct value * value_subscripted_rvalue (struct value *array, struct value *idx, int lowerbound);
579
580/* User function handler. */
581
582typedef struct value *(*internal_function_fn) (void *cookie,
583 int argc,
584 struct value **argv);
585
586void add_internal_function (const char *name, const char *doc,
587 internal_function_fn handler,
588 void *cookie, void (*destroyer) (void *));
589
590struct value *call_internal_function (struct value *function,
591 int argc, struct value **argv);
592
593char *value_internal_function_name (struct value *);
594
579595#endif /* !defined (VALUE_H) */
toggle raw diff