Fix seg-fault in the DWARF reader code when accessing an abbreviatuin table with...
[binutils-gdb.git] / gdbsupport / btrace-common.cc
blob2d54e8ba82d7c23aa366f3113c97b0d2f22b0167
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 "common-defs.h"
21 #include "btrace-common.h"
24 /* See btrace-common.h. */
26 const char *
27 btrace_format_string (enum btrace_format format)
29 switch (format)
31 case BTRACE_FORMAT_NONE:
32 return _("No or unknown format");
34 case BTRACE_FORMAT_BTS:
35 return _("Branch Trace Store");
37 case BTRACE_FORMAT_PT:
38 return _("Intel Processor Trace");
41 internal_error (_("Unknown branch trace format"));
44 /* See btrace-common.h. */
46 const char *
47 btrace_format_short_string (enum btrace_format format)
49 switch (format)
51 case BTRACE_FORMAT_NONE:
52 return "unknown";
54 case BTRACE_FORMAT_BTS:
55 return "bts";
57 case BTRACE_FORMAT_PT:
58 return "pt";
61 internal_error (_("Unknown branch trace format"));
64 /* See btrace-common.h. */
66 void
67 btrace_data::fini ()
69 switch (format)
71 case BTRACE_FORMAT_NONE:
72 /* Nothing to do. */
73 return;
75 case BTRACE_FORMAT_BTS:
76 delete variant.bts.blocks;
77 variant.bts.blocks = nullptr;
78 return;
80 case BTRACE_FORMAT_PT:
81 xfree (variant.pt.data);
82 return;
85 internal_error (_("Unkown branch trace format."));
88 /* See btrace-common.h. */
90 bool
91 btrace_data::empty () const
93 switch (format)
95 case BTRACE_FORMAT_NONE:
96 return true;
98 case BTRACE_FORMAT_BTS:
99 return variant.bts.blocks->empty ();
101 case BTRACE_FORMAT_PT:
102 return (variant.pt.size == 0);
105 internal_error (_("Unkown branch trace format."));
108 /* See btrace-common.h. */
110 void
111 btrace_data::clear ()
113 fini ();
114 format = BTRACE_FORMAT_NONE;
117 /* See btrace-common.h. */
120 btrace_data_append (struct btrace_data *dst,
121 const struct btrace_data *src)
123 switch (src->format)
125 case BTRACE_FORMAT_NONE:
126 return 0;
128 case BTRACE_FORMAT_BTS:
129 switch (dst->format)
131 default:
132 return -1;
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:
140 unsigned int blk;
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)
147 const btrace_block &block
148 = src->variant.bts.blocks->at (--blk);
149 dst->variant.bts.blocks->push_back (block);
153 return 0;
155 case BTRACE_FORMAT_PT:
156 switch (dst->format)
158 default:
159 return -1;
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:
168 gdb_byte *data;
169 size_t size;
171 size = src->variant.pt.size + dst->variant.pt.size;
172 data = (gdb_byte *) xmalloc (size);
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);
179 xfree (dst->variant.pt.data);
181 dst->variant.pt.data = data;
182 dst->variant.pt.size = size;
185 return 0;
188 internal_error (_("Unkown branch trace format."));