1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
|
/* Support for debug methods in Python.
Copyright (C) 2013-2016 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "arch-utils.h"
#include "extension-priv.h"
#include "objfiles.h"
#include "value.h"
#include "language.h"
#include "python.h"
#include "python-internal.h"
static const char enabled_field_name[] = "enabled";
static const char match_method_name[] = "match";
static const char get_arg_types_method_name[] = "get_arg_types";
static const char get_result_type_method_name[] = "get_result_type";
static const char matchers_attr_str[] = "xmethods";
static PyObject *py_match_method_name = NULL;
static PyObject *py_get_arg_types_method_name = NULL;
struct gdbpy_worker_data
{
PyObject *worker;
PyObject *this_type;
};
static struct xmethod_worker *new_python_xmethod_worker (PyObject *item,
PyObject *py_obj_type);
/* Implementation of free_xmethod_worker_data for Python. */
void
gdbpy_free_xmethod_worker_data (const struct extension_language_defn *extlang,
void *data)
{
struct gdbpy_worker_data *worker_data = (struct gdbpy_worker_data *) data;
struct cleanup *cleanups;
gdb_assert (worker_data->worker != NULL && worker_data->this_type != NULL);
/* We don't do much here, but we still need the GIL. */
cleanups = ensure_python_env (get_current_arch (), current_language);
Py_DECREF (worker_data->worker);
Py_DECREF (worker_data->this_type);
xfree (worker_data);
do_cleanups (cleanups);
}
/* Implementation of clone_xmethod_worker_data for Python. */
void *
gdbpy_clone_xmethod_worker_data (const struct extension_language_defn *extlang,
void *data)
{
struct gdbpy_worker_data *worker_data
= (struct gdbpy_worker_data *) data, *new_data;
struct cleanup *cleanups;
gdb_assert (worker_data->worker != NULL && worker_data->this_type != NULL);
/* We don't do much here, but we still need the GIL. */
cleanups = ensure_python_env (get_current_arch (), current_language);
new_data = XCNEW (struct gdbpy_worker_data);
new_data->worker = worker_data->worker;
new_data->this_type = worker_data->this_type;
Py_INCREF (new_data->worker);
Py_INCREF (new_data->this_type);
do_cleanups (cleanups);
return new_data;
}
/* Invoke the "match" method of the MATCHER and return a new reference
to the result. Returns NULL on error. */
static PyObject *
invoke_match_method (PyObject *matcher, PyObject *py_obj_type,
const char *xmethod_name)
{
PyObject *py_xmethod_name;
PyObject *match_method, *enabled_field, *match_result;
struct cleanup *cleanups;
int enabled;
cleanups = make_cleanup (null_cleanup, NULL);
enabled_field = PyObject_GetAttrString (matcher, enabled_field_name);
if (enabled_field == NULL)
{
do_cleanups (cleanups);
return NULL;
}
make_cleanup_py_decref (enabled_field);
enabled = PyObject_IsTrue (enabled_field);
if (enabled == -1)
{
do_cleanups (cleanups);
return NULL;
}
if (enabled == 0)
{
/* Return 'None' if the matcher is not enabled. */
do_cleanups (cleanups);
Py_RETURN_NONE;
}
match_method = PyObject_GetAttrString (matcher, match_method_name);
if (match_method == NULL)
{
do_cleanups (cleanups);
return NULL;
}
make_cleanup_py_decref (match_method);
py_xmethod_name = PyString_FromString (xmethod_name);
if (py_xmethod_name == NULL)
{
do_cleanups (cleanups);
return NULL;
}
make_cleanup_py_decref (py_xmethod_name);
match_result = PyObject_CallMethodObjArgs (matcher,
py_match_method_name,
py_obj_type,
py_xmethod_name,
NULL);
do_cleanups (cleanups);
return match_result;
}
/* Implementation of get_matching_xmethod_workers for Python. */
enum ext_lang_rc
gdbpy_get_matching_xmethod_workers
(const struct extension_language_defn *extlang,
struct type *obj_type, const char *method_name,
xmethod_worker_vec **dm_vec)
{
struct cleanup *cleanups;
struct objfile *objfile;
VEC (xmethod_worker_ptr) *worker_vec = NULL;
PyObject *py_type, *py_progspace;
PyObject *py_xmethod_matcher_list = NULL, *list_iter, *matcher;
gdb_assert (obj_type != NULL && method_name != NULL);
cleanups = ensure_python_env (get_current_arch (), current_language);
py_type = type_to_type_object (obj_type);
if (py_type == NULL)
{
gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
make_cleanup_py_decref (py_type);
/* Create an empty list of debug methods. */
py_xmethod_matcher_list = PyList_New (0);
if (py_xmethod_matcher_list == NULL)
{
gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
/* Gather debug method matchers registered with the object files.
This could be done differently by iterating over each objfile's matcher
list individually, but there's no data yet to show it's needed. */
ALL_OBJFILES (objfile)
{
PyObject *py_objfile = objfile_to_objfile_object (objfile);
PyObject *objfile_matchers, *temp = py_xmethod_matcher_list;
if (py_objfile == NULL)
{
gdbpy_print_stack ();
Py_DECREF (py_xmethod_matcher_list);
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
objfile_matchers = objfpy_get_xmethods (py_objfile, NULL);
py_xmethod_matcher_list = PySequence_Concat (temp, objfile_matchers);
Py_DECREF (temp);
Py_DECREF (objfile_matchers);
if (py_xmethod_matcher_list == NULL)
{
gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
}
/* Gather debug methods matchers registered with the current program
space. */
py_progspace = pspace_to_pspace_object (current_program_space);
if (py_progspace != NULL)
{
PyObject *temp = py_xmethod_matcher_list;
PyObject *pspace_matchers = pspy_get_xmethods (py_progspace, NULL);
py_xmethod_matcher_list = PySequence_Concat (temp, pspace_matchers);
Py_DECREF (temp);
Py_DECREF (pspace_matchers);
if (py_xmethod_matcher_list == NULL)
{
gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
}
else
{
gdbpy_print_stack ();
Py_DECREF (py_xmethod_matcher_list);
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
/* Gather debug method matchers registered globally. */
if (gdb_python_module != NULL
&& PyObject_HasAttrString (gdb_python_module, matchers_attr_str))
{
PyObject *gdb_matchers;
PyObject *temp = py_xmethod_matcher_list;
gdb_matchers = PyObject_GetAttrString (gdb_python_module,
matchers_attr_str);
if (gdb_matchers != NULL)
{
py_xmethod_matcher_list = PySequence_Concat (temp, gdb_matchers);
Py_DECREF (temp);
Py_DECREF (gdb_matchers);
if (py_xmethod_matcher_list == NULL)
{
gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
}
else
{
gdbpy_print_stack ();
Py_DECREF (py_xmethod_matcher_list);
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
}
/* Safe to make a cleanup for py_xmethod_matcher_list now as it
will not change any more. */
make_cleanup_py_decref (py_xmethod_matcher_list);
list_iter = PyObject_GetIter (py_xmethod_matcher_list);
if (list_iter == NULL)
{
gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
while ((matcher = PyIter_Next (list_iter)) != NULL)
{
PyObject *match_result = invoke_match_method (matcher, py_type,
method_name);
if (match_result == NULL)
{
gdbpy_print_stack ();
Py_DECREF (matcher);
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
if (match_result == Py_None)
; /* This means there was no match. */
else if (PySequence_Check (match_result))
{
PyObject *iter = PyObject_GetIter (match_result);
PyObject *py_worker;
if (iter == NULL)
{
gdbpy_print_stack ();
Py_DECREF (matcher);
Py_DECREF (match_result);
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
while ((py_worker = PyIter_Next (iter)) != NULL)
{
struct xmethod_worker *worker;
worker = new_python_xmethod_worker (py_worker, py_type);
VEC_safe_push (xmethod_worker_ptr, worker_vec, worker);
Py_DECREF (py_worker);
}
Py_DECREF (iter);
/* Report any error that could have occurred while iterating. */
if (PyErr_Occurred ())
{
gdbpy_print_stack ();
Py_DECREF (matcher);
Py_DECREF (match_result);
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
}
else
{
struct xmethod_worker *worker;
worker = new_python_xmethod_worker (match_result, py_type);
VEC_safe_push (xmethod_worker_ptr, worker_vec, worker);
}
Py_DECREF (match_result);
Py_DECREF (matcher);
}
Py_DECREF (list_iter);
/* Report any error that could have occurred while iterating. */
if (PyErr_Occurred ())
{
gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
do_cleanups (cleanups);
*dm_vec = worker_vec;
return EXT_LANG_RC_OK;
}
/* Implementation of get_xmethod_arg_types for Python. */
enum ext_lang_rc
gdbpy_get_xmethod_arg_types (const struct extension_language_defn *extlang,
struct xmethod_worker *worker,
int *nargs, struct type ***arg_types)
{
struct gdbpy_worker_data *worker_data
= (struct gdbpy_worker_data *) worker->data;
PyObject *py_worker = worker_data->worker;
PyObject *get_arg_types_method;
PyObject *py_argtype_list, *list_iter = NULL, *item;
struct cleanup *cleanups;
struct type **type_array, *obj_type;
int i = 1, arg_count;
/* Set nargs to -1 so that any premature return from this function returns
an invalid/unusable number of arg types. */
*nargs = -1;
cleanups = ensure_python_env (get_current_arch (), current_language);
get_arg_types_method = PyObject_GetAttrString (py_worker,
get_arg_types_method_name);
if (get_arg_types_method == NULL)
{
gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
make_cleanup_py_decref (get_arg_types_method);
py_argtype_list = PyObject_CallMethodObjArgs (py_worker,
py_get_arg_types_method_name,
NULL);
if (py_argtype_list == NULL)
{
gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
make_cleanup_py_decref (py_argtype_list);
if (py_argtype_list == Py_None)
arg_count = 0;
else if (PySequence_Check (py_argtype_list))
{
arg_count = PySequence_Size (py_argtype_list);
if (arg_count == -1)
{
gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
list_iter = PyObject_GetIter (py_argtype_list);
if (list_iter == NULL)
{
gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
make_cleanup_py_decref (list_iter);
}
else
arg_count = 1;
/* Include the 'this' argument in the size. */
type_array = XCNEWVEC (struct type *, arg_count + 1);
i = 1;
if (list_iter != NULL)
{
while ((item = PyIter_Next (list_iter)) != NULL)
{
struct type *arg_type = type_object_to_type (item);
Py_DECREF (item);
if (arg_type == NULL)
{
PyErr_SetString (PyExc_TypeError,
_("Arg type returned by the get_arg_types "
"method of a debug method worker object is "
"not a gdb.Type object."));
break;
}
type_array[i] = arg_type;
i++;
}
}
else if (arg_count == 1)
{
/* py_argtype_list is not actually a list but a single gdb.Type
object. */
struct type *arg_type = type_object_to_type (py_argtype_list);
if (arg_type == NULL)
{
PyErr_SetString (PyExc_TypeError,
_("Arg type returned by the get_arg_types method "
"of an xmethod worker object is not a gdb.Type "
"object."));
}
else
{
type_array[i] = arg_type;
i++;
}
}
if (PyErr_Occurred ())
{
gdbpy_print_stack ();
do_cleanups (cleanups);
xfree (type_array);
return EXT_LANG_RC_ERROR;
}
/* Add the type of 'this' as the first argument. The 'this' pointer should
be a 'const' value. Hence, create a 'const' variant of the 'this' pointer
type. */
obj_type = type_object_to_type (worker_data->this_type);
type_array[0] = make_cv_type (1, 0, lookup_pointer_type (obj_type), NULL);
*nargs = i;
*arg_types = type_array;
do_cleanups (cleanups);
return EXT_LANG_RC_OK;
}
/* Implementation of get_xmethod_result_type for Python. */
enum ext_lang_rc
gdbpy_get_xmethod_result_type (const struct extension_language_defn *extlang,
struct xmethod_worker *worker,
struct value *obj,
struct value **args, int nargs,
struct type **result_type_ptr)
{
struct gdbpy_worker_data *worker_data
= (struct gdbpy_worker_data *) worker->data;
PyObject *py_worker = worker_data->worker;
PyObject *py_value_obj, *py_arg_tuple, *py_result_type;
PyObject *get_result_type_method;
struct type *obj_type, *this_type;
struct cleanup *cleanups;
int i;
cleanups = ensure_python_env (get_current_arch (), current_language);
/* First see if there is a get_result_type method.
If not this could be an old xmethod (pre 7.9.1). */
get_result_type_method
= PyObject_GetAttrString (py_worker, get_result_type_method_name);
if (get_result_type_method == NULL)
{
PyErr_Clear ();
do_cleanups (cleanups);
*result_type_ptr = NULL;
return EXT_LANG_RC_OK;
}
make_cleanup_py_decref (get_result_type_method);
obj_type = check_typedef (value_type (obj));
this_type = check_typedef (type_object_to_type (worker_data->this_type));
if (TYPE_CODE (obj_type) == TYPE_CODE_PTR)
{
struct type *this_ptr = lookup_pointer_type (this_type);
if (!types_equal (obj_type, this_ptr))
obj = value_cast (this_ptr, obj);
}
else if (TYPE_CODE (obj_type) == TYPE_CODE_REF)
{
struct type *this_ref = lookup_reference_type (this_type);
if (!types_equal (obj_type, this_ref))
obj = value_cast (this_ref, obj);
}
else
{
if (!types_equal (obj_type, this_type))
obj = value_cast (this_type, obj);
}
py_value_obj = value_to_value_object (obj);
if (py_value_obj == NULL)
goto Fail;
make_cleanup_py_decref (py_value_obj);
py_arg_tuple = PyTuple_New (nargs + 1);
if (py_arg_tuple == NULL)
goto Fail;
make_cleanup_py_decref (py_arg_tuple);
/* PyTuple_SET_ITEM steals the reference of the element. Hence INCREF the
reference to the 'this' object as we have a cleanup to DECREF it. */
Py_INCREF (py_value_obj);
PyTuple_SET_ITEM (py_arg_tuple, 0, py_value_obj);
for (i = 0; i < nargs; i++)
{
PyObject *py_value_arg = value_to_value_object (args[i]);
if (py_value_arg == NULL)
goto Fail;
PyTuple_SET_ITEM (py_arg_tuple, i + 1, py_value_arg);
}
py_result_type = PyObject_CallObject (get_result_type_method, py_arg_tuple);
if (py_result_type == NULL)
goto Fail;
make_cleanup_py_decref (py_result_type);
*result_type_ptr = type_object_to_type (py_result_type);
if (*result_type_ptr == NULL)
{
PyErr_SetString (PyExc_TypeError,
_("Type returned by the get_result_type method of an"
" xmethod worker object is not a gdb.Type object."));
goto Fail;
}
do_cleanups (cleanups);
return EXT_LANG_RC_OK;
Fail:
gdbpy_print_stack ();
do_cleanups (cleanups);
return EXT_LANG_RC_ERROR;
}
/* Implementation of invoke_xmethod for Python. */
struct value *
gdbpy_invoke_xmethod (const struct extension_language_defn *extlang,
struct xmethod_worker *worker,
struct value *obj, struct value **args, int nargs)
{
int i;
struct cleanup *cleanups;
PyObject *py_value_obj, *py_arg_tuple, *py_result;
struct type *obj_type, *this_type;
struct value *res = NULL;
struct gdbpy_worker_data *worker_data
= (struct gdbpy_worker_data *) worker->data;
PyObject *xmethod_worker = worker_data->worker;
cleanups = ensure_python_env (get_current_arch (), current_language);
obj_type = check_typedef (value_type (obj));
this_type = check_typedef (type_object_to_type (worker_data->this_type));
if (TYPE_CODE (obj_type) == TYPE_CODE_PTR)
{
struct type *this_ptr = lookup_pointer_type (this_type);
if (!types_equal (obj_type, this_ptr))
obj = value_cast (this_ptr, obj);
}
else if (TYPE_CODE (obj_type) == TYPE_CODE_REF)
{
struct type *this_ref = lookup_reference_type (this_type);
if (!types_equal (obj_type, this_ref))
obj = value_cast (this_ref, obj);
}
else
{
if (!types_equal (obj_type, this_type))
obj = value_cast (this_type, obj);
}
py_value_obj = value_to_value_object (obj);
if (py_value_obj == NULL)
{
gdbpy_print_stack ();
error (_("Error while executing Python code."));
}
make_cleanup_py_decref (py_value_obj);
py_arg_tuple = PyTuple_New (nargs + 1);
if (py_arg_tuple == NULL)
{
gdbpy_print_stack ();
error (_("Error while executing Python code."));
}
make_cleanup_py_decref (py_arg_tuple);
/* PyTuple_SET_ITEM steals the reference of the element. Hence INCREF the
reference to the 'this' object as we have a cleanup to DECREF it. */
Py_INCREF (py_value_obj);
PyTuple_SET_ITEM (py_arg_tuple, 0, py_value_obj);
for (i = 0; i < nargs; i++)
{
PyObject *py_value_arg = value_to_value_object (args[i]);
if (py_value_arg == NULL)
{
gdbpy_print_stack ();
error (_("Error while executing Python code."));
}
PyTuple_SET_ITEM (py_arg_tuple, i + 1, py_value_arg);
}
py_result = PyObject_CallObject (xmethod_worker, py_arg_tuple);
if (py_result == NULL)
{
gdbpy_print_stack ();
error (_("Error while executing Python code."));
}
make_cleanup_py_decref (py_result);
if (py_result != Py_None)
{
res = convert_value_from_python (py_result);
if (res == NULL)
{
gdbpy_print_stack ();
error (_("Error while executing Python code."));
}
}
else
{
res = allocate_value (lookup_typename (python_language, python_gdbarch,
"void", NULL, 0));
}
do_cleanups (cleanups);
return res;
}
/* Creates a new Python xmethod_worker object.
The new object has data of type 'struct gdbpy_worker_data' composed
with the components PY_WORKER and THIS_TYPE. */
static struct xmethod_worker *
new_python_xmethod_worker (PyObject *py_worker, PyObject *this_type)
{
struct gdbpy_worker_data *data;
gdb_assert (py_worker != NULL && this_type != NULL);
data = XCNEW (struct gdbpy_worker_data);
data->worker = py_worker;
data->this_type = this_type;
Py_INCREF (py_worker);
Py_INCREF (this_type);
return new_xmethod_worker (&extension_language_python, data);
}
int
gdbpy_initialize_xmethods (void)
{
py_match_method_name = PyString_FromString (match_method_name);
if (py_match_method_name == NULL)
return -1;
py_get_arg_types_method_name
= PyString_FromString (get_arg_types_method_name);
if (py_get_arg_types_method_name == NULL)
return -1;
return 1;
}
|