[patch] Fix python gdb.execute to not paginate
Jan Kratochvil
jan.kratochvil@redhat.com
Fri Aug 6 10:29:00 GMT 2010
On Fri, 06 Aug 2010 02:49:32 +0200, Doug Evans wrote:
> IWBN to bury the implementation details. I.e. move the setting of
> batch_flag into utils.c. Plus calling init_page_info here feels
> wrong. New function in utils.c that performs all the needed changes,
OK, done.
> and have just one make_cleanup_restore_foo routine to switch back?
Using the benevolence of your '?' mark - not done in this patch. I find the
code is more simple without introducing new closure structure when the
functionality can be built from the existing cleanup stubs:
+ back_to = make_cleanup (do_restore_page_info_cleanup, NULL);
+ make_cleanup_restore_uinteger (&lines_per_page);
+ make_cleanup_restore_uinteger (&chars_per_line);
+
+ make_cleanup_restore_integer (&batch_flag);
On Fri, 06 Aug 2010 03:22:41 +0200, Doug Evans wrote:
> Actually, I wonder if it makes sense to bury more implementation
> details. Make an API of it, running a gdb command and collecting the
> result as a string.
OK, done. I would (also) like a normal canned commands $(...) support.
On Fri, 06 Aug 2010 10:34:57 +0200, Eli Zaretskii wrote:
> Okay with those changes.
Used your text.
No regressions on {x86_64,x86_64-m32,i686}-fedora14snapshot-linux-gnu.
Thanks,
Jan
gdb/
2010-08-06 Jan Kratochvil <jan.kratochvil@redhat.com>
* defs.h (make_cleanup_restore_uinteger, make_cleanup_restore_ui_file)
(set_batch_flag_and_make_cleanup_restore_page_info): New declarations.
* gdbcmd.h (execute_command_to_string): New declaration.
* python/python.c (struct restore_ui_file_closure, restore_ui_file)
(make_cleanup_restore_ui_file): Move to utils.c
(execute_gdb_command) <to_string>: Move ...
* top.c (execute_command_to_string): ... here. Call
set_batch_flag_and_make_cleanup_restore_page_info.
* utils.c (make_cleanup_restore_uinteger): New.
(struct restore_ui_file_closure, do_restore_ui_file)
(make_cleanup_restore_ui_file): Move here from python/python.c.
(init_page_info) <batch_flag>
(do_restore_page_info_cleanup)
(set_batch_flag_and_make_cleanup_restore_page_info): New.
gdb/testsuite/
2010-08-06 Jan Kratochvil <jan.kratochvil@redhat.com>
* gdb.python/python.exp (show height, set height 10)
(verify pagination beforehand, verify pagination beforehand: q)
(gdb.execute does not page, verify pagination afterwards)
(verify pagination afterwards: q): New.
gdb/doc/
2010-08-06 Jan Kratochvil <jan.kratochvil@redhat.com>
Eli Zaretskii <eliz@gnu.org>
* gdb.texinfo (Mode Options) <-batch>
(Basic Python) <gdb.execute>: Describe setting width and height.
--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -351,10 +351,14 @@ struct obstack;
extern struct cleanup *make_cleanup_obstack_free (struct obstack *obstack);
extern struct cleanup *make_cleanup_restore_integer (int *variable);
+extern struct cleanup *make_cleanup_restore_uinteger (unsigned int *variable);
struct target_ops;
extern struct cleanup *make_cleanup_unpush_target (struct target_ops *ops);
+extern
+ struct cleanup *make_cleanup_restore_ui_file (struct ui_file **variable);
+
extern struct cleanup *make_final_cleanup (make_cleanup_ftype *, void *);
extern struct cleanup *make_my_cleanup (struct cleanup **,
@@ -386,6 +390,9 @@ extern int yquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
extern void init_page_info (void);
+extern
+ struct cleanup *set_batch_flag_and_make_cleanup_restore_page_info (void);
+
extern char *gdb_realpath (const char *);
extern char *xfullpath (const char *);
--- a/gdb/gdbcmd.h
+++ b/gdb/gdbcmd.h
@@ -129,6 +129,7 @@ extern struct cmd_list_element *showchecklist;
extern struct cmd_list_element *save_cmdlist;
extern void execute_command (char *, int);
+extern char *execute_command_to_string (char *p, int from_tty);
enum command_control_type execute_control_command (struct command_line *);
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -309,33 +310,6 @@ gdbpy_target_wide_charset (PyObject *self, PyObject *args)
return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
}
-struct restore_ui_file_closure
-{
- struct ui_file **variable;
- struct ui_file *value;
-};
-
-static void
-restore_ui_file (void *p)
-{
- struct restore_ui_file_closure *closure = p;
-
- *(closure->variable) = closure->value;
-}
-
-/* Remember the current value of *VARIABLE and make it restored when
- the cleanup is run. */
-struct cleanup *
-make_cleanup_restore_ui_file (struct ui_file **variable)
-{
- struct restore_ui_file_closure *c = XNEW (struct restore_ui_file_closure);
-
- c->variable = variable;
- c->value = *variable;
-
- return make_cleanup_dtor (restore_ui_file, (void *) c, xfree);
-}
-
/* A Python function which evaluates a string using the gdb CLI. */
static PyObject *
@@ -376,27 +350,15 @@ execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
/* Copy the argument text in case the command modifies it. */
char *copy = xstrdup (arg);
struct cleanup *cleanup = make_cleanup (xfree, copy);
- struct ui_file *str_file = NULL;
if (to_string)
+ result = execute_command_to_string (copy, from_tty);
+ else
{
- str_file = mem_fileopen ();
-
- make_cleanup_restore_ui_file (&gdb_stdout);
- make_cleanup_restore_ui_file (&gdb_stderr);
- make_cleanup_ui_file_delete (str_file);
-
- gdb_stdout = str_file;
- gdb_stderr = str_file;
+ result = NULL;
+ execute_command (copy, from_tty);
}
- execute_command (copy, from_tty);
-
- if (str_file)
- result = ui_file_xstrdup (str_file, NULL);
- else
- result = NULL;
-
do_cleanups (cleanup);
}
GDB_PY_HANDLE_EXCEPTION (except);
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -458,6 +458,39 @@ execute_command (char *p, int from_tty)
}
}
+/* Run execute_command for P and FROM_TTY. Capture its output into the
+ returned string, do not display it to the screen. BATCH_FLAG will be
+ temporarily set to true. */
+
+char *
+execute_command_to_string (char *p, int from_tty)
+{
+ struct ui_file *str_file;
+ struct cleanup *cleanup;
+ char *retval;
+
+ /* GDB_STDOUT should be better already restored during these
+ restoration callbacks. */
+ cleanup = set_batch_flag_and_make_cleanup_restore_page_info ();
+
+ str_file = mem_fileopen ();
+
+ make_cleanup_restore_ui_file (&gdb_stdout);
+ make_cleanup_restore_ui_file (&gdb_stderr);
+ make_cleanup_ui_file_delete (str_file);
+
+ gdb_stdout = str_file;
+ gdb_stderr = str_file;
+
+ execute_command (p, from_tty);
+
+ retval = ui_file_xstrdup (str_file, NULL);
+
+ do_cleanups (cleanup);
+
+ return retval;
+}
+
/* Read commands from `instream' and execute them
until end of file or error reading instream. */
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -352,6 +352,14 @@ make_cleanup_restore_integer (int *variable)
xfree);
}
+/* Remember the current value of *VARIABLE and make it restored when the cleanup
+ is run. */
+struct cleanup *
+make_cleanup_restore_uinteger (unsigned int *variable)
+{
+ return make_cleanup_restore_integer ((int *) variable);
+}
+
/* Helper for make_cleanup_unpush_target. */
static void
@@ -370,6 +378,33 @@ make_cleanup_unpush_target (struct target_ops *ops)
return make_my_cleanup (&cleanup_chain, do_unpush_target, ops);
}
+struct restore_ui_file_closure
+{
+ struct ui_file **variable;
+ struct ui_file *value;
+};
+
+static void
+do_restore_ui_file (void *p)
+{
+ struct restore_ui_file_closure *closure = p;
+
+ *(closure->variable) = closure->value;
+}
+
+/* Remember the current value of *VARIABLE and make it restored when
+ the cleanup is run. */
+struct cleanup *
+make_cleanup_restore_ui_file (struct ui_file **variable)
+{
+ struct restore_ui_file_closure *c = XNEW (struct restore_ui_file_closure);
+
+ c->variable = variable;
+ c->value = *variable;
+
+ return make_cleanup_dtor (do_restore_ui_file, (void *) c, xfree);
+}
+
struct cleanup *
make_my_cleanup2 (struct cleanup **pmy_chain, make_cleanup_ftype *function,
void *arg, void (*free_arg) (void *))
@@ -2052,6 +2087,12 @@ static int wrap_column;
void
init_page_info (void)
{
+ if (batch_flag)
+ {
+ lines_per_page = UINT_MAX;
+ chars_per_line = UINT_MAX;
+ }
+ else
#if defined(TUI)
if (!tui_get_command_dimension (&chars_per_line, &lines_per_page))
#endif
@@ -2096,6 +2137,32 @@ init_page_info (void)
set_width ();
}
+static void
+do_restore_page_info_cleanup (void *arg)
+{
+ set_screen_size ();
+ set_width ();
+}
+
+/* Temporarily set BATCH_FLAG and the associated unlimited terminal size.
+ Provide cleanup for restoring the original state. */
+
+struct cleanup *
+set_batch_flag_and_make_cleanup_restore_page_info (void)
+{
+ struct cleanup *back_to;
+
+ back_to = make_cleanup (do_restore_page_info_cleanup, NULL);
+ make_cleanup_restore_uinteger (&lines_per_page);
+ make_cleanup_restore_uinteger (&chars_per_line);
+
+ make_cleanup_restore_integer (&batch_flag);
+ batch_flag = 1;
+ init_page_info ();
+
+ return back_to;
+}
+
/* Set the screen size based on LINES_PER_PAGE and CHARS_PER_LINE. */
static void
--- a/gdb/testsuite/gdb.python/python.exp
+++ b/gdb/testsuite/gdb.python/python.exp
@@ -87,3 +87,26 @@ gdb_test "python import itertools; print 'IMPOR'+'TED'" "IMPORTED" "pythonX.Y/li
gdb_test_no_output \
"python x = gdb.execute('printf \"%d\", 23', to_string = True)"
gdb_test "python print x" "23"
+
+# Test (no) pagination of the executed command.
+gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
+set lines 10
+gdb_test_no_output "set height $lines"
+
+set test "verify pagination beforehand"
+gdb_test_multiple "python print \"\\n\" * $lines" $test {
+ -re "---Type <return> to continue, or q <return> to quit---$" {
+ pass $test
+ }
+}
+gdb_test "q" "Quit" "verify pagination beforehand: q"
+
+gdb_test "python if gdb.execute('python print \"\\\\n\" * $lines', to_string=True) == \"\\n\" * [expr $lines + 1]: print \"yes\"" "yes" "gdb.execute does not page"
+
+set test "verify pagination afterwards"
+gdb_test_multiple "python print \"\\n\" * $lines" $test {
+ -re "---Type <return> to continue, or q <return> to quit---$" {
+ pass $test
+ }
+}
+gdb_test "q" "Quit" "verify pagination afterwards: q"
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -1031,9 +1031,9 @@ Run in batch mode. Exit with status @code{0} after processing all the
command files specified with @samp{-x} (and all commands from
initialization files, if not inhibited with @samp{-n}). Exit with
nonzero status if an error occurs in executing the @value{GDBN} commands
-in the command files. Batch mode also disables pagination;
-@pxref{Screen Size} and acts as if @kbd{set confirm off} were in
-effect (@pxref{Messages/Warnings}).
+in the command files. Batch mode also disables pagination, sets unlimited
+terminal width and height @pxref{Screen Size}, and acts as if @kbd{set confirm
+off} were in effect (@pxref{Messages/Warnings}).
Batch mode may be useful for running @value{GDBN} as a filter, for
example to download and run a program on another computer; in order to
@@ -20484,7 +20484,9 @@ By default, any output produced by @var{command} is sent to
@value{GDBN}'s standard output. If the @var{to_string} parameter is
@code{True}, then output will be collected by @code{gdb.execute} and
returned as a string. The default is @code{False}, in which case the
-return value is @code{None}.
+return value is @code{None}. If @var{to_string} is @code{True}, the
+@value{GDBN} virtual terminal will be temporarily set to unlimited width
+and height, and its pagination will be disabled; @pxref{Screen Size}.
@end defun
@findex gdb.breakpoints
More information about the Gdb-patches
mailing list