py-xmethods.c revision 1.7 1 1.1 christos /* Support for debug methods in Python.
2 1.1 christos
3 1.7 christos Copyright (C) 2013-2020 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "defs.h"
21 1.1 christos #include "arch-utils.h"
22 1.1 christos #include "extension-priv.h"
23 1.1 christos #include "objfiles.h"
24 1.1 christos #include "value.h"
25 1.1 christos #include "language.h"
26 1.1 christos
27 1.1 christos #include "python.h"
28 1.1 christos #include "python-internal.h"
29 1.1 christos
30 1.1 christos static const char enabled_field_name[] = "enabled";
31 1.1 christos static const char match_method_name[] = "match";
32 1.1 christos static const char get_arg_types_method_name[] = "get_arg_types";
33 1.1 christos static const char get_result_type_method_name[] = "get_result_type";
34 1.1 christos static const char matchers_attr_str[] = "xmethods";
35 1.1 christos
36 1.1 christos static PyObject *py_match_method_name = NULL;
37 1.1 christos static PyObject *py_get_arg_types_method_name = NULL;
38 1.1 christos
39 1.6 christos struct python_xmethod_worker : xmethod_worker
40 1.1 christos {
41 1.6 christos python_xmethod_worker (PyObject *worker, PyObject *this_type);
42 1.6 christos ~python_xmethod_worker ();
43 1.6 christos
44 1.6 christos DISABLE_COPY_AND_ASSIGN (python_xmethod_worker);
45 1.1 christos
46 1.6 christos /* Implementation of xmethod_worker::invoke for Python. */
47 1.1 christos
48 1.6 christos value *invoke (value *obj, gdb::array_view<value *> args) override;
49 1.1 christos
50 1.6 christos /* Implementation of xmethod_worker::do_get_arg_types for Python. */
51 1.1 christos
52 1.6 christos ext_lang_rc do_get_arg_types (std::vector<type *> *type_args) override;
53 1.1 christos
54 1.6 christos /* Implementation of xmethod_worker::do_get_result_type for Python.
55 1.1 christos
56 1.6 christos For backward compatibility with 7.9, which did not support getting the
57 1.6 christos result type, if the get_result_type operation is not provided by WORKER
58 1.6 christos then EXT_LANG_RC_OK is returned and NULL is returned in *RESULT_TYPE. */
59 1.1 christos
60 1.6 christos ext_lang_rc do_get_result_type (value *obj, gdb::array_view<value *> args,
61 1.6 christos type **result_type_ptr) override;
62 1.1 christos
63 1.6 christos private:
64 1.1 christos
65 1.6 christos PyObject *m_py_worker;
66 1.6 christos PyObject *m_this_type;
67 1.6 christos };
68 1.1 christos
69 1.6 christos python_xmethod_worker::~python_xmethod_worker ()
70 1.6 christos {
71 1.1 christos /* We don't do much here, but we still need the GIL. */
72 1.5 christos gdbpy_enter enter_py (get_current_arch (), current_language);
73 1.1 christos
74 1.6 christos Py_DECREF (m_py_worker);
75 1.6 christos Py_DECREF (m_this_type);
76 1.1 christos }
77 1.1 christos
78 1.1 christos /* Invoke the "match" method of the MATCHER and return a new reference
79 1.1 christos to the result. Returns NULL on error. */
80 1.1 christos
81 1.1 christos static PyObject *
82 1.1 christos invoke_match_method (PyObject *matcher, PyObject *py_obj_type,
83 1.1 christos const char *xmethod_name)
84 1.1 christos {
85 1.1 christos int enabled;
86 1.1 christos
87 1.5 christos gdbpy_ref<> enabled_field (PyObject_GetAttrString (matcher,
88 1.5 christos enabled_field_name));
89 1.1 christos if (enabled_field == NULL)
90 1.5 christos return NULL;
91 1.1 christos
92 1.5 christos enabled = PyObject_IsTrue (enabled_field.get ());
93 1.1 christos if (enabled == -1)
94 1.5 christos return NULL;
95 1.1 christos if (enabled == 0)
96 1.1 christos {
97 1.1 christos /* Return 'None' if the matcher is not enabled. */
98 1.1 christos Py_RETURN_NONE;
99 1.1 christos }
100 1.1 christos
101 1.5 christos gdbpy_ref<> match_method (PyObject_GetAttrString (matcher,
102 1.5 christos match_method_name));
103 1.1 christos if (match_method == NULL)
104 1.5 christos return NULL;
105 1.1 christos
106 1.5 christos gdbpy_ref<> py_xmethod_name (PyString_FromString (xmethod_name));
107 1.1 christos if (py_xmethod_name == NULL)
108 1.5 christos return NULL;
109 1.1 christos
110 1.5 christos return PyObject_CallMethodObjArgs (matcher, py_match_method_name,
111 1.5 christos py_obj_type, py_xmethod_name.get (),
112 1.5 christos NULL);
113 1.1 christos }
114 1.1 christos
115 1.1 christos /* Implementation of get_matching_xmethod_workers for Python. */
116 1.1 christos
117 1.1 christos enum ext_lang_rc
118 1.1 christos gdbpy_get_matching_xmethod_workers
119 1.1 christos (const struct extension_language_defn *extlang,
120 1.1 christos struct type *obj_type, const char *method_name,
121 1.6 christos std::vector<xmethod_worker_up> *dm_vec)
122 1.1 christos {
123 1.1 christos gdb_assert (obj_type != NULL && method_name != NULL);
124 1.1 christos
125 1.5 christos gdbpy_enter enter_py (get_current_arch (), current_language);
126 1.1 christos
127 1.5 christos gdbpy_ref<> py_type (type_to_type_object (obj_type));
128 1.1 christos if (py_type == NULL)
129 1.1 christos {
130 1.1 christos gdbpy_print_stack ();
131 1.1 christos return EXT_LANG_RC_ERROR;
132 1.1 christos }
133 1.1 christos
134 1.1 christos /* Create an empty list of debug methods. */
135 1.5 christos gdbpy_ref<> py_xmethod_matcher_list (PyList_New (0));
136 1.1 christos if (py_xmethod_matcher_list == NULL)
137 1.1 christos {
138 1.1 christos gdbpy_print_stack ();
139 1.1 christos return EXT_LANG_RC_ERROR;
140 1.1 christos }
141 1.1 christos
142 1.1 christos /* Gather debug method matchers registered with the object files.
143 1.1 christos This could be done differently by iterating over each objfile's matcher
144 1.1 christos list individually, but there's no data yet to show it's needed. */
145 1.6 christos for (objfile *objfile : current_program_space->objfiles ())
146 1.1 christos {
147 1.6 christos gdbpy_ref<> py_objfile = objfile_to_objfile_object (objfile);
148 1.1 christos
149 1.1 christos if (py_objfile == NULL)
150 1.1 christos {
151 1.1 christos gdbpy_print_stack ();
152 1.1 christos return EXT_LANG_RC_ERROR;
153 1.1 christos }
154 1.1 christos
155 1.6 christos gdbpy_ref<> objfile_matchers (objfpy_get_xmethods (py_objfile.get (),
156 1.6 christos NULL));
157 1.5 christos gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
158 1.5 christos objfile_matchers.get ()));
159 1.5 christos if (temp == NULL)
160 1.1 christos {
161 1.1 christos gdbpy_print_stack ();
162 1.1 christos return EXT_LANG_RC_ERROR;
163 1.1 christos }
164 1.5 christos
165 1.5 christos py_xmethod_matcher_list = std::move (temp);
166 1.1 christos }
167 1.1 christos
168 1.1 christos /* Gather debug methods matchers registered with the current program
169 1.1 christos space. */
170 1.6 christos gdbpy_ref<> py_progspace = pspace_to_pspace_object (current_program_space);
171 1.1 christos if (py_progspace != NULL)
172 1.1 christos {
173 1.6 christos gdbpy_ref<> pspace_matchers (pspy_get_xmethods (py_progspace.get (),
174 1.6 christos NULL));
175 1.1 christos
176 1.5 christos gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
177 1.5 christos pspace_matchers.get ()));
178 1.5 christos if (temp == NULL)
179 1.1 christos {
180 1.1 christos gdbpy_print_stack ();
181 1.1 christos return EXT_LANG_RC_ERROR;
182 1.1 christos }
183 1.5 christos
184 1.5 christos py_xmethod_matcher_list = std::move (temp);
185 1.1 christos }
186 1.1 christos else
187 1.1 christos {
188 1.1 christos gdbpy_print_stack ();
189 1.1 christos return EXT_LANG_RC_ERROR;
190 1.1 christos }
191 1.1 christos
192 1.1 christos /* Gather debug method matchers registered globally. */
193 1.1 christos if (gdb_python_module != NULL
194 1.1 christos && PyObject_HasAttrString (gdb_python_module, matchers_attr_str))
195 1.1 christos {
196 1.5 christos gdbpy_ref<> gdb_matchers (PyObject_GetAttrString (gdb_python_module,
197 1.5 christos matchers_attr_str));
198 1.1 christos if (gdb_matchers != NULL)
199 1.1 christos {
200 1.5 christos gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
201 1.5 christos gdb_matchers.get ()));
202 1.5 christos if (temp == NULL)
203 1.1 christos {
204 1.1 christos gdbpy_print_stack ();
205 1.1 christos return EXT_LANG_RC_ERROR;
206 1.1 christos }
207 1.5 christos
208 1.5 christos py_xmethod_matcher_list = std::move (temp);
209 1.1 christos }
210 1.1 christos else
211 1.1 christos {
212 1.1 christos gdbpy_print_stack ();
213 1.1 christos return EXT_LANG_RC_ERROR;
214 1.1 christos }
215 1.1 christos }
216 1.1 christos
217 1.5 christos gdbpy_ref<> list_iter (PyObject_GetIter (py_xmethod_matcher_list.get ()));
218 1.1 christos if (list_iter == NULL)
219 1.1 christos {
220 1.1 christos gdbpy_print_stack ();
221 1.1 christos return EXT_LANG_RC_ERROR;
222 1.1 christos }
223 1.5 christos while (true)
224 1.1 christos {
225 1.5 christos gdbpy_ref<> matcher (PyIter_Next (list_iter.get ()));
226 1.5 christos if (matcher == NULL)
227 1.5 christos {
228 1.5 christos if (PyErr_Occurred ())
229 1.5 christos {
230 1.5 christos gdbpy_print_stack ();
231 1.5 christos return EXT_LANG_RC_ERROR;
232 1.5 christos }
233 1.5 christos break;
234 1.5 christos }
235 1.5 christos
236 1.5 christos gdbpy_ref<> match_result (invoke_match_method (matcher.get (),
237 1.5 christos py_type.get (),
238 1.5 christos method_name));
239 1.1 christos
240 1.1 christos if (match_result == NULL)
241 1.1 christos {
242 1.1 christos gdbpy_print_stack ();
243 1.1 christos return EXT_LANG_RC_ERROR;
244 1.1 christos }
245 1.1 christos if (match_result == Py_None)
246 1.1 christos ; /* This means there was no match. */
247 1.5 christos else if (PySequence_Check (match_result.get ()))
248 1.1 christos {
249 1.5 christos gdbpy_ref<> iter (PyObject_GetIter (match_result.get ()));
250 1.1 christos
251 1.1 christos if (iter == NULL)
252 1.1 christos {
253 1.1 christos gdbpy_print_stack ();
254 1.1 christos return EXT_LANG_RC_ERROR;
255 1.1 christos }
256 1.5 christos while (true)
257 1.1 christos {
258 1.1 christos struct xmethod_worker *worker;
259 1.1 christos
260 1.5 christos gdbpy_ref<> py_worker (PyIter_Next (iter.get ()));
261 1.5 christos if (py_worker == NULL)
262 1.5 christos {
263 1.5 christos if (PyErr_Occurred ())
264 1.5 christos {
265 1.5 christos gdbpy_print_stack ();
266 1.5 christos return EXT_LANG_RC_ERROR;
267 1.5 christos }
268 1.5 christos break;
269 1.5 christos }
270 1.5 christos
271 1.6 christos worker = new python_xmethod_worker (py_worker.get (),
272 1.5 christos py_type.get ());
273 1.6 christos
274 1.6 christos dm_vec->emplace_back (worker);
275 1.1 christos }
276 1.1 christos }
277 1.1 christos else
278 1.1 christos {
279 1.1 christos struct xmethod_worker *worker;
280 1.1 christos
281 1.6 christos worker = new python_xmethod_worker (match_result.get (),
282 1.5 christos py_type.get ());
283 1.6 christos dm_vec->emplace_back (worker);
284 1.1 christos }
285 1.1 christos }
286 1.1 christos
287 1.1 christos return EXT_LANG_RC_OK;
288 1.1 christos }
289 1.1 christos
290 1.6 christos /* See declaration. */
291 1.1 christos
292 1.6 christos ext_lang_rc
293 1.6 christos python_xmethod_worker::do_get_arg_types (std::vector<type *> *arg_types)
294 1.1 christos {
295 1.5 christos /* The gdbpy_enter object needs to be placed first, so that it's the last to
296 1.5 christos be destroyed. */
297 1.5 christos gdbpy_enter enter_py (get_current_arch (), current_language);
298 1.5 christos struct type *obj_type;
299 1.1 christos int i = 1, arg_count;
300 1.5 christos gdbpy_ref<> list_iter;
301 1.1 christos
302 1.5 christos gdbpy_ref<> get_arg_types_method
303 1.6 christos (PyObject_GetAttrString (m_py_worker, get_arg_types_method_name));
304 1.1 christos if (get_arg_types_method == NULL)
305 1.1 christos {
306 1.1 christos gdbpy_print_stack ();
307 1.1 christos return EXT_LANG_RC_ERROR;
308 1.1 christos }
309 1.1 christos
310 1.5 christos gdbpy_ref<> py_argtype_list
311 1.6 christos (PyObject_CallMethodObjArgs (m_py_worker, py_get_arg_types_method_name,
312 1.5 christos NULL));
313 1.1 christos if (py_argtype_list == NULL)
314 1.1 christos {
315 1.1 christos gdbpy_print_stack ();
316 1.1 christos return EXT_LANG_RC_ERROR;
317 1.1 christos }
318 1.5 christos
319 1.1 christos if (py_argtype_list == Py_None)
320 1.1 christos arg_count = 0;
321 1.5 christos else if (PySequence_Check (py_argtype_list.get ()))
322 1.1 christos {
323 1.5 christos arg_count = PySequence_Size (py_argtype_list.get ());
324 1.1 christos if (arg_count == -1)
325 1.1 christos {
326 1.1 christos gdbpy_print_stack ();
327 1.1 christos return EXT_LANG_RC_ERROR;
328 1.1 christos }
329 1.1 christos
330 1.5 christos list_iter.reset (PyObject_GetIter (py_argtype_list.get ()));
331 1.1 christos if (list_iter == NULL)
332 1.1 christos {
333 1.1 christos gdbpy_print_stack ();
334 1.1 christos return EXT_LANG_RC_ERROR;
335 1.1 christos }
336 1.1 christos }
337 1.1 christos else
338 1.1 christos arg_count = 1;
339 1.1 christos
340 1.1 christos /* Include the 'this' argument in the size. */
341 1.6 christos arg_types->resize (arg_count + 1);
342 1.1 christos i = 1;
343 1.1 christos if (list_iter != NULL)
344 1.1 christos {
345 1.5 christos while (true)
346 1.1 christos {
347 1.5 christos gdbpy_ref<> item (PyIter_Next (list_iter.get ()));
348 1.5 christos if (item == NULL)
349 1.5 christos {
350 1.5 christos if (PyErr_Occurred ())
351 1.5 christos {
352 1.5 christos gdbpy_print_stack ();
353 1.5 christos return EXT_LANG_RC_ERROR;
354 1.5 christos }
355 1.5 christos break;
356 1.5 christos }
357 1.1 christos
358 1.5 christos struct type *arg_type = type_object_to_type (item.get ());
359 1.1 christos if (arg_type == NULL)
360 1.1 christos {
361 1.1 christos PyErr_SetString (PyExc_TypeError,
362 1.1 christos _("Arg type returned by the get_arg_types "
363 1.1 christos "method of a debug method worker object is "
364 1.1 christos "not a gdb.Type object."));
365 1.5 christos return EXT_LANG_RC_ERROR;
366 1.1 christos }
367 1.1 christos
368 1.6 christos (*arg_types)[i] = arg_type;
369 1.1 christos i++;
370 1.1 christos }
371 1.1 christos }
372 1.1 christos else if (arg_count == 1)
373 1.1 christos {
374 1.1 christos /* py_argtype_list is not actually a list but a single gdb.Type
375 1.1 christos object. */
376 1.5 christos struct type *arg_type = type_object_to_type (py_argtype_list.get ());
377 1.1 christos
378 1.1 christos if (arg_type == NULL)
379 1.1 christos {
380 1.1 christos PyErr_SetString (PyExc_TypeError,
381 1.1 christos _("Arg type returned by the get_arg_types method "
382 1.1 christos "of an xmethod worker object is not a gdb.Type "
383 1.1 christos "object."));
384 1.5 christos return EXT_LANG_RC_ERROR;
385 1.1 christos }
386 1.1 christos else
387 1.1 christos {
388 1.6 christos (*arg_types)[i] = arg_type;
389 1.1 christos i++;
390 1.1 christos }
391 1.1 christos }
392 1.1 christos
393 1.1 christos /* Add the type of 'this' as the first argument. The 'this' pointer should
394 1.1 christos be a 'const' value. Hence, create a 'const' variant of the 'this' pointer
395 1.1 christos type. */
396 1.6 christos obj_type = type_object_to_type (m_this_type);
397 1.6 christos (*arg_types)[0] = make_cv_type (1, 0, lookup_pointer_type (obj_type),
398 1.6 christos NULL);
399 1.1 christos
400 1.1 christos return EXT_LANG_RC_OK;
401 1.1 christos }
402 1.1 christos
403 1.6 christos /* See declaration. */
404 1.1 christos
405 1.6 christos ext_lang_rc
406 1.6 christos python_xmethod_worker::do_get_result_type (value *obj,
407 1.6 christos gdb::array_view<value *> args,
408 1.6 christos type **result_type_ptr)
409 1.1 christos {
410 1.1 christos struct type *obj_type, *this_type;
411 1.1 christos int i;
412 1.1 christos
413 1.5 christos gdbpy_enter enter_py (get_current_arch (), current_language);
414 1.1 christos
415 1.1 christos /* First see if there is a get_result_type method.
416 1.1 christos If not this could be an old xmethod (pre 7.9.1). */
417 1.5 christos gdbpy_ref<> get_result_type_method
418 1.6 christos (PyObject_GetAttrString (m_py_worker, get_result_type_method_name));
419 1.1 christos if (get_result_type_method == NULL)
420 1.1 christos {
421 1.1 christos PyErr_Clear ();
422 1.1 christos *result_type_ptr = NULL;
423 1.1 christos return EXT_LANG_RC_OK;
424 1.1 christos }
425 1.1 christos
426 1.1 christos obj_type = check_typedef (value_type (obj));
427 1.6 christos this_type = check_typedef (type_object_to_type (m_this_type));
428 1.7 christos if (obj_type->code () == TYPE_CODE_PTR)
429 1.1 christos {
430 1.1 christos struct type *this_ptr = lookup_pointer_type (this_type);
431 1.1 christos
432 1.1 christos if (!types_equal (obj_type, this_ptr))
433 1.1 christos obj = value_cast (this_ptr, obj);
434 1.1 christos }
435 1.5 christos else if (TYPE_IS_REFERENCE (obj_type))
436 1.1 christos {
437 1.5 christos struct type *this_ref
438 1.7 christos = lookup_reference_type (this_type, obj_type->code ());
439 1.1 christos
440 1.1 christos if (!types_equal (obj_type, this_ref))
441 1.1 christos obj = value_cast (this_ref, obj);
442 1.1 christos }
443 1.1 christos else
444 1.1 christos {
445 1.1 christos if (!types_equal (obj_type, this_type))
446 1.1 christos obj = value_cast (this_type, obj);
447 1.1 christos }
448 1.5 christos gdbpy_ref<> py_value_obj (value_to_value_object (obj));
449 1.1 christos if (py_value_obj == NULL)
450 1.5 christos {
451 1.5 christos gdbpy_print_stack ();
452 1.5 christos return EXT_LANG_RC_ERROR;
453 1.5 christos }
454 1.1 christos
455 1.6 christos gdbpy_ref<> py_arg_tuple (PyTuple_New (args.size () + 1));
456 1.1 christos if (py_arg_tuple == NULL)
457 1.5 christos {
458 1.5 christos gdbpy_print_stack ();
459 1.5 christos return EXT_LANG_RC_ERROR;
460 1.5 christos }
461 1.1 christos
462 1.5 christos /* PyTuple_SET_ITEM steals the reference of the element, hence the
463 1.5 christos release. */
464 1.5 christos PyTuple_SET_ITEM (py_arg_tuple.get (), 0, py_value_obj.release ());
465 1.1 christos
466 1.6 christos for (i = 0; i < args.size (); i++)
467 1.1 christos {
468 1.1 christos PyObject *py_value_arg = value_to_value_object (args[i]);
469 1.1 christos
470 1.1 christos if (py_value_arg == NULL)
471 1.5 christos {
472 1.5 christos gdbpy_print_stack ();
473 1.5 christos return EXT_LANG_RC_ERROR;
474 1.5 christos }
475 1.5 christos PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg);
476 1.1 christos }
477 1.1 christos
478 1.5 christos gdbpy_ref<> py_result_type
479 1.5 christos (PyObject_CallObject (get_result_type_method.get (), py_arg_tuple.get ()));
480 1.1 christos if (py_result_type == NULL)
481 1.5 christos {
482 1.5 christos gdbpy_print_stack ();
483 1.5 christos return EXT_LANG_RC_ERROR;
484 1.5 christos }
485 1.1 christos
486 1.5 christos *result_type_ptr = type_object_to_type (py_result_type.get ());
487 1.1 christos if (*result_type_ptr == NULL)
488 1.1 christos {
489 1.1 christos PyErr_SetString (PyExc_TypeError,
490 1.1 christos _("Type returned by the get_result_type method of an"
491 1.1 christos " xmethod worker object is not a gdb.Type object."));
492 1.5 christos gdbpy_print_stack ();
493 1.5 christos return EXT_LANG_RC_ERROR;
494 1.1 christos }
495 1.1 christos
496 1.1 christos return EXT_LANG_RC_OK;
497 1.1 christos }
498 1.1 christos
499 1.6 christos /* See declaration. */
500 1.1 christos
501 1.1 christos struct value *
502 1.6 christos python_xmethod_worker::invoke (struct value *obj,
503 1.6 christos gdb::array_view<value *> args)
504 1.1 christos {
505 1.6 christos gdbpy_enter enter_py (get_current_arch (), current_language);
506 1.6 christos
507 1.1 christos int i;
508 1.1 christos struct type *obj_type, *this_type;
509 1.1 christos struct value *res = NULL;
510 1.1 christos
511 1.1 christos obj_type = check_typedef (value_type (obj));
512 1.6 christos this_type = check_typedef (type_object_to_type (m_this_type));
513 1.7 christos if (obj_type->code () == TYPE_CODE_PTR)
514 1.1 christos {
515 1.1 christos struct type *this_ptr = lookup_pointer_type (this_type);
516 1.1 christos
517 1.1 christos if (!types_equal (obj_type, this_ptr))
518 1.1 christos obj = value_cast (this_ptr, obj);
519 1.1 christos }
520 1.5 christos else if (TYPE_IS_REFERENCE (obj_type))
521 1.1 christos {
522 1.5 christos struct type *this_ref
523 1.7 christos = lookup_reference_type (this_type, obj_type->code ());
524 1.1 christos
525 1.1 christos if (!types_equal (obj_type, this_ref))
526 1.1 christos obj = value_cast (this_ref, obj);
527 1.1 christos }
528 1.1 christos else
529 1.1 christos {
530 1.1 christos if (!types_equal (obj_type, this_type))
531 1.1 christos obj = value_cast (this_type, obj);
532 1.1 christos }
533 1.5 christos gdbpy_ref<> py_value_obj (value_to_value_object (obj));
534 1.1 christos if (py_value_obj == NULL)
535 1.1 christos {
536 1.1 christos gdbpy_print_stack ();
537 1.1 christos error (_("Error while executing Python code."));
538 1.1 christos }
539 1.1 christos
540 1.6 christos gdbpy_ref<> py_arg_tuple (PyTuple_New (args.size () + 1));
541 1.1 christos if (py_arg_tuple == NULL)
542 1.1 christos {
543 1.1 christos gdbpy_print_stack ();
544 1.1 christos error (_("Error while executing Python code."));
545 1.1 christos }
546 1.1 christos
547 1.5 christos /* PyTuple_SET_ITEM steals the reference of the element, hence the
548 1.5 christos release. */
549 1.5 christos PyTuple_SET_ITEM (py_arg_tuple.get (), 0, py_value_obj.release ());
550 1.1 christos
551 1.6 christos for (i = 0; i < args.size (); i++)
552 1.1 christos {
553 1.1 christos PyObject *py_value_arg = value_to_value_object (args[i]);
554 1.1 christos
555 1.1 christos if (py_value_arg == NULL)
556 1.1 christos {
557 1.1 christos gdbpy_print_stack ();
558 1.1 christos error (_("Error while executing Python code."));
559 1.1 christos }
560 1.1 christos
561 1.5 christos PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg);
562 1.1 christos }
563 1.1 christos
564 1.6 christos gdbpy_ref<> py_result (PyObject_CallObject (m_py_worker,
565 1.5 christos py_arg_tuple.get ()));
566 1.1 christos if (py_result == NULL)
567 1.1 christos {
568 1.1 christos gdbpy_print_stack ();
569 1.1 christos error (_("Error while executing Python code."));
570 1.1 christos }
571 1.1 christos
572 1.1 christos if (py_result != Py_None)
573 1.1 christos {
574 1.5 christos res = convert_value_from_python (py_result.get ());
575 1.1 christos if (res == NULL)
576 1.1 christos {
577 1.1 christos gdbpy_print_stack ();
578 1.1 christos error (_("Error while executing Python code."));
579 1.1 christos }
580 1.1 christos }
581 1.1 christos else
582 1.1 christos {
583 1.7 christos res = allocate_value (lookup_typename (python_language,
584 1.1 christos "void", NULL, 0));
585 1.1 christos }
586 1.1 christos
587 1.1 christos return res;
588 1.1 christos }
589 1.1 christos
590 1.6 christos python_xmethod_worker::python_xmethod_worker (PyObject *py_worker,
591 1.6 christos PyObject *this_type)
592 1.6 christos : xmethod_worker (&extension_language_python),
593 1.6 christos m_py_worker (py_worker), m_this_type (this_type)
594 1.1 christos {
595 1.6 christos gdb_assert (m_py_worker != NULL && m_this_type != NULL);
596 1.1 christos
597 1.1 christos Py_INCREF (py_worker);
598 1.1 christos Py_INCREF (this_type);
599 1.1 christos }
600 1.1 christos
601 1.1 christos int
602 1.1 christos gdbpy_initialize_xmethods (void)
603 1.1 christos {
604 1.1 christos py_match_method_name = PyString_FromString (match_method_name);
605 1.1 christos if (py_match_method_name == NULL)
606 1.1 christos return -1;
607 1.1 christos
608 1.1 christos py_get_arg_types_method_name
609 1.1 christos = PyString_FromString (get_arg_types_method_name);
610 1.1 christos if (py_get_arg_types_method_name == NULL)
611 1.1 christos return -1;
612 1.1 christos
613 1.1 christos return 1;
614 1.1 christos }
615