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