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