1 /* Python frame filters
3 Copyright (C) 2013-2017 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"
41 /* Helper function to extract a symbol, a name and a language
42 definition from a Python object that conforms to the "Symbol Value"
43 interface. OBJ is the Python object to extract the values from.
44 NAME is a pass-through argument where the name of the symbol will
45 be written. NAME is allocated in this function, but the caller is
46 responsible for clean up. SYM is a pass-through argument where the
47 symbol will be written and SYM_BLOCK is a pass-through argument to
48 write the block where the symbol lies in. In the case of the API
49 returning a string, this will be set to NULL. LANGUAGE is also a
50 pass-through argument denoting the language attributed to the
51 Symbol. In the case of SYM being NULL, this will be set to the
52 current language. Returns EXT_LANG_BT_ERROR on error with the
53 appropriate Python exception set, and EXT_LANG_BT_OK on success. */
55 static enum ext_lang_bt_status
56 extract_sym (PyObject
*obj
, gdb::unique_xmalloc_ptr
<char> *name
,
57 struct symbol
**sym
, struct block
**sym_block
,
58 const struct language_defn
**language
)
60 gdbpy_ref
result (PyObject_CallMethod (obj
, "symbol", NULL
));
63 return EXT_LANG_BT_ERROR
;
65 /* For 'symbol' callback, the function can return a symbol or a
67 if (gdbpy_is_string (result
.get ()))
69 *name
= python_string_to_host_string (result
.get ());
72 return EXT_LANG_BT_ERROR
;
73 /* If the API returns a string (and not a symbol), then there is
74 no symbol derived language available and the frame filter has
75 either overridden the symbol with a string, or supplied a
76 entirely synthetic symbol/value pairing. In that case, use
78 *language
= python_language
;
84 /* This type checks 'result' during the conversion so we
85 just call it unconditionally and check the return. */
86 *sym
= symbol_object_to_symbol (result
.get ());
87 /* TODO: currently, we have no way to recover the block in which SYMBOL
88 was found, so we have no block to return. Trying to evaluate SYMBOL
89 will yield an incorrect value when it's located in a FRAME and
90 evaluated from another frame (as permitted in nested functions). */
95 PyErr_SetString (PyExc_RuntimeError
,
96 _("Unexpected value. Expecting a "
97 "gdb.Symbol or a Python string."));
98 return EXT_LANG_BT_ERROR
;
101 /* Duplicate the symbol name, so the caller has consistency
102 in garbage collection. */
103 name
->reset (xstrdup (SYMBOL_PRINT_NAME (*sym
)));
105 /* If a symbol is specified attempt to determine the language
106 from the symbol. If mode is not "auto", then the language
107 has been explicitly set, use that. */
108 if (language_mode
== language_mode_auto
)
109 *language
= language_def (SYMBOL_LANGUAGE (*sym
));
111 *language
= current_language
;
114 return EXT_LANG_BT_OK
;
117 /* Helper function to extract a value from an object that conforms to
118 the "Symbol Value" interface. OBJ is the Python object to extract
119 the value from. VALUE is a pass-through argument where the value
120 will be written. If the object does not have the value attribute,
121 or provides the Python None for a value, VALUE will be set to NULL
122 and this function will return as successful. Returns EXT_LANG_BT_ERROR
123 on error with the appropriate Python exception set, and EXT_LANG_BT_OK on
126 static enum ext_lang_bt_status
127 extract_value (PyObject
*obj
, struct value
**value
)
129 if (PyObject_HasAttrString (obj
, "value"))
131 gdbpy_ref
vresult (PyObject_CallMethod (obj
, "value", NULL
));
134 return EXT_LANG_BT_ERROR
;
136 /* The Python code has returned 'None' for a value, so we set
137 value to NULL. This flags that GDB should read the
139 if (vresult
== Py_None
)
142 return EXT_LANG_BT_OK
;
146 *value
= convert_value_from_python (vresult
.get ());
149 return EXT_LANG_BT_ERROR
;
151 return EXT_LANG_BT_OK
;
157 return EXT_LANG_BT_OK
;
160 /* MI prints only certain values according to the type of symbol and
161 also what the user has specified. SYM is the symbol to check, and
162 MI_PRINT_TYPES is an enum specifying what the user wants emitted
163 for the MI command in question. */
165 mi_should_print (struct symbol
*sym
, enum mi_print_types type
)
169 switch (SYMBOL_CLASS (sym
))
172 case LOC_UNDEF
: /* catches errors */
173 case LOC_CONST
: /* constant */
174 case LOC_TYPEDEF
: /* local typedef */
175 case LOC_LABEL
: /* local label */
176 case LOC_BLOCK
: /* local function */
177 case LOC_CONST_BYTES
: /* loc. byte seq. */
178 case LOC_UNRESOLVED
: /* unresolved static */
179 case LOC_OPTIMIZED_OUT
: /* optimized out */
183 case LOC_ARG
: /* argument */
184 case LOC_REF_ARG
: /* reference arg */
185 case LOC_REGPARM_ADDR
: /* indirect register arg */
186 case LOC_LOCAL
: /* stack local */
187 case LOC_STATIC
: /* static */
188 case LOC_REGISTER
: /* register */
189 case LOC_COMPUTED
: /* computed location */
190 if (type
== MI_PRINT_LOCALS
)
191 print_me
= ! SYMBOL_IS_ARGUMENT (sym
);
193 print_me
= SYMBOL_IS_ARGUMENT (sym
);
198 /* Helper function which outputs a type name extracted from VAL to a
199 "type" field in the output stream OUT. OUT is the ui-out structure
200 the type name will be output too, and VAL is the value that the
201 type will be extracted from. Returns EXT_LANG_BT_ERROR on error, with
202 any GDB exceptions converted to a Python exception, or EXT_LANG_BT_OK on
205 static enum ext_lang_bt_status
206 py_print_type (struct ui_out
*out
, struct value
*val
)
211 check_typedef (value_type (val
));
214 type_print (value_type (val
), "", &stb
, -1);
215 out
->field_stream ("type", stb
);
217 CATCH (except
, RETURN_MASK_ALL
)
219 gdbpy_convert_exception (except
);
220 return EXT_LANG_BT_ERROR
;
224 return EXT_LANG_BT_OK
;
227 /* Helper function which outputs a value to an output field in a
228 stream. OUT is the ui-out structure the value will be output to,
229 VAL is the value that will be printed, OPTS contains the value
230 printing options, ARGS_TYPE is an enumerator describing the
231 argument format, and LANGUAGE is the language_defn that the value
232 will be printed with. Returns EXT_LANG_BT_ERROR on error, with any GDB
233 exceptions converted to a Python exception, or EXT_LANG_BT_OK on
236 static enum ext_lang_bt_status
237 py_print_value (struct ui_out
*out
, struct value
*val
,
238 const struct value_print_options
*opts
,
240 enum ext_lang_frame_args args_type
,
241 const struct language_defn
*language
)
243 int should_print
= 0;
245 /* MI does not print certain values, differentiated by type,
246 depending on what ARGS_TYPE indicates. Test type against option.
247 For CLI print all values. */
248 if (args_type
== MI_PRINT_SIMPLE_VALUES
249 || args_type
== MI_PRINT_ALL_VALUES
)
251 struct type
*type
= NULL
;
255 type
= check_typedef (value_type (val
));
257 CATCH (except
, RETURN_MASK_ALL
)
259 gdbpy_convert_exception (except
);
260 return EXT_LANG_BT_ERROR
;
264 if (args_type
== MI_PRINT_ALL_VALUES
)
266 else if (args_type
== MI_PRINT_SIMPLE_VALUES
267 && TYPE_CODE (type
) != TYPE_CODE_ARRAY
268 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
269 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
272 else if (args_type
!= NO_VALUES
)
281 common_val_print (val
, &stb
, indent
, opts
, language
);
282 out
->field_stream ("value", stb
);
284 CATCH (except
, RETURN_MASK_ALL
)
286 gdbpy_convert_exception (except
);
287 return EXT_LANG_BT_ERROR
;
292 return EXT_LANG_BT_OK
;
295 /* Helper function to call a Python method and extract an iterator
296 from the result. If the function returns anything but an iterator
297 the exception is preserved and NULL is returned. FILTER is the
298 Python object to call, and FUNC is the name of the method. Returns
299 a PyObject, or NULL on error with the appropriate exception set.
300 This function can return an iterator, or NULL. */
303 get_py_iter_from_func (PyObject
*filter
, char *func
)
305 if (PyObject_HasAttrString (filter
, func
))
307 gdbpy_ref
result (PyObject_CallMethod (filter
, func
, NULL
));
311 if (result
== Py_None
)
313 return result
.release ();
317 return PyObject_GetIter (result
.get ());
327 /* Helper function to output a single frame argument and value to an
328 output stream. This function will account for entry values if the
329 FV parameter is populated, the frame argument has entry values
330 associated with them, and the appropriate "set entry-value"
331 options are set. Will output in CLI or MI like format depending
332 on the type of output stream detected. OUT is the output stream,
333 SYM_NAME is the name of the symbol. If SYM_NAME is populated then
334 it must have an accompanying value in the parameter FV. FA is a
335 frame argument structure. If FA is populated, both SYM_NAME and
336 FV are ignored. OPTS contains the value printing options,
337 ARGS_TYPE is an enumerator describing the argument format,
338 PRINT_ARGS_FIELD is a flag which indicates if we output "ARGS=1"
339 in MI output in commands where both arguments and locals are
340 printed. Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions
341 converted to a Python exception, or EXT_LANG_BT_OK on success. */
343 static enum ext_lang_bt_status
344 py_print_single_arg (struct ui_out
*out
,
345 const char *sym_name
,
346 struct frame_arg
*fa
,
348 const struct value_print_options
*opts
,
349 enum ext_lang_frame_args args_type
,
350 int print_args_field
,
351 const struct language_defn
*language
)
354 enum ext_lang_bt_status retval
= EXT_LANG_BT_OK
;
358 if (fa
->val
== NULL
&& fa
->error
== NULL
)
359 return EXT_LANG_BT_OK
;
360 language
= language_def (SYMBOL_LANGUAGE (fa
->sym
));
368 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
370 /* MI has varying rules for tuples, but generally if there is only
371 one element in each item in the list, do not start a tuple. The
372 exception is -stack-list-variables which emits an ARGS="1" field
373 if the value is a frame argument. This is denoted in this
374 function with PRINT_ARGS_FIELD which is flag from the caller to
375 emit the ARGS field. */
376 if (out
->is_mi_like_p ())
378 if (print_args_field
|| args_type
!= NO_VALUES
)
379 make_cleanup_ui_out_tuple_begin_end (out
, NULL
);
382 annotate_arg_begin ();
384 /* If frame argument is populated, check for entry-values and the
385 entry value options. */
390 fprintf_symbol_filtered (&stb
, SYMBOL_PRINT_NAME (fa
->sym
),
391 SYMBOL_LANGUAGE (fa
->sym
),
392 DMGL_PARAMS
| DMGL_ANSI
);
393 if (fa
->entry_kind
== print_entry_values_compact
)
397 fprintf_symbol_filtered (&stb
, SYMBOL_PRINT_NAME (fa
->sym
),
398 SYMBOL_LANGUAGE (fa
->sym
),
399 DMGL_PARAMS
| DMGL_ANSI
);
401 if (fa
->entry_kind
== print_entry_values_only
402 || fa
->entry_kind
== print_entry_values_compact
)
404 out
->field_stream ("name", stb
);
407 /* Otherwise, just output the name. */
408 out
->field_string ("name", sym_name
);
410 annotate_arg_name_end ();
412 if (! out
->is_mi_like_p ())
415 if (print_args_field
)
416 out
->field_int ("arg", 1);
418 /* For MI print the type, but only for simple values. This seems
419 weird, but this is how MI choose to format the various output
421 if (args_type
== MI_PRINT_SIMPLE_VALUES
&& val
!= NULL
)
423 if (py_print_type (out
, val
) == EXT_LANG_BT_ERROR
)
425 retval
= EXT_LANG_BT_ERROR
;
426 do_cleanups (cleanups
);
430 if (retval
!= EXT_LANG_BT_ERROR
)
433 annotate_arg_value (value_type (val
));
435 /* If the output is to the CLI, and the user option "set print
436 frame-arguments" is set to none, just output "...". */
437 if (! out
->is_mi_like_p () && args_type
== NO_VALUES
)
438 out
->field_string ("value", "...");
441 /* Otherwise, print the value for both MI and the CLI, except
442 for the case of MI_PRINT_NO_VALUES. */
443 if (args_type
!= NO_VALUES
)
447 gdb_assert (fa
!= NULL
&& fa
->error
!= NULL
);
448 out
->field_fmt ("value",
449 _("<error reading variable: %s>"),
452 else if (py_print_value (out
, val
, opts
, 0, args_type
, language
)
453 == EXT_LANG_BT_ERROR
)
454 retval
= EXT_LANG_BT_ERROR
;
458 do_cleanups (cleanups
);
461 CATCH (except
, RETURN_MASK_ERROR
)
463 gdbpy_convert_exception (except
);
470 /* Helper function to loop over frame arguments provided by the
471 "frame_arguments" Python API. Elements in the iterator must
472 conform to the "Symbol Value" interface. ITER is the Python
473 iterable object, OUT is the output stream, ARGS_TYPE is an
474 enumerator describing the argument format, PRINT_ARGS_FIELD is a
475 flag which indicates if we output "ARGS=1" in MI output in commands
476 where both arguments and locals are printed, and FRAME is the
477 backing frame. Returns EXT_LANG_BT_ERROR on error, with any GDB
478 exceptions converted to a Python exception, or EXT_LANG_BT_OK on
481 static enum ext_lang_bt_status
482 enumerate_args (PyObject
*iter
,
484 enum ext_lang_frame_args args_type
,
485 int print_args_field
,
486 struct frame_info
*frame
)
488 struct value_print_options opts
;
490 get_user_print_options (&opts
);
492 if (args_type
== CLI_SCALAR_VALUES
)
494 /* True in "summary" mode, false otherwise. */
502 annotate_frame_args ();
504 CATCH (except
, RETURN_MASK_ALL
)
506 gdbpy_convert_exception (except
);
507 return EXT_LANG_BT_ERROR
;
511 /* Collect the first argument outside of the loop, so output of
512 commas in the argument output is correct. At the end of the
513 loop block collect another item from the iterator, and, if it is
514 not null emit a comma. */
515 gdbpy_ref
item (PyIter_Next (iter
));
516 if (item
== NULL
&& PyErr_Occurred ())
517 return EXT_LANG_BT_ERROR
;
521 const struct language_defn
*language
;
522 gdb::unique_xmalloc_ptr
<char> sym_name
;
524 struct block
*sym_block
;
526 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
528 success
= extract_sym (item
.get (), &sym_name
, &sym
, &sym_block
,
530 if (success
== EXT_LANG_BT_ERROR
)
531 return EXT_LANG_BT_ERROR
;
533 success
= extract_value (item
.get (), &val
);
534 if (success
== EXT_LANG_BT_ERROR
)
535 return EXT_LANG_BT_ERROR
;
537 if (sym
&& out
->is_mi_like_p ()
538 && ! mi_should_print (sym
, MI_PRINT_ARGS
))
541 /* If the object did not provide a value, read it using
542 read_frame_args and account for entry values, if any. */
545 struct frame_arg arg
, entryarg
;
547 /* If there is no value, and also no symbol, set error and
551 PyErr_SetString (PyExc_RuntimeError
,
552 _("No symbol or value provided."));
553 return EXT_LANG_BT_ERROR
;
558 read_frame_arg (sym
, frame
, &arg
, &entryarg
);
560 CATCH (except
, RETURN_MASK_ALL
)
562 gdbpy_convert_exception (except
);
563 return EXT_LANG_BT_ERROR
;
567 /* The object has not provided a value, so this is a frame
568 argument to be read by GDB. In this case we have to
569 account for entry-values. */
571 if (arg
.entry_kind
!= print_entry_values_only
)
573 if (py_print_single_arg (out
, NULL
, &arg
,
577 NULL
) == EXT_LANG_BT_ERROR
)
580 xfree (entryarg
.error
);
581 return EXT_LANG_BT_ERROR
;
585 if (entryarg
.entry_kind
!= print_entry_values_no
)
587 if (arg
.entry_kind
!= print_entry_values_only
)
592 out
->wrap_hint (" ");
594 CATCH (except
, RETURN_MASK_ALL
)
597 xfree (entryarg
.error
);
598 gdbpy_convert_exception (except
);
599 return EXT_LANG_BT_ERROR
;
604 if (py_print_single_arg (out
, NULL
, &entryarg
, NULL
, &opts
,
605 args_type
, print_args_field
, NULL
)
606 == EXT_LANG_BT_ERROR
)
609 xfree (entryarg
.error
);
610 return EXT_LANG_BT_ERROR
;
615 xfree (entryarg
.error
);
619 /* If the object has provided a value, we just print that. */
622 if (py_print_single_arg (out
, sym_name
.get (), NULL
, val
, &opts
,
623 args_type
, print_args_field
,
624 language
) == EXT_LANG_BT_ERROR
)
625 return EXT_LANG_BT_ERROR
;
629 /* Collect the next item from the iterator. If
630 this is the last item, do not print the
632 item
.reset (PyIter_Next (iter
));
639 CATCH (except
, RETURN_MASK_ALL
)
641 gdbpy_convert_exception (except
);
642 return EXT_LANG_BT_ERROR
;
646 else if (PyErr_Occurred ())
647 return EXT_LANG_BT_ERROR
;
653 CATCH (except
, RETURN_MASK_ALL
)
655 gdbpy_convert_exception (except
);
656 return EXT_LANG_BT_ERROR
;
661 return EXT_LANG_BT_OK
;
665 /* Helper function to loop over variables provided by the
666 "frame_locals" Python API. Elements in the iterable must conform
667 to the "Symbol Value" interface. ITER is the Python iterable
668 object, OUT is the output stream, INDENT is whether we should
669 indent the output (for CLI), ARGS_TYPE is an enumerator describing
670 the argument format, PRINT_ARGS_FIELD is flag which indicates
671 whether to output the ARGS field in the case of
672 -stack-list-variables and FRAME is the backing frame. Returns
673 EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to a Python
674 exception, or EXT_LANG_BT_OK on success. */
676 static enum ext_lang_bt_status
677 enumerate_locals (PyObject
*iter
,
680 enum ext_lang_frame_args args_type
,
681 int print_args_field
,
682 struct frame_info
*frame
)
684 struct value_print_options opts
;
686 get_user_print_options (&opts
);
691 const struct language_defn
*language
;
692 gdb::unique_xmalloc_ptr
<char> sym_name
;
694 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
696 struct block
*sym_block
;
697 int local_indent
= 8 + (8 * indent
);
698 struct cleanup
*locals_cleanups
;
700 gdbpy_ref
item (PyIter_Next (iter
));
704 locals_cleanups
= make_cleanup (null_cleanup
, NULL
);
706 success
= extract_sym (item
.get (), &sym_name
, &sym
, &sym_block
,
708 if (success
== EXT_LANG_BT_ERROR
)
710 do_cleanups (locals_cleanups
);
714 success
= extract_value (item
.get (), &val
);
715 if (success
== EXT_LANG_BT_ERROR
)
717 do_cleanups (locals_cleanups
);
721 if (sym
!= NULL
&& out
->is_mi_like_p ()
722 && ! mi_should_print (sym
, MI_PRINT_LOCALS
))
724 do_cleanups (locals_cleanups
);
728 /* If the object did not provide a value, read it. */
733 val
= read_var_value (sym
, sym_block
, frame
);
735 CATCH (except
, RETURN_MASK_ERROR
)
737 gdbpy_convert_exception (except
);
738 do_cleanups (locals_cleanups
);
744 /* With PRINT_NO_VALUES, MI does not emit a tuple normally as
745 each output contains only one field. The exception is
746 -stack-list-variables, which always provides a tuple. */
747 if (out
->is_mi_like_p ())
749 if (print_args_field
|| args_type
!= NO_VALUES
)
750 make_cleanup_ui_out_tuple_begin_end (out
, NULL
);
754 if (! out
->is_mi_like_p ())
756 /* If the output is not MI we indent locals. */
757 out
->spaces (local_indent
);
760 out
->field_string ("name", sym_name
.get ());
762 if (! out
->is_mi_like_p ())
765 CATCH (except
, RETURN_MASK_ERROR
)
767 gdbpy_convert_exception (except
);
768 do_cleanups (locals_cleanups
);
773 if (args_type
== MI_PRINT_SIMPLE_VALUES
)
775 if (py_print_type (out
, val
) == EXT_LANG_BT_ERROR
)
777 do_cleanups (locals_cleanups
);
782 /* CLI always prints values for locals. MI uses the
783 simple/no/all system. */
784 if (! out
->is_mi_like_p ())
786 int val_indent
= (indent
+ 1) * 4;
788 if (py_print_value (out
, val
, &opts
, val_indent
, args_type
,
789 language
) == EXT_LANG_BT_ERROR
)
791 do_cleanups (locals_cleanups
);
797 if (args_type
!= NO_VALUES
)
799 if (py_print_value (out
, val
, &opts
, 0, args_type
,
800 language
) == EXT_LANG_BT_ERROR
)
802 do_cleanups (locals_cleanups
);
808 do_cleanups (locals_cleanups
);
814 CATCH (except
, RETURN_MASK_ERROR
)
816 gdbpy_convert_exception (except
);
822 if (!PyErr_Occurred ())
823 return EXT_LANG_BT_OK
;
826 return EXT_LANG_BT_ERROR
;
829 /* Helper function for -stack-list-variables. Returns EXT_LANG_BT_ERROR on
830 error, or EXT_LANG_BT_OK on success. */
832 static enum ext_lang_bt_status
833 py_mi_print_variables (PyObject
*filter
, struct ui_out
*out
,
834 struct value_print_options
*opts
,
835 enum ext_lang_frame_args args_type
,
836 struct frame_info
*frame
)
838 struct cleanup
*old_chain
;
840 gdbpy_ref
args_iter (get_py_iter_from_func (filter
, "frame_args"));
841 if (args_iter
== NULL
)
842 return EXT_LANG_BT_ERROR
;
844 gdbpy_ref
locals_iter (get_py_iter_from_func (filter
, "frame_locals"));
845 if (locals_iter
== NULL
)
846 return EXT_LANG_BT_ERROR
;
848 old_chain
= make_cleanup_ui_out_list_begin_end (out
, "variables");
850 if (args_iter
!= Py_None
)
851 if (enumerate_args (args_iter
.get (), out
, args_type
, 1, frame
)
852 == EXT_LANG_BT_ERROR
)
855 if (locals_iter
!= Py_None
)
856 if (enumerate_locals (locals_iter
.get (), out
, 1, args_type
, 1, frame
)
857 == EXT_LANG_BT_ERROR
)
860 do_cleanups (old_chain
);
861 return EXT_LANG_BT_OK
;
864 do_cleanups (old_chain
);
865 return EXT_LANG_BT_ERROR
;
868 /* Helper function for printing locals. This function largely just
869 creates the wrapping tuple, and calls enumerate_locals. Returns
870 EXT_LANG_BT_ERROR on error, or EXT_LANG_BT_OK on success. */
872 static enum ext_lang_bt_status
873 py_print_locals (PyObject
*filter
,
875 enum ext_lang_frame_args args_type
,
877 struct frame_info
*frame
)
879 struct cleanup
*old_chain
;
881 gdbpy_ref
locals_iter (get_py_iter_from_func (filter
, "frame_locals"));
882 if (locals_iter
== NULL
)
883 return EXT_LANG_BT_ERROR
;
885 old_chain
= make_cleanup_ui_out_list_begin_end (out
, "locals");
887 if (locals_iter
!= Py_None
)
888 if (enumerate_locals (locals_iter
.get (), out
, indent
, args_type
,
889 0, frame
) == EXT_LANG_BT_ERROR
)
892 do_cleanups (old_chain
);
893 return EXT_LANG_BT_OK
;
896 do_cleanups (old_chain
);
897 return EXT_LANG_BT_ERROR
;
900 /* Helper function for printing frame arguments. This function
901 largely just creates the wrapping tuple, and calls enumerate_args.
902 Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to
903 a Python exception, or EXT_LANG_BT_OK on success. */
905 static enum ext_lang_bt_status
906 py_print_args (PyObject
*filter
,
908 enum ext_lang_frame_args args_type
,
909 struct frame_info
*frame
)
911 struct cleanup
*old_chain
;
913 gdbpy_ref
args_iter (get_py_iter_from_func (filter
, "frame_args"));
914 if (args_iter
== NULL
)
915 return EXT_LANG_BT_ERROR
;
917 old_chain
= make_cleanup_ui_out_list_begin_end (out
, "args");
921 annotate_frame_args ();
922 if (! out
->is_mi_like_p ())
925 CATCH (except
, RETURN_MASK_ALL
)
927 gdbpy_convert_exception (except
);
932 if (args_iter
!= Py_None
)
933 if (enumerate_args (args_iter
.get (), out
, args_type
, 0, frame
)
934 == EXT_LANG_BT_ERROR
)
939 if (! out
->is_mi_like_p ())
942 CATCH (except
, RETURN_MASK_ALL
)
944 gdbpy_convert_exception (except
);
949 do_cleanups (old_chain
);
950 return EXT_LANG_BT_OK
;
953 do_cleanups (old_chain
);
954 return EXT_LANG_BT_ERROR
;
957 /* Print a single frame to the designated output stream, detecting
958 whether the output is MI or console, and formatting the output
959 according to the conventions of that protocol. FILTER is the
960 frame-filter associated with this frame. FLAGS is an integer
961 describing the various print options. The FLAGS variables is
962 described in "apply_frame_filter" function. ARGS_TYPE is an
963 enumerator describing the argument format. OUT is the output
964 stream to print, INDENT is the level of indention for this frame
965 (in the case of elided frames), and LEVELS_PRINTED is a hash-table
966 containing all the frames level that have already been printed.
967 If a frame level has been printed, do not print it again (in the
968 case of elided frames). Returns EXT_LANG_BT_ERROR on error, with any
969 GDB exceptions converted to a Python exception, or EXT_LANG_BT_COMPLETED
970 on success. It can also throw an exception RETURN_QUIT. */
972 static enum ext_lang_bt_status
973 py_print_frame (PyObject
*filter
, int flags
,
974 enum ext_lang_frame_args args_type
,
975 struct ui_out
*out
, int indent
, htab_t levels_printed
)
978 CORE_ADDR address
= 0;
979 struct gdbarch
*gdbarch
= NULL
;
980 struct frame_info
*frame
= NULL
;
981 struct cleanup
*cleanup_stack
;
982 struct value_print_options opts
;
983 int print_level
, print_frame_info
, print_args
, print_locals
;
984 gdb::unique_xmalloc_ptr
<char> function_to_free
;
986 /* Extract print settings from FLAGS. */
987 print_level
= (flags
& PRINT_LEVEL
) ? 1 : 0;
988 print_frame_info
= (flags
& PRINT_FRAME_INFO
) ? 1 : 0;
989 print_args
= (flags
& PRINT_ARGS
) ? 1 : 0;
990 print_locals
= (flags
& PRINT_LOCALS
) ? 1 : 0;
992 get_user_print_options (&opts
);
994 /* Get the underlying frame. This is needed to determine GDB
995 architecture, and also, in the cases of frame variables/arguments to
996 read them if they returned filter object requires us to do so. */
997 gdbpy_ref
py_inf_frame (PyObject_CallMethod (filter
, "inferior_frame", NULL
));
998 if (py_inf_frame
== NULL
)
999 return EXT_LANG_BT_ERROR
;
1001 frame
= frame_object_to_frame_info (py_inf_frame
.get ());
1003 return EXT_LANG_BT_ERROR
;
1007 gdbarch
= get_frame_arch (frame
);
1009 CATCH (except
, RETURN_MASK_ERROR
)
1011 gdbpy_convert_exception (except
);
1012 return EXT_LANG_BT_ERROR
;
1016 /* stack-list-variables. */
1017 if (print_locals
&& print_args
&& ! print_frame_info
)
1019 if (py_mi_print_variables (filter
, out
, &opts
,
1020 args_type
, frame
) == EXT_LANG_BT_ERROR
)
1021 return EXT_LANG_BT_ERROR
;
1022 return EXT_LANG_BT_COMPLETED
;
1025 cleanup_stack
= make_cleanup (null_cleanup
, NULL
);
1027 /* -stack-list-locals does not require a
1028 wrapping frame attribute. */
1029 if (print_frame_info
|| (print_args
&& ! print_locals
))
1030 make_cleanup_ui_out_tuple_begin_end (out
, "frame");
1032 if (print_frame_info
)
1034 /* Elided frames are also printed with this function (recursively)
1035 and are printed with indention. */
1040 out
->spaces (indent
* 4);
1042 CATCH (except
, RETURN_MASK_ERROR
)
1044 gdbpy_convert_exception (except
);
1045 do_cleanups (cleanup_stack
);
1046 return EXT_LANG_BT_ERROR
;
1051 /* The address is required for frame annotations, and also for
1052 address printing. */
1053 if (PyObject_HasAttrString (filter
, "address"))
1055 gdbpy_ref
paddr (PyObject_CallMethod (filter
, "address", NULL
));
1059 do_cleanups (cleanup_stack
);
1060 return EXT_LANG_BT_ERROR
;
1063 if (paddr
!= Py_None
)
1065 if (get_addr_from_python (paddr
.get (), &address
) < 0)
1067 do_cleanups (cleanup_stack
);
1068 return EXT_LANG_BT_ERROR
;
1076 /* Print frame level. MI does not require the level if
1077 locals/variables only are being printed. */
1078 if ((print_frame_info
|| print_args
) && print_level
)
1080 struct frame_info
**slot
;
1083 slot
= (struct frame_info
**) htab_find_slot (levels_printed
,
1087 level
= frame_relative_level (frame
);
1089 /* Check if this frame has already been printed (there are cases
1090 where elided synthetic dummy-frames have to 'borrow' the frame
1091 architecture from the eliding frame. If that is the case, do
1092 not print 'level', but print spaces. */
1094 out
->field_skip ("level");
1098 annotate_frame_begin (print_level
? level
: 0,
1101 out
->field_fmt_int (2, ui_left
, "level",
1105 CATCH (except
, RETURN_MASK_ERROR
)
1107 gdbpy_convert_exception (except
);
1108 do_cleanups (cleanup_stack
);
1109 return EXT_LANG_BT_ERROR
;
1114 if (print_frame_info
)
1116 /* Print address to the address field. If an address is not provided,
1118 if (opts
.addressprint
&& has_addr
)
1122 annotate_frame_address ();
1123 out
->field_core_addr ("addr", gdbarch
, address
);
1124 annotate_frame_address_end ();
1127 CATCH (except
, RETURN_MASK_ERROR
)
1129 gdbpy_convert_exception (except
);
1130 do_cleanups (cleanup_stack
);
1131 return EXT_LANG_BT_ERROR
;
1136 /* Print frame function name. */
1137 if (PyObject_HasAttrString (filter
, "function"))
1139 gdbpy_ref
py_func (PyObject_CallMethod (filter
, "function", NULL
));
1140 const char *function
= NULL
;
1142 if (py_func
== NULL
)
1144 do_cleanups (cleanup_stack
);
1145 return EXT_LANG_BT_ERROR
;
1148 if (gdbpy_is_string (py_func
.get ()))
1150 function_to_free
= python_string_to_host_string (py_func
.get ());
1152 if (function_to_free
== NULL
)
1154 do_cleanups (cleanup_stack
);
1155 return EXT_LANG_BT_ERROR
;
1158 function
= function_to_free
.get ();
1160 else if (PyLong_Check (py_func
.get ()))
1163 struct bound_minimal_symbol msymbol
;
1165 if (get_addr_from_python (py_func
.get (), &addr
) < 0)
1167 do_cleanups (cleanup_stack
);
1168 return EXT_LANG_BT_ERROR
;
1171 msymbol
= lookup_minimal_symbol_by_pc (addr
);
1172 if (msymbol
.minsym
!= NULL
)
1173 function
= MSYMBOL_PRINT_NAME (msymbol
.minsym
);
1175 else if (py_func
!= Py_None
)
1177 PyErr_SetString (PyExc_RuntimeError
,
1178 _("FrameDecorator.function: expecting a " \
1179 "String, integer or None."));
1180 do_cleanups (cleanup_stack
);
1181 return EXT_LANG_BT_ERROR
;
1186 annotate_frame_function_name ();
1187 if (function
== NULL
)
1188 out
->field_skip ("func");
1190 out
->field_string ("func", function
);
1192 CATCH (except
, RETURN_MASK_ERROR
)
1194 gdbpy_convert_exception (except
);
1195 do_cleanups (cleanup_stack
);
1196 return EXT_LANG_BT_ERROR
;
1203 /* Frame arguments. Check the result, and error if something went
1207 if (py_print_args (filter
, out
, args_type
, frame
) == EXT_LANG_BT_ERROR
)
1209 do_cleanups (cleanup_stack
);
1210 return EXT_LANG_BT_ERROR
;
1214 /* File name/source/line number information. */
1215 if (print_frame_info
)
1219 annotate_frame_source_begin ();
1221 CATCH (except
, RETURN_MASK_ERROR
)
1223 gdbpy_convert_exception (except
);
1224 do_cleanups (cleanup_stack
);
1225 return EXT_LANG_BT_ERROR
;
1229 if (PyObject_HasAttrString (filter
, "filename"))
1231 gdbpy_ref
py_fn (PyObject_CallMethod (filter
, "filename", NULL
));
1235 do_cleanups (cleanup_stack
);
1236 return EXT_LANG_BT_ERROR
;
1239 if (py_fn
!= Py_None
)
1241 gdb::unique_xmalloc_ptr
<char>
1242 filename (python_string_to_host_string (py_fn
.get ()));
1244 if (filename
== NULL
)
1246 do_cleanups (cleanup_stack
);
1247 return EXT_LANG_BT_ERROR
;
1252 out
->wrap_hint (" ");
1254 annotate_frame_source_file ();
1255 out
->field_string ("file", filename
.get ());
1256 annotate_frame_source_file_end ();
1258 CATCH (except
, RETURN_MASK_ERROR
)
1260 gdbpy_convert_exception (except
);
1261 do_cleanups (cleanup_stack
);
1262 return EXT_LANG_BT_ERROR
;
1268 if (PyObject_HasAttrString (filter
, "line"))
1270 gdbpy_ref
py_line (PyObject_CallMethod (filter
, "line", NULL
));
1273 if (py_line
== NULL
)
1275 do_cleanups (cleanup_stack
);
1276 return EXT_LANG_BT_ERROR
;
1279 if (py_line
!= Py_None
)
1281 line
= PyLong_AsLong (py_line
.get ());
1282 if (PyErr_Occurred ())
1284 do_cleanups (cleanup_stack
);
1285 return EXT_LANG_BT_ERROR
;
1291 annotate_frame_source_line ();
1292 out
->field_int ("line", line
);
1294 CATCH (except
, RETURN_MASK_ERROR
)
1296 gdbpy_convert_exception (except
);
1297 do_cleanups (cleanup_stack
);
1298 return EXT_LANG_BT_ERROR
;
1305 /* For MI we need to deal with the "children" list population of
1306 elided frames, so if MI output detected do not send newline. */
1307 if (! out
->is_mi_like_p ())
1311 annotate_frame_end ();
1314 CATCH (except
, RETURN_MASK_ERROR
)
1316 gdbpy_convert_exception (except
);
1317 do_cleanups (cleanup_stack
);
1318 return EXT_LANG_BT_ERROR
;
1325 if (py_print_locals (filter
, out
, args_type
, indent
,
1326 frame
) == EXT_LANG_BT_ERROR
)
1328 do_cleanups (cleanup_stack
);
1329 return EXT_LANG_BT_ERROR
;
1334 /* Finally recursively print elided frames, if any. */
1335 gdbpy_ref
elided (get_py_iter_from_func (filter
, "elided"));
1338 do_cleanups (cleanup_stack
);
1339 return EXT_LANG_BT_ERROR
;
1342 if (elided
!= Py_None
)
1346 make_cleanup_ui_out_list_begin_end (out
, "children");
1348 if (! out
->is_mi_like_p ())
1351 while ((item
= PyIter_Next (elided
.get ())))
1353 gdbpy_ref
item_ref (item
);
1355 enum ext_lang_bt_status success
= py_print_frame (item
, flags
,
1360 if (success
== EXT_LANG_BT_ERROR
)
1362 do_cleanups (cleanup_stack
);
1363 return EXT_LANG_BT_ERROR
;
1366 if (item
== NULL
&& PyErr_Occurred ())
1368 do_cleanups (cleanup_stack
);
1369 return EXT_LANG_BT_ERROR
;
1374 do_cleanups (cleanup_stack
);
1375 return EXT_LANG_BT_COMPLETED
;
1378 /* Helper function to initiate frame filter invocation at starting
1382 bootstrap_python_frame_filters (struct frame_info
*frame
,
1383 int frame_low
, int frame_high
)
1385 gdbpy_ref
frame_obj (frame_info_to_frame_object (frame
));
1386 if (frame_obj
== NULL
)
1389 gdbpy_ref
module (PyImport_ImportModule ("gdb.frames"));
1393 gdbpy_ref
sort_func (PyObject_GetAttrString (module
.get (),
1394 "execute_frame_filters"));
1395 if (sort_func
== NULL
)
1398 gdbpy_ref
py_frame_low (PyInt_FromLong (frame_low
));
1399 if (py_frame_low
== NULL
)
1402 gdbpy_ref
py_frame_high (PyInt_FromLong (frame_high
));
1403 if (py_frame_high
== NULL
)
1406 gdbpy_ref
iterable (PyObject_CallFunctionObjArgs (sort_func
.get (),
1408 py_frame_low
.get (),
1409 py_frame_high
.get (),
1411 if (iterable
== NULL
)
1414 if (iterable
!= Py_None
)
1415 return PyObject_GetIter (iterable
.get ());
1417 return iterable
.release ();
1420 /* This is the only publicly exported function in this file. FRAME
1421 is the source frame to start frame-filter invocation. FLAGS is an
1422 integer holding the flags for printing. The following elements of
1423 the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
1424 PRINT_LEVEL is a flag indicating whether to print the frame's
1425 relative level in the output. PRINT_FRAME_INFO is a flag that
1426 indicates whether this function should print the frame
1427 information, PRINT_ARGS is a flag that indicates whether to print
1428 frame arguments, and PRINT_LOCALS, likewise, with frame local
1429 variables. ARGS_TYPE is an enumerator describing the argument
1430 format, OUT is the output stream to print. FRAME_LOW is the
1431 beginning of the slice of frames to print, and FRAME_HIGH is the
1432 upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error,
1433 or EXT_LANG_BT_COMPLETED on success. */
1435 enum ext_lang_bt_status
1436 gdbpy_apply_frame_filter (const struct extension_language_defn
*extlang
,
1437 struct frame_info
*frame
, int flags
,
1438 enum ext_lang_frame_args args_type
,
1439 struct ui_out
*out
, int frame_low
, int frame_high
)
1441 struct gdbarch
*gdbarch
= NULL
;
1442 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
1444 if (!gdb_python_initialized
)
1445 return EXT_LANG_BT_NO_FILTERS
;
1449 gdbarch
= get_frame_arch (frame
);
1451 CATCH (except
, RETURN_MASK_ALL
)
1453 /* Let gdb try to print the stack trace. */
1454 return EXT_LANG_BT_NO_FILTERS
;
1458 gdbpy_enter
enter_py (gdbarch
, current_language
);
1460 gdbpy_ref
iterable (bootstrap_python_frame_filters (frame
, frame_low
,
1463 if (iterable
== NULL
)
1465 /* Normally if there is an error GDB prints the exception,
1466 abandons the backtrace and exits. The user can then call "bt
1467 no-filters", and get a default backtrace (it would be
1468 confusing to automatically start a standard backtrace halfway
1469 through a Python filtered backtrace). However in the case
1470 where GDB cannot initialize the frame filters (most likely
1471 due to incorrect auto-load paths), GDB has printed nothing.
1472 In this case it is OK to print the default backtrace after
1473 printing the error message. GDB returns EXT_LANG_BT_NO_FILTERS
1474 here to signify there are no filters after printing the
1475 initialization error. This return code will trigger a
1476 default backtrace. */
1478 gdbpy_print_stack ();
1479 return EXT_LANG_BT_NO_FILTERS
;
1482 /* If iterable is None, then there are no frame filters registered.
1483 If this is the case, defer to default GDB printing routines in MI
1485 if (iterable
== Py_None
)
1486 return EXT_LANG_BT_NO_FILTERS
;
1488 htab_up
levels_printed (htab_create (20,
1495 gdbpy_ref
item (PyIter_Next (iterable
.get ()));
1499 if (PyErr_Occurred ())
1501 gdbpy_print_stack ();
1502 return EXT_LANG_BT_ERROR
;
1507 success
= py_print_frame (item
.get (), flags
, args_type
, out
, 0,
1508 levels_printed
.get ());
1510 /* Do not exit on error printing a single frame. Print the
1511 error and continue with other frames. */
1512 if (success
== EXT_LANG_BT_ERROR
)
1513 gdbpy_print_stack ();