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