aboutsummaryrefslogtreecommitdiff
path: root/gdb/python/python.c
diff options
context:
space:
mode:
authorTom Tromey <tromey@redhat.com>2012-11-12 17:41:59 +0000
committerTom Tromey <tromey@redhat.com>2012-11-12 17:41:59 +0000
commit18a9fc1261b8a654fe6db7eea7e60b0c59296d96 (patch)
tree50b1290ab9827f259ea103cf50367d7ac6c0e878 /gdb/python/python.c
parentbd69fc683f383772bb8fab43c5d4af8d0cd4a8b4 (diff)
downloadbinutils-18a9fc1261b8a654fe6db7eea7e60b0c59296d96.zip
binutils-18a9fc1261b8a654fe6db7eea7e60b0c59296d96.tar.gz
binutils-18a9fc1261b8a654fe6db7eea7e60b0c59296d96.tar.bz2
* NEWS: Update.
* data-directory/Makefile.in (PYTHON_FILES): Add type_printers.py. * python/lib/gdb/command/type_printers.py: New file. * python/lib/gdb/command/types.py (TypePrinter): New class. (_get_some_type_recognizers, get_type_recognizers, apply_type_recognizers, register_type_printer): New functions. * python/py-objfile.c (objfile_object) <type_printers>: New field. (objfpy_dealloc): Decref new field. (objfpy_new): Set new field. (objfpy_get_type_printers, objfpy_set_type_printers): New functions. (objfile_to_objfile_object): Set new field. (objfile_getset): Add "type_printers". * python/py-progspace.c (pspace_object) <type_printers>: New field. (pspy_dealloc): Decref new field. (pspy_new): Set new field. (pspy_get_type_printers, pspy_set_type_printers): New functions. (pspace_to_pspace_object): Set new field. (pspace_getset): Add "type_printers". * python/python.c (start_type_printers, apply_type_printers, free_type_printers): New functions. (_initialize_python): Set gdb.type_printers. * python/python.h (start_type_printers, apply_type_printers, free_type_printers): Declare. * typeprint.c (type_print_raw_options, default_ptype_flags): Update for new fields. (do_free_global_table, create_global_typedef_table, find_global_typedef): New functions. (find_typedef_in_hash): Use find_global_typedef. (whatis_exp): Use create_global_typedef_table. Change cleanup handling. * typeprint.h (struct type_print_options) <global_typedefs, global_printers>: New fields. doc * gdb.texinfo (Symbols): Document "info type-printers", "enable type-printer" and "disable type-printer". (Python API): Add new node to menu. (Type Printing API): New node. (Progspaces In Python): Document type_printers field. (Objfiles In Python): Likewise. (gdb.types) <get_type_recognizers, apply_type_recognizers, register_type_printer, TypePrinter>: Document. testsuite * gdb.base/completion.exp: Update for "info type-printers". * gdb.python/py-typeprint.cc: New file. * gdb.python/py-typeprint.exp: New file. * gdb.python/py-typeprint.py: New file.
Diffstat (limited to 'gdb/python/python.c')
-rw-r--r--gdb/python/python.c136
1 files changed, 136 insertions, 0 deletions
diff --git a/gdb/python/python.c b/gdb/python/python.c
index cd50e50..359d238 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1181,6 +1181,125 @@ gdbpy_objfiles (PyObject *unused1, PyObject *unused2)
return list;
}
+/* Compute the list of active type printers and return it. The result
+ of this function can be passed to apply_type_printers, and should
+ be freed by free_type_printers. */
+
+void *
+start_type_printers (void)
+{
+ struct cleanup *cleanups;
+ PyObject *type_module, *func, *result_obj;
+
+ cleanups = ensure_python_env (get_current_arch (), current_language);
+
+ type_module = PyImport_ImportModule ("gdb.types");
+ if (type_module == NULL)
+ {
+ gdbpy_print_stack ();
+ goto done;
+ }
+ make_cleanup_py_decref (type_module);
+
+ func = PyObject_GetAttrString (type_module, "get_type_recognizers");
+ if (func == NULL)
+ {
+ gdbpy_print_stack ();
+ goto done;
+ }
+ make_cleanup_py_decref (func);
+
+ result_obj = PyObject_CallFunctionObjArgs (func, (char *) NULL);
+ if (result_obj == NULL)
+ gdbpy_print_stack ();
+
+ done:
+ do_cleanups (cleanups);
+ return result_obj;
+}
+
+/* If TYPE is recognized by some type printer, return a newly
+ allocated string holding the type's replacement name. The caller
+ is responsible for freeing the string. Otherwise, return NULL.
+
+ This function has a bit of a funny name, since it actually applies
+ recognizers, but this seemed clearer given the start_type_printers
+ and free_type_printers functions. */
+
+char *
+apply_type_printers (void *printers, struct type *type)
+{
+ struct cleanup *cleanups;
+ PyObject *type_obj, *type_module, *func, *result_obj;
+ PyObject *printers_obj = printers;
+ char *result = NULL;
+
+ if (printers_obj == NULL)
+ return NULL;
+
+ cleanups = ensure_python_env (get_current_arch (), current_language);
+
+ type_obj = type_to_type_object (type);
+ if (type_obj == NULL)
+ {
+ gdbpy_print_stack ();
+ goto done;
+ }
+ make_cleanup_py_decref (type_obj);
+
+ type_module = PyImport_ImportModule ("gdb.types");
+ if (type_module == NULL)
+ {
+ gdbpy_print_stack ();
+ goto done;
+ }
+ make_cleanup_py_decref (type_module);
+
+ func = PyObject_GetAttrString (type_module, "apply_type_recognizers");
+ if (func == NULL)
+ {
+ gdbpy_print_stack ();
+ goto done;
+ }
+ make_cleanup_py_decref (func);
+
+ result_obj = PyObject_CallFunctionObjArgs (func, printers_obj,
+ type_obj, (char *) NULL);
+ if (result_obj == NULL)
+ {
+ gdbpy_print_stack ();
+ goto done;
+ }
+ make_cleanup_py_decref (result_obj);
+
+ if (result_obj != Py_None)
+ {
+ result = python_string_to_host_string (result_obj);
+ if (result == NULL)
+ gdbpy_print_stack ();
+ }
+
+ done:
+ do_cleanups (cleanups);
+ return result;
+}
+
+/* Free the result of start_type_printers. */
+
+void
+free_type_printers (void *arg)
+{
+ struct cleanup *cleanups;
+ PyObject *printers = arg;
+
+ if (printers == NULL)
+ return;
+
+ cleanups = ensure_python_env (get_current_arch (), current_language);
+ Py_DECREF (printers);
+ do_cleanups (cleanups);
+}
+
#else /* HAVE_PYTHON */
/* Dummy implementation of the gdb "python-interactive" and "python"
@@ -1238,6 +1357,23 @@ gdbpy_breakpoint_has_py_cond (struct breakpoint_object *bp_obj)
"scripting is not supported."));
}
+void *
+start_type_printers (void)
+{
+ return NULL;
+}
+
+char *
+apply_type_printers (void *ignore, struct type *type)
+{
+ return NULL;
+}
+
+void
+free_type_printers (void *arg)
+{
+}
+
#endif /* HAVE_PYTHON */