Home | History | Annotate | Line # | Download | only in gdb
      1 /* Everything about exec catchpoints, for GDB.
      2 
      3    Copyright (C) 1986-2024 Free Software Foundation, Inc.
      4 
      5    This file is part of GDB.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19 
     20 
     21 #include "annotate.h"
     22 #include "arch-utils.h"
     23 #include "breakpoint.h"
     24 #include "cli/cli-decode.h"
     25 #include "inferior.h"
     26 #include "mi/mi-common.h"
     27 #include "target.h"
     28 #include "valprint.h"
     29 
     30 /* Exec catchpoints.  */
     31 
     32 /* An instance of this type is used to represent an exec catchpoint.
     33    A breakpoint is really of this type iff its ops pointer points to
     34    CATCH_EXEC_BREAKPOINT_OPS.  */
     35 
     36 struct exec_catchpoint : public catchpoint
     37 {
     38   exec_catchpoint (struct gdbarch *gdbarch, bool temp, const char *cond_string)
     39     : catchpoint (gdbarch, temp, cond_string)
     40   {
     41   }
     42 
     43   int insert_location (struct bp_location *) override;
     44   int remove_location (struct bp_location *,
     45 		       enum remove_bp_reason reason) override;
     46   int breakpoint_hit (const struct bp_location *bl,
     47 		      const address_space *aspace,
     48 		      CORE_ADDR bp_addr,
     49 		      const target_waitstatus &ws) override;
     50   enum print_stop_action print_it (const bpstat *bs) const override;
     51   bool print_one (const bp_location **) const override;
     52   void print_mention () const override;
     53   void print_recreate (struct ui_file *fp) const override;
     54 
     55   /* Filename of a program whose exec triggered this catchpoint.
     56      This field is only valid immediately after this catchpoint has
     57      triggered.  */
     58   gdb::unique_xmalloc_ptr<char> exec_pathname;
     59 };
     60 
     61 int
     62 exec_catchpoint::insert_location (struct bp_location *bl)
     63 {
     64   return target_insert_exec_catchpoint (inferior_ptid.pid ());
     65 }
     66 
     67 int
     68 exec_catchpoint::remove_location (struct bp_location *bl,
     69 				  enum remove_bp_reason reason)
     70 {
     71   return target_remove_exec_catchpoint (inferior_ptid.pid ());
     72 }
     73 
     74 int
     75 exec_catchpoint::breakpoint_hit (const struct bp_location *bl,
     76 				 const address_space *aspace,
     77 				 CORE_ADDR bp_addr,
     78 				 const target_waitstatus &ws)
     79 {
     80   if (ws.kind () != TARGET_WAITKIND_EXECD)
     81     return 0;
     82 
     83   exec_pathname = make_unique_xstrdup (ws.execd_pathname ());
     84   return 1;
     85 }
     86 
     87 enum print_stop_action
     88 exec_catchpoint::print_it (const bpstat *bs) const
     89 {
     90   struct ui_out *uiout = current_uiout;
     91 
     92   annotate_catchpoint (number);
     93   maybe_print_thread_hit_breakpoint (uiout);
     94   if (disposition == disp_del)
     95     uiout->text ("Temporary catchpoint ");
     96   else
     97     uiout->text ("Catchpoint ");
     98   if (uiout->is_mi_like_p ())
     99     {
    100       uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
    101       uiout->field_string ("disp", bpdisp_text (disposition));
    102     }
    103   uiout->field_signed ("bkptno", number);
    104   uiout->text (" (exec'd ");
    105   uiout->field_string ("new-exec", exec_pathname.get ());
    106   uiout->text ("), ");
    107 
    108   return PRINT_SRC_AND_LOC;
    109 }
    110 
    111 bool
    112 exec_catchpoint::print_one (const bp_location **last_loc) const
    113 {
    114   struct value_print_options opts;
    115   struct ui_out *uiout = current_uiout;
    116 
    117   get_user_print_options (&opts);
    118 
    119   /* Field 4, the address, is omitted (which makes the columns
    120      not line up too nicely with the headers, but the effect
    121      is relatively readable).  */
    122   if (opts.addressprint)
    123     uiout->field_skip ("addr");
    124   annotate_field (5);
    125   uiout->text ("exec");
    126   if (exec_pathname != NULL)
    127     {
    128       uiout->text (", program \"");
    129       uiout->field_string ("what", exec_pathname.get ());
    130       uiout->text ("\" ");
    131     }
    132 
    133   if (uiout->is_mi_like_p ())
    134     uiout->field_string ("catch-type", "exec");
    135 
    136   return true;
    137 }
    138 
    139 void
    140 exec_catchpoint::print_mention () const
    141 {
    142   gdb_printf (_("Catchpoint %d (exec)"), number);
    143 }
    144 
    145 /* Implement the "print_recreate" method for exec catchpoints.  */
    146 
    147 void
    148 exec_catchpoint::print_recreate (struct ui_file *fp) const
    149 {
    150   gdb_printf (fp, "catch exec");
    151   print_recreate_thread (fp);
    152 }
    153 
    154 /* This function attempts to parse an optional "if <cond>" clause
    155    from the arg string.  If one is not found, it returns NULL.
    156 
    157    Else, it returns a pointer to the condition string.  (It does not
    158    attempt to evaluate the string against a particular block.)  And,
    159    it updates arg to point to the first character following the parsed
    160    if clause in the arg string.  */
    161 
    162 const char *
    163 ep_parse_optional_if_clause (const char **arg)
    164 {
    165   const char *cond_string;
    166 
    167   if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((unsigned char)(*arg)[2]))
    168     return NULL;
    169 
    170   /* Skip the "if" keyword.  */
    171   (*arg) += 2;
    172 
    173   /* Skip any extra leading whitespace, and record the start of the
    174      condition string.  */
    175   *arg = skip_spaces (*arg);
    176   cond_string = *arg;
    177 
    178   /* Assume that the condition occupies the remainder of the arg
    179      string.  */
    180   (*arg) += strlen (cond_string);
    181 
    182   return cond_string;
    183 }
    184 
    185 /* Commands to deal with catching events, such as signals, exceptions,
    186    process start/exit, etc.  */
    187 
    188 static void
    189 catch_exec_command_1 (const char *arg, int from_tty,
    190 		      struct cmd_list_element *command)
    191 {
    192   struct gdbarch *gdbarch = get_current_arch ();
    193   const char *cond_string = NULL;
    194   bool temp = command->context () == CATCH_TEMPORARY;
    195 
    196   if (!arg)
    197     arg = "";
    198   arg = skip_spaces (arg);
    199 
    200   /* The allowed syntax is:
    201      catch exec
    202      catch exec if <cond>
    203 
    204      First, check if there's an if clause.  */
    205   cond_string = ep_parse_optional_if_clause (&arg);
    206 
    207   if ((*arg != '\0') && !isspace ((unsigned char)*arg))
    208     error (_("Junk at end of arguments."));
    209 
    210   std::unique_ptr<exec_catchpoint> c
    211     (new exec_catchpoint (gdbarch, temp, cond_string));
    212 
    213   install_breakpoint (0, std::move (c), 1);
    214 }
    215 
    216 void _initialize_break_catch_exec ();
    217 void
    218 _initialize_break_catch_exec ()
    219 {
    220   add_catch_command ("exec", _("Catch calls to exec."),
    221 		     catch_exec_command_1,
    222 		     NULL,
    223 		     CATCH_PERMANENT,
    224 		     CATCH_TEMPORARY);
    225 }
    226