GDB (xrefs)

btrace-common.c
Go to the documentation of this file.
1 /* Copyright (C) 2014-2018 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_init (struct btrace_data *data)
68 {
69 data->format = BTRACE_FORMAT_NONE;
70 }
71
72 /* See btrace-common.h. */
73
74 void
75 btrace_data_fini (struct btrace_data *data)
76 {
77 switch (data->format)
78 {
79 case BTRACE_FORMAT_NONE:
80 /* Nothing to do. */
81 return;
82
83 case BTRACE_FORMAT_BTS:
84 VEC_free (btrace_block_s, data->variant.bts.blocks);
85 return;
86
87 case BTRACE_FORMAT_PT:
88 xfree (data->variant.pt.data);
89 return;
90 }
91
92 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
93 }
94
95 /* See btrace-common.h. */
96
97 int
98 btrace_data_empty (struct btrace_data *data)
99 {
100 switch (data->format)
101 {
102 case BTRACE_FORMAT_NONE:
103 return 1;
104
105 case BTRACE_FORMAT_BTS:
106 return VEC_empty (btrace_block_s, data->variant.bts.blocks);
107
108 case BTRACE_FORMAT_PT:
109 return (data->variant.pt.size == 0);
110 }
111
112 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
113 }
114
115 /* See btrace-common.h. */
116
117 void
118 btrace_data_clear (struct btrace_data *data)
119 {
120 btrace_data_fini (data);
121 btrace_data_init (data);
122 }
123
124 /* See btrace-common.h. */
125
126 int
127 btrace_data_append (struct btrace_data *dst,
128 const struct btrace_data *src)
129 {
130 switch (src->format)
131 {
132 case BTRACE_FORMAT_NONE:
133 return 0;
134
135 case BTRACE_FORMAT_BTS:
136 switch (dst->format)
137 {
138 default:
139 return -1;
140
141 case BTRACE_FORMAT_NONE:
142 dst->format = BTRACE_FORMAT_BTS;
143 dst->variant.bts.blocks = NULL;
144
145 /* Fall-through. */
146 case BTRACE_FORMAT_BTS:
147 {
148 unsigned int blk;
149
150 /* We copy blocks in reverse order to have the oldest block at
151 index zero. */
152 blk = VEC_length (btrace_block_s, src->variant.bts.blocks);
153 while (blk != 0)
154 {
155 btrace_block_s *block;
156
157 block = VEC_index (btrace_block_s, src->variant.bts.blocks,
158 --blk);
159
160 VEC_safe_push (btrace_block_s, dst->variant.bts.blocks, block);
161 }
162 }
163 }
164 return 0;
165
166 case BTRACE_FORMAT_PT:
167 switch (dst->format)
168 {
169 default:
170 return -1;
171
172 case BTRACE_FORMAT_NONE:
173 dst->format = BTRACE_FORMAT_PT;
174 dst->variant.pt.data = NULL;
175 dst->variant.pt.size = 0;
176
177 /* fall-through. */
178 case BTRACE_FORMAT_PT:
179 {
180 gdb_byte *data;
181 size_t size;
182
183 size = src->variant.pt.size + dst->variant.pt.size;
184 data = (gdb_byte *) xmalloc (size);
185
186 memcpy (data, dst->variant.pt.data, dst->variant.pt.size);
187 memcpy (data + dst->variant.pt.size, src->variant.pt.data,
188 src->variant.pt.size);
189
190 xfree (dst->variant.pt.data);
191
192 dst->variant.pt.data = data;
193 dst->variant.pt.size = size;
194 }
195 }
196 return 0;
197 }
198
199 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
200 }
btrace_data::variant
union btrace_data::@24 variant
xfree
void xfree(void *)
btrace_data_fini
void btrace_data_fini(struct btrace_data *data)
Definition: btrace-common.c:75
btrace_data::bts
struct btrace_data_bts bts
Definition: btrace-common.h:172
internal_error
void internal_error(const char *file, int line, const char *fmt,...)
Definition: errors.c:50
VEC_safe_push
#define VEC_safe_push(T, V, O)
Definition: vec.h:276
_
#define _(String)
Definition: gdb_locale.h:35
btrace_format
btrace_format
Definition: btrace-common.h:54
btrace_block
Definition: btrace-common.h:37
btrace_data_clear
void btrace_data_clear(struct btrace_data *data)
Definition: btrace-common.c:118
btrace_format_string
const char * btrace_format_string(enum btrace_format format)
Definition: btrace-common.c:27
btrace_data::format
enum btrace_format format
Definition: btrace-common.h:167
VEC_length
#define VEC_length(T, V)
Definition: vec.h:140
BTRACE_FORMAT_NONE
Definition: btrace-common.h:57
VEC_index
#define VEC_index(T, V, I)
Definition: vec.h:167
btrace_data::pt
struct btrace_data_pt pt
Definition: btrace-common.h:175
common-defs.h
BTRACE_FORMAT_PT
Definition: btrace-common.h:64
xmalloc
void * xmalloc(YYSIZE_T)
btrace_data_empty
int btrace_data_empty(struct btrace_data *data)
Definition: btrace-common.c:98
block
Definition: block.h:60
VEC_empty
#define VEC_empty(T, V)
Definition: vec.h:148
btrace-common.h
gdb_byte
bfd_byte gdb_byte
Definition: common-types.h:38
BTRACE_FORMAT_BTS
Definition: btrace-common.h:61
btrace_data_pt::data
gdb_byte * data
Definition: btrace-common.h:158
VEC_free
#define VEC_free(T, V)
Definition: vec.h:196
btrace_data_pt::size
size_t size
Definition: btrace-common.h:161
btrace_data_init
void btrace_data_init(struct btrace_data *data)
Definition: btrace-common.c:67
btrace_format_short_string
const char * btrace_format_short_string(enum btrace_format format)
Definition: btrace-common.c:47
size
size_t size
Definition: go32-nat.c:242
btrace_data_append
int btrace_data_append(struct btrace_data *dst, const struct btrace_data *src)
Definition: btrace-common.c:127
btrace_data
Definition: btrace-common.h:165