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