]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/python/py-framefilter.c
Update copyright year range in all GDB files
[thirdparty/binutils-gdb.git] / gdb / python / py-framefilter.c
1 /* Python frame filters
2
3 Copyright (C) 2013-2021 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 "objfiles.h"
22 #include "symtab.h"
23 #include "language.h"
24 #include "arch-utils.h"
25 #include "python.h"
26 #include "ui-out.h"
27 #include "valprint.h"
28 #include "stack.h"
29 #include "source.h"
30 #include "annotate.h"
31 #include "hashtab.h"
32 #include "demangle.h"
33 #include "mi/mi-cmds.h"
34 #include "python-internal.h"
35 #include "gdbsupport/gdb_optional.h"
36 #include "cli/cli-style.h"
37
38 enum mi_print_types
39 {
40 MI_PRINT_ARGS,
41 MI_PRINT_LOCALS
42 };
43
44 /* Helper function to extract a symbol, a name and a language
45 definition from a Python object that conforms to the "Symbol Value"
46 interface. OBJ is the Python object to extract the values from.
47 NAME is a pass-through argument where the name of the symbol will
48 be written. NAME is allocated in this function, but the caller is
49 responsible for clean up. SYM is a pass-through argument where the
50 symbol will be written and SYM_BLOCK is a pass-through argument to
51 write the block where the symbol lies in. In the case of the API
52 returning a string, this will be set to NULL. LANGUAGE is also a
53 pass-through argument denoting the language attributed to the
54 Symbol. In the case of SYM being NULL, this will be set to the
55 current language. Returns EXT_LANG_BT_ERROR on error with the
56 appropriate Python exception set, and EXT_LANG_BT_OK on success. */
57
58 static enum ext_lang_bt_status
59 extract_sym (PyObject *obj, gdb::unique_xmalloc_ptr<char> *name,
60 struct symbol **sym, const struct block **sym_block,
61 const struct language_defn **language)
62 {
63 gdbpy_ref<> result (PyObject_CallMethod (obj, "symbol", NULL));
64
65 if (result == NULL)
66 return EXT_LANG_BT_ERROR;
67
68 /* For 'symbol' callback, the function can return a symbol or a
69 string. */
70 if (gdbpy_is_string (result.get ()))
71 {
72 *name = python_string_to_host_string (result.get ());
73
74 if (*name == NULL)
75 return EXT_LANG_BT_ERROR;
76 /* If the API returns a string (and not a symbol), then there is
77 no symbol derived language available and the frame filter has
78 either overridden the symbol with a string, or supplied a
79 entirely synthetic symbol/value pairing. In that case, use
80 python_language. */
81 *language = python_language;
82 *sym = NULL;
83 *sym_block = NULL;
84 }
85 else
86 {
87 /* This type checks 'result' during the conversion so we
88 just call it unconditionally and check the return. */
89 *sym = symbol_object_to_symbol (result.get ());
90 /* TODO: currently, we have no way to recover the block in which SYMBOL
91 was found, so we have no block to return. Trying to evaluate SYMBOL
92 will yield an incorrect value when it's located in a FRAME and
93 evaluated from another frame (as permitted in nested functions). */
94 *sym_block = NULL;
95
96 if (*sym == NULL)
97 {
98 PyErr_SetString (PyExc_RuntimeError,
99 _("Unexpected value. Expecting a "
100 "gdb.Symbol or a Python string."));
101 return EXT_LANG_BT_ERROR;
102 }
103
104 /* Duplicate the symbol name, so the caller has consistency
105 in garbage collection. */
106 name->reset (xstrdup ((*sym)->print_name ()));
107
108 /* If a symbol is specified attempt to determine the language
109 from the symbol. If mode is not "auto", then the language
110 has been explicitly set, use that. */
111 if (language_mode == language_mode_auto)
112 *language = language_def ((*sym)->language ());
113 else
114 *language = current_language;
115 }
116
117 return EXT_LANG_BT_OK;
118 }
119
120 /* Helper function to extract a value from an object that conforms to
121 the "Symbol Value" interface. OBJ is the Python object to extract
122 the value from. VALUE is a pass-through argument where the value
123 will be written. If the object does not have the value attribute,
124 or provides the Python None for a value, VALUE will be set to NULL
125 and this function will return as successful. Returns EXT_LANG_BT_ERROR
126 on error with the appropriate Python exception set, and EXT_LANG_BT_OK on
127 success. */
128
129 static enum ext_lang_bt_status
130 extract_value (PyObject *obj, struct value **value)
131 {
132 if (PyObject_HasAttrString (obj, "value"))
133 {
134 gdbpy_ref<> vresult (PyObject_CallMethod (obj, "value", NULL));
135
136 if (vresult == NULL)
137 return EXT_LANG_BT_ERROR;
138
139 /* The Python code has returned 'None' for a value, so we set
140 value to NULL. This flags that GDB should read the
141 value. */
142 if (vresult == Py_None)
143 {
144 *value = NULL;
145 return EXT_LANG_BT_OK;
146 }
147 else
148 {
149 *value = convert_value_from_python (vresult.get ());
150
151 if (*value == NULL)
152 return EXT_LANG_BT_ERROR;
153
154 return EXT_LANG_BT_OK;
155 }
156 }
157 else
158 *value = NULL;
159
160 return EXT_LANG_BT_OK;
161 }
162
163 /* MI prints only certain values according to the type of symbol and
164 also what the user has specified. SYM is the symbol to check, and
165 MI_PRINT_TYPES is an enum specifying what the user wants emitted
166 for the MI command in question. */
167 static int
168 mi_should_print (struct symbol *sym, enum mi_print_types type)
169 {
170 int print_me = 0;
171
172 switch (SYMBOL_CLASS (sym))
173 {
174 default:
175 case LOC_UNDEF: /* catches errors */
176 case LOC_CONST: /* constant */
177 case LOC_TYPEDEF: /* local typedef */
178 case LOC_LABEL: /* local label */
179 case LOC_BLOCK: /* local function */
180 case LOC_CONST_BYTES: /* loc. byte seq. */
181 case LOC_UNRESOLVED: /* unresolved static */
182 case LOC_OPTIMIZED_OUT: /* optimized out */
183 print_me = 0;
184 break;
185
186 case LOC_ARG: /* argument */
187 case LOC_REF_ARG: /* reference arg */
188 case LOC_REGPARM_ADDR: /* indirect register arg */
189 case LOC_LOCAL: /* stack local */
190 case LOC_STATIC: /* static */
191 case LOC_REGISTER: /* register */
192 case LOC_COMPUTED: /* computed location */
193 if (type == MI_PRINT_LOCALS)
194 print_me = ! SYMBOL_IS_ARGUMENT (sym);
195 else
196 print_me = SYMBOL_IS_ARGUMENT (sym);
197 }
198 return print_me;
199 }
200
201 /* Helper function which outputs a type name extracted from VAL to a
202 "type" field in the output stream OUT. OUT is the ui-out structure
203 the type name will be output too, and VAL is the value that the
204 type will be extracted from. */
205
206 static void
207 py_print_type (struct ui_out *out, struct value *val)
208 {
209 check_typedef (value_type (val));
210
211 string_file stb;
212 type_print (value_type (val), "", &stb, -1);
213 out->field_stream ("type", stb);
214 }
215
216 /* Helper function which outputs a value to an output field in a
217 stream. OUT is the ui-out structure the value will be output to,
218 VAL is the value that will be printed, OPTS contains the value
219 printing options, ARGS_TYPE is an enumerator describing the
220 argument format, and LANGUAGE is the language_defn that the value
221 will be printed with. */
222
223 static void
224 py_print_value (struct ui_out *out, struct value *val,
225 const struct value_print_options *opts,
226 int indent,
227 enum ext_lang_frame_args args_type,
228 const struct language_defn *language)
229 {
230 int should_print = 0;
231
232 /* MI does not print certain values, differentiated by type,
233 depending on what ARGS_TYPE indicates. Test type against option.
234 For CLI print all values. */
235 if (args_type == MI_PRINT_SIMPLE_VALUES
236 || args_type == MI_PRINT_ALL_VALUES)
237 {
238 struct type *type = check_typedef (value_type (val));
239
240 if (args_type == MI_PRINT_ALL_VALUES)
241 should_print = 1;
242 else if (args_type == MI_PRINT_SIMPLE_VALUES
243 && type->code () != TYPE_CODE_ARRAY
244 && type->code () != TYPE_CODE_STRUCT
245 && type->code () != TYPE_CODE_UNION)
246 should_print = 1;
247 }
248 else if (args_type != NO_VALUES)
249 should_print = 1;
250
251 if (should_print)
252 {
253 string_file stb;
254
255 common_val_print (val, &stb, indent, opts, language);
256 out->field_stream ("value", stb);
257 }
258 }
259
260 /* Helper function to call a Python method and extract an iterator
261 from the result. If the function returns anything but an iterator
262 the exception is preserved and NULL is returned. FILTER is the
263 Python object to call, and FUNC is the name of the method. Returns
264 a PyObject, or NULL on error with the appropriate exception set.
265 This function can return an iterator, or NULL. */
266
267 static PyObject *
268 get_py_iter_from_func (PyObject *filter, const char *func)
269 {
270 if (PyObject_HasAttrString (filter, func))
271 {
272 gdbpy_ref<> result (PyObject_CallMethod (filter, func, NULL));
273
274 if (result != NULL)
275 {
276 if (result == Py_None)
277 {
278 return result.release ();
279 }
280 else
281 {
282 return PyObject_GetIter (result.get ());
283 }
284 }
285 }
286 else
287 Py_RETURN_NONE;
288
289 return NULL;
290 }
291
292 /* Helper function to output a single frame argument and value to an
293 output stream. This function will account for entry values if the
294 FV parameter is populated, the frame argument has entry values
295 associated with them, and the appropriate "set entry-value"
296 options are set. Will output in CLI or MI like format depending
297 on the type of output stream detected. OUT is the output stream,
298 SYM_NAME is the name of the symbol. If SYM_NAME is populated then
299 it must have an accompanying value in the parameter FV. FA is a
300 frame argument structure. If FA is populated, both SYM_NAME and
301 FV are ignored. OPTS contains the value printing options,
302 ARGS_TYPE is an enumerator describing the argument format,
303 PRINT_ARGS_FIELD is a flag which indicates if we output "ARGS=1"
304 in MI output in commands where both arguments and locals are
305 printed. */
306
307 static void
308 py_print_single_arg (struct ui_out *out,
309 const char *sym_name,
310 struct frame_arg *fa,
311 struct value *fv,
312 const struct value_print_options *opts,
313 enum ext_lang_frame_args args_type,
314 int print_args_field,
315 const struct language_defn *language)
316 {
317 struct value *val;
318
319 if (fa != NULL)
320 {
321 if (fa->val == NULL && fa->error == NULL)
322 return;
323 language = language_def (fa->sym->language ());
324 val = fa->val;
325 }
326 else
327 val = fv;
328
329 gdb::optional<ui_out_emit_tuple> maybe_tuple;
330
331 /* MI has varying rules for tuples, but generally if there is only
332 one element in each item in the list, do not start a tuple. The
333 exception is -stack-list-variables which emits an ARGS="1" field
334 if the value is a frame argument. This is denoted in this
335 function with PRINT_ARGS_FIELD which is flag from the caller to
336 emit the ARGS field. */
337 if (out->is_mi_like_p ())
338 {
339 if (print_args_field || args_type != NO_VALUES)
340 maybe_tuple.emplace (out, nullptr);
341 }
342
343 annotate_arg_begin ();
344
345 /* If frame argument is populated, check for entry-values and the
346 entry value options. */
347 if (fa != NULL)
348 {
349 string_file stb;
350
351 fprintf_symbol_filtered (&stb, fa->sym->print_name (),
352 fa->sym->language (),
353 DMGL_PARAMS | DMGL_ANSI);
354 if (fa->entry_kind == print_entry_values_compact)
355 {
356 stb.puts ("=");
357
358 fprintf_symbol_filtered (&stb, fa->sym->print_name (),
359 fa->sym->language (),
360 DMGL_PARAMS | DMGL_ANSI);
361 }
362 if (fa->entry_kind == print_entry_values_only
363 || fa->entry_kind == print_entry_values_compact)
364 stb.puts ("@entry");
365 out->field_stream ("name", stb);
366 }
367 else
368 /* Otherwise, just output the name. */
369 out->field_string ("name", sym_name);
370
371 annotate_arg_name_end ();
372
373 out->text ("=");
374
375 if (print_args_field)
376 out->field_signed ("arg", 1);
377
378 /* For MI print the type, but only for simple values. This seems
379 weird, but this is how MI choose to format the various output
380 types. */
381 if (args_type == MI_PRINT_SIMPLE_VALUES && val != NULL)
382 py_print_type (out, val);
383
384 if (val != NULL)
385 annotate_arg_value (value_type (val));
386
387 /* If the output is to the CLI, and the user option "set print
388 frame-arguments" is set to none, just output "...". */
389 if (! out->is_mi_like_p () && args_type == NO_VALUES)
390 out->field_string ("value", "...");
391 else
392 {
393 /* Otherwise, print the value for both MI and the CLI, except
394 for the case of MI_PRINT_NO_VALUES. */
395 if (args_type != NO_VALUES)
396 {
397 if (val == NULL)
398 {
399 gdb_assert (fa != NULL && fa->error != NULL);
400 out->field_fmt ("value", metadata_style.style (),
401 _("<error reading variable: %s>"),
402 fa->error.get ());
403 }
404 else
405 py_print_value (out, val, opts, 0, args_type, language);
406 }
407 }
408 }
409
410 /* Helper function to loop over frame arguments provided by the
411 "frame_arguments" Python API. Elements in the iterator must
412 conform to the "Symbol Value" interface. ITER is the Python
413 iterable object, OUT is the output stream, ARGS_TYPE is an
414 enumerator describing the argument format, PRINT_ARGS_FIELD is a
415 flag which indicates if we output "ARGS=1" in MI output in commands
416 where both arguments and locals are printed, and FRAME is the
417 backing frame. Returns EXT_LANG_BT_ERROR on error, with any GDB
418 exceptions converted to a Python exception, or EXT_LANG_BT_OK on
419 success. */
420
421 static enum ext_lang_bt_status
422 enumerate_args (PyObject *iter,
423 struct ui_out *out,
424 enum ext_lang_frame_args args_type,
425 int print_args_field,
426 struct frame_info *frame)
427 {
428 struct value_print_options opts;
429
430 get_user_print_options (&opts);
431
432 if (args_type == CLI_SCALAR_VALUES)
433 {
434 /* True in "summary" mode, false otherwise. */
435 opts.summary = 1;
436 }
437
438 opts.deref_ref = 1;
439
440 annotate_frame_args ();
441
442 /* Collect the first argument outside of the loop, so output of
443 commas in the argument output is correct. At the end of the
444 loop block collect another item from the iterator, and, if it is
445 not null emit a comma. */
446 gdbpy_ref<> item (PyIter_Next (iter));
447 if (item == NULL && PyErr_Occurred ())
448 return EXT_LANG_BT_ERROR;
449
450 while (item != NULL)
451 {
452 const struct language_defn *language;
453 gdb::unique_xmalloc_ptr<char> sym_name;
454 struct symbol *sym;
455 const struct block *sym_block;
456 struct value *val;
457 enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
458
459 success = extract_sym (item.get (), &sym_name, &sym, &sym_block,
460 &language);
461 if (success == EXT_LANG_BT_ERROR)
462 return EXT_LANG_BT_ERROR;
463
464 success = extract_value (item.get (), &val);
465 if (success == EXT_LANG_BT_ERROR)
466 return EXT_LANG_BT_ERROR;
467
468 if (sym && out->is_mi_like_p ()
469 && ! mi_should_print (sym, MI_PRINT_ARGS))
470 continue;
471
472 /* If the object did not provide a value, read it using
473 read_frame_args and account for entry values, if any. */
474 if (val == NULL)
475 {
476 struct frame_arg arg, entryarg;
477
478 /* If there is no value, and also no symbol, set error and
479 exit. */
480 if (sym == NULL)
481 {
482 PyErr_SetString (PyExc_RuntimeError,
483 _("No symbol or value provided."));
484 return EXT_LANG_BT_ERROR;
485 }
486
487 read_frame_arg (user_frame_print_options,
488 sym, frame, &arg, &entryarg);
489
490 /* The object has not provided a value, so this is a frame
491 argument to be read by GDB. In this case we have to
492 account for entry-values. */
493
494 if (arg.entry_kind != print_entry_values_only)
495 {
496 py_print_single_arg (out, NULL, &arg,
497 NULL, &opts,
498 args_type,
499 print_args_field,
500 NULL);
501 }
502
503 if (entryarg.entry_kind != print_entry_values_no)
504 {
505 if (arg.entry_kind != print_entry_values_only)
506 {
507 out->text (", ");
508 out->wrap_hint (" ");
509 }
510
511 py_print_single_arg (out, NULL, &entryarg, NULL, &opts,
512 args_type, print_args_field, NULL);
513 }
514 }
515 else
516 {
517 /* If the object has provided a value, we just print that. */
518 if (val != NULL)
519 py_print_single_arg (out, sym_name.get (), NULL, val, &opts,
520 args_type, print_args_field,
521 language);
522 }
523
524 /* Collect the next item from the iterator. If
525 this is the last item, do not print the
526 comma. */
527 item.reset (PyIter_Next (iter));
528 if (item != NULL)
529 out->text (", ");
530 else if (PyErr_Occurred ())
531 return EXT_LANG_BT_ERROR;
532
533 annotate_arg_end ();
534 }
535
536 return EXT_LANG_BT_OK;
537 }
538
539
540 /* Helper function to loop over variables provided by the
541 "frame_locals" Python API. Elements in the iterable must conform
542 to the "Symbol Value" interface. ITER is the Python iterable
543 object, OUT is the output stream, INDENT is whether we should
544 indent the output (for CLI), ARGS_TYPE is an enumerator describing
545 the argument format, PRINT_ARGS_FIELD is flag which indicates
546 whether to output the ARGS field in the case of
547 -stack-list-variables and FRAME is the backing frame. Returns
548 EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to a Python
549 exception, or EXT_LANG_BT_OK on success. */
550
551 static enum ext_lang_bt_status
552 enumerate_locals (PyObject *iter,
553 struct ui_out *out,
554 int indent,
555 enum ext_lang_frame_args args_type,
556 int print_args_field,
557 struct frame_info *frame)
558 {
559 struct value_print_options opts;
560
561 get_user_print_options (&opts);
562 opts.deref_ref = 1;
563
564 while (true)
565 {
566 const struct language_defn *language;
567 gdb::unique_xmalloc_ptr<char> sym_name;
568 struct value *val;
569 enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
570 struct symbol *sym;
571 const struct block *sym_block;
572 int local_indent = 8 + (8 * indent);
573 gdb::optional<ui_out_emit_tuple> tuple;
574
575 gdbpy_ref<> item (PyIter_Next (iter));
576 if (item == NULL)
577 break;
578
579 success = extract_sym (item.get (), &sym_name, &sym, &sym_block,
580 &language);
581 if (success == EXT_LANG_BT_ERROR)
582 return EXT_LANG_BT_ERROR;
583
584 success = extract_value (item.get (), &val);
585 if (success == EXT_LANG_BT_ERROR)
586 return EXT_LANG_BT_ERROR;
587
588 if (sym != NULL && out->is_mi_like_p ()
589 && ! mi_should_print (sym, MI_PRINT_LOCALS))
590 continue;
591
592 /* If the object did not provide a value, read it. */
593 if (val == NULL)
594 val = read_var_value (sym, sym_block, frame);
595
596 /* With PRINT_NO_VALUES, MI does not emit a tuple normally as
597 each output contains only one field. The exception is
598 -stack-list-variables, which always provides a tuple. */
599 if (out->is_mi_like_p ())
600 {
601 if (print_args_field || args_type != NO_VALUES)
602 tuple.emplace (out, nullptr);
603 }
604
605 /* If the output is not MI we indent locals. */
606 out->spaces (local_indent);
607 out->field_string ("name", sym_name.get ());
608 out->text (" = ");
609
610 if (args_type == MI_PRINT_SIMPLE_VALUES)
611 py_print_type (out, val);
612
613 /* CLI always prints values for locals. MI uses the
614 simple/no/all system. */
615 if (! out->is_mi_like_p ())
616 {
617 int val_indent = (indent + 1) * 4;
618
619 py_print_value (out, val, &opts, val_indent, args_type,
620 language);
621 }
622 else
623 {
624 if (args_type != NO_VALUES)
625 py_print_value (out, val, &opts, 0, args_type,
626 language);
627 }
628
629 out->text ("\n");
630 }
631
632 if (!PyErr_Occurred ())
633 return EXT_LANG_BT_OK;
634
635 return EXT_LANG_BT_ERROR;
636 }
637
638 /* Helper function for -stack-list-variables. Returns EXT_LANG_BT_ERROR on
639 error, or EXT_LANG_BT_OK on success. */
640
641 static enum ext_lang_bt_status
642 py_mi_print_variables (PyObject *filter, struct ui_out *out,
643 struct value_print_options *opts,
644 enum ext_lang_frame_args args_type,
645 struct frame_info *frame)
646 {
647 gdbpy_ref<> args_iter (get_py_iter_from_func (filter, "frame_args"));
648 if (args_iter == NULL)
649 return EXT_LANG_BT_ERROR;
650
651 gdbpy_ref<> locals_iter (get_py_iter_from_func (filter, "frame_locals"));
652 if (locals_iter == NULL)
653 return EXT_LANG_BT_ERROR;
654
655 ui_out_emit_list list_emitter (out, "variables");
656
657 if (args_iter != Py_None
658 && (enumerate_args (args_iter.get (), out, args_type, 1, frame)
659 == EXT_LANG_BT_ERROR))
660 return EXT_LANG_BT_ERROR;
661
662 if (locals_iter != Py_None
663 && (enumerate_locals (locals_iter.get (), out, 1, args_type, 1, frame)
664 == EXT_LANG_BT_ERROR))
665 return EXT_LANG_BT_ERROR;
666
667 return EXT_LANG_BT_OK;
668 }
669
670 /* Helper function for printing locals. This function largely just
671 creates the wrapping tuple, and calls enumerate_locals. Returns
672 EXT_LANG_BT_ERROR on error, or EXT_LANG_BT_OK on success. */
673
674 static enum ext_lang_bt_status
675 py_print_locals (PyObject *filter,
676 struct ui_out *out,
677 enum ext_lang_frame_args args_type,
678 int indent,
679 struct frame_info *frame)
680 {
681 gdbpy_ref<> locals_iter (get_py_iter_from_func (filter, "frame_locals"));
682 if (locals_iter == NULL)
683 return EXT_LANG_BT_ERROR;
684
685 ui_out_emit_list list_emitter (out, "locals");
686
687 if (locals_iter != Py_None
688 && (enumerate_locals (locals_iter.get (), out, indent, args_type,
689 0, frame) == EXT_LANG_BT_ERROR))
690 return EXT_LANG_BT_ERROR;
691
692 return EXT_LANG_BT_OK;
693 }
694
695 /* Helper function for printing frame arguments. This function
696 largely just creates the wrapping tuple, and calls enumerate_args.
697 Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to
698 a Python exception, or EXT_LANG_BT_OK on success. */
699
700 static enum ext_lang_bt_status
701 py_print_args (PyObject *filter,
702 struct ui_out *out,
703 enum ext_lang_frame_args args_type,
704 struct frame_info *frame)
705 {
706 gdbpy_ref<> args_iter (get_py_iter_from_func (filter, "frame_args"));
707 if (args_iter == NULL)
708 return EXT_LANG_BT_ERROR;
709
710 ui_out_emit_list list_emitter (out, "args");
711
712 out->wrap_hint (" ");
713 annotate_frame_args ();
714 out->text (" (");
715
716 if (args_type == CLI_PRESENCE)
717 {
718 if (args_iter != Py_None)
719 {
720 gdbpy_ref<> item (PyIter_Next (args_iter.get ()));
721
722 if (item != NULL)
723 out->text ("...");
724 else if (PyErr_Occurred ())
725 return EXT_LANG_BT_ERROR;
726 }
727 }
728 else if (args_iter != Py_None
729 && (enumerate_args (args_iter.get (), out, args_type, 0, frame)
730 == EXT_LANG_BT_ERROR))
731 return EXT_LANG_BT_ERROR;
732
733 out->text (")");
734
735 return EXT_LANG_BT_OK;
736 }
737
738 /* Print a single frame to the designated output stream, detecting
739 whether the output is MI or console, and formatting the output
740 according to the conventions of that protocol. FILTER is the
741 frame-filter associated with this frame. FLAGS is an integer
742 describing the various print options. The FLAGS variables is
743 described in "apply_frame_filter" function. ARGS_TYPE is an
744 enumerator describing the argument format. OUT is the output
745 stream to print, INDENT is the level of indention for this frame
746 (in the case of elided frames), and LEVELS_PRINTED is a hash-table
747 containing all the frames level that have already been printed.
748 If a frame level has been printed, do not print it again (in the
749 case of elided frames). Returns EXT_LANG_BT_ERROR on error, with any
750 GDB exceptions converted to a Python exception, or EXT_LANG_BT_OK
751 on success. It can also throw an exception RETURN_QUIT. */
752
753 static enum ext_lang_bt_status
754 py_print_frame (PyObject *filter, frame_filter_flags flags,
755 enum ext_lang_frame_args args_type,
756 struct ui_out *out, int indent, htab_t levels_printed)
757 {
758 int has_addr = 0;
759 CORE_ADDR address = 0;
760 struct gdbarch *gdbarch = NULL;
761 struct frame_info *frame = NULL;
762 struct value_print_options opts;
763
764 int print_level, print_frame_info, print_args, print_locals;
765 /* Note that the below default in non-mi mode is the same as the
766 default value for the backtrace command (see the call to print_frame_info
767 in backtrace_command_1).
768 Having the same default ensures that 'bt' and 'bt no-filters'
769 have the same behaviour when some filters exist but do not apply
770 to a frame. */
771 enum print_what print_what
772 = out->is_mi_like_p () ? LOC_AND_ADDRESS : LOCATION;
773 gdb::unique_xmalloc_ptr<char> function_to_free;
774
775 /* Extract print settings from FLAGS. */
776 print_level = (flags & PRINT_LEVEL) ? 1 : 0;
777 print_frame_info = (flags & PRINT_FRAME_INFO) ? 1 : 0;
778 print_args = (flags & PRINT_ARGS) ? 1 : 0;
779 print_locals = (flags & PRINT_LOCALS) ? 1 : 0;
780
781 get_user_print_options (&opts);
782 if (print_frame_info)
783 {
784 gdb::optional<enum print_what> user_frame_info_print_what;
785
786 get_user_print_what_frame_info (&user_frame_info_print_what);
787 if (!out->is_mi_like_p () && user_frame_info_print_what.has_value ())
788 {
789 /* Use the specific frame information desired by the user. */
790 print_what = *user_frame_info_print_what;
791 }
792 }
793
794 /* Get the underlying frame. This is needed to determine GDB
795 architecture, and also, in the cases of frame variables/arguments to
796 read them if they returned filter object requires us to do so. */
797 gdbpy_ref<> py_inf_frame (PyObject_CallMethod (filter, "inferior_frame",
798 NULL));
799 if (py_inf_frame == NULL)
800 return EXT_LANG_BT_ERROR;
801
802 frame = frame_object_to_frame_info (py_inf_frame.get ());
803 if (frame == NULL)
804 return EXT_LANG_BT_ERROR;
805
806 symtab_and_line sal = find_frame_sal (frame);
807
808 gdbarch = get_frame_arch (frame);
809
810 /* stack-list-variables. */
811 if (print_locals && print_args && ! print_frame_info)
812 {
813 if (py_mi_print_variables (filter, out, &opts,
814 args_type, frame) == EXT_LANG_BT_ERROR)
815 return EXT_LANG_BT_ERROR;
816 return EXT_LANG_BT_OK;
817 }
818
819 gdb::optional<ui_out_emit_tuple> tuple;
820
821 /* -stack-list-locals does not require a
822 wrapping frame attribute. */
823 if (print_frame_info || (print_args && ! print_locals))
824 tuple.emplace (out, "frame");
825
826 if (print_frame_info)
827 {
828 /* Elided frames are also printed with this function (recursively)
829 and are printed with indention. */
830 if (indent > 0)
831 out->spaces (indent * 4);
832
833 /* The address is required for frame annotations, and also for
834 address printing. */
835 if (PyObject_HasAttrString (filter, "address"))
836 {
837 gdbpy_ref<> paddr (PyObject_CallMethod (filter, "address", NULL));
838
839 if (paddr == NULL)
840 return EXT_LANG_BT_ERROR;
841
842 if (paddr != Py_None)
843 {
844 if (get_addr_from_python (paddr.get (), &address) < 0)
845 return EXT_LANG_BT_ERROR;
846
847 has_addr = 1;
848 }
849 }
850 }
851
852 /* For MI, each piece is controlled individually. */
853 bool location_print = (print_frame_info
854 && !out->is_mi_like_p ()
855 && (print_what == LOCATION
856 || print_what == SRC_AND_LOC
857 || print_what == LOC_AND_ADDRESS
858 || print_what == SHORT_LOCATION));
859
860 /* Print frame level. MI does not require the level if
861 locals/variables only are being printed. */
862 if (print_level
863 && (location_print
864 || (out->is_mi_like_p () && (print_frame_info || print_args))))
865 {
866 struct frame_info **slot;
867 int level;
868
869 slot = (struct frame_info **) htab_find_slot (levels_printed,
870 frame, INSERT);
871
872 level = frame_relative_level (frame);
873
874 /* Check if this frame has already been printed (there are cases
875 where elided synthetic dummy-frames have to 'borrow' the frame
876 architecture from the eliding frame. If that is the case, do
877 not print 'level', but print spaces. */
878 if (*slot == frame)
879 out->field_skip ("level");
880 else
881 {
882 *slot = frame;
883 annotate_frame_begin (print_level ? level : 0,
884 gdbarch, address);
885 out->text ("#");
886 out->field_fmt_signed (2, ui_left, "level", level);
887 }
888 }
889
890 if (location_print || (out->is_mi_like_p () && print_frame_info))
891 {
892 /* Print address to the address field. If an address is not provided,
893 print nothing. */
894 if (opts.addressprint && has_addr)
895 {
896 if (!sal.symtab
897 || frame_show_address (frame, sal)
898 || print_what == LOC_AND_ADDRESS)
899 {
900 annotate_frame_address ();
901 out->field_core_addr ("addr", gdbarch, address);
902 if (get_frame_pc_masked (frame))
903 out->field_string ("pac", " [PAC]");
904 annotate_frame_address_end ();
905 out->text (" in ");
906 }
907 }
908
909 /* Print frame function name. */
910 if (PyObject_HasAttrString (filter, "function"))
911 {
912 gdbpy_ref<> py_func (PyObject_CallMethod (filter, "function", NULL));
913 const char *function = NULL;
914
915 if (py_func == NULL)
916 return EXT_LANG_BT_ERROR;
917
918 if (gdbpy_is_string (py_func.get ()))
919 {
920 function_to_free = python_string_to_host_string (py_func.get ());
921
922 if (function_to_free == NULL)
923 return EXT_LANG_BT_ERROR;
924
925 function = function_to_free.get ();
926 }
927 else if (PyLong_Check (py_func.get ()))
928 {
929 CORE_ADDR addr;
930 struct bound_minimal_symbol msymbol;
931
932 if (get_addr_from_python (py_func.get (), &addr) < 0)
933 return EXT_LANG_BT_ERROR;
934
935 msymbol = lookup_minimal_symbol_by_pc (addr);
936 if (msymbol.minsym != NULL)
937 function = msymbol.minsym->print_name ();
938 }
939 else if (py_func != Py_None)
940 {
941 PyErr_SetString (PyExc_RuntimeError,
942 _("FrameDecorator.function: expecting a " \
943 "String, integer or None."));
944 return EXT_LANG_BT_ERROR;
945 }
946
947 annotate_frame_function_name ();
948 if (function == NULL)
949 out->field_skip ("func");
950 else
951 out->field_string ("func", function, function_name_style.style ());
952 }
953 }
954
955
956 /* Frame arguments. Check the result, and error if something went
957 wrong. */
958 if (print_args && (location_print || out->is_mi_like_p ()))
959 {
960 if (py_print_args (filter, out, args_type, frame) == EXT_LANG_BT_ERROR)
961 return EXT_LANG_BT_ERROR;
962 }
963
964 /* File name/source/line number information. */
965 bool print_location_source
966 = ((location_print && print_what != SHORT_LOCATION)
967 || (out->is_mi_like_p () && print_frame_info));
968 if (print_location_source)
969 {
970 annotate_frame_source_begin ();
971
972 if (PyObject_HasAttrString (filter, "filename"))
973 {
974 gdbpy_ref<> py_fn (PyObject_CallMethod (filter, "filename", NULL));
975
976 if (py_fn == NULL)
977 return EXT_LANG_BT_ERROR;
978
979 if (py_fn != Py_None)
980 {
981 gdb::unique_xmalloc_ptr<char>
982 filename (python_string_to_host_string (py_fn.get ()));
983
984 if (filename == NULL)
985 return EXT_LANG_BT_ERROR;
986
987 out->wrap_hint (" ");
988 out->text (" at ");
989 annotate_frame_source_file ();
990 out->field_string ("file", filename.get (),
991 file_name_style.style ());
992 annotate_frame_source_file_end ();
993 }
994 }
995
996 if (PyObject_HasAttrString (filter, "line"))
997 {
998 gdbpy_ref<> py_line (PyObject_CallMethod (filter, "line", NULL));
999 int line;
1000
1001 if (py_line == NULL)
1002 return EXT_LANG_BT_ERROR;
1003
1004 if (py_line != Py_None)
1005 {
1006 line = PyLong_AsLong (py_line.get ());
1007 if (PyErr_Occurred ())
1008 return EXT_LANG_BT_ERROR;
1009
1010 out->text (":");
1011 annotate_frame_source_line ();
1012 out->field_signed ("line", line);
1013 }
1014 }
1015 if (out->is_mi_like_p ())
1016 out->field_string ("arch",
1017 (gdbarch_bfd_arch_info (gdbarch))->printable_name);
1018 }
1019
1020 bool source_print
1021 = (! out->is_mi_like_p ()
1022 && (print_what == SRC_LINE || print_what == SRC_AND_LOC));
1023 if (source_print)
1024 {
1025 if (print_location_source)
1026 out->text ("\n"); /* Newline after the location source. */
1027 print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
1028 }
1029
1030 /* For MI we need to deal with the "children" list population of
1031 elided frames, so if MI output detected do not send newline. */
1032 if (! out->is_mi_like_p ())
1033 {
1034 annotate_frame_end ();
1035 /* print_source_lines has already printed a newline. */
1036 if (!source_print)
1037 out->text ("\n");
1038 }
1039
1040 if (print_locals)
1041 {
1042 if (py_print_locals (filter, out, args_type, indent,
1043 frame) == EXT_LANG_BT_ERROR)
1044 return EXT_LANG_BT_ERROR;
1045 }
1046
1047 if ((flags & PRINT_HIDE) == 0)
1048 {
1049 /* Finally recursively print elided frames, if any. */
1050 gdbpy_ref<> elided (get_py_iter_from_func (filter, "elided"));
1051 if (elided == NULL)
1052 return EXT_LANG_BT_ERROR;
1053
1054 if (elided != Py_None)
1055 {
1056 PyObject *item;
1057
1058 ui_out_emit_list inner_list_emiter (out, "children");
1059
1060 indent++;
1061
1062 while ((item = PyIter_Next (elided.get ())))
1063 {
1064 gdbpy_ref<> item_ref (item);
1065
1066 enum ext_lang_bt_status success
1067 = py_print_frame (item, flags, args_type, out, indent,
1068 levels_printed);
1069
1070 if (success == EXT_LANG_BT_ERROR)
1071 return EXT_LANG_BT_ERROR;
1072 }
1073 if (item == NULL && PyErr_Occurred ())
1074 return EXT_LANG_BT_ERROR;
1075 }
1076 }
1077
1078 return EXT_LANG_BT_OK;
1079 }
1080
1081 /* Helper function to initiate frame filter invocation at starting
1082 frame FRAME. */
1083
1084 static PyObject *
1085 bootstrap_python_frame_filters (struct frame_info *frame,
1086 int frame_low, int frame_high)
1087 {
1088 gdbpy_ref<> frame_obj (frame_info_to_frame_object (frame));
1089 if (frame_obj == NULL)
1090 return NULL;
1091
1092 gdbpy_ref<> module (PyImport_ImportModule ("gdb.frames"));
1093 if (module == NULL)
1094 return NULL;
1095
1096 gdbpy_ref<> sort_func (PyObject_GetAttrString (module.get (),
1097 "execute_frame_filters"));
1098 if (sort_func == NULL)
1099 return NULL;
1100
1101 gdbpy_ref<> py_frame_low = gdb_py_object_from_longest (frame_low);
1102 if (py_frame_low == NULL)
1103 return NULL;
1104
1105 gdbpy_ref<> py_frame_high = gdb_py_object_from_longest (frame_high);
1106 if (py_frame_high == NULL)
1107 return NULL;
1108
1109 gdbpy_ref<> iterable (PyObject_CallFunctionObjArgs (sort_func.get (),
1110 frame_obj.get (),
1111 py_frame_low.get (),
1112 py_frame_high.get (),
1113 NULL));
1114 if (iterable == NULL)
1115 return NULL;
1116
1117 if (iterable != Py_None)
1118 return PyObject_GetIter (iterable.get ());
1119 else
1120 return iterable.release ();
1121 }
1122
1123 /* This is the only publicly exported function in this file. FRAME
1124 is the source frame to start frame-filter invocation. FLAGS is an
1125 integer holding the flags for printing. The following elements of
1126 the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
1127 PRINT_LEVEL is a flag indicating whether to print the frame's
1128 relative level in the output. PRINT_FRAME_INFO is a flag that
1129 indicates whether this function should print the frame
1130 information, PRINT_ARGS is a flag that indicates whether to print
1131 frame arguments, and PRINT_LOCALS, likewise, with frame local
1132 variables. ARGS_TYPE is an enumerator describing the argument
1133 format, OUT is the output stream to print. FRAME_LOW is the
1134 beginning of the slice of frames to print, and FRAME_HIGH is the
1135 upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error,
1136 or EXT_LANG_BT_OK on success. */
1137
1138 enum ext_lang_bt_status
1139 gdbpy_apply_frame_filter (const struct extension_language_defn *extlang,
1140 struct frame_info *frame, frame_filter_flags flags,
1141 enum ext_lang_frame_args args_type,
1142 struct ui_out *out, int frame_low, int frame_high)
1143 {
1144 struct gdbarch *gdbarch = NULL;
1145 enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
1146
1147 if (!gdb_python_initialized)
1148 return EXT_LANG_BT_NO_FILTERS;
1149
1150 try
1151 {
1152 gdbarch = get_frame_arch (frame);
1153 }
1154 catch (const gdb_exception_error &except)
1155 {
1156 /* Let gdb try to print the stack trace. */
1157 return EXT_LANG_BT_NO_FILTERS;
1158 }
1159
1160 gdbpy_enter enter_py (gdbarch, current_language);
1161
1162 /* When we're limiting the number of frames, be careful to request
1163 one extra frame, so that we can print a message if there are more
1164 frames. */
1165 int frame_countdown = -1;
1166 if ((flags & PRINT_MORE_FRAMES) != 0 && frame_low >= 0 && frame_high >= 0)
1167 {
1168 ++frame_high;
1169 /* This has an extra +1 because it is checked before a frame is
1170 printed. */
1171 frame_countdown = frame_high - frame_low + 1;
1172 }
1173
1174 gdbpy_ref<> iterable (bootstrap_python_frame_filters (frame, frame_low,
1175 frame_high));
1176
1177 if (iterable == NULL)
1178 {
1179 /* Normally if there is an error GDB prints the exception,
1180 abandons the backtrace and exits. The user can then call "bt
1181 no-filters", and get a default backtrace (it would be
1182 confusing to automatically start a standard backtrace halfway
1183 through a Python filtered backtrace). However in the case
1184 where GDB cannot initialize the frame filters (most likely
1185 due to incorrect auto-load paths), GDB has printed nothing.
1186 In this case it is OK to print the default backtrace after
1187 printing the error message. GDB returns EXT_LANG_BT_NO_FILTERS
1188 here to signify there are no filters after printing the
1189 initialization error. This return code will trigger a
1190 default backtrace. */
1191
1192 gdbpy_print_stack_or_quit ();
1193 return EXT_LANG_BT_NO_FILTERS;
1194 }
1195
1196 /* If iterable is None, then there are no frame filters registered.
1197 If this is the case, defer to default GDB printing routines in MI
1198 and CLI. */
1199 if (iterable == Py_None)
1200 return EXT_LANG_BT_NO_FILTERS;
1201
1202 htab_up levels_printed (htab_create (20,
1203 htab_hash_pointer,
1204 htab_eq_pointer,
1205 NULL));
1206
1207 while (true)
1208 {
1209 gdbpy_ref<> item (PyIter_Next (iterable.get ()));
1210
1211 if (item == NULL)
1212 {
1213 if (PyErr_Occurred ())
1214 {
1215 gdbpy_print_stack_or_quit ();
1216 return EXT_LANG_BT_ERROR;
1217 }
1218 break;
1219 }
1220
1221 if (frame_countdown != -1)
1222 {
1223 gdb_assert ((flags & PRINT_MORE_FRAMES) != 0);
1224 --frame_countdown;
1225 if (frame_countdown == 0)
1226 {
1227 /* We've printed all the frames we were asked to
1228 print, but more frames existed. */
1229 printf_filtered (_("(More stack frames follow...)\n"));
1230 break;
1231 }
1232 }
1233
1234 try
1235 {
1236 success = py_print_frame (item.get (), flags, args_type, out, 0,
1237 levels_printed.get ());
1238 }
1239 catch (const gdb_exception_error &except)
1240 {
1241 gdbpy_convert_exception (except);
1242 success = EXT_LANG_BT_ERROR;
1243 }
1244
1245 /* Do not exit on error printing a single frame. Print the
1246 error and continue with other frames. */
1247 if (success == EXT_LANG_BT_ERROR)
1248 gdbpy_print_stack_or_quit ();
1249 }
1250
1251 return success;
1252 }