PR29262, memory leak in pr_function_type
[binutils-gdb.git] / gdbserver / tracepoint.cc
index bbca48b2efd36c2bd3f9b427b6f293eec94398f2..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);
@@ -863,7 +855,7 @@ EXTERN_C_POP
 
 static struct tracepoint *last_tracepoint;
 
-static const char *eval_result_names[] =
+static const char * const eval_result_names[] =
   {
     "terror:in the attic",  /* this should never be reported */
     "terror:empty expression",
@@ -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
 
@@ -2433,7 +2425,7 @@ clear_installed_tracepoints (void)
   struct tracepoint *tpoint;
   struct tracepoint *prev_stpoint;
 
-  pause_all (1);
+  target_pause_all (true);
 
   prev_stpoint = NULL;
 
@@ -2486,7 +2478,7 @@ clear_installed_tracepoints (void)
       tpoint->handle = NULL;
     }
 
-  unpause_all (1);
+  target_unpause_all (true);
 }
 
 /* Parse a packet that defines a tracepoint.  */
@@ -2602,14 +2594,14 @@ cmd_qtdp (char *own_buf)
       struct tracepoint *tp = NULL;
 
       /* Pause all threads temporarily while we patch tracepoints.  */
-      pause_all (0);
+      target_pause_all (false);
 
       /* download_tracepoint will update global `tracepoints'
         list, so it is unsafe to leave threads in jump pad.  */
-      stabilize_threads ();
+      target_stabilize_threads ();
 
       /* Freeze threads.  */
-      pause_all (1);
+      target_pause_all (true);
 
 
       if (tpoint->type != trap_tracepoint)
@@ -2658,7 +2650,7 @@ cmd_qtdp (char *own_buf)
            write_ok (own_buf);
        }
 
-      unpause_all (1);
+      target_unpause_all (true);
       return;
     }
 
@@ -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 "
@@ -3117,17 +3098,11 @@ install_fast_tracepoint (struct tracepoint *tpoint, char *errbuf)
   trampoline_size = 0;
 
   /* Install the jump pad.  */
-  err = install_fast_tracepoint_jump_pad (tpoint->obj_addr_on_target,
-                                         tpoint->address,
-                                         collect,
-                                         ipa_sym_addrs.addr_collecting,
-                                         tpoint->orig_size,
-                                         &jentry,
-                                         &trampoline, &trampoline_size,
-                                         fjump, &fjump_size,
-                                         &tpoint->adjusted_insn_addr,
-                                         &tpoint->adjusted_insn_addr_end,
-                                         errbuf);
+  err = target_install_fast_tracepoint_jump_pad
+    (tpoint->obj_addr_on_target, tpoint->address, collect,
+     ipa_sym_addrs.addr_collecting, tpoint->orig_size, &jentry,
+     &trampoline, &trampoline_size, fjump, &fjump_size,
+     &tpoint->adjusted_insn_addr, &tpoint->adjusted_insn_addr_end, errbuf);
 
   if (err)
     return 1;
@@ -3220,16 +3195,16 @@ cmd_qtstart (char *packet)
   trace_debug ("Starting the trace");
 
   /* Pause all threads temporarily while we patch tracepoints.  */
-  pause_all (0);
+  target_pause_all (false);
 
   /* Get threads out of jump pads.  Safe to do here, since this is a
      top level command.  And, required to do here, since we're
      deleting/rewriting jump pads.  */
 
-  stabilize_threads ();
+  target_stabilize_threads ();
 
   /* Freeze threads.  */
-  pause_all (1);
+  target_pause_all (true);
 
   /* Sync the fast tracepoints list in the inferior ftlib.  */
   if (agent_loaded_p ())
@@ -3248,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.  */
@@ -3370,7 +3345,7 @@ cmd_qtstart (char *packet)
       clear_installed_tracepoints ();
       if (*packet == '\0')
        write_enn (packet);
-      unpause_all (1);
+      target_unpause_all (true);
       return;
     }
 
@@ -3418,7 +3393,7 @@ cmd_qtstart (char *packet)
        error ("Error setting flush_trace_buffer breakpoint");
     }
 
-  unpause_all (1);
+  target_unpause_all (true);
 
   write_ok (packet);
 }
@@ -3445,7 +3420,7 @@ stop_tracing (void)
      when we're sure we can move all threads out of the jump pads).
      We can't now, since we may be getting here due to the inferior
      agent calling us.  */
-  pause_all (1);
+  target_pause_all (true);
 
   /* Stop logging. Tracepoints can still be hit, but they will not be
      recorded.  */
@@ -3522,7 +3497,7 @@ stop_tracing (void)
       flush_trace_buffer_bkpt = NULL;
     }
 
-  unpause_all (1);
+  target_unpause_all (true);
 }
 
 static int
@@ -3668,11 +3643,11 @@ cmd_qtstatus (char *packet)
 
   if (agent_loaded_p ())
     {
-      pause_all (1);
+      target_pause_all (true);
 
       upload_fast_traceframes ();
 
-      unpause_all (1);
+      target_unpause_all (true);
    }
 
   stop_reason_rsp = (char *) tracing_stop_reason;
@@ -3981,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;
     }
 }
 
@@ -4414,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;
@@ -4427,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;
@@ -4447,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.  */
@@ -4532,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)
        {
@@ -4594,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.  */
@@ -6578,12 +6548,12 @@ upload_fast_traceframes (void)
 
   trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx);
 
-  pause_all (1);
+  target_pause_all (true);
 
   delete_breakpoint (about_to_request_buffer_space_bkpt);
   about_to_request_buffer_space_bkpt = NULL;
 
-  unpause_all (1);
+  target_unpause_all (true);
 
   if (trace_buffer_is_full)
     stop_tracing ();
@@ -6861,13 +6831,13 @@ run_inferior_command (char *cmd, int len)
 
   trace_debug ("run_inferior_command: running: %s", cmd);
 
-  pause_all (0);
+  target_pause_all (false);
   uninsert_all_breakpoints ();
 
   err = agent_run_command (pid, (const char *) cmd, len);
 
   reinsert_all_breakpoints ();
-  unpause_all (0);
+  target_unpause_all (false);
 
   return err;
 }
@@ -6905,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)
@@ -6948,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");