PR29262, memory leak in pr_function_type
[binutils-gdb.git] / gdbserver / tracepoint.cc
index 6881f31c0cecf610a0c02d2242e090a536e7be1b..18b2b0b3d779e0347f1ce4ce1b1e5d84a7934e1f 100644 (file)
@@ -1,5 +1,5 @@
 /* Tracepoint code for remote server for GDB.
-   Copyright (C) 2009-2020 Free Software Foundation, Inc.
+   Copyright (C) 2009-2022 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -77,28 +77,21 @@ trace_vdebug (const char *fmt, ...)
   va_end (ap);
 }
 
-#define trace_debug_1(level, fmt, args...)     \
+#define trace_debug(fmt, args...)      \
   do {                                         \
-    if (level <= debug_threads)                \
+    if (debug_threads)                         \
       trace_vdebug ((fmt), ##args);            \
   } while (0)
 
 #else
 
-#define trace_debug_1(level, fmt, args...)     \
+#define trace_debug(fmt, args...)      \
   do {                                         \
-    if (level <= debug_threads)                        \
-      {                                                \
-       debug_printf ((fmt), ##args);           \
-       debug_printf ("\n");                    \
-      }                                                \
+      threads_debug_printf ((fmt), ##args);    \
   } while (0)
 
 #endif
 
-#define trace_debug(FMT, args...)              \
-  trace_debug_1 (1, FMT, ##args)
-
 /* Prefix exported symbols, for good citizenship.  All the symbols
    that need exporting are defined in this module.  Note that all
    these symbols must be tagged with IP_AGENT_EXPORT_*.  */
@@ -327,8 +320,7 @@ tracepoint_look_up_symbols (void)
 
       if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0)
        {
-         if (debug_threads)
-           debug_printf ("symbol `%s' not found\n", symbol_list[i].name);
+         threads_debug_printf ("symbol `%s' not found", symbol_list[i].name);
          return;
        }
     }
@@ -402,11 +394,11 @@ tracepoint_handler (CORE_ADDR address)
 }
 
 /* Breakpoint at "stop_tracing" in the inferior lib.  */
-struct breakpoint *stop_tracing_bkpt;
+static struct breakpoint *stop_tracing_bkpt;
 static int stop_tracing_handler (CORE_ADDR);
 
 /* Breakpoint at "flush_trace_buffer" in the inferior lib.  */
-struct breakpoint *flush_trace_buffer_bkpt;
+static struct breakpoint *flush_trace_buffer_bkpt;
 static int flush_trace_buffer_handler (CORE_ADDR);
 
 static void download_trace_state_variables (void);
@@ -912,7 +904,7 @@ struct trace_state_variable
 /* Linked list of all trace state variables.  */
 
 #ifdef IN_PROCESS_AGENT
-struct trace_state_variable *alloced_trace_state_variables;
+static struct trace_state_variable *alloced_trace_state_variables;
 #endif
 
 IP_AGENT_EXPORT_VAR struct trace_state_variable *trace_state_variables;
@@ -1170,7 +1162,7 @@ IP_AGENT_EXPORT_VAR unsigned int trace_buffer_ctrl_curr;
    it doesn't need to sync with itself.  Define it as array anyway so
    that the rest of the code base doesn't need to care for the
    difference.  */
-struct trace_buffer_control trace_buffer_ctrl[1];
+static trace_buffer_control trace_buffer_ctrl[1];
 # define TRACE_BUFFER_CTRL_CURR 0
 #endif
 
@@ -1264,23 +1256,23 @@ static int tracing_stop_tpnum;
 /* 64-bit timestamps for the trace run's start and finish, expressed
    in microseconds from the Unix epoch.  */
 
-LONGEST tracing_start_time;
-LONGEST tracing_stop_time;
+static LONGEST tracing_start_time;
+static LONGEST tracing_stop_time;
 
 /* The (optional) user-supplied name of the user that started the run.
    This is an arbitrary string, and may be NULL.  */
 
-char *tracing_user_name;
+static char *tracing_user_name;
 
 /* Optional user-supplied text describing the run.  This is
    an arbitrary string, and may be NULL.  */
 
-char *tracing_notes;
+static char *tracing_notes;
 
 /* Optional user-supplied text explaining a tstop command.  This is an
    arbitrary string, and may be NULL.  */
 
-char *tracing_stop_note;
+static char *tracing_stop_note;
 
 #endif
 
@@ -2792,21 +2784,10 @@ cmd_qtenable_disable (char *own_buf, int enable)
 
       if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
        {
-         int ret;
          int offset = offsetof (struct tracepoint, enabled);
          CORE_ADDR obj_addr = tp->obj_addr_on_target + offset;
 
-         ret = prepare_to_access_memory ();
-         if (ret)
-           {
-             trace_debug ("Failed to temporarily stop inferior threads");
-             write_enn (own_buf);
-             return;
-           }
-
-         ret = write_inferior_int8 (obj_addr, enable);
-         done_accessing_memory ();
-         
+         int ret = write_inferior_int8 (obj_addr, enable);
          if (ret)
            {
              trace_debug ("Cannot write enabled flag into "
@@ -3242,7 +3223,7 @@ cmd_qtstart (char *packet)
       /* Tell IPA about the correct tdesc.  */
       if (write_inferior_integer (ipa_sym_addrs.addr_ipa_tdesc_idx,
                                  target_get_ipa_tdesc_idx ()))
-        error ("Error setting ipa_tdesc_idx variable in lib");
+       error ("Error setting ipa_tdesc_idx variable in lib");
     }
 
   /* Start out empty.  */
@@ -3975,18 +3956,14 @@ gdb_agent_about_to_close (int pid)
 
   if (!maybe_write_ipa_not_loaded (buf))
     {
-      struct thread_info *saved_thread;
-
-      saved_thread = current_thread;
+      scoped_restore_current_thread restore_thread;
 
       /* Find any thread which belongs to process PID.  */
-      current_thread = find_any_thread_of_pid (pid);
+      switch_to_thread (find_any_thread_of_pid (pid));
 
       strcpy (buf, "close");
 
       run_inferior_command (buf, strlen (buf) + 1);
-
-      current_thread = saved_thread;
     }
 }
 
@@ -4408,7 +4385,7 @@ tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
   wstep_link = &tinfo->while_stepping;
 
   trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
-              target_pid_to_str (tinfo->id),
+              target_pid_to_str (tinfo->id).c_str (),
               wstep->tp_number, paddress (wstep->tp_address));
 
   ctx.base.type = trap_tracepoint;
@@ -4421,7 +4398,7 @@ tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
        {
          trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
                       wstep->tp_number, paddress (wstep->tp_address),
-                      target_pid_to_str (tinfo->id));
+                      target_pid_to_str (tinfo->id).c_str ());
 
          /* Unlink.  */
          *wstep_link = wstep->next;
@@ -4441,7 +4418,7 @@ tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
        {
          /* The requested numbers of steps have occurred.  */
          trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
-                      target_pid_to_str (tinfo->id),
+                      target_pid_to_str (tinfo->id).c_str (),
                       wstep->tp_number, paddress (wstep->tp_address));
 
          /* Unlink the wstep.  */
@@ -4526,15 +4503,14 @@ handle_tracepoint_bkpts (struct thread_info *tinfo, CORE_ADDR stop_pc)
                   ipa_expr_eval_result,
                   paddress (ipa_error_tracepoint));
 
-      if (debug_threads)
-       {
-         if (ipa_trace_buffer_is_full)
-           trace_debug ("lib stopped due to full buffer.");
-         if (ipa_stopping_tracepoint)
-           trace_debug ("lib stopped due to tpoint");
-         if (ipa_error_tracepoint)
-           trace_debug ("lib stopped due to error");
-       }
+      if (ipa_trace_buffer_is_full)
+       trace_debug ("lib stopped due to full buffer.");
+
+      if (ipa_stopping_tracepoint)
+       trace_debug ("lib stopped due to tpoint");
+
+      if (ipa_error_tracepoint)
+       trace_debug ("lib stopped due to error");
 
       if (ipa_stopping_tracepoint != 0)
        {
@@ -4588,7 +4564,7 @@ tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc)
          && tpoint->type != static_tracepoint)
        {
          trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
-                      target_pid_to_str (tinfo->id),
+                      target_pid_to_str (tinfo->id).c_str (),
                       tpoint->number, paddress (tpoint->address));
 
          /* Test the condition if present, and collect if true.  */
@@ -6899,8 +6875,13 @@ init_named_socket (const char *name)
 
   addr.sun_family = AF_UNIX;
 
-  strncpy (addr.sun_path, name, UNIX_PATH_MAX);
-  addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
+  if (strlen (name) >= ARRAY_SIZE (addr.sun_path))
+    {
+      warning ("socket name too long for sockaddr_un::sun_path field: %s", name);
+      return -1;
+    }
+
+  strcpy (addr.sun_path, name);
 
   result = access (name, F_OK);
   if (result == 0)
@@ -6942,8 +6923,8 @@ gdb_agent_socket_init (void)
 {
   int result, fd;
 
-  result = xsnprintf (agent_socket_name, UNIX_PATH_MAX, "%s/gdb_ust%d",
-                     SOCK_DIR, getpid ());
+  result = snprintf (agent_socket_name, UNIX_PATH_MAX, "%s/gdb_ust%d",
+                    SOCK_DIR, getpid ());
   if (result >= UNIX_PATH_MAX)
     {
       trace_debug ("string overflow allocating socket name");