[2/4] Use gdb::checked_static_cast for tracepoints

Message ID 20230915-watchpoint-casts-v1-2-a4ff35c9644e@adacore.com
State New
Headers
Series Use checked_static_cast for breakpoint casts |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

Tom Tromey Sept. 15, 2023, 6:36 p.m. UTC
  This replaces some casts to 'tracepoint *' with checked_static_cast.
Some functions are changed to accept a 'tracepoint *' now, for better
type safety.
---
 gdb/breakpoint.c       | 33 +++++++++++++++++++--------------
 gdb/mi/mi-cmd-break.c  | 10 +++++++---
 gdb/remote.c           | 11 +++++------
 gdb/target-debug.h     |  2 ++
 gdb/target-delegates.c | 16 ++++++++--------
 gdb/target.c           |  2 +-
 gdb/target.h           |  4 ++--
 gdb/tracefile-tfile.c  |  4 ++--
 gdb/tracepoint.c       | 28 +++++++++++++++++-----------
 gdb/tracepoint.h       |  4 ++--
 10 files changed, 65 insertions(+), 49 deletions(-)
  

Patch

diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index c0d86f38c73..134ad76ce9a 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -1433,7 +1433,7 @@  validate_commands_for_breakpoint (struct breakpoint *b,
 {
   if (is_tracepoint (b))
     {
-      struct tracepoint *t = (struct tracepoint *) b;
+      struct tracepoint *t = gdb::checked_static_cast<struct tracepoint *> (b);
       struct command_line *c;
       struct command_line *while_stepping = 0;
 
@@ -1471,7 +1471,7 @@  validate_commands_for_breakpoint (struct breakpoint *b,
 		while_stepping = c;
 	    }
 
-	  validate_actionline (c->line, b);
+	  validate_actionline (c->line, t);
 	}
       if (while_stepping)
 	{
@@ -1645,7 +1645,9 @@  commands_command_1 (const char *arg, int from_tty,
 
 	       auto do_validate = [=] (const char *line)
 				  {
-				    validate_actionline (line, b);
+				    struct tracepoint *t
+				      = gdb::checked_static_cast<struct tracepoint *> (b);
+				    validate_actionline (line, t);
 				  };
 	       gdb::function_view<void (const char *)> validator;
 	       if (is_tracepoint (b))
@@ -6762,7 +6764,7 @@  print_one_breakpoint_location (struct breakpoint *b,
 
   if (!part_of_multiple && is_tracepoint (b))
     {
-      struct tracepoint *tp = (struct tracepoint *) b;
+      struct tracepoint *tp = gdb::checked_static_cast<struct tracepoint *> (b);
 
       if (tp->traceframe_usage)
 	{
@@ -6794,7 +6796,7 @@  print_one_breakpoint_location (struct breakpoint *b,
 
   if (is_tracepoint (b))
     {
-      struct tracepoint *t = (struct tracepoint *) b;
+      struct tracepoint *t = gdb::checked_static_cast<struct tracepoint *> (b);
 
       if (!part_of_multiple && t->pass_count)
 	{
@@ -12811,9 +12813,8 @@  ambiguous_names_p (const bp_location_range &locs)
    precisely because it confuses tools).  */
 
 static struct symtab_and_line
-update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
+update_static_tracepoint (struct tracepoint *tp, struct symtab_and_line sal)
 {
-  struct tracepoint *tp = (struct tracepoint *) b;
   struct static_tracepoint_marker marker;
   CORE_ADDR pc;
 
@@ -12825,7 +12826,7 @@  update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
     {
       if (tp->static_trace_marker_id != marker.str_id)
 	warning (_("static tracepoint %d changed probed marker from %s to %s"),
-		 b->number, tp->static_trace_marker_id.c_str (),
+		 tp->number, tp->static_trace_marker_id.c_str (),
 		 marker.str_id.c_str ());
 
       tp->static_trace_marker_id = std::move (marker.str_id);
@@ -12856,7 +12857,7 @@  update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
 
 	  warning (_("marker for static tracepoint %d (%s) not "
 		     "found at previous line number"),
-		   b->number, tp->static_trace_marker_id.c_str ());
+		   tp->number, tp->static_trace_marker_id.c_str ());
 
 	  symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
 	  sym = find_pc_sect_function (tpmarker->address, NULL);
@@ -12882,17 +12883,17 @@  update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
 	  uiout->field_signed ("line", sal2.line);
 	  uiout->text ("\n");
 
-	  b->first_loc ().line_number = sal2.line;
-	  b->first_loc ().symtab = sym != NULL ? sal2.symtab : NULL;
+	  tp->first_loc ().line_number = sal2.line;
+	  tp->first_loc ().symtab = sym != NULL ? sal2.symtab : NULL;
 
 	  std::unique_ptr<explicit_location_spec> els
 	    (new explicit_location_spec ());
 	  els->source_filename
 	    = xstrdup (symtab_to_filename_for_display (sal2.symtab));
-	  els->line_offset.offset = b->first_loc ().line_number;
+	  els->line_offset.offset = tp->first_loc ().line_number;
 	  els->line_offset.sign = LINE_OFFSET_NONE;
 
-	  b->locspec = std::move (els);
+	  tp->locspec = std::move (els);
 
 	  /* Might be nice to check if function changed, and warn if
 	     so.  */
@@ -13153,7 +13154,11 @@  code_breakpoint::location_spec_to_sals (location_spec *locspec,
 	}
 
       if (type == bp_static_tracepoint)
-	sals[0] = update_static_tracepoint (this, sals[0]);
+	{
+	  struct tracepoint *t
+	    = gdb::checked_static_cast<struct tracepoint *> (this);
+	  sals[0] = update_static_tracepoint (t, sals[0]);
+	}
 
       *found = 1;
     }
diff --git a/gdb/mi/mi-cmd-break.c b/gdb/mi/mi-cmd-break.c
index 975bc1c5dde..2294ea3723b 100644
--- a/gdb/mi/mi-cmd-break.c
+++ b/gdb/mi/mi-cmd-break.c
@@ -585,11 +585,15 @@  mi_cmd_break_commands (const char *command, const char *const *argv, int argc)
       };
 
   if (is_tracepoint (b))
-    break_command = read_command_lines_1 (reader, 1,
-					  [=] (const char *line)
+    {
+      struct tracepoint *t
+	= gdb::checked_static_cast<struct tracepoint *> (b);
+      break_command = read_command_lines_1 (reader, 1,
+					    [=] (const char *line)
 					    {
-					      validate_actionline (line, b);
+					      validate_actionline (line, t);
 					    });
+    }
   else
     break_command = read_command_lines_1 (reader, 1, 0);
 
diff --git a/gdb/remote.c b/gdb/remote.c
index ba81c5b0b6f..38b4145be30 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -911,7 +911,7 @@  class remote_target : public process_stratum_target
 
   int get_trace_status (struct trace_status *ts) override;
 
-  void get_tracepoint_status (struct breakpoint *tp, struct uploaded_tp *utp)
+  void get_tracepoint_status (struct tracepoint *tp, struct uploaded_tp *utp)
     override;
 
   void trace_stop () override;
@@ -13256,7 +13256,7 @@  remote_target::download_tracepoint (struct bp_location *loc)
   std::vector<std::string> stepping_actions;
   char *pkt;
   struct breakpoint *b = loc->owner;
-  struct tracepoint *t = (struct tracepoint *) b;
+  struct tracepoint *t = gdb::checked_static_cast<struct tracepoint *> (b);
   struct remote_state *rs = get_remote_state ();
   int ret;
   const char *err_msg = _("Tracepoint packet too large for target.");
@@ -13675,12 +13675,11 @@  remote_target::get_trace_status (struct trace_status *ts)
 }
 
 void
-remote_target::get_tracepoint_status (struct breakpoint *bp,
+remote_target::get_tracepoint_status (struct tracepoint *tp,
 				      struct uploaded_tp *utp)
 {
   struct remote_state *rs = get_remote_state ();
   char *reply;
-  struct tracepoint *tp = (struct tracepoint *) bp;
   size_t size = get_remote_packet_size ();
 
   if (tp)
@@ -13700,7 +13699,7 @@  remote_target::get_tracepoint_status (struct breakpoint *bp,
 	  if (reply && *reply)
 	    {
 	      if (*reply == 'V')
-		parse_tracepoint_status (reply + 1, bp, utp);
+		parse_tracepoint_status (reply + 1, tp, utp);
 	    }
 	}
     }
@@ -13715,7 +13714,7 @@  remote_target::get_tracepoint_status (struct breakpoint *bp,
       if (reply && *reply)
 	{
 	  if (*reply == 'V')
-	    parse_tracepoint_status (reply + 1, bp, utp);
+	    parse_tracepoint_status (reply + 1, tp, utp);
 	}
     }
 }
diff --git a/gdb/target-debug.h b/gdb/target-debug.h
index a028d2ba5f4..2eccc7840d6 100644
--- a/gdb/target-debug.h
+++ b/gdb/target-debug.h
@@ -126,6 +126,8 @@ 
   target_debug_do_print (host_address_to_string (X))
 #define target_debug_print_struct_breakpoint_p(X)	\
   target_debug_do_print (host_address_to_string (X))
+#define target_debug_print_struct_tracepoint_p(X)	\
+  target_debug_do_print (host_address_to_string (X))
 #define target_debug_print_struct_uploaded_tp_p(X)	\
   target_debug_do_print (host_address_to_string (X))
 #define target_debug_print_struct_uploaded_tp_pp(X)	\
diff --git a/gdb/target-delegates.c b/gdb/target-delegates.c
index 8b066249624..909f2725035 100644
--- a/gdb/target-delegates.c
+++ b/gdb/target-delegates.c
@@ -143,7 +143,7 @@  struct dummy_target : public target_ops
   void trace_set_readonly_regions () override;
   void trace_start () override;
   int get_trace_status (struct trace_status *arg0) override;
-  void get_tracepoint_status (struct breakpoint *arg0, struct uploaded_tp *arg1) override;
+  void get_tracepoint_status (struct tracepoint *arg0, struct uploaded_tp *arg1) override;
   void trace_stop () override;
   int trace_find (enum trace_find_type arg0, int arg1, CORE_ADDR arg2, CORE_ADDR arg3, int *arg4) override;
   bool get_trace_state_variable_value (int arg0, LONGEST *arg1) override;
@@ -318,7 +318,7 @@  struct debug_target : public target_ops
   void trace_set_readonly_regions () override;
   void trace_start () override;
   int get_trace_status (struct trace_status *arg0) override;
-  void get_tracepoint_status (struct breakpoint *arg0, struct uploaded_tp *arg1) override;
+  void get_tracepoint_status (struct tracepoint *arg0, struct uploaded_tp *arg1) override;
   void trace_stop () override;
   int trace_find (enum trace_find_type arg0, int arg1, CORE_ADDR arg2, CORE_ADDR arg3, int *arg4) override;
   bool get_trace_state_variable_value (int arg0, LONGEST *arg1) override;
@@ -3216,24 +3216,24 @@  debug_target::get_trace_status (struct trace_status *arg0)
 }
 
 void
-target_ops::get_tracepoint_status (struct breakpoint *arg0, struct uploaded_tp *arg1)
+target_ops::get_tracepoint_status (struct tracepoint *arg0, struct uploaded_tp *arg1)
 {
   this->beneath ()->get_tracepoint_status (arg0, arg1);
 }
 
 void
-dummy_target::get_tracepoint_status (struct breakpoint *arg0, struct uploaded_tp *arg1)
+dummy_target::get_tracepoint_status (struct tracepoint *arg0, struct uploaded_tp *arg1)
 {
   tcomplain ();
 }
 
 void
-debug_target::get_tracepoint_status (struct breakpoint *arg0, struct uploaded_tp *arg1)
+debug_target::get_tracepoint_status (struct tracepoint *arg0, struct uploaded_tp *arg1)
 {
   gdb_printf (gdb_stdlog, "-> %s->get_tracepoint_status (...)\n", this->beneath ()->shortname ());
   this->beneath ()->get_tracepoint_status (arg0, arg1);
   gdb_printf (gdb_stdlog, "<- %s->get_tracepoint_status (", this->beneath ()->shortname ());
-  target_debug_print_struct_breakpoint_p (arg0);
+  target_debug_print_struct_tracepoint_p (arg0);
   gdb_puts (", ", gdb_stdlog);
   target_debug_print_struct_uploaded_tp_p (arg1);
   gdb_puts (")\n", gdb_stdlog);
@@ -4553,9 +4553,9 @@  dummy_target::fetch_x86_xsave_layout ()
 x86_xsave_layout
 debug_target::fetch_x86_xsave_layout ()
 {
-  x86_xsave_layout result;
   gdb_printf (gdb_stdlog, "-> %s->fetch_x86_xsave_layout (...)\n", this->beneath ()->shortname ());
-  result = this->beneath ()->fetch_x86_xsave_layout ();
+  x86_xsave_layout result
+    = this->beneath ()->fetch_x86_xsave_layout ();
   gdb_printf (gdb_stdlog, "<- %s->fetch_x86_xsave_layout (", this->beneath ()->shortname ());
   gdb_puts (") = ", gdb_stdlog);
   target_debug_print_x86_xsave_layout (result);
diff --git a/gdb/target.c b/gdb/target.c
index 68d7fe2bb56..8cb4fa1736d 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -667,7 +667,7 @@  target_get_trace_status (trace_status *ts)
 }
 
 void
-target_get_tracepoint_status (breakpoint *tp, uploaded_tp *utp)
+target_get_tracepoint_status (tracepoint *tp, uploaded_tp *utp)
 {
   return current_inferior ()->top_target ()->get_tracepoint_status (tp, utp);
 }
diff --git a/gdb/target.h b/gdb/target.h
index aa07075f9f2..18388a32d19 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -1043,7 +1043,7 @@  struct target_ops
     virtual int get_trace_status (struct trace_status *ts)
       TARGET_DEFAULT_RETURN (-1);
 
-    virtual void get_tracepoint_status (struct breakpoint *tp,
+    virtual void get_tracepoint_status (struct tracepoint *tp,
 					struct uploaded_tp *utp)
       TARGET_DEFAULT_NORETURN (tcomplain ());
 
@@ -2255,7 +2255,7 @@  extern void target_trace_set_readonly_regions ();
 
 extern int target_get_trace_status (trace_status *ts);
 
-extern void target_get_tracepoint_status (breakpoint *tp, uploaded_tp *utp);
+extern void target_get_tracepoint_status (tracepoint *tp, uploaded_tp *utp);
 
 extern void target_trace_stop ();
 
diff --git a/gdb/tracefile-tfile.c b/gdb/tracefile-tfile.c
index 3440f375021..1158a8beeb2 100644
--- a/gdb/tracefile-tfile.c
+++ b/gdb/tracefile-tfile.c
@@ -67,7 +67,7 @@  class tfile_target final : public tracefile_target
   bool get_trace_state_variable_value (int tsv, LONGEST *val) override;
   traceframe_info_up traceframe_info () override;
 
-  void get_tracepoint_status (struct breakpoint *tp,
+  void get_tracepoint_status (struct tracepoint *tp,
 			      struct uploaded_tp *utp) override;
 };
 
@@ -633,7 +633,7 @@  tfile_target::files_info ()
 }
 
 void
-tfile_target::get_tracepoint_status (struct breakpoint *tp, struct uploaded_tp *utp)
+tfile_target::get_tracepoint_status (struct tracepoint *tp, struct uploaded_tp *utp)
 {
   /* Other bits of trace status were collected as part of opening the
      trace files, so nothing to do here.  */
diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index 205380476b3..9cec91f0fd4 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -157,7 +157,7 @@  static std::string trace_stop_notes;
 
 struct collection_list;
 
-static counted_command_line all_tracepoint_actions (struct breakpoint *);
+static counted_command_line all_tracepoint_actions (struct tracepoint *);
 
 static struct trace_status trace_status;
 
@@ -626,12 +626,11 @@  finalize_tracepoint_aexpr (struct agent_expr *aexpr)
 
 /* worker function */
 void
-validate_actionline (const char *line, struct breakpoint *b)
+validate_actionline (const char *line, struct tracepoint *t)
 {
   struct cmd_list_element *c;
   const char *tmp_p;
   const char *p;
-  struct tracepoint *t = (struct tracepoint *) b;
 
   /* If EOF is typed, *line is NULL.  */
   if (line == NULL)
@@ -1479,7 +1478,9 @@  encode_actions (struct bp_location *tloc,
   gdbarch_virtual_frame_pointer (tloc->gdbarch,
 				 tloc->address, &frame_reg, &frame_offset);
 
-  counted_command_line actions = all_tracepoint_actions (tloc->owner);
+  struct tracepoint *t
+    = gdb::checked_static_cast<struct tracepoint *> (tloc->owner);
+  counted_command_line actions = all_tracepoint_actions (t);
   encode_actions_1 (actions.get (), tloc, frame_reg, frame_offset,
 		    tracepoint_list, stepping_list);
   encode_actions_1 (breakpoint_commands (tloc->owner), tloc,
@@ -1884,8 +1885,12 @@  tstatus_command (const char *args, int from_tty)
 		(long int) (ts->stop_time % 1000000));
 
   /* Now report any per-tracepoint status available.  */
-  for (breakpoint &t : all_tracepoints ())
-    target_get_tracepoint_status (&t, NULL);
+  for (breakpoint &b : all_tracepoints ())
+    {
+      struct tracepoint *t
+	= gdb::checked_static_cast<struct tracepoint *> (&b);
+      target_get_tracepoint_status (t, nullptr);
+    }
 }
 
 /* Report the trace status to uiout, in a way suitable for MI, and not
@@ -2751,7 +2756,7 @@  get_traceframe_location (int *stepping_frame_p)
 /* Return the default collect actions of a tracepoint T.  */
 
 static counted_command_line
-all_tracepoint_actions (struct breakpoint *t)
+all_tracepoint_actions (struct tracepoint *t)
 {
   counted_command_line actions (nullptr, command_lines_deleter ());
 
@@ -2794,7 +2799,9 @@  tdump_command (const char *args, int from_tty)
 
   select_frame (get_current_frame ());
 
-  counted_command_line actions = all_tracepoint_actions (loc->owner);
+  struct tracepoint *t
+    = gdb::checked_static_cast<struct tracepoint *> (loc->owner);
+  counted_command_line actions = all_tracepoint_actions (t);
 
   trace_dump_actions (actions.get (), 0, stepping_frame, from_tty);
   trace_dump_actions (breakpoint_commands (loc->owner), 0, stepping_frame,
@@ -3059,7 +3066,7 @@  merge_uploaded_tracepoints (struct uploaded_tp **uploaded_tps)
 
 	  /* Mark this location as already inserted.  */
 	  loc->inserted = 1;
-	  t = (struct tracepoint *) loc->owner;
+	  t = gdb::checked_static_cast<struct tracepoint *> (loc->owner);
 	  gdb_printf (_("Assuming tracepoint %d is same "
 			"as target's tracepoint %d at %s.\n"),
 		      loc->owner->number, utp->number,
@@ -3368,11 +3375,10 @@  Status line: '%s'\n"), p, line);
 }
 
 void
-parse_tracepoint_status (const char *p, struct breakpoint *bp,
+parse_tracepoint_status (const char *p, struct tracepoint *tp,
 			 struct uploaded_tp *utp)
 {
   ULONGEST uval;
-  struct tracepoint *tp = (struct tracepoint *) bp;
 
   p = unpack_varlen_hex (p, &uval);
   if (tp)
diff --git a/gdb/tracepoint.h b/gdb/tracepoint.h
index a637d6b71d3..60b51390002 100644
--- a/gdb/tracepoint.h
+++ b/gdb/tracepoint.h
@@ -354,7 +354,7 @@  extern void encode_actions_rsp (struct bp_location *tloc,
 				std::vector<std::string> *tdp_actions,
 				std::vector<std::string> *stepping_actions);
 
-extern void validate_actionline (const char *, struct breakpoint *);
+extern void validate_actionline (const char *, struct tracepoint *);
 extern void validate_trace_state_variable_name (const char *name);
 
 extern struct trace_state_variable *find_trace_state_variable (const char *name);
@@ -369,7 +369,7 @@  extern int encode_source_string (int num, ULONGEST addr,
 
 extern void parse_trace_status (const char *line, struct trace_status *ts);
 
-extern void parse_tracepoint_status (const char *p, struct breakpoint *tp,
+extern void parse_tracepoint_status (const char *p, struct tracepoint *tp,
 				     struct uploaded_tp *utp);
 
 extern void parse_tracepoint_definition (const char *line,