1 /* Python frame filters
3 Copyright (C) 2013-2015 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"
40 /* Helper function to extract a symbol, a name and a language
41 definition from a Python object that conforms to the "Symbol Value"
42 interface. OBJ is the Python object to extract the values from.
43 NAME is a pass-through argument where the name of the symbol will
44 be written. NAME is allocated in this function, but the caller is
45 responsible for clean up. SYM is a pass-through argument where the
46 symbol will be written. In the case of the API returning a string,
47 this will be set to NULL. LANGUAGE is also a pass-through argument
48 denoting the language attributed to the Symbol. In the case of SYM
49 being NULL, this will be set to the current language. Returns
50 EXT_LANG_BT_ERROR on error with the appropriate Python exception set, and
51 EXT_LANG_BT_OK on success. */
53 static enum ext_lang_bt_status
54 extract_sym (PyObject
*obj
, char **name
, struct symbol
**sym
,
55 const struct language_defn
**language
)
57 PyObject
*result
= PyObject_CallMethod (obj
, "symbol", NULL
);
60 return EXT_LANG_BT_ERROR
;
62 /* For 'symbol' callback, the function can return a symbol or a
64 if (gdbpy_is_string (result
))
66 *name
= python_string_to_host_string (result
);
70 return EXT_LANG_BT_ERROR
;
71 /* If the API returns a string (and not a symbol), then there is
72 no symbol derived language available and the frame filter has
73 either overridden the symbol with a string, or supplied a
74 entirely synthetic symbol/value pairing. In that case, use
76 *language
= python_language
;
81 /* This type checks 'result' during the conversion so we
82 just call it unconditionally and check the return. */
83 *sym
= symbol_object_to_symbol (result
);
89 PyErr_SetString (PyExc_RuntimeError
,
90 _("Unexpected value. Expecting a "
91 "gdb.Symbol or a Python string."));
92 return EXT_LANG_BT_ERROR
;
95 /* Duplicate the symbol name, so the caller has consistency
96 in garbage collection. */
97 *name
= xstrdup (SYMBOL_PRINT_NAME (*sym
));
99 /* If a symbol is specified attempt to determine the language
100 from the symbol. If mode is not "auto", then the language
101 has been explicitly set, use that. */
102 if (language_mode
== language_mode_auto
)
103 *language
= language_def (SYMBOL_LANGUAGE (*sym
));
105 *language
= current_language
;
108 return EXT_LANG_BT_OK
;
111 /* Helper function to extract a value from an object that conforms to
112 the "Symbol Value" interface. OBJ is the Python object to extract
113 the value from. VALUE is a pass-through argument where the value
114 will be written. If the object does not have the value attribute,
115 or provides the Python None for a value, VALUE will be set to NULL
116 and this function will return as successful. Returns EXT_LANG_BT_ERROR
117 on error with the appropriate Python exception set, and EXT_LANG_BT_OK on
120 static enum ext_lang_bt_status
121 extract_value (PyObject
*obj
, struct value
**value
)
123 if (PyObject_HasAttrString (obj
, "value"))
125 PyObject
*vresult
= PyObject_CallMethod (obj
, "value", NULL
);
128 return EXT_LANG_BT_ERROR
;
130 /* The Python code has returned 'None' for a value, so we set
131 value to NULL. This flags that GDB should read the
133 if (vresult
== Py_None
)
137 return EXT_LANG_BT_OK
;
141 *value
= convert_value_from_python (vresult
);
145 return EXT_LANG_BT_ERROR
;
147 return EXT_LANG_BT_OK
;
153 return EXT_LANG_BT_OK
;
156 /* MI prints only certain values according to the type of symbol and
157 also what the user has specified. SYM is the symbol to check, and
158 MI_PRINT_TYPES is an enum specifying what the user wants emitted
159 for the MI command in question. */
161 mi_should_print (struct symbol
*sym
, enum mi_print_types type
)
165 switch (SYMBOL_CLASS (sym
))
168 case LOC_UNDEF
: /* catches errors */
169 case LOC_CONST
: /* constant */
170 case LOC_TYPEDEF
: /* local typedef */
171 case LOC_LABEL
: /* local label */
172 case LOC_BLOCK
: /* local function */
173 case LOC_CONST_BYTES
: /* loc. byte seq. */
174 case LOC_UNRESOLVED
: /* unresolved static */
175 case LOC_OPTIMIZED_OUT
: /* optimized out */
179 case LOC_ARG
: /* argument */
180 case LOC_REF_ARG
: /* reference arg */
181 case LOC_REGPARM_ADDR
: /* indirect register arg */
182 case LOC_LOCAL
: /* stack local */
183 case LOC_STATIC
: /* static */
184 case LOC_REGISTER
: /* register */
185 case LOC_COMPUTED
: /* computed location */
186 if (type
== MI_PRINT_LOCALS
)
187 print_me
= ! SYMBOL_IS_ARGUMENT (sym
);
189 print_me
= SYMBOL_IS_ARGUMENT (sym
);
194 /* Helper function which outputs a type name extracted from VAL to a
195 "type" field in the output stream OUT. OUT is the ui-out structure
196 the type name will be output too, and VAL is the value that the
197 type will be extracted from. Returns EXT_LANG_BT_ERROR on error, with
198 any GDB exceptions converted to a Python exception, or EXT_LANG_BT_OK on
201 static enum ext_lang_bt_status
202 py_print_type (struct ui_out
*out
, struct value
*val
)
209 struct cleanup
*cleanup
;
211 stb
= mem_fileopen ();
212 cleanup
= make_cleanup_ui_file_delete (stb
);
213 type
= check_typedef (value_type (val
));
214 type_print (value_type (val
), "", stb
, -1);
215 ui_out_field_stream (out
, "type", stb
);
216 do_cleanups (cleanup
);
218 CATCH (except
, RETURN_MASK_ALL
)
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;
245 int local_indent
= (4 * indent
);
247 /* Never set an indent level for common_val_print if MI. */
248 if (ui_out_is_mi_like_p (out
))
251 /* MI does not print certain values, differentiated by type,
252 depending on what ARGS_TYPE indicates. Test type against option.
253 For CLI print all values. */
254 if (args_type
== MI_PRINT_SIMPLE_VALUES
255 || args_type
== MI_PRINT_ALL_VALUES
)
257 struct type
*type
= NULL
;
261 type
= check_typedef (value_type (val
));
263 CATCH (except
, RETURN_MASK_ALL
)
265 gdbpy_convert_exception (except
);
266 return EXT_LANG_BT_ERROR
;
270 if (args_type
== MI_PRINT_ALL_VALUES
)
272 else if (args_type
== MI_PRINT_SIMPLE_VALUES
273 && TYPE_CODE (type
) != TYPE_CODE_ARRAY
274 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
275 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
278 else if (args_type
!= NO_VALUES
)
286 struct cleanup
*cleanup
;
288 stb
= mem_fileopen ();
289 cleanup
= make_cleanup_ui_file_delete (stb
);
290 common_val_print (val
, stb
, indent
, opts
, language
);
291 ui_out_field_stream (out
, "value", stb
);
292 do_cleanups (cleanup
);
294 CATCH (except
, RETURN_MASK_ALL
)
296 gdbpy_convert_exception (except
);
297 return EXT_LANG_BT_ERROR
;
302 return EXT_LANG_BT_OK
;
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 EXT_LANG_BT_ERROR on error, with any GDB exceptions
354 converted to a Python exception, or EXT_LANG_BT_OK on success. */
356 static enum ext_lang_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 ext_lang_frame_args args_type
,
363 int print_args_field
,
364 const struct language_defn
*language
)
367 enum ext_lang_bt_status retval
= EXT_LANG_BT_OK
;
371 if (fa
->val
== NULL
&& fa
->error
== NULL
)
372 return EXT_LANG_BT_OK
;
373 language
= language_def (SYMBOL_LANGUAGE (fa
->sym
));
381 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
383 /* MI has varying rules for tuples, but generally if there is only
384 one element in each item in the list, do not start a tuple. The
385 exception is -stack-list-variables which emits an ARGS="1" field
386 if the value is a frame argument. This is denoted in this
387 function with PRINT_ARGS_FIELD which is flag from the caller to
388 emit the ARGS field. */
389 if (ui_out_is_mi_like_p (out
))
391 if (print_args_field
|| args_type
!= NO_VALUES
)
392 make_cleanup_ui_out_tuple_begin_end (out
, NULL
);
395 annotate_arg_begin ();
397 /* If frame argument is populated, check for entry-values and the
398 entry value options. */
403 stb
= mem_fileopen ();
404 make_cleanup_ui_file_delete (stb
);
405 fprintf_symbol_filtered (stb
, SYMBOL_PRINT_NAME (fa
->sym
),
406 SYMBOL_LANGUAGE (fa
->sym
),
407 DMGL_PARAMS
| DMGL_ANSI
);
408 if (fa
->entry_kind
== print_entry_values_compact
)
410 fputs_filtered ("=", stb
);
412 fprintf_symbol_filtered (stb
, SYMBOL_PRINT_NAME (fa
->sym
),
413 SYMBOL_LANGUAGE (fa
->sym
),
414 DMGL_PARAMS
| DMGL_ANSI
);
416 if (fa
->entry_kind
== print_entry_values_only
417 || fa
->entry_kind
== print_entry_values_compact
)
419 fputs_filtered ("@entry", stb
);
421 ui_out_field_stream (out
, "name", stb
);
424 /* Otherwise, just output the name. */
425 ui_out_field_string (out
, "name", sym_name
);
427 annotate_arg_name_end ();
429 if (! ui_out_is_mi_like_p (out
))
430 ui_out_text (out
, "=");
432 if (print_args_field
)
433 ui_out_field_int (out
, "arg", 1);
435 /* For MI print the type, but only for simple values. This seems
436 weird, but this is how MI choose to format the various output
438 if (args_type
== MI_PRINT_SIMPLE_VALUES
&& val
!= NULL
)
440 if (py_print_type (out
, val
) == EXT_LANG_BT_ERROR
)
442 retval
= EXT_LANG_BT_ERROR
;
443 do_cleanups (cleanups
);
449 annotate_arg_value (value_type (val
));
451 /* If the output is to the CLI, and the user option "set print
452 frame-arguments" is set to none, just output "...". */
453 if (! ui_out_is_mi_like_p (out
) && args_type
== NO_VALUES
)
454 ui_out_field_string (out
, "value", "...");
457 /* Otherwise, print the value for both MI and the CLI, except
458 for the case of MI_PRINT_NO_VALUES. */
459 if (args_type
!= NO_VALUES
)
463 gdb_assert (fa
!= NULL
&& fa
->error
!= NULL
);
464 ui_out_field_fmt (out
, "value",
465 _("<error reading variable: %s>"),
468 else if (py_print_value (out
, val
, opts
, 0, args_type
, language
)
469 == EXT_LANG_BT_ERROR
)
470 retval
= EXT_LANG_BT_ERROR
;
474 do_cleanups (cleanups
);
476 CATCH (except
, RETURN_MASK_ERROR
)
478 gdbpy_convert_exception (except
);
485 /* Helper function to loop over frame arguments provided by the
486 "frame_arguments" Python API. Elements in the iterator must
487 conform to the "Symbol Value" interface. ITER is the Python
488 iterable object, OUT is the output stream, ARGS_TYPE is an
489 enumerator describing the argument format, PRINT_ARGS_FIELD is a
490 flag which indicates if we output "ARGS=1" in MI output in commands
491 where both arguments and locals are printed, and FRAME is the
492 backing frame. Returns EXT_LANG_BT_ERROR on error, with any GDB
493 exceptions converted to a Python exception, or EXT_LANG_BT_OK on
496 static enum ext_lang_bt_status
497 enumerate_args (PyObject
*iter
,
499 enum ext_lang_frame_args args_type
,
500 int print_args_field
,
501 struct frame_info
*frame
)
504 struct value_print_options opts
;
506 get_user_print_options (&opts
);
508 if (args_type
== CLI_SCALAR_VALUES
)
510 /* True in "summary" mode, false otherwise. */
518 annotate_frame_args ();
520 CATCH (except
, RETURN_MASK_ALL
)
522 gdbpy_convert_exception (except
);
527 /* Collect the first argument outside of the loop, so output of
528 commas in the argument output is correct. At the end of the
529 loop block collect another item from the iterator, and, if it is
530 not null emit a comma. */
531 item
= PyIter_Next (iter
);
532 if (item
== NULL
&& PyErr_Occurred ())
537 const struct language_defn
*language
;
541 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
543 success
= extract_sym (item
, &sym_name
, &sym
, &language
);
544 if (success
== EXT_LANG_BT_ERROR
)
550 success
= extract_value (item
, &val
);
551 if (success
== EXT_LANG_BT_ERROR
)
561 if (sym
&& ui_out_is_mi_like_p (out
)
562 && ! mi_should_print (sym
, MI_PRINT_ARGS
))
568 /* If the object did not provide a value, read it using
569 read_frame_args and account for entry values, if any. */
572 struct frame_arg arg
, entryarg
;
574 /* If there is no value, and also no symbol, set error and
578 PyErr_SetString (PyExc_RuntimeError
,
579 _("No symbol or value provided."));
586 read_frame_arg (sym
, frame
, &arg
, &entryarg
);
588 CATCH (except
, RETURN_MASK_ALL
)
591 gdbpy_convert_exception (except
);
596 /* The object has not provided a value, so this is a frame
597 argument to be read by GDB. In this case we have to
598 account for entry-values. */
600 if (arg
.entry_kind
!= print_entry_values_only
)
602 if (py_print_single_arg (out
, NULL
, &arg
,
606 NULL
) == EXT_LANG_BT_ERROR
)
609 xfree (entryarg
.error
);
615 if (entryarg
.entry_kind
!= print_entry_values_no
)
617 if (arg
.entry_kind
!= print_entry_values_only
)
621 ui_out_text (out
, ", ");
622 ui_out_wrap_hint (out
, " ");
624 CATCH (except
, RETURN_MASK_ALL
)
627 xfree (entryarg
.error
);
629 gdbpy_convert_exception (except
);
635 if (py_print_single_arg (out
, NULL
, &entryarg
, NULL
, &opts
,
636 args_type
, print_args_field
, NULL
)
637 == EXT_LANG_BT_ERROR
)
640 xfree (entryarg
.error
);
647 xfree (entryarg
.error
);
651 /* If the object has provided a value, we just print that. */
654 if (py_print_single_arg (out
, sym_name
, NULL
, val
, &opts
,
655 args_type
, print_args_field
,
656 language
) == EXT_LANG_BT_ERROR
)
666 /* Collect the next item from the iterator. If
667 this is the last item, do not print the
669 item
= PyIter_Next (iter
);
674 ui_out_text (out
, ", ");
676 CATCH (except
, RETURN_MASK_ALL
)
679 gdbpy_convert_exception (except
);
684 else if (PyErr_Occurred ())
691 CATCH (except
, RETURN_MASK_ALL
)
694 gdbpy_convert_exception (except
);
700 return EXT_LANG_BT_OK
;
703 return EXT_LANG_BT_ERROR
;
707 /* Helper function to loop over variables provided by the
708 "frame_locals" Python API. Elements in the iterable must conform
709 to the "Symbol Value" interface. ITER is the Python iterable
710 object, OUT is the output stream, INDENT is whether we should
711 indent the output (for CLI), ARGS_TYPE is an enumerator describing
712 the argument format, PRINT_ARGS_FIELD is flag which indicates
713 whether to output the ARGS field in the case of
714 -stack-list-variables and FRAME is the backing frame. Returns
715 EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to a Python
716 exception, or EXT_LANG_BT_OK on success. */
718 static enum ext_lang_bt_status
719 enumerate_locals (PyObject
*iter
,
722 enum ext_lang_frame_args args_type
,
723 int print_args_field
,
724 struct frame_info
*frame
)
727 struct value_print_options opts
;
729 get_user_print_options (&opts
);
732 while ((item
= PyIter_Next (iter
)))
734 const struct language_defn
*language
;
737 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
739 int local_indent
= 8 + (8 * indent
);
740 struct cleanup
*locals_cleanups
;
742 locals_cleanups
= make_cleanup_py_decref (item
);
744 success
= extract_sym (item
, &sym_name
, &sym
, &language
);
745 if (success
== EXT_LANG_BT_ERROR
)
747 do_cleanups (locals_cleanups
);
751 make_cleanup (xfree
, sym_name
);
753 success
= extract_value (item
, &val
);
754 if (success
== EXT_LANG_BT_ERROR
)
756 do_cleanups (locals_cleanups
);
760 if (sym
!= NULL
&& ui_out_is_mi_like_p (out
)
761 && ! mi_should_print (sym
, MI_PRINT_LOCALS
))
763 do_cleanups (locals_cleanups
);
767 /* If the object did not provide a value, read it. */
772 val
= read_var_value (sym
, frame
);
774 CATCH (except
, RETURN_MASK_ERROR
)
776 gdbpy_convert_exception (except
);
777 do_cleanups (locals_cleanups
);
783 /* With PRINT_NO_VALUES, MI does not emit a tuple normally as
784 each output contains only one field. The exception is
785 -stack-list-variables, which always provides a tuple. */
786 if (ui_out_is_mi_like_p (out
))
788 if (print_args_field
|| args_type
!= NO_VALUES
)
789 make_cleanup_ui_out_tuple_begin_end (out
, NULL
);
793 if (! ui_out_is_mi_like_p (out
))
795 /* If the output is not MI we indent locals. */
796 ui_out_spaces (out
, local_indent
);
799 ui_out_field_string (out
, "name", sym_name
);
801 if (! ui_out_is_mi_like_p (out
))
802 ui_out_text (out
, " = ");
804 CATCH (except
, RETURN_MASK_ERROR
)
806 gdbpy_convert_exception (except
);
807 do_cleanups (locals_cleanups
);
812 if (args_type
== MI_PRINT_SIMPLE_VALUES
)
814 if (py_print_type (out
, val
) == EXT_LANG_BT_ERROR
)
816 do_cleanups (locals_cleanups
);
821 /* CLI always prints values for locals. MI uses the
822 simple/no/all system. */
823 if (! ui_out_is_mi_like_p (out
))
825 int val_indent
= (indent
+ 1) * 4;
827 if (py_print_value (out
, val
, &opts
, val_indent
, args_type
,
828 language
) == EXT_LANG_BT_ERROR
)
830 do_cleanups (locals_cleanups
);
836 if (args_type
!= NO_VALUES
)
838 if (py_print_value (out
, val
, &opts
, 0, args_type
,
839 language
) == EXT_LANG_BT_ERROR
)
841 do_cleanups (locals_cleanups
);
847 do_cleanups (locals_cleanups
);
851 ui_out_text (out
, "\n");
853 CATCH (except
, RETURN_MASK_ERROR
)
855 gdbpy_convert_exception (except
);
861 if (item
== NULL
&& PyErr_Occurred ())
864 return EXT_LANG_BT_OK
;
867 return EXT_LANG_BT_ERROR
;
870 /* Helper function for -stack-list-variables. Returns EXT_LANG_BT_ERROR on
871 error, or EXT_LANG_BT_OK on success. */
873 static enum ext_lang_bt_status
874 py_mi_print_variables (PyObject
*filter
, struct ui_out
*out
,
875 struct value_print_options
*opts
,
876 enum ext_lang_frame_args args_type
,
877 struct frame_info
*frame
)
879 struct cleanup
*old_chain
;
881 PyObject
*locals_iter
;
883 args_iter
= get_py_iter_from_func (filter
, "frame_args");
884 old_chain
= make_cleanup_py_xdecref (args_iter
);
885 if (args_iter
== NULL
)
888 locals_iter
= get_py_iter_from_func (filter
, "frame_locals");
889 if (locals_iter
== NULL
)
892 make_cleanup_py_decref (locals_iter
);
893 make_cleanup_ui_out_list_begin_end (out
, "variables");
895 if (args_iter
!= Py_None
)
896 if (enumerate_args (args_iter
, out
, args_type
, 1, frame
)
897 == EXT_LANG_BT_ERROR
)
900 if (locals_iter
!= Py_None
)
901 if (enumerate_locals (locals_iter
, out
, 1, args_type
, 1, frame
)
902 == EXT_LANG_BT_ERROR
)
905 do_cleanups (old_chain
);
906 return EXT_LANG_BT_OK
;
909 do_cleanups (old_chain
);
910 return EXT_LANG_BT_ERROR
;
913 /* Helper function for printing locals. This function largely just
914 creates the wrapping tuple, and calls enumerate_locals. Returns
915 EXT_LANG_BT_ERROR on error, or EXT_LANG_BT_OK on success. */
917 static enum ext_lang_bt_status
918 py_print_locals (PyObject
*filter
,
920 enum ext_lang_frame_args args_type
,
922 struct frame_info
*frame
)
924 PyObject
*locals_iter
= get_py_iter_from_func (filter
,
926 struct cleanup
*old_chain
= make_cleanup_py_xdecref (locals_iter
);
928 if (locals_iter
== NULL
)
931 make_cleanup_ui_out_list_begin_end (out
, "locals");
933 if (locals_iter
!= Py_None
)
934 if (enumerate_locals (locals_iter
, out
, indent
, args_type
,
935 0, frame
) == EXT_LANG_BT_ERROR
)
938 do_cleanups (old_chain
);
939 return EXT_LANG_BT_OK
;
942 do_cleanups (old_chain
);
943 return EXT_LANG_BT_ERROR
;
946 /* Helper function for printing frame arguments. This function
947 largely just creates the wrapping tuple, and calls enumerate_args.
948 Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to
949 a Python exception, or EXT_LANG_BT_OK on success. */
951 static enum ext_lang_bt_status
952 py_print_args (PyObject
*filter
,
954 enum ext_lang_frame_args args_type
,
955 struct frame_info
*frame
)
957 PyObject
*args_iter
= get_py_iter_from_func (filter
, "frame_args");
958 struct cleanup
*old_chain
= make_cleanup_py_xdecref (args_iter
);
960 if (args_iter
== NULL
)
963 make_cleanup_ui_out_list_begin_end (out
, "args");
967 annotate_frame_args ();
968 if (! ui_out_is_mi_like_p (out
))
969 ui_out_text (out
, " (");
971 CATCH (except
, RETURN_MASK_ALL
)
973 gdbpy_convert_exception (except
);
978 if (args_iter
!= Py_None
)
979 if (enumerate_args (args_iter
, out
, args_type
, 0, frame
)
980 == EXT_LANG_BT_ERROR
)
985 if (! ui_out_is_mi_like_p (out
))
986 ui_out_text (out
, ")");
988 CATCH (except
, RETURN_MASK_ALL
)
990 gdbpy_convert_exception (except
);
995 do_cleanups (old_chain
);
996 return EXT_LANG_BT_OK
;
999 do_cleanups (old_chain
);
1000 return EXT_LANG_BT_ERROR
;
1003 /* Print a single frame to the designated output stream, detecting
1004 whether the output is MI or console, and formatting the output
1005 according to the conventions of that protocol. FILTER is the
1006 frame-filter associated with this frame. FLAGS is an integer
1007 describing the various print options. The FLAGS variables is
1008 described in "apply_frame_filter" function. ARGS_TYPE is an
1009 enumerator describing the argument format. OUT is the output
1010 stream to print, INDENT is the level of indention for this frame
1011 (in the case of elided frames), and LEVELS_PRINTED is a hash-table
1012 containing all the frames level that have already been printed.
1013 If a frame level has been printed, do not print it again (in the
1014 case of elided frames). Returns EXT_LANG_BT_ERROR on error, with any
1015 GDB exceptions converted to a Python exception, or EXT_LANG_BT_COMPLETED
1016 on success. It can also throw an exception RETURN_QUIT. */
1018 static enum ext_lang_bt_status
1019 py_print_frame (PyObject
*filter
, int flags
,
1020 enum ext_lang_frame_args args_type
,
1021 struct ui_out
*out
, int indent
, htab_t levels_printed
)
1024 CORE_ADDR address
= 0;
1025 struct gdbarch
*gdbarch
= NULL
;
1026 struct frame_info
*frame
= NULL
;
1027 struct cleanup
*cleanup_stack
;
1028 struct value_print_options opts
;
1029 PyObject
*py_inf_frame
;
1030 int print_level
, print_frame_info
, print_args
, print_locals
;
1032 /* Extract print settings from FLAGS. */
1033 print_level
= (flags
& PRINT_LEVEL
) ? 1 : 0;
1034 print_frame_info
= (flags
& PRINT_FRAME_INFO
) ? 1 : 0;
1035 print_args
= (flags
& PRINT_ARGS
) ? 1 : 0;
1036 print_locals
= (flags
& PRINT_LOCALS
) ? 1 : 0;
1038 get_user_print_options (&opts
);
1040 /* Get the underlying frame. This is needed to determine GDB
1041 architecture, and also, in the cases of frame variables/arguments to
1042 read them if they returned filter object requires us to do so. */
1043 py_inf_frame
= PyObject_CallMethod (filter
, "inferior_frame", NULL
);
1044 if (py_inf_frame
== NULL
)
1045 return EXT_LANG_BT_ERROR
;
1047 frame
= frame_object_to_frame_info (py_inf_frame
);;
1049 Py_DECREF (py_inf_frame
);
1052 return EXT_LANG_BT_ERROR
;
1056 gdbarch
= get_frame_arch (frame
);
1058 CATCH (except
, RETURN_MASK_ERROR
)
1060 gdbpy_convert_exception (except
);
1061 return EXT_LANG_BT_ERROR
;
1065 /* stack-list-variables. */
1066 if (print_locals
&& print_args
&& ! print_frame_info
)
1068 if (py_mi_print_variables (filter
, out
, &opts
,
1069 args_type
, frame
) == EXT_LANG_BT_ERROR
)
1070 return EXT_LANG_BT_ERROR
;
1071 return EXT_LANG_BT_COMPLETED
;
1074 cleanup_stack
= make_cleanup (null_cleanup
, NULL
);
1076 /* -stack-list-locals does not require a
1077 wrapping frame attribute. */
1078 if (print_frame_info
|| (print_args
&& ! print_locals
))
1079 make_cleanup_ui_out_tuple_begin_end (out
, "frame");
1081 if (print_frame_info
)
1083 /* Elided frames are also printed with this function (recursively)
1084 and are printed with indention. */
1089 ui_out_spaces (out
, indent
*4);
1091 CATCH (except
, RETURN_MASK_ERROR
)
1093 gdbpy_convert_exception (except
);
1094 do_cleanups (cleanup_stack
);
1095 return EXT_LANG_BT_ERROR
;
1100 /* The address is required for frame annotations, and also for
1101 address printing. */
1102 if (PyObject_HasAttrString (filter
, "address"))
1104 PyObject
*paddr
= PyObject_CallMethod (filter
, "address", NULL
);
1108 do_cleanups (cleanup_stack
);
1109 return EXT_LANG_BT_ERROR
;
1112 if (paddr
!= Py_None
)
1114 address
= PyLong_AsLong (paddr
);
1121 /* Print frame level. MI does not require the level if
1122 locals/variables only are being printed. */
1123 if ((print_frame_info
|| print_args
) && print_level
)
1125 struct frame_info
**slot
;
1128 slot
= (struct frame_info
**) htab_find_slot (levels_printed
,
1132 level
= frame_relative_level (frame
);
1134 /* Check if this frame has already been printed (there are cases
1135 where elided synthetic dummy-frames have to 'borrow' the frame
1136 architecture from the eliding frame. If that is the case, do
1137 not print 'level', but print spaces. */
1139 ui_out_field_skip (out
, "level");
1143 annotate_frame_begin (print_level
? level
: 0,
1145 ui_out_text (out
, "#");
1146 ui_out_field_fmt_int (out
, 2, ui_left
, "level",
1150 CATCH (except
, RETURN_MASK_ERROR
)
1152 gdbpy_convert_exception (except
);
1153 do_cleanups (cleanup_stack
);
1154 return EXT_LANG_BT_ERROR
;
1159 if (print_frame_info
)
1161 /* Print address to the address field. If an address is not provided,
1163 if (opts
.addressprint
&& has_addr
)
1167 annotate_frame_address ();
1168 ui_out_field_core_addr (out
, "addr", gdbarch
, address
);
1169 annotate_frame_address_end ();
1170 ui_out_text (out
, " in ");
1172 CATCH (except
, RETURN_MASK_ERROR
)
1174 gdbpy_convert_exception (except
);
1175 do_cleanups (cleanup_stack
);
1176 return EXT_LANG_BT_ERROR
;
1181 /* Print frame function name. */
1182 if (PyObject_HasAttrString (filter
, "function"))
1184 PyObject
*py_func
= PyObject_CallMethod (filter
, "function", NULL
);
1185 struct cleanup
*py_func_cleanup
;
1186 const char *function
= NULL
;
1188 if (py_func
== NULL
)
1190 do_cleanups (cleanup_stack
);
1191 return EXT_LANG_BT_ERROR
;
1193 py_func_cleanup
= make_cleanup_py_decref (py_func
);
1195 if (gdbpy_is_string (py_func
))
1197 char *function_to_free
;
1199 function
= function_to_free
=
1200 python_string_to_host_string (py_func
);
1202 if (function
== NULL
)
1204 do_cleanups (cleanup_stack
);
1205 return EXT_LANG_BT_ERROR
;
1207 make_cleanup (xfree
, function_to_free
);
1209 else if (PyLong_Check (py_func
))
1211 CORE_ADDR addr
= PyLong_AsUnsignedLongLong (py_func
);
1212 struct bound_minimal_symbol msymbol
;
1214 if (PyErr_Occurred ())
1216 do_cleanups (cleanup_stack
);
1217 return EXT_LANG_BT_ERROR
;
1220 msymbol
= lookup_minimal_symbol_by_pc (addr
);
1221 if (msymbol
.minsym
!= NULL
)
1222 function
= MSYMBOL_PRINT_NAME (msymbol
.minsym
);
1224 else if (py_func
!= Py_None
)
1226 PyErr_SetString (PyExc_RuntimeError
,
1227 _("FrameDecorator.function: expecting a " \
1228 "String, integer or None."));
1229 do_cleanups (cleanup_stack
);
1230 return EXT_LANG_BT_ERROR
;
1235 annotate_frame_function_name ();
1236 if (function
== NULL
)
1237 ui_out_field_skip (out
, "func");
1239 ui_out_field_string (out
, "func", function
);
1241 CATCH (except
, RETURN_MASK_ERROR
)
1243 gdbpy_convert_exception (except
);
1244 do_cleanups (cleanup_stack
);
1245 return EXT_LANG_BT_ERROR
;
1249 do_cleanups (py_func_cleanup
);
1254 /* Frame arguments. Check the result, and error if something went
1258 if (py_print_args (filter
, out
, args_type
, frame
) == EXT_LANG_BT_ERROR
)
1260 do_cleanups (cleanup_stack
);
1261 return EXT_LANG_BT_ERROR
;
1265 /* File name/source/line number information. */
1266 if (print_frame_info
)
1270 annotate_frame_source_begin ();
1272 CATCH (except
, RETURN_MASK_ERROR
)
1274 gdbpy_convert_exception (except
);
1275 do_cleanups (cleanup_stack
);
1276 return EXT_LANG_BT_ERROR
;
1280 if (PyObject_HasAttrString (filter
, "filename"))
1282 PyObject
*py_fn
= PyObject_CallMethod (filter
, "filename", NULL
);
1283 struct cleanup
*py_fn_cleanup
;
1287 do_cleanups (cleanup_stack
);
1288 return EXT_LANG_BT_ERROR
;
1290 py_fn_cleanup
= make_cleanup_py_decref (py_fn
);
1292 if (py_fn
!= Py_None
)
1294 char *filename
= python_string_to_host_string (py_fn
);
1296 if (filename
== NULL
)
1298 do_cleanups (cleanup_stack
);
1299 return EXT_LANG_BT_ERROR
;
1302 make_cleanup (xfree
, filename
);
1305 ui_out_wrap_hint (out
, " ");
1306 ui_out_text (out
, " at ");
1307 annotate_frame_source_file ();
1308 ui_out_field_string (out
, "file", filename
);
1309 annotate_frame_source_file_end ();
1311 CATCH (except
, RETURN_MASK_ERROR
)
1313 gdbpy_convert_exception (except
);
1314 do_cleanups (cleanup_stack
);
1315 return EXT_LANG_BT_ERROR
;
1319 do_cleanups (py_fn_cleanup
);
1322 if (PyObject_HasAttrString (filter
, "line"))
1324 PyObject
*py_line
= PyObject_CallMethod (filter
, "line", NULL
);
1325 struct cleanup
*py_line_cleanup
;
1328 if (py_line
== NULL
)
1330 do_cleanups (cleanup_stack
);
1331 return EXT_LANG_BT_ERROR
;
1333 py_line_cleanup
= make_cleanup_py_decref (py_line
);
1335 if (py_line
!= Py_None
)
1337 line
= PyLong_AsLong (py_line
);
1340 ui_out_text (out
, ":");
1341 annotate_frame_source_line ();
1342 ui_out_field_int (out
, "line", line
);
1344 CATCH (except
, RETURN_MASK_ERROR
)
1346 gdbpy_convert_exception (except
);
1347 do_cleanups (cleanup_stack
);
1348 return EXT_LANG_BT_ERROR
;
1352 do_cleanups (py_line_cleanup
);
1356 /* For MI we need to deal with the "children" list population of
1357 elided frames, so if MI output detected do not send newline. */
1358 if (! ui_out_is_mi_like_p (out
))
1362 annotate_frame_end ();
1363 ui_out_text (out
, "\n");
1365 CATCH (except
, RETURN_MASK_ERROR
)
1367 gdbpy_convert_exception (except
);
1368 do_cleanups (cleanup_stack
);
1369 return EXT_LANG_BT_ERROR
;
1376 if (py_print_locals (filter
, out
, args_type
, indent
,
1377 frame
) == EXT_LANG_BT_ERROR
)
1379 do_cleanups (cleanup_stack
);
1380 return EXT_LANG_BT_ERROR
;
1386 struct cleanup
*elided_cleanup
;
1388 /* Finally recursively print elided frames, if any. */
1389 elided
= get_py_iter_from_func (filter
, "elided");
1392 do_cleanups (cleanup_stack
);
1393 return EXT_LANG_BT_ERROR
;
1395 elided_cleanup
= make_cleanup_py_decref (elided
);
1397 if (elided
!= Py_None
)
1401 make_cleanup_ui_out_list_begin_end (out
, "children");
1403 if (! ui_out_is_mi_like_p (out
))
1406 while ((item
= PyIter_Next (elided
)))
1408 struct cleanup
*item_cleanup
= make_cleanup_py_decref (item
);
1410 enum ext_lang_bt_status success
= py_print_frame (item
, flags
,
1415 do_cleanups (item_cleanup
);
1417 if (success
== EXT_LANG_BT_ERROR
)
1419 do_cleanups (cleanup_stack
);
1420 return EXT_LANG_BT_ERROR
;
1423 if (item
== NULL
&& PyErr_Occurred ())
1425 do_cleanups (cleanup_stack
);
1426 return EXT_LANG_BT_ERROR
;
1429 do_cleanups (elided_cleanup
);
1432 do_cleanups (cleanup_stack
);
1433 return EXT_LANG_BT_COMPLETED
;
1436 /* Helper function to initiate frame filter invocation at starting
1440 bootstrap_python_frame_filters (struct frame_info
*frame
,
1441 int frame_low
, int frame_high
)
1443 struct cleanup
*cleanups
=
1444 make_cleanup (null_cleanup
, NULL
);
1445 PyObject
*module
, *sort_func
, *iterable
, *frame_obj
, *iterator
;
1446 PyObject
*py_frame_low
, *py_frame_high
;
1448 frame_obj
= frame_info_to_frame_object (frame
);
1449 if (frame_obj
== NULL
)
1451 make_cleanup_py_decref (frame_obj
);
1453 module
= PyImport_ImportModule ("gdb.frames");
1456 make_cleanup_py_decref (module
);
1458 sort_func
= PyObject_GetAttrString (module
, "execute_frame_filters");
1459 if (sort_func
== NULL
)
1461 make_cleanup_py_decref (sort_func
);
1463 py_frame_low
= PyInt_FromLong (frame_low
);
1464 if (py_frame_low
== NULL
)
1466 make_cleanup_py_decref (py_frame_low
);
1468 py_frame_high
= PyInt_FromLong (frame_high
);
1469 if (py_frame_high
== NULL
)
1471 make_cleanup_py_decref (py_frame_high
);
1473 iterable
= PyObject_CallFunctionObjArgs (sort_func
, frame_obj
,
1477 if (iterable
== NULL
)
1480 do_cleanups (cleanups
);
1482 if (iterable
!= Py_None
)
1484 iterator
= PyObject_GetIter (iterable
);
1485 Py_DECREF (iterable
);
1495 do_cleanups (cleanups
);
1499 /* This is the only publicly exported function in this file. FRAME
1500 is the source frame to start frame-filter invocation. FLAGS is an
1501 integer holding the flags for printing. The following elements of
1502 the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
1503 PRINT_LEVEL is a flag indicating whether to print the frame's
1504 relative level in the output. PRINT_FRAME_INFO is a flag that
1505 indicates whether this function should print the frame
1506 information, PRINT_ARGS is a flag that indicates whether to print
1507 frame arguments, and PRINT_LOCALS, likewise, with frame local
1508 variables. ARGS_TYPE is an enumerator describing the argument
1509 format, OUT is the output stream to print. FRAME_LOW is the
1510 beginning of the slice of frames to print, and FRAME_HIGH is the
1511 upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error,
1512 or EXT_LANG_BT_COMPLETED on success. */
1514 enum ext_lang_bt_status
1515 gdbpy_apply_frame_filter (const struct extension_language_defn
*extlang
,
1516 struct frame_info
*frame
, int flags
,
1517 enum ext_lang_frame_args args_type
,
1518 struct ui_out
*out
, int frame_low
, int frame_high
)
1520 struct gdbarch
*gdbarch
= NULL
;
1521 struct cleanup
*cleanups
;
1522 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
1525 htab_t levels_printed
;
1527 if (!gdb_python_initialized
)
1528 return EXT_LANG_BT_NO_FILTERS
;
1532 gdbarch
= get_frame_arch (frame
);
1534 CATCH (except
, RETURN_MASK_ALL
)
1536 /* Let gdb try to print the stack trace. */
1537 return EXT_LANG_BT_NO_FILTERS
;
1541 cleanups
= ensure_python_env (gdbarch
, current_language
);
1543 iterable
= bootstrap_python_frame_filters (frame
, frame_low
, frame_high
);
1545 if (iterable
== NULL
)
1547 /* Normally if there is an error GDB prints the exception,
1548 abandons the backtrace and exits. The user can then call "bt
1549 no-filters", and get a default backtrace (it would be
1550 confusing to automatically start a standard backtrace halfway
1551 through a Python filtered backtrace). However in the case
1552 where GDB cannot initialize the frame filters (most likely
1553 due to incorrect auto-load paths), GDB has printed nothing.
1554 In this case it is OK to print the default backtrace after
1555 printing the error message. GDB returns EXT_LANG_BT_NO_FILTERS
1556 here to signify there are no filters after printing the
1557 initialization error. This return code will trigger a
1558 default backtrace. */
1560 gdbpy_print_stack ();
1561 do_cleanups (cleanups
);
1562 return EXT_LANG_BT_NO_FILTERS
;
1565 /* If iterable is None, then there are no frame filters registered.
1566 If this is the case, defer to default GDB printing routines in MI
1568 make_cleanup_py_decref (iterable
);
1569 if (iterable
== Py_None
)
1571 success
= EXT_LANG_BT_NO_FILTERS
;
1575 levels_printed
= htab_create (20,
1579 make_cleanup_htab_delete (levels_printed
);
1581 while ((item
= PyIter_Next (iterable
)))
1583 struct cleanup
*item_cleanup
= make_cleanup_py_decref (item
);
1585 success
= py_print_frame (item
, flags
, args_type
, out
, 0,
1588 do_cleanups (item_cleanup
);
1590 /* Do not exit on error printing a single frame. Print the
1591 error and continue with other frames. */
1592 if (success
== EXT_LANG_BT_ERROR
)
1593 gdbpy_print_stack ();
1596 if (item
== NULL
&& PyErr_Occurred ())
1600 do_cleanups (cleanups
);
1603 /* Exit and abandon backtrace on error, printing the exception that
1606 gdbpy_print_stack ();
1607 do_cleanups (cleanups
);
1608 return EXT_LANG_BT_ERROR
;