Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 1/5] gdbserver: clean up some older code in tracepoint.cc
Date: Mon, 16 Nov 2026 11:10:07 +0000	[thread overview]
Message-ID: <6ea6d9167585bb2be6704dfb116aeea520cb306f.1794827178.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1794827178.git.aburgess@redhat.com>

In the following commits I plan to make some changes in, and fix some
bugs in various functions in gdbserver/tracepoint.cc.  These functions
are all older GDB code, using older GDB coding styles.  This commit is
a refactor to address some of these older issues.  Things changed:

  1. Move variable declarations inline within the function rather than
     having them all at the top of the function.

  2. Don't treat pointers as booleans.

  3. Convert some ints to bool where this requires only minimal
     changes.

  4. Add missing function headers.

  5. Rename tppacket to defining_tracepoint in cmd_qtdp, as the new
     name is more descriptive.

  6. Rename saved to start_of_type in cmd_qtdpsrc for the same reason.

  7. Rename saved to start_of_text in cmd_qtnotes for the same reason.

  8. Convert NULL to nullptr.

  9. Change 'char *' to 'const char *' where possible.

I am only cleaning up functions that I plan to touch later in this
series.

There should be no user visible changes after this commit.
---
 gdbserver/tracepoint.cc | 222 +++++++++++++++++++---------------------
 1 file changed, 104 insertions(+), 118 deletions(-)

diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc
index 6f657257ee8..e7c1f0c9e7f 100644
--- a/gdbserver/tracepoint.cc
+++ b/gdbserver/tracepoint.cc
@@ -2234,10 +2234,9 @@ static void
 cmd_qtinit (char *packet)
 {
   client_state &cs = get_client_state ();
-  struct trace_state_variable *tsv, *prev, *next;
 
   /* Can't do this command without a pid attached.  */
-  if (current_thread == NULL)
+  if (current_thread == nullptr)
     {
       write_enn (packet);
       return;
@@ -2253,19 +2252,19 @@ cmd_qtinit (char *packet)
   clear_installed_tracepoints ();
   clear_readonly_regions ();
 
-  tracepoints = NULL;
+  tracepoints = nullptr;
 
   /* Clear out any leftover trace state variables.  Ones with target
      defined getters should be kept however.  */
-  prev = NULL;
-  tsv = trace_state_variables;
-  while (tsv)
+  struct trace_state_variable *prev = nullptr;
+  struct trace_state_variable *tsv = trace_state_variables;
+  while (tsv != nullptr)
     {
       trace_debug ("Looking at var %d", tsv->number);
-      if (tsv->getter == NULL)
+      if (tsv->getter == nullptr)
 	{
-	  next = tsv->next;
-	  if (prev)
+	  struct trace_state_variable *next = tsv->next;
+	  if (prev != nullptr)
 	    prev->next = next;
 	  else
 	    trace_state_variables = next;
@@ -2293,16 +2292,16 @@ cmd_qtinit (char *packet)
 static void
 clear_installed_tracepoints (void)
 {
-  struct tracepoint *tpoint;
-
   target_pause_all (true);
 
   /* Restore any bytes overwritten by tracepoints.  */
-  for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
+  for (struct tracepoint *tpoint = tracepoints;
+       tpoint != nullptr;
+       tpoint = tpoint->next)
     {
       /* Catch the case where we might try to remove a tracepoint that
 	 was never actually installed.  */
-      if (tpoint->handle == NULL)
+      if (tpoint->handle == nullptr)
 	{
 	  trace_debug ("Tracepoint %d at 0x%s was "
 		       "never installed, nothing to clear",
@@ -2330,7 +2329,7 @@ clear_installed_tracepoints (void)
 	  break;
 	}
 
-      tpoint->handle = NULL;
+      tpoint->handle = nullptr;
     }
 
   target_unpause_all (true);
@@ -2341,37 +2340,33 @@ clear_installed_tracepoints (void)
 static void
 cmd_qtdp (char *own_buf)
 {
-  int tppacket;
-  /* Whether there is a trailing hyphen at the end of the QTDP packet.  */
-  int trail_hyphen = 0;
-  ULONGEST num;
-  ULONGEST addr;
-  ULONGEST count;
-  struct tracepoint *tpoint;
-  const char *packet = own_buf;
-
-  packet += strlen ("QTDP:");
+  const char *packet = own_buf + strlen ("QTDP:");
 
   /* A hyphen at the beginning marks a packet specifying actions for a
      tracepoint already supplied.  */
-  tppacket = 1;
+  bool defining_tracepoint = true;
   if (*packet == '-')
     {
-      tppacket = 0;
+      defining_tracepoint = false;
       ++packet;
     }
+
+  ULONGEST num, addr;
   packet = unpack_varlen_hex (packet, &num);
   ++packet; /* skip a colon */
   packet = unpack_varlen_hex (packet, &addr);
   ++packet; /* skip a colon */
 
   /* See if we already have this tracepoint.  */
-  tpoint = find_tracepoint (num, addr);
+  struct tracepoint *tpoint = find_tracepoint (num, addr);
 
-  if (tppacket)
+  /* Whether there is a trailing hyphen at the end of the QTDP packet.  */
+  bool trail_hyphen = false;
+
+  if (defining_tracepoint)
     {
       /* Duplicate tracepoints are never allowed.  */
-      if (tpoint)
+      if (tpoint != nullptr)
 	{
 	  trace_debug ("Tracepoint error: tracepoint %d"
 		       " at 0x%s already exists",
@@ -2385,6 +2380,8 @@ cmd_qtdp (char *own_buf)
       tpoint->enabled = (*packet == 'E');
       ++packet; /* skip 'E' */
       ++packet; /* skip a colon */
+
+      ULONGEST count;
       packet = unpack_varlen_hex (packet, &count);
       tpoint->step_count = count;
       ++packet; /* skip a colon */
@@ -2414,7 +2411,7 @@ cmd_qtdp (char *own_buf)
 	}
       if (*packet == '-')
 	{
-	  trail_hyphen = 1;
+	  trail_hyphen = true;
 	  trace_debug ("Also has actions\n");
 	}
 
@@ -2424,7 +2421,7 @@ cmd_qtdp (char *own_buf)
 		   tpoint->number, paddress (tpoint->address), tpoint->enabled,
 		   tpoint->step_count, tpoint->pass_count);
     }
-  else if (tpoint)
+  else if (tpoint != nullptr)
     add_tracepoint_action (tpoint, packet);
   else
     {
@@ -2440,7 +2437,7 @@ cmd_qtdp (char *own_buf)
      trailing hyphen in QTDP packet.  */
   if (tracing && !trail_hyphen)
     {
-      struct tracepoint *tp = NULL;
+      struct tracepoint *tp = nullptr;
 
       /* Pause all threads temporarily while we patch tracepoints.  */
       target_pause_all (false);
@@ -2456,7 +2453,7 @@ cmd_qtdp (char *own_buf)
       if (tpoint->type != trap_tracepoint)
 	{
 	  /* Find another fast tracepoint at the same address.  */
-	  for (tp = tracepoints; tp; tp = tp->next)
+	  for (tp = tracepoints; tp != nullptr; tp = tp->next)
 	    {
 	      if (tp->address == tpoint->address && tp->type == tpoint->type
 		  && tp->number != tpoint->number)
@@ -2464,7 +2461,7 @@ cmd_qtdp (char *own_buf)
 	    }
 
 	  /* TPOINT is installed at the same address as TP.  */
-	  if (tp)
+	  if (tp != nullptr)
 	    {
 	      if (tpoint->type == fast_tracepoint)
 		clone_fast_tracepoint (tpoint, tp);
@@ -2487,7 +2484,7 @@ cmd_qtdp (char *own_buf)
 	{
 	  download_tracepoint (tpoint);
 
-	  if (tpoint->type == trap_tracepoint || tp == NULL)
+	  if (tpoint->type == trap_tracepoint || tp == nullptr)
 	    {
 	      install_tracepoint (tpoint, own_buf);
 	      if (!streq (own_buf, "OK"))
@@ -2504,28 +2501,22 @@ cmd_qtdp (char *own_buf)
   write_ok (own_buf);
 }
 
+/* Handle the QTDPsrc packet in OWN_BUF.  */
+
 static void
 cmd_qtdpsrc (char *own_buf)
 {
-  ULONGEST num, addr, start, slen;
-  struct tracepoint *tpoint;
-  const char *packet = own_buf;
-  const char *saved;
-  char *srctype, *src;
-  size_t nbytes;
-  struct source_string *last, *newlast;
-
-  packet += strlen ("QTDPsrc:");
+  const char *packet = own_buf + strlen ("QTDPsrc:");
 
+  ULONGEST num, addr;
   packet = unpack_varlen_hex (packet, &num);
   ++packet; /* skip a colon */
   packet = unpack_varlen_hex (packet, &addr);
   ++packet; /* skip a colon */
 
   /* See if we already have this tracepoint.  */
-  tpoint = find_tracepoint (num, addr);
-
-  if (!tpoint)
+  struct tracepoint *tpoint = find_tracepoint (num, addr);
+  if (tpoint == nullptr)
     {
       trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
 		   (int) num, paddress (addr));
@@ -2533,30 +2524,36 @@ cmd_qtdpsrc (char *own_buf)
       return;
     }
 
-  saved = packet;
+  /* The start of the string type field within PACKET.  */
+  const char *start_of_type = packet;
   packet = strchr (packet, ':');
-  srctype = (char *) xmalloc (packet - saved + 1);
-  memcpy (srctype, saved, packet - saved);
-  srctype[packet - saved] = '\0';
+  char *srctype = (char *) xmalloc (packet - start_of_type + 1);
+  memcpy (srctype, start_of_type, packet - start_of_type);
+  srctype[packet - start_of_type] = '\0';
   ++packet;
+
+  ULONGEST start, slen;
   packet = unpack_varlen_hex (packet, &start);
   ++packet; /* skip a colon */
   packet = unpack_varlen_hex (packet, &slen);
   ++packet; /* skip a colon */
-  src = (char *) xmalloc (slen + 1);
-  nbytes = hex2bin (packet, (gdb_byte *) src, strlen (packet) / 2);
+  char *src = (char *) xmalloc (slen + 1);
+  size_t nbytes = hex2bin (packet, (gdb_byte *) src, strlen (packet) / 2);
   src[nbytes] = '\0';
 
-  newlast = XNEW (struct source_string);
+  struct source_string *newlast = XNEW (struct source_string);
   newlast->type = srctype;
   newlast->str = src;
-  newlast->next = NULL;
+  newlast->next = nullptr;
   /* Always add a source string to the end of the list;
      this keeps sequences of actions/commands in the right
      order.  */
-  if (tpoint->source_strings)
+  if (tpoint->source_strings != nullptr)
     {
-      for (last = tpoint->source_strings; last->next; last = last->next)
+      struct source_string *last;
+      for (last = tpoint->source_strings;
+	   last->next != nullptr;
+	   last = last->next)
 	;
       last->next = newlast;
     }
@@ -2566,17 +2563,14 @@ cmd_qtdpsrc (char *own_buf)
   write_ok (own_buf);
 }
 
+/* Handle the QTDV packet in OWN_BUF.  */
+
 static void
 cmd_qtdv (char *own_buf)
 {
+  const char *packet = own_buf + strlen ("QTDV:");
+
   ULONGEST num, val, builtin;
-  char *varname;
-  size_t nbytes;
-  struct trace_state_variable *tsv;
-  const char *packet = own_buf;
-
-  packet += strlen ("QTDV:");
-
   packet = unpack_varlen_hex (packet, &num);
   ++packet; /* skip a colon */
   packet = unpack_varlen_hex (packet, &val);
@@ -2584,12 +2578,12 @@ cmd_qtdv (char *own_buf)
   packet = unpack_varlen_hex (packet, &builtin);
   ++packet; /* skip a colon */
 
-  nbytes = strlen (packet) / 2;
-  varname = (char *) xmalloc (nbytes + 1);
+  size_t nbytes = strlen (packet) / 2;
+  char *varname = (char *) xmalloc (nbytes + 1);
   nbytes = hex2bin (packet, (gdb_byte *) varname, nbytes);
   varname[nbytes] = '\0';
 
-  tsv = create_trace_state_variable (num, 1);
+  struct trace_state_variable *tsv = create_trace_state_variable (num, 1);
   tsv->initial_value = (LONGEST) val;
   tsv->name = varname;
 
@@ -2598,21 +2592,22 @@ cmd_qtdv (char *own_buf)
   write_ok (own_buf);
 }
 
-static void
-cmd_qtenable_disable (char *own_buf, int enable)
-{
-  const char *packet = own_buf;
-  ULONGEST num, addr;
-  struct tracepoint *tp;
+/* Handle a QTEnable or QTDisable packet in OWN_BUF.  When ENABLE is
+   true it's a QTEnable packet, otherwise it's a QTDisable packet.  */
 
-  packet += strlen (enable ? "QTEnable:" : "QTDisable:");
+static void
+cmd_qtenable_disable (char *own_buf, bool enable)
+{
+  const char *packet = own_buf + strlen (enable ? "QTEnable:" : "QTDisable:");
+
+  ULONGEST num, addr;
   packet = unpack_varlen_hex (packet, &num);
   ++packet; /* skip a colon */
   packet = unpack_varlen_hex (packet, &addr);
 
-  tp = find_tracepoint (num, addr);
+  struct tracepoint *tp = find_tracepoint (num, addr);
 
-  if (tp)
+  if (tp != nullptr)
     {
       if ((enable && tp->enabled) || (!enable && !tp->enabled))
 	{
@@ -2635,7 +2630,7 @@ cmd_qtenable_disable (char *own_buf, int enable)
 	  CORE_ADDR obj_addr = tp->obj_addr_on_target + offset;
 
 	  int ret = write_inferior_int8 (obj_addr, enable);
-	  if (ret)
+	  if (ret != 0)
 	    {
 	      trace_debug ("Cannot write enabled flag into "
 			   "inferior process memory");
@@ -2711,24 +2706,22 @@ clear_readonly_regions (void)
 static void
 cmd_qtro (char *own_buf)
 {
-  ULONGEST start, end;
-  struct readonly_region *roreg;
-  const char *packet = own_buf;
-
   trace_debug ("Want to mark readonly regions");
 
   clear_readonly_regions ();
 
-  packet += strlen ("QTro");
+  const char *packet = own_buf + strlen ("QTro");
 
   while (*packet == ':')
     {
       ++packet;  /* skip a colon */
+
+      ULONGEST start, end;
       packet = unpack_varlen_hex (packet, &start);
       ++packet;  /* skip a comma */
       packet = unpack_varlen_hex (packet, &end);
 
-      roreg = XNEW (struct readonly_region);
+      struct readonly_region *roreg = XNEW (struct readonly_region);
       roreg->start = start;
       roreg->end = end;
       roreg->next = readonly_regions;
@@ -3458,23 +3451,21 @@ cmd_qtstatus (char *packet)
 	   buf1, buf2);
 }
 
+/* Handle the qTP packet in OWN_BUF.  */
+
 static void
 cmd_qtp (char *own_buf)
 {
+  const char *packet = own_buf + strlen ("qTP:");
+
   ULONGEST num, addr;
-  struct tracepoint *tpoint;
-  const char *packet = own_buf;
-
-  packet += strlen ("qTP:");
-
   packet = unpack_varlen_hex (packet, &num);
   ++packet; /* skip a colon */
   packet = unpack_varlen_hex (packet, &addr);
 
   /* See if we already have this tracepoint.  */
-  tpoint = find_tracepoint (num, addr);
-
-  if (!tpoint)
+  struct tracepoint *tpoint = find_tracepoint (num, addr);
+  if (tpoint == nullptr)
     {
       trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
 		   (int) num, paddress (addr));
@@ -3718,12 +3709,9 @@ cmd_qtminftpilen (char *packet)
 static void
 cmd_qtbuffer (char *own_buf)
 {
-  ULONGEST offset, num, tot;
-  unsigned char *tbp;
-  const char *packet = own_buf;
-
-  packet += strlen ("qTBuffer:");
+  const char *packet = own_buf + strlen ("qTBuffer:");
 
+  ULONGEST offset, num;
   packet = unpack_varlen_hex (packet, &offset);
   ++packet; /* skip a comma */
   unpack_varlen_hex (packet, &num);
@@ -3731,7 +3719,7 @@ cmd_qtbuffer (char *own_buf)
   trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
 	       (int) num, phex_nz (offset, 0));
 
-  tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
+  ULONGEST tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
 
   /* If we're right at the end, reply specially that we're done.  */
   if (offset == tot)
@@ -3749,7 +3737,7 @@ cmd_qtbuffer (char *own_buf)
 
   /* Compute the pointer corresponding to the given offset, accounting
      for wraparound.  */
-  tbp = trace_buffer_start + offset;
+  unsigned char *tbp = trace_buffer_start + offset;
   if (tbp >= trace_buffer_wrap)
     tbp -= (trace_buffer_wrap - trace_buffer_lo);
 
@@ -3810,25 +3798,23 @@ cmd_bigqtbuffer_size (char *own_buf)
   write_ok (own_buf);
 }
 
+/* Handle the QTNotes packet in OWN_BUF.  */
+
 static void
 cmd_qtnotes (char *own_buf)
 {
-  size_t nbytes;
-  char *saved, *user, *notes, *stopnote;
-  char *packet = own_buf;
+  const char *packet = own_buf + strlen ("QTNotes:");
 
-  packet += strlen ("QTNotes:");
-
-  while (*packet)
+  while (*packet != '\0')
     {
       if (startswith (packet, "user:"))
 	{
 	  packet += strlen ("user:");
-	  saved = packet;
+	  const char *start_of_text = packet;
 	  packet = strchr (packet, ';');
-	  nbytes = (packet - saved) / 2;
-	  user = (char *) xmalloc (nbytes + 1);
-	  nbytes = hex2bin (saved, (gdb_byte *) user, nbytes);
+	  size_t nbytes = (packet - start_of_text) / 2;
+	  char *user = (char *) xmalloc (nbytes + 1);
+	  nbytes = hex2bin (start_of_text, (gdb_byte *) user, nbytes);
 	  user[nbytes] = '\0';
 	  ++packet; /* skip the semicolon */
 	  trace_debug ("User is '%s'", user);
@@ -3838,11 +3824,11 @@ cmd_qtnotes (char *own_buf)
       else if (startswith (packet, "notes:"))
 	{
 	  packet += strlen ("notes:");
-	  saved = packet;
+	  const char *start_of_text = packet;
 	  packet = strchr (packet, ';');
-	  nbytes = (packet - saved) / 2;
-	  notes = (char *) xmalloc (nbytes + 1);
-	  nbytes = hex2bin (saved, (gdb_byte *) notes, nbytes);
+	  size_t nbytes = (packet - start_of_text) / 2;
+	  char *notes = (char *) xmalloc (nbytes + 1);
+	  nbytes = hex2bin (start_of_text, (gdb_byte *) notes, nbytes);
 	  notes[nbytes] = '\0';
 	  ++packet; /* skip the semicolon */
 	  trace_debug ("Notes is '%s'", notes);
@@ -3852,11 +3838,11 @@ cmd_qtnotes (char *own_buf)
       else if (startswith (packet, "tstop:"))
 	{
 	  packet += strlen ("tstop:");
-	  saved = packet;
+	  const char *start_of_text = packet;
 	  packet = strchr (packet, ';');
-	  nbytes = (packet - saved) / 2;
-	  stopnote = (char *) xmalloc (nbytes + 1);
-	  nbytes = hex2bin (saved, (gdb_byte *) stopnote, nbytes);
+	  size_t nbytes = (packet - start_of_text) / 2;
+	  char *stopnote = (char *) xmalloc (nbytes + 1);
+	  nbytes = hex2bin (start_of_text, (gdb_byte *) stopnote, nbytes);
 	  stopnote[nbytes] = '\0';
 	  ++packet; /* skip the semicolon */
 	  trace_debug ("tstop note is '%s'", stopnote);
@@ -3890,12 +3876,12 @@ handle_tracepoint_general_set (char *packet)
     }
   else if (startswith (packet, "QTEnable:"))
     {
-      cmd_qtenable_disable (packet, 1);
+      cmd_qtenable_disable (packet, true);
       return 1;
     }
   else if (startswith (packet, "QTDisable:"))
     {
-      cmd_qtenable_disable (packet, 0);
+      cmd_qtenable_disable (packet, false);
       return 1;
     }
   else if (startswith (packet, "QTDV:"))
-- 
2.25.4


  reply	other threads:[~2026-11-16 11:11 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 11:10 [PATCH 0/5] gdbserver: fix buffer overflow issue in QTDPsrc packet Andrew Burgess
2026-11-16 11:10 ` Andrew Burgess [this message]
2026-11-16 11:10 ` [PATCH 2/5] gdbserver: prevent buffer overrun for " Andrew Burgess
2026-11-19 19:51   ` Keith Seitz
2026-11-23 17:15     ` Andrew Burgess
2026-11-23 17:57       ` Keith Seitz
2026-11-16 11:10 ` [PATCH 3/5] gdbserver: improve packet parsing in tracepoint.cc Andrew Burgess
2026-11-16 11:10 ` [PATCH 4/5] gdbserver: don't leak trace variable names if reusing variable numbers Andrew Burgess
2026-11-16 11:10 ` [PATCH 5/5] gdbserver: use unique_xmalloc_ptr to avoid memory leaks Andrew Burgess
2026-11-19 19:52 ` [PATCH 0/5] gdbserver: fix buffer overflow issue in QTDPsrc packet Keith Seitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6ea6d9167585bb2be6704dfb116aeea520cb306f.1794827178.git.aburgess@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox