static PyObject * pretty_print_one_value (PyObject *printer, struct value **out_value) { gdbpy_ref<> result; *out_value = NULL; TRY { result.reset (PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, NULL)); if (result != NULL) { if (! gdbpy_is_string (result.get ()) && ! gdbpy_is_lazy_string (result.get ()) && result != Py_None) { *out_value = convert_value_from_python (result.get ()); if (PyErr_Occurred ()) *out_value = NULL; result = NULL; } } } CATCH (except, RETURN_MASK_ALL) { } END_CATCH return result.release (); }
static PyObject * pretty_print_one_value (PyObject *printer, struct value **out_value) { volatile struct gdb_exception except; PyObject *result = NULL; *out_value = NULL; TRY_CATCH (except, RETURN_MASK_ALL) { result = PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, NULL); if (result) { if (! gdbpy_is_string (result) && ! gdbpy_is_lazy_string (result) && result != Py_None) { *out_value = convert_value_from_python (result); if (PyErr_Occurred ()) *out_value = NULL; Py_DECREF (result); result = NULL; } } }
/* 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; }
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; }