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