[1/3] Use std::vector in uploaded_tp

Message ID 1521667509-303-1-git-send-email-simon.marchi@ericsson.com
State New
Headers show
Series
  • [1/3] Use std::vector in uploaded_tp
Related show

Commit Message

Simon Marchi March 21, 2018, 9:25 p.m.
From: Simon Marchi <simon.marchi@polymtl.ca>


This patch changes the VEC(char_ptr) fields in uploaded_tp to use
std::vector<char *>.  At first, I wanted to creep in more changes, like
using std::string, but it was making the patch too big and less focused,
so I decided to keep it to just that.

It also looks like the strings in those vectors are never free'd.  If
so, we can fix that in another patch.

gdb/ChangeLog:

	* tracepoint.h (struct uploaded_tp): Initialize fields.
	<actions, step_actions, cmd_strings>: Change type to
	std::vector<char *>.
	* tracepoint.c (get_uploaded_tp): Allocate with new.
	(free_uploaded_tps): Free with delete.
	(parse_tracepoint_definition): Adjust to std::vector change.
	* breakpoint.c (read_uploaded_action): Likewise.
	(create_tracepoint_from_upload): Likewise.
	* ctf.c (ctf_write_uploaded_tp): Likewise.
	(SET_ARRAY_FIELD): Likewise.
	* tracefile-tfile.c (tfile_write_uploaded_tp): Likewise.
---
 gdb/breakpoint.c      | 16 +++++++++-------
 gdb/ctf.c             | 18 ++++++++----------
 gdb/tracefile-tfile.c |  8 +++-----
 gdb/tracepoint.c      | 13 +++++--------
 gdb/tracepoint.h      | 32 ++++++++++++++++----------------
 5 files changed, 41 insertions(+), 46 deletions(-)

-- 
2.7.4

Comments

Simon Marchi March 30, 2018, 9:19 p.m. | #1
On 2018-03-21 05:25 PM, Simon Marchi wrote:
> From: Simon Marchi <simon.marchi@polymtl.ca>

> 

> This patch changes the VEC(char_ptr) fields in uploaded_tp to use

> std::vector<char *>.  At first, I wanted to creep in more changes, like

> using std::string, but it was making the patch too big and less focused,

> so I decided to keep it to just that.

> 

> It also looks like the strings in those vectors are never free'd.  If

> so, we can fix that in another patch.


I pushed this series.

Simon

Patch

diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 85f2fd8..ca6d6f6 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -14754,11 +14754,13 @@  static int next_cmd;
 static char *
 read_uploaded_action (void)
 {
-  char *rslt;
+  char *rslt = nullptr;
 
-  VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
-
-  next_cmd++;
+  if (next_cmd < this_utp->cmd_strings.size ())
+    {
+      rslt = this_utp->cmd_strings[next_cmd];
+      next_cmd++;
+    }
 
   return rslt;
 }
@@ -14830,7 +14832,7 @@  create_tracepoint_from_upload (struct uploaded_tp *utp)
      special-purpose "reader" function and call the usual command line
      reader, then pass the result to the breakpoint command-setting
      function.  */
-  if (!VEC_empty (char_ptr, utp->cmd_strings))
+  if (!utp->cmd_strings.empty ())
     {
       command_line_up cmd_list;
 
@@ -14841,8 +14843,8 @@  create_tracepoint_from_upload (struct uploaded_tp *utp)
 
       breakpoint_set_commands (tp, std::move (cmd_list));
     }
-  else if (!VEC_empty (char_ptr, utp->actions)
-	   || !VEC_empty (char_ptr, utp->step_actions))
+  else if (!utp->actions.empty ()
+	   || !utp->step_actions.empty ())
     warning (_("Uploaded tracepoint %d actions "
 	       "have no source form, ignoring them"),
 	     utp->number);
diff --git a/gdb/ctf.c b/gdb/ctf.c
index d589ed3..07ae93b 100644
--- a/gdb/ctf.c
+++ b/gdb/ctf.c
@@ -530,8 +530,6 @@  ctf_write_uploaded_tp (struct trace_file_writer *self,
   int64_t int64;
   uint32_t u32;
   const gdb_byte zero = 0;
-  int a;
-  char *act;
 
   /* Event Id.  */
   int32 = CTF_EVENT_ID_TP_DEF;
@@ -569,15 +567,15 @@  ctf_write_uploaded_tp (struct trace_file_writer *self,
   ctf_save_write (&writer->tcs, &zero, 1);
 
   /* actions */
-  u32 = VEC_length (char_ptr, tp->actions);
+  u32 = tp->actions.size ();
   ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
-  for (a = 0; VEC_iterate (char_ptr, tp->actions, a, act); ++a)
+  for (char *act : tp->actions)
     ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
 
   /* step_actions */
-  u32 = VEC_length (char_ptr, tp->step_actions);
+  u32 = tp->step_actions.size ();
   ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
-  for (a = 0; VEC_iterate (char_ptr, tp->step_actions, a, act); ++a)
+  for (char *act : tp->step_actions)
     ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
 
   /* at_string */
@@ -593,9 +591,9 @@  ctf_write_uploaded_tp (struct trace_file_writer *self,
   ctf_save_write (&writer->tcs, &zero, 1);
 
   /* cmd_strings */
-  u32 = VEC_length (char_ptr, tp->cmd_strings);
+  u32 = tp->cmd_strings.size ();
   ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
-  for (a = 0; VEC_iterate (char_ptr, tp->cmd_strings, a, act); ++a)
+  for (char *act : tp->cmd_strings)
     ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1);
 
 }
@@ -992,8 +990,8 @@  ctf_read_tsv (struct uploaded_tsv **uploaded_tsvs)
 	  const struct bt_definition *element				\
 	    = bt_ctf_get_index ((EVENT), def, i);			\
 									\
-	  VEC_safe_push (char_ptr, (VAR)->ARRAY,			\
-			 xstrdup (bt_ctf_get_string (element)));	\
+	  (VAR)->ARRAY.push_back					\
+	    (xstrdup (bt_ctf_get_string (element)));			\
 	}								\
     }									\
   while (0)
diff --git a/gdb/tracefile-tfile.c b/gdb/tracefile-tfile.c
index 4f0dc59..d7e9347 100644
--- a/gdb/tracefile-tfile.c
+++ b/gdb/tracefile-tfile.c
@@ -221,8 +221,6 @@  tfile_write_uploaded_tp (struct trace_file_writer *self,
 {
   struct tfile_trace_file_writer *writer
     = (struct tfile_trace_file_writer *) self;
-  int a;
-  char *act;
   char buf[MAX_TRACE_UPLOAD];
 
   fprintf (writer->fp, "tp T%x:%s:%c:%x:%x",
@@ -235,10 +233,10 @@  tfile_write_uploaded_tp (struct trace_file_writer *self,
 	     ":X%x,%s", (unsigned int) strlen (utp->cond) / 2,
 	     utp->cond);
   fprintf (writer->fp, "\n");
-  for (a = 0; VEC_iterate (char_ptr, utp->actions, a, act); ++a)
+  for (char *act : utp->actions)
     fprintf (writer->fp, "tp A%x:%s:%s\n",
 	     utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act);
-  for (a = 0; VEC_iterate (char_ptr, utp->step_actions, a, act); ++a)
+  for (char *act : utp->step_actions)
     fprintf (writer->fp, "tp S%x:%s:%s\n",
 	     utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act);
   if (utp->at_string)
@@ -254,7 +252,7 @@  tfile_write_uploaded_tp (struct trace_file_writer *self,
 			    buf, MAX_TRACE_UPLOAD);
       fprintf (writer->fp, "tp Z%s\n", buf);
     }
-  for (a = 0; VEC_iterate (char_ptr, utp->cmd_strings, a, act); ++a)
+  for (char *act : utp->cmd_strings)
     {
       encode_source_string (utp->number, utp->addr, "cmd", act,
 			    buf, MAX_TRACE_UPLOAD);
diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index 30fe206..2ebbdfc 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -3062,12 +3062,9 @@  get_uploaded_tp (int num, ULONGEST addr, struct uploaded_tp **utpp)
     if (utp->number == num && utp->addr == addr)
       return utp;
 
-  utp = XCNEW (struct uploaded_tp);
+  utp = new uploaded_tp;
   utp->number = num;
   utp->addr = addr;
-  utp->actions = NULL;
-  utp->step_actions = NULL;
-  utp->cmd_strings = NULL;
   utp->next = *utpp;
   *utpp = utp;
 
@@ -3082,7 +3079,7 @@  free_uploaded_tps (struct uploaded_tp **utpp)
   while (*utpp)
     {
       next_one = (*utpp)->next;
-      xfree (*utpp);
+      delete *utpp;
       *utpp = next_one;
     }
 }
@@ -3599,12 +3596,12 @@  parse_tracepoint_definition (const char *line, struct uploaded_tp **utpp)
   else if (piece == 'A')
     {
       utp = get_uploaded_tp (num, addr, utpp);
-      VEC_safe_push (char_ptr, utp->actions, xstrdup (p));
+      utp->actions.push_back (xstrdup (p));
     }
   else if (piece == 'S')
     {
       utp = get_uploaded_tp (num, addr, utpp);
-      VEC_safe_push (char_ptr, utp->step_actions, xstrdup (p));
+      utp->step_actions.push_back (xstrdup (p));
     }
   else if (piece == 'Z')
     {
@@ -3628,7 +3625,7 @@  parse_tracepoint_definition (const char *line, struct uploaded_tp **utpp)
       else if (startswith (srctype, "cond:"))
 	utp->cond_string = xstrdup (buf);
       else if (startswith (srctype, "cmd:"))
-	VEC_safe_push (char_ptr, utp->cmd_strings, xstrdup (buf));
+	utp->cmd_strings.push_back (xstrdup (buf));
     }
   else if (piece == 'V')
     {
diff --git a/gdb/tracepoint.h b/gdb/tracepoint.h
index 9f4596e..ff2e2a4 100644
--- a/gdb/tracepoint.h
+++ b/gdb/tracepoint.h
@@ -165,38 +165,38 @@  extern const char *stop_reason_names[];
 
 struct uploaded_tp
 {
-  int number;
-  enum bptype type;
-  ULONGEST addr;
-  int enabled;
-  int step;
-  int pass;
-  int orig_size;
+  int number = 0;
+  enum bptype type = bp_none;
+  ULONGEST addr = 0;
+  int enabled = 0;
+  int step = 0;
+  int pass = 0;
+  int orig_size = 0;
 
   /* String that is the encoded form of the tracepoint's condition.  */
-  char *cond;
+  char *cond = nullptr;
 
   /* Vectors of strings that are the encoded forms of a tracepoint's
      actions.  */
-  VEC(char_ptr) *actions;
-  VEC(char_ptr) *step_actions;
+  std::vector<char *> actions;
+  std::vector<char *> step_actions;
 
   /* The original string defining the location of the tracepoint.  */
-  char *at_string;
+  char *at_string = nullptr;
 
   /* The original string defining the tracepoint's condition.  */
-  char *cond_string;
+  char *cond_string = nullptr;
 
   /* List of original strings defining the tracepoint's actions.  */
-  VEC(char_ptr) *cmd_strings;
+  std::vector<char *> cmd_strings;
 
   /* The tracepoint's current hit count.  */
-  int hit_count;
+  int hit_count = 0;
 
   /* The tracepoint's current traceframe usage.  */
-  ULONGEST traceframe_usage;
+  ULONGEST traceframe_usage = 0;
 
-  struct uploaded_tp *next;
+  struct uploaded_tp *next = nullptr;
 };
 
 /* Struct recording info about trace state variables on the target.  */