1 /* Python frame filters
3 Copyright (C) 2013-2014 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 "exceptions.h"
25 #include "arch-utils.h"
32 #include "mi/mi-cmds.h"
33 #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. In the case of the API returning a string,
48 this will be set to NULL. LANGUAGE is also a pass-through argument
49 denoting the language attributed to the Symbol. In the case of SYM
50 being NULL, this will be set to the current language. Returns
51 PY_BT_ERROR on error with the appropriate Python exception set, and
52 PY_BT_OK on success. */
54 static enum py_bt_status
55 extract_sym (PyObject
*obj
, char **name
, struct symbol
**sym
,
56 const struct language_defn
**language
)
58 PyObject
*result
= PyObject_CallMethod (obj
, "symbol", NULL
);
63 /* For 'symbol' callback, the function can return a symbol or a
65 if (gdbpy_is_string (result
))
67 *name
= python_string_to_host_string (result
);
72 /* If the API returns a string (and not a symbol), then there is
73 no symbol derived language available and the frame filter has
74 either overridden the symbol with a string, or supplied a
75 entirely synthetic symbol/value pairing. In that case, use
77 *language
= python_language
;
82 /* This type checks 'result' during the conversion so we
83 just call it unconditionally and check the return. */
84 *sym
= symbol_object_to_symbol (result
);
90 PyErr_SetString (PyExc_RuntimeError
,
91 _("Unexpected value. Expecting a "
92 "gdb.Symbol or a Python string."));
96 /* Duplicate the symbol name, so the caller has consistency
97 in garbage collection. */
98 *name
= xstrdup (SYMBOL_PRINT_NAME (*sym
));
100 /* If a symbol is specified attempt to determine the language
101 from the symbol. If mode is not "auto", then the language
102 has been explicitly set, use that. */
103 if (language_mode
== language_mode_auto
)
104 *language
= language_def (SYMBOL_LANGUAGE (*sym
));
106 *language
= current_language
;
112 /* Helper function to extract a value from an object that conforms to
113 the "Symbol Value" interface. OBJ is the Python object to extract
114 the value from. VALUE is a pass-through argument where the value
115 will be written. If the object does not have the value attribute,
116 or provides the Python None for a value, VALUE will be set to NULL
117 and this function will return as successful. Returns PY_BT_ERROR
118 on error with the appropriate Python exception set, and PY_BT_OK on
121 static enum py_bt_status
122 extract_value (PyObject
*obj
, struct value
**value
)
124 if (PyObject_HasAttrString (obj
, "value"))
126 PyObject
*vresult
= PyObject_CallMethod (obj
, "value", NULL
);
131 /* The Python code has returned 'None' for a value, so we set
132 value to NULL. This flags that GDB should read the
134 if (vresult
== Py_None
)
142 *value
= convert_value_from_python (vresult
);
157 /* MI prints only certain values according to the type of symbol and
158 also what the user has specified. SYM is the symbol to check, and
159 MI_PRINT_TYPES is an enum specifying what the user wants emitted
160 for the MI command in question. */
162 mi_should_print (struct symbol
*sym
, enum mi_print_types type
)
166 switch (SYMBOL_CLASS (sym
))
169 case LOC_UNDEF
: /* catches errors */
170 case LOC_CONST
: /* constant */
171 case LOC_TYPEDEF
: /* local typedef */
172 case LOC_LABEL
: /* local label */
173 case LOC_BLOCK
: /* local function */
174 case LOC_CONST_BYTES
: /* loc. byte seq. */
175 case LOC_UNRESOLVED
: /* unresolved static */
176 case LOC_OPTIMIZED_OUT
: /* optimized out */
180 case LOC_ARG
: /* argument */
181 case LOC_REF_ARG
: /* reference arg */
182 case LOC_REGPARM_ADDR
: /* indirect register arg */
183 case LOC_LOCAL
: /* stack local */
184 case LOC_STATIC
: /* static */
185 case LOC_REGISTER
: /* register */
186 case LOC_COMPUTED
: /* computed location */
187 if (type
== MI_PRINT_LOCALS
)
188 print_me
= ! SYMBOL_IS_ARGUMENT (sym
);
190 print_me
= SYMBOL_IS_ARGUMENT (sym
);
195 /* Helper function which outputs a type name extracted from VAL to a
196 "type" field in the output stream OUT. OUT is the ui-out structure
197 the type name will be output too, and VAL is the value that the
198 type will be extracted from. Returns PY_BT_ERROR on error, with
199 any GDB exceptions converted to a Python exception, or PY_BT_OK on
202 static enum py_bt_status
203 py_print_type (struct ui_out
*out
, struct value
*val
)
205 volatile struct gdb_exception except
;
207 TRY_CATCH (except
, RETURN_MASK_ALL
)
211 struct cleanup
*cleanup
;
213 stb
= mem_fileopen ();
214 cleanup
= make_cleanup_ui_file_delete (stb
);
215 type
= check_typedef (value_type (val
));
216 type_print (value_type (val
), "", stb
, -1);
217 ui_out_field_stream (out
, "type", stb
);
218 do_cleanups (cleanup
);
220 if (except
.reason
< 0)
222 gdbpy_convert_exception (except
);
229 /* Helper function which outputs a value to an output field in a
230 stream. OUT is the ui-out structure the value will be output to,
231 VAL is the value that will be printed, OPTS contains the value
232 printing options, ARGS_TYPE is an enumerator describing the
233 argument format, and LANGUAGE is the language_defn that the value
234 will be printed with. Returns PY_BT_ERROR on error, with any GDB
235 exceptions converted to a Python exception, or PY_BT_OK on
238 static enum py_bt_status
239 py_print_value (struct ui_out
*out
, struct value
*val
,
240 const struct value_print_options
*opts
,
242 enum py_frame_args args_type
,
243 const struct language_defn
*language
)
245 int should_print
= 0;
246 volatile struct gdb_exception except
;
247 int local_indent
= (4 * indent
);
249 /* Never set an indent level for common_val_print if MI. */
250 if (ui_out_is_mi_like_p (out
))
253 /* MI does not print certain values, differentiated by type,
254 depending on what ARGS_TYPE indicates. Test type against option.
255 For CLI print all values. */
256 if (args_type
== MI_PRINT_SIMPLE_VALUES
257 || args_type
== MI_PRINT_ALL_VALUES
)
259 struct type
*type
= NULL
;
261 TRY_CATCH (except
, RETURN_MASK_ALL
)
263 type
= check_typedef (value_type (val
));
265 if (except
.reason
< 0)
267 gdbpy_convert_exception (except
);
271 if (args_type
== MI_PRINT_ALL_VALUES
)
273 else if (args_type
== MI_PRINT_SIMPLE_VALUES
274 && TYPE_CODE (type
) != TYPE_CODE_ARRAY
275 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
276 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
279 else if (args_type
!= NO_VALUES
)
284 TRY_CATCH (except
, RETURN_MASK_ALL
)
287 struct cleanup
*cleanup
;
289 stb
= mem_fileopen ();
290 cleanup
= make_cleanup_ui_file_delete (stb
);
291 common_val_print (val
, stb
, indent
, opts
, language
);
292 ui_out_field_stream (out
, "value", stb
);
293 do_cleanups (cleanup
);
295 if (except
.reason
< 0)
297 gdbpy_convert_exception (except
);
305 /* Helper function to call a Python method and extract an iterator
306 from the result. If the function returns anything but an iterator
307 the exception is preserved and NULL is returned. FILTER is the
308 Python object to call, and FUNC is the name of the method. Returns
309 a PyObject, or NULL on error with the appropriate exception set.
310 This function can return an iterator, or NULL. */
313 get_py_iter_from_func (PyObject
*filter
, char *func
)
315 if (PyObject_HasAttrString (filter
, func
))
317 PyObject
*result
= PyObject_CallMethod (filter
, func
, NULL
);
321 if (result
== Py_None
)
327 PyObject
*iterator
= PyObject_GetIter (result
);
340 /* Helper function to output a single frame argument and value to an
341 output stream. This function will account for entry values if the
342 FV parameter is populated, the frame argument has entry values
343 associated with them, and the appropriate "set entry-value"
344 options are set. Will output in CLI or MI like format depending
345 on the type of output stream detected. OUT is the output stream,
346 SYM_NAME is the name of the symbol. If SYM_NAME is populated then
347 it must have an accompanying value in the parameter FV. FA is a
348 frame argument structure. If FA is populated, both SYM_NAME and
349 FV are ignored. OPTS contains the value printing options,
350 ARGS_TYPE is an enumerator describing the argument format,
351 PRINT_ARGS_FIELD is a flag which indicates if we output "ARGS=1"
352 in MI output in commands where both arguments and locals are
353 printed. Returns PY_BT_ERROR on error, with any GDB exceptions
354 converted to a Python exception, or PY_BT_OK on success. */
356 static enum py_bt_status
357 py_print_single_arg (struct ui_out
*out
,
358 const char *sym_name
,
359 struct frame_arg
*fa
,
361 const struct value_print_options
*opts
,
362 enum py_frame_args args_type
,
363 int print_args_field
,
364 const struct language_defn
*language
)
367 volatile struct gdb_exception except
;
371 language
= language_def (SYMBOL_LANGUAGE (fa
->sym
));
377 TRY_CATCH (except
, RETURN_MASK_ALL
)
379 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
381 /* MI has varying rules for tuples, but generally if there is only
382 one element in each item in the list, do not start a tuple. The
383 exception is -stack-list-variables which emits an ARGS="1" field
384 if the value is a frame argument. This is denoted in this
385 function with PRINT_ARGS_FIELD which is flag from the caller to
386 emit the ARGS field. */
387 if (ui_out_is_mi_like_p (out
))
389 if (print_args_field
|| args_type
!= NO_VALUES
)
390 make_cleanup_ui_out_tuple_begin_end (out
, NULL
);
393 annotate_arg_begin ();
395 /* If frame argument is populated, check for entry-values and the
396 entry value options. */
401 stb
= mem_fileopen ();
402 make_cleanup_ui_file_delete (stb
);
403 fprintf_symbol_filtered (stb
, SYMBOL_PRINT_NAME (fa
->sym
),
404 SYMBOL_LANGUAGE (fa
->sym
),
405 DMGL_PARAMS
| DMGL_ANSI
);
406 if (fa
->entry_kind
== print_entry_values_compact
)
408 fputs_filtered ("=", stb
);
410 fprintf_symbol_filtered (stb
, SYMBOL_PRINT_NAME (fa
->sym
),
411 SYMBOL_LANGUAGE (fa
->sym
),
412 DMGL_PARAMS
| DMGL_ANSI
);
414 if (fa
->entry_kind
== print_entry_values_only
415 || fa
->entry_kind
== print_entry_values_compact
)
417 fputs_filtered ("@entry", stb
);
419 ui_out_field_stream (out
, "name", stb
);
422 /* Otherwise, just output the name. */
423 ui_out_field_string (out
, "name", sym_name
);
425 annotate_arg_name_end ();
427 if (! ui_out_is_mi_like_p (out
))
428 ui_out_text (out
, "=");
430 if (print_args_field
)
431 ui_out_field_int (out
, "arg", 1);
433 /* For MI print the type, but only for simple values. This seems
434 weird, but this is how MI choose to format the various output
436 if (args_type
== MI_PRINT_SIMPLE_VALUES
)
438 if (py_print_type (out
, val
) == PY_BT_ERROR
)
440 do_cleanups (cleanups
);
445 annotate_arg_value (value_type (val
));
447 /* If the output is to the CLI, and the user option "set print
448 frame-arguments" is set to none, just output "...". */
449 if (! ui_out_is_mi_like_p (out
) && args_type
== NO_VALUES
)
450 ui_out_field_string (out
, "value", "...");
453 /* Otherwise, print the value for both MI and the CLI, except
454 for the case of MI_PRINT_NO_VALUES. */
455 if (args_type
!= NO_VALUES
)
457 if (py_print_value (out
, val
, opts
, 0, args_type
, language
)
460 do_cleanups (cleanups
);
466 do_cleanups (cleanups
);
468 if (except
.reason
< 0)
470 gdbpy_convert_exception (except
);
480 /* Helper function to loop over frame arguments provided by the
481 "frame_arguments" Python API. Elements in the iterator must
482 conform to the "Symbol Value" interface. ITER is the Python
483 iterable object, OUT is the output stream, ARGS_TYPE is an
484 enumerator describing the argument format, PRINT_ARGS_FIELD is a
485 flag which indicates if we output "ARGS=1" in MI output in commands
486 where both arguments and locals are printed, and FRAME is the
487 backing frame. Returns PY_BT_ERROR on error, with any GDB
488 exceptions converted to a Python exception, or PY_BT_OK on
491 static enum py_bt_status
492 enumerate_args (PyObject
*iter
,
494 enum py_frame_args args_type
,
495 int print_args_field
,
496 struct frame_info
*frame
)
499 struct value_print_options opts
;
500 volatile struct gdb_exception except
;
502 get_user_print_options (&opts
);
504 if (args_type
== CLI_SCALAR_VALUES
)
506 /* True in "summary" mode, false otherwise. */
512 TRY_CATCH (except
, RETURN_MASK_ALL
)
514 annotate_frame_args ();
516 if (except
.reason
< 0)
518 gdbpy_convert_exception (except
);
522 /* Collect the first argument outside of the loop, so output of
523 commas in the argument output is correct. At the end of the
524 loop block collect another item from the iterator, and, if it is
525 not null emit a comma. */
526 item
= PyIter_Next (iter
);
527 if (item
== NULL
&& PyErr_Occurred ())
532 const struct language_defn
*language
;
536 enum py_bt_status success
= PY_BT_ERROR
;
538 success
= extract_sym (item
, &sym_name
, &sym
, &language
);
539 if (success
== PY_BT_ERROR
)
545 success
= extract_value (item
, &val
);
546 if (success
== PY_BT_ERROR
)
556 if (sym
&& ui_out_is_mi_like_p (out
)
557 && ! mi_should_print (sym
, MI_PRINT_ARGS
))
563 /* If the object did not provide a value, read it using
564 read_frame_args and account for entry values, if any. */
567 struct frame_arg arg
, entryarg
;
569 /* If there is no value, and also no symbol, set error and
573 PyErr_SetString (PyExc_RuntimeError
,
574 _("No symbol or value provided."));
579 TRY_CATCH (except
, RETURN_MASK_ALL
)
581 read_frame_arg (sym
, frame
, &arg
, &entryarg
);
583 if (except
.reason
< 0)
586 gdbpy_convert_exception (except
);
590 /* The object has not provided a value, so this is a frame
591 argument to be read by GDB. In this case we have to
592 account for entry-values. */
594 if (arg
.entry_kind
!= print_entry_values_only
)
596 if (py_print_single_arg (out
, NULL
, &arg
,
600 NULL
) == PY_BT_ERROR
)
603 xfree (entryarg
.error
);
609 if (entryarg
.entry_kind
!= print_entry_values_no
)
611 if (arg
.entry_kind
!= print_entry_values_only
)
613 TRY_CATCH (except
, RETURN_MASK_ALL
)
615 ui_out_text (out
, ", ");
616 ui_out_wrap_hint (out
, " ");
618 if (except
.reason
< 0)
621 xfree (entryarg
.error
);
623 gdbpy_convert_exception (except
);
628 if (py_print_single_arg (out
, NULL
, &entryarg
, NULL
,
630 print_args_field
, NULL
) == PY_BT_ERROR
)
633 xfree (entryarg
.error
);
640 xfree (entryarg
.error
);
644 /* If the object has provided a value, we just print that. */
647 if (py_print_single_arg (out
, sym_name
, NULL
, val
, &opts
,
648 args_type
, print_args_field
,
649 language
) == PY_BT_ERROR
)
659 /* Collect the next item from the iterator. If
660 this is the last item, do not print the
662 item
= PyIter_Next (iter
);
665 TRY_CATCH (except
, RETURN_MASK_ALL
)
667 ui_out_text (out
, ", ");
669 if (except
.reason
< 0)
672 gdbpy_convert_exception (except
);
676 else if (PyErr_Occurred ())
679 TRY_CATCH (except
, RETURN_MASK_ALL
)
683 if (except
.reason
< 0)
686 gdbpy_convert_exception (except
);
698 /* Helper function to loop over variables provided by the
699 "frame_locals" Python API. Elements in the iterable must conform
700 to the "Symbol Value" interface. ITER is the Python iterable
701 object, OUT is the output stream, INDENT is whether we should
702 indent the output (for CLI), ARGS_TYPE is an enumerator describing
703 the argument format, PRINT_ARGS_FIELD is flag which indicates
704 whether to output the ARGS field in the case of
705 -stack-list-variables and FRAME is the backing frame. Returns
706 PY_BT_ERROR on error, with any GDB exceptions converted to a Python
707 exception, or PY_BT_OK on success. */
709 static enum py_bt_status
710 enumerate_locals (PyObject
*iter
,
713 enum py_frame_args args_type
,
714 int print_args_field
,
715 struct frame_info
*frame
)
718 struct value_print_options opts
;
720 get_user_print_options (&opts
);
723 while ((item
= PyIter_Next (iter
)))
725 const struct language_defn
*language
;
728 enum py_bt_status success
= PY_BT_ERROR
;
730 volatile struct gdb_exception except
;
731 int local_indent
= 8 + (8 * indent
);
732 struct cleanup
*locals_cleanups
;
734 locals_cleanups
= make_cleanup_py_decref (item
);
736 success
= extract_sym (item
, &sym_name
, &sym
, &language
);
737 if (success
== PY_BT_ERROR
)
739 do_cleanups (locals_cleanups
);
743 make_cleanup (xfree
, sym_name
);
745 success
= extract_value (item
, &val
);
746 if (success
== PY_BT_ERROR
)
748 do_cleanups (locals_cleanups
);
752 if (sym
!= NULL
&& ui_out_is_mi_like_p (out
)
753 && ! mi_should_print (sym
, MI_PRINT_LOCALS
))
755 do_cleanups (locals_cleanups
);
759 /* If the object did not provide a value, read it. */
762 TRY_CATCH (except
, RETURN_MASK_ALL
)
764 val
= read_var_value (sym
, frame
);
766 if (except
.reason
< 0)
768 gdbpy_convert_exception (except
);
769 do_cleanups (locals_cleanups
);
774 /* With PRINT_NO_VALUES, MI does not emit a tuple normally as
775 each output contains only one field. The exception is
776 -stack-list-variables, which always provides a tuple. */
777 if (ui_out_is_mi_like_p (out
))
779 if (print_args_field
|| args_type
!= NO_VALUES
)
780 make_cleanup_ui_out_tuple_begin_end (out
, NULL
);
782 TRY_CATCH (except
, RETURN_MASK_ALL
)
784 if (! ui_out_is_mi_like_p (out
))
786 /* If the output is not MI we indent locals. */
787 ui_out_spaces (out
, local_indent
);
790 ui_out_field_string (out
, "name", sym_name
);
792 if (! ui_out_is_mi_like_p (out
))
793 ui_out_text (out
, " = ");
795 if (except
.reason
< 0)
797 gdbpy_convert_exception (except
);
798 do_cleanups (locals_cleanups
);
802 if (args_type
== MI_PRINT_SIMPLE_VALUES
)
804 if (py_print_type (out
, val
) == PY_BT_ERROR
)
806 do_cleanups (locals_cleanups
);
811 /* CLI always prints values for locals. MI uses the
812 simple/no/all system. */
813 if (! ui_out_is_mi_like_p (out
))
815 int val_indent
= (indent
+ 1) * 4;
817 if (py_print_value (out
, val
, &opts
, val_indent
, args_type
,
818 language
) == PY_BT_ERROR
)
820 do_cleanups (locals_cleanups
);
826 if (args_type
!= NO_VALUES
)
828 if (py_print_value (out
, val
, &opts
, 0, args_type
,
829 language
) == PY_BT_ERROR
)
831 do_cleanups (locals_cleanups
);
837 do_cleanups (locals_cleanups
);
839 TRY_CATCH (except
, RETURN_MASK_ALL
)
841 ui_out_text (out
, "\n");
843 if (except
.reason
< 0)
845 gdbpy_convert_exception (except
);
850 if (item
== NULL
&& PyErr_Occurred ())
859 /* Helper function for -stack-list-variables. Returns PY_BT_ERROR on
860 error, or PY_BT_OK on success. */
862 static enum py_bt_status
863 py_mi_print_variables (PyObject
*filter
, struct ui_out
*out
,
864 struct value_print_options
*opts
,
865 enum py_frame_args args_type
,
866 struct frame_info
*frame
)
868 struct cleanup
*old_chain
;
870 PyObject
*locals_iter
;
872 args_iter
= get_py_iter_from_func (filter
, "frame_args");
873 old_chain
= make_cleanup_py_xdecref (args_iter
);
874 if (args_iter
== NULL
)
877 locals_iter
= get_py_iter_from_func (filter
, "frame_locals");
878 if (locals_iter
== NULL
)
881 make_cleanup_py_decref (locals_iter
);
882 make_cleanup_ui_out_list_begin_end (out
, "variables");
884 if (args_iter
!= Py_None
)
885 if (enumerate_args (args_iter
, out
, args_type
, 1, frame
) == PY_BT_ERROR
)
888 if (locals_iter
!= Py_None
)
889 if (enumerate_locals (locals_iter
, out
, 1, args_type
, 1, frame
)
893 do_cleanups (old_chain
);
897 do_cleanups (old_chain
);
901 /* Helper function for printing locals. This function largely just
902 creates the wrapping tuple, and calls enumerate_locals. Returns
903 PY_BT_ERROR on error, or PY_BT_OK on success.*/
905 static enum py_bt_status
906 py_print_locals (PyObject
*filter
,
908 enum py_frame_args args_type
,
910 struct frame_info
*frame
)
912 PyObject
*locals_iter
= get_py_iter_from_func (filter
,
914 struct cleanup
*old_chain
= make_cleanup_py_xdecref (locals_iter
);
916 if (locals_iter
== NULL
)
919 make_cleanup_ui_out_list_begin_end (out
, "locals");
921 if (locals_iter
!= Py_None
)
922 if (enumerate_locals (locals_iter
, out
, indent
, args_type
,
923 0, frame
) == PY_BT_ERROR
)
926 do_cleanups (old_chain
);
930 do_cleanups (old_chain
);
934 /* Helper function for printing frame arguments. This function
935 largely just creates the wrapping tuple, and calls enumerate_args.
936 Returns PY_BT_ERROR on error, with any GDB exceptions converted to
937 a Python exception, or PY_BT_OK on success. */
939 static enum py_bt_status
940 py_print_args (PyObject
*filter
,
942 enum py_frame_args args_type
,
943 struct frame_info
*frame
)
945 PyObject
*args_iter
= get_py_iter_from_func (filter
, "frame_args");
946 struct cleanup
*old_chain
= make_cleanup_py_xdecref (args_iter
);
947 volatile struct gdb_exception except
;
949 if (args_iter
== NULL
)
952 make_cleanup_ui_out_list_begin_end (out
, "args");
954 TRY_CATCH (except
, RETURN_MASK_ALL
)
956 annotate_frame_args ();
957 if (! ui_out_is_mi_like_p (out
))
958 ui_out_text (out
, " (");
960 if (except
.reason
< 0)
962 gdbpy_convert_exception (except
);
966 if (args_iter
!= Py_None
)
967 if (enumerate_args (args_iter
, out
, args_type
, 0, frame
) == PY_BT_ERROR
)
970 TRY_CATCH (except
, RETURN_MASK_ALL
)
972 if (! ui_out_is_mi_like_p (out
))
973 ui_out_text (out
, ")");
975 if (except
.reason
< 0)
977 gdbpy_convert_exception (except
);
981 do_cleanups (old_chain
);
985 do_cleanups (old_chain
);
989 /* Print a single frame to the designated output stream, detecting
990 whether the output is MI or console, and formatting the output
991 according to the conventions of that protocol. FILTER is the
992 frame-filter associated with this frame. FLAGS is an integer
993 describing the various print options. The FLAGS variables is
994 described in "apply_frame_filter" function. ARGS_TYPE is an
995 enumerator describing the argument format. OUT is the output
996 stream to print, INDENT is the level of indention for this frame
997 (in the case of elided frames), and LEVELS_PRINTED is a hash-table
998 containing all the frames level that have already been printed.
999 If a frame level has been printed, do not print it again (in the
1000 case of elided frames). Returns PY_BT_ERROR on error, with any
1001 GDB exceptions converted to a Python exception, or PY_BT_COMPLETED
1004 static enum py_bt_status
1005 py_print_frame (PyObject
*filter
, int flags
, enum py_frame_args args_type
,
1006 struct ui_out
*out
, int indent
, htab_t levels_printed
)
1009 CORE_ADDR address
= 0;
1010 struct gdbarch
*gdbarch
= NULL
;
1011 struct frame_info
*frame
= NULL
;
1012 struct cleanup
*cleanup_stack
= make_cleanup (null_cleanup
, NULL
);
1013 struct value_print_options opts
;
1014 PyObject
*py_inf_frame
, *elided
;
1015 int print_level
, print_frame_info
, print_args
, print_locals
;
1016 volatile struct gdb_exception except
;
1018 /* Extract print settings from FLAGS. */
1019 print_level
= (flags
& PRINT_LEVEL
) ? 1 : 0;
1020 print_frame_info
= (flags
& PRINT_FRAME_INFO
) ? 1 : 0;
1021 print_args
= (flags
& PRINT_ARGS
) ? 1 : 0;
1022 print_locals
= (flags
& PRINT_LOCALS
) ? 1 : 0;
1024 get_user_print_options (&opts
);
1026 /* Get the underlying frame. This is needed to determine GDB
1027 architecture, and also, in the cases of frame variables/arguments to
1028 read them if they returned filter object requires us to do so. */
1029 py_inf_frame
= PyObject_CallMethod (filter
, "inferior_frame", NULL
);
1030 if (py_inf_frame
== NULL
)
1033 frame
= frame_object_to_frame_info (py_inf_frame
);;
1035 Py_DECREF (py_inf_frame
);
1040 TRY_CATCH (except
, RETURN_MASK_ALL
)
1042 gdbarch
= get_frame_arch (frame
);
1044 if (except
.reason
< 0)
1046 gdbpy_convert_exception (except
);
1051 /* stack-list-variables. */
1052 if (print_locals
&& print_args
&& ! print_frame_info
)
1054 if (py_mi_print_variables (filter
, out
, &opts
,
1055 args_type
, frame
) == PY_BT_ERROR
)
1059 do_cleanups (cleanup_stack
);
1060 return PY_BT_COMPLETED
;
1064 /* -stack-list-locals does not require a
1065 wrapping frame attribute. */
1066 if (print_frame_info
|| (print_args
&& ! print_locals
))
1067 make_cleanup_ui_out_tuple_begin_end (out
, "frame");
1069 if (print_frame_info
)
1071 /* Elided frames are also printed with this function (recursively)
1072 and are printed with indention. */
1075 TRY_CATCH (except
, RETURN_MASK_ALL
)
1077 ui_out_spaces (out
, indent
*4);
1079 if (except
.reason
< 0)
1081 gdbpy_convert_exception (except
);
1086 /* The address is required for frame annotations, and also for
1087 address printing. */
1088 if (PyObject_HasAttrString (filter
, "address"))
1090 PyObject
*paddr
= PyObject_CallMethod (filter
, "address", NULL
);
1093 if (paddr
!= Py_None
)
1095 address
= PyLong_AsLong (paddr
);
1105 /* Print frame level. MI does not require the level if
1106 locals/variables only are being printed. */
1107 if ((print_frame_info
|| print_args
) && print_level
)
1109 struct frame_info
**slot
;
1111 volatile struct gdb_exception except
;
1113 slot
= (struct frame_info
**) htab_find_slot (levels_printed
,
1115 TRY_CATCH (except
, RETURN_MASK_ALL
)
1117 level
= frame_relative_level (frame
);
1119 /* Check if this frame has already been printed (there are cases
1120 where elided synthetic dummy-frames have to 'borrow' the frame
1121 architecture from the eliding frame. If that is the case, do
1122 not print 'level', but print spaces. */
1124 ui_out_field_skip (out
, "level");
1128 annotate_frame_begin (print_level
? level
: 0,
1130 ui_out_text (out
, "#");
1131 ui_out_field_fmt_int (out
, 2, ui_left
, "level",
1135 if (except
.reason
< 0)
1137 gdbpy_convert_exception (except
);
1142 if (print_frame_info
)
1144 /* Print address to the address field. If an address is not provided,
1146 if (opts
.addressprint
&& has_addr
)
1148 TRY_CATCH (except
, RETURN_MASK_ALL
)
1150 annotate_frame_address ();
1151 ui_out_field_core_addr (out
, "addr", gdbarch
, address
);
1152 annotate_frame_address_end ();
1153 ui_out_text (out
, " in ");
1155 if (except
.reason
< 0)
1157 gdbpy_convert_exception (except
);
1162 /* Print frame function name. */
1163 if (PyObject_HasAttrString (filter
, "function"))
1165 PyObject
*py_func
= PyObject_CallMethod (filter
, "function", NULL
);
1167 if (py_func
!= NULL
)
1169 const char *function
= NULL
;
1171 if (gdbpy_is_string (py_func
))
1173 char *function_to_free
= NULL
;
1175 function
= function_to_free
=
1176 python_string_to_host_string (py_func
);
1178 if (function
== NULL
)
1180 Py_DECREF (py_func
);
1183 make_cleanup (xfree
, function_to_free
);
1185 else if (PyLong_Check (py_func
))
1187 CORE_ADDR addr
= PyLong_AsUnsignedLongLong (py_func
);
1188 struct bound_minimal_symbol msymbol
;
1190 if (PyErr_Occurred ())
1193 msymbol
= lookup_minimal_symbol_by_pc (addr
);
1194 if (msymbol
.minsym
!= NULL
)
1195 function
= SYMBOL_PRINT_NAME (msymbol
.minsym
);
1197 else if (py_func
!= Py_None
)
1199 PyErr_SetString (PyExc_RuntimeError
,
1200 _("FrameDecorator.function: expecting a " \
1201 "String, integer or None."));
1202 Py_DECREF (py_func
);
1207 TRY_CATCH (except
, RETURN_MASK_ALL
)
1209 annotate_frame_function_name ();
1210 if (function
== NULL
)
1211 ui_out_field_skip (out
, "func");
1213 ui_out_field_string (out
, "func", function
);
1215 if (except
.reason
< 0)
1217 Py_DECREF (py_func
);
1218 gdbpy_convert_exception (except
);
1221 Py_DECREF (py_func
);
1229 /* Frame arguments. Check the result, and error if something went
1233 if (py_print_args (filter
, out
, args_type
, frame
) == PY_BT_ERROR
)
1237 /* File name/source/line number information. */
1238 if (print_frame_info
)
1240 TRY_CATCH (except
, RETURN_MASK_ALL
)
1242 annotate_frame_source_begin ();
1244 if (except
.reason
< 0)
1246 gdbpy_convert_exception (except
);
1250 if (PyObject_HasAttrString (filter
, "filename"))
1252 PyObject
*py_fn
= PyObject_CallMethod (filter
, "filename",
1256 if (py_fn
!= Py_None
)
1258 char *filename
= python_string_to_host_string (py_fn
);
1260 if (filename
== NULL
)
1266 make_cleanup (xfree
, filename
);
1267 TRY_CATCH (except
, RETURN_MASK_ALL
)
1269 ui_out_wrap_hint (out
, " ");
1270 ui_out_text (out
, " at ");
1271 annotate_frame_source_file ();
1272 ui_out_field_string (out
, "file", filename
);
1273 annotate_frame_source_file_end ();
1275 if (except
.reason
< 0)
1278 gdbpy_convert_exception (except
);
1288 if (PyObject_HasAttrString (filter
, "line"))
1290 PyObject
*py_line
= PyObject_CallMethod (filter
, "line", NULL
);
1293 if (py_line
!= NULL
)
1295 if (py_line
!= Py_None
)
1297 line
= PyLong_AsLong (py_line
);
1298 TRY_CATCH (except
, RETURN_MASK_ALL
)
1300 ui_out_text (out
, ":");
1301 annotate_frame_source_line ();
1302 ui_out_field_int (out
, "line", line
);
1304 if (except
.reason
< 0)
1306 Py_DECREF (py_line
);
1307 gdbpy_convert_exception (except
);
1311 Py_DECREF (py_line
);
1318 /* For MI we need to deal with the "children" list population of
1319 elided frames, so if MI output detected do not send newline. */
1320 if (! ui_out_is_mi_like_p (out
))
1322 TRY_CATCH (except
, RETURN_MASK_ALL
)
1324 annotate_frame_end ();
1325 ui_out_text (out
, "\n");
1327 if (except
.reason
< 0)
1329 gdbpy_convert_exception (except
);
1336 if (py_print_locals (filter
, out
, args_type
, indent
,
1337 frame
) == PY_BT_ERROR
)
1341 /* Finally recursively print elided frames, if any. */
1342 elided
= get_py_iter_from_func (filter
, "elided");
1346 make_cleanup_py_decref (elided
);
1347 if (elided
!= Py_None
)
1351 make_cleanup_ui_out_list_begin_end (out
, "children");
1353 if (! ui_out_is_mi_like_p (out
))
1356 while ((item
= PyIter_Next (elided
)))
1358 enum py_bt_status success
= py_print_frame (item
, flags
,
1363 if (success
== PY_BT_ERROR
)
1371 if (item
== NULL
&& PyErr_Occurred ())
1376 do_cleanups (cleanup_stack
);
1377 return PY_BT_COMPLETED
;
1380 do_cleanups (cleanup_stack
);
1384 /* Helper function to initiate frame filter invocation at starting
1388 bootstrap_python_frame_filters (struct frame_info
*frame
,
1389 int frame_low
, int frame_high
)
1391 struct cleanup
*cleanups
=
1392 make_cleanup (null_cleanup
, NULL
);
1393 PyObject
*module
, *sort_func
, *iterable
, *frame_obj
, *iterator
;
1394 PyObject
*py_frame_low
, *py_frame_high
;
1396 frame_obj
= frame_info_to_frame_object (frame
);
1397 if (frame_obj
== NULL
)
1399 make_cleanup_py_decref (frame_obj
);
1401 module
= PyImport_ImportModule ("gdb.frames");
1404 make_cleanup_py_decref (module
);
1406 sort_func
= PyObject_GetAttrString (module
, "execute_frame_filters");
1407 if (sort_func
== NULL
)
1409 make_cleanup_py_decref (sort_func
);
1411 py_frame_low
= PyInt_FromLong (frame_low
);
1412 if (py_frame_low
== NULL
)
1414 make_cleanup_py_decref (py_frame_low
);
1416 py_frame_high
= PyInt_FromLong (frame_high
);
1417 if (py_frame_high
== NULL
)
1419 make_cleanup_py_decref (py_frame_high
);
1421 iterable
= PyObject_CallFunctionObjArgs (sort_func
, frame_obj
,
1425 if (iterable
== NULL
)
1428 do_cleanups (cleanups
);
1430 if (iterable
!= Py_None
)
1432 iterator
= PyObject_GetIter (iterable
);
1433 Py_DECREF (iterable
);
1443 do_cleanups (cleanups
);
1447 /* This is the only publicly exported function in this file. FRAME
1448 is the source frame to start frame-filter invocation. FLAGS is an
1449 integer holding the flags for printing. The following elements of
1450 the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
1451 PRINT_LEVEL is a flag indicating whether to print the frame's
1452 relative level in the output. PRINT_FRAME_INFO is a flag that
1453 indicates whether this function should print the frame
1454 information, PRINT_ARGS is a flag that indicates whether to print
1455 frame arguments, and PRINT_LOCALS, likewise, with frame local
1456 variables. ARGS_TYPE is an enumerator describing the argument
1457 format, OUT is the output stream to print. FRAME_LOW is the
1458 beginning of the slice of frames to print, and FRAME_HIGH is the
1459 upper limit of the frames to count. Returns PY_BT_ERROR on error,
1460 or PY_BT_COMPLETED on success.*/
1463 apply_frame_filter (struct frame_info
*frame
, int flags
,
1464 enum py_frame_args args_type
,
1465 struct ui_out
*out
, int frame_low
,
1469 struct gdbarch
*gdbarch
= NULL
;
1470 struct cleanup
*cleanups
;
1471 enum py_bt_status success
= PY_BT_ERROR
;
1473 volatile struct gdb_exception except
;
1475 htab_t levels_printed
;
1477 if (!gdb_python_initialized
)
1478 return PY_BT_NO_FILTERS
;
1480 TRY_CATCH (except
, RETURN_MASK_ALL
)
1482 gdbarch
= get_frame_arch (frame
);
1484 if (except
.reason
< 0)
1486 /* Let gdb try to print the stack trace. */
1487 return PY_BT_NO_FILTERS
;
1490 cleanups
= ensure_python_env (gdbarch
, current_language
);
1492 iterable
= bootstrap_python_frame_filters (frame
, frame_low
, frame_high
);
1494 if (iterable
== NULL
)
1496 /* Normally if there is an error GDB prints the exception,
1497 abandons the backtrace and exits. The user can then call "bt
1498 no-filters", and get a default backtrace (it would be
1499 confusing to automatically start a standard backtrace halfway
1500 through a Python filtered backtrace). However in the case
1501 where GDB cannot initialize the frame filters (most likely
1502 due to incorrect auto-load paths), GDB has printed nothing.
1503 In this case it is OK to print the default backtrace after
1504 printing the error message. GDB returns PY_BT_NO_FILTERS
1505 here to signify there are no filters after printing the
1506 initialization error. This return code will trigger a
1507 default backtrace. */
1509 gdbpy_print_stack ();
1510 do_cleanups (cleanups
);
1511 return PY_BT_NO_FILTERS
;
1514 /* If iterable is None, then there are no frame filters registered.
1515 If this is the case, defer to default GDB printing routines in MI
1517 make_cleanup_py_decref (iterable
);
1518 if (iterable
== Py_None
)
1520 success
= PY_BT_NO_FILTERS
;
1524 levels_printed
= htab_create (20,
1528 make_cleanup_htab_delete (levels_printed
);
1530 while ((item
= PyIter_Next (iterable
)))
1532 success
= py_print_frame (item
, flags
, args_type
, out
, 0,
1535 /* Do not exit on error printing a single frame. Print the
1536 error and continue with other frames. */
1537 if (success
== PY_BT_ERROR
)
1538 gdbpy_print_stack ();
1543 if (item
== NULL
&& PyErr_Occurred ())
1547 do_cleanups (cleanups
);
1550 /* Exit and abandon backtrace on error, printing the exception that
1553 gdbpy_print_stack ();
1554 do_cleanups (cleanups
);