public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/2] New option to switch off source code display
@ 2021-09-08 21:34 Andrew Burgess
  2021-09-08 21:34 ` [PATCH 1/2] gdb: remove duplicate cmd_list_element declarations Andrew Burgess
  2021-09-08 21:34 ` [PATCH 2/2] gdb: add setting to disable reading source code files Andrew Burgess
  0 siblings, 2 replies; 8+ messages in thread
From: Andrew Burgess @ 2021-09-08 21:34 UTC (permalink / raw)
  To: gdb-patches

See patch #2 for the motivation.  Patch #1 is a small cleanup.

Thanks,
Andrew

---

Andrew Burgess (2):
  gdb: remove duplicate cmd_list_element declarations
  gdb: add setting to disable reading source code files

 gdb/NEWS                               |   9 +++
 gdb/cli/cli-cmds.c                     |   4 +
 gdb/cli/cli-cmds.h                     |  40 ++++++++++
 gdb/doc/gdb.texinfo                    |  26 ++++++
 gdb/gdbcmd.h                           | 105 +------------------------
 gdb/source.c                           |  66 +++++++++++++++-
 gdb/testsuite/gdb.base/source-open.c   |  25 ++++++
 gdb/testsuite/gdb.base/source-open.exp |  45 +++++++++++
 8 files changed, 213 insertions(+), 107 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/source-open.c
 create mode 100644 gdb/testsuite/gdb.base/source-open.exp

-- 
2.25.4


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

* [PATCH 1/2] gdb: remove duplicate cmd_list_element declarations
  2021-09-08 21:34 [PATCH 0/2] New option to switch off source code display Andrew Burgess
@ 2021-09-08 21:34 ` Andrew Burgess
  2021-09-23 20:21   ` Tom Tromey
  2021-09-08 21:34 ` [PATCH 2/2] gdb: add setting to disable reading source code files Andrew Burgess
  1 sibling, 1 reply; 8+ messages in thread
From: Andrew Burgess @ 2021-09-08 21:34 UTC (permalink / raw)
  To: gdb-patches

For some reason we have two locations where cmd_list_elements are
declared, cli/cli-cmds.h and gdbcmd.h.  Worse still there is
duplication between these two locations.

In this commit I have moved all of the cmd_list_element declarations
from gdbcmd.h into cli/cli-cmds.h and removed the duplicates.

There should be no user visible changes after this commit.
---
 gdb/cli/cli-cmds.h |  32 ++++++++++++++
 gdb/gdbcmd.h       | 105 +--------------------------------------------
 2 files changed, 33 insertions(+), 104 deletions(-)

diff --git a/gdb/cli/cli-cmds.h b/gdb/cli/cli-cmds.h
index b57645cdc8c..ce2e38886f1 100644
--- a/gdb/cli/cli-cmds.h
+++ b/gdb/cli/cli-cmds.h
@@ -93,10 +93,38 @@ extern struct cmd_list_element *maintenanceprintlist;
 
 extern struct cmd_list_element *maintenanceflushlist;
 
+/* Chain containing all defined "maintenance check" subcommands.  */
+
+extern struct cmd_list_element *maintenancechecklist;
+
+/* Chain containing all defined "maintenance set" subcommands.  */
+
+extern struct cmd_list_element *maintenance_set_cmdlist;
+
+/* Chain containing all defined "maintenance show" subcommands.  */
+
+extern struct cmd_list_element *maintenance_show_cmdlist;
+
 extern struct cmd_list_element *setprintlist;
 
 extern struct cmd_list_element *showprintlist;
 
+/* Chain containing all defined "set print raw" subcommands.  */
+
+extern struct cmd_list_element *setprintrawlist;
+
+/* Chain containing all defined "show print raw" subcommands.  */
+
+extern struct cmd_list_element *showprintrawlist;
+
+/* Chain containing all defined "set print type" subcommands.  */
+
+extern struct cmd_list_element *setprinttypelist;
+
+/* Chain containing all defined "show print type" subcommands.  */
+
+extern struct cmd_list_element *showprinttypelist;
+
 extern struct cmd_list_element *setdebuglist;
 
 extern struct cmd_list_element *showdebuglist;
@@ -105,6 +133,10 @@ extern struct cmd_list_element *setchecklist;
 
 extern struct cmd_list_element *showchecklist;
 
+/* Chain containing all defined "save" subcommands.  */
+
+extern struct cmd_list_element *save_cmdlist;
+
 /* Limit the call depth of user-defined commands */
 
 extern unsigned int max_user_call_depth;
diff --git a/gdb/gdbcmd.h b/gdb/gdbcmd.h
index f541ebd92d2..f18b23d1234 100644
--- a/gdb/gdbcmd.h
+++ b/gdb/gdbcmd.h
@@ -27,110 +27,7 @@
 #include "command.h"
 #include "ui-out.h"
 #include "cli/cli-script.h"
-
-/* Chain containing all defined commands.  */
-
-extern struct cmd_list_element *cmdlist;
-
-/* Chain containing all defined info subcommands.  */
-
-extern struct cmd_list_element *infolist;
-
-/* Chain containing all defined enable subcommands.  */
-
-extern struct cmd_list_element *enablelist;
-
-/* Chain containing all defined disable subcommands.  */
-
-extern struct cmd_list_element *disablelist;
-
-/* Chain containing all defined delete subcommands.  */
-
-extern struct cmd_list_element *deletelist;
-
-/* Chain containing all defined detach subcommands.  */
-
-extern struct cmd_list_element *detachlist;
-
-/* Chain containing all defined kill subcommands.  */
-
-extern struct cmd_list_element *killlist;
-
-/* Chain containing all defined stop subcommands.  */
-
-extern struct cmd_list_element *stoplist;
-
-/* Chain containing all defined set subcommands.  */
-
-extern struct cmd_list_element *setlist;
-
-/* Chain containing all defined unset subcommands.  */
-
-extern struct cmd_list_element *unsetlist;
-
-/* Chain containing all defined show subcommands.  */
-
-extern struct cmd_list_element *showlist;
-
-/* Chain containing all defined \"set history\".  */
-
-extern struct cmd_list_element *sethistlist;
-
-/* Chain containing all defined \"show history\".  */
-
-extern struct cmd_list_element *showhistlist;
-
-/* Chain containing all defined \"unset history\".  */
-
-extern struct cmd_list_element *unsethistlist;
-
-/* Chain containing all defined maintenance subcommands.  */
-
-extern struct cmd_list_element *maintenancelist;
-
-/* Chain containing all defined "maintenance info" subcommands.  */
-
-extern struct cmd_list_element *maintenanceinfolist;
-
-/* Chain containing all defined "maintenance print" subcommands.  */
-
-extern struct cmd_list_element *maintenanceprintlist;
-
-/* Chain containing all defined "maintenance check" subcommands.  */
-
-extern struct cmd_list_element *maintenancechecklist;
-
-/* Chain containing all defined "maintenance set" subcommands.  */
-
-extern struct cmd_list_element *maintenance_set_cmdlist;
-
-/* Chain containing all defined "maintenance show" subcommands.  */
-
-extern struct cmd_list_element *maintenance_show_cmdlist;
-
-extern struct cmd_list_element *setprintlist;
-
-extern struct cmd_list_element *showprintlist;
-
-extern struct cmd_list_element *setprintrawlist;
-
-extern struct cmd_list_element *showprintrawlist;
-
-extern struct cmd_list_element *setprinttypelist;
-
-extern struct cmd_list_element *showprinttypelist;
-
-extern struct cmd_list_element *setdebuglist;
-
-extern struct cmd_list_element *showdebuglist;
-
-extern struct cmd_list_element *setchecklist;
-
-extern struct cmd_list_element *showchecklist;
-
-/* Chain containing all defined "save" subcommands.  */
-
-extern struct cmd_list_element *save_cmdlist;
+#include "cli/cli-cmds.h"
 
 extern void execute_command (const char *, int);
 
-- 
2.25.4


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

* [PATCH 2/2] gdb: add setting to disable reading source code files
  2021-09-08 21:34 [PATCH 0/2] New option to switch off source code display Andrew Burgess
  2021-09-08 21:34 ` [PATCH 1/2] gdb: remove duplicate cmd_list_element declarations Andrew Burgess
@ 2021-09-08 21:34 ` Andrew Burgess
  2021-09-09 18:51   ` Philippe Waroquiers
                     ` (3 more replies)
  1 sibling, 4 replies; 8+ messages in thread
From: Andrew Burgess @ 2021-09-08 21:34 UTC (permalink / raw)
  To: gdb-patches

In some situations it is possible that a user might not want GDB to
try and access source code files, for example, the source code might
be stored on a slow to access network file system.

It is almost certainly possible that using some combination of 'set
directories' and/or 'set substitute-path' a user can trick GDB into
being unable to find the source files, but this feels like a rather
crude way to solve the problem.

In this commit a new option is add that stops GDB from opening and
reading the source files.  A user can run with source code reading
disabled if this is required, then re-enable later if they decide
that they now want to view the source code.
---
 gdb/NEWS                               |  9 ++++
 gdb/cli/cli-cmds.c                     |  4 ++
 gdb/cli/cli-cmds.h                     |  8 ++++
 gdb/doc/gdb.texinfo                    | 26 ++++++++++
 gdb/source.c                           | 66 ++++++++++++++++++++++++--
 gdb/testsuite/gdb.base/source-open.c   | 25 ++++++++++
 gdb/testsuite/gdb.base/source-open.exp | 45 ++++++++++++++++++
 7 files changed, 180 insertions(+), 3 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/source-open.c
 create mode 100644 gdb/testsuite/gdb.base/source-open.exp

diff --git a/gdb/NEWS b/gdb/NEWS
index f9485520438..cb4c9ccf902 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,8 @@
 
 *** Changes since GDB 11
 
+* New commands
+
 maint set backtrace-on-fatal-signal on|off
 maint show backtrace-on-fatal-signal
   This setting is 'on' by default.  When 'on' GDB will print a limited
@@ -10,6 +12,13 @@ maint show backtrace-on-fatal-signal
   fatal signal.  This only supported on some platforms where the
   backtrace and backtrace_symbols_fd functions are available.
 
+set source open on|off
+show source open
+  This command, which is on by default, controls whether GDB will try
+  to open source code files.  Switching this off will stop GDB trying
+  to open and read soure code files, which can be useful if the files
+  are located over a slow network connection.
+
 * Python API
 
   ** New function gdb.add_history(), which takes a gdb.Value object
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 5ff0b77eb68..e5c7152ba7a 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -168,6 +168,10 @@ struct cmd_list_element *setchecklist;
 
 struct cmd_list_element *showchecklist;
 
+struct cmd_list_element *setsourcelist;
+
+struct cmd_list_element *showsourcelist;
+
 /* Command tracing state.  */
 
 int source_verbose = 0;
diff --git a/gdb/cli/cli-cmds.h b/gdb/cli/cli-cmds.h
index ce2e38886f1..a4b4168896e 100644
--- a/gdb/cli/cli-cmds.h
+++ b/gdb/cli/cli-cmds.h
@@ -137,6 +137,14 @@ extern struct cmd_list_element *showchecklist;
 
 extern struct cmd_list_element *save_cmdlist;
 
+/* Chain containing all defined "set source" subcommands.  */
+
+extern struct cmd_list_element *setsourcelist;
+
+/* Chain containing all defined "show source" subcommands.  */
+
+extern struct cmd_list_element *showsourcelist;
+
 /* Limit the call depth of user-defined commands */
 
 extern unsigned int max_user_call_depth;
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 58479ef3ed6..8bf711033d6 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -8919,6 +8919,7 @@
 * Search::                      Searching source files
 * Source Path::                 Specifying source directories
 * Machine Code::                Source and machine code
+* Disable Reading Source::	Disable Reading Source Code
 @end menu
 
 @node List
@@ -9926,6 +9927,31 @@
 instruction.
 @end table
 
+@node Disable Reading Source
+@section Disable Reading Source Code
+@cindex source code, disable access
+
+In some cases it can be desirable to prevent @value{GDBN} from
+accessing source code files.  One case where this might be desirable
+is if the source code files are located over a slow network
+connection.
+
+The following command can be used to control whether @value{GDBN}
+should access source code files or not:
+
+@table @code
+@kindex set source open
+@kindex show source open
+@item set source open @r{[}on@r{|}off@r{]}
+@itemx show source open
+When this option is @code{on}, which is the default, @value{GDBN} will
+access source code files when needed, for example to print source
+lines when @value{GDBN} stops, or in response to the @code{list}
+command.
+
+When this option is @code{off}, @value{GDBN} will not access source
+code files.
+@end table
 
 @node Data
 @chapter Examining Data
diff --git a/gdb/source.c b/gdb/source.c
index 7d1934bd6c9..e5a1524cedf 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -148,7 +148,21 @@ show_filename_display_string (struct ui_file *file, int from_tty,
 {
   fprintf_filtered (file, _("Filenames are displayed as \"%s\".\n"), value);
 }
- 
+
+/* When true GDB will stat and open source files at as required, but when
+   false, GDB will avoid accessing source files as much as possible.  */
+
+static bool source_open = true;
+
+/* Implement 'show source open'.  */
+
+static void
+show_source_open (struct ui_file *file, int from_tty,
+		  struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Source opening is \"%s\".\n"), value);
+}
+
 /* Line number of last line printed.  Default for various commands.
    current_source_line is usually, but not always, the same as this.  */
 
@@ -1048,8 +1062,13 @@ find_and_open_source (const char *filename,
   const char *p;
   int result;
 
-  /* Quick way out if we already know its full name.  */
+  /* If reading of source files is disabled then return a result indicating
+     the attempt to read this source file failed, GDB will then display
+     filename and line number instead.  */
+  if (!source_open)
+    return scoped_fd(-1);
 
+  /* Quick way out if we already know its full name.  */
   if (*fullname)
     {
       /* The user may have requested that source paths be rewritten
@@ -1062,6 +1081,7 @@ find_and_open_source (const char *filename,
 	*fullname = std::move (rewritten_fullname);
 
       result = gdb_open_cloexec (fullname->get (), OPEN_MODE, 0);
+
       if (result >= 0)
 	{
 	  *fullname = gdb_realpath (fullname->get ());
@@ -1300,7 +1320,7 @@ print_source_lines_base (struct symtab *s, int line, int stopline,
 
   /* If printing of source lines is disabled, just print file and line
      number.  */
-  if (uiout->test_flags (ui_source_list))
+  if (uiout->test_flags (ui_source_list) && source_open)
     {
       /* Only prints "No such file or directory" once.  */
       if (s == last_source_visited)
@@ -1603,6 +1623,9 @@ search_command_helper (const char *regex, int from_tty, bool forward)
   if (loc->symtab () == nullptr)
     select_source_symtab (0);
 
+  if (!source_open)
+    error (_("source code access disabled"));
+
   scoped_fd desc (open_source_file (loc->symtab ()));
   if (desc.get () < 0)
     perror_with_name (symtab_to_filename_for_display (loc->symtab ()));
@@ -1927,6 +1950,22 @@ source_lines_range::source_lines_range (int startline,
     }
 }
 
+/* Handle the "set source" base command.  */
+
+static void
+set_source (const char *arg, int from_tty)
+{
+  help_list (setsourcelist, "set source ", all_commands, gdb_stdout);
+}
+
+/* Handle the "show source" base command.  */
+
+static void
+show_source (const char *args, int from_tty)
+{
+  help_list (showsourcelist, "show source ", all_commands, gdb_stdout);
+}
+
 \f
 void _initialize_source ();
 void
@@ -2050,4 +2089,25 @@ By default, relative filenames are displayed."),
 			show_filename_display_string,
 			&setlist, &showlist);
 
+  add_prefix_cmd ("source", no_class, set_source,
+                  _("Generic command for setting how sources are handled."),
+                  &setsourcelist, 0, &setlist);
+
+  add_prefix_cmd ("source", no_class, show_source,
+                  _("Generic command for showing source settings."),
+                  &showsourcelist, 0, &showlist);
+
+  add_setshow_boolean_cmd ("open", class_files, &source_open, _("\
+Set whether GDB should open source files."), _("\
+Show whether GDB should open source files."), _("\
+When this option is on GDB will open source files and display the\n\
+contents when appropriate, for example, when GDB stops, or the list\n\
+command is used.\n\
+When this option is off GDB will not try to open source files, instead\n\
+GDB will print the file and line number that would have been displayed.\n\
+This can be useful if access to source code files is slow, for example\n\
+due to the source being located over a slow network connection."),
+                           NULL,
+                           show_source_open,
+                           &setsourcelist, &showsourcelist);
 }
diff --git a/gdb/testsuite/gdb.base/source-open.c b/gdb/testsuite/gdb.base/source-open.c
new file mode 100644
index 00000000000..1fb5edb9e75
--- /dev/null
+++ b/gdb/testsuite/gdb.base/source-open.c
@@ -0,0 +1,25 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2021 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <stdio.h>
+
+int
+main (int argc, char* argv[])
+{
+  printf ("source code line.");
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/source-open.exp b/gdb/testsuite/gdb.base/source-open.exp
new file mode 100644
index 00000000000..bae179bb47d
--- /dev/null
+++ b/gdb/testsuite/gdb.base/source-open.exp
@@ -0,0 +1,45 @@
+# Copyright 2021 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test the 'set source open on|off' command.
+
+standard_testfile
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
+    return -1
+}
+
+gdb_start
+gdb_test "show source open" "Source opening is \"on\"\\." \
+    "source opening is initially on"
+gdb_test_no_output "set listsize 1"
+gdb_test_no_output "set source open on"
+gdb_test "list 18" "18\t#include <stdio.h>" "source reading on"
+
+# File opening is turned off therefore source lines are not printed.
+gdb_test_no_output "set source open off"
+gdb_test "show source open" "Source opening is \"off\"\\." \
+    "source opening is now off"
+gdb_test "list 18" "18\tin .*/${srcfile}" "source reading off"
+
+gdb_test "forward-search main" "source code access disabled"
+gdb_test "reverse-search main" "source code access disabled"
+
+# And finally, turn source code reading back on.
+gdb_test_no_output "set source open on" \
+    "turn source reading back on"
+gdb_test "show source open" "Source opening is \"on\"\\." \
+    "source opening is on again"
+gdb_test "list 18" "18\t#include <stdio.h>" "source reading on again"
-- 
2.25.4


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

* Re: [PATCH 2/2] gdb: add setting to disable reading source code files
  2021-09-08 21:34 ` [PATCH 2/2] gdb: add setting to disable reading source code files Andrew Burgess
@ 2021-09-09 18:51   ` Philippe Waroquiers
  2021-09-23 20:27   ` Tom Tromey
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Philippe Waroquiers @ 2021-09-09 18:51 UTC (permalink / raw)
  To: Andrew Burgess, gdb-patches

On Wed, 2021-09-08 at 22:34 +0100, Andrew Burgess wrote:
> +set source open on|off
> +show source open
> +  This command, which is on by default, controls whether GDB will try
This command  -> This setting ?

Thanks
Philippe



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

* Re: [PATCH 1/2] gdb: remove duplicate cmd_list_element declarations
  2021-09-08 21:34 ` [PATCH 1/2] gdb: remove duplicate cmd_list_element declarations Andrew Burgess
@ 2021-09-23 20:21   ` Tom Tromey
  0 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2021-09-23 20:21 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

>>>>> "Andrew" == Andrew Burgess <andrew.burgess@embecosm.com> writes:

Andrew> For some reason we have two locations where cmd_list_elements are
Andrew> declared, cli/cli-cmds.h and gdbcmd.h.  Worse still there is
Andrew> duplication between these two locations.

Andrew> In this commit I have moved all of the cmd_list_element declarations
Andrew> from gdbcmd.h into cli/cli-cmds.h and removed the duplicates.

Thanks for cleaning this up.
It looks good to me.

Tom

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

* Re: [PATCH 2/2] gdb: add setting to disable reading source code files
  2021-09-08 21:34 ` [PATCH 2/2] gdb: add setting to disable reading source code files Andrew Burgess
  2021-09-09 18:51   ` Philippe Waroquiers
@ 2021-09-23 20:27   ` Tom Tromey
  2021-09-24 16:14   ` Pedro Alves
  2021-09-27 10:33   ` Andrew Burgess
  3 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2021-09-23 20:27 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

>>>>> "Andrew" == Andrew Burgess <andrew.burgess@embecosm.com> writes:

Andrew> In some situations it is possible that a user might not want GDB to
Andrew> try and access source code files, for example, the source code might
Andrew> be stored on a slow to access network file system.
[...]

Looks good to me.

Tom

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

* Re: [PATCH 2/2] gdb: add setting to disable reading source code files
  2021-09-08 21:34 ` [PATCH 2/2] gdb: add setting to disable reading source code files Andrew Burgess
  2021-09-09 18:51   ` Philippe Waroquiers
  2021-09-23 20:27   ` Tom Tromey
@ 2021-09-24 16:14   ` Pedro Alves
  2021-09-27 10:33   ` Andrew Burgess
  3 siblings, 0 replies; 8+ messages in thread
From: Pedro Alves @ 2021-09-24 16:14 UTC (permalink / raw)
  To: Andrew Burgess, gdb-patches

Hi Andrew,

Just some typos/nits.

On 2021-09-08 10:34 p.m., Andrew Burgess wrote:

> 
> diff --git a/gdb/NEWS b/gdb/NEWS
> index f9485520438..cb4c9ccf902 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -3,6 +3,8 @@
>  
>  *** Changes since GDB 11
>  
> +* New commands
> +
>  maint set backtrace-on-fatal-signal on|off
>  maint show backtrace-on-fatal-signal
>    This setting is 'on' by default.  When 'on' GDB will print a limited
> @@ -10,6 +12,13 @@ maint show backtrace-on-fatal-signal
>    fatal signal.  This only supported on some platforms where the
>    backtrace and backtrace_symbols_fd functions are available.
>  
> +set source open on|off
> +show source open
> +  This command, which is on by default, controls whether GDB will try
> +  to open source code files.  Switching this off will stop GDB trying
> +  to open and read soure code files, which can be useful if the files

Typo: "soure"

> +  are located over a slow network connection.
> +
>  * Python API
>  


> +When this option is @code{off}, @value{GDBN} will not access source
> +code files.
> +@end table
>  
>  @node Data
>  @chapter Examining Data
> diff --git a/gdb/source.c b/gdb/source.c
> index 7d1934bd6c9..e5a1524cedf 100644
> --- a/gdb/source.c
> +++ b/gdb/source.c
> @@ -148,7 +148,21 @@ show_filename_display_string (struct ui_file *file, int from_tty,
>  {
>    fprintf_filtered (file, _("Filenames are displayed as \"%s\".\n"), value);
>  }
> - 
> +
> +/* When true GDB will stat and open source files at as required, but when

The "at" in "open source files at as" seems spurious.

> +   false, GDB will avoid accessing source files as much as possible.  */
> +
> +static bool source_open = true;
> +
> +/* Implement 'show source open'.  */
> +
> +static void
> +show_source_open (struct ui_file *file, int from_tty,
> +		  struct cmd_list_element *c, const char *value)
> +{
> +  fprintf_filtered (file, _("Source opening is \"%s\".\n"), value);
> +}
> +
>  /* Line number of last line printed.  Default for various commands.
>     current_source_line is usually, but not always, the same as this.  */
>  
> @@ -1048,8 +1062,13 @@ find_and_open_source (const char *filename,
>    const char *p;
>    int result;
>  
> -  /* Quick way out if we already know its full name.  */
> +  /* If reading of source files is disabled then return a result indicating
> +     the attempt to read this source file failed, GDB will then display

I think there should be a period between "failed, GDB".

> +     filename and line number instead.  */
> +  if (!source_open)
> +    return scoped_fd(-1);

Missing space before parens.

> +}
> diff --git a/gdb/testsuite/gdb.base/source-open.exp b/gdb/testsuite/gdb.base/source-open.exp
> new file mode 100644
> index 00000000000..bae179bb47d
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/source-open.exp
> @@ -0,0 +1,45 @@
> +# Copyright 2021 Free Software Foundation, Inc.
> +
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
> +
> +# Test the 'set source open on|off' command.
> +
> +standard_testfile
> +
> +if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
> +    return -1
> +}
> +
> +gdb_start

Either gdb_start is not needed, or use build_executable instead of prepare_for_testing.

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

* Re: [PATCH 2/2] gdb: add setting to disable reading source code files
  2021-09-08 21:34 ` [PATCH 2/2] gdb: add setting to disable reading source code files Andrew Burgess
                     ` (2 preceding siblings ...)
  2021-09-24 16:14   ` Pedro Alves
@ 2021-09-27 10:33   ` Andrew Burgess
  3 siblings, 0 replies; 8+ messages in thread
From: Andrew Burgess @ 2021-09-27 10:33 UTC (permalink / raw)
  To: gdb-patches

Thank you everyone for the feedback.  I incorporated all the proposed
fixes and pushed the patch below.

Thanks,
Andrew

---

commit fde1a9a3ee8c95bb7bae6c764755bcbffdbb0308
Author: Andrew Burgess <andrew.burgess@embecosm.com>
Date:   Thu Apr 26 16:21:34 2018 +0100

    gdb: add setting to disable reading source code files
    
    In some situations it is possible that a user might not want GDB to
    try and access source code files, for example, the source code might
    be stored on a slow to access network file system.
    
    It is almost certainly possible that using some combination of 'set
    directories' and/or 'set substitute-path' a user can trick GDB into
    being unable to find the source files, but this feels like a rather
    crude way to solve the problem.
    
    In this commit a new option is add that stops GDB from opening and
    reading the source files.  A user can run with source code reading
    disabled if this is required, then re-enable later if they decide
    that they now want to view the source code.

diff --git a/gdb/NEWS b/gdb/NEWS
index f9485520438..d7c29c82edb 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,8 @@
 
 *** Changes since GDB 11
 
+* New commands
+
 maint set backtrace-on-fatal-signal on|off
 maint show backtrace-on-fatal-signal
   This setting is 'on' by default.  When 'on' GDB will print a limited
@@ -10,6 +12,13 @@ maint show backtrace-on-fatal-signal
   fatal signal.  This only supported on some platforms where the
   backtrace and backtrace_symbols_fd functions are available.
 
+set source open on|off
+show source open
+  This setting, which is on by default, controls whether GDB will try
+  to open source code files.  Switching this off will stop GDB trying
+  to open and read source code files, which can be useful if the files
+  are located over a slow network connection.
+
 * Python API
 
   ** New function gdb.add_history(), which takes a gdb.Value object
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 5ff0b77eb68..e5c7152ba7a 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -168,6 +168,10 @@ struct cmd_list_element *setchecklist;
 
 struct cmd_list_element *showchecklist;
 
+struct cmd_list_element *setsourcelist;
+
+struct cmd_list_element *showsourcelist;
+
 /* Command tracing state.  */
 
 int source_verbose = 0;
diff --git a/gdb/cli/cli-cmds.h b/gdb/cli/cli-cmds.h
index ce2e38886f1..a4b4168896e 100644
--- a/gdb/cli/cli-cmds.h
+++ b/gdb/cli/cli-cmds.h
@@ -137,6 +137,14 @@ extern struct cmd_list_element *showchecklist;
 
 extern struct cmd_list_element *save_cmdlist;
 
+/* Chain containing all defined "set source" subcommands.  */
+
+extern struct cmd_list_element *setsourcelist;
+
+/* Chain containing all defined "show source" subcommands.  */
+
+extern struct cmd_list_element *showsourcelist;
+
 /* Limit the call depth of user-defined commands */
 
 extern unsigned int max_user_call_depth;
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index bf731a1feb5..fcfdc26ac1a 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -8919,6 +8919,7 @@
 * Search::                      Searching source files
 * Source Path::                 Specifying source directories
 * Machine Code::                Source and machine code
+* Disable Reading Source::	Disable Reading Source Code
 @end menu
 
 @node List
@@ -9926,6 +9927,31 @@
 instruction.
 @end table
 
+@node Disable Reading Source
+@section Disable Reading Source Code
+@cindex source code, disable access
+
+In some cases it can be desirable to prevent @value{GDBN} from
+accessing source code files.  One case where this might be desirable
+is if the source code files are located over a slow network
+connection.
+
+The following command can be used to control whether @value{GDBN}
+should access source code files or not:
+
+@table @code
+@kindex set source open
+@kindex show source open
+@item set source open @r{[}on@r{|}off@r{]}
+@itemx show source open
+When this option is @code{on}, which is the default, @value{GDBN} will
+access source code files when needed, for example to print source
+lines when @value{GDBN} stops, or in response to the @code{list}
+command.
+
+When this option is @code{off}, @value{GDBN} will not access source
+code files.
+@end table
 
 @node Data
 @chapter Examining Data
diff --git a/gdb/source.c b/gdb/source.c
index 7d1934bd6c9..eec6e77eaa3 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -148,7 +148,21 @@ show_filename_display_string (struct ui_file *file, int from_tty,
 {
   fprintf_filtered (file, _("Filenames are displayed as \"%s\".\n"), value);
 }
- 
+
+/* When true GDB will stat and open source files as required, but when
+   false, GDB will avoid accessing source files as much as possible.  */
+
+static bool source_open = true;
+
+/* Implement 'show source open'.  */
+
+static void
+show_source_open (struct ui_file *file, int from_tty,
+		  struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Source opening is \"%s\".\n"), value);
+}
+
 /* Line number of last line printed.  Default for various commands.
    current_source_line is usually, but not always, the same as this.  */
 
@@ -1048,8 +1062,13 @@ find_and_open_source (const char *filename,
   const char *p;
   int result;
 
-  /* Quick way out if we already know its full name.  */
+  /* If reading of source files is disabled then return a result indicating
+     the attempt to read this source file failed.  GDB will then display
+     the filename and line number instead.  */
+  if (!source_open)
+    return scoped_fd (-1);
 
+  /* Quick way out if we already know its full name.  */
   if (*fullname)
     {
       /* The user may have requested that source paths be rewritten
@@ -1062,6 +1081,7 @@ find_and_open_source (const char *filename,
 	*fullname = std::move (rewritten_fullname);
 
       result = gdb_open_cloexec (fullname->get (), OPEN_MODE, 0);
+
       if (result >= 0)
 	{
 	  *fullname = gdb_realpath (fullname->get ());
@@ -1300,7 +1320,7 @@ print_source_lines_base (struct symtab *s, int line, int stopline,
 
   /* If printing of source lines is disabled, just print file and line
      number.  */
-  if (uiout->test_flags (ui_source_list))
+  if (uiout->test_flags (ui_source_list) && source_open)
     {
       /* Only prints "No such file or directory" once.  */
       if (s == last_source_visited)
@@ -1603,6 +1623,9 @@ search_command_helper (const char *regex, int from_tty, bool forward)
   if (loc->symtab () == nullptr)
     select_source_symtab (0);
 
+  if (!source_open)
+    error (_("source code access disabled"));
+
   scoped_fd desc (open_source_file (loc->symtab ()));
   if (desc.get () < 0)
     perror_with_name (symtab_to_filename_for_display (loc->symtab ()));
@@ -1927,6 +1950,22 @@ source_lines_range::source_lines_range (int startline,
     }
 }
 
+/* Handle the "set source" base command.  */
+
+static void
+set_source (const char *arg, int from_tty)
+{
+  help_list (setsourcelist, "set source ", all_commands, gdb_stdout);
+}
+
+/* Handle the "show source" base command.  */
+
+static void
+show_source (const char *args, int from_tty)
+{
+  help_list (showsourcelist, "show source ", all_commands, gdb_stdout);
+}
+
 \f
 void _initialize_source ();
 void
@@ -2050,4 +2089,25 @@ By default, relative filenames are displayed."),
 			show_filename_display_string,
 			&setlist, &showlist);
 
+  add_prefix_cmd ("source", no_class, set_source,
+                  _("Generic command for setting how sources are handled."),
+                  &setsourcelist, 0, &setlist);
+
+  add_prefix_cmd ("source", no_class, show_source,
+                  _("Generic command for showing source settings."),
+                  &showsourcelist, 0, &showlist);
+
+  add_setshow_boolean_cmd ("open", class_files, &source_open, _("\
+Set whether GDB should open source files."), _("\
+Show whether GDB should open source files."), _("\
+When this option is on GDB will open source files and display the\n\
+contents when appropriate, for example, when GDB stops, or the list\n\
+command is used.\n\
+When this option is off GDB will not try to open source files, instead\n\
+GDB will print the file and line number that would have been displayed.\n\
+This can be useful if access to source code files is slow, for example\n\
+due to the source being located over a slow network connection."),
+                           NULL,
+                           show_source_open,
+                           &setsourcelist, &showsourcelist);
 }
diff --git a/gdb/testsuite/gdb.base/source-open.c b/gdb/testsuite/gdb.base/source-open.c
new file mode 100644
index 00000000000..1fb5edb9e75
--- /dev/null
+++ b/gdb/testsuite/gdb.base/source-open.c
@@ -0,0 +1,25 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2021 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <stdio.h>
+
+int
+main (int argc, char* argv[])
+{
+  printf ("source code line.");
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/source-open.exp b/gdb/testsuite/gdb.base/source-open.exp
new file mode 100644
index 00000000000..eb801427bf3
--- /dev/null
+++ b/gdb/testsuite/gdb.base/source-open.exp
@@ -0,0 +1,44 @@
+# Copyright 2021 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test the 'set source open on|off' command.
+
+standard_testfile
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
+    return -1
+}
+
+gdb_test "show source open" "Source opening is \"on\"\\." \
+    "source opening is initially on"
+gdb_test_no_output "set listsize 1"
+gdb_test_no_output "set source open on"
+gdb_test "list 18" "18\t#include <stdio.h>" "source reading on"
+
+# File opening is turned off therefore source lines are not printed.
+gdb_test_no_output "set source open off"
+gdb_test "show source open" "Source opening is \"off\"\\." \
+    "source opening is now off"
+gdb_test "list 18" "18\tin .*/${srcfile}" "source reading off"
+
+gdb_test "forward-search main" "source code access disabled"
+gdb_test "reverse-search main" "source code access disabled"
+
+# And finally, turn source code reading back on.
+gdb_test_no_output "set source open on" \
+    "turn source reading back on"
+gdb_test "show source open" "Source opening is \"on\"\\." \
+    "source opening is on again"
+gdb_test "list 18" "18\t#include <stdio.h>" "source reading on again"

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

end of thread, other threads:[~2021-09-27 10:33 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-08 21:34 [PATCH 0/2] New option to switch off source code display Andrew Burgess
2021-09-08 21:34 ` [PATCH 1/2] gdb: remove duplicate cmd_list_element declarations Andrew Burgess
2021-09-23 20:21   ` Tom Tromey
2021-09-08 21:34 ` [PATCH 2/2] gdb: add setting to disable reading source code files Andrew Burgess
2021-09-09 18:51   ` Philippe Waroquiers
2021-09-23 20:27   ` Tom Tromey
2021-09-24 16:14   ` Pedro Alves
2021-09-27 10:33   ` 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).