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