1 /* Python frame filters
3 Copyright (C) 2013-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "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
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 = make_cleanup (null_cleanup, NULL);
1018 struct value_print_options opts;
1019 PyObject *py_inf_frame, *elided;
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)
1038 frame = frame_object_to_frame_info (py_inf_frame);;
1040 Py_DECREF (py_inf_frame);
1045 TRY_CATCH (except, RETURN_MASK_ALL)
1047 gdbarch = get_frame_arch (frame);
1049 if (except.reason < 0)
1051 gdbpy_convert_exception (except);
1056 /* stack-list-variables. */
1057 if (print_locals && print_args && ! print_frame_info)
1059 if (py_mi_print_variables (filter, out, &opts,
1060 args_type, frame) == EXT_LANG_BT_ERROR)
1064 do_cleanups (cleanup_stack);
1065 return EXT_LANG_BT_COMPLETED;
1069 /* -stack-list-locals does not require a
1070 wrapping frame attribute. */
1071 if (print_frame_info || (print_args && ! print_locals))
1072 make_cleanup_ui_out_tuple_begin_end (out, "frame");
1074 if (print_frame_info)
1076 /* Elided frames are also printed with this function (recursively)
1077 and are printed with indention. */
1080 TRY_CATCH (except, RETURN_MASK_ALL)
1082 ui_out_spaces (out, indent*4);
1084 if (except.reason < 0)
1086 gdbpy_convert_exception (except);
1091 /* The address is required for frame annotations, and also for
1092 address printing. */
1093 if (PyObject_HasAttrString (filter, "address"))
1095 PyObject *paddr = PyObject_CallMethod (filter, "address", NULL);
1098 if (paddr != Py_None)
1100 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);
1147 if (print_frame_info)
1149 /* Print address to the address field. If an address is not provided,
1151 if (opts.addressprint && has_addr)
1153 TRY_CATCH (except, RETURN_MASK_ALL)
1155 annotate_frame_address ();
1156 ui_out_field_core_addr (out, "addr", gdbarch, address);
1157 annotate_frame_address_end ();
1158 ui_out_text (out, " in ");
1160 if (except.reason < 0)
1162 gdbpy_convert_exception (except);
1167 /* Print frame function name. */
1168 if (PyObject_HasAttrString (filter, "function"))
1170 PyObject *py_func = PyObject_CallMethod (filter, "function", NULL);
1172 if (py_func != NULL)
1174 const char *function = NULL;
1176 if (gdbpy_is_string (py_func))
1178 char *function_to_free = NULL;
1180 function = function_to_free =
1181 python_string_to_host_string (py_func);
1183 if (function == NULL)
1185 Py_DECREF (py_func);
1188 make_cleanup (xfree, function_to_free);
1190 else if (PyLong_Check (py_func))
1192 CORE_ADDR addr = PyLong_AsUnsignedLongLong (py_func);
1193 struct bound_minimal_symbol msymbol;
1195 if (PyErr_Occurred ())
1198 msymbol = lookup_minimal_symbol_by_pc (addr);
1199 if (msymbol.minsym != NULL)
1200 function = MSYMBOL_PRINT_NAME (msymbol.minsym);
1202 else if (py_func != Py_None)
1204 PyErr_SetString (PyExc_RuntimeError,
1205 _("FrameDecorator.function: expecting a " \
1206 "String, integer or None."));
1207 Py_DECREF (py_func);
1212 TRY_CATCH (except, RETURN_MASK_ALL)
1214 annotate_frame_function_name ();
1215 if (function == NULL)
1216 ui_out_field_skip (out, "func");
1218 ui_out_field_string (out, "func", function);
1220 if (except.reason < 0)
1222 Py_DECREF (py_func);
1223 gdbpy_convert_exception (except);
1226 Py_DECREF (py_func);
1234 /* Frame arguments. Check the result, and error if something went
1238 if (py_print_args (filter, out, args_type, frame) == EXT_LANG_BT_ERROR)
1242 /* File name/source/line number information. */
1243 if (print_frame_info)
1245 TRY_CATCH (except, RETURN_MASK_ALL)
1247 annotate_frame_source_begin ();
1249 if (except.reason < 0)
1251 gdbpy_convert_exception (except);
1255 if (PyObject_HasAttrString (filter, "filename"))
1257 PyObject *py_fn = PyObject_CallMethod (filter, "filename",
1261 if (py_fn != Py_None)
1263 char *filename = python_string_to_host_string (py_fn);
1265 if (filename == NULL)
1271 make_cleanup (xfree, filename);
1272 TRY_CATCH (except, RETURN_MASK_ALL)
1274 ui_out_wrap_hint (out, " ");
1275 ui_out_text (out, " at ");
1276 annotate_frame_source_file ();
1277 ui_out_field_string (out, "file", filename);
1278 annotate_frame_source_file_end ();
1280 if (except.reason < 0)
1283 gdbpy_convert_exception (except);
1293 if (PyObject_HasAttrString (filter, "line"))
1295 PyObject *py_line = PyObject_CallMethod (filter, "line", NULL);
1298 if (py_line != NULL)
1300 if (py_line != Py_None)
1302 line = PyLong_AsLong (py_line);
1303 TRY_CATCH (except, RETURN_MASK_ALL)
1305 ui_out_text (out, ":");
1306 annotate_frame_source_line ();
1307 ui_out_field_int (out, "line", line);
1309 if (except.reason < 0)
1311 Py_DECREF (py_line);
1312 gdbpy_convert_exception (except);
1316 Py_DECREF (py_line);
1323 /* For MI we need to deal with the "children" list population of
1324 elided frames, so if MI output detected do not send newline. */
1325 if (! ui_out_is_mi_like_p (out))
1327 TRY_CATCH (except, RETURN_MASK_ALL)
1329 annotate_frame_end ();
1330 ui_out_text (out, "\n");
1332 if (except.reason < 0)
1334 gdbpy_convert_exception (except);
1341 if (py_print_locals (filter, out, args_type, indent,
1342 frame) == EXT_LANG_BT_ERROR)
1346 /* Finally recursively print elided frames, if any. */
1347 elided = get_py_iter_from_func (filter, "elided");
1351 make_cleanup_py_decref (elided);
1352 if (elided != Py_None)
1356 make_cleanup_ui_out_list_begin_end (out, "children");
1358 if (! ui_out_is_mi_like_p (out))
1361 while ((item = PyIter_Next (elided)))
1363 enum ext_lang_bt_status success = py_print_frame (item, flags,
1368 if (success == EXT_LANG_BT_ERROR)
1376 if (item == NULL && PyErr_Occurred ())
1381 do_cleanups (cleanup_stack);
1382 return EXT_LANG_BT_COMPLETED;
1385 do_cleanups (cleanup_stack);
1386 return EXT_LANG_BT_ERROR;
1389 /* Helper function to initiate frame filter invocation at starting
1393 bootstrap_python_frame_filters (struct frame_info *frame,
1394 int frame_low, int frame_high)
1396 struct cleanup *cleanups =
1397 make_cleanup (null_cleanup, NULL);
1398 PyObject *module, *sort_func, *iterable, *frame_obj, *iterator;
1399 PyObject *py_frame_low, *py_frame_high;
1401 frame_obj = frame_info_to_frame_object (frame);
1402 if (frame_obj == NULL)
1404 make_cleanup_py_decref (frame_obj);
1406 module = PyImport_ImportModule ("gdb.frames");
1409 make_cleanup_py_decref (module);
1411 sort_func = PyObject_GetAttrString (module, "execute_frame_filters");
1412 if (sort_func == NULL)
1414 make_cleanup_py_decref (sort_func);
1416 py_frame_low = PyInt_FromLong (frame_low);
1417 if (py_frame_low == NULL)
1419 make_cleanup_py_decref (py_frame_low);
1421 py_frame_high = PyInt_FromLong (frame_high);
1422 if (py_frame_high == NULL)
1424 make_cleanup_py_decref (py_frame_high);
1426 iterable = PyObject_CallFunctionObjArgs (sort_func, frame_obj,
1430 if (iterable == NULL)
1433 do_cleanups (cleanups);
1435 if (iterable != Py_None)
1437 iterator = PyObject_GetIter (iterable);
1438 Py_DECREF (iterable);
1448 do_cleanups (cleanups);
1452 /* This is the only publicly exported function in this file. FRAME
1453 is the source frame to start frame-filter invocation. FLAGS is an
1454 integer holding the flags for printing. The following elements of
1455 the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
1456 PRINT_LEVEL is a flag indicating whether to print the frame's
1457 relative level in the output. PRINT_FRAME_INFO is a flag that
1458 indicates whether this function should print the frame
1459 information, PRINT_ARGS is a flag that indicates whether to print
1460 frame arguments, and PRINT_LOCALS, likewise, with frame local
1461 variables. ARGS_TYPE is an enumerator describing the argument
1462 format, OUT is the output stream to print. FRAME_LOW is the
1463 beginning of the slice of frames to print, and FRAME_HIGH is the
1464 upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error,
1465 or EXT_LANG_BT_COMPLETED on success. */
1467 enum ext_lang_bt_status
1468 gdbpy_apply_frame_filter (const struct extension_language_defn *extlang,
1469 struct frame_info *frame, int flags,
1470 enum ext_lang_frame_args args_type,
1471 struct ui_out *out, int frame_low, int frame_high)
1473 struct gdbarch *gdbarch = NULL;
1474 struct cleanup *cleanups;
1475 enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
1477 volatile struct gdb_exception except;
1479 htab_t levels_printed;
1481 if (!gdb_python_initialized)
1482 return EXT_LANG_BT_NO_FILTERS;
1484 TRY_CATCH (except, RETURN_MASK_ALL)
1486 gdbarch = get_frame_arch (frame);
1488 if (except.reason < 0)
1490 /* Let gdb try to print the stack trace. */
1491 return EXT_LANG_BT_NO_FILTERS;
1494 cleanups = ensure_python_env (gdbarch, current_language);
1496 iterable = bootstrap_python_frame_filters (frame, frame_low, frame_high);
1498 if (iterable == NULL)
1500 /* Normally if there is an error GDB prints the exception,
1501 abandons the backtrace and exits. The user can then call "bt
1502 no-filters", and get a default backtrace (it would be
1503 confusing to automatically start a standard backtrace halfway
1504 through a Python filtered backtrace). However in the case
1505 where GDB cannot initialize the frame filters (most likely
1506 due to incorrect auto-load paths), GDB has printed nothing.
1507 In this case it is OK to print the default backtrace after
1508 printing the error message. GDB returns EXT_LANG_BT_NO_FILTERS
1509 here to signify there are no filters after printing the
1510 initialization error. This return code will trigger a
1511 default backtrace. */
1513 gdbpy_print_stack ();
1514 do_cleanups (cleanups);
1515 return EXT_LANG_BT_NO_FILTERS;
1518 /* If iterable is None, then there are no frame filters registered.
1519 If this is the case, defer to default GDB printing routines in MI
1521 make_cleanup_py_decref (iterable);
1522 if (iterable == Py_None)
1524 success = EXT_LANG_BT_NO_FILTERS;
1528 levels_printed = htab_create (20,
1532 make_cleanup_htab_delete (levels_printed);
1534 while ((item = PyIter_Next (iterable)))
1536 success = py_print_frame (item, flags, args_type, out, 0,
1539 /* Do not exit on error printing a single frame. Print the
1540 error and continue with other frames. */
1541 if (success == EXT_LANG_BT_ERROR)
1542 gdbpy_print_stack ();
1547 if (item == NULL && PyErr_Occurred ())
1551 do_cleanups (cleanups);
1554 /* Exit and abandon backtrace on error, printing the exception that
1557 gdbpy_print_stack ();
1558 do_cleanups (cleanups);
1559 return EXT_LANG_BT_ERROR;