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