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