]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdbsupport/btrace-common.cc
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdbsupport / btrace-common.cc
1 /* Copyright (C) 2014-2024 Free Software Foundation, Inc.
2
3 Contributed by Intel Corp. <markus.t.metzger@intel.com>
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "common-defs.h"
21 #include "btrace-common.h"
22
23
24 /* See btrace-common.h. */
25
26 const char *
27 btrace_format_string (enum btrace_format format)
28 {
29 switch (format)
30 {
31 case BTRACE_FORMAT_NONE:
32 return _("No or unknown format");
33
34 case BTRACE_FORMAT_BTS:
35 return _("Branch Trace Store");
36
37 case BTRACE_FORMAT_PT:
38 return _("Intel Processor Trace");
39 }
40
41 internal_error (_("Unknown branch trace format"));
42 }
43
44 /* See btrace-common.h. */
45
46 const char *
47 btrace_format_short_string (enum btrace_format format)
48 {
49 switch (format)
50 {
51 case BTRACE_FORMAT_NONE:
52 return "unknown";
53
54 case BTRACE_FORMAT_BTS:
55 return "bts";
56
57 case BTRACE_FORMAT_PT:
58 return "pt";
59 }
60
61 internal_error (_("Unknown branch trace format"));
62 }
63
64 /* See btrace-common.h. */
65
66 void
67 btrace_data::fini ()
68 {
69 switch (format)
70 {
71 case BTRACE_FORMAT_NONE:
72 /* Nothing to do. */
73 return;
74
75 case BTRACE_FORMAT_BTS:
76 delete variant.bts.blocks;
77 variant.bts.blocks = nullptr;
78 return;
79
80 case BTRACE_FORMAT_PT:
81 xfree (variant.pt.data);
82 return;
83 }
84
85 internal_error (_("Unkown branch trace format."));
86 }
87
88 /* See btrace-common.h. */
89
90 bool
91 btrace_data::empty () const
92 {
93 switch (format)
94 {
95 case BTRACE_FORMAT_NONE:
96 return true;
97
98 case BTRACE_FORMAT_BTS:
99 return variant.bts.blocks->empty ();
100
101 case BTRACE_FORMAT_PT:
102 return (variant.pt.size == 0);
103 }
104
105 internal_error (_("Unkown branch trace format."));
106 }
107
108 /* See btrace-common.h. */
109
110 void
111 btrace_data::clear ()
112 {
113 fini ();
114 format = BTRACE_FORMAT_NONE;
115 }
116
117 /* See btrace-common.h. */
118
119 int
120 btrace_data_append (struct btrace_data *dst,
121 const struct btrace_data *src)
122 {
123 switch (src->format)
124 {
125 case BTRACE_FORMAT_NONE:
126 return 0;
127
128 case BTRACE_FORMAT_BTS:
129 switch (dst->format)
130 {
131 default:
132 return -1;
133
134 case BTRACE_FORMAT_NONE:
135 dst->format = BTRACE_FORMAT_BTS;
136 dst->variant.bts.blocks = new std::vector<btrace_block>;
137 [[fallthrough]];
138 case BTRACE_FORMAT_BTS:
139 {
140 unsigned int blk;
141
142 /* We copy blocks in reverse order to have the oldest block at
143 index zero. */
144 blk = src->variant.bts.blocks->size ();
145 while (blk != 0)
146 {
147 const btrace_block &block
148 = src->variant.bts.blocks->at (--blk);
149 dst->variant.bts.blocks->push_back (block);
150 }
151 }
152 }
153 return 0;
154
155 case BTRACE_FORMAT_PT:
156 switch (dst->format)
157 {
158 default:
159 return -1;
160
161 case BTRACE_FORMAT_NONE:
162 dst->format = BTRACE_FORMAT_PT;
163 dst->variant.pt.data = NULL;
164 dst->variant.pt.size = 0;
165 [[fallthrough]];
166 case BTRACE_FORMAT_PT:
167 {
168 gdb_byte *data;
169 size_t size;
170
171 size = src->variant.pt.size + dst->variant.pt.size;
172 data = (gdb_byte *) xmalloc (size);
173
174 if (dst->variant.pt.size > 0)
175 memcpy (data, dst->variant.pt.data, dst->variant.pt.size);
176 memcpy (data + dst->variant.pt.size, src->variant.pt.data,
177 src->variant.pt.size);
178
179 xfree (dst->variant.pt.data);
180
181 dst->variant.pt.data = data;
182 dst->variant.pt.size = size;
183 }
184 }
185 return 0;
186 }
187
188 internal_error (_("Unkown branch trace format."));
189 }