From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28900 invoked by alias); 12 Nov 2008 01:54:31 -0000 Mailing-List: contact archer-commits-help@sourceware.org; run by ezmlm Sender: Precedence: bulk List-Post: List-Help: List-Subscribe: Received: (qmail 28830 invoked by uid 306); 12 Nov 2008 01:54:30 -0000 Date: Wed, 12 Nov 2008 01:54:00 -0000 Message-ID: <20081112015430.28786.qmail@sourceware.org> From: tromey@sourceware.org To: archer-commits@sourceware.org Subject: [SCM] archer-tromey-python: gdb X-Git-Refname: refs/heads/archer-tromey-python X-Git-Reftype: branch X-Git-Oldrev: 97c867eb4023181294200de2f15111dc425ed93b X-Git-Newrev: 4d3313b792a9e87b61a11f42b08e7ec29414c15d X-SW-Source: 2008-q4/txt/msg00094.txt.bz2 List-Id: The branch, archer-tromey-python has been updated via 4d3313b792a9e87b61a11f42b08e7ec29414c15d (commit) via 1a936d11fbb67589fbf52540a7121614469da8aa (commit) via fa91a4ceeeced81d5bd9c4708503a2ba7ff4d693 (commit) from 97c867eb4023181294200de2f15111dc425ed93b (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email. - Log ----------------------------------------------------------------- commit 4d3313b792a9e87b61a11f42b08e7ec29414c15d Author: Tom Tromey Date: Tue Nov 11 18:52:27 2008 -0700 gdb * varobj.c (varobj_get_display_hint): Use gdbpy_get_display_hint. * python/python.c (apply_pretty_printer): Remove. (gdbpy_get_display_hint): New function. (print_string_repr): New function. (print_children): Likewise. (apply_val_pretty_printer): Rewrite. Change return type. (_initialize_python) <_format_children>: Remove. (pretty_print_one_value): Remove 'children' argument. (apply_varobj_pretty_printer): Update. * python/python.h (apply_pretty_printer): Remove. (apply_val_pretty_printer): Update. * utils.c (fputs_indented): Remove. * defs.h (fputs_indented): Remove. * valprint.c (value_print): Don't pretty-print. (val_print): Update. * python/python-internal.h (gdbpy_get_display_hint): Declare. gdb/testsuite * gdb.python/python-prettyprint.exp (run_lang_tests): Ensure "set print pretty" is on. Update for recent changes. commit 1a936d11fbb67589fbf52540a7121614469da8aa Author: Tom Tromey Date: Tue Nov 11 18:43:13 2008 -0700 * Makefile.in (HFILES_NO_SRCDIR): Add python headers. Remove obsolete headers. commit fa91a4ceeeced81d5bd9c4708503a2ba7ff4d693 Author: Tom Tromey Date: Mon Nov 10 14:26:08 2008 -0700 * python/python-objfile.c (gdbpy_initialize_objfile): Don't set tp_new. ----------------------------------------------------------------------- Summary of changes: gdb/ChangeLog | 29 +++ gdb/Makefile.in | 7 +- gdb/defs.h | 2 - gdb/python/python-internal.h | 1 + gdb/python/python-objfile.c | 1 - gdb/python/python.c | 285 ++++++++++++++-------- gdb/python/python.h | 14 +- gdb/testsuite/ChangeLog | 5 + gdb/testsuite/gdb.python/python-prettyprint.exp | 17 +- gdb/utils.c | 31 --- gdb/valprint.c | 33 +--- gdb/varobj.c | 16 +-- 12 files changed, 244 insertions(+), 197 deletions(-) First 500 lines of diff: diff --git a/gdb/ChangeLog b/gdb/ChangeLog index ceaa078..d1b0bbd 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,32 @@ +2008-11-11 Tom Tromey + + * varobj.c (varobj_get_display_hint): Use gdbpy_get_display_hint. + * python/python.c (apply_pretty_printer): Remove. + (gdbpy_get_display_hint): New function. + (print_string_repr): New function. + (print_children): Likewise. + (apply_val_pretty_printer): Rewrite. Change return type. + (_initialize_python) <_format_children>: Remove. + (pretty_print_one_value): Remove 'children' argument. + (apply_varobj_pretty_printer): Update. + * python/python.h (apply_pretty_printer): Remove. + (apply_val_pretty_printer): Update. + * utils.c (fputs_indented): Remove. + * defs.h (fputs_indented): Remove. + * valprint.c (value_print): Don't pretty-print. + (val_print): Update. + * python/python-internal.h (gdbpy_get_display_hint): Declare. + +2008-11-11 Tom Tromey + + * Makefile.in (HFILES_NO_SRCDIR): Add python headers. Remove + obsolete headers. + +2008-11-10 Tom Tromey + + * python/python-objfile.c (gdbpy_initialize_objfile): Don't set + tp_new. + 2008-11-10 Tom Tromey * python/python-type.c (set_type): Use parent's originator, if it diff --git a/gdb/Makefile.in b/gdb/Makefile.in index a0716af..9329892 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -700,7 +700,7 @@ osf-share/cma_stack_int.h osf-share/cma_init.h \ osf-share/cma_deb_core.h osf-share/AT386/cma_thread_io.h \ osf-share/cma_sched.h proc-utils.h arm-tdep.h ax-gdb.h ppcnbsd-tdep.h \ cli-out.h gdb_expat.h breakpoint.h infcall.h obsd-tdep.h gnu-v2-abi.h \ -exec.h m32r-tdep.h osabi.h gdb-events.h gdbcore.h solib-som.h \ +exec.h m32r-tdep.h osabi.h gdbcore.h solib-som.h \ i386bsd-nat.h xml-support.h xml-tdesc.h alphabsd-tdep.h gdb_obstack.h \ ia64-tdep.h ada-lang.h varobj.h frv-tdep.h nto-tdep.h serial.h \ c-lang.h frame.h event-loop.h block.h cli/cli-setshow.h \ @@ -717,7 +717,7 @@ gdbarch.h bsd-uthread.h gdb_thread_db.h gdb_stat.h memory-map.h \ mdebugread.h m88k-tdep.h stabsread.h hppa-linux-offsets.h linux-fork.h \ ser-unix.h scm-lang.h inf-ptrace.h terminal.h ui-out.h frame-base.h \ f-lang.h dwarf2loc.h value.h sparc-tdep.h defs.h target-descriptions.h \ -objfiles.h vec.h disasm.h mips-tdep.h gstdint.h ser-base.h \ +objfiles.h vec.h disasm.h mips-tdep.h ser-base.h \ gdb_curses.h bfd-target.h memattr.h inferior.h ax.h dummy-frame.h \ inflow.h fbsd-nat.h libunwind-frame.h completer.h inf-ttrace.h \ solib-target.h shnbsd-tdep.h gdb_vfork.h alpha-tdep.h dwarf2expr.h \ @@ -750,7 +750,8 @@ config/sparc/nm-sol2.h config/nm-linux.h config/mips/nm-irix5.h \ config/rs6000/nm-rs6000.h top.h bsd-kvm.h gdb-stabs.h reggroups.h \ annotate.h sim-regno.h dictionary.h dfp.h main.h frame-unwind.h \ remote-fileio.h i386-linux-tdep.h vax-tdep.h objc-lang.h \ -sentinel-frame.h bcache.h symfile.h +sentinel-frame.h bcache.h symfile.h \ +python/python.h python/python-internal.h # Header files that already have srcdir in them, or which are in objdir. diff --git a/gdb/defs.h b/gdb/defs.h index 21f1daa..23a6599 100644 --- a/gdb/defs.h +++ b/gdb/defs.h @@ -446,8 +446,6 @@ extern struct ui_file *gdb_stdtargin; extern void fputs_filtered (const char *, struct ui_file *); -extern void fputs_indented (const char *, struct ui_file *); - extern void fputs_unfiltered (const char *, struct ui_file *); extern int fputc_filtered (int c, struct ui_file *); diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 71c2297..8f756c8 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -130,6 +130,7 @@ char *apply_varobj_pretty_printer (PyObject *print_obj, struct value *value, struct value **replacement); PyObject *gdbpy_get_varobj_pretty_printer (struct type *type); PyObject *gdbpy_instantiate_printer (PyObject *cons, struct value *value); +char *gdbpy_get_display_hint (PyObject *printer); extern PyObject *gdbpy_children_cst; extern PyObject *gdbpy_to_string_cst; diff --git a/gdb/python/python-objfile.c b/gdb/python/python-objfile.c index 0a942c8..335e05b 100644 --- a/gdb/python/python-objfile.c +++ b/gdb/python/python-objfile.c @@ -159,7 +159,6 @@ gdbpy_initialize_objfile (void) objfpy_objfile_data_key = register_objfile_data_with_cleanup (clean_up_objfile); - objfile_object_type.tp_new = PyType_GenericNew; if (PyType_Ready (&objfile_object_type) < 0) return; diff --git a/gdb/python/python.c b/gdb/python/python.c index 9f4448b..206b4aa 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -748,12 +748,9 @@ find_pretty_printer (struct type *type) the function returns a string, an xmalloc()d copy is returned. Otherwise, if the function returns a value, a *OUT_VALUE is set to the value, and NULL is returned. On error, *OUT_VALUE is set to - NULL and NULL is returned. If CHILDREN is true, we may also try to - call an object's "children" method and format the output - accordingly. */ + NULL and NULL is returned. */ static char * -pretty_print_one_value (PyObject *printer, struct value **out_value, - int children) +pretty_print_one_value (PyObject *printer, struct value **out_value) { char *output = NULL; volatile struct gdb_exception except; @@ -762,20 +759,7 @@ pretty_print_one_value (PyObject *printer, struct value **out_value, { PyObject *result; - /* If CHILDREN is true, call _format_children. Otherwise, just - try to call the object's to_string method. */ - if (children - && PyObject_HasAttrString (gdb_module, "_format_children")) - { - PyObject *fmt = PyObject_GetAttrString (gdb_module, - "_format_children"); - result = PyObject_CallFunctionObjArgs (fmt, printer, NULL); - Py_DECREF (fmt); - } - else - result = PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, - NULL); - + result = PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, NULL); if (result) { if (gdbpy_is_string (result)) @@ -821,44 +805,172 @@ gdbpy_instantiate_printer (PyObject *cons, struct value *value) return result; } -/* Try to pretty-print VALUE. Return an xmalloc()d string - representation of the value. If the result is NULL, and *OUT_VALUE - is set, then *OUT_VALUE is a value which should be printed in place - of VALUE. *OUT_VALUE is not passed back to the pretty-printer. - Returns NULL and sets *OUT_VALUE to NULL on error or if no - pretty-printer was available. */ +/* Return the display hint for the object printer, PRINTER. Return + NULL if there is no display_hint method, or if the method did not + return a string. On error, print stack trace and return NULL. On + success, return an xmalloc()d string. */ char * -apply_pretty_printer (struct value *value, struct value **out_value) +gdbpy_get_display_hint (PyObject *printer) { - PyObject *func, *printer; - char *output = NULL; + PyObject *hint; + char *result = NULL; - *out_value = NULL; - - /* Find the constructor. */ - func = find_pretty_printer (value_type (value)); - if (! func) + if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst)) return NULL; - /* Instantiate it to get a printer object. */ - printer = gdbpy_instantiate_printer (func, value); - if (printer) + hint = PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst, NULL); + if (gdbpy_is_string (hint)) + result = python_string_to_host_string (hint); + if (hint) + Py_DECREF (hint); + else + gdbpy_print_stack (); + + return result; +} + +/* Helper for apply_val_pretty_printer which calls to_string and + formats the result. */ +static void +print_string_repr (PyObject *printer, struct ui_file *stream, int format, + int deref_ref, int recurse, + enum val_prettyprint pretty, + const struct language_defn *language) +{ + char *output; + struct value *replacement = NULL; + + output = pretty_print_one_value (printer, &replacement); + if (output) { - /* If instantiation returns None, then don't try to print. */ - if (printer != Py_None) - output = pretty_print_one_value (printer, out_value, 1); - Py_DECREF (printer); + fputs_filtered (output, stream); + xfree (output); } - Py_DECREF (func); + else if (replacement) + common_val_print (replacement, stream, format, deref_ref, + recurse, pretty, language); + else + gdbpy_print_stack (); +} - return output; +/* Helper for apply_val_pretty_printer that formats children of the + printer, if any exist. */ +static void +print_children (PyObject *printer, struct ui_file *stream, int format, + int deref_ref, int recurse, + enum val_prettyprint pretty, + const struct language_defn *language) +{ + char *hint; + int is_map = 0, i; + PyObject *children, *iter; + struct cleanup *cleanups; + + if (! PyObject_HasAttr (printer, gdbpy_children_cst)) + return; + + /* If we are printing a map, we want some special formatting. */ + hint = gdbpy_get_display_hint (printer); + if (hint) + { + is_map = ! strcmp (hint, "map"); + xfree (hint); + } + + children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst, + NULL); + if (! children) + { + gdbpy_print_stack (); + return; + } + + cleanups = make_cleanup_py_decref (children); + + iter = PyObject_GetIter (children); + if (!iter) + { + gdbpy_print_stack (); + goto done; + } + make_cleanup_py_decref (iter); + + for (i = 0; ; ++i) + { + PyObject *py_v, *item = PyIter_Next (iter); + char *name; + struct cleanup *inner_cleanup; + + if (! item) + break; + inner_cleanup = make_cleanup_py_decref (item); + + if (! PyArg_ParseTuple (item, "sO", &name, &py_v)) + { + gdbpy_print_stack (); + continue; + } + + if (i == 0) + fputs_filtered (" = {", stream); + else if (! is_map || i % 2 == 0) + fputs_filtered (pretty ? "," : ", ", stream); + + if (pretty && (! is_map || i % 2 == 0)) + { + fputs_filtered ("\n", stream); + print_spaces_filtered (2 + 2 * recurse, stream); + } + + if (is_map && i % 2 == 0) + fputs_filtered ("[", stream); + else if (! is_map) + { + fputs_filtered (name, stream); + fputs_filtered (" = ", stream); + } + + if (gdbpy_is_string (py_v)) + { + char *text = python_string_to_host_string (py_v); + if (! text) + gdbpy_print_stack (); + else + { + fputs_filtered (text, stream); + xfree (text); + } + } + else + { + struct value *value = convert_value_from_python (py_v); + common_val_print (value, stream, format, deref_ref, + recurse + 1, pretty, language); + } + + if (is_map && i % 2 == 0) + fputs_filtered ("] = ", stream); + else if (! pretty) + wrap_here (n_spaces (2 + 2 * recurse)); + + do_cleanups (inner_cleanup); + } + + if (i) + { + if (pretty) + { + fputs_filtered ("\n", stream); + print_spaces_filtered (2 * recurse, stream); + } + fputs_filtered ("}", stream); + } + + done: + do_cleanups (cleanups); } -/* Like apply_pretty_printer, but called from the 'val' (and not - 'value') printing code. Arguments are as for val_print. Returns - an xmalloc()d pretty-printed string if a pretty-printer was found - and was successful. */ -char * +int apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr, int embedded_offset, CORE_ADDR address, struct ui_file *stream, int format, @@ -867,13 +979,15 @@ apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr, const struct language_defn *language) { PyObject *func, *printer; - struct value *value, *replacement = NULL; - char *output = NULL; + struct value *value; + char *hint; + struct cleanup *cleanups; + int result = 0; /* Find the constructor. */ func = find_pretty_printer (type); if (! func) - return NULL; + return 0; /* Instantiate the printer. */ value = value_from_contents_and_address (type, valaddr, embedded_offset, @@ -882,25 +996,24 @@ apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr, Py_DECREF (func); if (!printer) - return NULL; + { + gdbpy_print_stack (); + return 0; + } + cleanups = make_cleanup_py_decref (printer); if (printer != Py_None) - output = pretty_print_one_value (printer, &replacement, 1); - Py_DECREF (printer); - if (output) - return output; - - if (! replacement) - return NULL; + { + print_string_repr (printer, stream, format, deref_ref, + recurse, pretty, language); + print_children (printer, stream, format, deref_ref, + recurse, pretty, language); - language->la_val_print (value_type (replacement), - value_contents_all (replacement), - value_embedded_offset (replacement), - value_address (replacement), - stream, format, deref_ref, recurse, - pretty); + result = 1; + } - return xstrdup (""); + do_cleanups (cleanups); + return result; } /* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the @@ -916,7 +1029,7 @@ apply_varobj_pretty_printer (PyObject *printer_obj, struct value *value, struct value **replacement) { *replacement = NULL; - return pretty_print_one_value (printer_obj, replacement, 0); + return pretty_print_one_value (printer_obj, replacement); } /* Find a pretty-printer object for the varobj module. Returns a new @@ -995,14 +1108,7 @@ eval_python_from_control_command (struct command_line *cmd) error (_("Python scripting is not supported in this copy of GDB.")); } -char * -apply_pretty_printer (struct value *ignore, struct value **out) -{ - *out = NULL; - return NULL; -} - -char * +int apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr, int embedded_offset, CORE_ADDR address, struct ui_ifle *stream, int format, @@ -1010,7 +1116,7 @@ apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr, enum val_prettyprint pretty, const language_defn *language) { - return NULL; + return 0; } void @@ -1155,35 +1261,6 @@ sys.stderr = GdbOutputFile()\n\ sys.stdout = GdbOutputFile()\n\ "); - PyRun_SimpleString ("\ -def _format_children(obj):\n\ - result = []\n\ - if hasattr(obj, 'to_string'):\n\ - result.append(str(obj.to_string()))\n\ - if hasattr(obj, 'display_hint'):\n\ - is_map = 'map' == obj.display_hint()\n\ - else:\n\ - is_map = False\n\ - max = gdb.get_parameter('print elements')\n\ - i = 0\n\ - previous = None\n\ - if hasattr(obj, 'children'):\n\ - for elt in obj.children():\n\ - (name, val) = elt\n\ - if is_map:\n\ - if i % 2 == 0:\n\ - previous = val\n\ - else:\n\ - result.append('[%s] = %s' % (str (previous), str (val)))\n\ - else:\n\ - result.append('%s = %s' % (name, str(val)))\n\ - i = i + 1\n\ - if max != None and i == max:\n\ - break\n\ - return '\\n'.join(result)\n\ -\n\ -gdb._format_children = _format_children\n\ -"); #endif /* HAVE_PYTHON */ } diff --git a/gdb/python/python.h b/gdb/python/python.h index ef0560c..a2aec3f 100644 --- a/gdb/python/python.h +++ b/gdb/python/python.h @@ -30,13 +30,11 @@ void source_python_script (FILE *stream, char *file); void run_python_script (int argc, char **argv); -char *apply_pretty_printer (struct value *, struct value **); - -char *apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr, - int embedded_offset, CORE_ADDR address, - struct ui_file *stream, int format, - int deref_ref, int recurse, - enum val_prettyprint pretty, - const struct language_defn *language); +int apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr, + int embedded_offset, CORE_ADDR address, + struct ui_file *stream, int format, + int deref_ref, int recurse, + enum val_prettyprint pretty, + const struct language_defn *language); hooks/post-receive -- Repository for Project Archer.