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