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