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