Home | History | Annotate | Line # | Download | only in gdbsupport
      1      1.1  christos /* Branch trace support for GDB, the GNU debugger.
      2      1.1  christos 
      3  1.1.1.3  christos    Copyright (C) 2013-2024 Free Software Foundation, Inc.
      4      1.1  christos 
      5      1.1  christos    Contributed by Intel Corp. <markus.t.metzger (at) intel.com>.
      6      1.1  christos 
      7      1.1  christos    This file is part of GDB.
      8      1.1  christos 
      9      1.1  christos    This program is free software; you can redistribute it and/or modify
     10      1.1  christos    it under the terms of the GNU General Public License as published by
     11      1.1  christos    the Free Software Foundation; either version 3 of the License, or
     12      1.1  christos    (at your option) any later version.
     13      1.1  christos 
     14      1.1  christos    This program is distributed in the hope that it will be useful,
     15      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     16      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17      1.1  christos    GNU General Public License for more details.
     18      1.1  christos 
     19      1.1  christos    You should have received a copy of the GNU General Public License
     20      1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     21      1.1  christos 
     22      1.1  christos #ifndef COMMON_BTRACE_COMMON_H
     23      1.1  christos #define COMMON_BTRACE_COMMON_H
     24      1.1  christos 
     25      1.1  christos /* Branch tracing (btrace) is a per-thread control-flow execution trace of the
     26      1.1  christos    inferior.  For presentation purposes, the branch trace is represented as a
     27      1.1  christos    list of sequential control-flow blocks, one such list per thread.  */
     28      1.1  christos 
     29      1.1  christos /* A branch trace block.
     30      1.1  christos 
     31      1.1  christos    This represents a block of sequential control-flow.  Adjacent blocks will be
     32      1.1  christos    connected via calls, returns, or jumps.  The latter can be direct or
     33      1.1  christos    indirect, conditional or unconditional.  Branches can further be
     34      1.1  christos    asynchronous, e.g. interrupts.  */
     35      1.1  christos struct btrace_block
     36      1.1  christos {
     37      1.1  christos   /* The address of the first byte of the first instruction in the block.
     38      1.1  christos      The address may be zero if we do not know the beginning of this block,
     39      1.1  christos      such as for the first block in a delta trace.  */
     40      1.1  christos   CORE_ADDR begin;
     41      1.1  christos 
     42      1.1  christos   /* The address of the first byte of the last instruction in the block.  */
     43      1.1  christos   CORE_ADDR end;
     44      1.1  christos 
     45      1.1  christos   /* Simple constructor.  */
     46      1.1  christos   btrace_block (CORE_ADDR begin, CORE_ADDR end)
     47      1.1  christos     : begin (begin),
     48      1.1  christos       end (end)
     49      1.1  christos   {
     50      1.1  christos     /* Nothing.  */
     51      1.1  christos   }
     52      1.1  christos };
     53      1.1  christos 
     54      1.1  christos /* Enumeration of btrace formats.  */
     55      1.1  christos 
     56      1.1  christos enum btrace_format
     57      1.1  christos {
     58      1.1  christos   /* No branch trace format.  */
     59      1.1  christos   BTRACE_FORMAT_NONE,
     60      1.1  christos 
     61      1.1  christos   /* Branch trace is in Branch Trace Store (BTS) format.
     62      1.1  christos      Actually, the format is a sequence of blocks derived from BTS.  */
     63      1.1  christos   BTRACE_FORMAT_BTS,
     64      1.1  christos 
     65      1.1  christos   /* Branch trace is in Intel Processor Trace format.  */
     66      1.1  christos   BTRACE_FORMAT_PT
     67      1.1  christos };
     68      1.1  christos 
     69      1.1  christos /* An enumeration of cpu vendors.  */
     70      1.1  christos 
     71      1.1  christos enum btrace_cpu_vendor
     72      1.1  christos {
     73      1.1  christos   /* We do not know this vendor.  */
     74      1.1  christos   CV_UNKNOWN,
     75      1.1  christos 
     76      1.1  christos   /* Intel.  */
     77      1.1  christos   CV_INTEL,
     78      1.1  christos 
     79      1.1  christos   /* AMD.  */
     80      1.1  christos   CV_AMD
     81      1.1  christos };
     82      1.1  christos 
     83      1.1  christos /* A cpu identifier.  */
     84      1.1  christos 
     85      1.1  christos struct btrace_cpu
     86      1.1  christos {
     87      1.1  christos   /* The processor vendor.  */
     88      1.1  christos   enum btrace_cpu_vendor vendor;
     89      1.1  christos 
     90      1.1  christos   /* The cpu family.  */
     91      1.1  christos   unsigned short family;
     92      1.1  christos 
     93      1.1  christos   /* The cpu model.  */
     94      1.1  christos   unsigned char model;
     95      1.1  christos 
     96      1.1  christos   /* The cpu stepping.  */
     97      1.1  christos   unsigned char stepping;
     98      1.1  christos };
     99      1.1  christos 
    100      1.1  christos /* A BTS configuration.  */
    101      1.1  christos 
    102      1.1  christos struct btrace_config_bts
    103      1.1  christos {
    104      1.1  christos   /* The size of the branch trace buffer in bytes.
    105      1.1  christos 
    106      1.1  christos      This is unsigned int and not size_t since it is registered as
    107      1.1  christos      control variable for "set record btrace bts buffer-size".  */
    108      1.1  christos   unsigned int size;
    109      1.1  christos };
    110      1.1  christos 
    111      1.1  christos /* An Intel Processor Trace configuration.  */
    112      1.1  christos 
    113      1.1  christos struct btrace_config_pt
    114      1.1  christos {
    115      1.1  christos   /* The size of the branch trace buffer in bytes.
    116      1.1  christos 
    117      1.1  christos      This is unsigned int and not size_t since it is registered as
    118      1.1  christos      control variable for "set record btrace pt buffer-size".  */
    119      1.1  christos   unsigned int size;
    120      1.1  christos };
    121      1.1  christos 
    122      1.1  christos /* A branch tracing configuration.
    123      1.1  christos 
    124      1.1  christos    This describes the requested configuration as well as the actually
    125      1.1  christos    obtained configuration.
    126      1.1  christos    We describe the configuration for all different formats so we can
    127      1.1  christos    easily switch between formats.  */
    128      1.1  christos 
    129      1.1  christos struct btrace_config
    130      1.1  christos {
    131      1.1  christos   /* The branch tracing format.  */
    132      1.1  christos   enum btrace_format format;
    133      1.1  christos 
    134      1.1  christos   /* The BTS format configuration.  */
    135      1.1  christos   struct btrace_config_bts bts;
    136      1.1  christos 
    137      1.1  christos   /* The Intel Processor Trace format configuration.  */
    138      1.1  christos   struct btrace_config_pt pt;
    139      1.1  christos };
    140      1.1  christos 
    141      1.1  christos /* Branch trace in BTS format.  */
    142      1.1  christos struct btrace_data_bts
    143      1.1  christos {
    144      1.1  christos   /* Branch trace is represented as a vector of branch trace blocks starting
    145      1.1  christos      with the most recent block.  This needs to be a pointer as we place
    146      1.1  christos      btrace_data_bts into a union.  */
    147      1.1  christos   std::vector<btrace_block> *blocks;
    148      1.1  christos };
    149      1.1  christos 
    150      1.1  christos /* Configuration information to go with the trace data.  */
    151      1.1  christos struct btrace_data_pt_config
    152      1.1  christos {
    153      1.1  christos   /* The processor on which the trace has been collected.  */
    154      1.1  christos   struct btrace_cpu cpu;
    155      1.1  christos };
    156      1.1  christos 
    157      1.1  christos /* Branch trace in Intel Processor Trace format.  */
    158      1.1  christos struct btrace_data_pt
    159      1.1  christos {
    160      1.1  christos   /* Some configuration information to go with the data.  */
    161      1.1  christos   struct btrace_data_pt_config config;
    162      1.1  christos 
    163      1.1  christos   /* The trace data.  */
    164      1.1  christos   gdb_byte *data;
    165      1.1  christos 
    166      1.1  christos   /* The size of DATA in bytes.  */
    167      1.1  christos   size_t size;
    168      1.1  christos };
    169      1.1  christos 
    170      1.1  christos /* The branch trace data.  */
    171      1.1  christos struct btrace_data
    172      1.1  christos {
    173      1.1  christos   btrace_data () = default;
    174      1.1  christos 
    175      1.1  christos   ~btrace_data ()
    176      1.1  christos   {
    177      1.1  christos     fini ();
    178      1.1  christos   }
    179      1.1  christos 
    180      1.1  christos   btrace_data &operator= (btrace_data &&other)
    181      1.1  christos   {
    182      1.1  christos     if (this != &other)
    183      1.1  christos       {
    184      1.1  christos 	fini ();
    185      1.1  christos 	format = other.format;
    186      1.1  christos 	variant = other.variant;
    187      1.1  christos 	other.format = BTRACE_FORMAT_NONE;
    188      1.1  christos       }
    189      1.1  christos     return *this;
    190      1.1  christos   }
    191      1.1  christos 
    192      1.1  christos   /* Return true if this is empty; false otherwise.  */
    193      1.1  christos   bool empty () const;
    194      1.1  christos 
    195      1.1  christos   /* Clear this object.  */
    196      1.1  christos   void clear ();
    197      1.1  christos 
    198      1.1  christos   enum btrace_format format = BTRACE_FORMAT_NONE;
    199      1.1  christos 
    200      1.1  christos   union
    201      1.1  christos   {
    202      1.1  christos     /* Format == BTRACE_FORMAT_BTS.  */
    203      1.1  christos     struct btrace_data_bts bts;
    204      1.1  christos 
    205      1.1  christos     /* Format == BTRACE_FORMAT_PT.  */
    206      1.1  christos     struct btrace_data_pt pt;
    207      1.1  christos   } variant;
    208      1.1  christos 
    209      1.1  christos private:
    210      1.1  christos 
    211      1.1  christos   DISABLE_COPY_AND_ASSIGN (btrace_data);
    212      1.1  christos 
    213      1.1  christos   void fini ();
    214      1.1  christos };
    215      1.1  christos 
    216      1.1  christos /* Target specific branch trace information.  */
    217  1.1.1.3  christos struct btrace_target_info
    218  1.1.1.3  christos {
    219  1.1.1.3  christos   btrace_target_info (ptid_t ptid) : ptid (ptid)
    220  1.1.1.3  christos     {}
    221  1.1.1.3  christos 
    222  1.1.1.3  christos   btrace_target_info (ptid_t ptid, btrace_config conf)
    223  1.1.1.3  christos     : ptid (ptid), conf (conf)
    224  1.1.1.3  christos     {}
    225  1.1.1.3  christos 
    226  1.1.1.3  christos   virtual ~btrace_target_info () = default;
    227  1.1.1.3  christos 
    228  1.1.1.3  christos   /* The ptid of this thread.  */
    229  1.1.1.3  christos   ptid_t ptid {};
    230  1.1.1.3  christos 
    231  1.1.1.3  christos   /* The obtained branch trace configuration.  */
    232  1.1.1.3  christos   btrace_config conf {};
    233  1.1.1.3  christos };
    234      1.1  christos 
    235      1.1  christos /* Enumeration of btrace read types.  */
    236      1.1  christos 
    237      1.1  christos enum btrace_read_type
    238      1.1  christos {
    239      1.1  christos   /* Send all available trace.  */
    240      1.1  christos   BTRACE_READ_ALL,
    241      1.1  christos 
    242      1.1  christos   /* Send all available trace, if it changed.  */
    243      1.1  christos   BTRACE_READ_NEW,
    244      1.1  christos 
    245      1.1  christos   /* Send the trace since the last request.  This will fail if the trace
    246      1.1  christos      buffer overflowed.  */
    247      1.1  christos   BTRACE_READ_DELTA
    248      1.1  christos };
    249      1.1  christos 
    250      1.1  christos /* Enumeration of btrace errors.  */
    251      1.1  christos 
    252      1.1  christos enum btrace_error
    253      1.1  christos {
    254      1.1  christos   /* No error.  Everything is OK.  */
    255      1.1  christos   BTRACE_ERR_NONE,
    256      1.1  christos 
    257      1.1  christos   /* An unknown error.  */
    258      1.1  christos   BTRACE_ERR_UNKNOWN,
    259      1.1  christos 
    260      1.1  christos   /* Branch tracing is not supported on this system.  */
    261      1.1  christos   BTRACE_ERR_NOT_SUPPORTED,
    262      1.1  christos 
    263      1.1  christos   /* The branch trace buffer overflowed; no delta read possible.  */
    264      1.1  christos   BTRACE_ERR_OVERFLOW
    265      1.1  christos };
    266      1.1  christos 
    267      1.1  christos /* Return a string representation of FORMAT.  */
    268      1.1  christos extern const char *btrace_format_string (enum btrace_format format);
    269      1.1  christos 
    270      1.1  christos /* Return an abbreviation string representation of FORMAT.  */
    271      1.1  christos extern const char *btrace_format_short_string (enum btrace_format format);
    272      1.1  christos 
    273      1.1  christos /* Append the branch trace data from SRC to the end of DST.
    274      1.1  christos    Both SRC and DST must use the same format.
    275      1.1  christos    Returns zero on success; a negative number otherwise.  */
    276      1.1  christos extern int btrace_data_append (struct btrace_data *dst,
    277      1.1  christos 			       const struct btrace_data *src);
    278      1.1  christos 
    279      1.1  christos #endif /* COMMON_BTRACE_COMMON_H */
    280