1 /* Python frame filters
3 Copyright (C) 2013-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "arch-utils.h"
31 #include "mi/mi-cmds.h"
32 #include "python-internal.h"
41 /* Helper function to extract a symbol, a name and a language
42 definition from a Python object that conforms to the "Symbol Value"
43 interface. OBJ is the Python object to extract the values from.
44 NAME is a pass-through argument where the name of the symbol will
45 be written. NAME is allocated in this function, but the caller is
46 responsible for clean up. SYM is a pass-through argument where the
47 symbol will be written and SYM_BLOCK is a pass-through argument to
48 write the block where the symbol lies in. In the case of the API
49 returning a string, this will be set to NULL. LANGUAGE is also a
50 pass-through argument denoting the language attributed to the
51 Symbol. In the case of SYM being NULL, this will be set to the
52 current language. Returns EXT_LANG_BT_ERROR on error with the
53 appropriate Python exception set, and EXT_LANG_BT_OK on success. */
55 static enum ext_lang_bt_status
56 extract_sym (PyObject
*obj
, gdb::unique_xmalloc_ptr
<char> *name
,
57 struct symbol
**sym
, struct block
**sym_block
,
58 const struct language_defn
**language
)
60 gdbpy_ref
result (PyObject_CallMethod (obj
, "symbol", NULL
));
63 return EXT_LANG_BT_ERROR
;
65 /* For 'symbol' callback, the function can return a symbol or a
67 if (gdbpy_is_string (result
.get ()))
69 *name
= python_string_to_host_string (result
.get ());
72 return EXT_LANG_BT_ERROR
;
73 /* If the API returns a string (and not a symbol), then there is
74 no symbol derived language available and the frame filter has
75 either overridden the symbol with a string, or supplied a
76 entirely synthetic symbol/value pairing. In that case, use
78 *language
= python_language
;
84 /* This type checks 'result' during the conversion so we
85 just call it unconditionally and check the return. */
86 *sym
= symbol_object_to_symbol (result
.get ());
87 /* TODO: currently, we have no way to recover the block in which SYMBOL
88 was found, so we have no block to return. Trying to evaluate SYMBOL
89 will yield an incorrect value when it's located in a FRAME and
90 evaluated from another frame (as permitted in nested functions). */
95 PyErr_SetString (PyExc_RuntimeError
,
96 _("Unexpected value. Expecting a "
97 "gdb.Symbol or a Python string."));
98 return EXT_LANG_BT_ERROR
;
101 /* Duplicate the symbol name, so the caller has consistency
102 in garbage collection. */
103 name
->reset (xstrdup (SYMBOL_PRINT_NAME (*sym
)));
105 /* If a symbol is specified attempt to determine the language
106 from the symbol. If mode is not "auto", then the language
107 has been explicitly set, use that. */
108 if (language_mode
== language_mode_auto
)
109 *language
= language_def (SYMBOL_LANGUAGE (*sym
));
111 *language
= current_language
;
114 return EXT_LANG_BT_OK
;
117 /* Helper function to extract a value from an object that conforms to
118 the "Symbol Value" interface. OBJ is the Python object to extract
119 the value from. VALUE is a pass-through argument where the value
120 will be written. If the object does not have the value attribute,
121 or provides the Python None for a value, VALUE will be set to NULL
122 and this function will return as successful. Returns EXT_LANG_BT_ERROR
123 on error with the appropriate Python exception set, and EXT_LANG_BT_OK on
126 static enum ext_lang_bt_status
127 extract_value (PyObject
*obj
, struct value
**value
)
129 if (PyObject_HasAttrString (obj
, "value"))
131 gdbpy_ref
vresult (PyObject_CallMethod (obj
, "value", NULL
));
134 return EXT_LANG_BT_ERROR
;
136 /* The Python code has returned 'None' for a value, so we set
137 value to NULL. This flags that GDB should read the
139 if (vresult
== Py_None
)
142 return EXT_LANG_BT_OK
;
146 *value
= convert_value_from_python (vresult
.get ());
149 return EXT_LANG_BT_ERROR
;
151 return EXT_LANG_BT_OK
;
157 return EXT_LANG_BT_OK
;
160 /* MI prints only certain values according to the type of symbol and
161 also what the user has specified. SYM is the symbol to check, and
162 MI_PRINT_TYPES is an enum specifying what the user wants emitted
163 for the MI command in question. */
165 mi_should_print (struct symbol
*sym
, enum mi_print_types type
)
169 switch (SYMBOL_CLASS (sym
))
172 case LOC_UNDEF
: /* catches errors */
173 case LOC_CONST
: /* constant */
174 case LOC_TYPEDEF
: /* local typedef */
175 case LOC_LABEL
: /* local label */
176 case LOC_BLOCK
: /* local function */
177 case LOC_CONST_BYTES
: /* loc. byte seq. */
178 case LOC_UNRESOLVED
: /* unresolved static */
179 case LOC_OPTIMIZED_OUT
: /* optimized out */
183 case LOC_ARG
: /* argument */
184 case LOC_REF_ARG
: /* reference arg */
185 case LOC_REGPARM_ADDR
: /* indirect register arg */
186 case LOC_LOCAL
: /* stack local */
187 case LOC_STATIC
: /* static */
188 case LOC_REGISTER
: /* register */
189 case LOC_COMPUTED
: /* computed location */
190 if (type
== MI_PRINT_LOCALS
)
191 print_me
= ! SYMBOL_IS_ARGUMENT (sym
);
193 print_me
= SYMBOL_IS_ARGUMENT (sym
);
198 /* Helper function which outputs a type name extracted from VAL to a
199 "type" field in the output stream OUT. OUT is the ui-out structure
200 the type name will be output too, and VAL is the value that the
201 type will be extracted from. Returns EXT_LANG_BT_ERROR on error, with
202 any GDB exceptions converted to a Python exception, or EXT_LANG_BT_OK on
205 static enum ext_lang_bt_status
206 py_print_type (struct ui_out
*out
, struct value
*val
)
212 struct cleanup
*cleanup
;
214 stb
= mem_fileopen ();
215 cleanup
= make_cleanup_ui_file_delete (stb
);
216 check_typedef (value_type (val
));
217 type_print (value_type (val
), "", stb
, -1);
218 out
->field_stream ("type", stb
);
219 do_cleanups (cleanup
);
221 CATCH (except
, RETURN_MASK_ALL
)
223 gdbpy_convert_exception (except
);
224 return EXT_LANG_BT_ERROR
;
228 return EXT_LANG_BT_OK
;
231 /* Helper function which outputs a value to an output field in a
232 stream. OUT is the ui-out structure the value will be output to,
233 VAL is the value that will be printed, OPTS contains the value
234 printing options, ARGS_TYPE is an enumerator describing the
235 argument format, and LANGUAGE is the language_defn that the value
236 will be printed with. Returns EXT_LANG_BT_ERROR on error, with any GDB
237 exceptions converted to a Python exception, or EXT_LANG_BT_OK on
240 static enum ext_lang_bt_status
241 py_print_value (struct ui_out
*out
, struct value
*val
,
242 const struct value_print_options
*opts
,
244 enum ext_lang_frame_args args_type
,
245 const struct language_defn
*language
)
247 int should_print
= 0;
249 /* MI does not print certain values, differentiated by type,
250 depending on what ARGS_TYPE indicates. Test type against option.
251 For CLI print all values. */
252 if (args_type
== MI_PRINT_SIMPLE_VALUES
253 || args_type
== MI_PRINT_ALL_VALUES
)
255 struct type
*type
= NULL
;
259 type
= check_typedef (value_type (val
));
261 CATCH (except
, RETURN_MASK_ALL
)
263 gdbpy_convert_exception (except
);
264 return EXT_LANG_BT_ERROR
;
268 if (args_type
== MI_PRINT_ALL_VALUES
)
270 else if (args_type
== MI_PRINT_SIMPLE_VALUES
271 && TYPE_CODE (type
) != TYPE_CODE_ARRAY
272 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
273 && TYPE_CODE (type
) != TYPE_CODE_UNION
)
276 else if (args_type
!= NO_VALUES
)
284 struct cleanup
*cleanup
;
286 stb
= mem_fileopen ();
287 cleanup
= make_cleanup_ui_file_delete (stb
);
288 common_val_print (val
, stb
, indent
, opts
, language
);
289 out
->field_stream ("value", stb
);
290 do_cleanups (cleanup
);
292 CATCH (except
, RETURN_MASK_ALL
)
294 gdbpy_convert_exception (except
);
295 return EXT_LANG_BT_ERROR
;
300 return EXT_LANG_BT_OK
;
303 /* Helper function to call a Python method and extract an iterator
304 from the result. If the function returns anything but an iterator
305 the exception is preserved and NULL is returned. FILTER is the
306 Python object to call, and FUNC is the name of the method. Returns
307 a PyObject, or NULL on error with the appropriate exception set.
308 This function can return an iterator, or NULL. */
311 get_py_iter_from_func (PyObject
*filter
, char *func
)
313 if (PyObject_HasAttrString (filter
, func
))
315 gdbpy_ref
result (PyObject_CallMethod (filter
, func
, NULL
));
319 if (result
== Py_None
)
321 return result
.release ();
325 return PyObject_GetIter (result
.get ());
335 /* Helper function to output a single frame argument and value to an
336 output stream. This function will account for entry values if the
337 FV parameter is populated, the frame argument has entry values
338 associated with them, and the appropriate "set entry-value"
339 options are set. Will output in CLI or MI like format depending
340 on the type of output stream detected. OUT is the output stream,
341 SYM_NAME is the name of the symbol. If SYM_NAME is populated then
342 it must have an accompanying value in the parameter FV. FA is a
343 frame argument structure. If FA is populated, both SYM_NAME and
344 FV are ignored. OPTS contains the value printing options,
345 ARGS_TYPE is an enumerator describing the argument format,
346 PRINT_ARGS_FIELD is a flag which indicates if we output "ARGS=1"
347 in MI output in commands where both arguments and locals are
348 printed. Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions
349 converted to a Python exception, or EXT_LANG_BT_OK on success. */
351 static enum ext_lang_bt_status
352 py_print_single_arg (struct ui_out
*out
,
353 const char *sym_name
,
354 struct frame_arg
*fa
,
356 const struct value_print_options
*opts
,
357 enum ext_lang_frame_args args_type
,
358 int print_args_field
,
359 const struct language_defn
*language
)
362 enum ext_lang_bt_status retval
= EXT_LANG_BT_OK
;
366 if (fa
->val
== NULL
&& fa
->error
== NULL
)
367 return EXT_LANG_BT_OK
;
368 language
= language_def (SYMBOL_LANGUAGE (fa
->sym
));
376 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
378 /* MI has varying rules for tuples, but generally if there is only
379 one element in each item in the list, do not start a tuple. The
380 exception is -stack-list-variables which emits an ARGS="1" field
381 if the value is a frame argument. This is denoted in this
382 function with PRINT_ARGS_FIELD which is flag from the caller to
383 emit the ARGS field. */
384 if (out
->is_mi_like_p ())
386 if (print_args_field
|| args_type
!= NO_VALUES
)
387 make_cleanup_ui_out_tuple_begin_end (out
, NULL
);
390 annotate_arg_begin ();
392 /* If frame argument is populated, check for entry-values and the
393 entry value options. */
398 stb
= mem_fileopen ();
399 make_cleanup_ui_file_delete (stb
);
400 fprintf_symbol_filtered (stb
, SYMBOL_PRINT_NAME (fa
->sym
),
401 SYMBOL_LANGUAGE (fa
->sym
),
402 DMGL_PARAMS
| DMGL_ANSI
);
403 if (fa
->entry_kind
== print_entry_values_compact
)
405 fputs_filtered ("=", stb
);
407 fprintf_symbol_filtered (stb
, SYMBOL_PRINT_NAME (fa
->sym
),
408 SYMBOL_LANGUAGE (fa
->sym
),
409 DMGL_PARAMS
| DMGL_ANSI
);
411 if (fa
->entry_kind
== print_entry_values_only
412 || fa
->entry_kind
== print_entry_values_compact
)
414 fputs_filtered ("@entry", stb
);
416 out
->field_stream ("name", stb
);
419 /* Otherwise, just output the name. */
420 out
->field_string ("name", sym_name
);
422 annotate_arg_name_end ();
424 if (! out
->is_mi_like_p ())
427 if (print_args_field
)
428 out
->field_int ("arg", 1);
430 /* For MI print the type, but only for simple values. This seems
431 weird, but this is how MI choose to format the various output
433 if (args_type
== MI_PRINT_SIMPLE_VALUES
&& val
!= NULL
)
435 if (py_print_type (out
, val
) == EXT_LANG_BT_ERROR
)
437 retval
= EXT_LANG_BT_ERROR
;
438 do_cleanups (cleanups
);
442 if (retval
!= EXT_LANG_BT_ERROR
)
445 annotate_arg_value (value_type (val
));
447 /* If the output is to the CLI, and the user option "set print
448 frame-arguments" is set to none, just output "...". */
449 if (! out
->is_mi_like_p () && args_type
== NO_VALUES
)
450 out
->field_string ("value", "...");
453 /* Otherwise, print the value for both MI and the CLI, except
454 for the case of MI_PRINT_NO_VALUES. */
455 if (args_type
!= NO_VALUES
)
459 gdb_assert (fa
!= NULL
&& fa
->error
!= NULL
);
460 out
->field_fmt ("value",
461 _("<error reading variable: %s>"),
464 else if (py_print_value (out
, val
, opts
, 0, args_type
, language
)
465 == EXT_LANG_BT_ERROR
)
466 retval
= EXT_LANG_BT_ERROR
;
470 do_cleanups (cleanups
);
473 CATCH (except
, RETURN_MASK_ERROR
)
475 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
;
503 get_user_print_options (&opts
);
505 if (args_type
== CLI_SCALAR_VALUES
)
507 /* True in "summary" mode, false otherwise. */
515 annotate_frame_args ();
517 CATCH (except
, RETURN_MASK_ALL
)
519 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
;
535 gdb::unique_xmalloc_ptr
<char> sym_name
;
537 struct block
*sym_block
;
539 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
541 success
= extract_sym (item
, &sym_name
, &sym
, &sym_block
, &language
);
542 if (success
== EXT_LANG_BT_ERROR
)
548 success
= extract_value (item
, &val
);
549 if (success
== EXT_LANG_BT_ERROR
)
558 if (sym
&& out
->is_mi_like_p ()
559 && ! mi_should_print (sym
, MI_PRINT_ARGS
))
562 /* If the object did not provide a value, read it using
563 read_frame_args and account for entry values, if any. */
566 struct frame_arg arg
, entryarg
;
568 /* If there is no value, and also no symbol, set error and
572 PyErr_SetString (PyExc_RuntimeError
,
573 _("No symbol or value provided."));
579 read_frame_arg (sym
, frame
, &arg
, &entryarg
);
581 CATCH (except
, RETURN_MASK_ALL
)
583 gdbpy_convert_exception (except
);
588 /* The object has not provided a value, so this is a frame
589 argument to be read by GDB. In this case we have to
590 account for entry-values. */
592 if (arg
.entry_kind
!= print_entry_values_only
)
594 if (py_print_single_arg (out
, NULL
, &arg
,
598 NULL
) == EXT_LANG_BT_ERROR
)
601 xfree (entryarg
.error
);
606 if (entryarg
.entry_kind
!= print_entry_values_no
)
608 if (arg
.entry_kind
!= print_entry_values_only
)
613 out
->wrap_hint (" ");
615 CATCH (except
, RETURN_MASK_ALL
)
618 xfree (entryarg
.error
);
619 gdbpy_convert_exception (except
);
625 if (py_print_single_arg (out
, NULL
, &entryarg
, NULL
, &opts
,
626 args_type
, print_args_field
, NULL
)
627 == EXT_LANG_BT_ERROR
)
630 xfree (entryarg
.error
);
636 xfree (entryarg
.error
);
640 /* If the object has provided a value, we just print that. */
643 if (py_print_single_arg (out
, sym_name
.get (), NULL
, val
, &opts
,
644 args_type
, print_args_field
,
645 language
) == EXT_LANG_BT_ERROR
)
650 /* Collect the next item from the iterator. If
651 this is the last item, do not print the
653 item
= PyIter_Next (iter
);
660 CATCH (except
, RETURN_MASK_ALL
)
663 gdbpy_convert_exception (except
);
668 else if (PyErr_Occurred ())
675 CATCH (except
, RETURN_MASK_ALL
)
678 gdbpy_convert_exception (except
);
684 return EXT_LANG_BT_OK
;
687 return EXT_LANG_BT_ERROR
;
691 /* Helper function to loop over variables provided by the
692 "frame_locals" Python API. Elements in the iterable must conform
693 to the "Symbol Value" interface. ITER is the Python iterable
694 object, OUT is the output stream, INDENT is whether we should
695 indent the output (for CLI), ARGS_TYPE is an enumerator describing
696 the argument format, PRINT_ARGS_FIELD is flag which indicates
697 whether to output the ARGS field in the case of
698 -stack-list-variables and FRAME is the backing frame. Returns
699 EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to a Python
700 exception, or EXT_LANG_BT_OK on success. */
702 static enum ext_lang_bt_status
703 enumerate_locals (PyObject
*iter
,
706 enum ext_lang_frame_args args_type
,
707 int print_args_field
,
708 struct frame_info
*frame
)
711 struct value_print_options opts
;
713 get_user_print_options (&opts
);
716 while ((item
= PyIter_Next (iter
)))
718 const struct language_defn
*language
;
719 gdb::unique_xmalloc_ptr
<char> sym_name
;
721 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
723 struct block
*sym_block
;
724 int local_indent
= 8 + (8 * indent
);
725 struct cleanup
*locals_cleanups
;
727 locals_cleanups
= make_cleanup_py_decref (item
);
729 success
= extract_sym (item
, &sym_name
, &sym
, &sym_block
, &language
);
730 if (success
== EXT_LANG_BT_ERROR
)
732 do_cleanups (locals_cleanups
);
736 success
= extract_value (item
, &val
);
737 if (success
== EXT_LANG_BT_ERROR
)
739 do_cleanups (locals_cleanups
);
743 if (sym
!= NULL
&& out
->is_mi_like_p ()
744 && ! mi_should_print (sym
, MI_PRINT_LOCALS
))
746 do_cleanups (locals_cleanups
);
750 /* If the object did not provide a value, read it. */
755 val
= read_var_value (sym
, sym_block
, frame
);
757 CATCH (except
, RETURN_MASK_ERROR
)
759 gdbpy_convert_exception (except
);
760 do_cleanups (locals_cleanups
);
766 /* With PRINT_NO_VALUES, MI does not emit a tuple normally as
767 each output contains only one field. The exception is
768 -stack-list-variables, which always provides a tuple. */
769 if (out
->is_mi_like_p ())
771 if (print_args_field
|| args_type
!= NO_VALUES
)
772 make_cleanup_ui_out_tuple_begin_end (out
, NULL
);
776 if (! out
->is_mi_like_p ())
778 /* If the output is not MI we indent locals. */
779 out
->spaces (local_indent
);
782 out
->field_string ("name", sym_name
.get ());
784 if (! out
->is_mi_like_p ())
787 CATCH (except
, RETURN_MASK_ERROR
)
789 gdbpy_convert_exception (except
);
790 do_cleanups (locals_cleanups
);
795 if (args_type
== MI_PRINT_SIMPLE_VALUES
)
797 if (py_print_type (out
, val
) == EXT_LANG_BT_ERROR
)
799 do_cleanups (locals_cleanups
);
804 /* CLI always prints values for locals. MI uses the
805 simple/no/all system. */
806 if (! out
->is_mi_like_p ())
808 int val_indent
= (indent
+ 1) * 4;
810 if (py_print_value (out
, val
, &opts
, val_indent
, args_type
,
811 language
) == EXT_LANG_BT_ERROR
)
813 do_cleanups (locals_cleanups
);
819 if (args_type
!= NO_VALUES
)
821 if (py_print_value (out
, val
, &opts
, 0, args_type
,
822 language
) == EXT_LANG_BT_ERROR
)
824 do_cleanups (locals_cleanups
);
830 do_cleanups (locals_cleanups
);
836 CATCH (except
, RETURN_MASK_ERROR
)
838 gdbpy_convert_exception (except
);
844 if (item
== NULL
&& PyErr_Occurred ())
847 return EXT_LANG_BT_OK
;
850 return EXT_LANG_BT_ERROR
;
853 /* Helper function for -stack-list-variables. Returns EXT_LANG_BT_ERROR on
854 error, or EXT_LANG_BT_OK on success. */
856 static enum ext_lang_bt_status
857 py_mi_print_variables (PyObject
*filter
, struct ui_out
*out
,
858 struct value_print_options
*opts
,
859 enum ext_lang_frame_args args_type
,
860 struct frame_info
*frame
)
862 struct cleanup
*old_chain
;
864 PyObject
*locals_iter
;
866 args_iter
= get_py_iter_from_func (filter
, "frame_args");
867 old_chain
= make_cleanup_py_xdecref (args_iter
);
868 if (args_iter
== NULL
)
871 locals_iter
= get_py_iter_from_func (filter
, "frame_locals");
872 if (locals_iter
== NULL
)
875 make_cleanup_py_decref (locals_iter
);
876 make_cleanup_ui_out_list_begin_end (out
, "variables");
878 if (args_iter
!= Py_None
)
879 if (enumerate_args (args_iter
, out
, args_type
, 1, frame
)
880 == EXT_LANG_BT_ERROR
)
883 if (locals_iter
!= Py_None
)
884 if (enumerate_locals (locals_iter
, out
, 1, args_type
, 1, frame
)
885 == EXT_LANG_BT_ERROR
)
888 do_cleanups (old_chain
);
889 return EXT_LANG_BT_OK
;
892 do_cleanups (old_chain
);
893 return EXT_LANG_BT_ERROR
;
896 /* Helper function for printing locals. This function largely just
897 creates the wrapping tuple, and calls enumerate_locals. Returns
898 EXT_LANG_BT_ERROR on error, or EXT_LANG_BT_OK on success. */
900 static enum ext_lang_bt_status
901 py_print_locals (PyObject
*filter
,
903 enum ext_lang_frame_args args_type
,
905 struct frame_info
*frame
)
907 PyObject
*locals_iter
= get_py_iter_from_func (filter
,
909 struct cleanup
*old_chain
= make_cleanup_py_xdecref (locals_iter
);
911 if (locals_iter
== NULL
)
914 make_cleanup_ui_out_list_begin_end (out
, "locals");
916 if (locals_iter
!= Py_None
)
917 if (enumerate_locals (locals_iter
, out
, indent
, args_type
,
918 0, frame
) == EXT_LANG_BT_ERROR
)
921 do_cleanups (old_chain
);
922 return EXT_LANG_BT_OK
;
925 do_cleanups (old_chain
);
926 return EXT_LANG_BT_ERROR
;
929 /* Helper function for printing frame arguments. This function
930 largely just creates the wrapping tuple, and calls enumerate_args.
931 Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to
932 a Python exception, or EXT_LANG_BT_OK on success. */
934 static enum ext_lang_bt_status
935 py_print_args (PyObject
*filter
,
937 enum ext_lang_frame_args args_type
,
938 struct frame_info
*frame
)
940 PyObject
*args_iter
= get_py_iter_from_func (filter
, "frame_args");
941 struct cleanup
*old_chain
= make_cleanup_py_xdecref (args_iter
);
943 if (args_iter
== NULL
)
946 make_cleanup_ui_out_list_begin_end (out
, "args");
950 annotate_frame_args ();
951 if (! out
->is_mi_like_p ())
954 CATCH (except
, RETURN_MASK_ALL
)
956 gdbpy_convert_exception (except
);
961 if (args_iter
!= Py_None
)
962 if (enumerate_args (args_iter
, out
, args_type
, 0, frame
)
963 == EXT_LANG_BT_ERROR
)
968 if (! out
->is_mi_like_p ())
971 CATCH (except
, RETURN_MASK_ALL
)
973 gdbpy_convert_exception (except
);
978 do_cleanups (old_chain
);
979 return EXT_LANG_BT_OK
;
982 do_cleanups (old_chain
);
983 return EXT_LANG_BT_ERROR
;
986 /* Print a single frame to the designated output stream, detecting
987 whether the output is MI or console, and formatting the output
988 according to the conventions of that protocol. FILTER is the
989 frame-filter associated with this frame. FLAGS is an integer
990 describing the various print options. The FLAGS variables is
991 described in "apply_frame_filter" function. ARGS_TYPE is an
992 enumerator describing the argument format. OUT is the output
993 stream to print, INDENT is the level of indention for this frame
994 (in the case of elided frames), and LEVELS_PRINTED is a hash-table
995 containing all the frames level that have already been printed.
996 If a frame level has been printed, do not print it again (in the
997 case of elided frames). Returns EXT_LANG_BT_ERROR on error, with any
998 GDB exceptions converted to a Python exception, or EXT_LANG_BT_COMPLETED
999 on success. It can also throw an exception RETURN_QUIT. */
1001 static enum ext_lang_bt_status
1002 py_print_frame (PyObject
*filter
, int flags
,
1003 enum ext_lang_frame_args args_type
,
1004 struct ui_out
*out
, int indent
, htab_t levels_printed
)
1007 CORE_ADDR address
= 0;
1008 struct gdbarch
*gdbarch
= NULL
;
1009 struct frame_info
*frame
= NULL
;
1010 struct cleanup
*cleanup_stack
;
1011 struct value_print_options opts
;
1012 PyObject
*py_inf_frame
;
1013 int print_level
, print_frame_info
, print_args
, print_locals
;
1014 gdb::unique_xmalloc_ptr
<char> function_to_free
;
1016 /* Extract print settings from FLAGS. */
1017 print_level
= (flags
& PRINT_LEVEL
) ? 1 : 0;
1018 print_frame_info
= (flags
& PRINT_FRAME_INFO
) ? 1 : 0;
1019 print_args
= (flags
& PRINT_ARGS
) ? 1 : 0;
1020 print_locals
= (flags
& PRINT_LOCALS
) ? 1 : 0;
1022 get_user_print_options (&opts
);
1024 /* Get the underlying frame. This is needed to determine GDB
1025 architecture, and also, in the cases of frame variables/arguments to
1026 read them if they returned filter object requires us to do so. */
1027 py_inf_frame
= PyObject_CallMethod (filter
, "inferior_frame", NULL
);
1028 if (py_inf_frame
== NULL
)
1029 return EXT_LANG_BT_ERROR
;
1031 frame
= frame_object_to_frame_info (py_inf_frame
);;
1033 Py_DECREF (py_inf_frame
);
1036 return EXT_LANG_BT_ERROR
;
1040 gdbarch
= get_frame_arch (frame
);
1042 CATCH (except
, RETURN_MASK_ERROR
)
1044 gdbpy_convert_exception (except
);
1045 return EXT_LANG_BT_ERROR
;
1049 /* stack-list-variables. */
1050 if (print_locals
&& print_args
&& ! print_frame_info
)
1052 if (py_mi_print_variables (filter
, out
, &opts
,
1053 args_type
, frame
) == EXT_LANG_BT_ERROR
)
1054 return EXT_LANG_BT_ERROR
;
1055 return EXT_LANG_BT_COMPLETED
;
1058 cleanup_stack
= make_cleanup (null_cleanup
, NULL
);
1060 /* -stack-list-locals does not require a
1061 wrapping frame attribute. */
1062 if (print_frame_info
|| (print_args
&& ! print_locals
))
1063 make_cleanup_ui_out_tuple_begin_end (out
, "frame");
1065 if (print_frame_info
)
1067 /* Elided frames are also printed with this function (recursively)
1068 and are printed with indention. */
1073 out
->spaces (indent
* 4);
1075 CATCH (except
, RETURN_MASK_ERROR
)
1077 gdbpy_convert_exception (except
);
1078 do_cleanups (cleanup_stack
);
1079 return EXT_LANG_BT_ERROR
;
1084 /* The address is required for frame annotations, and also for
1085 address printing. */
1086 if (PyObject_HasAttrString (filter
, "address"))
1088 PyObject
*paddr
= PyObject_CallMethod (filter
, "address", NULL
);
1092 do_cleanups (cleanup_stack
);
1093 return EXT_LANG_BT_ERROR
;
1096 if (paddr
!= Py_None
)
1098 if (get_addr_from_python (paddr
, &address
) < 0)
1101 do_cleanups (cleanup_stack
);
1102 return EXT_LANG_BT_ERROR
;
1111 /* Print frame level. MI does not require the level if
1112 locals/variables only are being printed. */
1113 if ((print_frame_info
|| print_args
) && print_level
)
1115 struct frame_info
**slot
;
1118 slot
= (struct frame_info
**) htab_find_slot (levels_printed
,
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 out
->field_skip ("level");
1133 annotate_frame_begin (print_level
? level
: 0,
1136 out
->field_fmt_int (2, ui_left
, "level",
1140 CATCH (except
, RETURN_MASK_ERROR
)
1142 gdbpy_convert_exception (except
);
1143 do_cleanups (cleanup_stack
);
1144 return EXT_LANG_BT_ERROR
;
1149 if (print_frame_info
)
1151 /* Print address to the address field. If an address is not provided,
1153 if (opts
.addressprint
&& has_addr
)
1157 annotate_frame_address ();
1158 out
->field_core_addr ("addr", gdbarch
, address
);
1159 annotate_frame_address_end ();
1162 CATCH (except
, RETURN_MASK_ERROR
)
1164 gdbpy_convert_exception (except
);
1165 do_cleanups (cleanup_stack
);
1166 return EXT_LANG_BT_ERROR
;
1171 /* Print frame function name. */
1172 if (PyObject_HasAttrString (filter
, "function"))
1174 PyObject
*py_func
= PyObject_CallMethod (filter
, "function", NULL
);
1175 struct cleanup
*py_func_cleanup
;
1176 const char *function
= NULL
;
1178 if (py_func
== NULL
)
1180 do_cleanups (cleanup_stack
);
1181 return EXT_LANG_BT_ERROR
;
1183 py_func_cleanup
= make_cleanup_py_decref (py_func
);
1185 if (gdbpy_is_string (py_func
))
1187 function_to_free
= python_string_to_host_string (py_func
);
1189 if (function_to_free
== NULL
)
1191 do_cleanups (cleanup_stack
);
1192 return EXT_LANG_BT_ERROR
;
1195 function
= function_to_free
.get ();
1197 else if (PyLong_Check (py_func
))
1200 struct bound_minimal_symbol msymbol
;
1202 if (get_addr_from_python (py_func
, &addr
) < 0)
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
;
1223 annotate_frame_function_name ();
1224 if (function
== NULL
)
1225 out
->field_skip ("func");
1227 out
->field_string ("func", function
);
1229 CATCH (except
, RETURN_MASK_ERROR
)
1231 gdbpy_convert_exception (except
);
1232 do_cleanups (cleanup_stack
);
1233 return EXT_LANG_BT_ERROR
;
1237 do_cleanups (py_func_cleanup
);
1242 /* Frame arguments. Check the result, and error if something went
1246 if (py_print_args (filter
, out
, args_type
, frame
) == EXT_LANG_BT_ERROR
)
1248 do_cleanups (cleanup_stack
);
1249 return EXT_LANG_BT_ERROR
;
1253 /* File name/source/line number information. */
1254 if (print_frame_info
)
1258 annotate_frame_source_begin ();
1260 CATCH (except
, RETURN_MASK_ERROR
)
1262 gdbpy_convert_exception (except
);
1263 do_cleanups (cleanup_stack
);
1264 return EXT_LANG_BT_ERROR
;
1268 if (PyObject_HasAttrString (filter
, "filename"))
1270 PyObject
*py_fn
= PyObject_CallMethod (filter
, "filename", NULL
);
1271 struct cleanup
*py_fn_cleanup
;
1275 do_cleanups (cleanup_stack
);
1276 return EXT_LANG_BT_ERROR
;
1278 py_fn_cleanup
= make_cleanup_py_decref (py_fn
);
1280 if (py_fn
!= Py_None
)
1282 gdb::unique_xmalloc_ptr
<char>
1283 filename (python_string_to_host_string (py_fn
));
1285 if (filename
== NULL
)
1287 do_cleanups (cleanup_stack
);
1288 return EXT_LANG_BT_ERROR
;
1293 out
->wrap_hint (" ");
1295 annotate_frame_source_file ();
1296 out
->field_string ("file", filename
.get ());
1297 annotate_frame_source_file_end ();
1299 CATCH (except
, RETURN_MASK_ERROR
)
1301 gdbpy_convert_exception (except
);
1302 do_cleanups (cleanup_stack
);
1303 return EXT_LANG_BT_ERROR
;
1307 do_cleanups (py_fn_cleanup
);
1310 if (PyObject_HasAttrString (filter
, "line"))
1312 PyObject
*py_line
= PyObject_CallMethod (filter
, "line", NULL
);
1313 struct cleanup
*py_line_cleanup
;
1316 if (py_line
== NULL
)
1318 do_cleanups (cleanup_stack
);
1319 return EXT_LANG_BT_ERROR
;
1321 py_line_cleanup
= make_cleanup_py_decref (py_line
);
1323 if (py_line
!= Py_None
)
1325 line
= PyLong_AsLong (py_line
);
1326 if (PyErr_Occurred ())
1328 do_cleanups (cleanup_stack
);
1329 return EXT_LANG_BT_ERROR
;
1335 annotate_frame_source_line ();
1336 out
->field_int ("line", line
);
1338 CATCH (except
, RETURN_MASK_ERROR
)
1340 gdbpy_convert_exception (except
);
1341 do_cleanups (cleanup_stack
);
1342 return EXT_LANG_BT_ERROR
;
1346 do_cleanups (py_line_cleanup
);
1350 /* For MI we need to deal with the "children" list population of
1351 elided frames, so if MI output detected do not send newline. */
1352 if (! out
->is_mi_like_p ())
1356 annotate_frame_end ();
1359 CATCH (except
, RETURN_MASK_ERROR
)
1361 gdbpy_convert_exception (except
);
1362 do_cleanups (cleanup_stack
);
1363 return EXT_LANG_BT_ERROR
;
1370 if (py_print_locals (filter
, out
, args_type
, indent
,
1371 frame
) == EXT_LANG_BT_ERROR
)
1373 do_cleanups (cleanup_stack
);
1374 return EXT_LANG_BT_ERROR
;
1380 struct cleanup
*elided_cleanup
;
1382 /* Finally recursively print elided frames, if any. */
1383 elided
= get_py_iter_from_func (filter
, "elided");
1386 do_cleanups (cleanup_stack
);
1387 return EXT_LANG_BT_ERROR
;
1389 elided_cleanup
= make_cleanup_py_decref (elided
);
1391 if (elided
!= Py_None
)
1395 make_cleanup_ui_out_list_begin_end (out
, "children");
1397 if (! out
->is_mi_like_p ())
1400 while ((item
= PyIter_Next (elided
)))
1402 struct cleanup
*item_cleanup
= make_cleanup_py_decref (item
);
1404 enum ext_lang_bt_status success
= py_print_frame (item
, flags
,
1409 do_cleanups (item_cleanup
);
1411 if (success
== EXT_LANG_BT_ERROR
)
1413 do_cleanups (cleanup_stack
);
1414 return EXT_LANG_BT_ERROR
;
1417 if (item
== NULL
&& PyErr_Occurred ())
1419 do_cleanups (cleanup_stack
);
1420 return EXT_LANG_BT_ERROR
;
1423 do_cleanups (elided_cleanup
);
1426 do_cleanups (cleanup_stack
);
1427 return EXT_LANG_BT_COMPLETED
;
1430 /* Helper function to initiate frame filter invocation at starting
1434 bootstrap_python_frame_filters (struct frame_info
*frame
,
1435 int frame_low
, int frame_high
)
1437 gdbpy_ref
frame_obj (frame_info_to_frame_object (frame
));
1438 if (frame_obj
== NULL
)
1441 gdbpy_ref
module (PyImport_ImportModule ("gdb.frames"));
1445 gdbpy_ref
sort_func (PyObject_GetAttrString (module
.get (),
1446 "execute_frame_filters"));
1447 if (sort_func
== NULL
)
1450 gdbpy_ref
py_frame_low (PyInt_FromLong (frame_low
));
1451 if (py_frame_low
== NULL
)
1454 gdbpy_ref
py_frame_high (PyInt_FromLong (frame_high
));
1455 if (py_frame_high
== NULL
)
1458 gdbpy_ref
iterable (PyObject_CallFunctionObjArgs (sort_func
.get (),
1460 py_frame_low
.get (),
1461 py_frame_high
.get (),
1463 if (iterable
== NULL
)
1466 if (iterable
!= Py_None
)
1467 return PyObject_GetIter (iterable
.get ());
1469 return iterable
.release ();
1472 /* This is the only publicly exported function in this file. FRAME
1473 is the source frame to start frame-filter invocation. FLAGS is an
1474 integer holding the flags for printing. The following elements of
1475 the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
1476 PRINT_LEVEL is a flag indicating whether to print the frame's
1477 relative level in the output. PRINT_FRAME_INFO is a flag that
1478 indicates whether this function should print the frame
1479 information, PRINT_ARGS is a flag that indicates whether to print
1480 frame arguments, and PRINT_LOCALS, likewise, with frame local
1481 variables. ARGS_TYPE is an enumerator describing the argument
1482 format, OUT is the output stream to print. FRAME_LOW is the
1483 beginning of the slice of frames to print, and FRAME_HIGH is the
1484 upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error,
1485 or EXT_LANG_BT_COMPLETED on success. */
1487 enum ext_lang_bt_status
1488 gdbpy_apply_frame_filter (const struct extension_language_defn
*extlang
,
1489 struct frame_info
*frame
, int flags
,
1490 enum ext_lang_frame_args args_type
,
1491 struct ui_out
*out
, int frame_low
, int frame_high
)
1493 struct gdbarch
*gdbarch
= NULL
;
1494 enum ext_lang_bt_status success
= EXT_LANG_BT_ERROR
;
1496 if (!gdb_python_initialized
)
1497 return EXT_LANG_BT_NO_FILTERS
;
1501 gdbarch
= get_frame_arch (frame
);
1503 CATCH (except
, RETURN_MASK_ALL
)
1505 /* Let gdb try to print the stack trace. */
1506 return EXT_LANG_BT_NO_FILTERS
;
1510 gdbpy_enter
enter_py (gdbarch
, current_language
);
1512 gdbpy_ref
iterable (bootstrap_python_frame_filters (frame
, frame_low
,
1515 if (iterable
== NULL
)
1517 /* Normally if there is an error GDB prints the exception,
1518 abandons the backtrace and exits. The user can then call "bt
1519 no-filters", and get a default backtrace (it would be
1520 confusing to automatically start a standard backtrace halfway
1521 through a Python filtered backtrace). However in the case
1522 where GDB cannot initialize the frame filters (most likely
1523 due to incorrect auto-load paths), GDB has printed nothing.
1524 In this case it is OK to print the default backtrace after
1525 printing the error message. GDB returns EXT_LANG_BT_NO_FILTERS
1526 here to signify there are no filters after printing the
1527 initialization error. This return code will trigger a
1528 default backtrace. */
1530 gdbpy_print_stack ();
1531 return EXT_LANG_BT_NO_FILTERS
;
1534 /* If iterable is None, then there are no frame filters registered.
1535 If this is the case, defer to default GDB printing routines in MI
1537 if (iterable
== Py_None
)
1538 return EXT_LANG_BT_NO_FILTERS
;
1540 htab_up
levels_printed (htab_create (20,
1547 gdbpy_ref
item (PyIter_Next (iterable
.get ()));
1551 if (PyErr_Occurred ())
1553 gdbpy_print_stack ();
1554 return EXT_LANG_BT_ERROR
;
1559 success
= py_print_frame (item
.get (), flags
, args_type
, out
, 0,
1560 levels_printed
.get ());
1562 /* Do not exit on error printing a single frame. Print the
1563 error and continue with other frames. */
1564 if (success
== EXT_LANG_BT_ERROR
)
1565 gdbpy_print_stack ();