C++ify xmethod_worker, get rid of VEC(xmethod_worker_ptr)
[deliverable/binutils-gdb.git] / gdb / python / py-xmethods.c
1 /* Support for debug methods in Python.
2
3 Copyright (C) 2013-2018 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 #include "py-ref.h"
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";
34 static const char get_result_type_method_name[] = "get_result_type";
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;
39
40 struct python_xmethod_worker : xmethod_worker
41 {
42 python_xmethod_worker (PyObject *worker, PyObject *this_type);
43 ~python_xmethod_worker ();
44
45 DISABLE_COPY_AND_ASSIGN (python_xmethod_worker);
46
47 /* Implementation of xmethod_worker::invoke for Python. */
48
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.
60
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. */
64
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 {
76 /* We don't do much here, but we still need the GIL. */
77 gdbpy_enter enter_py (get_current_arch (), current_language);
78
79 Py_DECREF (m_py_worker);
80 Py_DECREF (m_this_type);
81 }
82
83 /* See declaration. */
84
85 xmethod_worker_up
86 python_xmethod_worker::clone ()
87 {
88 /* We don't do much here, but we still need the GIL. */
89 gdbpy_enter enter_py (get_current_arch (), current_language);
90
91 xmethod_worker *worker = new python_xmethod_worker (m_py_worker, m_this_type);
92
93 return xmethod_worker_up (worker);
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 {
103 int enabled;
104
105 gdbpy_ref<> enabled_field (PyObject_GetAttrString (matcher,
106 enabled_field_name));
107 if (enabled_field == NULL)
108 return NULL;
109
110 enabled = PyObject_IsTrue (enabled_field.get ());
111 if (enabled == -1)
112 return NULL;
113 if (enabled == 0)
114 {
115 /* Return 'None' if the matcher is not enabled. */
116 Py_RETURN_NONE;
117 }
118
119 gdbpy_ref<> match_method (PyObject_GetAttrString (matcher,
120 match_method_name));
121 if (match_method == NULL)
122 return NULL;
123
124 gdbpy_ref<> py_xmethod_name (PyString_FromString (xmethod_name));
125 if (py_xmethod_name == NULL)
126 return NULL;
127
128 return PyObject_CallMethodObjArgs (matcher, py_match_method_name,
129 py_obj_type, py_xmethod_name.get (),
130 NULL);
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,
139 std::vector<xmethod_worker_up> *dm_vec)
140 {
141 struct objfile *objfile;
142 PyObject *py_progspace;
143
144 gdb_assert (obj_type != NULL && method_name != NULL);
145
146 gdbpy_enter enter_py (get_current_arch (), current_language);
147
148 gdbpy_ref<> py_type (type_to_type_object (obj_type));
149 if (py_type == NULL)
150 {
151 gdbpy_print_stack ();
152 return EXT_LANG_RC_ERROR;
153 }
154
155 /* Create an empty list of debug methods. */
156 gdbpy_ref<> py_xmethod_matcher_list (PyList_New (0));
157 if (py_xmethod_matcher_list == NULL)
158 {
159 gdbpy_print_stack ();
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);
169
170 if (py_objfile == NULL)
171 {
172 gdbpy_print_stack ();
173 return EXT_LANG_RC_ERROR;
174 }
175
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 ()));
179 if (temp == NULL)
180 {
181 gdbpy_print_stack ();
182 return EXT_LANG_RC_ERROR;
183 }
184
185 py_xmethod_matcher_list = std::move (temp);
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 {
193 gdbpy_ref<> pspace_matchers (pspy_get_xmethods (py_progspace, NULL));
194
195 gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
196 pspace_matchers.get ()));
197 if (temp == NULL)
198 {
199 gdbpy_print_stack ();
200 return EXT_LANG_RC_ERROR;
201 }
202
203 py_xmethod_matcher_list = std::move (temp);
204 }
205 else
206 {
207 gdbpy_print_stack ();
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 {
215 gdbpy_ref<> gdb_matchers (PyObject_GetAttrString (gdb_python_module,
216 matchers_attr_str));
217 if (gdb_matchers != NULL)
218 {
219 gdbpy_ref<> temp (PySequence_Concat (py_xmethod_matcher_list.get (),
220 gdb_matchers.get ()));
221 if (temp == NULL)
222 {
223 gdbpy_print_stack ();
224 return EXT_LANG_RC_ERROR;
225 }
226
227 py_xmethod_matcher_list = std::move (temp);
228 }
229 else
230 {
231 gdbpy_print_stack ();
232 return EXT_LANG_RC_ERROR;
233 }
234 }
235
236 gdbpy_ref<> list_iter (PyObject_GetIter (py_xmethod_matcher_list.get ()));
237 if (list_iter == NULL)
238 {
239 gdbpy_print_stack ();
240 return EXT_LANG_RC_ERROR;
241 }
242 while (true)
243 {
244 gdbpy_ref<> matcher (PyIter_Next (list_iter.get ()));
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
255 gdbpy_ref<> match_result (invoke_match_method (matcher.get (),
256 py_type.get (),
257 method_name));
258
259 if (match_result == NULL)
260 {
261 gdbpy_print_stack ();
262 return EXT_LANG_RC_ERROR;
263 }
264 if (match_result == Py_None)
265 ; /* This means there was no match. */
266 else if (PySequence_Check (match_result.get ()))
267 {
268 gdbpy_ref<> iter (PyObject_GetIter (match_result.get ()));
269
270 if (iter == NULL)
271 {
272 gdbpy_print_stack ();
273 return EXT_LANG_RC_ERROR;
274 }
275 while (true)
276 {
277 struct xmethod_worker *worker;
278
279 gdbpy_ref<> py_worker (PyIter_Next (iter.get ()));
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
290 worker = new python_xmethod_worker (py_worker.get (),
291 py_type.get ());
292
293 dm_vec->emplace_back (worker);
294 }
295 }
296 else
297 {
298 struct xmethod_worker *worker;
299
300 worker = new python_xmethod_worker (match_result.get (),
301 py_type.get ());
302 dm_vec->emplace_back (worker);
303 }
304 }
305
306 return EXT_LANG_RC_OK;
307 }
308
309 /* See declaration. */
310
311 ext_lang_rc
312 python_xmethod_worker::do_get_arg_types (int *nargs, type ***arg_types)
313 {
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);
317 struct type *obj_type;
318 int i = 1, arg_count;
319 gdbpy_ref<> list_iter;
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
325 gdbpy_ref<> get_arg_types_method
326 (PyObject_GetAttrString (m_py_worker, get_arg_types_method_name));
327 if (get_arg_types_method == NULL)
328 {
329 gdbpy_print_stack ();
330 return EXT_LANG_RC_ERROR;
331 }
332
333 gdbpy_ref<> py_argtype_list
334 (PyObject_CallMethodObjArgs (m_py_worker, py_get_arg_types_method_name,
335 NULL));
336 if (py_argtype_list == NULL)
337 {
338 gdbpy_print_stack ();
339 return EXT_LANG_RC_ERROR;
340 }
341
342 if (py_argtype_list == Py_None)
343 arg_count = 0;
344 else if (PySequence_Check (py_argtype_list.get ()))
345 {
346 arg_count = PySequence_Size (py_argtype_list.get ());
347 if (arg_count == -1)
348 {
349 gdbpy_print_stack ();
350 return EXT_LANG_RC_ERROR;
351 }
352
353 list_iter.reset (PyObject_GetIter (py_argtype_list.get ()));
354 if (list_iter == NULL)
355 {
356 gdbpy_print_stack ();
357 return EXT_LANG_RC_ERROR;
358 }
359 }
360 else
361 arg_count = 1;
362
363 /* Include the 'this' argument in the size. */
364 gdb::unique_xmalloc_ptr<struct type *> type_array
365 (XCNEWVEC (struct type *, arg_count + 1));
366 i = 1;
367 if (list_iter != NULL)
368 {
369 while (true)
370 {
371 gdbpy_ref<> item (PyIter_Next (list_iter.get ()));
372 if (item == NULL)
373 {
374 if (PyErr_Occurred ())
375 {
376 gdbpy_print_stack ();
377 return EXT_LANG_RC_ERROR;
378 }
379 break;
380 }
381
382 struct type *arg_type = type_object_to_type (item.get ());
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."));
389 return EXT_LANG_RC_ERROR;
390 }
391
392 (type_array.get ())[i] = arg_type;
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. */
400 struct type *arg_type = type_object_to_type (py_argtype_list.get ());
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."));
408 return EXT_LANG_RC_ERROR;
409 }
410 else
411 {
412 (type_array.get ())[i] = arg_type;
413 i++;
414 }
415 }
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. */
420 obj_type = type_object_to_type (m_this_type);
421 (type_array.get ())[0] = make_cv_type (1, 0, lookup_pointer_type (obj_type),
422 NULL);
423 *nargs = i;
424 *arg_types = type_array.release ();
425
426 return EXT_LANG_RC_OK;
427 }
428
429 /* See declaration. */
430
431 ext_lang_rc
432 python_xmethod_worker::do_get_result_type (value *obj, value **args, int nargs,
433 type **result_type_ptr)
434 {
435 struct type *obj_type, *this_type;
436 int i;
437
438 gdbpy_enter enter_py (get_current_arch (), current_language);
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). */
442 gdbpy_ref<> get_result_type_method
443 (PyObject_GetAttrString (m_py_worker, get_result_type_method_name));
444 if (get_result_type_method == NULL)
445 {
446 PyErr_Clear ();
447 *result_type_ptr = NULL;
448 return EXT_LANG_RC_OK;
449 }
450
451 obj_type = check_typedef (value_type (obj));
452 this_type = check_typedef (type_object_to_type (m_this_type));
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 }
460 else if (TYPE_IS_REFERENCE (obj_type))
461 {
462 struct type *this_ref
463 = lookup_reference_type (this_type, TYPE_CODE (obj_type));
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 }
473 gdbpy_ref<> py_value_obj (value_to_value_object (obj));
474 if (py_value_obj == NULL)
475 {
476 gdbpy_print_stack ();
477 return EXT_LANG_RC_ERROR;
478 }
479
480 gdbpy_ref<> py_arg_tuple (PyTuple_New (nargs + 1));
481 if (py_arg_tuple == NULL)
482 {
483 gdbpy_print_stack ();
484 return EXT_LANG_RC_ERROR;
485 }
486
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 ());
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)
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);
501 }
502
503 gdbpy_ref<> py_result_type
504 (PyObject_CallObject (get_result_type_method.get (), py_arg_tuple.get ()));
505 if (py_result_type == NULL)
506 {
507 gdbpy_print_stack ();
508 return EXT_LANG_RC_ERROR;
509 }
510
511 *result_type_ptr = type_object_to_type (py_result_type.get ());
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."));
517 gdbpy_print_stack ();
518 return EXT_LANG_RC_ERROR;
519 }
520
521 return EXT_LANG_RC_OK;
522 }
523
524 /* See declaration. */
525
526 struct value *
527 python_xmethod_worker::invoke (struct value *obj, struct value **args,
528 int nargs)
529 {
530 gdbpy_enter enter_py (get_current_arch (), current_language);
531
532 int i;
533 struct type *obj_type, *this_type;
534 struct value *res = NULL;
535
536 obj_type = check_typedef (value_type (obj));
537 this_type = check_typedef (type_object_to_type (m_this_type));
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 }
545 else if (TYPE_IS_REFERENCE (obj_type))
546 {
547 struct type *this_ref
548 = lookup_reference_type (this_type, TYPE_CODE (obj_type));
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 }
558 gdbpy_ref<> py_value_obj (value_to_value_object (obj));
559 if (py_value_obj == NULL)
560 {
561 gdbpy_print_stack ();
562 error (_("Error while executing Python code."));
563 }
564
565 gdbpy_ref<> py_arg_tuple (PyTuple_New (nargs + 1));
566 if (py_arg_tuple == NULL)
567 {
568 gdbpy_print_stack ();
569 error (_("Error while executing Python code."));
570 }
571
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 ());
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
586 PyTuple_SET_ITEM (py_arg_tuple.get (), i + 1, py_value_arg);
587 }
588
589 gdbpy_ref<> py_result (PyObject_CallObject (m_py_worker,
590 py_arg_tuple.get ()));
591 if (py_result == NULL)
592 {
593 gdbpy_print_stack ();
594 error (_("Error while executing Python code."));
595 }
596
597 if (py_result != Py_None)
598 {
599 res = convert_value_from_python (py_result.get ());
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
612 return res;
613 }
614
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)
619 {
620 gdb_assert (m_py_worker != NULL && m_this_type != NULL);
621
622 Py_INCREF (py_worker);
623 Py_INCREF (this_type);
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
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 }
This page took 0.042594 seconds and 4 git commands to generate.