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