Remove ptid_get_pid
[binutils-gdb.git] / gdb / linespec.c
index 91dabb6117fd211032197ee45964bc765e667de0..2a4189278ef57847b5cfd2046c470b9c77a9d9f8 100644 (file)
@@ -80,9 +80,6 @@ enum class linespec_complete_what
 typedef struct symbol *symbolp;
 DEF_VEC_P (symbolp);
 
-typedef struct type *typep;
-DEF_VEC_P (typep);
-
 /* An address entry is used to ensure that any given location is only
    added to the result a single time.  It holds an address and the
    program space from which the address came.  */
@@ -377,10 +374,9 @@ static void add_matching_symbols_to_info (const char *name,
                                          struct collect_info *info,
                                          struct program_space *pspace);
 
-static void add_all_symbol_names_from_pspace (struct collect_info *info,
-                                             struct program_space *pspace,
-                                             VEC (const_char_ptr) *names,
-                                             enum search_domain search_domain);
+static void add_all_symbol_names_from_pspace
+    (struct collect_info *info, struct program_space *pspace,
+     const std::vector<const char *> &names, enum search_domain search_domain);
 
 static VEC (symtab_ptr) *
   collect_symtabs_from_filename (const char *file,
@@ -1211,11 +1207,11 @@ iterate_over_file_blocks
 
 static void
 find_methods (struct type *t, enum language t_lang, const char *name,
-             VEC (const_char_ptr) **result_names,
-             VEC (typep) **superclasses)
+             std::vector<const char *> *result_names,
+             std::vector<struct type *> *superclasses)
 {
   int ibase;
-  const char *class_name = type_name_no_tag (t);
+  const char *class_name = TYPE_NAME (t);
 
   /* Ignore this class if it doesn't have a name.  This is ugly, but
      unless we figure out how to get the physname without the name of
@@ -1266,14 +1262,14 @@ find_methods (struct type *t, enum language t_lang, const char *name,
                  if (TYPE_FN_FIELD_STUB (f, field_counter))
                    continue;
                  phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
-                 VEC_safe_push (const_char_ptr, *result_names, phys_name);
+                 result_names->push_back (phys_name);
                }
            }
        }
     }
 
   for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
-    VEC_safe_push (typep, *superclasses, TYPE_BASECLASS (t, ibase));
+    superclasses->push_back (TYPE_BASECLASS (t, ibase));
 }
 
 /* Find an instance of the character C in the string S that is outside
@@ -2200,6 +2196,7 @@ create_sals_line_offset (struct linespec_state *self,
 
            if (self->funfirstline)
              skip_prologue_sal (&intermediate_results[i]);
+           intermediate_results[i].symbol = sym;
            add_sal_to_sals (self, &values, &intermediate_results[i],
                             sym ? SYMBOL_NATURAL_NAME (sym) : NULL, 0);
          }
@@ -2228,6 +2225,7 @@ convert_address_location_to_sals (struct linespec_state *self,
   sal.pc = address;
   sal.section = find_pc_overlay (address);
   sal.explicit_pc = 1;
+  sal.symbol = find_pc_sect_containing_function (sal.pc, sal.section);
 
   std::vector<symtab_and_line> sals;
   add_sal_to_sals (self, &sals, &sal, core_addr_to_string (address), 1);
@@ -2262,12 +2260,6 @@ convert_linespec_to_sals (struct linespec_state *state, linespec_p ls)
   else if (ls->function_symbols != NULL || ls->minimal_symbols != NULL)
     {
       /* We have just a bunch of functions and/or methods.  */
-      int i;
-      struct symtab_and_line sal;
-      struct symbol *sym;
-      bound_minimal_symbol_d *elem;
-      struct program_space *pspace;
-
       if (ls->function_symbols != NULL)
        {
          /* Sort symbols so that symbols with the same program space are next
@@ -2276,30 +2268,81 @@ convert_linespec_to_sals (struct linespec_state *state, linespec_p ls)
                 VEC_length (symbolp, ls->function_symbols),
                 sizeof (symbolp), compare_symbols);
 
-         for (i = 0; VEC_iterate (symbolp, ls->function_symbols, i, sym); ++i)
+         struct symbol *sym;
+         for (int i = 0; VEC_iterate (symbolp, ls->function_symbols, i, sym); ++i)
            {
-             pspace = SYMTAB_PSPACE (symbol_symtab (sym));
+             program_space *pspace = SYMTAB_PSPACE (symbol_symtab (sym));
              set_current_program_space (pspace);
-             if (symbol_to_sal (&sal, state->funfirstline, sym)
-                 && maybe_add_address (state->addr_set, pspace, sal.pc))
-               add_sal_to_sals (state, &sals, &sal,
-                                SYMBOL_NATURAL_NAME (sym), 0);
+
+             /* Don't skip to the first line of the function if we
+                had found an ifunc minimal symbol for this function,
+                because that means that this function is an ifunc
+                resolver with the same name as the ifunc itself.  */
+             bool found_ifunc = false;
+
+             if (state->funfirstline
+                  && ls->minimal_symbols != NULL
+                  && SYMBOL_CLASS (sym) == LOC_BLOCK)
+               {
+                 const CORE_ADDR addr
+                   = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
+
+                 bound_minimal_symbol_d *elem;
+                 for (int m = 0;
+                      VEC_iterate (bound_minimal_symbol_d, ls->minimal_symbols,
+                                   m, elem);
+                      ++m)
+                   {
+                     if (MSYMBOL_TYPE (elem->minsym) == mst_text_gnu_ifunc
+                         || MSYMBOL_TYPE (elem->minsym) == mst_data_gnu_ifunc)
+                       {
+                         CORE_ADDR msym_addr = BMSYMBOL_VALUE_ADDRESS (*elem);
+                         if (MSYMBOL_TYPE (elem->minsym) == mst_data_gnu_ifunc)
+                           {
+                             struct gdbarch *gdbarch
+                               = get_objfile_arch (elem->objfile);
+                             msym_addr
+                               = (gdbarch_convert_from_func_ptr_addr
+                                  (gdbarch,
+                                   msym_addr,
+                                   current_top_target ()));
+                           }
+
+                         if (msym_addr == addr)
+                           {
+                             found_ifunc = true;
+                             break;
+                           }
+                       }
+                   }
+               }
+
+             if (!found_ifunc)
+               {
+                 symtab_and_line sal;
+                 if (symbol_to_sal (&sal, state->funfirstline, sym)
+                     && maybe_add_address (state->addr_set, pspace, sal.pc))
+                   add_sal_to_sals (state, &sals, &sal,
+                                    SYMBOL_NATURAL_NAME (sym), 0);
+               }
            }
        }
 
       if (ls->minimal_symbols != NULL)
        {
-         /* Sort minimal symbols by program space, too.  */
+         /* Sort minimal symbols by program space, too  */
          qsort (VEC_address (bound_minimal_symbol_d, ls->minimal_symbols),
                 VEC_length (bound_minimal_symbol_d, ls->minimal_symbols),
                 sizeof (bound_minimal_symbol_d), compare_msymbols);
 
-         for (i = 0;
+         bound_minimal_symbol_d *elem;
+
+         for (int i = 0;
               VEC_iterate (bound_minimal_symbol_d, ls->minimal_symbols,
                            i, elem);
               ++i)
            {
-             pspace = elem->objfile->pspace;
+             program_space *pspace = elem->objfile->pspace;
              set_current_program_space (pspace);
              minsym_found (state, elem->objfile, elem->minsym, &sals);
            }
@@ -3165,16 +3208,10 @@ event_location_to_sals (linespec_parser *parser,
 
        if (addr_string != NULL)
          {
-           char *expr = xstrdup (addr_string);
-           const char *const_expr = expr;
-           struct cleanup *cleanup = make_cleanup (xfree, expr);
-
-           addr = linespec_expression_to_pc (&const_expr);
+           addr = linespec_expression_to_pc (&addr_string);
            if (PARSER_STATE (parser)->canonical != NULL)
              PARSER_STATE (parser)->canonical->location
                = copy_event_location (location);
-
-           do_cleanups (cleanup);
          }
 
        result = convert_address_location_to_sals (PARSER_STATE (parser),
@@ -3258,7 +3295,7 @@ decode_line_full (const struct event_location *location, int flags,
 
   if (select_mode == NULL)
     {
-      if (interp_ui_out (top_level_interpreter ())->is_mi_like_p ())
+      if (top_level_interpreter ()->interp_ui_out ()->is_mi_like_p ())
        select_mode = multiple_symbols_all;
       else
        select_mode = multiple_symbols_select_mode ();
@@ -3404,20 +3441,19 @@ static std::vector<symtab_and_line>
 decode_objc (struct linespec_state *self, linespec_p ls, const char *arg)
 {
   struct collect_info info;
-  VEC (const_char_ptr) *symbol_names = NULL;
+  std::vector<const char *> symbol_names;
   const char *new_argptr;
-  struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
-                                         &symbol_names);
 
   info.state = self;
   info.file_symtabs = NULL;
   VEC_safe_push (symtab_ptr, info.file_symtabs, NULL);
-  make_cleanup (VEC_cleanup (symtab_ptr), &info.file_symtabs);
+  struct cleanup *cleanup = make_cleanup (VEC_cleanup (symtab_ptr),
+                                         &info.file_symtabs);
   info.result.symbols = NULL;
   info.result.minimal_symbols = NULL;
 
   new_argptr = find_imps (arg, &symbol_names);
-  if (VEC_empty (const_char_ptr, symbol_names))
+  if (symbol_names.empty ())
     {
       do_cleanups (cleanup);
       return {};
@@ -3643,46 +3679,34 @@ compare_msymbols (const void *a, const void *b)
 static void
 add_all_symbol_names_from_pspace (struct collect_info *info,
                                  struct program_space *pspace,
-                                 VEC (const_char_ptr) *names,
+                                 const std::vector<const char *> &names,
                                  enum search_domain search_domain)
 {
-  int ix;
-  const char *iter;
-
-  for (ix = 0; VEC_iterate (const_char_ptr, names, ix, iter); ++ix)
+  for (const char *iter : names)
     add_matching_symbols_to_info (iter,
                                  symbol_name_match_type::FULL,
                                  search_domain, info, pspace);
 }
 
 static void
-find_superclass_methods (VEC (typep) *superclasses,
+find_superclass_methods (std::vector<struct type *> &&superclasses,
                         const char *name, enum language name_lang,
-                        VEC (const_char_ptr) **result_names)
+                        std::vector<const char *> *result_names)
 {
-  int old_len = VEC_length (const_char_ptr, *result_names);
-  VEC (typep) *iter_classes;
-  struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
+  size_t old_len = result_names->size ();
 
-  iter_classes = superclasses;
   while (1)
     {
-      VEC (typep) *new_supers = NULL;
-      int ix;
-      struct type *t;
+      std::vector<struct type *> new_supers;
 
-      make_cleanup (VEC_cleanup (typep), &new_supers);
-      for (ix = 0; VEC_iterate (typep, iter_classes, ix, t); ++ix)
+      for (type *t : superclasses)
        find_methods (t, name_lang, name, result_names, &new_supers);
 
-      if (VEC_length (const_char_ptr, *result_names) != old_len
-         || VEC_empty (typep, new_supers))
+      if (result_names->size () != old_len || new_supers.empty ())
        break;
 
-      iter_classes = new_supers;
+      superclasses = std::move (new_supers);
     }
-
-  do_cleanups (cleanup);
 }
 
 /* This finds the method METHOD_NAME in the class CLASS_NAME whose type is
@@ -3696,11 +3720,10 @@ find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs,
             VEC (bound_minimal_symbol_d) **minsyms)
 {
   struct symbol *sym;
-  struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
   int ix;
-  int last_result_len;
-  VEC (typep) *superclass_vec;
-  VEC (const_char_ptr) *result_names;
+  size_t last_result_len;
+  std::vector<struct type *> superclass_vec;
+  std::vector<const char *> result_names;
   struct collect_info info;
 
   /* Sort symbols so that symbols with the same program space are next
@@ -3724,10 +3747,6 @@ find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs,
      those names.  This loop is written in a somewhat funny way
      because we collect data across the program space before deciding
      what to do.  */
-  superclass_vec = NULL;
-  make_cleanup (VEC_cleanup (typep), &superclass_vec);
-  result_names = NULL;
-  make_cleanup (VEC_cleanup (const_char_ptr), &result_names);
   last_result_len = 0;
   for (ix = 0; VEC_iterate (symbolp, sym_classes, ix, sym); ++ix)
     {
@@ -3752,8 +3771,8 @@ find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs,
        {
          /* If we did not find a direct implementation anywhere in
             this program space, consider superclasses.  */
-         if (VEC_length (const_char_ptr, result_names) == last_result_len)
-           find_superclass_methods (superclass_vec, method_name,
+         if (result_names.size () == last_result_len)
+           find_superclass_methods (std::move (superclass_vec), method_name,
                                     SYMBOL_LANGUAGE (sym), &result_names);
 
          /* We have a list of candidate symbol names, so now we
@@ -3762,8 +3781,8 @@ find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs,
          add_all_symbol_names_from_pspace (&info, pspace, result_names,
                                            FUNCTIONS_DOMAIN);
 
-         VEC_truncate (typep, superclass_vec, 0);
-         last_result_len = VEC_length (const_char_ptr, result_names);
+         superclass_vec.clear ();
+         last_result_len = result_names.size ();
        }
     }
 
@@ -3772,7 +3791,6 @@ find_method (struct linespec_state *self, VEC (symtab_ptr) *file_symtabs,
     {
       *symbols = info.result.symbols;
       *minsyms = info.result.minimal_symbols;
-      do_cleanups (cleanup);
       return;
     }
 
@@ -3908,9 +3926,7 @@ find_function_symbols (struct linespec_state *state,
                       VEC (bound_minimal_symbol_d) **minsyms)
 {
   struct collect_info info;
-  VEC (const_char_ptr) *symbol_names = NULL;
-  struct cleanup *cleanup = make_cleanup (VEC_cleanup (const_char_ptr),
-                                         &symbol_names);
+  std::vector<const char *> symbol_names;
 
   info.state = state;
   info.result.symbols = NULL;
@@ -3919,15 +3935,13 @@ find_function_symbols (struct linespec_state *state,
 
   /* Try NAME as an Objective-C selector.  */
   find_imps (name, &symbol_names);
-  if (!VEC_empty (const_char_ptr, symbol_names))
+  if (!symbol_names.empty ())
     add_all_symbol_names_from_pspace (&info, state->search_pspace,
                                      symbol_names, FUNCTIONS_DOMAIN);
   else
     add_matching_symbols_to_info (name, name_match_type, FUNCTIONS_DOMAIN,
                                  &info, state->search_pspace);
 
-  do_cleanups (cleanup);
-
   if (VEC_empty (symbolp, info.result.symbols))
     {
       VEC_free (symbolp, info.result.symbols);
@@ -4277,33 +4291,36 @@ minsym_found (struct linespec_state *self, struct objfile *objfile,
              struct minimal_symbol *msymbol,
              std::vector<symtab_and_line> *result)
 {
-  struct symtab_and_line sal;
+  bool want_start_sal;
 
   CORE_ADDR func_addr;
-  if (msymbol_is_function (objfile, msymbol, &func_addr))
-    {
-      sal = find_pc_sect_line (func_addr, NULL, 0);
+  bool is_function = msymbol_is_function (objfile, msymbol, &func_addr);
 
-      if (self->funfirstline)
-       {
-         if (sal.symtab != NULL
-             && (COMPUNIT_LOCATIONS_VALID (SYMTAB_COMPUNIT (sal.symtab))
-                 || SYMTAB_LANGUAGE (sal.symtab) == language_asm))
-           {
-             struct gdbarch *gdbarch = get_objfile_arch (objfile);
+  if (is_function)
+    {
+      const char *msym_name = MSYMBOL_LINKAGE_NAME (msymbol);
 
-             sal.pc = func_addr;
-             if (gdbarch_skip_entrypoint_p (gdbarch))
-               sal.pc = gdbarch_skip_entrypoint (gdbarch, sal.pc);
-           }
-         else
-           skip_prologue_sal (&sal);
-       }
+      if (MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
+         || MSYMBOL_TYPE (msymbol) == mst_data_gnu_ifunc)
+       want_start_sal = gnu_ifunc_resolve_name (msym_name, &func_addr);
+      else
+       want_start_sal = true;
     }
+
+  symtab_and_line sal;
+
+  if (is_function && want_start_sal)
+    sal = find_function_start_sal (func_addr, NULL, self->funfirstline);
   else
     {
       sal.objfile = objfile;
-      sal.pc = MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
+      sal.msymbol = msymbol;
+      /* Store func_addr, not the minsym's address in case this was an
+        ifunc that hasn't been resolved yet.  */
+      if (is_function)
+       sal.pc = func_addr;
+      else
+       sal.pc = MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
       sal.pspace = current_program_space;
     }
 
@@ -4376,6 +4393,7 @@ add_minsym (struct minimal_symbol *minsym, struct objfile *objfile,
 
   struct bound_minimal_symbol mo = {minsym, objfile};
   msyms->push_back (mo);
+  return;
 }
 
 /* Search for minimal symbols called NAME.  If SEARCH_PSPACE
@@ -4416,6 +4434,7 @@ search_minsyms_for_name (struct collect_info *info,
                                            add_minsym (msym, objfile, nullptr,
                                                        info->state->list_mode,
                                                        &minsyms);
+                                           return false;
                                          });
        }
       }
@@ -4431,6 +4450,7 @@ search_minsyms_for_name (struct collect_info *info,
               {
                 add_minsym (msym, SYMTAB_OBJFILE (symtab), symtab,
                             info->state->list_mode, &minsyms);
+                return false;
               });
        }
     }
@@ -4446,7 +4466,7 @@ search_minsyms_for_name (struct collect_info *info,
         classification as the very first minsym in the list.  */
       classification = classify_mtype (MSYMBOL_TYPE (minsyms[0].minsym));
 
-      for (const struct bound_minimal_symbol &item : minsyms)
+      for (const bound_minimal_symbol &item : minsyms)
        {
          if (classify_mtype (MSYMBOL_TYPE (item.minsym)) != classification)
            break;