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