diff options
author | Pedro Alves <palves@redhat.com> | 2017-11-29 19:33:23 +0000 |
---|---|---|
committer | Pedro Alves <palves@redhat.com> | 2017-11-29 19:43:48 +0000 |
commit | a20714ff39f621961151d0c204e89062ab2107eb (patch) | |
tree | 6a04775e3cd8c3f617986bb98abc2d3df77f54df /gdb/cp-support.c | |
parent | a207cff2da9f154e4f581b19dcde215593bfccf9 (diff) | |
download | gdb-a20714ff39f621961151d0c204e89062ab2107eb.zip gdb-a20714ff39f621961151d0c204e89062ab2107eb.tar.gz gdb-a20714ff39f621961151d0c204e89062ab2107eb.tar.bz2 |
Make "break foo" find "A::foo", A::B::foo", etc. [C++ and wild matching]
This patch teaches GDB about setting breakpoints in all scopes
(namespaces and classes) by default.
Here's a contrived example:
(gdb) b func<tab>
(anonymous namespace)::A::function() Bn::(anonymous namespace)::B::function() function(int, int)
(anonymous namespace)::B::function() Bn::(anonymous namespace)::function() gdb::(anonymous namespace)::A::function()
(anonymous namespace)::B::function() const Bn::(anonymous namespace)::function(int, int) gdb::(anonymous namespace)::function()
(anonymous namespace)::function() Bn::B::func() gdb::(anonymous namespace)::function(int, int)
(anonymous namespace)::function(int, int) Bn::B::function() gdb::A::func()
A::func() Bn::func() gdb::A::function()
A::function() Bn::function() gdb::func()
B::func() Bn::function(int, int) gdb::function()
B::function() Bn::function(long) gdb::function(int, int)
B::function() const func() gdb::function(long)
B::function_const() const function()
(gdb) b function
Breakpoint 1 at 0x4005ce: function. (26 locations)
(gdb) b B::function<tab>
(anonymous namespace)::B::function() B::function() const Bn::B::function()
(anonymous namespace)::B::function() const B::function_const() const
B::function() Bn::(anonymous namespace)::B::function()
(gdb) b B::function
Breakpoint 1 at 0x40072c: B::function. (6 locations)
To get back the original behavior of interpreting the function name as
a fully-qualified name, you can use the new "-qualified" (or "-q")
option/flag (added by this commit). For example:
(gdb) b B::function
(anonymous namespace)::B::function() B::function() const Bn::B::function()
(anonymous namespace)::B::function() const B::function_const() const
B::function() Bn::(anonymous namespace)::B::function()
vs:
(gdb) b -qualified B::function
B::function() B::function() const B::function_const() const
I've chosen "-qualified" / "-q" because "-f" (for "full" or
"fully-qualified") is already taken for "-function".
Note: the "-qualified" option works with both linespecs and explicit
locations. I.e., these are equivalent:
(gdb) b -q func
(gdb) b -q -f func
and so are these:
(gdb) b -q filename.cc:func
(gdb) b -q -s filename.cc -f func
(gdb) b -s filename.cc -q -f func
(gdb) b -s filename.cc -f func -q
To better understand why I consider wild matching the better default,
consider what happens when we get to the point when _all_ of GDB is
wrapped under "namespace gdb {}". I have a patch series that does
that, and when I started debugging that GDB, I immediately became
frustrated. You'd have to write "b gdb::internal_error", "b
gdb::foo", "b gdb::bar", etc. etc., which gets annoying pretty
quickly. OTOH, consider how this makes it very easy to set
breakpoints in classes wrapped in anonymous namespaces. You just
don't think of them, GDB finds the symbols for you automatically.
(At the Cauldron a couple months ago, several people told me that they
run into a similar issue when debugging other C++ projects. One
example was when debugging LLVM, which puts all its code under the
"llvm" namespace.)
Implementation-wise, what the patch does is:
- makes C++ symbol name hashing only consider the last component of
a symbol name. (so that we can look up symbol names by
last-component name only).
- adds a C++ symbol name matcher for symbol_name_match_type::WILD,
which ignores missing leading specifiers / components.
- adjusts a few preexisting testsuite tests to use "-qualified" when
they mean it.
- adds new testsuite tests.
- adds unit tests.
Grows the gdb.linespec/ tests like this:
-# of expected passes 7823
+# of expected passes 8977
gdb/ChangeLog:
2017-11-29 Pedro Alves <palves@redhat.com>
* NEWS: Mention that breakpoints on C++ functions are now set on
on all namespaces/classes by default, and mention "break
-qualified".
* ax-gdb.c (agent_command_1): Adjust to pass a
symbol_name_match_type to new_linespec_location.
* breakpoint.c (parse_breakpoint_sals): Adjust to
get_linespec_location's return type change.
(strace_marker_create_sals_from_location): Adjust to pass a
symbol_name_match_type to new_linespec_location.
(strace_marker_decode_location): Adjust to get_linespec_location's
return type change.
(strace_command): Adjust to pass a symbol_name_match_type to
new_linespec_location.
(LOCATION_HELP_STRING): Add paragraph about wildmatching, and
mention "-qualified".
* c-lang.c (cplus_language_defn): Install cp_search_name_hash.
* completer.c (explicit_location_match_type::MATCH_QUALIFIED): New
enumerator.
(complete_address_and_linespec_locations): New parameter
'match_type'. Pass it down.
(explicit_options): Add "-qualified".
(collect_explicit_location_matches): Pass the requested match type
to the linespec completers. Handle MATCH_QUALIFIED.
(location_completer): Handle "-qualified" combined with linespecs.
* cp-support.c (cp_search_name_hash): New.
(cp_symbol_name_matches_1): Implement wild matching for C++.
(cp_fq_symbol_name_matches): Reimplement.
(cp_get_symbol_name_matcher): Return different matchers depending
on the lookup name's match type.
(selftests::test_cp_symbol_name_matches): Add wild matching tests.
* cp-support.h (cp_search_name_hash): New declaration.
* dwarf2read.c
(selftests::dw2_expand_symtabs_matching::test_symbols): Add
symbols.
(test_dw2_expand_symtabs_matching_symbol): Add wild matching
tests.
* guile/scm-breakpoint.c (gdbscm_register_breakpoint_x): Adjust to
pass a symbol_name_match_type to new_linespec_location.
* linespec.c (linespec_parse_basic): Lookup function symbols using
the parser's symbol name match type.
(convert_explicit_location_to_linespec): New
symbol_name_match_type parameter. Pass it down to
find_linespec_symbols.
(convert_explicit_location_to_sals): Pass the location's name
match type to convert_explicit_location_to_linespec.
(parse_linespec): New match_type parameter. Save it in the
parser.
(linespec_parser_new): Default to symbol_name_match_type::WILD.
(linespec_complete_function): New symbol_name_match_type
parameter. Use it.
(complete_linespec_component): Pass down the parser's recorded
name match type.
(linespec_complete_label): New symbol_name_match_type parameter.
Use it.
(linespec_complete): New symbol_name_match_type parameter. Save
it in the parser and pass it down. Adjust to
get_linespec_location's prototype change.
(find_function_symbols, find_linespec_symbols): New
symbol_name_match_type parameter. Pass it down instead of
assuming symbol_name_match_type::WILD.
* linespec.h (linespec_complete, linespec_complete_function)
(linespec_complete_label): New symbol_name_match_type parameter.
* location.c (event_location::linespec_location): Now a struct
linespec_location.
(EL_LINESPEC): Adjust.
(initialize_explicit_location): Default to
symbol_name_match_type::WILD.
(new_linespec_location): New symbol_name_match_type parameter.
Record it in the location.
(get_linespec_location): Now returns a struct linespec_location.
(new_explicit_location): Also copy func_name_match_type.
(explicit_to_string_internal)
(string_to_explicit_location): Handle "-qualified".
(copy_event_location): Adjust to LINESPEC_LOCATION type change.
Copy symbol_name_match_type fields.
(event_location_deleter::operator()): Adjust to LINESPEC_LOCATION
type change.
(event_location_to_string): Adjust to LINESPEC_LOCATION type
change. Handle "-qualfied".
(string_to_explicit_location): Handle "-qualified".
(string_to_event_location_basic): New symbol_name_match_type
parameter. Pass it down.
(string_to_event_location): Handle "-qualified".
* location.h (struct linespec_location): New.
(explicit_location::func_name_match_type): New field.
(new_linespec_location): Now returns a const linespec_location *.
(string_to_event_location_basic): New symbol_name_match_type
parameter.
(explicit_completion_info::saw_explicit_location_option): New
field.
* mi/mi-cmd-break.c (mi_cmd_break_insert_1): Adjust to pass a
symbol_name_match_type to new_linespec_location.
* python/py-breakpoint.c (bppy_init): Likewise.
* python/python.c (gdbpy_decode_line): Likewise.
gdb/testsuite/ChangeLog:
2017-11-29 Pedro Alves <palves@redhat.com>
* gdb.base/langs.exp: Use -qualified.
* gdb.cp/meth-typedefs.exp: Use -qualified, and add tests without
it.
* gdb.cp/namespace.exp: Use -qualified.
* gdb.linespec/cpcompletion.exp (overload-2, fqn, fqn-2)
(overload-3, template-overload, template-ret-type, const-overload)
(const-overload-quoted, anon-ns, ambiguous-prefix): New
procedures.
(test_driver): Call them.
* gdb.cp/save-bp-qualified.cc: New.
* gdb.cp/save-bp-qualified.exp: New.
* gdb.linespec/explicit.exp: Test -qualified.
* lib/completion-support.exp (completion::explicit_opts_list): Add
"-qualified".
* lib/gdb.exp (gdb_breakpoint): Handle "qualified".
gdb/doc/ChangeLog:
2017-11-29 Pedro Alves <palves@redhat.com>
* gdb.texinfo (Linespec Locations): Document how "function" is
interpreted in C++ and Ada. Document "-qualified".
(Explicit Locations): Document how "-function" is interpreted in
C++ and Ada. Document "-qualified".
Diffstat (limited to 'gdb/cp-support.c')
-rw-r--r-- | gdb/cp-support.c | 163 |
1 files changed, 153 insertions, 10 deletions
diff --git a/gdb/cp-support.c b/gdb/cp-support.c index 6c6825b..172d821 100644 --- a/gdb/cp-support.c +++ b/gdb/cp-support.c @@ -1615,11 +1615,48 @@ gdb_sniff_from_mangled_name (const char *mangled, char **demangled) return *demangled != NULL; } -/* C++ symbol_name_matcher_ftype implementation. */ +/* See cp-support.h. */ + +unsigned int +cp_search_name_hash (const char *search_name) +{ + /* cp_entire_prefix_len assumes a fully-qualified name with no + leading "::". */ + if (startswith (search_name, "::")) + search_name += 2; + + unsigned int prefix_len = cp_entire_prefix_len (search_name); + if (prefix_len != 0) + search_name += prefix_len + 2; + + return default_search_name_hash (search_name); +} + +/* Helper for cp_symbol_name_matches (i.e., symbol_name_matcher_ftype + implementation for symbol_name_match_type::WILD matching). Split + to a separate function for unit-testing convenience. + + If SYMBOL_SEARCH_NAME has more scopes than LOOKUP_NAME, we try to + match ignoring the extra leading scopes of SYMBOL_SEARCH_NAME. + This allows conveniently setting breakpoints on functions/methods + inside any namespace/class without specifying the fully-qualified + name. + + E.g., these match: -/* Helper for cp_fq_symbol_name_matches (i.e., - symbol_name_matcher_ftype implementation). Split to a separate - function for unit-testing convenience. + [symbol search name] [lookup name] + foo::bar::func foo::bar::func + foo::bar::func bar::func + foo::bar::func func + + While these don't: + + [symbol search name] [lookup name] + foo::zbar::func bar::func + foo::bar::func foo::func + + See more examples in the test_cp_symbol_name_matches selftest + function below. See symbol_name_matcher_ftype for description of SYMBOL_SEARCH_NAME and COMP_MATCH_RES. @@ -1636,8 +1673,68 @@ cp_symbol_name_matches_1 (const char *symbol_search_name, strncmp_iw_mode mode, completion_match_result *comp_match_res) { + const char *sname = symbol_search_name; + + while (true) + { + if (strncmp_iw_with_mode (sname, lookup_name, lookup_name_len, + mode, language_cplus) == 0) + { + if (comp_match_res != NULL) + { + /* Note here we set different MATCH and MATCH_FOR_LCD + strings. This is because with + + (gdb) b push_bac[TAB] + + we want the completion matches to list + + std::vector<int>::push_back(...) + std::vector<char>::push_back(...) + + etc., which are SYMBOL_SEARCH_NAMEs, while we want + the input line to auto-complete to + + (gdb) push_back(...) + + which is SNAME, not to + + (gdb) std::vector< + + which would be the regular common prefix between all + the matches otherwise. */ + comp_match_res->set_match (symbol_search_name, sname); + } + return true; + } + + unsigned int len = cp_find_first_component (sname); + + if (sname[len] == '\0') + return false; + + gdb_assert (sname[len] == ':'); + /* Skip the '::'. */ + sname += len + 2; + } +} + +/* C++ symbol_name_matcher_ftype implementation. */ + +static bool +cp_fq_symbol_name_matches (const char *symbol_search_name, + const lookup_name_info &lookup_name, + completion_match_result *comp_match_res) +{ + /* Get the demangled name. */ + const std::string &name = lookup_name.cplus ().lookup_name (); + + strncmp_iw_mode mode = (lookup_name.completion_mode () + ? strncmp_iw_mode::NORMAL + : strncmp_iw_mode::MATCH_PARAMS); + if (strncmp_iw_with_mode (symbol_search_name, - lookup_name, lookup_name_len, + name.c_str (), name.size (), mode, language_cplus) == 0) { if (comp_match_res != NULL) @@ -1648,12 +1745,13 @@ cp_symbol_name_matches_1 (const char *symbol_search_name, return false; } -/* C++ symbol_name_matcher_ftype implementation. */ +/* C++ symbol_name_matcher_ftype implementation for wild matches. + Defers work to cp_symbol_name_matches_1. */ static bool -cp_fq_symbol_name_matches (const char *symbol_search_name, - const lookup_name_info &lookup_name, - completion_match_result *comp_match_res) +cp_symbol_name_matches (const char *symbol_search_name, + const lookup_name_info &lookup_name, + completion_match_result *comp_match_res) { /* Get the demangled name. */ const std::string &name = lookup_name.cplus ().lookup_name (); @@ -1672,7 +1770,16 @@ cp_fq_symbol_name_matches (const char *symbol_search_name, symbol_name_matcher_ftype * cp_get_symbol_name_matcher (const lookup_name_info &lookup_name) { - return cp_fq_symbol_name_matches; + switch (lookup_name.match_type ()) + { + case symbol_name_match_type::FULL: + case symbol_name_match_type::EXPRESSION: + return cp_fq_symbol_name_matches; + case symbol_name_match_type::WILD: + return cp_symbol_name_matches; + } + + gdb_assert_not_reached (""); } #if GDB_SELF_TEST @@ -1807,6 +1914,42 @@ test_cp_symbol_name_matches () CHECK_MATCH_C ("abc::def::ghi()", "abc::def::ghi ( )"); CHECK_MATCH_C ("function()", "function()"); CHECK_MATCH_C ("bar::function()", "bar::function()"); + + /* Wild matching tests follow. */ + + /* Tests matching symbols in some scope. */ + CHECK_MATCH_C ("foo::function()", "function"); + CHECK_MATCH_C ("foo::function(int)", "function"); + CHECK_MATCH_C ("foo::bar::function()", "function"); + CHECK_MATCH_C ("bar::function()", "bar::function"); + CHECK_MATCH_C ("foo::bar::function()", "bar::function"); + CHECK_MATCH_C ("foo::bar::function(int)", "bar::function"); + + /* Same, with parameters in the lookup name. */ + CHECK_MATCH_C ("foo::function()", "function()"); + CHECK_MATCH_C ("foo::bar::function()", "function()"); + CHECK_MATCH_C ("foo::function(int)", "function(int)"); + CHECK_MATCH_C ("foo::function()", "foo::function()"); + CHECK_MATCH_C ("foo::bar::function()", "bar::function()"); + CHECK_MATCH_C ("foo::bar::function(int)", "bar::function(int)"); + CHECK_MATCH_C ("bar::function()", "bar::function()"); + + CHECK_NOT_MATCH_C ("foo::bar::function(int)", "bar::function()"); + + CHECK_MATCH_C ("(anonymous namespace)::bar::function(int)", + "bar::function(int)"); + CHECK_MATCH_C ("foo::(anonymous namespace)::bar::function(int)", + "function(int)"); + + /* Lookup scope wider than symbol scope, should not match. */ + CHECK_NOT_MATCH_C ("function()", "bar::function"); + CHECK_NOT_MATCH_C ("function()", "bar::function()"); + + /* Explicit global scope doesn't match. */ + CHECK_NOT_MATCH_C ("foo::function()", "::function"); + CHECK_NOT_MATCH_C ("foo::function()", "::function()"); + CHECK_NOT_MATCH_C ("foo::function(int)", "::function()"); + CHECK_NOT_MATCH_C ("foo::function(int)", "::function(int)"); } /* If non-NULL, return STR wrapped in quotes. Otherwise, return a |