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