aboutsummaryrefslogtreecommitdiff
path: root/gdb/python
diff options
context:
space:
mode:
Diffstat (limited to 'gdb/python')
-rw-r--r--gdb/python/python-internal.h8
-rw-r--r--gdb/python/python-prettyprint.c74
-rw-r--r--gdb/python/python.c3
3 files changed, 85 insertions, 0 deletions
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 983d24d..35d3870 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -111,6 +111,14 @@ char *python_string_to_host_string (PyObject *obj);
PyObject *target_string_to_unicode (const gdb_byte *str, int length);
int gdbpy_is_string (PyObject *obj);
+/* Note that these are declared here, and not in python.h with the
+ other pretty-printer functions, because they refer to PyObject. */
+char *apply_varobj_pretty_printer (PyObject *print_obj,
+ struct value **replacement);
+PyObject *gdbpy_get_varobj_pretty_printer (struct value *value);
+char *gdbpy_get_display_hint (PyObject *printer);
+PyObject *gdbpy_default_visualizer (PyObject *self, PyObject *args);
+
extern PyObject *gdbpy_doc_cst;
extern PyObject *gdbpy_children_cst;
extern PyObject *gdbpy_to_string_cst;
diff --git a/gdb/python/python-prettyprint.c b/gdb/python/python-prettyprint.c
index 5be4f36..6e17f9a 100644
--- a/gdb/python/python-prettyprint.c
+++ b/gdb/python/python-prettyprint.c
@@ -508,6 +508,80 @@ apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
return result;
}
+/* 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. This function returns an xmalloc()d string if
+ the printer returns a string. The printer may return a replacement
+ value instead; in this case *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. */
+char *
+apply_varobj_pretty_printer (PyObject *printer_obj,
+ struct value **replacement)
+{
+ char *result;
+ PyGILState_STATE state = PyGILState_Ensure ();
+
+ *replacement = NULL;
+ result = pretty_print_one_value (printer_obj, replacement);
+ if (result == NULL);
+ gdbpy_print_stack ();
+ PyGILState_Release (state);
+
+ return result;
+}
+
+/* Find a pretty-printer object for the varobj module. Returns a new
+ reference to the object if successful; returns NULL if not. VALUE
+ is the value for which a printer tests to determine if it
+ can pretty-print the value. */
+PyObject *
+gdbpy_get_varobj_pretty_printer (struct value *value)
+{
+ PyObject *val_obj;
+ PyObject *pretty_printer = NULL;
+ volatile struct gdb_exception except;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ value = value_copy (value);
+ }
+ GDB_PY_HANDLE_EXCEPTION (except);
+
+ val_obj = value_to_value_object (value);
+ if (! val_obj)
+ return NULL;
+
+ pretty_printer = find_pretty_printer (val_obj);
+ Py_DECREF (val_obj);
+ return pretty_printer;
+}
+
+/* A Python function which wraps find_pretty_printer and instantiates
+ the resulting class. This accepts a Value argument and returns a
+ pretty printer instance, or None. This function is useful as an
+ argument to the MI command -var-set-visualizer. */
+PyObject *
+gdbpy_default_visualizer (PyObject *self, PyObject *args)
+{
+ PyObject *val_obj;
+ PyObject *cons, *printer = NULL;
+ struct value *value;
+
+ if (! PyArg_ParseTuple (args, "O", &val_obj))
+ return NULL;
+ value = value_object_to_value (val_obj);
+ if (! value)
+ {
+ PyErr_SetString (PyExc_TypeError, "argument must be a gdb.Value");
+ return NULL;
+ }
+
+ cons = find_pretty_printer (val_obj);
+ return cons;
+}
+
#else /* HAVE_PYTHON */
int
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 19098cc..7f32d66 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -615,6 +615,9 @@ static PyMethodDef GdbMethods[] =
{ "get_parameter", get_parameter, METH_VARARGS,
"Return a gdb parameter's value" },
+ { "default_visualizer", gdbpy_default_visualizer, METH_VARARGS,
+ "Find the default visualizer for a Value." },
+
{ "current_objfile", gdbpy_get_current_objfile, METH_NOARGS,
"Return the current Objfile being loaded, or None." },
{ "objfiles", gdbpy_objfiles, METH_NOARGS,