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 and SYM_BLOCK is a pass-through argument to
47 write the block where the symbol lies in. In the case of the API
48 returning a string, this will be set to NULL. LANGUAGE is also a
49 pass-through argument denoting the language attributed to the
50 Symbol. In the case of SYM being NULL, this will be set to the
51 current language. Returns EXT_LANG_BT_ERROR on error with the
52 appropriate Python exception set, and EXT_LANG_BT_OK on success. */
54 static enum ext_lang_bt_status
55 extract_sym (PyObject
*obj
, char **name
, struct symbol
**sym
,
56 struct block
**sym_block
, const struct language_defn
**language
)
58 PyObject
*result
= PyObject_CallMethod (obj
, "symbol", NULL
);
61 return EXT_LANG_BT_ERROR
;
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
);
71 return EXT_LANG_BT_ERROR
;
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
;
83 /* This type checks 'result' during the conversion so we
84 just call it unconditionally and check the return. */
85 *sym
= symbol_object_to_symbol (result
);
86 /* TODO: currently, we have no way to recover the block in which SYMBOL
87 was found, so we have no block to return. Trying to evaluate SYMBOL
88 will yield an incorrect value when it's located in a FRAME and
89 evaluated from another frame (as permitted in nested functions). */
96 PyErr_SetString (PyExc_RuntimeError
,
97 _("Unexpected value. Expecting a "
98 "gdb.Symbol or a Python string."));
99 return EXT_LANG_BT_ERROR
;
102 /* Duplicate the symbol name, so the caller has consistency
103 in garbage collection. */
104 *name
= xstrdup (SYMBOL_PRINT_NAME (*sym
));
106 /* If a symbol is specified attempt to determine the language
107 from the symbol. If mode is not "auto", then the language
108 has been explicitly set, use that. */
109 if (language_mode
== language_mode_auto
)
110 *language
= language_def (SYMBOL_LANGUAGE (*sym
));
112 *language
= current_language
;
115 return EXT_LANG_BT_OK
;
118 /* Helper function to extract a value from an object that conforms to
119 the "Symbol Value" interface. OBJ is the Python object to extract
120 the value from. VALUE is a pass-through argument where the value
121 will be written. If the object does not have the value attribute,
122 or provides the Python None for a value, VALUE will be set to NULL
123 and this function will return as successful. Returns EXT_LANG_BT_ERROR
124 on error with the appropriate Python exception set, and EXT_LANG_BT_OK on
127 static enum ext_lang_bt_status
128 extract_value (PyObject
*obj
, struct value
**value
)
130 if (PyObject_HasAttrString (obj
, "value"))
132 PyObject
*vresult
= PyObject_CallMethod (obj
, "value", NULL
);
135 return EXT_LANG_BT_ERROR
;
137 /* The Python code has returned 'None' for a value, so we set
138 value to NULL. This flags that GDB should read the
140 if (vresult
== Py_None
)
144 return EXT_LANG_BT_OK
;
148 *value
= convert_value_from_python (vresult
);
152 return EXT_LANG_BT_ERROR
;
154 return EXT_LANG_BT_OK
;
160 return EXT_LANG_BT_OK
;
163 /* MI prints only certain values according to the type of symbol and
164 also what the user has specified. SYM is the symbol to check, and
165 MI_PRINT_TYPES is an enum specifying what the user wants emitted
166 for the MI command in question. */
168 mi_should_print (struct symbol
*sym
, enum mi_print_types type
)
172 switch (SYMBOL_CLASS (sym
))
175 case LOC_UNDEF
: /* catches errors */
176 case LOC_CONST
: /* constant */
177 case LOC_TYPEDEF
: /* local typedef */
178 case LOC_LABEL
: /* local label */
179 case LOC_BLOCK
: /* local function */
180 case LOC_CONST_BYTES
: /* loc. byte seq. */
181 case LOC_UNRESOLVED
: /* unresolved static */
182 case LOC_OPTIMIZED_OUT
: /* optimized out */
186 case LOC_ARG
: /* argument */
187 case LOC_REF_ARG
: /* reference arg */
188 case LOC_REGPARM_ADDR
: /* indirect register arg */
189 case LOC_LOCAL
: /* stack local */
190 case LOC_STATIC
: /* static */
191 case LOC_REGISTER
: /* register */
192 case LOC_COMPUTED
: /* computed location */
193 if (type
== MI_PRINT_LOCALS
)
194 print_me
= ! SYMBOL_IS_ARGUMENT (sym
);
196 print_me
= SYMBOL_IS_ARGUMENT (sym
);
201 /* Helper function which outputs a type name extracted from VAL to a
202 "type" field in the output stream OUT. OUT is the ui-out structure
203 the type name will be output too, and VAL is the value that the
204 type will be extracted from. Returns EXT_LANG_BT_ERROR on error, with
205 any GDB exceptions converted to a Python exception, or EXT_LANG_BT_OK on
208 static enum ext_lang_bt_status
209 py_print_type (struct ui_out
*out
, struct value
*val
)
216 struct cleanup
*cleanup
;
218 stb
= mem_fileopen ();
219 cleanup
= make_cleanup_ui_file_delete (stb
);
220 type
= check_typedef (value_type (val
));
221 type_print (value_type (val
), "", stb
, -1);
222 ui_out_field_stream (out
, "type", stb
);
223 do_cleanups (cleanup
);
225 CATCH (except
, RETURN_MASK_ALL
)
227 gdbpy_convert_exception (except
);
228 return EXT_LANG_BT_ERROR
;
232 return EXT_LANG_BT_OK
;
235 /* Helper function which outputs a value to an output field in a
236 stream. OUT is the ui-out structure the value will be output to,
237 VAL is the value that will be printed, OPTS contains the value
238 printing options, ARGS_TYPE is an enumerator describing the
239 argument format, and LANGUAGE is the language_defn that the value
240 will be printed with. Returns EXT_LANG_BT_ERROR on error, with any GDB
241 exceptions converted to a Python exception, or EXT_LANG_BT_OK on
244 static enum ext_lang_bt_status
245 py_print_value (struct ui_out
*out
, struct value
*val
,
246 const struct value_print_options
*opts
,
248 enum ext_lang_frame_args args_type
,
249 const struct language_defn
*language
)
251 int should_print
= 0;
252 int local_indent
= (4 * indent
);
254 /* Never set an indent level for common_val_print if MI. */
255 if (ui_out_is_mi_like_p (out
))
258 /* MI does not print certain values, differentiated by type,
259 depending on what ARGS_TYPE indicates. Test type against option.
260 For CLI print all values. */
261 if (args_type
== MI_PRINT_SIMPLE_VALUES
262 || args_type
== MI_PRINT_ALL_VALUES
)
264 struct type
*type
= NULL
;
268 type
= check_typedef (value_type (val
));
270 CATCH (except
, RETURN_MASK_ALL
)
272 gdbpy_convert_exception (except
);
273 return EXT_LANG_BT_ERROR
;
277 if (args_type
== MI_PRINT_ALL_VALUES
)
279 else if (args_type
== MI_PRINT_SIMPLE_VALUES
280 && TYPE_CODE (type
) != TYPE_CODE_ARRAY
281 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
282 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
285 else if (args_type
!= NO_VALUES
)
293 struct cleanup
*cleanup
;
295 stb
= mem_fileopen ();
296 cleanup
= make_cleanup_ui_file_delete (stb
);
297 common_val_print (val
, stb
, indent
, opts
, language
);
298 ui_out_field_stream (out
, "value", stb
);
299 do_cleanups (cleanup
);
301 CATCH (except
, RETURN_MASK_ALL
)
303 gdbpy_convert_exception (except
);
304 return EXT_LANG_BT_ERROR
;
309 return EXT_LANG_BT_OK
;
312 /* Helper function to call a Python method and extract an iterator
313 from the result. If the function returns anything but an iterator
314 the exception is preserved and NULL is returned. FILTER is the
315 Python object to call, and FUNC is the name of the method. Returns
316 a PyObject, or NULL on error with the appropriate exception set.
317 This function can return an iterator, or NULL. */
320 get_py_iter_from_func (PyObject
*filter
, char *func
)
322 if (PyObject_HasAttrString (filter
, func
))
324 PyObject
*result
= PyObject_CallMethod (filter
, func
, NULL
);
328 if (result
== Py_None
)
334 PyObject
*iterator
= PyObject_GetIter (result
);
347 /* Helper function to output a single frame argument and value to an
348 output stream. This function will account for entry values if the
349 FV parameter is populated, the frame argument has entry values
350 associated with them, and the appropriate "set entry-value"
351 options are set. Will output in CLI or MI like format depending
352 on the type of output stream detected. OUT is the output stream,
353 SYM_NAME is the name of the symbol. If SYM_NAME is populated then
354 it must have an accompanying value in the parameter FV. FA is a
355 frame argument structure. If FA is populated, both SYM_NAME and
356 FV are ignored. OPTS contains the value printing options,
357 ARGS_TYPE is an enumerator describing the argument format,
358 PRINT_ARGS_FIELD is a flag which indicates if we output "ARGS=1"
359 in MI output in commands where both arguments and locals are
360 printed. Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions
361 converted to a Python exception, or EXT_LANG_BT_OK on success. */
363 static enum ext_lang_bt_status
364 py_print_single_arg (struct ui_out
*out
,
365 const char *sym_name
,
366 struct frame_arg
*fa
,
368 const struct value_print_options
*opts
,
369 enum ext_lang_frame_args args_type
,
370 int print_args_field
,
371 const struct language_defn
*language
)
374 enum ext_lang_bt_status retval
= EXT_LANG_BT_OK
;
378 if (fa
->val
== NULL
&& fa
->error
== NULL
)
379 return EXT_LANG_BT_OK
;
380 language
= language_def (SYMBOL_LANGUAGE (fa
->sym
));
388 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
390 /* MI has varying rules for tuples, but generally if there is only
391 one element in each item in the list, do not start a tuple. The
392 exception is -stack-list-variables which emits an ARGS="1" field
393 if the value is a frame argument. This is denoted in this
394 function with PRINT_ARGS_FIELD which is flag from the caller to
395 emit the ARGS field. */
396 if (ui_out_is_mi_like_p (out
))
398 if (print_args_field
|| args_type
!= NO_VALUES
)
399 make_cleanup_ui_out_tuple_begin_end (out
, NULL
);
402 annotate_arg_begin ();
404 /* If frame argument is populated, check for entry-values and the
405 entry value options. */
410 stb
= mem_fileopen ();
411 make_cleanup_ui_file_delete (stb
);
412 fprintf_symbol_filtered (stb
, SYMBOL_PRINT_NAME (fa
->sym
),
413 SYMBOL_LANGUAGE (fa
->sym
),
414 DMGL_PARAMS
| DMGL_ANSI
);
415 if (fa
->entry_kind
== print_entry_values_compact
)
417 fputs_filtered ("=", stb
);
419 fprintf_symbol_filtered (stb
, SYMBOL_PRINT_NAME (fa
->sym
),
420 SYMBOL_LANGUAGE (fa
->sym
),
421 DMGL_PARAMS
| DMGL_ANSI
);
423 if (fa
->entry_kind
== print_entry_values_only
424 || fa
->entry_kind
== print_entry_values_compact
)
426 fputs_filtered ("@entry", stb
);
428 ui_out_field_stream (out
, "name", stb
);
431 /* Otherwise, just output the name. */
432 ui_out_field_string (out
, "name", sym_name
);
434 annotate_arg_name_end ();
436 if (! ui_out_is_mi_like_p (out
))
437 ui_out_text (out
, "=");
439 if (print_args_field
)
440 ui_out_field_int (out
, "arg", 1);
442 /* For MI print the type, but only for simple values. This seems
443 weird, but this is how MI choose to format the various output
445 if (args_type
== MI_PRINT_SIMPLE_VALUES
&& val
!= NULL
)
447 if (py_print_type (out
, val
) == EXT_LANG_BT_ERROR
)
449 retval
= EXT_LANG_BT_ERROR
;
450 do_cleanups (cleanups
);
456 annotate_arg_value (value_type (val
));
458 /* If the output is to the CLI, and the user option "set print
459 frame-arguments" is set to none, just output "...". */
460 if (! ui_out_is_mi_like_p (out
) && args_type
== NO_VALUES
)
461 ui_out_field_string (out
, "value", "...");
464 /* Otherwise, print the value for both MI and the CLI, except
465 for the case of MI_PRINT_NO_VALUES. */
466 if (args_type
!= NO_VALUES
)
470 gdb_assert (fa
!= NULL
&& fa
->error
!= NULL
);
471 ui_out_field_fmt (out
, "value",
472 _("<error reading variable: %s>"),
475 else if (py_print_value (out
, val
, opts
, 0, args_type
, language
)
476 == EXT_LANG_BT_ERROR
)
477 retval
= EXT_LANG_BT_ERROR
;
481 do_cleanups (cleanups
);
483 CATCH (except
, RETURN_MASK_ERROR
)
485 gdbpy_convert_exception (except
);
492 /* Helper function to loop over frame arguments provided by the
493 "frame_arguments" Python API. Elements in the iterator must
494 conform to the "Symbol Value" interface. ITER is the Python
495 iterable object, OUT is the output stream, ARGS_TYPE is an
496 enumerator describing the argument format, PRINT_ARGS_FIELD is a
497 flag which indicates if we output "ARGS=1" in MI output in commands
498 where both arguments and locals are printed, and FRAME is the
499 backing frame. Returns EXT_LANG_BT_ERROR on error, with any GDB
500 exceptions converted to a Python exception, or EXT_LANG_BT_OK on
503 static enum ext_lang_bt_status
504 enumerate_args (PyObject
*iter
,
506 enum ext_lang_frame_args args_type
,
507 int print_args_field
,
508 struct frame_info
*frame
)
511 struct value_print_options opts
;
513 get_user_print_options (&opts
);
515 if (args_type
== CLI_SCALAR_VALUES
)
517 /* True in "summary" mode, false otherwise. */
525 annotate_frame_args ();
527 CATCH (except
, RETURN_MASK_ALL
)
529 gdbpy_convert_exception (except
);
534 /* Collect the first argument outside of the loop, so output of
535 commas in the argument output is correct. At the end of the
536 loop block collect another item from the iterator, and, if it is
537 not null emit a comma. */
538 item
= PyIter_Next (iter
);
539 if (item
== NULL
&& PyErr_Occurred ())
544 const struct language_defn
*language
;
547 struct block
*sym_block
;
549 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
551 success
= extract_sym (item
, &sym_name
, &sym
, &sym_block
, &language
);
552 if (success
== EXT_LANG_BT_ERROR
)
558 success
= extract_value (item
, &val
);
559 if (success
== EXT_LANG_BT_ERROR
)
569 if (sym
&& ui_out_is_mi_like_p (out
)
570 && ! mi_should_print (sym
, MI_PRINT_ARGS
))
576 /* If the object did not provide a value, read it using
577 read_frame_args and account for entry values, if any. */
580 struct frame_arg arg
, entryarg
;
582 /* If there is no value, and also no symbol, set error and
586 PyErr_SetString (PyExc_RuntimeError
,
587 _("No symbol or value provided."));
594 read_frame_arg (sym
, frame
, &arg
, &entryarg
);
596 CATCH (except
, RETURN_MASK_ALL
)
599 gdbpy_convert_exception (except
);
604 /* The object has not provided a value, so this is a frame
605 argument to be read by GDB. In this case we have to
606 account for entry-values. */
608 if (arg
.entry_kind
!= print_entry_values_only
)
610 if (py_print_single_arg (out
, NULL
, &arg
,
614 NULL
) == EXT_LANG_BT_ERROR
)
617 xfree (entryarg
.error
);
623 if (entryarg
.entry_kind
!= print_entry_values_no
)
625 if (arg
.entry_kind
!= print_entry_values_only
)
629 ui_out_text (out
, ", ");
630 ui_out_wrap_hint (out
, " ");
632 CATCH (except
, RETURN_MASK_ALL
)
635 xfree (entryarg
.error
);
637 gdbpy_convert_exception (except
);
643 if (py_print_single_arg (out
, NULL
, &entryarg
, NULL
, &opts
,
644 args_type
, print_args_field
, NULL
)
645 == EXT_LANG_BT_ERROR
)
648 xfree (entryarg
.error
);
655 xfree (entryarg
.error
);
659 /* If the object has provided a value, we just print that. */
662 if (py_print_single_arg (out
, sym_name
, NULL
, val
, &opts
,
663 args_type
, print_args_field
,
664 language
) == EXT_LANG_BT_ERROR
)
674 /* Collect the next item from the iterator. If
675 this is the last item, do not print the
677 item
= PyIter_Next (iter
);
682 ui_out_text (out
, ", ");
684 CATCH (except
, RETURN_MASK_ALL
)
687 gdbpy_convert_exception (except
);
692 else if (PyErr_Occurred ())
699 CATCH (except
, RETURN_MASK_ALL
)
702 gdbpy_convert_exception (except
);
708 return EXT_LANG_BT_OK
;
711 return EXT_LANG_BT_ERROR
;
715 /* Helper function to loop over variables provided by the
716 "frame_locals" Python API. Elements in the iterable must conform
717 to the "Symbol Value" interface. ITER is the Python iterable
718 object, OUT is the output stream, INDENT is whether we should
719 indent the output (for CLI), ARGS_TYPE is an enumerator describing
720 the argument format, PRINT_ARGS_FIELD is flag which indicates
721 whether to output the ARGS field in the case of
722 -stack-list-variables and FRAME is the backing frame. Returns
723 EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to a Python
724 exception, or EXT_LANG_BT_OK on success. */
726 static enum ext_lang_bt_status
727 enumerate_locals (PyObject
*iter
,
730 enum ext_lang_frame_args args_type
,
731 int print_args_field
,
732 struct frame_info
*frame
)
735 struct value_print_options opts
;
737 get_user_print_options (&opts
);
740 while ((item
= PyIter_Next (iter
)))
742 const struct language_defn
*language
;
745 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
747 struct block
*sym_block
;
748 int local_indent
= 8 + (8 * indent
);
749 struct cleanup
*locals_cleanups
;
751 locals_cleanups
= make_cleanup_py_decref (item
);
753 success
= extract_sym (item
, &sym_name
, &sym
, &sym_block
, &language
);
754 if (success
== EXT_LANG_BT_ERROR
)
756 do_cleanups (locals_cleanups
);
760 make_cleanup (xfree
, sym_name
);
762 success
= extract_value (item
, &val
);
763 if (success
== EXT_LANG_BT_ERROR
)
765 do_cleanups (locals_cleanups
);
769 if (sym
!= NULL
&& ui_out_is_mi_like_p (out
)
770 && ! mi_should_print (sym
, MI_PRINT_LOCALS
))
772 do_cleanups (locals_cleanups
);
776 /* If the object did not provide a value, read it. */
781 val
= read_var_value (sym
, sym_block
, frame
);
783 CATCH (except
, RETURN_MASK_ERROR
)
785 gdbpy_convert_exception (except
);
786 do_cleanups (locals_cleanups
);
792 /* With PRINT_NO_VALUES, MI does not emit a tuple normally as
793 each output contains only one field. The exception is
794 -stack-list-variables, which always provides a tuple. */
795 if (ui_out_is_mi_like_p (out
))
797 if (print_args_field
|| args_type
!= NO_VALUES
)
798 make_cleanup_ui_out_tuple_begin_end (out
, NULL
);
802 if (! ui_out_is_mi_like_p (out
))
804 /* If the output is not MI we indent locals. */
805 ui_out_spaces (out
, local_indent
);
808 ui_out_field_string (out
, "name", sym_name
);
810 if (! ui_out_is_mi_like_p (out
))
811 ui_out_text (out
, " = ");
813 CATCH (except
, RETURN_MASK_ERROR
)
815 gdbpy_convert_exception (except
);
816 do_cleanups (locals_cleanups
);
821 if (args_type
== MI_PRINT_SIMPLE_VALUES
)
823 if (py_print_type (out
, val
) == EXT_LANG_BT_ERROR
)
825 do_cleanups (locals_cleanups
);
830 /* CLI always prints values for locals. MI uses the
831 simple/no/all system. */
832 if (! ui_out_is_mi_like_p (out
))
834 int val_indent
= (indent
+ 1) * 4;
836 if (py_print_value (out
, val
, &opts
, val_indent
, args_type
,
837 language
) == EXT_LANG_BT_ERROR
)
839 do_cleanups (locals_cleanups
);
845 if (args_type
!= NO_VALUES
)
847 if (py_print_value (out
, val
, &opts
, 0, args_type
,
848 language
) == EXT_LANG_BT_ERROR
)
850 do_cleanups (locals_cleanups
);
856 do_cleanups (locals_cleanups
);
860 ui_out_text (out
, "\n");
862 CATCH (except
, RETURN_MASK_ERROR
)
864 gdbpy_convert_exception (except
);
870 if (item
== NULL
&& PyErr_Occurred ())
873 return EXT_LANG_BT_OK
;
876 return EXT_LANG_BT_ERROR
;
879 /* Helper function for -stack-list-variables. Returns EXT_LANG_BT_ERROR on
880 error, or EXT_LANG_BT_OK on success. */
882 static enum ext_lang_bt_status
883 py_mi_print_variables (PyObject
*filter
, struct ui_out
*out
,
884 struct value_print_options
*opts
,
885 enum ext_lang_frame_args args_type
,
886 struct frame_info
*frame
)
888 struct cleanup
*old_chain
;
890 PyObject
*locals_iter
;
892 args_iter
= get_py_iter_from_func (filter
, "frame_args");
893 old_chain
= make_cleanup_py_xdecref (args_iter
);
894 if (args_iter
== NULL
)
897 locals_iter
= get_py_iter_from_func (filter
, "frame_locals");
898 if (locals_iter
== NULL
)
901 make_cleanup_py_decref (locals_iter
);
902 make_cleanup_ui_out_list_begin_end (out
, "variables");
904 if (args_iter
!= Py_None
)
905 if (enumerate_args (args_iter
, out
, args_type
, 1, frame
)
906 == EXT_LANG_BT_ERROR
)
909 if (locals_iter
!= Py_None
)
910 if (enumerate_locals (locals_iter
, out
, 1, args_type
, 1, frame
)
911 == EXT_LANG_BT_ERROR
)
914 do_cleanups (old_chain
);
915 return EXT_LANG_BT_OK
;
918 do_cleanups (old_chain
);
919 return EXT_LANG_BT_ERROR
;
922 /* Helper function for printing locals. This function largely just
923 creates the wrapping tuple, and calls enumerate_locals. Returns
924 EXT_LANG_BT_ERROR on error, or EXT_LANG_BT_OK on success. */
926 static enum ext_lang_bt_status
927 py_print_locals (PyObject
*filter
,
929 enum ext_lang_frame_args args_type
,
931 struct frame_info
*frame
)
933 PyObject
*locals_iter
= get_py_iter_from_func (filter
,
935 struct cleanup
*old_chain
= make_cleanup_py_xdecref (locals_iter
);
937 if (locals_iter
== NULL
)
940 make_cleanup_ui_out_list_begin_end (out
, "locals");
942 if (locals_iter
!= Py_None
)
943 if (enumerate_locals (locals_iter
, out
, indent
, args_type
,
944 0, frame
) == EXT_LANG_BT_ERROR
)
947 do_cleanups (old_chain
);
948 return EXT_LANG_BT_OK
;
951 do_cleanups (old_chain
);
952 return EXT_LANG_BT_ERROR
;
955 /* Helper function for printing frame arguments. This function
956 largely just creates the wrapping tuple, and calls enumerate_args.
957 Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to
958 a Python exception, or EXT_LANG_BT_OK on success. */
960 static enum ext_lang_bt_status
961 py_print_args (PyObject
*filter
,
963 enum ext_lang_frame_args args_type
,
964 struct frame_info
*frame
)
966 PyObject
*args_iter
= get_py_iter_from_func (filter
, "frame_args");
967 struct cleanup
*old_chain
= make_cleanup_py_xdecref (args_iter
);
969 if (args_iter
== NULL
)
972 make_cleanup_ui_out_list_begin_end (out
, "args");
976 annotate_frame_args ();
977 if (! ui_out_is_mi_like_p (out
))
978 ui_out_text (out
, " (");
980 CATCH (except
, RETURN_MASK_ALL
)
982 gdbpy_convert_exception (except
);
987 if (args_iter
!= Py_None
)
988 if (enumerate_args (args_iter
, out
, args_type
, 0, frame
)
989 == EXT_LANG_BT_ERROR
)
994 if (! ui_out_is_mi_like_p (out
))
995 ui_out_text (out
, ")");
997 CATCH (except
, RETURN_MASK_ALL
)
999 gdbpy_convert_exception (except
);
1004 do_cleanups (old_chain
);
1005 return EXT_LANG_BT_OK
;
1008 do_cleanups (old_chain
);
1009 return EXT_LANG_BT_ERROR
;
1012 /* Print a single frame to the designated output stream, detecting
1013 whether the output is MI or console, and formatting the output
1014 according to the conventions of that protocol. FILTER is the
1015 frame-filter associated with this frame. FLAGS is an integer
1016 describing the various print options. The FLAGS variables is
1017 described in "apply_frame_filter" function. ARGS_TYPE is an
1018 enumerator describing the argument format. OUT is the output
1019 stream to print, INDENT is the level of indention for this frame
1020 (in the case of elided frames), and LEVELS_PRINTED is a hash-table
1021 containing all the frames level that have already been printed.
1022 If a frame level has been printed, do not print it again (in the
1023 case of elided frames). Returns EXT_LANG_BT_ERROR on error, with any
1024 GDB exceptions converted to a Python exception, or EXT_LANG_BT_COMPLETED
1025 on success. It can also throw an exception RETURN_QUIT. */
1027 static enum ext_lang_bt_status
1028 py_print_frame (PyObject
*filter
, int flags
,
1029 enum ext_lang_frame_args args_type
,
1030 struct ui_out
*out
, int indent
, htab_t levels_printed
)
1033 CORE_ADDR address
= 0;
1034 struct gdbarch
*gdbarch
= NULL
;
1035 struct frame_info
*frame
= NULL
;
1036 struct cleanup
*cleanup_stack
;
1037 struct value_print_options opts
;
1038 PyObject
*py_inf_frame
;
1039 int print_level
, print_frame_info
, print_args
, print_locals
;
1041 /* Extract print settings from FLAGS. */
1042 print_level
= (flags
& PRINT_LEVEL
) ? 1 : 0;
1043 print_frame_info
= (flags
& PRINT_FRAME_INFO
) ? 1 : 0;
1044 print_args
= (flags
& PRINT_ARGS
) ? 1 : 0;
1045 print_locals
= (flags
& PRINT_LOCALS
) ? 1 : 0;
1047 get_user_print_options (&opts
);
1049 /* Get the underlying frame. This is needed to determine GDB
1050 architecture, and also, in the cases of frame variables/arguments to
1051 read them if they returned filter object requires us to do so. */
1052 py_inf_frame
= PyObject_CallMethod (filter
, "inferior_frame", NULL
);
1053 if (py_inf_frame
== NULL
)
1054 return EXT_LANG_BT_ERROR
;
1056 frame
= frame_object_to_frame_info (py_inf_frame
);;
1058 Py_DECREF (py_inf_frame
);
1061 return EXT_LANG_BT_ERROR
;
1065 gdbarch
= get_frame_arch (frame
);
1067 CATCH (except
, RETURN_MASK_ERROR
)
1069 gdbpy_convert_exception (except
);
1070 return EXT_LANG_BT_ERROR
;
1074 /* stack-list-variables. */
1075 if (print_locals
&& print_args
&& ! print_frame_info
)
1077 if (py_mi_print_variables (filter
, out
, &opts
,
1078 args_type
, frame
) == EXT_LANG_BT_ERROR
)
1079 return EXT_LANG_BT_ERROR
;
1080 return EXT_LANG_BT_COMPLETED
;
1083 cleanup_stack
= make_cleanup (null_cleanup
, NULL
);
1085 /* -stack-list-locals does not require a
1086 wrapping frame attribute. */
1087 if (print_frame_info
|| (print_args
&& ! print_locals
))
1088 make_cleanup_ui_out_tuple_begin_end (out
, "frame");
1090 if (print_frame_info
)
1092 /* Elided frames are also printed with this function (recursively)
1093 and are printed with indention. */
1098 ui_out_spaces (out
, indent
*4);
1100 CATCH (except
, RETURN_MASK_ERROR
)
1102 gdbpy_convert_exception (except
);
1103 do_cleanups (cleanup_stack
);
1104 return EXT_LANG_BT_ERROR
;
1109 /* The address is required for frame annotations, and also for
1110 address printing. */
1111 if (PyObject_HasAttrString (filter
, "address"))
1113 PyObject
*paddr
= PyObject_CallMethod (filter
, "address", NULL
);
1117 do_cleanups (cleanup_stack
);
1118 return EXT_LANG_BT_ERROR
;
1121 if (paddr
!= Py_None
)
1123 address
= PyLong_AsLong (paddr
);
1130 /* Print frame level. MI does not require the level if
1131 locals/variables only are being printed. */
1132 if ((print_frame_info
|| print_args
) && print_level
)
1134 struct frame_info
**slot
;
1137 slot
= (struct frame_info
**) htab_find_slot (levels_printed
,
1141 level
= frame_relative_level (frame
);
1143 /* Check if this frame has already been printed (there are cases
1144 where elided synthetic dummy-frames have to 'borrow' the frame
1145 architecture from the eliding frame. If that is the case, do
1146 not print 'level', but print spaces. */
1148 ui_out_field_skip (out
, "level");
1152 annotate_frame_begin (print_level
? level
: 0,
1154 ui_out_text (out
, "#");
1155 ui_out_field_fmt_int (out
, 2, ui_left
, "level",
1159 CATCH (except
, RETURN_MASK_ERROR
)
1161 gdbpy_convert_exception (except
);
1162 do_cleanups (cleanup_stack
);
1163 return EXT_LANG_BT_ERROR
;
1168 if (print_frame_info
)
1170 /* Print address to the address field. If an address is not provided,
1172 if (opts
.addressprint
&& has_addr
)
1176 annotate_frame_address ();
1177 ui_out_field_core_addr (out
, "addr", gdbarch
, address
);
1178 annotate_frame_address_end ();
1179 ui_out_text (out
, " in ");
1181 CATCH (except
, RETURN_MASK_ERROR
)
1183 gdbpy_convert_exception (except
);
1184 do_cleanups (cleanup_stack
);
1185 return EXT_LANG_BT_ERROR
;
1190 /* Print frame function name. */
1191 if (PyObject_HasAttrString (filter
, "function"))
1193 PyObject
*py_func
= PyObject_CallMethod (filter
, "function", NULL
);
1194 struct cleanup
*py_func_cleanup
;
1195 const char *function
= NULL
;
1197 if (py_func
== NULL
)
1199 do_cleanups (cleanup_stack
);
1200 return EXT_LANG_BT_ERROR
;
1202 py_func_cleanup
= make_cleanup_py_decref (py_func
);
1204 if (gdbpy_is_string (py_func
))
1206 char *function_to_free
;
1208 function
= function_to_free
=
1209 python_string_to_host_string (py_func
);
1211 if (function
== NULL
)
1213 do_cleanups (cleanup_stack
);
1214 return EXT_LANG_BT_ERROR
;
1216 make_cleanup (xfree
, function_to_free
);
1218 else if (PyLong_Check (py_func
))
1220 CORE_ADDR addr
= PyLong_AsUnsignedLongLong (py_func
);
1221 struct bound_minimal_symbol msymbol
;
1223 if (PyErr_Occurred ())
1225 do_cleanups (cleanup_stack
);
1226 return EXT_LANG_BT_ERROR
;
1229 msymbol
= lookup_minimal_symbol_by_pc (addr
);
1230 if (msymbol
.minsym
!= NULL
)
1231 function
= MSYMBOL_PRINT_NAME (msymbol
.minsym
);
1233 else if (py_func
!= Py_None
)
1235 PyErr_SetString (PyExc_RuntimeError
,
1236 _("FrameDecorator.function: expecting a " \
1237 "String, integer or None."));
1238 do_cleanups (cleanup_stack
);
1239 return EXT_LANG_BT_ERROR
;
1244 annotate_frame_function_name ();
1245 if (function
== NULL
)
1246 ui_out_field_skip (out
, "func");
1248 ui_out_field_string (out
, "func", function
);
1250 CATCH (except
, RETURN_MASK_ERROR
)
1252 gdbpy_convert_exception (except
);
1253 do_cleanups (cleanup_stack
);
1254 return EXT_LANG_BT_ERROR
;
1258 do_cleanups (py_func_cleanup
);
1263 /* Frame arguments. Check the result, and error if something went
1267 if (py_print_args (filter
, out
, args_type
, frame
) == EXT_LANG_BT_ERROR
)
1269 do_cleanups (cleanup_stack
);
1270 return EXT_LANG_BT_ERROR
;
1274 /* File name/source/line number information. */
1275 if (print_frame_info
)
1279 annotate_frame_source_begin ();
1281 CATCH (except
, RETURN_MASK_ERROR
)
1283 gdbpy_convert_exception (except
);
1284 do_cleanups (cleanup_stack
);
1285 return EXT_LANG_BT_ERROR
;
1289 if (PyObject_HasAttrString (filter
, "filename"))
1291 PyObject
*py_fn
= PyObject_CallMethod (filter
, "filename", NULL
);
1292 struct cleanup
*py_fn_cleanup
;
1296 do_cleanups (cleanup_stack
);
1297 return EXT_LANG_BT_ERROR
;
1299 py_fn_cleanup
= make_cleanup_py_decref (py_fn
);
1301 if (py_fn
!= Py_None
)
1303 char *filename
= python_string_to_host_string (py_fn
);
1305 if (filename
== NULL
)
1307 do_cleanups (cleanup_stack
);
1308 return EXT_LANG_BT_ERROR
;
1311 make_cleanup (xfree
, filename
);
1314 ui_out_wrap_hint (out
, " ");
1315 ui_out_text (out
, " at ");
1316 annotate_frame_source_file ();
1317 ui_out_field_string (out
, "file", filename
);
1318 annotate_frame_source_file_end ();
1320 CATCH (except
, RETURN_MASK_ERROR
)
1322 gdbpy_convert_exception (except
);
1323 do_cleanups (cleanup_stack
);
1324 return EXT_LANG_BT_ERROR
;
1328 do_cleanups (py_fn_cleanup
);
1331 if (PyObject_HasAttrString (filter
, "line"))
1333 PyObject
*py_line
= PyObject_CallMethod (filter
, "line", NULL
);
1334 struct cleanup
*py_line_cleanup
;
1337 if (py_line
== NULL
)
1339 do_cleanups (cleanup_stack
);
1340 return EXT_LANG_BT_ERROR
;
1342 py_line_cleanup
= make_cleanup_py_decref (py_line
);
1344 if (py_line
!= Py_None
)
1346 line
= PyLong_AsLong (py_line
);
1349 ui_out_text (out
, ":");
1350 annotate_frame_source_line ();
1351 ui_out_field_int (out
, "line", line
);
1353 CATCH (except
, RETURN_MASK_ERROR
)
1355 gdbpy_convert_exception (except
);
1356 do_cleanups (cleanup_stack
);
1357 return EXT_LANG_BT_ERROR
;
1361 do_cleanups (py_line_cleanup
);
1365 /* For MI we need to deal with the "children" list population of
1366 elided frames, so if MI output detected do not send newline. */
1367 if (! ui_out_is_mi_like_p (out
))
1371 annotate_frame_end ();
1372 ui_out_text (out
, "\n");
1374 CATCH (except
, RETURN_MASK_ERROR
)
1376 gdbpy_convert_exception (except
);
1377 do_cleanups (cleanup_stack
);
1378 return EXT_LANG_BT_ERROR
;
1385 if (py_print_locals (filter
, out
, args_type
, indent
,
1386 frame
) == EXT_LANG_BT_ERROR
)
1388 do_cleanups (cleanup_stack
);
1389 return EXT_LANG_BT_ERROR
;
1395 struct cleanup
*elided_cleanup
;
1397 /* Finally recursively print elided frames, if any. */
1398 elided
= get_py_iter_from_func (filter
, "elided");
1401 do_cleanups (cleanup_stack
);
1402 return EXT_LANG_BT_ERROR
;
1404 elided_cleanup
= make_cleanup_py_decref (elided
);
1406 if (elided
!= Py_None
)
1410 make_cleanup_ui_out_list_begin_end (out
, "children");
1412 if (! ui_out_is_mi_like_p (out
))
1415 while ((item
= PyIter_Next (elided
)))
1417 struct cleanup
*item_cleanup
= make_cleanup_py_decref (item
);
1419 enum ext_lang_bt_status success
= py_print_frame (item
, flags
,
1424 do_cleanups (item_cleanup
);
1426 if (success
== EXT_LANG_BT_ERROR
)
1428 do_cleanups (cleanup_stack
);
1429 return EXT_LANG_BT_ERROR
;
1432 if (item
== NULL
&& PyErr_Occurred ())
1434 do_cleanups (cleanup_stack
);
1435 return EXT_LANG_BT_ERROR
;
1438 do_cleanups (elided_cleanup
);
1441 do_cleanups (cleanup_stack
);
1442 return EXT_LANG_BT_COMPLETED
;
1445 /* Helper function to initiate frame filter invocation at starting
1449 bootstrap_python_frame_filters (struct frame_info
*frame
,
1450 int frame_low
, int frame_high
)
1452 struct cleanup
*cleanups
=
1453 make_cleanup (null_cleanup
, NULL
);
1454 PyObject
*module
, *sort_func
, *iterable
, *frame_obj
, *iterator
;
1455 PyObject
*py_frame_low
, *py_frame_high
;
1457 frame_obj
= frame_info_to_frame_object (frame
);
1458 if (frame_obj
== NULL
)
1460 make_cleanup_py_decref (frame_obj
);
1462 module
= PyImport_ImportModule ("gdb.frames");
1465 make_cleanup_py_decref (module
);
1467 sort_func
= PyObject_GetAttrString (module
, "execute_frame_filters");
1468 if (sort_func
== NULL
)
1470 make_cleanup_py_decref (sort_func
);
1472 py_frame_low
= PyInt_FromLong (frame_low
);
1473 if (py_frame_low
== NULL
)
1475 make_cleanup_py_decref (py_frame_low
);
1477 py_frame_high
= PyInt_FromLong (frame_high
);
1478 if (py_frame_high
== NULL
)
1480 make_cleanup_py_decref (py_frame_high
);
1482 iterable
= PyObject_CallFunctionObjArgs (sort_func
, frame_obj
,
1486 if (iterable
== NULL
)
1489 do_cleanups (cleanups
);
1491 if (iterable
!= Py_None
)
1493 iterator
= PyObject_GetIter (iterable
);
1494 Py_DECREF (iterable
);
1504 do_cleanups (cleanups
);
1508 /* This is the only publicly exported function in this file. FRAME
1509 is the source frame to start frame-filter invocation. FLAGS is an
1510 integer holding the flags for printing. The following elements of
1511 the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
1512 PRINT_LEVEL is a flag indicating whether to print the frame's
1513 relative level in the output. PRINT_FRAME_INFO is a flag that
1514 indicates whether this function should print the frame
1515 information, PRINT_ARGS is a flag that indicates whether to print
1516 frame arguments, and PRINT_LOCALS, likewise, with frame local
1517 variables. ARGS_TYPE is an enumerator describing the argument
1518 format, OUT is the output stream to print. FRAME_LOW is the
1519 beginning of the slice of frames to print, and FRAME_HIGH is the
1520 upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error,
1521 or EXT_LANG_BT_COMPLETED on success. */
1523 enum ext_lang_bt_status
1524 gdbpy_apply_frame_filter (const struct extension_language_defn
*extlang
,
1525 struct frame_info
*frame
, int flags
,
1526 enum ext_lang_frame_args args_type
,
1527 struct ui_out
*out
, int frame_low
, int frame_high
)
1529 struct gdbarch
*gdbarch
= NULL
;
1530 struct cleanup
*cleanups
;
1531 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
1534 htab_t levels_printed
;
1536 if (!gdb_python_initialized
)
1537 return EXT_LANG_BT_NO_FILTERS
;
1541 gdbarch
= get_frame_arch (frame
);
1543 CATCH (except
, RETURN_MASK_ALL
)
1545 /* Let gdb try to print the stack trace. */
1546 return EXT_LANG_BT_NO_FILTERS
;
1550 cleanups
= ensure_python_env (gdbarch
, current_language
);
1552 iterable
= bootstrap_python_frame_filters (frame
, frame_low
, frame_high
);
1554 if (iterable
== NULL
)
1556 /* Normally if there is an error GDB prints the exception,
1557 abandons the backtrace and exits. The user can then call "bt
1558 no-filters", and get a default backtrace (it would be
1559 confusing to automatically start a standard backtrace halfway
1560 through a Python filtered backtrace). However in the case
1561 where GDB cannot initialize the frame filters (most likely
1562 due to incorrect auto-load paths), GDB has printed nothing.
1563 In this case it is OK to print the default backtrace after
1564 printing the error message. GDB returns EXT_LANG_BT_NO_FILTERS
1565 here to signify there are no filters after printing the
1566 initialization error. This return code will trigger a
1567 default backtrace. */
1569 gdbpy_print_stack ();
1570 do_cleanups (cleanups
);
1571 return EXT_LANG_BT_NO_FILTERS
;
1574 /* If iterable is None, then there are no frame filters registered.
1575 If this is the case, defer to default GDB printing routines in MI
1577 make_cleanup_py_decref (iterable
);
1578 if (iterable
== Py_None
)
1580 success
= EXT_LANG_BT_NO_FILTERS
;
1584 levels_printed
= htab_create (20,
1588 make_cleanup_htab_delete (levels_printed
);
1590 while ((item
= PyIter_Next (iterable
)))
1592 struct cleanup
*item_cleanup
= make_cleanup_py_decref (item
);
1594 success
= py_print_frame (item
, flags
, args_type
, out
, 0,
1597 do_cleanups (item_cleanup
);
1599 /* Do not exit on error printing a single frame. Print the
1600 error and continue with other frames. */
1601 if (success
== EXT_LANG_BT_ERROR
)
1602 gdbpy_print_stack ();
1605 if (item
== NULL
&& PyErr_Occurred ())
1609 do_cleanups (cleanups
);
1612 /* Exit and abandon backtrace on error, printing the exception that
1615 gdbpy_print_stack ();
1616 do_cleanups (cleanups
);
1617 return EXT_LANG_BT_ERROR
;