[gdb/testsuite] Fix end_sequence addresses
[binutils-gdb.git] / gdbsupport / btrace-common.cc
blob2358a9fab33828471332590913286b959baddeac
1 /* Copyright (C) 2014-2024 Free Software Foundation, Inc.
3 Contributed by Intel Corp. <markus.t.metzger@intel.com>
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "btrace-common.h"
23 /* See btrace-common.h. */
25 const char *
26 btrace_format_string (enum btrace_format format)
28 switch (format)
30 case BTRACE_FORMAT_NONE:
31 return _("No or unknown format");
33 case BTRACE_FORMAT_BTS:
34 return _("Branch Trace Store");
36 case BTRACE_FORMAT_PT:
37 return _("Intel Processor Trace");
40 internal_error (_("Unknown branch trace format"));
43 /* See btrace-common.h. */
45 const char *
46 btrace_format_short_string (enum btrace_format format)
48 switch (format)
50 case BTRACE_FORMAT_NONE:
51 return "unknown";
53 case BTRACE_FORMAT_BTS:
54 return "bts";
56 case BTRACE_FORMAT_PT:
57 return "pt";
60 internal_error (_("Unknown branch trace format"));
63 /* See btrace-common.h. */
65 void
66 btrace_data::fini ()
68 switch (format)
70 case BTRACE_FORMAT_NONE:
71 /* Nothing to do. */
72 return;
74 case BTRACE_FORMAT_BTS:
75 delete variant.bts.blocks;
76 variant.bts.blocks = nullptr;
77 return;
79 case BTRACE_FORMAT_PT:
80 xfree (variant.pt.data);
81 return;
84 internal_error (_("Unkown branch trace format."));
87 /* See btrace-common.h. */
89 bool
90 btrace_data::empty () const
92 switch (format)
94 case BTRACE_FORMAT_NONE:
95 return true;
97 case BTRACE_FORMAT_BTS:
98 return variant.bts.blocks->empty ();
100 case BTRACE_FORMAT_PT:
101 return (variant.pt.size == 0);
104 internal_error (_("Unkown branch trace format."));
107 /* See btrace-common.h. */
109 void
110 btrace_data::clear ()
112 fini ();
113 format = BTRACE_FORMAT_NONE;
116 /* See btrace-common.h. */
119 btrace_data_append (struct btrace_data *dst,
120 const struct btrace_data *src)
122 switch (src->format)
124 case BTRACE_FORMAT_NONE:
125 return 0;
127 case BTRACE_FORMAT_BTS:
128 switch (dst->format)
130 default:
131 return -1;
133 case BTRACE_FORMAT_NONE:
134 dst->format = BTRACE_FORMAT_BTS;
135 dst->variant.bts.blocks = new std::vector<btrace_block>;
136 [[fallthrough]];
137 case BTRACE_FORMAT_BTS:
139 unsigned int blk;
141 /* We copy blocks in reverse order to have the oldest block at
142 index zero. */
143 blk = src->variant.bts.blocks->size ();
144 while (blk != 0)
146 const btrace_block &block
147 = src->variant.bts.blocks->at (--blk);
148 dst->variant.bts.blocks->push_back (block);
152 return 0;
154 case BTRACE_FORMAT_PT:
155 switch (dst->format)
157 default:
158 return -1;
160 case BTRACE_FORMAT_NONE:
161 dst->format = BTRACE_FORMAT_PT;
162 dst->variant.pt.data = NULL;
163 dst->variant.pt.size = 0;
164 [[fallthrough]];
165 case BTRACE_FORMAT_PT:
167 gdb_byte *data;
168 size_t size;
170 size = src->variant.pt.size + dst->variant.pt.size;
171 data = (gdb_byte *) xmalloc (size);
173 if (dst->variant.pt.size > 0)
174 memcpy (data, dst->variant.pt.data, dst->variant.pt.size);
175 memcpy (data + dst->variant.pt.size, src->variant.pt.data,
176 src->variant.pt.size);
178 xfree (dst->variant.pt.data);
180 dst->variant.pt.data = data;
181 dst->variant.pt.size = size;
184 return 0;
187 internal_error (_("Unkown branch trace format."));