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