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