aboutsummaryrefslogtreecommitdiff
path: root/bindings/cxx/include/libswocxx/libswocxx.hpp
blob: d842671a04603320923ca10b11b4b5044b1faedc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 * This file is part of the libswo project.
 *
 * Copyright (C) 2015 Marc Schink <swo-dev@marcschink.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef LIBSWOCXX_HPP
#define LIBSWOCXX_HPP

#include <stdint.h>
#include <string>
#include <vector>
#include <stdexcept>
#include <type_traits>

#include <libswo/libswo.h>

namespace libswo
{

using namespace std;

enum class LogLevel {
	NONE = LIBSWO_LOG_LEVEL_NONE,
	ERROR = LIBSWO_LOG_LEVEL_ERROR,
	WARNING = LIBSWO_LOG_LEVEL_WARNING,
	INFO = LIBSWO_LOG_LEVEL_INFO,
	DEBUG = LIBSWO_LOG_LEVEL_DEBUG
};

enum class PacketType {
	UNKNOWN = LIBSWO_PACKET_TYPE_UNKNOWN,
	SYNC = LIBSWO_PACKET_TYPE_SYNC,
	OF = LIBSWO_PACKET_TYPE_OF,
	LTS = LIBSWO_PACKET_TYPE_LTS,
	GTS1 = LIBSWO_PACKET_TYPE_GTS1,
	GTS2 = LIBSWO_PACKET_TYPE_GTS2,
	EXT = LIBSWO_PACKET_TYPE_EXT,
	INST = LIBSWO_PACKET_TYPE_INST,
	HW = LIBSWO_PACKET_TYPE_HW,
	DWT_EVTCNT = LIBSWO_PACKET_TYPE_DWT_EVTCNT,
	DWT_EXCTRC = LIBSWO_PACKET_TYPE_DWT_EXCTRC,
	DWT_PC_SAMPLE = LIBSWO_PACKET_TYPE_DWT_PC_SAMPLE,
	DWT_PC_VALUE = LIBSWO_PACKET_TYPE_DWT_PC_VALUE,
	DWT_ADDR_OFFSET = LIBSWO_PACKET_TYPE_DWT_ADDR_OFFSET,
	DWT_DATA_VALUE = LIBSWO_PACKET_TYPE_DWT_DATA_VALUE
};

enum class LocalTimestampRelation {
	SYNC = LIBSWO_LTS_REL_SYNC,
	TS = LIBSWO_LTS_REL_TS,
	SRC = LIBSWO_LTS_REL_SRC,
	BOTH = LIBSWO_LTS_REL_BOTH
};

enum class ExtensionSource {
	ITM = LIBSWO_EXT_SRC_ITM,
	HW = LIBSWO_EXT_SRC_HW
};

enum class ExceptionTraceFunction {
	RESERVED = LIBSWO_EXCTRC_FUNC_RESERVED,
	ENTER = LIBSWO_EXCTRC_FUNC_ENTER,
	EXIT = LIBSWO_EXCTRC_FUNC_EXIT,
	RETURN = LIBSWO_EXCTRC_FUNC_RETURN
};

enum class DecoderFlags {
	NONE = 0,
	EOS = LIBSWO_DF_EOS
};

const char LOG_DOMAIN_DEFAULT[] = LIBSWO_LOG_DOMAIN_DEFAULT;
const unsigned int LOG_DOMAIN_MAX_LENGTH = LIBSWO_LOG_DOMAIN_MAX_LENGTH;

typedef int (*LogCallback)(LogLevel level, const std::string &message,
		void *user_data);

class LIBSWO_API Error : public exception
{
public:
	Error(int error_code);
	~Error(void) throw();

	const char *what(void) const throw();
	const int code;
};

class LIBSWO_API Packet
{
public:
	virtual ~Packet(void) = 0;

	PacketType get_type(void) const;
	size_t get_size(void) const;
	virtual const string to_string(void) const = 0;
protected:
	union libswo_packet _packet;
};

typedef int (*DecoderCallback)(const Packet &packet, void *user_data);

class LIBSWO_API PayloadPacket : public Packet
{
public:
	virtual ~PayloadPacket(void) = 0;

	const vector<uint8_t> get_data(void) const;

	static const unsigned int MAX_SIZE = LIBSWO_MAX_PAYLOAD_SIZE;
};

class LIBSWO_API Unknown : public PayloadPacket
{
public:
	Unknown(const struct libswo_packet_unknown *packet);
	Unknown(const union libswo_packet *packet);

	const string to_string(void) const;
};

class LIBSWO_API Synchronization : public Packet
{
public:
	Synchronization(const struct libswo_packet_sync *packet);
	Synchronization(const union libswo_packet *packet);

	const string to_string(void) const;
};

class LIBSWO_API Overflow : public PayloadPacket
{
public:
	Overflow(const struct libswo_packet_of *packet);
	Overflow(const union libswo_packet *packet);

	const string to_string(void) const;
};

class LIBSWO_API LocalTimestamp : public PayloadPacket
{
public:
	LocalTimestamp(const struct libswo_packet_lts *packet);
	LocalTimestamp(const union libswo_packet *packet);

	LocalTimestampRelation get_relation() const;
	uint32_t get_value(void) const;

	const string to_string(void) const;
};

class LIBSWO_API GlobalTimestamp1 : public PayloadPacket
{
public:
	GlobalTimestamp1(const struct libswo_packet_gts1 *packet);
	GlobalTimestamp1(const union libswo_packet *packet);

	uint32_t get_value(void) const;
	bool get_clkch(void) const;
	bool get_wrap(void) const;

	const string to_string(void) const;
};

class LIBSWO_API GlobalTimestamp2 : public PayloadPacket
{
public:
	GlobalTimestamp2(const struct libswo_packet_gts2 *packet);
	GlobalTimestamp2(const union libswo_packet *packet);

	uint32_t get_value(void) const;

	const string to_string(void) const;
};

class LIBSWO_API Extension : public PayloadPacket
{
public:
	Extension(const struct libswo_packet_ext *packet);
	Extension(const union libswo_packet *packet);

	ExtensionSource get_source(void) const;
	uint32_t get_value(void) const;

	const string to_string(void) const;
};

class LIBSWO_API SourcePacket : public PayloadPacket
{
public:
	virtual ~SourcePacket(void);

	uint8_t get_address(void) const;
	const vector<uint8_t> get_payload(void) const;
	uint32_t get_value(void) const;

	virtual const string to_string(void) const = 0;

	static const unsigned int MAX_ADDRESS = LIBSWO_MAX_SOURCE_ADDRESS;
};

class LIBSWO_API Instrumentation : public SourcePacket
{
public:
	Instrumentation(const struct libswo_packet_inst *packet);
	Instrumentation(const union libswo_packet *packet);

	const string to_string(void) const;
};

class LIBSWO_API Hardware : public SourcePacket
{
public:
	Hardware(const struct libswo_packet_hw *packet);
	Hardware(const union libswo_packet *packet);

	const string to_string(void) const;
};

class LIBSWO_API EventCounter : public Hardware
{
public:
	EventCounter(const struct libswo_packet_dwt_evtcnt *packet);
	EventCounter(const union libswo_packet *packet);

	bool get_cpi(void) const;
	bool get_exc(void) const;
	bool get_sleep(void) const;
	bool get_lsu(void) const;
	bool get_fold(void) const;
	bool get_cyc(void) const;

	const string to_string(void) const;
};

class LIBSWO_API ExceptionTrace : public Hardware
{
public:
	ExceptionTrace(const struct libswo_packet_dwt_exctrc *packet);
	ExceptionTrace(const union libswo_packet *packet);

	uint16_t get_exception(void) const;
	ExceptionTraceFunction get_function(void) const;

	const string to_string(void) const;
};

class LIBSWO_API PCSample : public Hardware
{
public:
	PCSample(const struct libswo_packet_dwt_pc_sample *packet);
	PCSample(const union libswo_packet *packet);

	bool get_sleep(void) const;
	uint32_t get_pc(void) const;

	const string to_string(void) const;
};

class LIBSWO_API PCValue : public Hardware
{
public:
	PCValue(const struct libswo_packet_dwt_pc_value *packet);
	PCValue(const union libswo_packet *packet);

	uint8_t get_comparator(void) const;
	uint32_t get_pc(void) const;

	const string to_string(void) const;
};

class LIBSWO_API AddressOffset : public Hardware
{
public:
	AddressOffset(const struct libswo_packet_dwt_addr_offset *packet);
	AddressOffset(const union libswo_packet *packet);

	uint8_t get_comparator(void) const;
	uint16_t get_offset(void) const;

	const string to_string(void) const;
};

class LIBSWO_API DataValue : public Hardware
{
public:
	DataValue(const struct libswo_packet_dwt_data_value *packet);
	DataValue(const union libswo_packet *packet);

	bool get_wnr(void) const;
	uint8_t get_comparator(void) const;
	uint32_t get_data_value(void) const;

	const string to_string(void) const;
};

class LIBSWO_PRIV DecoderCallbackHelper
{
public:
	DecoderCallback callback;
	void *user_data;
};

class LIBSWO_PRIV LogCallbackHelper
{
public:
	LogCallback callback;
	void *user_data;
};

class LIBSWO_API Context
{
public:
	Context(size_t buffer_size = DEFAULT_BUFFER_SIZE);
	Context(uint8_t *buffer, size_t buffer_size = DEFAULT_BUFFER_SIZE);
	~Context(void);

	LogLevel get_log_level(void) const;
	void set_log_level(LogLevel level);

	string get_log_domain(void) const;
	void set_log_domain(const string &domain);
	void set_log_callback(LogCallback callback, void *user_data = NULL);

	void set_callback(DecoderCallback callback, void *user_data = NULL);

	void feed(const uint8_t *data, size_t length);
	void decode(DecoderFlags flags = DecoderFlags::NONE);

	static const size_t DEFAULT_BUFFER_SIZE = LIBSWO_DEFAULT_BUFFER_SIZE;
private:
	struct libswo_context *_context;
	DecoderCallbackHelper _decoder_callback;
	LogCallbackHelper _log_callback;
};

class LIBSWO_API Version
{
public:
	static int get_package_major(void);
	static int get_package_minor(void);
	static int get_package_micro(void);
	static string get_package_string(void);

	static int get_library_current(void);
	static int get_library_revision(void);
	static int get_library_age(void);
	static string get_library_string(void);
};

inline DecoderFlags operator|(DecoderFlags lhs, DecoderFlags rhs)
{
	using type = std::underlying_type<DecoderFlags>::type;

	return static_cast<DecoderFlags>(static_cast<type>(lhs) |
		static_cast<type>(rhs));
}

}

#endif /* LIBSWOCXX_HPP */