framefilter quit: Make it exception safe
[deliverable/binutils-gdb.git] / gdb / python / py-framefilter.c
1 /* Python frame filters
2
3 Copyright (C) 2013-2015 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "objfiles.h"
22 #include "symtab.h"
23 #include "language.h"
24 #include "arch-utils.h"
25 #include "python.h"
26 #include "ui-out.h"
27 #include "valprint.h"
28 #include "annotate.h"
29 #include "hashtab.h"
30 #include "demangle.h"
31 #include "mi/mi-cmds.h"
32 #include "python-internal.h"
33
34 enum mi_print_types
35 {
36 MI_PRINT_ARGS,
37 MI_PRINT_LOCALS
38 };
39
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. */
52
53 static enum ext_lang_bt_status
54 extract_sym (PyObject *obj, char **name, struct symbol **sym,
55 const struct language_defn **language)
56 {
57 PyObject *result = PyObject_CallMethod (obj, "symbol", NULL);
58
59 if (result == NULL)
60 return EXT_LANG_BT_ERROR;
61
62 /* For 'symbol' callback, the function can return a symbol or a
63 string. */
64 if (gdbpy_is_string (result))
65 {
66 *name = python_string_to_host_string (result);
67 Py_DECREF (result);
68
69 if (*name == NULL)
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
75 python_language. */
76 *language = python_language;
77 *sym = NULL;
78 }
79 else
80 {
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);
84
85 Py_DECREF (result);
86
87 if (*sym == NULL)
88 {
89 PyErr_SetString (PyExc_RuntimeError,
90 _("Unexpected value. Expecting a "
91 "gdb.Symbol or a Python string."));
92 return EXT_LANG_BT_ERROR;
93 }
94
95 /* Duplicate the symbol name, so the caller has consistency
96 in garbage collection. */
97 *name = xstrdup (SYMBOL_PRINT_NAME (*sym));
98
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));
104 else
105 *language = current_language;
106 }
107
108 return EXT_LANG_BT_OK;
109 }
110
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
118 success. */
119
120 static enum ext_lang_bt_status
121 extract_value (PyObject *obj, struct value **value)
122 {
123 if (PyObject_HasAttrString (obj, "value"))
124 {
125 PyObject *vresult = PyObject_CallMethod (obj, "value", NULL);
126
127 if (vresult == NULL)
128 return EXT_LANG_BT_ERROR;
129
130 /* The Python code has returned 'None' for a value, so we set
131 value to NULL. This flags that GDB should read the
132 value. */
133 if (vresult == Py_None)
134 {
135 Py_DECREF (vresult);
136 *value = NULL;
137 return EXT_LANG_BT_OK;
138 }
139 else
140 {
141 *value = convert_value_from_python (vresult);
142 Py_DECREF (vresult);
143
144 if (*value == NULL)
145 return EXT_LANG_BT_ERROR;
146
147 return EXT_LANG_BT_OK;
148 }
149 }
150 else
151 *value = NULL;
152
153 return EXT_LANG_BT_OK;
154 }
155
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. */
160 static int
161 mi_should_print (struct symbol *sym, enum mi_print_types type)
162 {
163 int print_me = 0;
164
165 switch (SYMBOL_CLASS (sym))
166 {
167 default:
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 */
176 print_me = 0;
177 break;
178
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);
188 else
189 print_me = SYMBOL_IS_ARGUMENT (sym);
190 }
191 return print_me;
192 }
193
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
199 success. */
200
201 static enum ext_lang_bt_status
202 py_print_type (struct ui_out *out, struct value *val)
203 {
204 volatile struct gdb_exception except;
205
206 TRY_CATCH (except, RETURN_MASK_ALL)
207 {
208 struct type *type;
209 struct ui_file *stb;
210 struct cleanup *cleanup;
211
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);
218 }
219 if (except.reason < 0)
220 {
221 gdbpy_convert_exception (except);
222 return EXT_LANG_BT_ERROR;
223 }
224
225 return EXT_LANG_BT_OK;
226 }
227
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
235 success. */
236
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,
240 int indent,
241 enum ext_lang_frame_args args_type,
242 const struct language_defn *language)
243 {
244 int should_print = 0;
245 volatile struct gdb_exception except;
246 int local_indent = (4 * indent);
247
248 /* Never set an indent level for common_val_print if MI. */
249 if (ui_out_is_mi_like_p (out))
250 local_indent = 0;
251
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)
257 {
258 struct type *type = NULL;
259
260 TRY_CATCH (except, RETURN_MASK_ALL)
261 {
262 type = check_typedef (value_type (val));
263 }
264 if (except.reason < 0)
265 {
266 gdbpy_convert_exception (except);
267 return EXT_LANG_BT_ERROR;
268 }
269
270 if (args_type == MI_PRINT_ALL_VALUES)
271 should_print = 1;
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)
276 should_print = 1;
277 }
278 else if (args_type != NO_VALUES)
279 should_print = 1;
280
281 if (should_print)
282 {
283 TRY_CATCH (except, RETURN_MASK_ALL)
284 {
285 struct ui_file *stb;
286 struct cleanup *cleanup;
287
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);
293 }
294 if (except.reason < 0)
295 {
296 gdbpy_convert_exception (except);
297 return EXT_LANG_BT_ERROR;
298 }
299 }
300
301 return EXT_LANG_BT_OK;
302 }
303
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. */
310
311 static PyObject *
312 get_py_iter_from_func (PyObject *filter, char *func)
313 {
314 if (PyObject_HasAttrString (filter, func))
315 {
316 PyObject *result = PyObject_CallMethod (filter, func, NULL);
317
318 if (result != NULL)
319 {
320 if (result == Py_None)
321 {
322 return result;
323 }
324 else
325 {
326 PyObject *iterator = PyObject_GetIter (result);
327
328 Py_DECREF (result);
329 return iterator;
330 }
331 }
332 }
333 else
334 Py_RETURN_NONE;
335
336 return NULL;
337 }
338
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. */
354
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,
359 struct value *fv,
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)
364 {
365 struct value *val;
366 volatile struct gdb_exception except;
367 enum ext_lang_bt_status retval = EXT_LANG_BT_OK;
368
369 if (fa != NULL)
370 {
371 if (fa->val == NULL && fa->error == NULL)
372 return EXT_LANG_BT_OK;
373 language = language_def (SYMBOL_LANGUAGE (fa->sym));
374 val = fa->val;
375 }
376 else
377 val = fv;
378
379 TRY_CATCH (except, RETURN_MASK_ALL)
380 {
381 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
382
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))
390 {
391 if (print_args_field || args_type != NO_VALUES)
392 make_cleanup_ui_out_tuple_begin_end (out, NULL);
393 }
394
395 annotate_arg_begin ();
396
397 /* If frame argument is populated, check for entry-values and the
398 entry value options. */
399 if (fa != NULL)
400 {
401 struct ui_file *stb;
402
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)
409 {
410 fputs_filtered ("=", stb);
411
412 fprintf_symbol_filtered (stb, SYMBOL_PRINT_NAME (fa->sym),
413 SYMBOL_LANGUAGE (fa->sym),
414 DMGL_PARAMS | DMGL_ANSI);
415 }
416 if (fa->entry_kind == print_entry_values_only
417 || fa->entry_kind == print_entry_values_compact)
418 {
419 fputs_filtered ("@entry", stb);
420 }
421 ui_out_field_stream (out, "name", stb);
422 }
423 else
424 /* Otherwise, just output the name. */
425 ui_out_field_string (out, "name", sym_name);
426
427 annotate_arg_name_end ();
428
429 if (! ui_out_is_mi_like_p (out))
430 ui_out_text (out, "=");
431
432 if (print_args_field)
433 ui_out_field_int (out, "arg", 1);
434
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
437 types. */
438 if (args_type == MI_PRINT_SIMPLE_VALUES && val != NULL)
439 {
440 if (py_print_type (out, val) == EXT_LANG_BT_ERROR)
441 {
442 retval = EXT_LANG_BT_ERROR;
443 do_cleanups (cleanups);
444 continue;
445 }
446 }
447
448 if (val != NULL)
449 annotate_arg_value (value_type (val));
450
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", "...");
455 else
456 {
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)
460 {
461 if (val == NULL)
462 {
463 gdb_assert (fa != NULL && fa->error != NULL);
464 ui_out_field_fmt (out, "value",
465 _("<error reading variable: %s>"),
466 fa->error);
467 }
468 else if (py_print_value (out, val, opts, 0, args_type, language)
469 == EXT_LANG_BT_ERROR)
470 retval = EXT_LANG_BT_ERROR;
471 }
472 }
473
474 do_cleanups (cleanups);
475 }
476 if (except.reason < 0)
477 gdbpy_convert_exception (except);
478
479 return retval;
480 }
481
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
491 success. */
492
493 static enum ext_lang_bt_status
494 enumerate_args (PyObject *iter,
495 struct ui_out *out,
496 enum ext_lang_frame_args args_type,
497 int print_args_field,
498 struct frame_info *frame)
499 {
500 PyObject *item;
501 struct value_print_options opts;
502 volatile struct gdb_exception except;
503
504 get_user_print_options (&opts);
505
506 if (args_type == CLI_SCALAR_VALUES)
507 {
508 /* True in "summary" mode, false otherwise. */
509 opts.summary = 1;
510 }
511
512 opts.deref_ref = 1;
513
514 TRY_CATCH (except, RETURN_MASK_ALL)
515 {
516 annotate_frame_args ();
517 }
518 if (except.reason < 0)
519 {
520 gdbpy_convert_exception (except);
521 goto error;
522 }
523
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 ())
530 goto error;
531
532 while (item)
533 {
534 const struct language_defn *language;
535 char *sym_name;
536 struct symbol *sym;
537 struct value *val;
538 enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
539
540 success = extract_sym (item, &sym_name, &sym, &language);
541 if (success == EXT_LANG_BT_ERROR)
542 {
543 Py_DECREF (item);
544 goto error;
545 }
546
547 success = extract_value (item, &val);
548 if (success == EXT_LANG_BT_ERROR)
549 {
550 xfree (sym_name);
551 Py_DECREF (item);
552 goto error;
553 }
554
555 Py_DECREF (item);
556 item = NULL;
557
558 if (sym && ui_out_is_mi_like_p (out)
559 && ! mi_should_print (sym, MI_PRINT_ARGS))
560 {
561 xfree (sym_name);
562 continue;
563 }
564
565 /* If the object did not provide a value, read it using
566 read_frame_args and account for entry values, if any. */
567 if (val == NULL)
568 {
569 struct frame_arg arg, entryarg;
570
571 /* If there is no value, and also no symbol, set error and
572 exit. */
573 if (sym == NULL)
574 {
575 PyErr_SetString (PyExc_RuntimeError,
576 _("No symbol or value provided."));
577 xfree (sym_name);
578 goto error;
579 }
580
581 TRY_CATCH (except, RETURN_MASK_ALL)
582 {
583 read_frame_arg (sym, frame, &arg, &entryarg);
584 }
585 if (except.reason < 0)
586 {
587 xfree (sym_name);
588 gdbpy_convert_exception (except);
589 goto error;
590 }
591
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. */
595
596 if (arg.entry_kind != print_entry_values_only)
597 {
598 if (py_print_single_arg (out, NULL, &arg,
599 NULL, &opts,
600 args_type,
601 print_args_field,
602 NULL) == EXT_LANG_BT_ERROR)
603 {
604 xfree (arg.error);
605 xfree (entryarg.error);
606 xfree (sym_name);
607 goto error;
608 }
609 }
610
611 if (entryarg.entry_kind != print_entry_values_no)
612 {
613 if (arg.entry_kind != print_entry_values_only)
614 {
615 TRY_CATCH (except, RETURN_MASK_ALL)
616 {
617 ui_out_text (out, ", ");
618 ui_out_wrap_hint (out, " ");
619 }
620 if (except.reason < 0)
621 {
622 xfree (arg.error);
623 xfree (entryarg.error);
624 xfree (sym_name);
625 gdbpy_convert_exception (except);
626 goto error;
627 }
628 }
629
630 if (py_print_single_arg (out, NULL, &entryarg, NULL, &opts,
631 args_type, print_args_field, NULL)
632 == EXT_LANG_BT_ERROR)
633 {
634 xfree (arg.error);
635 xfree (entryarg.error);
636 xfree (sym_name);
637 goto error;
638 }
639 }
640
641 xfree (arg.error);
642 xfree (entryarg.error);
643 }
644 else
645 {
646 /* If the object has provided a value, we just print that. */
647 if (val != NULL)
648 {
649 if (py_print_single_arg (out, sym_name, NULL, val, &opts,
650 args_type, print_args_field,
651 language) == EXT_LANG_BT_ERROR)
652 {
653 xfree (sym_name);
654 goto error;
655 }
656 }
657 }
658
659 xfree (sym_name);
660
661 /* Collect the next item from the iterator. If
662 this is the last item, do not print the
663 comma. */
664 item = PyIter_Next (iter);
665 if (item != NULL)
666 {
667 TRY_CATCH (except, RETURN_MASK_ALL)
668 {
669 ui_out_text (out, ", ");
670 }
671 if (except.reason < 0)
672 {
673 Py_DECREF (item);
674 gdbpy_convert_exception (except);
675 goto error;
676 }
677 }
678 else if (PyErr_Occurred ())
679 goto error;
680
681 TRY_CATCH (except, RETURN_MASK_ALL)
682 {
683 annotate_arg_end ();
684 }
685 if (except.reason < 0)
686 {
687 Py_DECREF (item);
688 gdbpy_convert_exception (except);
689 goto error;
690 }
691 }
692
693 return EXT_LANG_BT_OK;
694
695 error:
696 return EXT_LANG_BT_ERROR;
697 }
698
699
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. */
710
711 static enum ext_lang_bt_status
712 enumerate_locals (PyObject *iter,
713 struct ui_out *out,
714 int indent,
715 enum ext_lang_frame_args args_type,
716 int print_args_field,
717 struct frame_info *frame)
718 {
719 PyObject *item;
720 struct value_print_options opts;
721
722 get_user_print_options (&opts);
723 opts.deref_ref = 1;
724
725 while ((item = PyIter_Next (iter)))
726 {
727 const struct language_defn *language;
728 char *sym_name;
729 struct value *val;
730 enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
731 struct symbol *sym;
732 volatile struct gdb_exception except;
733 int local_indent = 8 + (8 * indent);
734 struct cleanup *locals_cleanups;
735
736 locals_cleanups = make_cleanup_py_decref (item);
737
738 success = extract_sym (item, &sym_name, &sym, &language);
739 if (success == EXT_LANG_BT_ERROR)
740 {
741 do_cleanups (locals_cleanups);
742 goto error;
743 }
744
745 make_cleanup (xfree, sym_name);
746
747 success = extract_value (item, &val);
748 if (success == EXT_LANG_BT_ERROR)
749 {
750 do_cleanups (locals_cleanups);
751 goto error;
752 }
753
754 if (sym != NULL && ui_out_is_mi_like_p (out)
755 && ! mi_should_print (sym, MI_PRINT_LOCALS))
756 {
757 do_cleanups (locals_cleanups);
758 continue;
759 }
760
761 /* If the object did not provide a value, read it. */
762 if (val == NULL)
763 {
764 TRY_CATCH (except, RETURN_MASK_ALL)
765 {
766 val = read_var_value (sym, frame);
767 }
768 if (except.reason < 0)
769 {
770 gdbpy_convert_exception (except);
771 do_cleanups (locals_cleanups);
772 goto error;
773 }
774 }
775
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))
780 {
781 if (print_args_field || args_type != NO_VALUES)
782 make_cleanup_ui_out_tuple_begin_end (out, NULL);
783 }
784 TRY_CATCH (except, RETURN_MASK_ALL)
785 {
786 if (! ui_out_is_mi_like_p (out))
787 {
788 /* If the output is not MI we indent locals. */
789 ui_out_spaces (out, local_indent);
790 }
791
792 ui_out_field_string (out, "name", sym_name);
793
794 if (! ui_out_is_mi_like_p (out))
795 ui_out_text (out, " = ");
796 }
797 if (except.reason < 0)
798 {
799 gdbpy_convert_exception (except);
800 do_cleanups (locals_cleanups);
801 goto error;
802 }
803
804 if (args_type == MI_PRINT_SIMPLE_VALUES)
805 {
806 if (py_print_type (out, val) == EXT_LANG_BT_ERROR)
807 {
808 do_cleanups (locals_cleanups);
809 goto error;
810 }
811 }
812
813 /* CLI always prints values for locals. MI uses the
814 simple/no/all system. */
815 if (! ui_out_is_mi_like_p (out))
816 {
817 int val_indent = (indent + 1) * 4;
818
819 if (py_print_value (out, val, &opts, val_indent, args_type,
820 language) == EXT_LANG_BT_ERROR)
821 {
822 do_cleanups (locals_cleanups);
823 goto error;
824 }
825 }
826 else
827 {
828 if (args_type != NO_VALUES)
829 {
830 if (py_print_value (out, val, &opts, 0, args_type,
831 language) == EXT_LANG_BT_ERROR)
832 {
833 do_cleanups (locals_cleanups);
834 goto error;
835 }
836 }
837 }
838
839 do_cleanups (locals_cleanups);
840
841 TRY_CATCH (except, RETURN_MASK_ALL)
842 {
843 ui_out_text (out, "\n");
844 }
845 if (except.reason < 0)
846 {
847 gdbpy_convert_exception (except);
848 goto error;
849 }
850 }
851
852 if (item == NULL && PyErr_Occurred ())
853 goto error;
854
855 return EXT_LANG_BT_OK;
856
857 error:
858 return EXT_LANG_BT_ERROR;
859 }
860
861 /* Helper function for -stack-list-variables. Returns EXT_LANG_BT_ERROR on
862 error, or EXT_LANG_BT_OK on success. */
863
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)
869 {
870 struct cleanup *old_chain;
871 PyObject *args_iter;
872 PyObject *locals_iter;
873
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)
877 goto error;
878
879 locals_iter = get_py_iter_from_func (filter, "frame_locals");
880 if (locals_iter == NULL)
881 goto error;
882
883 make_cleanup_py_decref (locals_iter);
884 make_cleanup_ui_out_list_begin_end (out, "variables");
885
886 if (args_iter != Py_None)
887 if (enumerate_args (args_iter, out, args_type, 1, frame)
888 == EXT_LANG_BT_ERROR)
889 goto error;
890
891 if (locals_iter != Py_None)
892 if (enumerate_locals (locals_iter, out, 1, args_type, 1, frame)
893 == EXT_LANG_BT_ERROR)
894 goto error;
895
896 do_cleanups (old_chain);
897 return EXT_LANG_BT_OK;
898
899 error:
900 do_cleanups (old_chain);
901 return EXT_LANG_BT_ERROR;
902 }
903
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. */
907
908 static enum ext_lang_bt_status
909 py_print_locals (PyObject *filter,
910 struct ui_out *out,
911 enum ext_lang_frame_args args_type,
912 int indent,
913 struct frame_info *frame)
914 {
915 PyObject *locals_iter = get_py_iter_from_func (filter,
916 "frame_locals");
917 struct cleanup *old_chain = make_cleanup_py_xdecref (locals_iter);
918
919 if (locals_iter == NULL)
920 goto locals_error;
921
922 make_cleanup_ui_out_list_begin_end (out, "locals");
923
924 if (locals_iter != Py_None)
925 if (enumerate_locals (locals_iter, out, indent, args_type,
926 0, frame) == EXT_LANG_BT_ERROR)
927 goto locals_error;
928
929 do_cleanups (old_chain);
930 return EXT_LANG_BT_OK;
931
932 locals_error:
933 do_cleanups (old_chain);
934 return EXT_LANG_BT_ERROR;
935 }
936
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. */
941
942 static enum ext_lang_bt_status
943 py_print_args (PyObject *filter,
944 struct ui_out *out,
945 enum ext_lang_frame_args args_type,
946 struct frame_info *frame)
947 {
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;
951
952 if (args_iter == NULL)
953 goto args_error;
954
955 make_cleanup_ui_out_list_begin_end (out, "args");
956
957 TRY_CATCH (except, RETURN_MASK_ALL)
958 {
959 annotate_frame_args ();
960 if (! ui_out_is_mi_like_p (out))
961 ui_out_text (out, " (");
962 }
963 if (except.reason < 0)
964 {
965 gdbpy_convert_exception (except);
966 goto args_error;
967 }
968
969 if (args_iter != Py_None)
970 if (enumerate_args (args_iter, out, args_type, 0, frame)
971 == EXT_LANG_BT_ERROR)
972 goto args_error;
973
974 TRY_CATCH (except, RETURN_MASK_ALL)
975 {
976 if (! ui_out_is_mi_like_p (out))
977 ui_out_text (out, ")");
978 }
979 if (except.reason < 0)
980 {
981 gdbpy_convert_exception (except);
982 goto args_error;
983 }
984
985 do_cleanups (old_chain);
986 return EXT_LANG_BT_OK;
987
988 args_error:
989 do_cleanups (old_chain);
990 return EXT_LANG_BT_ERROR;
991 }
992
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. */
1007
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)
1012 {
1013 int has_addr = 0;
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;
1022
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;
1028
1029 get_user_print_options (&opts);
1030
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;
1037
1038 frame = frame_object_to_frame_info (py_inf_frame);;
1039
1040 Py_DECREF (py_inf_frame);
1041
1042 if (frame == NULL)
1043 return EXT_LANG_BT_ERROR;
1044
1045 TRY_CATCH (except, RETURN_MASK_ALL)
1046 {
1047 gdbarch = get_frame_arch (frame);
1048 }
1049 if (except.reason < 0)
1050 {
1051 gdbpy_convert_exception (except);
1052 return EXT_LANG_BT_ERROR;
1053 }
1054
1055 /* stack-list-variables. */
1056 if (print_locals && print_args && ! print_frame_info)
1057 {
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;
1062 }
1063
1064 cleanup_stack = make_cleanup (null_cleanup, NULL);
1065
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");
1070
1071 if (print_frame_info)
1072 {
1073 /* Elided frames are also printed with this function (recursively)
1074 and are printed with indention. */
1075 if (indent > 0)
1076 {
1077 TRY_CATCH (except, RETURN_MASK_ALL)
1078 {
1079 ui_out_spaces (out, indent*4);
1080 }
1081 if (except.reason < 0)
1082 {
1083 gdbpy_convert_exception (except);
1084 do_cleanups (cleanup_stack);
1085 return EXT_LANG_BT_ERROR;
1086 }
1087 }
1088
1089 /* The address is required for frame annotations, and also for
1090 address printing. */
1091 if (PyObject_HasAttrString (filter, "address"))
1092 {
1093 PyObject *paddr = PyObject_CallMethod (filter, "address", NULL);
1094
1095 if (paddr == NULL)
1096 {
1097 do_cleanups (cleanup_stack);
1098 return EXT_LANG_BT_ERROR;
1099 }
1100
1101 if (paddr != Py_None)
1102 {
1103 address = PyLong_AsLong (paddr);
1104 has_addr = 1;
1105 }
1106 Py_DECREF (paddr);
1107 }
1108 }
1109
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)
1113 {
1114 struct frame_info **slot;
1115 int level;
1116 volatile struct gdb_exception except;
1117
1118 slot = (struct frame_info **) htab_find_slot (levels_printed,
1119 frame, INSERT);
1120 TRY_CATCH (except, RETURN_MASK_ALL)
1121 {
1122 level = frame_relative_level (frame);
1123
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. */
1128 if (*slot == frame)
1129 ui_out_field_skip (out, "level");
1130 else
1131 {
1132 *slot = frame;
1133 annotate_frame_begin (print_level ? level : 0,
1134 gdbarch, address);
1135 ui_out_text (out, "#");
1136 ui_out_field_fmt_int (out, 2, ui_left, "level",
1137 level);
1138 }
1139 }
1140 if (except.reason < 0)
1141 {
1142 gdbpy_convert_exception (except);
1143 do_cleanups (cleanup_stack);
1144 return EXT_LANG_BT_ERROR;
1145 }
1146 }
1147
1148 if (print_frame_info)
1149 {
1150 /* Print address to the address field. If an address is not provided,
1151 print nothing. */
1152 if (opts.addressprint && has_addr)
1153 {
1154 TRY_CATCH (except, RETURN_MASK_ALL)
1155 {
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 ");
1160 }
1161 if (except.reason < 0)
1162 {
1163 gdbpy_convert_exception (except);
1164 do_cleanups (cleanup_stack);
1165 return EXT_LANG_BT_ERROR;
1166 }
1167 }
1168
1169 /* Print frame function name. */
1170 if (PyObject_HasAttrString (filter, "function"))
1171 {
1172 PyObject *py_func = PyObject_CallMethod (filter, "function", NULL);
1173 struct cleanup *py_func_cleanup;
1174 const char *function = NULL;
1175
1176 if (py_func == NULL)
1177 {
1178 do_cleanups (cleanup_stack);
1179 return EXT_LANG_BT_ERROR;
1180 }
1181 py_func_cleanup = make_cleanup_py_decref (py_func);
1182
1183 if (gdbpy_is_string (py_func))
1184 {
1185 char *function_to_free;
1186
1187 function = function_to_free =
1188 python_string_to_host_string (py_func);
1189
1190 if (function == NULL)
1191 {
1192 do_cleanups (cleanup_stack);
1193 return EXT_LANG_BT_ERROR;
1194 }
1195 make_cleanup (xfree, function_to_free);
1196 }
1197 else if (PyLong_Check (py_func))
1198 {
1199 CORE_ADDR addr = PyLong_AsUnsignedLongLong (py_func);
1200 struct bound_minimal_symbol msymbol;
1201
1202 if (PyErr_Occurred ())
1203 {
1204 do_cleanups (cleanup_stack);
1205 return EXT_LANG_BT_ERROR;
1206 }
1207
1208 msymbol = lookup_minimal_symbol_by_pc (addr);
1209 if (msymbol.minsym != NULL)
1210 function = MSYMBOL_PRINT_NAME (msymbol.minsym);
1211 }
1212 else if (py_func != Py_None)
1213 {
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;
1219 }
1220
1221 TRY_CATCH (except, RETURN_MASK_ALL)
1222 {
1223 annotate_frame_function_name ();
1224 if (function == NULL)
1225 ui_out_field_skip (out, "func");
1226 else
1227 ui_out_field_string (out, "func", function);
1228 }
1229 if (except.reason < 0)
1230 {
1231 gdbpy_convert_exception (except);
1232 do_cleanups (cleanup_stack);
1233 return EXT_LANG_BT_ERROR;
1234 }
1235 do_cleanups (py_func_cleanup);
1236 }
1237 }
1238
1239
1240 /* Frame arguments. Check the result, and error if something went
1241 wrong. */
1242 if (print_args)
1243 {
1244 if (py_print_args (filter, out, args_type, frame) == EXT_LANG_BT_ERROR)
1245 {
1246 do_cleanups (cleanup_stack);
1247 return EXT_LANG_BT_ERROR;
1248 }
1249 }
1250
1251 /* File name/source/line number information. */
1252 if (print_frame_info)
1253 {
1254 TRY_CATCH (except, RETURN_MASK_ALL)
1255 {
1256 annotate_frame_source_begin ();
1257 }
1258 if (except.reason < 0)
1259 {
1260 gdbpy_convert_exception (except);
1261 do_cleanups (cleanup_stack);
1262 return EXT_LANG_BT_ERROR;
1263 }
1264
1265 if (PyObject_HasAttrString (filter, "filename"))
1266 {
1267 PyObject *py_fn = PyObject_CallMethod (filter, "filename", NULL);
1268 struct cleanup *py_fn_cleanup;
1269
1270 if (py_fn == NULL)
1271 {
1272 do_cleanups (cleanup_stack);
1273 return EXT_LANG_BT_ERROR;
1274 }
1275 py_fn_cleanup = make_cleanup_py_decref (py_fn);
1276
1277 if (py_fn != Py_None)
1278 {
1279 char *filename = python_string_to_host_string (py_fn);
1280
1281 if (filename == NULL)
1282 {
1283 do_cleanups (cleanup_stack);
1284 return EXT_LANG_BT_ERROR;
1285 }
1286
1287 make_cleanup (xfree, filename);
1288 TRY_CATCH (except, RETURN_MASK_ALL)
1289 {
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 ();
1295 }
1296 if (except.reason < 0)
1297 {
1298 gdbpy_convert_exception (except);
1299 do_cleanups (cleanup_stack);
1300 return EXT_LANG_BT_ERROR;
1301 }
1302 }
1303 do_cleanups (py_fn_cleanup);
1304 }
1305
1306 if (PyObject_HasAttrString (filter, "line"))
1307 {
1308 PyObject *py_line = PyObject_CallMethod (filter, "line", NULL);
1309 struct cleanup *py_line_cleanup;
1310 int line;
1311
1312 if (py_line == NULL)
1313 {
1314 do_cleanups (cleanup_stack);
1315 return EXT_LANG_BT_ERROR;
1316 }
1317 py_line_cleanup = make_cleanup_py_decref (py_line);
1318
1319 if (py_line != Py_None)
1320 {
1321 line = PyLong_AsLong (py_line);
1322 TRY_CATCH (except, RETURN_MASK_ALL)
1323 {
1324 ui_out_text (out, ":");
1325 annotate_frame_source_line ();
1326 ui_out_field_int (out, "line", line);
1327 }
1328 if (except.reason < 0)
1329 {
1330 gdbpy_convert_exception (except);
1331 do_cleanups (cleanup_stack);
1332 return EXT_LANG_BT_ERROR;
1333 }
1334 }
1335 do_cleanups (py_line_cleanup);
1336 }
1337 }
1338
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))
1342 {
1343 TRY_CATCH (except, RETURN_MASK_ALL)
1344 {
1345 annotate_frame_end ();
1346 ui_out_text (out, "\n");
1347 }
1348 if (except.reason < 0)
1349 {
1350 gdbpy_convert_exception (except);
1351 do_cleanups (cleanup_stack);
1352 return EXT_LANG_BT_ERROR;
1353 }
1354 }
1355
1356 if (print_locals)
1357 {
1358 if (py_print_locals (filter, out, args_type, indent,
1359 frame) == EXT_LANG_BT_ERROR)
1360 {
1361 do_cleanups (cleanup_stack);
1362 return EXT_LANG_BT_ERROR;
1363 }
1364 }
1365
1366 {
1367 PyObject *elided;
1368 struct cleanup *elided_cleanup;
1369
1370 /* Finally recursively print elided frames, if any. */
1371 elided = get_py_iter_from_func (filter, "elided");
1372 if (elided == NULL)
1373 {
1374 do_cleanups (cleanup_stack);
1375 return EXT_LANG_BT_ERROR;
1376 }
1377 elided_cleanup = make_cleanup_py_decref (elided);
1378
1379 if (elided != Py_None)
1380 {
1381 PyObject *item;
1382
1383 make_cleanup_ui_out_list_begin_end (out, "children");
1384
1385 if (! ui_out_is_mi_like_p (out))
1386 indent++;
1387
1388 while ((item = PyIter_Next (elided)))
1389 {
1390 struct cleanup *item_cleanup = make_cleanup_py_decref (item);
1391
1392 enum ext_lang_bt_status success = py_print_frame (item, flags,
1393 args_type, out,
1394 indent,
1395 levels_printed);
1396
1397 do_cleanups (item_cleanup);
1398
1399 if (success == EXT_LANG_BT_ERROR)
1400 {
1401 do_cleanups (cleanup_stack);
1402 return EXT_LANG_BT_ERROR;
1403 }
1404 }
1405 if (item == NULL && PyErr_Occurred ())
1406 {
1407 do_cleanups (cleanup_stack);
1408 return EXT_LANG_BT_ERROR;
1409 }
1410 }
1411 do_cleanups (elided_cleanup);
1412 }
1413
1414 do_cleanups (cleanup_stack);
1415 return EXT_LANG_BT_COMPLETED;
1416 }
1417
1418 /* Helper function to initiate frame filter invocation at starting
1419 frame FRAME. */
1420
1421 static PyObject *
1422 bootstrap_python_frame_filters (struct frame_info *frame,
1423 int frame_low, int frame_high)
1424 {
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;
1429
1430 frame_obj = frame_info_to_frame_object (frame);
1431 if (frame_obj == NULL)
1432 goto error;
1433 make_cleanup_py_decref (frame_obj);
1434
1435 module = PyImport_ImportModule ("gdb.frames");
1436 if (module == NULL)
1437 goto error;
1438 make_cleanup_py_decref (module);
1439
1440 sort_func = PyObject_GetAttrString (module, "execute_frame_filters");
1441 if (sort_func == NULL)
1442 goto error;
1443 make_cleanup_py_decref (sort_func);
1444
1445 py_frame_low = PyInt_FromLong (frame_low);
1446 if (py_frame_low == NULL)
1447 goto error;
1448 make_cleanup_py_decref (py_frame_low);
1449
1450 py_frame_high = PyInt_FromLong (frame_high);
1451 if (py_frame_high == NULL)
1452 goto error;
1453 make_cleanup_py_decref (py_frame_high);
1454
1455 iterable = PyObject_CallFunctionObjArgs (sort_func, frame_obj,
1456 py_frame_low,
1457 py_frame_high,
1458 NULL);
1459 if (iterable == NULL)
1460 goto error;
1461
1462 do_cleanups (cleanups);
1463
1464 if (iterable != Py_None)
1465 {
1466 iterator = PyObject_GetIter (iterable);
1467 Py_DECREF (iterable);
1468 }
1469 else
1470 {
1471 return iterable;
1472 }
1473
1474 return iterator;
1475
1476 error:
1477 do_cleanups (cleanups);
1478 return NULL;
1479 }
1480
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. */
1495
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)
1501 {
1502 struct gdbarch *gdbarch = NULL;
1503 struct cleanup *cleanups;
1504 enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
1505 PyObject *iterable;
1506 volatile struct gdb_exception except;
1507 PyObject *item;
1508 htab_t levels_printed;
1509
1510 if (!gdb_python_initialized)
1511 return EXT_LANG_BT_NO_FILTERS;
1512
1513 TRY_CATCH (except, RETURN_MASK_ALL)
1514 {
1515 gdbarch = get_frame_arch (frame);
1516 }
1517 if (except.reason < 0)
1518 {
1519 /* Let gdb try to print the stack trace. */
1520 return EXT_LANG_BT_NO_FILTERS;
1521 }
1522
1523 cleanups = ensure_python_env (gdbarch, current_language);
1524
1525 iterable = bootstrap_python_frame_filters (frame, frame_low, frame_high);
1526
1527 if (iterable == NULL)
1528 {
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. */
1541
1542 gdbpy_print_stack ();
1543 do_cleanups (cleanups);
1544 return EXT_LANG_BT_NO_FILTERS;
1545 }
1546
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
1549 and CLI. */
1550 make_cleanup_py_decref (iterable);
1551 if (iterable == Py_None)
1552 {
1553 success = EXT_LANG_BT_NO_FILTERS;
1554 goto done;
1555 }
1556
1557 levels_printed = htab_create (20,
1558 htab_hash_pointer,
1559 htab_eq_pointer,
1560 NULL);
1561 make_cleanup_htab_delete (levels_printed);
1562
1563 while ((item = PyIter_Next (iterable)))
1564 {
1565 struct cleanup *item_cleanup = make_cleanup_py_decref (item);
1566
1567 success = py_print_frame (item, flags, args_type, out, 0,
1568 levels_printed);
1569
1570 do_cleanups (item_cleanup);
1571
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 ();
1576 }
1577
1578 if (item == NULL && PyErr_Occurred ())
1579 goto error;
1580
1581 done:
1582 do_cleanups (cleanups);
1583 return success;
1584
1585 /* Exit and abandon backtrace on error, printing the exception that
1586 is set. */
1587 error:
1588 gdbpy_print_stack ();
1589 do_cleanups (cleanups);
1590 return EXT_LANG_BT_ERROR;
1591 }
This page took 0.091867 seconds and 5 git commands to generate.