+/* An instance of this type is used to represent an solib catchpoint.
+ It includes a "struct breakpoint" as a kind of base class; users
+ downcast to "struct breakpoint *" when needed. A breakpoint is
+ really of this type iff its ops pointer points to
+ CATCH_SOLIB_BREAKPOINT_OPS. */
+
+struct solib_catchpoint
+{
+ /* The base class. */
+ struct breakpoint base;
+
+ /* True for "catch load", false for "catch unload". */
+ unsigned char is_load;
+
+ /* Regular expression to match, if any. COMPILED is only valid when
+ REGEX is non-NULL. */
+ char *regex;
+ regex_t compiled;
+};
+
+static void
+dtor_catch_solib (struct breakpoint *b)
+{
+ struct solib_catchpoint *self = (struct solib_catchpoint *) b;
+
+ if (self->regex)
+ regfree (&self->compiled);
+ xfree (self->regex);
+
+ base_breakpoint_ops.dtor (b);
+}
+
+static int
+insert_catch_solib (struct bp_location *ignore)
+{
+ return 0;
+}
+
+static int
+remove_catch_solib (struct bp_location *ignore)
+{
+ return 0;
+}
+
+static int
+breakpoint_hit_catch_solib (const struct bp_location *bl,
+ struct address_space *aspace,
+ CORE_ADDR bp_addr,
+ const struct target_waitstatus *ws)
+{
+ struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
+ struct breakpoint *other;
+
+ if (ws->kind == TARGET_WAITKIND_LOADED)
+ return 1;
+
+ ALL_BREAKPOINTS (other)
+ {
+ struct bp_location *other_bl;
+
+ if (other == bl->owner)
+ continue;
+
+ if (other->type != bp_shlib_event)
+ continue;
+
+ if (self->base.pspace != NULL && other->pspace != self->base.pspace)
+ continue;
+
+ for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
+ {
+ if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+static void
+check_status_catch_solib (struct bpstats *bs)
+{
+ struct solib_catchpoint *self
+ = (struct solib_catchpoint *) bs->breakpoint_at;
+ int ix;
+
+ if (self->is_load)
+ {
+ struct so_list *iter;
+
+ for (ix = 0;
+ VEC_iterate (so_list_ptr, current_program_space->added_solibs,
+ ix, iter);
+ ++ix)
+ {
+ if (!self->regex
+ || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
+ return;
+ }
+ }
+ else
+ {
+ char *iter;
+
+ for (ix = 0;
+ VEC_iterate (char_ptr, current_program_space->deleted_solibs,
+ ix, iter);
+ ++ix)
+ {
+ if (!self->regex
+ || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
+ return;
+ }
+ }
+
+ bs->stop = 0;
+ bs->print_it = print_it_noop;
+}
+
+static enum print_stop_action
+print_it_catch_solib (bpstat bs)
+{
+ struct breakpoint *b = bs->breakpoint_at;
+ struct ui_out *uiout = current_uiout;
+
+ annotate_catchpoint (b->number);
+ if (b->disposition == disp_del)
+ ui_out_text (uiout, "\nTemporary catchpoint ");
+ else
+ ui_out_text (uiout, "\nCatchpoint ");
+ ui_out_field_int (uiout, "bkptno", b->number);
+ ui_out_text (uiout, "\n");
+ if (ui_out_is_mi_like_p (uiout))
+ ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
+ print_solib_event (1);
+ return PRINT_SRC_AND_LOC;
+}
+
+static void
+print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
+{
+ struct solib_catchpoint *self = (struct solib_catchpoint *) b;
+ struct value_print_options opts;
+ struct ui_out *uiout = current_uiout;
+ char *msg;
+
+ get_user_print_options (&opts);
+ /* Field 4, the address, is omitted (which makes the columns not
+ line up too nicely with the headers, but the effect is relatively
+ readable). */
+ if (opts.addressprint)
+ {
+ annotate_field (4);
+ ui_out_field_skip (uiout, "addr");
+ }
+
+ annotate_field (5);
+ if (self->is_load)
+ {
+ if (self->regex)
+ msg = xstrprintf (_("load of library matching %s"), self->regex);
+ else
+ msg = xstrdup (_("load of library"));
+ }
+ else
+ {
+ if (self->regex)
+ msg = xstrprintf (_("unload of library matching %s"), self->regex);
+ else
+ msg = xstrdup (_("unload of library"));
+ }
+ ui_out_field_string (uiout, "what", msg);
+ xfree (msg);
+}
+
+static void
+print_mention_catch_solib (struct breakpoint *b)
+{
+ struct solib_catchpoint *self = (struct solib_catchpoint *) b;
+
+ printf_filtered (_("Catchpoint %d (%s)"), b->number,
+ self->is_load ? "load" : "unload");
+}
+
+static void
+print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
+{
+ struct solib_catchpoint *self = (struct solib_catchpoint *) b;
+
+ fprintf_unfiltered (fp, "%s %s",
+ b->disposition == disp_del ? "tcatch" : "catch",
+ self->is_load ? "load" : "unload");
+ if (self->regex)
+ fprintf_unfiltered (fp, " %s", self->regex);
+ fprintf_unfiltered (fp, "\n");
+}
+
+static struct breakpoint_ops catch_solib_breakpoint_ops;
+
+/* A helper function that does all the work for "catch load" and
+ "catch unload". */
+
+static void
+catch_load_or_unload (char *arg, int from_tty, int is_load,
+ struct cmd_list_element *command)
+{
+ struct solib_catchpoint *c;
+ struct gdbarch *gdbarch = get_current_arch ();
+ int tempflag;
+ regex_t compiled;
+ struct cleanup *cleanup;
+
+ tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
+
+ if (!arg)
+ arg = "";
+ arg = skip_spaces (arg);
+
+ c = XCNEW (struct solib_catchpoint);
+ cleanup = make_cleanup (xfree, c);
+
+ if (*arg != '\0')
+ {
+ int errcode;
+
+ errcode = regcomp (&c->compiled, arg, REG_NOSUB);
+ if (errcode != 0)
+ {
+ char *err = get_regcomp_error (errcode, &c->compiled);
+
+ make_cleanup (xfree, err);
+ error (_("Invalid regexp (%s): %s"), err, arg);
+ }
+ c->regex = xstrdup (arg);
+ }
+
+ c->is_load = is_load;
+ init_catchpoint (&c->base, gdbarch, tempflag, NULL,
+ &catch_solib_breakpoint_ops);
+
+ discard_cleanups (cleanup);
+ install_breakpoint (0, &c->base, 1);
+}
+
+static void
+catch_load_command_1 (char *arg, int from_tty,
+ struct cmd_list_element *command)
+{
+ catch_load_or_unload (arg, from_tty, 1, command);
+}
+
+static void
+catch_unload_command_1 (char *arg, int from_tty,
+ struct cmd_list_element *command)
+{
+ catch_load_or_unload (arg, from_tty, 0, command);
+}
+