enum ext_lang_rc gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang, struct type *type, LONGEST embedded_offset, CORE_ADDR address, struct ui_file *stream, int recurse, struct value *val, const struct value_print_options *options, const struct language_defn *language) { struct gdbarch *gdbarch = get_type_arch (type); struct value *value; enum string_repr_result print_result; if (value_lazy (val)) value_fetch_lazy (val); /* No pretty-printer support for unavailable values. */ if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type))) return EXT_LANG_RC_NOP; if (!gdb_python_initialized) return EXT_LANG_RC_NOP; gdbpy_enter enter_py (gdbarch, language); /* Instantiate the printer. */ value = value_from_component (val, type, embedded_offset); gdbpy_ref<> val_obj (value_to_value_object (value)); if (val_obj == NULL) { print_stack_unless_memory_error (stream); return EXT_LANG_RC_ERROR; } /* Find the constructor. */ gdbpy_ref<> printer (find_pretty_printer (val_obj.get ())); if (printer == NULL) { print_stack_unless_memory_error (stream); return EXT_LANG_RC_ERROR; } if (printer == Py_None) return EXT_LANG_RC_NOP; /* If we are printing a map, we want some special formatting. */ gdb::unique_xmalloc_ptr<char> hint (gdbpy_get_display_hint (printer.get ())); /* Print the section */ print_result = print_string_repr (printer.get (), hint.get (), stream, recurse, options, language, gdbarch); if (print_result != string_repr_error) print_children (printer.get (), hint.get (), stream, recurse, options, language, print_result == string_repr_none); if (PyErr_Occurred ()) print_stack_unless_memory_error (stream); return EXT_LANG_RC_OK; }
/* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the print object. It must have a 'to_string' method (but this is checked by varobj, not here) which takes no arguments and returns a string. The printer will return a value and in the case of a Python string being returned, this function will return a PyObject containing the string. For any other type, *REPLACEMENT is set to the replacement value and this function returns NULL. On error, *REPLACEMENT is set to NULL and this function also returns NULL. */ PyObject * apply_varobj_pretty_printer (PyObject *printer_obj, struct value **replacement, struct ui_file *stream) { PyObject *py_str = NULL; *replacement = NULL; py_str = pretty_print_one_value (printer_obj, replacement); if (*replacement == NULL && py_str == NULL) print_stack_unless_memory_error (stream); return py_str; }
/* Helper for gdbpy_apply_val_pretty_printer that formats children of the printer, if any exist. If is_py_none is true, then nothing has been printed by to_string, and format output accordingly. */ static void print_children (PyObject *printer, const char *hint, struct ui_file *stream, int recurse, const struct value_print_options *options, const struct language_defn *language, int is_py_none) { int is_map, is_array, done_flag, pretty; unsigned int i; if (! PyObject_HasAttr (printer, gdbpy_children_cst)) return; /* If we are printing a map or an array, we want some special formatting. */ is_map = hint && ! strcmp (hint, "map"); is_array = hint && ! strcmp (hint, "array"); gdbpy_ref<> children (PyObject_CallMethodObjArgs (printer, gdbpy_children_cst, NULL)); if (children == NULL) { print_stack_unless_memory_error (stream); return; } gdbpy_ref<> iter (PyObject_GetIter (children.get ())); if (iter == NULL) { print_stack_unless_memory_error (stream); return; } /* Use the prettyformat_arrays option if we are printing an array, and the pretty option otherwise. */ if (is_array) pretty = options->prettyformat_arrays; else { if (options->prettyformat == Val_prettyformat) pretty = 1; else pretty = options->prettyformat_structs; } /* Manufacture a dummy Python frame to work around Python 2.4 bug, where it insists on having a non-NULL tstate->frame when a generator is called. */ #ifndef IS_PY3K dummy_python_frame frame; if (frame.failed ()) { gdbpy_print_stack (); return; } #endif done_flag = 0; for (i = 0; i < options->print_max; ++i) { PyObject *py_v; const char *name; gdbpy_ref<> item (PyIter_Next (iter.get ())); if (item == NULL) { if (PyErr_Occurred ()) print_stack_unless_memory_error (stream); /* Set a flag so we can know whether we printed all the available elements. */ else done_flag = 1; break; } if (! PyTuple_Check (item.get ()) || PyTuple_Size (item.get ()) != 2) { PyErr_SetString (PyExc_TypeError, _("Result of children iterator not a tuple" " of two elements.")); gdbpy_print_stack (); continue; } if (! PyArg_ParseTuple (item.get (), "sO", &name, &py_v)) { /* The user won't necessarily get a stack trace here, so provide more context. */ if (gdbpy_print_python_errors_p ()) fprintf_unfiltered (gdb_stderr, _("Bad result from children iterator.\n")); gdbpy_print_stack (); continue; } /* Print initial "{". For other elements, there are three cases: 1. Maps. Print a "," after each value element. 2. Arrays. Always print a ",". 3. Other. Always print a ",". */ if (i == 0) { if (is_py_none) fputs_filtered ("{", stream); else fputs_filtered (" = {", stream); } else if (! is_map || i % 2 == 0) fputs_filtered (pretty ? "," : ", ", stream); /* In summary mode, we just want to print "= {...}" if there is a value. */ if (options->summary) { /* This increment tricks the post-loop logic to print what we want. */ ++i; /* Likewise. */ pretty = 0; break; } if (! is_map || i % 2 == 0) { if (pretty) { fputs_filtered ("\n", stream); print_spaces_filtered (2 + 2 * recurse, stream); } else wrap_here (n_spaces (2 + 2 *recurse)); } if (is_map && i % 2 == 0) fputs_filtered ("[", stream); else if (is_array) { /* We print the index, not whatever the child method returned as the name. */ if (options->print_array_indexes) fprintf_filtered (stream, "[%d] = ", i); } else if (! is_map) { fputs_filtered (name, stream); fputs_filtered (" = ", stream); } if (gdbpy_is_lazy_string (py_v)) { CORE_ADDR addr; struct type *type; long length; gdb::unique_xmalloc_ptr<char> encoding; struct value_print_options local_opts = *options; gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding); local_opts.addressprint = 0; val_print_string (type, encoding.get (), addr, (int) length, stream, &local_opts); } else if (gdbpy_is_string (py_v)) { gdb::unique_xmalloc_ptr<char> output; output = python_string_to_host_string (py_v); if (!output) gdbpy_print_stack (); else fputs_filtered (output.get (), stream); } else { struct value *value = convert_value_from_python (py_v); if (value == NULL) { gdbpy_print_stack (); error (_("Error while executing Python code.")); } else common_val_print (value, stream, recurse + 1, options, language); } if (is_map && i % 2 == 0) fputs_filtered ("] = ", stream); } if (i) { if (!done_flag) { if (pretty) { fputs_filtered ("\n", stream); print_spaces_filtered (2 + 2 * recurse, stream); } fputs_filtered ("...", stream); } if (pretty) { fputs_filtered ("\n", stream); print_spaces_filtered (2 * recurse, stream); } fputs_filtered ("}", stream); } }
static enum string_repr_result print_string_repr (PyObject *printer, const char *hint, struct ui_file *stream, int recurse, const struct value_print_options *options, const struct language_defn *language, struct gdbarch *gdbarch) { struct value *replacement = NULL; enum string_repr_result result = string_repr_ok; gdbpy_ref<> py_str (pretty_print_one_value (printer, &replacement)); if (py_str != NULL) { if (py_str == Py_None) result = string_repr_none; else if (gdbpy_is_lazy_string (py_str.get ())) { CORE_ADDR addr; long length; struct type *type; gdb::unique_xmalloc_ptr<char> encoding; struct value_print_options local_opts = *options; gdbpy_extract_lazy_string (py_str.get (), &addr, &type, &length, &encoding); local_opts.addressprint = 0; val_print_string (type, encoding.get (), addr, (int) length, stream, &local_opts); } else { gdbpy_ref<> string (python_string_to_target_python_string (py_str.get ())); if (string != NULL) { char *output; long length; struct type *type; #ifdef IS_PY3K output = PyBytes_AS_STRING (string.get ()); length = PyBytes_GET_SIZE (string.get ()); #else output = PyString_AsString (string.get ()); length = PyString_Size (string.get ()); #endif type = builtin_type (gdbarch)->builtin_char; if (hint && !strcmp (hint, "string")) LA_PRINT_STRING (stream, type, (gdb_byte *) output, length, NULL, 0, options); else fputs_filtered (output, stream); } else { result = string_repr_error; print_stack_unless_memory_error (stream); } } } else if (replacement) { struct value_print_options opts = *options; opts.addressprint = 0; common_val_print (replacement, stream, recurse, &opts, language); } else { result = string_repr_error; print_stack_unless_memory_error (stream); } return result; }
enum ext_lang_rc gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang, struct type *type, const gdb_byte *valaddr, int embedded_offset, CORE_ADDR address, struct ui_file *stream, int recurse, const struct value *val, const struct value_print_options *options, const struct language_defn *language) { struct gdbarch *gdbarch = get_type_arch (type); PyObject *printer = NULL; PyObject *val_obj = NULL; struct value *value; char *hint = NULL; struct cleanup *cleanups; enum ext_lang_rc result = EXT_LANG_RC_NOP; enum string_repr_result print_result; /* No pretty-printer support for unavailable values. */ if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type))) return EXT_LANG_RC_NOP; if (!gdb_python_initialized) return EXT_LANG_RC_NOP; cleanups = ensure_python_env (gdbarch, language); /* Instantiate the printer. */ if (valaddr) valaddr += embedded_offset; value = value_from_contents_and_address (type, valaddr, address + embedded_offset); set_value_component_location (value, val); /* set_value_component_location resets the address, so we may need to set it again. */ if (VALUE_LVAL (value) != lval_internalvar && VALUE_LVAL (value) != lval_internalvar_component && VALUE_LVAL (value) != lval_computed) set_value_address (value, address + embedded_offset); val_obj = value_to_value_object (value); if (! val_obj) { result = EXT_LANG_RC_ERROR; goto done; } /* Find the constructor. */ printer = find_pretty_printer (val_obj); Py_DECREF (val_obj); if (printer == NULL) { result = EXT_LANG_RC_ERROR; goto done; } make_cleanup_py_decref (printer); if (printer == Py_None) { result = EXT_LANG_RC_NOP; goto done; } /* If we are printing a map, we want some special formatting. */ hint = gdbpy_get_display_hint (printer); make_cleanup (free_current_contents, &hint); /* Print the section */ print_result = print_string_repr (printer, hint, stream, recurse, options, language, gdbarch); if (print_result != string_repr_error) print_children (printer, hint, stream, recurse, options, language, print_result == string_repr_none); result = EXT_LANG_RC_OK; done: if (PyErr_Occurred ()) print_stack_unless_memory_error (stream); do_cleanups (cleanups); return result; }
static enum string_repr_result print_string_repr (PyObject *printer, const char *hint, struct ui_file *stream, int recurse, const struct value_print_options *options, const struct language_defn *language, struct gdbarch *gdbarch) { struct value *replacement = NULL; PyObject *py_str = NULL; enum string_repr_result result = string_repr_ok; py_str = pretty_print_one_value (printer, &replacement); if (py_str) { struct cleanup *cleanup = make_cleanup_py_decref (py_str); if (py_str == Py_None) result = string_repr_none; else if (gdbpy_is_lazy_string (py_str)) { CORE_ADDR addr; long length; struct type *type; char *encoding = NULL; struct value_print_options local_opts = *options; make_cleanup (free_current_contents, &encoding); gdbpy_extract_lazy_string (py_str, &addr, &type, &length, &encoding); local_opts.addressprint = 0; val_print_string (type, encoding, addr, (int) length, stream, &local_opts); } else { PyObject *string; string = python_string_to_target_python_string (py_str); if (string) { char *output; long length; struct type *type; make_cleanup_py_decref (string); #ifdef IS_PY3K output = PyBytes_AS_STRING (string); length = PyBytes_GET_SIZE (string); #else output = PyString_AsString (string); length = PyString_Size (string); #endif type = builtin_type (gdbarch)->builtin_char; if (hint && !strcmp (hint, "string")) LA_PRINT_STRING (stream, type, (gdb_byte *) output, length, NULL, 0, options); else fputs_filtered (output, stream); } else { result = string_repr_error; print_stack_unless_memory_error (stream); } } do_cleanups (cleanup); } else if (replacement) { struct value_print_options opts = *options; opts.addressprint = 0; common_val_print (replacement, stream, recurse, &opts, language); } else { result = string_repr_error; print_stack_unless_memory_error (stream); } return result; }