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