Commit | Line | Data |
---|---|---|
1e611234 PM |
1 | /* Python frame filters |
2 | ||
618f726f | 3 | Copyright (C) 2013-2016 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" | |
33 | ||
34 | enum mi_print_types | |
35 | { | |
36 | MI_PRINT_ARGS, | |
37 | MI_PRINT_LOCALS | |
38 | }; | |
39 | ||
40 | /* Helper function to extract a symbol, a name and a language | |
41 | definition from a Python object that conforms to the "Symbol Value" | |
42 | interface. OBJ is the Python object to extract the values from. | |
43 | NAME is a pass-through argument where the name of the symbol will | |
44 | be written. NAME is allocated in this function, but the caller is | |
45 | responsible for clean up. SYM is a pass-through argument where the | |
63e43d3a PMR |
46 | symbol will be written and SYM_BLOCK is a pass-through argument to |
47 | write the block where the symbol lies in. In the case of the API | |
48 | returning a string, this will be set to NULL. LANGUAGE is also a | |
49 | pass-through argument denoting the language attributed to the | |
50 | Symbol. In the case of SYM being NULL, this will be set to the | |
51 | current language. Returns EXT_LANG_BT_ERROR on error with the | |
52 | appropriate Python exception set, and EXT_LANG_BT_OK on success. */ | |
1e611234 | 53 | |
6dddc817 | 54 | static enum ext_lang_bt_status |
1e611234 | 55 | extract_sym (PyObject *obj, char **name, struct symbol **sym, |
63e43d3a | 56 | struct block **sym_block, const struct language_defn **language) |
1e611234 PM |
57 | { |
58 | PyObject *result = PyObject_CallMethod (obj, "symbol", NULL); | |
59 | ||
60 | if (result == NULL) | |
6dddc817 | 61 | return EXT_LANG_BT_ERROR; |
1e611234 PM |
62 | |
63 | /* For 'symbol' callback, the function can return a symbol or a | |
64 | string. */ | |
65 | if (gdbpy_is_string (result)) | |
66 | { | |
67 | *name = python_string_to_host_string (result); | |
68 | Py_DECREF (result); | |
69 | ||
70 | if (*name == NULL) | |
6dddc817 | 71 | return EXT_LANG_BT_ERROR; |
1e611234 PM |
72 | /* If the API returns a string (and not a symbol), then there is |
73 | no symbol derived language available and the frame filter has | |
74 | either overridden the symbol with a string, or supplied a | |
75 | entirely synthetic symbol/value pairing. In that case, use | |
76 | python_language. */ | |
77 | *language = python_language; | |
78 | *sym = NULL; | |
63e43d3a | 79 | *sym_block = NULL; |
1e611234 PM |
80 | } |
81 | else | |
82 | { | |
83 | /* This type checks 'result' during the conversion so we | |
84 | just call it unconditionally and check the return. */ | |
85 | *sym = symbol_object_to_symbol (result); | |
63e43d3a PMR |
86 | /* TODO: currently, we have no way to recover the block in which SYMBOL |
87 | was found, so we have no block to return. Trying to evaluate SYMBOL | |
88 | will yield an incorrect value when it's located in a FRAME and | |
89 | evaluated from another frame (as permitted in nested functions). */ | |
90 | *sym_block = NULL; | |
1e611234 PM |
91 | |
92 | Py_DECREF (result); | |
93 | ||
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. */ | |
104 | *name = xstrdup (SYMBOL_PRINT_NAME (*sym)); | |
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 | { | |
132 | PyObject *vresult = PyObject_CallMethod (obj, "value", NULL); | |
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 | { | |
142 | Py_DECREF (vresult); | |
143 | *value = NULL; | |
6dddc817 | 144 | return EXT_LANG_BT_OK; |
1e611234 PM |
145 | } |
146 | else | |
147 | { | |
148 | *value = convert_value_from_python (vresult); | |
149 | Py_DECREF (vresult); | |
150 | ||
151 | if (*value == NULL) | |
6dddc817 | 152 | return EXT_LANG_BT_ERROR; |
1e611234 | 153 | |
6dddc817 | 154 | return EXT_LANG_BT_OK; |
1e611234 PM |
155 | } |
156 | } | |
157 | else | |
158 | *value = NULL; | |
159 | ||
6dddc817 | 160 | return EXT_LANG_BT_OK; |
1e611234 PM |
161 | } |
162 | ||
163 | /* MI prints only certain values according to the type of symbol and | |
164 | also what the user has specified. SYM is the symbol to check, and | |
165 | MI_PRINT_TYPES is an enum specifying what the user wants emitted | |
166 | for the MI command in question. */ | |
167 | static int | |
168 | mi_should_print (struct symbol *sym, enum mi_print_types type) | |
169 | { | |
170 | int print_me = 0; | |
171 | ||
172 | switch (SYMBOL_CLASS (sym)) | |
173 | { | |
174 | default: | |
175 | case LOC_UNDEF: /* catches errors */ | |
176 | case LOC_CONST: /* constant */ | |
177 | case LOC_TYPEDEF: /* local typedef */ | |
178 | case LOC_LABEL: /* local label */ | |
179 | case LOC_BLOCK: /* local function */ | |
180 | case LOC_CONST_BYTES: /* loc. byte seq. */ | |
181 | case LOC_UNRESOLVED: /* unresolved static */ | |
182 | case LOC_OPTIMIZED_OUT: /* optimized out */ | |
183 | print_me = 0; | |
184 | break; | |
185 | ||
186 | case LOC_ARG: /* argument */ | |
187 | case LOC_REF_ARG: /* reference arg */ | |
188 | case LOC_REGPARM_ADDR: /* indirect register arg */ | |
189 | case LOC_LOCAL: /* stack local */ | |
190 | case LOC_STATIC: /* static */ | |
191 | case LOC_REGISTER: /* register */ | |
192 | case LOC_COMPUTED: /* computed location */ | |
193 | if (type == MI_PRINT_LOCALS) | |
194 | print_me = ! SYMBOL_IS_ARGUMENT (sym); | |
195 | else | |
196 | print_me = SYMBOL_IS_ARGUMENT (sym); | |
197 | } | |
198 | return print_me; | |
199 | } | |
200 | ||
201 | /* Helper function which outputs a type name extracted from VAL to a | |
202 | "type" field in the output stream OUT. OUT is the ui-out structure | |
203 | the type name will be output too, and VAL is the value that the | |
6dddc817 DE |
204 | type will be extracted from. Returns EXT_LANG_BT_ERROR on error, with |
205 | any GDB exceptions converted to a Python exception, or EXT_LANG_BT_OK on | |
1e611234 PM |
206 | success. */ |
207 | ||
6dddc817 | 208 | static enum ext_lang_bt_status |
1e611234 PM |
209 | py_print_type (struct ui_out *out, struct value *val) |
210 | { | |
1e611234 | 211 | |
492d29ea | 212 | TRY |
1e611234 | 213 | { |
1e611234 PM |
214 | struct ui_file *stb; |
215 | struct cleanup *cleanup; | |
216 | ||
217 | stb = mem_fileopen (); | |
218 | cleanup = make_cleanup_ui_file_delete (stb); | |
78cc6c2d | 219 | check_typedef (value_type (val)); |
1e611234 PM |
220 | type_print (value_type (val), "", stb, -1); |
221 | ui_out_field_stream (out, "type", stb); | |
222 | do_cleanups (cleanup); | |
223 | } | |
492d29ea | 224 | CATCH (except, RETURN_MASK_ALL) |
1e611234 PM |
225 | { |
226 | gdbpy_convert_exception (except); | |
6dddc817 | 227 | return EXT_LANG_BT_ERROR; |
1e611234 | 228 | } |
492d29ea | 229 | END_CATCH |
1e611234 | 230 | |
6dddc817 | 231 | return EXT_LANG_BT_OK; |
1e611234 PM |
232 | } |
233 | ||
234 | /* Helper function which outputs a value to an output field in a | |
235 | stream. OUT is the ui-out structure the value will be output to, | |
236 | VAL is the value that will be printed, OPTS contains the value | |
237 | printing options, ARGS_TYPE is an enumerator describing the | |
238 | argument format, and LANGUAGE is the language_defn that the value | |
6dddc817 DE |
239 | will be printed with. Returns EXT_LANG_BT_ERROR on error, with any GDB |
240 | exceptions converted to a Python exception, or EXT_LANG_BT_OK on | |
1e611234 PM |
241 | success. */ |
242 | ||
6dddc817 | 243 | static enum ext_lang_bt_status |
1e611234 PM |
244 | py_print_value (struct ui_out *out, struct value *val, |
245 | const struct value_print_options *opts, | |
246 | int indent, | |
6dddc817 | 247 | enum ext_lang_frame_args args_type, |
1e611234 PM |
248 | const struct language_defn *language) |
249 | { | |
250 | int should_print = 0; | |
1e611234 PM |
251 | |
252 | /* MI does not print certain values, differentiated by type, | |
253 | depending on what ARGS_TYPE indicates. Test type against option. | |
254 | For CLI print all values. */ | |
255 | if (args_type == MI_PRINT_SIMPLE_VALUES | |
256 | || args_type == MI_PRINT_ALL_VALUES) | |
257 | { | |
258 | struct type *type = NULL; | |
259 | ||
492d29ea | 260 | TRY |
1e611234 PM |
261 | { |
262 | type = check_typedef (value_type (val)); | |
263 | } | |
492d29ea | 264 | CATCH (except, RETURN_MASK_ALL) |
1e611234 PM |
265 | { |
266 | gdbpy_convert_exception (except); | |
6dddc817 | 267 | return EXT_LANG_BT_ERROR; |
1e611234 | 268 | } |
492d29ea | 269 | END_CATCH |
1e611234 PM |
270 | |
271 | if (args_type == MI_PRINT_ALL_VALUES) | |
272 | should_print = 1; | |
273 | else if (args_type == MI_PRINT_SIMPLE_VALUES | |
274 | && TYPE_CODE (type) != TYPE_CODE_ARRAY | |
275 | && TYPE_CODE (type) != TYPE_CODE_STRUCT | |
276 | && TYPE_CODE (type) != TYPE_CODE_UNION) | |
277 | should_print = 1; | |
278 | } | |
279 | else if (args_type != NO_VALUES) | |
280 | should_print = 1; | |
281 | ||
282 | if (should_print) | |
283 | { | |
492d29ea | 284 | TRY |
1e611234 PM |
285 | { |
286 | struct ui_file *stb; | |
287 | struct cleanup *cleanup; | |
288 | ||
289 | stb = mem_fileopen (); | |
290 | cleanup = make_cleanup_ui_file_delete (stb); | |
291 | common_val_print (val, stb, indent, opts, language); | |
292 | ui_out_field_stream (out, "value", stb); | |
293 | do_cleanups (cleanup); | |
294 | } | |
492d29ea | 295 | CATCH (except, RETURN_MASK_ALL) |
1e611234 PM |
296 | { |
297 | gdbpy_convert_exception (except); | |
6dddc817 | 298 | return EXT_LANG_BT_ERROR; |
1e611234 | 299 | } |
492d29ea | 300 | END_CATCH |
1e611234 PM |
301 | } |
302 | ||
6dddc817 | 303 | return EXT_LANG_BT_OK; |
1e611234 PM |
304 | } |
305 | ||
306 | /* Helper function to call a Python method and extract an iterator | |
307 | from the result. If the function returns anything but an iterator | |
308 | the exception is preserved and NULL is returned. FILTER is the | |
309 | Python object to call, and FUNC is the name of the method. Returns | |
310 | a PyObject, or NULL on error with the appropriate exception set. | |
311 | This function can return an iterator, or NULL. */ | |
312 | ||
313 | static PyObject * | |
314 | get_py_iter_from_func (PyObject *filter, char *func) | |
315 | { | |
316 | if (PyObject_HasAttrString (filter, func)) | |
317 | { | |
318 | PyObject *result = PyObject_CallMethod (filter, func, NULL); | |
319 | ||
320 | if (result != NULL) | |
321 | { | |
322 | if (result == Py_None) | |
323 | { | |
324 | return result; | |
325 | } | |
326 | else | |
327 | { | |
328 | PyObject *iterator = PyObject_GetIter (result); | |
329 | ||
330 | Py_DECREF (result); | |
331 | return iterator; | |
332 | } | |
333 | } | |
334 | } | |
335 | else | |
336 | Py_RETURN_NONE; | |
337 | ||
338 | return NULL; | |
339 | } | |
340 | ||
341 | /* Helper function to output a single frame argument and value to an | |
342 | output stream. This function will account for entry values if the | |
343 | FV parameter is populated, the frame argument has entry values | |
344 | associated with them, and the appropriate "set entry-value" | |
345 | options are set. Will output in CLI or MI like format depending | |
346 | on the type of output stream detected. OUT is the output stream, | |
347 | SYM_NAME is the name of the symbol. If SYM_NAME is populated then | |
348 | it must have an accompanying value in the parameter FV. FA is a | |
349 | frame argument structure. If FA is populated, both SYM_NAME and | |
350 | FV are ignored. OPTS contains the value printing options, | |
351 | ARGS_TYPE is an enumerator describing the argument format, | |
352 | PRINT_ARGS_FIELD is a flag which indicates if we output "ARGS=1" | |
353 | in MI output in commands where both arguments and locals are | |
6dddc817 DE |
354 | printed. Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions |
355 | converted to a Python exception, or EXT_LANG_BT_OK on success. */ | |
1e611234 | 356 | |
6dddc817 | 357 | static enum ext_lang_bt_status |
1e611234 PM |
358 | py_print_single_arg (struct ui_out *out, |
359 | const char *sym_name, | |
360 | struct frame_arg *fa, | |
361 | struct value *fv, | |
362 | const struct value_print_options *opts, | |
6dddc817 | 363 | enum ext_lang_frame_args args_type, |
1e611234 PM |
364 | int print_args_field, |
365 | const struct language_defn *language) | |
366 | { | |
367 | struct value *val; | |
c75bd3a2 | 368 | enum ext_lang_bt_status retval = EXT_LANG_BT_OK; |
1e611234 PM |
369 | |
370 | if (fa != NULL) | |
371 | { | |
c75bd3a2 JK |
372 | if (fa->val == NULL && fa->error == NULL) |
373 | return EXT_LANG_BT_OK; | |
1e611234 PM |
374 | language = language_def (SYMBOL_LANGUAGE (fa->sym)); |
375 | val = fa->val; | |
376 | } | |
377 | else | |
378 | val = fv; | |
379 | ||
492d29ea | 380 | TRY |
1e611234 PM |
381 | { |
382 | struct cleanup *cleanups = make_cleanup (null_cleanup, NULL); | |
383 | ||
384 | /* MI has varying rules for tuples, but generally if there is only | |
385 | one element in each item in the list, do not start a tuple. The | |
386 | exception is -stack-list-variables which emits an ARGS="1" field | |
387 | if the value is a frame argument. This is denoted in this | |
388 | function with PRINT_ARGS_FIELD which is flag from the caller to | |
389 | emit the ARGS field. */ | |
390 | if (ui_out_is_mi_like_p (out)) | |
391 | { | |
392 | if (print_args_field || args_type != NO_VALUES) | |
393 | make_cleanup_ui_out_tuple_begin_end (out, NULL); | |
394 | } | |
395 | ||
396 | annotate_arg_begin (); | |
397 | ||
398 | /* If frame argument is populated, check for entry-values and the | |
399 | entry value options. */ | |
400 | if (fa != NULL) | |
401 | { | |
402 | struct ui_file *stb; | |
403 | ||
404 | stb = mem_fileopen (); | |
405 | make_cleanup_ui_file_delete (stb); | |
406 | fprintf_symbol_filtered (stb, SYMBOL_PRINT_NAME (fa->sym), | |
407 | SYMBOL_LANGUAGE (fa->sym), | |
408 | DMGL_PARAMS | DMGL_ANSI); | |
409 | if (fa->entry_kind == print_entry_values_compact) | |
410 | { | |
411 | fputs_filtered ("=", stb); | |
412 | ||
413 | fprintf_symbol_filtered (stb, SYMBOL_PRINT_NAME (fa->sym), | |
414 | SYMBOL_LANGUAGE (fa->sym), | |
415 | DMGL_PARAMS | DMGL_ANSI); | |
416 | } | |
417 | if (fa->entry_kind == print_entry_values_only | |
418 | || fa->entry_kind == print_entry_values_compact) | |
419 | { | |
420 | fputs_filtered ("@entry", stb); | |
421 | } | |
422 | ui_out_field_stream (out, "name", stb); | |
423 | } | |
424 | else | |
425 | /* Otherwise, just output the name. */ | |
426 | ui_out_field_string (out, "name", sym_name); | |
427 | ||
428 | annotate_arg_name_end (); | |
429 | ||
430 | if (! ui_out_is_mi_like_p (out)) | |
431 | ui_out_text (out, "="); | |
432 | ||
433 | if (print_args_field) | |
434 | ui_out_field_int (out, "arg", 1); | |
435 | ||
436 | /* For MI print the type, but only for simple values. This seems | |
437 | weird, but this is how MI choose to format the various output | |
438 | types. */ | |
c75bd3a2 | 439 | if (args_type == MI_PRINT_SIMPLE_VALUES && val != NULL) |
1e611234 | 440 | { |
6dddc817 | 441 | if (py_print_type (out, val) == EXT_LANG_BT_ERROR) |
1e611234 | 442 | { |
c75bd3a2 | 443 | retval = EXT_LANG_BT_ERROR; |
1e611234 | 444 | do_cleanups (cleanups); |
1e611234 PM |
445 | } |
446 | } | |
447 | ||
9c6595ab | 448 | if (retval != EXT_LANG_BT_ERROR) |
1e611234 | 449 | { |
9c6595ab PA |
450 | if (val != NULL) |
451 | annotate_arg_value (value_type (val)); | |
452 | ||
453 | /* If the output is to the CLI, and the user option "set print | |
454 | frame-arguments" is set to none, just output "...". */ | |
455 | if (! ui_out_is_mi_like_p (out) && args_type == NO_VALUES) | |
456 | ui_out_field_string (out, "value", "..."); | |
457 | else | |
1e611234 | 458 | { |
9c6595ab PA |
459 | /* Otherwise, print the value for both MI and the CLI, except |
460 | for the case of MI_PRINT_NO_VALUES. */ | |
461 | if (args_type != NO_VALUES) | |
1e611234 | 462 | { |
9c6595ab PA |
463 | if (val == NULL) |
464 | { | |
465 | gdb_assert (fa != NULL && fa->error != NULL); | |
466 | ui_out_field_fmt (out, "value", | |
467 | _("<error reading variable: %s>"), | |
468 | fa->error); | |
469 | } | |
470 | else if (py_print_value (out, val, opts, 0, args_type, language) | |
471 | == EXT_LANG_BT_ERROR) | |
472 | retval = EXT_LANG_BT_ERROR; | |
1e611234 PM |
473 | } |
474 | } | |
1e611234 | 475 | |
9c6595ab PA |
476 | do_cleanups (cleanups); |
477 | } | |
1e611234 | 478 | } |
492d29ea PA |
479 | CATCH (except, RETURN_MASK_ERROR) |
480 | { | |
481 | gdbpy_convert_exception (except); | |
482 | } | |
483 | END_CATCH | |
1e611234 | 484 | |
c75bd3a2 | 485 | return retval; |
1e611234 PM |
486 | } |
487 | ||
488 | /* Helper function to loop over frame arguments provided by the | |
489 | "frame_arguments" Python API. Elements in the iterator must | |
490 | conform to the "Symbol Value" interface. ITER is the Python | |
491 | iterable object, OUT is the output stream, ARGS_TYPE is an | |
492 | enumerator describing the argument format, PRINT_ARGS_FIELD is a | |
493 | flag which indicates if we output "ARGS=1" in MI output in commands | |
494 | where both arguments and locals are printed, and FRAME is the | |
6dddc817 DE |
495 | backing frame. Returns EXT_LANG_BT_ERROR on error, with any GDB |
496 | exceptions converted to a Python exception, or EXT_LANG_BT_OK on | |
1e611234 PM |
497 | success. */ |
498 | ||
6dddc817 | 499 | static enum ext_lang_bt_status |
1e611234 PM |
500 | enumerate_args (PyObject *iter, |
501 | struct ui_out *out, | |
6dddc817 | 502 | enum ext_lang_frame_args args_type, |
1e611234 PM |
503 | int print_args_field, |
504 | struct frame_info *frame) | |
505 | { | |
506 | PyObject *item; | |
507 | struct value_print_options opts; | |
1e611234 PM |
508 | |
509 | get_user_print_options (&opts); | |
510 | ||
511 | if (args_type == CLI_SCALAR_VALUES) | |
512 | { | |
513 | /* True in "summary" mode, false otherwise. */ | |
514 | opts.summary = 1; | |
515 | } | |
516 | ||
517 | opts.deref_ref = 1; | |
518 | ||
492d29ea | 519 | TRY |
1e611234 PM |
520 | { |
521 | annotate_frame_args (); | |
522 | } | |
492d29ea | 523 | CATCH (except, RETURN_MASK_ALL) |
1e611234 PM |
524 | { |
525 | gdbpy_convert_exception (except); | |
526 | goto error; | |
527 | } | |
492d29ea | 528 | END_CATCH |
1e611234 PM |
529 | |
530 | /* Collect the first argument outside of the loop, so output of | |
531 | commas in the argument output is correct. At the end of the | |
532 | loop block collect another item from the iterator, and, if it is | |
533 | not null emit a comma. */ | |
534 | item = PyIter_Next (iter); | |
535 | if (item == NULL && PyErr_Occurred ()) | |
536 | goto error; | |
537 | ||
538 | while (item) | |
539 | { | |
540 | const struct language_defn *language; | |
541 | char *sym_name; | |
542 | struct symbol *sym; | |
63e43d3a | 543 | struct block *sym_block; |
1e611234 | 544 | struct value *val; |
6dddc817 | 545 | enum ext_lang_bt_status success = EXT_LANG_BT_ERROR; |
1e611234 | 546 | |
63e43d3a | 547 | success = extract_sym (item, &sym_name, &sym, &sym_block, &language); |
6dddc817 | 548 | if (success == EXT_LANG_BT_ERROR) |
1e611234 PM |
549 | { |
550 | Py_DECREF (item); | |
551 | goto error; | |
552 | } | |
553 | ||
554 | success = extract_value (item, &val); | |
6dddc817 | 555 | if (success == EXT_LANG_BT_ERROR) |
1e611234 PM |
556 | { |
557 | xfree (sym_name); | |
558 | Py_DECREF (item); | |
559 | goto error; | |
560 | } | |
561 | ||
562 | Py_DECREF (item); | |
563 | item = NULL; | |
564 | ||
565 | if (sym && ui_out_is_mi_like_p (out) | |
566 | && ! mi_should_print (sym, MI_PRINT_ARGS)) | |
567 | { | |
568 | xfree (sym_name); | |
569 | continue; | |
570 | } | |
571 | ||
572 | /* If the object did not provide a value, read it using | |
573 | read_frame_args and account for entry values, if any. */ | |
574 | if (val == NULL) | |
575 | { | |
576 | struct frame_arg arg, entryarg; | |
577 | ||
578 | /* If there is no value, and also no symbol, set error and | |
579 | exit. */ | |
580 | if (sym == NULL) | |
581 | { | |
582 | PyErr_SetString (PyExc_RuntimeError, | |
583 | _("No symbol or value provided.")); | |
584 | xfree (sym_name); | |
585 | goto error; | |
586 | } | |
587 | ||
492d29ea | 588 | TRY |
1e611234 PM |
589 | { |
590 | read_frame_arg (sym, frame, &arg, &entryarg); | |
591 | } | |
492d29ea | 592 | CATCH (except, RETURN_MASK_ALL) |
1e611234 PM |
593 | { |
594 | xfree (sym_name); | |
595 | gdbpy_convert_exception (except); | |
596 | goto error; | |
597 | } | |
492d29ea | 598 | END_CATCH |
1e611234 PM |
599 | |
600 | /* The object has not provided a value, so this is a frame | |
601 | argument to be read by GDB. In this case we have to | |
602 | account for entry-values. */ | |
603 | ||
604 | if (arg.entry_kind != print_entry_values_only) | |
605 | { | |
606 | if (py_print_single_arg (out, NULL, &arg, | |
607 | NULL, &opts, | |
608 | args_type, | |
609 | print_args_field, | |
6dddc817 | 610 | NULL) == EXT_LANG_BT_ERROR) |
1e611234 PM |
611 | { |
612 | xfree (arg.error); | |
613 | xfree (entryarg.error); | |
614 | xfree (sym_name); | |
615 | goto error; | |
616 | } | |
617 | } | |
618 | ||
619 | if (entryarg.entry_kind != print_entry_values_no) | |
620 | { | |
621 | if (arg.entry_kind != print_entry_values_only) | |
622 | { | |
492d29ea | 623 | TRY |
1e611234 PM |
624 | { |
625 | ui_out_text (out, ", "); | |
626 | ui_out_wrap_hint (out, " "); | |
627 | } | |
492d29ea | 628 | CATCH (except, RETURN_MASK_ALL) |
1e611234 PM |
629 | { |
630 | xfree (arg.error); | |
631 | xfree (entryarg.error); | |
632 | xfree (sym_name); | |
633 | gdbpy_convert_exception (except); | |
634 | goto error; | |
635 | } | |
492d29ea | 636 | END_CATCH |
1e611234 PM |
637 | } |
638 | ||
6dddc817 DE |
639 | if (py_print_single_arg (out, NULL, &entryarg, NULL, &opts, |
640 | args_type, print_args_field, NULL) | |
641 | == EXT_LANG_BT_ERROR) | |
1e611234 PM |
642 | { |
643 | xfree (arg.error); | |
644 | xfree (entryarg.error); | |
645 | xfree (sym_name); | |
646 | goto error; | |
647 | } | |
648 | } | |
649 | ||
650 | xfree (arg.error); | |
651 | xfree (entryarg.error); | |
652 | } | |
653 | else | |
654 | { | |
655 | /* If the object has provided a value, we just print that. */ | |
656 | if (val != NULL) | |
657 | { | |
658 | if (py_print_single_arg (out, sym_name, NULL, val, &opts, | |
659 | args_type, print_args_field, | |
6dddc817 | 660 | language) == EXT_LANG_BT_ERROR) |
1e611234 PM |
661 | { |
662 | xfree (sym_name); | |
663 | goto error; | |
664 | } | |
665 | } | |
666 | } | |
667 | ||
668 | xfree (sym_name); | |
669 | ||
670 | /* Collect the next item from the iterator. If | |
671 | this is the last item, do not print the | |
672 | comma. */ | |
673 | item = PyIter_Next (iter); | |
674 | if (item != NULL) | |
675 | { | |
492d29ea | 676 | TRY |
1e611234 PM |
677 | { |
678 | ui_out_text (out, ", "); | |
679 | } | |
492d29ea | 680 | CATCH (except, RETURN_MASK_ALL) |
1e611234 PM |
681 | { |
682 | Py_DECREF (item); | |
683 | gdbpy_convert_exception (except); | |
684 | goto error; | |
685 | } | |
492d29ea | 686 | END_CATCH |
1e611234 PM |
687 | } |
688 | else if (PyErr_Occurred ()) | |
689 | goto error; | |
690 | ||
492d29ea | 691 | TRY |
1e611234 PM |
692 | { |
693 | annotate_arg_end (); | |
694 | } | |
492d29ea | 695 | CATCH (except, RETURN_MASK_ALL) |
1e611234 PM |
696 | { |
697 | Py_DECREF (item); | |
698 | gdbpy_convert_exception (except); | |
699 | goto error; | |
700 | } | |
492d29ea | 701 | END_CATCH |
1e611234 PM |
702 | } |
703 | ||
6dddc817 | 704 | return EXT_LANG_BT_OK; |
1e611234 PM |
705 | |
706 | error: | |
6dddc817 | 707 | return EXT_LANG_BT_ERROR; |
1e611234 PM |
708 | } |
709 | ||
710 | ||
711 | /* Helper function to loop over variables provided by the | |
712 | "frame_locals" Python API. Elements in the iterable must conform | |
713 | to the "Symbol Value" interface. ITER is the Python iterable | |
714 | object, OUT is the output stream, INDENT is whether we should | |
715 | indent the output (for CLI), ARGS_TYPE is an enumerator describing | |
716 | the argument format, PRINT_ARGS_FIELD is flag which indicates | |
717 | whether to output the ARGS field in the case of | |
718 | -stack-list-variables and FRAME is the backing frame. Returns | |
6dddc817 DE |
719 | EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to a Python |
720 | exception, or EXT_LANG_BT_OK on success. */ | |
1e611234 | 721 | |
6dddc817 | 722 | static enum ext_lang_bt_status |
1e611234 PM |
723 | enumerate_locals (PyObject *iter, |
724 | struct ui_out *out, | |
725 | int indent, | |
6dddc817 | 726 | enum ext_lang_frame_args args_type, |
1e611234 PM |
727 | int print_args_field, |
728 | struct frame_info *frame) | |
729 | { | |
730 | PyObject *item; | |
731 | struct value_print_options opts; | |
732 | ||
733 | get_user_print_options (&opts); | |
734 | opts.deref_ref = 1; | |
735 | ||
736 | while ((item = PyIter_Next (iter))) | |
737 | { | |
738 | const struct language_defn *language; | |
739 | char *sym_name; | |
740 | struct value *val; | |
6dddc817 | 741 | enum ext_lang_bt_status success = EXT_LANG_BT_ERROR; |
1e611234 | 742 | struct symbol *sym; |
63e43d3a | 743 | struct block *sym_block; |
1e611234 PM |
744 | int local_indent = 8 + (8 * indent); |
745 | struct cleanup *locals_cleanups; | |
746 | ||
747 | locals_cleanups = make_cleanup_py_decref (item); | |
748 | ||
63e43d3a | 749 | success = extract_sym (item, &sym_name, &sym, &sym_block, &language); |
6dddc817 | 750 | if (success == EXT_LANG_BT_ERROR) |
1e611234 PM |
751 | { |
752 | do_cleanups (locals_cleanups); | |
753 | goto error; | |
754 | } | |
755 | ||
756 | make_cleanup (xfree, sym_name); | |
757 | ||
758 | success = extract_value (item, &val); | |
6dddc817 | 759 | if (success == EXT_LANG_BT_ERROR) |
1e611234 PM |
760 | { |
761 | do_cleanups (locals_cleanups); | |
762 | goto error; | |
763 | } | |
764 | ||
765 | if (sym != NULL && ui_out_is_mi_like_p (out) | |
766 | && ! mi_should_print (sym, MI_PRINT_LOCALS)) | |
767 | { | |
768 | do_cleanups (locals_cleanups); | |
769 | continue; | |
770 | } | |
771 | ||
772 | /* If the object did not provide a value, read it. */ | |
773 | if (val == NULL) | |
774 | { | |
492d29ea | 775 | TRY |
1e611234 | 776 | { |
63e43d3a | 777 | val = read_var_value (sym, sym_block, frame); |
1e611234 | 778 | } |
492d29ea | 779 | CATCH (except, RETURN_MASK_ERROR) |
1e611234 PM |
780 | { |
781 | gdbpy_convert_exception (except); | |
782 | do_cleanups (locals_cleanups); | |
783 | goto error; | |
784 | } | |
492d29ea | 785 | END_CATCH |
1e611234 PM |
786 | } |
787 | ||
788 | /* With PRINT_NO_VALUES, MI does not emit a tuple normally as | |
789 | each output contains only one field. The exception is | |
790 | -stack-list-variables, which always provides a tuple. */ | |
791 | if (ui_out_is_mi_like_p (out)) | |
792 | { | |
793 | if (print_args_field || args_type != NO_VALUES) | |
794 | make_cleanup_ui_out_tuple_begin_end (out, NULL); | |
795 | } | |
492d29ea | 796 | TRY |
1e611234 PM |
797 | { |
798 | if (! ui_out_is_mi_like_p (out)) | |
799 | { | |
800 | /* If the output is not MI we indent locals. */ | |
801 | ui_out_spaces (out, local_indent); | |
802 | } | |
803 | ||
804 | ui_out_field_string (out, "name", sym_name); | |
805 | ||
806 | if (! ui_out_is_mi_like_p (out)) | |
807 | ui_out_text (out, " = "); | |
808 | } | |
492d29ea | 809 | CATCH (except, RETURN_MASK_ERROR) |
1e611234 PM |
810 | { |
811 | gdbpy_convert_exception (except); | |
812 | do_cleanups (locals_cleanups); | |
813 | goto error; | |
814 | } | |
492d29ea | 815 | END_CATCH |
1e611234 PM |
816 | |
817 | if (args_type == MI_PRINT_SIMPLE_VALUES) | |
818 | { | |
6dddc817 | 819 | if (py_print_type (out, val) == EXT_LANG_BT_ERROR) |
1e611234 PM |
820 | { |
821 | do_cleanups (locals_cleanups); | |
822 | goto error; | |
823 | } | |
824 | } | |
825 | ||
826 | /* CLI always prints values for locals. MI uses the | |
827 | simple/no/all system. */ | |
828 | if (! ui_out_is_mi_like_p (out)) | |
829 | { | |
830 | int val_indent = (indent + 1) * 4; | |
831 | ||
832 | if (py_print_value (out, val, &opts, val_indent, args_type, | |
6dddc817 | 833 | language) == EXT_LANG_BT_ERROR) |
1e611234 PM |
834 | { |
835 | do_cleanups (locals_cleanups); | |
836 | goto error; | |
837 | } | |
838 | } | |
839 | else | |
840 | { | |
841 | if (args_type != NO_VALUES) | |
842 | { | |
843 | if (py_print_value (out, val, &opts, 0, args_type, | |
6dddc817 | 844 | language) == EXT_LANG_BT_ERROR) |
1e611234 PM |
845 | { |
846 | do_cleanups (locals_cleanups); | |
847 | goto error; | |
848 | } | |
849 | } | |
850 | } | |
851 | ||
852 | do_cleanups (locals_cleanups); | |
853 | ||
492d29ea | 854 | TRY |
1e611234 PM |
855 | { |
856 | ui_out_text (out, "\n"); | |
857 | } | |
492d29ea | 858 | CATCH (except, RETURN_MASK_ERROR) |
1e611234 PM |
859 | { |
860 | gdbpy_convert_exception (except); | |
861 | goto error; | |
862 | } | |
492d29ea | 863 | END_CATCH |
1e611234 PM |
864 | } |
865 | ||
866 | if (item == NULL && PyErr_Occurred ()) | |
867 | goto error; | |
868 | ||
6dddc817 | 869 | return EXT_LANG_BT_OK; |
1e611234 PM |
870 | |
871 | error: | |
6dddc817 | 872 | return EXT_LANG_BT_ERROR; |
1e611234 PM |
873 | } |
874 | ||
6dddc817 DE |
875 | /* Helper function for -stack-list-variables. Returns EXT_LANG_BT_ERROR on |
876 | error, or EXT_LANG_BT_OK on success. */ | |
1e611234 | 877 | |
6dddc817 | 878 | static enum ext_lang_bt_status |
1e611234 PM |
879 | py_mi_print_variables (PyObject *filter, struct ui_out *out, |
880 | struct value_print_options *opts, | |
6dddc817 | 881 | enum ext_lang_frame_args args_type, |
1e611234 PM |
882 | struct frame_info *frame) |
883 | { | |
884 | struct cleanup *old_chain; | |
885 | PyObject *args_iter; | |
886 | PyObject *locals_iter; | |
887 | ||
888 | args_iter = get_py_iter_from_func (filter, "frame_args"); | |
889 | old_chain = make_cleanup_py_xdecref (args_iter); | |
890 | if (args_iter == NULL) | |
891 | goto error; | |
892 | ||
893 | locals_iter = get_py_iter_from_func (filter, "frame_locals"); | |
894 | if (locals_iter == NULL) | |
895 | goto error; | |
896 | ||
897 | make_cleanup_py_decref (locals_iter); | |
898 | make_cleanup_ui_out_list_begin_end (out, "variables"); | |
899 | ||
900 | if (args_iter != Py_None) | |
6dddc817 DE |
901 | if (enumerate_args (args_iter, out, args_type, 1, frame) |
902 | == EXT_LANG_BT_ERROR) | |
1e611234 PM |
903 | goto error; |
904 | ||
905 | if (locals_iter != Py_None) | |
906 | if (enumerate_locals (locals_iter, out, 1, args_type, 1, frame) | |
6dddc817 | 907 | == EXT_LANG_BT_ERROR) |
1e611234 PM |
908 | goto error; |
909 | ||
910 | do_cleanups (old_chain); | |
6dddc817 | 911 | return EXT_LANG_BT_OK; |
1e611234 PM |
912 | |
913 | error: | |
914 | do_cleanups (old_chain); | |
6dddc817 | 915 | return EXT_LANG_BT_ERROR; |
1e611234 PM |
916 | } |
917 | ||
918 | /* Helper function for printing locals. This function largely just | |
919 | creates the wrapping tuple, and calls enumerate_locals. Returns | |
6dddc817 | 920 | EXT_LANG_BT_ERROR on error, or EXT_LANG_BT_OK on success. */ |
1e611234 | 921 | |
6dddc817 | 922 | static enum ext_lang_bt_status |
1e611234 PM |
923 | py_print_locals (PyObject *filter, |
924 | struct ui_out *out, | |
6dddc817 | 925 | enum ext_lang_frame_args args_type, |
1e611234 PM |
926 | int indent, |
927 | struct frame_info *frame) | |
928 | { | |
929 | PyObject *locals_iter = get_py_iter_from_func (filter, | |
930 | "frame_locals"); | |
931 | struct cleanup *old_chain = make_cleanup_py_xdecref (locals_iter); | |
932 | ||
933 | if (locals_iter == NULL) | |
934 | goto locals_error; | |
935 | ||
936 | make_cleanup_ui_out_list_begin_end (out, "locals"); | |
937 | ||
938 | if (locals_iter != Py_None) | |
939 | if (enumerate_locals (locals_iter, out, indent, args_type, | |
6dddc817 | 940 | 0, frame) == EXT_LANG_BT_ERROR) |
1e611234 PM |
941 | goto locals_error; |
942 | ||
943 | do_cleanups (old_chain); | |
6dddc817 | 944 | return EXT_LANG_BT_OK; |
1e611234 PM |
945 | |
946 | locals_error: | |
947 | do_cleanups (old_chain); | |
6dddc817 | 948 | return EXT_LANG_BT_ERROR; |
1e611234 PM |
949 | } |
950 | ||
951 | /* Helper function for printing frame arguments. This function | |
952 | largely just creates the wrapping tuple, and calls enumerate_args. | |
6dddc817 DE |
953 | Returns EXT_LANG_BT_ERROR on error, with any GDB exceptions converted to |
954 | a Python exception, or EXT_LANG_BT_OK on success. */ | |
1e611234 | 955 | |
6dddc817 | 956 | static enum ext_lang_bt_status |
1e611234 PM |
957 | py_print_args (PyObject *filter, |
958 | struct ui_out *out, | |
6dddc817 | 959 | enum ext_lang_frame_args args_type, |
1e611234 PM |
960 | struct frame_info *frame) |
961 | { | |
962 | PyObject *args_iter = get_py_iter_from_func (filter, "frame_args"); | |
963 | struct cleanup *old_chain = make_cleanup_py_xdecref (args_iter); | |
1e611234 PM |
964 | |
965 | if (args_iter == NULL) | |
966 | goto args_error; | |
967 | ||
968 | make_cleanup_ui_out_list_begin_end (out, "args"); | |
969 | ||
492d29ea | 970 | TRY |
1e611234 PM |
971 | { |
972 | annotate_frame_args (); | |
973 | if (! ui_out_is_mi_like_p (out)) | |
974 | ui_out_text (out, " ("); | |
975 | } | |
492d29ea | 976 | CATCH (except, RETURN_MASK_ALL) |
1e611234 PM |
977 | { |
978 | gdbpy_convert_exception (except); | |
979 | goto args_error; | |
980 | } | |
492d29ea | 981 | END_CATCH |
1e611234 PM |
982 | |
983 | if (args_iter != Py_None) | |
6dddc817 DE |
984 | if (enumerate_args (args_iter, out, args_type, 0, frame) |
985 | == EXT_LANG_BT_ERROR) | |
1e611234 PM |
986 | goto args_error; |
987 | ||
492d29ea | 988 | TRY |
1e611234 PM |
989 | { |
990 | if (! ui_out_is_mi_like_p (out)) | |
991 | ui_out_text (out, ")"); | |
992 | } | |
492d29ea | 993 | CATCH (except, RETURN_MASK_ALL) |
1e611234 PM |
994 | { |
995 | gdbpy_convert_exception (except); | |
996 | goto args_error; | |
997 | } | |
492d29ea | 998 | END_CATCH |
1e611234 PM |
999 | |
1000 | do_cleanups (old_chain); | |
6dddc817 | 1001 | return EXT_LANG_BT_OK; |
1e611234 PM |
1002 | |
1003 | args_error: | |
1004 | do_cleanups (old_chain); | |
6dddc817 | 1005 | return EXT_LANG_BT_ERROR; |
1e611234 PM |
1006 | } |
1007 | ||
1008 | /* Print a single frame to the designated output stream, detecting | |
1009 | whether the output is MI or console, and formatting the output | |
1010 | according to the conventions of that protocol. FILTER is the | |
1011 | frame-filter associated with this frame. FLAGS is an integer | |
1012 | describing the various print options. The FLAGS variables is | |
1013 | described in "apply_frame_filter" function. ARGS_TYPE is an | |
1014 | enumerator describing the argument format. OUT is the output | |
1015 | stream to print, INDENT is the level of indention for this frame | |
1016 | (in the case of elided frames), and LEVELS_PRINTED is a hash-table | |
1017 | containing all the frames level that have already been printed. | |
1018 | If a frame level has been printed, do not print it again (in the | |
6dddc817 DE |
1019 | case of elided frames). Returns EXT_LANG_BT_ERROR on error, with any |
1020 | GDB exceptions converted to a Python exception, or EXT_LANG_BT_COMPLETED | |
b99bf4e3 | 1021 | on success. It can also throw an exception RETURN_QUIT. */ |
1e611234 | 1022 | |
6dddc817 DE |
1023 | static enum ext_lang_bt_status |
1024 | py_print_frame (PyObject *filter, int flags, | |
1025 | enum ext_lang_frame_args args_type, | |
1e611234 PM |
1026 | struct ui_out *out, int indent, htab_t levels_printed) |
1027 | { | |
1028 | int has_addr = 0; | |
1029 | CORE_ADDR address = 0; | |
1030 | struct gdbarch *gdbarch = NULL; | |
1031 | struct frame_info *frame = NULL; | |
b99bf4e3 | 1032 | struct cleanup *cleanup_stack; |
1e611234 | 1033 | struct value_print_options opts; |
34019068 | 1034 | PyObject *py_inf_frame; |
1e611234 | 1035 | int print_level, print_frame_info, print_args, print_locals; |
1e611234 PM |
1036 | |
1037 | /* Extract print settings from FLAGS. */ | |
1038 | print_level = (flags & PRINT_LEVEL) ? 1 : 0; | |
1039 | print_frame_info = (flags & PRINT_FRAME_INFO) ? 1 : 0; | |
1040 | print_args = (flags & PRINT_ARGS) ? 1 : 0; | |
1041 | print_locals = (flags & PRINT_LOCALS) ? 1 : 0; | |
1042 | ||
1043 | get_user_print_options (&opts); | |
1044 | ||
1045 | /* Get the underlying frame. This is needed to determine GDB | |
1046 | architecture, and also, in the cases of frame variables/arguments to | |
1047 | read them if they returned filter object requires us to do so. */ | |
1048 | py_inf_frame = PyObject_CallMethod (filter, "inferior_frame", NULL); | |
1049 | if (py_inf_frame == NULL) | |
b99bf4e3 | 1050 | return EXT_LANG_BT_ERROR; |
1e611234 PM |
1051 | |
1052 | frame = frame_object_to_frame_info (py_inf_frame);; | |
1053 | ||
1054 | Py_DECREF (py_inf_frame); | |
1055 | ||
1056 | if (frame == NULL) | |
b99bf4e3 | 1057 | return EXT_LANG_BT_ERROR; |
1e611234 | 1058 | |
492d29ea | 1059 | TRY |
1e611234 PM |
1060 | { |
1061 | gdbarch = get_frame_arch (frame); | |
1062 | } | |
492d29ea | 1063 | CATCH (except, RETURN_MASK_ERROR) |
1e611234 PM |
1064 | { |
1065 | gdbpy_convert_exception (except); | |
800eb1ce | 1066 | return EXT_LANG_BT_ERROR; |
1e611234 | 1067 | } |
492d29ea | 1068 | END_CATCH |
1e611234 | 1069 | |
1e611234 PM |
1070 | /* stack-list-variables. */ |
1071 | if (print_locals && print_args && ! print_frame_info) | |
1072 | { | |
1073 | if (py_mi_print_variables (filter, out, &opts, | |
6dddc817 | 1074 | args_type, frame) == EXT_LANG_BT_ERROR) |
b99bf4e3 | 1075 | return EXT_LANG_BT_ERROR; |
34019068 | 1076 | return EXT_LANG_BT_COMPLETED; |
1e611234 PM |
1077 | } |
1078 | ||
b99bf4e3 JK |
1079 | cleanup_stack = make_cleanup (null_cleanup, NULL); |
1080 | ||
1e611234 PM |
1081 | /* -stack-list-locals does not require a |
1082 | wrapping frame attribute. */ | |
1083 | if (print_frame_info || (print_args && ! print_locals)) | |
1084 | make_cleanup_ui_out_tuple_begin_end (out, "frame"); | |
1085 | ||
1086 | if (print_frame_info) | |
1087 | { | |
1088 | /* Elided frames are also printed with this function (recursively) | |
1089 | and are printed with indention. */ | |
1090 | if (indent > 0) | |
1091 | { | |
492d29ea | 1092 | TRY |
8d4a54e2 JK |
1093 | { |
1094 | ui_out_spaces (out, indent*4); | |
1095 | } | |
492d29ea | 1096 | CATCH (except, RETURN_MASK_ERROR) |
8d4a54e2 JK |
1097 | { |
1098 | gdbpy_convert_exception (except); | |
800eb1ce JK |
1099 | do_cleanups (cleanup_stack); |
1100 | return EXT_LANG_BT_ERROR; | |
8d4a54e2 | 1101 | } |
492d29ea | 1102 | END_CATCH |
1e611234 PM |
1103 | } |
1104 | ||
1105 | /* The address is required for frame annotations, and also for | |
1106 | address printing. */ | |
1107 | if (PyObject_HasAttrString (filter, "address")) | |
1108 | { | |
1109 | PyObject *paddr = PyObject_CallMethod (filter, "address", NULL); | |
34019068 JK |
1110 | |
1111 | if (paddr == NULL) | |
800eb1ce JK |
1112 | { |
1113 | do_cleanups (cleanup_stack); | |
1114 | return EXT_LANG_BT_ERROR; | |
1115 | } | |
34019068 JK |
1116 | |
1117 | if (paddr != Py_None) | |
1e611234 | 1118 | { |
34019068 JK |
1119 | address = PyLong_AsLong (paddr); |
1120 | has_addr = 1; | |
1e611234 | 1121 | } |
34019068 | 1122 | Py_DECREF (paddr); |
1e611234 PM |
1123 | } |
1124 | } | |
1125 | ||
1126 | /* Print frame level. MI does not require the level if | |
1127 | locals/variables only are being printed. */ | |
1128 | if ((print_frame_info || print_args) && print_level) | |
1129 | { | |
1130 | struct frame_info **slot; | |
1131 | int level; | |
1e611234 PM |
1132 | |
1133 | slot = (struct frame_info **) htab_find_slot (levels_printed, | |
1134 | frame, INSERT); | |
492d29ea | 1135 | TRY |
1e611234 PM |
1136 | { |
1137 | level = frame_relative_level (frame); | |
1138 | ||
1139 | /* Check if this frame has already been printed (there are cases | |
1140 | where elided synthetic dummy-frames have to 'borrow' the frame | |
1141 | architecture from the eliding frame. If that is the case, do | |
1142 | not print 'level', but print spaces. */ | |
1143 | if (*slot == frame) | |
1144 | ui_out_field_skip (out, "level"); | |
1145 | else | |
1146 | { | |
1147 | *slot = frame; | |
1148 | annotate_frame_begin (print_level ? level : 0, | |
1149 | gdbarch, address); | |
1150 | ui_out_text (out, "#"); | |
1151 | ui_out_field_fmt_int (out, 2, ui_left, "level", | |
1152 | level); | |
1153 | } | |
1154 | } | |
492d29ea | 1155 | CATCH (except, RETURN_MASK_ERROR) |
1e611234 PM |
1156 | { |
1157 | gdbpy_convert_exception (except); | |
800eb1ce JK |
1158 | do_cleanups (cleanup_stack); |
1159 | return EXT_LANG_BT_ERROR; | |
1e611234 | 1160 | } |
492d29ea | 1161 | END_CATCH |
1e611234 PM |
1162 | } |
1163 | ||
1164 | if (print_frame_info) | |
1165 | { | |
1166 | /* Print address to the address field. If an address is not provided, | |
1167 | print nothing. */ | |
1168 | if (opts.addressprint && has_addr) | |
1169 | { | |
492d29ea | 1170 | TRY |
1e611234 PM |
1171 | { |
1172 | annotate_frame_address (); | |
1173 | ui_out_field_core_addr (out, "addr", gdbarch, address); | |
1174 | annotate_frame_address_end (); | |
1175 | ui_out_text (out, " in "); | |
1176 | } | |
492d29ea | 1177 | CATCH (except, RETURN_MASK_ERROR) |
1e611234 PM |
1178 | { |
1179 | gdbpy_convert_exception (except); | |
800eb1ce JK |
1180 | do_cleanups (cleanup_stack); |
1181 | return EXT_LANG_BT_ERROR; | |
1e611234 | 1182 | } |
492d29ea | 1183 | END_CATCH |
1e611234 PM |
1184 | } |
1185 | ||
1186 | /* Print frame function name. */ | |
1187 | if (PyObject_HasAttrString (filter, "function")) | |
1188 | { | |
1189 | PyObject *py_func = PyObject_CallMethod (filter, "function", NULL); | |
b99bf4e3 | 1190 | struct cleanup *py_func_cleanup; |
34019068 | 1191 | const char *function = NULL; |
1e611234 | 1192 | |
34019068 | 1193 | if (py_func == NULL) |
800eb1ce JK |
1194 | { |
1195 | do_cleanups (cleanup_stack); | |
1196 | return EXT_LANG_BT_ERROR; | |
1197 | } | |
b99bf4e3 | 1198 | py_func_cleanup = make_cleanup_py_decref (py_func); |
1e611234 | 1199 | |
34019068 JK |
1200 | if (gdbpy_is_string (py_func)) |
1201 | { | |
1202 | char *function_to_free; | |
1e611234 | 1203 | |
34019068 JK |
1204 | function = function_to_free = |
1205 | python_string_to_host_string (py_func); | |
1e611234 | 1206 | |
34019068 | 1207 | if (function == NULL) |
1e611234 | 1208 | { |
800eb1ce JK |
1209 | do_cleanups (cleanup_stack); |
1210 | return EXT_LANG_BT_ERROR; | |
1e611234 | 1211 | } |
34019068 JK |
1212 | make_cleanup (xfree, function_to_free); |
1213 | } | |
1214 | else if (PyLong_Check (py_func)) | |
1215 | { | |
1216 | CORE_ADDR addr = PyLong_AsUnsignedLongLong (py_func); | |
1217 | struct bound_minimal_symbol msymbol; | |
1e611234 | 1218 | |
34019068 | 1219 | if (PyErr_Occurred ()) |
800eb1ce JK |
1220 | { |
1221 | do_cleanups (cleanup_stack); | |
1222 | return EXT_LANG_BT_ERROR; | |
1223 | } | |
34019068 JK |
1224 | |
1225 | msymbol = lookup_minimal_symbol_by_pc (addr); | |
1226 | if (msymbol.minsym != NULL) | |
1227 | function = MSYMBOL_PRINT_NAME (msymbol.minsym); | |
1228 | } | |
1229 | else if (py_func != Py_None) | |
1230 | { | |
1231 | PyErr_SetString (PyExc_RuntimeError, | |
1232 | _("FrameDecorator.function: expecting a " \ | |
1233 | "String, integer or None.")); | |
800eb1ce JK |
1234 | do_cleanups (cleanup_stack); |
1235 | return EXT_LANG_BT_ERROR; | |
1e611234 | 1236 | } |
34019068 | 1237 | |
492d29ea | 1238 | TRY |
34019068 JK |
1239 | { |
1240 | annotate_frame_function_name (); | |
1241 | if (function == NULL) | |
1242 | ui_out_field_skip (out, "func"); | |
1243 | else | |
1244 | ui_out_field_string (out, "func", function); | |
1245 | } | |
492d29ea | 1246 | CATCH (except, RETURN_MASK_ERROR) |
34019068 | 1247 | { |
34019068 | 1248 | gdbpy_convert_exception (except); |
800eb1ce JK |
1249 | do_cleanups (cleanup_stack); |
1250 | return EXT_LANG_BT_ERROR; | |
34019068 | 1251 | } |
492d29ea PA |
1252 | END_CATCH |
1253 | ||
b99bf4e3 | 1254 | do_cleanups (py_func_cleanup); |
1e611234 | 1255 | } |
1e611234 PM |
1256 | } |
1257 | ||
1258 | ||
1259 | /* Frame arguments. Check the result, and error if something went | |
1260 | wrong. */ | |
1261 | if (print_args) | |
1262 | { | |
6dddc817 | 1263 | if (py_print_args (filter, out, args_type, frame) == EXT_LANG_BT_ERROR) |
800eb1ce JK |
1264 | { |
1265 | do_cleanups (cleanup_stack); | |
1266 | return EXT_LANG_BT_ERROR; | |
1267 | } | |
1e611234 PM |
1268 | } |
1269 | ||
1270 | /* File name/source/line number information. */ | |
1271 | if (print_frame_info) | |
1272 | { | |
492d29ea | 1273 | TRY |
1e611234 PM |
1274 | { |
1275 | annotate_frame_source_begin (); | |
1276 | } | |
492d29ea | 1277 | CATCH (except, RETURN_MASK_ERROR) |
1e611234 PM |
1278 | { |
1279 | gdbpy_convert_exception (except); | |
800eb1ce JK |
1280 | do_cleanups (cleanup_stack); |
1281 | return EXT_LANG_BT_ERROR; | |
1e611234 | 1282 | } |
492d29ea | 1283 | END_CATCH |
1e611234 PM |
1284 | |
1285 | if (PyObject_HasAttrString (filter, "filename")) | |
1286 | { | |
8d4a54e2 | 1287 | PyObject *py_fn = PyObject_CallMethod (filter, "filename", NULL); |
b99bf4e3 | 1288 | struct cleanup *py_fn_cleanup; |
8d4a54e2 | 1289 | |
34019068 | 1290 | if (py_fn == NULL) |
800eb1ce JK |
1291 | { |
1292 | do_cleanups (cleanup_stack); | |
1293 | return EXT_LANG_BT_ERROR; | |
1294 | } | |
b99bf4e3 | 1295 | py_fn_cleanup = make_cleanup_py_decref (py_fn); |
34019068 JK |
1296 | |
1297 | if (py_fn != Py_None) | |
1e611234 | 1298 | { |
34019068 | 1299 | char *filename = python_string_to_host_string (py_fn); |
1e611234 | 1300 | |
34019068 JK |
1301 | if (filename == NULL) |
1302 | { | |
800eb1ce JK |
1303 | do_cleanups (cleanup_stack); |
1304 | return EXT_LANG_BT_ERROR; | |
34019068 | 1305 | } |
8f28f522 | 1306 | |
34019068 | 1307 | make_cleanup (xfree, filename); |
492d29ea | 1308 | TRY |
34019068 JK |
1309 | { |
1310 | ui_out_wrap_hint (out, " "); | |
1311 | ui_out_text (out, " at "); | |
1312 | annotate_frame_source_file (); | |
1313 | ui_out_field_string (out, "file", filename); | |
1314 | annotate_frame_source_file_end (); | |
1315 | } | |
492d29ea | 1316 | CATCH (except, RETURN_MASK_ERROR) |
34019068 | 1317 | { |
34019068 | 1318 | gdbpy_convert_exception (except); |
800eb1ce JK |
1319 | do_cleanups (cleanup_stack); |
1320 | return EXT_LANG_BT_ERROR; | |
1e611234 | 1321 | } |
492d29ea | 1322 | END_CATCH |
1e611234 | 1323 | } |
b99bf4e3 | 1324 | do_cleanups (py_fn_cleanup); |
1e611234 PM |
1325 | } |
1326 | ||
1327 | if (PyObject_HasAttrString (filter, "line")) | |
1328 | { | |
1329 | PyObject *py_line = PyObject_CallMethod (filter, "line", NULL); | |
b99bf4e3 | 1330 | struct cleanup *py_line_cleanup; |
1e611234 PM |
1331 | int line; |
1332 | ||
34019068 | 1333 | if (py_line == NULL) |
800eb1ce JK |
1334 | { |
1335 | do_cleanups (cleanup_stack); | |
1336 | return EXT_LANG_BT_ERROR; | |
1337 | } | |
b99bf4e3 | 1338 | py_line_cleanup = make_cleanup_py_decref (py_line); |
34019068 JK |
1339 | |
1340 | if (py_line != Py_None) | |
1e611234 | 1341 | { |
34019068 | 1342 | line = PyLong_AsLong (py_line); |
492d29ea | 1343 | TRY |
1e611234 | 1344 | { |
34019068 JK |
1345 | ui_out_text (out, ":"); |
1346 | annotate_frame_source_line (); | |
1347 | ui_out_field_int (out, "line", line); | |
1348 | } | |
492d29ea | 1349 | CATCH (except, RETURN_MASK_ERROR) |
34019068 | 1350 | { |
34019068 | 1351 | gdbpy_convert_exception (except); |
800eb1ce JK |
1352 | do_cleanups (cleanup_stack); |
1353 | return EXT_LANG_BT_ERROR; | |
1e611234 | 1354 | } |
492d29ea | 1355 | END_CATCH |
1e611234 | 1356 | } |
b99bf4e3 | 1357 | do_cleanups (py_line_cleanup); |
1e611234 PM |
1358 | } |
1359 | } | |
1360 | ||
1361 | /* For MI we need to deal with the "children" list population of | |
1362 | elided frames, so if MI output detected do not send newline. */ | |
1363 | if (! ui_out_is_mi_like_p (out)) | |
1364 | { | |
492d29ea | 1365 | TRY |
1e611234 PM |
1366 | { |
1367 | annotate_frame_end (); | |
1368 | ui_out_text (out, "\n"); | |
1369 | } | |
492d29ea | 1370 | CATCH (except, RETURN_MASK_ERROR) |
1e611234 PM |
1371 | { |
1372 | gdbpy_convert_exception (except); | |
800eb1ce JK |
1373 | do_cleanups (cleanup_stack); |
1374 | return EXT_LANG_BT_ERROR; | |
1e611234 | 1375 | } |
492d29ea | 1376 | END_CATCH |
1e611234 PM |
1377 | } |
1378 | ||
1379 | if (print_locals) | |
1380 | { | |
1381 | if (py_print_locals (filter, out, args_type, indent, | |
6dddc817 | 1382 | frame) == EXT_LANG_BT_ERROR) |
800eb1ce JK |
1383 | { |
1384 | do_cleanups (cleanup_stack); | |
1385 | return EXT_LANG_BT_ERROR; | |
1386 | } | |
1e611234 PM |
1387 | } |
1388 | ||
34019068 JK |
1389 | { |
1390 | PyObject *elided; | |
b99bf4e3 | 1391 | struct cleanup *elided_cleanup; |
1e611234 | 1392 | |
34019068 JK |
1393 | /* Finally recursively print elided frames, if any. */ |
1394 | elided = get_py_iter_from_func (filter, "elided"); | |
1395 | if (elided == NULL) | |
800eb1ce JK |
1396 | { |
1397 | do_cleanups (cleanup_stack); | |
1398 | return EXT_LANG_BT_ERROR; | |
1399 | } | |
b99bf4e3 | 1400 | elided_cleanup = make_cleanup_py_decref (elided); |
1e611234 | 1401 | |
34019068 JK |
1402 | if (elided != Py_None) |
1403 | { | |
1404 | PyObject *item; | |
1e611234 | 1405 | |
34019068 | 1406 | make_cleanup_ui_out_list_begin_end (out, "children"); |
1e611234 | 1407 | |
34019068 JK |
1408 | if (! ui_out_is_mi_like_p (out)) |
1409 | indent++; | |
1e611234 | 1410 | |
34019068 JK |
1411 | while ((item = PyIter_Next (elided))) |
1412 | { | |
b99bf4e3 JK |
1413 | struct cleanup *item_cleanup = make_cleanup_py_decref (item); |
1414 | ||
34019068 JK |
1415 | enum ext_lang_bt_status success = py_print_frame (item, flags, |
1416 | args_type, out, | |
1417 | indent, | |
1418 | levels_printed); | |
1e611234 | 1419 | |
b99bf4e3 JK |
1420 | do_cleanups (item_cleanup); |
1421 | ||
34019068 JK |
1422 | if (success == EXT_LANG_BT_ERROR) |
1423 | { | |
800eb1ce JK |
1424 | do_cleanups (cleanup_stack); |
1425 | return EXT_LANG_BT_ERROR; | |
34019068 | 1426 | } |
34019068 JK |
1427 | } |
1428 | if (item == NULL && PyErr_Occurred ()) | |
800eb1ce JK |
1429 | { |
1430 | do_cleanups (cleanup_stack); | |
1431 | return EXT_LANG_BT_ERROR; | |
1432 | } | |
34019068 | 1433 | } |
b99bf4e3 JK |
1434 | do_cleanups (elided_cleanup); |
1435 | } | |
1e611234 PM |
1436 | |
1437 | do_cleanups (cleanup_stack); | |
6dddc817 | 1438 | return EXT_LANG_BT_COMPLETED; |
1e611234 PM |
1439 | } |
1440 | ||
1441 | /* Helper function to initiate frame filter invocation at starting | |
1442 | frame FRAME. */ | |
1443 | ||
1444 | static PyObject * | |
1445 | bootstrap_python_frame_filters (struct frame_info *frame, | |
1446 | int frame_low, int frame_high) | |
1447 | { | |
1448 | struct cleanup *cleanups = | |
1449 | make_cleanup (null_cleanup, NULL); | |
1450 | PyObject *module, *sort_func, *iterable, *frame_obj, *iterator; | |
1451 | PyObject *py_frame_low, *py_frame_high; | |
1452 | ||
1453 | frame_obj = frame_info_to_frame_object (frame); | |
1454 | if (frame_obj == NULL) | |
1455 | goto error; | |
1456 | make_cleanup_py_decref (frame_obj); | |
1457 | ||
1458 | module = PyImport_ImportModule ("gdb.frames"); | |
1459 | if (module == NULL) | |
1460 | goto error; | |
1461 | make_cleanup_py_decref (module); | |
1462 | ||
1463 | sort_func = PyObject_GetAttrString (module, "execute_frame_filters"); | |
1464 | if (sort_func == NULL) | |
1465 | goto error; | |
1466 | make_cleanup_py_decref (sort_func); | |
1467 | ||
1468 | py_frame_low = PyInt_FromLong (frame_low); | |
1469 | if (py_frame_low == NULL) | |
1470 | goto error; | |
1471 | make_cleanup_py_decref (py_frame_low); | |
1472 | ||
1473 | py_frame_high = PyInt_FromLong (frame_high); | |
1474 | if (py_frame_high == NULL) | |
1475 | goto error; | |
1476 | make_cleanup_py_decref (py_frame_high); | |
1477 | ||
1478 | iterable = PyObject_CallFunctionObjArgs (sort_func, frame_obj, | |
1479 | py_frame_low, | |
1480 | py_frame_high, | |
1481 | NULL); | |
1482 | if (iterable == NULL) | |
1483 | goto error; | |
1484 | ||
1485 | do_cleanups (cleanups); | |
1486 | ||
1487 | if (iterable != Py_None) | |
1488 | { | |
1489 | iterator = PyObject_GetIter (iterable); | |
1490 | Py_DECREF (iterable); | |
1491 | } | |
1492 | else | |
1493 | { | |
1494 | return iterable; | |
1495 | } | |
1496 | ||
1497 | return iterator; | |
1498 | ||
1499 | error: | |
1500 | do_cleanups (cleanups); | |
1501 | return NULL; | |
1502 | } | |
1503 | ||
1504 | /* This is the only publicly exported function in this file. FRAME | |
1505 | is the source frame to start frame-filter invocation. FLAGS is an | |
1506 | integer holding the flags for printing. The following elements of | |
1507 | the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS: | |
1508 | PRINT_LEVEL is a flag indicating whether to print the frame's | |
1509 | relative level in the output. PRINT_FRAME_INFO is a flag that | |
1510 | indicates whether this function should print the frame | |
1511 | information, PRINT_ARGS is a flag that indicates whether to print | |
1512 | frame arguments, and PRINT_LOCALS, likewise, with frame local | |
1513 | variables. ARGS_TYPE is an enumerator describing the argument | |
1514 | format, OUT is the output stream to print. FRAME_LOW is the | |
1515 | beginning of the slice of frames to print, and FRAME_HIGH is the | |
6dddc817 DE |
1516 | upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error, |
1517 | or EXT_LANG_BT_COMPLETED on success. */ | |
1518 | ||
1519 | enum ext_lang_bt_status | |
1520 | gdbpy_apply_frame_filter (const struct extension_language_defn *extlang, | |
1521 | struct frame_info *frame, int flags, | |
1522 | enum ext_lang_frame_args args_type, | |
1523 | struct ui_out *out, int frame_low, int frame_high) | |
1e611234 PM |
1524 | { |
1525 | struct gdbarch *gdbarch = NULL; | |
1526 | struct cleanup *cleanups; | |
6dddc817 | 1527 | enum ext_lang_bt_status success = EXT_LANG_BT_ERROR; |
1e611234 | 1528 | PyObject *iterable; |
1e611234 PM |
1529 | PyObject *item; |
1530 | htab_t levels_printed; | |
1531 | ||
8ee002df | 1532 | if (!gdb_python_initialized) |
6dddc817 | 1533 | return EXT_LANG_BT_NO_FILTERS; |
8ee002df | 1534 | |
492d29ea | 1535 | TRY |
1e611234 PM |
1536 | { |
1537 | gdbarch = get_frame_arch (frame); | |
1538 | } | |
492d29ea | 1539 | CATCH (except, RETURN_MASK_ALL) |
1e611234 | 1540 | { |
21909fa1 | 1541 | /* Let gdb try to print the stack trace. */ |
6dddc817 | 1542 | return EXT_LANG_BT_NO_FILTERS; |
1e611234 | 1543 | } |
492d29ea | 1544 | END_CATCH |
1e611234 | 1545 | |
21909fa1 TT |
1546 | cleanups = ensure_python_env (gdbarch, current_language); |
1547 | ||
1e611234 PM |
1548 | iterable = bootstrap_python_frame_filters (frame, frame_low, frame_high); |
1549 | ||
1550 | if (iterable == NULL) | |
8ee002df PM |
1551 | { |
1552 | /* Normally if there is an error GDB prints the exception, | |
1553 | abandons the backtrace and exits. The user can then call "bt | |
1554 | no-filters", and get a default backtrace (it would be | |
1555 | confusing to automatically start a standard backtrace halfway | |
1556 | through a Python filtered backtrace). However in the case | |
1557 | where GDB cannot initialize the frame filters (most likely | |
1558 | due to incorrect auto-load paths), GDB has printed nothing. | |
1559 | In this case it is OK to print the default backtrace after | |
6dddc817 | 1560 | printing the error message. GDB returns EXT_LANG_BT_NO_FILTERS |
8ee002df PM |
1561 | here to signify there are no filters after printing the |
1562 | initialization error. This return code will trigger a | |
1563 | default backtrace. */ | |
1564 | ||
1565 | gdbpy_print_stack (); | |
1566 | do_cleanups (cleanups); | |
6dddc817 | 1567 | return EXT_LANG_BT_NO_FILTERS; |
8ee002df | 1568 | } |
1e611234 PM |
1569 | |
1570 | /* If iterable is None, then there are no frame filters registered. | |
1571 | If this is the case, defer to default GDB printing routines in MI | |
1572 | and CLI. */ | |
1573 | make_cleanup_py_decref (iterable); | |
1574 | if (iterable == Py_None) | |
1575 | { | |
6dddc817 | 1576 | success = EXT_LANG_BT_NO_FILTERS; |
1e611234 PM |
1577 | goto done; |
1578 | } | |
1579 | ||
1580 | levels_printed = htab_create (20, | |
1581 | htab_hash_pointer, | |
1582 | htab_eq_pointer, | |
1583 | NULL); | |
1584 | make_cleanup_htab_delete (levels_printed); | |
1585 | ||
1586 | while ((item = PyIter_Next (iterable))) | |
1587 | { | |
b99bf4e3 JK |
1588 | struct cleanup *item_cleanup = make_cleanup_py_decref (item); |
1589 | ||
1e611234 PM |
1590 | success = py_print_frame (item, flags, args_type, out, 0, |
1591 | levels_printed); | |
1592 | ||
b99bf4e3 JK |
1593 | do_cleanups (item_cleanup); |
1594 | ||
1e611234 PM |
1595 | /* Do not exit on error printing a single frame. Print the |
1596 | error and continue with other frames. */ | |
6dddc817 | 1597 | if (success == EXT_LANG_BT_ERROR) |
1e611234 | 1598 | gdbpy_print_stack (); |
1e611234 PM |
1599 | } |
1600 | ||
1601 | if (item == NULL && PyErr_Occurred ()) | |
1602 | goto error; | |
1603 | ||
1604 | done: | |
1605 | do_cleanups (cleanups); | |
1606 | return success; | |
1607 | ||
8ee002df PM |
1608 | /* Exit and abandon backtrace on error, printing the exception that |
1609 | is set. */ | |
1e611234 PM |
1610 | error: |
1611 | gdbpy_print_stack (); | |
1612 | do_cleanups (cleanups); | |
6dddc817 | 1613 | return EXT_LANG_BT_ERROR; |
1e611234 | 1614 | } |