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