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