static void print_stack_unless_memory_error (struct ui_file *stream) { if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error)) { struct cleanup *cleanup; PyObject *type, *value, *trace; char *msg; PyErr_Fetch (&type, &value, &trace); cleanup = make_cleanup_py_decref (type); make_cleanup_py_decref (value); make_cleanup_py_decref (trace); msg = gdbpy_exception_to_string (type, value); make_cleanup (xfree, msg); if (msg == NULL || *msg == '\0') fprintf_filtered (stream, _("<error reading variable>")); else fprintf_filtered (stream, _("<error reading variable: %s>"), msg); do_cleanups (cleanup); } else gdbpy_print_stack (); }
static PyObject * create_inferior_call_event_object (inferior_call_kind flag, ptid_t ptid, CORE_ADDR addr) { int pid; long tid, lwp; PyObject *event; PyObject *ptid_obj = NULL; PyObject *addr_obj = NULL; int failed; struct cleanup *cleanups; struct cleanup *member_cleanups; switch (flag) { case INFERIOR_CALL_PRE: event = create_event_object (&inferior_call_pre_event_object_type); break; case INFERIOR_CALL_POST: event = create_event_object (&inferior_call_post_event_object_type); break; default: return NULL; } cleanups = make_cleanup_py_decref (event); ptid_obj = gdbpy_create_ptid_object (ptid); if (ptid_obj == NULL) goto fail; member_cleanups = make_cleanup_py_decref (ptid_obj); failed = evpy_add_attribute (event, "ptid", ptid_obj) < 0; if (failed) goto fail; addr_obj = PyLong_FromLongLong (addr); if (addr_obj == NULL) goto fail; make_cleanup_py_decref (addr_obj); failed = evpy_add_attribute (event, "address", addr_obj) < 0; if (failed) goto fail; do_cleanups (member_cleanups); discard_cleanups (cleanups); return event; fail: do_cleanups (cleanups); return NULL; }
static void python_run_simple_file (FILE *file, const char *filename) { #ifndef _WIN32 PyRun_SimpleFile (file, filename); #else /* _WIN32 */ char *full_path; PyObject *python_file; struct cleanup *cleanup; /* Because we have a string for a filename, and are using Python to open the file, we need to expand any tilde in the path first. */ full_path = tilde_expand (filename); cleanup = make_cleanup (xfree, full_path); python_file = PyFile_FromString (full_path, "r"); if (! python_file) { do_cleanups (cleanup); gdbpy_print_stack (); error (_("Error while opening file: %s"), full_path); } make_cleanup_py_decref (python_file); PyRun_SimpleFile (PyFile_AsFile (python_file), filename); do_cleanups (cleanup); #endif /* _WIN32 */ }
static PyObject * create_register_changed_event_object (struct frame_info *frame, int regnum) { PyObject *event; PyObject *frame_obj = NULL; PyObject *regnum_obj = NULL; int failed; struct cleanup *cleanups; struct cleanup *member_cleanups; event = create_event_object (®ister_changed_event_object_type); if (event == NULL) return NULL; cleanups = make_cleanup_py_decref (event); frame_obj = frame_info_to_frame_object (frame); if (frame_obj == NULL) goto fail; member_cleanups = make_cleanup_py_decref (frame_obj); failed = evpy_add_attribute (event, "frame", frame_obj) < 0; if (failed) goto fail; regnum_obj = PyLong_FromLongLong (regnum); if (regnum_obj == NULL) goto fail; make_cleanup_py_decref (regnum_obj); failed = evpy_add_attribute (event, "regnum", regnum_obj) < 0; if (failed) goto fail; do_cleanups (member_cleanups); discard_cleanups (cleanups); return event; fail: do_cleanups (cleanups); return NULL; }
static PyObject * create_memory_changed_event_object (CORE_ADDR addr, ssize_t len) { PyObject *event; PyObject *addr_obj = NULL; PyObject *len_obj = NULL; int failed; struct cleanup *cleanups; struct cleanup *member_cleanups; event = create_event_object (&memory_changed_event_object_type); if (event == NULL) return NULL; cleanups = make_cleanup_py_decref (event); addr_obj = PyLong_FromLongLong (addr); if (addr_obj == NULL) goto fail; member_cleanups = make_cleanup_py_decref (addr_obj); failed = evpy_add_attribute (event, "address", addr_obj) < 0; if (failed) goto fail; len_obj = PyLong_FromLong (len); if (len_obj == NULL) goto fail; make_cleanup_py_decref (len_obj); failed = evpy_add_attribute (event, "length", len_obj) < 0; if (failed) goto fail; do_cleanups (member_cleanups); discard_cleanups (cleanups); return event; fail: do_cleanups (cleanups); return NULL; }
struct varobj_iter * py_varobj_get_iterator (struct varobj *var, PyObject *printer) { PyObject *children; int i; PyObject *iter; struct py_varobj_iter *py_iter; struct cleanup *back_to = varobj_ensure_python_env (var); if (!PyObject_HasAttr (printer, gdbpy_children_cst)) { do_cleanups (back_to); return NULL; } children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst, NULL); if (children == NULL) { gdbpy_print_stack_check_interrupt (); error (_("Null value returned for children")); } make_cleanup_py_decref (children); iter = PyObject_GetIter (children); if (iter == NULL) { gdbpy_print_stack_check_interrupt (); error (_("Could not get children iterator")); } py_iter = py_varobj_iter_new (var, iter); do_cleanups (back_to); return &py_iter->base; }
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; }
/* 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; PyObject *children, *iter; #ifndef IS_PY3K PyObject *frame; #endif struct cleanup *cleanups; 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"); children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst, NULL); if (! children) { print_stack_unless_memory_error (stream); return; } cleanups = make_cleanup_py_decref (children); iter = PyObject_GetIter (children); if (!iter) { print_stack_unless_memory_error (stream); goto done; } make_cleanup_py_decref (iter); /* 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 frame = push_dummy_python_frame (); if (!frame) { gdbpy_print_stack (); goto done; } make_cleanup_py_decref (frame); #endif done_flag = 0; for (i = 0; i < options->print_max; ++i) { PyObject *py_v, *item = PyIter_Next (iter); const char *name; struct cleanup *inner_cleanup; if (! item) { 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) || PyTuple_Size (item) != 2) { PyErr_SetString (PyExc_TypeError, _("Result of children iterator not a tuple" " of two elements.")); gdbpy_print_stack (); Py_DECREF (item); continue; } if (! PyArg_ParseTuple (item, "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 (); Py_DECREF (item); continue; } inner_cleanup = make_cleanup_py_decref (item); /* 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; char *encoding = NULL; struct value_print_options local_opts = *options; make_cleanup (free_current_contents, &encoding); gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding); local_opts.addressprint = 0; val_print_string (type, encoding, addr, (int) length, stream, &local_opts); } else if (gdbpy_is_string (py_v)) { char *output; output = python_string_to_host_string (py_v); if (!output) gdbpy_print_stack (); else { fputs_filtered (output, stream); xfree (output); } } 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); do_cleanups (inner_cleanup); } 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); } done: do_cleanups (cleanups); }
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; }