xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/btrace-common.cc (revision 5ba1f45f2a09259cc846f20c7c5501604d633c90)
1*5ba1f45fSchristos /* Copyright (C) 2014-2024 Free Software Foundation, Inc.
28dffb485Schristos 
38dffb485Schristos    Contributed by Intel Corp. <markus.t.metzger@intel.com>
48dffb485Schristos 
58dffb485Schristos    This file is part of GDB.
68dffb485Schristos 
78dffb485Schristos    This program is free software; you can redistribute it and/or modify
88dffb485Schristos    it under the terms of the GNU General Public License as published by
98dffb485Schristos    the Free Software Foundation; either version 3 of the License, or
108dffb485Schristos    (at your option) any later version.
118dffb485Schristos 
128dffb485Schristos    This program is distributed in the hope that it will be useful,
138dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
148dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
158dffb485Schristos    GNU General Public License for more details.
168dffb485Schristos 
178dffb485Schristos    You should have received a copy of the GNU General Public License
188dffb485Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
198dffb485Schristos 
208dffb485Schristos #include "btrace-common.h"
218dffb485Schristos 
228dffb485Schristos 
238dffb485Schristos /* See btrace-common.h.  */
248dffb485Schristos 
258dffb485Schristos const char *
268dffb485Schristos btrace_format_string (enum btrace_format format)
278dffb485Schristos {
288dffb485Schristos   switch (format)
298dffb485Schristos     {
308dffb485Schristos     case BTRACE_FORMAT_NONE:
318dffb485Schristos       return _("No or unknown format");
328dffb485Schristos 
338dffb485Schristos     case BTRACE_FORMAT_BTS:
348dffb485Schristos       return _("Branch Trace Store");
358dffb485Schristos 
368dffb485Schristos     case BTRACE_FORMAT_PT:
378dffb485Schristos       return _("Intel Processor Trace");
388dffb485Schristos     }
398dffb485Schristos 
404b169a6bSchristos   internal_error (_("Unknown branch trace format"));
418dffb485Schristos }
428dffb485Schristos 
438dffb485Schristos /* See btrace-common.h.  */
448dffb485Schristos 
458dffb485Schristos const char *
468dffb485Schristos btrace_format_short_string (enum btrace_format format)
478dffb485Schristos {
488dffb485Schristos   switch (format)
498dffb485Schristos     {
508dffb485Schristos     case BTRACE_FORMAT_NONE:
518dffb485Schristos       return "unknown";
528dffb485Schristos 
538dffb485Schristos     case BTRACE_FORMAT_BTS:
548dffb485Schristos       return "bts";
558dffb485Schristos 
568dffb485Schristos     case BTRACE_FORMAT_PT:
578dffb485Schristos       return "pt";
588dffb485Schristos     }
598dffb485Schristos 
604b169a6bSchristos   internal_error (_("Unknown branch trace format"));
618dffb485Schristos }
628dffb485Schristos 
638dffb485Schristos /* See btrace-common.h.  */
648dffb485Schristos 
658dffb485Schristos void
668dffb485Schristos btrace_data::fini ()
678dffb485Schristos {
688dffb485Schristos   switch (format)
698dffb485Schristos     {
708dffb485Schristos     case BTRACE_FORMAT_NONE:
718dffb485Schristos       /* Nothing to do.  */
728dffb485Schristos       return;
738dffb485Schristos 
748dffb485Schristos     case BTRACE_FORMAT_BTS:
758dffb485Schristos       delete variant.bts.blocks;
768dffb485Schristos       variant.bts.blocks = nullptr;
778dffb485Schristos       return;
788dffb485Schristos 
798dffb485Schristos     case BTRACE_FORMAT_PT:
808dffb485Schristos       xfree (variant.pt.data);
818dffb485Schristos       return;
828dffb485Schristos     }
838dffb485Schristos 
844b169a6bSchristos   internal_error (_("Unkown branch trace format."));
858dffb485Schristos }
868dffb485Schristos 
878dffb485Schristos /* See btrace-common.h.  */
888dffb485Schristos 
898dffb485Schristos bool
908dffb485Schristos btrace_data::empty () const
918dffb485Schristos {
928dffb485Schristos   switch (format)
938dffb485Schristos     {
948dffb485Schristos     case BTRACE_FORMAT_NONE:
958dffb485Schristos       return true;
968dffb485Schristos 
978dffb485Schristos     case BTRACE_FORMAT_BTS:
988dffb485Schristos       return variant.bts.blocks->empty ();
998dffb485Schristos 
1008dffb485Schristos     case BTRACE_FORMAT_PT:
1018dffb485Schristos       return (variant.pt.size == 0);
1028dffb485Schristos     }
1038dffb485Schristos 
1044b169a6bSchristos   internal_error (_("Unkown branch trace format."));
1058dffb485Schristos }
1068dffb485Schristos 
1078dffb485Schristos /* See btrace-common.h.  */
1088dffb485Schristos 
1098dffb485Schristos void
1108dffb485Schristos btrace_data::clear ()
1118dffb485Schristos {
1128dffb485Schristos   fini ();
1138dffb485Schristos   format = BTRACE_FORMAT_NONE;
1148dffb485Schristos }
1158dffb485Schristos 
1168dffb485Schristos /* See btrace-common.h.  */
1178dffb485Schristos 
1188dffb485Schristos int
1198dffb485Schristos btrace_data_append (struct btrace_data *dst,
1208dffb485Schristos 		    const struct btrace_data *src)
1218dffb485Schristos {
1228dffb485Schristos   switch (src->format)
1238dffb485Schristos     {
1248dffb485Schristos     case BTRACE_FORMAT_NONE:
1258dffb485Schristos       return 0;
1268dffb485Schristos 
1278dffb485Schristos     case BTRACE_FORMAT_BTS:
1288dffb485Schristos       switch (dst->format)
1298dffb485Schristos 	{
1308dffb485Schristos 	default:
1318dffb485Schristos 	  return -1;
1328dffb485Schristos 
1338dffb485Schristos 	case BTRACE_FORMAT_NONE:
1348dffb485Schristos 	  dst->format = BTRACE_FORMAT_BTS;
1358dffb485Schristos 	  dst->variant.bts.blocks = new std::vector<btrace_block>;
136*5ba1f45fSchristos 	  [[fallthrough]];
1378dffb485Schristos 	case BTRACE_FORMAT_BTS:
1388dffb485Schristos 	  {
1398dffb485Schristos 	    unsigned int blk;
1408dffb485Schristos 
1418dffb485Schristos 	    /* We copy blocks in reverse order to have the oldest block at
1428dffb485Schristos 	       index zero.  */
1438dffb485Schristos 	    blk = src->variant.bts.blocks->size ();
1448dffb485Schristos 	    while (blk != 0)
1458dffb485Schristos 	      {
1468dffb485Schristos 		const btrace_block &block
1478dffb485Schristos 		  = src->variant.bts.blocks->at (--blk);
1488dffb485Schristos 		dst->variant.bts.blocks->push_back (block);
1498dffb485Schristos 	      }
1508dffb485Schristos 	  }
1518dffb485Schristos 	}
1528dffb485Schristos       return 0;
1538dffb485Schristos 
1548dffb485Schristos     case BTRACE_FORMAT_PT:
1558dffb485Schristos       switch (dst->format)
1568dffb485Schristos 	{
1578dffb485Schristos 	default:
1588dffb485Schristos 	  return -1;
1598dffb485Schristos 
1608dffb485Schristos 	case BTRACE_FORMAT_NONE:
1618dffb485Schristos 	  dst->format = BTRACE_FORMAT_PT;
1628dffb485Schristos 	  dst->variant.pt.data = NULL;
1638dffb485Schristos 	  dst->variant.pt.size = 0;
164*5ba1f45fSchristos 	  [[fallthrough]];
1658dffb485Schristos 	case BTRACE_FORMAT_PT:
1668dffb485Schristos 	  {
1678dffb485Schristos 	    gdb_byte *data;
1688dffb485Schristos 	    size_t size;
1698dffb485Schristos 
1708dffb485Schristos 	    size = src->variant.pt.size + dst->variant.pt.size;
1718dffb485Schristos 	    data = (gdb_byte *) xmalloc (size);
1728dffb485Schristos 
1738dffb485Schristos 	    if (dst->variant.pt.size > 0)
1748dffb485Schristos 	      memcpy (data, dst->variant.pt.data, dst->variant.pt.size);
1758dffb485Schristos 	    memcpy (data + dst->variant.pt.size, src->variant.pt.data,
1768dffb485Schristos 		    src->variant.pt.size);
1778dffb485Schristos 
1788dffb485Schristos 	    xfree (dst->variant.pt.data);
1798dffb485Schristos 
1808dffb485Schristos 	    dst->variant.pt.data = data;
1818dffb485Schristos 	    dst->variant.pt.size = size;
1828dffb485Schristos 	  }
1838dffb485Schristos 	}
1848dffb485Schristos       return 0;
1858dffb485Schristos     }
1868dffb485Schristos 
1874b169a6bSchristos   internal_error (_("Unkown branch trace format."));
1888dffb485Schristos }
189