+ lookup_probe.address = address;
+ lookup.probe = &lookup_probe;
+ slot = htab_find_slot (info->probes_table, &lookup, NO_INSERT);
+
+ if (slot == NULL)
+ return NULL;
+
+ return (struct probe_and_action *) *slot;
+}
+
+/* Decide what action to take when the specified solib event probe is
+ hit. */
+
+static enum probe_action
+solib_event_probe_action (struct probe_and_action *pa)
+{
+ enum probe_action action;
+ unsigned probe_argc;
+ struct frame_info *frame = get_current_frame ();
+
+ action = pa->action;
+ if (action == DO_NOTHING || action == PROBES_INTERFACE_FAILED)
+ return action;
+
+ gdb_assert (action == FULL_RELOAD || action == UPDATE_OR_RELOAD);
+
+ /* Check that an appropriate number of arguments has been supplied.
+ We expect:
+ arg0: Lmid_t lmid (mandatory)
+ arg1: struct r_debug *debug_base (mandatory)
+ arg2: struct link_map *new (optional, for incremental updates) */
+ probe_argc = get_probe_argument_count (pa->probe, frame);
+ if (probe_argc == 2)
+ action = FULL_RELOAD;
+ else if (probe_argc < 2)
+ action = PROBES_INTERFACE_FAILED;
+
+ return action;
+}
+
+/* Populate the shared object list by reading the entire list of
+ shared objects from the inferior. Handle special cases relating
+ to the first elements of the list. Returns nonzero on success. */
+
+static int
+solist_update_full (struct svr4_info *info)
+{
+ free_solib_list (info);
+ info->solib_list = svr4_current_sos_direct (info);
+
+ return 1;
+}
+
+/* Update the shared object list starting from the link-map entry
+ passed by the linker in the probe's third argument. Returns
+ nonzero if the list was successfully updated, or zero to indicate
+ failure. */
+
+static int
+solist_update_incremental (struct svr4_info *info, CORE_ADDR lm)
+{
+ struct so_list *tail;
+ CORE_ADDR prev_lm;
+
+ /* svr4_current_sos_direct contains logic to handle a number of
+ special cases relating to the first elements of the list. To
+ avoid duplicating this logic we defer to solist_update_full
+ if the list is empty. */
+ if (info->solib_list == NULL)
+ return 0;
+
+ /* Fall back to a full update if we are using a remote target
+ that does not support incremental transfers. */
+ if (info->using_xfer && !target_augmented_libraries_svr4_read ())
+ return 0;
+
+ /* Walk to the end of the list. */
+ for (tail = info->solib_list; tail->next != NULL; tail = tail->next)
+ /* Nothing. */;
+ prev_lm = tail->lm_info->lm_addr;
+
+ /* Read the new objects. */
+ if (info->using_xfer)
+ {
+ struct svr4_library_list library_list;
+ char annex[64];
+
+ xsnprintf (annex, sizeof (annex), "start=%s;prev=%s",
+ phex_nz (lm, sizeof (lm)),
+ phex_nz (prev_lm, sizeof (prev_lm)));
+ if (!svr4_current_sos_via_xfer_libraries (&library_list, annex))
+ return 0;
+
+ tail->next = library_list.head;
+ }
+ else
+ {
+ struct so_list **link = &tail->next;
+
+ /* IGNORE_FIRST may safely be set to zero here because the
+ above check and deferral to solist_update_full ensures
+ that this call to svr4_read_so_list will never see the
+ first element. */
+ if (!svr4_read_so_list (lm, prev_lm, &link, 0))
+ return 0;
+ }
+
+ return 1;
+}
+
+/* Disable the probes-based linker interface and revert to the
+ original interface. We don't reset the breakpoints as the
+ ones set up for the probes-based interface are adequate. */
+
+static void
+disable_probes_interface_cleanup (void *arg)
+{
+ struct svr4_info *info = get_svr4_info ();
+
+ warning (_("Probes-based dynamic linker interface failed.\n"
+ "Reverting to original interface.\n"));
+
+ free_probes_table (info);
+ free_solib_list (info);
+}
+
+/* Update the solib list as appropriate when using the
+ probes-based linker interface. Do nothing if using the
+ standard interface. */
+
+static void
+svr4_handle_solib_event (void)
+{
+ struct svr4_info *info = get_svr4_info ();
+ struct probe_and_action *pa;
+ enum probe_action action;
+ struct cleanup *old_chain, *usm_chain;
+ struct value *val;
+ CORE_ADDR pc, debug_base, lm = 0;
+ int is_initial_ns;
+ struct frame_info *frame = get_current_frame ();
+
+ /* Do nothing if not using the probes interface. */
+ if (info->probes_table == NULL)
+ return;
+
+ /* If anything goes wrong we revert to the original linker
+ interface. */
+ old_chain = make_cleanup (disable_probes_interface_cleanup, NULL);
+
+ pc = regcache_read_pc (get_current_regcache ());
+ pa = solib_event_probe_at (info, pc);
+ if (pa == NULL)
+ {
+ do_cleanups (old_chain);
+ return;
+ }
+
+ action = solib_event_probe_action (pa);
+ if (action == PROBES_INTERFACE_FAILED)
+ {
+ do_cleanups (old_chain);
+ return;
+ }
+
+ if (action == DO_NOTHING)
+ {
+ discard_cleanups (old_chain);
+ return;
+ }
+
+ /* evaluate_probe_argument looks up symbols in the dynamic linker
+ using find_pc_section. find_pc_section is accelerated by a cache
+ called the section map. The section map is invalidated every
+ time a shared library is loaded or unloaded, and if the inferior
+ is generating a lot of shared library events then the section map
+ will be updated every time svr4_handle_solib_event is called.
+ We called find_pc_section in svr4_create_solib_event_breakpoints,
+ so we can guarantee that the dynamic linker's sections are in the
+ section map. We can therefore inhibit section map updates across
+ these calls to evaluate_probe_argument and save a lot of time. */
+ inhibit_section_map_updates (current_program_space);
+ usm_chain = make_cleanup (resume_section_map_updates_cleanup,
+ current_program_space);
+
+ val = evaluate_probe_argument (pa->probe, 1, frame);
+ if (val == NULL)
+ {
+ do_cleanups (old_chain);
+ return;
+ }
+
+ debug_base = value_as_address (val);
+ if (debug_base == 0)
+ {
+ do_cleanups (old_chain);
+ return;
+ }
+
+ /* Always locate the debug struct, in case it moved. */
+ info->debug_base = 0;
+ if (locate_base (info) == 0)
+ {
+ do_cleanups (old_chain);
+ return;
+ }
+
+ /* GDB does not currently support libraries loaded via dlmopen
+ into namespaces other than the initial one. We must ignore
+ any namespace other than the initial namespace here until
+ support for this is added to GDB. */
+ if (debug_base != info->debug_base)
+ action = DO_NOTHING;
+
+ if (action == UPDATE_OR_RELOAD)
+ {
+ val = evaluate_probe_argument (pa->probe, 2, frame);
+ if (val != NULL)
+ lm = value_as_address (val);
+
+ if (lm == 0)
+ action = FULL_RELOAD;
+ }
+
+ /* Resume section map updates. */
+ do_cleanups (usm_chain);
+
+ if (action == UPDATE_OR_RELOAD)
+ {
+ if (!solist_update_incremental (info, lm))
+ action = FULL_RELOAD;
+ }
+
+ if (action == FULL_RELOAD)
+ {
+ if (!solist_update_full (info))
+ {
+ do_cleanups (old_chain);
+ return;
+ }
+ }
+
+ discard_cleanups (old_chain);
+}
+
+/* Helper function for svr4_update_solib_event_breakpoints. */
+
+static int
+svr4_update_solib_event_breakpoint (struct breakpoint *b, void *arg)
+{
+ struct bp_location *loc;
+
+ if (b->type != bp_shlib_event)
+ {
+ /* Continue iterating. */
+ return 0;
+ }
+
+ for (loc = b->loc; loc != NULL; loc = loc->next)
+ {
+ struct svr4_info *info;
+ struct probe_and_action *pa;
+
+ info = program_space_data (loc->pspace, solib_svr4_pspace_data);
+ if (info == NULL || info->probes_table == NULL)
+ continue;
+
+ pa = solib_event_probe_at (info, loc->address);
+ if (pa == NULL)
+ continue;
+
+ if (pa->action == DO_NOTHING)
+ {
+ if (b->enable_state == bp_disabled && stop_on_solib_events)
+ enable_breakpoint (b);
+ else if (b->enable_state == bp_enabled && !stop_on_solib_events)
+ disable_breakpoint (b);
+ }
+
+ break;
+ }
+
+ /* Continue iterating. */
+ return 0;
+}
+
+/* Enable or disable optional solib event breakpoints as appropriate.
+ Called whenever stop_on_solib_events is changed. */
+
+static void
+svr4_update_solib_event_breakpoints (void)
+{
+ iterate_over_breakpoints (svr4_update_solib_event_breakpoint, NULL);
+}
+
+/* Create and register solib event breakpoints. PROBES is an array
+ of NUM_PROBES elements, each of which is vector of probes. A
+ solib event breakpoint will be created and registered for each
+ probe. */
+
+static void
+svr4_create_probe_breakpoints (struct gdbarch *gdbarch,
+ VEC (probe_p) **probes)
+{
+ int i;
+
+ for (i = 0; i < NUM_PROBES; i++)
+ {
+ enum probe_action action = probe_info[i].action;
+ struct probe *probe;
+ int ix;
+
+ for (ix = 0;
+ VEC_iterate (probe_p, probes[i], ix, probe);
+ ++ix)
+ {
+ create_solib_event_breakpoint (gdbarch, probe->address);
+ register_solib_event_probe (probe, action);
+ }
+ }
+
+ svr4_update_solib_event_breakpoints ();
+}
+
+/* Both the SunOS and the SVR4 dynamic linkers call a marker function
+ before and after mapping and unmapping shared libraries. The sole
+ purpose of this method is to allow debuggers to set a breakpoint so
+ they can track these changes.
+
+ Some versions of the glibc dynamic linker contain named probes
+ to allow more fine grained stopping. Given the address of the
+ original marker function, this function attempts to find these
+ probes, and if found, sets breakpoints on those instead. If the
+ probes aren't found, a single breakpoint is set on the original
+ marker function. */
+
+static void
+svr4_create_solib_event_breakpoints (struct gdbarch *gdbarch,
+ CORE_ADDR address)
+{
+ struct obj_section *os;
+
+ os = find_pc_section (address);
+ if (os != NULL)
+ {
+ int with_prefix;
+
+ for (with_prefix = 0; with_prefix <= 1; with_prefix++)
+ {
+ VEC (probe_p) *probes[NUM_PROBES];
+ int all_probes_found = 1;
+ int checked_can_use_probe_arguments = 0;
+ int i;
+
+ memset (probes, 0, sizeof (probes));
+ for (i = 0; i < NUM_PROBES; i++)
+ {
+ const char *name = probe_info[i].name;
+ struct probe *p;
+ char buf[32];
+
+ /* Fedora 17 and Red Hat Enterprise Linux 6.2-6.4
+ shipped with an early version of the probes code in
+ which the probes' names were prefixed with "rtld_"
+ and the "map_failed" probe did not exist. The
+ locations of the probes are otherwise the same, so
+ we check for probes with prefixed names if probes
+ with unprefixed names are not present. */
+ if (with_prefix)
+ {
+ xsnprintf (buf, sizeof (buf), "rtld_%s", name);
+ name = buf;
+ }
+
+ probes[i] = find_probes_in_objfile (os->objfile, "rtld", name);
+
+ /* The "map_failed" probe did not exist in early
+ versions of the probes code in which the probes'
+ names were prefixed with "rtld_". */
+ if (strcmp (name, "rtld_map_failed") == 0)
+ continue;
+
+ if (VEC_empty (probe_p, probes[i]))
+ {
+ all_probes_found = 0;
+ break;
+ }
+
+ /* Ensure probe arguments can be evaluated. */
+ if (!checked_can_use_probe_arguments)
+ {
+ p = VEC_index (probe_p, probes[i], 0);
+ if (!can_evaluate_probe_arguments (p))
+ {
+ all_probes_found = 0;
+ break;
+ }
+ checked_can_use_probe_arguments = 1;
+ }
+ }
+
+ if (all_probes_found)
+ svr4_create_probe_breakpoints (gdbarch, probes);
+
+ for (i = 0; i < NUM_PROBES; i++)
+ VEC_free (probe_p, probes[i]);
+
+ if (all_probes_found)
+ return;
+ }
+ }
+
+ create_solib_event_breakpoint (gdbarch, address);
+}
+
+/* Helper function for gdb_bfd_lookup_symbol. */
+
+static int
+cmp_name_and_sec_flags (asymbol *sym, void *data)
+{
+ return (strcmp (sym->name, (const char *) data) == 0
+ && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0);
+}
+/* Arrange for dynamic linker to hit breakpoint.