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
)
204 volatile struct gdb_exception except
;
206 TRY_CATCH (except
, RETURN_MASK_ALL
)
210 struct cleanup
*cleanup
;
212 stb
= mem_fileopen ();
213 cleanup
= make_cleanup_ui_file_delete (stb
);
214 type
= check_typedef (value_type (val
));
215 type_print (value_type (val
), "", stb
, -1);
216 ui_out_field_stream (out
, "type", stb
);
217 do_cleanups (cleanup
);
219 if (except
.reason
< 0)
221 gdbpy_convert_exception (except
);
222 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 volatile struct gdb_exception except
;
246 int local_indent
= (4 * indent
);
248 /* Never set an indent level for common_val_print if MI. */
249 if (ui_out_is_mi_like_p (out
))
252 /* MI does not print certain values, differentiated by type,
253 depending on what ARGS_TYPE indicates. Test type against option.
254 For CLI print all values. */
255 if (args_type
== MI_PRINT_SIMPLE_VALUES
256 || args_type
== MI_PRINT_ALL_VALUES
)
258 struct type
*type
= NULL
;
260 TRY_CATCH (except
, RETURN_MASK_ALL
)
262 type
= check_typedef (value_type (val
));
264 if (except
.reason
< 0)
266 gdbpy_convert_exception (except
);
267 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
)
283 TRY_CATCH (except
, RETURN_MASK_ALL
)
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 if (except
.reason
< 0)
296 gdbpy_convert_exception (except
);
297 return EXT_LANG_BT_ERROR
;
301 return EXT_LANG_BT_OK
;
304 /* Helper function to call a Python method and extract an iterator
305 from the result. If the function returns anything but an iterator
306 the exception is preserved and NULL is returned. FILTER is the
307 Python object to call, and FUNC is the name of the method. Returns
308 a PyObject, or NULL on error with the appropriate exception set.
309 This function can return an iterator, or NULL. */
312 get_py_iter_from_func (PyObject
*filter
, char *func
)
314 if (PyObject_HasAttrString (filter
, func
))
316 PyObject
*result
= PyObject_CallMethod (filter
, func
, NULL
);
320 if (result
== Py_None
)
326 PyObject
*iterator
= PyObject_GetIter (result
);
339 /* Helper function to output a single frame argument and value to an
340 output stream. This function will account for entry values if the
341 FV parameter is populated, the frame argument has entry values
342 associated with them, and the appropriate "set entry-value"
343 options are set. Will output in CLI or MI like format depending
344 on the type of output stream detected. OUT is the output stream,
345 SYM_NAME is the name of the symbol. If SYM_NAME is populated then
346 it must have an accompanying value in the parameter FV. FA is a
347 frame argument structure. If FA is populated, both SYM_NAME and
348 FV are ignored. OPTS contains the value printing options,
349 ARGS_TYPE is an enumerator describing the argument format,
350 PRINT_ARGS_FIELD is a flag which indicates if we output "ARGS=1"
351 in MI output in commands where both arguments and locals are
352 printed. Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions
353 converted to a Python exception, or EXT_LANG_BT_OK on success. */
355 static enum ext_lang_bt_status
356 py_print_single_arg (struct ui_out
*out
,
357 const char *sym_name
,
358 struct frame_arg
*fa
,
360 const struct value_print_options
*opts
,
361 enum ext_lang_frame_args args_type
,
362 int print_args_field
,
363 const struct language_defn
*language
)
366 volatile struct gdb_exception except
;
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
));
379 TRY_CATCH (except
, RETURN_MASK_ALL
)
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 if (except
.reason
< 0)
477 gdbpy_convert_exception (except
);
482 /* Helper function to loop over frame arguments provided by the
483 "frame_arguments" Python API. Elements in the iterator must
484 conform to the "Symbol Value" interface. ITER is the Python
485 iterable object, OUT is the output stream, ARGS_TYPE is an
486 enumerator describing the argument format, PRINT_ARGS_FIELD is a
487 flag which indicates if we output "ARGS=1" in MI output in commands
488 where both arguments and locals are printed, and FRAME is the
489 backing frame. Returns EXT_LANG_BT_ERROR on error, with any GDB
490 exceptions converted to a Python exception, or EXT_LANG_BT_OK on
493 static enum ext_lang_bt_status
494 enumerate_args (PyObject
*iter
,
496 enum ext_lang_frame_args args_type
,
497 int print_args_field
,
498 struct frame_info
*frame
)
501 struct value_print_options opts
;
502 volatile struct gdb_exception except
;
504 get_user_print_options (&opts
);
506 if (args_type
== CLI_SCALAR_VALUES
)
508 /* True in "summary" mode, false otherwise. */
514 TRY_CATCH (except
, RETURN_MASK_ALL
)
516 annotate_frame_args ();
518 if (except
.reason
< 0)
520 gdbpy_convert_exception (except
);
524 /* Collect the first argument outside of the loop, so output of
525 commas in the argument output is correct. At the end of the
526 loop block collect another item from the iterator, and, if it is
527 not null emit a comma. */
528 item
= PyIter_Next (iter
);
529 if (item
== NULL
&& PyErr_Occurred ())
534 const struct language_defn
*language
;
538 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
540 success
= extract_sym (item
, &sym_name
, &sym
, &language
);
541 if (success
== EXT_LANG_BT_ERROR
)
547 success
= extract_value (item
, &val
);
548 if (success
== EXT_LANG_BT_ERROR
)
558 if (sym
&& ui_out_is_mi_like_p (out
)
559 && ! mi_should_print (sym
, MI_PRINT_ARGS
))
565 /* If the object did not provide a value, read it using
566 read_frame_args and account for entry values, if any. */
569 struct frame_arg arg
, entryarg
;
571 /* If there is no value, and also no symbol, set error and
575 PyErr_SetString (PyExc_RuntimeError
,
576 _("No symbol or value provided."));
581 TRY_CATCH (except
, RETURN_MASK_ALL
)
583 read_frame_arg (sym
, frame
, &arg
, &entryarg
);
585 if (except
.reason
< 0)
588 gdbpy_convert_exception (except
);
592 /* The object has not provided a value, so this is a frame
593 argument to be read by GDB. In this case we have to
594 account for entry-values. */
596 if (arg
.entry_kind
!= print_entry_values_only
)
598 if (py_print_single_arg (out
, NULL
, &arg
,
602 NULL
) == EXT_LANG_BT_ERROR
)
605 xfree (entryarg
.error
);
611 if (entryarg
.entry_kind
!= print_entry_values_no
)
613 if (arg
.entry_kind
!= print_entry_values_only
)
615 TRY_CATCH (except
, RETURN_MASK_ALL
)
617 ui_out_text (out
, ", ");
618 ui_out_wrap_hint (out
, " ");
620 if (except
.reason
< 0)
623 xfree (entryarg
.error
);
625 gdbpy_convert_exception (except
);
630 if (py_print_single_arg (out
, NULL
, &entryarg
, NULL
, &opts
,
631 args_type
, print_args_field
, NULL
)
632 == EXT_LANG_BT_ERROR
)
635 xfree (entryarg
.error
);
642 xfree (entryarg
.error
);
646 /* If the object has provided a value, we just print that. */
649 if (py_print_single_arg (out
, sym_name
, NULL
, val
, &opts
,
650 args_type
, print_args_field
,
651 language
) == EXT_LANG_BT_ERROR
)
661 /* Collect the next item from the iterator. If
662 this is the last item, do not print the
664 item
= PyIter_Next (iter
);
667 TRY_CATCH (except
, RETURN_MASK_ALL
)
669 ui_out_text (out
, ", ");
671 if (except
.reason
< 0)
674 gdbpy_convert_exception (except
);
678 else if (PyErr_Occurred ())
681 TRY_CATCH (except
, RETURN_MASK_ALL
)
685 if (except
.reason
< 0)
688 gdbpy_convert_exception (except
);
693 return EXT_LANG_BT_OK
;
696 return EXT_LANG_BT_ERROR
;
700 /* Helper function to loop over variables provided by the
701 "frame_locals" Python API. Elements in the iterable must conform
702 to the "Symbol Value" interface. ITER is the Python iterable
703 object, OUT is the output stream, INDENT is whether we should
704 indent the output (for CLI), ARGS_TYPE is an enumerator describing
705 the argument format, PRINT_ARGS_FIELD is flag which indicates
706 whether to output the ARGS field in the case of
707 -stack-list-variables and FRAME is the backing frame. Returns
708 EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to a Python
709 exception, or EXT_LANG_BT_OK on success. */
711 static enum ext_lang_bt_status
712 enumerate_locals (PyObject
*iter
,
715 enum ext_lang_frame_args args_type
,
716 int print_args_field
,
717 struct frame_info
*frame
)
720 struct value_print_options opts
;
722 get_user_print_options (&opts
);
725 while ((item
= PyIter_Next (iter
)))
727 const struct language_defn
*language
;
730 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
732 volatile struct gdb_exception except
;
733 int local_indent
= 8 + (8 * indent
);
734 struct cleanup
*locals_cleanups
;
736 locals_cleanups
= make_cleanup_py_decref (item
);
738 success
= extract_sym (item
, &sym_name
, &sym
, &language
);
739 if (success
== EXT_LANG_BT_ERROR
)
741 do_cleanups (locals_cleanups
);
745 make_cleanup (xfree
, sym_name
);
747 success
= extract_value (item
, &val
);
748 if (success
== EXT_LANG_BT_ERROR
)
750 do_cleanups (locals_cleanups
);
754 if (sym
!= NULL
&& ui_out_is_mi_like_p (out
)
755 && ! mi_should_print (sym
, MI_PRINT_LOCALS
))
757 do_cleanups (locals_cleanups
);
761 /* If the object did not provide a value, read it. */
764 TRY_CATCH (except
, RETURN_MASK_ALL
)
766 val
= read_var_value (sym
, frame
);
768 if (except
.reason
< 0)
770 gdbpy_convert_exception (except
);
771 do_cleanups (locals_cleanups
);
776 /* With PRINT_NO_VALUES, MI does not emit a tuple normally as
777 each output contains only one field. The exception is
778 -stack-list-variables, which always provides a tuple. */
779 if (ui_out_is_mi_like_p (out
))
781 if (print_args_field
|| args_type
!= NO_VALUES
)
782 make_cleanup_ui_out_tuple_begin_end (out
, NULL
);
784 TRY_CATCH (except
, RETURN_MASK_ALL
)
786 if (! ui_out_is_mi_like_p (out
))
788 /* If the output is not MI we indent locals. */
789 ui_out_spaces (out
, local_indent
);
792 ui_out_field_string (out
, "name", sym_name
);
794 if (! ui_out_is_mi_like_p (out
))
795 ui_out_text (out
, " = ");
797 if (except
.reason
< 0)
799 gdbpy_convert_exception (except
);
800 do_cleanups (locals_cleanups
);
804 if (args_type
== MI_PRINT_SIMPLE_VALUES
)
806 if (py_print_type (out
, val
) == EXT_LANG_BT_ERROR
)
808 do_cleanups (locals_cleanups
);
813 /* CLI always prints values for locals. MI uses the
814 simple/no/all system. */
815 if (! ui_out_is_mi_like_p (out
))
817 int val_indent
= (indent
+ 1) * 4;
819 if (py_print_value (out
, val
, &opts
, val_indent
, args_type
,
820 language
) == EXT_LANG_BT_ERROR
)
822 do_cleanups (locals_cleanups
);
828 if (args_type
!= NO_VALUES
)
830 if (py_print_value (out
, val
, &opts
, 0, args_type
,
831 language
) == EXT_LANG_BT_ERROR
)
833 do_cleanups (locals_cleanups
);
839 do_cleanups (locals_cleanups
);
841 TRY_CATCH (except
, RETURN_MASK_ALL
)
843 ui_out_text (out
, "\n");
845 if (except
.reason
< 0)
847 gdbpy_convert_exception (except
);
852 if (item
== NULL
&& PyErr_Occurred ())
855 return EXT_LANG_BT_OK
;
858 return EXT_LANG_BT_ERROR
;
861 /* Helper function for -stack-list-variables. Returns EXT_LANG_BT_ERROR on
862 error, or EXT_LANG_BT_OK on success. */
864 static enum ext_lang_bt_status
865 py_mi_print_variables (PyObject
*filter
, struct ui_out
*out
,
866 struct value_print_options
*opts
,
867 enum ext_lang_frame_args args_type
,
868 struct frame_info
*frame
)
870 struct cleanup
*old_chain
;
872 PyObject
*locals_iter
;
874 args_iter
= get_py_iter_from_func (filter
, "frame_args");
875 old_chain
= make_cleanup_py_xdecref (args_iter
);
876 if (args_iter
== NULL
)
879 locals_iter
= get_py_iter_from_func (filter
, "frame_locals");
880 if (locals_iter
== NULL
)
883 make_cleanup_py_decref (locals_iter
);
884 make_cleanup_ui_out_list_begin_end (out
, "variables");
886 if (args_iter
!= Py_None
)
887 if (enumerate_args (args_iter
, out
, args_type
, 1, frame
)
888 == EXT_LANG_BT_ERROR
)
891 if (locals_iter
!= Py_None
)
892 if (enumerate_locals (locals_iter
, out
, 1, args_type
, 1, frame
)
893 == EXT_LANG_BT_ERROR
)
896 do_cleanups (old_chain
);
897 return EXT_LANG_BT_OK
;
900 do_cleanups (old_chain
);
901 return EXT_LANG_BT_ERROR
;
904 /* Helper function for printing locals. This function largely just
905 creates the wrapping tuple, and calls enumerate_locals. Returns
906 EXT_LANG_BT_ERROR on error, or EXT_LANG_BT_OK on success. */
908 static enum ext_lang_bt_status
909 py_print_locals (PyObject
*filter
,
911 enum ext_lang_frame_args args_type
,
913 struct frame_info
*frame
)
915 PyObject
*locals_iter
= get_py_iter_from_func (filter
,
917 struct cleanup
*old_chain
= make_cleanup_py_xdecref (locals_iter
);
919 if (locals_iter
== NULL
)
922 make_cleanup_ui_out_list_begin_end (out
, "locals");
924 if (locals_iter
!= Py_None
)
925 if (enumerate_locals (locals_iter
, out
, indent
, args_type
,
926 0, frame
) == EXT_LANG_BT_ERROR
)
929 do_cleanups (old_chain
);
930 return EXT_LANG_BT_OK
;
933 do_cleanups (old_chain
);
934 return EXT_LANG_BT_ERROR
;
937 /* Helper function for printing frame arguments. This function
938 largely just creates the wrapping tuple, and calls enumerate_args.
939 Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to
940 a Python exception, or EXT_LANG_BT_OK on success. */
942 static enum ext_lang_bt_status
943 py_print_args (PyObject
*filter
,
945 enum ext_lang_frame_args args_type
,
946 struct frame_info
*frame
)
948 PyObject
*args_iter
= get_py_iter_from_func (filter
, "frame_args");
949 struct cleanup
*old_chain
= make_cleanup_py_xdecref (args_iter
);
950 volatile struct gdb_exception except
;
952 if (args_iter
== NULL
)
955 make_cleanup_ui_out_list_begin_end (out
, "args");
957 TRY_CATCH (except
, RETURN_MASK_ALL
)
959 annotate_frame_args ();
960 if (! ui_out_is_mi_like_p (out
))
961 ui_out_text (out
, " (");
963 if (except
.reason
< 0)
965 gdbpy_convert_exception (except
);
969 if (args_iter
!= Py_None
)
970 if (enumerate_args (args_iter
, out
, args_type
, 0, frame
)
971 == EXT_LANG_BT_ERROR
)
974 TRY_CATCH (except
, RETURN_MASK_ALL
)
976 if (! ui_out_is_mi_like_p (out
))
977 ui_out_text (out
, ")");
979 if (except
.reason
< 0)
981 gdbpy_convert_exception (except
);
985 do_cleanups (old_chain
);
986 return EXT_LANG_BT_OK
;
989 do_cleanups (old_chain
);
990 return EXT_LANG_BT_ERROR
;
993 /* Print a single frame to the designated output stream, detecting
994 whether the output is MI or console, and formatting the output
995 according to the conventions of that protocol. FILTER is the
996 frame-filter associated with this frame. FLAGS is an integer
997 describing the various print options. The FLAGS variables is
998 described in "apply_frame_filter" function. ARGS_TYPE is an
999 enumerator describing the argument format. OUT is the output
1000 stream to print, INDENT is the level of indention for this frame
1001 (in the case of elided frames), and LEVELS_PRINTED is a hash-table
1002 containing all the frames level that have already been printed.
1003 If a frame level has been printed, do not print it again (in the
1004 case of elided frames). Returns EXT_LANG_BT_ERROR on error, with any
1005 GDB exceptions converted to a Python exception, or EXT_LANG_BT_COMPLETED
1006 on success. It can also throw an exception RETURN_QUIT. */
1008 static enum ext_lang_bt_status
1009 py_print_frame (PyObject
*filter
, int flags
,
1010 enum ext_lang_frame_args args_type
,
1011 struct ui_out
*out
, int indent
, htab_t levels_printed
)
1014 CORE_ADDR address
= 0;
1015 struct gdbarch
*gdbarch
= NULL
;
1016 struct frame_info
*frame
= NULL
;
1017 struct cleanup
*cleanup_stack
;
1018 struct value_print_options opts
;
1019 PyObject
*py_inf_frame
;
1020 int print_level
, print_frame_info
, print_args
, print_locals
;
1021 volatile struct gdb_exception except
;
1023 /* Extract print settings from FLAGS. */
1024 print_level
= (flags
& PRINT_LEVEL
) ? 1 : 0;
1025 print_frame_info
= (flags
& PRINT_FRAME_INFO
) ? 1 : 0;
1026 print_args
= (flags
& PRINT_ARGS
) ? 1 : 0;
1027 print_locals
= (flags
& PRINT_LOCALS
) ? 1 : 0;
1029 get_user_print_options (&opts
);
1031 /* Get the underlying frame. This is needed to determine GDB
1032 architecture, and also, in the cases of frame variables/arguments to
1033 read them if they returned filter object requires us to do so. */
1034 py_inf_frame
= PyObject_CallMethod (filter
, "inferior_frame", NULL
);
1035 if (py_inf_frame
== NULL
)
1036 return EXT_LANG_BT_ERROR
;
1038 frame
= frame_object_to_frame_info (py_inf_frame
);;
1040 Py_DECREF (py_inf_frame
);
1043 return EXT_LANG_BT_ERROR
;
1045 TRY_CATCH (except
, RETURN_MASK_ALL
)
1047 gdbarch
= get_frame_arch (frame
);
1049 if (except
.reason
< 0)
1051 gdbpy_convert_exception (except
);
1052 return EXT_LANG_BT_ERROR
;
1055 /* stack-list-variables. */
1056 if (print_locals
&& print_args
&& ! print_frame_info
)
1058 if (py_mi_print_variables (filter
, out
, &opts
,
1059 args_type
, frame
) == EXT_LANG_BT_ERROR
)
1060 return EXT_LANG_BT_ERROR
;
1061 return EXT_LANG_BT_COMPLETED
;
1064 cleanup_stack
= make_cleanup (null_cleanup
, NULL
);
1066 /* -stack-list-locals does not require a
1067 wrapping frame attribute. */
1068 if (print_frame_info
|| (print_args
&& ! print_locals
))
1069 make_cleanup_ui_out_tuple_begin_end (out
, "frame");
1071 if (print_frame_info
)
1073 /* Elided frames are also printed with this function (recursively)
1074 and are printed with indention. */
1077 TRY_CATCH (except
, RETURN_MASK_ALL
)
1079 ui_out_spaces (out
, indent
*4);
1081 if (except
.reason
< 0)
1083 gdbpy_convert_exception (except
);
1084 do_cleanups (cleanup_stack
);
1085 return EXT_LANG_BT_ERROR
;
1089 /* The address is required for frame annotations, and also for
1090 address printing. */
1091 if (PyObject_HasAttrString (filter
, "address"))
1093 PyObject
*paddr
= PyObject_CallMethod (filter
, "address", NULL
);
1097 do_cleanups (cleanup_stack
);
1098 return EXT_LANG_BT_ERROR
;
1101 if (paddr
!= Py_None
)
1103 address
= PyLong_AsLong (paddr
);
1110 /* Print frame level. MI does not require the level if
1111 locals/variables only are being printed. */
1112 if ((print_frame_info
|| print_args
) && print_level
)
1114 struct frame_info
**slot
;
1116 volatile struct gdb_exception except
;
1118 slot
= (struct frame_info
**) htab_find_slot (levels_printed
,
1120 TRY_CATCH (except
, RETURN_MASK_ALL
)
1122 level
= frame_relative_level (frame
);
1124 /* Check if this frame has already been printed (there are cases
1125 where elided synthetic dummy-frames have to 'borrow' the frame
1126 architecture from the eliding frame. If that is the case, do
1127 not print 'level', but print spaces. */
1129 ui_out_field_skip (out
, "level");
1133 annotate_frame_begin (print_level
? level
: 0,
1135 ui_out_text (out
, "#");
1136 ui_out_field_fmt_int (out
, 2, ui_left
, "level",
1140 if (except
.reason
< 0)
1142 gdbpy_convert_exception (except
);
1143 do_cleanups (cleanup_stack
);
1144 return EXT_LANG_BT_ERROR
;
1148 if (print_frame_info
)
1150 /* Print address to the address field. If an address is not provided,
1152 if (opts
.addressprint
&& has_addr
)
1154 TRY_CATCH (except
, RETURN_MASK_ALL
)
1156 annotate_frame_address ();
1157 ui_out_field_core_addr (out
, "addr", gdbarch
, address
);
1158 annotate_frame_address_end ();
1159 ui_out_text (out
, " in ");
1161 if (except
.reason
< 0)
1163 gdbpy_convert_exception (except
);
1164 do_cleanups (cleanup_stack
);
1165 return EXT_LANG_BT_ERROR
;
1169 /* Print frame function name. */
1170 if (PyObject_HasAttrString (filter
, "function"))
1172 PyObject
*py_func
= PyObject_CallMethod (filter
, "function", NULL
);
1173 struct cleanup
*py_func_cleanup
;
1174 const char *function
= NULL
;
1176 if (py_func
== NULL
)
1178 do_cleanups (cleanup_stack
);
1179 return EXT_LANG_BT_ERROR
;
1181 py_func_cleanup
= make_cleanup_py_decref (py_func
);
1183 if (gdbpy_is_string (py_func
))
1185 char *function_to_free
;
1187 function
= function_to_free
=
1188 python_string_to_host_string (py_func
);
1190 if (function
== NULL
)
1192 do_cleanups (cleanup_stack
);
1193 return EXT_LANG_BT_ERROR
;
1195 make_cleanup (xfree
, function_to_free
);
1197 else if (PyLong_Check (py_func
))
1199 CORE_ADDR addr
= PyLong_AsUnsignedLongLong (py_func
);
1200 struct bound_minimal_symbol msymbol
;
1202 if (PyErr_Occurred ())
1204 do_cleanups (cleanup_stack
);
1205 return EXT_LANG_BT_ERROR
;
1208 msymbol
= lookup_minimal_symbol_by_pc (addr
);
1209 if (msymbol
.minsym
!= NULL
)
1210 function
= MSYMBOL_PRINT_NAME (msymbol
.minsym
);
1212 else if (py_func
!= Py_None
)
1214 PyErr_SetString (PyExc_RuntimeError
,
1215 _("FrameDecorator.function: expecting a " \
1216 "String, integer or None."));
1217 do_cleanups (cleanup_stack
);
1218 return EXT_LANG_BT_ERROR
;
1221 TRY_CATCH (except
, RETURN_MASK_ALL
)
1223 annotate_frame_function_name ();
1224 if (function
== NULL
)
1225 ui_out_field_skip (out
, "func");
1227 ui_out_field_string (out
, "func", function
);
1229 if (except
.reason
< 0)
1231 gdbpy_convert_exception (except
);
1232 do_cleanups (cleanup_stack
);
1233 return EXT_LANG_BT_ERROR
;
1235 do_cleanups (py_func_cleanup
);
1240 /* Frame arguments. Check the result, and error if something went
1244 if (py_print_args (filter
, out
, args_type
, frame
) == EXT_LANG_BT_ERROR
)
1246 do_cleanups (cleanup_stack
);
1247 return EXT_LANG_BT_ERROR
;
1251 /* File name/source/line number information. */
1252 if (print_frame_info
)
1254 TRY_CATCH (except
, RETURN_MASK_ALL
)
1256 annotate_frame_source_begin ();
1258 if (except
.reason
< 0)
1260 gdbpy_convert_exception (except
);
1261 do_cleanups (cleanup_stack
);
1262 return EXT_LANG_BT_ERROR
;
1265 if (PyObject_HasAttrString (filter
, "filename"))
1267 PyObject
*py_fn
= PyObject_CallMethod (filter
, "filename", NULL
);
1268 struct cleanup
*py_fn_cleanup
;
1272 do_cleanups (cleanup_stack
);
1273 return EXT_LANG_BT_ERROR
;
1275 py_fn_cleanup
= make_cleanup_py_decref (py_fn
);
1277 if (py_fn
!= Py_None
)
1279 char *filename
= python_string_to_host_string (py_fn
);
1281 if (filename
== NULL
)
1283 do_cleanups (cleanup_stack
);
1284 return EXT_LANG_BT_ERROR
;
1287 make_cleanup (xfree
, filename
);
1288 TRY_CATCH (except
, RETURN_MASK_ALL
)
1290 ui_out_wrap_hint (out
, " ");
1291 ui_out_text (out
, " at ");
1292 annotate_frame_source_file ();
1293 ui_out_field_string (out
, "file", filename
);
1294 annotate_frame_source_file_end ();
1296 if (except
.reason
< 0)
1298 gdbpy_convert_exception (except
);
1299 do_cleanups (cleanup_stack
);
1300 return EXT_LANG_BT_ERROR
;
1303 do_cleanups (py_fn_cleanup
);
1306 if (PyObject_HasAttrString (filter
, "line"))
1308 PyObject
*py_line
= PyObject_CallMethod (filter
, "line", NULL
);
1309 struct cleanup
*py_line_cleanup
;
1312 if (py_line
== NULL
)
1314 do_cleanups (cleanup_stack
);
1315 return EXT_LANG_BT_ERROR
;
1317 py_line_cleanup
= make_cleanup_py_decref (py_line
);
1319 if (py_line
!= Py_None
)
1321 line
= PyLong_AsLong (py_line
);
1322 TRY_CATCH (except
, RETURN_MASK_ALL
)
1324 ui_out_text (out
, ":");
1325 annotate_frame_source_line ();
1326 ui_out_field_int (out
, "line", line
);
1328 if (except
.reason
< 0)
1330 gdbpy_convert_exception (except
);
1331 do_cleanups (cleanup_stack
);
1332 return EXT_LANG_BT_ERROR
;
1335 do_cleanups (py_line_cleanup
);
1339 /* For MI we need to deal with the "children" list population of
1340 elided frames, so if MI output detected do not send newline. */
1341 if (! ui_out_is_mi_like_p (out
))
1343 TRY_CATCH (except
, RETURN_MASK_ALL
)
1345 annotate_frame_end ();
1346 ui_out_text (out
, "\n");
1348 if (except
.reason
< 0)
1350 gdbpy_convert_exception (except
);
1351 do_cleanups (cleanup_stack
);
1352 return EXT_LANG_BT_ERROR
;
1358 if (py_print_locals (filter
, out
, args_type
, indent
,
1359 frame
) == EXT_LANG_BT_ERROR
)
1361 do_cleanups (cleanup_stack
);
1362 return EXT_LANG_BT_ERROR
;
1368 struct cleanup
*elided_cleanup
;
1370 /* Finally recursively print elided frames, if any. */
1371 elided
= get_py_iter_from_func (filter
, "elided");
1374 do_cleanups (cleanup_stack
);
1375 return EXT_LANG_BT_ERROR
;
1377 elided_cleanup
= make_cleanup_py_decref (elided
);
1379 if (elided
!= Py_None
)
1383 make_cleanup_ui_out_list_begin_end (out
, "children");
1385 if (! ui_out_is_mi_like_p (out
))
1388 while ((item
= PyIter_Next (elided
)))
1390 struct cleanup
*item_cleanup
= make_cleanup_py_decref (item
);
1392 enum ext_lang_bt_status success
= py_print_frame (item
, flags
,
1397 do_cleanups (item_cleanup
);
1399 if (success
== EXT_LANG_BT_ERROR
)
1401 do_cleanups (cleanup_stack
);
1402 return EXT_LANG_BT_ERROR
;
1405 if (item
== NULL
&& PyErr_Occurred ())
1407 do_cleanups (cleanup_stack
);
1408 return EXT_LANG_BT_ERROR
;
1411 do_cleanups (elided_cleanup
);
1414 do_cleanups (cleanup_stack
);
1415 return EXT_LANG_BT_COMPLETED
;
1418 /* Helper function to initiate frame filter invocation at starting
1422 bootstrap_python_frame_filters (struct frame_info
*frame
,
1423 int frame_low
, int frame_high
)
1425 struct cleanup
*cleanups
=
1426 make_cleanup (null_cleanup
, NULL
);
1427 PyObject
*module
, *sort_func
, *iterable
, *frame_obj
, *iterator
;
1428 PyObject
*py_frame_low
, *py_frame_high
;
1430 frame_obj
= frame_info_to_frame_object (frame
);
1431 if (frame_obj
== NULL
)
1433 make_cleanup_py_decref (frame_obj
);
1435 module
= PyImport_ImportModule ("gdb.frames");
1438 make_cleanup_py_decref (module
);
1440 sort_func
= PyObject_GetAttrString (module
, "execute_frame_filters");
1441 if (sort_func
== NULL
)
1443 make_cleanup_py_decref (sort_func
);
1445 py_frame_low
= PyInt_FromLong (frame_low
);
1446 if (py_frame_low
== NULL
)
1448 make_cleanup_py_decref (py_frame_low
);
1450 py_frame_high
= PyInt_FromLong (frame_high
);
1451 if (py_frame_high
== NULL
)
1453 make_cleanup_py_decref (py_frame_high
);
1455 iterable
= PyObject_CallFunctionObjArgs (sort_func
, frame_obj
,
1459 if (iterable
== NULL
)
1462 do_cleanups (cleanups
);
1464 if (iterable
!= Py_None
)
1466 iterator
= PyObject_GetIter (iterable
);
1467 Py_DECREF (iterable
);
1477 do_cleanups (cleanups
);
1481 /* This is the only publicly exported function in this file. FRAME
1482 is the source frame to start frame-filter invocation. FLAGS is an
1483 integer holding the flags for printing. The following elements of
1484 the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
1485 PRINT_LEVEL is a flag indicating whether to print the frame's
1486 relative level in the output. PRINT_FRAME_INFO is a flag that
1487 indicates whether this function should print the frame
1488 information, PRINT_ARGS is a flag that indicates whether to print
1489 frame arguments, and PRINT_LOCALS, likewise, with frame local
1490 variables. ARGS_TYPE is an enumerator describing the argument
1491 format, OUT is the output stream to print. FRAME_LOW is the
1492 beginning of the slice of frames to print, and FRAME_HIGH is the
1493 upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error,
1494 or EXT_LANG_BT_COMPLETED on success. */
1496 enum ext_lang_bt_status
1497 gdbpy_apply_frame_filter (const struct extension_language_defn
*extlang
,
1498 struct frame_info
*frame
, int flags
,
1499 enum ext_lang_frame_args args_type
,
1500 struct ui_out
*out
, int frame_low
, int frame_high
)
1502 struct gdbarch
*gdbarch
= NULL
;
1503 struct cleanup
*cleanups
;
1504 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
1506 volatile struct gdb_exception except
;
1508 htab_t levels_printed
;
1510 if (!gdb_python_initialized
)
1511 return EXT_LANG_BT_NO_FILTERS
;
1513 TRY_CATCH (except
, RETURN_MASK_ALL
)
1515 gdbarch
= get_frame_arch (frame
);
1517 if (except
.reason
< 0)
1519 /* Let gdb try to print the stack trace. */
1520 return EXT_LANG_BT_NO_FILTERS
;
1523 cleanups
= ensure_python_env (gdbarch
, current_language
);
1525 iterable
= bootstrap_python_frame_filters (frame
, frame_low
, frame_high
);
1527 if (iterable
== NULL
)
1529 /* Normally if there is an error GDB prints the exception,
1530 abandons the backtrace and exits. The user can then call "bt
1531 no-filters", and get a default backtrace (it would be
1532 confusing to automatically start a standard backtrace halfway
1533 through a Python filtered backtrace). However in the case
1534 where GDB cannot initialize the frame filters (most likely
1535 due to incorrect auto-load paths), GDB has printed nothing.
1536 In this case it is OK to print the default backtrace after
1537 printing the error message. GDB returns EXT_LANG_BT_NO_FILTERS
1538 here to signify there are no filters after printing the
1539 initialization error. This return code will trigger a
1540 default backtrace. */
1542 gdbpy_print_stack ();
1543 do_cleanups (cleanups
);
1544 return EXT_LANG_BT_NO_FILTERS
;
1547 /* If iterable is None, then there are no frame filters registered.
1548 If this is the case, defer to default GDB printing routines in MI
1550 make_cleanup_py_decref (iterable
);
1551 if (iterable
== Py_None
)
1553 success
= EXT_LANG_BT_NO_FILTERS
;
1557 levels_printed
= htab_create (20,
1561 make_cleanup_htab_delete (levels_printed
);
1563 while ((item
= PyIter_Next (iterable
)))
1565 struct cleanup
*item_cleanup
= make_cleanup_py_decref (item
);
1567 success
= py_print_frame (item
, flags
, args_type
, out
, 0,
1570 do_cleanups (item_cleanup
);
1572 /* Do not exit on error printing a single frame. Print the
1573 error and continue with other frames. */
1574 if (success
== EXT_LANG_BT_ERROR
)
1575 gdbpy_print_stack ();
1578 if (item
== NULL
&& PyErr_Occurred ())
1582 do_cleanups (cleanups
);
1585 /* Exit and abandon backtrace on error, printing the exception that
1588 gdbpy_print_stack ();
1589 do_cleanups (cleanups
);
1590 return EXT_LANG_BT_ERROR
;