public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/3 v2] Limit tab-completion result when list is large
@ 2014-11-06 10:50 Gary Benson
  2014-11-06 10:51 ` [PATCH 1/3 v2] Add expansion_notify callback to expand_symtabs_matching Gary Benson
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Gary Benson @ 2014-11-06 10:50 UTC (permalink / raw)
  To: gdb-patches

Hi all,

When debugging large programs, pressing the Tab key at the wrong
time causes GDB to lock up while it builds a completion list.  As
an example, with LibreOffice:

 $ gdb /usr/lib64/libreoffice/program/soffice.bin
 (gdb) start
 (gdb) b <Tab>

On my (pretty fast!) machine the user is left hanging for nearly a
minute while GDB builds a list of the 212405 places the user could
break on.  This is PR cli/11920.

PR cli/15548 proposes to abort building the completion list if it
grows too large, the theory being that nobody is going to scroll
through thousands of screens of completions to find the one they
want.  These three patches implement this.  The default limit is 200
completions, which the user can override with a new set/show option.

Tested on RHEL6.5 x86_64, no regressions.

Is this ok to commit?

Thanks,
Gary

--
http://gbenson.net/

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

* [PATCH 1/3 v2] Add expansion_notify callback to expand_symtabs_matching
  2014-11-06 10:50 [PATCH 0/3 v2] Limit tab-completion result when list is large Gary Benson
@ 2014-11-06 10:51 ` Gary Benson
  2014-11-06 10:51 ` [PATCH 2/3 v2] Interleave completion list building with symbol table expansion Gary Benson
  2014-11-06 10:51 ` [PATCH 3/3 v2] Implement completion limiting Gary Benson
  2 siblings, 0 replies; 9+ messages in thread
From: Gary Benson @ 2014-11-06 10:51 UTC (permalink / raw)
  To: gdb-patches

This commit adds a new callback parameter, "expansion_notify", to the
top-level expand_symtabs_matching function and to all the vectorized
functions it defers to.  If expansion_notify is non-NULL, it will be
called every time a symbol table is expanded.

gdb/ChangeLog:

	* symfile.h (expand_symtabs_exp_notify_ftype): New typedef.
	(struct quick_symbol_functions) <expand_symtabs_matching>:
	New argument expansion_notify.  All uses updated.
	(expand_symtabs_matching): New argument expansion_notify.
	All uses updated.
	* symfile-debug.c (debug_qf_expand_symtabs_matching):
	Also print expansion notify.
	* symtab.c (expand_symtabs_matching_via_partial): Call
	expansion_notify whenever a partial symbol table is expanded.
	* dwarf2read.c (dw2_expand_symtabs_matching): Call
	expansion_notify whenever a symbol table is instantiated.
---
 gdb/ChangeLog       |   14 ++++++++++++++
 gdb/ada-lang.c      |    6 +++---
 gdb/dwarf2read.c    |   14 +++++++++++++-
 gdb/linespec.c      |    2 +-
 gdb/psymtab.c       |    8 +++++++-
 gdb/symfile-debug.c |    5 ++++-
 gdb/symfile.c       |    4 +++-
 gdb/symfile.h       |    8 ++++++++
 gdb/symmisc.c       |    2 +-
 gdb/symtab.c        |    6 +++---
 10 files changed, 57 insertions(+), 12 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 5793cd2..8f6e086 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -6178,8 +6178,8 @@ ada_make_symbol_completion_list (const char *text0, const char *word,
     data.word = word;
     data.wild_match = wild_match_p;
     data.encoded = encoded_p;
-    expand_symtabs_matching (NULL, ada_complete_symbol_matcher, ALL_DOMAIN,
-			     &data);
+    expand_symtabs_matching (NULL, ada_complete_symbol_matcher, NULL,
+			     ALL_DOMAIN, &data);
   }
 
   /* At this point scan through the misc symbol vectors and add each
@@ -12839,7 +12839,7 @@ ada_add_global_exceptions (regex_t *preg, VEC(ada_exc_info) **exceptions)
   struct objfile *objfile;
   struct symtab *s;
 
-  expand_symtabs_matching (NULL, ada_exc_search_name_matches,
+  expand_symtabs_matching (NULL, ada_exc_search_name_matches, NULL,
 			   VARIABLES_DOMAIN, preg);
 
   ALL_PRIMARY_SYMTABS (objfile, s)
diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index 3f2a127..402cf96 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -3782,6 +3782,7 @@ dw2_expand_symtabs_matching
   (struct objfile *objfile,
    expand_symtabs_file_matcher_ftype *file_matcher,
    expand_symtabs_symbol_matcher_ftype *symbol_matcher,
+   expand_symtabs_exp_notify_ftype *expansion_notify,
    enum search_domain kind,
    void *data)
 {
@@ -3953,7 +3954,18 @@ dw2_expand_symtabs_matching
 
 	  per_cu = dw2_get_cutu (cu_index);
 	  if (file_matcher == NULL || per_cu->v.quick->mark)
-	    dw2_instantiate_symtab (per_cu);
+	    {
+	      int symtab_was_null = (per_cu->v.quick->symtab == NULL);
+
+	      dw2_instantiate_symtab (per_cu);
+
+	      if (expansion_notify != NULL
+		  && symtab_was_null
+		  && per_cu->v.quick->symtab != NULL)
+		{
+		  expansion_notify (per_cu->v.quick->symtab, data);
+		}
+	    }
 	}
     }
 }
diff --git a/gdb/linespec.c b/gdb/linespec.c
index 5325702..21d7011 100644
--- a/gdb/linespec.c
+++ b/gdb/linespec.c
@@ -1028,7 +1028,7 @@ iterate_over_all_matching_symtabs (struct linespec_state *state,
       if (objfile->sf)
 	objfile->sf->qf->expand_symtabs_matching (objfile, NULL,
 						  iterate_name_matcher,
-						  ALL_DOMAIN,
+						  NULL, ALL_DOMAIN,
 						  &matcher_data);
 
       ALL_OBJFILE_PRIMARY_SYMTABS (objfile, symtab)
diff --git a/gdb/psymtab.c b/gdb/psymtab.c
index 6c0c880..e28b100 100644
--- a/gdb/psymtab.c
+++ b/gdb/psymtab.c
@@ -1364,6 +1364,7 @@ expand_symtabs_matching_via_partial
   (struct objfile *objfile,
    expand_symtabs_file_matcher_ftype *file_matcher,
    expand_symtabs_symbol_matcher_ftype *symbol_matcher,
+   expand_symtabs_exp_notify_ftype *expansion_notify,
    enum search_domain kind,
    void *data)
 {
@@ -1406,7 +1407,12 @@ expand_symtabs_matching_via_partial
 	}
 
       if (recursively_search_psymtabs (ps, objfile, kind, symbol_matcher, data))
-	psymtab_to_symtab (objfile, ps);
+	{
+	  struct symtab *symtab = psymtab_to_symtab (objfile, ps);
+
+	  if (expansion_notify != NULL)
+	    expansion_notify (symtab, data);
+	}
     }
 }
 
diff --git a/gdb/symfile-debug.c b/gdb/symfile-debug.c
index 170ba3a..75ef025 100644
--- a/gdb/symfile-debug.c
+++ b/gdb/symfile-debug.c
@@ -287,22 +287,25 @@ debug_qf_expand_symtabs_matching
   (struct objfile *objfile,
    expand_symtabs_file_matcher_ftype *file_matcher,
    expand_symtabs_symbol_matcher_ftype *symbol_matcher,
+   expand_symtabs_exp_notify_ftype *expansion_notify,
    enum search_domain kind, void *data)
 {
   const struct debug_sym_fns_data *debug_data =
     objfile_data (objfile, symfile_debug_objfile_data_key);
 
   fprintf_filtered (gdb_stdlog,
-		    "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n",
+		    "qf->expand_symtabs_matching (%s, %s, %s, %s, %s, %s)\n",
 		    debug_objfile_name (objfile),
 		    host_address_to_string (file_matcher),
 		    host_address_to_string (symbol_matcher),
+		    host_address_to_string (expansion_notify),
 		    search_domain_name (kind),
 		    host_address_to_string (data));
 
   debug_data->real_sf->qf->expand_symtabs_matching (objfile,
 						    file_matcher,
 						    symbol_matcher,
+						    expansion_notify,
 						    kind, data);
 }
 
diff --git a/gdb/symfile.c b/gdb/symfile.c
index b16abe4..b35ddd9 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -3902,6 +3902,7 @@ symfile_free_objfile (struct objfile *objfile)
 void
 expand_symtabs_matching (expand_symtabs_file_matcher_ftype *file_matcher,
 			 expand_symtabs_symbol_matcher_ftype *symbol_matcher,
+			 expand_symtabs_exp_notify_ftype *expansion_notify,
 			 enum search_domain kind,
 			 void *data)
 {
@@ -3911,7 +3912,8 @@ expand_symtabs_matching (expand_symtabs_file_matcher_ftype *file_matcher,
   {
     if (objfile->sf)
       objfile->sf->qf->expand_symtabs_matching (objfile, file_matcher,
-						symbol_matcher, kind,
+						symbol_matcher,
+						expansion_notify, kind,
 						data);
   }
 }
diff --git a/gdb/symfile.h b/gdb/symfile.h
index f56aff3..bcff871 100644
--- a/gdb/symfile.h
+++ b/gdb/symfile.h
@@ -137,6 +137,12 @@ typedef int (expand_symtabs_file_matcher_ftype) (const char *filename,
 typedef int (expand_symtabs_symbol_matcher_ftype) (const char *name,
 						   void *data);
 
+/* Callback for quick_symbol_functions->expand_symtabs_matching
+   to be called after a symtab has been expanded.  */
+
+typedef void (expand_symtabs_exp_notify_ftype) (struct symtab *symtab,
+						void *data);
+
 /* The "quick" symbol functions exist so that symbol readers can
    avoiding an initial read of all the symbols.  For example, symbol
    readers might choose to use the "partial symbol table" utilities,
@@ -282,6 +288,7 @@ struct quick_symbol_functions
     (struct objfile *objfile,
      expand_symtabs_file_matcher_ftype *file_matcher,
      expand_symtabs_symbol_matcher_ftype *symbol_matcher,
+     expand_symtabs_exp_notify_ftype *expansion_notify,
      enum search_domain kind,
      void *data);
 
@@ -565,6 +572,7 @@ extern struct cleanup *increment_reading_symtab (void);
 
 void expand_symtabs_matching (expand_symtabs_file_matcher_ftype *,
 			      expand_symtabs_symbol_matcher_ftype *,
+			      expand_symtabs_exp_notify_ftype *,
 			      enum search_domain kind, void *data);
 
 void map_symbol_filenames (symbol_filename_ftype *fun, void *data,
diff --git a/gdb/symmisc.c b/gdb/symmisc.c
index 223a7d1..e48fae7 100644
--- a/gdb/symmisc.c
+++ b/gdb/symmisc.c
@@ -897,7 +897,7 @@ maintenance_expand_symtabs (char *args, int from_tty)
 	{
 	  objfile->sf->qf->expand_symtabs_matching
 	    (objfile, maintenance_expand_file_matcher,
-	     maintenance_expand_name_matcher, ALL_DOMAIN, regexp);
+	     maintenance_expand_name_matcher, NULL, ALL_DOMAIN, regexp);
 	}
     }
 
diff --git a/gdb/symtab.c b/gdb/symtab.c
index ed164f7..836f3bc 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -3775,7 +3775,7 @@ search_symbols (const char *regexp, enum search_domain kind,
 			    ? NULL
 			    : search_symbols_file_matches),
 			   search_symbols_name_matches,
-			   kind, &datum);
+			   NULL, kind, &datum);
 
   /* Here, we search through the minimal symbol tables for functions
      and variables that match, and force their symbols to be read.
@@ -4564,8 +4564,8 @@ default_make_symbol_completion_list_break_on (const char *text,
   /* Look through the partial symtabs for all symbols which begin
      by matching SYM_TEXT.  Expand all CUs that you find to the list.
      The real names will get added by COMPLETION_LIST_ADD_SYMBOL below.  */
-  expand_symtabs_matching (NULL, symbol_completion_matcher, ALL_DOMAIN,
-			   &datum);
+  expand_symtabs_matching (NULL, symbol_completion_matcher, NULL,
+			   ALL_DOMAIN, &datum);
 
   /* At this point scan through the misc symbol vectors and add each
      symbol you find to the list.  Eventually we want to ignore
-- 
1.7.1

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

* [PATCH 2/3 v2] Interleave completion list building with symbol table expansion
  2014-11-06 10:50 [PATCH 0/3 v2] Limit tab-completion result when list is large Gary Benson
  2014-11-06 10:51 ` [PATCH 1/3 v2] Add expansion_notify callback to expand_symtabs_matching Gary Benson
@ 2014-11-06 10:51 ` Gary Benson
  2014-11-06 10:51 ` [PATCH 3/3 v2] Implement completion limiting Gary Benson
  2 siblings, 0 replies; 9+ messages in thread
From: Gary Benson @ 2014-11-06 10:51 UTC (permalink / raw)
  To: gdb-patches

This commit makes default_make_symbol_completion_list_break_on build
the list of completions as it expands the necessary symbol tables,
rather than expanding all necessary symbol tables first and then
building the completion lists second.  This allows for the early
termination of symbol table expansion if required.

gdb/ChangeLog:

	* symtab.c (struct add_name_data) <code>: New field.
	Updated comments.
	(add_symtab_completions): New function.
	(symtab_expansion_callback): Likewise.
	(default_make_symbol_completion_list_break_on): Set datum.code.
	Move minimal symbol scan before calling expand_symtabs_matching.
	Scan known primary symtabs for externs and statics before calling
	expand_symtabs_matching.  Pass symtab_expansion_callback as
	expansion_notify argument to expand_symtabs_matching.  Do not scan
	primary symtabs for externs and statics after calling
	expand_symtabs_matching.
---
 gdb/ChangeLog |   14 +++++++
 gdb/symtab.c  |  106 ++++++++++++++++++++++++++++++++++++---------------------
 2 files changed, 81 insertions(+), 39 deletions(-)

diff --git a/gdb/symtab.c b/gdb/symtab.c
index 836f3bc..ebb7b8f 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -4424,15 +4424,19 @@ completion_list_add_fields (struct symbol *sym, const char *sym_text,
     }
 }
 
-/* Type of the user_data argument passed to add_macro_name or
-   symbol_completion_matcher.  The contents are simply whatever is
-   needed by completion_list_add_name.  */
+/* Type of the user_data argument passed to add_macro_name,
+   symbol_completion_matcher and symtab_expansion_callback.  */
+
 struct add_name_data
 {
+  /* Arguments required by completion_list_add_name.  */
   const char *sym_text;
   int sym_text_len;
   const char *text;
   const char *word;
+
+  /* Extra argument required for add_symtab_completions.  */
+  enum type_code code;
 };
 
 /* A callback used with macro_for_each and macro_for_each_in_scope.
@@ -4460,6 +4464,52 @@ symbol_completion_matcher (const char *name, void *user_data)
   return compare_symbol_name (name, datum->sym_text, datum->sym_text_len);
 }
 
+/* Add matching symbols from SYMTAB to the current completion list.  */
+
+static void
+add_symtab_completions (struct symtab *symtab,
+			const char *sym_text, int sym_text_len,
+			const char *text, const char *word,
+			enum type_code code)
+{
+  struct symbol *sym;
+  const struct block *b;
+  struct block_iterator iter;
+  int i;
+
+  if (!symtab->primary)
+    return;
+
+  for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
+    {
+      QUIT;
+      b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), i);
+      ALL_BLOCK_SYMBOLS (b, iter, sym)
+	{
+	  if (code == TYPE_CODE_UNDEF
+	      || (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
+		  && TYPE_CODE (SYMBOL_TYPE (sym)) == code))
+	    COMPLETION_LIST_ADD_SYMBOL (sym,
+					sym_text, sym_text_len,
+					text, word);
+	}
+    }
+}
+
+/* Callback to add completions to the current list when symbol tables
+   are expanded during completion list generation.  */
+
+static void
+symtab_expansion_callback (struct symtab *symtab, void *user_data)
+{
+  struct add_name_data *datum = (struct add_name_data *) user_data;
+
+  add_symtab_completions (symtab,
+			  datum->sym_text, datum->sym_text_len,
+			  datum->text, datum->word,
+			  datum->code);
+}
+
 VEC (char_ptr) *
 default_make_symbol_completion_list_break_on (const char *text,
 					      const char *word,
@@ -4560,17 +4610,12 @@ default_make_symbol_completion_list_break_on (const char *text,
   datum.sym_text_len = sym_text_len;
   datum.text = text;
   datum.word = word;
-
-  /* Look through the partial symtabs for all symbols which begin
-     by matching SYM_TEXT.  Expand all CUs that you find to the list.
-     The real names will get added by COMPLETION_LIST_ADD_SYMBOL below.  */
-  expand_symtabs_matching (NULL, symbol_completion_matcher, NULL,
-			   ALL_DOMAIN, &datum);
+  datum.code = code;
 
   /* At this point scan through the misc symbol vectors and add each
      symbol you find to the list.  Eventually we want to ignore
      anything that isn't a text symbol (everything else will be
-     handled by the psymtab code above).  */
+     handled by the psymtab code below).  */
 
   if (code == TYPE_CODE_UNDEF)
     {
@@ -4585,6 +4630,18 @@ default_make_symbol_completion_list_break_on (const char *text,
 	}
     }
 
+  /* Add completions for all currently loaded symbol tables.  */
+  ALL_PRIMARY_SYMTABS (objfile, s)
+    add_symtab_completions (s, sym_text, sym_text_len, text, word, code);
+
+  /* Look through the partial symtabs for all symbols which begin
+     by matching SYM_TEXT.  Expand all CUs that you find to the list.
+     symtab_expansion_callback is called for each expanded symtab,
+     causing those symtab's completions to be added to the list too.  */
+  expand_symtabs_matching (NULL, symbol_completion_matcher,
+			   symtab_expansion_callback, ALL_DOMAIN,
+			   &datum);
+
   /* Search upwards from currently selected frame (so that we can
      complete on local vars).  Also catch fields of types defined in
      this places which match our text string.  Only complete on types
@@ -4634,35 +4691,6 @@ default_make_symbol_completion_list_break_on (const char *text,
 	  completion_list_add_fields (sym, sym_text, sym_text_len, text, word);
     }
 
-  /* Go through the symtabs and check the externs and statics for
-     symbols which match.  */
-
-  ALL_PRIMARY_SYMTABS (objfile, s)
-  {
-    QUIT;
-    b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
-    ALL_BLOCK_SYMBOLS (b, iter, sym)
-      {
-	if (code == TYPE_CODE_UNDEF
-	    || (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
-		&& TYPE_CODE (SYMBOL_TYPE (sym)) == code))
-	  COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
-      }
-  }
-
-  ALL_PRIMARY_SYMTABS (objfile, s)
-  {
-    QUIT;
-    b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
-    ALL_BLOCK_SYMBOLS (b, iter, sym)
-      {
-	if (code == TYPE_CODE_UNDEF
-	    || (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
-		&& TYPE_CODE (SYMBOL_TYPE (sym)) == code))
-	  COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
-      }
-  }
-
   /* Skip macros if we are completing a struct tag -- arguable but
      usually what is expected.  */
   if (current_language->la_macro_expansion == macro_expansion_c
-- 
1.7.1

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

* [PATCH 3/3 v2] Implement completion limiting
  2014-11-06 10:50 [PATCH 0/3 v2] Limit tab-completion result when list is large Gary Benson
  2014-11-06 10:51 ` [PATCH 1/3 v2] Add expansion_notify callback to expand_symtabs_matching Gary Benson
  2014-11-06 10:51 ` [PATCH 2/3 v2] Interleave completion list building with symbol table expansion Gary Benson
@ 2014-11-06 10:51 ` Gary Benson
  2014-11-06 16:28   ` Eli Zaretskii
  2014-11-24  6:09   ` Doug Evans
  2 siblings, 2 replies; 9+ messages in thread
From: Gary Benson @ 2014-11-06 10:51 UTC (permalink / raw)
  To: gdb-patches

This commit adds a new exception, TOO_MANY_COMPLETIONS_ERROR, to be
thrown whenever the completer has generated too many possibilities to
be useful.  A new user-settable variable, "max_completions", is added
to control this behaviour.  A top-level completion limit is added to
complete_line_internal, as the final check to ensure the user never
sees too many completions.  An additional limit is added to
default_make_symbol_completion_list_break_on, to halt time-consuming
symbol table expansions.

gdb/ChangeLog:

	PR cli/9007
	PR cli/11920
	PR cli/15548
	* common/common-exceptions.h (enum errors)
	<TOO_MANY_COMPLETIONS_ERROR>: New value.
	* completer.h (completion_tracker_t): New typedef.
	(new_completion_tracker): New declaration.
	(make_cleanup_free_completion_tracker): Likewise.
	(maybe_limit_completions): Likewise.
	* completer.c [TUI]: Include tui/tui.h and tui/tui-io.h.
	(max_completions): New static variable.
	(new_completion_tracker): New function.
	(make_cleanup_free_completion_tracker): Likewise.
	(maybe_limit_completions): Likewise.
	(complete_line_internal): Do not generate any completions if
	max_completions = 0.  Limit the number of completions if
	max_completions >= 0.
	(line_completion_function): Handle TOO_MANY_COMPLETIONS_ERROR.
	(_initialize_completer): New declaration and function.
	* symtab.c: Include completer.h.
	(completion_tracker): New static variable.
	(completion_list_add_name): Call maybe_limit_completions.
	(default_make_symbol_completion_list_break_on): Maintain
	completion_tracker across calls to completion_list_add_name.
	* NEWS (New Options): Mention set/show max-completions.

gdb/doc/ChangeLog:

	* gdb.texinfo (Command Completion): Document new
	"set/show max-completions" option.

gdb/testsuite/ChangeLog:

	* gdb.base/completion.exp: Disable completion limiting for
	existing tests.  Add new tests to check completion limiting.
	* gdb.linespec/ls-errs.exp: Disable completion limiting.
---
 gdb/ChangeLog                          |   28 ++++++
 gdb/NEWS                               |   11 ++-
 gdb/common/common-exceptions.h         |    4 +
 gdb/completer.c                        |  146 ++++++++++++++++++++++++++++++--
 gdb/completer.h                        |   23 +++++
 gdb/doc/ChangeLog                      |    5 +
 gdb/doc/gdb.texinfo                    |   25 ++++++
 gdb/symtab.c                           |   22 +++++-
 gdb/testsuite/ChangeLog                |    6 ++
 gdb/testsuite/gdb.base/completion.exp  |   33 +++++++
 gdb/testsuite/gdb.linespec/ls-errs.exp |    3 +
 11 files changed, 298 insertions(+), 8 deletions(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index 649c29e..b5d971e 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -22,11 +22,20 @@
   ** $_any_caller_is(name [, number_of_frames])
   ** $_any_caller_matches(regexp [, number_of_frames])
 
-* New commands
+* New commands (for set/show, see "New options" below)
 
 queue-signal signal-name-or-number
   Queue a signal to be delivered to the thread when it is resumed.
 
+* New options
+
+set max-completions
+show max-completions
+  Set the maximum number of possibilities to be considered during
+  completion.  The default value is 200.  This limit allows GDB to
+  avoid generating large completion lists, the computation of which
+  can cause the debugger to become temporarily unresponsive.
+
 * On resume, GDB now always passes the signal the program had stopped
   for to the thread the signal was sent to, even if the user changed
   threads before resuming.  Previously GDB would often (but not
diff --git a/gdb/common/common-exceptions.h b/gdb/common/common-exceptions.h
index 5f750c3..73fd255 100644
--- a/gdb/common/common-exceptions.h
+++ b/gdb/common/common-exceptions.h
@@ -99,6 +99,10 @@ enum errors {
   /* Requested feature, method, mechanism, etc. is not supported.  */
   NOT_SUPPORTED_ERROR,
 
+  /* The number of candidates generated during line completion has
+     exceeded the user's specified limit.  */
+  TOO_MANY_COMPLETIONS_ERROR,
+
   /* Add more errors here.  */
   NR_ERRORS
 };
diff --git a/gdb/completer.c b/gdb/completer.c
index a0f3fa3..b26d403 100644
--- a/gdb/completer.c
+++ b/gdb/completer.c
@@ -39,6 +39,11 @@
 
 #include "completer.h"
 
+#ifdef TUI
+#include "tui/tui.h"
+#include "tui/tui-io.h"
+#endif
+
 /* Prototypes for local functions.  */
 static
 char *line_completion_function (const char *text, int matches, 
@@ -778,9 +783,67 @@ complete_line_internal (const char *text,
 
   return list;
 }
-/* Generate completions all at once.  Returns a vector of strings.
-   Each element is allocated with xmalloc.  It can also return NULL if
-   there are no completions.
+
+/* Maximum number of possibilities to consider before the completer
+   bails by throwing TOO_MANY_COMPLETIONS_ERROR.  Negative values
+   disable limiting.  */
+static int max_completions = 200;
+
+/* See completer.h.  */
+
+completion_tracker_t
+new_completion_tracker (void)
+{
+  if (max_completions < 1)
+    return NULL;
+
+  return htab_create_alloc (max_completions,
+			    htab_hash_string, (htab_eq) streq,
+			    NULL, xcalloc, xfree);
+}
+
+/* See completer.h.  */
+
+struct cleanup *
+make_cleanup_free_completion_tracker (completion_tracker_t tracker)
+{
+  if (tracker == NULL)
+    return make_cleanup (null_cleanup, NULL);
+
+  return make_cleanup_htab_delete (tracker);
+}
+
+/* See completer.h.  */
+
+void
+maybe_limit_completions (completion_tracker_t tracker, char *name)
+{
+  if (max_completions < 0)
+    return;
+
+  if (tracker != NULL)
+    {
+      void **slot = htab_find_slot (tracker, name, INSERT);
+
+      if (*slot != HTAB_EMPTY_ENTRY)
+	return;
+
+      if (htab_elements (tracker) <= max_completions)
+	{
+	  *slot = name;
+	  return;
+	}
+    }
+
+  throw_error (TOO_MANY_COMPLETIONS_ERROR,
+	       _("Too many possibilities."));
+}
+
+/* Generate completions all at once.  Returns a vector of strings
+   allocated with xmalloc.  Returns NULL if there are no completions
+   or if max_completions is 0.  Throws TOO_MANY_COMPLETIONS_ERROR if
+   max_completions is greater than zero and the number of completions
+   is greater than max_completions.
 
    TEXT is the caller's idea of the "word" we are looking at.
 
@@ -793,8 +856,33 @@ complete_line_internal (const char *text,
 VEC (char_ptr) *
 complete_line (const char *text, const char *line_buffer, int point)
 {
-  return complete_line_internal (text, line_buffer, 
-				 point, handle_completions);
+  VEC (char_ptr) *list = NULL;
+  struct cleanup *old_chain;
+
+  list = complete_line_internal (text, line_buffer, point,
+				 handle_completions);
+  old_chain = make_cleanup_free_char_ptr_vec (list);
+
+  /* Possibly throw TOO_MANY_COMPLETIONS_ERROR.  Individual
+     completers may do this too, to avoid unnecessary work,
+     but this is the ultimate check that stops users seeing
+     more completions than they wanted.  */
+  if (max_completions >= 0)
+    {
+      completion_tracker_t tracker = new_completion_tracker ();
+      struct cleanup *limit_chain =
+	make_cleanup_free_completion_tracker (tracker);
+      char *candidate;
+      int ix;
+
+      for (ix = 0; VEC_iterate (char_ptr, list, ix, candidate); ++ix)
+	maybe_limit_completions (tracker, candidate);
+
+      do_cleanups (limit_chain);
+    }
+
+  discard_cleanups (old_chain);
+  return list;
 }
 
 /* Complete on command names.  Used by "help".  */
@@ -881,6 +969,8 @@ line_completion_function (const char *text, int matches,
 
   if (matches == 0)
     {
+      volatile struct gdb_exception ex;
+
       /* The caller is beginning to accumulate a new set of
          completions, so we need to find all of them now, and cache
          them for returning one at a time on future calls.  */
@@ -894,7 +984,35 @@ line_completion_function (const char *text, int matches,
 	  VEC_free (char_ptr, list);
 	}
       index = 0;
-      list = complete_line (text, line_buffer, point);
+
+      TRY_CATCH (ex, RETURN_MASK_ALL)
+	list = complete_line (text, line_buffer, point);
+
+      if (ex.reason < 0)
+	{
+	  if (ex.error != TOO_MANY_COMPLETIONS_ERROR)
+	    throw_exception (ex);
+
+	  if (rl_completion_type != TAB)
+	    {
+#if defined(TUI)
+	      if (tui_active)
+		{
+		  tui_puts ("\n");
+		  tui_puts (ex.message);
+		  tui_puts ("\n");
+		}
+	      else
+#endif
+		{
+		  rl_crlf ();
+		  fputs (ex.message, rl_outstream);
+		  rl_crlf ();
+		}
+
+	      rl_on_new_line ();
+	    }
+	}
     }
 
   /* If we found a list of potential completions during initialization
@@ -978,3 +1096,19 @@ skip_quoted (const char *str)
 {
   return skip_quoted_chars (str, NULL, NULL);
 }
+
+extern initialize_file_ftype _initialize_completer; /* -Wmissing-prototypes */
+
+void
+_initialize_completer (void)
+{
+  add_setshow_zuinteger_unlimited_cmd ("max-completions", no_class,
+				       &max_completions, _("\
+Set maximum number of line completion possibilities."), _("\
+Show maximum number of line completion possibilities."), _("\
+Use this to limit the number of possibilities considered\n\
+during line completion.  Specifying \"unlimited\" or -1\n\
+disables limiting.  Note that setting either no limit or\n\
+a very large limit can cause pauses during completion."),
+				       NULL, NULL, &setlist, &showlist);
+}
diff --git a/gdb/completer.h b/gdb/completer.h
index bc7ed96..5079343 100644
--- a/gdb/completer.h
+++ b/gdb/completer.h
@@ -63,4 +63,27 @@ extern const char *skip_quoted_chars (const char *, const char *,
 
 extern const char *skip_quoted (const char *);
 
+/* Object to track how many unique completions have been generated.
+   Used to limit the size of generated completion lists.  */
+
+typedef htab_t completion_tracker_t;
+
+/* Create a new completion tracker.  */
+
+extern completion_tracker_t new_completion_tracker (void);
+
+/* Make a cleanup to free a completion tracker.  */
+
+extern struct cleanup *make_cleanup_free_completion_tracker
+		      (completion_tracker_t tracker);
+
+/* Add the completion NAME to the list of generated completions if
+   it is not there already.  Throw TOO_MANY_COMPLETIONS_ERROR if
+   max_completions >= 0 and the number of generated completions is
+   greater than max_completions.  Do nothing if max_completions is
+   negative.  */
+
+extern void maybe_limit_completions (completion_tracker_t tracker,
+				     char *name);
+
 #endif /* defined (COMPLETER_H) */
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 15c2908..2c63581 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -1600,6 +1600,31 @@ means @kbd{@key{META} ?}.  You can type this either by holding down a
 key designated as the @key{META} shift on your keyboard (if there is
 one) while typing @kbd{?}, or as @key{ESC} followed by @kbd{?}.
 
+If the number of possible completions is large, @value{GDBN} will
+print a message rather than displaying the list:
+
+@smallexample
+(@value{GDBP}) b @key{TAB}@key{TAB}
+Too many possibilities.
+(@value{GDBP}) b
+@end smallexample
+
+@noindent
+This behavior can be controlled with the following commands:
+
+@table @code
+@kindex set max-completions
+@item set max-completions @var{limit}
+@itemx set max-completions unlimited
+Set the maximum number of possibilities to be considered during
+completion.  The default value is 200.  Note that setting either
+no limit or a very large limit can cause pauses during completion.
+@kindex show max-completions
+@item show max-completions
+Show the maximum number of possibilities to be considered during
+completion.
+@end table
+
 @cindex quotes in commands
 @cindex completion of quoted strings
 Sometimes the string you need, while logically a ``word'', may contain
diff --git a/gdb/symtab.c b/gdb/symtab.c
index ebb7b8f..a2a1e09 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -60,6 +60,7 @@
 #include "macroscope.h"
 
 #include "parser-defs.h"
+#include "completer.h"
 
 /* Forward declarations for local functions.  */
 
@@ -4256,6 +4257,15 @@ static VEC (char_ptr) *return_val;
       completion_list_add_name \
 	(MSYMBOL_NATURAL_NAME (symbol), (sym_text), (len), (text), (word))
 
+/* Tracker for how many unique completions have been generated.  Used
+   to terminate completion list generation early if the list has grown
+   to a size so large as to be useless.  This helps avoid GDB seeming
+   to lock up in the event the user requests to complete on something
+   vague that necessitates the time consuming expansion of many symbol
+   tables.  */
+
+completion_tracker_t completion_tracker;
+
 /*  Test to see if the symbol specified by SYMNAME (which is already
    demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
    characters.  If so, add it to the current completion list.  */
@@ -4296,6 +4306,12 @@ completion_list_add_name (const char *symname,
       }
 
     VEC_safe_push (char_ptr, return_val, new);
+
+    /* Throw TOO_MANY_COMPLETIONS_ERROR if we've generated too many
+       completions.  We check this after pushing NEW to RETURN_VAL so
+       it's freed by default_make_symbol_completion_list_break_on's
+       cleanup if the exception is thrown.  */
+    maybe_limit_completions (completion_tracker, new);
   }
 }
 
@@ -4532,7 +4548,7 @@ default_make_symbol_completion_list_break_on (const char *text,
   /* Length of sym_text.  */
   int sym_text_len;
   struct add_name_data datum;
-  struct cleanup *back_to;
+  struct cleanup *back_to, *limit_chain;
 
   /* Now look for the symbol we are supposed to complete on.  */
   {
@@ -4606,6 +4622,9 @@ default_make_symbol_completion_list_break_on (const char *text,
   return_val = NULL;
   back_to = make_cleanup (do_free_completion_list, &return_val);
 
+  completion_tracker = new_completion_tracker ();
+  limit_chain = make_cleanup_free_completion_tracker (completion_tracker);
+
   datum.sym_text = sym_text;
   datum.sym_text_len = sym_text_len;
   datum.text = text;
@@ -4717,6 +4736,7 @@ default_make_symbol_completion_list_break_on (const char *text,
       macro_for_each (macro_user_macros, add_macro_name, &datum);
     }
 
+  do_cleanups (limit_chain);
   discard_cleanups (back_to);
   return (return_val);
 }
diff --git a/gdb/testsuite/gdb.base/completion.exp b/gdb/testsuite/gdb.base/completion.exp
index c633a51..9b92a05 100644
--- a/gdb/testsuite/gdb.base/completion.exp
+++ b/gdb/testsuite/gdb.base/completion.exp
@@ -67,6 +67,7 @@ if ![runto_main] then {
 }
 
 set timeout 30
+gdb_test_no_output "set max-completions unlimited"
 
 gdb_test_no_output "complete print values\[0\].x." \
     "field completion with invalid field"
@@ -747,4 +748,36 @@ gdb_test_multiple "" "$test" {
     }
 }
 
+#
+# Completion limiting.
+#
+
+gdb_test_no_output "set max-completions 5"
+
+set test "completion limiting using tab character"
+send_gdb "p\t"
+gdb_test_multiple "" "$test" {
+    -re "^p\\\x07$" {
+	send_gdb "\t"
+	gdb_test_multiple "" "$test" {
+	    -re "Too many possibilities.\r\n\\\x07$gdb_prompt p$" {
+		send_gdb "\n"
+		gdb_test_multiple "" "$test" {
+		    -re "$gdb_prompt $" {
+			pass "$test"
+		    }
+		}
+	    }
+        }
+    }
+}
+
+set test "completion limiting using complete command"
+send_gdb "complete p\n"
+gdb_test_multiple "" "$test" {
+    -re "Too many possibilities.\r\n$gdb_prompt $" {
+	pass "$test"
+    }
+}
+
 return 0
diff --git a/gdb/testsuite/gdb.linespec/ls-errs.exp b/gdb/testsuite/gdb.linespec/ls-errs.exp
index 86056c5..30b4716 100644
--- a/gdb/testsuite/gdb.linespec/ls-errs.exp
+++ b/gdb/testsuite/gdb.linespec/ls-errs.exp
@@ -26,6 +26,9 @@ if {[prepare_for_testing $testfile $exefile $srcfile \
 # Turn off the pending breakpoint queries.
 gdb_test_no_output "set breakpoint pending off"
 
+# Turn off completion limiting
+gdb_test_no_output "set max-completions unlimited"
+
 # We intentionally do not use gdb_breakpoint for these tests.
 
 # Break at 'linespec' and expect the message in ::error_messages indexed by
-- 
1.7.1

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

* Re: [PATCH 3/3 v2] Implement completion limiting
  2014-11-06 10:51 ` [PATCH 3/3 v2] Implement completion limiting Gary Benson
@ 2014-11-06 16:28   ` Eli Zaretskii
  2014-11-21 10:46     ` Gary Benson
  2014-11-24  6:09   ` Doug Evans
  1 sibling, 1 reply; 9+ messages in thread
From: Eli Zaretskii @ 2014-11-06 16:28 UTC (permalink / raw)
  To: Gary Benson; +Cc: gdb-patches

> From: Gary Benson <gbenson@redhat.com>
> Date: Thu,  6 Nov 2014 10:50:46 +0000
> 
> --- a/gdb/NEWS
> +++ b/gdb/NEWS

OK for this part.

> +void
> +_initialize_completer (void)
> +{
> +  add_setshow_zuinteger_unlimited_cmd ("max-completions", no_class,
> +				       &max_completions, _("\
> +Set maximum number of line completion possibilities."), _("\
> +Show maximum number of line completion possibilities."), _("\

I suggest to use "completion candidates" instead of "line completion
possibilities".

> +Use this to limit the number of possibilities considered\n\
> +during line completion.  Specifying \"unlimited\" or -1\n\
          ^^^^^^^^^^^^^^^
And here use just "completion".  "Line completion" is IMO confusing,
since we don't complete "lines".

> +@table @code
> +@kindex set max-completions
> +@item set max-completions @var{limit}
> +@itemx set max-completions unlimited
> +Set the maximum number of possibilities to be considered during
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
"Candidates to be shown" is better.

> +completion.  The default value is 200.  Note that setting either
> +no limit or a very large limit can cause pauses during completion.

Not "cause pauses", but rather "make completion slow", I think.

> +@kindex show max-completions
> +@item show max-completions
> +Show the maximum number of possibilities to be considered during
> +completion.

Suggest the same change here as above.

The documentation part of the patch is OK with those changes.

Thanks.

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

* Re: [PATCH 3/3 v2] Implement completion limiting
  2014-11-06 16:28   ` Eli Zaretskii
@ 2014-11-21 10:46     ` Gary Benson
  0 siblings, 0 replies; 9+ messages in thread
From: Gary Benson @ 2014-11-21 10:46 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: gdb-patches

Eli Zaretskii wrote:
> > From: Gary Benson <gbenson@redhat.com>
> > Date: Thu,  6 Nov 2014 10:50:46 +0000
> > --- a/gdb/NEWS
> > +++ b/gdb/NEWS
> 
> OK for this part.
> 
> > +void
> > +_initialize_completer (void)
> > +{
> > +  add_setshow_zuinteger_unlimited_cmd ("max-completions", no_class,
> > +				       &max_completions, _("\
> > +Set maximum number of line completion possibilities."), _("\
> > +Show maximum number of line completion possibilities."), _("\
> 
> I suggest to use "completion candidates" instead of "line completion
> possibilities".
> 
> > +Use this to limit the number of possibilities considered\n\
> > +during line completion.  Specifying \"unlimited\" or -1\n\
>           ^^^^^^^^^^^^^^^
> And here use just "completion".  "Line completion" is IMO confusing,
> since we don't complete "lines".
> 
> > +@table @code
> > +@kindex set max-completions
> > +@item set max-completions @var{limit}
> > +@itemx set max-completions unlimited
> > +Set the maximum number of possibilities to be considered during
>                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> "Candidates to be shown" is better.
> 
> > +completion.  The default value is 200.  Note that setting either
> > +no limit or a very large limit can cause pauses during completion.
> 
> Not "cause pauses", but rather "make completion slow", I think.
> 
> > +@kindex show max-completions
> > +@item show max-completions
> > +Show the maximum number of possibilities to be considered during
> > +completion.
> 
> Suggest the same change here as above.
> 
> The documentation part of the patch is OK with those changes.
> 
> Thanks.

Thanks Eli.

Cheers,
Gary

-- 
http://gbenson.net/

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

* Re: [PATCH 3/3 v2] Implement completion limiting
  2014-11-06 10:51 ` [PATCH 3/3 v2] Implement completion limiting Gary Benson
  2014-11-06 16:28   ` Eli Zaretskii
@ 2014-11-24  6:09   ` Doug Evans
  1 sibling, 0 replies; 9+ messages in thread
From: Doug Evans @ 2014-11-24  6:09 UTC (permalink / raw)
  To: Gary Benson; +Cc: gdb-patches

Gary Benson <gbenson@redhat.com> writes:
> This commit adds a new exception, TOO_MANY_COMPLETIONS_ERROR, to be
> thrown whenever the completer has generated too many possibilities to
> be useful.  A new user-settable variable, "max_completions", is added
> to control this behaviour.  A top-level completion limit is added to
> complete_line_internal, as the final check to ensure the user never
> sees too many completions.  An additional limit is added to
> default_make_symbol_completion_list_break_on, to halt time-consuming
> symbol table expansions.
>
> gdb/ChangeLog:
>
> 	PR cli/9007
> 	PR cli/11920
> 	PR cli/15548
> 	* common/common-exceptions.h (enum errors)
> 	<TOO_MANY_COMPLETIONS_ERROR>: New value.
> 	* completer.h (completion_tracker_t): New typedef.
> 	(new_completion_tracker): New declaration.
> 	(make_cleanup_free_completion_tracker): Likewise.
> 	(maybe_limit_completions): Likewise.
> 	* completer.c [TUI]: Include tui/tui.h and tui/tui-io.h.
> 	(max_completions): New static variable.
> 	(new_completion_tracker): New function.
> 	(make_cleanup_free_completion_tracker): Likewise.
> 	(maybe_limit_completions): Likewise.
> 	(complete_line_internal): Do not generate any completions if
> 	max_completions = 0.  Limit the number of completions if
> 	max_completions >= 0.
> 	(line_completion_function): Handle TOO_MANY_COMPLETIONS_ERROR.
> 	(_initialize_completer): New declaration and function.
> 	* symtab.c: Include completer.h.
> 	(completion_tracker): New static variable.
> 	(completion_list_add_name): Call maybe_limit_completions.
> 	(default_make_symbol_completion_list_break_on): Maintain
> 	completion_tracker across calls to completion_list_add_name.
> 	* NEWS (New Options): Mention set/show max-completions.

Hi.

I only skimmed this patch set.
I want to give it a more thorough look
once the patch has been updated to apply
to current HEAD.

Thanks very much for working on this!

> +void
> +maybe_limit_completions (completion_tracker_t tracker, char *name)
> +{
> +  if (max_completions < 0)
> +    return;
> +
> +  if (tracker != NULL)
> +    {
> +      void **slot = htab_find_slot (tracker, name, INSERT);
> +
> +      if (*slot != HTAB_EMPTY_ENTRY)
> +	return;
> +
> +      if (htab_elements (tracker) <= max_completions)
> +	{
> +	  *slot = name;
> +	  return;
> +	}
> +    }
> +
> +  throw_error (TOO_MANY_COMPLETIONS_ERROR,
> +	       _("Too many possibilities."));
> +}

This will throw an error if tracker == NULL.
Is that intended?

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

* Re: [PATCH 2/3 v2] Interleave completion list building with symbol table expansion
  2014-11-27 13:16 ` [PATCH 2/3 v2] Interleave completion list building with symbol table expansion Gary Benson
@ 2014-12-05 22:58   ` Doug Evans
  0 siblings, 0 replies; 9+ messages in thread
From: Doug Evans @ 2014-12-05 22:58 UTC (permalink / raw)
  To: Gary Benson; +Cc: gdb-patches, Eli Zaretskii

Gary Benson <gbenson@redhat.com> writes:
> This commit makes default_make_symbol_completion_list_break_on build
> the list of completions as it expands the necessary symbol tables,
> rather than expanding all necessary symbol tables first and then
> building the completion lists second.  This allows for the early
> termination of symbol table expansion if required.
>
> gdb/ChangeLog:
>
> 	* symtab.c (struct add_name_data) <code>: New field.
> 	Updated comments.
> 	(add_symtab_completions): New function.
> 	(symtab_expansion_callback): Likewise.
> 	(default_make_symbol_completion_list_break_on): Set datum.code.
> 	Move minimal symbol scan before calling expand_symtabs_matching.
> 	Scan known primary symtabs for externs and statics before calling
> 	expand_symtabs_matching.  Pass symtab_expansion_callback as
> 	expansion_notify argument to expand_symtabs_matching.  Do not scan
> 	primary symtabs for externs and statics after calling
> 	expand_symtabs_matching.

LGTM

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

* [PATCH 2/3 v2] Interleave completion list building with symbol table expansion
  2014-11-27 13:16 [PATCH 0/3 v2] Abort completion when list becomes large Gary Benson
@ 2014-11-27 13:16 ` Gary Benson
  2014-12-05 22:58   ` Doug Evans
  0 siblings, 1 reply; 9+ messages in thread
From: Gary Benson @ 2014-11-27 13:16 UTC (permalink / raw)
  To: gdb-patches; +Cc: Doug Evans, Eli Zaretskii

This commit makes default_make_symbol_completion_list_break_on build
the list of completions as it expands the necessary symbol tables,
rather than expanding all necessary symbol tables first and then
building the completion lists second.  This allows for the early
termination of symbol table expansion if required.

gdb/ChangeLog:

	* symtab.c (struct add_name_data) <code>: New field.
	Updated comments.
	(add_symtab_completions): New function.
	(symtab_expansion_callback): Likewise.
	(default_make_symbol_completion_list_break_on): Set datum.code.
	Move minimal symbol scan before calling expand_symtabs_matching.
	Scan known primary symtabs for externs and statics before calling
	expand_symtabs_matching.  Pass symtab_expansion_callback as
	expansion_notify argument to expand_symtabs_matching.  Do not scan
	primary symtabs for externs and statics after calling
	expand_symtabs_matching.
---
 gdb/ChangeLog |   14 ++++++++
 gdb/symtab.c  |  105 ++++++++++++++++++++++++++++++++++++---------------------
 2 files changed, 80 insertions(+), 39 deletions(-)

diff --git a/gdb/symtab.c b/gdb/symtab.c
index 3f137e2..a419e62 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -4323,15 +4323,19 @@ completion_list_add_fields (struct symbol *sym, const char *sym_text,
     }
 }
 
-/* Type of the user_data argument passed to add_macro_name or
-   symbol_completion_matcher.  The contents are simply whatever is
-   needed by completion_list_add_name.  */
+/* Type of the user_data argument passed to add_macro_name,
+   symbol_completion_matcher and symtab_expansion_callback.  */
+
 struct add_name_data
 {
+  /* Arguments required by completion_list_add_name.  */
   const char *sym_text;
   int sym_text_len;
   const char *text;
   const char *word;
+
+  /* Extra argument required for add_symtab_completions.  */
+  enum type_code code;
 };
 
 /* A callback used with macro_for_each and macro_for_each_in_scope.
@@ -4359,6 +4363,50 @@ symbol_completion_matcher (const char *name, void *user_data)
   return compare_symbol_name (name, datum->sym_text, datum->sym_text_len);
 }
 
+/* Add matching symbols from SYMTAB to the current completion list.  */
+
+static void
+add_symtab_completions (struct compunit_symtab *cust,
+			const char *sym_text, int sym_text_len,
+			const char *text, const char *word,
+			enum type_code code)
+{
+  struct symbol *sym;
+  const struct block *b;
+  struct block_iterator iter;
+  int i;
+
+  for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
+    {
+      QUIT;
+      b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), i);
+      ALL_BLOCK_SYMBOLS (b, iter, sym)
+	{
+	  if (code == TYPE_CODE_UNDEF
+	      || (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
+		  && TYPE_CODE (SYMBOL_TYPE (sym)) == code))
+	    COMPLETION_LIST_ADD_SYMBOL (sym,
+					sym_text, sym_text_len,
+					text, word);
+	}
+    }
+}
+
+/* Callback to add completions to the current list when symbol tables
+   are expanded during completion list generation.  */
+
+static void
+symtab_expansion_callback (struct compunit_symtab *symtab,
+			   void *user_data)
+{
+  struct add_name_data *datum = (struct add_name_data *) user_data;
+
+  add_symtab_completions (symtab,
+			  datum->sym_text, datum->sym_text_len,
+			  datum->text, datum->word,
+			  datum->code);
+}
+
 VEC (char_ptr) *
 default_make_symbol_completion_list_break_on (const char *text,
 					      const char *word,
@@ -4459,17 +4507,12 @@ default_make_symbol_completion_list_break_on (const char *text,
   datum.sym_text_len = sym_text_len;
   datum.text = text;
   datum.word = word;
-
-  /* Look through the partial symtabs for all symbols which begin
-     by matching SYM_TEXT.  Expand all CUs that you find to the list.
-     The real names will get added by COMPLETION_LIST_ADD_SYMBOL below.  */
-  expand_symtabs_matching (NULL, symbol_completion_matcher, NULL,
-			   ALL_DOMAIN, &datum);
+  datum.code = code;
 
   /* At this point scan through the misc symbol vectors and add each
      symbol you find to the list.  Eventually we want to ignore
      anything that isn't a text symbol (everything else will be
-     handled by the psymtab code above).  */
+     handled by the psymtab code below).  */
 
   if (code == TYPE_CODE_UNDEF)
     {
@@ -4484,6 +4527,19 @@ default_make_symbol_completion_list_break_on (const char *text,
 	}
     }
 
+  /* Add completions for all currently loaded symbol tables.  */
+  ALL_COMPUNITS (objfile, cust)
+    add_symtab_completions (cust, sym_text, sym_text_len, text, word,
+			    code);
+
+  /* Look through the partial symtabs for all symbols which begin
+     by matching SYM_TEXT.  Expand all CUs that you find to the list.
+     symtab_expansion_callback is called for each expanded symtab,
+     causing those symtab's completions to be added to the list too.  */
+  expand_symtabs_matching (NULL, symbol_completion_matcher,
+			   symtab_expansion_callback, ALL_DOMAIN,
+			   &datum);
+
   /* Search upwards from currently selected frame (so that we can
      complete on local vars).  Also catch fields of types defined in
      this places which match our text string.  Only complete on types
@@ -4533,35 +4589,6 @@ default_make_symbol_completion_list_break_on (const char *text,
 	  completion_list_add_fields (sym, sym_text, sym_text_len, text, word);
     }
 
-  /* Go through the symtabs and check the externs and statics for
-     symbols which match.  */
-
-  ALL_COMPUNITS (objfile, cust)
-  {
-    QUIT;
-    b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), GLOBAL_BLOCK);
-    ALL_BLOCK_SYMBOLS (b, iter, sym)
-      {
-	if (code == TYPE_CODE_UNDEF
-	    || (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
-		&& TYPE_CODE (SYMBOL_TYPE (sym)) == code))
-	  COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
-      }
-  }
-
-  ALL_COMPUNITS (objfile, cust)
-  {
-    QUIT;
-    b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), STATIC_BLOCK);
-    ALL_BLOCK_SYMBOLS (b, iter, sym)
-      {
-	if (code == TYPE_CODE_UNDEF
-	    || (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
-		&& TYPE_CODE (SYMBOL_TYPE (sym)) == code))
-	  COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
-      }
-  }
-
   /* Skip macros if we are completing a struct tag -- arguable but
      usually what is expected.  */
   if (current_language->la_macro_expansion == macro_expansion_c
-- 
1.7.1

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

end of thread, other threads:[~2014-12-05 22:58 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-06 10:50 [PATCH 0/3 v2] Limit tab-completion result when list is large Gary Benson
2014-11-06 10:51 ` [PATCH 1/3 v2] Add expansion_notify callback to expand_symtabs_matching Gary Benson
2014-11-06 10:51 ` [PATCH 2/3 v2] Interleave completion list building with symbol table expansion Gary Benson
2014-11-06 10:51 ` [PATCH 3/3 v2] Implement completion limiting Gary Benson
2014-11-06 16:28   ` Eli Zaretskii
2014-11-21 10:46     ` Gary Benson
2014-11-24  6:09   ` Doug Evans
2014-11-27 13:16 [PATCH 0/3 v2] Abort completion when list becomes large Gary Benson
2014-11-27 13:16 ` [PATCH 2/3 v2] Interleave completion list building with symbol table expansion Gary Benson
2014-12-05 22:58   ` Doug Evans

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