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