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