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