public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] gdb/testsuite: rewrite capture_command_output proc
@ 2022-10-02 16:27 Andrew Burgess
  0 siblings, 0 replies; only message in thread
From: Andrew Burgess @ 2022-10-02 16:27 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=e7b1ba07bc9dc28819c9a2523f5e5cdb43e13ca2

commit e7b1ba07bc9dc28819c9a2523f5e5cdb43e13ca2
Author: Andrew Burgess <aburgess@redhat.com>
Date:   Sun Aug 28 20:21:07 2022 +0100

    gdb/testsuite: rewrite capture_command_output proc
    
    I noticed a test failure in gdb.base/completion.exp for RISC-V on a
    native Linux target.  Upon investigation I discovered a couple of
    reasons for the failure, this commit addresses one of them.  A later
    commit will address the other issue.
    
    The completion.exp test makes use of the capture_command_output proc
    to collect the output of the 'maint print registers' command.  For
    RISC-V this command produces a lot of output.
    
    Currently the capture_command_output proc tries to collect the
    complete command output in a single expect buffer, and what I see is
    an error caused by the expect buffer becoming full.
    
    This commit rewrites capture_command_output to make use of
    gdb_test_multiple to collect the command output line at a time, in
    this way we avoid overflowing the expect buffer.
    
    The capture_command_output proc has some logic for skipping a prefix
    pattern, which is passed in to the proc as an argument.  In order to
    handle this correctly (only matching the prefix at the start of the
    command output), I use two gdb_test_multiple calls, the first spots
    and discards the echoed command and the (optional) prefix pattern, the
    second gdb_test_multiple call then collects the rest of the command
    output line at a time until a prompt is seen.
    
    There is one slight oddity with the current implementation, which I
    have changed in my rewrite, this does, slightly, change the behaviour
    of the proc.
    
    The current implementation uses this pattern:
    
      -re "[string_to_regexp ${command}]\[\r\n\]+${prefix}(.*)\[\r\n\]+$gdb_prompt $"
    
    Now a typical command output will look like this:
    
      output here\r\n
      (gdb)
    
    As the TCL regexp matching is greedy, TCL will try to match as much as
    possible in one part of the pattern before moving on to the next.
    Thus, when this matches against (.*)[\r\n]+, the (.*) will end up
    matching against 'output here\r' and the [\r\n]+ will match '\n' only.
    
    In short the previous implementation would leave the '\r' on the end
    of the returned text, but not the final trailing '\n'.
    
    Now clearly I could make a new version of capture_command_output that
    maintained this behaviour, but I couldn't see any reason to do this.
    So, my new implementation drops the final '\r\n' completely, in our
    example above we now return 'output here' with no '\r'.
    
    This change doesn't seem to affect any of the existing tests, but I
    thought it was worth mentioning.

Diff:
---
 gdb/testsuite/lib/gdb.exp | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)

diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 5ab4df1bcf3..432ed5e34ca 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -7857,12 +7857,41 @@ proc capture_command_output { command prefix } {
     global gdb_prompt
     global expect_out
 
+    set code {
+	-re "^[string_to_regexp ${command}]\r\n" {
+	    if { $prefix != "" } {
+		exp_continue
+	    }
+	}
+    }
+
+    if { $prefix != "" } {
+	append code {
+	    -re "^${prefix}" {
+		# Nothing, we just move onto the next gdb_test_multiple
+		# call, which actually collects the command output.
+	    }
+	}
+    }
+
+    gdb_test_multiple "$command" "capture_command_output for $command" $code
+
     set output_string ""
-    gdb_test_multiple "$command" "capture_command_output for $command" {
-	-re "[string_to_regexp ${command}]\[\r\n\]+${prefix}(.*)\[\r\n\]+$gdb_prompt $" {
-	    set output_string $expect_out(1,string)
+    gdb_test_multiple "" "" {
+	-re "^(\[^\r\n\]+\r\n)" {
+	    if { ![string equal $output_string ""] } {
+		set output_string [join [list $output_string $expect_out(1,string)] ""]
+	    } else {
+		set output_string $expect_out(1,string)
+	    }
+	    exp_continue
+	}
+
+	-re "^$gdb_prompt $" {
 	}
     }
+
+    set output_string [regsub "\r\n$" $output_string ""]
     return $output_string
 }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-10-02 16:27 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-02 16:27 [binutils-gdb] gdb/testsuite: rewrite capture_command_output proc Andrew Burgess

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