Rename common to gdbsupport
[deliverable/binutils-gdb.git] / gdb / gdbsupport / btrace-common.c
1 /* Copyright (C) 2014-2019 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 (__FILE__, __LINE__, _("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 (__FILE__, __LINE__, _("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 VEC_free (btrace_block_s, variant.bts.blocks);
77 return;
78
79 case BTRACE_FORMAT_PT:
80 xfree (variant.pt.data);
81 return;
82 }
83
84 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
85 }
86
87 /* See btrace-common.h. */
88
89 bool
90 btrace_data::empty () const
91 {
92 switch (format)
93 {
94 case BTRACE_FORMAT_NONE:
95 return true;
96
97 case BTRACE_FORMAT_BTS:
98 return VEC_empty (btrace_block_s, variant.bts.blocks);
99
100 case BTRACE_FORMAT_PT:
101 return (variant.pt.size == 0);
102 }
103
104 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
105 }
106
107 /* See btrace-common.h. */
108
109 void
110 btrace_data::clear ()
111 {
112 fini ();
113 format = BTRACE_FORMAT_NONE;
114 }
115
116 /* See btrace-common.h. */
117
118 int
119 btrace_data_append (struct btrace_data *dst,
120 const struct btrace_data *src)
121 {
122 switch (src->format)
123 {
124 case BTRACE_FORMAT_NONE:
125 return 0;
126
127 case BTRACE_FORMAT_BTS:
128 switch (dst->format)
129 {
130 default:
131 return -1;
132
133 case BTRACE_FORMAT_NONE:
134 dst->format = BTRACE_FORMAT_BTS;
135 dst->variant.bts.blocks = NULL;
136
137 /* Fall-through. */
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 = VEC_length (btrace_block_s, src->variant.bts.blocks);
145 while (blk != 0)
146 {
147 btrace_block_s *block;
148
149 block = VEC_index (btrace_block_s, src->variant.bts.blocks,
150 --blk);
151
152 VEC_safe_push (btrace_block_s, dst->variant.bts.blocks, block);
153 }
154 }
155 }
156 return 0;
157
158 case BTRACE_FORMAT_PT:
159 switch (dst->format)
160 {
161 default:
162 return -1;
163
164 case BTRACE_FORMAT_NONE:
165 dst->format = BTRACE_FORMAT_PT;
166 dst->variant.pt.data = NULL;
167 dst->variant.pt.size = 0;
168
169 /* fall-through. */
170 case BTRACE_FORMAT_PT:
171 {
172 gdb_byte *data;
173 size_t size;
174
175 size = src->variant.pt.size + dst->variant.pt.size;
176 data = (gdb_byte *) xmalloc (size);
177
178 memcpy (data, dst->variant.pt.data, dst->variant.pt.size);
179 memcpy (data + dst->variant.pt.size, src->variant.pt.data,
180 src->variant.pt.size);
181
182 xfree (dst->variant.pt.data);
183
184 dst->variant.pt.data = data;
185 dst->variant.pt.size = size;
186 }
187 }
188 return 0;
189 }
190
191 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
192 }
This page took 0.034652 seconds and 4 git commands to generate.