1 /* Python frame filters
3 Copyright (C) 2013-2018 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"
31 #include "mi/mi-cmds.h"
32 #include "python-internal.h"
34 #include "common/gdb_optional.h"
42 /* Helper function to extract a symbol, a name and a language
43 definition from a Python object that conforms to the "Symbol Value"
44 interface. OBJ is the Python object to extract the values from.
45 NAME is a pass-through argument where the name of the symbol will
46 be written. NAME is allocated in this function, but the caller is
47 responsible for clean up. SYM is a pass-through argument where the
48 symbol will be written and SYM_BLOCK is a pass-through argument to
49 write the block where the symbol lies in. In the case of the API
50 returning a string, this will be set to NULL. LANGUAGE is also a
51 pass-through argument denoting the language attributed to the
52 Symbol. In the case of SYM being NULL, this will be set to the
53 current language. Returns EXT_LANG_BT_ERROR on error with the
54 appropriate Python exception set, and EXT_LANG_BT_OK on success. */
56 static enum ext_lang_bt_status
57 extract_sym (PyObject
*obj
, gdb::unique_xmalloc_ptr
<char> *name
,
58 struct symbol
**sym
, struct block
**sym_block
,
59 const struct language_defn
**language
)
61 gdbpy_ref
<> result (PyObject_CallMethod (obj
, "symbol", NULL
));
64 return EXT_LANG_BT_ERROR
;
66 /* For 'symbol' callback, the function can return a symbol or a
68 if (gdbpy_is_string (result
.get ()))
70 *name
= python_string_to_host_string (result
.get ());
73 return EXT_LANG_BT_ERROR
;
74 /* If the API returns a string (and not a symbol), then there is
75 no symbol derived language available and the frame filter has
76 either overridden the symbol with a string, or supplied a
77 entirely synthetic symbol/value pairing. In that case, use
79 *language
= python_language
;
85 /* This type checks 'result' during the conversion so we
86 just call it unconditionally and check the return. */
87 *sym
= symbol_object_to_symbol (result
.get ());
88 /* TODO: currently, we have no way to recover the block in which SYMBOL
89 was found, so we have no block to return. Trying to evaluate SYMBOL
90 will yield an incorrect value when it's located in a FRAME and
91 evaluated from another frame (as permitted in nested functions). */
96 PyErr_SetString (PyExc_RuntimeError
,
97 _("Unexpected value. Expecting a "
98 "gdb.Symbol or a Python string."));
99 return EXT_LANG_BT_ERROR
;
102 /* Duplicate the symbol name, so the caller has consistency
103 in garbage collection. */
104 name
->reset (xstrdup (SYMBOL_PRINT_NAME (*sym
)));
106 /* If a symbol is specified attempt to determine the language
107 from the symbol. If mode is not "auto", then the language
108 has been explicitly set, use that. */
109 if (language_mode
== language_mode_auto
)
110 *language
= language_def (SYMBOL_LANGUAGE (*sym
));
112 *language
= current_language
;
115 return EXT_LANG_BT_OK
;
118 /* Helper function to extract a value from an object that conforms to
119 the "Symbol Value" interface. OBJ is the Python object to extract
120 the value from. VALUE is a pass-through argument where the value
121 will be written. If the object does not have the value attribute,
122 or provides the Python None for a value, VALUE will be set to NULL
123 and this function will return as successful. Returns EXT_LANG_BT_ERROR
124 on error with the appropriate Python exception set, and EXT_LANG_BT_OK on
127 static enum ext_lang_bt_status
128 extract_value (PyObject
*obj
, struct value
**value
)
130 if (PyObject_HasAttrString (obj
, "value"))
132 gdbpy_ref
<> vresult (PyObject_CallMethod (obj
, "value", NULL
));
135 return EXT_LANG_BT_ERROR
;
137 /* The Python code has returned 'None' for a value, so we set
138 value to NULL. This flags that GDB should read the
140 if (vresult
== Py_None
)
143 return EXT_LANG_BT_OK
;
147 *value
= convert_value_from_python (vresult
.get ());
150 return EXT_LANG_BT_ERROR
;
152 return EXT_LANG_BT_OK
;
158 return EXT_LANG_BT_OK
;
161 /* MI prints only certain values according to the type of symbol and
162 also what the user has specified. SYM is the symbol to check, and
163 MI_PRINT_TYPES is an enum specifying what the user wants emitted
164 for the MI command in question. */
166 mi_should_print (struct symbol
*sym
, enum mi_print_types type
)
170 switch (SYMBOL_CLASS (sym
))
173 case LOC_UNDEF
: /* catches errors */
174 case LOC_CONST
: /* constant */
175 case LOC_TYPEDEF
: /* local typedef */
176 case LOC_LABEL
: /* local label */
177 case LOC_BLOCK
: /* local function */
178 case LOC_CONST_BYTES
: /* loc. byte seq. */
179 case LOC_UNRESOLVED
: /* unresolved static */
180 case LOC_OPTIMIZED_OUT
: /* optimized out */
184 case LOC_ARG
: /* argument */
185 case LOC_REF_ARG
: /* reference arg */
186 case LOC_REGPARM_ADDR
: /* indirect register arg */
187 case LOC_LOCAL
: /* stack local */
188 case LOC_STATIC
: /* static */
189 case LOC_REGISTER
: /* register */
190 case LOC_COMPUTED
: /* computed location */
191 if (type
== MI_PRINT_LOCALS
)
192 print_me
= ! SYMBOL_IS_ARGUMENT (sym
);
194 print_me
= SYMBOL_IS_ARGUMENT (sym
);
199 /* Helper function which outputs a type name extracted from VAL to a
200 "type" field in the output stream OUT. OUT is the ui-out structure
201 the type name will be output too, and VAL is the value that the
202 type will be extracted from. Returns EXT_LANG_BT_ERROR on error, with
203 any GDB exceptions converted to a Python exception, or EXT_LANG_BT_OK on
206 static enum ext_lang_bt_status
207 py_print_type (struct ui_out
*out
, struct value
*val
)
212 check_typedef (value_type (val
));
215 type_print (value_type (val
), "", &stb
, -1);
216 out
->field_stream ("type", stb
);
218 CATCH (except
, RETURN_MASK_ERROR
)
220 gdbpy_convert_exception (except
);
221 return EXT_LANG_BT_ERROR
;
225 return EXT_LANG_BT_OK
;
228 /* Helper function which outputs a value to an output field in a
229 stream. OUT is the ui-out structure the value will be output to,
230 VAL is the value that will be printed, OPTS contains the value
231 printing options, ARGS_TYPE is an enumerator describing the
232 argument format, and LANGUAGE is the language_defn that the value
233 will be printed with. Returns EXT_LANG_BT_ERROR on error, with any GDB
234 exceptions converted to a Python exception, or EXT_LANG_BT_OK on
237 static enum ext_lang_bt_status
238 py_print_value (struct ui_out
*out
, struct value
*val
,
239 const struct value_print_options
*opts
,
241 enum ext_lang_frame_args args_type
,
242 const struct language_defn
*language
)
244 int should_print
= 0;
246 /* MI does not print certain values, differentiated by type,
247 depending on what ARGS_TYPE indicates. Test type against option.
248 For CLI print all values. */
249 if (args_type
== MI_PRINT_SIMPLE_VALUES
250 || args_type
== MI_PRINT_ALL_VALUES
)
252 struct type
*type
= NULL
;
256 type
= check_typedef (value_type (val
));
258 CATCH (except
, RETURN_MASK_ERROR
)
260 gdbpy_convert_exception (except
);
261 return EXT_LANG_BT_ERROR
;
265 if (args_type
== MI_PRINT_ALL_VALUES
)
267 else if (args_type
== MI_PRINT_SIMPLE_VALUES
268 && TYPE_CODE (type
) != TYPE_CODE_ARRAY
269 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
270 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
273 else if (args_type
!= NO_VALUES
)
282 common_val_print (val
, &stb
, indent
, opts
, language
);
283 out
->field_stream ("value", stb
);
285 CATCH (except
, RETURN_MASK_ERROR
)
287 gdbpy_convert_exception (except
);
288 return EXT_LANG_BT_ERROR
;
293 return EXT_LANG_BT_OK
;
296 /* Helper function to call a Python method and extract an iterator
297 from the result. If the function returns anything but an iterator
298 the exception is preserved and NULL is returned. FILTER is the
299 Python object to call, and FUNC is the name of the method. Returns
300 a PyObject, or NULL on error with the appropriate exception set.
301 This function can return an iterator, or NULL. */
304 get_py_iter_from_func (PyObject
*filter
, const char *func
)
306 if (PyObject_HasAttrString (filter
, func
))
308 gdbpy_ref
<> result (PyObject_CallMethod (filter
, func
, NULL
));
312 if (result
== Py_None
)
314 return result
.release ();
318 return PyObject_GetIter (result
.get ());
328 /* Helper function to output a single frame argument and value to an
329 output stream. This function will account for entry values if the
330 FV parameter is populated, the frame argument has entry values
331 associated with them, and the appropriate "set entry-value"
332 options are set. Will output in CLI or MI like format depending
333 on the type of output stream detected. OUT is the output stream,
334 SYM_NAME is the name of the symbol. If SYM_NAME is populated then
335 it must have an accompanying value in the parameter FV. FA is a
336 frame argument structure. If FA is populated, both SYM_NAME and
337 FV are ignored. OPTS contains the value printing options,
338 ARGS_TYPE is an enumerator describing the argument format,
339 PRINT_ARGS_FIELD is a flag which indicates if we output "ARGS=1"
340 in MI output in commands where both arguments and locals are
341 printed. Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions
342 converted to a Python exception, or EXT_LANG_BT_OK on success. */
344 static enum ext_lang_bt_status
345 py_print_single_arg (struct ui_out
*out
,
346 const char *sym_name
,
347 struct frame_arg
*fa
,
349 const struct value_print_options
*opts
,
350 enum ext_lang_frame_args args_type
,
351 int print_args_field
,
352 const struct language_defn
*language
)
355 enum ext_lang_bt_status retval
= EXT_LANG_BT_OK
;
359 if (fa
->val
== NULL
&& fa
->error
== NULL
)
360 return EXT_LANG_BT_OK
;
361 language
= language_def (SYMBOL_LANGUAGE (fa
->sym
));
369 gdb::optional
<ui_out_emit_tuple
> maybe_tuple
;
371 /* MI has varying rules for tuples, but generally if there is only
372 one element in each item in the list, do not start a tuple. The
373 exception is -stack-list-variables which emits an ARGS="1" field
374 if the value is a frame argument. This is denoted in this
375 function with PRINT_ARGS_FIELD which is flag from the caller to
376 emit the ARGS field. */
377 if (out
->is_mi_like_p ())
379 if (print_args_field
|| args_type
!= NO_VALUES
)
380 maybe_tuple
.emplace (out
, nullptr);
383 annotate_arg_begin ();
385 /* If frame argument is populated, check for entry-values and the
386 entry value options. */
391 fprintf_symbol_filtered (&stb
, SYMBOL_PRINT_NAME (fa
->sym
),
392 SYMBOL_LANGUAGE (fa
->sym
),
393 DMGL_PARAMS
| DMGL_ANSI
);
394 if (fa
->entry_kind
== print_entry_values_compact
)
398 fprintf_symbol_filtered (&stb
, SYMBOL_PRINT_NAME (fa
->sym
),
399 SYMBOL_LANGUAGE (fa
->sym
),
400 DMGL_PARAMS
| DMGL_ANSI
);
402 if (fa
->entry_kind
== print_entry_values_only
403 || fa
->entry_kind
== print_entry_values_compact
)
405 out
->field_stream ("name", stb
);
408 /* Otherwise, just output the name. */
409 out
->field_string ("name", sym_name
);
411 annotate_arg_name_end ();
413 if (! out
->is_mi_like_p ())
416 if (print_args_field
)
417 out
->field_int ("arg", 1);
419 /* For MI print the type, but only for simple values. This seems
420 weird, but this is how MI choose to format the various output
422 if (args_type
== MI_PRINT_SIMPLE_VALUES
&& val
!= NULL
)
424 if (py_print_type (out
, val
) == EXT_LANG_BT_ERROR
)
425 retval
= EXT_LANG_BT_ERROR
;
428 if (retval
!= EXT_LANG_BT_ERROR
)
431 annotate_arg_value (value_type (val
));
433 /* If the output is to the CLI, and the user option "set print
434 frame-arguments" is set to none, just output "...". */
435 if (! out
->is_mi_like_p () && args_type
== NO_VALUES
)
436 out
->field_string ("value", "...");
439 /* Otherwise, print the value for both MI and the CLI, except
440 for the case of MI_PRINT_NO_VALUES. */
441 if (args_type
!= NO_VALUES
)
445 gdb_assert (fa
!= NULL
&& fa
->error
!= NULL
);
446 out
->field_fmt ("value",
447 _("<error reading variable: %s>"),
450 else if (py_print_value (out
, val
, opts
, 0, args_type
, language
)
451 == EXT_LANG_BT_ERROR
)
452 retval
= EXT_LANG_BT_ERROR
;
457 CATCH (except
, RETURN_MASK_ERROR
)
459 gdbpy_convert_exception (except
);
466 /* Helper function to loop over frame arguments provided by the
467 "frame_arguments" Python API. Elements in the iterator must
468 conform to the "Symbol Value" interface. ITER is the Python
469 iterable object, OUT is the output stream, ARGS_TYPE is an
470 enumerator describing the argument format, PRINT_ARGS_FIELD is a
471 flag which indicates if we output "ARGS=1" in MI output in commands
472 where both arguments and locals are printed, and FRAME is the
473 backing frame. Returns EXT_LANG_BT_ERROR on error, with any GDB
474 exceptions converted to a Python exception, or EXT_LANG_BT_OK on
477 static enum ext_lang_bt_status
478 enumerate_args (PyObject
*iter
,
480 enum ext_lang_frame_args args_type
,
481 int print_args_field
,
482 struct frame_info
*frame
)
484 struct value_print_options opts
;
486 get_user_print_options (&opts
);
488 if (args_type
== CLI_SCALAR_VALUES
)
490 /* True in "summary" mode, false otherwise. */
498 annotate_frame_args ();
500 CATCH (except
, RETURN_MASK_ERROR
)
502 gdbpy_convert_exception (except
);
503 return EXT_LANG_BT_ERROR
;
507 /* Collect the first argument outside of the loop, so output of
508 commas in the argument output is correct. At the end of the
509 loop block collect another item from the iterator, and, if it is
510 not null emit a comma. */
511 gdbpy_ref
<> item (PyIter_Next (iter
));
512 if (item
== NULL
&& PyErr_Occurred ())
513 return EXT_LANG_BT_ERROR
;
517 const struct language_defn
*language
;
518 gdb::unique_xmalloc_ptr
<char> sym_name
;
520 struct block
*sym_block
;
522 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
524 success
= extract_sym (item
.get (), &sym_name
, &sym
, &sym_block
,
526 if (success
== EXT_LANG_BT_ERROR
)
527 return EXT_LANG_BT_ERROR
;
529 success
= extract_value (item
.get (), &val
);
530 if (success
== EXT_LANG_BT_ERROR
)
531 return EXT_LANG_BT_ERROR
;
533 if (sym
&& out
->is_mi_like_p ()
534 && ! mi_should_print (sym
, MI_PRINT_ARGS
))
537 /* If the object did not provide a value, read it using
538 read_frame_args and account for entry values, if any. */
541 struct frame_arg arg
, entryarg
;
543 /* If there is no value, and also no symbol, set error and
547 PyErr_SetString (PyExc_RuntimeError
,
548 _("No symbol or value provided."));
549 return EXT_LANG_BT_ERROR
;
554 read_frame_arg (sym
, frame
, &arg
, &entryarg
);
556 CATCH (except
, RETURN_MASK_ERROR
)
558 gdbpy_convert_exception (except
);
559 return EXT_LANG_BT_ERROR
;
563 gdb::unique_xmalloc_ptr
<char> arg_holder (arg
.error
);
564 gdb::unique_xmalloc_ptr
<char> entry_holder (entryarg
.error
);
566 /* The object has not provided a value, so this is a frame
567 argument to be read by GDB. In this case we have to
568 account for entry-values. */
570 if (arg
.entry_kind
!= print_entry_values_only
)
572 if (py_print_single_arg (out
, NULL
, &arg
,
576 NULL
) == EXT_LANG_BT_ERROR
)
577 return EXT_LANG_BT_ERROR
;
580 if (entryarg
.entry_kind
!= print_entry_values_no
)
582 if (arg
.entry_kind
!= print_entry_values_only
)
587 out
->wrap_hint (" ");
589 CATCH (except
, RETURN_MASK_ERROR
)
591 gdbpy_convert_exception (except
);
592 return EXT_LANG_BT_ERROR
;
597 if (py_print_single_arg (out
, NULL
, &entryarg
, NULL
, &opts
,
598 args_type
, print_args_field
, NULL
)
599 == EXT_LANG_BT_ERROR
)
600 return EXT_LANG_BT_ERROR
;
605 /* If the object has provided a value, we just print that. */
608 if (py_print_single_arg (out
, sym_name
.get (), NULL
, val
, &opts
,
609 args_type
, print_args_field
,
610 language
) == EXT_LANG_BT_ERROR
)
611 return EXT_LANG_BT_ERROR
;
615 /* Collect the next item from the iterator. If
616 this is the last item, do not print the
618 item
.reset (PyIter_Next (iter
));
625 CATCH (except
, RETURN_MASK_ERROR
)
627 gdbpy_convert_exception (except
);
628 return EXT_LANG_BT_ERROR
;
632 else if (PyErr_Occurred ())
633 return EXT_LANG_BT_ERROR
;
639 CATCH (except
, RETURN_MASK_ERROR
)
641 gdbpy_convert_exception (except
);
642 return EXT_LANG_BT_ERROR
;
647 return EXT_LANG_BT_OK
;
651 /* Helper function to loop over variables provided by the
652 "frame_locals" Python API. Elements in the iterable must conform
653 to the "Symbol Value" interface. ITER is the Python iterable
654 object, OUT is the output stream, INDENT is whether we should
655 indent the output (for CLI), ARGS_TYPE is an enumerator describing
656 the argument format, PRINT_ARGS_FIELD is flag which indicates
657 whether to output the ARGS field in the case of
658 -stack-list-variables and FRAME is the backing frame. Returns
659 EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to a Python
660 exception, or EXT_LANG_BT_OK on success. */
662 static enum ext_lang_bt_status
663 enumerate_locals (PyObject
*iter
,
666 enum ext_lang_frame_args args_type
,
667 int print_args_field
,
668 struct frame_info
*frame
)
670 struct value_print_options opts
;
672 get_user_print_options (&opts
);
677 const struct language_defn
*language
;
678 gdb::unique_xmalloc_ptr
<char> sym_name
;
680 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
682 struct block
*sym_block
;
683 int local_indent
= 8 + (8 * indent
);
684 gdb::optional
<ui_out_emit_tuple
> tuple
;
686 gdbpy_ref
<> item (PyIter_Next (iter
));
690 success
= extract_sym (item
.get (), &sym_name
, &sym
, &sym_block
,
692 if (success
== EXT_LANG_BT_ERROR
)
693 return EXT_LANG_BT_ERROR
;
695 success
= extract_value (item
.get (), &val
);
696 if (success
== EXT_LANG_BT_ERROR
)
697 return EXT_LANG_BT_ERROR
;
699 if (sym
!= NULL
&& out
->is_mi_like_p ()
700 && ! mi_should_print (sym
, MI_PRINT_LOCALS
))
703 /* If the object did not provide a value, read it. */
708 val
= read_var_value (sym
, sym_block
, frame
);
710 CATCH (except
, RETURN_MASK_ERROR
)
712 gdbpy_convert_exception (except
);
713 return EXT_LANG_BT_ERROR
;
718 /* With PRINT_NO_VALUES, MI does not emit a tuple normally as
719 each output contains only one field. The exception is
720 -stack-list-variables, which always provides a tuple. */
721 if (out
->is_mi_like_p ())
723 if (print_args_field
|| args_type
!= NO_VALUES
)
724 tuple
.emplace (out
, nullptr);
728 if (! out
->is_mi_like_p ())
730 /* If the output is not MI we indent locals. */
731 out
->spaces (local_indent
);
734 out
->field_string ("name", sym_name
.get ());
736 if (! out
->is_mi_like_p ())
739 CATCH (except
, RETURN_MASK_ERROR
)
741 gdbpy_convert_exception (except
);
742 return EXT_LANG_BT_ERROR
;
746 if (args_type
== MI_PRINT_SIMPLE_VALUES
)
748 if (py_print_type (out
, val
) == EXT_LANG_BT_ERROR
)
749 return EXT_LANG_BT_ERROR
;
752 /* CLI always prints values for locals. MI uses the
753 simple/no/all system. */
754 if (! out
->is_mi_like_p ())
756 int val_indent
= (indent
+ 1) * 4;
758 if (py_print_value (out
, val
, &opts
, val_indent
, args_type
,
759 language
) == EXT_LANG_BT_ERROR
)
760 return EXT_LANG_BT_ERROR
;
764 if (args_type
!= NO_VALUES
)
766 if (py_print_value (out
, val
, &opts
, 0, args_type
,
767 language
) == EXT_LANG_BT_ERROR
)
768 return EXT_LANG_BT_ERROR
;
776 CATCH (except
, RETURN_MASK_ERROR
)
778 gdbpy_convert_exception (except
);
779 return EXT_LANG_BT_ERROR
;
784 if (!PyErr_Occurred ())
785 return EXT_LANG_BT_OK
;
787 return EXT_LANG_BT_ERROR
;
790 /* Helper function for -stack-list-variables. Returns EXT_LANG_BT_ERROR on
791 error, or EXT_LANG_BT_OK on success. */
793 static enum ext_lang_bt_status
794 py_mi_print_variables (PyObject
*filter
, struct ui_out
*out
,
795 struct value_print_options
*opts
,
796 enum ext_lang_frame_args args_type
,
797 struct frame_info
*frame
)
799 gdbpy_ref
<> args_iter (get_py_iter_from_func (filter
, "frame_args"));
800 if (args_iter
== NULL
)
801 return EXT_LANG_BT_ERROR
;
803 gdbpy_ref
<> locals_iter (get_py_iter_from_func (filter
, "frame_locals"));
804 if (locals_iter
== NULL
)
805 return EXT_LANG_BT_ERROR
;
807 ui_out_emit_list
list_emitter (out
, "variables");
809 if (args_iter
!= Py_None
810 && (enumerate_args (args_iter
.get (), out
, args_type
, 1, frame
)
811 == EXT_LANG_BT_ERROR
))
812 return EXT_LANG_BT_ERROR
;
814 if (locals_iter
!= Py_None
815 && (enumerate_locals (locals_iter
.get (), out
, 1, args_type
, 1, frame
)
816 == EXT_LANG_BT_ERROR
))
817 return EXT_LANG_BT_ERROR
;
819 return EXT_LANG_BT_OK
;
822 /* Helper function for printing locals. This function largely just
823 creates the wrapping tuple, and calls enumerate_locals. Returns
824 EXT_LANG_BT_ERROR on error, or EXT_LANG_BT_OK on success. */
826 static enum ext_lang_bt_status
827 py_print_locals (PyObject
*filter
,
829 enum ext_lang_frame_args args_type
,
831 struct frame_info
*frame
)
833 gdbpy_ref
<> locals_iter (get_py_iter_from_func (filter
, "frame_locals"));
834 if (locals_iter
== NULL
)
835 return EXT_LANG_BT_ERROR
;
837 ui_out_emit_list
list_emitter (out
, "locals");
839 if (locals_iter
!= Py_None
840 && (enumerate_locals (locals_iter
.get (), out
, indent
, args_type
,
841 0, frame
) == EXT_LANG_BT_ERROR
))
842 return EXT_LANG_BT_ERROR
;
844 return EXT_LANG_BT_OK
;
847 /* Helper function for printing frame arguments. This function
848 largely just creates the wrapping tuple, and calls enumerate_args.
849 Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to
850 a Python exception, or EXT_LANG_BT_OK on success. */
852 static enum ext_lang_bt_status
853 py_print_args (PyObject
*filter
,
855 enum ext_lang_frame_args args_type
,
856 struct frame_info
*frame
)
858 gdbpy_ref
<> args_iter (get_py_iter_from_func (filter
, "frame_args"));
859 if (args_iter
== NULL
)
860 return EXT_LANG_BT_ERROR
;
862 ui_out_emit_list
list_emitter (out
, "args");
866 annotate_frame_args ();
867 if (! out
->is_mi_like_p ())
870 CATCH (except
, RETURN_MASK_ERROR
)
872 gdbpy_convert_exception (except
);
873 return EXT_LANG_BT_ERROR
;
877 if (args_iter
!= Py_None
878 && (enumerate_args (args_iter
.get (), out
, args_type
, 0, frame
)
879 == EXT_LANG_BT_ERROR
))
880 return EXT_LANG_BT_ERROR
;
884 if (! out
->is_mi_like_p ())
887 CATCH (except
, RETURN_MASK_ERROR
)
889 gdbpy_convert_exception (except
);
890 return EXT_LANG_BT_ERROR
;
894 return EXT_LANG_BT_OK
;
897 /* Print a single frame to the designated output stream, detecting
898 whether the output is MI or console, and formatting the output
899 according to the conventions of that protocol. FILTER is the
900 frame-filter associated with this frame. FLAGS is an integer
901 describing the various print options. The FLAGS variables is
902 described in "apply_frame_filter" function. ARGS_TYPE is an
903 enumerator describing the argument format. OUT is the output
904 stream to print, INDENT is the level of indention for this frame
905 (in the case of elided frames), and LEVELS_PRINTED is a hash-table
906 containing all the frames level that have already been printed.
907 If a frame level has been printed, do not print it again (in the
908 case of elided frames). Returns EXT_LANG_BT_ERROR on error, with any
909 GDB exceptions converted to a Python exception, or EXT_LANG_BT_OK
910 on success. It can also throw an exception RETURN_QUIT. */
912 static enum ext_lang_bt_status
913 py_print_frame (PyObject
*filter
, frame_filter_flags flags
,
914 enum ext_lang_frame_args args_type
,
915 struct ui_out
*out
, int indent
, htab_t levels_printed
)
918 CORE_ADDR address
= 0;
919 struct gdbarch
*gdbarch
= NULL
;
920 struct frame_info
*frame
= NULL
;
921 struct value_print_options opts
;
922 int print_level
, print_frame_info
, print_args
, print_locals
;
923 gdb::unique_xmalloc_ptr
<char> function_to_free
;
925 /* Extract print settings from FLAGS. */
926 print_level
= (flags
& PRINT_LEVEL
) ? 1 : 0;
927 print_frame_info
= (flags
& PRINT_FRAME_INFO
) ? 1 : 0;
928 print_args
= (flags
& PRINT_ARGS
) ? 1 : 0;
929 print_locals
= (flags
& PRINT_LOCALS
) ? 1 : 0;
931 get_user_print_options (&opts
);
933 /* Get the underlying frame. This is needed to determine GDB
934 architecture, and also, in the cases of frame variables/arguments to
935 read them if they returned filter object requires us to do so. */
936 gdbpy_ref
<> py_inf_frame (PyObject_CallMethod (filter
, "inferior_frame",
938 if (py_inf_frame
== NULL
)
939 return EXT_LANG_BT_ERROR
;
941 frame
= frame_object_to_frame_info (py_inf_frame
.get ());
943 return EXT_LANG_BT_ERROR
;
947 gdbarch
= get_frame_arch (frame
);
949 CATCH (except
, RETURN_MASK_ERROR
)
951 gdbpy_convert_exception (except
);
952 return EXT_LANG_BT_ERROR
;
956 /* stack-list-variables. */
957 if (print_locals
&& print_args
&& ! print_frame_info
)
959 if (py_mi_print_variables (filter
, out
, &opts
,
960 args_type
, frame
) == EXT_LANG_BT_ERROR
)
961 return EXT_LANG_BT_ERROR
;
962 return EXT_LANG_BT_OK
;
965 gdb::optional
<ui_out_emit_tuple
> tuple
;
967 /* -stack-list-locals does not require a
968 wrapping frame attribute. */
969 if (print_frame_info
|| (print_args
&& ! print_locals
))
970 tuple
.emplace (out
, "frame");
972 if (print_frame_info
)
974 /* Elided frames are also printed with this function (recursively)
975 and are printed with indention. */
980 out
->spaces (indent
* 4);
982 CATCH (except
, RETURN_MASK_ERROR
)
984 gdbpy_convert_exception (except
);
985 return EXT_LANG_BT_ERROR
;
990 /* The address is required for frame annotations, and also for
992 if (PyObject_HasAttrString (filter
, "address"))
994 gdbpy_ref
<> paddr (PyObject_CallMethod (filter
, "address", NULL
));
997 return EXT_LANG_BT_ERROR
;
999 if (paddr
!= Py_None
)
1001 if (get_addr_from_python (paddr
.get (), &address
) < 0)
1002 return EXT_LANG_BT_ERROR
;
1009 /* Print frame level. MI does not require the level if
1010 locals/variables only are being printed. */
1011 if ((print_frame_info
|| print_args
) && print_level
)
1013 struct frame_info
**slot
;
1016 slot
= (struct frame_info
**) htab_find_slot (levels_printed
,
1020 level
= frame_relative_level (frame
);
1022 /* Check if this frame has already been printed (there are cases
1023 where elided synthetic dummy-frames have to 'borrow' the frame
1024 architecture from the eliding frame. If that is the case, do
1025 not print 'level', but print spaces. */
1027 out
->field_skip ("level");
1031 annotate_frame_begin (print_level
? level
: 0,
1034 out
->field_fmt_int (2, ui_left
, "level",
1038 CATCH (except
, RETURN_MASK_ERROR
)
1040 gdbpy_convert_exception (except
);
1041 return EXT_LANG_BT_ERROR
;
1046 if (print_frame_info
)
1048 /* Print address to the address field. If an address is not provided,
1050 if (opts
.addressprint
&& has_addr
)
1054 annotate_frame_address ();
1055 out
->field_core_addr ("addr", gdbarch
, address
);
1056 annotate_frame_address_end ();
1059 CATCH (except
, RETURN_MASK_ERROR
)
1061 gdbpy_convert_exception (except
);
1062 return EXT_LANG_BT_ERROR
;
1067 /* Print frame function name. */
1068 if (PyObject_HasAttrString (filter
, "function"))
1070 gdbpy_ref
<> py_func (PyObject_CallMethod (filter
, "function", NULL
));
1071 const char *function
= NULL
;
1073 if (py_func
== NULL
)
1074 return EXT_LANG_BT_ERROR
;
1076 if (gdbpy_is_string (py_func
.get ()))
1078 function_to_free
= python_string_to_host_string (py_func
.get ());
1080 if (function_to_free
== NULL
)
1081 return EXT_LANG_BT_ERROR
;
1083 function
= function_to_free
.get ();
1085 else if (PyLong_Check (py_func
.get ()))
1088 struct bound_minimal_symbol msymbol
;
1090 if (get_addr_from_python (py_func
.get (), &addr
) < 0)
1091 return EXT_LANG_BT_ERROR
;
1093 msymbol
= lookup_minimal_symbol_by_pc (addr
);
1094 if (msymbol
.minsym
!= NULL
)
1095 function
= MSYMBOL_PRINT_NAME (msymbol
.minsym
);
1097 else if (py_func
!= Py_None
)
1099 PyErr_SetString (PyExc_RuntimeError
,
1100 _("FrameDecorator.function: expecting a " \
1101 "String, integer or None."));
1102 return EXT_LANG_BT_ERROR
;
1107 annotate_frame_function_name ();
1108 if (function
== NULL
)
1109 out
->field_skip ("func");
1111 out
->field_string ("func", function
);
1113 CATCH (except
, RETURN_MASK_ERROR
)
1115 gdbpy_convert_exception (except
);
1116 return EXT_LANG_BT_ERROR
;
1123 /* Frame arguments. Check the result, and error if something went
1127 if (py_print_args (filter
, out
, args_type
, frame
) == EXT_LANG_BT_ERROR
)
1128 return EXT_LANG_BT_ERROR
;
1131 /* File name/source/line number information. */
1132 if (print_frame_info
)
1136 annotate_frame_source_begin ();
1138 CATCH (except
, RETURN_MASK_ERROR
)
1140 gdbpy_convert_exception (except
);
1141 return EXT_LANG_BT_ERROR
;
1145 if (PyObject_HasAttrString (filter
, "filename"))
1147 gdbpy_ref
<> py_fn (PyObject_CallMethod (filter
, "filename", NULL
));
1150 return EXT_LANG_BT_ERROR
;
1152 if (py_fn
!= Py_None
)
1154 gdb::unique_xmalloc_ptr
<char>
1155 filename (python_string_to_host_string (py_fn
.get ()));
1157 if (filename
== NULL
)
1158 return EXT_LANG_BT_ERROR
;
1162 out
->wrap_hint (" ");
1164 annotate_frame_source_file ();
1165 out
->field_string ("file", filename
.get ());
1166 annotate_frame_source_file_end ();
1168 CATCH (except
, RETURN_MASK_ERROR
)
1170 gdbpy_convert_exception (except
);
1171 return EXT_LANG_BT_ERROR
;
1177 if (PyObject_HasAttrString (filter
, "line"))
1179 gdbpy_ref
<> py_line (PyObject_CallMethod (filter
, "line", NULL
));
1182 if (py_line
== NULL
)
1183 return EXT_LANG_BT_ERROR
;
1185 if (py_line
!= Py_None
)
1187 line
= PyLong_AsLong (py_line
.get ());
1188 if (PyErr_Occurred ())
1189 return EXT_LANG_BT_ERROR
;
1194 annotate_frame_source_line ();
1195 out
->field_int ("line", line
);
1197 CATCH (except
, RETURN_MASK_ERROR
)
1199 gdbpy_convert_exception (except
);
1200 return EXT_LANG_BT_ERROR
;
1207 /* For MI we need to deal with the "children" list population of
1208 elided frames, so if MI output detected do not send newline. */
1209 if (! out
->is_mi_like_p ())
1213 annotate_frame_end ();
1216 CATCH (except
, RETURN_MASK_ERROR
)
1218 gdbpy_convert_exception (except
);
1219 return EXT_LANG_BT_ERROR
;
1226 if (py_print_locals (filter
, out
, args_type
, indent
,
1227 frame
) == EXT_LANG_BT_ERROR
)
1228 return EXT_LANG_BT_ERROR
;
1231 if ((flags
& PRINT_HIDE
) == 0)
1233 /* Finally recursively print elided frames, if any. */
1234 gdbpy_ref
<> elided (get_py_iter_from_func (filter
, "elided"));
1236 return EXT_LANG_BT_ERROR
;
1238 if (elided
!= Py_None
)
1242 ui_out_emit_list
inner_list_emiter (out
, "children");
1244 if (! out
->is_mi_like_p ())
1247 while ((item
= PyIter_Next (elided
.get ())))
1249 gdbpy_ref
<> item_ref (item
);
1251 enum ext_lang_bt_status success
1252 = py_print_frame (item
, flags
, args_type
, out
, indent
,
1255 if (success
== EXT_LANG_BT_ERROR
)
1256 return EXT_LANG_BT_ERROR
;
1258 if (item
== NULL
&& PyErr_Occurred ())
1259 return EXT_LANG_BT_ERROR
;
1263 return EXT_LANG_BT_OK
;
1266 /* Helper function to initiate frame filter invocation at starting
1270 bootstrap_python_frame_filters (struct frame_info
*frame
,
1271 int frame_low
, int frame_high
)
1273 gdbpy_ref
<> frame_obj (frame_info_to_frame_object (frame
));
1274 if (frame_obj
== NULL
)
1277 gdbpy_ref
<> module (PyImport_ImportModule ("gdb.frames"));
1281 gdbpy_ref
<> sort_func (PyObject_GetAttrString (module
.get (),
1282 "execute_frame_filters"));
1283 if (sort_func
== NULL
)
1286 gdbpy_ref
<> py_frame_low (PyInt_FromLong (frame_low
));
1287 if (py_frame_low
== NULL
)
1290 gdbpy_ref
<> py_frame_high (PyInt_FromLong (frame_high
));
1291 if (py_frame_high
== NULL
)
1294 gdbpy_ref
<> iterable (PyObject_CallFunctionObjArgs (sort_func
.get (),
1296 py_frame_low
.get (),
1297 py_frame_high
.get (),
1299 if (iterable
== NULL
)
1302 if (iterable
!= Py_None
)
1303 return PyObject_GetIter (iterable
.get ());
1305 return iterable
.release ();
1308 /* A helper function that will either print an exception or, if it is
1309 a KeyboardException, throw a quit. This can only be called when
1310 the Python exception is set. */
1313 throw_quit_or_print_exception ()
1315 if (PyErr_ExceptionMatches (PyExc_KeyboardInterrupt
))
1318 throw_quit ("Quit");
1320 gdbpy_print_stack ();
1323 /* This is the only publicly exported function in this file. FRAME
1324 is the source frame to start frame-filter invocation. FLAGS is an
1325 integer holding the flags for printing. The following elements of
1326 the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
1327 PRINT_LEVEL is a flag indicating whether to print the frame's
1328 relative level in the output. PRINT_FRAME_INFO is a flag that
1329 indicates whether this function should print the frame
1330 information, PRINT_ARGS is a flag that indicates whether to print
1331 frame arguments, and PRINT_LOCALS, likewise, with frame local
1332 variables. ARGS_TYPE is an enumerator describing the argument
1333 format, OUT is the output stream to print. FRAME_LOW is the
1334 beginning of the slice of frames to print, and FRAME_HIGH is the
1335 upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error,
1336 or EXT_LANG_BT_OK on success. */
1338 enum ext_lang_bt_status
1339 gdbpy_apply_frame_filter (const struct extension_language_defn
*extlang
,
1340 struct frame_info
*frame
, frame_filter_flags flags
,
1341 enum ext_lang_frame_args args_type
,
1342 struct ui_out
*out
, int frame_low
, int frame_high
)
1344 struct gdbarch
*gdbarch
= NULL
;
1345 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
1347 if (!gdb_python_initialized
)
1348 return EXT_LANG_BT_NO_FILTERS
;
1352 gdbarch
= get_frame_arch (frame
);
1354 CATCH (except
, RETURN_MASK_ERROR
)
1356 /* Let gdb try to print the stack trace. */
1357 return EXT_LANG_BT_NO_FILTERS
;
1361 gdbpy_enter
enter_py (gdbarch
, current_language
);
1363 /* When we're limiting the number of frames, be careful to request
1364 one extra frame, so that we can print a message if there are more
1366 int frame_countdown
= -1;
1367 if ((flags
& PRINT_MORE_FRAMES
) != 0 && frame_low
>= 0 && frame_high
>= 0)
1370 /* This has an extra +1 because it is checked before a frame is
1372 frame_countdown
= frame_high
- frame_low
+ 1;
1375 gdbpy_ref
<> iterable (bootstrap_python_frame_filters (frame
, frame_low
,
1378 if (iterable
== NULL
)
1380 /* Normally if there is an error GDB prints the exception,
1381 abandons the backtrace and exits. The user can then call "bt
1382 no-filters", and get a default backtrace (it would be
1383 confusing to automatically start a standard backtrace halfway
1384 through a Python filtered backtrace). However in the case
1385 where GDB cannot initialize the frame filters (most likely
1386 due to incorrect auto-load paths), GDB has printed nothing.
1387 In this case it is OK to print the default backtrace after
1388 printing the error message. GDB returns EXT_LANG_BT_NO_FILTERS
1389 here to signify there are no filters after printing the
1390 initialization error. This return code will trigger a
1391 default backtrace. */
1393 throw_quit_or_print_exception ();
1394 return EXT_LANG_BT_NO_FILTERS
;
1397 /* If iterable is None, then there are no frame filters registered.
1398 If this is the case, defer to default GDB printing routines in MI
1400 if (iterable
== Py_None
)
1401 return EXT_LANG_BT_NO_FILTERS
;
1403 htab_up
levels_printed (htab_create (20,
1410 gdbpy_ref
<> item (PyIter_Next (iterable
.get ()));
1414 if (PyErr_Occurred ())
1416 throw_quit_or_print_exception ();
1417 return EXT_LANG_BT_ERROR
;
1422 if (frame_countdown
!= -1)
1424 gdb_assert ((flags
& PRINT_MORE_FRAMES
) != 0);
1426 if (frame_countdown
== 0)
1428 /* We've printed all the frames we were asked to
1429 print, but more frames existed. */
1430 printf_filtered (_("(More stack frames follow...)\n"));
1435 success
= py_print_frame (item
.get (), flags
, args_type
, out
, 0,
1436 levels_printed
.get ());
1438 /* Do not exit on error printing a single frame. Print the
1439 error and continue with other frames. */
1440 if (success
== EXT_LANG_BT_ERROR
)
1441 throw_quit_or_print_exception ();