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