Commit | Line | Data |
---|---|---|
1e611234 PM |
1 | /* Python frame filters |
2 | ||
42a4f53d | 3 | Copyright (C) 2013-2019 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" | |
d4b0bb18 | 33 | #include "common/gdb_optional.h" |
1e611234 PM |
34 | |
35 | enum 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 | 55 | static enum ext_lang_bt_status |
9b972014 TT |
56 | extract_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 | { |
7780f186 | 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 | 126 | static enum ext_lang_bt_status |
1e611234 PM |
127 | extract_value (PyObject *obj, struct value **value) |
128 | { | |
129 | if (PyObject_HasAttrString (obj, "value")) | |
130 | { | |
7780f186 | 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. */ | |
164 | static int | |
165 | mi_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 | |
76c939ac | 201 | type will be extracted from. */ |
1e611234 | 202 | |
76c939ac | 203 | static void |
1e611234 PM |
204 | py_print_type (struct ui_out *out, struct value *val) |
205 | { | |
76c939ac | 206 | check_typedef (value_type (val)); |
1e611234 | 207 | |
76c939ac TT |
208 | string_file stb; |
209 | type_print (value_type (val), "", &stb, -1); | |
210 | out->field_stream ("type", stb); | |
1e611234 PM |
211 | } |
212 | ||
213 | /* Helper function which outputs a value to an output field in a | |
214 | stream. OUT is the ui-out structure the value will be output to, | |
215 | VAL is the value that will be printed, OPTS contains the value | |
216 | printing options, ARGS_TYPE is an enumerator describing the | |
217 | argument format, and LANGUAGE is the language_defn that the value | |
76c939ac | 218 | will be printed with. */ |
1e611234 | 219 | |
76c939ac | 220 | static void |
1e611234 PM |
221 | py_print_value (struct ui_out *out, struct value *val, |
222 | const struct value_print_options *opts, | |
223 | int indent, | |
6dddc817 | 224 | enum ext_lang_frame_args args_type, |
1e611234 PM |
225 | const struct language_defn *language) |
226 | { | |
227 | int should_print = 0; | |
1e611234 PM |
228 | |
229 | /* MI does not print certain values, differentiated by type, | |
230 | depending on what ARGS_TYPE indicates. Test type against option. | |
231 | For CLI print all values. */ | |
232 | if (args_type == MI_PRINT_SIMPLE_VALUES | |
233 | || args_type == MI_PRINT_ALL_VALUES) | |
234 | { | |
76c939ac | 235 | struct type *type = check_typedef (value_type (val)); |
1e611234 PM |
236 | |
237 | if (args_type == MI_PRINT_ALL_VALUES) | |
238 | should_print = 1; | |
239 | else if (args_type == MI_PRINT_SIMPLE_VALUES | |
240 | && TYPE_CODE (type) != TYPE_CODE_ARRAY | |
241 | && TYPE_CODE (type) != TYPE_CODE_STRUCT | |
242 | && TYPE_CODE (type) != TYPE_CODE_UNION) | |
243 | should_print = 1; | |
244 | } | |
245 | else if (args_type != NO_VALUES) | |
246 | should_print = 1; | |
247 | ||
248 | if (should_print) | |
249 | { | |
76c939ac | 250 | string_file stb; |
1e611234 | 251 | |
76c939ac TT |
252 | common_val_print (val, &stb, indent, opts, language); |
253 | out->field_stream ("value", stb); | |
1e611234 | 254 | } |
1e611234 PM |
255 | } |
256 | ||
257 | /* Helper function to call a Python method and extract an iterator | |
258 | from the result. If the function returns anything but an iterator | |
259 | the exception is preserved and NULL is returned. FILTER is the | |
260 | Python object to call, and FUNC is the name of the method. Returns | |
261 | a PyObject, or NULL on error with the appropriate exception set. | |
262 | This function can return an iterator, or NULL. */ | |
263 | ||
264 | static PyObject * | |
a121b7c1 | 265 | get_py_iter_from_func (PyObject *filter, const char *func) |
1e611234 PM |
266 | { |
267 | if (PyObject_HasAttrString (filter, func)) | |
268 | { | |
7780f186 | 269 | gdbpy_ref<> result (PyObject_CallMethod (filter, func, NULL)); |
1e611234 PM |
270 | |
271 | if (result != NULL) | |
272 | { | |
273 | if (result == Py_None) | |
274 | { | |
ee0a3fb8 | 275 | return result.release (); |
1e611234 PM |
276 | } |
277 | else | |
278 | { | |
ee0a3fb8 | 279 | return PyObject_GetIter (result.get ()); |
1e611234 PM |
280 | } |
281 | } | |
282 | } | |
283 | else | |
284 | Py_RETURN_NONE; | |
285 | ||
286 | return NULL; | |
287 | } | |
288 | ||
289 | /* Helper function to output a single frame argument and value to an | |
290 | output stream. This function will account for entry values if the | |
291 | FV parameter is populated, the frame argument has entry values | |
292 | associated with them, and the appropriate "set entry-value" | |
293 | options are set. Will output in CLI or MI like format depending | |
294 | on the type of output stream detected. OUT is the output stream, | |
295 | SYM_NAME is the name of the symbol. If SYM_NAME is populated then | |
296 | it must have an accompanying value in the parameter FV. FA is a | |
297 | frame argument structure. If FA is populated, both SYM_NAME and | |
298 | FV are ignored. OPTS contains the value printing options, | |
299 | ARGS_TYPE is an enumerator describing the argument format, | |
300 | PRINT_ARGS_FIELD is a flag which indicates if we output "ARGS=1" | |
301 | in MI output in commands where both arguments and locals are | |
76c939ac | 302 | printed. */ |
1e611234 | 303 | |
76c939ac | 304 | static void |
1e611234 PM |
305 | py_print_single_arg (struct ui_out *out, |
306 | const char *sym_name, | |
307 | struct frame_arg *fa, | |
308 | struct value *fv, | |
309 | const struct value_print_options *opts, | |
6dddc817 | 310 | enum ext_lang_frame_args args_type, |
1e611234 PM |
311 | int print_args_field, |
312 | const struct language_defn *language) | |
313 | { | |
314 | struct value *val; | |
1e611234 PM |
315 | |
316 | if (fa != NULL) | |
317 | { | |
c75bd3a2 | 318 | if (fa->val == NULL && fa->error == NULL) |
76c939ac | 319 | return; |
1e611234 PM |
320 | language = language_def (SYMBOL_LANGUAGE (fa->sym)); |
321 | val = fa->val; | |
322 | } | |
323 | else | |
324 | val = fv; | |
325 | ||
76c939ac | 326 | gdb::optional<ui_out_emit_tuple> maybe_tuple; |
1e611234 | 327 | |
76c939ac | 328 | /* MI has varying rules for tuples, but generally if there is only |
1e611234 PM |
329 | one element in each item in the list, do not start a tuple. The |
330 | exception is -stack-list-variables which emits an ARGS="1" field | |
331 | if the value is a frame argument. This is denoted in this | |
332 | function with PRINT_ARGS_FIELD which is flag from the caller to | |
333 | emit the ARGS field. */ | |
76c939ac TT |
334 | if (out->is_mi_like_p ()) |
335 | { | |
336 | if (print_args_field || args_type != NO_VALUES) | |
337 | maybe_tuple.emplace (out, nullptr); | |
338 | } | |
1e611234 | 339 | |
76c939ac TT |
340 | annotate_arg_begin (); |
341 | ||
342 | /* If frame argument is populated, check for entry-values and the | |
343 | entry value options. */ | |
344 | if (fa != NULL) | |
345 | { | |
346 | string_file stb; | |
1e611234 | 347 | |
76c939ac TT |
348 | fprintf_symbol_filtered (&stb, SYMBOL_PRINT_NAME (fa->sym), |
349 | SYMBOL_LANGUAGE (fa->sym), | |
350 | DMGL_PARAMS | DMGL_ANSI); | |
351 | if (fa->entry_kind == print_entry_values_compact) | |
1e611234 | 352 | { |
76c939ac | 353 | stb.puts ("="); |
1e611234 | 354 | |
d7e74731 | 355 | fprintf_symbol_filtered (&stb, SYMBOL_PRINT_NAME (fa->sym), |
1e611234 PM |
356 | SYMBOL_LANGUAGE (fa->sym), |
357 | DMGL_PARAMS | DMGL_ANSI); | |
1e611234 | 358 | } |
76c939ac TT |
359 | if (fa->entry_kind == print_entry_values_only |
360 | || fa->entry_kind == print_entry_values_compact) | |
361 | stb.puts ("@entry"); | |
362 | out->field_stream ("name", stb); | |
363 | } | |
364 | else | |
365 | /* Otherwise, just output the name. */ | |
366 | out->field_string ("name", sym_name); | |
1e611234 | 367 | |
76c939ac | 368 | annotate_arg_name_end (); |
1e611234 | 369 | |
2038b7fd | 370 | out->text ("="); |
1e611234 | 371 | |
76c939ac TT |
372 | if (print_args_field) |
373 | out->field_int ("arg", 1); | |
1e611234 | 374 | |
76c939ac TT |
375 | /* For MI print the type, but only for simple values. This seems |
376 | weird, but this is how MI choose to format the various output | |
377 | types. */ | |
378 | if (args_type == MI_PRINT_SIMPLE_VALUES && val != NULL) | |
379 | py_print_type (out, val); | |
1e611234 | 380 | |
76c939ac TT |
381 | if (val != NULL) |
382 | annotate_arg_value (value_type (val)); | |
9c6595ab | 383 | |
76c939ac TT |
384 | /* If the output is to the CLI, and the user option "set print |
385 | frame-arguments" is set to none, just output "...". */ | |
386 | if (! out->is_mi_like_p () && args_type == NO_VALUES) | |
387 | out->field_string ("value", "..."); | |
388 | else | |
389 | { | |
390 | /* Otherwise, print the value for both MI and the CLI, except | |
391 | for the case of MI_PRINT_NO_VALUES. */ | |
392 | if (args_type != NO_VALUES) | |
393 | { | |
394 | if (val == NULL) | |
1e611234 | 395 | { |
76c939ac TT |
396 | gdb_assert (fa != NULL && fa->error != NULL); |
397 | out->field_fmt ("value", | |
398 | _("<error reading variable: %s>"), | |
399 | fa->error); | |
1e611234 | 400 | } |
76c939ac TT |
401 | else |
402 | py_print_value (out, val, opts, 0, args_type, language); | |
9c6595ab | 403 | } |
1e611234 | 404 | } |
1e611234 PM |
405 | } |
406 | ||
407 | /* Helper function to loop over frame arguments provided by the | |
408 | "frame_arguments" Python API. Elements in the iterator must | |
409 | conform to the "Symbol Value" interface. ITER is the Python | |
410 | iterable object, OUT is the output stream, ARGS_TYPE is an | |
411 | enumerator describing the argument format, PRINT_ARGS_FIELD is a | |
412 | flag which indicates if we output "ARGS=1" in MI output in commands | |
413 | where both arguments and locals are printed, and FRAME is the | |
6dddc817 DE |
414 | backing frame. Returns EXT_LANG_BT_ERROR on error, with any GDB |
415 | exceptions converted to a Python exception, or EXT_LANG_BT_OK on | |
1e611234 PM |
416 | success. */ |
417 | ||
6dddc817 | 418 | static enum ext_lang_bt_status |
1e611234 PM |
419 | enumerate_args (PyObject *iter, |
420 | struct ui_out *out, | |
6dddc817 | 421 | enum ext_lang_frame_args args_type, |
1e611234 PM |
422 | int print_args_field, |
423 | struct frame_info *frame) | |
424 | { | |
1e611234 | 425 | struct value_print_options opts; |
1e611234 PM |
426 | |
427 | get_user_print_options (&opts); | |
428 | ||
429 | if (args_type == CLI_SCALAR_VALUES) | |
430 | { | |
431 | /* True in "summary" mode, false otherwise. */ | |
432 | opts.summary = 1; | |
433 | } | |
434 | ||
435 | opts.deref_ref = 1; | |
436 | ||
76c939ac | 437 | annotate_frame_args (); |
1e611234 PM |
438 | |
439 | /* Collect the first argument outside of the loop, so output of | |
440 | commas in the argument output is correct. At the end of the | |
441 | loop block collect another item from the iterator, and, if it is | |
442 | not null emit a comma. */ | |
7780f186 | 443 | gdbpy_ref<> item (PyIter_Next (iter)); |
1e611234 | 444 | if (item == NULL && PyErr_Occurred ()) |
06fc9bf7 | 445 | return EXT_LANG_BT_ERROR; |
1e611234 | 446 | |
06fc9bf7 | 447 | while (item != NULL) |
1e611234 PM |
448 | { |
449 | const struct language_defn *language; | |
9b972014 | 450 | gdb::unique_xmalloc_ptr<char> sym_name; |
1e611234 | 451 | struct symbol *sym; |
63e43d3a | 452 | struct block *sym_block; |
1e611234 | 453 | struct value *val; |
6dddc817 | 454 | enum ext_lang_bt_status success = EXT_LANG_BT_ERROR; |
1e611234 | 455 | |
06fc9bf7 TT |
456 | success = extract_sym (item.get (), &sym_name, &sym, &sym_block, |
457 | &language); | |
6dddc817 | 458 | if (success == EXT_LANG_BT_ERROR) |
06fc9bf7 | 459 | return EXT_LANG_BT_ERROR; |
1e611234 | 460 | |
06fc9bf7 | 461 | success = extract_value (item.get (), &val); |
6dddc817 | 462 | if (success == EXT_LANG_BT_ERROR) |
06fc9bf7 | 463 | return EXT_LANG_BT_ERROR; |
1e611234 | 464 | |
112e8700 | 465 | if (sym && out->is_mi_like_p () |
1e611234 | 466 | && ! mi_should_print (sym, MI_PRINT_ARGS)) |
9b972014 | 467 | continue; |
1e611234 PM |
468 | |
469 | /* If the object did not provide a value, read it using | |
470 | read_frame_args and account for entry values, if any. */ | |
471 | if (val == NULL) | |
472 | { | |
473 | struct frame_arg arg, entryarg; | |
474 | ||
475 | /* If there is no value, and also no symbol, set error and | |
476 | exit. */ | |
477 | if (sym == NULL) | |
478 | { | |
479 | PyErr_SetString (PyExc_RuntimeError, | |
480 | _("No symbol or value provided.")); | |
06fc9bf7 | 481 | return EXT_LANG_BT_ERROR; |
1e611234 PM |
482 | } |
483 | ||
76c939ac | 484 | read_frame_arg (sym, frame, &arg, &entryarg); |
1e611234 | 485 | |
7a630bc2 TT |
486 | gdb::unique_xmalloc_ptr<char> arg_holder (arg.error); |
487 | gdb::unique_xmalloc_ptr<char> entry_holder (entryarg.error); | |
488 | ||
1e611234 PM |
489 | /* The object has not provided a value, so this is a frame |
490 | argument to be read by GDB. In this case we have to | |
491 | account for entry-values. */ | |
492 | ||
493 | if (arg.entry_kind != print_entry_values_only) | |
494 | { | |
76c939ac TT |
495 | py_print_single_arg (out, NULL, &arg, |
496 | NULL, &opts, | |
497 | args_type, | |
498 | print_args_field, | |
499 | NULL); | |
1e611234 PM |
500 | } |
501 | ||
502 | if (entryarg.entry_kind != print_entry_values_no) | |
503 | { | |
504 | if (arg.entry_kind != print_entry_values_only) | |
505 | { | |
76c939ac TT |
506 | out->text (", "); |
507 | out->wrap_hint (" "); | |
1e611234 PM |
508 | } |
509 | ||
76c939ac TT |
510 | py_print_single_arg (out, NULL, &entryarg, NULL, &opts, |
511 | args_type, print_args_field, NULL); | |
1e611234 | 512 | } |
1e611234 PM |
513 | } |
514 | else | |
515 | { | |
516 | /* If the object has provided a value, we just print that. */ | |
517 | if (val != NULL) | |
76c939ac TT |
518 | py_print_single_arg (out, sym_name.get (), NULL, val, &opts, |
519 | args_type, print_args_field, | |
520 | language); | |
1e611234 PM |
521 | } |
522 | ||
1e611234 PM |
523 | /* Collect the next item from the iterator. If |
524 | this is the last item, do not print the | |
525 | comma. */ | |
06fc9bf7 | 526 | item.reset (PyIter_Next (iter)); |
1e611234 | 527 | if (item != NULL) |
76c939ac | 528 | out->text (", "); |
1e611234 | 529 | else if (PyErr_Occurred ()) |
06fc9bf7 | 530 | return EXT_LANG_BT_ERROR; |
1e611234 | 531 | |
76c939ac | 532 | annotate_arg_end (); |
1e611234 PM |
533 | } |
534 | ||
6dddc817 | 535 | return EXT_LANG_BT_OK; |
1e611234 PM |
536 | } |
537 | ||
538 | ||
539 | /* Helper function to loop over variables provided by the | |
540 | "frame_locals" Python API. Elements in the iterable must conform | |
541 | to the "Symbol Value" interface. ITER is the Python iterable | |
542 | object, OUT is the output stream, INDENT is whether we should | |
543 | indent the output (for CLI), ARGS_TYPE is an enumerator describing | |
544 | the argument format, PRINT_ARGS_FIELD is flag which indicates | |
545 | whether to output the ARGS field in the case of | |
546 | -stack-list-variables and FRAME is the backing frame. Returns | |
6dddc817 DE |
547 | EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to a Python |
548 | exception, or EXT_LANG_BT_OK on success. */ | |
1e611234 | 549 | |
6dddc817 | 550 | static enum ext_lang_bt_status |
1e611234 PM |
551 | enumerate_locals (PyObject *iter, |
552 | struct ui_out *out, | |
553 | int indent, | |
6dddc817 | 554 | enum ext_lang_frame_args args_type, |
1e611234 PM |
555 | int print_args_field, |
556 | struct frame_info *frame) | |
557 | { | |
1e611234 PM |
558 | struct value_print_options opts; |
559 | ||
560 | get_user_print_options (&opts); | |
561 | opts.deref_ref = 1; | |
562 | ||
13df46cc | 563 | while (true) |
1e611234 PM |
564 | { |
565 | const struct language_defn *language; | |
9b972014 | 566 | gdb::unique_xmalloc_ptr<char> sym_name; |
1e611234 | 567 | struct value *val; |
6dddc817 | 568 | enum ext_lang_bt_status success = EXT_LANG_BT_ERROR; |
1e611234 | 569 | struct symbol *sym; |
63e43d3a | 570 | struct block *sym_block; |
1e611234 | 571 | int local_indent = 8 + (8 * indent); |
d4b0bb18 | 572 | gdb::optional<ui_out_emit_tuple> tuple; |
1e611234 | 573 | |
7780f186 | 574 | gdbpy_ref<> item (PyIter_Next (iter)); |
13df46cc TT |
575 | if (item == NULL) |
576 | break; | |
1e611234 | 577 | |
13df46cc TT |
578 | success = extract_sym (item.get (), &sym_name, &sym, &sym_block, |
579 | &language); | |
6dddc817 | 580 | if (success == EXT_LANG_BT_ERROR) |
d4b0bb18 | 581 | return EXT_LANG_BT_ERROR; |
1e611234 | 582 | |
13df46cc | 583 | success = extract_value (item.get (), &val); |
6dddc817 | 584 | if (success == EXT_LANG_BT_ERROR) |
d4b0bb18 | 585 | return EXT_LANG_BT_ERROR; |
1e611234 | 586 | |
112e8700 | 587 | if (sym != NULL && out->is_mi_like_p () |
1e611234 | 588 | && ! mi_should_print (sym, MI_PRINT_LOCALS)) |
d4b0bb18 | 589 | continue; |
1e611234 PM |
590 | |
591 | /* If the object did not provide a value, read it. */ | |
592 | if (val == NULL) | |
76c939ac | 593 | val = read_var_value (sym, sym_block, frame); |
1e611234 PM |
594 | |
595 | /* With PRINT_NO_VALUES, MI does not emit a tuple normally as | |
596 | each output contains only one field. The exception is | |
597 | -stack-list-variables, which always provides a tuple. */ | |
112e8700 | 598 | if (out->is_mi_like_p ()) |
1e611234 PM |
599 | { |
600 | if (print_args_field || args_type != NO_VALUES) | |
d4b0bb18 | 601 | tuple.emplace (out, nullptr); |
1e611234 | 602 | } |
1e611234 | 603 | |
2038b7fd TT |
604 | /* If the output is not MI we indent locals. */ |
605 | out->spaces (local_indent); | |
76c939ac | 606 | out->field_string ("name", sym_name.get ()); |
2038b7fd | 607 | out->text (" = "); |
1e611234 PM |
608 | |
609 | if (args_type == MI_PRINT_SIMPLE_VALUES) | |
76c939ac | 610 | py_print_type (out, val); |
1e611234 PM |
611 | |
612 | /* CLI always prints values for locals. MI uses the | |
613 | simple/no/all system. */ | |
112e8700 | 614 | if (! out->is_mi_like_p ()) |
1e611234 PM |
615 | { |
616 | int val_indent = (indent + 1) * 4; | |
617 | ||
76c939ac TT |
618 | py_print_value (out, val, &opts, val_indent, args_type, |
619 | language); | |
1e611234 PM |
620 | } |
621 | else | |
622 | { | |
623 | if (args_type != NO_VALUES) | |
76c939ac TT |
624 | py_print_value (out, val, &opts, 0, args_type, |
625 | language); | |
1e611234 PM |
626 | } |
627 | ||
76c939ac | 628 | out->text ("\n"); |
1e611234 PM |
629 | } |
630 | ||
13df46cc TT |
631 | if (!PyErr_Occurred ()) |
632 | return EXT_LANG_BT_OK; | |
1e611234 | 633 | |
6dddc817 | 634 | return EXT_LANG_BT_ERROR; |
1e611234 PM |
635 | } |
636 | ||
6dddc817 DE |
637 | /* Helper function for -stack-list-variables. Returns EXT_LANG_BT_ERROR on |
638 | error, or EXT_LANG_BT_OK on success. */ | |
1e611234 | 639 | |
6dddc817 | 640 | static enum ext_lang_bt_status |
1e611234 PM |
641 | py_mi_print_variables (PyObject *filter, struct ui_out *out, |
642 | struct value_print_options *opts, | |
6dddc817 | 643 | enum ext_lang_frame_args args_type, |
1e611234 PM |
644 | struct frame_info *frame) |
645 | { | |
7780f186 | 646 | gdbpy_ref<> args_iter (get_py_iter_from_func (filter, "frame_args")); |
1e611234 | 647 | if (args_iter == NULL) |
13df46cc | 648 | return EXT_LANG_BT_ERROR; |
1e611234 | 649 | |
7780f186 | 650 | gdbpy_ref<> locals_iter (get_py_iter_from_func (filter, "frame_locals")); |
1e611234 | 651 | if (locals_iter == NULL) |
13df46cc | 652 | return EXT_LANG_BT_ERROR; |
1e611234 | 653 | |
d4b0bb18 | 654 | ui_out_emit_list list_emitter (out, "variables"); |
1e611234 | 655 | |
d4b0bb18 TT |
656 | if (args_iter != Py_None |
657 | && (enumerate_args (args_iter.get (), out, args_type, 1, frame) | |
658 | == EXT_LANG_BT_ERROR)) | |
659 | return EXT_LANG_BT_ERROR; | |
1e611234 | 660 | |
d4b0bb18 TT |
661 | if (locals_iter != Py_None |
662 | && (enumerate_locals (locals_iter.get (), out, 1, args_type, 1, frame) | |
663 | == EXT_LANG_BT_ERROR)) | |
664 | return EXT_LANG_BT_ERROR; | |
1e611234 | 665 | |
6dddc817 | 666 | return EXT_LANG_BT_OK; |
1e611234 PM |
667 | } |
668 | ||
669 | /* Helper function for printing locals. This function largely just | |
670 | creates the wrapping tuple, and calls enumerate_locals. Returns | |
6dddc817 | 671 | EXT_LANG_BT_ERROR on error, or EXT_LANG_BT_OK on success. */ |
1e611234 | 672 | |
6dddc817 | 673 | static enum ext_lang_bt_status |
1e611234 PM |
674 | py_print_locals (PyObject *filter, |
675 | struct ui_out *out, | |
6dddc817 | 676 | enum ext_lang_frame_args args_type, |
1e611234 PM |
677 | int indent, |
678 | struct frame_info *frame) | |
679 | { | |
7780f186 | 680 | gdbpy_ref<> locals_iter (get_py_iter_from_func (filter, "frame_locals")); |
1e611234 | 681 | if (locals_iter == NULL) |
13df46cc | 682 | return EXT_LANG_BT_ERROR; |
1e611234 | 683 | |
d4b0bb18 | 684 | ui_out_emit_list list_emitter (out, "locals"); |
1e611234 | 685 | |
d4b0bb18 TT |
686 | if (locals_iter != Py_None |
687 | && (enumerate_locals (locals_iter.get (), out, indent, args_type, | |
688 | 0, frame) == EXT_LANG_BT_ERROR)) | |
689 | return EXT_LANG_BT_ERROR; | |
1e611234 | 690 | |
6dddc817 | 691 | return EXT_LANG_BT_OK; |
1e611234 PM |
692 | } |
693 | ||
694 | /* Helper function for printing frame arguments. This function | |
695 | largely just creates the wrapping tuple, and calls enumerate_args. | |
6dddc817 DE |
696 | Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to |
697 | a Python exception, or EXT_LANG_BT_OK on success. */ | |
1e611234 | 698 | |
6dddc817 | 699 | static enum ext_lang_bt_status |
1e611234 PM |
700 | py_print_args (PyObject *filter, |
701 | struct ui_out *out, | |
6dddc817 | 702 | enum ext_lang_frame_args args_type, |
1e611234 PM |
703 | struct frame_info *frame) |
704 | { | |
7780f186 | 705 | gdbpy_ref<> args_iter (get_py_iter_from_func (filter, "frame_args")); |
1e611234 | 706 | if (args_iter == NULL) |
13df46cc | 707 | return EXT_LANG_BT_ERROR; |
1e611234 | 708 | |
d4b0bb18 | 709 | ui_out_emit_list list_emitter (out, "args"); |
1e611234 | 710 | |
76c939ac TT |
711 | out->wrap_hint (" "); |
712 | annotate_frame_args (); | |
2038b7fd | 713 | out->text (" ("); |
1e611234 | 714 | |
d4b0bb18 TT |
715 | if (args_iter != Py_None |
716 | && (enumerate_args (args_iter.get (), out, args_type, 0, frame) | |
717 | == EXT_LANG_BT_ERROR)) | |
718 | return EXT_LANG_BT_ERROR; | |
1e611234 | 719 | |
2038b7fd | 720 | out->text (")"); |
1e611234 | 721 | |
6dddc817 | 722 | return EXT_LANG_BT_OK; |
1e611234 PM |
723 | } |
724 | ||
725 | /* Print a single frame to the designated output stream, detecting | |
726 | whether the output is MI or console, and formatting the output | |
727 | according to the conventions of that protocol. FILTER is the | |
728 | frame-filter associated with this frame. FLAGS is an integer | |
729 | describing the various print options. The FLAGS variables is | |
730 | described in "apply_frame_filter" function. ARGS_TYPE is an | |
731 | enumerator describing the argument format. OUT is the output | |
732 | stream to print, INDENT is the level of indention for this frame | |
733 | (in the case of elided frames), and LEVELS_PRINTED is a hash-table | |
734 | containing all the frames level that have already been printed. | |
735 | If a frame level has been printed, do not print it again (in the | |
6dddc817 | 736 | case of elided frames). Returns EXT_LANG_BT_ERROR on error, with any |
63283d4a | 737 | GDB exceptions converted to a Python exception, or EXT_LANG_BT_OK |
b99bf4e3 | 738 | on success. It can also throw an exception RETURN_QUIT. */ |
1e611234 | 739 | |
6dddc817 | 740 | static enum ext_lang_bt_status |
d4dd3282 | 741 | py_print_frame (PyObject *filter, frame_filter_flags flags, |
6dddc817 | 742 | enum ext_lang_frame_args args_type, |
1e611234 PM |
743 | struct ui_out *out, int indent, htab_t levels_printed) |
744 | { | |
745 | int has_addr = 0; | |
746 | CORE_ADDR address = 0; | |
747 | struct gdbarch *gdbarch = NULL; | |
748 | struct frame_info *frame = NULL; | |
1e611234 | 749 | struct value_print_options opts; |
1e611234 | 750 | int print_level, print_frame_info, print_args, print_locals; |
9b972014 | 751 | gdb::unique_xmalloc_ptr<char> function_to_free; |
1e611234 PM |
752 | |
753 | /* Extract print settings from FLAGS. */ | |
754 | print_level = (flags & PRINT_LEVEL) ? 1 : 0; | |
755 | print_frame_info = (flags & PRINT_FRAME_INFO) ? 1 : 0; | |
756 | print_args = (flags & PRINT_ARGS) ? 1 : 0; | |
757 | print_locals = (flags & PRINT_LOCALS) ? 1 : 0; | |
758 | ||
759 | get_user_print_options (&opts); | |
760 | ||
761 | /* Get the underlying frame. This is needed to determine GDB | |
762 | architecture, and also, in the cases of frame variables/arguments to | |
763 | read them if they returned filter object requires us to do so. */ | |
7780f186 TT |
764 | gdbpy_ref<> py_inf_frame (PyObject_CallMethod (filter, "inferior_frame", |
765 | NULL)); | |
1e611234 | 766 | if (py_inf_frame == NULL) |
b99bf4e3 | 767 | return EXT_LANG_BT_ERROR; |
1e611234 | 768 | |
74c49d45 | 769 | frame = frame_object_to_frame_info (py_inf_frame.get ()); |
1e611234 | 770 | if (frame == NULL) |
b99bf4e3 | 771 | return EXT_LANG_BT_ERROR; |
1e611234 | 772 | |
76c939ac | 773 | gdbarch = get_frame_arch (frame); |
1e611234 | 774 | |
1e611234 PM |
775 | /* stack-list-variables. */ |
776 | if (print_locals && print_args && ! print_frame_info) | |
777 | { | |
778 | if (py_mi_print_variables (filter, out, &opts, | |
6dddc817 | 779 | args_type, frame) == EXT_LANG_BT_ERROR) |
b99bf4e3 | 780 | return EXT_LANG_BT_ERROR; |
63283d4a | 781 | return EXT_LANG_BT_OK; |
1e611234 PM |
782 | } |
783 | ||
d4b0bb18 | 784 | gdb::optional<ui_out_emit_tuple> tuple; |
b99bf4e3 | 785 | |
1e611234 PM |
786 | /* -stack-list-locals does not require a |
787 | wrapping frame attribute. */ | |
788 | if (print_frame_info || (print_args && ! print_locals)) | |
d4b0bb18 | 789 | tuple.emplace (out, "frame"); |
1e611234 PM |
790 | |
791 | if (print_frame_info) | |
792 | { | |
793 | /* Elided frames are also printed with this function (recursively) | |
794 | and are printed with indention. */ | |
795 | if (indent > 0) | |
76c939ac | 796 | out->spaces (indent * 4); |
1e611234 PM |
797 | |
798 | /* The address is required for frame annotations, and also for | |
799 | address printing. */ | |
800 | if (PyObject_HasAttrString (filter, "address")) | |
801 | { | |
7780f186 | 802 | gdbpy_ref<> paddr (PyObject_CallMethod (filter, "address", NULL)); |
34019068 JK |
803 | |
804 | if (paddr == NULL) | |
d4b0bb18 | 805 | return EXT_LANG_BT_ERROR; |
34019068 JK |
806 | |
807 | if (paddr != Py_None) | |
1e611234 | 808 | { |
74c49d45 | 809 | if (get_addr_from_python (paddr.get (), &address) < 0) |
d4b0bb18 | 810 | return EXT_LANG_BT_ERROR; |
30a7bb83 | 811 | |
34019068 | 812 | has_addr = 1; |
1e611234 | 813 | } |
1e611234 PM |
814 | } |
815 | } | |
816 | ||
817 | /* Print frame level. MI does not require the level if | |
818 | locals/variables only are being printed. */ | |
819 | if ((print_frame_info || print_args) && print_level) | |
820 | { | |
821 | struct frame_info **slot; | |
822 | int level; | |
1e611234 PM |
823 | |
824 | slot = (struct frame_info **) htab_find_slot (levels_printed, | |
825 | frame, INSERT); | |
76c939ac TT |
826 | |
827 | level = frame_relative_level (frame); | |
828 | ||
829 | /* Check if this frame has already been printed (there are cases | |
830 | where elided synthetic dummy-frames have to 'borrow' the frame | |
831 | architecture from the eliding frame. If that is the case, do | |
832 | not print 'level', but print spaces. */ | |
833 | if (*slot == frame) | |
834 | out->field_skip ("level"); | |
835 | else | |
1e611234 | 836 | { |
76c939ac TT |
837 | *slot = frame; |
838 | annotate_frame_begin (print_level ? level : 0, | |
839 | gdbarch, address); | |
840 | out->text ("#"); | |
841 | out->field_fmt_int (2, ui_left, "level", | |
842 | level); | |
1e611234 PM |
843 | } |
844 | } | |
845 | ||
846 | if (print_frame_info) | |
847 | { | |
848 | /* Print address to the address field. If an address is not provided, | |
849 | print nothing. */ | |
850 | if (opts.addressprint && has_addr) | |
851 | { | |
76c939ac TT |
852 | annotate_frame_address (); |
853 | out->field_core_addr ("addr", gdbarch, address); | |
854 | annotate_frame_address_end (); | |
855 | out->text (" in "); | |
1e611234 PM |
856 | } |
857 | ||
858 | /* Print frame function name. */ | |
859 | if (PyObject_HasAttrString (filter, "function")) | |
860 | { | |
7780f186 | 861 | gdbpy_ref<> py_func (PyObject_CallMethod (filter, "function", NULL)); |
34019068 | 862 | const char *function = NULL; |
1e611234 | 863 | |
34019068 | 864 | if (py_func == NULL) |
d4b0bb18 | 865 | return EXT_LANG_BT_ERROR; |
1e611234 | 866 | |
3b4e0e01 | 867 | if (gdbpy_is_string (py_func.get ())) |
34019068 | 868 | { |
3b4e0e01 | 869 | function_to_free = python_string_to_host_string (py_func.get ()); |
1e611234 | 870 | |
9b972014 | 871 | if (function_to_free == NULL) |
d4b0bb18 | 872 | return EXT_LANG_BT_ERROR; |
9b972014 TT |
873 | |
874 | function = function_to_free.get (); | |
34019068 | 875 | } |
3b4e0e01 | 876 | else if (PyLong_Check (py_func.get ())) |
34019068 | 877 | { |
30a7bb83 | 878 | CORE_ADDR addr; |
34019068 | 879 | struct bound_minimal_symbol msymbol; |
1e611234 | 880 | |
3b4e0e01 | 881 | if (get_addr_from_python (py_func.get (), &addr) < 0) |
d4b0bb18 | 882 | return EXT_LANG_BT_ERROR; |
34019068 JK |
883 | |
884 | msymbol = lookup_minimal_symbol_by_pc (addr); | |
885 | if (msymbol.minsym != NULL) | |
886 | function = MSYMBOL_PRINT_NAME (msymbol.minsym); | |
887 | } | |
888 | else if (py_func != Py_None) | |
889 | { | |
890 | PyErr_SetString (PyExc_RuntimeError, | |
891 | _("FrameDecorator.function: expecting a " \ | |
892 | "String, integer or None.")); | |
800eb1ce | 893 | return EXT_LANG_BT_ERROR; |
1e611234 | 894 | } |
34019068 | 895 | |
76c939ac TT |
896 | annotate_frame_function_name (); |
897 | if (function == NULL) | |
898 | out->field_skip ("func"); | |
899 | else | |
cbe56571 | 900 | out->field_string ("func", function, ui_out_style_kind::FUNCTION); |
1e611234 | 901 | } |
1e611234 PM |
902 | } |
903 | ||
904 | ||
905 | /* Frame arguments. Check the result, and error if something went | |
906 | wrong. */ | |
907 | if (print_args) | |
908 | { | |
6dddc817 | 909 | if (py_print_args (filter, out, args_type, frame) == EXT_LANG_BT_ERROR) |
d4b0bb18 | 910 | return EXT_LANG_BT_ERROR; |
1e611234 PM |
911 | } |
912 | ||
913 | /* File name/source/line number information. */ | |
914 | if (print_frame_info) | |
915 | { | |
76c939ac | 916 | annotate_frame_source_begin (); |
1e611234 PM |
917 | |
918 | if (PyObject_HasAttrString (filter, "filename")) | |
919 | { | |
7780f186 | 920 | gdbpy_ref<> py_fn (PyObject_CallMethod (filter, "filename", NULL)); |
8d4a54e2 | 921 | |
34019068 | 922 | if (py_fn == NULL) |
d4b0bb18 | 923 | return EXT_LANG_BT_ERROR; |
34019068 JK |
924 | |
925 | if (py_fn != Py_None) | |
1e611234 | 926 | { |
9b972014 | 927 | gdb::unique_xmalloc_ptr<char> |
3b4e0e01 | 928 | filename (python_string_to_host_string (py_fn.get ())); |
1e611234 | 929 | |
34019068 | 930 | if (filename == NULL) |
d4b0bb18 | 931 | return EXT_LANG_BT_ERROR; |
8f28f522 | 932 | |
76c939ac TT |
933 | out->wrap_hint (" "); |
934 | out->text (" at "); | |
935 | annotate_frame_source_file (); | |
cbe56571 TT |
936 | out->field_string ("file", filename.get (), |
937 | ui_out_style_kind::FILE); | |
76c939ac | 938 | annotate_frame_source_file_end (); |
1e611234 | 939 | } |
1e611234 PM |
940 | } |
941 | ||
942 | if (PyObject_HasAttrString (filter, "line")) | |
943 | { | |
7780f186 | 944 | gdbpy_ref<> py_line (PyObject_CallMethod (filter, "line", NULL)); |
1e611234 PM |
945 | int line; |
946 | ||
34019068 | 947 | if (py_line == NULL) |
d4b0bb18 | 948 | return EXT_LANG_BT_ERROR; |
34019068 JK |
949 | |
950 | if (py_line != Py_None) | |
1e611234 | 951 | { |
3b4e0e01 | 952 | line = PyLong_AsLong (py_line.get ()); |
30a7bb83 | 953 | if (PyErr_Occurred ()) |
d4b0bb18 | 954 | return EXT_LANG_BT_ERROR; |
30a7bb83 | 955 | |
76c939ac TT |
956 | out->text (":"); |
957 | annotate_frame_source_line (); | |
958 | out->field_int ("line", line); | |
1e611234 | 959 | } |
1e611234 | 960 | } |
3bf9c013 JV |
961 | if (out->is_mi_like_p ()) |
962 | out->field_string ("arch", | |
963 | (gdbarch_bfd_arch_info (gdbarch))->printable_name); | |
1e611234 PM |
964 | } |
965 | ||
966 | /* For MI we need to deal with the "children" list population of | |
967 | elided frames, so if MI output detected do not send newline. */ | |
112e8700 | 968 | if (! out->is_mi_like_p ()) |
1e611234 | 969 | { |
76c939ac TT |
970 | annotate_frame_end (); |
971 | out->text ("\n"); | |
1e611234 PM |
972 | } |
973 | ||
974 | if (print_locals) | |
975 | { | |
976 | if (py_print_locals (filter, out, args_type, indent, | |
6dddc817 | 977 | frame) == EXT_LANG_BT_ERROR) |
d4b0bb18 | 978 | return EXT_LANG_BT_ERROR; |
1e611234 PM |
979 | } |
980 | ||
978d6c75 TT |
981 | if ((flags & PRINT_HIDE) == 0) |
982 | { | |
983 | /* Finally recursively print elided frames, if any. */ | |
984 | gdbpy_ref<> elided (get_py_iter_from_func (filter, "elided")); | |
985 | if (elided == NULL) | |
986 | return EXT_LANG_BT_ERROR; | |
1e611234 | 987 | |
978d6c75 TT |
988 | if (elided != Py_None) |
989 | { | |
990 | PyObject *item; | |
1e611234 | 991 | |
978d6c75 | 992 | ui_out_emit_list inner_list_emiter (out, "children"); |
1e611234 | 993 | |
2038b7fd | 994 | indent++; |
1e611234 | 995 | |
978d6c75 TT |
996 | while ((item = PyIter_Next (elided.get ()))) |
997 | { | |
998 | gdbpy_ref<> item_ref (item); | |
b99bf4e3 | 999 | |
978d6c75 TT |
1000 | enum ext_lang_bt_status success |
1001 | = py_print_frame (item, flags, args_type, out, indent, | |
1002 | levels_printed); | |
1e611234 | 1003 | |
978d6c75 TT |
1004 | if (success == EXT_LANG_BT_ERROR) |
1005 | return EXT_LANG_BT_ERROR; | |
1006 | } | |
1007 | if (item == NULL && PyErr_Occurred ()) | |
1008 | return EXT_LANG_BT_ERROR; | |
1009 | } | |
1010 | } | |
1e611234 | 1011 | |
63283d4a | 1012 | return EXT_LANG_BT_OK; |
1e611234 PM |
1013 | } |
1014 | ||
1015 | /* Helper function to initiate frame filter invocation at starting | |
1016 | frame FRAME. */ | |
1017 | ||
1018 | static PyObject * | |
1019 | bootstrap_python_frame_filters (struct frame_info *frame, | |
1020 | int frame_low, int frame_high) | |
1021 | { | |
7780f186 | 1022 | gdbpy_ref<> frame_obj (frame_info_to_frame_object (frame)); |
1e611234 | 1023 | if (frame_obj == NULL) |
ee0a3fb8 | 1024 | return NULL; |
1e611234 | 1025 | |
7780f186 | 1026 | gdbpy_ref<> module (PyImport_ImportModule ("gdb.frames")); |
1e611234 | 1027 | if (module == NULL) |
ee0a3fb8 | 1028 | return NULL; |
1e611234 | 1029 | |
7780f186 TT |
1030 | gdbpy_ref<> sort_func (PyObject_GetAttrString (module.get (), |
1031 | "execute_frame_filters")); | |
1e611234 | 1032 | if (sort_func == NULL) |
ee0a3fb8 | 1033 | return NULL; |
1e611234 | 1034 | |
7780f186 | 1035 | gdbpy_ref<> py_frame_low (PyInt_FromLong (frame_low)); |
1e611234 | 1036 | if (py_frame_low == NULL) |
ee0a3fb8 | 1037 | return NULL; |
1e611234 | 1038 | |
7780f186 | 1039 | gdbpy_ref<> py_frame_high (PyInt_FromLong (frame_high)); |
1e611234 | 1040 | if (py_frame_high == NULL) |
ee0a3fb8 | 1041 | return NULL; |
1e611234 | 1042 | |
7780f186 TT |
1043 | gdbpy_ref<> iterable (PyObject_CallFunctionObjArgs (sort_func.get (), |
1044 | frame_obj.get (), | |
1045 | py_frame_low.get (), | |
1046 | py_frame_high.get (), | |
1047 | NULL)); | |
1e611234 | 1048 | if (iterable == NULL) |
ee0a3fb8 | 1049 | return NULL; |
1e611234 PM |
1050 | |
1051 | if (iterable != Py_None) | |
ee0a3fb8 | 1052 | return PyObject_GetIter (iterable.get ()); |
1e611234 | 1053 | else |
ee0a3fb8 | 1054 | return iterable.release (); |
1e611234 PM |
1055 | } |
1056 | ||
1057 | /* This is the only publicly exported function in this file. FRAME | |
1058 | is the source frame to start frame-filter invocation. FLAGS is an | |
1059 | integer holding the flags for printing. The following elements of | |
1060 | the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS: | |
1061 | PRINT_LEVEL is a flag indicating whether to print the frame's | |
1062 | relative level in the output. PRINT_FRAME_INFO is a flag that | |
1063 | indicates whether this function should print the frame | |
1064 | information, PRINT_ARGS is a flag that indicates whether to print | |
1065 | frame arguments, and PRINT_LOCALS, likewise, with frame local | |
1066 | variables. ARGS_TYPE is an enumerator describing the argument | |
1067 | format, OUT is the output stream to print. FRAME_LOW is the | |
1068 | beginning of the slice of frames to print, and FRAME_HIGH is the | |
6dddc817 | 1069 | upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error, |
63283d4a | 1070 | or EXT_LANG_BT_OK on success. */ |
6dddc817 DE |
1071 | |
1072 | enum ext_lang_bt_status | |
1073 | gdbpy_apply_frame_filter (const struct extension_language_defn *extlang, | |
d4dd3282 | 1074 | struct frame_info *frame, frame_filter_flags flags, |
6dddc817 DE |
1075 | enum ext_lang_frame_args args_type, |
1076 | struct ui_out *out, int frame_low, int frame_high) | |
1e611234 PM |
1077 | { |
1078 | struct gdbarch *gdbarch = NULL; | |
6dddc817 | 1079 | enum ext_lang_bt_status success = EXT_LANG_BT_ERROR; |
1e611234 | 1080 | |
8ee002df | 1081 | if (!gdb_python_initialized) |
6dddc817 | 1082 | return EXT_LANG_BT_NO_FILTERS; |
8ee002df | 1083 | |
492d29ea | 1084 | TRY |
1e611234 PM |
1085 | { |
1086 | gdbarch = get_frame_arch (frame); | |
1087 | } | |
92256134 | 1088 | CATCH (except, RETURN_MASK_ERROR) |
1e611234 | 1089 | { |
21909fa1 | 1090 | /* Let gdb try to print the stack trace. */ |
6dddc817 | 1091 | return EXT_LANG_BT_NO_FILTERS; |
1e611234 | 1092 | } |
492d29ea | 1093 | END_CATCH |
1e611234 | 1094 | |
6349f452 | 1095 | gdbpy_enter enter_py (gdbarch, current_language); |
21909fa1 | 1096 | |
6893c19a TT |
1097 | /* When we're limiting the number of frames, be careful to request |
1098 | one extra frame, so that we can print a message if there are more | |
1099 | frames. */ | |
1100 | int frame_countdown = -1; | |
1101 | if ((flags & PRINT_MORE_FRAMES) != 0 && frame_low >= 0 && frame_high >= 0) | |
1102 | { | |
1103 | ++frame_high; | |
1104 | /* This has an extra +1 because it is checked before a frame is | |
1105 | printed. */ | |
1106 | frame_countdown = frame_high - frame_low + 1; | |
1107 | } | |
1108 | ||
7780f186 TT |
1109 | gdbpy_ref<> iterable (bootstrap_python_frame_filters (frame, frame_low, |
1110 | frame_high)); | |
1e611234 PM |
1111 | |
1112 | if (iterable == NULL) | |
8ee002df PM |
1113 | { |
1114 | /* Normally if there is an error GDB prints the exception, | |
1115 | abandons the backtrace and exits. The user can then call "bt | |
1116 | no-filters", and get a default backtrace (it would be | |
1117 | confusing to automatically start a standard backtrace halfway | |
1118 | through a Python filtered backtrace). However in the case | |
1119 | where GDB cannot initialize the frame filters (most likely | |
1120 | due to incorrect auto-load paths), GDB has printed nothing. | |
1121 | In this case it is OK to print the default backtrace after | |
6dddc817 | 1122 | printing the error message. GDB returns EXT_LANG_BT_NO_FILTERS |
8ee002df PM |
1123 | here to signify there are no filters after printing the |
1124 | initialization error. This return code will trigger a | |
1125 | default backtrace. */ | |
1126 | ||
6ef2312a | 1127 | gdbpy_print_stack_or_quit (); |
6dddc817 | 1128 | return EXT_LANG_BT_NO_FILTERS; |
8ee002df | 1129 | } |
1e611234 PM |
1130 | |
1131 | /* If iterable is None, then there are no frame filters registered. | |
1132 | If this is the case, defer to default GDB printing routines in MI | |
1133 | and CLI. */ | |
1e611234 | 1134 | if (iterable == Py_None) |
6349f452 | 1135 | return EXT_LANG_BT_NO_FILTERS; |
1e611234 | 1136 | |
6349f452 TT |
1137 | htab_up levels_printed (htab_create (20, |
1138 | htab_hash_pointer, | |
1139 | htab_eq_pointer, | |
1140 | NULL)); | |
1e611234 | 1141 | |
6349f452 | 1142 | while (true) |
1e611234 | 1143 | { |
7780f186 | 1144 | gdbpy_ref<> item (PyIter_Next (iterable.get ())); |
b99bf4e3 | 1145 | |
6349f452 TT |
1146 | if (item == NULL) |
1147 | { | |
1148 | if (PyErr_Occurred ()) | |
1149 | { | |
6ef2312a | 1150 | gdbpy_print_stack_or_quit (); |
6349f452 TT |
1151 | return EXT_LANG_BT_ERROR; |
1152 | } | |
1153 | break; | |
1154 | } | |
1e611234 | 1155 | |
6893c19a TT |
1156 | if (frame_countdown != -1) |
1157 | { | |
1158 | gdb_assert ((flags & PRINT_MORE_FRAMES) != 0); | |
1159 | --frame_countdown; | |
1160 | if (frame_countdown == 0) | |
1161 | { | |
1162 | /* We've printed all the frames we were asked to | |
1163 | print, but more frames existed. */ | |
1164 | printf_filtered (_("(More stack frames follow...)\n")); | |
1165 | break; | |
1166 | } | |
1167 | } | |
1168 | ||
76c939ac TT |
1169 | TRY |
1170 | { | |
1171 | success = py_print_frame (item.get (), flags, args_type, out, 0, | |
1172 | levels_printed.get ()); | |
1173 | } | |
1174 | CATCH (except, RETURN_MASK_ERROR) | |
1175 | { | |
1176 | gdbpy_convert_exception (except); | |
1177 | success = EXT_LANG_BT_ERROR; | |
1178 | } | |
1179 | END_CATCH | |
b99bf4e3 | 1180 | |
1e611234 PM |
1181 | /* Do not exit on error printing a single frame. Print the |
1182 | error and continue with other frames. */ | |
6dddc817 | 1183 | if (success == EXT_LANG_BT_ERROR) |
6ef2312a | 1184 | gdbpy_print_stack_or_quit (); |
1e611234 PM |
1185 | } |
1186 | ||
1e611234 | 1187 | return success; |
1e611234 | 1188 | } |