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