public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [patch] Fix python gdb.execute to not paginate
@ 2010-08-05 21:20 Jan Kratochvil
  2010-08-06  0:49 ` Doug Evans
  2010-08-06  8:35 ` Eli Zaretskii
  0 siblings, 2 replies; 11+ messages in thread
From: Jan Kratochvil @ 2010-08-05 21:20 UTC (permalink / raw)
  To: gdb-patches; +Cc: David Malcolm

Hi,

downstream Bug: https://bugzilla.redhat.com/show_bug.cgi?id=620930
(gdb) python for i in range(100): a = gdb.execute('info registers', to_string=True)
---Type <return> to continue, or q <return> to quit---

When at it I have merged it with making --batch more batch, as the output IMO
should not depend on the momentarily terminal - wrapping and indenting the
output on its width.

The patch makes python gdb.execute running in the --batch mode.  I understand
it may not be universally right but so far I believe it is.  What do you
think?

(--batch should be IMO somehow merged now with `set interactive-mode'.  That
is not a part of this patch.)


Regards,
Jan


gdb/
2010-08-05  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* defs.h (make_cleanup_restore_uinteger)
	(make_cleanup_restore_page_info): New declarations.
	* python/python.c: Include main.h.
	(execute_gdb_command) <to_string>: Temporarily set BATCH_FLAG and call
	init_page_info.
	* utils.c (make_cleanup_restore_uinteger)
	(init_page_info) <batch_flag>
	(do_restore_page_info_cleanup, make_cleanup_restore_page_info): New.

gdb/testsuite/
2010-08-05  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-05  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* gdb.texinfo (Mode Options) <-batch>
	(Basic Python) <gdb.execute>: Describe setting width and height.

--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -351,6 +351,7 @@ 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);
@@ -385,6 +386,7 @@ extern int nquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
 extern int yquery (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
 
 extern void init_page_info (void);
+extern struct cleanup *make_cleanup_restore_page_info (void);
 
 extern char *gdb_realpath (const char *);
 extern char *xfullpath (const char *);
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -46,6 +46,7 @@ static int gdbpy_should_print_stack = 1;
 #include "version.h"
 #include "target.h"
 #include "gdbthread.h"
+#include "main.h"
 
 static PyMethodDef GdbMethods[];
 
@@ -380,6 +381,13 @@ execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
 
       if (to_string)
 	{
+	  /* GDB_STDOUT should be better already restored during these
+	     restoration callbacks.  */
+	  make_cleanup_restore_page_info ();
+	  make_cleanup_restore_integer (&batch_flag);
+	  batch_flag = 1;
+	  init_page_info ();
+
 	  str_file = mem_fileopen ();
 
 	  make_cleanup_restore_ui_file (&gdb_stdout);
--- 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
@@ -2052,6 +2060,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 +2110,25 @@ init_page_info (void)
   set_width ();
 }
 
+static void
+do_restore_page_info_cleanup (void *arg)
+{
+  set_screen_size ();
+  set_width ();
+}
+
+struct cleanup *
+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);
+
+  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}.  With @code{True} @var{to_string} the
+@value{GDBN} virtual terminal has temporarily set unlimited width and height
+with disabled pagination; @pxref{Screen Size}.
 @end defun
 
 @findex gdb.breakpoints

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [patch] Fix python gdb.execute to not paginate
  2010-08-05 21:20 [patch] Fix python gdb.execute to not paginate Jan Kratochvil
@ 2010-08-06  0:49 ` Doug Evans
  2010-08-06  1:22   ` Doug Evans
  2010-08-06 10:29   ` Jan Kratochvil
  2010-08-06  8:35 ` Eli Zaretskii
  1 sibling, 2 replies; 11+ messages in thread
From: Doug Evans @ 2010-08-06  0:49 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches, David Malcolm

On Thu, Aug 5, 2010 at 2:20 PM, Jan Kratochvil
<jan.kratochvil@redhat.com> wrote:
> Hi,
>
> downstream Bug: https://bugzilla.redhat.com/show_bug.cgi?id=620930
> (gdb) python for i in range(100): a = gdb.execute('info registers', to_string=True)
> ---Type <return> to continue, or q <return> to quit---
>
> When at it I have merged it with making --batch more batch, as the output IMO
> should not depend on the momentarily terminal - wrapping and indenting the
> output on its width.
>
> The patch makes python gdb.execute running in the --batch mode.  I understand
> it may not be universally right but so far I believe it is.  What do you
> think?
>
> (--batch should be IMO somehow merged now with `set interactive-mode'.  That
> is not a part of this patch.)
>
>
> Regards,
> Jan
>
>
> gdb/
> 2010-08-05  Jan Kratochvil  <jan.kratochvil@redhat.com>
>
>        * defs.h (make_cleanup_restore_uinteger)
>        (make_cleanup_restore_page_info): New declarations.
>        * python/python.c: Include main.h.
>        (execute_gdb_command) <to_string>: Temporarily set BATCH_FLAG and call
>        init_page_info.
>        * utils.c (make_cleanup_restore_uinteger)
>        (init_page_info) <batch_flag>
>        (do_restore_page_info_cleanup, make_cleanup_restore_page_info): New.
>
> gdb/testsuite/
> 2010-08-05  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-05  Jan Kratochvil  <jan.kratochvil@redhat.com>
>
>        * gdb.texinfo (Mode Options) <-batch>
>        (Basic Python) <gdb.execute>: Describe setting width and height.

Yikes, nasty problem.

@@ -380,6 +381,13 @@ execute_gdb_command (PyObject *self, PyObject
*args, PyObject *kw)

      if (to_string)
       {
+         /* GDB_STDOUT should be better already restored during these
+            restoration callbacks.  */
+         make_cleanup_restore_page_info ();
+         make_cleanup_restore_integer (&batch_flag);
+         batch_flag = 1;
+         init_page_info ();
+
         str_file = mem_fileopen ();

         make_cleanup_restore_ui_file (&gdb_stdout);

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,
and have just one make_cleanup_restore_foo routine to switch back?

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [patch] Fix python gdb.execute to not paginate
  2010-08-06  0:49 ` Doug Evans
@ 2010-08-06  1:22   ` Doug Evans
  2010-08-06 17:15     ` Tom Tromey
  2010-08-06 10:29   ` Jan Kratochvil
  1 sibling, 1 reply; 11+ messages in thread
From: Doug Evans @ 2010-08-06  1:22 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches, David Malcolm

On Thu, Aug 5, 2010 at 5:49 PM, Doug Evans <dje@google.com> wrote:
> On Thu, Aug 5, 2010 at 2:20 PM, Jan Kratochvil
> <jan.kratochvil@redhat.com> wrote:
>> Hi,
>>
>> downstream Bug: https://bugzilla.redhat.com/show_bug.cgi?id=620930
>> (gdb) python for i in range(100): a = gdb.execute('info registers', to_string=True)
>> ---Type <return> to continue, or q <return> to quit---
>>
>> When at it I have merged it with making --batch more batch, as the output IMO
>> should not depend on the momentarily terminal - wrapping and indenting the
>> output on its width.
>>
>> The patch makes python gdb.execute running in the --batch mode.  I understand
>> it may not be universally right but so far I believe it is.  What do you
>> think?
>>
>> (--batch should be IMO somehow merged now with `set interactive-mode'.  That
>> is not a part of this patch.)
>>
>>
>> Regards,
>> Jan
>>
>>
>> gdb/
>> 2010-08-05  Jan Kratochvil  <jan.kratochvil@redhat.com>
>>
>>        * defs.h (make_cleanup_restore_uinteger)
>>        (make_cleanup_restore_page_info): New declarations.
>>        * python/python.c: Include main.h.
>>        (execute_gdb_command) <to_string>: Temporarily set BATCH_FLAG and call
>>        init_page_info.
>>        * utils.c (make_cleanup_restore_uinteger)
>>        (init_page_info) <batch_flag>
>>        (do_restore_page_info_cleanup, make_cleanup_restore_page_info): New.
>>
>> gdb/testsuite/
>> 2010-08-05  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-05  Jan Kratochvil  <jan.kratochvil@redhat.com>
>>
>>        * gdb.texinfo (Mode Options) <-batch>
>>        (Basic Python) <gdb.execute>: Describe setting width and height.
>
> Yikes, nasty problem.
>
> @@ -380,6 +381,13 @@ execute_gdb_command (PyObject *self, PyObject
> *args, PyObject *kw)
>
>      if (to_string)
>       {
> +         /* GDB_STDOUT should be better already restored during these
> +            restoration callbacks.  */
> +         make_cleanup_restore_page_info ();
> +         make_cleanup_restore_integer (&batch_flag);
> +         batch_flag = 1;
> +         init_page_info ();
> +
>         str_file = mem_fileopen ();
>
>         make_cleanup_restore_ui_file (&gdb_stdout);
>
> 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,
> and have just one make_cleanup_restore_foo routine to switch back?

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.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [patch] Fix python gdb.execute to not paginate
  2010-08-05 21:20 [patch] Fix python gdb.execute to not paginate Jan Kratochvil
  2010-08-06  0:49 ` Doug Evans
@ 2010-08-06  8:35 ` Eli Zaretskii
  1 sibling, 0 replies; 11+ messages in thread
From: Eli Zaretskii @ 2010-08-06  8:35 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches, dmalcolm

> Date: Thu, 5 Aug 2010 23:20:08 +0200
> From: Jan Kratochvil <jan.kratochvil@redhat.com>
> Cc: David Malcolm <dmalcolm@redhat.com>
> 
> gdb/doc/
> 2010-08-05  Jan Kratochvil  <jan.kratochvil@redhat.com>
> 
> 	* gdb.texinfo (Mode Options) <-batch>
> 	(Basic Python) <gdb.execute>: Describe setting width and height.

Thanks.

>  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
                            ^                    ^
The semi-colon should be removed, and a comma added after @pxref's
closing brace.

> +return value is @code{None}.  With @code{True} @var{to_string} the
> +@value{GDBN} virtual terminal has temporarily set unlimited width and height
> +with disabled pagination; @pxref{Screen Size}.

Suggest to rephrase as follows (assuming I got your intent correctly):

                                    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}.

Okay with those changes.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [patch] Fix python gdb.execute to not paginate
  2010-08-06  0:49 ` Doug Evans
  2010-08-06  1:22   ` Doug Evans
@ 2010-08-06 10:29   ` Jan Kratochvil
  2010-08-06 21:37     ` Doug Evans
  1 sibling, 1 reply; 11+ messages in thread
From: Jan Kratochvil @ 2010-08-06 10:29 UTC (permalink / raw)
  To: Doug Evans, Eli Zaretskii; +Cc: gdb-patches, David Malcolm

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [patch] Fix python gdb.execute to not paginate
  2010-08-06  1:22   ` Doug Evans
@ 2010-08-06 17:15     ` Tom Tromey
  2010-08-06 17:27       ` Doug Evans
  0 siblings, 1 reply; 11+ messages in thread
From: Tom Tromey @ 2010-08-06 17:15 UTC (permalink / raw)
  To: Doug Evans; +Cc: Jan Kratochvil, gdb-patches, David Malcolm

>>>>> "Doug" == Doug Evans <dje@google.com> writes:

Doug> Actually, I wonder if it makes sense to bury more implementation
Doug> details.  Make an API of it, running a gdb command and collecting the
Doug> result as a string.

If we're talking about the long term, really the whole pagination
interface is broken.  Pagination should be a per-stream thing, and
functions like wrap_here should have a stream parameter.  Having to
manipulate globals to get the right result here is clearly bogus.

I don't think fixing this should be a requirement for a patch, though.

Tom

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [patch] Fix python gdb.execute to not paginate
  2010-08-06 17:15     ` Tom Tromey
@ 2010-08-06 17:27       ` Doug Evans
  0 siblings, 0 replies; 11+ messages in thread
From: Doug Evans @ 2010-08-06 17:27 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Jan Kratochvil, gdb-patches, David Malcolm

On Fri, Aug 6, 2010 at 10:15 AM, Tom Tromey <tromey@redhat.com> wrote:
>>>>>> "Doug" == Doug Evans <dje@google.com> writes:
>
> Doug> Actually, I wonder if it makes sense to bury more implementation
> Doug> details.  Make an API of it, running a gdb command and collecting the
> Doug> result as a string.
>
> If we're talking about the long term, really the whole pagination
> interface is broken.  Pagination should be a per-stream thing, and
> functions like wrap_here should have a stream parameter.  Having to
> manipulate globals to get the right result here is clearly bogus.

No disagreement there.

> I don't think fixing this should be a requirement for a patch, though.

I'm not sure you're suggesting otherwise, but I'd still like to see
the modicum of changes I've requested implemented.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [patch] Fix python gdb.execute to not paginate
  2010-08-06 10:29   ` Jan Kratochvil
@ 2010-08-06 21:37     ` Doug Evans
  2010-08-07 15:02       ` Jan Kratochvil
  0 siblings, 1 reply; 11+ messages in thread
From: Doug Evans @ 2010-08-06 21:37 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: Eli Zaretskii, gdb-patches, David Malcolm

Thanks.
Comments inline.

On Fri, Aug 6, 2010 at 3:29 AM, Jan Kratochvil
<jan.kratochvil@redhat.com> wrote:
> 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:

Sure.
I wasn't suggesting introducing a new "closure" for this pass (at
least as far has how "closure" is currently used in gdb).
Rather, I was imagining all of these fiddly implementation details:

> +  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);

... being wrapped in functions that hid them.
There's a clear conceptual boundary here.

> 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);
> +

Is this (and below) done to avoid the 80 char limit?
I haven't seen this formatting style used in gdb (having extern on a
line by itself).

dwarf2-frame.h does this:

extern const struct frame_base *
  dwarf2_frame_base_sniffer (struct frame_info *this_frame);

I like it.
I'm not sure what the coding style manual says.

>  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.  */

The coding standards have this in gdbint.texinfo:

"Put a blank line between the block comments preceding function or
variable definitions, and the definition itself."

though I see an example earlier in that section that doesn't have a
blank line (though that example is for which column to put the
function name, not spacing).

At any rate, I like it and I'd like to start enforcing it for function
definitions.

> +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.  */

Ditto.  Add blank line.

> +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
>


Ok with those changes.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [patch] Fix python gdb.execute to not paginate
  2010-08-06 21:37     ` Doug Evans
@ 2010-08-07 15:02       ` Jan Kratochvil
  2010-08-09 17:30         ` Tom Tromey
  0 siblings, 1 reply; 11+ messages in thread
From: Jan Kratochvil @ 2010-08-07 15:02 UTC (permalink / raw)
  To: Doug Evans; +Cc: Eli Zaretskii, gdb-patches, David Malcolm

On Fri, 06 Aug 2010 23:37:36 +0200, Doug Evans wrote:
> I wasn't suggesting introducing a new "closure" for this pass (at
> least as far has how "closure" is currently used in gdb).
> Rather, I was imagining all of these fiddly implementation details:
> 
> On Fri, Aug 6, 2010 at 3:29 AM, Jan Kratochvil <jan.kratochvil@redhat.com> wrote:
> > +  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);
> 
> ... being wrapped in functions that hid them.
> There's a clear conceptual boundary here.

I am not sure if you request new changes or just make comments on the current
patch.  Made there now one more wrapping into a new cleanup function.


> > +extern
> > +  struct cleanup *make_cleanup_restore_ui_file (struct ui_file **variable);
> > +
> 
> Is this (and below) done to avoid the 80 char limit?
> I haven't seen this formatting style used in gdb (having extern on a
> line by itself).
> 
> dwarf2-frame.h does this:
> 
> extern const struct frame_base *
>   dwarf2_frame_base_sniffer (struct frame_info *this_frame);

Thanks, I see I did not search enough to find the current style for this case.


> I'm not sure what the coding style manual says.

If you refer to http://www.gnu.org/prep/standards/standards.html#Formatting it
does not say much.


> > +/* Remember the current value of *VARIABLE and make it restored when the cleanup
> > +   is run.  */
> 
> The coding standards have this in gdbint.texinfo:
> 
> "Put a blank line between the block comments preceding function or
> variable definitions, and the definition itself."
> 
> though I see an example earlier in that section that doesn't have a
> blank line (though that example is for which column to put the
> function name, not spacing).

Changed.  Yes, I prefer the blank line there and I use the blank line in my
GDB patches.  But there was a precedent case there not using the blank line so
I followed more the current coding style.  GDB does not always follow the GNU
coding style.


> Ok with those changes.

Checked-in.


Thanks,
Jan


http://sourceware.org/ml/gdb-cvs/2010-08/msg00031.html

--- src/gdb/ChangeLog	2010/08/06 19:45:58	1.12064
+++ src/gdb/ChangeLog	2010/08/07 15:00:36	1.12065
@@ -1,3 +1,22 @@
+2010-08-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
+
+	* defs.h (make_cleanup_restore_uinteger, make_cleanup_restore_ui_file)
+	(make_cleanup_restore_page_info)
+	(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_integer): New source file blank line.
+	(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, make_cleanup_restore_page_info)
+	(set_batch_flag_and_make_cleanup_restore_page_info): New.
+
 2010-08-06  Maciej W. Rozycki  <macro@codesourcery.com>
 
 	* thread.c (add_thread_silent): Use null_ptid instead of
--- src/gdb/testsuite/ChangeLog	2010/08/02 23:41:18	1.2405
+++ src/gdb/testsuite/ChangeLog	2010/08/07 15:00:38	1.2406
@@ -1,3 +1,10 @@
+2010-08-07  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.
+
 2010-08-02  Doug Evans  <dje@google.com>
 
 	* gdb.cp/namespace.exp: When "print ::cOtherFileClassVar" fails
--- src/gdb/doc/ChangeLog	2010/07/31 15:34:41	1.1093
+++ src/gdb/doc/ChangeLog	2010/08/07 15:00:38	1.1094
@@ -1,3 +1,9 @@
+2010-08-07  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.
+
 2010-07-31  Paul Pluzhnikov  <ppluzhnikov@google.com>
 
 	* gdb.texinfo (Threads): Document 'debug libthread-db'.
--- src/gdb/defs.h	2010/07/19 17:51:23	1.276
+++ src/gdb/defs.h	2010/08/07 15:00:37	1.277
@@ -351,10 +351,14 @@
 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,10 @@
 
 extern void init_page_info (void);
 
+extern struct cleanup *make_cleanup_restore_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 *);
 
--- src/gdb/gdbcmd.h	2010/07/13 20:51:33	1.21
+++ src/gdb/gdbcmd.h	2010/08/07 15:00:37	1.22
@@ -129,6 +129,7 @@
 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 *);
 
--- src/gdb/top.c	2010/07/27 19:11:51	1.182
+++ src/gdb/top.c	2010/08/07 15:00:37	1.183
@@ -458,6 +458,39 @@
     }
 }
 
+/* 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.  */
 
--- src/gdb/utils.c	2010/07/27 19:11:51	1.238
+++ src/gdb/utils.c	2010/08/07 15:00:37	1.239
@@ -339,6 +339,7 @@
 
 /* Remember the current value of *VARIABLE and make it restored when the cleanup
    is run.  */
+
 struct cleanup *
 make_cleanup_restore_integer (int *variable)
 {
@@ -352,6 +353,15 @@
 			   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 +380,34 @@
   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 +2090,12 @@
 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 +2140,44 @@
   set_width ();
 }
 
+/* Helper for make_cleanup_restore_page_info.  */
+
+static void
+do_restore_page_info_cleanup (void *arg)
+{
+  set_screen_size ();
+  set_width ();
+}
+
+/* Provide cleanup for restoring the terminal size.  */
+
+struct cleanup *
+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);
+
+  return back_to;
+}
+
+/* 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 = make_cleanup_restore_page_info ();
+  
+  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
--- src/gdb/doc/gdb.texinfo	2010/07/31 15:34:41	1.747
+++ src/gdb/doc/gdb.texinfo	2010/08/07 15:00:38	1.748
@@ -1031,9 +1031,9 @@
 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 @@
 @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
--- src/gdb/python/python.c	2010/06/28 21:16:03	1.44
+++ src/gdb/python/python.c	2010/08/07 15:00:38	1.45
@@ -309,33 +309,6 @@
   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 +349,15 @@
       /* 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);
--- src/gdb/testsuite/gdb.python/python.exp	2010/06/25 18:15:18	1.9
+++ src/gdb/testsuite/gdb.python/python.exp	2010/08/07 15:00:39	1.10
@@ -87,3 +87,26 @@
 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"

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [patch] Fix python gdb.execute to not paginate
  2010-08-07 15:02       ` Jan Kratochvil
@ 2010-08-09 17:30         ` Tom Tromey
  2010-08-09 19:24           ` Jan Kratochvil
  0 siblings, 1 reply; 11+ messages in thread
From: Tom Tromey @ 2010-08-09 17:30 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: Doug Evans, Eli Zaretskii, gdb-patches, David Malcolm

>>>>> "Jan" == Jan Kratochvil <jan.kratochvil@redhat.com> writes:

Jan> +2010-08-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
Jan> +
Jan> +	* defs.h (make_cleanup_restore_uinteger, make_cleanup_restore_ui_file)
Jan> +	(make_cleanup_restore_page_info)
Jan> +	(set_batch_flag_and_make_cleanup_restore_page_info): New declarations.
Jan> +	* gdbcmd.h (execute_command_to_string): New declaration.
Jan> +	* python/python.c (struct restore_ui_file_closure, restore_ui_file)
Jan> +	(make_cleanup_restore_ui_file): Move to utils.c
Jan> +	(execute_gdb_command) <to_string>: Move ...
Jan> +	* top.c (execute_command_to_string): ... here.  Call
Jan> +	set_batch_flag_and_make_cleanup_restore_page_info.
Jan> +	* utils.c (make_cleanup_restore_integer): New source file blank line.
Jan> +	(make_cleanup_restore_uinteger): New.
Jan> +	(struct restore_ui_file_closure, do_restore_ui_file)
Jan> +	(make_cleanup_restore_ui_file): Move here from python/python.c.
Jan> +	(init_page_info) <batch_flag>
Jan> +	(do_restore_page_info_cleanup, make_cleanup_restore_page_info)
Jan> +	(set_batch_flag_and_make_cleanup_restore_page_info): New.

I think this should go on the 7.2 branch as well.
It fixes a pretty serious bug in a feature that is new to 7.2.

Tom

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [patch] Fix python gdb.execute to not paginate
  2010-08-09 17:30         ` Tom Tromey
@ 2010-08-09 19:24           ` Jan Kratochvil
  0 siblings, 0 replies; 11+ messages in thread
From: Jan Kratochvil @ 2010-08-09 19:24 UTC (permalink / raw)
  To: Tom Tromey
  Cc: Doug Evans, Eli Zaretskii, gdb-patches, David Malcolm, Joel Brobecker

On Mon, 09 Aug 2010 19:30:32 +0200, Tom Tromey wrote:
> I think this should go on the 7.2 branch as well.
> It fixes a pretty serious bug in a feature that is new to 7.2.

Checked-in gdb_7_2-branch.

Minor rediff had to be done.


Thanks,
Jan


http://sourceware.org/ml/gdb-cvs/2010-08/msg00037.html

--- src/gdb/ChangeLog	2010/08/06 19:51:48	1.11973.2.27
+++ src/gdb/ChangeLog	2010/08/09 19:22:57	1.11973.2.28
@@ -1,3 +1,22 @@
+2010-08-09  Jan Kratochvil  <jan.kratochvil@redhat.com>
+
+	* defs.h (make_cleanup_restore_uinteger, make_cleanup_restore_ui_file)
+	(make_cleanup_restore_page_info)
+	(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_integer): New source file blank line.
+	(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, make_cleanup_restore_page_info)
+	(set_batch_flag_and_make_cleanup_restore_page_info): New.
+
 2010-08-06  Maciej W. Rozycki  <macro@codesourcery.com>
 
 	* thread.c (add_thread_silent): Use null_ptid instead of
--- src/gdb/defs.h	2010/06/26 06:44:47	1.275
+++ src/gdb/defs.h	2010/08/09 19:22:58	1.275.2.1
@@ -351,6 +351,10 @@
 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);
+
+extern struct cleanup *
+  make_cleanup_restore_ui_file (struct ui_file **variable);
 
 extern struct cleanup *make_final_cleanup (make_cleanup_ftype *, void *);
 
@@ -383,6 +387,10 @@
 
 extern void init_page_info (void);
 
+extern struct cleanup *make_cleanup_restore_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 *);
 
--- src/gdb/gdbcmd.h	2010/01/01 07:31:32	1.20
+++ src/gdb/gdbcmd.h	2010/08/09 19:22:58	1.20.4.1
@@ -125,6 +125,7 @@
 extern struct cmd_list_element *showchecklist;
 
 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 *);
 
--- src/gdb/top.c	2010/07/27 19:13:11	1.181.2.1
+++ src/gdb/top.c	2010/08/09 19:22:58	1.181.2.2
@@ -458,6 +458,39 @@
     }
 }
 
+/* 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.  */
 
--- src/gdb/utils.c	2010/07/27 19:13:11	1.236.2.1
+++ src/gdb/utils.c	2010/08/09 19:22:58	1.236.2.2
@@ -339,6 +339,7 @@
 
 /* Remember the current value of *VARIABLE and make it restored when the cleanup
    is run.  */
+
 struct cleanup *
 make_cleanup_restore_integer (int *variable)
 {
@@ -352,6 +353,43 @@
 			   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);
+}
+
+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 *))
@@ -2034,6 +2072,12 @@
 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
@@ -2078,6 +2122,44 @@
   set_width ();
 }
 
+/* Helper for make_cleanup_restore_page_info.  */
+
+static void
+do_restore_page_info_cleanup (void *arg)
+{
+  set_screen_size ();
+  set_width ();
+}
+
+/* Provide cleanup for restoring the terminal size.  */
+
+struct cleanup *
+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);
+
+  return back_to;
+}
+
+/* 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 = make_cleanup_restore_page_info ();
+  
+  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
--- src/gdb/doc/ChangeLog	2010/07/30 14:40:59	1.1083.2.3
+++ src/gdb/doc/ChangeLog	2010/08/09 19:22:58	1.1083.2.4
@@ -1,3 +1,9 @@
+2010-08-09  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.
+
 2010-07-30  Hui Zhu  <teawater@gmail.com>
 
 	* gdb.texinfo (Inferiors and Programs): Update the introduce of
--- src/gdb/doc/gdb.texinfo	2010/07/30 14:40:59	1.737.2.3
+++ src/gdb/doc/gdb.texinfo	2010/08/09 19:22:59	1.737.2.4
@@ -1031,9 +1031,9 @@
 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
@@ -20432,7 +20432,9 @@
 @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
--- src/gdb/python/python.c	2010/06/28 21:16:03	1.44
+++ src/gdb/python/python.c	2010/08/09 19:22:59	1.44.2.1
@@ -309,33 +309,6 @@
   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 +349,15 @@
       /* 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);
--- src/gdb/testsuite/ChangeLog	2010/07/27 23:21:07	1.2376.2.8
+++ src/gdb/testsuite/ChangeLog	2010/08/09 19:22:59	1.2376.2.9
@@ -1,3 +1,10 @@
+2010-08-09  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.
+
 2010-07-27  Jan Kratochvil  <jan.kratochvil@redhat.com>
 
 	* gdb.base/help.exp (help disassemble): Update the content.
--- src/gdb/testsuite/gdb.python/python.exp	2010/06/25 18:15:18	1.9
+++ src/gdb/testsuite/gdb.python/python.exp	2010/08/09 19:23:00	1.9.2.1
@@ -87,3 +87,26 @@
 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"

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2010-08-09 19:24 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-05 21:20 [patch] Fix python gdb.execute to not paginate Jan Kratochvil
2010-08-06  0:49 ` Doug Evans
2010-08-06  1:22   ` Doug Evans
2010-08-06 17:15     ` Tom Tromey
2010-08-06 17:27       ` Doug Evans
2010-08-06 10:29   ` Jan Kratochvil
2010-08-06 21:37     ` Doug Evans
2010-08-07 15:02       ` Jan Kratochvil
2010-08-09 17:30         ` Tom Tromey
2010-08-09 19:24           ` Jan Kratochvil
2010-08-06  8:35 ` Eli Zaretskii

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).