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