Patchwork [v4] Add support for the readnever concept

login
register
mail settings
Submitter Sergio Durigan Junior
Date Nov. 30, 2017, 12:25 a.m.
Message ID <20171130002529.10592-1-sergiodj@redhat.com>
Download mbox | patch
Permalink /patch/24613/
State New
Headers show

Comments

Sergio Durigan Junior - Nov. 30, 2017, 12:25 a.m.
Changes from v3:

- Implement readnever logic on gdb/coffread.c and gdb/xcoffread.c.

- Cleanup and improve testsuite (specifically the test to make sure
  that passing '--readnever' and '--readnow' simultaneously fails).

- Split off the fix to 'symbol-file' command argument parsing.  This
  patch depends on it
  (<https://sourceware.org/ml/gdb-patches/2017-11/msg00799.html>).


[ The documentation parts have already been approved by Eli.  ]

The purpose of this concept is to turn the load of debugging
information off, either globally (via the '--readnever' option), or
objfile-specific.  The implementation proposed here is an extension of
the patch distributed with Fedora GDB; looking at the Fedora patch
itself and the history, one can see some reasons why it was never
resubmitted:

  - The patch appears to have been introduced as a workaround, at
    least initially;
  - The patch is far from perfect, as it simply shunts the load of
    DWARF debugging information, without really worrying about the
    other debug format.
  - Who really does non-symbolic debugging anyways?

One use of this feature is when a user simply wants to do the
following sequence: attach, dump core, detach.  Loading the debugging
information in this case is an unnecessary cause of delay.

This patch expands the version shipped with Fedora GDB in order to
make the feature available for all the debuginfo backends, not only
for DWARF.  It also implements a per-objfile flag which can be
activated by using the "-readnever" command when using the
'add-symbol-file' or 'symbol-file' commands.

While implementing the code for the 'symbol-file' command, I noticed a
bug in 'symbol_file_command': GDB adds the symbol file before
finishing parsing all the options, which means that the position of an
option in the command impacts whether it will be considered or not.  I
changed the code there in order to only add the symbol file after all
options have been parsed.

It's also worth mentioning that this patch tests whether GDB correctly
fails to initialize if both '--readnow' and '--readnever' options are
passed.  We didn't have any infrastructure to test that, so I added a
new version of 'gdb_spawn', called 'gdb_spawn_ignore_error', which can
be used to start a GDB that will fail without compromising the entire
testcase.

Tested on the BuildBot.

gdb/ChangeLog:

2017-11-29  Andrew Cagney  <cagney@redhat.com>
	    Joel Brobecker  <brobecker@adacore.com>
	    Sergio Durigan Junior  <sergiodj@redhat.com>

	* NEWS (Changes since GDB 8.0: Mention new '--readnever'
	feature.
	* coffread.c: Include "top.h".
	(coff_symfile_read): Do not map over sections with
	'coff_locate_sections' if readnever is on.
	* dwarf2read.c: Include "top.h".
	(dwarf2_has_info): Return 0 if READNEVER_SYMBOL_FILES is set.
	* elfread.c: Include "top.h".
	(elf_symfile_read): Do not map over sections with
	'elf_locate_sections' if readnever is on.
	* main.c (validate_readnow_readnever): New function.
	(captured_main_1): Add support for --readnever.
	(print_gdb_help): Document --readnever.
	* objfile-flags.h (enum objfile_flag) <OBJF_READNEVER>: New
	flag.
	* symfile.c (readnever_symbol_files): New global.
	(symbol_file_add_with_addrs): Set 'OBJF_READNEVER' when
	'READNEVER_SYMBOL_FILES' is set.
	(validate_readnow_readnever): New function.
	(symbol_file_command): Handle '-readnever' option.  Move call
	to 'symbol_file_add_main_1' out of the argument parsing loop.
	Call 'validate_readnow_readnever'.
	(add_symbol_file_command): Handle '-readnever' option.
	Document it.  Call 'validate_readnow_readnever'.
	* top.h (readnever_symbol_files): New extern global.
	* xcoffread.c (xcoff_initial_scan): Do not read debug
	information if readnever is on.

gdb/doc/ChangeLog:

2017-11-29  Andrew Cagney  <cagney@redhat.com>
	    Joel Brobecker  <brobecker@adacore.com>
	    Sergio Durigan Junior  <sergiodj@redhat.com>

	* gdb.texinfo (File Options): Document --readnever.
	(Commands to Specify Files): Likewise, for 'symbol-file'.

gdb/testsuite/ChangeLog:

2017-11-29  Joel Brobecker  <brobecker@adacore.com>
	    Sergio Durigan Junior  <sergiodj@redhat.com>
	    Pedro Alves  <palves@redhat.com>

	* gdb.base/readnever.c, gdb.base/readnever.exp: New files.
	* lib/gdb.exp (default_gdb_spawn): Add 'ignore_error'
	parameter.  Handle case when 'ignore_error' is set.
	(gdb_spawn_ignore_error): New function.
---
 gdb/NEWS                             |  6 +++
 gdb/coffread.c                       |  4 +-
 gdb/doc/gdb.texinfo                  | 17 ++++++++
 gdb/dwarf2read.c                     |  4 ++
 gdb/elfread.c                        |  4 +-
 gdb/main.c                           | 37 ++++++++++++++--
 gdb/objfile-flags.h                  |  4 ++
 gdb/symfile.c                        | 47 ++++++++++++++++----
 gdb/testsuite/gdb.base/readnever.c   | 41 ++++++++++++++++++
 gdb/testsuite/gdb.base/readnever.exp | 83 ++++++++++++++++++++++++++++++++++++
 gdb/top.h                            |  1 +
 gdb/xcoffread.c                      | 49 +++++++++++----------
 12 files changed, 260 insertions(+), 37 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/readnever.c
 create mode 100644 gdb/testsuite/gdb.base/readnever.exp
Pedro Alves - Nov. 30, 2017, 11:52 a.m.
On 11/30/2017 12:25 AM, Sergio Durigan Junior wrote:

> - Split off the fix to 'symbol-file' command argument parsing.  This
>   patch depends on it
>   (<https://sourceware.org/ml/gdb-patches/2017-11/msg00799.html>).

This info above conflicts with ...

> While implementing the code for the 'symbol-file' command, I noticed a
> bug in 'symbol_file_command': GDB adds the symbol file before
> finishing parsing all the options, which means that the position of an
> option in the command impacts whether it will be considered or not.  I
> changed the code there in order to only add the symbol file after all
> options have been parsed.

... this info here.  The patch still has that bit...

> 
> It's also worth mentioning that this patch tests whether GDB correctly
> fails to initialize if both '--readnow' and '--readnever' options are
> passed.  We didn't have any infrastructure to test that, so I added a
> new version of 'gdb_spawn', called 'gdb_spawn_ignore_error', which can
> be used to start a GDB that will fail without compromising the entire
> testcase.

This is stale.

> gdb/testsuite/ChangeLog:
> 
> 2017-11-29  Joel Brobecker  <brobecker@adacore.com>
> 	    Sergio Durigan Junior  <sergiodj@redhat.com>
> 	    Pedro Alves  <palves@redhat.com>
> 
> 	* gdb.base/readnever.c, gdb.base/readnever.exp: New files.
> 	* lib/gdb.exp (default_gdb_spawn): Add 'ignore_error'
> 	parameter.  Handle case when 'ignore_error' is set.
> 	(gdb_spawn_ignore_error): New function.

Also stale...

> @@ -699,7 +700,8 @@ coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
>  	}
>      }
>  
> -  bfd_map_over_sections (abfd, coff_locate_sections, (void *) info);
> +  if (!readnever_symbol_files && !(objfile->flags & OBJF_READNEVER))
> +    bfd_map_over_sections (abfd, coff_locate_sections, (void *) info);

I realized something: is it really ever possible to get here with

 readnever_symbol_files && !(objfile->flags & OBJF_READNEVER)

??

If readnever_symbol_files is set, then it'll have been propagated to
objfile->flags before we get here, no?

So references to the global readnever_symbol_files can (and should)
be removed from the readers, right?  This removes the need for
the new '#include "top.h"'s too, so please remove those from
the patch along the way.

>  
>    if (info->stabsects)
>      {
> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
> index 4f552225aa..a3127dd637 100644
> --- a/gdb/doc/gdb.texinfo
> +++ b/gdb/doc/gdb.texinfo
> @@ -1037,6 +1037,15 @@ Read each symbol file's entire symbol table immediately, rather than
>  the default, which is to read it incrementally as it is needed.
>  This makes startup slower, but makes future operations faster.
>  
> +@item --readnever
> +@cindex @code{--readnever}, command-line option
> +Do not read each symbol file's symbolic debug information.  This makes
> +startup faster but at the expense of not being able to perform
> +symbolic debugging.  DWARF unwind information is also not read,
> +meaning backtraces may become incomplete or inaccurate.  One use of
> +this is when a user simply wants to do the following sequence: attach,
> +dump core, detach.  Loading the debugging information in this case is
> +an unnecessary cause of delay.
>  @end table
>  
>  @node Mode Options
> @@ -18576,6 +18585,14 @@ tables by using the @samp{-readnow} option with any of the commands that
>  load symbol table information, if you want to be sure @value{GDBN} has the
>  entire symbol table available.
>  
> +@cindex @code{-readnever}, option for symbol-file command
> +@cindex never read symbols
> +@cindex symbols, never read
> +@item symbol-file @r{[} -readnever @r{]} @var{filename}
> +@itemx file @r{[} -readnever @r{]} @var{filename}
> +You can instruct @value{GDBN} to never read the symbolic information
> +contained in @var{filename} by using the @samp{-readnever} option.

Maybe copy the rationale for this found in "--readnever" here
or xref the "--readnever" command line option?


>  #define READNOW_HELP \
>    "The '-readnow' option will cause GDB to read the entire symbol file\n\
>  immediately.  This makes the command slower, but may make future operations\n\
> -faster."
> +faster.\n"
> +
> +#define READNEVER_HELP \
> +  "The '-readnever' option will prevent GDB from reading the symbol file's\n\
> +symbolic debug information."
>  

Why not merge these two macros in one?  We'll always want to
use them together, in the same order?  You could rename the
macro to READNOW_READNEVER_HELP for example.

Thanks,
Pedro Alves
Sergio Durigan Junior - Dec. 1, 2017, 4:35 a.m.
On Thursday, November 30 2017, Pedro Alves wrote:

> On 11/30/2017 12:25 AM, Sergio Durigan Junior wrote:
>
>> - Split off the fix to 'symbol-file' command argument parsing.  This
>>   patch depends on it
>>   (<https://sourceware.org/ml/gdb-patches/2017-11/msg00799.html>).
>
> This info above conflicts with ...
>
>> While implementing the code for the 'symbol-file' command, I noticed a
>> bug in 'symbol_file_command': GDB adds the symbol file before
>> finishing parsing all the options, which means that the position of an
>> option in the command impacts whether it will be considered or not.  I
>> changed the code there in order to only add the symbol file after all
>> options have been parsed.
>
> ... this info here.  The patch still has that bit...

You're right, I'll remove this bit since there's a whole new patch (with
all its glorious versions) to implement that.

>> 
>> It's also worth mentioning that this patch tests whether GDB correctly
>> fails to initialize if both '--readnow' and '--readnever' options are
>> passed.  We didn't have any infrastructure to test that, so I added a
>> new version of 'gdb_spawn', called 'gdb_spawn_ignore_error', which can
>> be used to start a GDB that will fail without compromising the entire
>> testcase.
>
> This is stale.

Removed the part that refers to 'gdb_spawn_ignore_error'.

>> gdb/testsuite/ChangeLog:
>> 
>> 2017-11-29  Joel Brobecker  <brobecker@adacore.com>
>> 	    Sergio Durigan Junior  <sergiodj@redhat.com>
>> 	    Pedro Alves  <palves@redhat.com>
>> 
>> 	* gdb.base/readnever.c, gdb.base/readnever.exp: New files.
>> 	* lib/gdb.exp (default_gdb_spawn): Add 'ignore_error'
>> 	parameter.  Handle case when 'ignore_error' is set.
>> 	(gdb_spawn_ignore_error): New function.
>
> Also stale...

Removed the part about lib/gdb.exp.

>> @@ -699,7 +700,8 @@ coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
>>  	}
>>      }
>>  
>> -  bfd_map_over_sections (abfd, coff_locate_sections, (void *) info);
>> +  if (!readnever_symbol_files && !(objfile->flags & OBJF_READNEVER))
>> +    bfd_map_over_sections (abfd, coff_locate_sections, (void *) info);
>
> I realized something: is it really ever possible to get here with
>
>  readnever_symbol_files && !(objfile->flags & OBJF_READNEVER)
>
> ??
>
> If readnever_symbol_files is set, then it'll have been propagated to
> objfile->flags before we get here, no?

Hm, I think so, yes.  I mean, assuming that objfile != NULL when we call
the readers, then yes; we always call symbol_file_add_with_addrs which
is responsible for propagating the flag.

> So references to the global readnever_symbol_files can (and should)
> be removed from the readers, right?  This removes the need for
> the new '#include "top.h"'s too, so please remove those from
> the patch along the way.

OK, done.

>>  
>>    if (info->stabsects)
>>      {
>> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
>> index 4f552225aa..a3127dd637 100644
>> --- a/gdb/doc/gdb.texinfo
>> +++ b/gdb/doc/gdb.texinfo
>> @@ -1037,6 +1037,15 @@ Read each symbol file's entire symbol table immediately, rather than
>>  the default, which is to read it incrementally as it is needed.
>>  This makes startup slower, but makes future operations faster.
>>  
>> +@item --readnever
>> +@cindex @code{--readnever}, command-line option
>> +Do not read each symbol file's symbolic debug information.  This makes
>> +startup faster but at the expense of not being able to perform
>> +symbolic debugging.  DWARF unwind information is also not read,
>> +meaning backtraces may become incomplete or inaccurate.  One use of
>> +this is when a user simply wants to do the following sequence: attach,
>> +dump core, detach.  Loading the debugging information in this case is
>> +an unnecessary cause of delay.
>>  @end table
>>  
>>  @node Mode Options
>> @@ -18576,6 +18585,14 @@ tables by using the @samp{-readnow} option with any of the commands that
>>  load symbol table information, if you want to be sure @value{GDBN} has the
>>  entire symbol table available.
>>  
>> +@cindex @code{-readnever}, option for symbol-file command
>> +@cindex never read symbols
>> +@cindex symbols, never read
>> +@item symbol-file @r{[} -readnever @r{]} @var{filename}
>> +@itemx file @r{[} -readnever @r{]} @var{filename}
>> +You can instruct @value{GDBN} to never read the symbolic information
>> +contained in @var{filename} by using the @samp{-readnever} option.
>
> Maybe copy the rationale for this found in "--readnever" here
> or xref the "--readnever" command line option?

I used an xref.

>
>>  #define READNOW_HELP \
>>    "The '-readnow' option will cause GDB to read the entire symbol file\n\
>>  immediately.  This makes the command slower, but may make future operations\n\
>> -faster."
>> +faster.\n"
>> +
>> +#define READNEVER_HELP \
>> +  "The '-readnever' option will prevent GDB from reading the symbol file's\n\
>> +symbolic debug information."
>>  
>
> Why not merge these two macros in one?  We'll always want to
> use them together, in the same order?  You could rename the
> macro to READNOW_READNEVER_HELP for example.

Did it.

I'll wait until we sort out the patch to extend the argument parser of
'{add-,}symbol-file' before I submit v5.

Thanks,
Pedro Alves - Dec. 1, 2017, 12:43 p.m.
On 12/01/2017 04:35 AM, Sergio Durigan Junior wrote:

>> If readnever_symbol_files is set, then it'll have been propagated to
>> objfile->flags before we get here, no?
> 
> Hm, I think so, yes.  I mean, assuming that objfile != NULL when we call
> the readers, then yes; 

Certainly you can assume that.  A reader's job is to read
in the passed in objfile.  The very first line of elf_symfile_read
would immediately crash otherwise, for example.

Thanks,
Pedro Alves
Tom Tromey - Dec. 1, 2017, 5:15 p.m.
>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> On 12/01/2017 04:35 AM, Sergio Durigan Junior wrote:
>>> If readnever_symbol_files is set, then it'll have been propagated to
objfile-> flags before we get here, no?
>> 
>> Hm, I think so, yes.  I mean, assuming that objfile != NULL when we call
>> the readers, then yes; 

Pedro> Certainly you can assume that.  A reader's job is to read
Pedro> in the passed in objfile.  The very first line of elf_symfile_read
Pedro> would immediately crash otherwise, for example.

Perhaps an ATTRIBUTE_NONNULL is in order somewhere.

Tom
Sergio Durigan Junior - Dec. 1, 2017, 5:21 p.m.
On Friday, December 01 2017, Tom Tromey wrote:

>>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:
>
> Pedro> On 12/01/2017 04:35 AM, Sergio Durigan Junior wrote:
>>>> If readnever_symbol_files is set, then it'll have been propagated to
> objfile-> flags before we get here, no?
>>> 
>>> Hm, I think so, yes.  I mean, assuming that objfile != NULL when we call
>>> the readers, then yes; 
>
> Pedro> Certainly you can assume that.  A reader's job is to read
> Pedro> in the passed in objfile.  The very first line of elf_symfile_read
> Pedro> would immediately crash otherwise, for example.
>
> Perhaps an ATTRIBUTE_NONNULL is in order somewhere.

I thought about a gdb_assert, actually.  But yeah, I agree.  I'll push
what Pedro has approved, and if he's OK with it, I'll prepare a patch
with a gdb_assert.
Pedro Alves - Dec. 1, 2017, 8 p.m.
On 12/01/2017 05:21 PM, Sergio Durigan Junior wrote:
> On Friday, December 01 2017, Tom Tromey wrote:
> 
>>>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:
>>
>> Pedro> On 12/01/2017 04:35 AM, Sergio Durigan Junior wrote:
>>>>> If readnever_symbol_files is set, then it'll have been propagated to
>> objfile-> flags before we get here, no?
>>>>
>>>> Hm, I think so, yes.  I mean, assuming that objfile != NULL when we call
>>>> the readers, then yes; 
>>
>> Pedro> Certainly you can assume that.  A reader's job is to read
>> Pedro> in the passed in objfile.  The very first line of elf_symfile_read
>> Pedro> would immediately crash otherwise, for example.
>>
>> Perhaps an ATTRIBUTE_NONNULL is in order somewhere.
> 
> I thought about a gdb_assert, actually.  But yeah, I agree.  I'll push
> what Pedro has approved, and if he's OK with it, I'll prepare a patch
> with a gdb_assert.
> 

gdb_assert (objfile != NULL) sprinkled around in the readers would be
pointless IMO.  It's not like there's any real risk that you'd
be sometimes passing a NULL pointer down to the readers in some
corner cases.  That'd be pretty gross mistake.  The objfile is kind of
like a "this" pointer here:

static void
read_symbols (struct objfile *objfile, symfile_add_flags add_flags)
{
  (*objfile->sf->sym_read) (objfile, add_flags);
...

(obviously the above crashes with a NULL objfile.  And up the
call stack, there are many other places that'd crash first
before you even get here.)

Thanks,
Pedro Alves

Patch

diff --git a/gdb/NEWS b/gdb/NEWS
index 06df52819b..ff9a819918 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,12 @@ 
 
 *** Changes since GDB 8.0
 
+* New "--readnever" command line option instructs GDB to not read each
+  symbol file's symbolic debug information.  This makes startup faster
+  but at the expense of not being able to perform symbolic debugging.
+  This option is intended for use cases where symbolic debugging will
+  not be used, e.g., when you only need to dump the debuggee's core.
+
 * GDB now uses the GNU MPFR library, if available, to emulate target
   floating-point arithmetic during expression evaluation when the target
   uses different floating-point formats than the host.  At least version
diff --git a/gdb/coffread.c b/gdb/coffread.c
index 62b6d7945a..ffe36f8751 100644
--- a/gdb/coffread.c
+++ b/gdb/coffread.c
@@ -42,6 +42,7 @@ 
 
 #include "psymtab.h"
 #include "build-id.h"
+#include "top.h"
 
 /* Key for COFF-associated data.  */
 
@@ -699,7 +700,8 @@  coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
 	}
     }
 
-  bfd_map_over_sections (abfd, coff_locate_sections, (void *) info);
+  if (!readnever_symbol_files && !(objfile->flags & OBJF_READNEVER))
+    bfd_map_over_sections (abfd, coff_locate_sections, (void *) info);
 
   if (info->stabsects)
     {
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 4f552225aa..a3127dd637 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -1037,6 +1037,15 @@  Read each symbol file's entire symbol table immediately, rather than
 the default, which is to read it incrementally as it is needed.
 This makes startup slower, but makes future operations faster.
 
+@item --readnever
+@cindex @code{--readnever}, command-line option
+Do not read each symbol file's symbolic debug information.  This makes
+startup faster but at the expense of not being able to perform
+symbolic debugging.  DWARF unwind information is also not read,
+meaning backtraces may become incomplete or inaccurate.  One use of
+this is when a user simply wants to do the following sequence: attach,
+dump core, detach.  Loading the debugging information in this case is
+an unnecessary cause of delay.
 @end table
 
 @node Mode Options
@@ -18576,6 +18585,14 @@  tables by using the @samp{-readnow} option with any of the commands that
 load symbol table information, if you want to be sure @value{GDBN} has the
 entire symbol table available.
 
+@cindex @code{-readnever}, option for symbol-file command
+@cindex never read symbols
+@cindex symbols, never read
+@item symbol-file @r{[} -readnever @r{]} @var{filename}
+@itemx file @r{[} -readnever @r{]} @var{filename}
+You can instruct @value{GDBN} to never read the symbolic information
+contained in @var{filename} by using the @samp{-readnever} option.
+
 @c FIXME: for now no mention of directories, since this seems to be in
 @c flux.  13mar1992 status is that in theory GDB would look either in
 @c current dir or in same dir as myprog; but issues like competing
diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index 2572179a75..a30fea0865 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -82,6 +82,7 @@ 
 #include <unordered_set>
 #include <unordered_map>
 #include "selftest.h"
+#include "top.h"
 
 /* When == 1, print basic high level tracing messages.
    When > 1, be more verbose.
@@ -2319,6 +2320,9 @@  int
 dwarf2_has_info (struct objfile *objfile,
                  const struct dwarf2_debug_sections *names)
 {
+  if (readnever_symbol_files || (objfile->flags & OBJF_READNEVER))
+    return 0;
+
   dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
 			objfile_data (objfile, dwarf2_objfile_data_key));
   if (!dwarf2_per_objfile)
diff --git a/gdb/elfread.c b/gdb/elfread.c
index f2483025ba..167fee25e1 100644
--- a/gdb/elfread.c
+++ b/gdb/elfread.c
@@ -47,6 +47,7 @@ 
 #include "build-id.h"
 #include "location.h"
 #include "auxv.h"
+#include "top.h"
 
 /* Forward declarations.  */
 extern const struct sym_fns elf_sym_fns_gdb_index;
@@ -1171,7 +1172,8 @@  elf_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
   struct elfinfo ei;
 
   memset ((char *) &ei, 0, sizeof (ei));
-  bfd_map_over_sections (abfd, elf_locate_sections, (void *) & ei);
+  if (!readnever_symbol_files && !(objfile->flags & OBJF_READNEVER))
+    bfd_map_over_sections (abfd, elf_locate_sections, (void *) & ei);
 
   elf_read_minimal_symbols (objfile, symfile_flags, &ei);
 
diff --git a/gdb/main.c b/gdb/main.c
index 61168faf50..8f04da107a 100644
--- a/gdb/main.c
+++ b/gdb/main.c
@@ -402,6 +402,19 @@  symbol_file_add_main_adapter (const char *arg, int from_tty)
   symbol_file_add_main (arg, add_flags);
 }
 
+/* Perform validation of the '--readnow' and '--readnever' flags.  */
+
+static void
+validate_readnow_readnever ()
+{
+  if (readnever_symbol_files && readnow_symbol_files)
+    {
+      error (_("%s: '--readnow' and '--readnever' cannot be "
+	       "specified simultaneously"),
+	     gdb_program_name);
+    }
+}
+
 /* Type of this option.  */
 enum cmdarg_kind
 {
@@ -579,14 +592,17 @@  captured_main_1 (struct captured_main_args *context)
       OPT_NOWINDOWS,
       OPT_WINDOWS,
       OPT_IX,
-      OPT_IEX
+      OPT_IEX,
+      OPT_READNOW,
+      OPT_READNEVER
     };
     static struct option long_options[] =
     {
       {"tui", no_argument, 0, OPT_TUI},
       {"dbx", no_argument, &dbx_commands, 1},
-      {"readnow", no_argument, &readnow_symbol_files, 1},
-      {"r", no_argument, &readnow_symbol_files, 1},
+      {"readnow", no_argument, NULL, OPT_READNOW},
+      {"readnever", no_argument, NULL, OPT_READNEVER},
+      {"r", no_argument, NULL, OPT_READNOW},
       {"quiet", no_argument, &quiet, 1},
       {"q", no_argument, &quiet, 1},
       {"silent", no_argument, &quiet, 1},
@@ -809,6 +825,20 @@  captured_main_1 (struct captured_main_args *context)
 	    }
 	    break;
 
+	  case OPT_READNOW:
+	    {
+	      readnow_symbol_files = 1;
+	      validate_readnow_readnever ();
+	    }
+	    break;
+
+	  case OPT_READNEVER:
+	    {
+	      readnever_symbol_files = 1;
+	      validate_readnow_readnever ();
+	    }
+	    break;
+
 	  case '?':
 	    error (_("Use `%s --help' for a complete list of options."),
 		   gdb_program_name);
@@ -1183,6 +1213,7 @@  Selection of debuggee and its files:\n\n\
   --se=FILE          Use FILE as symbol file and executable file.\n\
   --symbols=SYMFILE  Read symbols from SYMFILE.\n\
   --readnow          Fully read symbol files on first access.\n\
+  --readnever        Do not read symbol files.\n\
   --write            Set writing into executable and core files.\n\n\
 "), stream);
   fputs_unfiltered (_("\
diff --git a/gdb/objfile-flags.h b/gdb/objfile-flags.h
index 43ba8aaefd..f2a2ccfa8c 100644
--- a/gdb/objfile-flags.h
+++ b/gdb/objfile-flags.h
@@ -64,6 +64,10 @@  enum objfile_flag
        unrelated to filesystem names.  It can be for example
        "<image in memory>".  */
     OBJF_NOT_FILENAME = 1 << 6,
+
+    /* User requested that we do not read this objfile's symbolic
+       information.  */
+    OBJF_READNEVER = 1 << 7,
   };
 
 DEF_ENUM_FLAGS_TYPE (enum objfile_flag, objfile_flags);
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 4bbe0b5a62..95314829d0 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -81,6 +81,7 @@  static void clear_symtab_users_cleanup (void *ignore);
 
 /* Global variables owned by this file.  */
 int readnow_symbol_files;	/* Read full symbols immediately.  */
+int readnever_symbol_files;	/* Never read full symbols.  */
 
 /* Functions this file defines.  */
 
@@ -1131,6 +1132,12 @@  symbol_file_add_with_addrs (bfd *abfd, const char *name,
       flags |= OBJF_READNOW;
       add_flags &= ~SYMFILE_NO_READ;
     }
+  else if (readnever_symbol_files
+	   || (parent != NULL && (parent->flags & OBJF_READNEVER)))
+    {
+      flags |= OBJF_READNEVER;
+      add_flags |= SYMFILE_NO_READ;
+    }
 
   /* Give user a chance to burp if we'd be
      interactively wiping out any existing symbols.  */
@@ -1594,6 +1601,16 @@  find_separate_debug_file_by_debuglink (struct objfile *objfile)
   return debugfile;
 }
 
+/* Make sure that OBJF_{READNOW,READNEVER} are not set
+   simultaneously.  */
+
+static void
+validate_readnow_readnever (objfile_flags flags)
+{
+  if ((flags & OBJF_READNOW) && (flags & OBJF_READNEVER))
+    error (_("-readnow and -readnever cannot be used simultaneously"));
+}
+
 /* This is the symbol-file command.  Read the file, analyze its
    symbols, and add a struct symtab to a symtab list.  The syntax of
    the command is rather bizarre:
@@ -1631,17 +1648,20 @@  symbol_file_command (const char *args, int from_tty)
 	{
 	  if (strcmp (arg, "-readnow") == 0)
 	    flags |= OBJF_READNOW;
+	  else if (strcmp (arg, "-readnever") == 0)
+	    flags |= OBJF_READNEVER;
 	  else if (*arg == '-')
 	    error (_("unknown option `%s'"), arg);
 	  else
-	    {
-	      symbol_file_add_main_1 (arg, add_flags, flags);
-	      name = arg;
-	    }
+	    name = arg;
 	}
 
       if (name == NULL)
 	error (_("no symbol file name was specified"));
+
+      validate_readnow_readnever (flags);
+
+      symbol_file_add_main_1 (name, add_flags, flags);
     }
 }
 
@@ -2238,6 +2258,8 @@  add_symbol_file_command (const char *args, int from_tty)
 	    }
 	  else if (strcmp (arg, "-readnow") == 0)
 	    flags |= OBJF_READNOW;
+	  else if (strcmp (arg, "-readnever") == 0)
+	    flags |= OBJF_READNEVER;
 	  else if (strcmp (arg, "-s") == 0)
 	    {
 	      expecting_sec_name = 1;
@@ -2248,6 +2270,8 @@  add_symbol_file_command (const char *args, int from_tty)
 	}
     }
 
+  validate_readnow_readnever (flags);
+
   /* This command takes at least two arguments.  The first one is a
      filename, and the second is the address where this file has been
      loaded.  Abort now if this address hasn't been provided by the
@@ -3881,23 +3905,28 @@  _initialize_symfile (void)
 #define READNOW_HELP \
   "The '-readnow' option will cause GDB to read the entire symbol file\n\
 immediately.  This makes the command slower, but may make future operations\n\
-faster."
+faster.\n"
+
+#define READNEVER_HELP \
+  "The '-readnever' option will prevent GDB from reading the symbol file's\n\
+symbolic debug information."
 
   c = add_cmd ("symbol-file", class_files, symbol_file_command, _("\
 Load symbol table from executable file FILE.\n\
-Usage: symbol-file [-readnow] FILE\n\
+Usage: symbol-file [-readnow | -readnever] FILE\n\
 The `file' command can also load symbol tables, as well as setting the file\n\
-to execute.\n" READNOW_HELP), &cmdlist);
+to execute.\n" READNOW_HELP READNEVER_HELP), &cmdlist);
   set_cmd_completer (c, filename_completer);
 
   c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command, _("\
 Load symbols from FILE, assuming FILE has been dynamically loaded.\n\
-Usage: add-symbol-file FILE ADDR [-readnow | -s SECT-NAME SECT-ADDR]...\n\
+Usage: add-symbol-file FILE ADDR [-readnow | -readnever | \
+-s SECT-NAME SECT-ADDR]...\n\
 ADDR is the starting address of the file's text.\n\
 Each '-s' argument provides a section name and address, and\n\
 should be specified if the data and bss segments are not contiguous\n\
 with the text.  SECT-NAME is a section name to be loaded at SECT-ADDR.\n"
-READNOW_HELP),
+READNOW_HELP READNEVER_HELP),
 	       &cmdlist);
   set_cmd_completer (c, filename_completer);
 
diff --git a/gdb/testsuite/gdb.base/readnever.c b/gdb/testsuite/gdb.base/readnever.c
new file mode 100644
index 0000000000..6489e891de
--- /dev/null
+++ b/gdb/testsuite/gdb.base/readnever.c
@@ -0,0 +1,41 @@ 
+/* Copyright 2016-2017 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/>.  */
+
+static void
+fun_three (int a, char b, void *c)
+{
+  /* Do nothing.  */
+}
+
+static void
+fun_two (unsigned int p, const char *y)
+{
+  fun_three ((int) p, '1', (void *) y);
+}
+
+static void
+fun_one (int *x)
+{
+  fun_two (10, (const char *) x);
+}
+
+int
+main (void)
+{
+  int a = 10;
+
+  fun_one (&a);
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/readnever.exp b/gdb/testsuite/gdb.base/readnever.exp
new file mode 100644
index 0000000000..151f12a6bb
--- /dev/null
+++ b/gdb/testsuite/gdb.base/readnever.exp
@@ -0,0 +1,83 @@ 
+# Copyright 2016-2017 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/>.
+
+standard_testfile .c
+
+if { [build_executable "failed to build" $testfile $srcfile { debug }] == -1 } {
+    untested "Couldn't compile ${srcfile}"
+    return -1
+}
+
+save_vars { GDBFLAGS } {
+    append GDBFLAGS " --readnever"
+    clean_restart ${binfile}
+}
+
+if ![runto_main] then {
+    perror "couldn't run to breakpoint"
+    continue
+}
+
+gdb_test "break fun_three" \
+         "Breakpoint $decimal at $hex"
+
+gdb_test "continue" \
+         "Breakpoint $decimal, $hex in fun_three \\(\\)"
+
+gdb_test "backtrace" \
+         [multi_line "#0  $hex in fun_three \\(\\)" \
+                     "#1  $hex in fun_two \\(\\)" \
+                     "#2  $hex in fun_one \\(\\)" \
+                     "#3  $hex in main \\(\\)" ]
+
+gdb_test_no_output "maint info symtabs" \
+    "maint info symtabs no output for --readnever"
+gdb_test_no_output "maint info psymtabs" \
+    "maint info psymtabs no output for --readnever"
+
+# Test invalid combination of flags.
+save_vars { GDBFLAGS } {
+    append GDBFLAGS " --readnever --readnow"
+    gdb_exit
+    gdb_spawn
+
+    set test "test readnow and readnever at the same time"
+    gdb_test_multiple "" $test {
+	"'--readnow' and '--readnever' cannot be specified simultaneously" {
+	    pass $test
+	    set test "expect eof after failure"
+	    gdb_test_multiple "" $test {
+		eof {
+		    pass $test
+		}
+	    }
+	}
+    }
+}
+
+
+# Test symbol-file's -readnever option.
+
+# Restart GDB without the --readnever option.
+gdb_exit
+gdb_start
+gdb_test "symbol-file ${binfile}0.o -readnever" \
+    "Reading symbols from ${binfile}0\.o\.\.\.\\\(no debugging symbols found\\\)\.\.\.done\." \
+    "use symbol-file -readnever"
+
+gdb_test_no_output "maint info symtabs" \
+    "maint info symtabs no output for symbol-file -readnever"
+gdb_test_no_output "maint info psymtabs" \
+    "maint info psymtabs no output for symbol-file -readnever"
diff --git a/gdb/top.h b/gdb/top.h
index 26fe87842f..a1df64f383 100644
--- a/gdb/top.h
+++ b/gdb/top.h
@@ -267,6 +267,7 @@  extern int gdb_in_secondary_prompt_p (struct ui *ui);
 
 /* From random places.  */
 extern int readnow_symbol_files;
+extern int readnever_symbol_files;
 
 /* Perform _initialize initialization.  */
 extern void gdb_init (char *);
diff --git a/gdb/xcoffread.c b/gdb/xcoffread.c
index 46a43bc9ae..e84a2f93e8 100644
--- a/gdb/xcoffread.c
+++ b/gdb/xcoffread.c
@@ -48,6 +48,7 @@ 
 #include "psympriv.h"
 
 #include "gdb-stabs.h"
+#include "top.h"
 
 /* For interface with stabsread.c.  */
 #include "aout/stab_gnu.h"
@@ -2975,31 +2976,33 @@  xcoff_initial_scan (struct objfile *objfile, symfile_add_flags symfile_flags)
       /* Read the string table.  */
       init_stringtab (abfd, stringtab_offset, objfile);
 
-      /* Read the .debug section, if present.  */
-      {
-	struct bfd_section *secp;
-	bfd_size_type length;
-	bfd_byte *debugsec = NULL;
+      /* Read the .debug section, if present and if we're not ignoring
+	 it.  */
+      if (!readnever_symbol_files && !(objfile->flags & OBJF_READNEVER))
+	{
+	  struct bfd_section *secp;
+	  bfd_size_type length;
+	  bfd_byte *debugsec = NULL;
 
-	secp = bfd_get_section_by_name (abfd, ".debug");
-	if (secp)
-	  {
-	    length = bfd_section_size (abfd, secp);
-	    if (length)
-	      {
-		debugsec
-		  = (bfd_byte *) obstack_alloc (&objfile->objfile_obstack,
-						length);
+	  secp = bfd_get_section_by_name (abfd, ".debug");
+	  if (secp)
+	    {
+	      length = bfd_section_size (abfd, secp);
+	      if (length)
+		{
+		  debugsec
+		    = (bfd_byte *) obstack_alloc (&objfile->objfile_obstack,
+						  length);
 
-		if (!bfd_get_full_section_contents (abfd, secp, &debugsec))
-		  {
-		    error (_("Error reading .debug section of `%s': %s"),
-			   name, bfd_errmsg (bfd_get_error ()));
-		  }
-	      }
-	  }
-	info->debugsec = (char *) debugsec;
-      }
+		  if (!bfd_get_full_section_contents (abfd, secp, &debugsec))
+		    {
+		      error (_("Error reading .debug section of `%s': %s"),
+			     name, bfd_errmsg (bfd_get_error ()));
+		    }
+		}
+	    }
+	  info->debugsec = (char *) debugsec;
+	}
     }
 
   /* Read the symbols.  We keep them in core because we will want to