Commit | Line | Data |
---|---|---|
42a4f53d | 1 | /* Copyright (C) 2014-2019 Free Software Foundation, Inc. |
734b0e4b MM |
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"); | |
b20a6524 MM |
36 | |
37 | case BTRACE_FORMAT_PT: | |
bc504a31 | 38 | return _("Intel Processor Trace"); |
734b0e4b MM |
39 | } |
40 | ||
41 | internal_error (__FILE__, __LINE__, _("Unknown branch trace format")); | |
42 | } | |
43 | ||
44 | /* See btrace-common.h. */ | |
45 | ||
38b022b4 SM |
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 | ||
734b0e4b | 66 | void |
8dcc53b3 | 67 | btrace_data::fini () |
734b0e4b | 68 | { |
8dcc53b3 | 69 | switch (format) |
734b0e4b MM |
70 | { |
71 | case BTRACE_FORMAT_NONE: | |
72 | /* Nothing to do. */ | |
73 | return; | |
74 | ||
75 | case BTRACE_FORMAT_BTS: | |
8dcc53b3 | 76 | VEC_free (btrace_block_s, variant.bts.blocks); |
734b0e4b | 77 | return; |
b20a6524 MM |
78 | |
79 | case BTRACE_FORMAT_PT: | |
8dcc53b3 | 80 | xfree (variant.pt.data); |
b20a6524 | 81 | return; |
734b0e4b MM |
82 | } |
83 | ||
84 | internal_error (__FILE__, __LINE__, _("Unkown branch trace format.")); | |
85 | } | |
86 | ||
87 | /* See btrace-common.h. */ | |
88 | ||
8dcc53b3 TT |
89 | bool |
90 | btrace_data::empty () const | |
734b0e4b | 91 | { |
8dcc53b3 | 92 | switch (format) |
734b0e4b MM |
93 | { |
94 | case BTRACE_FORMAT_NONE: | |
8dcc53b3 | 95 | return true; |
734b0e4b MM |
96 | |
97 | case BTRACE_FORMAT_BTS: | |
8dcc53b3 | 98 | return VEC_empty (btrace_block_s, variant.bts.blocks); |
b20a6524 MM |
99 | |
100 | case BTRACE_FORMAT_PT: | |
8dcc53b3 | 101 | return (variant.pt.size == 0); |
734b0e4b MM |
102 | } |
103 | ||
104 | internal_error (__FILE__, __LINE__, _("Unkown branch trace format.")); | |
105 | } | |
9be54cae MM |
106 | |
107 | /* See btrace-common.h. */ | |
108 | ||
109 | void | |
8dcc53b3 | 110 | btrace_data::clear () |
9be54cae | 111 | { |
8dcc53b3 TT |
112 | fini (); |
113 | format = BTRACE_FORMAT_NONE; | |
9be54cae MM |
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. */ | |
40e050d2 | 170 | case BTRACE_FORMAT_PT: |
9be54cae MM |
171 | { |
172 | gdb_byte *data; | |
e7b01ce0 | 173 | size_t size; |
9be54cae MM |
174 | |
175 | size = src->variant.pt.size + dst->variant.pt.size; | |
224c3ddb | 176 | data = (gdb_byte *) xmalloc (size); |
9be54cae MM |
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 | } |