[1/4] gdb: new 'maint flush source-cache' command

Message ID 96703b6e331430c39078c96bba0fe0edee5c83e5.1641565040.git.aburgess@redhat.com
State New
Headers show
Series
  • Source highlight non utf-8 characters using Python
Related show

Commit Message

Simon Marchi via Gdb-patches Jan. 7, 2022, 2:23 p.m.
This commit adds a new 'maint flush source-cache' command, this
flushes the cache of source file contents.

After flushing GDB is forced to reread source files the next time any
source lines are to be displayed.

I've added a test for this new feature.  The test is a little weird,
in that it modifies a source file after compilation, and makes use of
the cache flush so that the changes show up when listing the source
file.  I'm not sure when such a situation would ever crop up in real
life, but maybe we can imagine such cases.

In reality, this command is useful for testing the syntax highlighting
within GDB, we can adjust the syntax highlighting settings, flush the
cache, and then get the file contents re-highlighted using the new
settings.
---
 gdb/NEWS                                      |  3 ++
 gdb/doc/gdb.texinfo                           |  9 +++++
 gdb/source-cache.c                            | 15 ++++++++
 gdb/testsuite/gdb.base/cached-source-file.exp | 38 +++++++++++++++++++
 4 files changed, 65 insertions(+)

-- 
2.25.4

Comments

Simon Marchi via Gdb-patches Jan. 7, 2022, 2:49 p.m. | #1
> Date: Fri,  7 Jan 2022 14:23:11 +0000

> From: Andrew Burgess via Gdb-patches <gdb-patches@sourceware.org>

> Cc: Andrew Burgess <aburgess@redhat.com>

> 

> This commit adds a new 'maint flush source-cache' command, this

> flushes the cache of source file contents.

> 

> After flushing GDB is forced to reread source files the next time any

> source lines are to be displayed.

> 

> I've added a test for this new feature.  The test is a little weird,

> in that it modifies a source file after compilation, and makes use of

> the cache flush so that the changes show up when listing the source

> file.  I'm not sure when such a situation would ever crop up in real

> life, but maybe we can imagine such cases.

> 

> In reality, this command is useful for testing the syntax highlighting

> within GDB, we can adjust the syntax highlighting settings, flush the

> cache, and then get the file contents re-highlighted using the new

> settings.

> ---

>  gdb/NEWS                                      |  3 ++

>  gdb/doc/gdb.texinfo                           |  9 +++++

>  gdb/source-cache.c                            | 15 ++++++++

>  gdb/testsuite/gdb.base/cached-source-file.exp | 38 +++++++++++++++++++

>  4 files changed, 65 insertions(+)

> 

> diff --git a/gdb/NEWS b/gdb/NEWS

> index c26e15b530a..105fcf74ff4 100644

> --- a/gdb/NEWS

> +++ b/gdb/NEWS

> @@ -64,6 +64,9 @@ set debug threads on|off

>  show debug threads

>    Print additional debug messages about thread creation and deletion.

>  

> +maint flush source-cache

> +  Flush the contents of the source code cache.

> +

>  * Changed commands

>  

>  maint packet

> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo

> index 48873aa34fe..e17149afa2a 100644

> --- a/gdb/doc/gdb.texinfo

> +++ b/gdb/doc/gdb.texinfo

> @@ -39473,6 +39473,15 @@

>  register fetching, or frame unwinding.  The command @code{flushregs}

>  is deprecated in favor of @code{maint flush register-cache}.

>  

> +@kindex maint flush source-cache

> +@cindex source code, caching

> +@item maint flush source-cache

> +Flush @value{GDBN}'s cache of source code file contents.  After

> +@value{GDBN} reads a source file, and optionally applies styling

> +(@pxref{Output Styling}), the file contents are cached.  This command

> +clears that cache.  The next time @value{GDBN} wants to show lines

> +from a source file, the content will be re-read.

> +


The documentation parts are okay, but how about including in the
manual the hint about where this feature is useful, like what you say
in the commit log?
Tom Tromey Jan. 10, 2022, 3:18 p.m. | #2
>>>>> "Andrew" == Andrew Burgess via Gdb-patches <gdb-patches@sourceware.org> writes:


Andrew> +static void
Andrew> +source_cache_flush_command (const char *command, int from_tty)
Andrew> +{
Andrew> +  forget_cached_source_info ();
Andrew> +  if (from_tty)
Andrew> +    printf_filtered (_("Source cache flushed.\n"));

For a maint command in particular, I'd say unconditionally printing is
fine.

This looks good to me.

Tom
Simon Marchi via Gdb-patches Jan. 11, 2022, 12:13 p.m. | #3
* Eli Zaretskii <eliz@gnu.org> [2022-01-07 16:49:50 +0200]:

> > Date: Fri,  7 Jan 2022 14:23:11 +0000

> > From: Andrew Burgess via Gdb-patches <gdb-patches@sourceware.org>

> > Cc: Andrew Burgess <aburgess@redhat.com>

> > 

> > This commit adds a new 'maint flush source-cache' command, this

> > flushes the cache of source file contents.

> > 

> > After flushing GDB is forced to reread source files the next time any

> > source lines are to be displayed.

> > 

> > I've added a test for this new feature.  The test is a little weird,

> > in that it modifies a source file after compilation, and makes use of

> > the cache flush so that the changes show up when listing the source

> > file.  I'm not sure when such a situation would ever crop up in real

> > life, but maybe we can imagine such cases.

> > 

> > In reality, this command is useful for testing the syntax highlighting

> > within GDB, we can adjust the syntax highlighting settings, flush the

> > cache, and then get the file contents re-highlighted using the new

> > settings.

> > ---

> >  gdb/NEWS                                      |  3 ++

> >  gdb/doc/gdb.texinfo                           |  9 +++++

> >  gdb/source-cache.c                            | 15 ++++++++

> >  gdb/testsuite/gdb.base/cached-source-file.exp | 38 +++++++++++++++++++

> >  4 files changed, 65 insertions(+)

> > 

> > diff --git a/gdb/NEWS b/gdb/NEWS

> > index c26e15b530a..105fcf74ff4 100644

> > --- a/gdb/NEWS

> > +++ b/gdb/NEWS

> > @@ -64,6 +64,9 @@ set debug threads on|off

> >  show debug threads

> >    Print additional debug messages about thread creation and deletion.

> >  

> > +maint flush source-cache

> > +  Flush the contents of the source code cache.

> > +

> >  * Changed commands

> >  

> >  maint packet

> > diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo

> > index 48873aa34fe..e17149afa2a 100644

> > --- a/gdb/doc/gdb.texinfo

> > +++ b/gdb/doc/gdb.texinfo

> > @@ -39473,6 +39473,15 @@

> >  register fetching, or frame unwinding.  The command @code{flushregs}

> >  is deprecated in favor of @code{maint flush register-cache}.

> >  

> > +@kindex maint flush source-cache

> > +@cindex source code, caching

> > +@item maint flush source-cache

> > +Flush @value{GDBN}'s cache of source code file contents.  After

> > +@value{GDBN} reads a source file, and optionally applies styling

> > +(@pxref{Output Styling}), the file contents are cached.  This command

> > +clears that cache.  The next time @value{GDBN} wants to show lines

> > +from a source file, the content will be re-read.

> > +

> 

> The documentation parts are okay, but how about including in the

> manual the hint about where this feature is useful, like what you say

> in the commit log?

>


Thanks for the feedback.  An updated patch is below, here's the new
manual entry for 'maint flush source-cache':

  @kindex maint flush source-cache
  @cindex source code, caching
  @item maint flush source-cache
  Flush @value{GDBN}'s cache of source code file contents.  After
  @value{GDBN} reads a source file, and optionally applies styling
  (@pxref{Output Styling}), the file contents are cached.  This command
  clears that cache.  The next time @value{GDBN} wants to show lines
  from a source file, the content will be re-read.

  This command is useful when debugging issues related to source code
  styling.  After flushing the cache any source code displayed by
  @value{GDBN} will be re-read and re-styled.

Is that OK?

Thanks,
Andrew

---

commit e780b58dfcbe12abc7638c5c808fbc43f1176f08
Author: Andrew Burgess <aburgess@redhat.com>
Date:   Fri Nov 26 13:51:36 2021 +0000

    gdb: new 'maint flush source-cache' command
    
    This commit adds a new 'maint flush source-cache' command, this
    flushes the cache of source file contents.
    
    After flushing GDB is forced to reread source files the next time any
    source lines are to be displayed.
    
    I've added a test for this new feature.  The test is a little weird,
    in that it modifies a source file after compilation, and makes use of
    the cache flush so that the changes show up when listing the source
    file.  I'm not sure when such a situation would ever crop up in real
    life, but maybe we can imagine such cases.
    
    In reality, this command is useful for testing the syntax highlighting
    within GDB, we can adjust the syntax highlighting settings, flush the
    cache, and then get the file contents re-highlighted using the new
    settings.

diff --git a/gdb/NEWS b/gdb/NEWS
index c26e15b530a..105fcf74ff4 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -64,6 +64,9 @@ set debug threads on|off
 show debug threads
   Print additional debug messages about thread creation and deletion.
 
+maint flush source-cache
+  Flush the contents of the source code cache.
+
 * Changed commands
 
 maint packet
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 48873aa34fe..222a01023e0 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -39473,6 +39473,19 @@
 register fetching, or frame unwinding.  The command @code{flushregs}
 is deprecated in favor of @code{maint flush register-cache}.
 
+@kindex maint flush source-cache
+@cindex source code, caching
+@item maint flush source-cache
+Flush @value{GDBN}'s cache of source code file contents.  After
+@value{GDBN} reads a source file, and optionally applies styling
+(@pxref{Output Styling}), the file contents are cached.  This command
+clears that cache.  The next time @value{GDBN} wants to show lines
+from a source file, the content will be re-read.
+
+This command is useful when debugging issues related to source code
+styling.  After flushing the cache any source code displayed by
+@value{GDBN} will be re-read and re-styled.
+
 @kindex maint print objfiles
 @cindex info for known object files
 @item maint print objfiles @r{[}@var{regexp}@r{]}
diff --git a/gdb/source-cache.c b/gdb/source-cache.c
index fc789eef8f9..0650768cc0e 100644
--- a/gdb/source-cache.c
+++ b/gdb/source-cache.c
@@ -25,6 +25,7 @@
 #include "gdbsupport/selftest.h"
 #include "objfiles.h"
 #include "exec.h"
+#include "cli/cli-cmds.h"
 
 #ifdef HAVE_SOURCE_HIGHLIGHT
 /* If Gnulib redirects 'open' and 'close' to its replacements
@@ -323,6 +324,15 @@ source_cache::get_source_lines (struct symtab *s, int first_line,
 			first_line, last_line, lines);
 }
 
+/* Implement 'maint flush source-cache' command.  */
+
+static void
+source_cache_flush_command (const char *command, int from_tty)
+{
+  forget_cached_source_info ();
+  printf_filtered (_("Source cache flushed.\n"));
+}
+
 #if GDB_SELF_TEST
 namespace selftests
 {
@@ -346,6 +356,10 @@ void _initialize_source_cache ();
 void
 _initialize_source_cache ()
 {
+  add_cmd ("source-cache", class_maintenance, source_cache_flush_command,
+	   _("Force gdb to flush its source code cache."),
+	   &maintenanceflushlist);
+
 #if GDB_SELF_TEST
   selftests::register_test ("source-cache", selftests::extract_lines_test);
 #endif
diff --git a/gdb/testsuite/gdb.base/cached-source-file.exp b/gdb/testsuite/gdb.base/cached-source-file.exp
index d4e64f32120..75a13378691 100644
--- a/gdb/testsuite/gdb.base/cached-source-file.exp
+++ b/gdb/testsuite/gdb.base/cached-source-file.exp
@@ -100,3 +100,41 @@ gdb_test "run" $re "rerun program" $q y
 # changed for GDB.
 gdb_test "list" "${bp_line}\[ \t\]+printf \\(\"foo\\\\n\"\\); /\\\* new-marker \\\*/.*" \
     "verify that the source code is properly reloaded"
+
+# Modify the source file again.  As before, this only works locally
+# because of the TCL commands.
+set bkpsrc [standard_output_file $testfile].c.bkp
+set bkpsrcfd [open $bkpsrc w]
+set srcfd [open $srcfile r]
+
+while { [gets $srcfd line] != -1 } {
+    if { [string first "new-marker" $line] != -1 } {
+	# Modify the printf line that we added previously.
+	puts $bkpsrcfd "  printf (\"foo\\n\"); /* new-marker updated */"
+    } else {
+	puts $bkpsrcfd $line
+    }
+}
+
+close $bkpsrcfd
+close $srcfd
+file rename -force -- $bkpsrc $srcfile
+
+# As before, delay so that at least one second has passed.  GDB still
+# will not spot that the source file has changed, as GDB doesn't do a
+# time check unless the binary has also changed, this delay just
+# allows us to confirm this behaviour.
+sleep 1
+
+# List the printf line again, we should not see the file changes yet
+# as the binary is unchanged, so the cached contents will still be
+# used.
+gdb_test "list ${bp_line}" "${bp_line}\[ \t\]+printf \\(\"foo\\\\n\"\\); /\\\* new-marker \\\*/.*" \
+    "verify that the source code change is not seen yet"
+
+gdb_test "maint flush source-cache" "Source cache flushed\\."
+
+# List the printf line again.  After the cache flush GDB will re-read
+# the source file and we should now see the changes.
+gdb_test "list ${bp_line}" "${bp_line}\[ \t\]+printf \\(\"foo\\\\n\"\\); /\\\* new-marker updated \\\*/.*" \
+    "verify that the updated source code change is not seen"
Simon Marchi via Gdb-patches Jan. 11, 2022, 1:31 p.m. | #4
> Date: Tue, 11 Jan 2022 12:13:19 +0000

> From: Andrew Burgess <aburgess@redhat.com>

> Cc: gdb-patches@sourceware.org

> 

>   @kindex maint flush source-cache

>   @cindex source code, caching

>   @item maint flush source-cache

>   Flush @value{GDBN}'s cache of source code file contents.  After

>   @value{GDBN} reads a source file, and optionally applies styling

>   (@pxref{Output Styling}), the file contents are cached.  This command

>   clears that cache.  The next time @value{GDBN} wants to show lines

>   from a source file, the content will be re-read.

> 

>   This command is useful when debugging issues related to source code

>   styling.  After flushing the cache any source code displayed by

>   @value{GDBN} will be re-read and re-styled.

> 

> Is that OK?


Yes, thanks.
Simon Marchi via Gdb-patches Jan. 12, 2022, 11:38 a.m. | #5
* Eli Zaretskii <eliz@gnu.org> [2022-01-11 15:31:16 +0200]:

> > Date: Tue, 11 Jan 2022 12:13:19 +0000

> > From: Andrew Burgess <aburgess@redhat.com>

> > Cc: gdb-patches@sourceware.org

> > 

> >   @kindex maint flush source-cache

> >   @cindex source code, caching

> >   @item maint flush source-cache

> >   Flush @value{GDBN}'s cache of source code file contents.  After

> >   @value{GDBN} reads a source file, and optionally applies styling

> >   (@pxref{Output Styling}), the file contents are cached.  This command

> >   clears that cache.  The next time @value{GDBN} wants to show lines

> >   from a source file, the content will be re-read.

> > 

> >   This command is useful when debugging issues related to source code

> >   styling.  After flushing the cache any source code displayed by

> >   @value{GDBN} will be re-read and re-styled.

> > 

> > Is that OK?

> 

> Yes, thanks.

> 


Thanks, I pushed this patch.

Andrew

Patch

diff --git a/gdb/NEWS b/gdb/NEWS
index c26e15b530a..105fcf74ff4 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -64,6 +64,9 @@  set debug threads on|off
 show debug threads
   Print additional debug messages about thread creation and deletion.
 
+maint flush source-cache
+  Flush the contents of the source code cache.
+
 * Changed commands
 
 maint packet
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 48873aa34fe..e17149afa2a 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -39473,6 +39473,15 @@ 
 register fetching, or frame unwinding.  The command @code{flushregs}
 is deprecated in favor of @code{maint flush register-cache}.
 
+@kindex maint flush source-cache
+@cindex source code, caching
+@item maint flush source-cache
+Flush @value{GDBN}'s cache of source code file contents.  After
+@value{GDBN} reads a source file, and optionally applies styling
+(@pxref{Output Styling}), the file contents are cached.  This command
+clears that cache.  The next time @value{GDBN} wants to show lines
+from a source file, the content will be re-read.
+
 @kindex maint print objfiles
 @cindex info for known object files
 @item maint print objfiles @r{[}@var{regexp}@r{]}
diff --git a/gdb/source-cache.c b/gdb/source-cache.c
index fc789eef8f9..733d1d272cd 100644
--- a/gdb/source-cache.c
+++ b/gdb/source-cache.c
@@ -25,6 +25,7 @@ 
 #include "gdbsupport/selftest.h"
 #include "objfiles.h"
 #include "exec.h"
+#include "cli/cli-cmds.h"
 
 #ifdef HAVE_SOURCE_HIGHLIGHT
 /* If Gnulib redirects 'open' and 'close' to its replacements
@@ -323,6 +324,16 @@  source_cache::get_source_lines (struct symtab *s, int first_line,
 			first_line, last_line, lines);
 }
 
+/* Implement 'maint flush source-cache' command.  */
+
+static void
+source_cache_flush_command (const char *command, int from_tty)
+{
+  forget_cached_source_info ();
+  if (from_tty)
+    printf_filtered (_("Source cache flushed.\n"));
+}
+
 #if GDB_SELF_TEST
 namespace selftests
 {
@@ -346,6 +357,10 @@  void _initialize_source_cache ();
 void
 _initialize_source_cache ()
 {
+  add_cmd ("source-cache", class_maintenance, source_cache_flush_command,
+	   _("Force gdb to flush its source code cache."),
+	   &maintenanceflushlist);
+
 #if GDB_SELF_TEST
   selftests::register_test ("source-cache", selftests::extract_lines_test);
 #endif
diff --git a/gdb/testsuite/gdb.base/cached-source-file.exp b/gdb/testsuite/gdb.base/cached-source-file.exp
index d4e64f32120..75a13378691 100644
--- a/gdb/testsuite/gdb.base/cached-source-file.exp
+++ b/gdb/testsuite/gdb.base/cached-source-file.exp
@@ -100,3 +100,41 @@  gdb_test "run" $re "rerun program" $q y
 # changed for GDB.
 gdb_test "list" "${bp_line}\[ \t\]+printf \\(\"foo\\\\n\"\\); /\\\* new-marker \\\*/.*" \
     "verify that the source code is properly reloaded"
+
+# Modify the source file again.  As before, this only works locally
+# because of the TCL commands.
+set bkpsrc [standard_output_file $testfile].c.bkp
+set bkpsrcfd [open $bkpsrc w]
+set srcfd [open $srcfile r]
+
+while { [gets $srcfd line] != -1 } {
+    if { [string first "new-marker" $line] != -1 } {
+	# Modify the printf line that we added previously.
+	puts $bkpsrcfd "  printf (\"foo\\n\"); /* new-marker updated */"
+    } else {
+	puts $bkpsrcfd $line
+    }
+}
+
+close $bkpsrcfd
+close $srcfd
+file rename -force -- $bkpsrc $srcfile
+
+# As before, delay so that at least one second has passed.  GDB still
+# will not spot that the source file has changed, as GDB doesn't do a
+# time check unless the binary has also changed, this delay just
+# allows us to confirm this behaviour.
+sleep 1
+
+# List the printf line again, we should not see the file changes yet
+# as the binary is unchanged, so the cached contents will still be
+# used.
+gdb_test "list ${bp_line}" "${bp_line}\[ \t\]+printf \\(\"foo\\\\n\"\\); /\\\* new-marker \\\*/.*" \
+    "verify that the source code change is not seen yet"
+
+gdb_test "maint flush source-cache" "Source cache flushed\\."
+
+# List the printf line again.  After the cache flush GDB will re-read
+# the source file and we should now see the changes.
+gdb_test "list ${bp_line}" "${bp_line}\[ \t\]+printf \\(\"foo\\\\n\"\\); /\\\* new-marker updated \\\*/.*" \
+    "verify that the updated source code change is not seen"