Home | History | Annotate | Line # | Download | only in gdbserver
tracepoint.cc revision 1.1
      1  1.1  christos /* Tracepoint code for remote server for GDB.
      2  1.1  christos    Copyright (C) 2009-2020 Free Software Foundation, Inc.
      3  1.1  christos 
      4  1.1  christos    This file is part of GDB.
      5  1.1  christos 
      6  1.1  christos    This program is free software; you can redistribute it and/or modify
      7  1.1  christos    it under the terms of the GNU General Public License as published by
      8  1.1  christos    the Free Software Foundation; either version 3 of the License, or
      9  1.1  christos    (at your option) any later version.
     10  1.1  christos 
     11  1.1  christos    This program is distributed in the hope that it will be useful,
     12  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14  1.1  christos    GNU General Public License for more details.
     15  1.1  christos 
     16  1.1  christos    You should have received a copy of the GNU General Public License
     17  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     18  1.1  christos 
     19  1.1  christos #include "server.h"
     20  1.1  christos #include "tracepoint.h"
     21  1.1  christos #include "gdbthread.h"
     22  1.1  christos #include "gdbsupport/rsp-low.h"
     23  1.1  christos 
     24  1.1  christos #include <ctype.h>
     25  1.1  christos #include <fcntl.h>
     26  1.1  christos #include <unistd.h>
     27  1.1  christos #include <chrono>
     28  1.1  christos #include <inttypes.h>
     29  1.1  christos #include "ax.h"
     30  1.1  christos #include "tdesc.h"
     31  1.1  christos 
     32  1.1  christos #define IPA_SYM_STRUCT_NAME ipa_sym_addresses
     33  1.1  christos #include "gdbsupport/agent.h"
     34  1.1  christos 
     35  1.1  christos #define DEFAULT_TRACE_BUFFER_SIZE 5242880 /* 5*1024*1024 */
     36  1.1  christos 
     37  1.1  christos /* This file is built for both GDBserver, and the in-process
     38  1.1  christos    agent (IPA), a shared library that includes a tracing agent that is
     39  1.1  christos    loaded by the inferior to support fast tracepoints.  Fast
     40  1.1  christos    tracepoints (or more accurately, jump based tracepoints) are
     41  1.1  christos    implemented by patching the tracepoint location with a jump into a
     42  1.1  christos    small trampoline function whose job is to save the register state,
     43  1.1  christos    call the in-process tracing agent, and then execute the original
     44  1.1  christos    instruction that was under the tracepoint jump (possibly adjusted,
     45  1.1  christos    if PC-relative, or some such).
     46  1.1  christos 
     47  1.1  christos    The current synchronization design is pull based.  That means,
     48  1.1  christos    GDBserver does most of the work, by peeking/poking at the inferior
     49  1.1  christos    agent's memory directly for downloading tracepoint and associated
     50  1.1  christos    objects, and for uploading trace frames.  Whenever the IPA needs
     51  1.1  christos    something from GDBserver (trace buffer is full, tracing stopped for
     52  1.1  christos    some reason, etc.) the IPA calls a corresponding hook function
     53  1.1  christos    where GDBserver has placed a breakpoint.
     54  1.1  christos 
     55  1.1  christos    Each of the agents has its own trace buffer.  When browsing the
     56  1.1  christos    trace frames built from slow and fast tracepoints from GDB (tfind
     57  1.1  christos    mode), there's no guarantee the user is seeing the trace frames in
     58  1.1  christos    strict chronological creation order, although, GDBserver tries to
     59  1.1  christos    keep the order relatively reasonable, by syncing the trace buffers
     60  1.1  christos    at appropriate times.
     61  1.1  christos 
     62  1.1  christos */
     63  1.1  christos 
     64  1.1  christos #ifdef IN_PROCESS_AGENT
     65  1.1  christos 
     66  1.1  christos static void trace_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
     67  1.1  christos 
     68  1.1  christos static void
     69  1.1  christos trace_vdebug (const char *fmt, ...)
     70  1.1  christos {
     71  1.1  christos   char buf[1024];
     72  1.1  christos   va_list ap;
     73  1.1  christos 
     74  1.1  christos   va_start (ap, fmt);
     75  1.1  christos   vsprintf (buf, fmt, ap);
     76  1.1  christos   fprintf (stderr, PROG "/tracepoint: %s\n", buf);
     77  1.1  christos   va_end (ap);
     78  1.1  christos }
     79  1.1  christos 
     80  1.1  christos #define trace_debug_1(level, fmt, args...)	\
     81  1.1  christos   do {						\
     82  1.1  christos     if (level <= debug_threads)		\
     83  1.1  christos       trace_vdebug ((fmt), ##args);		\
     84  1.1  christos   } while (0)
     85  1.1  christos 
     86  1.1  christos #else
     87  1.1  christos 
     88  1.1  christos #define trace_debug_1(level, fmt, args...)	\
     89  1.1  christos   do {						\
     90  1.1  christos     if (level <= debug_threads)			\
     91  1.1  christos       {						\
     92  1.1  christos 	debug_printf ((fmt), ##args);		\
     93  1.1  christos 	debug_printf ("\n");			\
     94  1.1  christos       }						\
     95  1.1  christos   } while (0)
     96  1.1  christos 
     97  1.1  christos #endif
     98  1.1  christos 
     99  1.1  christos #define trace_debug(FMT, args...)		\
    100  1.1  christos   trace_debug_1 (1, FMT, ##args)
    101  1.1  christos 
    102  1.1  christos /* Prefix exported symbols, for good citizenship.  All the symbols
    103  1.1  christos    that need exporting are defined in this module.  Note that all
    104  1.1  christos    these symbols must be tagged with IP_AGENT_EXPORT_*.  */
    105  1.1  christos #ifdef IN_PROCESS_AGENT
    106  1.1  christos # define gdb_tp_heap_buffer IPA_SYM_EXPORTED_NAME (gdb_tp_heap_buffer)
    107  1.1  christos # define gdb_jump_pad_buffer IPA_SYM_EXPORTED_NAME (gdb_jump_pad_buffer)
    108  1.1  christos # define gdb_jump_pad_buffer_end IPA_SYM_EXPORTED_NAME (gdb_jump_pad_buffer_end)
    109  1.1  christos # define gdb_trampoline_buffer IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer)
    110  1.1  christos # define gdb_trampoline_buffer_end IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer_end)
    111  1.1  christos # define gdb_trampoline_buffer_error IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer_error)
    112  1.1  christos # define collecting IPA_SYM_EXPORTED_NAME (collecting)
    113  1.1  christos # define gdb_collect_ptr IPA_SYM_EXPORTED_NAME (gdb_collect_ptr)
    114  1.1  christos # define stop_tracing IPA_SYM_EXPORTED_NAME (stop_tracing)
    115  1.1  christos # define flush_trace_buffer IPA_SYM_EXPORTED_NAME (flush_trace_buffer)
    116  1.1  christos # define about_to_request_buffer_space IPA_SYM_EXPORTED_NAME (about_to_request_buffer_space)
    117  1.1  christos # define trace_buffer_is_full IPA_SYM_EXPORTED_NAME (trace_buffer_is_full)
    118  1.1  christos # define stopping_tracepoint IPA_SYM_EXPORTED_NAME (stopping_tracepoint)
    119  1.1  christos # define expr_eval_result IPA_SYM_EXPORTED_NAME (expr_eval_result)
    120  1.1  christos # define error_tracepoint IPA_SYM_EXPORTED_NAME (error_tracepoint)
    121  1.1  christos # define tracepoints IPA_SYM_EXPORTED_NAME (tracepoints)
    122  1.1  christos # define tracing IPA_SYM_EXPORTED_NAME (tracing)
    123  1.1  christos # define trace_buffer_ctrl IPA_SYM_EXPORTED_NAME (trace_buffer_ctrl)
    124  1.1  christos # define trace_buffer_ctrl_curr IPA_SYM_EXPORTED_NAME (trace_buffer_ctrl_curr)
    125  1.1  christos # define trace_buffer_lo IPA_SYM_EXPORTED_NAME (trace_buffer_lo)
    126  1.1  christos # define trace_buffer_hi IPA_SYM_EXPORTED_NAME (trace_buffer_hi)
    127  1.1  christos # define traceframe_read_count IPA_SYM_EXPORTED_NAME (traceframe_read_count)
    128  1.1  christos # define traceframe_write_count IPA_SYM_EXPORTED_NAME (traceframe_write_count)
    129  1.1  christos # define traceframes_created IPA_SYM_EXPORTED_NAME (traceframes_created)
    130  1.1  christos # define trace_state_variables IPA_SYM_EXPORTED_NAME (trace_state_variables)
    131  1.1  christos # define get_raw_reg_ptr IPA_SYM_EXPORTED_NAME (get_raw_reg_ptr)
    132  1.1  christos # define get_trace_state_variable_value_ptr \
    133  1.1  christos   IPA_SYM_EXPORTED_NAME (get_trace_state_variable_value_ptr)
    134  1.1  christos # define set_trace_state_variable_value_ptr \
    135  1.1  christos   IPA_SYM_EXPORTED_NAME (set_trace_state_variable_value_ptr)
    136  1.1  christos # define ust_loaded IPA_SYM_EXPORTED_NAME (ust_loaded)
    137  1.1  christos # define helper_thread_id IPA_SYM_EXPORTED_NAME (helper_thread_id)
    138  1.1  christos # define cmd_buf IPA_SYM_EXPORTED_NAME (cmd_buf)
    139  1.1  christos # define ipa_tdesc_idx IPA_SYM_EXPORTED_NAME (ipa_tdesc_idx)
    140  1.1  christos #endif
    141  1.1  christos 
    142  1.1  christos #ifndef IN_PROCESS_AGENT
    143  1.1  christos 
    144  1.1  christos /* Addresses of in-process agent's symbols GDBserver cares about.  */
    145  1.1  christos 
    146  1.1  christos struct ipa_sym_addresses
    147  1.1  christos {
    148  1.1  christos   CORE_ADDR addr_gdb_tp_heap_buffer;
    149  1.1  christos   CORE_ADDR addr_gdb_jump_pad_buffer;
    150  1.1  christos   CORE_ADDR addr_gdb_jump_pad_buffer_end;
    151  1.1  christos   CORE_ADDR addr_gdb_trampoline_buffer;
    152  1.1  christos   CORE_ADDR addr_gdb_trampoline_buffer_end;
    153  1.1  christos   CORE_ADDR addr_gdb_trampoline_buffer_error;
    154  1.1  christos   CORE_ADDR addr_collecting;
    155  1.1  christos   CORE_ADDR addr_gdb_collect_ptr;
    156  1.1  christos   CORE_ADDR addr_stop_tracing;
    157  1.1  christos   CORE_ADDR addr_flush_trace_buffer;
    158  1.1  christos   CORE_ADDR addr_about_to_request_buffer_space;
    159  1.1  christos   CORE_ADDR addr_trace_buffer_is_full;
    160  1.1  christos   CORE_ADDR addr_stopping_tracepoint;
    161  1.1  christos   CORE_ADDR addr_expr_eval_result;
    162  1.1  christos   CORE_ADDR addr_error_tracepoint;
    163  1.1  christos   CORE_ADDR addr_tracepoints;
    164  1.1  christos   CORE_ADDR addr_tracing;
    165  1.1  christos   CORE_ADDR addr_trace_buffer_ctrl;
    166  1.1  christos   CORE_ADDR addr_trace_buffer_ctrl_curr;
    167  1.1  christos   CORE_ADDR addr_trace_buffer_lo;
    168  1.1  christos   CORE_ADDR addr_trace_buffer_hi;
    169  1.1  christos   CORE_ADDR addr_traceframe_read_count;
    170  1.1  christos   CORE_ADDR addr_traceframe_write_count;
    171  1.1  christos   CORE_ADDR addr_traceframes_created;
    172  1.1  christos   CORE_ADDR addr_trace_state_variables;
    173  1.1  christos   CORE_ADDR addr_get_raw_reg_ptr;
    174  1.1  christos   CORE_ADDR addr_get_trace_state_variable_value_ptr;
    175  1.1  christos   CORE_ADDR addr_set_trace_state_variable_value_ptr;
    176  1.1  christos   CORE_ADDR addr_ust_loaded;
    177  1.1  christos   CORE_ADDR addr_ipa_tdesc_idx;
    178  1.1  christos };
    179  1.1  christos 
    180  1.1  christos static struct
    181  1.1  christos {
    182  1.1  christos   const char *name;
    183  1.1  christos   int offset;
    184  1.1  christos } symbol_list[] = {
    185  1.1  christos   IPA_SYM(gdb_tp_heap_buffer),
    186  1.1  christos   IPA_SYM(gdb_jump_pad_buffer),
    187  1.1  christos   IPA_SYM(gdb_jump_pad_buffer_end),
    188  1.1  christos   IPA_SYM(gdb_trampoline_buffer),
    189  1.1  christos   IPA_SYM(gdb_trampoline_buffer_end),
    190  1.1  christos   IPA_SYM(gdb_trampoline_buffer_error),
    191  1.1  christos   IPA_SYM(collecting),
    192  1.1  christos   IPA_SYM(gdb_collect_ptr),
    193  1.1  christos   IPA_SYM(stop_tracing),
    194  1.1  christos   IPA_SYM(flush_trace_buffer),
    195  1.1  christos   IPA_SYM(about_to_request_buffer_space),
    196  1.1  christos   IPA_SYM(trace_buffer_is_full),
    197  1.1  christos   IPA_SYM(stopping_tracepoint),
    198  1.1  christos   IPA_SYM(expr_eval_result),
    199  1.1  christos   IPA_SYM(error_tracepoint),
    200  1.1  christos   IPA_SYM(tracepoints),
    201  1.1  christos   IPA_SYM(tracing),
    202  1.1  christos   IPA_SYM(trace_buffer_ctrl),
    203  1.1  christos   IPA_SYM(trace_buffer_ctrl_curr),
    204  1.1  christos   IPA_SYM(trace_buffer_lo),
    205  1.1  christos   IPA_SYM(trace_buffer_hi),
    206  1.1  christos   IPA_SYM(traceframe_read_count),
    207  1.1  christos   IPA_SYM(traceframe_write_count),
    208  1.1  christos   IPA_SYM(traceframes_created),
    209  1.1  christos   IPA_SYM(trace_state_variables),
    210  1.1  christos   IPA_SYM(get_raw_reg_ptr),
    211  1.1  christos   IPA_SYM(get_trace_state_variable_value_ptr),
    212  1.1  christos   IPA_SYM(set_trace_state_variable_value_ptr),
    213  1.1  christos   IPA_SYM(ust_loaded),
    214  1.1  christos   IPA_SYM(ipa_tdesc_idx),
    215  1.1  christos };
    216  1.1  christos 
    217  1.1  christos static struct ipa_sym_addresses ipa_sym_addrs;
    218  1.1  christos 
    219  1.1  christos static int read_inferior_integer (CORE_ADDR symaddr, int *val);
    220  1.1  christos 
    221  1.1  christos /* Returns true if both the in-process agent library and the static
    222  1.1  christos    tracepoints libraries are loaded in the inferior, and agent has
    223  1.1  christos    capability on static tracepoints.  */
    224  1.1  christos 
    225  1.1  christos static int
    226  1.1  christos in_process_agent_supports_ust (void)
    227  1.1  christos {
    228  1.1  christos   int loaded = 0;
    229  1.1  christos 
    230  1.1  christos   if (!agent_loaded_p ())
    231  1.1  christos     {
    232  1.1  christos       warning ("In-process agent not loaded");
    233  1.1  christos       return 0;
    234  1.1  christos     }
    235  1.1  christos 
    236  1.1  christos   if (agent_capability_check (AGENT_CAPA_STATIC_TRACE))
    237  1.1  christos     {
    238  1.1  christos       /* Agent understands static tracepoint, then check whether UST is in
    239  1.1  christos 	 fact loaded in the inferior.  */
    240  1.1  christos       if (read_inferior_integer (ipa_sym_addrs.addr_ust_loaded, &loaded))
    241  1.1  christos 	{
    242  1.1  christos 	  warning ("Error reading ust_loaded in lib");
    243  1.1  christos 	  return 0;
    244  1.1  christos 	}
    245  1.1  christos 
    246  1.1  christos       return loaded;
    247  1.1  christos     }
    248  1.1  christos   else
    249  1.1  christos     return 0;
    250  1.1  christos }
    251  1.1  christos 
    252  1.1  christos static void
    253  1.1  christos write_e_ipa_not_loaded (char *buffer)
    254  1.1  christos {
    255  1.1  christos   sprintf (buffer,
    256  1.1  christos 	   "E.In-process agent library not loaded in process.  "
    257  1.1  christos 	   "Fast and static tracepoints unavailable.");
    258  1.1  christos }
    259  1.1  christos 
    260  1.1  christos /* Write an error to BUFFER indicating that UST isn't loaded in the
    261  1.1  christos    inferior.  */
    262  1.1  christos 
    263  1.1  christos static void
    264  1.1  christos write_e_ust_not_loaded (char *buffer)
    265  1.1  christos {
    266  1.1  christos #ifdef HAVE_UST
    267  1.1  christos   sprintf (buffer,
    268  1.1  christos 	   "E.UST library not loaded in process.  "
    269  1.1  christos 	   "Static tracepoints unavailable.");
    270  1.1  christos #else
    271  1.1  christos   sprintf (buffer, "E.GDBserver was built without static tracepoints support");
    272  1.1  christos #endif
    273  1.1  christos }
    274  1.1  christos 
    275  1.1  christos /* If the in-process agent library isn't loaded in the inferior, write
    276  1.1  christos    an error to BUFFER, and return 1.  Otherwise, return 0.  */
    277  1.1  christos 
    278  1.1  christos static int
    279  1.1  christos maybe_write_ipa_not_loaded (char *buffer)
    280  1.1  christos {
    281  1.1  christos   if (!agent_loaded_p ())
    282  1.1  christos     {
    283  1.1  christos       write_e_ipa_not_loaded (buffer);
    284  1.1  christos       return 1;
    285  1.1  christos     }
    286  1.1  christos   return 0;
    287  1.1  christos }
    288  1.1  christos 
    289  1.1  christos /* If the in-process agent library and the ust (static tracepoints)
    290  1.1  christos    library aren't loaded in the inferior, write an error to BUFFER,
    291  1.1  christos    and return 1.  Otherwise, return 0.  */
    292  1.1  christos 
    293  1.1  christos static int
    294  1.1  christos maybe_write_ipa_ust_not_loaded (char *buffer)
    295  1.1  christos {
    296  1.1  christos   if (!agent_loaded_p ())
    297  1.1  christos     {
    298  1.1  christos       write_e_ipa_not_loaded (buffer);
    299  1.1  christos       return 1;
    300  1.1  christos     }
    301  1.1  christos   else if (!in_process_agent_supports_ust ())
    302  1.1  christos     {
    303  1.1  christos       write_e_ust_not_loaded (buffer);
    304  1.1  christos       return 1;
    305  1.1  christos     }
    306  1.1  christos   return 0;
    307  1.1  christos }
    308  1.1  christos 
    309  1.1  christos /* Cache all future symbols that the tracepoints module might request.
    310  1.1  christos    We can not request symbols at arbitrary states in the remote
    311  1.1  christos    protocol, only when the client tells us that new symbols are
    312  1.1  christos    available.  So when we load the in-process library, make sure to
    313  1.1  christos    check the entire list.  */
    314  1.1  christos 
    315  1.1  christos void
    316  1.1  christos tracepoint_look_up_symbols (void)
    317  1.1  christos {
    318  1.1  christos   int i;
    319  1.1  christos 
    320  1.1  christos   if (agent_loaded_p ())
    321  1.1  christos     return;
    322  1.1  christos 
    323  1.1  christos   for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
    324  1.1  christos     {
    325  1.1  christos       CORE_ADDR *addrp =
    326  1.1  christos 	(CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);
    327  1.1  christos 
    328  1.1  christos       if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0)
    329  1.1  christos 	{
    330  1.1  christos 	  if (debug_threads)
    331  1.1  christos 	    debug_printf ("symbol `%s' not found\n", symbol_list[i].name);
    332  1.1  christos 	  return;
    333  1.1  christos 	}
    334  1.1  christos     }
    335  1.1  christos 
    336  1.1  christos   agent_look_up_symbols (NULL);
    337  1.1  christos }
    338  1.1  christos 
    339  1.1  christos #endif
    340  1.1  christos 
    341  1.1  christos /* GDBserver places a breakpoint on the IPA's version (which is a nop)
    342  1.1  christos    of the "stop_tracing" function.  When this breakpoint is hit,
    343  1.1  christos    tracing stopped in the IPA for some reason.  E.g., due to
    344  1.1  christos    tracepoint reaching the pass count, hitting conditional expression
    345  1.1  christos    evaluation error, etc.
    346  1.1  christos 
    347  1.1  christos    The IPA's trace buffer is never in circular tracing mode: instead,
    348  1.1  christos    GDBserver's is, and whenever the in-process buffer fills, it calls
    349  1.1  christos    "flush_trace_buffer", which triggers an internal breakpoint.
    350  1.1  christos    GDBserver reacts to this breakpoint by pulling the meanwhile
    351  1.1  christos    collected data.  Old frames discarding is always handled on the
    352  1.1  christos    GDBserver side.  */
    353  1.1  christos 
    354  1.1  christos #ifdef IN_PROCESS_AGENT
    355  1.1  christos int
    356  1.1  christos read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
    357  1.1  christos {
    358  1.1  christos   memcpy (myaddr, (void *) (uintptr_t) memaddr, len);
    359  1.1  christos   return 0;
    360  1.1  christos }
    361  1.1  christos 
    362  1.1  christos /* Call this in the functions where GDBserver places a breakpoint, so
    363  1.1  christos    that the compiler doesn't try to be clever and skip calling the
    364  1.1  christos    function at all.  This is necessary, even if we tell the compiler
    365  1.1  christos    to not inline said functions.  */
    366  1.1  christos 
    367  1.1  christos #if defined(__GNUC__)
    368  1.1  christos #  define UNKNOWN_SIDE_EFFECTS() asm ("")
    369  1.1  christos #else
    370  1.1  christos #  define UNKNOWN_SIDE_EFFECTS() do {} while (0)
    371  1.1  christos #endif
    372  1.1  christos 
    373  1.1  christos /* This is needed for -Wmissing-declarations.  */
    374  1.1  christos IP_AGENT_EXPORT_FUNC void stop_tracing (void);
    375  1.1  christos 
    376  1.1  christos IP_AGENT_EXPORT_FUNC void
    377  1.1  christos stop_tracing (void)
    378  1.1  christos {
    379  1.1  christos   /* GDBserver places breakpoint here.  */
    380  1.1  christos   UNKNOWN_SIDE_EFFECTS();
    381  1.1  christos }
    382  1.1  christos 
    383  1.1  christos /* This is needed for -Wmissing-declarations.  */
    384  1.1  christos IP_AGENT_EXPORT_FUNC void flush_trace_buffer (void);
    385  1.1  christos 
    386  1.1  christos IP_AGENT_EXPORT_FUNC void
    387  1.1  christos flush_trace_buffer (void)
    388  1.1  christos {
    389  1.1  christos   /* GDBserver places breakpoint here.  */
    390  1.1  christos   UNKNOWN_SIDE_EFFECTS();
    391  1.1  christos }
    392  1.1  christos 
    393  1.1  christos #endif
    394  1.1  christos 
    395  1.1  christos #ifndef IN_PROCESS_AGENT
    396  1.1  christos static int
    397  1.1  christos tracepoint_handler (CORE_ADDR address)
    398  1.1  christos {
    399  1.1  christos   trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
    400  1.1  christos 	       paddress (address));
    401  1.1  christos   return 0;
    402  1.1  christos }
    403  1.1  christos 
    404  1.1  christos /* Breakpoint at "stop_tracing" in the inferior lib.  */
    405  1.1  christos struct breakpoint *stop_tracing_bkpt;
    406  1.1  christos static int stop_tracing_handler (CORE_ADDR);
    407  1.1  christos 
    408  1.1  christos /* Breakpoint at "flush_trace_buffer" in the inferior lib.  */
    409  1.1  christos struct breakpoint *flush_trace_buffer_bkpt;
    410  1.1  christos static int flush_trace_buffer_handler (CORE_ADDR);
    411  1.1  christos 
    412  1.1  christos static void download_trace_state_variables (void);
    413  1.1  christos static void upload_fast_traceframes (void);
    414  1.1  christos 
    415  1.1  christos static int run_inferior_command (char *cmd, int len);
    416  1.1  christos 
    417  1.1  christos static int
    418  1.1  christos read_inferior_integer (CORE_ADDR symaddr, int *val)
    419  1.1  christos {
    420  1.1  christos   return read_inferior_memory (symaddr, (unsigned char *) val,
    421  1.1  christos 			       sizeof (*val));
    422  1.1  christos }
    423  1.1  christos 
    424  1.1  christos struct tracepoint;
    425  1.1  christos static int tracepoint_send_agent (struct tracepoint *tpoint);
    426  1.1  christos 
    427  1.1  christos static int
    428  1.1  christos read_inferior_uinteger (CORE_ADDR symaddr, unsigned int *val)
    429  1.1  christos {
    430  1.1  christos   return read_inferior_memory (symaddr, (unsigned char *) val,
    431  1.1  christos 			       sizeof (*val));
    432  1.1  christos }
    433  1.1  christos 
    434  1.1  christos static int
    435  1.1  christos read_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR *val)
    436  1.1  christos {
    437  1.1  christos   void *pval = (void *) (uintptr_t) val;
    438  1.1  christos   int ret;
    439  1.1  christos 
    440  1.1  christos   ret = read_inferior_memory (symaddr, (unsigned char *) &pval, sizeof (pval));
    441  1.1  christos   *val = (uintptr_t) pval;
    442  1.1  christos   return ret;
    443  1.1  christos }
    444  1.1  christos 
    445  1.1  christos static int
    446  1.1  christos write_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR val)
    447  1.1  christos {
    448  1.1  christos   void *pval = (void *) (uintptr_t) val;
    449  1.1  christos   return target_write_memory (symaddr,
    450  1.1  christos 				(unsigned char *) &pval, sizeof (pval));
    451  1.1  christos }
    452  1.1  christos 
    453  1.1  christos static int
    454  1.1  christos write_inferior_integer (CORE_ADDR symaddr, int val)
    455  1.1  christos {
    456  1.1  christos   return target_write_memory (symaddr, (unsigned char *) &val, sizeof (val));
    457  1.1  christos }
    458  1.1  christos 
    459  1.1  christos static int
    460  1.1  christos write_inferior_int8 (CORE_ADDR symaddr, int8_t val)
    461  1.1  christos {
    462  1.1  christos   return target_write_memory (symaddr, (unsigned char *) &val, sizeof (val));
    463  1.1  christos }
    464  1.1  christos 
    465  1.1  christos static int
    466  1.1  christos write_inferior_uinteger (CORE_ADDR symaddr, unsigned int val)
    467  1.1  christos {
    468  1.1  christos   return target_write_memory (symaddr, (unsigned char *) &val, sizeof (val));
    469  1.1  christos }
    470  1.1  christos 
    471  1.1  christos static CORE_ADDR target_malloc (ULONGEST size);
    472  1.1  christos 
    473  1.1  christos #define COPY_FIELD_TO_BUF(BUF, OBJ, FIELD)	\
    474  1.1  christos   do {							\
    475  1.1  christos     memcpy (BUF, &(OBJ)->FIELD, sizeof ((OBJ)->FIELD)); \
    476  1.1  christos     BUF += sizeof ((OBJ)->FIELD);			\
    477  1.1  christos   } while (0)
    478  1.1  christos 
    479  1.1  christos #endif
    480  1.1  christos 
    481  1.1  christos /* Base action.  Concrete actions inherit this.  */
    482  1.1  christos 
    483  1.1  christos struct tracepoint_action
    484  1.1  christos {
    485  1.1  christos   char type;
    486  1.1  christos };
    487  1.1  christos 
    488  1.1  christos /* An 'M' (collect memory) action.  */
    489  1.1  christos struct collect_memory_action
    490  1.1  christos {
    491  1.1  christos   struct tracepoint_action base;
    492  1.1  christos 
    493  1.1  christos   ULONGEST addr;
    494  1.1  christos   ULONGEST len;
    495  1.1  christos   int32_t basereg;
    496  1.1  christos };
    497  1.1  christos 
    498  1.1  christos /* An 'R' (collect registers) action.  */
    499  1.1  christos 
    500  1.1  christos struct collect_registers_action
    501  1.1  christos {
    502  1.1  christos   struct tracepoint_action base;
    503  1.1  christos };
    504  1.1  christos 
    505  1.1  christos /* An 'X' (evaluate expression) action.  */
    506  1.1  christos 
    507  1.1  christos struct eval_expr_action
    508  1.1  christos {
    509  1.1  christos   struct tracepoint_action base;
    510  1.1  christos 
    511  1.1  christos   struct agent_expr *expr;
    512  1.1  christos };
    513  1.1  christos 
    514  1.1  christos /* An 'L' (collect static trace data) action.  */
    515  1.1  christos struct collect_static_trace_data_action
    516  1.1  christos {
    517  1.1  christos   struct tracepoint_action base;
    518  1.1  christos };
    519  1.1  christos 
    520  1.1  christos #ifndef IN_PROCESS_AGENT
    521  1.1  christos static CORE_ADDR
    522  1.1  christos m_tracepoint_action_download (const struct tracepoint_action *action)
    523  1.1  christos {
    524  1.1  christos   CORE_ADDR ipa_action = target_malloc (sizeof (struct collect_memory_action));
    525  1.1  christos 
    526  1.1  christos   target_write_memory (ipa_action, (unsigned char *) action,
    527  1.1  christos 			 sizeof (struct collect_memory_action));
    528  1.1  christos 
    529  1.1  christos   return ipa_action;
    530  1.1  christos }
    531  1.1  christos static char *
    532  1.1  christos m_tracepoint_action_send (char *buffer, const struct tracepoint_action *action)
    533  1.1  christos {
    534  1.1  christos   struct collect_memory_action *maction
    535  1.1  christos     = (struct collect_memory_action *) action;
    536  1.1  christos 
    537  1.1  christos   COPY_FIELD_TO_BUF (buffer, maction, addr);
    538  1.1  christos   COPY_FIELD_TO_BUF (buffer, maction, len);
    539  1.1  christos   COPY_FIELD_TO_BUF (buffer, maction, basereg);
    540  1.1  christos 
    541  1.1  christos   return buffer;
    542  1.1  christos }
    543  1.1  christos 
    544  1.1  christos static CORE_ADDR
    545  1.1  christos r_tracepoint_action_download (const struct tracepoint_action *action)
    546  1.1  christos {
    547  1.1  christos   CORE_ADDR ipa_action = target_malloc (sizeof (struct collect_registers_action));
    548  1.1  christos 
    549  1.1  christos   target_write_memory (ipa_action, (unsigned char *) action,
    550  1.1  christos 			 sizeof (struct collect_registers_action));
    551  1.1  christos 
    552  1.1  christos   return ipa_action;
    553  1.1  christos }
    554  1.1  christos 
    555  1.1  christos static char *
    556  1.1  christos r_tracepoint_action_send (char *buffer, const struct tracepoint_action *action)
    557  1.1  christos {
    558  1.1  christos   return buffer;
    559  1.1  christos }
    560  1.1  christos 
    561  1.1  christos static CORE_ADDR download_agent_expr (struct agent_expr *expr);
    562  1.1  christos 
    563  1.1  christos static CORE_ADDR
    564  1.1  christos x_tracepoint_action_download (const struct tracepoint_action *action)
    565  1.1  christos {
    566  1.1  christos   CORE_ADDR ipa_action = target_malloc (sizeof (struct eval_expr_action));
    567  1.1  christos   CORE_ADDR expr;
    568  1.1  christos 
    569  1.1  christos   target_write_memory (ipa_action, (unsigned char *) action,
    570  1.1  christos 			 sizeof (struct eval_expr_action));
    571  1.1  christos   expr = download_agent_expr (((struct eval_expr_action *) action)->expr);
    572  1.1  christos   write_inferior_data_pointer (ipa_action
    573  1.1  christos 			       + offsetof (struct eval_expr_action, expr),
    574  1.1  christos 			       expr);
    575  1.1  christos 
    576  1.1  christos   return ipa_action;
    577  1.1  christos }
    578  1.1  christos 
    579  1.1  christos /* Copy agent expression AEXPR to buffer pointed by P.  If AEXPR is NULL,
    580  1.1  christos    copy 0 to P.  Return updated header of buffer.  */
    581  1.1  christos 
    582  1.1  christos static char *
    583  1.1  christos agent_expr_send (char *p, const struct agent_expr *aexpr)
    584  1.1  christos {
    585  1.1  christos   /* Copy the length of condition first, and then copy its
    586  1.1  christos      content.  */
    587  1.1  christos   if (aexpr == NULL)
    588  1.1  christos     {
    589  1.1  christos       memset (p, 0, 4);
    590  1.1  christos       p += 4;
    591  1.1  christos     }
    592  1.1  christos   else
    593  1.1  christos     {
    594  1.1  christos       memcpy (p, &aexpr->length, 4);
    595  1.1  christos       p +=4;
    596  1.1  christos 
    597  1.1  christos       memcpy (p, aexpr->bytes, aexpr->length);
    598  1.1  christos       p += aexpr->length;
    599  1.1  christos     }
    600  1.1  christos   return p;
    601  1.1  christos }
    602  1.1  christos 
    603  1.1  christos static char *
    604  1.1  christos x_tracepoint_action_send ( char *buffer, const struct tracepoint_action *action)
    605  1.1  christos {
    606  1.1  christos   struct eval_expr_action *eaction = (struct eval_expr_action *) action;
    607  1.1  christos 
    608  1.1  christos   return agent_expr_send (buffer, eaction->expr);
    609  1.1  christos }
    610  1.1  christos 
    611  1.1  christos static CORE_ADDR
    612  1.1  christos l_tracepoint_action_download (const struct tracepoint_action *action)
    613  1.1  christos {
    614  1.1  christos   CORE_ADDR ipa_action
    615  1.1  christos     = target_malloc (sizeof (struct collect_static_trace_data_action));
    616  1.1  christos 
    617  1.1  christos   target_write_memory (ipa_action, (unsigned char *) action,
    618  1.1  christos 			 sizeof (struct collect_static_trace_data_action));
    619  1.1  christos 
    620  1.1  christos   return ipa_action;
    621  1.1  christos }
    622  1.1  christos 
    623  1.1  christos static char *
    624  1.1  christos l_tracepoint_action_send (char *buffer, const struct tracepoint_action *action)
    625  1.1  christos {
    626  1.1  christos   return buffer;
    627  1.1  christos }
    628  1.1  christos 
    629  1.1  christos static char *
    630  1.1  christos tracepoint_action_send (char *buffer, const struct tracepoint_action *action)
    631  1.1  christos {
    632  1.1  christos   switch (action->type)
    633  1.1  christos     {
    634  1.1  christos     case 'M':
    635  1.1  christos       return m_tracepoint_action_send (buffer, action);
    636  1.1  christos     case 'R':
    637  1.1  christos       return r_tracepoint_action_send (buffer, action);
    638  1.1  christos     case 'X':
    639  1.1  christos       return x_tracepoint_action_send (buffer, action);
    640  1.1  christos     case 'L':
    641  1.1  christos       return l_tracepoint_action_send (buffer, action);
    642  1.1  christos     }
    643  1.1  christos   error ("Unknown trace action '%c'.", action->type);
    644  1.1  christos }
    645  1.1  christos 
    646  1.1  christos static CORE_ADDR
    647  1.1  christos tracepoint_action_download (const struct tracepoint_action *action)
    648  1.1  christos {
    649  1.1  christos   switch (action->type)
    650  1.1  christos     {
    651  1.1  christos     case 'M':
    652  1.1  christos       return m_tracepoint_action_download (action);
    653  1.1  christos     case 'R':
    654  1.1  christos       return r_tracepoint_action_download (action);
    655  1.1  christos     case 'X':
    656  1.1  christos       return x_tracepoint_action_download (action);
    657  1.1  christos     case 'L':
    658  1.1  christos       return l_tracepoint_action_download (action);
    659  1.1  christos     }
    660  1.1  christos   error ("Unknown trace action '%c'.", action->type);
    661  1.1  christos }
    662  1.1  christos #endif
    663  1.1  christos 
    664  1.1  christos /* This structure describes a piece of the source-level definition of
    665  1.1  christos    the tracepoint.  The contents are not interpreted by the target,
    666  1.1  christos    but preserved verbatim for uploading upon reconnection.  */
    667  1.1  christos 
    668  1.1  christos struct source_string
    669  1.1  christos {
    670  1.1  christos   /* The type of string, such as "cond" for a conditional.  */
    671  1.1  christos   char *type;
    672  1.1  christos 
    673  1.1  christos   /* The source-level string itself.  For the sake of target
    674  1.1  christos      debugging, we store it in plaintext, even though it is always
    675  1.1  christos      transmitted in hex.  */
    676  1.1  christos   char *str;
    677  1.1  christos 
    678  1.1  christos   /* Link to the next one in the list.  We link them in the order
    679  1.1  christos      received, in case some make up an ordered list of commands or
    680  1.1  christos      some such.  */
    681  1.1  christos   struct source_string *next;
    682  1.1  christos };
    683  1.1  christos 
    684  1.1  christos enum tracepoint_type
    685  1.1  christos {
    686  1.1  christos   /* Trap based tracepoint.  */
    687  1.1  christos   trap_tracepoint,
    688  1.1  christos 
    689  1.1  christos   /* A fast tracepoint implemented with a jump instead of a trap.  */
    690  1.1  christos   fast_tracepoint,
    691  1.1  christos 
    692  1.1  christos   /* A static tracepoint, implemented by a program call into a tracing
    693  1.1  christos      library.  */
    694  1.1  christos   static_tracepoint
    695  1.1  christos };
    696  1.1  christos 
    697  1.1  christos struct tracepoint_hit_ctx;
    698  1.1  christos 
    699  1.1  christos typedef enum eval_result_type (*condfn) (unsigned char *,
    700  1.1  christos 					 ULONGEST *);
    701  1.1  christos 
    702  1.1  christos /* The definition of a tracepoint.  */
    703  1.1  christos 
    704  1.1  christos /* Tracepoints may have multiple locations, each at a different
    705  1.1  christos    address.  This can occur with optimizations, template
    706  1.1  christos    instantiation, etc.  Since the locations may be in different
    707  1.1  christos    scopes, the conditions and actions may be different for each
    708  1.1  christos    location.  Our target version of tracepoints is more like GDB's
    709  1.1  christos    notion of "breakpoint locations", but we have almost nothing that
    710  1.1  christos    is not per-location, so we bother having two kinds of objects.  The
    711  1.1  christos    key consequence is that numbers are not unique, and that it takes
    712  1.1  christos    both number and address to identify a tracepoint uniquely.  */
    713  1.1  christos 
    714  1.1  christos struct tracepoint
    715  1.1  christos {
    716  1.1  christos   /* The number of the tracepoint, as specified by GDB.  Several
    717  1.1  christos      tracepoint objects here may share a number.  */
    718  1.1  christos   uint32_t number;
    719  1.1  christos 
    720  1.1  christos   /* Address at which the tracepoint is supposed to trigger.  Several
    721  1.1  christos      tracepoints may share an address.  */
    722  1.1  christos   CORE_ADDR address;
    723  1.1  christos 
    724  1.1  christos   /* Tracepoint type.  */
    725  1.1  christos   enum tracepoint_type type;
    726  1.1  christos 
    727  1.1  christos   /* True if the tracepoint is currently enabled.  */
    728  1.1  christos   int8_t enabled;
    729  1.1  christos 
    730  1.1  christos   /* The number of single steps that will be performed after each
    731  1.1  christos      tracepoint hit.  */
    732  1.1  christos   uint64_t step_count;
    733  1.1  christos 
    734  1.1  christos   /* The number of times the tracepoint may be hit before it will
    735  1.1  christos      terminate the entire tracing run.  */
    736  1.1  christos   uint64_t pass_count;
    737  1.1  christos 
    738  1.1  christos   /* Pointer to the agent expression that is the tracepoint's
    739  1.1  christos      conditional, or NULL if the tracepoint is unconditional.  */
    740  1.1  christos   struct agent_expr *cond;
    741  1.1  christos 
    742  1.1  christos   /* The list of actions to take when the tracepoint triggers.  */
    743  1.1  christos   uint32_t numactions;
    744  1.1  christos   struct tracepoint_action **actions;
    745  1.1  christos 
    746  1.1  christos   /* Count of the times we've hit this tracepoint during the run.
    747  1.1  christos      Note that while-stepping steps are not counted as "hits".  */
    748  1.1  christos   uint64_t hit_count;
    749  1.1  christos 
    750  1.1  christos   /* Cached sum of the sizes of traceframes created by this point.  */
    751  1.1  christos   uint64_t traceframe_usage;
    752  1.1  christos 
    753  1.1  christos   CORE_ADDR compiled_cond;
    754  1.1  christos 
    755  1.1  christos   /* Link to the next tracepoint in the list.  */
    756  1.1  christos   struct tracepoint *next;
    757  1.1  christos 
    758  1.1  christos #ifndef IN_PROCESS_AGENT
    759  1.1  christos   /* The list of actions to take when the tracepoint triggers, in
    760  1.1  christos      string/packet form.  */
    761  1.1  christos   char **actions_str;
    762  1.1  christos 
    763  1.1  christos   /* The collection of strings that describe the tracepoint as it was
    764  1.1  christos      entered into GDB.  These are not used by the target, but are
    765  1.1  christos      reported back to GDB upon reconnection.  */
    766  1.1  christos   struct source_string *source_strings;
    767  1.1  christos 
    768  1.1  christos   /* The number of bytes displaced by fast tracepoints. It may subsume
    769  1.1  christos      multiple instructions, for multi-byte fast tracepoints.  This
    770  1.1  christos      field is only valid for fast tracepoints.  */
    771  1.1  christos   uint32_t orig_size;
    772  1.1  christos 
    773  1.1  christos   /* Only for fast tracepoints.  */
    774  1.1  christos   CORE_ADDR obj_addr_on_target;
    775  1.1  christos 
    776  1.1  christos   /* Address range where the original instruction under a fast
    777  1.1  christos      tracepoint was relocated to.  (_end is actually one byte past
    778  1.1  christos      the end).  */
    779  1.1  christos   CORE_ADDR adjusted_insn_addr;
    780  1.1  christos   CORE_ADDR adjusted_insn_addr_end;
    781  1.1  christos 
    782  1.1  christos   /* The address range of the piece of the jump pad buffer that was
    783  1.1  christos      assigned to this fast tracepoint.  (_end is actually one byte
    784  1.1  christos      past the end).*/
    785  1.1  christos   CORE_ADDR jump_pad;
    786  1.1  christos   CORE_ADDR jump_pad_end;
    787  1.1  christos 
    788  1.1  christos   /* The address range of the piece of the trampoline buffer that was
    789  1.1  christos      assigned to this fast tracepoint.  (_end is actually one byte
    790  1.1  christos      past the end).  */
    791  1.1  christos   CORE_ADDR trampoline;
    792  1.1  christos   CORE_ADDR trampoline_end;
    793  1.1  christos 
    794  1.1  christos   /* The list of actions to take while in a stepping loop.  These
    795  1.1  christos      fields are only valid for patch-based tracepoints.  */
    796  1.1  christos   int num_step_actions;
    797  1.1  christos   struct tracepoint_action **step_actions;
    798  1.1  christos   /* Same, but in string/packet form.  */
    799  1.1  christos   char **step_actions_str;
    800  1.1  christos 
    801  1.1  christos   /* Handle returned by the breakpoint or tracepoint module when we
    802  1.1  christos      inserted the trap or jump, or hooked into a static tracepoint.
    803  1.1  christos      NULL if we haven't inserted it yet.  */
    804  1.1  christos   void *handle;
    805  1.1  christos #endif
    806  1.1  christos 
    807  1.1  christos };
    808  1.1  christos 
    809  1.1  christos #ifndef IN_PROCESS_AGENT
    810  1.1  christos 
    811  1.1  christos /* Given `while-stepping', a thread may be collecting data for more
    812  1.1  christos    than one tracepoint simultaneously.  On the other hand, the same
    813  1.1  christos    tracepoint with a while-stepping action may be hit by more than one
    814  1.1  christos    thread simultaneously (but not quite, each thread could be handling
    815  1.1  christos    a different step).  Each thread holds a list of these objects,
    816  1.1  christos    representing the current step of each while-stepping action being
    817  1.1  christos    collected.  */
    818  1.1  christos 
    819  1.1  christos struct wstep_state
    820  1.1  christos {
    821  1.1  christos   struct wstep_state *next;
    822  1.1  christos 
    823  1.1  christos   /* The tracepoint number.  */
    824  1.1  christos   int tp_number;
    825  1.1  christos   /* The tracepoint's address.  */
    826  1.1  christos   CORE_ADDR tp_address;
    827  1.1  christos 
    828  1.1  christos   /* The number of the current step in this 'while-stepping'
    829  1.1  christos      action.  */
    830  1.1  christos   long current_step;
    831  1.1  christos };
    832  1.1  christos 
    833  1.1  christos #endif
    834  1.1  christos 
    835  1.1  christos EXTERN_C_PUSH
    836  1.1  christos 
    837  1.1  christos /* The linked list of all tracepoints.  Marked explicitly as used as
    838  1.1  christos    the in-process library doesn't use it for the fast tracepoints
    839  1.1  christos    support.  */
    840  1.1  christos IP_AGENT_EXPORT_VAR struct tracepoint *tracepoints;
    841  1.1  christos 
    842  1.1  christos /* The first tracepoint to exceed its pass count.  */
    843  1.1  christos 
    844  1.1  christos IP_AGENT_EXPORT_VAR struct tracepoint *stopping_tracepoint;
    845  1.1  christos 
    846  1.1  christos /* True if the trace buffer is full or otherwise no longer usable.  */
    847  1.1  christos 
    848  1.1  christos IP_AGENT_EXPORT_VAR int trace_buffer_is_full;
    849  1.1  christos 
    850  1.1  christos /* The first error that occurred during expression evaluation.  */
    851  1.1  christos 
    852  1.1  christos /* Stored as an int to avoid the IPA ABI being dependent on whatever
    853  1.1  christos    the compiler decides to use for the enum's underlying type.  Holds
    854  1.1  christos    enum eval_result_type values.  */
    855  1.1  christos IP_AGENT_EXPORT_VAR int expr_eval_result = expr_eval_no_error;
    856  1.1  christos 
    857  1.1  christos EXTERN_C_POP
    858  1.1  christos 
    859  1.1  christos #ifndef IN_PROCESS_AGENT
    860  1.1  christos 
    861  1.1  christos /* Pointer to the last tracepoint in the list, new tracepoints are
    862  1.1  christos    linked in at the end.  */
    863  1.1  christos 
    864  1.1  christos static struct tracepoint *last_tracepoint;
    865  1.1  christos 
    866  1.1  christos static const char *eval_result_names[] =
    867  1.1  christos   {
    868  1.1  christos     "terror:in the attic",  /* this should never be reported */
    869  1.1  christos     "terror:empty expression",
    870  1.1  christos     "terror:empty stack",
    871  1.1  christos     "terror:stack overflow",
    872  1.1  christos     "terror:stack underflow",
    873  1.1  christos     "terror:unhandled opcode",
    874  1.1  christos     "terror:unrecognized opcode",
    875  1.1  christos     "terror:divide by zero"
    876  1.1  christos   };
    877  1.1  christos 
    878  1.1  christos #endif
    879  1.1  christos 
    880  1.1  christos /* The tracepoint in which the error occurred.  */
    881  1.1  christos 
    882  1.1  christos EXTERN_C_PUSH
    883  1.1  christos IP_AGENT_EXPORT_VAR struct tracepoint *error_tracepoint;
    884  1.1  christos EXTERN_C_POP
    885  1.1  christos 
    886  1.1  christos struct trace_state_variable
    887  1.1  christos {
    888  1.1  christos   /* This is the name of the variable as used in GDB.  The target
    889  1.1  christos      doesn't use the name, but needs to have it for saving and
    890  1.1  christos      reconnection purposes.  */
    891  1.1  christos   char *name;
    892  1.1  christos 
    893  1.1  christos   /* This number identifies the variable uniquely.  Numbers may be
    894  1.1  christos      assigned either by the target (in the case of builtin variables),
    895  1.1  christos      or by GDB, and are presumed unique during the course of a trace
    896  1.1  christos      experiment.  */
    897  1.1  christos   int number;
    898  1.1  christos 
    899  1.1  christos   /* The variable's initial value, a 64-bit signed integer always.  */
    900  1.1  christos   LONGEST initial_value;
    901  1.1  christos 
    902  1.1  christos   /* The variable's value, a 64-bit signed integer always.  */
    903  1.1  christos   LONGEST value;
    904  1.1  christos 
    905  1.1  christos   /* Pointer to a getter function, used to supply computed values.  */
    906  1.1  christos   LONGEST (*getter) (void);
    907  1.1  christos 
    908  1.1  christos   /* Link to the next variable.  */
    909  1.1  christos   struct trace_state_variable *next;
    910  1.1  christos };
    911  1.1  christos 
    912  1.1  christos /* Linked list of all trace state variables.  */
    913  1.1  christos 
    914  1.1  christos #ifdef IN_PROCESS_AGENT
    915  1.1  christos struct trace_state_variable *alloced_trace_state_variables;
    916  1.1  christos #endif
    917  1.1  christos 
    918  1.1  christos IP_AGENT_EXPORT_VAR struct trace_state_variable *trace_state_variables;
    919  1.1  christos 
    920  1.1  christos /* The results of tracing go into a fixed-size space known as the
    921  1.1  christos    "trace buffer".  Because usage follows a limited number of
    922  1.1  christos    patterns, we manage it ourselves rather than with malloc.  Basic
    923  1.1  christos    rules are that we create only one trace frame at a time, each is
    924  1.1  christos    variable in size, they are never moved once created, and we only
    925  1.1  christos    discard if we are doing a circular buffer, and then only the oldest
    926  1.1  christos    ones.  Each trace frame includes its own size, so we don't need to
    927  1.1  christos    link them together, and the trace frame number is relative to the
    928  1.1  christos    first one, so we don't need to record numbers.  A trace frame also
    929  1.1  christos    records the number of the tracepoint that created it.  The data
    930  1.1  christos    itself is a series of blocks, each introduced by a single character
    931  1.1  christos    and with a defined format.  Each type of block has enough
    932  1.1  christos    type/length info to allow scanners to jump quickly from one block
    933  1.1  christos    to the next without reading each byte in the block.  */
    934  1.1  christos 
    935  1.1  christos /* Trace buffer management would be simple - advance a free pointer
    936  1.1  christos    from beginning to end, then stop - were it not for the circular
    937  1.1  christos    buffer option, which is a useful way to prevent a trace run from
    938  1.1  christos    stopping prematurely because the buffer filled up.  In the circular
    939  1.1  christos    case, the location of the first trace frame (trace_buffer_start)
    940  1.1  christos    moves as old trace frames are discarded.  Also, since we grow trace
    941  1.1  christos    frames incrementally as actions are performed, we wrap around to
    942  1.1  christos    the beginning of the trace buffer.  This is per-block, so each
    943  1.1  christos    block within a trace frame remains contiguous.  Things get messy
    944  1.1  christos    when the wrapped-around trace frame is the one being discarded; the
    945  1.1  christos    free space ends up in two parts at opposite ends of the buffer.  */
    946  1.1  christos 
    947  1.1  christos #ifndef ATTR_PACKED
    948  1.1  christos #  if defined(__GNUC__)
    949  1.1  christos #    define ATTR_PACKED __attribute__ ((packed))
    950  1.1  christos #  else
    951  1.1  christos #    define ATTR_PACKED /* nothing */
    952  1.1  christos #  endif
    953  1.1  christos #endif
    954  1.1  christos 
    955  1.1  christos /* The data collected at a tracepoint hit.  This object should be as
    956  1.1  christos    small as possible, since there may be a great many of them.  We do
    957  1.1  christos    not need to keep a frame number, because they are all sequential
    958  1.1  christos    and there are no deletions; so the Nth frame in the buffer is
    959  1.1  christos    always frame number N.  */
    960  1.1  christos 
    961  1.1  christos struct traceframe
    962  1.1  christos {
    963  1.1  christos   /* Number of the tracepoint that collected this traceframe.  A value
    964  1.1  christos      of 0 indicates the current end of the trace buffer.  We make this
    965  1.1  christos      a 16-bit field because it's never going to happen that GDB's
    966  1.1  christos      numbering of tracepoints reaches 32,000.  */
    967  1.1  christos   int tpnum : 16;
    968  1.1  christos 
    969  1.1  christos   /* The size of the data in this trace frame.  We limit this to 32
    970  1.1  christos      bits, even on a 64-bit target, because it's just implausible that
    971  1.1  christos      one is validly going to collect 4 gigabytes of data at a single
    972  1.1  christos      tracepoint hit.  */
    973  1.1  christos   unsigned int data_size : 32;
    974  1.1  christos 
    975  1.1  christos   /* The base of the trace data, which is contiguous from this point.  */
    976  1.1  christos   unsigned char data[0];
    977  1.1  christos 
    978  1.1  christos } ATTR_PACKED;
    979  1.1  christos 
    980  1.1  christos /* The size of the EOB marker, in bytes.  A traceframe with zeroed
    981  1.1  christos    fields (and no data) marks the end of trace data.  */
    982  1.1  christos #define TRACEFRAME_EOB_MARKER_SIZE offsetof (struct traceframe, data)
    983  1.1  christos 
    984  1.1  christos /* This flag is true if the trace buffer is circular, meaning that
    985  1.1  christos    when it fills, the oldest trace frames are discarded in order to
    986  1.1  christos    make room.  */
    987  1.1  christos 
    988  1.1  christos #ifndef IN_PROCESS_AGENT
    989  1.1  christos static int circular_trace_buffer;
    990  1.1  christos #endif
    991  1.1  christos 
    992  1.1  christos /* Size of the trace buffer.  */
    993  1.1  christos 
    994  1.1  christos static LONGEST trace_buffer_size;
    995  1.1  christos 
    996  1.1  christos EXTERN_C_PUSH
    997  1.1  christos 
    998  1.1  christos /* Pointer to the block of memory that traceframes all go into.  */
    999  1.1  christos 
   1000  1.1  christos IP_AGENT_EXPORT_VAR unsigned char *trace_buffer_lo;
   1001  1.1  christos 
   1002  1.1  christos /* Pointer to the end of the trace buffer, more precisely to the byte
   1003  1.1  christos    after the end of the buffer.  */
   1004  1.1  christos 
   1005  1.1  christos IP_AGENT_EXPORT_VAR unsigned char *trace_buffer_hi;
   1006  1.1  christos 
   1007  1.1  christos EXTERN_C_POP
   1008  1.1  christos 
   1009  1.1  christos /* Control structure holding the read/write/etc. pointers into the
   1010  1.1  christos    trace buffer.  We need more than one of these to implement a
   1011  1.1  christos    transaction-like mechanism to guarantees that both GDBserver and the
   1012  1.1  christos    in-process agent can try to change the trace buffer
   1013  1.1  christos    simultaneously.  */
   1014  1.1  christos 
   1015  1.1  christos struct trace_buffer_control
   1016  1.1  christos {
   1017  1.1  christos   /* Pointer to the first trace frame in the buffer.  In the
   1018  1.1  christos      non-circular case, this is equal to trace_buffer_lo, otherwise it
   1019  1.1  christos      moves around in the buffer.  */
   1020  1.1  christos   unsigned char *start;
   1021  1.1  christos 
   1022  1.1  christos   /* Pointer to the free part of the trace buffer.  Note that we clear
   1023  1.1  christos      several bytes at and after this pointer, so that traceframe
   1024  1.1  christos      scans/searches terminate properly.  */
   1025  1.1  christos   unsigned char *free;
   1026  1.1  christos 
   1027  1.1  christos   /* Pointer to the byte after the end of the free part.  Note that
   1028  1.1  christos      this may be smaller than trace_buffer_free in the circular case,
   1029  1.1  christos      and means that the free part is in two pieces.  Initially it is
   1030  1.1  christos      equal to trace_buffer_hi, then is generally equivalent to
   1031  1.1  christos      trace_buffer_start.  */
   1032  1.1  christos   unsigned char *end_free;
   1033  1.1  christos 
   1034  1.1  christos   /* Pointer to the wraparound.  If not equal to trace_buffer_hi, then
   1035  1.1  christos      this is the point at which the trace data breaks, and resumes at
   1036  1.1  christos      trace_buffer_lo.  */
   1037  1.1  christos   unsigned char *wrap;
   1038  1.1  christos };
   1039  1.1  christos 
   1040  1.1  christos /* Same as above, to be used by GDBserver when updating the in-process
   1041  1.1  christos    agent.  */
   1042  1.1  christos struct ipa_trace_buffer_control
   1043  1.1  christos {
   1044  1.1  christos   uintptr_t start;
   1045  1.1  christos   uintptr_t free;
   1046  1.1  christos   uintptr_t end_free;
   1047  1.1  christos   uintptr_t wrap;
   1048  1.1  christos };
   1049  1.1  christos 
   1050  1.1  christos 
   1051  1.1  christos /* We have possibly both GDBserver and an inferior thread accessing
   1052  1.1  christos    the same IPA trace buffer memory.  The IPA is the producer (tries
   1053  1.1  christos    to put new frames in the buffer), while GDBserver occasionally
   1054  1.1  christos    consumes them, that is, flushes the IPA's buffer into its own
   1055  1.1  christos    buffer.  Both sides need to update the trace buffer control
   1056  1.1  christos    pointers (current head, tail, etc.).  We can't use a global lock to
   1057  1.1  christos    synchronize the accesses, as otherwise we could deadlock GDBserver
   1058  1.1  christos    (if the thread holding the lock stops for a signal, say).  So
   1059  1.1  christos    instead of that, we use a transaction scheme where GDBserver writes
   1060  1.1  christos    always prevail over the IPAs writes, and, we have the IPA detect
   1061  1.1  christos    the commit failure/overwrite, and retry the whole attempt.  This is
   1062  1.1  christos    mainly implemented by having a global token object that represents
   1063  1.1  christos    who wrote last to the buffer control structure.  We need to freeze
   1064  1.1  christos    any inferior writing to the buffer while GDBserver touches memory,
   1065  1.1  christos    so that the inferior can correctly detect that GDBserver had been
   1066  1.1  christos    there, otherwise, it could mistakingly think its commit was
   1067  1.1  christos    successful; that's implemented by simply having GDBserver set a
   1068  1.1  christos    breakpoint the inferior hits if it is the critical region.
   1069  1.1  christos 
   1070  1.1  christos    There are three cycling trace buffer control structure copies
   1071  1.1  christos    (buffer head, tail, etc.), with the token object including an index
   1072  1.1  christos    indicating which is current live copy.  The IPA tentatively builds
   1073  1.1  christos    an updated copy in a non-current control structure, while GDBserver
   1074  1.1  christos    always clobbers the current version directly.  The IPA then tries
   1075  1.1  christos    to atomically "commit" its version; if GDBserver clobbered the
   1076  1.1  christos    structure meanwhile, that will fail, and the IPA restarts the
   1077  1.1  christos    allocation process.
   1078  1.1  christos 
   1079  1.1  christos    Listing the step in further detail, we have:
   1080  1.1  christos 
   1081  1.1  christos   In-process agent (producer):
   1082  1.1  christos 
   1083  1.1  christos   - passes by `about_to_request_buffer_space' breakpoint/lock
   1084  1.1  christos 
   1085  1.1  christos   - reads current token, extracts current trace buffer control index,
   1086  1.1  christos     and starts tentatively updating the rightmost one (0->1, 1->2,
   1087  1.1  christos     2->0).  Note that only one inferior thread is executing this code
   1088  1.1  christos     at any given time, due to an outer lock in the jump pads.
   1089  1.1  christos 
   1090  1.1  christos   - updates counters, and tries to commit the token.
   1091  1.1  christos 
   1092  1.1  christos   - passes by second `about_to_request_buffer_space' breakpoint/lock,
   1093  1.1  christos     leaving the sync region.
   1094  1.1  christos 
   1095  1.1  christos   - checks if the update was effective.
   1096  1.1  christos 
   1097  1.1  christos   - if trace buffer was found full, hits flush_trace_buffer
   1098  1.1  christos     breakpoint, and restarts later afterwards.
   1099  1.1  christos 
   1100  1.1  christos   GDBserver (consumer):
   1101  1.1  christos 
   1102  1.1  christos   - sets `about_to_request_buffer_space' breakpoint/lock.
   1103  1.1  christos 
   1104  1.1  christos   - updates the token unconditionally, using the current buffer
   1105  1.1  christos     control index, since it knows that the IP agent always writes to
   1106  1.1  christos     the rightmost, and due to the breakpoint, at most one IP thread
   1107  1.1  christos     can try to update the trace buffer concurrently to GDBserver, so
   1108  1.1  christos     there will be no danger of trace buffer control index wrap making
   1109  1.1  christos     the IPA write to the same index as GDBserver.
   1110  1.1  christos 
   1111  1.1  christos   - flushes the IP agent's trace buffer completely, and updates the
   1112  1.1  christos     current trace buffer control structure.  GDBserver *always* wins.
   1113  1.1  christos 
   1114  1.1  christos   - removes the `about_to_request_buffer_space' breakpoint.
   1115  1.1  christos 
   1116  1.1  christos The token is stored in the `trace_buffer_ctrl_curr' variable.
   1117  1.1  christos Internally, it's bits are defined as:
   1118  1.1  christos 
   1119  1.1  christos  |-------------+-----+-------------+--------+-------------+--------------|
   1120  1.1  christos  | Bit offsets |  31 |   30 - 20   |   19   |    18-8     |     7-0      |
   1121  1.1  christos  |-------------+-----+-------------+--------+-------------+--------------|
   1122  1.1  christos  | What        | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
   1123  1.1  christos  |-------------+-----+-------------+--------+-------------+--------------|
   1124  1.1  christos 
   1125  1.1  christos  GSB  - GDBserver Stamp Bit
   1126  1.1  christos  PC   - Previous Counter
   1127  1.1  christos  CC   - Current Counter
   1128  1.1  christos  TBCI - Trace Buffer Control Index
   1129  1.1  christos 
   1130  1.1  christos 
   1131  1.1  christos An IPA update of `trace_buffer_ctrl_curr' does:
   1132  1.1  christos 
   1133  1.1  christos     - read CC from the current token, save as PC.
   1134  1.1  christos     - updates pointers
   1135  1.1  christos     - atomically tries to write PC+1,CC
   1136  1.1  christos 
   1137  1.1  christos A GDBserver update of `trace_buffer_ctrl_curr' does:
   1138  1.1  christos 
   1139  1.1  christos     - reads PC and CC from the current token.
   1140  1.1  christos     - updates pointers
   1141  1.1  christos     - writes GSB,PC,CC
   1142  1.1  christos */
   1143  1.1  christos 
   1144  1.1  christos /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
   1145  1.1  christos    for the counters described below.  The cleared bits are used to
   1146  1.1  christos    hold the index of the items of the `trace_buffer_ctrl' array that
   1147  1.1  christos    is "current".  */
   1148  1.1  christos #define GDBSERVER_FLUSH_COUNT_MASK        0xfffffff0
   1149  1.1  christos 
   1150  1.1  christos /* `trace_buffer_ctrl_curr' contains two counters.  The `previous'
   1151  1.1  christos    counter, and the `current' counter.  */
   1152  1.1  christos 
   1153  1.1  christos #define GDBSERVER_FLUSH_COUNT_MASK_PREV   0x7ff00000
   1154  1.1  christos #define GDBSERVER_FLUSH_COUNT_MASK_CURR   0x0007ff00
   1155  1.1  christos 
   1156  1.1  christos /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
   1157  1.1  christos    always stamps this bit as set.  */
   1158  1.1  christos #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
   1159  1.1  christos 
   1160  1.1  christos #ifdef IN_PROCESS_AGENT
   1161  1.1  christos IP_AGENT_EXPORT_VAR struct trace_buffer_control trace_buffer_ctrl[3];
   1162  1.1  christos IP_AGENT_EXPORT_VAR unsigned int trace_buffer_ctrl_curr;
   1163  1.1  christos 
   1164  1.1  christos # define TRACE_BUFFER_CTRL_CURR \
   1165  1.1  christos   (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
   1166  1.1  christos 
   1167  1.1  christos #else
   1168  1.1  christos 
   1169  1.1  christos /* The GDBserver side agent only needs one instance of this object, as
   1170  1.1  christos    it doesn't need to sync with itself.  Define it as array anyway so
   1171  1.1  christos    that the rest of the code base doesn't need to care for the
   1172  1.1  christos    difference.  */
   1173  1.1  christos struct trace_buffer_control trace_buffer_ctrl[1];
   1174  1.1  christos # define TRACE_BUFFER_CTRL_CURR 0
   1175  1.1  christos #endif
   1176  1.1  christos 
   1177  1.1  christos /* These are convenience macros used to access the current trace
   1178  1.1  christos    buffer control in effect.  */
   1179  1.1  christos #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
   1180  1.1  christos #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
   1181  1.1  christos #define trace_buffer_end_free \
   1182  1.1  christos   (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
   1183  1.1  christos #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
   1184  1.1  christos 
   1185  1.1  christos 
   1186  1.1  christos /* Macro that returns a pointer to the first traceframe in the buffer.  */
   1187  1.1  christos 
   1188  1.1  christos #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
   1189  1.1  christos 
   1190  1.1  christos /* Macro that returns a pointer to the next traceframe in the buffer.
   1191  1.1  christos    If the computed location is beyond the wraparound point, subtract
   1192  1.1  christos    the offset of the wraparound.  */
   1193  1.1  christos 
   1194  1.1  christos #define NEXT_TRACEFRAME_1(TF) \
   1195  1.1  christos   (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
   1196  1.1  christos 
   1197  1.1  christos #define NEXT_TRACEFRAME(TF) \
   1198  1.1  christos   ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF)  \
   1199  1.1  christos 			  - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
   1200  1.1  christos 			     ? (trace_buffer_wrap - trace_buffer_lo)	\
   1201  1.1  christos 			     : 0)))
   1202  1.1  christos 
   1203  1.1  christos /* The difference between these counters represents the total number
   1204  1.1  christos    of complete traceframes present in the trace buffer.  The IP agent
   1205  1.1  christos    writes to the write count, GDBserver writes to read count.  */
   1206  1.1  christos 
   1207  1.1  christos IP_AGENT_EXPORT_VAR unsigned int traceframe_write_count;
   1208  1.1  christos IP_AGENT_EXPORT_VAR unsigned int traceframe_read_count;
   1209  1.1  christos 
   1210  1.1  christos /* Convenience macro.  */
   1211  1.1  christos 
   1212  1.1  christos #define traceframe_count \
   1213  1.1  christos   ((unsigned int) (traceframe_write_count - traceframe_read_count))
   1214  1.1  christos 
   1215  1.1  christos /* The count of all traceframes created in the current run, including
   1216  1.1  christos    ones that were discarded to make room.  */
   1217  1.1  christos 
   1218  1.1  christos IP_AGENT_EXPORT_VAR int traceframes_created;
   1219  1.1  christos 
   1220  1.1  christos #ifndef IN_PROCESS_AGENT
   1221  1.1  christos 
   1222  1.1  christos /* Read-only regions are address ranges whose contents don't change,
   1223  1.1  christos    and so can be read from target memory even while looking at a trace
   1224  1.1  christos    frame.  Without these, disassembly for instance will likely fail,
   1225  1.1  christos    because the program code is not usually collected into a trace
   1226  1.1  christos    frame.  This data structure does not need to be very complicated or
   1227  1.1  christos    particularly efficient, it's only going to be used occasionally,
   1228  1.1  christos    and only by some commands.  */
   1229  1.1  christos 
   1230  1.1  christos struct readonly_region
   1231  1.1  christos {
   1232  1.1  christos   /* The bounds of the region.  */
   1233  1.1  christos   CORE_ADDR start, end;
   1234  1.1  christos 
   1235  1.1  christos   /* Link to the next one.  */
   1236  1.1  christos   struct readonly_region *next;
   1237  1.1  christos };
   1238  1.1  christos 
   1239  1.1  christos /* Linked list of readonly regions.  This list stays in effect from
   1240  1.1  christos    one tstart to the next.  */
   1241  1.1  christos 
   1242  1.1  christos static struct readonly_region *readonly_regions;
   1243  1.1  christos 
   1244  1.1  christos #endif
   1245  1.1  christos 
   1246  1.1  christos /* The global that controls tracing overall.  */
   1247  1.1  christos 
   1248  1.1  christos IP_AGENT_EXPORT_VAR int tracing;
   1249  1.1  christos 
   1250  1.1  christos #ifndef IN_PROCESS_AGENT
   1251  1.1  christos 
   1252  1.1  christos /* Controls whether tracing should continue after GDB disconnects.  */
   1253  1.1  christos 
   1254  1.1  christos int disconnected_tracing;
   1255  1.1  christos 
   1256  1.1  christos /* The reason for the last tracing run to have stopped.  We initialize
   1257  1.1  christos    to a distinct string so that GDB can distinguish between "stopped
   1258  1.1  christos    after running" and "stopped because never run" cases.  */
   1259  1.1  christos 
   1260  1.1  christos static const char *tracing_stop_reason = "tnotrun";
   1261  1.1  christos 
   1262  1.1  christos static int tracing_stop_tpnum;
   1263  1.1  christos 
   1264  1.1  christos /* 64-bit timestamps for the trace run's start and finish, expressed
   1265  1.1  christos    in microseconds from the Unix epoch.  */
   1266  1.1  christos 
   1267  1.1  christos LONGEST tracing_start_time;
   1268  1.1  christos LONGEST tracing_stop_time;
   1269  1.1  christos 
   1270  1.1  christos /* The (optional) user-supplied name of the user that started the run.
   1271  1.1  christos    This is an arbitrary string, and may be NULL.  */
   1272  1.1  christos 
   1273  1.1  christos char *tracing_user_name;
   1274  1.1  christos 
   1275  1.1  christos /* Optional user-supplied text describing the run.  This is
   1276  1.1  christos    an arbitrary string, and may be NULL.  */
   1277  1.1  christos 
   1278  1.1  christos char *tracing_notes;
   1279  1.1  christos 
   1280  1.1  christos /* Optional user-supplied text explaining a tstop command.  This is an
   1281  1.1  christos    arbitrary string, and may be NULL.  */
   1282  1.1  christos 
   1283  1.1  christos char *tracing_stop_note;
   1284  1.1  christos 
   1285  1.1  christos #endif
   1286  1.1  christos 
   1287  1.1  christos /* Functions local to this file.  */
   1288  1.1  christos 
   1289  1.1  christos /* Base "class" for tracepoint type specific data to be passed down to
   1290  1.1  christos    collect_data_at_tracepoint.  */
   1291  1.1  christos struct tracepoint_hit_ctx
   1292  1.1  christos {
   1293  1.1  christos   enum tracepoint_type type;
   1294  1.1  christos };
   1295  1.1  christos 
   1296  1.1  christos #ifdef IN_PROCESS_AGENT
   1297  1.1  christos 
   1298  1.1  christos /* Fast/jump tracepoint specific data to be passed down to
   1299  1.1  christos    collect_data_at_tracepoint.  */
   1300  1.1  christos struct fast_tracepoint_ctx
   1301  1.1  christos {
   1302  1.1  christos   struct tracepoint_hit_ctx base;
   1303  1.1  christos 
   1304  1.1  christos   struct regcache regcache;
   1305  1.1  christos   int regcache_initted;
   1306  1.1  christos   unsigned char *regspace;
   1307  1.1  christos 
   1308  1.1  christos   unsigned char *regs;
   1309  1.1  christos   struct tracepoint *tpoint;
   1310  1.1  christos };
   1311  1.1  christos 
   1312  1.1  christos /* Static tracepoint specific data to be passed down to
   1313  1.1  christos    collect_data_at_tracepoint.  */
   1314  1.1  christos struct static_tracepoint_ctx
   1315  1.1  christos {
   1316  1.1  christos   struct tracepoint_hit_ctx base;
   1317  1.1  christos 
   1318  1.1  christos   /* The regcache corresponding to the registers state at the time of
   1319  1.1  christos      the tracepoint hit.  Initialized lazily, from REGS.  */
   1320  1.1  christos   struct regcache regcache;
   1321  1.1  christos   int regcache_initted;
   1322  1.1  christos 
   1323  1.1  christos   /* The buffer space REGCACHE above uses.  We use a separate buffer
   1324  1.1  christos      instead of letting the regcache malloc for both signal safety and
   1325  1.1  christos      performance reasons; this is allocated on the stack instead.  */
   1326  1.1  christos   unsigned char *regspace;
   1327  1.1  christos 
   1328  1.1  christos   /* The register buffer as passed on by lttng/ust.  */
   1329  1.1  christos   struct registers *regs;
   1330  1.1  christos 
   1331  1.1  christos   /* The "printf" formatter and the args the user passed to the marker
   1332  1.1  christos      call.  We use this to be able to collect "static trace data"
   1333  1.1  christos      ($_sdata).  */
   1334  1.1  christos   const char *fmt;
   1335  1.1  christos   va_list *args;
   1336  1.1  christos 
   1337  1.1  christos   /* The GDB tracepoint matching the probed marker that was "hit".  */
   1338  1.1  christos   struct tracepoint *tpoint;
   1339  1.1  christos };
   1340  1.1  christos 
   1341  1.1  christos #else
   1342  1.1  christos 
   1343  1.1  christos /* Static tracepoint specific data to be passed down to
   1344  1.1  christos    collect_data_at_tracepoint.  */
   1345  1.1  christos struct trap_tracepoint_ctx
   1346  1.1  christos {
   1347  1.1  christos   struct tracepoint_hit_ctx base;
   1348  1.1  christos 
   1349  1.1  christos   struct regcache *regcache;
   1350  1.1  christos };
   1351  1.1  christos 
   1352  1.1  christos #endif
   1353  1.1  christos 
   1354  1.1  christos #ifndef IN_PROCESS_AGENT
   1355  1.1  christos static CORE_ADDR traceframe_get_pc (struct traceframe *tframe);
   1356  1.1  christos static int traceframe_read_tsv (int num, LONGEST *val);
   1357  1.1  christos #endif
   1358  1.1  christos 
   1359  1.1  christos static int condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
   1360  1.1  christos 					 struct tracepoint *tpoint);
   1361  1.1  christos 
   1362  1.1  christos #ifndef IN_PROCESS_AGENT
   1363  1.1  christos static void clear_readonly_regions (void);
   1364  1.1  christos static void clear_installed_tracepoints (void);
   1365  1.1  christos #endif
   1366  1.1  christos 
   1367  1.1  christos static void collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
   1368  1.1  christos 					CORE_ADDR stop_pc,
   1369  1.1  christos 					struct tracepoint *tpoint);
   1370  1.1  christos #ifndef IN_PROCESS_AGENT
   1371  1.1  christos static void collect_data_at_step (struct tracepoint_hit_ctx *ctx,
   1372  1.1  christos 				  CORE_ADDR stop_pc,
   1373  1.1  christos 				  struct tracepoint *tpoint, int current_step);
   1374  1.1  christos static void compile_tracepoint_condition (struct tracepoint *tpoint,
   1375  1.1  christos 					  CORE_ADDR *jump_entry);
   1376  1.1  christos #endif
   1377  1.1  christos static void do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
   1378  1.1  christos 				     CORE_ADDR stop_pc,
   1379  1.1  christos 				     struct tracepoint *tpoint,
   1380  1.1  christos 				     struct traceframe *tframe,
   1381  1.1  christos 				     struct tracepoint_action *taction);
   1382  1.1  christos 
   1383  1.1  christos #ifndef IN_PROCESS_AGENT
   1384  1.1  christos static struct tracepoint *fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR);
   1385  1.1  christos 
   1386  1.1  christos static void install_tracepoint (struct tracepoint *, char *own_buf);
   1387  1.1  christos static void download_tracepoint (struct tracepoint *);
   1388  1.1  christos static int install_fast_tracepoint (struct tracepoint *, char *errbuf);
   1389  1.1  christos static void clone_fast_tracepoint (struct tracepoint *to,
   1390  1.1  christos 				   const struct tracepoint *from);
   1391  1.1  christos #endif
   1392  1.1  christos 
   1393  1.1  christos static LONGEST get_timestamp (void);
   1394  1.1  christos 
   1395  1.1  christos #if defined(__GNUC__)
   1396  1.1  christos #  define memory_barrier() asm volatile ("" : : : "memory")
   1397  1.1  christos #else
   1398  1.1  christos #  define memory_barrier() do {} while (0)
   1399  1.1  christos #endif
   1400  1.1  christos 
   1401  1.1  christos /* We only build the IPA if this builtin is supported, and there are
   1402  1.1  christos    no uses of this in GDBserver itself, so we're safe in defining this
   1403  1.1  christos    unconditionally.  */
   1404  1.1  christos #define cmpxchg(mem, oldval, newval) \
   1405  1.1  christos   __sync_val_compare_and_swap (mem, oldval, newval)
   1406  1.1  christos 
   1407  1.1  christos /* Record that an error occurred during expression evaluation.  */
   1408  1.1  christos 
   1409  1.1  christos static void
   1410  1.1  christos record_tracepoint_error (struct tracepoint *tpoint, const char *which,
   1411  1.1  christos 			 enum eval_result_type rtype)
   1412  1.1  christos {
   1413  1.1  christos   trace_debug ("Tracepoint %d at %s %s eval reports error %d",
   1414  1.1  christos 	       tpoint->number, paddress (tpoint->address), which, rtype);
   1415  1.1  christos 
   1416  1.1  christos #ifdef IN_PROCESS_AGENT
   1417  1.1  christos   /* Only record the first error we get.  */
   1418  1.1  christos   if (cmpxchg (&expr_eval_result,
   1419  1.1  christos 	       expr_eval_no_error,
   1420  1.1  christos 	       rtype) != expr_eval_no_error)
   1421  1.1  christos     return;
   1422  1.1  christos #else
   1423  1.1  christos   if (expr_eval_result != expr_eval_no_error)
   1424  1.1  christos     return;
   1425  1.1  christos #endif
   1426  1.1  christos 
   1427  1.1  christos   error_tracepoint = tpoint;
   1428  1.1  christos }
   1429  1.1  christos 
   1430  1.1  christos /* Trace buffer management.  */
   1431  1.1  christos 
   1432  1.1  christos static void
   1433  1.1  christos clear_trace_buffer (void)
   1434  1.1  christos {
   1435  1.1  christos   trace_buffer_start = trace_buffer_lo;
   1436  1.1  christos   trace_buffer_free = trace_buffer_lo;
   1437  1.1  christos   trace_buffer_end_free = trace_buffer_hi;
   1438  1.1  christos   trace_buffer_wrap = trace_buffer_hi;
   1439  1.1  christos   /* A traceframe with zeroed fields marks the end of trace data.  */
   1440  1.1  christos   ((struct traceframe *) trace_buffer_free)->tpnum = 0;
   1441  1.1  christos   ((struct traceframe *) trace_buffer_free)->data_size = 0;
   1442  1.1  christos   traceframe_read_count = traceframe_write_count = 0;
   1443  1.1  christos   traceframes_created = 0;
   1444  1.1  christos }
   1445  1.1  christos 
   1446  1.1  christos #ifndef IN_PROCESS_AGENT
   1447  1.1  christos 
   1448  1.1  christos static void
   1449  1.1  christos clear_inferior_trace_buffer (void)
   1450  1.1  christos {
   1451  1.1  christos   CORE_ADDR ipa_trace_buffer_lo;
   1452  1.1  christos   CORE_ADDR ipa_trace_buffer_hi;
   1453  1.1  christos   struct traceframe ipa_traceframe = { 0 };
   1454  1.1  christos   struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
   1455  1.1  christos 
   1456  1.1  christos   read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
   1457  1.1  christos 			      &ipa_trace_buffer_lo);
   1458  1.1  christos   read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
   1459  1.1  christos 			      &ipa_trace_buffer_hi);
   1460  1.1  christos 
   1461  1.1  christos   ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
   1462  1.1  christos   ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
   1463  1.1  christos   ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
   1464  1.1  christos   ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
   1465  1.1  christos 
   1466  1.1  christos   /* A traceframe with zeroed fields marks the end of trace data.  */
   1467  1.1  christos   target_write_memory (ipa_sym_addrs.addr_trace_buffer_ctrl,
   1468  1.1  christos 			 (unsigned char *) &ipa_trace_buffer_ctrl,
   1469  1.1  christos 			 sizeof (ipa_trace_buffer_ctrl));
   1470  1.1  christos 
   1471  1.1  christos   write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr, 0);
   1472  1.1  christos 
   1473  1.1  christos   /* A traceframe with zeroed fields marks the end of trace data.  */
   1474  1.1  christos   target_write_memory (ipa_trace_buffer_lo,
   1475  1.1  christos 			 (unsigned char *) &ipa_traceframe,
   1476  1.1  christos 			 sizeof (ipa_traceframe));
   1477  1.1  christos 
   1478  1.1  christos   write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count, 0);
   1479  1.1  christos   write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count, 0);
   1480  1.1  christos   write_inferior_integer (ipa_sym_addrs.addr_traceframes_created, 0);
   1481  1.1  christos }
   1482  1.1  christos 
   1483  1.1  christos #endif
   1484  1.1  christos 
   1485  1.1  christos static void
   1486  1.1  christos init_trace_buffer (LONGEST bufsize)
   1487  1.1  christos {
   1488  1.1  christos   size_t alloc_size;
   1489  1.1  christos 
   1490  1.1  christos   trace_buffer_size = bufsize;
   1491  1.1  christos 
   1492  1.1  christos   /* Make sure to internally allocate at least space for the EOB
   1493  1.1  christos      marker.  */
   1494  1.1  christos   alloc_size = (bufsize < TRACEFRAME_EOB_MARKER_SIZE
   1495  1.1  christos 		? TRACEFRAME_EOB_MARKER_SIZE : bufsize);
   1496  1.1  christos   trace_buffer_lo = (unsigned char *) xrealloc (trace_buffer_lo, alloc_size);
   1497  1.1  christos 
   1498  1.1  christos   trace_buffer_hi = trace_buffer_lo + trace_buffer_size;
   1499  1.1  christos 
   1500  1.1  christos   clear_trace_buffer ();
   1501  1.1  christos }
   1502  1.1  christos 
   1503  1.1  christos #ifdef IN_PROCESS_AGENT
   1504  1.1  christos 
   1505  1.1  christos /* This is needed for -Wmissing-declarations.  */
   1506  1.1  christos IP_AGENT_EXPORT_FUNC void about_to_request_buffer_space (void);
   1507  1.1  christos 
   1508  1.1  christos IP_AGENT_EXPORT_FUNC void
   1509  1.1  christos about_to_request_buffer_space (void)
   1510  1.1  christos {
   1511  1.1  christos   /* GDBserver places breakpoint here while it goes about to flush
   1512  1.1  christos      data at random times.  */
   1513  1.1  christos   UNKNOWN_SIDE_EFFECTS();
   1514  1.1  christos }
   1515  1.1  christos 
   1516  1.1  christos #endif
   1517  1.1  christos 
   1518  1.1  christos /* Carve out a piece of the trace buffer, returning NULL in case of
   1519  1.1  christos    failure.  */
   1520  1.1  christos 
   1521  1.1  christos static void *
   1522  1.1  christos trace_buffer_alloc (size_t amt)
   1523  1.1  christos {
   1524  1.1  christos   unsigned char *rslt;
   1525  1.1  christos   struct trace_buffer_control *tbctrl;
   1526  1.1  christos   unsigned int curr;
   1527  1.1  christos #ifdef IN_PROCESS_AGENT
   1528  1.1  christos   unsigned int prev, prev_filtered;
   1529  1.1  christos   unsigned int commit_count;
   1530  1.1  christos   unsigned int commit;
   1531  1.1  christos   unsigned int readout;
   1532  1.1  christos #else
   1533  1.1  christos   struct traceframe *oldest;
   1534  1.1  christos   unsigned char *new_start;
   1535  1.1  christos #endif
   1536  1.1  christos 
   1537  1.1  christos   trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
   1538  1.1  christos 	       (long) amt, (long) sizeof (struct traceframe));
   1539  1.1  christos 
   1540  1.1  christos   /* Account for the EOB marker.  */
   1541  1.1  christos   amt += TRACEFRAME_EOB_MARKER_SIZE;
   1542  1.1  christos 
   1543  1.1  christos #ifdef IN_PROCESS_AGENT
   1544  1.1  christos  again:
   1545  1.1  christos   memory_barrier ();
   1546  1.1  christos 
   1547  1.1  christos   /* Read the current token and extract the index to try to write to,
   1548  1.1  christos      storing it in CURR.  */
   1549  1.1  christos   prev = trace_buffer_ctrl_curr;
   1550  1.1  christos   prev_filtered = prev & ~GDBSERVER_FLUSH_COUNT_MASK;
   1551  1.1  christos   curr = prev_filtered + 1;
   1552  1.1  christos   if (curr > 2)
   1553  1.1  christos     curr = 0;
   1554  1.1  christos 
   1555  1.1  christos   about_to_request_buffer_space ();
   1556  1.1  christos 
   1557  1.1  christos   /* Start out with a copy of the current state.  GDBserver may be
   1558  1.1  christos      midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
   1559  1.1  christos      be able to commit anyway if that happens.  */
   1560  1.1  christos   trace_buffer_ctrl[curr]
   1561  1.1  christos     = trace_buffer_ctrl[prev_filtered];
   1562  1.1  christos   trace_debug ("trying curr=%u", curr);
   1563  1.1  christos #else
   1564  1.1  christos   /* The GDBserver's agent doesn't need all that syncing, and always
   1565  1.1  christos      updates TCB 0 (there's only one, mind you).  */
   1566  1.1  christos   curr = 0;
   1567  1.1  christos #endif
   1568  1.1  christos   tbctrl = &trace_buffer_ctrl[curr];
   1569  1.1  christos 
   1570  1.1  christos   /* Offsets are easier to grok for debugging than raw addresses,
   1571  1.1  christos      especially for the small trace buffer sizes that are useful for
   1572  1.1  christos      testing.  */
   1573  1.1  christos   trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
   1574  1.1  christos 	       curr,
   1575  1.1  christos 	       (int) (tbctrl->start - trace_buffer_lo),
   1576  1.1  christos 	       (int) (tbctrl->free - trace_buffer_lo),
   1577  1.1  christos 	       (int) (tbctrl->end_free - trace_buffer_lo),
   1578  1.1  christos 	       (int) (tbctrl->wrap - trace_buffer_lo),
   1579  1.1  christos 	       (int) (trace_buffer_hi - trace_buffer_lo));
   1580  1.1  christos 
   1581  1.1  christos   /* The algorithm here is to keep trying to get a contiguous block of
   1582  1.1  christos      the requested size, possibly discarding older traceframes to free
   1583  1.1  christos      up space.  Since free space might come in one or two pieces,
   1584  1.1  christos      depending on whether discarded traceframes wrapped around at the
   1585  1.1  christos      high end of the buffer, we test both pieces after each
   1586  1.1  christos      discard.  */
   1587  1.1  christos   while (1)
   1588  1.1  christos     {
   1589  1.1  christos       /* First, if we have two free parts, try the upper one first.  */
   1590  1.1  christos       if (tbctrl->end_free < tbctrl->free)
   1591  1.1  christos 	{
   1592  1.1  christos 	  if (tbctrl->free + amt <= trace_buffer_hi)
   1593  1.1  christos 	    /* We have enough in the upper part.  */
   1594  1.1  christos 	    break;
   1595  1.1  christos 	  else
   1596  1.1  christos 	    {
   1597  1.1  christos 	      /* Our high part of free space wasn't enough.  Give up
   1598  1.1  christos 		 on it for now, set wraparound.  We will recover the
   1599  1.1  christos 		 space later, if/when the wrapped-around traceframe is
   1600  1.1  christos 		 discarded.  */
   1601  1.1  christos 	      trace_debug ("Upper part too small, setting wraparound");
   1602  1.1  christos 	      tbctrl->wrap = tbctrl->free;
   1603  1.1  christos 	      tbctrl->free = trace_buffer_lo;
   1604  1.1  christos 	    }
   1605  1.1  christos 	}
   1606  1.1  christos 
   1607  1.1  christos       /* The normal case.  */
   1608  1.1  christos       if (tbctrl->free + amt <= tbctrl->end_free)
   1609  1.1  christos 	break;
   1610  1.1  christos 
   1611  1.1  christos #ifdef IN_PROCESS_AGENT
   1612  1.1  christos       /* The IP Agent's buffer is always circular.  It isn't used
   1613  1.1  christos 	 currently, but `circular_trace_buffer' could represent
   1614  1.1  christos 	 GDBserver's mode.  If we didn't find space, ask GDBserver to
   1615  1.1  christos 	 flush.  */
   1616  1.1  christos 
   1617  1.1  christos       flush_trace_buffer ();
   1618  1.1  christos       memory_barrier ();
   1619  1.1  christos       if (tracing)
   1620  1.1  christos 	{
   1621  1.1  christos 	  trace_debug ("gdbserver flushed buffer, retrying");
   1622  1.1  christos 	  goto again;
   1623  1.1  christos 	}
   1624  1.1  christos 
   1625  1.1  christos       /* GDBserver cancelled the tracing.  Bail out as well.  */
   1626  1.1  christos       return NULL;
   1627  1.1  christos #else
   1628  1.1  christos       /* If we're here, then neither part is big enough, and
   1629  1.1  christos 	 non-circular trace buffers are now full.  */
   1630  1.1  christos       if (!circular_trace_buffer)
   1631  1.1  christos 	{
   1632  1.1  christos 	  trace_debug ("Not enough space in the trace buffer");
   1633  1.1  christos 	  return NULL;
   1634  1.1  christos 	}
   1635  1.1  christos 
   1636  1.1  christos       trace_debug ("Need more space in the trace buffer");
   1637  1.1  christos 
   1638  1.1  christos       /* If we have a circular buffer, we can try discarding the
   1639  1.1  christos 	 oldest traceframe and see if that helps.  */
   1640  1.1  christos       oldest = FIRST_TRACEFRAME ();
   1641  1.1  christos       if (oldest->tpnum == 0)
   1642  1.1  christos 	{
   1643  1.1  christos 	  /* Not good; we have no traceframes to free.  Perhaps we're
   1644  1.1  christos 	     asking for a block that is larger than the buffer?  In
   1645  1.1  christos 	     any case, give up.  */
   1646  1.1  christos 	  trace_debug ("No traceframes to discard");
   1647  1.1  christos 	  return NULL;
   1648  1.1  christos 	}
   1649  1.1  christos 
   1650  1.1  christos       /* We don't run this code in the in-process agent currently.
   1651  1.1  christos 	 E.g., we could leave the in-process agent in autonomous
   1652  1.1  christos 	 circular mode if we only have fast tracepoints.  If we do
   1653  1.1  christos 	 that, then this bit becomes racy with GDBserver, which also
   1654  1.1  christos 	 writes to this counter.  */
   1655  1.1  christos       --traceframe_write_count;
   1656  1.1  christos 
   1657  1.1  christos       new_start = (unsigned char *) NEXT_TRACEFRAME (oldest);
   1658  1.1  christos       /* If we freed the traceframe that wrapped around, go back
   1659  1.1  christos 	 to the non-wrap case.  */
   1660  1.1  christos       if (new_start < tbctrl->start)
   1661  1.1  christos 	{
   1662  1.1  christos 	  trace_debug ("Discarding past the wraparound");
   1663  1.1  christos 	  tbctrl->wrap = trace_buffer_hi;
   1664  1.1  christos 	}
   1665  1.1  christos       tbctrl->start = new_start;
   1666  1.1  christos       tbctrl->end_free = tbctrl->start;
   1667  1.1  christos 
   1668  1.1  christos       trace_debug ("Discarded a traceframe\n"
   1669  1.1  christos 		   "Trace buffer [%d], start=%d free=%d "
   1670  1.1  christos 		   "endfree=%d wrap=%d hi=%d",
   1671  1.1  christos 		   curr,
   1672  1.1  christos 		   (int) (tbctrl->start - trace_buffer_lo),
   1673  1.1  christos 		   (int) (tbctrl->free - trace_buffer_lo),
   1674  1.1  christos 		   (int) (tbctrl->end_free - trace_buffer_lo),
   1675  1.1  christos 		   (int) (tbctrl->wrap - trace_buffer_lo),
   1676  1.1  christos 		   (int) (trace_buffer_hi - trace_buffer_lo));
   1677  1.1  christos 
   1678  1.1  christos       /* Now go back around the loop.  The discard might have resulted
   1679  1.1  christos 	 in either one or two pieces of free space, so we want to try
   1680  1.1  christos 	 both before freeing any more traceframes.  */
   1681  1.1  christos #endif
   1682  1.1  christos     }
   1683  1.1  christos 
   1684  1.1  christos   /* If we get here, we know we can provide the asked-for space.  */
   1685  1.1  christos 
   1686  1.1  christos   rslt = tbctrl->free;
   1687  1.1  christos 
   1688  1.1  christos   /* Adjust the request back down, now that we know we have space for
   1689  1.1  christos      the marker, but don't commit to AMT yet, we may still need to
   1690  1.1  christos      restart the operation if GDBserver touches the trace buffer
   1691  1.1  christos      (obviously only important in the in-process agent's version).  */
   1692  1.1  christos   tbctrl->free += (amt - sizeof (struct traceframe));
   1693  1.1  christos 
   1694  1.1  christos   /* Or not.  If GDBserver changed the trace buffer behind our back,
   1695  1.1  christos      we get to restart a new allocation attempt.  */
   1696  1.1  christos 
   1697  1.1  christos #ifdef IN_PROCESS_AGENT
   1698  1.1  christos   /* Build the tentative token.  */
   1699  1.1  christos   commit_count = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) + 0x100)
   1700  1.1  christos 		  & GDBSERVER_FLUSH_COUNT_MASK_CURR);
   1701  1.1  christos   commit = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) << 12)
   1702  1.1  christos 	    | commit_count
   1703  1.1  christos 	    | curr);
   1704  1.1  christos 
   1705  1.1  christos   /* Try to commit it.  */
   1706  1.1  christos   readout = cmpxchg (&trace_buffer_ctrl_curr, prev, commit);
   1707  1.1  christos   if (readout != prev)
   1708  1.1  christos     {
   1709  1.1  christos       trace_debug ("GDBserver has touched the trace buffer, restarting."
   1710  1.1  christos 		   " (prev=%08x, commit=%08x, readout=%08x)",
   1711  1.1  christos 		   prev, commit, readout);
   1712  1.1  christos       goto again;
   1713  1.1  christos     }
   1714  1.1  christos 
   1715  1.1  christos   /* Hold your horses here.  Even if that change was committed,
   1716  1.1  christos      GDBserver could come in, and clobber it.  We need to hold to be
   1717  1.1  christos      able to tell if GDBserver clobbers before or after we committed
   1718  1.1  christos      the change.  Whenever GDBserver goes about touching the IPA
   1719  1.1  christos      buffer, it sets a breakpoint in this routine, so we have a sync
   1720  1.1  christos      point here.  */
   1721  1.1  christos   about_to_request_buffer_space ();
   1722  1.1  christos 
   1723  1.1  christos   /* Check if the change has been effective, even if GDBserver stopped
   1724  1.1  christos      us at the breakpoint.  */
   1725  1.1  christos 
   1726  1.1  christos   {
   1727  1.1  christos     unsigned int refetch;
   1728  1.1  christos 
   1729  1.1  christos     memory_barrier ();
   1730  1.1  christos 
   1731  1.1  christos     refetch = trace_buffer_ctrl_curr;
   1732  1.1  christos 
   1733  1.1  christos     if (refetch == commit
   1734  1.1  christos 	|| ((refetch & GDBSERVER_FLUSH_COUNT_MASK_PREV) >> 12) == commit_count)
   1735  1.1  christos       {
   1736  1.1  christos 	/* effective */
   1737  1.1  christos 	trace_debug ("change is effective: (prev=%08x, commit=%08x, "
   1738  1.1  christos 		     "readout=%08x, refetch=%08x)",
   1739  1.1  christos 		     prev, commit, readout, refetch);
   1740  1.1  christos       }
   1741  1.1  christos     else
   1742  1.1  christos       {
   1743  1.1  christos 	trace_debug ("GDBserver has touched the trace buffer, not effective."
   1744  1.1  christos 		     " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
   1745  1.1  christos 		     prev, commit, readout, refetch);
   1746  1.1  christos 	goto again;
   1747  1.1  christos       }
   1748  1.1  christos   }
   1749  1.1  christos #endif
   1750  1.1  christos 
   1751  1.1  christos   /* We have a new piece of the trace buffer.  Hurray!  */
   1752  1.1  christos 
   1753  1.1  christos   /* Add an EOB marker just past this allocation.  */
   1754  1.1  christos   ((struct traceframe *) tbctrl->free)->tpnum = 0;
   1755  1.1  christos   ((struct traceframe *) tbctrl->free)->data_size = 0;
   1756  1.1  christos 
   1757  1.1  christos   /* Adjust the request back down, now that we know we have space for
   1758  1.1  christos      the marker.  */
   1759  1.1  christos   amt -= sizeof (struct traceframe);
   1760  1.1  christos 
   1761  1.1  christos   if (debug_threads)
   1762  1.1  christos     {
   1763  1.1  christos       trace_debug ("Allocated %d bytes", (int) amt);
   1764  1.1  christos       trace_debug ("Trace buffer [%d] start=%d free=%d "
   1765  1.1  christos 		   "endfree=%d wrap=%d hi=%d",
   1766  1.1  christos 		   curr,
   1767  1.1  christos 		   (int) (tbctrl->start - trace_buffer_lo),
   1768  1.1  christos 		   (int) (tbctrl->free - trace_buffer_lo),
   1769  1.1  christos 		   (int) (tbctrl->end_free - trace_buffer_lo),
   1770  1.1  christos 		   (int) (tbctrl->wrap - trace_buffer_lo),
   1771  1.1  christos 		   (int) (trace_buffer_hi - trace_buffer_lo));
   1772  1.1  christos     }
   1773  1.1  christos 
   1774  1.1  christos   return rslt;
   1775  1.1  christos }
   1776  1.1  christos 
   1777  1.1  christos #ifndef IN_PROCESS_AGENT
   1778  1.1  christos 
   1779  1.1  christos /* Return the total free space.  This is not necessarily the largest
   1780  1.1  christos    block we can allocate, because of the two-part case.  */
   1781  1.1  christos 
   1782  1.1  christos static int
   1783  1.1  christos free_space (void)
   1784  1.1  christos {
   1785  1.1  christos   if (trace_buffer_free <= trace_buffer_end_free)
   1786  1.1  christos     return trace_buffer_end_free - trace_buffer_free;
   1787  1.1  christos   else
   1788  1.1  christos     return ((trace_buffer_end_free - trace_buffer_lo)
   1789  1.1  christos 	    + (trace_buffer_hi - trace_buffer_free));
   1790  1.1  christos }
   1791  1.1  christos 
   1792  1.1  christos /* An 'S' in continuation packets indicates remainder are for
   1793  1.1  christos    while-stepping.  */
   1794  1.1  christos 
   1795  1.1  christos static int seen_step_action_flag;
   1796  1.1  christos 
   1797  1.1  christos /* Create a tracepoint (location) with given number and address.  Add this
   1798  1.1  christos    new tracepoint to list and sort this list.  */
   1799  1.1  christos 
   1800  1.1  christos static struct tracepoint *
   1801  1.1  christos add_tracepoint (int num, CORE_ADDR addr)
   1802  1.1  christos {
   1803  1.1  christos   struct tracepoint *tpoint, **tp_next;
   1804  1.1  christos 
   1805  1.1  christos   tpoint = XNEW (struct tracepoint);
   1806  1.1  christos   tpoint->number = num;
   1807  1.1  christos   tpoint->address = addr;
   1808  1.1  christos   tpoint->numactions = 0;
   1809  1.1  christos   tpoint->actions = NULL;
   1810  1.1  christos   tpoint->actions_str = NULL;
   1811  1.1  christos   tpoint->cond = NULL;
   1812  1.1  christos   tpoint->num_step_actions = 0;
   1813  1.1  christos   tpoint->step_actions = NULL;
   1814  1.1  christos   tpoint->step_actions_str = NULL;
   1815  1.1  christos   /* Start all off as regular (slow) tracepoints.  */
   1816  1.1  christos   tpoint->type = trap_tracepoint;
   1817  1.1  christos   tpoint->orig_size = -1;
   1818  1.1  christos   tpoint->source_strings = NULL;
   1819  1.1  christos   tpoint->compiled_cond = 0;
   1820  1.1  christos   tpoint->handle = NULL;
   1821  1.1  christos   tpoint->next = NULL;
   1822  1.1  christos 
   1823  1.1  christos   /* Find a place to insert this tracepoint into list in order to keep
   1824  1.1  christos      the tracepoint list still in the ascending order.  There may be
   1825  1.1  christos      multiple tracepoints at the same address as TPOINT's, and this
   1826  1.1  christos      guarantees TPOINT is inserted after all the tracepoints which are
   1827  1.1  christos      set at the same address.  For example, fast tracepoints A, B, C are
   1828  1.1  christos      set at the same address, and D is to be insert at the same place as
   1829  1.1  christos      well,
   1830  1.1  christos 
   1831  1.1  christos      -->| A |--> | B |-->| C |->...
   1832  1.1  christos 
   1833  1.1  christos      One jump pad was created for tracepoint A, B, and C, and the target
   1834  1.1  christos      address of A is referenced/used in jump pad.  So jump pad will let
   1835  1.1  christos      inferior jump to A.  If D is inserted in front of A, like this,
   1836  1.1  christos 
   1837  1.1  christos      -->| D |-->| A |--> | B |-->| C |->...
   1838  1.1  christos 
   1839  1.1  christos      without updating jump pad, D is not reachable during collect, which
   1840  1.1  christos      is wrong.  As we can see, the order of B, C and D doesn't matter, but
   1841  1.1  christos      A should always be the `first' one.  */
   1842  1.1  christos   for (tp_next = &tracepoints;
   1843  1.1  christos        (*tp_next) != NULL && (*tp_next)->address <= tpoint->address;
   1844  1.1  christos        tp_next = &(*tp_next)->next)
   1845  1.1  christos     ;
   1846  1.1  christos   tpoint->next = *tp_next;
   1847  1.1  christos   *tp_next = tpoint;
   1848  1.1  christos   last_tracepoint = tpoint;
   1849  1.1  christos 
   1850  1.1  christos   seen_step_action_flag = 0;
   1851  1.1  christos 
   1852  1.1  christos   return tpoint;
   1853  1.1  christos }
   1854  1.1  christos 
   1855  1.1  christos #ifndef IN_PROCESS_AGENT
   1856  1.1  christos 
   1857  1.1  christos /* Return the tracepoint with the given number and address, or NULL.  */
   1858  1.1  christos 
   1859  1.1  christos static struct tracepoint *
   1860  1.1  christos find_tracepoint (int id, CORE_ADDR addr)
   1861  1.1  christos {
   1862  1.1  christos   struct tracepoint *tpoint;
   1863  1.1  christos 
   1864  1.1  christos   for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
   1865  1.1  christos     if (tpoint->number == id && tpoint->address == addr)
   1866  1.1  christos       return tpoint;
   1867  1.1  christos 
   1868  1.1  christos   return NULL;
   1869  1.1  christos }
   1870  1.1  christos 
   1871  1.1  christos /* Remove TPOINT from global list.  */
   1872  1.1  christos 
   1873  1.1  christos static void
   1874  1.1  christos remove_tracepoint (struct tracepoint *tpoint)
   1875  1.1  christos {
   1876  1.1  christos   struct tracepoint *tp, *tp_prev;
   1877  1.1  christos 
   1878  1.1  christos   for (tp = tracepoints, tp_prev = NULL; tp && tp != tpoint;
   1879  1.1  christos        tp_prev = tp, tp = tp->next)
   1880  1.1  christos     ;
   1881  1.1  christos 
   1882  1.1  christos   if (tp)
   1883  1.1  christos     {
   1884  1.1  christos       if (tp_prev)
   1885  1.1  christos 	tp_prev->next = tp->next;
   1886  1.1  christos       else
   1887  1.1  christos 	tracepoints = tp->next;
   1888  1.1  christos 
   1889  1.1  christos       xfree (tp);
   1890  1.1  christos     }
   1891  1.1  christos }
   1892  1.1  christos 
   1893  1.1  christos /* There may be several tracepoints with the same number (because they
   1894  1.1  christos    are "locations", in GDB parlance); return the next one after the
   1895  1.1  christos    given tracepoint, or search from the beginning of the list if the
   1896  1.1  christos    first argument is NULL.  */
   1897  1.1  christos 
   1898  1.1  christos static struct tracepoint *
   1899  1.1  christos find_next_tracepoint_by_number (struct tracepoint *prev_tp, int num)
   1900  1.1  christos {
   1901  1.1  christos   struct tracepoint *tpoint;
   1902  1.1  christos 
   1903  1.1  christos   if (prev_tp)
   1904  1.1  christos     tpoint = prev_tp->next;
   1905  1.1  christos   else
   1906  1.1  christos     tpoint = tracepoints;
   1907  1.1  christos   for (; tpoint; tpoint = tpoint->next)
   1908  1.1  christos     if (tpoint->number == num)
   1909  1.1  christos       return tpoint;
   1910  1.1  christos 
   1911  1.1  christos   return NULL;
   1912  1.1  christos }
   1913  1.1  christos 
   1914  1.1  christos #endif
   1915  1.1  christos 
   1916  1.1  christos /* Append another action to perform when the tracepoint triggers.  */
   1917  1.1  christos 
   1918  1.1  christos static void
   1919  1.1  christos add_tracepoint_action (struct tracepoint *tpoint, const char *packet)
   1920  1.1  christos {
   1921  1.1  christos   const char *act;
   1922  1.1  christos 
   1923  1.1  christos   if (*packet == 'S')
   1924  1.1  christos     {
   1925  1.1  christos       seen_step_action_flag = 1;
   1926  1.1  christos       ++packet;
   1927  1.1  christos     }
   1928  1.1  christos 
   1929  1.1  christos   act = packet;
   1930  1.1  christos 
   1931  1.1  christos   while (*act)
   1932  1.1  christos     {
   1933  1.1  christos       const char *act_start = act;
   1934  1.1  christos       struct tracepoint_action *action = NULL;
   1935  1.1  christos 
   1936  1.1  christos       switch (*act)
   1937  1.1  christos 	{
   1938  1.1  christos 	case 'M':
   1939  1.1  christos 	  {
   1940  1.1  christos 	    struct collect_memory_action *maction =
   1941  1.1  christos 	      XNEW (struct collect_memory_action);
   1942  1.1  christos 	    ULONGEST basereg;
   1943  1.1  christos 	    int is_neg;
   1944  1.1  christos 
   1945  1.1  christos 	    maction->base.type = *act;
   1946  1.1  christos 	    action = &maction->base;
   1947  1.1  christos 
   1948  1.1  christos 	    ++act;
   1949  1.1  christos 	    is_neg = (*act == '-');
   1950  1.1  christos 	    if (*act == '-')
   1951  1.1  christos 	      ++act;
   1952  1.1  christos 	    act = unpack_varlen_hex (act, &basereg);
   1953  1.1  christos 	    ++act;
   1954  1.1  christos 	    act = unpack_varlen_hex (act, &maction->addr);
   1955  1.1  christos 	    ++act;
   1956  1.1  christos 	    act = unpack_varlen_hex (act, &maction->len);
   1957  1.1  christos 	    maction->basereg = (is_neg
   1958  1.1  christos 				? - (int) basereg
   1959  1.1  christos 				: (int) basereg);
   1960  1.1  christos 	    trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
   1961  1.1  christos 			 pulongest (maction->len),
   1962  1.1  christos 			 paddress (maction->addr), maction->basereg);
   1963  1.1  christos 	    break;
   1964  1.1  christos 	  }
   1965  1.1  christos 	case 'R':
   1966  1.1  christos 	  {
   1967  1.1  christos 	    struct collect_registers_action *raction =
   1968  1.1  christos 	      XNEW (struct collect_registers_action);
   1969  1.1  christos 
   1970  1.1  christos 	    raction->base.type = *act;
   1971  1.1  christos 	    action = &raction->base;
   1972  1.1  christos 
   1973  1.1  christos 	    trace_debug ("Want to collect registers");
   1974  1.1  christos 	    ++act;
   1975  1.1  christos 	    /* skip past hex digits of mask for now */
   1976  1.1  christos 	    while (isxdigit(*act))
   1977  1.1  christos 	      ++act;
   1978  1.1  christos 	    break;
   1979  1.1  christos 	  }
   1980  1.1  christos 	case 'L':
   1981  1.1  christos 	  {
   1982  1.1  christos 	    struct collect_static_trace_data_action *raction =
   1983  1.1  christos 	      XNEW (struct collect_static_trace_data_action);
   1984  1.1  christos 
   1985  1.1  christos 	    raction->base.type = *act;
   1986  1.1  christos 	    action = &raction->base;
   1987  1.1  christos 
   1988  1.1  christos 	    trace_debug ("Want to collect static trace data");
   1989  1.1  christos 	    ++act;
   1990  1.1  christos 	    break;
   1991  1.1  christos 	  }
   1992  1.1  christos 	case 'S':
   1993  1.1  christos 	  trace_debug ("Unexpected step action, ignoring");
   1994  1.1  christos 	  ++act;
   1995  1.1  christos 	  break;
   1996  1.1  christos 	case 'X':
   1997  1.1  christos 	  {
   1998  1.1  christos 	    struct eval_expr_action *xaction = XNEW (struct eval_expr_action);
   1999  1.1  christos 
   2000  1.1  christos 	    xaction->base.type = *act;
   2001  1.1  christos 	    action = &xaction->base;
   2002  1.1  christos 
   2003  1.1  christos 	    trace_debug ("Want to evaluate expression");
   2004  1.1  christos 	    xaction->expr = gdb_parse_agent_expr (&act);
   2005  1.1  christos 	    break;
   2006  1.1  christos 	  }
   2007  1.1  christos 	default:
   2008  1.1  christos 	  trace_debug ("unknown trace action '%c', ignoring...", *act);
   2009  1.1  christos 	  break;
   2010  1.1  christos 	case '-':
   2011  1.1  christos 	  break;
   2012  1.1  christos 	}
   2013  1.1  christos 
   2014  1.1  christos       if (action == NULL)
   2015  1.1  christos 	break;
   2016  1.1  christos 
   2017  1.1  christos       if (seen_step_action_flag)
   2018  1.1  christos 	{
   2019  1.1  christos 	  tpoint->num_step_actions++;
   2020  1.1  christos 
   2021  1.1  christos 	  tpoint->step_actions
   2022  1.1  christos 	    = XRESIZEVEC (struct tracepoint_action *, tpoint->step_actions,
   2023  1.1  christos 			  tpoint->num_step_actions);
   2024  1.1  christos 	  tpoint->step_actions_str
   2025  1.1  christos 	    = XRESIZEVEC (char *, tpoint->step_actions_str,
   2026  1.1  christos 			  tpoint->num_step_actions);
   2027  1.1  christos 	  tpoint->step_actions[tpoint->num_step_actions - 1] = action;
   2028  1.1  christos 	  tpoint->step_actions_str[tpoint->num_step_actions - 1]
   2029  1.1  christos 	    = savestring (act_start, act - act_start);
   2030  1.1  christos 	}
   2031  1.1  christos       else
   2032  1.1  christos 	{
   2033  1.1  christos 	  tpoint->numactions++;
   2034  1.1  christos 	  tpoint->actions
   2035  1.1  christos 	    = XRESIZEVEC (struct tracepoint_action *, tpoint->actions,
   2036  1.1  christos 			  tpoint->numactions);
   2037  1.1  christos 	  tpoint->actions_str
   2038  1.1  christos 	    = XRESIZEVEC (char *, tpoint->actions_str, tpoint->numactions);
   2039  1.1  christos 	  tpoint->actions[tpoint->numactions - 1] = action;
   2040  1.1  christos 	  tpoint->actions_str[tpoint->numactions - 1]
   2041  1.1  christos 	    = savestring (act_start, act - act_start);
   2042  1.1  christos 	}
   2043  1.1  christos     }
   2044  1.1  christos }
   2045  1.1  christos 
   2046  1.1  christos #endif
   2047  1.1  christos 
   2048  1.1  christos /* Find or create a trace state variable with the given number.  */
   2049  1.1  christos 
   2050  1.1  christos static struct trace_state_variable *
   2051  1.1  christos get_trace_state_variable (int num)
   2052  1.1  christos {
   2053  1.1  christos   struct trace_state_variable *tsv;
   2054  1.1  christos 
   2055  1.1  christos #ifdef IN_PROCESS_AGENT
   2056  1.1  christos   /* Search for an existing variable.  */
   2057  1.1  christos   for (tsv = alloced_trace_state_variables; tsv; tsv = tsv->next)
   2058  1.1  christos     if (tsv->number == num)
   2059  1.1  christos       return tsv;
   2060  1.1  christos #endif
   2061  1.1  christos 
   2062  1.1  christos   /* Search for an existing variable.  */
   2063  1.1  christos   for (tsv = trace_state_variables; tsv; tsv = tsv->next)
   2064  1.1  christos     if (tsv->number == num)
   2065  1.1  christos       return tsv;
   2066  1.1  christos 
   2067  1.1  christos   return NULL;
   2068  1.1  christos }
   2069  1.1  christos 
   2070  1.1  christos /* Find or create a trace state variable with the given number.  */
   2071  1.1  christos 
   2072  1.1  christos static struct trace_state_variable *
   2073  1.1  christos create_trace_state_variable (int num, int gdb)
   2074  1.1  christos {
   2075  1.1  christos   struct trace_state_variable *tsv;
   2076  1.1  christos 
   2077  1.1  christos   tsv = get_trace_state_variable (num);
   2078  1.1  christos   if (tsv != NULL)
   2079  1.1  christos     return tsv;
   2080  1.1  christos 
   2081  1.1  christos   /* Create a new variable.  */
   2082  1.1  christos   tsv = XNEW (struct trace_state_variable);
   2083  1.1  christos   tsv->number = num;
   2084  1.1  christos   tsv->initial_value = 0;
   2085  1.1  christos   tsv->value = 0;
   2086  1.1  christos   tsv->getter = NULL;
   2087  1.1  christos   tsv->name = NULL;
   2088  1.1  christos #ifdef IN_PROCESS_AGENT
   2089  1.1  christos   if (!gdb)
   2090  1.1  christos     {
   2091  1.1  christos       tsv->next = alloced_trace_state_variables;
   2092  1.1  christos       alloced_trace_state_variables = tsv;
   2093  1.1  christos     }
   2094  1.1  christos   else
   2095  1.1  christos #endif
   2096  1.1  christos     {
   2097  1.1  christos       tsv->next = trace_state_variables;
   2098  1.1  christos       trace_state_variables = tsv;
   2099  1.1  christos     }
   2100  1.1  christos   return tsv;
   2101  1.1  christos }
   2102  1.1  christos 
   2103  1.1  christos /* This is needed for -Wmissing-declarations.  */
   2104  1.1  christos IP_AGENT_EXPORT_FUNC LONGEST get_trace_state_variable_value (int num);
   2105  1.1  christos 
   2106  1.1  christos IP_AGENT_EXPORT_FUNC LONGEST
   2107  1.1  christos get_trace_state_variable_value (int num)
   2108  1.1  christos {
   2109  1.1  christos   struct trace_state_variable *tsv;
   2110  1.1  christos 
   2111  1.1  christos   tsv = get_trace_state_variable (num);
   2112  1.1  christos 
   2113  1.1  christos   if (!tsv)
   2114  1.1  christos     {
   2115  1.1  christos       trace_debug ("No trace state variable %d, skipping value get", num);
   2116  1.1  christos       return 0;
   2117  1.1  christos     }
   2118  1.1  christos 
   2119  1.1  christos   /* Call a getter function if we have one.  While it's tempting to
   2120  1.1  christos      set up something to only call the getter once per tracepoint hit,
   2121  1.1  christos      it could run afoul of thread races. Better to let the getter
   2122  1.1  christos      handle it directly, if necessary to worry about it.  */
   2123  1.1  christos   if (tsv->getter)
   2124  1.1  christos     tsv->value = (tsv->getter) ();
   2125  1.1  christos 
   2126  1.1  christos   trace_debug ("get_trace_state_variable_value(%d) ==> %s",
   2127  1.1  christos 	       num, plongest (tsv->value));
   2128  1.1  christos 
   2129  1.1  christos   return tsv->value;
   2130  1.1  christos }
   2131  1.1  christos 
   2132  1.1  christos /* This is needed for -Wmissing-declarations.  */
   2133  1.1  christos IP_AGENT_EXPORT_FUNC void set_trace_state_variable_value (int num,
   2134  1.1  christos 							  LONGEST val);
   2135  1.1  christos 
   2136  1.1  christos IP_AGENT_EXPORT_FUNC void
   2137  1.1  christos set_trace_state_variable_value (int num, LONGEST val)
   2138  1.1  christos {
   2139  1.1  christos   struct trace_state_variable *tsv;
   2140  1.1  christos 
   2141  1.1  christos   tsv = get_trace_state_variable (num);
   2142  1.1  christos 
   2143  1.1  christos   if (!tsv)
   2144  1.1  christos     {
   2145  1.1  christos       trace_debug ("No trace state variable %d, skipping value set", num);
   2146  1.1  christos       return;
   2147  1.1  christos     }
   2148  1.1  christos 
   2149  1.1  christos   tsv->value = val;
   2150  1.1  christos }
   2151  1.1  christos 
   2152  1.1  christos LONGEST
   2153  1.1  christos agent_get_trace_state_variable_value (int num)
   2154  1.1  christos {
   2155  1.1  christos   return get_trace_state_variable_value (num);
   2156  1.1  christos }
   2157  1.1  christos 
   2158  1.1  christos void
   2159  1.1  christos agent_set_trace_state_variable_value (int num, LONGEST val)
   2160  1.1  christos {
   2161  1.1  christos   set_trace_state_variable_value (num, val);
   2162  1.1  christos }
   2163  1.1  christos 
   2164  1.1  christos static void
   2165  1.1  christos set_trace_state_variable_name (int num, const char *name)
   2166  1.1  christos {
   2167  1.1  christos   struct trace_state_variable *tsv;
   2168  1.1  christos 
   2169  1.1  christos   tsv = get_trace_state_variable (num);
   2170  1.1  christos 
   2171  1.1  christos   if (!tsv)
   2172  1.1  christos     {
   2173  1.1  christos       trace_debug ("No trace state variable %d, skipping name set", num);
   2174  1.1  christos       return;
   2175  1.1  christos     }
   2176  1.1  christos 
   2177  1.1  christos   tsv->name = (char *) name;
   2178  1.1  christos }
   2179  1.1  christos 
   2180  1.1  christos static void
   2181  1.1  christos set_trace_state_variable_getter (int num, LONGEST (*getter) (void))
   2182  1.1  christos {
   2183  1.1  christos   struct trace_state_variable *tsv;
   2184  1.1  christos 
   2185  1.1  christos   tsv = get_trace_state_variable (num);
   2186  1.1  christos 
   2187  1.1  christos   if (!tsv)
   2188  1.1  christos     {
   2189  1.1  christos       trace_debug ("No trace state variable %d, skipping getter set", num);
   2190  1.1  christos       return;
   2191  1.1  christos     }
   2192  1.1  christos 
   2193  1.1  christos   tsv->getter = getter;
   2194  1.1  christos }
   2195  1.1  christos 
   2196  1.1  christos /* Add a raw traceframe for the given tracepoint.  */
   2197  1.1  christos 
   2198  1.1  christos static struct traceframe *
   2199  1.1  christos add_traceframe (struct tracepoint *tpoint)
   2200  1.1  christos {
   2201  1.1  christos   struct traceframe *tframe;
   2202  1.1  christos 
   2203  1.1  christos   tframe
   2204  1.1  christos     = (struct traceframe *) trace_buffer_alloc (sizeof (struct traceframe));
   2205  1.1  christos 
   2206  1.1  christos   if (tframe == NULL)
   2207  1.1  christos     return NULL;
   2208  1.1  christos 
   2209  1.1  christos   tframe->tpnum = tpoint->number;
   2210  1.1  christos   tframe->data_size = 0;
   2211  1.1  christos 
   2212  1.1  christos   return tframe;
   2213  1.1  christos }
   2214  1.1  christos 
   2215  1.1  christos /* Add a block to the traceframe currently being worked on.  */
   2216  1.1  christos 
   2217  1.1  christos static unsigned char *
   2218  1.1  christos add_traceframe_block (struct traceframe *tframe,
   2219  1.1  christos 		      struct tracepoint *tpoint, int amt)
   2220  1.1  christos {
   2221  1.1  christos   unsigned char *block;
   2222  1.1  christos 
   2223  1.1  christos   if (!tframe)
   2224  1.1  christos     return NULL;
   2225  1.1  christos 
   2226  1.1  christos   block = (unsigned char *) trace_buffer_alloc (amt);
   2227  1.1  christos 
   2228  1.1  christos   if (!block)
   2229  1.1  christos     return NULL;
   2230  1.1  christos 
   2231  1.1  christos   gdb_assert (tframe->tpnum == tpoint->number);
   2232  1.1  christos 
   2233  1.1  christos   tframe->data_size += amt;
   2234  1.1  christos   tpoint->traceframe_usage += amt;
   2235  1.1  christos 
   2236  1.1  christos   return block;
   2237  1.1  christos }
   2238  1.1  christos 
   2239  1.1  christos /* Flag that the current traceframe is finished.  */
   2240  1.1  christos 
   2241  1.1  christos static void
   2242  1.1  christos finish_traceframe (struct traceframe *tframe)
   2243  1.1  christos {
   2244  1.1  christos   ++traceframe_write_count;
   2245  1.1  christos   ++traceframes_created;
   2246  1.1  christos }
   2247  1.1  christos 
   2248  1.1  christos #ifndef IN_PROCESS_AGENT
   2249  1.1  christos 
   2250  1.1  christos /* Given a traceframe number NUM, find the NUMth traceframe in the
   2251  1.1  christos    buffer.  */
   2252  1.1  christos 
   2253  1.1  christos static struct traceframe *
   2254  1.1  christos find_traceframe (int num)
   2255  1.1  christos {
   2256  1.1  christos   struct traceframe *tframe;
   2257  1.1  christos   int tfnum = 0;
   2258  1.1  christos 
   2259  1.1  christos   for (tframe = FIRST_TRACEFRAME ();
   2260  1.1  christos        tframe->tpnum != 0;
   2261  1.1  christos        tframe = NEXT_TRACEFRAME (tframe))
   2262  1.1  christos     {
   2263  1.1  christos       if (tfnum == num)
   2264  1.1  christos 	return tframe;
   2265  1.1  christos       ++tfnum;
   2266  1.1  christos     }
   2267  1.1  christos 
   2268  1.1  christos   return NULL;
   2269  1.1  christos }
   2270  1.1  christos 
   2271  1.1  christos static CORE_ADDR
   2272  1.1  christos get_traceframe_address (struct traceframe *tframe)
   2273  1.1  christos {
   2274  1.1  christos   CORE_ADDR addr;
   2275  1.1  christos   struct tracepoint *tpoint;
   2276  1.1  christos 
   2277  1.1  christos   addr = traceframe_get_pc (tframe);
   2278  1.1  christos 
   2279  1.1  christos   if (addr)
   2280  1.1  christos     return addr;
   2281  1.1  christos 
   2282  1.1  christos   /* Fallback strategy, will be incorrect for while-stepping frames
   2283  1.1  christos      and multi-location tracepoints.  */
   2284  1.1  christos   tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
   2285  1.1  christos   return tpoint->address;
   2286  1.1  christos }
   2287  1.1  christos 
   2288  1.1  christos /* Search for the next traceframe whose address is inside or outside
   2289  1.1  christos    the given range.  */
   2290  1.1  christos 
   2291  1.1  christos static struct traceframe *
   2292  1.1  christos find_next_traceframe_in_range (CORE_ADDR lo, CORE_ADDR hi, int inside_p,
   2293  1.1  christos 			       int *tfnump)
   2294  1.1  christos {
   2295  1.1  christos   client_state &cs = get_client_state ();
   2296  1.1  christos   struct traceframe *tframe;
   2297  1.1  christos   CORE_ADDR tfaddr;
   2298  1.1  christos 
   2299  1.1  christos   *tfnump = cs.current_traceframe + 1;
   2300  1.1  christos   tframe = find_traceframe (*tfnump);
   2301  1.1  christos   /* The search is not supposed to wrap around.  */
   2302  1.1  christos   if (!tframe)
   2303  1.1  christos     {
   2304  1.1  christos       *tfnump = -1;
   2305  1.1  christos       return NULL;
   2306  1.1  christos     }
   2307  1.1  christos 
   2308  1.1  christos   for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
   2309  1.1  christos     {
   2310  1.1  christos       tfaddr = get_traceframe_address (tframe);
   2311  1.1  christos       if (inside_p
   2312  1.1  christos 	  ? (lo <= tfaddr && tfaddr <= hi)
   2313  1.1  christos 	  : (lo > tfaddr || tfaddr > hi))
   2314  1.1  christos 	return tframe;
   2315  1.1  christos       ++*tfnump;
   2316  1.1  christos     }
   2317  1.1  christos 
   2318  1.1  christos   *tfnump = -1;
   2319  1.1  christos   return NULL;
   2320  1.1  christos }
   2321  1.1  christos 
   2322  1.1  christos /* Search for the next traceframe recorded by the given tracepoint.
   2323  1.1  christos    Note that for multi-location tracepoints, this will find whatever
   2324  1.1  christos    location appears first.  */
   2325  1.1  christos 
   2326  1.1  christos static struct traceframe *
   2327  1.1  christos find_next_traceframe_by_tracepoint (int num, int *tfnump)
   2328  1.1  christos {
   2329  1.1  christos   client_state &cs = get_client_state ();
   2330  1.1  christos   struct traceframe *tframe;
   2331  1.1  christos 
   2332  1.1  christos   *tfnump = cs.current_traceframe + 1;
   2333  1.1  christos   tframe = find_traceframe (*tfnump);
   2334  1.1  christos   /* The search is not supposed to wrap around.  */
   2335  1.1  christos   if (!tframe)
   2336  1.1  christos     {
   2337  1.1  christos       *tfnump = -1;
   2338  1.1  christos       return NULL;
   2339  1.1  christos     }
   2340  1.1  christos 
   2341  1.1  christos   for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
   2342  1.1  christos     {
   2343  1.1  christos       if (tframe->tpnum == num)
   2344  1.1  christos 	return tframe;
   2345  1.1  christos       ++*tfnump;
   2346  1.1  christos     }
   2347  1.1  christos 
   2348  1.1  christos   *tfnump = -1;
   2349  1.1  christos   return NULL;
   2350  1.1  christos }
   2351  1.1  christos 
   2352  1.1  christos #endif
   2353  1.1  christos 
   2354  1.1  christos #ifndef IN_PROCESS_AGENT
   2355  1.1  christos 
   2356  1.1  christos /* Clear all past trace state.  */
   2357  1.1  christos 
   2358  1.1  christos static void
   2359  1.1  christos cmd_qtinit (char *packet)
   2360  1.1  christos {
   2361  1.1  christos   client_state &cs = get_client_state ();
   2362  1.1  christos   struct trace_state_variable *tsv, *prev, *next;
   2363  1.1  christos 
   2364  1.1  christos   /* Can't do this command without a pid attached.  */
   2365  1.1  christos   if (current_thread == NULL)
   2366  1.1  christos     {
   2367  1.1  christos       write_enn (packet);
   2368  1.1  christos       return;
   2369  1.1  christos     }
   2370  1.1  christos 
   2371  1.1  christos   /* Make sure we don't try to read from a trace frame.  */
   2372  1.1  christos   cs.current_traceframe = -1;
   2373  1.1  christos 
   2374  1.1  christos   stop_tracing ();
   2375  1.1  christos 
   2376  1.1  christos   trace_debug ("Initializing the trace");
   2377  1.1  christos 
   2378  1.1  christos   clear_installed_tracepoints ();
   2379  1.1  christos   clear_readonly_regions ();
   2380  1.1  christos 
   2381  1.1  christos   tracepoints = NULL;
   2382  1.1  christos   last_tracepoint = NULL;
   2383  1.1  christos 
   2384  1.1  christos   /* Clear out any leftover trace state variables.  Ones with target
   2385  1.1  christos      defined getters should be kept however.  */
   2386  1.1  christos   prev = NULL;
   2387  1.1  christos   tsv = trace_state_variables;
   2388  1.1  christos   while (tsv)
   2389  1.1  christos     {
   2390  1.1  christos       trace_debug ("Looking at var %d", tsv->number);
   2391  1.1  christos       if (tsv->getter == NULL)
   2392  1.1  christos 	{
   2393  1.1  christos 	  next = tsv->next;
   2394  1.1  christos 	  if (prev)
   2395  1.1  christos 	    prev->next = next;
   2396  1.1  christos 	  else
   2397  1.1  christos 	    trace_state_variables = next;
   2398  1.1  christos 	  trace_debug ("Deleting var %d", tsv->number);
   2399  1.1  christos 	  free (tsv);
   2400  1.1  christos 	  tsv = next;
   2401  1.1  christos 	}
   2402  1.1  christos       else
   2403  1.1  christos 	{
   2404  1.1  christos 	  prev = tsv;
   2405  1.1  christos 	  tsv = tsv->next;
   2406  1.1  christos 	}
   2407  1.1  christos     }
   2408  1.1  christos 
   2409  1.1  christos   clear_trace_buffer ();
   2410  1.1  christos   clear_inferior_trace_buffer ();
   2411  1.1  christos 
   2412  1.1  christos   write_ok (packet);
   2413  1.1  christos }
   2414  1.1  christos 
   2415  1.1  christos /* Unprobe the UST marker at ADDRESS.  */
   2416  1.1  christos 
   2417  1.1  christos static void
   2418  1.1  christos unprobe_marker_at (CORE_ADDR address)
   2419  1.1  christos {
   2420  1.1  christos   char cmd[IPA_CMD_BUF_SIZE];
   2421  1.1  christos 
   2422  1.1  christos   sprintf (cmd, "unprobe_marker_at:%s", paddress (address));
   2423  1.1  christos   run_inferior_command (cmd, strlen (cmd) + 1);
   2424  1.1  christos }
   2425  1.1  christos 
   2426  1.1  christos /* Restore the program to its pre-tracing state.  This routine may be called
   2427  1.1  christos    in error situations, so it needs to be careful about only restoring
   2428  1.1  christos    from known-valid bits.  */
   2429  1.1  christos 
   2430  1.1  christos static void
   2431  1.1  christos clear_installed_tracepoints (void)
   2432  1.1  christos {
   2433  1.1  christos   struct tracepoint *tpoint;
   2434  1.1  christos   struct tracepoint *prev_stpoint;
   2435  1.1  christos 
   2436  1.1  christos   target_pause_all (true);
   2437  1.1  christos 
   2438  1.1  christos   prev_stpoint = NULL;
   2439  1.1  christos 
   2440  1.1  christos   /* Restore any bytes overwritten by tracepoints.  */
   2441  1.1  christos   for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
   2442  1.1  christos     {
   2443  1.1  christos       /* Catch the case where we might try to remove a tracepoint that
   2444  1.1  christos 	 was never actually installed.  */
   2445  1.1  christos       if (tpoint->handle == NULL)
   2446  1.1  christos 	{
   2447  1.1  christos 	  trace_debug ("Tracepoint %d at 0x%s was "
   2448  1.1  christos 		       "never installed, nothing to clear",
   2449  1.1  christos 		       tpoint->number, paddress (tpoint->address));
   2450  1.1  christos 	  continue;
   2451  1.1  christos 	}
   2452  1.1  christos 
   2453  1.1  christos       switch (tpoint->type)
   2454  1.1  christos 	{
   2455  1.1  christos 	case trap_tracepoint:
   2456  1.1  christos 	  {
   2457  1.1  christos 	    struct breakpoint *bp
   2458  1.1  christos 	      = (struct breakpoint *) tpoint->handle;
   2459  1.1  christos 
   2460  1.1  christos 	    delete_breakpoint (bp);
   2461  1.1  christos 	  }
   2462  1.1  christos 	  break;
   2463  1.1  christos 	case fast_tracepoint:
   2464  1.1  christos 	  {
   2465  1.1  christos 	    struct fast_tracepoint_jump *jump
   2466  1.1  christos 	      = (struct fast_tracepoint_jump *) tpoint->handle;
   2467  1.1  christos 
   2468  1.1  christos 	    delete_fast_tracepoint_jump (jump);
   2469  1.1  christos 	  }
   2470  1.1  christos 	  break;
   2471  1.1  christos 	case static_tracepoint:
   2472  1.1  christos 	  if (prev_stpoint != NULL
   2473  1.1  christos 	      && prev_stpoint->address == tpoint->address)
   2474  1.1  christos 	    /* Nothing to do.  We already unprobed a tracepoint set at
   2475  1.1  christos 	       this marker address (and there can only be one probe
   2476  1.1  christos 	       per marker).  */
   2477  1.1  christos 	    ;
   2478  1.1  christos 	  else
   2479  1.1  christos 	    {
   2480  1.1  christos 	      unprobe_marker_at (tpoint->address);
   2481  1.1  christos 	      prev_stpoint = tpoint;
   2482  1.1  christos 	    }
   2483  1.1  christos 	  break;
   2484  1.1  christos 	}
   2485  1.1  christos 
   2486  1.1  christos       tpoint->handle = NULL;
   2487  1.1  christos     }
   2488  1.1  christos 
   2489  1.1  christos   target_unpause_all (true);
   2490  1.1  christos }
   2491  1.1  christos 
   2492  1.1  christos /* Parse a packet that defines a tracepoint.  */
   2493  1.1  christos 
   2494  1.1  christos static void
   2495  1.1  christos cmd_qtdp (char *own_buf)
   2496  1.1  christos {
   2497  1.1  christos   int tppacket;
   2498  1.1  christos   /* Whether there is a trailing hyphen at the end of the QTDP packet.  */
   2499  1.1  christos   int trail_hyphen = 0;
   2500  1.1  christos   ULONGEST num;
   2501  1.1  christos   ULONGEST addr;
   2502  1.1  christos   ULONGEST count;
   2503  1.1  christos   struct tracepoint *tpoint;
   2504  1.1  christos   const char *packet = own_buf;
   2505  1.1  christos 
   2506  1.1  christos   packet += strlen ("QTDP:");
   2507  1.1  christos 
   2508  1.1  christos   /* A hyphen at the beginning marks a packet specifying actions for a
   2509  1.1  christos      tracepoint already supplied.  */
   2510  1.1  christos   tppacket = 1;
   2511  1.1  christos   if (*packet == '-')
   2512  1.1  christos     {
   2513  1.1  christos       tppacket = 0;
   2514  1.1  christos       ++packet;
   2515  1.1  christos     }
   2516  1.1  christos   packet = unpack_varlen_hex (packet, &num);
   2517  1.1  christos   ++packet; /* skip a colon */
   2518  1.1  christos   packet = unpack_varlen_hex (packet, &addr);
   2519  1.1  christos   ++packet; /* skip a colon */
   2520  1.1  christos 
   2521  1.1  christos   /* See if we already have this tracepoint.  */
   2522  1.1  christos   tpoint = find_tracepoint (num, addr);
   2523  1.1  christos 
   2524  1.1  christos   if (tppacket)
   2525  1.1  christos     {
   2526  1.1  christos       /* Duplicate tracepoints are never allowed.  */
   2527  1.1  christos       if (tpoint)
   2528  1.1  christos 	{
   2529  1.1  christos 	  trace_debug ("Tracepoint error: tracepoint %d"
   2530  1.1  christos 		       " at 0x%s already exists",
   2531  1.1  christos 		       (int) num, paddress (addr));
   2532  1.1  christos 	  write_enn (own_buf);
   2533  1.1  christos 	  return;
   2534  1.1  christos 	}
   2535  1.1  christos 
   2536  1.1  christos       tpoint = add_tracepoint (num, addr);
   2537  1.1  christos 
   2538  1.1  christos       tpoint->enabled = (*packet == 'E');
   2539  1.1  christos       ++packet; /* skip 'E' */
   2540  1.1  christos       ++packet; /* skip a colon */
   2541  1.1  christos       packet = unpack_varlen_hex (packet, &count);
   2542  1.1  christos       tpoint->step_count = count;
   2543  1.1  christos       ++packet; /* skip a colon */
   2544  1.1  christos       packet = unpack_varlen_hex (packet, &count);
   2545  1.1  christos       tpoint->pass_count = count;
   2546  1.1  christos       /* See if we have any of the additional optional fields.  */
   2547  1.1  christos       while (*packet == ':')
   2548  1.1  christos 	{
   2549  1.1  christos 	  ++packet;
   2550  1.1  christos 	  if (*packet == 'F')
   2551  1.1  christos 	    {
   2552  1.1  christos 	      tpoint->type = fast_tracepoint;
   2553  1.1  christos 	      ++packet;
   2554  1.1  christos 	      packet = unpack_varlen_hex (packet, &count);
   2555  1.1  christos 	      tpoint->orig_size = count;
   2556  1.1  christos 	    }
   2557  1.1  christos 	  else if (*packet == 'S')
   2558  1.1  christos 	    {
   2559  1.1  christos 	      tpoint->type = static_tracepoint;
   2560  1.1  christos 	      ++packet;
   2561  1.1  christos 	    }
   2562  1.1  christos 	  else if (*packet == 'X')
   2563  1.1  christos 	    {
   2564  1.1  christos 	      tpoint->cond = gdb_parse_agent_expr (&packet);
   2565  1.1  christos 	    }
   2566  1.1  christos 	  else if (*packet == '-')
   2567  1.1  christos 	    break;
   2568  1.1  christos 	  else if (*packet == '\0')
   2569  1.1  christos 	    break;
   2570  1.1  christos 	  else
   2571  1.1  christos 	    trace_debug ("Unknown optional tracepoint field");
   2572  1.1  christos 	}
   2573  1.1  christos       if (*packet == '-')
   2574  1.1  christos 	{
   2575  1.1  christos 	  trail_hyphen = 1;
   2576  1.1  christos 	  trace_debug ("Also has actions\n");
   2577  1.1  christos 	}
   2578  1.1  christos 
   2579  1.1  christos       trace_debug ("Defined %stracepoint %d at 0x%s, "
   2580  1.1  christos 		   "enabled %d step %" PRIu64 " pass %" PRIu64,
   2581  1.1  christos 		   tpoint->type == fast_tracepoint ? "fast "
   2582  1.1  christos 		   : tpoint->type == static_tracepoint ? "static " : "",
   2583  1.1  christos 		   tpoint->number, paddress (tpoint->address), tpoint->enabled,
   2584  1.1  christos 		   tpoint->step_count, tpoint->pass_count);
   2585  1.1  christos     }
   2586  1.1  christos   else if (tpoint)
   2587  1.1  christos     add_tracepoint_action (tpoint, packet);
   2588  1.1  christos   else
   2589  1.1  christos     {
   2590  1.1  christos       trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
   2591  1.1  christos 		   (int) num, paddress (addr));
   2592  1.1  christos       write_enn (own_buf);
   2593  1.1  christos       return;
   2594  1.1  christos     }
   2595  1.1  christos 
   2596  1.1  christos   /* Install tracepoint during tracing only once for each tracepoint location.
   2597  1.1  christos      For each tracepoint loc, GDB may send multiple QTDP packets, and we can
   2598  1.1  christos      determine the last QTDP packet for one tracepoint location by checking
   2599  1.1  christos      trailing hyphen in QTDP packet.  */
   2600  1.1  christos   if (tracing && !trail_hyphen)
   2601  1.1  christos     {
   2602  1.1  christos       struct tracepoint *tp = NULL;
   2603  1.1  christos 
   2604  1.1  christos       /* Pause all threads temporarily while we patch tracepoints.  */
   2605  1.1  christos       target_pause_all (false);
   2606  1.1  christos 
   2607  1.1  christos       /* download_tracepoint will update global `tracepoints'
   2608  1.1  christos 	 list, so it is unsafe to leave threads in jump pad.  */
   2609  1.1  christos       target_stabilize_threads ();
   2610  1.1  christos 
   2611  1.1  christos       /* Freeze threads.  */
   2612  1.1  christos       target_pause_all (true);
   2613  1.1  christos 
   2614  1.1  christos 
   2615  1.1  christos       if (tpoint->type != trap_tracepoint)
   2616  1.1  christos 	{
   2617  1.1  christos 	  /* Find another fast or static tracepoint at the same address.  */
   2618  1.1  christos 	  for (tp = tracepoints; tp; tp = tp->next)
   2619  1.1  christos 	    {
   2620  1.1  christos 	      if (tp->address == tpoint->address && tp->type == tpoint->type
   2621  1.1  christos 		  && tp->number != tpoint->number)
   2622  1.1  christos 		break;
   2623  1.1  christos 	    }
   2624  1.1  christos 
   2625  1.1  christos 	  /* TPOINT is installed at the same address as TP.  */
   2626  1.1  christos 	  if (tp)
   2627  1.1  christos 	    {
   2628  1.1  christos 	      if (tpoint->type == fast_tracepoint)
   2629  1.1  christos 		clone_fast_tracepoint (tpoint, tp);
   2630  1.1  christos 	      else if (tpoint->type == static_tracepoint)
   2631  1.1  christos 		tpoint->handle = (void *) -1;
   2632  1.1  christos 	    }
   2633  1.1  christos 	}
   2634  1.1  christos 
   2635  1.1  christos       if (use_agent && tpoint->type == fast_tracepoint
   2636  1.1  christos 	  && agent_capability_check (AGENT_CAPA_FAST_TRACE))
   2637  1.1  christos 	{
   2638  1.1  christos 	  /* Download and install fast tracepoint by agent.  */
   2639  1.1  christos 	  if (tracepoint_send_agent (tpoint) == 0)
   2640  1.1  christos 	    write_ok (own_buf);
   2641  1.1  christos 	  else
   2642  1.1  christos 	    {
   2643  1.1  christos 	      write_enn (own_buf);
   2644  1.1  christos 	      remove_tracepoint (tpoint);
   2645  1.1  christos 	    }
   2646  1.1  christos 	}
   2647  1.1  christos       else
   2648  1.1  christos 	{
   2649  1.1  christos 	  download_tracepoint (tpoint);
   2650  1.1  christos 
   2651  1.1  christos 	  if (tpoint->type == trap_tracepoint || tp == NULL)
   2652  1.1  christos 	    {
   2653  1.1  christos 	      install_tracepoint (tpoint, own_buf);
   2654  1.1  christos 	      if (strcmp (own_buf, "OK") != 0)
   2655  1.1  christos 		remove_tracepoint (tpoint);
   2656  1.1  christos 	    }
   2657  1.1  christos 	  else
   2658  1.1  christos 	    write_ok (own_buf);
   2659  1.1  christos 	}
   2660  1.1  christos 
   2661  1.1  christos       target_unpause_all (true);
   2662  1.1  christos       return;
   2663  1.1  christos     }
   2664  1.1  christos 
   2665  1.1  christos   write_ok (own_buf);
   2666  1.1  christos }
   2667  1.1  christos 
   2668  1.1  christos static void
   2669  1.1  christos cmd_qtdpsrc (char *own_buf)
   2670  1.1  christos {
   2671  1.1  christos   ULONGEST num, addr, start, slen;
   2672  1.1  christos   struct tracepoint *tpoint;
   2673  1.1  christos   const char *packet = own_buf;
   2674  1.1  christos   const char *saved;
   2675  1.1  christos   char *srctype, *src;
   2676  1.1  christos   size_t nbytes;
   2677  1.1  christos   struct source_string *last, *newlast;
   2678  1.1  christos 
   2679  1.1  christos   packet += strlen ("QTDPsrc:");
   2680  1.1  christos 
   2681  1.1  christos   packet = unpack_varlen_hex (packet, &num);
   2682  1.1  christos   ++packet; /* skip a colon */
   2683  1.1  christos   packet = unpack_varlen_hex (packet, &addr);
   2684  1.1  christos   ++packet; /* skip a colon */
   2685  1.1  christos 
   2686  1.1  christos   /* See if we already have this tracepoint.  */
   2687  1.1  christos   tpoint = find_tracepoint (num, addr);
   2688  1.1  christos 
   2689  1.1  christos   if (!tpoint)
   2690  1.1  christos     {
   2691  1.1  christos       trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
   2692  1.1  christos 		   (int) num, paddress (addr));
   2693  1.1  christos       write_enn (own_buf);
   2694  1.1  christos       return;
   2695  1.1  christos     }
   2696  1.1  christos 
   2697  1.1  christos   saved = packet;
   2698  1.1  christos   packet = strchr (packet, ':');
   2699  1.1  christos   srctype = (char *) xmalloc (packet - saved + 1);
   2700  1.1  christos   memcpy (srctype, saved, packet - saved);
   2701  1.1  christos   srctype[packet - saved] = '\0';
   2702  1.1  christos   ++packet;
   2703  1.1  christos   packet = unpack_varlen_hex (packet, &start);
   2704  1.1  christos   ++packet; /* skip a colon */
   2705  1.1  christos   packet = unpack_varlen_hex (packet, &slen);
   2706  1.1  christos   ++packet; /* skip a colon */
   2707  1.1  christos   src = (char *) xmalloc (slen + 1);
   2708  1.1  christos   nbytes = hex2bin (packet, (gdb_byte *) src, strlen (packet) / 2);
   2709  1.1  christos   src[nbytes] = '\0';
   2710  1.1  christos 
   2711  1.1  christos   newlast = XNEW (struct source_string);
   2712  1.1  christos   newlast->type = srctype;
   2713  1.1  christos   newlast->str = src;
   2714  1.1  christos   newlast->next = NULL;
   2715  1.1  christos   /* Always add a source string to the end of the list;
   2716  1.1  christos      this keeps sequences of actions/commands in the right
   2717  1.1  christos      order.  */
   2718  1.1  christos   if (tpoint->source_strings)
   2719  1.1  christos     {
   2720  1.1  christos       for (last = tpoint->source_strings; last->next; last = last->next)
   2721  1.1  christos 	;
   2722  1.1  christos       last->next = newlast;
   2723  1.1  christos     }
   2724  1.1  christos   else
   2725  1.1  christos     tpoint->source_strings = newlast;
   2726  1.1  christos 
   2727  1.1  christos   write_ok (own_buf);
   2728  1.1  christos }
   2729  1.1  christos 
   2730  1.1  christos static void
   2731  1.1  christos cmd_qtdv (char *own_buf)
   2732  1.1  christos {
   2733  1.1  christos   ULONGEST num, val, builtin;
   2734  1.1  christos   char *varname;
   2735  1.1  christos   size_t nbytes;
   2736  1.1  christos   struct trace_state_variable *tsv;
   2737  1.1  christos   const char *packet = own_buf;
   2738  1.1  christos 
   2739  1.1  christos   packet += strlen ("QTDV:");
   2740  1.1  christos 
   2741  1.1  christos   packet = unpack_varlen_hex (packet, &num);
   2742  1.1  christos   ++packet; /* skip a colon */
   2743  1.1  christos   packet = unpack_varlen_hex (packet, &val);
   2744  1.1  christos   ++packet; /* skip a colon */
   2745  1.1  christos   packet = unpack_varlen_hex (packet, &builtin);
   2746  1.1  christos   ++packet; /* skip a colon */
   2747  1.1  christos 
   2748  1.1  christos   nbytes = strlen (packet) / 2;
   2749  1.1  christos   varname = (char *) xmalloc (nbytes + 1);
   2750  1.1  christos   nbytes = hex2bin (packet, (gdb_byte *) varname, nbytes);
   2751  1.1  christos   varname[nbytes] = '\0';
   2752  1.1  christos 
   2753  1.1  christos   tsv = create_trace_state_variable (num, 1);
   2754  1.1  christos   tsv->initial_value = (LONGEST) val;
   2755  1.1  christos   tsv->name = varname;
   2756  1.1  christos 
   2757  1.1  christos   set_trace_state_variable_value (num, (LONGEST) val);
   2758  1.1  christos 
   2759  1.1  christos   write_ok (own_buf);
   2760  1.1  christos }
   2761  1.1  christos 
   2762  1.1  christos static void
   2763  1.1  christos cmd_qtenable_disable (char *own_buf, int enable)
   2764  1.1  christos {
   2765  1.1  christos   const char *packet = own_buf;
   2766  1.1  christos   ULONGEST num, addr;
   2767  1.1  christos   struct tracepoint *tp;
   2768  1.1  christos 
   2769  1.1  christos   packet += strlen (enable ? "QTEnable:" : "QTDisable:");
   2770  1.1  christos   packet = unpack_varlen_hex (packet, &num);
   2771  1.1  christos   ++packet; /* skip a colon */
   2772  1.1  christos   packet = unpack_varlen_hex (packet, &addr);
   2773  1.1  christos 
   2774  1.1  christos   tp = find_tracepoint (num, addr);
   2775  1.1  christos 
   2776  1.1  christos   if (tp)
   2777  1.1  christos     {
   2778  1.1  christos       if ((enable && tp->enabled) || (!enable && !tp->enabled))
   2779  1.1  christos 	{
   2780  1.1  christos 	  trace_debug ("Tracepoint %d at 0x%s is already %s",
   2781  1.1  christos 		       (int) num, paddress (addr),
   2782  1.1  christos 		       enable ? "enabled" : "disabled");
   2783  1.1  christos 	  write_ok (own_buf);
   2784  1.1  christos 	  return;
   2785  1.1  christos 	}
   2786  1.1  christos 
   2787  1.1  christos       trace_debug ("%s tracepoint %d at 0x%s",
   2788  1.1  christos 		   enable ? "Enabling" : "Disabling",
   2789  1.1  christos 		   (int) num, paddress (addr));
   2790  1.1  christos 
   2791  1.1  christos       tp->enabled = enable;
   2792  1.1  christos 
   2793  1.1  christos       if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
   2794  1.1  christos 	{
   2795  1.1  christos 	  int ret;
   2796  1.1  christos 	  int offset = offsetof (struct tracepoint, enabled);
   2797  1.1  christos 	  CORE_ADDR obj_addr = tp->obj_addr_on_target + offset;
   2798  1.1  christos 
   2799  1.1  christos 	  ret = prepare_to_access_memory ();
   2800  1.1  christos 	  if (ret)
   2801  1.1  christos 	    {
   2802  1.1  christos 	      trace_debug ("Failed to temporarily stop inferior threads");
   2803  1.1  christos 	      write_enn (own_buf);
   2804  1.1  christos 	      return;
   2805  1.1  christos 	    }
   2806  1.1  christos 
   2807  1.1  christos 	  ret = write_inferior_int8 (obj_addr, enable);
   2808  1.1  christos 	  done_accessing_memory ();
   2809  1.1  christos 
   2810  1.1  christos 	  if (ret)
   2811  1.1  christos 	    {
   2812  1.1  christos 	      trace_debug ("Cannot write enabled flag into "
   2813  1.1  christos 			   "inferior process memory");
   2814  1.1  christos 	      write_enn (own_buf);
   2815  1.1  christos 	      return;
   2816  1.1  christos 	    }
   2817  1.1  christos 	}
   2818  1.1  christos 
   2819  1.1  christos       write_ok (own_buf);
   2820  1.1  christos     }
   2821  1.1  christos   else
   2822  1.1  christos     {
   2823  1.1  christos       trace_debug ("Tracepoint %d at 0x%s not found",
   2824  1.1  christos 		   (int) num, paddress (addr));
   2825  1.1  christos       write_enn (own_buf);
   2826  1.1  christos     }
   2827  1.1  christos }
   2828  1.1  christos 
   2829  1.1  christos static void
   2830  1.1  christos cmd_qtv (char *own_buf)
   2831  1.1  christos {
   2832  1.1  christos   client_state &cs = get_client_state ();
   2833  1.1  christos   ULONGEST num;
   2834  1.1  christos   LONGEST val = 0;
   2835  1.1  christos   int err;
   2836  1.1  christos   char *packet = own_buf;
   2837  1.1  christos 
   2838  1.1  christos   packet += strlen ("qTV:");
   2839  1.1  christos   unpack_varlen_hex (packet, &num);
   2840  1.1  christos 
   2841  1.1  christos   if (cs.current_traceframe >= 0)
   2842  1.1  christos     {
   2843  1.1  christos       err = traceframe_read_tsv ((int) num, &val);
   2844  1.1  christos       if (err)
   2845  1.1  christos 	{
   2846  1.1  christos 	  strcpy (own_buf, "U");
   2847  1.1  christos 	  return;
   2848  1.1  christos 	}
   2849  1.1  christos     }
   2850  1.1  christos   /* Only make tsv's be undefined before the first trace run.  After a
   2851  1.1  christos      trace run is over, the user might want to see the last value of
   2852  1.1  christos      the tsv, and it might not be available in a traceframe.  */
   2853  1.1  christos   else if (!tracing && strcmp (tracing_stop_reason, "tnotrun") == 0)
   2854  1.1  christos     {
   2855  1.1  christos       strcpy (own_buf, "U");
   2856  1.1  christos       return;
   2857  1.1  christos     }
   2858  1.1  christos   else
   2859  1.1  christos     val = get_trace_state_variable_value (num);
   2860  1.1  christos 
   2861  1.1  christos   sprintf (own_buf, "V%s", phex_nz (val, 0));
   2862  1.1  christos }
   2863  1.1  christos 
   2864  1.1  christos /* Clear out the list of readonly regions.  */
   2865  1.1  christos 
   2866  1.1  christos static void
   2867  1.1  christos clear_readonly_regions (void)
   2868  1.1  christos {
   2869  1.1  christos   struct readonly_region *roreg;
   2870  1.1  christos 
   2871  1.1  christos   while (readonly_regions)
   2872  1.1  christos     {
   2873  1.1  christos       roreg = readonly_regions;
   2874  1.1  christos       readonly_regions = readonly_regions->next;
   2875  1.1  christos       free (roreg);
   2876  1.1  christos     }
   2877  1.1  christos }
   2878  1.1  christos 
   2879  1.1  christos /* Parse the collection of address ranges whose contents GDB believes
   2880  1.1  christos    to be unchanging and so can be read directly from target memory
   2881  1.1  christos    even while looking at a traceframe.  */
   2882  1.1  christos 
   2883  1.1  christos static void
   2884  1.1  christos cmd_qtro (char *own_buf)
   2885  1.1  christos {
   2886  1.1  christos   ULONGEST start, end;
   2887  1.1  christos   struct readonly_region *roreg;
   2888  1.1  christos   const char *packet = own_buf;
   2889  1.1  christos 
   2890  1.1  christos   trace_debug ("Want to mark readonly regions");
   2891  1.1  christos 
   2892  1.1  christos   clear_readonly_regions ();
   2893  1.1  christos 
   2894  1.1  christos   packet += strlen ("QTro");
   2895  1.1  christos 
   2896  1.1  christos   while (*packet == ':')
   2897  1.1  christos     {
   2898  1.1  christos       ++packet;  /* skip a colon */
   2899  1.1  christos       packet = unpack_varlen_hex (packet, &start);
   2900  1.1  christos       ++packet;  /* skip a comma */
   2901  1.1  christos       packet = unpack_varlen_hex (packet, &end);
   2902  1.1  christos 
   2903  1.1  christos       roreg = XNEW (struct readonly_region);
   2904  1.1  christos       roreg->start = start;
   2905  1.1  christos       roreg->end = end;
   2906  1.1  christos       roreg->next = readonly_regions;
   2907  1.1  christos       readonly_regions = roreg;
   2908  1.1  christos       trace_debug ("Added readonly region from 0x%s to 0x%s",
   2909  1.1  christos 		   paddress (roreg->start), paddress (roreg->end));
   2910  1.1  christos     }
   2911  1.1  christos 
   2912  1.1  christos   write_ok (own_buf);
   2913  1.1  christos }
   2914  1.1  christos 
   2915  1.1  christos /* Test to see if the given range is in our list of readonly ranges.
   2916  1.1  christos    We only test for being entirely within a range, GDB is not going to
   2917  1.1  christos    send a single memory packet that spans multiple regions.  */
   2918  1.1  christos 
   2919  1.1  christos int
   2920  1.1  christos in_readonly_region (CORE_ADDR addr, ULONGEST length)
   2921  1.1  christos {
   2922  1.1  christos   struct readonly_region *roreg;
   2923  1.1  christos 
   2924  1.1  christos   for (roreg = readonly_regions; roreg; roreg = roreg->next)
   2925  1.1  christos     if (roreg->start <= addr && (addr + length - 1) <= roreg->end)
   2926  1.1  christos       return 1;
   2927  1.1  christos 
   2928  1.1  christos   return 0;
   2929  1.1  christos }
   2930  1.1  christos 
   2931  1.1  christos static CORE_ADDR gdb_jump_pad_head;
   2932  1.1  christos 
   2933  1.1  christos /* Return the address of the next free jump space.  */
   2934  1.1  christos 
   2935  1.1  christos static CORE_ADDR
   2936  1.1  christos get_jump_space_head (void)
   2937  1.1  christos {
   2938  1.1  christos   if (gdb_jump_pad_head == 0)
   2939  1.1  christos     {
   2940  1.1  christos       if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
   2941  1.1  christos 				      &gdb_jump_pad_head))
   2942  1.1  christos 	{
   2943  1.1  christos 	  internal_error (__FILE__, __LINE__,
   2944  1.1  christos 			  "error extracting jump_pad_buffer");
   2945  1.1  christos 	}
   2946  1.1  christos     }
   2947  1.1  christos 
   2948  1.1  christos   return gdb_jump_pad_head;
   2949  1.1  christos }
   2950  1.1  christos 
   2951  1.1  christos /* Reserve USED bytes from the jump space.  */
   2952  1.1  christos 
   2953  1.1  christos static void
   2954  1.1  christos claim_jump_space (ULONGEST used)
   2955  1.1  christos {
   2956  1.1  christos   trace_debug ("claim_jump_space reserves %s bytes at %s",
   2957  1.1  christos 	       pulongest (used), paddress (gdb_jump_pad_head));
   2958  1.1  christos   gdb_jump_pad_head += used;
   2959  1.1  christos }
   2960  1.1  christos 
   2961  1.1  christos static CORE_ADDR trampoline_buffer_head = 0;
   2962  1.1  christos static CORE_ADDR trampoline_buffer_tail;
   2963  1.1  christos 
   2964  1.1  christos /* Reserve USED bytes from the trampoline buffer and return the
   2965  1.1  christos    address of the start of the reserved space in TRAMPOLINE.  Returns
   2966  1.1  christos    non-zero if the space is successfully claimed.  */
   2967  1.1  christos 
   2968  1.1  christos int
   2969  1.1  christos claim_trampoline_space (ULONGEST used, CORE_ADDR *trampoline)
   2970  1.1  christos {
   2971  1.1  christos   if (!trampoline_buffer_head)
   2972  1.1  christos     {
   2973  1.1  christos       if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
   2974  1.1  christos 				      &trampoline_buffer_tail))
   2975  1.1  christos 	{
   2976  1.1  christos 	  internal_error (__FILE__, __LINE__,
   2977  1.1  christos 			  "error extracting trampoline_buffer");
   2978  1.1  christos 	}
   2979  1.1  christos 
   2980  1.1  christos       if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
   2981  1.1  christos 				      &trampoline_buffer_head))
   2982  1.1  christos 	{
   2983  1.1  christos 	  internal_error (__FILE__, __LINE__,
   2984  1.1  christos 			  "error extracting trampoline_buffer_end");
   2985  1.1  christos 	}
   2986  1.1  christos     }
   2987  1.1  christos 
   2988  1.1  christos   /* Start claiming space from the top of the trampoline space.  If
   2989  1.1  christos      the space is located at the bottom of the virtual address space,
   2990  1.1  christos      this reduces the possibility that corruption will occur if a null
   2991  1.1  christos      pointer is used to write to memory.  */
   2992  1.1  christos   if (trampoline_buffer_head - trampoline_buffer_tail < used)
   2993  1.1  christos     {
   2994  1.1  christos       trace_debug ("claim_trampoline_space failed to reserve %s bytes",
   2995  1.1  christos 		   pulongest (used));
   2996  1.1  christos       return 0;
   2997  1.1  christos     }
   2998  1.1  christos 
   2999  1.1  christos   trampoline_buffer_head -= used;
   3000  1.1  christos 
   3001  1.1  christos   trace_debug ("claim_trampoline_space reserves %s bytes at %s",
   3002  1.1  christos 	       pulongest (used), paddress (trampoline_buffer_head));
   3003  1.1  christos 
   3004  1.1  christos   *trampoline = trampoline_buffer_head;
   3005  1.1  christos   return 1;
   3006  1.1  christos }
   3007  1.1  christos 
   3008  1.1  christos /* Returns non-zero if there is space allocated for use in trampolines
   3009  1.1  christos    for fast tracepoints.  */
   3010  1.1  christos 
   3011  1.1  christos int
   3012  1.1  christos have_fast_tracepoint_trampoline_buffer (char *buf)
   3013  1.1  christos {
   3014  1.1  christos   CORE_ADDR trampoline_end, errbuf;
   3015  1.1  christos 
   3016  1.1  christos   if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
   3017  1.1  christos 				  &trampoline_end))
   3018  1.1  christos     {
   3019  1.1  christos       internal_error (__FILE__, __LINE__,
   3020  1.1  christos 		      "error extracting trampoline_buffer_end");
   3021  1.1  christos     }
   3022  1.1  christos 
   3023  1.1  christos   if (buf)
   3024  1.1  christos     {
   3025  1.1  christos       buf[0] = '\0';
   3026  1.1  christos       strcpy (buf, "was claiming");
   3027  1.1  christos       if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_error,
   3028  1.1  christos 				  &errbuf))
   3029  1.1  christos 	{
   3030  1.1  christos 	  internal_error (__FILE__, __LINE__,
   3031  1.1  christos 			  "error extracting errbuf");
   3032  1.1  christos 	}
   3033  1.1  christos 
   3034  1.1  christos       read_inferior_memory (errbuf, (unsigned char *) buf, 100);
   3035  1.1  christos     }
   3036  1.1  christos 
   3037  1.1  christos   return trampoline_end != 0;
   3038  1.1  christos }
   3039  1.1  christos 
   3040  1.1  christos /* Ask the IPA to probe the marker at ADDRESS.  Returns -1 if running
   3041  1.1  christos    the command fails, or 0 otherwise.  If the command ran
   3042  1.1  christos    successfully, but probing the marker failed, ERROUT will be filled
   3043  1.1  christos    with the error to reply to GDB, and -1 is also returned.  This
   3044  1.1  christos    allows directly passing IPA errors to GDB.  */
   3045  1.1  christos 
   3046  1.1  christos static int
   3047  1.1  christos probe_marker_at (CORE_ADDR address, char *errout)
   3048  1.1  christos {
   3049  1.1  christos   char cmd[IPA_CMD_BUF_SIZE];
   3050  1.1  christos   int err;
   3051  1.1  christos 
   3052  1.1  christos   sprintf (cmd, "probe_marker_at:%s", paddress (address));
   3053  1.1  christos   err = run_inferior_command (cmd, strlen (cmd) + 1);
   3054  1.1  christos 
   3055  1.1  christos   if (err == 0)
   3056  1.1  christos     {
   3057  1.1  christos       if (*cmd == 'E')
   3058  1.1  christos 	{
   3059  1.1  christos 	  strcpy (errout, cmd);
   3060  1.1  christos 	  return -1;
   3061  1.1  christos 	}
   3062  1.1  christos     }
   3063  1.1  christos 
   3064  1.1  christos   return err;
   3065  1.1  christos }
   3066  1.1  christos 
   3067  1.1  christos static void
   3068  1.1  christos clone_fast_tracepoint (struct tracepoint *to, const struct tracepoint *from)
   3069  1.1  christos {
   3070  1.1  christos   to->jump_pad = from->jump_pad;
   3071  1.1  christos   to->jump_pad_end = from->jump_pad_end;
   3072  1.1  christos   to->trampoline = from->trampoline;
   3073  1.1  christos   to->trampoline_end = from->trampoline_end;
   3074  1.1  christos   to->adjusted_insn_addr = from->adjusted_insn_addr;
   3075  1.1  christos   to->adjusted_insn_addr_end = from->adjusted_insn_addr_end;
   3076  1.1  christos   to->handle = from->handle;
   3077  1.1  christos 
   3078  1.1  christos   gdb_assert (from->handle);
   3079  1.1  christos   inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump *) from->handle);
   3080  1.1  christos }
   3081  1.1  christos 
   3082  1.1  christos #define MAX_JUMP_SIZE 20
   3083  1.1  christos 
   3084  1.1  christos /* Install fast tracepoint.  Return 0 if successful, otherwise return
   3085  1.1  christos    non-zero.  */
   3086  1.1  christos 
   3087  1.1  christos static int
   3088  1.1  christos install_fast_tracepoint (struct tracepoint *tpoint, char *errbuf)
   3089  1.1  christos {
   3090  1.1  christos   CORE_ADDR jentry, jump_entry;
   3091  1.1  christos   CORE_ADDR trampoline;
   3092  1.1  christos   CORE_ADDR collect;
   3093  1.1  christos   ULONGEST trampoline_size;
   3094  1.1  christos   int err = 0;
   3095  1.1  christos   /* The jump to the jump pad of the last fast tracepoint
   3096  1.1  christos      installed.  */
   3097  1.1  christos   unsigned char fjump[MAX_JUMP_SIZE];
   3098  1.1  christos   ULONGEST fjump_size;
   3099  1.1  christos 
   3100  1.1  christos   if (tpoint->orig_size < target_get_min_fast_tracepoint_insn_len ())
   3101  1.1  christos     {
   3102  1.1  christos       trace_debug ("Requested a fast tracepoint on an instruction "
   3103  1.1  christos 		   "that is of less than the minimum length.");
   3104  1.1  christos       return 0;
   3105  1.1  christos     }
   3106  1.1  christos 
   3107  1.1  christos   if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_collect_ptr,
   3108  1.1  christos 				  &collect))
   3109  1.1  christos     {
   3110  1.1  christos       error ("error extracting gdb_collect_ptr");
   3111  1.1  christos       return 1;
   3112  1.1  christos     }
   3113  1.1  christos 
   3114  1.1  christos   jentry = jump_entry = get_jump_space_head ();
   3115  1.1  christos 
   3116  1.1  christos   trampoline = 0;
   3117  1.1  christos   trampoline_size = 0;
   3118  1.1  christos 
   3119  1.1  christos   /* Install the jump pad.  */
   3120  1.1  christos   err = target_install_fast_tracepoint_jump_pad
   3121  1.1  christos     (tpoint->obj_addr_on_target, tpoint->address, collect,
   3122  1.1  christos      ipa_sym_addrs.addr_collecting, tpoint->orig_size, &jentry,
   3123  1.1  christos      &trampoline, &trampoline_size, fjump, &fjump_size,
   3124  1.1  christos      &tpoint->adjusted_insn_addr, &tpoint->adjusted_insn_addr_end, errbuf);
   3125  1.1  christos 
   3126  1.1  christos   if (err)
   3127  1.1  christos     return 1;
   3128  1.1  christos 
   3129  1.1  christos   /* Wire it in.  */
   3130  1.1  christos   tpoint->handle = set_fast_tracepoint_jump (tpoint->address, fjump,
   3131  1.1  christos 					     fjump_size);
   3132  1.1  christos 
   3133  1.1  christos   if (tpoint->handle != NULL)
   3134  1.1  christos     {
   3135  1.1  christos       tpoint->jump_pad = jump_entry;
   3136  1.1  christos       tpoint->jump_pad_end = jentry;
   3137  1.1  christos       tpoint->trampoline = trampoline;
   3138  1.1  christos       tpoint->trampoline_end = trampoline + trampoline_size;
   3139  1.1  christos 
   3140  1.1  christos       /* Pad to 8-byte alignment.  */
   3141  1.1  christos       jentry = ((jentry + 7) & ~0x7);
   3142  1.1  christos       claim_jump_space (jentry - jump_entry);
   3143  1.1  christos     }
   3144  1.1  christos 
   3145  1.1  christos   return 0;
   3146  1.1  christos }
   3147  1.1  christos 
   3148  1.1  christos 
   3149  1.1  christos /* Install tracepoint TPOINT, and write reply message in OWN_BUF.  */
   3150  1.1  christos 
   3151  1.1  christos static void
   3152  1.1  christos install_tracepoint (struct tracepoint *tpoint, char *own_buf)
   3153  1.1  christos {
   3154  1.1  christos   tpoint->handle = NULL;
   3155  1.1  christos   *own_buf = '\0';
   3156  1.1  christos 
   3157  1.1  christos   if (tpoint->type == trap_tracepoint)
   3158  1.1  christos     {
   3159  1.1  christos       /* Tracepoints are installed as memory breakpoints.  Just go
   3160  1.1  christos 	 ahead and install the trap.  The breakpoints module
   3161  1.1  christos 	 handles duplicated breakpoints, and the memory read
   3162  1.1  christos 	 routine handles un-patching traps from memory reads.  */
   3163  1.1  christos       tpoint->handle = set_breakpoint_at (tpoint->address,
   3164  1.1  christos 					  tracepoint_handler);
   3165  1.1  christos     }
   3166  1.1  christos   else if (tpoint->type == fast_tracepoint || tpoint->type == static_tracepoint)
   3167  1.1  christos     {
   3168  1.1  christos       if (!agent_loaded_p ())
   3169  1.1  christos 	{
   3170  1.1  christos 	  trace_debug ("Requested a %s tracepoint, but fast "
   3171  1.1  christos 		       "tracepoints aren't supported.",
   3172  1.1  christos 		       tpoint->type == static_tracepoint ? "static" : "fast");
   3173  1.1  christos 	  write_e_ipa_not_loaded (own_buf);
   3174  1.1  christos 	  return;
   3175  1.1  christos 	}
   3176  1.1  christos       if (tpoint->type == static_tracepoint
   3177  1.1  christos 	  && !in_process_agent_supports_ust ())
   3178  1.1  christos 	{
   3179  1.1  christos 	  trace_debug ("Requested a static tracepoint, but static "
   3180  1.1  christos 		       "tracepoints are not supported.");
   3181  1.1  christos 	  write_e_ust_not_loaded (own_buf);
   3182  1.1  christos 	  return;
   3183  1.1  christos 	}
   3184  1.1  christos 
   3185  1.1  christos       if (tpoint->type == fast_tracepoint)
   3186  1.1  christos 	install_fast_tracepoint (tpoint, own_buf);
   3187  1.1  christos       else
   3188  1.1  christos 	{
   3189  1.1  christos 	  if (probe_marker_at (tpoint->address, own_buf) == 0)
   3190  1.1  christos 	    tpoint->handle = (void *) -1;
   3191  1.1  christos 	}
   3192  1.1  christos 
   3193  1.1  christos     }
   3194  1.1  christos   else
   3195  1.1  christos     internal_error (__FILE__, __LINE__, "Unknown tracepoint type");
   3196  1.1  christos 
   3197  1.1  christos   if (tpoint->handle == NULL)
   3198  1.1  christos     {
   3199  1.1  christos       if (*own_buf == '\0')
   3200  1.1  christos 	write_enn (own_buf);
   3201  1.1  christos     }
   3202  1.1  christos   else
   3203  1.1  christos     write_ok (own_buf);
   3204  1.1  christos }
   3205  1.1  christos 
   3206  1.1  christos static void download_tracepoint_1 (struct tracepoint *tpoint);
   3207  1.1  christos 
   3208  1.1  christos static void
   3209  1.1  christos cmd_qtstart (char *packet)
   3210  1.1  christos {
   3211  1.1  christos   struct tracepoint *tpoint, *prev_ftpoint, *prev_stpoint;
   3212  1.1  christos   CORE_ADDR tpptr = 0, prev_tpptr = 0;
   3213  1.1  christos 
   3214  1.1  christos   trace_debug ("Starting the trace");
   3215  1.1  christos 
   3216  1.1  christos   /* Pause all threads temporarily while we patch tracepoints.  */
   3217  1.1  christos   target_pause_all (false);
   3218  1.1  christos 
   3219  1.1  christos   /* Get threads out of jump pads.  Safe to do here, since this is a
   3220  1.1  christos      top level command.  And, required to do here, since we're
   3221  1.1  christos      deleting/rewriting jump pads.  */
   3222  1.1  christos 
   3223  1.1  christos   target_stabilize_threads ();
   3224  1.1  christos 
   3225  1.1  christos   /* Freeze threads.  */
   3226  1.1  christos   target_pause_all (true);
   3227  1.1  christos 
   3228  1.1  christos   /* Sync the fast tracepoints list in the inferior ftlib.  */
   3229  1.1  christos   if (agent_loaded_p ())
   3230  1.1  christos     download_trace_state_variables ();
   3231  1.1  christos 
   3232  1.1  christos   /* No previous fast tpoint yet.  */
   3233  1.1  christos   prev_ftpoint = NULL;
   3234  1.1  christos 
   3235  1.1  christos   /* No previous static tpoint yet.  */
   3236  1.1  christos   prev_stpoint = NULL;
   3237  1.1  christos 
   3238  1.1  christos   *packet = '\0';
   3239  1.1  christos 
   3240  1.1  christos   if (agent_loaded_p ())
   3241  1.1  christos     {
   3242  1.1  christos       /* Tell IPA about the correct tdesc.  */
   3243  1.1  christos       if (write_inferior_integer (ipa_sym_addrs.addr_ipa_tdesc_idx,
   3244  1.1  christos 				  target_get_ipa_tdesc_idx ()))
   3245  1.1  christos         error ("Error setting ipa_tdesc_idx variable in lib");
   3246  1.1  christos     }
   3247  1.1  christos 
   3248  1.1  christos   /* Start out empty.  */
   3249  1.1  christos   if (agent_loaded_p ())
   3250  1.1  christos     write_inferior_data_pointer (ipa_sym_addrs.addr_tracepoints, 0);
   3251  1.1  christos 
   3252  1.1  christos   /* Download and install tracepoints.  */
   3253  1.1  christos   for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
   3254  1.1  christos     {
   3255  1.1  christos       /* Ensure all the hit counts start at zero.  */
   3256  1.1  christos       tpoint->hit_count = 0;
   3257  1.1  christos       tpoint->traceframe_usage = 0;
   3258  1.1  christos 
   3259  1.1  christos       if (tpoint->type == trap_tracepoint)
   3260  1.1  christos 	{
   3261  1.1  christos 	  /* Tracepoints are installed as memory breakpoints.  Just go
   3262  1.1  christos 	     ahead and install the trap.  The breakpoints module
   3263  1.1  christos 	     handles duplicated breakpoints, and the memory read
   3264  1.1  christos 	     routine handles un-patching traps from memory reads.  */
   3265  1.1  christos 	  tpoint->handle = set_breakpoint_at (tpoint->address,
   3266  1.1  christos 					      tracepoint_handler);
   3267  1.1  christos 	}
   3268  1.1  christos       else if (tpoint->type == fast_tracepoint
   3269  1.1  christos 	       || tpoint->type == static_tracepoint)
   3270  1.1  christos 	{
   3271  1.1  christos 	  if (maybe_write_ipa_not_loaded (packet))
   3272  1.1  christos 	    {
   3273  1.1  christos 	      trace_debug ("Requested a %s tracepoint, but fast "
   3274  1.1  christos 			   "tracepoints aren't supported.",
   3275  1.1  christos 			   tpoint->type == static_tracepoint
   3276  1.1  christos 			   ? "static" : "fast");
   3277  1.1  christos 	      break;
   3278  1.1  christos 	    }
   3279  1.1  christos 
   3280  1.1  christos 	  if (tpoint->type == fast_tracepoint)
   3281  1.1  christos 	    {
   3282  1.1  christos 	      int use_agent_p
   3283  1.1  christos 		= use_agent && agent_capability_check (AGENT_CAPA_FAST_TRACE);
   3284  1.1  christos 
   3285  1.1  christos 	      if (prev_ftpoint != NULL
   3286  1.1  christos 		  && prev_ftpoint->address == tpoint->address)
   3287  1.1  christos 		{
   3288  1.1  christos 		  if (use_agent_p)
   3289  1.1  christos 		    tracepoint_send_agent (tpoint);
   3290  1.1  christos 		  else
   3291  1.1  christos 		    download_tracepoint_1 (tpoint);
   3292  1.1  christos 
   3293  1.1  christos 		  clone_fast_tracepoint (tpoint, prev_ftpoint);
   3294  1.1  christos 		}
   3295  1.1  christos 	      else
   3296  1.1  christos 		{
   3297  1.1  christos 		  /* Tracepoint is installed successfully?  */
   3298  1.1  christos 		  int installed = 0;
   3299  1.1  christos 
   3300  1.1  christos 		  /* Download and install fast tracepoint by agent.  */
   3301  1.1  christos 		  if (use_agent_p)
   3302  1.1  christos 		    installed = !tracepoint_send_agent (tpoint);
   3303  1.1  christos 		  else
   3304  1.1  christos 		    {
   3305  1.1  christos 		      download_tracepoint_1 (tpoint);
   3306  1.1  christos 		      installed = !install_fast_tracepoint (tpoint, packet);
   3307  1.1  christos 		    }
   3308  1.1  christos 
   3309  1.1  christos 		  if (installed)
   3310  1.1  christos 		    prev_ftpoint = tpoint;
   3311  1.1  christos 		}
   3312  1.1  christos 	    }
   3313  1.1  christos 	  else
   3314  1.1  christos 	    {
   3315  1.1  christos 	      if (!in_process_agent_supports_ust ())
   3316  1.1  christos 		{
   3317  1.1  christos 		  trace_debug ("Requested a static tracepoint, but static "
   3318  1.1  christos 			       "tracepoints are not supported.");
   3319  1.1  christos 		  break;
   3320  1.1  christos 		}
   3321  1.1  christos 
   3322  1.1  christos 	      download_tracepoint_1 (tpoint);
   3323  1.1  christos 	      /* Can only probe a given marker once.  */
   3324  1.1  christos 	      if (prev_stpoint != NULL
   3325  1.1  christos 		  && prev_stpoint->address == tpoint->address)
   3326  1.1  christos 		tpoint->handle = (void *) -1;
   3327  1.1  christos 	      else
   3328  1.1  christos 		{
   3329  1.1  christos 		  if (probe_marker_at (tpoint->address, packet) == 0)
   3330  1.1  christos 		    {
   3331  1.1  christos 		      tpoint->handle = (void *) -1;
   3332  1.1  christos 
   3333  1.1  christos 		      /* So that we can handle multiple static tracepoints
   3334  1.1  christos 			 at the same address easily.  */
   3335  1.1  christos 		      prev_stpoint = tpoint;
   3336  1.1  christos 		    }
   3337  1.1  christos 		}
   3338  1.1  christos 	    }
   3339  1.1  christos 
   3340  1.1  christos 	  prev_tpptr = tpptr;
   3341  1.1  christos 	  tpptr = tpoint->obj_addr_on_target;
   3342  1.1  christos 
   3343  1.1  christos 	  if (tpoint == tracepoints)
   3344  1.1  christos 	    /* First object in list, set the head pointer in the
   3345  1.1  christos 	       inferior.  */
   3346  1.1  christos 	    write_inferior_data_pointer (ipa_sym_addrs.addr_tracepoints, tpptr);
   3347  1.1  christos 	  else
   3348  1.1  christos 	    write_inferior_data_pointer (prev_tpptr
   3349  1.1  christos 					 + offsetof (struct tracepoint, next),
   3350  1.1  christos 					 tpptr);
   3351  1.1  christos 	}
   3352  1.1  christos 
   3353  1.1  christos       /* Any failure in the inner loop is sufficient cause to give
   3354  1.1  christos 	 up.  */
   3355  1.1  christos       if (tpoint->handle == NULL)
   3356  1.1  christos 	break;
   3357  1.1  christos     }
   3358  1.1  christos 
   3359  1.1  christos   /* Any error in tracepoint insertion is unacceptable; better to
   3360  1.1  christos      address the problem now, than end up with a useless or misleading
   3361  1.1  christos      trace run.  */
   3362  1.1  christos   if (tpoint != NULL)
   3363  1.1  christos     {
   3364  1.1  christos       clear_installed_tracepoints ();
   3365  1.1  christos       if (*packet == '\0')
   3366  1.1  christos 	write_enn (packet);
   3367  1.1  christos       target_unpause_all (true);
   3368  1.1  christos       return;
   3369  1.1  christos     }
   3370  1.1  christos 
   3371  1.1  christos   stopping_tracepoint = NULL;
   3372  1.1  christos   trace_buffer_is_full = 0;
   3373  1.1  christos   expr_eval_result = expr_eval_no_error;
   3374  1.1  christos   error_tracepoint = NULL;
   3375  1.1  christos   tracing_start_time = get_timestamp ();
   3376  1.1  christos 
   3377  1.1  christos   /* Tracing is now active, hits will now start being logged.  */
   3378  1.1  christos   tracing = 1;
   3379  1.1  christos 
   3380  1.1  christos   if (agent_loaded_p ())
   3381  1.1  christos     {
   3382  1.1  christos       if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 1))
   3383  1.1  christos 	{
   3384  1.1  christos 	  internal_error (__FILE__, __LINE__,
   3385  1.1  christos 			  "Error setting tracing variable in lib");
   3386  1.1  christos 	}
   3387  1.1  christos 
   3388  1.1  christos       if (write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
   3389  1.1  christos 				       0))
   3390  1.1  christos 	{
   3391  1.1  christos 	  internal_error (__FILE__, __LINE__,
   3392  1.1  christos 			  "Error clearing stopping_tracepoint variable"
   3393  1.1  christos 			  " in lib");
   3394  1.1  christos 	}
   3395  1.1  christos 
   3396  1.1  christos       if (write_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full, 0))
   3397  1.1  christos 	{
   3398  1.1  christos 	  internal_error (__FILE__, __LINE__,
   3399  1.1  christos 			  "Error clearing trace_buffer_is_full variable"
   3400  1.1  christos 			  " in lib");
   3401  1.1  christos 	}
   3402  1.1  christos 
   3403  1.1  christos       stop_tracing_bkpt = set_breakpoint_at (ipa_sym_addrs.addr_stop_tracing,
   3404  1.1  christos 					     stop_tracing_handler);
   3405  1.1  christos       if (stop_tracing_bkpt == NULL)
   3406  1.1  christos 	error ("Error setting stop_tracing breakpoint");
   3407  1.1  christos 
   3408  1.1  christos       flush_trace_buffer_bkpt
   3409  1.1  christos 	= set_breakpoint_at (ipa_sym_addrs.addr_flush_trace_buffer,
   3410  1.1  christos 			     flush_trace_buffer_handler);
   3411  1.1  christos       if (flush_trace_buffer_bkpt == NULL)
   3412  1.1  christos 	error ("Error setting flush_trace_buffer breakpoint");
   3413  1.1  christos     }
   3414  1.1  christos 
   3415  1.1  christos   target_unpause_all (true);
   3416  1.1  christos 
   3417  1.1  christos   write_ok (packet);
   3418  1.1  christos }
   3419  1.1  christos 
   3420  1.1  christos /* End a tracing run, filling in a stop reason to report back to GDB,
   3421  1.1  christos    and removing the tracepoints from the code.  */
   3422  1.1  christos 
   3423  1.1  christos void
   3424  1.1  christos stop_tracing (void)
   3425  1.1  christos {
   3426  1.1  christos   if (!tracing)
   3427  1.1  christos     {
   3428  1.1  christos       trace_debug ("Tracing is already off, ignoring");
   3429  1.1  christos       return;
   3430  1.1  christos     }
   3431  1.1  christos 
   3432  1.1  christos   trace_debug ("Stopping the trace");
   3433  1.1  christos 
   3434  1.1  christos   /* Pause all threads before removing fast jumps from memory,
   3435  1.1  christos      breakpoints, and touching IPA state variables (inferior memory).
   3436  1.1  christos      Some thread may hit the internal tracing breakpoints, or be
   3437  1.1  christos      collecting this moment, but that's ok, we don't release the
   3438  1.1  christos      tpoint object's memory or the jump pads here (we only do that
   3439  1.1  christos      when we're sure we can move all threads out of the jump pads).
   3440  1.1  christos      We can't now, since we may be getting here due to the inferior
   3441  1.1  christos      agent calling us.  */
   3442  1.1  christos   target_pause_all (true);
   3443  1.1  christos 
   3444  1.1  christos   /* Stop logging. Tracepoints can still be hit, but they will not be
   3445  1.1  christos      recorded.  */
   3446  1.1  christos   tracing = 0;
   3447  1.1  christos   if (agent_loaded_p ())
   3448  1.1  christos     {
   3449  1.1  christos       if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 0))
   3450  1.1  christos 	{
   3451  1.1  christos 	  internal_error (__FILE__, __LINE__,
   3452  1.1  christos 			  "Error clearing tracing variable in lib");
   3453  1.1  christos 	}
   3454  1.1  christos     }
   3455  1.1  christos 
   3456  1.1  christos   tracing_stop_time = get_timestamp ();
   3457  1.1  christos   tracing_stop_reason = "t???";
   3458  1.1  christos   tracing_stop_tpnum = 0;
   3459  1.1  christos   if (stopping_tracepoint)
   3460  1.1  christos     {
   3461  1.1  christos       trace_debug ("Stopping the trace because "
   3462  1.1  christos 		   "tracepoint %d was hit %" PRIu64 " times",
   3463  1.1  christos 		   stopping_tracepoint->number,
   3464  1.1  christos 		   stopping_tracepoint->pass_count);
   3465  1.1  christos       tracing_stop_reason = "tpasscount";
   3466  1.1  christos       tracing_stop_tpnum = stopping_tracepoint->number;
   3467  1.1  christos     }
   3468  1.1  christos   else if (trace_buffer_is_full)
   3469  1.1  christos     {
   3470  1.1  christos       trace_debug ("Stopping the trace because the trace buffer is full");
   3471  1.1  christos       tracing_stop_reason = "tfull";
   3472  1.1  christos     }
   3473  1.1  christos   else if (expr_eval_result != expr_eval_no_error)
   3474  1.1  christos     {
   3475  1.1  christos       trace_debug ("Stopping the trace because of an expression eval error");
   3476  1.1  christos       tracing_stop_reason = eval_result_names[expr_eval_result];
   3477  1.1  christos       tracing_stop_tpnum = error_tracepoint->number;
   3478  1.1  christos     }
   3479  1.1  christos #ifndef IN_PROCESS_AGENT
   3480  1.1  christos   else if (!gdb_connected ())
   3481  1.1  christos     {
   3482  1.1  christos       trace_debug ("Stopping the trace because GDB disconnected");
   3483  1.1  christos       tracing_stop_reason = "tdisconnected";
   3484  1.1  christos     }
   3485  1.1  christos #endif
   3486  1.1  christos   else
   3487  1.1  christos     {
   3488  1.1  christos       trace_debug ("Stopping the trace because of a tstop command");
   3489  1.1  christos       tracing_stop_reason = "tstop";
   3490  1.1  christos     }
   3491  1.1  christos 
   3492  1.1  christos   stopping_tracepoint = NULL;
   3493  1.1  christos   error_tracepoint = NULL;
   3494  1.1  christos 
   3495  1.1  christos   /* Clear out the tracepoints.  */
   3496  1.1  christos   clear_installed_tracepoints ();
   3497  1.1  christos 
   3498  1.1  christos   if (agent_loaded_p ())
   3499  1.1  christos     {
   3500  1.1  christos       /* Pull in fast tracepoint trace frames from the inferior lib
   3501  1.1  christos 	 buffer into our buffer, even if our buffer is already full,
   3502  1.1  christos 	 because we want to present the full number of created frames
   3503  1.1  christos 	 in addition to what fit in the trace buffer.  */
   3504  1.1  christos       upload_fast_traceframes ();
   3505  1.1  christos     }
   3506  1.1  christos 
   3507  1.1  christos   if (stop_tracing_bkpt != NULL)
   3508  1.1  christos     {
   3509  1.1  christos       delete_breakpoint (stop_tracing_bkpt);
   3510  1.1  christos       stop_tracing_bkpt = NULL;
   3511  1.1  christos     }
   3512  1.1  christos 
   3513  1.1  christos   if (flush_trace_buffer_bkpt != NULL)
   3514  1.1  christos     {
   3515  1.1  christos       delete_breakpoint (flush_trace_buffer_bkpt);
   3516  1.1  christos       flush_trace_buffer_bkpt = NULL;
   3517  1.1  christos     }
   3518  1.1  christos 
   3519  1.1  christos   target_unpause_all (true);
   3520  1.1  christos }
   3521  1.1  christos 
   3522  1.1  christos static int
   3523  1.1  christos stop_tracing_handler (CORE_ADDR addr)
   3524  1.1  christos {
   3525  1.1  christos   trace_debug ("lib hit stop_tracing");
   3526  1.1  christos 
   3527  1.1  christos   /* Don't actually handle it here.  When we stop tracing we remove
   3528  1.1  christos      breakpoints from the inferior, and that is not allowed in a
   3529  1.1  christos      breakpoint handler (as the caller is walking the breakpoint
   3530  1.1  christos      list).  */
   3531  1.1  christos   return 0;
   3532  1.1  christos }
   3533  1.1  christos 
   3534  1.1  christos static int
   3535  1.1  christos flush_trace_buffer_handler (CORE_ADDR addr)
   3536  1.1  christos {
   3537  1.1  christos   trace_debug ("lib hit flush_trace_buffer");
   3538  1.1  christos   return 0;
   3539  1.1  christos }
   3540  1.1  christos 
   3541  1.1  christos static void
   3542  1.1  christos cmd_qtstop (char *packet)
   3543  1.1  christos {
   3544  1.1  christos   stop_tracing ();
   3545  1.1  christos   write_ok (packet);
   3546  1.1  christos }
   3547  1.1  christos 
   3548  1.1  christos static void
   3549  1.1  christos cmd_qtdisconnected (char *own_buf)
   3550  1.1  christos {
   3551  1.1  christos   ULONGEST setting;
   3552  1.1  christos   char *packet = own_buf;
   3553  1.1  christos 
   3554  1.1  christos   packet += strlen ("QTDisconnected:");
   3555  1.1  christos 
   3556  1.1  christos   unpack_varlen_hex (packet, &setting);
   3557  1.1  christos 
   3558  1.1  christos   write_ok (own_buf);
   3559  1.1  christos 
   3560  1.1  christos   disconnected_tracing = setting;
   3561  1.1  christos }
   3562  1.1  christos 
   3563  1.1  christos static void
   3564  1.1  christos cmd_qtframe (char *own_buf)
   3565  1.1  christos {
   3566  1.1  christos   client_state &cs = get_client_state ();
   3567  1.1  christos   ULONGEST frame, pc, lo, hi, num;
   3568  1.1  christos   int tfnum, tpnum;
   3569  1.1  christos   struct traceframe *tframe;
   3570  1.1  christos   const char *packet = own_buf;
   3571  1.1  christos 
   3572  1.1  christos   packet += strlen ("QTFrame:");
   3573  1.1  christos 
   3574  1.1  christos   if (startswith (packet, "pc:"))
   3575  1.1  christos     {
   3576  1.1  christos       packet += strlen ("pc:");
   3577  1.1  christos       unpack_varlen_hex (packet, &pc);
   3578  1.1  christos       trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc));
   3579  1.1  christos       tframe = find_next_traceframe_in_range (pc, pc, 1, &tfnum);
   3580  1.1  christos     }
   3581  1.1  christos   else if (startswith (packet, "range:"))
   3582  1.1  christos     {
   3583  1.1  christos       packet += strlen ("range:");
   3584  1.1  christos       packet = unpack_varlen_hex (packet, &lo);
   3585  1.1  christos       ++packet;
   3586  1.1  christos       unpack_varlen_hex (packet, &hi);
   3587  1.1  christos       trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
   3588  1.1  christos 		   paddress (lo), paddress (hi));
   3589  1.1  christos       tframe = find_next_traceframe_in_range (lo, hi, 1, &tfnum);
   3590  1.1  christos     }
   3591  1.1  christos   else if (startswith (packet, "outside:"))
   3592  1.1  christos     {
   3593  1.1  christos       packet += strlen ("outside:");
   3594  1.1  christos       packet = unpack_varlen_hex (packet, &lo);
   3595  1.1  christos       ++packet;
   3596  1.1  christos       unpack_varlen_hex (packet, &hi);
   3597  1.1  christos       trace_debug ("Want to find next traceframe "
   3598  1.1  christos 		   "outside the range 0x%s to 0x%s",
   3599  1.1  christos 		   paddress (lo), paddress (hi));
   3600  1.1  christos       tframe = find_next_traceframe_in_range (lo, hi, 0, &tfnum);
   3601  1.1  christos     }
   3602  1.1  christos   else if (startswith (packet, "tdp:"))
   3603  1.1  christos     {
   3604  1.1  christos       packet += strlen ("tdp:");
   3605  1.1  christos       unpack_varlen_hex (packet, &num);
   3606  1.1  christos       tpnum = (int) num;
   3607  1.1  christos       trace_debug ("Want to find next traceframe for tracepoint %d", tpnum);
   3608  1.1  christos       tframe = find_next_traceframe_by_tracepoint (tpnum, &tfnum);
   3609  1.1  christos     }
   3610  1.1  christos   else
   3611  1.1  christos     {
   3612  1.1  christos       unpack_varlen_hex (packet, &frame);
   3613  1.1  christos       tfnum = (int) frame;
   3614  1.1  christos       if (tfnum == -1)
   3615  1.1  christos 	{
   3616  1.1  christos 	  trace_debug ("Want to stop looking at traceframes");
   3617  1.1  christos 	  cs.current_traceframe = -1;
   3618  1.1  christos 	  write_ok (own_buf);
   3619  1.1  christos 	  return;
   3620  1.1  christos 	}
   3621  1.1  christos       trace_debug ("Want to look at traceframe %d", tfnum);
   3622  1.1  christos       tframe = find_traceframe (tfnum);
   3623  1.1  christos     }
   3624  1.1  christos 
   3625  1.1  christos   if (tframe)
   3626  1.1  christos     {
   3627  1.1  christos       cs.current_traceframe = tfnum;
   3628  1.1  christos       sprintf (own_buf, "F%xT%x", tfnum, tframe->tpnum);
   3629  1.1  christos     }
   3630  1.1  christos   else
   3631  1.1  christos     sprintf (own_buf, "F-1");
   3632  1.1  christos }
   3633  1.1  christos 
   3634  1.1  christos static void
   3635  1.1  christos cmd_qtstatus (char *packet)
   3636  1.1  christos {
   3637  1.1  christos   char *stop_reason_rsp = NULL;
   3638  1.1  christos   char *buf1, *buf2, *buf3;
   3639  1.1  christos   const char *str;
   3640  1.1  christos   int slen;
   3641  1.1  christos 
   3642  1.1  christos   /* Translate the plain text of the notes back into hex for
   3643  1.1  christos      transmission.  */
   3644  1.1  christos 
   3645  1.1  christos   str = (tracing_user_name ? tracing_user_name : "");
   3646  1.1  christos   slen = strlen (str);
   3647  1.1  christos   buf1 = (char *) alloca (slen * 2 + 1);
   3648  1.1  christos   bin2hex ((gdb_byte *) str, buf1, slen);
   3649  1.1  christos 
   3650  1.1  christos   str = (tracing_notes ? tracing_notes : "");
   3651  1.1  christos   slen = strlen (str);
   3652  1.1  christos   buf2 = (char *) alloca (slen * 2 + 1);
   3653  1.1  christos   bin2hex ((gdb_byte *) str, buf2, slen);
   3654  1.1  christos 
   3655  1.1  christos   str = (tracing_stop_note ? tracing_stop_note : "");
   3656  1.1  christos   slen = strlen (str);
   3657  1.1  christos   buf3 = (char *) alloca (slen * 2 + 1);
   3658  1.1  christos   bin2hex ((gdb_byte *) str, buf3, slen);
   3659  1.1  christos 
   3660  1.1  christos   trace_debug ("Returning trace status as %d, stop reason %s",
   3661  1.1  christos 	       tracing, tracing_stop_reason);
   3662  1.1  christos 
   3663  1.1  christos   if (agent_loaded_p ())
   3664  1.1  christos     {
   3665  1.1  christos       target_pause_all (true);
   3666  1.1  christos 
   3667  1.1  christos       upload_fast_traceframes ();
   3668  1.1  christos 
   3669  1.1  christos       target_unpause_all (true);
   3670  1.1  christos    }
   3671  1.1  christos 
   3672  1.1  christos   stop_reason_rsp = (char *) tracing_stop_reason;
   3673  1.1  christos 
   3674  1.1  christos   /* The user visible error string in terror needs to be hex encoded.
   3675  1.1  christos      We leave it as plain string in `tracing_stop_reason' to ease
   3676  1.1  christos      debugging.  */
   3677  1.1  christos   if (startswith (stop_reason_rsp, "terror:"))
   3678  1.1  christos     {
   3679  1.1  christos       const char *result_name;
   3680  1.1  christos       int hexstr_len;
   3681  1.1  christos       char *p;
   3682  1.1  christos 
   3683  1.1  christos       result_name = stop_reason_rsp + strlen ("terror:");
   3684  1.1  christos       hexstr_len = strlen (result_name) * 2;
   3685  1.1  christos       p = stop_reason_rsp
   3686  1.1  christos 	= (char *) alloca (strlen ("terror:") + hexstr_len + 1);
   3687  1.1  christos       strcpy (p, "terror:");
   3688  1.1  christos       p += strlen (p);
   3689  1.1  christos       bin2hex ((gdb_byte *) result_name, p, strlen (result_name));
   3690  1.1  christos     }
   3691  1.1  christos 
   3692  1.1  christos   /* If this was a forced stop, include any stop note that was supplied.  */
   3693  1.1  christos   if (strcmp (stop_reason_rsp, "tstop") == 0)
   3694  1.1  christos     {
   3695  1.1  christos       stop_reason_rsp = (char *) alloca (strlen ("tstop:") + strlen (buf3) + 1);
   3696  1.1  christos       strcpy (stop_reason_rsp, "tstop:");
   3697  1.1  christos       strcat (stop_reason_rsp, buf3);
   3698  1.1  christos     }
   3699  1.1  christos 
   3700  1.1  christos   sprintf (packet,
   3701  1.1  christos 	   "T%d;"
   3702  1.1  christos 	   "%s:%x;"
   3703  1.1  christos 	   "tframes:%x;tcreated:%x;"
   3704  1.1  christos 	   "tfree:%x;tsize:%s;"
   3705  1.1  christos 	   "circular:%d;"
   3706  1.1  christos 	   "disconn:%d;"
   3707  1.1  christos 	   "starttime:%s;stoptime:%s;"
   3708  1.1  christos 	   "username:%s;notes:%s:",
   3709  1.1  christos 	   tracing ? 1 : 0,
   3710  1.1  christos 	   stop_reason_rsp, tracing_stop_tpnum,
   3711  1.1  christos 	   traceframe_count, traceframes_created,
   3712  1.1  christos 	   free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0),
   3713  1.1  christos 	   circular_trace_buffer,
   3714  1.1  christos 	   disconnected_tracing,
   3715  1.1  christos 	   phex_nz (tracing_start_time, sizeof (tracing_start_time)),
   3716  1.1  christos 	   phex_nz (tracing_stop_time, sizeof (tracing_stop_time)),
   3717  1.1  christos 	   buf1, buf2);
   3718  1.1  christos }
   3719  1.1  christos 
   3720  1.1  christos static void
   3721  1.1  christos cmd_qtp (char *own_buf)
   3722  1.1  christos {
   3723  1.1  christos   ULONGEST num, addr;
   3724  1.1  christos   struct tracepoint *tpoint;
   3725  1.1  christos   const char *packet = own_buf;
   3726  1.1  christos 
   3727  1.1  christos   packet += strlen ("qTP:");
   3728  1.1  christos 
   3729  1.1  christos   packet = unpack_varlen_hex (packet, &num);
   3730  1.1  christos   ++packet; /* skip a colon */
   3731  1.1  christos   packet = unpack_varlen_hex (packet, &addr);
   3732  1.1  christos 
   3733  1.1  christos   /* See if we already have this tracepoint.  */
   3734  1.1  christos   tpoint = find_tracepoint (num, addr);
   3735  1.1  christos 
   3736  1.1  christos   if (!tpoint)
   3737  1.1  christos     {
   3738  1.1  christos       trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
   3739  1.1  christos 		   (int) num, paddress (addr));
   3740  1.1  christos       write_enn (own_buf);
   3741  1.1  christos       return;
   3742  1.1  christos     }
   3743  1.1  christos 
   3744  1.1  christos   sprintf (own_buf, "V%" PRIu64 ":%" PRIu64 "", tpoint->hit_count,
   3745  1.1  christos 	   tpoint->traceframe_usage);
   3746  1.1  christos }
   3747  1.1  christos 
   3748  1.1  christos /* State variables to help return all the tracepoint bits.  */
   3749  1.1  christos static struct tracepoint *cur_tpoint;
   3750  1.1  christos static unsigned int cur_action;
   3751  1.1  christos static unsigned int cur_step_action;
   3752  1.1  christos static struct source_string *cur_source_string;
   3753  1.1  christos static struct trace_state_variable *cur_tsv;
   3754  1.1  christos 
   3755  1.1  christos /* Compose a response that is an imitation of the syntax by which the
   3756  1.1  christos    tracepoint was originally downloaded.  */
   3757  1.1  christos 
   3758  1.1  christos static void
   3759  1.1  christos response_tracepoint (char *packet, struct tracepoint *tpoint)
   3760  1.1  christos {
   3761  1.1  christos   char *buf;
   3762  1.1  christos 
   3763  1.1  christos   sprintf (packet, "T%x:%s:%c:%" PRIx64 ":%" PRIx64, tpoint->number,
   3764  1.1  christos 	   paddress (tpoint->address),
   3765  1.1  christos 	   (tpoint->enabled ? 'E' : 'D'), tpoint->step_count,
   3766  1.1  christos 	   tpoint->pass_count);
   3767  1.1  christos   if (tpoint->type == fast_tracepoint)
   3768  1.1  christos     sprintf (packet + strlen (packet), ":F%x", tpoint->orig_size);
   3769  1.1  christos   else if (tpoint->type == static_tracepoint)
   3770  1.1  christos     sprintf (packet + strlen (packet), ":S");
   3771  1.1  christos 
   3772  1.1  christos   if (tpoint->cond)
   3773  1.1  christos     {
   3774  1.1  christos       buf = gdb_unparse_agent_expr (tpoint->cond);
   3775  1.1  christos       sprintf (packet + strlen (packet), ":X%x,%s",
   3776  1.1  christos 	       tpoint->cond->length, buf);
   3777  1.1  christos       free (buf);
   3778  1.1  christos     }
   3779  1.1  christos }
   3780  1.1  christos 
   3781  1.1  christos /* Compose a response that is an imitation of the syntax by which the
   3782  1.1  christos    tracepoint action was originally downloaded (with the difference
   3783  1.1  christos    that due to the way we store the actions, this will output a packet
   3784  1.1  christos    per action, while GDB could have combined more than one action
   3785  1.1  christos    per-packet.  */
   3786  1.1  christos 
   3787  1.1  christos static void
   3788  1.1  christos response_action (char *packet, struct tracepoint *tpoint,
   3789  1.1  christos 		 char *taction, int step)
   3790  1.1  christos {
   3791  1.1  christos   sprintf (packet, "%c%x:%s:%s",
   3792  1.1  christos 	   (step ? 'S' : 'A'), tpoint->number, paddress (tpoint->address),
   3793  1.1  christos 	   taction);
   3794  1.1  christos }
   3795  1.1  christos 
   3796  1.1  christos /* Compose a response that is an imitation of the syntax by which the
   3797  1.1  christos    tracepoint source piece was originally downloaded.  */
   3798  1.1  christos 
   3799  1.1  christos static void
   3800  1.1  christos response_source (char *packet,
   3801  1.1  christos 		 struct tracepoint *tpoint, struct source_string *src)
   3802  1.1  christos {
   3803  1.1  christos   char *buf;
   3804  1.1  christos   int len;
   3805  1.1  christos 
   3806  1.1  christos   len = strlen (src->str);
   3807  1.1  christos   buf = (char *) alloca (len * 2 + 1);
   3808  1.1  christos   bin2hex ((gdb_byte *) src->str, buf, len);
   3809  1.1  christos 
   3810  1.1  christos   sprintf (packet, "Z%x:%s:%s:%x:%x:%s",
   3811  1.1  christos 	   tpoint->number, paddress (tpoint->address),
   3812  1.1  christos 	   src->type, 0, len, buf);
   3813  1.1  christos }
   3814  1.1  christos 
   3815  1.1  christos /* Return the first piece of tracepoint definition, and initialize the
   3816  1.1  christos    state machine that will iterate through all the tracepoint
   3817  1.1  christos    bits.  */
   3818  1.1  christos 
   3819  1.1  christos static void
   3820  1.1  christos cmd_qtfp (char *packet)
   3821  1.1  christos {
   3822  1.1  christos   trace_debug ("Returning first tracepoint definition piece");
   3823  1.1  christos 
   3824  1.1  christos   cur_tpoint = tracepoints;
   3825  1.1  christos   cur_action = cur_step_action = 0;
   3826  1.1  christos   cur_source_string = NULL;
   3827  1.1  christos 
   3828  1.1  christos   if (cur_tpoint)
   3829  1.1  christos     response_tracepoint (packet, cur_tpoint);
   3830  1.1  christos   else
   3831  1.1  christos     strcpy (packet, "l");
   3832  1.1  christos }
   3833  1.1  christos 
   3834  1.1  christos /* Return additional pieces of tracepoint definition.  Each action and
   3835  1.1  christos    stepping action must go into its own packet, because of packet size
   3836  1.1  christos    limits, and so we use state variables to deliver one piece at a
   3837  1.1  christos    time.  */
   3838  1.1  christos 
   3839  1.1  christos static void
   3840  1.1  christos cmd_qtsp (char *packet)
   3841  1.1  christos {
   3842  1.1  christos   trace_debug ("Returning subsequent tracepoint definition piece");
   3843  1.1  christos 
   3844  1.1  christos   if (!cur_tpoint)
   3845  1.1  christos     {
   3846  1.1  christos       /* This case would normally never occur, but be prepared for
   3847  1.1  christos 	 GDB misbehavior.  */
   3848  1.1  christos       strcpy (packet, "l");
   3849  1.1  christos     }
   3850  1.1  christos   else if (cur_action < cur_tpoint->numactions)
   3851  1.1  christos     {
   3852  1.1  christos       response_action (packet, cur_tpoint,
   3853  1.1  christos 		       cur_tpoint->actions_str[cur_action], 0);
   3854  1.1  christos       ++cur_action;
   3855  1.1  christos     }
   3856  1.1  christos   else if (cur_step_action < cur_tpoint->num_step_actions)
   3857  1.1  christos     {
   3858  1.1  christos       response_action (packet, cur_tpoint,
   3859  1.1  christos 		       cur_tpoint->step_actions_str[cur_step_action], 1);
   3860  1.1  christos       ++cur_step_action;
   3861  1.1  christos     }
   3862  1.1  christos   else if ((cur_source_string
   3863  1.1  christos 	    ? cur_source_string->next
   3864  1.1  christos 	    : cur_tpoint->source_strings))
   3865  1.1  christos     {
   3866  1.1  christos       if (cur_source_string)
   3867  1.1  christos 	cur_source_string = cur_source_string->next;
   3868  1.1  christos       else
   3869  1.1  christos 	cur_source_string = cur_tpoint->source_strings;
   3870  1.1  christos       response_source (packet, cur_tpoint, cur_source_string);
   3871  1.1  christos     }
   3872  1.1  christos   else
   3873  1.1  christos     {
   3874  1.1  christos       cur_tpoint = cur_tpoint->next;
   3875  1.1  christos       cur_action = cur_step_action = 0;
   3876  1.1  christos       cur_source_string = NULL;
   3877  1.1  christos       if (cur_tpoint)
   3878  1.1  christos 	response_tracepoint (packet, cur_tpoint);
   3879  1.1  christos       else
   3880  1.1  christos 	strcpy (packet, "l");
   3881  1.1  christos     }
   3882  1.1  christos }
   3883  1.1  christos 
   3884  1.1  christos /* Compose a response that is an imitation of the syntax by which the
   3885  1.1  christos    trace state variable was originally downloaded.  */
   3886  1.1  christos 
   3887  1.1  christos static void
   3888  1.1  christos response_tsv (char *packet, struct trace_state_variable *tsv)
   3889  1.1  christos {
   3890  1.1  christos   char *buf = (char *) "";
   3891  1.1  christos   int namelen;
   3892  1.1  christos 
   3893  1.1  christos   if (tsv->name)
   3894  1.1  christos     {
   3895  1.1  christos       namelen = strlen (tsv->name);
   3896  1.1  christos       buf = (char *) alloca (namelen * 2 + 1);
   3897  1.1  christos       bin2hex ((gdb_byte *) tsv->name, buf, namelen);
   3898  1.1  christos     }
   3899  1.1  christos 
   3900  1.1  christos   sprintf (packet, "%x:%s:%x:%s", tsv->number, phex_nz (tsv->initial_value, 0),
   3901  1.1  christos 	   tsv->getter ? 1 : 0, buf);
   3902  1.1  christos }
   3903  1.1  christos 
   3904  1.1  christos /* Return the first trace state variable definition, and initialize
   3905  1.1  christos    the state machine that will iterate through all the tsv bits.  */
   3906  1.1  christos 
   3907  1.1  christos static void
   3908  1.1  christos cmd_qtfv (char *packet)
   3909  1.1  christos {
   3910  1.1  christos   trace_debug ("Returning first trace state variable definition");
   3911  1.1  christos 
   3912  1.1  christos   cur_tsv = trace_state_variables;
   3913  1.1  christos 
   3914  1.1  christos   if (cur_tsv)
   3915  1.1  christos     response_tsv (packet, cur_tsv);
   3916  1.1  christos   else
   3917  1.1  christos     strcpy (packet, "l");
   3918  1.1  christos }
   3919  1.1  christos 
   3920  1.1  christos /* Return additional trace state variable definitions. */
   3921  1.1  christos 
   3922  1.1  christos static void
   3923  1.1  christos cmd_qtsv (char *packet)
   3924  1.1  christos {
   3925  1.1  christos   trace_debug ("Returning additional trace state variable definition");
   3926  1.1  christos 
   3927  1.1  christos   if (cur_tsv)
   3928  1.1  christos     {
   3929  1.1  christos       cur_tsv = cur_tsv->next;
   3930  1.1  christos       if (cur_tsv)
   3931  1.1  christos 	response_tsv (packet, cur_tsv);
   3932  1.1  christos       else
   3933  1.1  christos 	strcpy (packet, "l");
   3934  1.1  christos     }
   3935  1.1  christos   else
   3936  1.1  christos     strcpy (packet, "l");
   3937  1.1  christos }
   3938  1.1  christos 
   3939  1.1  christos /* Return the first static tracepoint marker, and initialize the state
   3940  1.1  christos    machine that will iterate through all the static tracepoints
   3941  1.1  christos    markers.  */
   3942  1.1  christos 
   3943  1.1  christos static void
   3944  1.1  christos cmd_qtfstm (char *packet)
   3945  1.1  christos {
   3946  1.1  christos   if (!maybe_write_ipa_ust_not_loaded (packet))
   3947  1.1  christos     run_inferior_command (packet, strlen (packet) + 1);
   3948  1.1  christos }
   3949  1.1  christos 
   3950  1.1  christos /* Return additional static tracepoints markers.  */
   3951  1.1  christos 
   3952  1.1  christos static void
   3953  1.1  christos cmd_qtsstm (char *packet)
   3954  1.1  christos {
   3955  1.1  christos   if (!maybe_write_ipa_ust_not_loaded (packet))
   3956  1.1  christos     run_inferior_command (packet, strlen (packet) + 1);
   3957  1.1  christos }
   3958  1.1  christos 
   3959  1.1  christos /* Return the definition of the static tracepoint at a given address.
   3960  1.1  christos    Result packet is the same as qTsST's.  */
   3961  1.1  christos 
   3962  1.1  christos static void
   3963  1.1  christos cmd_qtstmat (char *packet)
   3964  1.1  christos {
   3965  1.1  christos   if (!maybe_write_ipa_ust_not_loaded (packet))
   3966  1.1  christos     run_inferior_command (packet, strlen (packet) + 1);
   3967  1.1  christos }
   3968  1.1  christos 
   3969  1.1  christos /* Sent the agent a command to close it.  */
   3970  1.1  christos 
   3971  1.1  christos void
   3972  1.1  christos gdb_agent_about_to_close (int pid)
   3973  1.1  christos {
   3974  1.1  christos   char buf[IPA_CMD_BUF_SIZE];
   3975  1.1  christos 
   3976  1.1  christos   if (!maybe_write_ipa_not_loaded (buf))
   3977  1.1  christos     {
   3978  1.1  christos       struct thread_info *saved_thread;
   3979  1.1  christos 
   3980  1.1  christos       saved_thread = current_thread;
   3981  1.1  christos 
   3982  1.1  christos       /* Find any thread which belongs to process PID.  */
   3983  1.1  christos       current_thread = find_any_thread_of_pid (pid);
   3984  1.1  christos 
   3985  1.1  christos       strcpy (buf, "close");
   3986  1.1  christos 
   3987  1.1  christos       run_inferior_command (buf, strlen (buf) + 1);
   3988  1.1  christos 
   3989  1.1  christos       current_thread = saved_thread;
   3990  1.1  christos     }
   3991  1.1  christos }
   3992  1.1  christos 
   3993  1.1  christos /* Return the minimum instruction size needed for fast tracepoints as a
   3994  1.1  christos    hexadecimal number.  */
   3995  1.1  christos 
   3996  1.1  christos static void
   3997  1.1  christos cmd_qtminftpilen (char *packet)
   3998  1.1  christos {
   3999  1.1  christos   if (current_thread == NULL)
   4000  1.1  christos     {
   4001  1.1  christos       /* Indicate that the minimum length is currently unknown.  */
   4002  1.1  christos       strcpy (packet, "0");
   4003  1.1  christos       return;
   4004  1.1  christos     }
   4005  1.1  christos 
   4006  1.1  christos   sprintf (packet, "%x", target_get_min_fast_tracepoint_insn_len ());
   4007  1.1  christos }
   4008  1.1  christos 
   4009  1.1  christos /* Respond to qTBuffer packet with a block of raw data from the trace
   4010  1.1  christos    buffer.  GDB may ask for a lot, but we are allowed to reply with
   4011  1.1  christos    only as much as will fit within packet limits or whatever.  */
   4012  1.1  christos 
   4013  1.1  christos static void
   4014  1.1  christos cmd_qtbuffer (char *own_buf)
   4015  1.1  christos {
   4016  1.1  christos   ULONGEST offset, num, tot;
   4017  1.1  christos   unsigned char *tbp;
   4018  1.1  christos   const char *packet = own_buf;
   4019  1.1  christos 
   4020  1.1  christos   packet += strlen ("qTBuffer:");
   4021  1.1  christos 
   4022  1.1  christos   packet = unpack_varlen_hex (packet, &offset);
   4023  1.1  christos   ++packet; /* skip a comma */
   4024  1.1  christos   unpack_varlen_hex (packet, &num);
   4025  1.1  christos 
   4026  1.1  christos   trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
   4027  1.1  christos 	       (int) num, phex_nz (offset, 0));
   4028  1.1  christos 
   4029  1.1  christos   tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
   4030  1.1  christos 
   4031  1.1  christos   /* If we're right at the end, reply specially that we're done.  */
   4032  1.1  christos   if (offset == tot)
   4033  1.1  christos     {
   4034  1.1  christos       strcpy (own_buf, "l");
   4035  1.1  christos       return;
   4036  1.1  christos     }
   4037  1.1  christos 
   4038  1.1  christos   /* Object to any other out-of-bounds request.  */
   4039  1.1  christos   if (offset > tot)
   4040  1.1  christos     {
   4041  1.1  christos       write_enn (own_buf);
   4042  1.1  christos       return;
   4043  1.1  christos     }
   4044  1.1  christos 
   4045  1.1  christos   /* Compute the pointer corresponding to the given offset, accounting
   4046  1.1  christos      for wraparound.  */
   4047  1.1  christos   tbp = trace_buffer_start + offset;
   4048  1.1  christos   if (tbp >= trace_buffer_wrap)
   4049  1.1  christos     tbp -= (trace_buffer_wrap - trace_buffer_lo);
   4050  1.1  christos 
   4051  1.1  christos   /* Trim to the remaining bytes if we're close to the end.  */
   4052  1.1  christos   if (num > tot - offset)
   4053  1.1  christos     num = tot - offset;
   4054  1.1  christos 
   4055  1.1  christos   /* Trim to available packet size.  */
   4056  1.1  christos   if (num >= (PBUFSIZ - 16) / 2 )
   4057  1.1  christos     num = (PBUFSIZ - 16) / 2;
   4058  1.1  christos 
   4059  1.1  christos   bin2hex (tbp, own_buf, num);
   4060  1.1  christos }
   4061  1.1  christos 
   4062  1.1  christos static void
   4063  1.1  christos cmd_bigqtbuffer_circular (char *own_buf)
   4064  1.1  christos {
   4065  1.1  christos   ULONGEST val;
   4066  1.1  christos   char *packet = own_buf;
   4067  1.1  christos 
   4068  1.1  christos   packet += strlen ("QTBuffer:circular:");
   4069  1.1  christos 
   4070  1.1  christos   unpack_varlen_hex (packet, &val);
   4071  1.1  christos   circular_trace_buffer = val;
   4072  1.1  christos   trace_debug ("Trace buffer is now %s",
   4073  1.1  christos 	       circular_trace_buffer ? "circular" : "linear");
   4074  1.1  christos   write_ok (own_buf);
   4075  1.1  christos }
   4076  1.1  christos 
   4077  1.1  christos static void
   4078  1.1  christos cmd_bigqtbuffer_size (char *own_buf)
   4079  1.1  christos {
   4080  1.1  christos   ULONGEST val;
   4081  1.1  christos   LONGEST sval;
   4082  1.1  christos   char *packet = own_buf;
   4083  1.1  christos 
   4084  1.1  christos   /* Can't change the size during a tracing run.  */
   4085  1.1  christos   if (tracing)
   4086  1.1  christos     {
   4087  1.1  christos       write_enn (own_buf);
   4088  1.1  christos       return;
   4089  1.1  christos     }
   4090  1.1  christos 
   4091  1.1  christos   packet += strlen ("QTBuffer:size:");
   4092  1.1  christos 
   4093  1.1  christos   /* -1 is sent as literal "-1".  */
   4094  1.1  christos   if (strcmp (packet, "-1") == 0)
   4095  1.1  christos     sval = DEFAULT_TRACE_BUFFER_SIZE;
   4096  1.1  christos   else
   4097  1.1  christos     {
   4098  1.1  christos       unpack_varlen_hex (packet, &val);
   4099  1.1  christos       sval = (LONGEST) val;
   4100  1.1  christos     }
   4101  1.1  christos 
   4102  1.1  christos   init_trace_buffer (sval);
   4103  1.1  christos   trace_debug ("Trace buffer is now %s bytes",
   4104  1.1  christos 	       plongest (trace_buffer_size));
   4105  1.1  christos   write_ok (own_buf);
   4106  1.1  christos }
   4107  1.1  christos 
   4108  1.1  christos static void
   4109  1.1  christos cmd_qtnotes (char *own_buf)
   4110  1.1  christos {
   4111  1.1  christos   size_t nbytes;
   4112  1.1  christos   char *saved, *user, *notes, *stopnote;
   4113  1.1  christos   char *packet = own_buf;
   4114  1.1  christos 
   4115  1.1  christos   packet += strlen ("QTNotes:");
   4116  1.1  christos 
   4117  1.1  christos   while (*packet)
   4118  1.1  christos     {
   4119  1.1  christos       if (startswith (packet, "user:"))
   4120  1.1  christos 	{
   4121  1.1  christos 	  packet += strlen ("user:");
   4122  1.1  christos 	  saved = packet;
   4123  1.1  christos 	  packet = strchr (packet, ';');
   4124  1.1  christos 	  nbytes = (packet - saved) / 2;
   4125  1.1  christos 	  user = (char *) xmalloc (nbytes + 1);
   4126  1.1  christos 	  nbytes = hex2bin (saved, (gdb_byte *) user, nbytes);
   4127  1.1  christos 	  user[nbytes] = '\0';
   4128  1.1  christos 	  ++packet; /* skip the semicolon */
   4129  1.1  christos 	  trace_debug ("User is '%s'", user);
   4130  1.1  christos 	  xfree (tracing_user_name);
   4131  1.1  christos 	  tracing_user_name = user;
   4132  1.1  christos 	}
   4133  1.1  christos       else if (startswith (packet, "notes:"))
   4134  1.1  christos 	{
   4135  1.1  christos 	  packet += strlen ("notes:");
   4136  1.1  christos 	  saved = packet;
   4137  1.1  christos 	  packet = strchr (packet, ';');
   4138  1.1  christos 	  nbytes = (packet - saved) / 2;
   4139  1.1  christos 	  notes = (char *) xmalloc (nbytes + 1);
   4140  1.1  christos 	  nbytes = hex2bin (saved, (gdb_byte *) notes, nbytes);
   4141  1.1  christos 	  notes[nbytes] = '\0';
   4142  1.1  christos 	  ++packet; /* skip the semicolon */
   4143  1.1  christos 	  trace_debug ("Notes is '%s'", notes);
   4144  1.1  christos 	  xfree (tracing_notes);
   4145  1.1  christos 	  tracing_notes = notes;
   4146  1.1  christos 	}
   4147  1.1  christos       else if (startswith (packet, "tstop:"))
   4148  1.1  christos 	{
   4149  1.1  christos 	  packet += strlen ("tstop:");
   4150  1.1  christos 	  saved = packet;
   4151  1.1  christos 	  packet = strchr (packet, ';');
   4152  1.1  christos 	  nbytes = (packet - saved) / 2;
   4153  1.1  christos 	  stopnote = (char *) xmalloc (nbytes + 1);
   4154  1.1  christos 	  nbytes = hex2bin (saved, (gdb_byte *) stopnote, nbytes);
   4155  1.1  christos 	  stopnote[nbytes] = '\0';
   4156  1.1  christos 	  ++packet; /* skip the semicolon */
   4157  1.1  christos 	  trace_debug ("tstop note is '%s'", stopnote);
   4158  1.1  christos 	  xfree (tracing_stop_note);
   4159  1.1  christos 	  tracing_stop_note = stopnote;
   4160  1.1  christos 	}
   4161  1.1  christos       else
   4162  1.1  christos 	break;
   4163  1.1  christos     }
   4164  1.1  christos 
   4165  1.1  christos   write_ok (own_buf);
   4166  1.1  christos }
   4167  1.1  christos 
   4168  1.1  christos int
   4169  1.1  christos handle_tracepoint_general_set (char *packet)
   4170  1.1  christos {
   4171  1.1  christos   if (strcmp ("QTinit", packet) == 0)
   4172  1.1  christos     {
   4173  1.1  christos       cmd_qtinit (packet);
   4174  1.1  christos       return 1;
   4175  1.1  christos     }
   4176  1.1  christos   else if (startswith (packet, "QTDP:"))
   4177  1.1  christos     {
   4178  1.1  christos       cmd_qtdp (packet);
   4179  1.1  christos       return 1;
   4180  1.1  christos     }
   4181  1.1  christos   else if (startswith (packet, "QTDPsrc:"))
   4182  1.1  christos     {
   4183  1.1  christos       cmd_qtdpsrc (packet);
   4184  1.1  christos       return 1;
   4185  1.1  christos     }
   4186  1.1  christos   else if (startswith (packet, "QTEnable:"))
   4187  1.1  christos     {
   4188  1.1  christos       cmd_qtenable_disable (packet, 1);
   4189  1.1  christos       return 1;
   4190  1.1  christos     }
   4191  1.1  christos   else if (startswith (packet, "QTDisable:"))
   4192  1.1  christos     {
   4193  1.1  christos       cmd_qtenable_disable (packet, 0);
   4194  1.1  christos       return 1;
   4195  1.1  christos     }
   4196  1.1  christos   else if (startswith (packet, "QTDV:"))
   4197  1.1  christos     {
   4198  1.1  christos       cmd_qtdv (packet);
   4199  1.1  christos       return 1;
   4200  1.1  christos     }
   4201  1.1  christos   else if (startswith (packet, "QTro:"))
   4202  1.1  christos     {
   4203  1.1  christos       cmd_qtro (packet);
   4204  1.1  christos       return 1;
   4205  1.1  christos     }
   4206  1.1  christos   else if (strcmp ("QTStart", packet) == 0)
   4207  1.1  christos     {
   4208  1.1  christos       cmd_qtstart (packet);
   4209  1.1  christos       return 1;
   4210  1.1  christos     }
   4211  1.1  christos   else if (strcmp ("QTStop", packet) == 0)
   4212  1.1  christos     {
   4213  1.1  christos       cmd_qtstop (packet);
   4214  1.1  christos       return 1;
   4215  1.1  christos     }
   4216  1.1  christos   else if (startswith (packet, "QTDisconnected:"))
   4217  1.1  christos     {
   4218  1.1  christos       cmd_qtdisconnected (packet);
   4219  1.1  christos       return 1;
   4220  1.1  christos     }
   4221  1.1  christos   else if (startswith (packet, "QTFrame:"))
   4222  1.1  christos     {
   4223  1.1  christos       cmd_qtframe (packet);
   4224  1.1  christos       return 1;
   4225  1.1  christos     }
   4226  1.1  christos   else if (startswith (packet, "QTBuffer:circular:"))
   4227  1.1  christos     {
   4228  1.1  christos       cmd_bigqtbuffer_circular (packet);
   4229  1.1  christos       return 1;
   4230  1.1  christos     }
   4231  1.1  christos   else if (startswith (packet, "QTBuffer:size:"))
   4232  1.1  christos     {
   4233  1.1  christos       cmd_bigqtbuffer_size (packet);
   4234  1.1  christos       return 1;
   4235  1.1  christos     }
   4236  1.1  christos   else if (startswith (packet, "QTNotes:"))
   4237  1.1  christos     {
   4238  1.1  christos       cmd_qtnotes (packet);
   4239  1.1  christos       return 1;
   4240  1.1  christos     }
   4241  1.1  christos 
   4242  1.1  christos   return 0;
   4243  1.1  christos }
   4244  1.1  christos 
   4245  1.1  christos int
   4246  1.1  christos handle_tracepoint_query (char *packet)
   4247  1.1  christos {
   4248  1.1  christos   if (strcmp ("qTStatus", packet) == 0)
   4249  1.1  christos     {
   4250  1.1  christos       cmd_qtstatus (packet);
   4251  1.1  christos       return 1;
   4252  1.1  christos     }
   4253  1.1  christos   else if (startswith (packet, "qTP:"))
   4254  1.1  christos     {
   4255  1.1  christos       cmd_qtp (packet);
   4256  1.1  christos       return 1;
   4257  1.1  christos     }
   4258  1.1  christos   else if (strcmp ("qTfP", packet) == 0)
   4259  1.1  christos     {
   4260  1.1  christos       cmd_qtfp (packet);
   4261  1.1  christos       return 1;
   4262  1.1  christos     }
   4263  1.1  christos   else if (strcmp ("qTsP", packet) == 0)
   4264  1.1  christos     {
   4265  1.1  christos       cmd_qtsp (packet);
   4266  1.1  christos       return 1;
   4267  1.1  christos     }
   4268  1.1  christos   else if (strcmp ("qTfV", packet) == 0)
   4269  1.1  christos     {
   4270  1.1  christos       cmd_qtfv (packet);
   4271  1.1  christos       return 1;
   4272  1.1  christos     }
   4273  1.1  christos   else if (strcmp ("qTsV", packet) == 0)
   4274  1.1  christos     {
   4275  1.1  christos       cmd_qtsv (packet);
   4276  1.1  christos       return 1;
   4277  1.1  christos     }
   4278  1.1  christos   else if (startswith (packet, "qTV:"))
   4279  1.1  christos     {
   4280  1.1  christos       cmd_qtv (packet);
   4281  1.1  christos       return 1;
   4282  1.1  christos     }
   4283  1.1  christos   else if (startswith (packet, "qTBuffer:"))
   4284  1.1  christos     {
   4285  1.1  christos       cmd_qtbuffer (packet);
   4286  1.1  christos       return 1;
   4287  1.1  christos     }
   4288  1.1  christos   else if (strcmp ("qTfSTM", packet) == 0)
   4289  1.1  christos     {
   4290  1.1  christos       cmd_qtfstm (packet);
   4291  1.1  christos       return 1;
   4292  1.1  christos     }
   4293  1.1  christos   else if (strcmp ("qTsSTM", packet) == 0)
   4294  1.1  christos     {
   4295  1.1  christos       cmd_qtsstm (packet);
   4296  1.1  christos       return 1;
   4297  1.1  christos     }
   4298  1.1  christos   else if (startswith (packet, "qTSTMat:"))
   4299  1.1  christos     {
   4300  1.1  christos       cmd_qtstmat (packet);
   4301  1.1  christos       return 1;
   4302  1.1  christos     }
   4303  1.1  christos   else if (strcmp ("qTMinFTPILen", packet) == 0)
   4304  1.1  christos     {
   4305  1.1  christos       cmd_qtminftpilen (packet);
   4306  1.1  christos       return 1;
   4307  1.1  christos     }
   4308  1.1  christos 
   4309  1.1  christos   return 0;
   4310  1.1  christos }
   4311  1.1  christos 
   4312  1.1  christos #endif
   4313  1.1  christos #ifndef IN_PROCESS_AGENT
   4314  1.1  christos 
   4315  1.1  christos /* Call this when thread TINFO has hit the tracepoint defined by
   4316  1.1  christos    TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
   4317  1.1  christos    action.  This adds a while-stepping collecting state item to the
   4318  1.1  christos    threads' collecting state list, so that we can keep track of
   4319  1.1  christos    multiple simultaneous while-stepping actions being collected by the
   4320  1.1  christos    same thread.  This can happen in cases like:
   4321  1.1  christos 
   4322  1.1  christos     ff0001  INSN1 <-- TP1, while-stepping 10 collect $regs
   4323  1.1  christos     ff0002  INSN2
   4324  1.1  christos     ff0003  INSN3 <-- TP2, collect $regs
   4325  1.1  christos     ff0004  INSN4 <-- TP3, while-stepping 10 collect $regs
   4326  1.1  christos     ff0005  INSN5
   4327  1.1  christos 
   4328  1.1  christos    Notice that when instruction INSN5 is reached, the while-stepping
   4329  1.1  christos    actions of both TP1 and TP3 are still being collected, and that TP2
   4330  1.1  christos    had been collected meanwhile.  The whole range of ff0001-ff0005
   4331  1.1  christos    should be single-stepped, due to at least TP1's while-stepping
   4332  1.1  christos    action covering the whole range.  */
   4333  1.1  christos 
   4334  1.1  christos static void
   4335  1.1  christos add_while_stepping_state (struct thread_info *tinfo,
   4336  1.1  christos 			  int tp_number, CORE_ADDR tp_address)
   4337  1.1  christos {
   4338  1.1  christos   struct wstep_state *wstep = XNEW (struct wstep_state);
   4339  1.1  christos 
   4340  1.1  christos   wstep->next = tinfo->while_stepping;
   4341  1.1  christos 
   4342  1.1  christos   wstep->tp_number = tp_number;
   4343  1.1  christos   wstep->tp_address = tp_address;
   4344  1.1  christos   wstep->current_step = 0;
   4345  1.1  christos 
   4346  1.1  christos   tinfo->while_stepping = wstep;
   4347  1.1  christos }
   4348  1.1  christos 
   4349  1.1  christos /* Release the while-stepping collecting state WSTEP.  */
   4350  1.1  christos 
   4351  1.1  christos static void
   4352  1.1  christos release_while_stepping_state (struct wstep_state *wstep)
   4353  1.1  christos {
   4354  1.1  christos   free (wstep);
   4355  1.1  christos }
   4356  1.1  christos 
   4357  1.1  christos /* Release all while-stepping collecting states currently associated
   4358  1.1  christos    with thread TINFO.  */
   4359  1.1  christos 
   4360  1.1  christos void
   4361  1.1  christos release_while_stepping_state_list (struct thread_info *tinfo)
   4362  1.1  christos {
   4363  1.1  christos   struct wstep_state *head;
   4364  1.1  christos 
   4365  1.1  christos   while (tinfo->while_stepping)
   4366  1.1  christos     {
   4367  1.1  christos       head = tinfo->while_stepping;
   4368  1.1  christos       tinfo->while_stepping = head->next;
   4369  1.1  christos       release_while_stepping_state (head);
   4370  1.1  christos     }
   4371  1.1  christos }
   4372  1.1  christos 
   4373  1.1  christos /* If TINFO was handling a 'while-stepping' action, the step has
   4374  1.1  christos    finished, so collect any step data needed, and check if any more
   4375  1.1  christos    steps are required.  Return true if the thread was indeed
   4376  1.1  christos    collecting tracepoint data, false otherwise.  */
   4377  1.1  christos 
   4378  1.1  christos int
   4379  1.1  christos tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
   4380  1.1  christos {
   4381  1.1  christos   struct tracepoint *tpoint;
   4382  1.1  christos   struct wstep_state *wstep;
   4383  1.1  christos   struct wstep_state **wstep_link;
   4384  1.1  christos   struct trap_tracepoint_ctx ctx;
   4385  1.1  christos 
   4386  1.1  christos   /* Pull in fast tracepoint trace frames from the inferior lib buffer into
   4387  1.1  christos      our buffer.  */
   4388  1.1  christos   if (agent_loaded_p ())
   4389  1.1  christos     upload_fast_traceframes ();
   4390  1.1  christos 
   4391  1.1  christos   /* Check if we were indeed collecting data for one of more
   4392  1.1  christos      tracepoints with a 'while-stepping' count.  */
   4393  1.1  christos   if (tinfo->while_stepping == NULL)
   4394  1.1  christos     return 0;
   4395  1.1  christos 
   4396  1.1  christos   if (!tracing)
   4397  1.1  christos     {
   4398  1.1  christos       /* We're not even tracing anymore.  Stop this thread from
   4399  1.1  christos 	 collecting.  */
   4400  1.1  christos       release_while_stepping_state_list (tinfo);
   4401  1.1  christos 
   4402  1.1  christos       /* The thread had stopped due to a single-step request indeed
   4403  1.1  christos 	 explained by a tracepoint.  */
   4404  1.1  christos       return 1;
   4405  1.1  christos     }
   4406  1.1  christos 
   4407  1.1  christos   wstep = tinfo->while_stepping;
   4408  1.1  christos   wstep_link = &tinfo->while_stepping;
   4409  1.1  christos 
   4410  1.1  christos   trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
   4411  1.1  christos 	       target_pid_to_str (tinfo->id),
   4412  1.1  christos 	       wstep->tp_number, paddress (wstep->tp_address));
   4413  1.1  christos 
   4414  1.1  christos   ctx.base.type = trap_tracepoint;
   4415  1.1  christos   ctx.regcache = get_thread_regcache (tinfo, 1);
   4416  1.1  christos 
   4417  1.1  christos   while (wstep != NULL)
   4418  1.1  christos     {
   4419  1.1  christos       tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address);
   4420  1.1  christos       if (tpoint == NULL)
   4421  1.1  christos 	{
   4422  1.1  christos 	  trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
   4423  1.1  christos 		       wstep->tp_number, paddress (wstep->tp_address),
   4424  1.1  christos 		       target_pid_to_str (tinfo->id));
   4425  1.1  christos 
   4426  1.1  christos 	  /* Unlink.  */
   4427  1.1  christos 	  *wstep_link = wstep->next;
   4428  1.1  christos 	  release_while_stepping_state (wstep);
   4429  1.1  christos 	  wstep = *wstep_link;
   4430  1.1  christos 	  continue;
   4431  1.1  christos 	}
   4432  1.1  christos 
   4433  1.1  christos       /* We've just finished one step.  */
   4434  1.1  christos       ++wstep->current_step;
   4435  1.1  christos 
   4436  1.1  christos       /* Collect data.  */
   4437  1.1  christos       collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx,
   4438  1.1  christos 			    stop_pc, tpoint, wstep->current_step);
   4439  1.1  christos 
   4440  1.1  christos       if (wstep->current_step >= tpoint->step_count)
   4441  1.1  christos 	{
   4442  1.1  christos 	  /* The requested numbers of steps have occurred.  */
   4443  1.1  christos 	  trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
   4444  1.1  christos 		       target_pid_to_str (tinfo->id),
   4445  1.1  christos 		       wstep->tp_number, paddress (wstep->tp_address));
   4446  1.1  christos 
   4447  1.1  christos 	  /* Unlink the wstep.  */
   4448  1.1  christos 	  *wstep_link = wstep->next;
   4449  1.1  christos 	  release_while_stepping_state (wstep);
   4450  1.1  christos 	  wstep = *wstep_link;
   4451  1.1  christos 
   4452  1.1  christos 	  /* Only check the hit count now, which ensure that we do all
   4453  1.1  christos 	     our stepping before stopping the run.  */
   4454  1.1  christos 	  if (tpoint->pass_count > 0
   4455  1.1  christos 	      && tpoint->hit_count >= tpoint->pass_count
   4456  1.1  christos 	      && stopping_tracepoint == NULL)
   4457  1.1  christos 	    stopping_tracepoint = tpoint;
   4458  1.1  christos 	}
   4459  1.1  christos       else
   4460  1.1  christos 	{
   4461  1.1  christos 	  /* Keep single-stepping until the requested numbers of steps
   4462  1.1  christos 	     have occurred.  */
   4463  1.1  christos 	  wstep_link = &wstep->next;
   4464  1.1  christos 	  wstep = *wstep_link;
   4465  1.1  christos 	}
   4466  1.1  christos 
   4467  1.1  christos       if (stopping_tracepoint
   4468  1.1  christos 	  || trace_buffer_is_full
   4469  1.1  christos 	  || expr_eval_result != expr_eval_no_error)
   4470  1.1  christos 	{
   4471  1.1  christos 	  stop_tracing ();
   4472  1.1  christos 	  break;
   4473  1.1  christos 	}
   4474  1.1  christos     }
   4475  1.1  christos 
   4476  1.1  christos   return 1;
   4477  1.1  christos }
   4478  1.1  christos 
   4479  1.1  christos /* Handle any internal tracing control breakpoint hits.  That means,
   4480  1.1  christos    pull traceframes from the IPA to our buffer, and syncing both
   4481  1.1  christos    tracing agents when the IPA's tracing stops for some reason.  */
   4482  1.1  christos 
   4483  1.1  christos int
   4484  1.1  christos handle_tracepoint_bkpts (struct thread_info *tinfo, CORE_ADDR stop_pc)
   4485  1.1  christos {
   4486  1.1  christos   /* Pull in fast tracepoint trace frames from the inferior in-process
   4487  1.1  christos      agent's buffer into our buffer.  */
   4488  1.1  christos 
   4489  1.1  christos   if (!agent_loaded_p ())
   4490  1.1  christos     return 0;
   4491  1.1  christos 
   4492  1.1  christos   upload_fast_traceframes ();
   4493  1.1  christos 
   4494  1.1  christos   /* Check if the in-process agent had decided we should stop
   4495  1.1  christos      tracing.  */
   4496  1.1  christos   if (stop_pc == ipa_sym_addrs.addr_stop_tracing)
   4497  1.1  christos     {
   4498  1.1  christos       int ipa_trace_buffer_is_full;
   4499  1.1  christos       CORE_ADDR ipa_stopping_tracepoint;
   4500  1.1  christos       int ipa_expr_eval_result;
   4501  1.1  christos       CORE_ADDR ipa_error_tracepoint;
   4502  1.1  christos 
   4503  1.1  christos       trace_debug ("lib stopped at stop_tracing");
   4504  1.1  christos 
   4505  1.1  christos       read_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full,
   4506  1.1  christos 			     &ipa_trace_buffer_is_full);
   4507  1.1  christos 
   4508  1.1  christos       read_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
   4509  1.1  christos 				  &ipa_stopping_tracepoint);
   4510  1.1  christos       write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 0);
   4511  1.1  christos 
   4512  1.1  christos       read_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint,
   4513  1.1  christos 				  &ipa_error_tracepoint);
   4514  1.1  christos       write_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint, 0);
   4515  1.1  christos 
   4516  1.1  christos       read_inferior_integer (ipa_sym_addrs.addr_expr_eval_result,
   4517  1.1  christos 			     &ipa_expr_eval_result);
   4518  1.1  christos       write_inferior_integer (ipa_sym_addrs.addr_expr_eval_result, 0);
   4519  1.1  christos 
   4520  1.1  christos       trace_debug ("lib: trace_buffer_is_full: %d, "
   4521  1.1  christos 		   "stopping_tracepoint: %s, "
   4522  1.1  christos 		   "ipa_expr_eval_result: %d, "
   4523  1.1  christos 		   "error_tracepoint: %s, ",
   4524  1.1  christos 		   ipa_trace_buffer_is_full,
   4525  1.1  christos 		   paddress (ipa_stopping_tracepoint),
   4526  1.1  christos 		   ipa_expr_eval_result,
   4527  1.1  christos 		   paddress (ipa_error_tracepoint));
   4528  1.1  christos 
   4529  1.1  christos       if (debug_threads)
   4530  1.1  christos 	{
   4531  1.1  christos 	  if (ipa_trace_buffer_is_full)
   4532  1.1  christos 	    trace_debug ("lib stopped due to full buffer.");
   4533  1.1  christos 	  if (ipa_stopping_tracepoint)
   4534  1.1  christos 	    trace_debug ("lib stopped due to tpoint");
   4535  1.1  christos 	  if (ipa_error_tracepoint)
   4536  1.1  christos 	    trace_debug ("lib stopped due to error");
   4537  1.1  christos 	}
   4538  1.1  christos 
   4539  1.1  christos       if (ipa_stopping_tracepoint != 0)
   4540  1.1  christos 	{
   4541  1.1  christos 	  stopping_tracepoint
   4542  1.1  christos 	    = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint);
   4543  1.1  christos 	}
   4544  1.1  christos       else if (ipa_expr_eval_result != expr_eval_no_error)
   4545  1.1  christos 	{
   4546  1.1  christos 	  expr_eval_result = ipa_expr_eval_result;
   4547  1.1  christos 	  error_tracepoint
   4548  1.1  christos 	    = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint);
   4549  1.1  christos 	}
   4550  1.1  christos       stop_tracing ();
   4551  1.1  christos       return 1;
   4552  1.1  christos     }
   4553  1.1  christos   else if (stop_pc == ipa_sym_addrs.addr_flush_trace_buffer)
   4554  1.1  christos     {
   4555  1.1  christos       trace_debug ("lib stopped at flush_trace_buffer");
   4556  1.1  christos       return 1;
   4557  1.1  christos     }
   4558  1.1  christos 
   4559  1.1  christos   return 0;
   4560  1.1  christos }
   4561  1.1  christos 
   4562  1.1  christos /* Return true if TINFO just hit a tracepoint.  Collect data if
   4563  1.1  christos    so.  */
   4564  1.1  christos 
   4565  1.1  christos int
   4566  1.1  christos tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc)
   4567  1.1  christos {
   4568  1.1  christos   struct tracepoint *tpoint;
   4569  1.1  christos   int ret = 0;
   4570  1.1  christos   struct trap_tracepoint_ctx ctx;
   4571  1.1  christos 
   4572  1.1  christos   /* Not tracing, don't handle.  */
   4573  1.1  christos   if (!tracing)
   4574  1.1  christos     return 0;
   4575  1.1  christos 
   4576  1.1  christos   ctx.base.type = trap_tracepoint;
   4577  1.1  christos   ctx.regcache = get_thread_regcache (tinfo, 1);
   4578  1.1  christos 
   4579  1.1  christos   for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
   4580  1.1  christos     {
   4581  1.1  christos       /* Note that we collect fast tracepoints here as well.  We'll
   4582  1.1  christos 	 step over the fast tracepoint jump later, which avoids the
   4583  1.1  christos 	 double collect.  However, we don't collect for static
   4584  1.1  christos 	 tracepoints here, because UST markers are compiled in program,
   4585  1.1  christos 	 and probes will be executed in program.  So static tracepoints
   4586  1.1  christos 	 are collected there.   */
   4587  1.1  christos       if (tpoint->enabled && stop_pc == tpoint->address
   4588  1.1  christos 	  && tpoint->type != static_tracepoint)
   4589  1.1  christos 	{
   4590  1.1  christos 	  trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
   4591  1.1  christos 		       target_pid_to_str (tinfo->id),
   4592  1.1  christos 		       tpoint->number, paddress (tpoint->address));
   4593  1.1  christos 
   4594  1.1  christos 	  /* Test the condition if present, and collect if true.  */
   4595  1.1  christos 	  if (!tpoint->cond
   4596  1.1  christos 	      || (condition_true_at_tracepoint
   4597  1.1  christos 		  ((struct tracepoint_hit_ctx *) &ctx, tpoint)))
   4598  1.1  christos 	    collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
   4599  1.1  christos 					stop_pc, tpoint);
   4600  1.1  christos 
   4601  1.1  christos 	  if (stopping_tracepoint
   4602  1.1  christos 	      || trace_buffer_is_full
   4603  1.1  christos 	      || expr_eval_result != expr_eval_no_error)
   4604  1.1  christos 	    {
   4605  1.1  christos 	      stop_tracing ();
   4606  1.1  christos 	    }
   4607  1.1  christos 	  /* If the tracepoint had a 'while-stepping' action, then set
   4608  1.1  christos 	     the thread to collect this tracepoint on the following
   4609  1.1  christos 	     single-steps.  */
   4610  1.1  christos 	  else if (tpoint->step_count > 0)
   4611  1.1  christos 	    {
   4612  1.1  christos 	      add_while_stepping_state (tinfo,
   4613  1.1  christos 					tpoint->number, tpoint->address);
   4614  1.1  christos 	    }
   4615  1.1  christos 
   4616  1.1  christos 	  ret = 1;
   4617  1.1  christos 	}
   4618  1.1  christos     }
   4619  1.1  christos 
   4620  1.1  christos   return ret;
   4621  1.1  christos }
   4622  1.1  christos 
   4623  1.1  christos #endif
   4624  1.1  christos 
   4625  1.1  christos #if defined IN_PROCESS_AGENT && defined HAVE_UST
   4626  1.1  christos struct ust_marker_data;
   4627  1.1  christos static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
   4628  1.1  christos 					    struct traceframe *tframe);
   4629  1.1  christos #endif
   4630  1.1  christos 
   4631  1.1  christos /* Create a trace frame for the hit of the given tracepoint in the
   4632  1.1  christos    given thread.  */
   4633  1.1  christos 
   4634  1.1  christos static void
   4635  1.1  christos collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc,
   4636  1.1  christos 			    struct tracepoint *tpoint)
   4637  1.1  christos {
   4638  1.1  christos   struct traceframe *tframe;
   4639  1.1  christos   int acti;
   4640  1.1  christos 
   4641  1.1  christos   /* Only count it as a hit when we actually collect data.  */
   4642  1.1  christos   tpoint->hit_count++;
   4643  1.1  christos 
   4644  1.1  christos   /* If we've exceeded a defined pass count, record the event for
   4645  1.1  christos      later, and finish the collection for this hit.  This test is only
   4646  1.1  christos      for nonstepping tracepoints, stepping tracepoints test at the end
   4647  1.1  christos      of their while-stepping loop.  */
   4648  1.1  christos   if (tpoint->pass_count > 0
   4649  1.1  christos       && tpoint->hit_count >= tpoint->pass_count
   4650  1.1  christos       && tpoint->step_count == 0
   4651  1.1  christos       && stopping_tracepoint == NULL)
   4652  1.1  christos     stopping_tracepoint = tpoint;
   4653  1.1  christos 
   4654  1.1  christos   trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64,
   4655  1.1  christos 	       tpoint->number, paddress (tpoint->address), tpoint->hit_count);
   4656  1.1  christos 
   4657  1.1  christos   tframe = add_traceframe (tpoint);
   4658  1.1  christos 
   4659  1.1  christos   if (tframe)
   4660  1.1  christos     {
   4661  1.1  christos       for (acti = 0; acti < tpoint->numactions; ++acti)
   4662  1.1  christos 	{
   4663  1.1  christos #ifndef IN_PROCESS_AGENT
   4664  1.1  christos 	  trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
   4665  1.1  christos 		       tpoint->number, paddress (tpoint->address),
   4666  1.1  christos 		       tpoint->actions_str[acti]);
   4667  1.1  christos #endif
   4668  1.1  christos 
   4669  1.1  christos 	  do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
   4670  1.1  christos 				   tpoint->actions[acti]);
   4671  1.1  christos 	}
   4672  1.1  christos 
   4673  1.1  christos       finish_traceframe (tframe);
   4674  1.1  christos     }
   4675  1.1  christos 
   4676  1.1  christos   if (tframe == NULL && tracing)
   4677  1.1  christos     trace_buffer_is_full = 1;
   4678  1.1  christos }
   4679  1.1  christos 
   4680  1.1  christos #ifndef IN_PROCESS_AGENT
   4681  1.1  christos 
   4682  1.1  christos static void
   4683  1.1  christos collect_data_at_step (struct tracepoint_hit_ctx *ctx,
   4684  1.1  christos 		      CORE_ADDR stop_pc,
   4685  1.1  christos 		      struct tracepoint *tpoint, int current_step)
   4686  1.1  christos {
   4687  1.1  christos   struct traceframe *tframe;
   4688  1.1  christos   int acti;
   4689  1.1  christos 
   4690  1.1  christos   trace_debug ("Making new step traceframe for "
   4691  1.1  christos 	       "tracepoint %d at 0x%s, step %d of %" PRIu64 ", hit %" PRIu64,
   4692  1.1  christos 	       tpoint->number, paddress (tpoint->address),
   4693  1.1  christos 	       current_step, tpoint->step_count,
   4694  1.1  christos 	       tpoint->hit_count);
   4695  1.1  christos 
   4696  1.1  christos   tframe = add_traceframe (tpoint);
   4697  1.1  christos 
   4698  1.1  christos   if (tframe)
   4699  1.1  christos     {
   4700  1.1  christos       for (acti = 0; acti < tpoint->num_step_actions; ++acti)
   4701  1.1  christos 	{
   4702  1.1  christos 	  trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
   4703  1.1  christos 		       tpoint->number, paddress (tpoint->address),
   4704  1.1  christos 		       tpoint->step_actions_str[acti]);
   4705  1.1  christos 
   4706  1.1  christos 	  do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
   4707  1.1  christos 				   tpoint->step_actions[acti]);
   4708  1.1  christos 	}
   4709  1.1  christos 
   4710  1.1  christos       finish_traceframe (tframe);
   4711  1.1  christos     }
   4712  1.1  christos 
   4713  1.1  christos   if (tframe == NULL && tracing)
   4714  1.1  christos     trace_buffer_is_full = 1;
   4715  1.1  christos }
   4716  1.1  christos 
   4717  1.1  christos #endif
   4718  1.1  christos 
   4719  1.1  christos #ifdef IN_PROCESS_AGENT
   4720  1.1  christos /* The target description index for IPA.  Passed from gdbserver, used
   4721  1.1  christos    to select ipa_tdesc.  */
   4722  1.1  christos EXTERN_C_PUSH
   4723  1.1  christos IP_AGENT_EXPORT_VAR int ipa_tdesc_idx;
   4724  1.1  christos EXTERN_C_POP
   4725  1.1  christos #endif
   4726  1.1  christos 
   4727  1.1  christos static struct regcache *
   4728  1.1  christos get_context_regcache (struct tracepoint_hit_ctx *ctx)
   4729  1.1  christos {
   4730  1.1  christos   struct regcache *regcache = NULL;
   4731  1.1  christos #ifdef IN_PROCESS_AGENT
   4732  1.1  christos   const struct target_desc *ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx);
   4733  1.1  christos 
   4734  1.1  christos   if (ctx->type == fast_tracepoint)
   4735  1.1  christos     {
   4736  1.1  christos       struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
   4737  1.1  christos       if (!fctx->regcache_initted)
   4738  1.1  christos 	{
   4739  1.1  christos 	  fctx->regcache_initted = 1;
   4740  1.1  christos 	  init_register_cache (&fctx->regcache, ipa_tdesc, fctx->regspace);
   4741  1.1  christos 	  supply_regblock (&fctx->regcache, NULL);
   4742  1.1  christos 	  supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
   4743  1.1  christos 	}
   4744  1.1  christos       regcache = &fctx->regcache;
   4745  1.1  christos     }
   4746  1.1  christos #ifdef HAVE_UST
   4747  1.1  christos   if (ctx->type == static_tracepoint)
   4748  1.1  christos     {
   4749  1.1  christos       struct static_tracepoint_ctx *sctx
   4750  1.1  christos 	= (struct static_tracepoint_ctx *) ctx;
   4751  1.1  christos 
   4752  1.1  christos       if (!sctx->regcache_initted)
   4753  1.1  christos 	{
   4754  1.1  christos 	  sctx->regcache_initted = 1;
   4755  1.1  christos 	  init_register_cache (&sctx->regcache, ipa_tdesc, sctx->regspace);
   4756  1.1  christos 	  supply_regblock (&sctx->regcache, NULL);
   4757  1.1  christos 	  /* Pass down the tracepoint address, because REGS doesn't
   4758  1.1  christos 	     include the PC, but we know what it must have been.  */
   4759  1.1  christos 	  supply_static_tracepoint_registers (&sctx->regcache,
   4760  1.1  christos 					      (const unsigned char *)
   4761  1.1  christos 					      sctx->regs,
   4762  1.1  christos 					      sctx->tpoint->address);
   4763  1.1  christos 	}
   4764  1.1  christos       regcache = &sctx->regcache;
   4765  1.1  christos     }
   4766  1.1  christos #endif
   4767  1.1  christos #else
   4768  1.1  christos   if (ctx->type == trap_tracepoint)
   4769  1.1  christos     {
   4770  1.1  christos       struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx;
   4771  1.1  christos       regcache = tctx->regcache;
   4772  1.1  christos     }
   4773  1.1  christos #endif
   4774  1.1  christos 
   4775  1.1  christos   gdb_assert (regcache != NULL);
   4776  1.1  christos 
   4777  1.1  christos   return regcache;
   4778  1.1  christos }
   4779  1.1  christos 
   4780  1.1  christos static void
   4781  1.1  christos do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
   4782  1.1  christos 			 CORE_ADDR stop_pc,
   4783  1.1  christos 			 struct tracepoint *tpoint,
   4784  1.1  christos 			 struct traceframe *tframe,
   4785  1.1  christos 			 struct tracepoint_action *taction)
   4786  1.1  christos {
   4787  1.1  christos   enum eval_result_type err;
   4788  1.1  christos 
   4789  1.1  christos   switch (taction->type)
   4790  1.1  christos     {
   4791  1.1  christos     case 'M':
   4792  1.1  christos       {
   4793  1.1  christos 	struct collect_memory_action *maction;
   4794  1.1  christos 	struct eval_agent_expr_context ax_ctx;
   4795  1.1  christos 
   4796  1.1  christos 	maction = (struct collect_memory_action *) taction;
   4797  1.1  christos 	ax_ctx.regcache = NULL;
   4798  1.1  christos 	ax_ctx.tframe = tframe;
   4799  1.1  christos 	ax_ctx.tpoint = tpoint;
   4800  1.1  christos 
   4801  1.1  christos 	trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
   4802  1.1  christos 		     pulongest (maction->len),
   4803  1.1  christos 		     paddress (maction->addr), maction->basereg);
   4804  1.1  christos 	/* (should use basereg) */
   4805  1.1  christos 	agent_mem_read (&ax_ctx, NULL, (CORE_ADDR) maction->addr,
   4806  1.1  christos 			maction->len);
   4807  1.1  christos 	break;
   4808  1.1  christos       }
   4809  1.1  christos     case 'R':
   4810  1.1  christos       {
   4811  1.1  christos 	unsigned char *regspace;
   4812  1.1  christos 	struct regcache tregcache;
   4813  1.1  christos 	struct regcache *context_regcache;
   4814  1.1  christos 	int regcache_size;
   4815  1.1  christos 
   4816  1.1  christos 	trace_debug ("Want to collect registers");
   4817  1.1  christos 
   4818  1.1  christos 	context_regcache = get_context_regcache (ctx);
   4819  1.1  christos 	regcache_size = register_cache_size (context_regcache->tdesc);
   4820  1.1  christos 
   4821  1.1  christos 	/* Collect all registers for now.  */
   4822  1.1  christos 	regspace = add_traceframe_block (tframe, tpoint, 1 + regcache_size);
   4823  1.1  christos 	if (regspace == NULL)
   4824  1.1  christos 	  {
   4825  1.1  christos 	    trace_debug ("Trace buffer block allocation failed, skipping");
   4826  1.1  christos 	    break;
   4827  1.1  christos 	  }
   4828  1.1  christos 	/* Identify a register block.  */
   4829  1.1  christos 	*regspace = 'R';
   4830  1.1  christos 
   4831  1.1  christos 	/* Wrap the regblock in a register cache (in the stack, we
   4832  1.1  christos 	   don't want to malloc here).  */
   4833  1.1  christos 	init_register_cache (&tregcache, context_regcache->tdesc,
   4834  1.1  christos 			     regspace + 1);
   4835  1.1  christos 
   4836  1.1  christos 	/* Copy the register data to the regblock.  */
   4837  1.1  christos 	regcache_cpy (&tregcache, context_regcache);
   4838  1.1  christos 
   4839  1.1  christos #ifndef IN_PROCESS_AGENT
   4840  1.1  christos 	/* On some platforms, trap-based tracepoints will have the PC
   4841  1.1  christos 	   pointing to the next instruction after the trap, but we
   4842  1.1  christos 	   don't want the user or GDB trying to guess whether the
   4843  1.1  christos 	   saved PC needs adjusting; so always record the adjusted
   4844  1.1  christos 	   stop_pc.  Note that we can't use tpoint->address instead,
   4845  1.1  christos 	   since it will be wrong for while-stepping actions.  This
   4846  1.1  christos 	   adjustment is a nop for fast tracepoints collected from the
   4847  1.1  christos 	   in-process lib (but not if GDBserver is collecting one
   4848  1.1  christos 	   preemptively), since the PC had already been adjusted to
   4849  1.1  christos 	   contain the tracepoint's address by the jump pad.  */
   4850  1.1  christos 	trace_debug ("Storing stop pc (0x%s) in regblock",
   4851  1.1  christos 		     paddress (stop_pc));
   4852  1.1  christos 
   4853  1.1  christos 	/* This changes the regblock, not the thread's
   4854  1.1  christos 	   regcache.  */
   4855  1.1  christos 	regcache_write_pc (&tregcache, stop_pc);
   4856  1.1  christos #endif
   4857  1.1  christos       }
   4858  1.1  christos       break;
   4859  1.1  christos     case 'X':
   4860  1.1  christos       {
   4861  1.1  christos 	struct eval_expr_action *eaction;
   4862  1.1  christos 	struct eval_agent_expr_context ax_ctx;
   4863  1.1  christos 
   4864  1.1  christos 	eaction = (struct eval_expr_action *) taction;
   4865  1.1  christos 	ax_ctx.regcache = get_context_regcache (ctx);
   4866  1.1  christos 	ax_ctx.tframe = tframe;
   4867  1.1  christos 	ax_ctx.tpoint = tpoint;
   4868  1.1  christos 
   4869  1.1  christos 	trace_debug ("Want to evaluate expression");
   4870  1.1  christos 
   4871  1.1  christos 	err = gdb_eval_agent_expr (&ax_ctx, eaction->expr, NULL);
   4872  1.1  christos 
   4873  1.1  christos 	if (err != expr_eval_no_error)
   4874  1.1  christos 	  {
   4875  1.1  christos 	    record_tracepoint_error (tpoint, "action expression", err);
   4876  1.1  christos 	    return;
   4877  1.1  christos 	  }
   4878  1.1  christos       }
   4879  1.1  christos       break;
   4880  1.1  christos     case 'L':
   4881  1.1  christos       {
   4882  1.1  christos #if defined IN_PROCESS_AGENT && defined HAVE_UST
   4883  1.1  christos 	trace_debug ("Want to collect static trace data");
   4884  1.1  christos 	collect_ust_data_at_tracepoint (ctx, tframe);
   4885  1.1  christos #else
   4886  1.1  christos 	trace_debug ("warning: collecting static trace data, "
   4887  1.1  christos 		     "but static tracepoints are not supported");
   4888  1.1  christos #endif
   4889  1.1  christos       }
   4890  1.1  christos       break;
   4891  1.1  christos     default:
   4892  1.1  christos       trace_debug ("unknown trace action '%c', ignoring", taction->type);
   4893  1.1  christos       break;
   4894  1.1  christos     }
   4895  1.1  christos }
   4896  1.1  christos 
   4897  1.1  christos static int
   4898  1.1  christos condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
   4899  1.1  christos 			      struct tracepoint *tpoint)
   4900  1.1  christos {
   4901  1.1  christos   ULONGEST value = 0;
   4902  1.1  christos   enum eval_result_type err;
   4903  1.1  christos 
   4904  1.1  christos   /* Presently, gdbserver doesn't run compiled conditions, only the
   4905  1.1  christos      IPA does.  If the program stops at a fast tracepoint's address
   4906  1.1  christos      (e.g., due to a breakpoint, trap tracepoint, or stepping),
   4907  1.1  christos      gdbserver preemptively collect the fast tracepoint.  Later, on
   4908  1.1  christos      resume, gdbserver steps over the fast tracepoint like it steps
   4909  1.1  christos      over breakpoints, so that the IPA doesn't see that fast
   4910  1.1  christos      tracepoint.  This avoids double collects of fast tracepoints in
   4911  1.1  christos      that stopping scenario.  Having gdbserver itself handle the fast
   4912  1.1  christos      tracepoint gives the user a consistent view of when fast or trap
   4913  1.1  christos      tracepoints are collected, compared to an alternative where only
   4914  1.1  christos      trap tracepoints are collected on stop, and fast tracepoints on
   4915  1.1  christos      resume.  When a fast tracepoint is being processed by gdbserver,
   4916  1.1  christos      it is always the non-compiled condition expression that is
   4917  1.1  christos      used.  */
   4918  1.1  christos #ifdef IN_PROCESS_AGENT
   4919  1.1  christos   if (tpoint->compiled_cond)
   4920  1.1  christos     {
   4921  1.1  christos       struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
   4922  1.1  christos       err = ((condfn) (uintptr_t) (tpoint->compiled_cond)) (fctx->regs, &value);
   4923  1.1  christos     }
   4924  1.1  christos   else
   4925  1.1  christos #endif
   4926  1.1  christos     {
   4927  1.1  christos       struct eval_agent_expr_context ax_ctx;
   4928  1.1  christos 
   4929  1.1  christos       ax_ctx.regcache = get_context_regcache (ctx);
   4930  1.1  christos       ax_ctx.tframe = NULL;
   4931  1.1  christos       ax_ctx.tpoint = tpoint;
   4932  1.1  christos 
   4933  1.1  christos       err = gdb_eval_agent_expr (&ax_ctx, tpoint->cond, &value);
   4934  1.1  christos     }
   4935  1.1  christos   if (err != expr_eval_no_error)
   4936  1.1  christos     {
   4937  1.1  christos       record_tracepoint_error (tpoint, "condition", err);
   4938  1.1  christos       /* The error case must return false.  */
   4939  1.1  christos       return 0;
   4940  1.1  christos     }
   4941  1.1  christos 
   4942  1.1  christos   trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
   4943  1.1  christos 	       tpoint->number, paddress (tpoint->address),
   4944  1.1  christos 	       pulongest (value));
   4945  1.1  christos   return (value ? 1 : 0);
   4946  1.1  christos }
   4947  1.1  christos 
   4948  1.1  christos /* Do memory copies for bytecodes.  */
   4949  1.1  christos /* Do the recording of memory blocks for actions and bytecodes.  */
   4950  1.1  christos 
   4951  1.1  christos int
   4952  1.1  christos agent_mem_read (struct eval_agent_expr_context *ctx,
   4953  1.1  christos 		unsigned char *to, CORE_ADDR from, ULONGEST len)
   4954  1.1  christos {
   4955  1.1  christos   unsigned char *mspace;
   4956  1.1  christos   ULONGEST remaining = len;
   4957  1.1  christos   unsigned short blocklen;
   4958  1.1  christos 
   4959  1.1  christos   /* If a 'to' buffer is specified, use it.  */
   4960  1.1  christos   if (to != NULL)
   4961  1.1  christos     {
   4962  1.1  christos       read_inferior_memory (from, to, len);
   4963  1.1  christos       return 0;
   4964  1.1  christos     }
   4965  1.1  christos 
   4966  1.1  christos   /* Otherwise, create a new memory block in the trace buffer.  */
   4967  1.1  christos   while (remaining > 0)
   4968  1.1  christos     {
   4969  1.1  christos       size_t sp;
   4970  1.1  christos 
   4971  1.1  christos       blocklen = (remaining > 65535 ? 65535 : remaining);
   4972  1.1  christos       sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
   4973  1.1  christos       mspace = add_traceframe_block (ctx->tframe, ctx->tpoint, sp);
   4974  1.1  christos       if (mspace == NULL)
   4975  1.1  christos 	return 1;
   4976  1.1  christos       /* Identify block as a memory block.  */
   4977  1.1  christos       *mspace = 'M';
   4978  1.1  christos       ++mspace;
   4979  1.1  christos       /* Record address and size.  */
   4980  1.1  christos       memcpy (mspace, &from, sizeof (from));
   4981  1.1  christos       mspace += sizeof (from);
   4982  1.1  christos       memcpy (mspace, &blocklen, sizeof (blocklen));
   4983  1.1  christos       mspace += sizeof (blocklen);
   4984  1.1  christos       /* Record the memory block proper.  */
   4985  1.1  christos       read_inferior_memory (from, mspace, blocklen);
   4986  1.1  christos       trace_debug ("%d bytes recorded", blocklen);
   4987  1.1  christos       remaining -= blocklen;
   4988  1.1  christos       from += blocklen;
   4989  1.1  christos     }
   4990  1.1  christos   return 0;
   4991  1.1  christos }
   4992  1.1  christos 
   4993  1.1  christos int
   4994  1.1  christos agent_mem_read_string (struct eval_agent_expr_context *ctx,
   4995  1.1  christos 		       unsigned char *to, CORE_ADDR from, ULONGEST len)
   4996  1.1  christos {
   4997  1.1  christos   unsigned char *buf, *mspace;
   4998  1.1  christos   ULONGEST remaining = len;
   4999  1.1  christos   unsigned short blocklen, i;
   5000  1.1  christos 
   5001  1.1  christos   /* To save a bit of space, block lengths are 16-bit, so break large
   5002  1.1  christos      requests into multiple blocks.  Bordering on overkill for strings,
   5003  1.1  christos      but it could happen that someone specifies a large max length.  */
   5004  1.1  christos   while (remaining > 0)
   5005  1.1  christos     {
   5006  1.1  christos       size_t sp;
   5007  1.1  christos 
   5008  1.1  christos       blocklen = (remaining > 65535 ? 65535 : remaining);
   5009  1.1  christos       /* We want working space to accumulate nonzero bytes, since
   5010  1.1  christos 	 traceframes must have a predecided size (otherwise it gets
   5011  1.1  christos 	 harder to wrap correctly for the circular case, etc).  */
   5012  1.1  christos       buf = (unsigned char *) xmalloc (blocklen + 1);
   5013  1.1  christos       for (i = 0; i < blocklen; ++i)
   5014  1.1  christos 	{
   5015  1.1  christos 	  /* Read the string one byte at a time, in case the string is
   5016  1.1  christos 	     at the end of a valid memory area - we don't want a
   5017  1.1  christos 	     correctly-terminated string to engender segvio
   5018  1.1  christos 	     complaints.  */
   5019  1.1  christos 	  read_inferior_memory (from + i, buf + i, 1);
   5020  1.1  christos 
   5021  1.1  christos 	  if (buf[i] == '\0')
   5022  1.1  christos 	    {
   5023  1.1  christos 	      blocklen = i + 1;
   5024  1.1  christos 	      /* Make sure outer loop stops now too.  */
   5025  1.1  christos 	      remaining = blocklen;
   5026  1.1  christos 	      break;
   5027  1.1  christos 	    }
   5028  1.1  christos 	}
   5029  1.1  christos       sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
   5030  1.1  christos       mspace = add_traceframe_block (ctx->tframe, ctx->tpoint, sp);
   5031  1.1  christos       if (mspace == NULL)
   5032  1.1  christos 	{
   5033  1.1  christos 	  xfree (buf);
   5034  1.1  christos 	  return 1;
   5035  1.1  christos 	}
   5036  1.1  christos       /* Identify block as a memory block.  */
   5037  1.1  christos       *mspace = 'M';
   5038  1.1  christos       ++mspace;
   5039  1.1  christos       /* Record address and size.  */
   5040  1.1  christos       memcpy ((void *) mspace, (void *) &from, sizeof (from));
   5041  1.1  christos       mspace += sizeof (from);
   5042  1.1  christos       memcpy ((void *) mspace, (void *) &blocklen, sizeof (blocklen));
   5043  1.1  christos       mspace += sizeof (blocklen);
   5044  1.1  christos       /* Copy the string contents.  */
   5045  1.1  christos       memcpy ((void *) mspace, (void *) buf, blocklen);
   5046  1.1  christos       remaining -= blocklen;
   5047  1.1  christos       from += blocklen;
   5048  1.1  christos       xfree (buf);
   5049  1.1  christos     }
   5050  1.1  christos   return 0;
   5051  1.1  christos }
   5052  1.1  christos 
   5053  1.1  christos /* Record the value of a trace state variable.  */
   5054  1.1  christos 
   5055  1.1  christos int
   5056  1.1  christos agent_tsv_read (struct eval_agent_expr_context *ctx, int n)
   5057  1.1  christos {
   5058  1.1  christos   unsigned char *vspace;
   5059  1.1  christos   LONGEST val;
   5060  1.1  christos 
   5061  1.1  christos   vspace = add_traceframe_block (ctx->tframe, ctx->tpoint,
   5062  1.1  christos 				 1 + sizeof (n) + sizeof (LONGEST));
   5063  1.1  christos   if (vspace == NULL)
   5064  1.1  christos     return 1;
   5065  1.1  christos   /* Identify block as a variable.  */
   5066  1.1  christos   *vspace = 'V';
   5067  1.1  christos   /* Record variable's number and value.  */
   5068  1.1  christos   memcpy (vspace + 1, &n, sizeof (n));
   5069  1.1  christos   val = get_trace_state_variable_value (n);
   5070  1.1  christos   memcpy (vspace + 1 + sizeof (n), &val, sizeof (val));
   5071  1.1  christos   trace_debug ("Variable %d recorded", n);
   5072  1.1  christos   return 0;
   5073  1.1  christos }
   5074  1.1  christos 
   5075  1.1  christos #ifndef IN_PROCESS_AGENT
   5076  1.1  christos 
   5077  1.1  christos /* Callback for traceframe_walk_blocks, used to find a given block
   5078  1.1  christos    type in a traceframe.  */
   5079  1.1  christos 
   5080  1.1  christos static int
   5081  1.1  christos match_blocktype (char blocktype, unsigned char *dataptr, void *data)
   5082  1.1  christos {
   5083  1.1  christos   char *wantedp = (char *) data;
   5084  1.1  christos 
   5085  1.1  christos   if (*wantedp == blocktype)
   5086  1.1  christos     return 1;
   5087  1.1  christos 
   5088  1.1  christos   return 0;
   5089  1.1  christos }
   5090  1.1  christos 
   5091  1.1  christos /* Walk over all traceframe blocks of the traceframe buffer starting
   5092  1.1  christos    at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
   5093  1.1  christos    block found, passing in DATA unmodified.  If CALLBACK returns true,
   5094  1.1  christos    this returns a pointer to where the block is found.  Returns NULL
   5095  1.1  christos    if no callback call returned true, indicating that all blocks have
   5096  1.1  christos    been walked.  */
   5097  1.1  christos 
   5098  1.1  christos static unsigned char *
   5099  1.1  christos traceframe_walk_blocks (unsigned char *database, unsigned int datasize,
   5100  1.1  christos 			int tfnum,
   5101  1.1  christos 			int (*callback) (char blocktype,
   5102  1.1  christos 					 unsigned char *dataptr,
   5103  1.1  christos 					 void *data),
   5104  1.1  christos 			void *data)
   5105  1.1  christos {
   5106  1.1  christos   unsigned char *dataptr;
   5107  1.1  christos 
   5108  1.1  christos   if (datasize == 0)
   5109  1.1  christos     {
   5110  1.1  christos       trace_debug ("traceframe %d has no data", tfnum);
   5111  1.1  christos       return NULL;
   5112  1.1  christos     }
   5113  1.1  christos 
   5114  1.1  christos   /* Iterate through a traceframe's blocks, looking for a block of the
   5115  1.1  christos      requested type.  */
   5116  1.1  christos   for (dataptr = database;
   5117  1.1  christos        dataptr < database + datasize;
   5118  1.1  christos        /* nothing */)
   5119  1.1  christos     {
   5120  1.1  christos       char blocktype;
   5121  1.1  christos       unsigned short mlen;
   5122  1.1  christos 
   5123  1.1  christos       if (dataptr == trace_buffer_wrap)
   5124  1.1  christos 	{
   5125  1.1  christos 	  /* Adjust to reflect wrapping part of the frame around to
   5126  1.1  christos 	     the beginning.  */
   5127  1.1  christos 	  datasize = dataptr - database;
   5128  1.1  christos 	  dataptr = database = trace_buffer_lo;
   5129  1.1  christos 	}
   5130  1.1  christos 
   5131  1.1  christos       blocktype = *dataptr++;
   5132  1.1  christos 
   5133  1.1  christos       if ((*callback) (blocktype, dataptr, data))
   5134  1.1  christos 	return dataptr;
   5135  1.1  christos 
   5136  1.1  christos       switch (blocktype)
   5137  1.1  christos 	{
   5138  1.1  christos 	case 'R':
   5139  1.1  christos 	  /* Skip over the registers block.  */
   5140  1.1  christos 	  dataptr += current_target_desc ()->registers_size;
   5141  1.1  christos 	  break;
   5142  1.1  christos 	case 'M':
   5143  1.1  christos 	  /* Skip over the memory block.  */
   5144  1.1  christos 	  dataptr += sizeof (CORE_ADDR);
   5145  1.1  christos 	  memcpy (&mlen, dataptr, sizeof (mlen));
   5146  1.1  christos 	  dataptr += (sizeof (mlen) + mlen);
   5147  1.1  christos 	  break;
   5148  1.1  christos 	case 'V':
   5149  1.1  christos 	  /* Skip over the TSV block.  */
   5150  1.1  christos 	  dataptr += (sizeof (int) + sizeof (LONGEST));
   5151  1.1  christos 	  break;
   5152  1.1  christos 	case 'S':
   5153  1.1  christos 	  /* Skip over the static trace data block.  */
   5154  1.1  christos 	  memcpy (&mlen, dataptr, sizeof (mlen));
   5155  1.1  christos 	  dataptr += (sizeof (mlen) + mlen);
   5156  1.1  christos 	  break;
   5157  1.1  christos 	default:
   5158  1.1  christos 	  trace_debug ("traceframe %d has unknown block type 0x%x",
   5159  1.1  christos 		       tfnum, blocktype);
   5160  1.1  christos 	  return NULL;
   5161  1.1  christos 	}
   5162  1.1  christos     }
   5163  1.1  christos 
   5164  1.1  christos   return NULL;
   5165  1.1  christos }
   5166  1.1  christos 
   5167  1.1  christos /* Look for the block of type TYPE_WANTED in the traceframe starting
   5168  1.1  christos    at DATABASE of DATASIZE bytes long.  TFNUM is the traceframe
   5169  1.1  christos    number.  */
   5170  1.1  christos 
   5171  1.1  christos static unsigned char *
   5172  1.1  christos traceframe_find_block_type (unsigned char *database, unsigned int datasize,
   5173  1.1  christos 			    int tfnum, char type_wanted)
   5174  1.1  christos {
   5175  1.1  christos   return traceframe_walk_blocks (database, datasize, tfnum,
   5176  1.1  christos 				 match_blocktype, &type_wanted);
   5177  1.1  christos }
   5178  1.1  christos 
   5179  1.1  christos static unsigned char *
   5180  1.1  christos traceframe_find_regblock (struct traceframe *tframe, int tfnum)
   5181  1.1  christos {
   5182  1.1  christos   unsigned char *regblock;
   5183  1.1  christos 
   5184  1.1  christos   regblock = traceframe_find_block_type (tframe->data,
   5185  1.1  christos 					 tframe->data_size,
   5186  1.1  christos 					 tfnum, 'R');
   5187  1.1  christos 
   5188  1.1  christos   if (regblock == NULL)
   5189  1.1  christos     trace_debug ("traceframe %d has no register data", tfnum);
   5190  1.1  christos 
   5191  1.1  christos   return regblock;
   5192  1.1  christos }
   5193  1.1  christos 
   5194  1.1  christos /* Get registers from a traceframe.  */
   5195  1.1  christos 
   5196  1.1  christos int
   5197  1.1  christos fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
   5198  1.1  christos {
   5199  1.1  christos   unsigned char *dataptr;
   5200  1.1  christos   struct tracepoint *tpoint;
   5201  1.1  christos   struct traceframe *tframe;
   5202  1.1  christos 
   5203  1.1  christos   tframe = find_traceframe (tfnum);
   5204  1.1  christos 
   5205  1.1  christos   if (tframe == NULL)
   5206  1.1  christos     {
   5207  1.1  christos       trace_debug ("traceframe %d not found", tfnum);
   5208  1.1  christos       return 1;
   5209  1.1  christos     }
   5210  1.1  christos 
   5211  1.1  christos   dataptr = traceframe_find_regblock (tframe, tfnum);
   5212  1.1  christos   if (dataptr == NULL)
   5213  1.1  christos     {
   5214  1.1  christos       /* Mark registers unavailable.  */
   5215  1.1  christos       supply_regblock (regcache, NULL);
   5216  1.1  christos 
   5217  1.1  christos       /* We can generally guess at a PC, although this will be
   5218  1.1  christos 	 misleading for while-stepping frames and multi-location
   5219  1.1  christos 	 tracepoints.  */
   5220  1.1  christos       tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
   5221  1.1  christos       if (tpoint != NULL)
   5222  1.1  christos 	regcache_write_pc (regcache, tpoint->address);
   5223  1.1  christos     }
   5224  1.1  christos   else
   5225  1.1  christos     supply_regblock (regcache, dataptr);
   5226  1.1  christos 
   5227  1.1  christos   return 0;
   5228  1.1  christos }
   5229  1.1  christos 
   5230  1.1  christos static CORE_ADDR
   5231  1.1  christos traceframe_get_pc (struct traceframe *tframe)
   5232  1.1  christos {
   5233  1.1  christos   struct regcache regcache;
   5234  1.1  christos   unsigned char *dataptr;
   5235  1.1  christos   const struct target_desc *tdesc = current_target_desc ();
   5236  1.1  christos 
   5237  1.1  christos   dataptr = traceframe_find_regblock (tframe, -1);
   5238  1.1  christos   if (dataptr == NULL)
   5239  1.1  christos     return 0;
   5240  1.1  christos 
   5241  1.1  christos   init_register_cache (&regcache, tdesc, dataptr);
   5242  1.1  christos   return regcache_read_pc (&regcache);
   5243  1.1  christos }
   5244  1.1  christos 
   5245  1.1  christos /* Read a requested block of memory from a trace frame.  */
   5246  1.1  christos 
   5247  1.1  christos int
   5248  1.1  christos traceframe_read_mem (int tfnum, CORE_ADDR addr,
   5249  1.1  christos 		     unsigned char *buf, ULONGEST length,
   5250  1.1  christos 		     ULONGEST *nbytes)
   5251  1.1  christos {
   5252  1.1  christos   struct traceframe *tframe;
   5253  1.1  christos   unsigned char *database, *dataptr;
   5254  1.1  christos   unsigned int datasize;
   5255  1.1  christos   CORE_ADDR maddr;
   5256  1.1  christos   unsigned short mlen;
   5257  1.1  christos 
   5258  1.1  christos   trace_debug ("traceframe_read_mem");
   5259  1.1  christos 
   5260  1.1  christos   tframe = find_traceframe (tfnum);
   5261  1.1  christos 
   5262  1.1  christos   if (!tframe)
   5263  1.1  christos     {
   5264  1.1  christos       trace_debug ("traceframe %d not found", tfnum);
   5265  1.1  christos       return 1;
   5266  1.1  christos     }
   5267  1.1  christos 
   5268  1.1  christos   datasize = tframe->data_size;
   5269  1.1  christos   database = dataptr = &tframe->data[0];
   5270  1.1  christos 
   5271  1.1  christos   /* Iterate through a traceframe's blocks, looking for memory.  */
   5272  1.1  christos   while ((dataptr = traceframe_find_block_type (dataptr,
   5273  1.1  christos 						datasize
   5274  1.1  christos 						- (dataptr - database),
   5275  1.1  christos 						tfnum, 'M')) != NULL)
   5276  1.1  christos     {
   5277  1.1  christos       memcpy (&maddr, dataptr, sizeof (maddr));
   5278  1.1  christos       dataptr += sizeof (maddr);
   5279  1.1  christos       memcpy (&mlen, dataptr, sizeof (mlen));
   5280  1.1  christos       dataptr += sizeof (mlen);
   5281  1.1  christos       trace_debug ("traceframe %d has %d bytes at %s",
   5282  1.1  christos 		   tfnum, mlen, paddress (maddr));
   5283  1.1  christos 
   5284  1.1  christos       /* If the block includes the first part of the desired range,
   5285  1.1  christos 	 return as much it has; GDB will re-request the remainder,
   5286  1.1  christos 	 which might be in a different block of this trace frame.  */
   5287  1.1  christos       if (maddr <= addr && addr < (maddr + mlen))
   5288  1.1  christos 	{
   5289  1.1  christos 	  ULONGEST amt = (maddr + mlen) - addr;
   5290  1.1  christos 	  if (amt > length)
   5291  1.1  christos 	    amt = length;
   5292  1.1  christos 
   5293  1.1  christos 	  memcpy (buf, dataptr + (addr - maddr), amt);
   5294  1.1  christos 	  *nbytes = amt;
   5295  1.1  christos 	  return 0;
   5296  1.1  christos 	}
   5297  1.1  christos 
   5298  1.1  christos       /* Skip over this block.  */
   5299  1.1  christos       dataptr += mlen;
   5300  1.1  christos     }
   5301  1.1  christos 
   5302  1.1  christos   trace_debug ("traceframe %d has no memory data for the desired region",
   5303  1.1  christos 	       tfnum);
   5304  1.1  christos 
   5305  1.1  christos   *nbytes = 0;
   5306  1.1  christos   return 0;
   5307  1.1  christos }
   5308  1.1  christos 
   5309  1.1  christos static int
   5310  1.1  christos traceframe_read_tsv (int tsvnum, LONGEST *val)
   5311  1.1  christos {
   5312  1.1  christos   client_state &cs = get_client_state ();
   5313  1.1  christos   int tfnum;
   5314  1.1  christos   struct traceframe *tframe;
   5315  1.1  christos   unsigned char *database, *dataptr;
   5316  1.1  christos   unsigned int datasize;
   5317  1.1  christos   int vnum;
   5318  1.1  christos   int found = 0;
   5319  1.1  christos 
   5320  1.1  christos   trace_debug ("traceframe_read_tsv");
   5321  1.1  christos 
   5322  1.1  christos   tfnum = cs.current_traceframe;
   5323  1.1  christos 
   5324  1.1  christos   if (tfnum < 0)
   5325  1.1  christos     {
   5326  1.1  christos       trace_debug ("no current traceframe");
   5327  1.1  christos       return 1;
   5328  1.1  christos     }
   5329  1.1  christos 
   5330  1.1  christos   tframe = find_traceframe (tfnum);
   5331  1.1  christos 
   5332  1.1  christos   if (tframe == NULL)
   5333  1.1  christos     {
   5334  1.1  christos       trace_debug ("traceframe %d not found", tfnum);
   5335  1.1  christos       return 1;
   5336  1.1  christos     }
   5337  1.1  christos 
   5338  1.1  christos   datasize = tframe->data_size;
   5339  1.1  christos   database = dataptr = &tframe->data[0];
   5340  1.1  christos 
   5341  1.1  christos   /* Iterate through a traceframe's blocks, looking for the last
   5342  1.1  christos      matched tsv.  */
   5343  1.1  christos   while ((dataptr = traceframe_find_block_type (dataptr,
   5344  1.1  christos 						datasize
   5345  1.1  christos 						- (dataptr - database),
   5346  1.1  christos 						tfnum, 'V')) != NULL)
   5347  1.1  christos     {
   5348  1.1  christos       memcpy (&vnum, dataptr, sizeof (vnum));
   5349  1.1  christos       dataptr += sizeof (vnum);
   5350  1.1  christos 
   5351  1.1  christos       trace_debug ("traceframe %d has variable %d", tfnum, vnum);
   5352  1.1  christos 
   5353  1.1  christos       /* Check that this is the variable we want.  */
   5354  1.1  christos       if (tsvnum == vnum)
   5355  1.1  christos 	{
   5356  1.1  christos 	  memcpy (val, dataptr, sizeof (*val));
   5357  1.1  christos 	  found = 1;
   5358  1.1  christos 	}
   5359  1.1  christos 
   5360  1.1  christos       /* Skip over this block.  */
   5361  1.1  christos       dataptr += sizeof (LONGEST);
   5362  1.1  christos     }
   5363  1.1  christos 
   5364  1.1  christos   if (!found)
   5365  1.1  christos     trace_debug ("traceframe %d has no data for variable %d",
   5366  1.1  christos 		 tfnum, tsvnum);
   5367  1.1  christos   return !found;
   5368  1.1  christos }
   5369  1.1  christos 
   5370  1.1  christos /* Read a requested block of static tracepoint data from a trace
   5371  1.1  christos    frame.  */
   5372  1.1  christos 
   5373  1.1  christos int
   5374  1.1  christos traceframe_read_sdata (int tfnum, ULONGEST offset,
   5375  1.1  christos 		       unsigned char *buf, ULONGEST length,
   5376  1.1  christos 		       ULONGEST *nbytes)
   5377  1.1  christos {
   5378  1.1  christos   struct traceframe *tframe;
   5379  1.1  christos   unsigned char *database, *dataptr;
   5380  1.1  christos   unsigned int datasize;
   5381  1.1  christos   unsigned short mlen;
   5382  1.1  christos 
   5383  1.1  christos   trace_debug ("traceframe_read_sdata");
   5384  1.1  christos 
   5385  1.1  christos   tframe = find_traceframe (tfnum);
   5386  1.1  christos 
   5387  1.1  christos   if (!tframe)
   5388  1.1  christos     {
   5389  1.1  christos       trace_debug ("traceframe %d not found", tfnum);
   5390  1.1  christos       return 1;
   5391  1.1  christos     }
   5392  1.1  christos 
   5393  1.1  christos   datasize = tframe->data_size;
   5394  1.1  christos   database = &tframe->data[0];
   5395  1.1  christos 
   5396  1.1  christos   /* Iterate through a traceframe's blocks, looking for static
   5397  1.1  christos      tracepoint data.  */
   5398  1.1  christos   dataptr = traceframe_find_block_type (database, datasize,
   5399  1.1  christos 					tfnum, 'S');
   5400  1.1  christos   if (dataptr != NULL)
   5401  1.1  christos     {
   5402  1.1  christos       memcpy (&mlen, dataptr, sizeof (mlen));
   5403  1.1  christos       dataptr += sizeof (mlen);
   5404  1.1  christos       if (offset < mlen)
   5405  1.1  christos 	{
   5406  1.1  christos 	  if (offset + length > mlen)
   5407  1.1  christos 	    length = mlen - offset;
   5408  1.1  christos 
   5409  1.1  christos 	  memcpy (buf, dataptr, length);
   5410  1.1  christos 	  *nbytes = length;
   5411  1.1  christos 	}
   5412  1.1  christos       else
   5413  1.1  christos 	*nbytes = 0;
   5414  1.1  christos       return 0;
   5415  1.1  christos     }
   5416  1.1  christos 
   5417  1.1  christos   trace_debug ("traceframe %d has no static trace data", tfnum);
   5418  1.1  christos 
   5419  1.1  christos   *nbytes = 0;
   5420  1.1  christos   return 0;
   5421  1.1  christos }
   5422  1.1  christos 
   5423  1.1  christos /* Callback for traceframe_walk_blocks.  Builds a traceframe-info
   5424  1.1  christos    object.  DATA is pointer to a struct buffer holding the
   5425  1.1  christos    traceframe-info object being built.  */
   5426  1.1  christos 
   5427  1.1  christos static int
   5428  1.1  christos build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
   5429  1.1  christos {
   5430  1.1  christos   struct buffer *buffer = (struct buffer *) data;
   5431  1.1  christos 
   5432  1.1  christos   switch (blocktype)
   5433  1.1  christos     {
   5434  1.1  christos     case 'M':
   5435  1.1  christos       {
   5436  1.1  christos 	unsigned short mlen;
   5437  1.1  christos 	CORE_ADDR maddr;
   5438  1.1  christos 
   5439  1.1  christos 	memcpy (&maddr, dataptr, sizeof (maddr));
   5440  1.1  christos 	dataptr += sizeof (maddr);
   5441  1.1  christos 	memcpy (&mlen, dataptr, sizeof (mlen));
   5442  1.1  christos 	dataptr += sizeof (mlen);
   5443  1.1  christos 	buffer_xml_printf (buffer,
   5444  1.1  christos 			   "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
   5445  1.1  christos 			   paddress (maddr), phex_nz (mlen, sizeof (mlen)));
   5446  1.1  christos 	break;
   5447  1.1  christos       }
   5448  1.1  christos     case 'V':
   5449  1.1  christos       {
   5450  1.1  christos 	int vnum;
   5451  1.1  christos 
   5452  1.1  christos 	memcpy (&vnum, dataptr, sizeof (vnum));
   5453  1.1  christos 	buffer_xml_printf (buffer, "<tvar id=\"%d\"/>\n", vnum);
   5454  1.1  christos 	break;
   5455  1.1  christos       }
   5456  1.1  christos     case 'R':
   5457  1.1  christos     case 'S':
   5458  1.1  christos       {
   5459  1.1  christos 	break;
   5460  1.1  christos       }
   5461  1.1  christos     default:
   5462  1.1  christos       warning ("Unhandled trace block type (%d) '%c ' "
   5463  1.1  christos 	       "while building trace frame info.",
   5464  1.1  christos 	       blocktype, blocktype);
   5465  1.1  christos       break;
   5466  1.1  christos     }
   5467  1.1  christos 
   5468  1.1  christos   return 0;
   5469  1.1  christos }
   5470  1.1  christos 
   5471  1.1  christos /* Build a traceframe-info object for traceframe number TFNUM into
   5472  1.1  christos    BUFFER.  */
   5473  1.1  christos 
   5474  1.1  christos int
   5475  1.1  christos traceframe_read_info (int tfnum, struct buffer *buffer)
   5476  1.1  christos {
   5477  1.1  christos   struct traceframe *tframe;
   5478  1.1  christos 
   5479  1.1  christos   trace_debug ("traceframe_read_info");
   5480  1.1  christos 
   5481  1.1  christos   tframe = find_traceframe (tfnum);
   5482  1.1  christos 
   5483  1.1  christos   if (!tframe)
   5484  1.1  christos     {
   5485  1.1  christos       trace_debug ("traceframe %d not found", tfnum);
   5486  1.1  christos       return 1;
   5487  1.1  christos     }
   5488  1.1  christos 
   5489  1.1  christos   buffer_grow_str (buffer, "<traceframe-info>\n");
   5490  1.1  christos   traceframe_walk_blocks (tframe->data, tframe->data_size,
   5491  1.1  christos 			  tfnum, build_traceframe_info_xml, buffer);
   5492  1.1  christos   buffer_grow_str0 (buffer, "</traceframe-info>\n");
   5493  1.1  christos   return 0;
   5494  1.1  christos }
   5495  1.1  christos 
   5496  1.1  christos /* Return the first fast tracepoint whose jump pad contains PC.  */
   5497  1.1  christos 
   5498  1.1  christos static struct tracepoint *
   5499  1.1  christos fast_tracepoint_from_jump_pad_address (CORE_ADDR pc)
   5500  1.1  christos {
   5501  1.1  christos   struct tracepoint *tpoint;
   5502  1.1  christos 
   5503  1.1  christos   for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
   5504  1.1  christos     if (tpoint->type == fast_tracepoint)
   5505  1.1  christos       if (tpoint->jump_pad <= pc && pc < tpoint->jump_pad_end)
   5506  1.1  christos 	return tpoint;
   5507  1.1  christos 
   5508  1.1  christos   return NULL;
   5509  1.1  christos }
   5510  1.1  christos 
   5511  1.1  christos /* Return the first fast tracepoint whose trampoline contains PC.  */
   5512  1.1  christos 
   5513  1.1  christos static struct tracepoint *
   5514  1.1  christos fast_tracepoint_from_trampoline_address (CORE_ADDR pc)
   5515  1.1  christos {
   5516  1.1  christos   struct tracepoint *tpoint;
   5517  1.1  christos 
   5518  1.1  christos   for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
   5519  1.1  christos     {
   5520  1.1  christos       if (tpoint->type == fast_tracepoint
   5521  1.1  christos 	  && tpoint->trampoline <= pc && pc < tpoint->trampoline_end)
   5522  1.1  christos 	return tpoint;
   5523  1.1  christos     }
   5524  1.1  christos 
   5525  1.1  christos   return NULL;
   5526  1.1  christos }
   5527  1.1  christos 
   5528  1.1  christos /* Return GDBserver's tracepoint that matches the IP Agent's
   5529  1.1  christos    tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
   5530  1.1  christos    address space.  */
   5531  1.1  christos 
   5532  1.1  christos static struct tracepoint *
   5533  1.1  christos fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj)
   5534  1.1  christos {
   5535  1.1  christos   struct tracepoint *tpoint;
   5536  1.1  christos 
   5537  1.1  christos   for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
   5538  1.1  christos     if (tpoint->type == fast_tracepoint)
   5539  1.1  christos       if (tpoint->obj_addr_on_target == ipa_tpoint_obj)
   5540  1.1  christos 	return tpoint;
   5541  1.1  christos 
   5542  1.1  christos   return NULL;
   5543  1.1  christos }
   5544  1.1  christos 
   5545  1.1  christos #endif
   5546  1.1  christos 
   5547  1.1  christos /* The type of the object that is used to synchronize fast tracepoint
   5548  1.1  christos    collection.  */
   5549  1.1  christos 
   5550  1.1  christos typedef struct collecting_t
   5551  1.1  christos {
   5552  1.1  christos   /* The fast tracepoint number currently collecting.  */
   5553  1.1  christos   uintptr_t tpoint;
   5554  1.1  christos 
   5555  1.1  christos   /* A number that GDBserver can use to identify the thread that is
   5556  1.1  christos      presently holding the collect lock.  This need not (and usually
   5557  1.1  christos      is not) the thread id, as getting the current thread ID usually
   5558  1.1  christos      requires a system call, which we want to avoid like the plague.
   5559  1.1  christos      Usually this is thread's TCB, found in the TLS (pseudo-)
   5560  1.1  christos      register, which is readable with a single insn on several
   5561  1.1  christos      architectures.  */
   5562  1.1  christos   uintptr_t thread_area;
   5563  1.1  christos } collecting_t;
   5564  1.1  christos 
   5565  1.1  christos #ifndef IN_PROCESS_AGENT
   5566  1.1  christos 
   5567  1.1  christos void
   5568  1.1  christos force_unlock_trace_buffer (void)
   5569  1.1  christos {
   5570  1.1  christos   write_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 0);
   5571  1.1  christos }
   5572  1.1  christos 
   5573  1.1  christos /* Check if the thread identified by THREAD_AREA which is stopped at
   5574  1.1  christos    STOP_PC, is presently locking the fast tracepoint collection, and
   5575  1.1  christos    if so, gather some status of said collection.  Returns 0 if the
   5576  1.1  christos    thread isn't collecting or in the jump pad at all.  1, if in the
   5577  1.1  christos    jump pad (or within gdb_collect) and hasn't executed the adjusted
   5578  1.1  christos    original insn yet (can set a breakpoint there and run to it).  2,
   5579  1.1  christos    if presently executing the adjusted original insn --- in which
   5580  1.1  christos    case, if we want to move the thread out of the jump pad, we need to
   5581  1.1  christos    single-step it until this function returns 0.  */
   5582  1.1  christos 
   5583  1.1  christos fast_tpoint_collect_result
   5584  1.1  christos fast_tracepoint_collecting (CORE_ADDR thread_area,
   5585  1.1  christos 			    CORE_ADDR stop_pc,
   5586  1.1  christos 			    struct fast_tpoint_collect_status *status)
   5587  1.1  christos {
   5588  1.1  christos   CORE_ADDR ipa_collecting;
   5589  1.1  christos   CORE_ADDR ipa_gdb_jump_pad_buffer, ipa_gdb_jump_pad_buffer_end;
   5590  1.1  christos   CORE_ADDR ipa_gdb_trampoline_buffer;
   5591  1.1  christos   CORE_ADDR ipa_gdb_trampoline_buffer_end;
   5592  1.1  christos   struct tracepoint *tpoint;
   5593  1.1  christos   int needs_breakpoint;
   5594  1.1  christos 
   5595  1.1  christos   /* The thread THREAD_AREA is either:
   5596  1.1  christos 
   5597  1.1  christos       0. not collecting at all, not within the jump pad, or within
   5598  1.1  christos 	 gdb_collect or one of its callees.
   5599  1.1  christos 
   5600  1.1  christos       1. in the jump pad and haven't reached gdb_collect
   5601  1.1  christos 
   5602  1.1  christos       2. within gdb_collect (out of the jump pad) (collect is set)
   5603  1.1  christos 
   5604  1.1  christos       3. we're in the jump pad, after gdb_collect having returned,
   5605  1.1  christos 	 possibly executing the adjusted insns.
   5606  1.1  christos 
   5607  1.1  christos       For cases 1 and 3, `collecting' may or not be set.  The jump pad
   5608  1.1  christos       doesn't have any complicated jump logic, so we can tell if the
   5609  1.1  christos       thread is executing the adjust original insn or not by just
   5610  1.1  christos       matching STOP_PC with known jump pad addresses.  If we it isn't
   5611  1.1  christos       yet executing the original insn, set a breakpoint there, and let
   5612  1.1  christos       the thread run to it, so to quickly step over a possible (many
   5613  1.1  christos       insns) gdb_collect call.  Otherwise, or when the breakpoint is
   5614  1.1  christos       hit, only a few (small number of) insns are left to be executed
   5615  1.1  christos       in the jump pad.  Single-step the thread until it leaves the
   5616  1.1  christos       jump pad.  */
   5617  1.1  christos 
   5618  1.1  christos  again:
   5619  1.1  christos   tpoint = NULL;
   5620  1.1  christos   needs_breakpoint = 0;
   5621  1.1  christos   trace_debug ("fast_tracepoint_collecting");
   5622  1.1  christos 
   5623  1.1  christos   if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
   5624  1.1  christos 				  &ipa_gdb_jump_pad_buffer))
   5625  1.1  christos     {
   5626  1.1  christos       internal_error (__FILE__, __LINE__,
   5627  1.1  christos 		      "error extracting `gdb_jump_pad_buffer'");
   5628  1.1  christos     }
   5629  1.1  christos   if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer_end,
   5630  1.1  christos 				  &ipa_gdb_jump_pad_buffer_end))
   5631  1.1  christos     {
   5632  1.1  christos       internal_error (__FILE__, __LINE__,
   5633  1.1  christos 		      "error extracting `gdb_jump_pad_buffer_end'");
   5634  1.1  christos     }
   5635  1.1  christos 
   5636  1.1  christos   if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
   5637  1.1  christos 				  &ipa_gdb_trampoline_buffer))
   5638  1.1  christos     {
   5639  1.1  christos       internal_error (__FILE__, __LINE__,
   5640  1.1  christos 		      "error extracting `gdb_trampoline_buffer'");
   5641  1.1  christos     }
   5642  1.1  christos   if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
   5643  1.1  christos 				  &ipa_gdb_trampoline_buffer_end))
   5644  1.1  christos     {
   5645  1.1  christos       internal_error (__FILE__, __LINE__,
   5646  1.1  christos 		      "error extracting `gdb_trampoline_buffer_end'");
   5647  1.1  christos     }
   5648  1.1  christos 
   5649  1.1  christos   if (ipa_gdb_jump_pad_buffer <= stop_pc
   5650  1.1  christos       && stop_pc < ipa_gdb_jump_pad_buffer_end)
   5651  1.1  christos     {
   5652  1.1  christos       /* We can tell which tracepoint(s) the thread is collecting by
   5653  1.1  christos 	 matching the jump pad address back to the tracepoint.  */
   5654  1.1  christos       tpoint = fast_tracepoint_from_jump_pad_address (stop_pc);
   5655  1.1  christos       if (tpoint == NULL)
   5656  1.1  christos 	{
   5657  1.1  christos 	  warning ("in jump pad, but no matching tpoint?");
   5658  1.1  christos 	  return fast_tpoint_collect_result::not_collecting;
   5659  1.1  christos 	}
   5660  1.1  christos       else
   5661  1.1  christos 	{
   5662  1.1  christos 	  trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
   5663  1.1  christos 		       "adj_insn(%s, %s)",
   5664  1.1  christos 		       tpoint->number, paddress (tpoint->address),
   5665  1.1  christos 		       paddress (tpoint->jump_pad),
   5666  1.1  christos 		       paddress (tpoint->jump_pad_end),
   5667  1.1  christos 		       paddress (tpoint->adjusted_insn_addr),
   5668  1.1  christos 		       paddress (tpoint->adjusted_insn_addr_end));
   5669  1.1  christos 	}
   5670  1.1  christos 
   5671  1.1  christos       /* Definitely in the jump pad.  May or may not need
   5672  1.1  christos 	 fast-exit-jump-pad breakpoint.  */
   5673  1.1  christos       if (tpoint->jump_pad <= stop_pc
   5674  1.1  christos 	  && stop_pc < tpoint->adjusted_insn_addr)
   5675  1.1  christos 	needs_breakpoint =  1;
   5676  1.1  christos     }
   5677  1.1  christos   else if (ipa_gdb_trampoline_buffer <= stop_pc
   5678  1.1  christos 	   && stop_pc < ipa_gdb_trampoline_buffer_end)
   5679  1.1  christos     {
   5680  1.1  christos       /* We can tell which tracepoint(s) the thread is collecting by
   5681  1.1  christos 	 matching the trampoline address back to the tracepoint.  */
   5682  1.1  christos       tpoint = fast_tracepoint_from_trampoline_address (stop_pc);
   5683  1.1  christos       if (tpoint == NULL)
   5684  1.1  christos 	{
   5685  1.1  christos 	  warning ("in trampoline, but no matching tpoint?");
   5686  1.1  christos 	  return fast_tpoint_collect_result::not_collecting;
   5687  1.1  christos 	}
   5688  1.1  christos       else
   5689  1.1  christos 	{
   5690  1.1  christos 	  trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
   5691  1.1  christos 		       tpoint->number, paddress (tpoint->address),
   5692  1.1  christos 		       paddress (tpoint->trampoline),
   5693  1.1  christos 		       paddress (tpoint->trampoline_end));
   5694  1.1  christos 	}
   5695  1.1  christos 
   5696  1.1  christos       /* Have not reached jump pad yet, but treat the trampoline as a
   5697  1.1  christos 	 part of the jump pad that is before the adjusted original
   5698  1.1  christos 	 instruction.  */
   5699  1.1  christos       needs_breakpoint = 1;
   5700  1.1  christos     }
   5701  1.1  christos   else
   5702  1.1  christos     {
   5703  1.1  christos       collecting_t ipa_collecting_obj;
   5704  1.1  christos 
   5705  1.1  christos       /* If `collecting' is set/locked, then the THREAD_AREA thread
   5706  1.1  christos 	 may or not be the one holding the lock.  We have to read the
   5707  1.1  christos 	 lock to find out.  */
   5708  1.1  christos 
   5709  1.1  christos       if (read_inferior_data_pointer (ipa_sym_addrs.addr_collecting,
   5710  1.1  christos 				      &ipa_collecting))
   5711  1.1  christos 	{
   5712  1.1  christos 	  trace_debug ("fast_tracepoint_collecting:"
   5713  1.1  christos 		       " failed reading 'collecting' in the inferior");
   5714  1.1  christos 	  return fast_tpoint_collect_result::not_collecting;
   5715  1.1  christos 	}
   5716  1.1  christos 
   5717  1.1  christos       if (!ipa_collecting)
   5718  1.1  christos 	{
   5719  1.1  christos 	  trace_debug ("fast_tracepoint_collecting: not collecting"
   5720  1.1  christos 		       " (and nobody is).");
   5721  1.1  christos 	  return fast_tpoint_collect_result::not_collecting;
   5722  1.1  christos 	}
   5723  1.1  christos 
   5724  1.1  christos       /* Some thread is collecting.  Check which.  */
   5725  1.1  christos       if (read_inferior_memory (ipa_collecting,
   5726  1.1  christos 				(unsigned char *) &ipa_collecting_obj,
   5727  1.1  christos 				sizeof (ipa_collecting_obj)) != 0)
   5728  1.1  christos 	goto again;
   5729  1.1  christos 
   5730  1.1  christos       if (ipa_collecting_obj.thread_area != thread_area)
   5731  1.1  christos 	{
   5732  1.1  christos 	  trace_debug ("fast_tracepoint_collecting: not collecting "
   5733  1.1  christos 		       "(another thread is)");
   5734  1.1  christos 	  return fast_tpoint_collect_result::not_collecting;
   5735  1.1  christos 	}
   5736  1.1  christos 
   5737  1.1  christos       tpoint
   5738  1.1  christos 	= fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj.tpoint);
   5739  1.1  christos       if (tpoint == NULL)
   5740  1.1  christos 	{
   5741  1.1  christos 	  warning ("fast_tracepoint_collecting: collecting, "
   5742  1.1  christos 		   "but tpoint %s not found?",
   5743  1.1  christos 		   paddress ((CORE_ADDR) ipa_collecting_obj.tpoint));
   5744  1.1  christos 	  return fast_tpoint_collect_result::not_collecting;
   5745  1.1  christos 	}
   5746  1.1  christos 
   5747  1.1  christos       /* The thread is within `gdb_collect', skip over the rest of
   5748  1.1  christos 	 fast tracepoint collection quickly using a breakpoint.  */
   5749  1.1  christos       needs_breakpoint = 1;
   5750  1.1  christos     }
   5751  1.1  christos 
   5752  1.1  christos   /* The caller wants a bit of status detail.  */
   5753  1.1  christos   if (status != NULL)
   5754  1.1  christos     {
   5755  1.1  christos       status->tpoint_num = tpoint->number;
   5756  1.1  christos       status->tpoint_addr = tpoint->address;
   5757  1.1  christos       status->adjusted_insn_addr = tpoint->adjusted_insn_addr;
   5758  1.1  christos       status->adjusted_insn_addr_end = tpoint->adjusted_insn_addr_end;
   5759  1.1  christos     }
   5760  1.1  christos 
   5761  1.1  christos   if (needs_breakpoint)
   5762  1.1  christos     {
   5763  1.1  christos       /* Hasn't executed the original instruction yet.  Set breakpoint
   5764  1.1  christos 	 there, and wait till it's hit, then single-step until exiting
   5765  1.1  christos 	 the jump pad.  */
   5766  1.1  christos 
   5767  1.1  christos       trace_debug ("\
   5768  1.1  christos fast_tracepoint_collecting, returning continue-until-break at %s",
   5769  1.1  christos 		   paddress (tpoint->adjusted_insn_addr));
   5770  1.1  christos 
   5771  1.1  christos       return fast_tpoint_collect_result::before_insn; /* continue */
   5772  1.1  christos     }
   5773  1.1  christos   else
   5774  1.1  christos     {
   5775  1.1  christos       /* Just single-step until exiting the jump pad.  */
   5776  1.1  christos 
   5777  1.1  christos       trace_debug ("fast_tracepoint_collecting, returning "
   5778  1.1  christos 		   "need-single-step (%s-%s)",
   5779  1.1  christos 		   paddress (tpoint->adjusted_insn_addr),
   5780  1.1  christos 		   paddress (tpoint->adjusted_insn_addr_end));
   5781  1.1  christos 
   5782  1.1  christos       return fast_tpoint_collect_result::at_insn; /* single-step */
   5783  1.1  christos     }
   5784  1.1  christos }
   5785  1.1  christos 
   5786  1.1  christos #endif
   5787  1.1  christos 
   5788  1.1  christos #ifdef IN_PROCESS_AGENT
   5789  1.1  christos 
   5790  1.1  christos /* The global fast tracepoint collect lock.  Points to a collecting_t
   5791  1.1  christos    object built on the stack by the jump pad, if presently locked;
   5792  1.1  christos    NULL if it isn't locked.  Note that this lock *must* be set while
   5793  1.1  christos    executing any *function other than the jump pad.  See
   5794  1.1  christos    fast_tracepoint_collecting.  */
   5795  1.1  christos EXTERN_C_PUSH
   5796  1.1  christos IP_AGENT_EXPORT_VAR collecting_t *collecting;
   5797  1.1  christos EXTERN_C_POP
   5798  1.1  christos 
   5799  1.1  christos /* This is needed for -Wmissing-declarations.  */
   5800  1.1  christos IP_AGENT_EXPORT_FUNC void gdb_collect (struct tracepoint *tpoint,
   5801  1.1  christos 				       unsigned char *regs);
   5802  1.1  christos 
   5803  1.1  christos /* This routine, called from the jump pad (in asm) is designed to be
   5804  1.1  christos    called from the jump pads of fast tracepoints, thus it is on the
   5805  1.1  christos    critical path.  */
   5806  1.1  christos 
   5807  1.1  christos IP_AGENT_EXPORT_FUNC void
   5808  1.1  christos gdb_collect (struct tracepoint *tpoint, unsigned char *regs)
   5809  1.1  christos {
   5810  1.1  christos   struct fast_tracepoint_ctx ctx;
   5811  1.1  christos   const struct target_desc *ipa_tdesc;
   5812  1.1  christos 
   5813  1.1  christos   /* Don't do anything until the trace run is completely set up.  */
   5814  1.1  christos   if (!tracing)
   5815  1.1  christos     return;
   5816  1.1  christos 
   5817  1.1  christos   ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx);
   5818  1.1  christos   ctx.base.type = fast_tracepoint;
   5819  1.1  christos   ctx.regs = regs;
   5820  1.1  christos   ctx.regcache_initted = 0;
   5821  1.1  christos   /* Wrap the regblock in a register cache (in the stack, we don't
   5822  1.1  christos      want to malloc here).  */
   5823  1.1  christos   ctx.regspace = (unsigned char *) alloca (ipa_tdesc->registers_size);
   5824  1.1  christos   if (ctx.regspace == NULL)
   5825  1.1  christos     {
   5826  1.1  christos       trace_debug ("Trace buffer block allocation failed, skipping");
   5827  1.1  christos       return;
   5828  1.1  christos     }
   5829  1.1  christos 
   5830  1.1  christos   for (ctx.tpoint = tpoint;
   5831  1.1  christos        ctx.tpoint != NULL && ctx.tpoint->address == tpoint->address;
   5832  1.1  christos        ctx.tpoint = ctx.tpoint->next)
   5833  1.1  christos     {
   5834  1.1  christos       if (!ctx.tpoint->enabled)
   5835  1.1  christos 	continue;
   5836  1.1  christos 
   5837  1.1  christos       /* Multiple tracepoints of different types, such as fast tracepoint and
   5838  1.1  christos 	 static tracepoint, can be set at the same address.  */
   5839  1.1  christos       if (ctx.tpoint->type != tpoint->type)
   5840  1.1  christos 	continue;
   5841  1.1  christos 
   5842  1.1  christos       /* Test the condition if present, and collect if true.  */
   5843  1.1  christos       if (ctx.tpoint->cond == NULL
   5844  1.1  christos 	  || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
   5845  1.1  christos 					   ctx.tpoint))
   5846  1.1  christos 	{
   5847  1.1  christos 	  collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
   5848  1.1  christos 				      ctx.tpoint->address, ctx.tpoint);
   5849  1.1  christos 
   5850  1.1  christos 	  /* Note that this will cause original insns to be written back
   5851  1.1  christos 	     to where we jumped from, but that's OK because we're jumping
   5852  1.1  christos 	     back to the next whole instruction.  This will go badly if
   5853  1.1  christos 	     instruction restoration is not atomic though.  */
   5854  1.1  christos 	  if (stopping_tracepoint
   5855  1.1  christos 	      || trace_buffer_is_full
   5856  1.1  christos 	      || expr_eval_result != expr_eval_no_error)
   5857  1.1  christos 	    {
   5858  1.1  christos 	      stop_tracing ();
   5859  1.1  christos 	      break;
   5860  1.1  christos 	    }
   5861  1.1  christos 	}
   5862  1.1  christos       else
   5863  1.1  christos 	{
   5864  1.1  christos 	  /* If there was a condition and it evaluated to false, the only
   5865  1.1  christos 	     way we would stop tracing is if there was an error during
   5866  1.1  christos 	     condition expression evaluation.  */
   5867  1.1  christos 	  if (expr_eval_result != expr_eval_no_error)
   5868  1.1  christos 	    {
   5869  1.1  christos 	      stop_tracing ();
   5870  1.1  christos 	      break;
   5871  1.1  christos 	    }
   5872  1.1  christos 	}
   5873  1.1  christos     }
   5874  1.1  christos }
   5875  1.1  christos 
   5876  1.1  christos /* These global variables points to the corresponding functions.  This is
   5877  1.1  christos    necessary on powerpc64, where asking for function symbol address from gdb
   5878  1.1  christos    results in returning the actual code pointer, instead of the descriptor
   5879  1.1  christos    pointer.  */
   5880  1.1  christos 
   5881  1.1  christos typedef void (*gdb_collect_ptr_type) (struct tracepoint *, unsigned char *);
   5882  1.1  christos typedef ULONGEST (*get_raw_reg_ptr_type) (const unsigned char *, int);
   5883  1.1  christos typedef LONGEST (*get_trace_state_variable_value_ptr_type) (int);
   5884  1.1  christos typedef void (*set_trace_state_variable_value_ptr_type) (int, LONGEST);
   5885  1.1  christos 
   5886  1.1  christos EXTERN_C_PUSH
   5887  1.1  christos IP_AGENT_EXPORT_VAR gdb_collect_ptr_type gdb_collect_ptr = gdb_collect;
   5888  1.1  christos IP_AGENT_EXPORT_VAR get_raw_reg_ptr_type get_raw_reg_ptr = get_raw_reg;
   5889  1.1  christos IP_AGENT_EXPORT_VAR get_trace_state_variable_value_ptr_type
   5890  1.1  christos   get_trace_state_variable_value_ptr = get_trace_state_variable_value;
   5891  1.1  christos IP_AGENT_EXPORT_VAR set_trace_state_variable_value_ptr_type
   5892  1.1  christos   set_trace_state_variable_value_ptr = set_trace_state_variable_value;
   5893  1.1  christos EXTERN_C_POP
   5894  1.1  christos 
   5895  1.1  christos #endif
   5896  1.1  christos 
   5897  1.1  christos #ifndef IN_PROCESS_AGENT
   5898  1.1  christos 
   5899  1.1  christos CORE_ADDR
   5900  1.1  christos get_raw_reg_func_addr (void)
   5901  1.1  christos {
   5902  1.1  christos   CORE_ADDR res;
   5903  1.1  christos   if (read_inferior_data_pointer (ipa_sym_addrs.addr_get_raw_reg_ptr, &res))
   5904  1.1  christos     {
   5905  1.1  christos       error ("error extracting get_raw_reg_ptr");
   5906  1.1  christos       return 0;
   5907  1.1  christos     }
   5908  1.1  christos   return res;
   5909  1.1  christos }
   5910  1.1  christos 
   5911  1.1  christos CORE_ADDR
   5912  1.1  christos get_get_tsv_func_addr (void)
   5913  1.1  christos {
   5914  1.1  christos   CORE_ADDR res;
   5915  1.1  christos   if (read_inferior_data_pointer (
   5916  1.1  christos 	ipa_sym_addrs.addr_get_trace_state_variable_value_ptr, &res))
   5917  1.1  christos     {
   5918  1.1  christos       error ("error extracting get_trace_state_variable_value_ptr");
   5919  1.1  christos       return 0;
   5920  1.1  christos     }
   5921  1.1  christos   return res;
   5922  1.1  christos }
   5923  1.1  christos 
   5924  1.1  christos CORE_ADDR
   5925  1.1  christos get_set_tsv_func_addr (void)
   5926  1.1  christos {
   5927  1.1  christos   CORE_ADDR res;
   5928  1.1  christos   if (read_inferior_data_pointer (
   5929  1.1  christos 	ipa_sym_addrs.addr_set_trace_state_variable_value_ptr, &res))
   5930  1.1  christos     {
   5931  1.1  christos       error ("error extracting set_trace_state_variable_value_ptr");
   5932  1.1  christos       return 0;
   5933  1.1  christos     }
   5934  1.1  christos   return res;
   5935  1.1  christos }
   5936  1.1  christos 
   5937  1.1  christos static void
   5938  1.1  christos compile_tracepoint_condition (struct tracepoint *tpoint,
   5939  1.1  christos 			      CORE_ADDR *jump_entry)
   5940  1.1  christos {
   5941  1.1  christos   CORE_ADDR entry_point = *jump_entry;
   5942  1.1  christos   enum eval_result_type err;
   5943  1.1  christos 
   5944  1.1  christos   trace_debug ("Starting condition compilation for tracepoint %d\n",
   5945  1.1  christos 	       tpoint->number);
   5946  1.1  christos 
   5947  1.1  christos   /* Initialize the global pointer to the code being built.  */
   5948  1.1  christos   current_insn_ptr = *jump_entry;
   5949  1.1  christos 
   5950  1.1  christos   emit_prologue ();
   5951  1.1  christos 
   5952  1.1  christos   err = compile_bytecodes (tpoint->cond);
   5953  1.1  christos 
   5954  1.1  christos   if (err == expr_eval_no_error)
   5955  1.1  christos     {
   5956  1.1  christos       emit_epilogue ();
   5957  1.1  christos 
   5958  1.1  christos       /* Record the beginning of the compiled code.  */
   5959  1.1  christos       tpoint->compiled_cond = entry_point;
   5960  1.1  christos 
   5961  1.1  christos       trace_debug ("Condition compilation for tracepoint %d complete\n",
   5962  1.1  christos 		   tpoint->number);
   5963  1.1  christos     }
   5964  1.1  christos   else
   5965  1.1  christos     {
   5966  1.1  christos       /* Leave the unfinished code in situ, but don't point to it.  */
   5967  1.1  christos 
   5968  1.1  christos       tpoint->compiled_cond = 0;
   5969  1.1  christos 
   5970  1.1  christos       trace_debug ("Condition compilation for tracepoint %d failed, "
   5971  1.1  christos 		   "error code %d",
   5972  1.1  christos 		   tpoint->number, err);
   5973  1.1  christos     }
   5974  1.1  christos 
   5975  1.1  christos   /* Update the code pointer passed in.  Note that we do this even if
   5976  1.1  christos      the compile fails, so that we can look at the partial results
   5977  1.1  christos      instead of letting them be overwritten.  */
   5978  1.1  christos   *jump_entry = current_insn_ptr;
   5979  1.1  christos 
   5980  1.1  christos   /* Leave a gap, to aid dump decipherment.  */
   5981  1.1  christos   *jump_entry += 16;
   5982  1.1  christos }
   5983  1.1  christos 
   5984  1.1  christos /* The base pointer of the IPA's heap.  This is the only memory the
   5985  1.1  christos    IPA is allowed to use.  The IPA should _not_ call the inferior's
   5986  1.1  christos    `malloc' during operation.  That'd be slow, and, most importantly,
   5987  1.1  christos    it may not be safe.  We may be collecting a tracepoint in a signal
   5988  1.1  christos    handler, for example.  */
   5989  1.1  christos static CORE_ADDR target_tp_heap;
   5990  1.1  christos 
   5991  1.1  christos /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
   5992  1.1  christos    to 8 bytes.  */
   5993  1.1  christos 
   5994  1.1  christos static CORE_ADDR
   5995  1.1  christos target_malloc (ULONGEST size)
   5996  1.1  christos {
   5997  1.1  christos   CORE_ADDR ptr;
   5998  1.1  christos 
   5999  1.1  christos   if (target_tp_heap == 0)
   6000  1.1  christos     {
   6001  1.1  christos       /* We have the pointer *address*, need what it points to.  */
   6002  1.1  christos       if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_tp_heap_buffer,
   6003  1.1  christos 				      &target_tp_heap))
   6004  1.1  christos 	{
   6005  1.1  christos 	  internal_error (__FILE__, __LINE__,
   6006  1.1  christos 			  "couldn't get target heap head pointer");
   6007  1.1  christos 	}
   6008  1.1  christos     }
   6009  1.1  christos 
   6010  1.1  christos   ptr = target_tp_heap;
   6011  1.1  christos   target_tp_heap += size;
   6012  1.1  christos 
   6013  1.1  christos   /* Pad to 8-byte alignment.  */
   6014  1.1  christos   target_tp_heap = ((target_tp_heap + 7) & ~0x7);
   6015  1.1  christos 
   6016  1.1  christos   return ptr;
   6017  1.1  christos }
   6018  1.1  christos 
   6019  1.1  christos static CORE_ADDR
   6020  1.1  christos download_agent_expr (struct agent_expr *expr)
   6021  1.1  christos {
   6022  1.1  christos   CORE_ADDR expr_addr;
   6023  1.1  christos   CORE_ADDR expr_bytes;
   6024  1.1  christos 
   6025  1.1  christos   expr_addr = target_malloc (sizeof (*expr));
   6026  1.1  christos   target_write_memory (expr_addr, (unsigned char *) expr, sizeof (*expr));
   6027  1.1  christos 
   6028  1.1  christos   expr_bytes = target_malloc (expr->length);
   6029  1.1  christos   write_inferior_data_pointer (expr_addr + offsetof (struct agent_expr, bytes),
   6030  1.1  christos 			       expr_bytes);
   6031  1.1  christos   target_write_memory (expr_bytes, expr->bytes, expr->length);
   6032  1.1  christos 
   6033  1.1  christos   return expr_addr;
   6034  1.1  christos }
   6035  1.1  christos 
   6036  1.1  christos /* Align V up to N bits.  */
   6037  1.1  christos #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
   6038  1.1  christos 
   6039  1.1  christos /* Sync tracepoint with IPA, but leave maintenance of linked list to caller.  */
   6040  1.1  christos 
   6041  1.1  christos static void
   6042  1.1  christos download_tracepoint_1 (struct tracepoint *tpoint)
   6043  1.1  christos {
   6044  1.1  christos   struct tracepoint target_tracepoint;
   6045  1.1  christos   CORE_ADDR tpptr = 0;
   6046  1.1  christos 
   6047  1.1  christos   gdb_assert (tpoint->type == fast_tracepoint
   6048  1.1  christos 	      || tpoint->type == static_tracepoint);
   6049  1.1  christos 
   6050  1.1  christos   if (tpoint->cond != NULL && target_emit_ops () != NULL)
   6051  1.1  christos     {
   6052  1.1  christos       CORE_ADDR jentry, jump_entry;
   6053  1.1  christos 
   6054  1.1  christos       jentry = jump_entry = get_jump_space_head ();
   6055  1.1  christos 
   6056  1.1  christos       if (tpoint->cond != NULL)
   6057  1.1  christos 	{
   6058  1.1  christos 	  /* Pad to 8-byte alignment. (needed?)  */
   6059  1.1  christos 	  /* Actually this should be left for the target to
   6060  1.1  christos 	     decide.  */
   6061  1.1  christos 	  jentry = UALIGN (jentry, 8);
   6062  1.1  christos 
   6063  1.1  christos 	  compile_tracepoint_condition (tpoint, &jentry);
   6064  1.1  christos 	}
   6065  1.1  christos 
   6066  1.1  christos       /* Pad to 8-byte alignment.  */
   6067  1.1  christos       jentry = UALIGN (jentry, 8);
   6068  1.1  christos       claim_jump_space (jentry - jump_entry);
   6069  1.1  christos     }
   6070  1.1  christos 
   6071  1.1  christos   target_tracepoint = *tpoint;
   6072  1.1  christos 
   6073  1.1  christos   tpptr = target_malloc (sizeof (*tpoint));
   6074  1.1  christos   tpoint->obj_addr_on_target = tpptr;
   6075  1.1  christos 
   6076  1.1  christos   /* Write the whole object.  We'll fix up its pointers in a bit.
   6077  1.1  christos      Assume no next for now.  This is fixed up above on the next
   6078  1.1  christos      iteration, if there's any.  */
   6079  1.1  christos   target_tracepoint.next = NULL;
   6080  1.1  christos   /* Need to clear this here too, since we're downloading the
   6081  1.1  christos      tracepoints before clearing our own copy.  */
   6082  1.1  christos   target_tracepoint.hit_count = 0;
   6083  1.1  christos 
   6084  1.1  christos   target_write_memory (tpptr, (unsigned char *) &target_tracepoint,
   6085  1.1  christos 			 sizeof (target_tracepoint));
   6086  1.1  christos 
   6087  1.1  christos   if (tpoint->cond)
   6088  1.1  christos     write_inferior_data_pointer (tpptr
   6089  1.1  christos 				 + offsetof (struct tracepoint, cond),
   6090  1.1  christos 				 download_agent_expr (tpoint->cond));
   6091  1.1  christos 
   6092  1.1  christos   if (tpoint->numactions)
   6093  1.1  christos     {
   6094  1.1  christos       int i;
   6095  1.1  christos       CORE_ADDR actions_array;
   6096  1.1  christos 
   6097  1.1  christos       /* The pointers array.  */
   6098  1.1  christos       actions_array
   6099  1.1  christos 	= target_malloc (sizeof (*tpoint->actions) * tpoint->numactions);
   6100  1.1  christos       write_inferior_data_pointer (tpptr + offsetof (struct tracepoint,
   6101  1.1  christos 						     actions),
   6102  1.1  christos 				   actions_array);
   6103  1.1  christos 
   6104  1.1  christos       /* Now for each pointer, download the action.  */
   6105  1.1  christos       for (i = 0; i < tpoint->numactions; i++)
   6106  1.1  christos 	{
   6107  1.1  christos 	  struct tracepoint_action *action = tpoint->actions[i];
   6108  1.1  christos 	  CORE_ADDR ipa_action = tracepoint_action_download (action);
   6109  1.1  christos 
   6110  1.1  christos 	  if (ipa_action != 0)
   6111  1.1  christos 	    write_inferior_data_pointer (actions_array
   6112  1.1  christos 					 + i * sizeof (*tpoint->actions),
   6113  1.1  christos 					 ipa_action);
   6114  1.1  christos 	}
   6115  1.1  christos     }
   6116  1.1  christos }
   6117  1.1  christos 
   6118  1.1  christos #define IPA_PROTO_FAST_TRACE_FLAG 0
   6119  1.1  christos #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
   6120  1.1  christos #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
   6121  1.1  christos #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
   6122  1.1  christos #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
   6123  1.1  christos 
   6124  1.1  christos /* Send a command to agent to download and install tracepoint TPOINT.  */
   6125  1.1  christos 
   6126  1.1  christos static int
   6127  1.1  christos tracepoint_send_agent (struct tracepoint *tpoint)
   6128  1.1  christos {
   6129  1.1  christos   char buf[IPA_CMD_BUF_SIZE];
   6130  1.1  christos   char *p;
   6131  1.1  christos   int i, ret;
   6132  1.1  christos 
   6133  1.1  christos   p = buf;
   6134  1.1  christos   strcpy (p, "FastTrace:");
   6135  1.1  christos   p += 10;
   6136  1.1  christos 
   6137  1.1  christos   COPY_FIELD_TO_BUF (p, tpoint, number);
   6138  1.1  christos   COPY_FIELD_TO_BUF (p, tpoint, address);
   6139  1.1  christos   COPY_FIELD_TO_BUF (p, tpoint, type);
   6140  1.1  christos   COPY_FIELD_TO_BUF (p, tpoint, enabled);
   6141  1.1  christos   COPY_FIELD_TO_BUF (p, tpoint, step_count);
   6142  1.1  christos   COPY_FIELD_TO_BUF (p, tpoint, pass_count);
   6143  1.1  christos   COPY_FIELD_TO_BUF (p, tpoint, numactions);
   6144  1.1  christos   COPY_FIELD_TO_BUF (p, tpoint, hit_count);
   6145  1.1  christos   COPY_FIELD_TO_BUF (p, tpoint, traceframe_usage);
   6146  1.1  christos   COPY_FIELD_TO_BUF (p, tpoint, compiled_cond);
   6147  1.1  christos   COPY_FIELD_TO_BUF (p, tpoint, orig_size);
   6148  1.1  christos 
   6149  1.1  christos   /* condition */
   6150  1.1  christos   p = agent_expr_send (p, tpoint->cond);
   6151  1.1  christos 
   6152  1.1  christos   /* tracepoint_action */
   6153  1.1  christos   for (i = 0; i < tpoint->numactions; i++)
   6154  1.1  christos     {
   6155  1.1  christos       struct tracepoint_action *action = tpoint->actions[i];
   6156  1.1  christos 
   6157  1.1  christos       p[0] = action->type;
   6158  1.1  christos       p = tracepoint_action_send (&p[1], action);
   6159  1.1  christos     }
   6160  1.1  christos 
   6161  1.1  christos   get_jump_space_head ();
   6162  1.1  christos   /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
   6163  1.1  christos      agent can use jump pad from it.  */
   6164  1.1  christos   if (tpoint->type == fast_tracepoint)
   6165  1.1  christos     {
   6166  1.1  christos       memcpy (p, &gdb_jump_pad_head, 8);
   6167  1.1  christos       p += 8;
   6168  1.1  christos     }
   6169  1.1  christos 
   6170  1.1  christos   ret = run_inferior_command (buf, (int) (ptrdiff_t) (p - buf));
   6171  1.1  christos   if (ret)
   6172  1.1  christos     return ret;
   6173  1.1  christos 
   6174  1.1  christos   if (!startswith (buf, "OK"))
   6175  1.1  christos     return 1;
   6176  1.1  christos 
   6177  1.1  christos   /* The value of tracepoint's target address is stored in BUF.  */
   6178  1.1  christos   memcpy (&tpoint->obj_addr_on_target,
   6179  1.1  christos 	  &buf[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET], 8);
   6180  1.1  christos 
   6181  1.1  christos   if (tpoint->type == fast_tracepoint)
   6182  1.1  christos     {
   6183  1.1  christos       unsigned char *insn
   6184  1.1  christos 	= (unsigned char *) &buf[IPA_PROTO_FAST_TRACE_FJUMP_INSN];
   6185  1.1  christos       int fjump_size;
   6186  1.1  christos 
   6187  1.1  christos      trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
   6188  1.1  christos 		  (unsigned int) tpoint->obj_addr_on_target,
   6189  1.1  christos 		  (unsigned int) gdb_jump_pad_head);
   6190  1.1  christos 
   6191  1.1  christos       memcpy (&gdb_jump_pad_head, &buf[IPA_PROTO_FAST_TRACE_JUMP_PAD], 8);
   6192  1.1  christos 
   6193  1.1  christos       /* This has been done in agent.  We should also set up record for it.  */
   6194  1.1  christos       memcpy (&fjump_size, &buf[IPA_PROTO_FAST_TRACE_FJUMP_SIZE], 4);
   6195  1.1  christos       /* Wire it in.  */
   6196  1.1  christos       tpoint->handle
   6197  1.1  christos 	= set_fast_tracepoint_jump (tpoint->address, insn, fjump_size);
   6198  1.1  christos     }
   6199  1.1  christos 
   6200  1.1  christos   return 0;
   6201  1.1  christos }
   6202  1.1  christos 
   6203  1.1  christos static void
   6204  1.1  christos download_tracepoint (struct tracepoint *tpoint)
   6205  1.1  christos {
   6206  1.1  christos   struct tracepoint *tp, *tp_prev;
   6207  1.1  christos 
   6208  1.1  christos   if (tpoint->type != fast_tracepoint
   6209  1.1  christos       && tpoint->type != static_tracepoint)
   6210  1.1  christos     return;
   6211  1.1  christos 
   6212  1.1  christos   download_tracepoint_1 (tpoint);
   6213  1.1  christos 
   6214  1.1  christos   /* Find the previous entry of TPOINT, which is fast tracepoint or
   6215  1.1  christos      static tracepoint.  */
   6216  1.1  christos   tp_prev = NULL;
   6217  1.1  christos   for (tp = tracepoints; tp != tpoint; tp = tp->next)
   6218  1.1  christos     {
   6219  1.1  christos       if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
   6220  1.1  christos 	tp_prev = tp;
   6221  1.1  christos     }
   6222  1.1  christos 
   6223  1.1  christos   if (tp_prev)
   6224  1.1  christos     {
   6225  1.1  christos       CORE_ADDR tp_prev_target_next_addr;
   6226  1.1  christos 
   6227  1.1  christos       /* Insert TPOINT after TP_PREV in IPA.  */
   6228  1.1  christos       if (read_inferior_data_pointer (tp_prev->obj_addr_on_target
   6229  1.1  christos 				      + offsetof (struct tracepoint, next),
   6230  1.1  christos 				      &tp_prev_target_next_addr))
   6231  1.1  christos 	{
   6232  1.1  christos 	  internal_error (__FILE__, __LINE__,
   6233  1.1  christos 			  "error reading `tp_prev->next'");
   6234  1.1  christos 	}
   6235  1.1  christos 
   6236  1.1  christos       /* tpoint->next = tp_prev->next */
   6237  1.1  christos       write_inferior_data_pointer (tpoint->obj_addr_on_target
   6238  1.1  christos 				   + offsetof (struct tracepoint, next),
   6239  1.1  christos 				   tp_prev_target_next_addr);
   6240  1.1  christos       /* tp_prev->next = tpoint */
   6241  1.1  christos       write_inferior_data_pointer (tp_prev->obj_addr_on_target
   6242  1.1  christos 				   + offsetof (struct tracepoint, next),
   6243  1.1  christos 				   tpoint->obj_addr_on_target);
   6244  1.1  christos     }
   6245  1.1  christos   else
   6246  1.1  christos     /* First object in list, set the head pointer in the
   6247  1.1  christos        inferior.  */
   6248  1.1  christos     write_inferior_data_pointer (ipa_sym_addrs.addr_tracepoints,
   6249  1.1  christos 				 tpoint->obj_addr_on_target);
   6250  1.1  christos 
   6251  1.1  christos }
   6252  1.1  christos 
   6253  1.1  christos static void
   6254  1.1  christos download_trace_state_variables (void)
   6255  1.1  christos {
   6256  1.1  christos   CORE_ADDR ptr = 0, prev_ptr = 0;
   6257  1.1  christos   struct trace_state_variable *tsv;
   6258  1.1  christos 
   6259  1.1  christos   /* Start out empty.  */
   6260  1.1  christos   write_inferior_data_pointer (ipa_sym_addrs.addr_trace_state_variables, 0);
   6261  1.1  christos 
   6262  1.1  christos   for (tsv = trace_state_variables; tsv != NULL; tsv = tsv->next)
   6263  1.1  christos     {
   6264  1.1  christos       struct trace_state_variable target_tsv;
   6265  1.1  christos 
   6266  1.1  christos       /* TSV's with a getter have been initialized equally in both the
   6267  1.1  christos 	 inferior and GDBserver.  Skip them.  */
   6268  1.1  christos       if (tsv->getter != NULL)
   6269  1.1  christos 	continue;
   6270  1.1  christos 
   6271  1.1  christos       target_tsv = *tsv;
   6272  1.1  christos 
   6273  1.1  christos       prev_ptr = ptr;
   6274  1.1  christos       ptr = target_malloc (sizeof (*tsv));
   6275  1.1  christos 
   6276  1.1  christos       if (tsv == trace_state_variables)
   6277  1.1  christos 	{
   6278  1.1  christos 	  /* First object in list, set the head pointer in the
   6279  1.1  christos 	     inferior.  */
   6280  1.1  christos 
   6281  1.1  christos 	  write_inferior_data_pointer (ipa_sym_addrs.addr_trace_state_variables,
   6282  1.1  christos 				       ptr);
   6283  1.1  christos 	}
   6284  1.1  christos       else
   6285  1.1  christos 	{
   6286  1.1  christos 	  write_inferior_data_pointer (prev_ptr
   6287  1.1  christos 				       + offsetof (struct trace_state_variable,
   6288  1.1  christos 						   next),
   6289  1.1  christos 				       ptr);
   6290  1.1  christos 	}
   6291  1.1  christos 
   6292  1.1  christos       /* Write the whole object.  We'll fix up its pointers in a bit.
   6293  1.1  christos 	 Assume no next, fixup when needed.  */
   6294  1.1  christos       target_tsv.next = NULL;
   6295  1.1  christos 
   6296  1.1  christos       target_write_memory (ptr, (unsigned char *) &target_tsv,
   6297  1.1  christos 			     sizeof (target_tsv));
   6298  1.1  christos 
   6299  1.1  christos       if (tsv->name != NULL)
   6300  1.1  christos 	{
   6301  1.1  christos 	  size_t size = strlen (tsv->name) + 1;
   6302  1.1  christos 	  CORE_ADDR name_addr = target_malloc (size);
   6303  1.1  christos 	  target_write_memory (name_addr,
   6304  1.1  christos 				 (unsigned char *) tsv->name, size);
   6305  1.1  christos 	  write_inferior_data_pointer (ptr
   6306  1.1  christos 				       + offsetof (struct trace_state_variable,
   6307  1.1  christos 						   name),
   6308  1.1  christos 				       name_addr);
   6309  1.1  christos 	}
   6310  1.1  christos 
   6311  1.1  christos       gdb_assert (tsv->getter == NULL);
   6312  1.1  christos     }
   6313  1.1  christos 
   6314  1.1  christos   if (prev_ptr != 0)
   6315  1.1  christos     {
   6316  1.1  christos       /* Fixup the next pointer in the last item in the list.  */
   6317  1.1  christos       write_inferior_data_pointer (prev_ptr
   6318  1.1  christos 				   + offsetof (struct trace_state_variable,
   6319  1.1  christos 					       next), 0);
   6320  1.1  christos     }
   6321  1.1  christos }
   6322  1.1  christos 
   6323  1.1  christos /* Upload complete trace frames out of the IP Agent's trace buffer
   6324  1.1  christos    into GDBserver's trace buffer.  This always uploads either all or
   6325  1.1  christos    no trace frames.  This is the counter part of
   6326  1.1  christos    `trace_alloc_trace_buffer'.  See its description of the atomic
   6327  1.1  christos    syncing mechanism.  */
   6328  1.1  christos 
   6329  1.1  christos static void
   6330  1.1  christos upload_fast_traceframes (void)
   6331  1.1  christos {
   6332  1.1  christos   unsigned int ipa_traceframe_read_count, ipa_traceframe_write_count;
   6333  1.1  christos   unsigned int ipa_traceframe_read_count_racy, ipa_traceframe_write_count_racy;
   6334  1.1  christos   CORE_ADDR tf;
   6335  1.1  christos   struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
   6336  1.1  christos   unsigned int curr_tbctrl_idx;
   6337  1.1  christos   unsigned int ipa_trace_buffer_ctrl_curr;
   6338  1.1  christos   unsigned int ipa_trace_buffer_ctrl_curr_old;
   6339  1.1  christos   CORE_ADDR ipa_trace_buffer_ctrl_addr;
   6340  1.1  christos   struct breakpoint *about_to_request_buffer_space_bkpt;
   6341  1.1  christos   CORE_ADDR ipa_trace_buffer_lo;
   6342  1.1  christos   CORE_ADDR ipa_trace_buffer_hi;
   6343  1.1  christos 
   6344  1.1  christos   if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
   6345  1.1  christos 			      &ipa_traceframe_read_count_racy))
   6346  1.1  christos     {
   6347  1.1  christos       /* This will happen in most targets if the current thread is
   6348  1.1  christos 	 running.  */
   6349  1.1  christos       return;
   6350  1.1  christos     }
   6351  1.1  christos 
   6352  1.1  christos   if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
   6353  1.1  christos 			      &ipa_traceframe_write_count_racy))
   6354  1.1  christos     return;
   6355  1.1  christos 
   6356  1.1  christos   trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
   6357  1.1  christos 	       ipa_traceframe_write_count_racy
   6358  1.1  christos 	       - ipa_traceframe_read_count_racy,
   6359  1.1  christos 	       ipa_traceframe_write_count_racy,
   6360  1.1  christos 	       ipa_traceframe_read_count_racy);
   6361  1.1  christos 
   6362  1.1  christos   if (ipa_traceframe_write_count_racy == ipa_traceframe_read_count_racy)
   6363  1.1  christos     return;
   6364  1.1  christos 
   6365  1.1  christos   about_to_request_buffer_space_bkpt
   6366  1.1  christos     = set_breakpoint_at (ipa_sym_addrs.addr_about_to_request_buffer_space,
   6367  1.1  christos 			 NULL);
   6368  1.1  christos 
   6369  1.1  christos   if (read_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
   6370  1.1  christos 			      &ipa_trace_buffer_ctrl_curr))
   6371  1.1  christos     return;
   6372  1.1  christos 
   6373  1.1  christos   ipa_trace_buffer_ctrl_curr_old = ipa_trace_buffer_ctrl_curr;
   6374  1.1  christos 
   6375  1.1  christos   curr_tbctrl_idx = ipa_trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK;
   6376  1.1  christos 
   6377  1.1  christos   {
   6378  1.1  christos     unsigned int prev, counter;
   6379  1.1  christos 
   6380  1.1  christos     /* Update the token, with new counters, and the GDBserver stamp
   6381  1.1  christos        bit.  Alway reuse the current TBC index.  */
   6382  1.1  christos     prev = ipa_trace_buffer_ctrl_curr & GDBSERVER_FLUSH_COUNT_MASK_CURR;
   6383  1.1  christos     counter = (prev + 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR;
   6384  1.1  christos 
   6385  1.1  christos     ipa_trace_buffer_ctrl_curr = (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
   6386  1.1  christos 				  | (prev << 12)
   6387  1.1  christos 				  | counter
   6388  1.1  christos 				  | curr_tbctrl_idx);
   6389  1.1  christos   }
   6390  1.1  christos 
   6391  1.1  christos   if (write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
   6392  1.1  christos 			       ipa_trace_buffer_ctrl_curr))
   6393  1.1  christos     return;
   6394  1.1  christos 
   6395  1.1  christos   trace_debug ("Lib: Committed %08x -> %08x",
   6396  1.1  christos 	       ipa_trace_buffer_ctrl_curr_old,
   6397  1.1  christos 	       ipa_trace_buffer_ctrl_curr);
   6398  1.1  christos 
   6399  1.1  christos   /* Re-read these, now that we've installed the
   6400  1.1  christos      `about_to_request_buffer_space' breakpoint/lock.  A thread could
   6401  1.1  christos      have finished a traceframe between the last read of these
   6402  1.1  christos      counters and setting the breakpoint above.  If we start
   6403  1.1  christos      uploading, we never want to leave this function with
   6404  1.1  christos      traceframe_read_count != 0, otherwise, GDBserver could end up
   6405  1.1  christos      incrementing the counter tokens more than once (due to event loop
   6406  1.1  christos      nesting), which would break the IP agent's "effective" detection
   6407  1.1  christos      (see trace_alloc_trace_buffer).  */
   6408  1.1  christos   if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
   6409  1.1  christos 			      &ipa_traceframe_read_count))
   6410  1.1  christos     return;
   6411  1.1  christos   if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
   6412  1.1  christos 			      &ipa_traceframe_write_count))
   6413  1.1  christos     return;
   6414  1.1  christos 
   6415  1.1  christos   if (debug_threads)
   6416  1.1  christos     {
   6417  1.1  christos       trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
   6418  1.1  christos 		   ipa_traceframe_write_count - ipa_traceframe_read_count,
   6419  1.1  christos 		   ipa_traceframe_write_count, ipa_traceframe_read_count);
   6420  1.1  christos 
   6421  1.1  christos       if (ipa_traceframe_write_count != ipa_traceframe_write_count_racy
   6422  1.1  christos 	  || ipa_traceframe_read_count != ipa_traceframe_read_count_racy)
   6423  1.1  christos 	trace_debug ("note that ipa_traceframe_count's parts changed");
   6424  1.1  christos     }
   6425  1.1  christos 
   6426  1.1  christos   /* Get the address of the current TBC object (the IP agent has an
   6427  1.1  christos      array of 3 such objects).  The index is stored in the TBC
   6428  1.1  christos      token.  */
   6429  1.1  christos   ipa_trace_buffer_ctrl_addr = ipa_sym_addrs.addr_trace_buffer_ctrl;
   6430  1.1  christos   ipa_trace_buffer_ctrl_addr
   6431  1.1  christos     += sizeof (struct ipa_trace_buffer_control) * curr_tbctrl_idx;
   6432  1.1  christos 
   6433  1.1  christos   if (read_inferior_memory (ipa_trace_buffer_ctrl_addr,
   6434  1.1  christos 			    (unsigned char *) &ipa_trace_buffer_ctrl,
   6435  1.1  christos 			    sizeof (struct ipa_trace_buffer_control)))
   6436  1.1  christos     return;
   6437  1.1  christos 
   6438  1.1  christos   if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
   6439  1.1  christos 				  &ipa_trace_buffer_lo))
   6440  1.1  christos     return;
   6441  1.1  christos   if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
   6442  1.1  christos 				  &ipa_trace_buffer_hi))
   6443  1.1  christos     return;
   6444  1.1  christos 
   6445  1.1  christos   /* Offsets are easier to grok for debugging than raw addresses,
   6446  1.1  christos      especially for the small trace buffer sizes that are useful for
   6447  1.1  christos      testing.  */
   6448  1.1  christos   trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
   6449  1.1  christos 	       "endfree=%d wrap=%d hi=%d",
   6450  1.1  christos 	       curr_tbctrl_idx,
   6451  1.1  christos 	       (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
   6452  1.1  christos 	       (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
   6453  1.1  christos 	       (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
   6454  1.1  christos 	       (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
   6455  1.1  christos 	       (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
   6456  1.1  christos 
   6457  1.1  christos   /* Note that the IPA's buffer is always circular.  */
   6458  1.1  christos 
   6459  1.1  christos #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
   6460  1.1  christos 
   6461  1.1  christos #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ)		\
   6462  1.1  christos   ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
   6463  1.1  christos 
   6464  1.1  christos #define IPA_NEXT_TRACEFRAME(TF, TFOBJ)					\
   6465  1.1  christos   (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ)					\
   6466  1.1  christos    - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
   6467  1.1  christos       ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo)		\
   6468  1.1  christos       : 0))
   6469  1.1  christos 
   6470  1.1  christos   tf = IPA_FIRST_TRACEFRAME ();
   6471  1.1  christos 
   6472  1.1  christos   while (ipa_traceframe_write_count - ipa_traceframe_read_count)
   6473  1.1  christos     {
   6474  1.1  christos       struct tracepoint *tpoint;
   6475  1.1  christos       struct traceframe *tframe;
   6476  1.1  christos       unsigned char *block;
   6477  1.1  christos       struct traceframe ipa_tframe;
   6478  1.1  christos 
   6479  1.1  christos       if (read_inferior_memory (tf, (unsigned char *) &ipa_tframe,
   6480  1.1  christos 				offsetof (struct traceframe, data)))
   6481  1.1  christos 	error ("Uploading: couldn't read traceframe at %s\n", paddress (tf));
   6482  1.1  christos 
   6483  1.1  christos       if (ipa_tframe.tpnum == 0)
   6484  1.1  christos 	{
   6485  1.1  christos 	  internal_error (__FILE__, __LINE__,
   6486  1.1  christos 			  "Uploading: No (more) fast traceframes, but"
   6487  1.1  christos 			  " ipa_traceframe_count == %u??\n",
   6488  1.1  christos 			  ipa_traceframe_write_count
   6489  1.1  christos 			  - ipa_traceframe_read_count);
   6490  1.1  christos 	}
   6491  1.1  christos 
   6492  1.1  christos       /* Note that this will be incorrect for multi-location
   6493  1.1  christos 	 tracepoints...  */
   6494  1.1  christos       tpoint = find_next_tracepoint_by_number (NULL, ipa_tframe.tpnum);
   6495  1.1  christos 
   6496  1.1  christos       tframe = add_traceframe (tpoint);
   6497  1.1  christos       if (tframe == NULL)
   6498  1.1  christos 	{
   6499  1.1  christos 	  trace_buffer_is_full = 1;
   6500  1.1  christos 	  trace_debug ("Uploading: trace buffer is full");
   6501  1.1  christos 	}
   6502  1.1  christos       else
   6503  1.1  christos 	{
   6504  1.1  christos 	  /* Copy the whole set of blocks in one go for now.  FIXME:
   6505  1.1  christos 	     split this in smaller blocks.  */
   6506  1.1  christos 	  block = add_traceframe_block (tframe, tpoint,
   6507  1.1  christos 					ipa_tframe.data_size);
   6508  1.1  christos 	  if (block != NULL)
   6509  1.1  christos 	    {
   6510  1.1  christos 	      if (read_inferior_memory (tf
   6511  1.1  christos 					+ offsetof (struct traceframe, data),
   6512  1.1  christos 					block, ipa_tframe.data_size))
   6513  1.1  christos 		error ("Uploading: Couldn't read traceframe data at %s\n",
   6514  1.1  christos 		       paddress (tf + offsetof (struct traceframe, data)));
   6515  1.1  christos 	    }
   6516  1.1  christos 
   6517  1.1  christos 	  trace_debug ("Uploading: traceframe didn't fit");
   6518  1.1  christos 	  finish_traceframe (tframe);
   6519  1.1  christos 	}
   6520  1.1  christos 
   6521  1.1  christos       tf = IPA_NEXT_TRACEFRAME (tf, &ipa_tframe);
   6522  1.1  christos 
   6523  1.1  christos       /* If we freed the traceframe that wrapped around, go back
   6524  1.1  christos 	 to the non-wrap case.  */
   6525  1.1  christos       if (tf < ipa_trace_buffer_ctrl.start)
   6526  1.1  christos 	{
   6527  1.1  christos 	  trace_debug ("Lib: Discarding past the wraparound");
   6528  1.1  christos 	  ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
   6529  1.1  christos 	}
   6530  1.1  christos       ipa_trace_buffer_ctrl.start = tf;
   6531  1.1  christos       ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_ctrl.start;
   6532  1.1  christos       ++ipa_traceframe_read_count;
   6533  1.1  christos 
   6534  1.1  christos       if (ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.free
   6535  1.1  christos 	  && ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.end_free)
   6536  1.1  christos 	{
   6537  1.1  christos 	  trace_debug ("Lib: buffer is fully empty.  "
   6538  1.1  christos 		       "Trace buffer [%d] start=%d free=%d endfree=%d",
   6539  1.1  christos 		       curr_tbctrl_idx,
   6540  1.1  christos 		       (int) (ipa_trace_buffer_ctrl.start
   6541  1.1  christos 			      - ipa_trace_buffer_lo),
   6542  1.1  christos 		       (int) (ipa_trace_buffer_ctrl.free
   6543  1.1  christos 			      - ipa_trace_buffer_lo),
   6544  1.1  christos 		       (int) (ipa_trace_buffer_ctrl.end_free
   6545  1.1  christos 			      - ipa_trace_buffer_lo));
   6546  1.1  christos 
   6547  1.1  christos 	  ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
   6548  1.1  christos 	  ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
   6549  1.1  christos 	  ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
   6550  1.1  christos 	  ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
   6551  1.1  christos 	}
   6552  1.1  christos 
   6553  1.1  christos       trace_debug ("Uploaded a traceframe\n"
   6554  1.1  christos 		   "Lib: Trace buffer [%d] start=%d free=%d "
   6555  1.1  christos 		   "endfree=%d wrap=%d hi=%d",
   6556  1.1  christos 		   curr_tbctrl_idx,
   6557  1.1  christos 		   (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
   6558  1.1  christos 		   (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
   6559  1.1  christos 		   (int) (ipa_trace_buffer_ctrl.end_free
   6560  1.1  christos 			  - ipa_trace_buffer_lo),
   6561  1.1  christos 		   (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
   6562  1.1  christos 		   (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
   6563  1.1  christos     }
   6564  1.1  christos 
   6565  1.1  christos   if (target_write_memory (ipa_trace_buffer_ctrl_addr,
   6566  1.1  christos 			     (unsigned char *) &ipa_trace_buffer_ctrl,
   6567  1.1  christos 			     sizeof (struct ipa_trace_buffer_control)))
   6568  1.1  christos     return;
   6569  1.1  christos 
   6570  1.1  christos   write_inferior_integer (ipa_sym_addrs.addr_traceframe_read_count,
   6571  1.1  christos 			  ipa_traceframe_read_count);
   6572  1.1  christos 
   6573  1.1  christos   trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx);
   6574  1.1  christos 
   6575  1.1  christos   target_pause_all (true);
   6576  1.1  christos 
   6577  1.1  christos   delete_breakpoint (about_to_request_buffer_space_bkpt);
   6578  1.1  christos   about_to_request_buffer_space_bkpt = NULL;
   6579  1.1  christos 
   6580  1.1  christos   target_unpause_all (true);
   6581  1.1  christos 
   6582  1.1  christos   if (trace_buffer_is_full)
   6583  1.1  christos     stop_tracing ();
   6584  1.1  christos }
   6585  1.1  christos #endif
   6586  1.1  christos 
   6587  1.1  christos #ifdef IN_PROCESS_AGENT
   6588  1.1  christos 
   6589  1.1  christos IP_AGENT_EXPORT_VAR int ust_loaded;
   6590  1.1  christos IP_AGENT_EXPORT_VAR char cmd_buf[IPA_CMD_BUF_SIZE];
   6591  1.1  christos 
   6592  1.1  christos #ifdef HAVE_UST
   6593  1.1  christos 
   6594  1.1  christos /* Static tracepoints.  */
   6595  1.1  christos 
   6596  1.1  christos /* UST puts a "struct tracepoint" in the global namespace, which
   6597  1.1  christos    conflicts with our tracepoint.  Arguably, being a library, it
   6598  1.1  christos    shouldn't take ownership of such a generic name.  We work around it
   6599  1.1  christos    here.  */
   6600  1.1  christos #define tracepoint ust_tracepoint
   6601  1.1  christos #include <ust/ust.h>
   6602  1.1  christos #undef tracepoint
   6603  1.1  christos 
   6604  1.1  christos extern int serialize_to_text (char *outbuf, int bufsize,
   6605  1.1  christos 			      const char *fmt, va_list ap);
   6606  1.1  christos 
   6607  1.1  christos #define GDB_PROBE_NAME "gdb"
   6608  1.1  christos 
   6609  1.1  christos /* We dynamically search for the UST symbols instead of linking them
   6610  1.1  christos    in.  This lets the user decide if the application uses static
   6611  1.1  christos    tracepoints, instead of always pulling libust.so in.  This vector
   6612  1.1  christos    holds pointers to all functions we care about.  */
   6613  1.1  christos 
   6614  1.1  christos static struct
   6615  1.1  christos {
   6616  1.1  christos   int (*serialize_to_text) (char *outbuf, int bufsize,
   6617  1.1  christos 			    const char *fmt, va_list ap);
   6618  1.1  christos 
   6619  1.1  christos   int (*ltt_probe_register) (struct ltt_available_probe *pdata);
   6620  1.1  christos   int (*ltt_probe_unregister) (struct ltt_available_probe *pdata);
   6621  1.1  christos 
   6622  1.1  christos   int (*ltt_marker_connect) (const char *channel, const char *mname,
   6623  1.1  christos 			     const char *pname);
   6624  1.1  christos   int (*ltt_marker_disconnect) (const char *channel, const char *mname,
   6625  1.1  christos 				const char *pname);
   6626  1.1  christos 
   6627  1.1  christos   void (*marker_iter_start) (struct marker_iter *iter);
   6628  1.1  christos   void (*marker_iter_next) (struct marker_iter *iter);
   6629  1.1  christos   void (*marker_iter_stop) (struct marker_iter *iter);
   6630  1.1  christos   void (*marker_iter_reset) (struct marker_iter *iter);
   6631  1.1  christos } ust_ops;
   6632  1.1  christos 
   6633  1.1  christos #include <dlfcn.h>
   6634  1.1  christos 
   6635  1.1  christos /* Cast through typeof to catch incompatible API changes.  Since UST
   6636  1.1  christos    only builds with gcc, we can freely use gcc extensions here
   6637  1.1  christos    too.  */
   6638  1.1  christos #define GET_UST_SYM(SYM)					\
   6639  1.1  christos   do								\
   6640  1.1  christos     {								\
   6641  1.1  christos       if (ust_ops.SYM == NULL)					\
   6642  1.1  christos 	ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM);	\
   6643  1.1  christos       if (ust_ops.SYM == NULL)					\
   6644  1.1  christos 	return 0;						\
   6645  1.1  christos     } while (0)
   6646  1.1  christos 
   6647  1.1  christos #define USTF(SYM) ust_ops.SYM
   6648  1.1  christos 
   6649  1.1  christos /* Get pointers to all libust.so functions we care about.  */
   6650  1.1  christos 
   6651  1.1  christos static int
   6652  1.1  christos dlsym_ust (void)
   6653  1.1  christos {
   6654  1.1  christos   GET_UST_SYM (serialize_to_text);
   6655  1.1  christos 
   6656  1.1  christos   GET_UST_SYM (ltt_probe_register);
   6657  1.1  christos   GET_UST_SYM (ltt_probe_unregister);
   6658  1.1  christos   GET_UST_SYM (ltt_marker_connect);
   6659  1.1  christos   GET_UST_SYM (ltt_marker_disconnect);
   6660  1.1  christos 
   6661  1.1  christos   GET_UST_SYM (marker_iter_start);
   6662  1.1  christos   GET_UST_SYM (marker_iter_next);
   6663  1.1  christos   GET_UST_SYM (marker_iter_stop);
   6664  1.1  christos   GET_UST_SYM (marker_iter_reset);
   6665  1.1  christos 
   6666  1.1  christos   ust_loaded = 1;
   6667  1.1  christos   return 1;
   6668  1.1  christos }
   6669  1.1  christos 
   6670  1.1  christos /* Given an UST marker, return the matching gdb static tracepoint.
   6671  1.1  christos    The match is done by address.  */
   6672  1.1  christos 
   6673  1.1  christos static struct tracepoint *
   6674  1.1  christos ust_marker_to_static_tracepoint (const struct marker *mdata)
   6675  1.1  christos {
   6676  1.1  christos   struct tracepoint *tpoint;
   6677  1.1  christos 
   6678  1.1  christos   for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
   6679  1.1  christos     {
   6680  1.1  christos       if (tpoint->type != static_tracepoint)
   6681  1.1  christos 	continue;
   6682  1.1  christos 
   6683  1.1  christos       if (tpoint->address == (uintptr_t) mdata->location)
   6684  1.1  christos 	return tpoint;
   6685  1.1  christos     }
   6686  1.1  christos 
   6687  1.1  christos   return NULL;
   6688  1.1  christos }
   6689  1.1  christos 
   6690  1.1  christos /* The probe function we install on lttng/ust markers.  Whenever a
   6691  1.1  christos    probed ust marker is hit, this function is called.  This is similar
   6692  1.1  christos    to gdb_collect, only for static tracepoints, instead of fast
   6693  1.1  christos    tracepoints.  */
   6694  1.1  christos 
   6695  1.1  christos static void
   6696  1.1  christos gdb_probe (const struct marker *mdata, void *probe_private,
   6697  1.1  christos 	   struct registers *regs, void *call_private,
   6698  1.1  christos 	   const char *fmt, va_list *args)
   6699  1.1  christos {
   6700  1.1  christos   struct tracepoint *tpoint;
   6701  1.1  christos   struct static_tracepoint_ctx ctx;
   6702  1.1  christos   const struct target_desc *ipa_tdesc;
   6703  1.1  christos 
   6704  1.1  christos   /* Don't do anything until the trace run is completely set up.  */
   6705  1.1  christos   if (!tracing)
   6706  1.1  christos     {
   6707  1.1  christos       trace_debug ("gdb_probe: not tracing\n");
   6708  1.1  christos       return;
   6709  1.1  christos     }
   6710  1.1  christos 
   6711  1.1  christos   ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx);
   6712  1.1  christos   ctx.base.type = static_tracepoint;
   6713  1.1  christos   ctx.regcache_initted = 0;
   6714  1.1  christos   ctx.regs = regs;
   6715  1.1  christos   ctx.fmt = fmt;
   6716  1.1  christos   ctx.args = args;
   6717  1.1  christos 
   6718  1.1  christos   /* Wrap the regblock in a register cache (in the stack, we don't
   6719  1.1  christos      want to malloc here).  */
   6720  1.1  christos   ctx.regspace = alloca (ipa_tdesc->registers_size);
   6721  1.1  christos   if (ctx.regspace == NULL)
   6722  1.1  christos     {
   6723  1.1  christos       trace_debug ("Trace buffer block allocation failed, skipping");
   6724  1.1  christos       return;
   6725  1.1  christos     }
   6726  1.1  christos 
   6727  1.1  christos   tpoint = ust_marker_to_static_tracepoint (mdata);
   6728  1.1  christos   if (tpoint == NULL)
   6729  1.1  christos     {
   6730  1.1  christos       trace_debug ("gdb_probe: marker not known: "
   6731  1.1  christos 		   "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
   6732  1.1  christos 		   mdata->location, mdata->channel,
   6733  1.1  christos 		   mdata->name, mdata->format);
   6734  1.1  christos       return;
   6735  1.1  christos     }
   6736  1.1  christos 
   6737  1.1  christos   if (!tpoint->enabled)
   6738  1.1  christos     {
   6739  1.1  christos       trace_debug ("gdb_probe: tracepoint disabled");
   6740  1.1  christos       return;
   6741  1.1  christos     }
   6742  1.1  christos 
   6743  1.1  christos   ctx.tpoint = tpoint;
   6744  1.1  christos 
   6745  1.1  christos   trace_debug ("gdb_probe: collecting marker: "
   6746  1.1  christos 	       "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
   6747  1.1  christos 	       mdata->location, mdata->channel,
   6748  1.1  christos 	       mdata->name, mdata->format);
   6749  1.1  christos 
   6750  1.1  christos   /* Test the condition if present, and collect if true.  */
   6751  1.1  christos   if (tpoint->cond == NULL
   6752  1.1  christos       || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
   6753  1.1  christos 				       tpoint))
   6754  1.1  christos     {
   6755  1.1  christos       collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
   6756  1.1  christos 				  tpoint->address, tpoint);
   6757  1.1  christos 
   6758  1.1  christos       if (stopping_tracepoint
   6759  1.1  christos 	  || trace_buffer_is_full
   6760  1.1  christos 	  || expr_eval_result != expr_eval_no_error)
   6761  1.1  christos 	stop_tracing ();
   6762  1.1  christos     }
   6763  1.1  christos   else
   6764  1.1  christos     {
   6765  1.1  christos       /* If there was a condition and it evaluated to false, the only
   6766  1.1  christos 	 way we would stop tracing is if there was an error during
   6767  1.1  christos 	 condition expression evaluation.  */
   6768  1.1  christos       if (expr_eval_result != expr_eval_no_error)
   6769  1.1  christos 	stop_tracing ();
   6770  1.1  christos     }
   6771  1.1  christos }
   6772  1.1  christos 
   6773  1.1  christos /* Called if the gdb static tracepoint requested collecting "$_sdata",
   6774  1.1  christos    static tracepoint string data.  This is a string passed to the
   6775  1.1  christos    tracing library by the user, at the time of the tracepoint marker
   6776  1.1  christos    call.  E.g., in the UST marker call:
   6777  1.1  christos 
   6778  1.1  christos      trace_mark (ust, bar33, "str %s", "FOOBAZ");
   6779  1.1  christos 
   6780  1.1  christos    the collected data is "str FOOBAZ".
   6781  1.1  christos */
   6782  1.1  christos 
   6783  1.1  christos static void
   6784  1.1  christos collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
   6785  1.1  christos 				struct traceframe *tframe)
   6786  1.1  christos {
   6787  1.1  christos   struct static_tracepoint_ctx *umd = (struct static_tracepoint_ctx *) ctx;
   6788  1.1  christos   unsigned char *bufspace;
   6789  1.1  christos   int size;
   6790  1.1  christos   va_list copy;
   6791  1.1  christos   unsigned short blocklen;
   6792  1.1  christos 
   6793  1.1  christos   if (umd == NULL)
   6794  1.1  christos     {
   6795  1.1  christos       trace_debug ("Wanted to collect static trace data, "
   6796  1.1  christos 		   "but there's no static trace data");
   6797  1.1  christos       return;
   6798  1.1  christos     }
   6799  1.1  christos 
   6800  1.1  christos   va_copy (copy, *umd->args);
   6801  1.1  christos   size = USTF(serialize_to_text) (NULL, 0, umd->fmt, copy);
   6802  1.1  christos   va_end (copy);
   6803  1.1  christos 
   6804  1.1  christos   trace_debug ("Want to collect ust data");
   6805  1.1  christos 
   6806  1.1  christos   /* 'S' + size + string */
   6807  1.1  christos   bufspace = add_traceframe_block (tframe, umd->tpoint,
   6808  1.1  christos 				   1 + sizeof (blocklen) + size + 1);
   6809  1.1  christos   if (bufspace == NULL)
   6810  1.1  christos     {
   6811  1.1  christos       trace_debug ("Trace buffer block allocation failed, skipping");
   6812  1.1  christos       return;
   6813  1.1  christos     }
   6814  1.1  christos 
   6815  1.1  christos   /* Identify a static trace data block.  */
   6816  1.1  christos   *bufspace = 'S';
   6817  1.1  christos 
   6818  1.1  christos   blocklen = size + 1;
   6819  1.1  christos   memcpy (bufspace + 1, &blocklen, sizeof (blocklen));
   6820  1.1  christos 
   6821  1.1  christos   va_copy (copy, *umd->args);
   6822  1.1  christos   USTF(serialize_to_text) ((char *) bufspace + 1 + sizeof (blocklen),
   6823  1.1  christos 			   size + 1, umd->fmt, copy);
   6824  1.1  christos   va_end (copy);
   6825  1.1  christos 
   6826  1.1  christos   trace_debug ("Storing static tracepoint data in regblock: %s",
   6827  1.1  christos 	       bufspace + 1 + sizeof (blocklen));
   6828  1.1  christos }
   6829  1.1  christos 
   6830  1.1  christos /* The probe to register with lttng/ust.  */
   6831  1.1  christos static struct ltt_available_probe gdb_ust_probe =
   6832  1.1  christos   {
   6833  1.1  christos     GDB_PROBE_NAME,
   6834  1.1  christos     NULL,
   6835  1.1  christos     gdb_probe,
   6836  1.1  christos   };
   6837  1.1  christos 
   6838  1.1  christos #endif /* HAVE_UST */
   6839  1.1  christos #endif /* IN_PROCESS_AGENT */
   6840  1.1  christos 
   6841  1.1  christos #ifndef IN_PROCESS_AGENT
   6842  1.1  christos 
   6843  1.1  christos /* Ask the in-process agent to run a command.  Since we don't want to
   6844  1.1  christos    have to handle the IPA hitting breakpoints while running the
   6845  1.1  christos    command, we pause all threads, remove all breakpoints, and then set
   6846  1.1  christos    the helper thread re-running.  We communicate with the helper
   6847  1.1  christos    thread by means of direct memory xfering, and a socket for
   6848  1.1  christos    synchronization.  */
   6849  1.1  christos 
   6850  1.1  christos static int
   6851  1.1  christos run_inferior_command (char *cmd, int len)
   6852  1.1  christos {
   6853  1.1  christos   int err = -1;
   6854  1.1  christos   int pid = current_ptid.pid ();
   6855  1.1  christos 
   6856  1.1  christos   trace_debug ("run_inferior_command: running: %s", cmd);
   6857  1.1  christos 
   6858  1.1  christos   target_pause_all (false);
   6859  1.1  christos   uninsert_all_breakpoints ();
   6860  1.1  christos 
   6861  1.1  christos   err = agent_run_command (pid, (const char *) cmd, len);
   6862  1.1  christos 
   6863  1.1  christos   reinsert_all_breakpoints ();
   6864  1.1  christos   target_unpause_all (false);
   6865  1.1  christos 
   6866  1.1  christos   return err;
   6867  1.1  christos }
   6868  1.1  christos 
   6869  1.1  christos #else /* !IN_PROCESS_AGENT */
   6870  1.1  christos 
   6871  1.1  christos #include <sys/socket.h>
   6872  1.1  christos #include <sys/un.h>
   6873  1.1  christos 
   6874  1.1  christos #ifndef UNIX_PATH_MAX
   6875  1.1  christos #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
   6876  1.1  christos #endif
   6877  1.1  christos 
   6878  1.1  christos /* Where we put the socked used for synchronization.  */
   6879  1.1  christos #define SOCK_DIR P_tmpdir
   6880  1.1  christos 
   6881  1.1  christos /* Thread ID of the helper thread.  GDBserver reads this to know which
   6882  1.1  christos    is the help thread.  This is an LWP id on Linux.  */
   6883  1.1  christos EXTERN_C_PUSH
   6884  1.1  christos IP_AGENT_EXPORT_VAR int helper_thread_id;
   6885  1.1  christos EXTERN_C_POP
   6886  1.1  christos 
   6887  1.1  christos static int
   6888  1.1  christos init_named_socket (const char *name)
   6889  1.1  christos {
   6890  1.1  christos   int result, fd;
   6891  1.1  christos   struct sockaddr_un addr;
   6892  1.1  christos 
   6893  1.1  christos   result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
   6894  1.1  christos   if (result == -1)
   6895  1.1  christos     {
   6896  1.1  christos       warning ("socket creation failed: %s", safe_strerror (errno));
   6897  1.1  christos       return -1;
   6898  1.1  christos     }
   6899  1.1  christos 
   6900  1.1  christos   addr.sun_family = AF_UNIX;
   6901  1.1  christos 
   6902  1.1  christos   strncpy (addr.sun_path, name, UNIX_PATH_MAX);
   6903  1.1  christos   addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
   6904  1.1  christos 
   6905  1.1  christos   result = access (name, F_OK);
   6906  1.1  christos   if (result == 0)
   6907  1.1  christos     {
   6908  1.1  christos       /* File exists.  */
   6909  1.1  christos       result = unlink (name);
   6910  1.1  christos       if (result == -1)
   6911  1.1  christos 	{
   6912  1.1  christos 	  warning ("unlink failed: %s", safe_strerror (errno));
   6913  1.1  christos 	  close (fd);
   6914  1.1  christos 	  return -1;
   6915  1.1  christos 	}
   6916  1.1  christos       warning ("socket %s already exists; overwriting", name);
   6917  1.1  christos     }
   6918  1.1  christos 
   6919  1.1  christos   result = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
   6920  1.1  christos   if (result == -1)
   6921  1.1  christos     {
   6922  1.1  christos       warning ("bind failed: %s", safe_strerror (errno));
   6923  1.1  christos       close (fd);
   6924  1.1  christos       return -1;
   6925  1.1  christos     }
   6926  1.1  christos 
   6927  1.1  christos   result = listen (fd, 1);
   6928  1.1  christos   if (result == -1)
   6929  1.1  christos     {
   6930  1.1  christos       warning ("listen: %s", safe_strerror (errno));
   6931  1.1  christos       close (fd);
   6932  1.1  christos       return -1;
   6933  1.1  christos     }
   6934  1.1  christos 
   6935  1.1  christos   return fd;
   6936  1.1  christos }
   6937  1.1  christos 
   6938  1.1  christos static char agent_socket_name[UNIX_PATH_MAX];
   6939  1.1  christos 
   6940  1.1  christos static int
   6941  1.1  christos gdb_agent_socket_init (void)
   6942  1.1  christos {
   6943  1.1  christos   int result, fd;
   6944  1.1  christos 
   6945  1.1  christos   result = xsnprintf (agent_socket_name, UNIX_PATH_MAX, "%s/gdb_ust%d",
   6946  1.1  christos 		      SOCK_DIR, getpid ());
   6947  1.1  christos   if (result >= UNIX_PATH_MAX)
   6948  1.1  christos     {
   6949  1.1  christos       trace_debug ("string overflow allocating socket name");
   6950  1.1  christos       return -1;
   6951  1.1  christos     }
   6952  1.1  christos 
   6953  1.1  christos   fd = init_named_socket (agent_socket_name);
   6954  1.1  christos   if (fd < 0)
   6955  1.1  christos     warning ("Error initializing named socket (%s) for communication with the "
   6956  1.1  christos 	     "ust helper thread. Check that directory exists and that it "
   6957  1.1  christos 	     "is writable.", agent_socket_name);
   6958  1.1  christos 
   6959  1.1  christos   return fd;
   6960  1.1  christos }
   6961  1.1  christos 
   6962  1.1  christos #ifdef HAVE_UST
   6963  1.1  christos 
   6964  1.1  christos /* The next marker to be returned on a qTsSTM command.  */
   6965  1.1  christos static const struct marker *next_st;
   6966  1.1  christos 
   6967  1.1  christos /* Returns the first known marker.  */
   6968  1.1  christos 
   6969  1.1  christos struct marker *
   6970  1.1  christos first_marker (void)
   6971  1.1  christos {
   6972  1.1  christos   struct marker_iter iter;
   6973  1.1  christos 
   6974  1.1  christos   USTF(marker_iter_reset) (&iter);
   6975  1.1  christos   USTF(marker_iter_start) (&iter);
   6976  1.1  christos 
   6977  1.1  christos   return iter.marker;
   6978  1.1  christos }
   6979  1.1  christos 
   6980  1.1  christos /* Returns the marker following M.  */
   6981  1.1  christos 
   6982  1.1  christos const struct marker *
   6983  1.1  christos next_marker (const struct marker *m)
   6984  1.1  christos {
   6985  1.1  christos   struct marker_iter iter;
   6986  1.1  christos 
   6987  1.1  christos   USTF(marker_iter_reset) (&iter);
   6988  1.1  christos   USTF(marker_iter_start) (&iter);
   6989  1.1  christos 
   6990  1.1  christos   for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
   6991  1.1  christos     {
   6992  1.1  christos       if (iter.marker == m)
   6993  1.1  christos 	{
   6994  1.1  christos 	  USTF(marker_iter_next) (&iter);
   6995  1.1  christos 	  return iter.marker;
   6996  1.1  christos 	}
   6997  1.1  christos     }
   6998  1.1  christos 
   6999  1.1  christos   return NULL;
   7000  1.1  christos }
   7001  1.1  christos 
   7002  1.1  christos /* Return an hexstr version of the STR C string, fit for sending to
   7003  1.1  christos    GDB.  */
   7004  1.1  christos 
   7005  1.1  christos static char *
   7006  1.1  christos cstr_to_hexstr (const char *str)
   7007  1.1  christos {
   7008  1.1  christos   int len = strlen (str);
   7009  1.1  christos   char *hexstr = xmalloc (len * 2 + 1);
   7010  1.1  christos   bin2hex ((gdb_byte *) str, hexstr, len);
   7011  1.1  christos   return hexstr;
   7012  1.1  christos }
   7013  1.1  christos 
   7014  1.1  christos /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
   7015  1.1  christos    packets.  */
   7016  1.1  christos 
   7017  1.1  christos static void
   7018  1.1  christos response_ust_marker (char *packet, const struct marker *st)
   7019  1.1  christos {
   7020  1.1  christos   char *strid, *format, *tmp;
   7021  1.1  christos 
   7022  1.1  christos   next_st = next_marker (st);
   7023  1.1  christos 
   7024  1.1  christos   tmp = xmalloc (strlen (st->channel) + 1 +
   7025  1.1  christos 		 strlen (st->name) + 1);
   7026  1.1  christos   sprintf (tmp, "%s/%s", st->channel, st->name);
   7027  1.1  christos 
   7028  1.1  christos   strid = cstr_to_hexstr (tmp);
   7029  1.1  christos   free (tmp);
   7030  1.1  christos 
   7031  1.1  christos   format = cstr_to_hexstr (st->format);
   7032  1.1  christos 
   7033  1.1  christos   sprintf (packet, "m%s:%s:%s",
   7034  1.1  christos 	   paddress ((uintptr_t) st->location),
   7035  1.1  christos 	   strid,
   7036  1.1  christos 	   format);
   7037  1.1  christos 
   7038  1.1  christos   free (strid);
   7039  1.1  christos   free (format);
   7040  1.1  christos }
   7041  1.1  christos 
   7042  1.1  christos /* Return the first static tracepoint, and initialize the state
   7043  1.1  christos    machine that will iterate through all the static tracepoints.  */
   7044  1.1  christos 
   7045  1.1  christos static void
   7046  1.1  christos cmd_qtfstm (char *packet)
   7047  1.1  christos {
   7048  1.1  christos   trace_debug ("Returning first trace state variable definition");
   7049  1.1  christos 
   7050  1.1  christos   if (first_marker ())
   7051  1.1  christos     response_ust_marker (packet, first_marker ());
   7052  1.1  christos   else
   7053  1.1  christos     strcpy (packet, "l");
   7054  1.1  christos }
   7055  1.1  christos 
   7056  1.1  christos /* Return additional trace state variable definitions. */
   7057  1.1  christos 
   7058  1.1  christos static void
   7059  1.1  christos cmd_qtsstm (char *packet)
   7060  1.1  christos {
   7061  1.1  christos   trace_debug ("Returning static tracepoint");
   7062  1.1  christos 
   7063  1.1  christos   if (next_st)
   7064  1.1  christos     response_ust_marker (packet, next_st);
   7065  1.1  christos   else
   7066  1.1  christos     strcpy (packet, "l");
   7067  1.1  christos }
   7068  1.1  christos 
   7069  1.1  christos /* Disconnect the GDB probe from a marker at a given address.  */
   7070  1.1  christos 
   7071  1.1  christos static void
   7072  1.1  christos unprobe_marker_at (char *packet)
   7073  1.1  christos {
   7074  1.1  christos   char *p = packet;
   7075  1.1  christos   ULONGEST address;
   7076  1.1  christos   struct marker_iter iter;
   7077  1.1  christos 
   7078  1.1  christos   p += sizeof ("unprobe_marker_at:") - 1;
   7079  1.1  christos 
   7080  1.1  christos   p = unpack_varlen_hex (p, &address);
   7081  1.1  christos 
   7082  1.1  christos   USTF(marker_iter_reset) (&iter);
   7083  1.1  christos   USTF(marker_iter_start) (&iter);
   7084  1.1  christos   for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
   7085  1.1  christos     if ((uintptr_t ) iter.marker->location == address)
   7086  1.1  christos       {
   7087  1.1  christos 	int result;
   7088  1.1  christos 
   7089  1.1  christos 	result = USTF(ltt_marker_disconnect) (iter.marker->channel,
   7090  1.1  christos 					      iter.marker->name,
   7091  1.1  christos 					      GDB_PROBE_NAME);
   7092  1.1  christos 	if (result < 0)
   7093  1.1  christos 	  warning ("could not disable marker %s/%s",
   7094  1.1  christos 		   iter.marker->channel, iter.marker->name);
   7095  1.1  christos 	break;
   7096  1.1  christos       }
   7097  1.1  christos }
   7098  1.1  christos 
   7099  1.1  christos /* Connect the GDB probe to a marker at a given address.  */
   7100  1.1  christos 
   7101  1.1  christos static int
   7102  1.1  christos probe_marker_at (char *packet)
   7103  1.1  christos {
   7104  1.1  christos   char *p = packet;
   7105  1.1  christos   ULONGEST address;
   7106  1.1  christos   struct marker_iter iter;
   7107  1.1  christos   struct marker *m;
   7108  1.1  christos 
   7109  1.1  christos   p += sizeof ("probe_marker_at:") - 1;
   7110  1.1  christos 
   7111  1.1  christos   p = unpack_varlen_hex (p, &address);
   7112  1.1  christos 
   7113  1.1  christos   USTF(marker_iter_reset) (&iter);
   7114  1.1  christos 
   7115  1.1  christos   for (USTF(marker_iter_start) (&iter), m = iter.marker;
   7116  1.1  christos        m != NULL;
   7117  1.1  christos        USTF(marker_iter_next) (&iter), m = iter.marker)
   7118  1.1  christos     if ((uintptr_t ) m->location == address)
   7119  1.1  christos       {
   7120  1.1  christos 	int result;
   7121  1.1  christos 
   7122  1.1  christos 	trace_debug ("found marker for address.  "
   7123  1.1  christos 		     "ltt_marker_connect (marker = %s/%s)",
   7124  1.1  christos 		     m->channel, m->name);
   7125  1.1  christos 
   7126  1.1  christos 	result = USTF(ltt_marker_connect) (m->channel, m->name,
   7127  1.1  christos 					   GDB_PROBE_NAME);
   7128  1.1  christos 	if (result && result != -EEXIST)
   7129  1.1  christos 	  trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
   7130  1.1  christos 		       m->channel, m->name, -result);
   7131  1.1  christos 
   7132  1.1  christos 	if (result < 0)
   7133  1.1  christos 	  {
   7134  1.1  christos 	    sprintf (packet, "E.could not connect marker: channel=%s, name=%s",
   7135  1.1  christos 		     m->channel, m->name);
   7136  1.1  christos 	    return -1;
   7137  1.1  christos 	  }
   7138  1.1  christos 
   7139  1.1  christos 	strcpy (packet, "OK");
   7140  1.1  christos 	return 0;
   7141  1.1  christos       }
   7142  1.1  christos 
   7143  1.1  christos   sprintf (packet, "E.no marker found at 0x%s", paddress (address));
   7144  1.1  christos   return -1;
   7145  1.1  christos }
   7146  1.1  christos 
   7147  1.1  christos static int
   7148  1.1  christos cmd_qtstmat (char *packet)
   7149  1.1  christos {
   7150  1.1  christos   char *p = packet;
   7151  1.1  christos   ULONGEST address;
   7152  1.1  christos   struct marker_iter iter;
   7153  1.1  christos   struct marker *m;
   7154  1.1  christos 
   7155  1.1  christos   p += sizeof ("qTSTMat:") - 1;
   7156  1.1  christos 
   7157  1.1  christos   p = unpack_varlen_hex (p, &address);
   7158  1.1  christos 
   7159  1.1  christos   USTF(marker_iter_reset) (&iter);
   7160  1.1  christos 
   7161  1.1  christos   for (USTF(marker_iter_start) (&iter), m = iter.marker;
   7162  1.1  christos        m != NULL;
   7163  1.1  christos        USTF(marker_iter_next) (&iter), m = iter.marker)
   7164  1.1  christos     if ((uintptr_t ) m->location == address)
   7165  1.1  christos       {
   7166  1.1  christos 	response_ust_marker (packet, m);
   7167  1.1  christos 	return 0;
   7168  1.1  christos       }
   7169  1.1  christos 
   7170  1.1  christos   strcpy (packet, "l");
   7171  1.1  christos   return -1;
   7172  1.1  christos }
   7173  1.1  christos 
   7174  1.1  christos static void
   7175  1.1  christos gdb_ust_init (void)
   7176  1.1  christos {
   7177  1.1  christos   if (!dlsym_ust ())
   7178  1.1  christos     return;
   7179  1.1  christos 
   7180  1.1  christos   USTF(ltt_probe_register) (&gdb_ust_probe);
   7181  1.1  christos }
   7182  1.1  christos 
   7183  1.1  christos #endif /* HAVE_UST */
   7184  1.1  christos 
   7185  1.1  christos #include <sys/syscall.h>
   7186  1.1  christos 
   7187  1.1  christos static void
   7188  1.1  christos gdb_agent_remove_socket (void)
   7189  1.1  christos {
   7190  1.1  christos   unlink (agent_socket_name);
   7191  1.1  christos }
   7192  1.1  christos 
   7193  1.1  christos /* Helper thread of agent.  */
   7194  1.1  christos 
   7195  1.1  christos static void *
   7196  1.1  christos gdb_agent_helper_thread (void *arg)
   7197  1.1  christos {
   7198  1.1  christos   int listen_fd;
   7199  1.1  christos 
   7200  1.1  christos   atexit (gdb_agent_remove_socket);
   7201  1.1  christos 
   7202  1.1  christos   while (1)
   7203  1.1  christos     {
   7204  1.1  christos       listen_fd = gdb_agent_socket_init ();
   7205  1.1  christos 
   7206  1.1  christos       if (helper_thread_id == 0)
   7207  1.1  christos 	helper_thread_id = syscall (SYS_gettid);
   7208  1.1  christos 
   7209  1.1  christos       if (listen_fd == -1)
   7210  1.1  christos 	{
   7211  1.1  christos 	  warning ("could not create sync socket");
   7212  1.1  christos 	  break;
   7213  1.1  christos 	}
   7214  1.1  christos 
   7215  1.1  christos       while (1)
   7216  1.1  christos 	{
   7217  1.1  christos 	  socklen_t tmp;
   7218  1.1  christos 	  struct sockaddr_un sockaddr;
   7219  1.1  christos 	  int fd;
   7220  1.1  christos 	  char buf[1];
   7221  1.1  christos 	  int ret;
   7222  1.1  christos 	  int stop_loop = 0;
   7223  1.1  christos 
   7224  1.1  christos 	  tmp = sizeof (sockaddr);
   7225  1.1  christos 
   7226  1.1  christos 	  do
   7227  1.1  christos 	    {
   7228  1.1  christos 	      fd = accept (listen_fd, (struct sockaddr *) &sockaddr, &tmp);
   7229  1.1  christos 	    }
   7230  1.1  christos 	  /* It seems an ERESTARTSYS can escape out of accept.  */
   7231  1.1  christos 	  while (fd == -512 || (fd == -1 && errno == EINTR));
   7232  1.1  christos 
   7233  1.1  christos 	  if (fd < 0)
   7234  1.1  christos 	    {
   7235  1.1  christos 	      warning ("Accept returned %d, error: %s",
   7236  1.1  christos 		       fd, safe_strerror (errno));
   7237  1.1  christos 	      break;
   7238  1.1  christos 	    }
   7239  1.1  christos 
   7240  1.1  christos 	  do
   7241  1.1  christos 	    {
   7242  1.1  christos 	      ret = read (fd, buf, 1);
   7243  1.1  christos 	    } while (ret == -1 && errno == EINTR);
   7244  1.1  christos 
   7245  1.1  christos 	  if (ret == -1)
   7246  1.1  christos 	    {
   7247  1.1  christos 	      warning ("reading socket (fd=%d) failed with %s",
   7248  1.1  christos 		       fd, safe_strerror (errno));
   7249  1.1  christos 	      close (fd);
   7250  1.1  christos 	      break;
   7251  1.1  christos 	    }
   7252  1.1  christos 
   7253  1.1  christos 	  if (cmd_buf[0])
   7254  1.1  christos 	    {
   7255  1.1  christos 	      if (startswith (cmd_buf, "close"))
   7256  1.1  christos 		{
   7257  1.1  christos 		  stop_loop = 1;
   7258  1.1  christos 		}
   7259  1.1  christos #ifdef HAVE_UST
   7260  1.1  christos 	      else if (strcmp ("qTfSTM", cmd_buf) == 0)
   7261  1.1  christos 		{
   7262  1.1  christos 		  cmd_qtfstm (cmd_buf);
   7263  1.1  christos 		}
   7264  1.1  christos 	      else if (strcmp ("qTsSTM", cmd_buf) == 0)
   7265  1.1  christos 		{
   7266  1.1  christos 		  cmd_qtsstm (cmd_buf);
   7267  1.1  christos 		}
   7268  1.1  christos 	      else if (startswith (cmd_buf, "unprobe_marker_at:"))
   7269  1.1  christos 		{
   7270  1.1  christos 		  unprobe_marker_at (cmd_buf);
   7271  1.1  christos 		}
   7272  1.1  christos 	      else if (startswith (cmd_buf, "probe_marker_at:"))
   7273  1.1  christos 		{
   7274  1.1  christos 		  probe_marker_at (cmd_buf);
   7275  1.1  christos 		}
   7276  1.1  christos 	      else if (startswith (cmd_buf, "qTSTMat:"))
   7277  1.1  christos 		{
   7278  1.1  christos 		  cmd_qtstmat (cmd_buf);
   7279  1.1  christos 		}
   7280  1.1  christos #endif /* HAVE_UST */
   7281  1.1  christos 	    }
   7282  1.1  christos 
   7283  1.1  christos 	  /* Fix compiler's warning: ignoring return value of 'write'.  */
   7284  1.1  christos 	  ret = write (fd, buf, 1);
   7285  1.1  christos 	  close (fd);
   7286  1.1  christos 
   7287  1.1  christos 	  if (stop_loop)
   7288  1.1  christos 	    {
   7289  1.1  christos 	      close (listen_fd);
   7290  1.1  christos 	      unlink (agent_socket_name);
   7291  1.1  christos 
   7292  1.1  christos 	      /* Sleep endlessly to wait the whole inferior stops.  This
   7293  1.1  christos 		 thread can not exit because GDB or GDBserver may still need
   7294  1.1  christos 		 'current_thread' (representing this thread) to access
   7295  1.1  christos 		 inferior memory.  Otherwise, this thread exits earlier than
   7296  1.1  christos 		 other threads, and 'current_thread' is set to NULL.  */
   7297  1.1  christos 	      while (1)
   7298  1.1  christos 		sleep (10);
   7299  1.1  christos 	    }
   7300  1.1  christos 	}
   7301  1.1  christos     }
   7302  1.1  christos 
   7303  1.1  christos   return NULL;
   7304  1.1  christos }
   7305  1.1  christos 
   7306  1.1  christos #include <signal.h>
   7307  1.1  christos #include <pthread.h>
   7308  1.1  christos 
   7309  1.1  christos EXTERN_C_PUSH
   7310  1.1  christos IP_AGENT_EXPORT_VAR int gdb_agent_capability = AGENT_CAPA_STATIC_TRACE;
   7311  1.1  christos EXTERN_C_POP
   7312  1.1  christos 
   7313  1.1  christos static void
   7314  1.1  christos gdb_agent_init (void)
   7315  1.1  christos {
   7316  1.1  christos   int res;
   7317  1.1  christos   pthread_t thread;
   7318  1.1  christos   sigset_t new_mask;
   7319  1.1  christos   sigset_t orig_mask;
   7320  1.1  christos 
   7321  1.1  christos   /* We want the helper thread to be as transparent as possible, so
   7322  1.1  christos      have it inherit an all-signals-blocked mask.  */
   7323  1.1  christos 
   7324  1.1  christos   sigfillset (&new_mask);
   7325  1.1  christos   res = pthread_sigmask (SIG_SETMASK, &new_mask, &orig_mask);
   7326  1.1  christos   if (res)
   7327  1.1  christos     perror_with_name ("pthread_sigmask (1)");
   7328  1.1  christos 
   7329  1.1  christos   res = pthread_create (&thread,
   7330  1.1  christos 			NULL,
   7331  1.1  christos 			gdb_agent_helper_thread,
   7332  1.1  christos 			NULL);
   7333  1.1  christos 
   7334  1.1  christos   res = pthread_sigmask (SIG_SETMASK, &orig_mask, NULL);
   7335  1.1  christos   if (res)
   7336  1.1  christos     perror_with_name ("pthread_sigmask (2)");
   7337  1.1  christos 
   7338  1.1  christos   while (helper_thread_id == 0)
   7339  1.1  christos     usleep (1);
   7340  1.1  christos 
   7341  1.1  christos #ifdef HAVE_UST
   7342  1.1  christos   gdb_ust_init ();
   7343  1.1  christos #endif
   7344  1.1  christos }
   7345  1.1  christos 
   7346  1.1  christos #include <sys/mman.h>
   7347  1.1  christos 
   7348  1.1  christos IP_AGENT_EXPORT_VAR char *gdb_tp_heap_buffer;
   7349  1.1  christos IP_AGENT_EXPORT_VAR char *gdb_jump_pad_buffer;
   7350  1.1  christos IP_AGENT_EXPORT_VAR char *gdb_jump_pad_buffer_end;
   7351  1.1  christos IP_AGENT_EXPORT_VAR char *gdb_trampoline_buffer;
   7352  1.1  christos IP_AGENT_EXPORT_VAR char *gdb_trampoline_buffer_end;
   7353  1.1  christos IP_AGENT_EXPORT_VAR char *gdb_trampoline_buffer_error;
   7354  1.1  christos 
   7355  1.1  christos /* Record the result of getting buffer space for fast tracepoint
   7356  1.1  christos    trampolines.  Any error message is copied, since caller may not be
   7357  1.1  christos    using persistent storage.  */
   7358  1.1  christos 
   7359  1.1  christos void
   7360  1.1  christos set_trampoline_buffer_space (CORE_ADDR begin, CORE_ADDR end, char *errmsg)
   7361  1.1  christos {
   7362  1.1  christos   gdb_trampoline_buffer = (char *) (uintptr_t) begin;
   7363  1.1  christos   gdb_trampoline_buffer_end = (char *) (uintptr_t) end;
   7364  1.1  christos   if (errmsg)
   7365  1.1  christos     strncpy (gdb_trampoline_buffer_error, errmsg, 99);
   7366  1.1  christos   else
   7367  1.1  christos     strcpy (gdb_trampoline_buffer_error, "no buffer passed");
   7368  1.1  christos }
   7369  1.1  christos 
   7370  1.1  christos static void __attribute__ ((constructor))
   7371  1.1  christos initialize_tracepoint_ftlib (void)
   7372  1.1  christos {
   7373  1.1  christos   initialize_tracepoint ();
   7374  1.1  christos 
   7375  1.1  christos   gdb_agent_init ();
   7376  1.1  christos }
   7377  1.1  christos 
   7378  1.1  christos #ifndef HAVE_GETAUXVAL
   7379  1.1  christos /* Retrieve the value of TYPE from the auxiliary vector.  If TYPE is not
   7380  1.1  christos    found, 0 is returned.  This function is provided if glibc is too old.  */
   7381  1.1  christos 
   7382  1.1  christos unsigned long
   7383  1.1  christos getauxval (unsigned long type)
   7384  1.1  christos {
   7385  1.1  christos   unsigned long data[2];
   7386  1.1  christos   FILE *f = fopen ("/proc/self/auxv", "r");
   7387  1.1  christos   unsigned long value = 0;
   7388  1.1  christos 
   7389  1.1  christos   if (f == NULL)
   7390  1.1  christos     return 0;
   7391  1.1  christos 
   7392  1.1  christos   while (fread (data, sizeof (data), 1, f) > 0)
   7393  1.1  christos     {
   7394  1.1  christos       if (data[0] == type)
   7395  1.1  christos 	{
   7396  1.1  christos 	  value = data[1];
   7397  1.1  christos 	  break;
   7398  1.1  christos 	}
   7399  1.1  christos     }
   7400  1.1  christos 
   7401  1.1  christos   fclose (f);
   7402  1.1  christos   return value;
   7403  1.1  christos }
   7404  1.1  christos #endif
   7405  1.1  christos 
   7406  1.1  christos #endif /* IN_PROCESS_AGENT */
   7407  1.1  christos 
   7408  1.1  christos /* Return a timestamp, expressed as microseconds of the usual Unix
   7409  1.1  christos    time.  (As the result is a 64-bit number, it will not overflow any
   7410  1.1  christos    time soon.)  */
   7411  1.1  christos 
   7412  1.1  christos static LONGEST
   7413  1.1  christos get_timestamp (void)
   7414  1.1  christos {
   7415  1.1  christos   using namespace std::chrono;
   7416  1.1  christos 
   7417  1.1  christos   steady_clock::time_point now = steady_clock::now ();
   7418  1.1  christos   return duration_cast<microseconds> (now.time_since_epoch ()).count ();
   7419  1.1  christos }
   7420  1.1  christos 
   7421  1.1  christos void
   7422  1.1  christos initialize_tracepoint (void)
   7423  1.1  christos {
   7424  1.1  christos   /* Start with the default size.  */
   7425  1.1  christos   init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE);
   7426  1.1  christos 
   7427  1.1  christos   /* Wire trace state variable 1 to be the timestamp.  This will be
   7428  1.1  christos      uploaded to GDB upon connection and become one of its trace state
   7429  1.1  christos      variables.  (In case you're wondering, if GDB already has a trace
   7430  1.1  christos      variable numbered 1, it will be renumbered.)  */
   7431  1.1  christos   create_trace_state_variable (1, 0);
   7432  1.1  christos   set_trace_state_variable_name (1, "trace_timestamp");
   7433  1.1  christos   set_trace_state_variable_getter (1, get_timestamp);
   7434  1.1  christos 
   7435  1.1  christos #ifdef IN_PROCESS_AGENT
   7436  1.1  christos   {
   7437  1.1  christos     int pagesize;
   7438  1.1  christos     size_t jump_pad_size;
   7439  1.1  christos 
   7440  1.1  christos     pagesize = sysconf (_SC_PAGE_SIZE);
   7441  1.1  christos     if (pagesize == -1)
   7442  1.1  christos       perror_with_name ("sysconf");
   7443  1.1  christos 
   7444  1.1  christos #define SCRATCH_BUFFER_NPAGES 20
   7445  1.1  christos 
   7446  1.1  christos     jump_pad_size = pagesize * SCRATCH_BUFFER_NPAGES;
   7447  1.1  christos 
   7448  1.1  christos     gdb_tp_heap_buffer = (char *) xmalloc (5 * 1024 * 1024);
   7449  1.1  christos     gdb_jump_pad_buffer = (char *) alloc_jump_pad_buffer (jump_pad_size);
   7450  1.1  christos     if (gdb_jump_pad_buffer == NULL)
   7451  1.1  christos       perror_with_name ("mmap");
   7452  1.1  christos     gdb_jump_pad_buffer_end = gdb_jump_pad_buffer + jump_pad_size;
   7453  1.1  christos   }
   7454  1.1  christos 
   7455  1.1  christos   gdb_trampoline_buffer = gdb_trampoline_buffer_end = 0;
   7456  1.1  christos 
   7457  1.1  christos   /* It's not a fatal error for something to go wrong with trampoline
   7458  1.1  christos      buffer setup, but it can be mysterious, so create a channel to
   7459  1.1  christos      report back on what went wrong, using a fixed size since we may
   7460  1.1  christos      not be able to allocate space later when the problem occurs.  */
   7461  1.1  christos   gdb_trampoline_buffer_error = (char *) xmalloc (IPA_BUFSIZ);
   7462  1.1  christos 
   7463  1.1  christos   strcpy (gdb_trampoline_buffer_error, "No errors reported");
   7464  1.1  christos 
   7465  1.1  christos   initialize_low_tracepoint ();
   7466  1.1  christos #endif
   7467  1.1  christos }
   7468