DWARF: handle non-local references in nested functions
[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 and SYM_BLOCK is a pass-through argument to
47 write the block where the symbol lies in. In the case of the API
48 returning a string, this will be set to NULL. LANGUAGE is also a
49 pass-through argument denoting the language attributed to the
50 Symbol. In the case of SYM being NULL, this will be set to the
51 current language. Returns EXT_LANG_BT_ERROR on error with the
52 appropriate Python exception set, and EXT_LANG_BT_OK on success. */
53
54 static enum ext_lang_bt_status
55 extract_sym (PyObject *obj, char **name, struct symbol **sym,
56 struct block **sym_block, const struct language_defn **language)
57 {
58 PyObject *result = PyObject_CallMethod (obj, "symbol", NULL);
59
60 if (result == NULL)
61 return EXT_LANG_BT_ERROR;
62
63 /* For 'symbol' callback, the function can return a symbol or a
64 string. */
65 if (gdbpy_is_string (result))
66 {
67 *name = python_string_to_host_string (result);
68 Py_DECREF (result);
69
70 if (*name == NULL)
71 return EXT_LANG_BT_ERROR;
72 /* If the API returns a string (and not a symbol), then there is
73 no symbol derived language available and the frame filter has
74 either overridden the symbol with a string, or supplied a
75 entirely synthetic symbol/value pairing. In that case, use
76 python_language. */
77 *language = python_language;
78 *sym = NULL;
79 *sym_block = NULL;
80 }
81 else
82 {
83 /* This type checks 'result' during the conversion so we
84 just call it unconditionally and check the return. */
85 *sym = symbol_object_to_symbol (result);
86 /* TODO: currently, we have no way to recover the block in which SYMBOL
87 was found, so we have no block to return. Trying to evaluate SYMBOL
88 will yield an incorrect value when it's located in a FRAME and
89 evaluated from another frame (as permitted in nested functions). */
90 *sym_block = NULL;
91
92 Py_DECREF (result);
93
94 if (*sym == NULL)
95 {
96 PyErr_SetString (PyExc_RuntimeError,
97 _("Unexpected value. Expecting a "
98 "gdb.Symbol or a Python string."));
99 return EXT_LANG_BT_ERROR;
100 }
101
102 /* Duplicate the symbol name, so the caller has consistency
103 in garbage collection. */
104 *name = xstrdup (SYMBOL_PRINT_NAME (*sym));
105
106 /* If a symbol is specified attempt to determine the language
107 from the symbol. If mode is not "auto", then the language
108 has been explicitly set, use that. */
109 if (language_mode == language_mode_auto)
110 *language = language_def (SYMBOL_LANGUAGE (*sym));
111 else
112 *language = current_language;
113 }
114
115 return EXT_LANG_BT_OK;
116 }
117
118 /* Helper function to extract a value from an object that conforms to
119 the "Symbol Value" interface. OBJ is the Python object to extract
120 the value from. VALUE is a pass-through argument where the value
121 will be written. If the object does not have the value attribute,
122 or provides the Python None for a value, VALUE will be set to NULL
123 and this function will return as successful. Returns EXT_LANG_BT_ERROR
124 on error with the appropriate Python exception set, and EXT_LANG_BT_OK on
125 success. */
126
127 static enum ext_lang_bt_status
128 extract_value (PyObject *obj, struct value **value)
129 {
130 if (PyObject_HasAttrString (obj, "value"))
131 {
132 PyObject *vresult = PyObject_CallMethod (obj, "value", NULL);
133
134 if (vresult == NULL)
135 return EXT_LANG_BT_ERROR;
136
137 /* The Python code has returned 'None' for a value, so we set
138 value to NULL. This flags that GDB should read the
139 value. */
140 if (vresult == Py_None)
141 {
142 Py_DECREF (vresult);
143 *value = NULL;
144 return EXT_LANG_BT_OK;
145 }
146 else
147 {
148 *value = convert_value_from_python (vresult);
149 Py_DECREF (vresult);
150
151 if (*value == NULL)
152 return EXT_LANG_BT_ERROR;
153
154 return EXT_LANG_BT_OK;
155 }
156 }
157 else
158 *value = NULL;
159
160 return EXT_LANG_BT_OK;
161 }
162
163 /* MI prints only certain values according to the type of symbol and
164 also what the user has specified. SYM is the symbol to check, and
165 MI_PRINT_TYPES is an enum specifying what the user wants emitted
166 for the MI command in question. */
167 static int
168 mi_should_print (struct symbol *sym, enum mi_print_types type)
169 {
170 int print_me = 0;
171
172 switch (SYMBOL_CLASS (sym))
173 {
174 default:
175 case LOC_UNDEF: /* catches errors */
176 case LOC_CONST: /* constant */
177 case LOC_TYPEDEF: /* local typedef */
178 case LOC_LABEL: /* local label */
179 case LOC_BLOCK: /* local function */
180 case LOC_CONST_BYTES: /* loc. byte seq. */
181 case LOC_UNRESOLVED: /* unresolved static */
182 case LOC_OPTIMIZED_OUT: /* optimized out */
183 print_me = 0;
184 break;
185
186 case LOC_ARG: /* argument */
187 case LOC_REF_ARG: /* reference arg */
188 case LOC_REGPARM_ADDR: /* indirect register arg */
189 case LOC_LOCAL: /* stack local */
190 case LOC_STATIC: /* static */
191 case LOC_REGISTER: /* register */
192 case LOC_COMPUTED: /* computed location */
193 if (type == MI_PRINT_LOCALS)
194 print_me = ! SYMBOL_IS_ARGUMENT (sym);
195 else
196 print_me = SYMBOL_IS_ARGUMENT (sym);
197 }
198 return print_me;
199 }
200
201 /* Helper function which outputs a type name extracted from VAL to a
202 "type" field in the output stream OUT. OUT is the ui-out structure
203 the type name will be output too, and VAL is the value that the
204 type will be extracted from. Returns EXT_LANG_BT_ERROR on error, with
205 any GDB exceptions converted to a Python exception, or EXT_LANG_BT_OK on
206 success. */
207
208 static enum ext_lang_bt_status
209 py_print_type (struct ui_out *out, struct value *val)
210 {
211
212 TRY
213 {
214 struct type *type;
215 struct ui_file *stb;
216 struct cleanup *cleanup;
217
218 stb = mem_fileopen ();
219 cleanup = make_cleanup_ui_file_delete (stb);
220 type = check_typedef (value_type (val));
221 type_print (value_type (val), "", stb, -1);
222 ui_out_field_stream (out, "type", stb);
223 do_cleanups (cleanup);
224 }
225 CATCH (except, RETURN_MASK_ALL)
226 {
227 gdbpy_convert_exception (except);
228 return EXT_LANG_BT_ERROR;
229 }
230 END_CATCH
231
232 return EXT_LANG_BT_OK;
233 }
234
235 /* Helper function which outputs a value to an output field in a
236 stream. OUT is the ui-out structure the value will be output to,
237 VAL is the value that will be printed, OPTS contains the value
238 printing options, ARGS_TYPE is an enumerator describing the
239 argument format, and LANGUAGE is the language_defn that the value
240 will be printed with. Returns EXT_LANG_BT_ERROR on error, with any GDB
241 exceptions converted to a Python exception, or EXT_LANG_BT_OK on
242 success. */
243
244 static enum ext_lang_bt_status
245 py_print_value (struct ui_out *out, struct value *val,
246 const struct value_print_options *opts,
247 int indent,
248 enum ext_lang_frame_args args_type,
249 const struct language_defn *language)
250 {
251 int should_print = 0;
252 int local_indent = (4 * indent);
253
254 /* Never set an indent level for common_val_print if MI. */
255 if (ui_out_is_mi_like_p (out))
256 local_indent = 0;
257
258 /* MI does not print certain values, differentiated by type,
259 depending on what ARGS_TYPE indicates. Test type against option.
260 For CLI print all values. */
261 if (args_type == MI_PRINT_SIMPLE_VALUES
262 || args_type == MI_PRINT_ALL_VALUES)
263 {
264 struct type *type = NULL;
265
266 TRY
267 {
268 type = check_typedef (value_type (val));
269 }
270 CATCH (except, RETURN_MASK_ALL)
271 {
272 gdbpy_convert_exception (except);
273 return EXT_LANG_BT_ERROR;
274 }
275 END_CATCH
276
277 if (args_type == MI_PRINT_ALL_VALUES)
278 should_print = 1;
279 else if (args_type == MI_PRINT_SIMPLE_VALUES
280 && TYPE_CODE (type) != TYPE_CODE_ARRAY
281 && TYPE_CODE (type) != TYPE_CODE_STRUCT
282 && TYPE_CODE (type) != TYPE_CODE_UNION)
283 should_print = 1;
284 }
285 else if (args_type != NO_VALUES)
286 should_print = 1;
287
288 if (should_print)
289 {
290 TRY
291 {
292 struct ui_file *stb;
293 struct cleanup *cleanup;
294
295 stb = mem_fileopen ();
296 cleanup = make_cleanup_ui_file_delete (stb);
297 common_val_print (val, stb, indent, opts, language);
298 ui_out_field_stream (out, "value", stb);
299 do_cleanups (cleanup);
300 }
301 CATCH (except, RETURN_MASK_ALL)
302 {
303 gdbpy_convert_exception (except);
304 return EXT_LANG_BT_ERROR;
305 }
306 END_CATCH
307 }
308
309 return EXT_LANG_BT_OK;
310 }
311
312 /* Helper function to call a Python method and extract an iterator
313 from the result. If the function returns anything but an iterator
314 the exception is preserved and NULL is returned. FILTER is the
315 Python object to call, and FUNC is the name of the method. Returns
316 a PyObject, or NULL on error with the appropriate exception set.
317 This function can return an iterator, or NULL. */
318
319 static PyObject *
320 get_py_iter_from_func (PyObject *filter, char *func)
321 {
322 if (PyObject_HasAttrString (filter, func))
323 {
324 PyObject *result = PyObject_CallMethod (filter, func, NULL);
325
326 if (result != NULL)
327 {
328 if (result == Py_None)
329 {
330 return result;
331 }
332 else
333 {
334 PyObject *iterator = PyObject_GetIter (result);
335
336 Py_DECREF (result);
337 return iterator;
338 }
339 }
340 }
341 else
342 Py_RETURN_NONE;
343
344 return NULL;
345 }
346
347 /* Helper function to output a single frame argument and value to an
348 output stream. This function will account for entry values if the
349 FV parameter is populated, the frame argument has entry values
350 associated with them, and the appropriate "set entry-value"
351 options are set. Will output in CLI or MI like format depending
352 on the type of output stream detected. OUT is the output stream,
353 SYM_NAME is the name of the symbol. If SYM_NAME is populated then
354 it must have an accompanying value in the parameter FV. FA is a
355 frame argument structure. If FA is populated, both SYM_NAME and
356 FV are ignored. OPTS contains the value printing options,
357 ARGS_TYPE is an enumerator describing the argument format,
358 PRINT_ARGS_FIELD is a flag which indicates if we output "ARGS=1"
359 in MI output in commands where both arguments and locals are
360 printed. Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions
361 converted to a Python exception, or EXT_LANG_BT_OK on success. */
362
363 static enum ext_lang_bt_status
364 py_print_single_arg (struct ui_out *out,
365 const char *sym_name,
366 struct frame_arg *fa,
367 struct value *fv,
368 const struct value_print_options *opts,
369 enum ext_lang_frame_args args_type,
370 int print_args_field,
371 const struct language_defn *language)
372 {
373 struct value *val;
374 enum ext_lang_bt_status retval = EXT_LANG_BT_OK;
375
376 if (fa != NULL)
377 {
378 if (fa->val == NULL && fa->error == NULL)
379 return EXT_LANG_BT_OK;
380 language = language_def (SYMBOL_LANGUAGE (fa->sym));
381 val = fa->val;
382 }
383 else
384 val = fv;
385
386 TRY
387 {
388 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
389
390 /* MI has varying rules for tuples, but generally if there is only
391 one element in each item in the list, do not start a tuple. The
392 exception is -stack-list-variables which emits an ARGS="1" field
393 if the value is a frame argument. This is denoted in this
394 function with PRINT_ARGS_FIELD which is flag from the caller to
395 emit the ARGS field. */
396 if (ui_out_is_mi_like_p (out))
397 {
398 if (print_args_field || args_type != NO_VALUES)
399 make_cleanup_ui_out_tuple_begin_end (out, NULL);
400 }
401
402 annotate_arg_begin ();
403
404 /* If frame argument is populated, check for entry-values and the
405 entry value options. */
406 if (fa != NULL)
407 {
408 struct ui_file *stb;
409
410 stb = mem_fileopen ();
411 make_cleanup_ui_file_delete (stb);
412 fprintf_symbol_filtered (stb, SYMBOL_PRINT_NAME (fa->sym),
413 SYMBOL_LANGUAGE (fa->sym),
414 DMGL_PARAMS | DMGL_ANSI);
415 if (fa->entry_kind == print_entry_values_compact)
416 {
417 fputs_filtered ("=", stb);
418
419 fprintf_symbol_filtered (stb, SYMBOL_PRINT_NAME (fa->sym),
420 SYMBOL_LANGUAGE (fa->sym),
421 DMGL_PARAMS | DMGL_ANSI);
422 }
423 if (fa->entry_kind == print_entry_values_only
424 || fa->entry_kind == print_entry_values_compact)
425 {
426 fputs_filtered ("@entry", stb);
427 }
428 ui_out_field_stream (out, "name", stb);
429 }
430 else
431 /* Otherwise, just output the name. */
432 ui_out_field_string (out, "name", sym_name);
433
434 annotate_arg_name_end ();
435
436 if (! ui_out_is_mi_like_p (out))
437 ui_out_text (out, "=");
438
439 if (print_args_field)
440 ui_out_field_int (out, "arg", 1);
441
442 /* For MI print the type, but only for simple values. This seems
443 weird, but this is how MI choose to format the various output
444 types. */
445 if (args_type == MI_PRINT_SIMPLE_VALUES && val != NULL)
446 {
447 if (py_print_type (out, val) == EXT_LANG_BT_ERROR)
448 {
449 retval = EXT_LANG_BT_ERROR;
450 do_cleanups (cleanups);
451 continue;
452 }
453 }
454
455 if (val != NULL)
456 annotate_arg_value (value_type (val));
457
458 /* If the output is to the CLI, and the user option "set print
459 frame-arguments" is set to none, just output "...". */
460 if (! ui_out_is_mi_like_p (out) && args_type == NO_VALUES)
461 ui_out_field_string (out, "value", "...");
462 else
463 {
464 /* Otherwise, print the value for both MI and the CLI, except
465 for the case of MI_PRINT_NO_VALUES. */
466 if (args_type != NO_VALUES)
467 {
468 if (val == NULL)
469 {
470 gdb_assert (fa != NULL && fa->error != NULL);
471 ui_out_field_fmt (out, "value",
472 _("<error reading variable: %s>"),
473 fa->error);
474 }
475 else if (py_print_value (out, val, opts, 0, args_type, language)
476 == EXT_LANG_BT_ERROR)
477 retval = EXT_LANG_BT_ERROR;
478 }
479 }
480
481 do_cleanups (cleanups);
482 }
483 CATCH (except, RETURN_MASK_ERROR)
484 {
485 gdbpy_convert_exception (except);
486 }
487 END_CATCH
488
489 return retval;
490 }
491
492 /* Helper function to loop over frame arguments provided by the
493 "frame_arguments" Python API. Elements in the iterator must
494 conform to the "Symbol Value" interface. ITER is the Python
495 iterable object, OUT is the output stream, ARGS_TYPE is an
496 enumerator describing the argument format, PRINT_ARGS_FIELD is a
497 flag which indicates if we output "ARGS=1" in MI output in commands
498 where both arguments and locals are printed, and FRAME is the
499 backing frame. Returns EXT_LANG_BT_ERROR on error, with any GDB
500 exceptions converted to a Python exception, or EXT_LANG_BT_OK on
501 success. */
502
503 static enum ext_lang_bt_status
504 enumerate_args (PyObject *iter,
505 struct ui_out *out,
506 enum ext_lang_frame_args args_type,
507 int print_args_field,
508 struct frame_info *frame)
509 {
510 PyObject *item;
511 struct value_print_options opts;
512
513 get_user_print_options (&opts);
514
515 if (args_type == CLI_SCALAR_VALUES)
516 {
517 /* True in "summary" mode, false otherwise. */
518 opts.summary = 1;
519 }
520
521 opts.deref_ref = 1;
522
523 TRY
524 {
525 annotate_frame_args ();
526 }
527 CATCH (except, RETURN_MASK_ALL)
528 {
529 gdbpy_convert_exception (except);
530 goto error;
531 }
532 END_CATCH
533
534 /* Collect the first argument outside of the loop, so output of
535 commas in the argument output is correct. At the end of the
536 loop block collect another item from the iterator, and, if it is
537 not null emit a comma. */
538 item = PyIter_Next (iter);
539 if (item == NULL && PyErr_Occurred ())
540 goto error;
541
542 while (item)
543 {
544 const struct language_defn *language;
545 char *sym_name;
546 struct symbol *sym;
547 struct block *sym_block;
548 struct value *val;
549 enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
550
551 success = extract_sym (item, &sym_name, &sym, &sym_block, &language);
552 if (success == EXT_LANG_BT_ERROR)
553 {
554 Py_DECREF (item);
555 goto error;
556 }
557
558 success = extract_value (item, &val);
559 if (success == EXT_LANG_BT_ERROR)
560 {
561 xfree (sym_name);
562 Py_DECREF (item);
563 goto error;
564 }
565
566 Py_DECREF (item);
567 item = NULL;
568
569 if (sym && ui_out_is_mi_like_p (out)
570 && ! mi_should_print (sym, MI_PRINT_ARGS))
571 {
572 xfree (sym_name);
573 continue;
574 }
575
576 /* If the object did not provide a value, read it using
577 read_frame_args and account for entry values, if any. */
578 if (val == NULL)
579 {
580 struct frame_arg arg, entryarg;
581
582 /* If there is no value, and also no symbol, set error and
583 exit. */
584 if (sym == NULL)
585 {
586 PyErr_SetString (PyExc_RuntimeError,
587 _("No symbol or value provided."));
588 xfree (sym_name);
589 goto error;
590 }
591
592 TRY
593 {
594 read_frame_arg (sym, frame, &arg, &entryarg);
595 }
596 CATCH (except, RETURN_MASK_ALL)
597 {
598 xfree (sym_name);
599 gdbpy_convert_exception (except);
600 goto error;
601 }
602 END_CATCH
603
604 /* The object has not provided a value, so this is a frame
605 argument to be read by GDB. In this case we have to
606 account for entry-values. */
607
608 if (arg.entry_kind != print_entry_values_only)
609 {
610 if (py_print_single_arg (out, NULL, &arg,
611 NULL, &opts,
612 args_type,
613 print_args_field,
614 NULL) == EXT_LANG_BT_ERROR)
615 {
616 xfree (arg.error);
617 xfree (entryarg.error);
618 xfree (sym_name);
619 goto error;
620 }
621 }
622
623 if (entryarg.entry_kind != print_entry_values_no)
624 {
625 if (arg.entry_kind != print_entry_values_only)
626 {
627 TRY
628 {
629 ui_out_text (out, ", ");
630 ui_out_wrap_hint (out, " ");
631 }
632 CATCH (except, RETURN_MASK_ALL)
633 {
634 xfree (arg.error);
635 xfree (entryarg.error);
636 xfree (sym_name);
637 gdbpy_convert_exception (except);
638 goto error;
639 }
640 END_CATCH
641 }
642
643 if (py_print_single_arg (out, NULL, &entryarg, NULL, &opts,
644 args_type, print_args_field, NULL)
645 == EXT_LANG_BT_ERROR)
646 {
647 xfree (arg.error);
648 xfree (entryarg.error);
649 xfree (sym_name);
650 goto error;
651 }
652 }
653
654 xfree (arg.error);
655 xfree (entryarg.error);
656 }
657 else
658 {
659 /* If the object has provided a value, we just print that. */
660 if (val != NULL)
661 {
662 if (py_print_single_arg (out, sym_name, NULL, val, &opts,
663 args_type, print_args_field,
664 language) == EXT_LANG_BT_ERROR)
665 {
666 xfree (sym_name);
667 goto error;
668 }
669 }
670 }
671
672 xfree (sym_name);
673
674 /* Collect the next item from the iterator. If
675 this is the last item, do not print the
676 comma. */
677 item = PyIter_Next (iter);
678 if (item != NULL)
679 {
680 TRY
681 {
682 ui_out_text (out, ", ");
683 }
684 CATCH (except, RETURN_MASK_ALL)
685 {
686 Py_DECREF (item);
687 gdbpy_convert_exception (except);
688 goto error;
689 }
690 END_CATCH
691 }
692 else if (PyErr_Occurred ())
693 goto error;
694
695 TRY
696 {
697 annotate_arg_end ();
698 }
699 CATCH (except, RETURN_MASK_ALL)
700 {
701 Py_DECREF (item);
702 gdbpy_convert_exception (except);
703 goto error;
704 }
705 END_CATCH
706 }
707
708 return EXT_LANG_BT_OK;
709
710 error:
711 return EXT_LANG_BT_ERROR;
712 }
713
714
715 /* Helper function to loop over variables provided by the
716 "frame_locals" Python API. Elements in the iterable must conform
717 to the "Symbol Value" interface. ITER is the Python iterable
718 object, OUT is the output stream, INDENT is whether we should
719 indent the output (for CLI), ARGS_TYPE is an enumerator describing
720 the argument format, PRINT_ARGS_FIELD is flag which indicates
721 whether to output the ARGS field in the case of
722 -stack-list-variables and FRAME is the backing frame. Returns
723 EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to a Python
724 exception, or EXT_LANG_BT_OK on success. */
725
726 static enum ext_lang_bt_status
727 enumerate_locals (PyObject *iter,
728 struct ui_out *out,
729 int indent,
730 enum ext_lang_frame_args args_type,
731 int print_args_field,
732 struct frame_info *frame)
733 {
734 PyObject *item;
735 struct value_print_options opts;
736
737 get_user_print_options (&opts);
738 opts.deref_ref = 1;
739
740 while ((item = PyIter_Next (iter)))
741 {
742 const struct language_defn *language;
743 char *sym_name;
744 struct value *val;
745 enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
746 struct symbol *sym;
747 struct block *sym_block;
748 int local_indent = 8 + (8 * indent);
749 struct cleanup *locals_cleanups;
750
751 locals_cleanups = make_cleanup_py_decref (item);
752
753 success = extract_sym (item, &sym_name, &sym, &sym_block, &language);
754 if (success == EXT_LANG_BT_ERROR)
755 {
756 do_cleanups (locals_cleanups);
757 goto error;
758 }
759
760 make_cleanup (xfree, sym_name);
761
762 success = extract_value (item, &val);
763 if (success == EXT_LANG_BT_ERROR)
764 {
765 do_cleanups (locals_cleanups);
766 goto error;
767 }
768
769 if (sym != NULL && ui_out_is_mi_like_p (out)
770 && ! mi_should_print (sym, MI_PRINT_LOCALS))
771 {
772 do_cleanups (locals_cleanups);
773 continue;
774 }
775
776 /* If the object did not provide a value, read it. */
777 if (val == NULL)
778 {
779 TRY
780 {
781 val = read_var_value (sym, sym_block, frame);
782 }
783 CATCH (except, RETURN_MASK_ERROR)
784 {
785 gdbpy_convert_exception (except);
786 do_cleanups (locals_cleanups);
787 goto error;
788 }
789 END_CATCH
790 }
791
792 /* With PRINT_NO_VALUES, MI does not emit a tuple normally as
793 each output contains only one field. The exception is
794 -stack-list-variables, which always provides a tuple. */
795 if (ui_out_is_mi_like_p (out))
796 {
797 if (print_args_field || args_type != NO_VALUES)
798 make_cleanup_ui_out_tuple_begin_end (out, NULL);
799 }
800 TRY
801 {
802 if (! ui_out_is_mi_like_p (out))
803 {
804 /* If the output is not MI we indent locals. */
805 ui_out_spaces (out, local_indent);
806 }
807
808 ui_out_field_string (out, "name", sym_name);
809
810 if (! ui_out_is_mi_like_p (out))
811 ui_out_text (out, " = ");
812 }
813 CATCH (except, RETURN_MASK_ERROR)
814 {
815 gdbpy_convert_exception (except);
816 do_cleanups (locals_cleanups);
817 goto error;
818 }
819 END_CATCH
820
821 if (args_type == MI_PRINT_SIMPLE_VALUES)
822 {
823 if (py_print_type (out, val) == EXT_LANG_BT_ERROR)
824 {
825 do_cleanups (locals_cleanups);
826 goto error;
827 }
828 }
829
830 /* CLI always prints values for locals. MI uses the
831 simple/no/all system. */
832 if (! ui_out_is_mi_like_p (out))
833 {
834 int val_indent = (indent + 1) * 4;
835
836 if (py_print_value (out, val, &opts, val_indent, args_type,
837 language) == EXT_LANG_BT_ERROR)
838 {
839 do_cleanups (locals_cleanups);
840 goto error;
841 }
842 }
843 else
844 {
845 if (args_type != NO_VALUES)
846 {
847 if (py_print_value (out, val, &opts, 0, args_type,
848 language) == EXT_LANG_BT_ERROR)
849 {
850 do_cleanups (locals_cleanups);
851 goto error;
852 }
853 }
854 }
855
856 do_cleanups (locals_cleanups);
857
858 TRY
859 {
860 ui_out_text (out, "\n");
861 }
862 CATCH (except, RETURN_MASK_ERROR)
863 {
864 gdbpy_convert_exception (except);
865 goto error;
866 }
867 END_CATCH
868 }
869
870 if (item == NULL && PyErr_Occurred ())
871 goto error;
872
873 return EXT_LANG_BT_OK;
874
875 error:
876 return EXT_LANG_BT_ERROR;
877 }
878
879 /* Helper function for -stack-list-variables. Returns EXT_LANG_BT_ERROR on
880 error, or EXT_LANG_BT_OK on success. */
881
882 static enum ext_lang_bt_status
883 py_mi_print_variables (PyObject *filter, struct ui_out *out,
884 struct value_print_options *opts,
885 enum ext_lang_frame_args args_type,
886 struct frame_info *frame)
887 {
888 struct cleanup *old_chain;
889 PyObject *args_iter;
890 PyObject *locals_iter;
891
892 args_iter = get_py_iter_from_func (filter, "frame_args");
893 old_chain = make_cleanup_py_xdecref (args_iter);
894 if (args_iter == NULL)
895 goto error;
896
897 locals_iter = get_py_iter_from_func (filter, "frame_locals");
898 if (locals_iter == NULL)
899 goto error;
900
901 make_cleanup_py_decref (locals_iter);
902 make_cleanup_ui_out_list_begin_end (out, "variables");
903
904 if (args_iter != Py_None)
905 if (enumerate_args (args_iter, out, args_type, 1, frame)
906 == EXT_LANG_BT_ERROR)
907 goto error;
908
909 if (locals_iter != Py_None)
910 if (enumerate_locals (locals_iter, out, 1, args_type, 1, frame)
911 == EXT_LANG_BT_ERROR)
912 goto error;
913
914 do_cleanups (old_chain);
915 return EXT_LANG_BT_OK;
916
917 error:
918 do_cleanups (old_chain);
919 return EXT_LANG_BT_ERROR;
920 }
921
922 /* Helper function for printing locals. This function largely just
923 creates the wrapping tuple, and calls enumerate_locals. Returns
924 EXT_LANG_BT_ERROR on error, or EXT_LANG_BT_OK on success. */
925
926 static enum ext_lang_bt_status
927 py_print_locals (PyObject *filter,
928 struct ui_out *out,
929 enum ext_lang_frame_args args_type,
930 int indent,
931 struct frame_info *frame)
932 {
933 PyObject *locals_iter = get_py_iter_from_func (filter,
934 "frame_locals");
935 struct cleanup *old_chain = make_cleanup_py_xdecref (locals_iter);
936
937 if (locals_iter == NULL)
938 goto locals_error;
939
940 make_cleanup_ui_out_list_begin_end (out, "locals");
941
942 if (locals_iter != Py_None)
943 if (enumerate_locals (locals_iter, out, indent, args_type,
944 0, frame) == EXT_LANG_BT_ERROR)
945 goto locals_error;
946
947 do_cleanups (old_chain);
948 return EXT_LANG_BT_OK;
949
950 locals_error:
951 do_cleanups (old_chain);
952 return EXT_LANG_BT_ERROR;
953 }
954
955 /* Helper function for printing frame arguments. This function
956 largely just creates the wrapping tuple, and calls enumerate_args.
957 Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to
958 a Python exception, or EXT_LANG_BT_OK on success. */
959
960 static enum ext_lang_bt_status
961 py_print_args (PyObject *filter,
962 struct ui_out *out,
963 enum ext_lang_frame_args args_type,
964 struct frame_info *frame)
965 {
966 PyObject *args_iter = get_py_iter_from_func (filter, "frame_args");
967 struct cleanup *old_chain = make_cleanup_py_xdecref (args_iter);
968
969 if (args_iter == NULL)
970 goto args_error;
971
972 make_cleanup_ui_out_list_begin_end (out, "args");
973
974 TRY
975 {
976 annotate_frame_args ();
977 if (! ui_out_is_mi_like_p (out))
978 ui_out_text (out, " (");
979 }
980 CATCH (except, RETURN_MASK_ALL)
981 {
982 gdbpy_convert_exception (except);
983 goto args_error;
984 }
985 END_CATCH
986
987 if (args_iter != Py_None)
988 if (enumerate_args (args_iter, out, args_type, 0, frame)
989 == EXT_LANG_BT_ERROR)
990 goto args_error;
991
992 TRY
993 {
994 if (! ui_out_is_mi_like_p (out))
995 ui_out_text (out, ")");
996 }
997 CATCH (except, RETURN_MASK_ALL)
998 {
999 gdbpy_convert_exception (except);
1000 goto args_error;
1001 }
1002 END_CATCH
1003
1004 do_cleanups (old_chain);
1005 return EXT_LANG_BT_OK;
1006
1007 args_error:
1008 do_cleanups (old_chain);
1009 return EXT_LANG_BT_ERROR;
1010 }
1011
1012 /* Print a single frame to the designated output stream, detecting
1013 whether the output is MI or console, and formatting the output
1014 according to the conventions of that protocol. FILTER is the
1015 frame-filter associated with this frame. FLAGS is an integer
1016 describing the various print options. The FLAGS variables is
1017 described in "apply_frame_filter" function. ARGS_TYPE is an
1018 enumerator describing the argument format. OUT is the output
1019 stream to print, INDENT is the level of indention for this frame
1020 (in the case of elided frames), and LEVELS_PRINTED is a hash-table
1021 containing all the frames level that have already been printed.
1022 If a frame level has been printed, do not print it again (in the
1023 case of elided frames). Returns EXT_LANG_BT_ERROR on error, with any
1024 GDB exceptions converted to a Python exception, or EXT_LANG_BT_COMPLETED
1025 on success. It can also throw an exception RETURN_QUIT. */
1026
1027 static enum ext_lang_bt_status
1028 py_print_frame (PyObject *filter, int flags,
1029 enum ext_lang_frame_args args_type,
1030 struct ui_out *out, int indent, htab_t levels_printed)
1031 {
1032 int has_addr = 0;
1033 CORE_ADDR address = 0;
1034 struct gdbarch *gdbarch = NULL;
1035 struct frame_info *frame = NULL;
1036 struct cleanup *cleanup_stack;
1037 struct value_print_options opts;
1038 PyObject *py_inf_frame;
1039 int print_level, print_frame_info, print_args, print_locals;
1040
1041 /* Extract print settings from FLAGS. */
1042 print_level = (flags & PRINT_LEVEL) ? 1 : 0;
1043 print_frame_info = (flags & PRINT_FRAME_INFO) ? 1 : 0;
1044 print_args = (flags & PRINT_ARGS) ? 1 : 0;
1045 print_locals = (flags & PRINT_LOCALS) ? 1 : 0;
1046
1047 get_user_print_options (&opts);
1048
1049 /* Get the underlying frame. This is needed to determine GDB
1050 architecture, and also, in the cases of frame variables/arguments to
1051 read them if they returned filter object requires us to do so. */
1052 py_inf_frame = PyObject_CallMethod (filter, "inferior_frame", NULL);
1053 if (py_inf_frame == NULL)
1054 return EXT_LANG_BT_ERROR;
1055
1056 frame = frame_object_to_frame_info (py_inf_frame);;
1057
1058 Py_DECREF (py_inf_frame);
1059
1060 if (frame == NULL)
1061 return EXT_LANG_BT_ERROR;
1062
1063 TRY
1064 {
1065 gdbarch = get_frame_arch (frame);
1066 }
1067 CATCH (except, RETURN_MASK_ERROR)
1068 {
1069 gdbpy_convert_exception (except);
1070 return EXT_LANG_BT_ERROR;
1071 }
1072 END_CATCH
1073
1074 /* stack-list-variables. */
1075 if (print_locals && print_args && ! print_frame_info)
1076 {
1077 if (py_mi_print_variables (filter, out, &opts,
1078 args_type, frame) == EXT_LANG_BT_ERROR)
1079 return EXT_LANG_BT_ERROR;
1080 return EXT_LANG_BT_COMPLETED;
1081 }
1082
1083 cleanup_stack = make_cleanup (null_cleanup, NULL);
1084
1085 /* -stack-list-locals does not require a
1086 wrapping frame attribute. */
1087 if (print_frame_info || (print_args && ! print_locals))
1088 make_cleanup_ui_out_tuple_begin_end (out, "frame");
1089
1090 if (print_frame_info)
1091 {
1092 /* Elided frames are also printed with this function (recursively)
1093 and are printed with indention. */
1094 if (indent > 0)
1095 {
1096 TRY
1097 {
1098 ui_out_spaces (out, indent*4);
1099 }
1100 CATCH (except, RETURN_MASK_ERROR)
1101 {
1102 gdbpy_convert_exception (except);
1103 do_cleanups (cleanup_stack);
1104 return EXT_LANG_BT_ERROR;
1105 }
1106 END_CATCH
1107 }
1108
1109 /* The address is required for frame annotations, and also for
1110 address printing. */
1111 if (PyObject_HasAttrString (filter, "address"))
1112 {
1113 PyObject *paddr = PyObject_CallMethod (filter, "address", NULL);
1114
1115 if (paddr == NULL)
1116 {
1117 do_cleanups (cleanup_stack);
1118 return EXT_LANG_BT_ERROR;
1119 }
1120
1121 if (paddr != Py_None)
1122 {
1123 address = PyLong_AsLong (paddr);
1124 has_addr = 1;
1125 }
1126 Py_DECREF (paddr);
1127 }
1128 }
1129
1130 /* Print frame level. MI does not require the level if
1131 locals/variables only are being printed. */
1132 if ((print_frame_info || print_args) && print_level)
1133 {
1134 struct frame_info **slot;
1135 int level;
1136
1137 slot = (struct frame_info **) htab_find_slot (levels_printed,
1138 frame, INSERT);
1139 TRY
1140 {
1141 level = frame_relative_level (frame);
1142
1143 /* Check if this frame has already been printed (there are cases
1144 where elided synthetic dummy-frames have to 'borrow' the frame
1145 architecture from the eliding frame. If that is the case, do
1146 not print 'level', but print spaces. */
1147 if (*slot == frame)
1148 ui_out_field_skip (out, "level");
1149 else
1150 {
1151 *slot = frame;
1152 annotate_frame_begin (print_level ? level : 0,
1153 gdbarch, address);
1154 ui_out_text (out, "#");
1155 ui_out_field_fmt_int (out, 2, ui_left, "level",
1156 level);
1157 }
1158 }
1159 CATCH (except, RETURN_MASK_ERROR)
1160 {
1161 gdbpy_convert_exception (except);
1162 do_cleanups (cleanup_stack);
1163 return EXT_LANG_BT_ERROR;
1164 }
1165 END_CATCH
1166 }
1167
1168 if (print_frame_info)
1169 {
1170 /* Print address to the address field. If an address is not provided,
1171 print nothing. */
1172 if (opts.addressprint && has_addr)
1173 {
1174 TRY
1175 {
1176 annotate_frame_address ();
1177 ui_out_field_core_addr (out, "addr", gdbarch, address);
1178 annotate_frame_address_end ();
1179 ui_out_text (out, " in ");
1180 }
1181 CATCH (except, RETURN_MASK_ERROR)
1182 {
1183 gdbpy_convert_exception (except);
1184 do_cleanups (cleanup_stack);
1185 return EXT_LANG_BT_ERROR;
1186 }
1187 END_CATCH
1188 }
1189
1190 /* Print frame function name. */
1191 if (PyObject_HasAttrString (filter, "function"))
1192 {
1193 PyObject *py_func = PyObject_CallMethod (filter, "function", NULL);
1194 struct cleanup *py_func_cleanup;
1195 const char *function = NULL;
1196
1197 if (py_func == NULL)
1198 {
1199 do_cleanups (cleanup_stack);
1200 return EXT_LANG_BT_ERROR;
1201 }
1202 py_func_cleanup = make_cleanup_py_decref (py_func);
1203
1204 if (gdbpy_is_string (py_func))
1205 {
1206 char *function_to_free;
1207
1208 function = function_to_free =
1209 python_string_to_host_string (py_func);
1210
1211 if (function == NULL)
1212 {
1213 do_cleanups (cleanup_stack);
1214 return EXT_LANG_BT_ERROR;
1215 }
1216 make_cleanup (xfree, function_to_free);
1217 }
1218 else if (PyLong_Check (py_func))
1219 {
1220 CORE_ADDR addr = PyLong_AsUnsignedLongLong (py_func);
1221 struct bound_minimal_symbol msymbol;
1222
1223 if (PyErr_Occurred ())
1224 {
1225 do_cleanups (cleanup_stack);
1226 return EXT_LANG_BT_ERROR;
1227 }
1228
1229 msymbol = lookup_minimal_symbol_by_pc (addr);
1230 if (msymbol.minsym != NULL)
1231 function = MSYMBOL_PRINT_NAME (msymbol.minsym);
1232 }
1233 else if (py_func != Py_None)
1234 {
1235 PyErr_SetString (PyExc_RuntimeError,
1236 _("FrameDecorator.function: expecting a " \
1237 "String, integer or None."));
1238 do_cleanups (cleanup_stack);
1239 return EXT_LANG_BT_ERROR;
1240 }
1241
1242 TRY
1243 {
1244 annotate_frame_function_name ();
1245 if (function == NULL)
1246 ui_out_field_skip (out, "func");
1247 else
1248 ui_out_field_string (out, "func", function);
1249 }
1250 CATCH (except, RETURN_MASK_ERROR)
1251 {
1252 gdbpy_convert_exception (except);
1253 do_cleanups (cleanup_stack);
1254 return EXT_LANG_BT_ERROR;
1255 }
1256 END_CATCH
1257
1258 do_cleanups (py_func_cleanup);
1259 }
1260 }
1261
1262
1263 /* Frame arguments. Check the result, and error if something went
1264 wrong. */
1265 if (print_args)
1266 {
1267 if (py_print_args (filter, out, args_type, frame) == EXT_LANG_BT_ERROR)
1268 {
1269 do_cleanups (cleanup_stack);
1270 return EXT_LANG_BT_ERROR;
1271 }
1272 }
1273
1274 /* File name/source/line number information. */
1275 if (print_frame_info)
1276 {
1277 TRY
1278 {
1279 annotate_frame_source_begin ();
1280 }
1281 CATCH (except, RETURN_MASK_ERROR)
1282 {
1283 gdbpy_convert_exception (except);
1284 do_cleanups (cleanup_stack);
1285 return EXT_LANG_BT_ERROR;
1286 }
1287 END_CATCH
1288
1289 if (PyObject_HasAttrString (filter, "filename"))
1290 {
1291 PyObject *py_fn = PyObject_CallMethod (filter, "filename", NULL);
1292 struct cleanup *py_fn_cleanup;
1293
1294 if (py_fn == NULL)
1295 {
1296 do_cleanups (cleanup_stack);
1297 return EXT_LANG_BT_ERROR;
1298 }
1299 py_fn_cleanup = make_cleanup_py_decref (py_fn);
1300
1301 if (py_fn != Py_None)
1302 {
1303 char *filename = python_string_to_host_string (py_fn);
1304
1305 if (filename == NULL)
1306 {
1307 do_cleanups (cleanup_stack);
1308 return EXT_LANG_BT_ERROR;
1309 }
1310
1311 make_cleanup (xfree, filename);
1312 TRY
1313 {
1314 ui_out_wrap_hint (out, " ");
1315 ui_out_text (out, " at ");
1316 annotate_frame_source_file ();
1317 ui_out_field_string (out, "file", filename);
1318 annotate_frame_source_file_end ();
1319 }
1320 CATCH (except, RETURN_MASK_ERROR)
1321 {
1322 gdbpy_convert_exception (except);
1323 do_cleanups (cleanup_stack);
1324 return EXT_LANG_BT_ERROR;
1325 }
1326 END_CATCH
1327 }
1328 do_cleanups (py_fn_cleanup);
1329 }
1330
1331 if (PyObject_HasAttrString (filter, "line"))
1332 {
1333 PyObject *py_line = PyObject_CallMethod (filter, "line", NULL);
1334 struct cleanup *py_line_cleanup;
1335 int line;
1336
1337 if (py_line == NULL)
1338 {
1339 do_cleanups (cleanup_stack);
1340 return EXT_LANG_BT_ERROR;
1341 }
1342 py_line_cleanup = make_cleanup_py_decref (py_line);
1343
1344 if (py_line != Py_None)
1345 {
1346 line = PyLong_AsLong (py_line);
1347 TRY
1348 {
1349 ui_out_text (out, ":");
1350 annotate_frame_source_line ();
1351 ui_out_field_int (out, "line", line);
1352 }
1353 CATCH (except, RETURN_MASK_ERROR)
1354 {
1355 gdbpy_convert_exception (except);
1356 do_cleanups (cleanup_stack);
1357 return EXT_LANG_BT_ERROR;
1358 }
1359 END_CATCH
1360 }
1361 do_cleanups (py_line_cleanup);
1362 }
1363 }
1364
1365 /* For MI we need to deal with the "children" list population of
1366 elided frames, so if MI output detected do not send newline. */
1367 if (! ui_out_is_mi_like_p (out))
1368 {
1369 TRY
1370 {
1371 annotate_frame_end ();
1372 ui_out_text (out, "\n");
1373 }
1374 CATCH (except, RETURN_MASK_ERROR)
1375 {
1376 gdbpy_convert_exception (except);
1377 do_cleanups (cleanup_stack);
1378 return EXT_LANG_BT_ERROR;
1379 }
1380 END_CATCH
1381 }
1382
1383 if (print_locals)
1384 {
1385 if (py_print_locals (filter, out, args_type, indent,
1386 frame) == EXT_LANG_BT_ERROR)
1387 {
1388 do_cleanups (cleanup_stack);
1389 return EXT_LANG_BT_ERROR;
1390 }
1391 }
1392
1393 {
1394 PyObject *elided;
1395 struct cleanup *elided_cleanup;
1396
1397 /* Finally recursively print elided frames, if any. */
1398 elided = get_py_iter_from_func (filter, "elided");
1399 if (elided == NULL)
1400 {
1401 do_cleanups (cleanup_stack);
1402 return EXT_LANG_BT_ERROR;
1403 }
1404 elided_cleanup = make_cleanup_py_decref (elided);
1405
1406 if (elided != Py_None)
1407 {
1408 PyObject *item;
1409
1410 make_cleanup_ui_out_list_begin_end (out, "children");
1411
1412 if (! ui_out_is_mi_like_p (out))
1413 indent++;
1414
1415 while ((item = PyIter_Next (elided)))
1416 {
1417 struct cleanup *item_cleanup = make_cleanup_py_decref (item);
1418
1419 enum ext_lang_bt_status success = py_print_frame (item, flags,
1420 args_type, out,
1421 indent,
1422 levels_printed);
1423
1424 do_cleanups (item_cleanup);
1425
1426 if (success == EXT_LANG_BT_ERROR)
1427 {
1428 do_cleanups (cleanup_stack);
1429 return EXT_LANG_BT_ERROR;
1430 }
1431 }
1432 if (item == NULL && PyErr_Occurred ())
1433 {
1434 do_cleanups (cleanup_stack);
1435 return EXT_LANG_BT_ERROR;
1436 }
1437 }
1438 do_cleanups (elided_cleanup);
1439 }
1440
1441 do_cleanups (cleanup_stack);
1442 return EXT_LANG_BT_COMPLETED;
1443 }
1444
1445 /* Helper function to initiate frame filter invocation at starting
1446 frame FRAME. */
1447
1448 static PyObject *
1449 bootstrap_python_frame_filters (struct frame_info *frame,
1450 int frame_low, int frame_high)
1451 {
1452 struct cleanup *cleanups =
1453 make_cleanup (null_cleanup, NULL);
1454 PyObject *module, *sort_func, *iterable, *frame_obj, *iterator;
1455 PyObject *py_frame_low, *py_frame_high;
1456
1457 frame_obj = frame_info_to_frame_object (frame);
1458 if (frame_obj == NULL)
1459 goto error;
1460 make_cleanup_py_decref (frame_obj);
1461
1462 module = PyImport_ImportModule ("gdb.frames");
1463 if (module == NULL)
1464 goto error;
1465 make_cleanup_py_decref (module);
1466
1467 sort_func = PyObject_GetAttrString (module, "execute_frame_filters");
1468 if (sort_func == NULL)
1469 goto error;
1470 make_cleanup_py_decref (sort_func);
1471
1472 py_frame_low = PyInt_FromLong (frame_low);
1473 if (py_frame_low == NULL)
1474 goto error;
1475 make_cleanup_py_decref (py_frame_low);
1476
1477 py_frame_high = PyInt_FromLong (frame_high);
1478 if (py_frame_high == NULL)
1479 goto error;
1480 make_cleanup_py_decref (py_frame_high);
1481
1482 iterable = PyObject_CallFunctionObjArgs (sort_func, frame_obj,
1483 py_frame_low,
1484 py_frame_high,
1485 NULL);
1486 if (iterable == NULL)
1487 goto error;
1488
1489 do_cleanups (cleanups);
1490
1491 if (iterable != Py_None)
1492 {
1493 iterator = PyObject_GetIter (iterable);
1494 Py_DECREF (iterable);
1495 }
1496 else
1497 {
1498 return iterable;
1499 }
1500
1501 return iterator;
1502
1503 error:
1504 do_cleanups (cleanups);
1505 return NULL;
1506 }
1507
1508 /* This is the only publicly exported function in this file. FRAME
1509 is the source frame to start frame-filter invocation. FLAGS is an
1510 integer holding the flags for printing. The following elements of
1511 the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
1512 PRINT_LEVEL is a flag indicating whether to print the frame's
1513 relative level in the output. PRINT_FRAME_INFO is a flag that
1514 indicates whether this function should print the frame
1515 information, PRINT_ARGS is a flag that indicates whether to print
1516 frame arguments, and PRINT_LOCALS, likewise, with frame local
1517 variables. ARGS_TYPE is an enumerator describing the argument
1518 format, OUT is the output stream to print. FRAME_LOW is the
1519 beginning of the slice of frames to print, and FRAME_HIGH is the
1520 upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error,
1521 or EXT_LANG_BT_COMPLETED on success. */
1522
1523 enum ext_lang_bt_status
1524 gdbpy_apply_frame_filter (const struct extension_language_defn *extlang,
1525 struct frame_info *frame, int flags,
1526 enum ext_lang_frame_args args_type,
1527 struct ui_out *out, int frame_low, int frame_high)
1528 {
1529 struct gdbarch *gdbarch = NULL;
1530 struct cleanup *cleanups;
1531 enum ext_lang_bt_status success = EXT_LANG_BT_ERROR;
1532 PyObject *iterable;
1533 PyObject *item;
1534 htab_t levels_printed;
1535
1536 if (!gdb_python_initialized)
1537 return EXT_LANG_BT_NO_FILTERS;
1538
1539 TRY
1540 {
1541 gdbarch = get_frame_arch (frame);
1542 }
1543 CATCH (except, RETURN_MASK_ALL)
1544 {
1545 /* Let gdb try to print the stack trace. */
1546 return EXT_LANG_BT_NO_FILTERS;
1547 }
1548 END_CATCH
1549
1550 cleanups = ensure_python_env (gdbarch, current_language);
1551
1552 iterable = bootstrap_python_frame_filters (frame, frame_low, frame_high);
1553
1554 if (iterable == NULL)
1555 {
1556 /* Normally if there is an error GDB prints the exception,
1557 abandons the backtrace and exits. The user can then call "bt
1558 no-filters", and get a default backtrace (it would be
1559 confusing to automatically start a standard backtrace halfway
1560 through a Python filtered backtrace). However in the case
1561 where GDB cannot initialize the frame filters (most likely
1562 due to incorrect auto-load paths), GDB has printed nothing.
1563 In this case it is OK to print the default backtrace after
1564 printing the error message. GDB returns EXT_LANG_BT_NO_FILTERS
1565 here to signify there are no filters after printing the
1566 initialization error. This return code will trigger a
1567 default backtrace. */
1568
1569 gdbpy_print_stack ();
1570 do_cleanups (cleanups);
1571 return EXT_LANG_BT_NO_FILTERS;
1572 }
1573
1574 /* If iterable is None, then there are no frame filters registered.
1575 If this is the case, defer to default GDB printing routines in MI
1576 and CLI. */
1577 make_cleanup_py_decref (iterable);
1578 if (iterable == Py_None)
1579 {
1580 success = EXT_LANG_BT_NO_FILTERS;
1581 goto done;
1582 }
1583
1584 levels_printed = htab_create (20,
1585 htab_hash_pointer,
1586 htab_eq_pointer,
1587 NULL);
1588 make_cleanup_htab_delete (levels_printed);
1589
1590 while ((item = PyIter_Next (iterable)))
1591 {
1592 struct cleanup *item_cleanup = make_cleanup_py_decref (item);
1593
1594 success = py_print_frame (item, flags, args_type, out, 0,
1595 levels_printed);
1596
1597 do_cleanups (item_cleanup);
1598
1599 /* Do not exit on error printing a single frame. Print the
1600 error and continue with other frames. */
1601 if (success == EXT_LANG_BT_ERROR)
1602 gdbpy_print_stack ();
1603 }
1604
1605 if (item == NULL && PyErr_Occurred ())
1606 goto error;
1607
1608 done:
1609 do_cleanups (cleanups);
1610 return success;
1611
1612 /* Exit and abandon backtrace on error, printing the exception that
1613 is set. */
1614 error:
1615 gdbpy_print_stack ();
1616 do_cleanups (cleanups);
1617 return EXT_LANG_BT_ERROR;
1618 }
This page took 0.063119 seconds and 5 git commands to generate.