From b6313243984b74ee6772dd8273b0e6073ad1815b Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Thu, 28 May 2009 01:09:20 +0000 Subject: gdb 2009-05-27 Vladimir Prus Tom Tromey Thiago Jung Bauermann * mi/mi-main.c (mi_cmd_list_features): List "python" feature. * varobj.h (varobj_set_visualizer): Declare. (varobj_get_display_hint): Likewise. (varobj_update_result_t) : New fields. * mi/mi-cmds.c (mi_cmds): Add var-set-visualizer. * mi/mi-cmds.h (mi_cmd_var_set_visualizer, mi_cmd_var_set_child_range): Declare. * mi/mi-cmd-var.c (mi_cmd_var_set_visualizer): New function. (mi_cmd_var_list_children): Emit display hint. (varobj_update_one): Emit display hint. Handle dynamic children. * python/python.c (GdbMethods): Add "default_visualizer". * python/python-internal.h (apply_varobj_pretty_printer, gdbpy_get_varobj_pretty_printer, gdbpy_get_display_hint): Declare. (gdbpy_default_visualizer): Likewise. * varobj.c: Include python.h, python-internal.h. (PyObject): New typedef. (struct varobj) : New fields. (varobj_create): Call install_default_visualizer. (instantiate_pretty_printer): New function. (varobj_set_display_format): Update. (varobj_get_display_hint): New function. (update_dynamic_varobj_children): New function. (varobj_get_num_children): Handle dynamic children. (varobj_list_children): Likewise. (install_new_value): Likewise. (varobj_add_child): New function. (install_visualizer): Likewise. (install_default_visualizer): Likewise. (varobj_set_visualizer): Likewise. (varobj_update): Handle dynamic children. (create_child): Use create_child_with_value. (create_child_with_value): New function. (value_get_print_value): Call pretty printer. Add value_formatter argument. (c_value_of_variable): Update. (varobj_invalidate): Always free all_rootvarobj. * python/python-prettyprint.c (apply_varobj_pretty_printer): New function. (gdbpy_get_varobj_pretty_printer): Likewise. (gdbpy_default_visualizer): Likewise. gdb/doc 2009-05-27 Tom Tromey * gdb.texinfo (GDB/MI Miscellaneous Commands): Document "python" feature. (GDB/MI Variable Objects): Document -var-set-visualizer. gdb/testsuite 2009-05-27 Tom Tromey Thiago Jung Bauermann * lib/mi-support.exp (mi_varobj_update_dynamic): New proc. (mi_child_regexp): Likewise. (mi_list_varobj_children_range): Likewise. (mi_get_features): Likewise. (mi_list_varobj_children): Rewrite. * gdb.python/python-mi.exp: New file. --- gdb/python/python-internal.h | 8 +++++ gdb/python/python-prettyprint.c | 74 +++++++++++++++++++++++++++++++++++++++++ gdb/python/python.c | 3 ++ 3 files changed, 85 insertions(+) (limited to 'gdb/python') 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, -- cgit v1.1