1 /* Python frame filters
3 Copyright (C) 2013-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
24 #include "arch-utils.h"
33 #include "mi/mi-cmds.h"
34 #include "python-internal.h"
35 #include "gdbsupport/gdb_optional.h"
43 /* Helper function to extract a symbol, a name and a language
44 definition from a Python object that conforms to the "Symbol Value"
45 interface. OBJ is the Python object to extract the values from.
46 NAME is a pass-through argument where the name of the symbol will
47 be written. NAME is allocated in this function, but the caller is
48 responsible for clean up. SYM is a pass-through argument where the
49 symbol will be written and SYM_BLOCK is a pass-through argument to
50 write the block where the symbol lies in. In the case of the API
51 returning a string, this will be set to NULL. LANGUAGE is also a
52 pass-through argument denoting the language attributed to the
53 Symbol. In the case of SYM being NULL, this will be set to the
54 current language. Returns EXT_LANG_BT_ERROR on error with the
55 appropriate Python exception set, and EXT_LANG_BT_OK on success. */
57 static enum ext_lang_bt_status
58 extract_sym (PyObject
*obj
, gdb::unique_xmalloc_ptr
<char> *name
,
59 struct symbol
**sym
, const struct block
**sym_block
,
60 const struct language_defn
**language
)
62 gdbpy_ref
<> result (PyObject_CallMethod (obj
, "symbol", NULL
));
65 return EXT_LANG_BT_ERROR
;
67 /* For 'symbol' callback, the function can return a symbol or a
69 if (gdbpy_is_string (result
.get ()))
71 *name
= python_string_to_host_string (result
.get ());
74 return EXT_LANG_BT_ERROR
;
75 /* If the API returns a string (and not a symbol), then there is
76 no symbol derived language available and the frame filter has
77 either overridden the symbol with a string, or supplied a
78 entirely synthetic symbol/value pairing. In that case, use
80 *language
= python_language
;
86 /* This type checks 'result' during the conversion so we
87 just call it unconditionally and check the return. */
88 *sym
= symbol_object_to_symbol (result
.get ());
89 /* TODO: currently, we have no way to recover the block in which SYMBOL
90 was found, so we have no block to return. Trying to evaluate SYMBOL
91 will yield an incorrect value when it's located in a FRAME and
92 evaluated from another frame (as permitted in nested functions). */
97 PyErr_SetString (PyExc_RuntimeError
,
98 _("Unexpected value. Expecting a "
99 "gdb.Symbol or a Python string."));
100 return EXT_LANG_BT_ERROR
;
103 /* Duplicate the symbol name, so the caller has consistency
104 in garbage collection. */
105 name
->reset (xstrdup (SYMBOL_PRINT_NAME (*sym
)));
107 /* If a symbol is specified attempt to determine the language
108 from the symbol. If mode is not "auto", then the language
109 has been explicitly set, use that. */
110 if (language_mode
== language_mode_auto
)
111 *language
= language_def (SYMBOL_LANGUAGE (*sym
));
113 *language
= current_language
;
116 return EXT_LANG_BT_OK
;
119 /* Helper function to extract a value from an object that conforms to
120 the "Symbol Value" interface. OBJ is the Python object to extract
121 the value from. VALUE is a pass-through argument where the value
122 will be written. If the object does not have the value attribute,
123 or provides the Python None for a value, VALUE will be set to NULL
124 and this function will return as successful. Returns EXT_LANG_BT_ERROR
125 on error with the appropriate Python exception set, and EXT_LANG_BT_OK on
128 static enum ext_lang_bt_status
129 extract_value (PyObject
*obj
, struct value
**value
)
131 if (PyObject_HasAttrString (obj
, "value"))
133 gdbpy_ref
<> vresult (PyObject_CallMethod (obj
, "value", NULL
));
136 return EXT_LANG_BT_ERROR
;
138 /* The Python code has returned 'None' for a value, so we set
139 value to NULL. This flags that GDB should read the
141 if (vresult
== Py_None
)
144 return EXT_LANG_BT_OK
;
148 *value
= convert_value_from_python (vresult
.get ());
151 return EXT_LANG_BT_ERROR
;
153 return EXT_LANG_BT_OK
;
159 return EXT_LANG_BT_OK
;
162 /* MI prints only certain values according to the type of symbol and
163 also what the user has specified. SYM is the symbol to check, and
164 MI_PRINT_TYPES is an enum specifying what the user wants emitted
165 for the MI command in question. */
167 mi_should_print (struct symbol
*sym
, enum mi_print_types type
)
171 switch (SYMBOL_CLASS (sym
))
174 case LOC_UNDEF
: /* catches errors */
175 case LOC_CONST
: /* constant */
176 case LOC_TYPEDEF
: /* local typedef */
177 case LOC_LABEL
: /* local label */
178 case LOC_BLOCK
: /* local function */
179 case LOC_CONST_BYTES
: /* loc. byte seq. */
180 case LOC_UNRESOLVED
: /* unresolved static */
181 case LOC_OPTIMIZED_OUT
: /* optimized out */
185 case LOC_ARG
: /* argument */
186 case LOC_REF_ARG
: /* reference arg */
187 case LOC_REGPARM_ADDR
: /* indirect register arg */
188 case LOC_LOCAL
: /* stack local */
189 case LOC_STATIC
: /* static */
190 case LOC_REGISTER
: /* register */
191 case LOC_COMPUTED
: /* computed location */
192 if (type
== MI_PRINT_LOCALS
)
193 print_me
= ! SYMBOL_IS_ARGUMENT (sym
);
195 print_me
= SYMBOL_IS_ARGUMENT (sym
);
200 /* Helper function which outputs a type name extracted from VAL to a
201 "type" field in the output stream OUT. OUT is the ui-out structure
202 the type name will be output too, and VAL is the value that the
203 type will be extracted from. */
206 py_print_type (struct ui_out
*out
, struct value
*val
)
208 check_typedef (value_type (val
));
211 type_print (value_type (val
), "", &stb
, -1);
212 out
->field_stream ("type", stb
);
215 /* Helper function which outputs a value to an output field in a
216 stream. OUT is the ui-out structure the value will be output to,
217 VAL is the value that will be printed, OPTS contains the value
218 printing options, ARGS_TYPE is an enumerator describing the
219 argument format, and LANGUAGE is the language_defn that the value
220 will be printed with. */
223 py_print_value (struct ui_out
*out
, struct value
*val
,
224 const struct value_print_options
*opts
,
226 enum ext_lang_frame_args args_type
,
227 const struct language_defn
*language
)
229 int should_print
= 0;
231 /* MI does not print certain values, differentiated by type,
232 depending on what ARGS_TYPE indicates. Test type against option.
233 For CLI print all values. */
234 if (args_type
== MI_PRINT_SIMPLE_VALUES
235 || args_type
== MI_PRINT_ALL_VALUES
)
237 struct type
*type
= check_typedef (value_type (val
));
239 if (args_type
== MI_PRINT_ALL_VALUES
)
241 else if (args_type
== MI_PRINT_SIMPLE_VALUES
242 && TYPE_CODE (type
) != TYPE_CODE_ARRAY
243 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
244 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
247 else if (args_type
!= NO_VALUES
)
254 common_val_print (val
, &stb
, indent
, opts
, language
);
255 out
->field_stream ("value", stb
);
259 /* Helper function to call a Python method and extract an iterator
260 from the result. If the function returns anything but an iterator
261 the exception is preserved and NULL is returned. FILTER is the
262 Python object to call, and FUNC is the name of the method. Returns
263 a PyObject, or NULL on error with the appropriate exception set.
264 This function can return an iterator, or NULL. */
267 get_py_iter_from_func (PyObject
*filter
, const char *func
)
269 if (PyObject_HasAttrString (filter
, func
))
271 gdbpy_ref
<> result (PyObject_CallMethod (filter
, func
, NULL
));
275 if (result
== Py_None
)
277 return result
.release ();
281 return PyObject_GetIter (result
.get ());
291 /* Helper function to output a single frame argument and value to an
292 output stream. This function will account for entry values if the
293 FV parameter is populated, the frame argument has entry values
294 associated with them, and the appropriate "set entry-value"
295 options are set. Will output in CLI or MI like format depending
296 on the type of output stream detected. OUT is the output stream,
297 SYM_NAME is the name of the symbol. If SYM_NAME is populated then
298 it must have an accompanying value in the parameter FV. FA is a
299 frame argument structure. If FA is populated, both SYM_NAME and
300 FV are ignored. OPTS contains the value printing options,
301 ARGS_TYPE is an enumerator describing the argument format,
302 PRINT_ARGS_FIELD is a flag which indicates if we output "ARGS=1"
303 in MI output in commands where both arguments and locals are
307 py_print_single_arg (struct ui_out
*out
,
308 const char *sym_name
,
309 struct frame_arg
*fa
,
311 const struct value_print_options
*opts
,
312 enum ext_lang_frame_args args_type
,
313 int print_args_field
,
314 const struct language_defn
*language
)
320 if (fa
->val
== NULL
&& fa
->error
== NULL
)
322 language
= language_def (SYMBOL_LANGUAGE (fa
->sym
));
328 gdb::optional
<ui_out_emit_tuple
> maybe_tuple
;
330 /* MI has varying rules for tuples, but generally if there is only
331 one element in each item in the list, do not start a tuple. The
332 exception is -stack-list-variables which emits an ARGS="1" field
333 if the value is a frame argument. This is denoted in this
334 function with PRINT_ARGS_FIELD which is flag from the caller to
335 emit the ARGS field. */
336 if (out
->is_mi_like_p ())
338 if (print_args_field
|| args_type
!= NO_VALUES
)
339 maybe_tuple
.emplace (out
, nullptr);
342 annotate_arg_begin ();
344 /* If frame argument is populated, check for entry-values and the
345 entry value options. */
350 fprintf_symbol_filtered (&stb
, SYMBOL_PRINT_NAME (fa
->sym
),
351 SYMBOL_LANGUAGE (fa
->sym
),
352 DMGL_PARAMS
| DMGL_ANSI
);
353 if (fa
->entry_kind
== print_entry_values_compact
)
357 fprintf_symbol_filtered (&stb
, SYMBOL_PRINT_NAME (fa
->sym
),
358 SYMBOL_LANGUAGE (fa
->sym
),
359 DMGL_PARAMS
| DMGL_ANSI
);
361 if (fa
->entry_kind
== print_entry_values_only
362 || fa
->entry_kind
== print_entry_values_compact
)
364 out
->field_stream ("name", stb
);
367 /* Otherwise, just output the name. */
368 out
->field_string ("name", sym_name
);
370 annotate_arg_name_end ();
374 if (print_args_field
)
375 out
->field_signed ("arg", 1);
377 /* For MI print the type, but only for simple values. This seems
378 weird, but this is how MI choose to format the various output
380 if (args_type
== MI_PRINT_SIMPLE_VALUES
&& val
!= NULL
)
381 py_print_type (out
, val
);
384 annotate_arg_value (value_type (val
));
386 /* If the output is to the CLI, and the user option "set print
387 frame-arguments" is set to none, just output "...". */
388 if (! out
->is_mi_like_p () && args_type
== NO_VALUES
)
389 out
->field_string ("value", "...");
392 /* Otherwise, print the value for both MI and the CLI, except
393 for the case of MI_PRINT_NO_VALUES. */
394 if (args_type
!= NO_VALUES
)
398 gdb_assert (fa
!= NULL
&& fa
->error
!= NULL
);
399 out
->field_fmt ("value",
400 _("<error reading variable: %s>"),
404 py_print_value (out
, val
, opts
, 0, args_type
, language
);
409 /* Helper function to loop over frame arguments provided by the
410 "frame_arguments" Python API. Elements in the iterator must
411 conform to the "Symbol Value" interface. ITER is the Python
412 iterable object, OUT is the output stream, ARGS_TYPE is an
413 enumerator describing the argument format, PRINT_ARGS_FIELD is a
414 flag which indicates if we output "ARGS=1" in MI output in commands
415 where both arguments and locals are printed, and FRAME is the
416 backing frame. Returns EXT_LANG_BT_ERROR on error, with any GDB
417 exceptions converted to a Python exception, or EXT_LANG_BT_OK on
420 static enum ext_lang_bt_status
421 enumerate_args (PyObject
*iter
,
423 enum ext_lang_frame_args args_type
,
424 int print_args_field
,
425 struct frame_info
*frame
)
427 struct value_print_options opts
;
429 get_user_print_options (&opts
);
431 if (args_type
== CLI_SCALAR_VALUES
)
433 /* True in "summary" mode, false otherwise. */
439 annotate_frame_args ();
441 /* Collect the first argument outside of the loop, so output of
442 commas in the argument output is correct. At the end of the
443 loop block collect another item from the iterator, and, if it is
444 not null emit a comma. */
445 gdbpy_ref
<> item (PyIter_Next (iter
));
446 if (item
== NULL
&& PyErr_Occurred ())
447 return EXT_LANG_BT_ERROR
;
451 const struct language_defn
*language
;
452 gdb::unique_xmalloc_ptr
<char> sym_name
;
454 const struct block
*sym_block
;
456 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
458 success
= extract_sym (item
.get (), &sym_name
, &sym
, &sym_block
,
460 if (success
== EXT_LANG_BT_ERROR
)
461 return EXT_LANG_BT_ERROR
;
463 success
= extract_value (item
.get (), &val
);
464 if (success
== EXT_LANG_BT_ERROR
)
465 return EXT_LANG_BT_ERROR
;
467 if (sym
&& out
->is_mi_like_p ()
468 && ! mi_should_print (sym
, MI_PRINT_ARGS
))
471 /* If the object did not provide a value, read it using
472 read_frame_args and account for entry values, if any. */
475 struct frame_arg arg
, entryarg
;
477 /* If there is no value, and also no symbol, set error and
481 PyErr_SetString (PyExc_RuntimeError
,
482 _("No symbol or value provided."));
483 return EXT_LANG_BT_ERROR
;
486 read_frame_arg (user_frame_print_options
,
487 sym
, frame
, &arg
, &entryarg
);
489 gdb::unique_xmalloc_ptr
<char> arg_holder (arg
.error
);
490 gdb::unique_xmalloc_ptr
<char> entry_holder (entryarg
.error
);
492 /* The object has not provided a value, so this is a frame
493 argument to be read by GDB. In this case we have to
494 account for entry-values. */
496 if (arg
.entry_kind
!= print_entry_values_only
)
498 py_print_single_arg (out
, NULL
, &arg
,
505 if (entryarg
.entry_kind
!= print_entry_values_no
)
507 if (arg
.entry_kind
!= print_entry_values_only
)
510 out
->wrap_hint (" ");
513 py_print_single_arg (out
, NULL
, &entryarg
, NULL
, &opts
,
514 args_type
, print_args_field
, NULL
);
519 /* If the object has provided a value, we just print that. */
521 py_print_single_arg (out
, sym_name
.get (), NULL
, val
, &opts
,
522 args_type
, print_args_field
,
526 /* Collect the next item from the iterator. If
527 this is the last item, do not print the
529 item
.reset (PyIter_Next (iter
));
532 else if (PyErr_Occurred ())
533 return EXT_LANG_BT_ERROR
;
538 return EXT_LANG_BT_OK
;
542 /* Helper function to loop over variables provided by the
543 "frame_locals" Python API. Elements in the iterable must conform
544 to the "Symbol Value" interface. ITER is the Python iterable
545 object, OUT is the output stream, INDENT is whether we should
546 indent the output (for CLI), ARGS_TYPE is an enumerator describing
547 the argument format, PRINT_ARGS_FIELD is flag which indicates
548 whether to output the ARGS field in the case of
549 -stack-list-variables and FRAME is the backing frame. Returns
550 EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to a Python
551 exception, or EXT_LANG_BT_OK on success. */
553 static enum ext_lang_bt_status
554 enumerate_locals (PyObject
*iter
,
557 enum ext_lang_frame_args args_type
,
558 int print_args_field
,
559 struct frame_info
*frame
)
561 struct value_print_options opts
;
563 get_user_print_options (&opts
);
568 const struct language_defn
*language
;
569 gdb::unique_xmalloc_ptr
<char> sym_name
;
571 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
573 const struct block
*sym_block
;
574 int local_indent
= 8 + (8 * indent
);
575 gdb::optional
<ui_out_emit_tuple
> tuple
;
577 gdbpy_ref
<> item (PyIter_Next (iter
));
581 success
= extract_sym (item
.get (), &sym_name
, &sym
, &sym_block
,
583 if (success
== EXT_LANG_BT_ERROR
)
584 return EXT_LANG_BT_ERROR
;
586 success
= extract_value (item
.get (), &val
);
587 if (success
== EXT_LANG_BT_ERROR
)
588 return EXT_LANG_BT_ERROR
;
590 if (sym
!= NULL
&& out
->is_mi_like_p ()
591 && ! mi_should_print (sym
, MI_PRINT_LOCALS
))
594 /* If the object did not provide a value, read it. */
596 val
= read_var_value (sym
, sym_block
, frame
);
598 /* With PRINT_NO_VALUES, MI does not emit a tuple normally as
599 each output contains only one field. The exception is
600 -stack-list-variables, which always provides a tuple. */
601 if (out
->is_mi_like_p ())
603 if (print_args_field
|| args_type
!= NO_VALUES
)
604 tuple
.emplace (out
, nullptr);
607 /* If the output is not MI we indent locals. */
608 out
->spaces (local_indent
);
609 out
->field_string ("name", sym_name
.get ());
612 if (args_type
== MI_PRINT_SIMPLE_VALUES
)
613 py_print_type (out
, val
);
615 /* CLI always prints values for locals. MI uses the
616 simple/no/all system. */
617 if (! out
->is_mi_like_p ())
619 int val_indent
= (indent
+ 1) * 4;
621 py_print_value (out
, val
, &opts
, val_indent
, args_type
,
626 if (args_type
!= NO_VALUES
)
627 py_print_value (out
, val
, &opts
, 0, args_type
,
634 if (!PyErr_Occurred ())
635 return EXT_LANG_BT_OK
;
637 return EXT_LANG_BT_ERROR
;
640 /* Helper function for -stack-list-variables. Returns EXT_LANG_BT_ERROR on
641 error, or EXT_LANG_BT_OK on success. */
643 static enum ext_lang_bt_status
644 py_mi_print_variables (PyObject
*filter
, struct ui_out
*out
,
645 struct value_print_options
*opts
,
646 enum ext_lang_frame_args args_type
,
647 struct frame_info
*frame
)
649 gdbpy_ref
<> args_iter (get_py_iter_from_func (filter
, "frame_args"));
650 if (args_iter
== NULL
)
651 return EXT_LANG_BT_ERROR
;
653 gdbpy_ref
<> locals_iter (get_py_iter_from_func (filter
, "frame_locals"));
654 if (locals_iter
== NULL
)
655 return EXT_LANG_BT_ERROR
;
657 ui_out_emit_list
list_emitter (out
, "variables");
659 if (args_iter
!= Py_None
660 && (enumerate_args (args_iter
.get (), out
, args_type
, 1, frame
)
661 == EXT_LANG_BT_ERROR
))
662 return EXT_LANG_BT_ERROR
;
664 if (locals_iter
!= Py_None
665 && (enumerate_locals (locals_iter
.get (), out
, 1, args_type
, 1, frame
)
666 == EXT_LANG_BT_ERROR
))
667 return EXT_LANG_BT_ERROR
;
669 return EXT_LANG_BT_OK
;
672 /* Helper function for printing locals. This function largely just
673 creates the wrapping tuple, and calls enumerate_locals. Returns
674 EXT_LANG_BT_ERROR on error, or EXT_LANG_BT_OK on success. */
676 static enum ext_lang_bt_status
677 py_print_locals (PyObject
*filter
,
679 enum ext_lang_frame_args args_type
,
681 struct frame_info
*frame
)
683 gdbpy_ref
<> locals_iter (get_py_iter_from_func (filter
, "frame_locals"));
684 if (locals_iter
== NULL
)
685 return EXT_LANG_BT_ERROR
;
687 ui_out_emit_list
list_emitter (out
, "locals");
689 if (locals_iter
!= Py_None
690 && (enumerate_locals (locals_iter
.get (), out
, indent
, args_type
,
691 0, frame
) == EXT_LANG_BT_ERROR
))
692 return EXT_LANG_BT_ERROR
;
694 return EXT_LANG_BT_OK
;
697 /* Helper function for printing frame arguments. This function
698 largely just creates the wrapping tuple, and calls enumerate_args.
699 Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to
700 a Python exception, or EXT_LANG_BT_OK on success. */
702 static enum ext_lang_bt_status
703 py_print_args (PyObject
*filter
,
705 enum ext_lang_frame_args args_type
,
706 struct frame_info
*frame
)
708 gdbpy_ref
<> args_iter (get_py_iter_from_func (filter
, "frame_args"));
709 if (args_iter
== NULL
)
710 return EXT_LANG_BT_ERROR
;
712 ui_out_emit_list
list_emitter (out
, "args");
714 out
->wrap_hint (" ");
715 annotate_frame_args ();
718 if (args_type
== CLI_PRESENCE
)
720 if (args_iter
!= Py_None
)
722 gdbpy_ref
<> item (PyIter_Next (args_iter
.get ()));
726 else if (PyErr_Occurred ())
727 return EXT_LANG_BT_ERROR
;
730 else if (args_iter
!= Py_None
731 && (enumerate_args (args_iter
.get (), out
, args_type
, 0, frame
)
732 == EXT_LANG_BT_ERROR
))
733 return EXT_LANG_BT_ERROR
;
737 return EXT_LANG_BT_OK
;
740 /* Print a single frame to the designated output stream, detecting
741 whether the output is MI or console, and formatting the output
742 according to the conventions of that protocol. FILTER is the
743 frame-filter associated with this frame. FLAGS is an integer
744 describing the various print options. The FLAGS variables is
745 described in "apply_frame_filter" function. ARGS_TYPE is an
746 enumerator describing the argument format. OUT is the output
747 stream to print, INDENT is the level of indention for this frame
748 (in the case of elided frames), and LEVELS_PRINTED is a hash-table
749 containing all the frames level that have already been printed.
750 If a frame level has been printed, do not print it again (in the
751 case of elided frames). Returns EXT_LANG_BT_ERROR on error, with any
752 GDB exceptions converted to a Python exception, or EXT_LANG_BT_OK
753 on success. It can also throw an exception RETURN_QUIT. */
755 static enum ext_lang_bt_status
756 py_print_frame (PyObject
*filter
, frame_filter_flags flags
,
757 enum ext_lang_frame_args args_type
,
758 struct ui_out
*out
, int indent
, htab_t levels_printed
)
761 CORE_ADDR address
= 0;
762 struct gdbarch
*gdbarch
= NULL
;
763 struct frame_info
*frame
= NULL
;
764 struct value_print_options opts
;
766 int print_level
, print_frame_info
, print_args
, print_locals
;
767 /* Note that the below default in non-mi mode is the same as the
768 default value for the backtrace command (see the call to print_frame_info
769 in backtrace_command_1).
770 Having the same default ensures that 'bt' and 'bt no-filters'
771 have the same behaviour when some filters exist but do not apply
773 enum print_what print_what
774 = out
->is_mi_like_p () ? LOC_AND_ADDRESS
: LOCATION
;
775 gdb::unique_xmalloc_ptr
<char> function_to_free
;
777 /* Extract print settings from FLAGS. */
778 print_level
= (flags
& PRINT_LEVEL
) ? 1 : 0;
779 print_frame_info
= (flags
& PRINT_FRAME_INFO
) ? 1 : 0;
780 print_args
= (flags
& PRINT_ARGS
) ? 1 : 0;
781 print_locals
= (flags
& PRINT_LOCALS
) ? 1 : 0;
783 get_user_print_options (&opts
);
784 if (print_frame_info
)
786 gdb::optional
<enum print_what
> user_frame_info_print_what
;
788 get_user_print_what_frame_info (&user_frame_info_print_what
);
789 if (!out
->is_mi_like_p () && user_frame_info_print_what
.has_value ())
791 /* Use the specific frame information desired by the user. */
792 print_what
= *user_frame_info_print_what
;
796 /* Get the underlying frame. This is needed to determine GDB
797 architecture, and also, in the cases of frame variables/arguments to
798 read them if they returned filter object requires us to do so. */
799 gdbpy_ref
<> py_inf_frame (PyObject_CallMethod (filter
, "inferior_frame",
801 if (py_inf_frame
== NULL
)
802 return EXT_LANG_BT_ERROR
;
804 frame
= frame_object_to_frame_info (py_inf_frame
.get ());
806 return EXT_LANG_BT_ERROR
;
808 symtab_and_line sal
= find_frame_sal (frame
);
810 gdbarch
= get_frame_arch (frame
);
812 /* stack-list-variables. */
813 if (print_locals
&& print_args
&& ! print_frame_info
)
815 if (py_mi_print_variables (filter
, out
, &opts
,
816 args_type
, frame
) == EXT_LANG_BT_ERROR
)
817 return EXT_LANG_BT_ERROR
;
818 return EXT_LANG_BT_OK
;
821 gdb::optional
<ui_out_emit_tuple
> tuple
;
823 /* -stack-list-locals does not require a
824 wrapping frame attribute. */
825 if (print_frame_info
|| (print_args
&& ! print_locals
))
826 tuple
.emplace (out
, "frame");
828 if (print_frame_info
)
830 /* Elided frames are also printed with this function (recursively)
831 and are printed with indention. */
833 out
->spaces (indent
* 4);
835 /* The address is required for frame annotations, and also for
837 if (PyObject_HasAttrString (filter
, "address"))
839 gdbpy_ref
<> paddr (PyObject_CallMethod (filter
, "address", NULL
));
842 return EXT_LANG_BT_ERROR
;
844 if (paddr
!= Py_None
)
846 if (get_addr_from_python (paddr
.get (), &address
) < 0)
847 return EXT_LANG_BT_ERROR
;
854 /* For MI, each piece is controlled individually. */
855 bool location_print
= (print_frame_info
856 && !out
->is_mi_like_p ()
857 && (print_what
== LOCATION
858 || print_what
== SRC_AND_LOC
859 || print_what
== LOC_AND_ADDRESS
860 || print_what
== SHORT_LOCATION
));
862 /* Print frame level. MI does not require the level if
863 locals/variables only are being printed. */
866 || (out
->is_mi_like_p () && (print_frame_info
|| print_args
))))
868 struct frame_info
**slot
;
871 slot
= (struct frame_info
**) htab_find_slot (levels_printed
,
874 level
= frame_relative_level (frame
);
876 /* Check if this frame has already been printed (there are cases
877 where elided synthetic dummy-frames have to 'borrow' the frame
878 architecture from the eliding frame. If that is the case, do
879 not print 'level', but print spaces. */
881 out
->field_skip ("level");
885 annotate_frame_begin (print_level
? level
: 0,
888 out
->field_fmt_signed (2, ui_left
, "level", level
);
892 if (location_print
|| (out
->is_mi_like_p () && print_frame_info
))
894 /* Print address to the address field. If an address is not provided,
896 if (opts
.addressprint
&& has_addr
)
899 || frame_show_address (frame
, sal
)
900 || print_what
== LOC_AND_ADDRESS
)
902 annotate_frame_address ();
903 out
->field_core_addr ("addr", gdbarch
, address
);
904 if (get_frame_pc_masked (frame
))
905 out
->field_string ("pac", " [PAC]");
906 annotate_frame_address_end ();
911 /* Print frame function name. */
912 if (PyObject_HasAttrString (filter
, "function"))
914 gdbpy_ref
<> py_func (PyObject_CallMethod (filter
, "function", NULL
));
915 const char *function
= NULL
;
918 return EXT_LANG_BT_ERROR
;
920 if (gdbpy_is_string (py_func
.get ()))
922 function_to_free
= python_string_to_host_string (py_func
.get ());
924 if (function_to_free
== NULL
)
925 return EXT_LANG_BT_ERROR
;
927 function
= function_to_free
.get ();
929 else if (PyLong_Check (py_func
.get ()))
932 struct bound_minimal_symbol msymbol
;
934 if (get_addr_from_python (py_func
.get (), &addr
) < 0)
935 return EXT_LANG_BT_ERROR
;
937 msymbol
= lookup_minimal_symbol_by_pc (addr
);
938 if (msymbol
.minsym
!= NULL
)
939 function
= MSYMBOL_PRINT_NAME (msymbol
.minsym
);
941 else if (py_func
!= Py_None
)
943 PyErr_SetString (PyExc_RuntimeError
,
944 _("FrameDecorator.function: expecting a " \
945 "String, integer or None."));
946 return EXT_LANG_BT_ERROR
;
949 annotate_frame_function_name ();
950 if (function
== NULL
)
951 out
->field_skip ("func");
953 out
->field_string ("func", function
, ui_out_style_kind::FUNCTION
);
958 /* Frame arguments. Check the result, and error if something went
960 if (print_args
&& (location_print
|| out
->is_mi_like_p ()))
962 if (py_print_args (filter
, out
, args_type
, frame
) == EXT_LANG_BT_ERROR
)
963 return EXT_LANG_BT_ERROR
;
966 /* File name/source/line number information. */
967 bool print_location_source
968 = ((location_print
&& print_what
!= SHORT_LOCATION
)
969 || (out
->is_mi_like_p () && print_frame_info
));
970 if (print_location_source
)
972 annotate_frame_source_begin ();
974 if (PyObject_HasAttrString (filter
, "filename"))
976 gdbpy_ref
<> py_fn (PyObject_CallMethod (filter
, "filename", NULL
));
979 return EXT_LANG_BT_ERROR
;
981 if (py_fn
!= Py_None
)
983 gdb::unique_xmalloc_ptr
<char>
984 filename (python_string_to_host_string (py_fn
.get ()));
986 if (filename
== NULL
)
987 return EXT_LANG_BT_ERROR
;
989 out
->wrap_hint (" ");
991 annotate_frame_source_file ();
992 out
->field_string ("file", filename
.get (),
993 ui_out_style_kind::FILE);
994 annotate_frame_source_file_end ();
998 if (PyObject_HasAttrString (filter
, "line"))
1000 gdbpy_ref
<> py_line (PyObject_CallMethod (filter
, "line", NULL
));
1003 if (py_line
== NULL
)
1004 return EXT_LANG_BT_ERROR
;
1006 if (py_line
!= Py_None
)
1008 line
= PyLong_AsLong (py_line
.get ());
1009 if (PyErr_Occurred ())
1010 return EXT_LANG_BT_ERROR
;
1013 annotate_frame_source_line ();
1014 out
->field_signed ("line", line
);
1017 if (out
->is_mi_like_p ())
1018 out
->field_string ("arch",
1019 (gdbarch_bfd_arch_info (gdbarch
))->printable_name
);
1023 = (! out
->is_mi_like_p ()
1024 && (print_what
== SRC_LINE
|| print_what
== SRC_AND_LOC
));
1027 if (print_location_source
)
1028 out
->text ("\n"); /* Newline after the location source. */
1029 print_source_lines (sal
.symtab
, sal
.line
, sal
.line
+ 1, 0);
1032 /* For MI we need to deal with the "children" list population of
1033 elided frames, so if MI output detected do not send newline. */
1034 if (! out
->is_mi_like_p ())
1036 annotate_frame_end ();
1037 /* print_source_lines has already printed a newline. */
1044 if (py_print_locals (filter
, out
, args_type
, indent
,
1045 frame
) == EXT_LANG_BT_ERROR
)
1046 return EXT_LANG_BT_ERROR
;
1049 if ((flags
& PRINT_HIDE
) == 0)
1051 /* Finally recursively print elided frames, if any. */
1052 gdbpy_ref
<> elided (get_py_iter_from_func (filter
, "elided"));
1054 return EXT_LANG_BT_ERROR
;
1056 if (elided
!= Py_None
)
1060 ui_out_emit_list
inner_list_emiter (out
, "children");
1064 while ((item
= PyIter_Next (elided
.get ())))
1066 gdbpy_ref
<> item_ref (item
);
1068 enum ext_lang_bt_status success
1069 = py_print_frame (item
, flags
, args_type
, out
, indent
,
1072 if (success
== EXT_LANG_BT_ERROR
)
1073 return EXT_LANG_BT_ERROR
;
1075 if (item
== NULL
&& PyErr_Occurred ())
1076 return EXT_LANG_BT_ERROR
;
1080 return EXT_LANG_BT_OK
;
1083 /* Helper function to initiate frame filter invocation at starting
1087 bootstrap_python_frame_filters (struct frame_info
*frame
,
1088 int frame_low
, int frame_high
)
1090 gdbpy_ref
<> frame_obj (frame_info_to_frame_object (frame
));
1091 if (frame_obj
== NULL
)
1094 gdbpy_ref
<> module (PyImport_ImportModule ("gdb.frames"));
1098 gdbpy_ref
<> sort_func (PyObject_GetAttrString (module
.get (),
1099 "execute_frame_filters"));
1100 if (sort_func
== NULL
)
1103 gdbpy_ref
<> py_frame_low (PyInt_FromLong (frame_low
));
1104 if (py_frame_low
== NULL
)
1107 gdbpy_ref
<> py_frame_high (PyInt_FromLong (frame_high
));
1108 if (py_frame_high
== NULL
)
1111 gdbpy_ref
<> iterable (PyObject_CallFunctionObjArgs (sort_func
.get (),
1113 py_frame_low
.get (),
1114 py_frame_high
.get (),
1116 if (iterable
== NULL
)
1119 if (iterable
!= Py_None
)
1120 return PyObject_GetIter (iterable
.get ());
1122 return iterable
.release ();
1125 /* This is the only publicly exported function in this file. FRAME
1126 is the source frame to start frame-filter invocation. FLAGS is an
1127 integer holding the flags for printing. The following elements of
1128 the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
1129 PRINT_LEVEL is a flag indicating whether to print the frame's
1130 relative level in the output. PRINT_FRAME_INFO is a flag that
1131 indicates whether this function should print the frame
1132 information, PRINT_ARGS is a flag that indicates whether to print
1133 frame arguments, and PRINT_LOCALS, likewise, with frame local
1134 variables. ARGS_TYPE is an enumerator describing the argument
1135 format, OUT is the output stream to print. FRAME_LOW is the
1136 beginning of the slice of frames to print, and FRAME_HIGH is the
1137 upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error,
1138 or EXT_LANG_BT_OK on success. */
1140 enum ext_lang_bt_status
1141 gdbpy_apply_frame_filter (const struct extension_language_defn
*extlang
,
1142 struct frame_info
*frame
, frame_filter_flags flags
,
1143 enum ext_lang_frame_args args_type
,
1144 struct ui_out
*out
, int frame_low
, int frame_high
)
1146 struct gdbarch
*gdbarch
= NULL
;
1147 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
1149 if (!gdb_python_initialized
)
1150 return EXT_LANG_BT_NO_FILTERS
;
1154 gdbarch
= get_frame_arch (frame
);
1156 catch (const gdb_exception_error
&except
)
1158 /* Let gdb try to print the stack trace. */
1159 return EXT_LANG_BT_NO_FILTERS
;
1162 gdbpy_enter
enter_py (gdbarch
, current_language
);
1164 /* When we're limiting the number of frames, be careful to request
1165 one extra frame, so that we can print a message if there are more
1167 int frame_countdown
= -1;
1168 if ((flags
& PRINT_MORE_FRAMES
) != 0 && frame_low
>= 0 && frame_high
>= 0)
1171 /* This has an extra +1 because it is checked before a frame is
1173 frame_countdown
= frame_high
- frame_low
+ 1;
1176 gdbpy_ref
<> iterable (bootstrap_python_frame_filters (frame
, frame_low
,
1179 if (iterable
== NULL
)
1181 /* Normally if there is an error GDB prints the exception,
1182 abandons the backtrace and exits. The user can then call "bt
1183 no-filters", and get a default backtrace (it would be
1184 confusing to automatically start a standard backtrace halfway
1185 through a Python filtered backtrace). However in the case
1186 where GDB cannot initialize the frame filters (most likely
1187 due to incorrect auto-load paths), GDB has printed nothing.
1188 In this case it is OK to print the default backtrace after
1189 printing the error message. GDB returns EXT_LANG_BT_NO_FILTERS
1190 here to signify there are no filters after printing the
1191 initialization error. This return code will trigger a
1192 default backtrace. */
1194 gdbpy_print_stack_or_quit ();
1195 return EXT_LANG_BT_NO_FILTERS
;
1198 /* If iterable is None, then there are no frame filters registered.
1199 If this is the case, defer to default GDB printing routines in MI
1201 if (iterable
== Py_None
)
1202 return EXT_LANG_BT_NO_FILTERS
;
1204 htab_up
levels_printed (htab_create (20,
1211 gdbpy_ref
<> item (PyIter_Next (iterable
.get ()));
1215 if (PyErr_Occurred ())
1217 gdbpy_print_stack_or_quit ();
1218 return EXT_LANG_BT_ERROR
;
1223 if (frame_countdown
!= -1)
1225 gdb_assert ((flags
& PRINT_MORE_FRAMES
) != 0);
1227 if (frame_countdown
== 0)
1229 /* We've printed all the frames we were asked to
1230 print, but more frames existed. */
1231 printf_filtered (_("(More stack frames follow...)\n"));
1238 success
= py_print_frame (item
.get (), flags
, args_type
, out
, 0,
1239 levels_printed
.get ());
1241 catch (const gdb_exception_error
&except
)
1243 gdbpy_convert_exception (except
);
1244 success
= EXT_LANG_BT_ERROR
;
1247 /* Do not exit on error printing a single frame. Print the
1248 error and continue with other frames. */
1249 if (success
== EXT_LANG_BT_ERROR
)
1250 gdbpy_print_stack_or_quit ();