aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: d44d90311368e4d980f2078063482a573454a6b3 (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
 * This file is part of the swodec project.
 *
 * Copyright (C) 2014 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/>.
 */

#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <glib.h>

#include <libswo/libswo.h>

#define BUFFER_SIZE	1024

static gchar *input_file = NULL;
static uint16_t packet_type_filter;
static uint32_t inst_address_filter;
static gboolean opt_decode_dwt;

void dwt_handle_packet(const struct libswo_packet_hw *packet);

static gboolean parse_filter_option(const gchar *option_name,
		const gchar *value, gpointer data, GError **error)
{
	gchar **tokens;
	unsigned int i;
	uint16_t tmp;

	(void)option_name;
	(void)data;
	(void)error;

	if (!strlen(value))
		return TRUE;

	i = 0;
	tokens = g_strsplit(value, ",", -1);
	tmp = 0x0000;

	while (tokens[i]) {
		g_strstrip(tokens[i]);

		if (!strlen(tokens[i])) {
			i++;
			continue;
		}

		if (!g_ascii_strcasecmp(tokens[i], "sync")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_SYNC);
		} else if (!g_ascii_strcasecmp(tokens[i], "of")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_OVERFLOW);
		} else if (!g_ascii_strcasecmp(tokens[i], "lts")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_LTS);
		} else if (!g_ascii_strcasecmp(tokens[i], "gts")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_GTS1);
			tmp |= (1 << LIBSWO_PACKET_TYPE_GTS2);
		} else if (!g_ascii_strcasecmp(tokens[i], "gts1")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_GTS1);
		} else if (!g_ascii_strcasecmp(tokens[i], "gts2")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_GTS2);
		} else if (!g_ascii_strcasecmp(tokens[i], "ext")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_EXT);
		} else if (!g_ascii_strcasecmp(tokens[i], "inst")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_INST);
		} else if (!g_ascii_strcasecmp(tokens[i], "hw")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_HW);
		} else if (!g_ascii_strcasecmp(tokens[i], "unknown")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_UNKNOWN);
		} else {
			g_critical("Invalid packet type: %s.", tokens[i]);
			g_strfreev(tokens);
			return FALSE;
		}

		i++;
	}

	/*
	 * Apply the packet type filter only if at least one valid packet type
	 * was specified.
	 */
	if (tmp > 0)
		packet_type_filter = tmp;

	g_strfreev(tokens);

	return TRUE;
}

static gboolean parse_inst_filter_option(const gchar *option_name,
		const gchar *value, gpointer data, GError **error)
{
	gchar **tokens;
	unsigned int i;
	uint32_t tmp;
	long int address;
	char *endptr;

	(void)option_name;
	(void)data;
	(void)error;

	if (!strlen(value))
		return TRUE;

	i = 0;
	tokens = g_strsplit(value, ",", -1);
	tmp = 0x00000000;

	while (tokens[i]) {
		g_strstrip(tokens[i]);

		if (!strlen(tokens[i])) {
			i++;
			continue;
		}

		address = strtoll(tokens[i], &endptr, 10);

		if (endptr == tokens[i] || *endptr != '\0') {
			g_critical("Invalid source address: %s.", tokens[i]);
			g_strfreev(tokens);
			return FALSE;
		}

		if (address < 0 || address > 31) {
			g_critical("Source address out of range: %li.",
				address);
			g_strfreev(tokens);
			return FALSE;
		}

		tmp |= (1 << address);
		i++;
	}

	/*
	 * Apply the instrumentation source address filter only if at least one
	 * valid source address was specified.
	 */
	if (tmp > 0)
		inst_address_filter = tmp;

	g_strfreev(tokens);

	return TRUE;
}

static GOptionEntry entries[] = {
	{"input-file", 'i', 0, G_OPTION_ARG_STRING, &input_file,
		"Load trace data from file", NULL},
	{"filter", 'f', 0, G_OPTION_ARG_CALLBACK, &parse_filter_option,
		"Filter for packet types", NULL},
	{"filter-inst", 0, 0, G_OPTION_ARG_CALLBACK, &parse_inst_filter_option,
		"Filter for instrumentation source addresses", NULL},
	{"dwt", 0, 0, G_OPTION_ARG_NONE, &opt_decode_dwt,
		"Enable DWT decoder", NULL},
	{NULL, 0, 0, 0, NULL, NULL, NULL}
};

static void handle_hw_packet(const union libswo_packet *packet)
{
	if (!(packet_type_filter & (1 << LIBSWO_PACKET_TYPE_HW)))
		return;

	if (opt_decode_dwt) {
		dwt_handle_packet(&packet->hw);
		return;
	}

	printf("Hardware source (address = %u, value = %x, size = %zu bytes)\n",
		packet->hw.address, packet->hw.value, packet->hw.size - 1);
}

static void handle_inst_packet(const union libswo_packet *packet)
{
	if (!(packet_type_filter & (1 << LIBSWO_PACKET_TYPE_INST)))
		return;

	if (!(inst_address_filter & (1 << packet->inst.address)))
		return;

	printf("Instrumentation (address = %u, value = %x, size = %zu bytes)\n",
		packet->inst.address, packet->inst.value,
		packet->inst.size - 1);
}

static void handle_overflow_packet(const union libswo_packet *packet)
{
	if (!(packet_type_filter & (1 << LIBSWO_PACKET_TYPE_OVERFLOW)))
		return;

	(void)packet;
	printf("Overflow\n");
}

static void handle_ext_packet(const union libswo_packet *packet)
{
	const char *src;

	if (!(packet_type_filter & (1 << LIBSWO_PACKET_TYPE_EXT)))
		return;

	switch (packet->ext.source) {
	case LIBSWO_EXT_SRC_ITM:
		src = "ITM";
		break;
	case LIBSWO_EXT_SRC_HW:
		src = "HW";
		break;
	}

	printf("Extension (source = %s, value = %x)\n", src,
		packet->ext.value);
}

static void handle_unknown_packet(const union libswo_packet *packet)
{
	if (!(packet_type_filter & (1 << LIBSWO_PACKET_TYPE_UNKNOWN)))
		return;

	printf("Unknown data (size = %zu bytes)\n", packet->unknown.size);
}

static void handle_sync_packet(const union libswo_packet *packet)
{
	if (!(packet_type_filter & (1 << LIBSWO_PACKET_TYPE_SYNC)))
		return;

	if (packet->sync.size % 8)
		printf("Synchronisation (size = %zu bits)\n",
			packet->sync.size);
	else
		printf("Synchronisation (size = %zu bytes)\n",
			packet->sync.size / 8);
}

static void handle_lts_packet(const union libswo_packet *packet)
{
	const char *tc;

	if (!(packet_type_filter & (1 << LIBSWO_PACKET_TYPE_LTS)))
		return;

	switch (packet->lts.relation) {
	case LIBSWO_LTS_REL_SYNC:
		tc = "synchronous";
		break;
	case LIBSWO_LTS_REL_TS:
		tc = "timestamp delayed";
		break;
	case LIBSWO_LTS_REL_SRC:
		tc = "data delayed";
		break;
	case LIBSWO_LTS_REL_BOTH:
		tc = "data and timestamp delayed";
		break;
	}

	printf("Local timestamp (relation = %s, value = %x)\n", tc,
		packet->lts.value);
}

static void handle_gts1_packet(const union libswo_packet *packet)
{
	if (!(packet_type_filter & (1 << LIBSWO_PACKET_TYPE_GTS1)))
		return;

	printf("Global timestamp (GTS1) (wrap = %u, clkch = %u, value = %x)\n",
		packet->gts1.wrap, packet->gts1.clkch, packet->gts1.value);
}

static void handle_gts2_packet(const union libswo_packet *packet)
{
	if (!(packet_type_filter & (1 << LIBSWO_PACKET_TYPE_GTS2)))
		return;

	printf("Global timestamp (GTS2) (value = %x)\n", packet->gts2.value);
}

static int packet_cb(struct libswo_context *ctx,
		const union libswo_packet *packet, void *user_data)
{
	(void)ctx;
	(void)user_data;

	switch (packet->type) {
	case LIBSWO_PACKET_TYPE_SYNC:
		handle_sync_packet(packet);
		break;
	case LIBSWO_PACKET_TYPE_INST:
		handle_inst_packet(packet);
		break;
	case LIBSWO_PACKET_TYPE_OVERFLOW:
		handle_overflow_packet(packet);
		break;
	case LIBSWO_PACKET_TYPE_EXT:
		handle_ext_packet(packet);
		break;
	case LIBSWO_PACKET_TYPE_LTS:
		handle_lts_packet(packet);
		break;
	case LIBSWO_PACKET_TYPE_GTS1:
		handle_gts1_packet(packet);
		break;
	case LIBSWO_PACKET_TYPE_GTS2:
		handle_gts2_packet(packet);
		break;
	case LIBSWO_PACKET_TYPE_HW:
		handle_hw_packet(packet);
		break;
	case LIBSWO_PACKET_TYPE_UNKNOWN:
		handle_unknown_packet(packet);
		break;
	default:
		break;
	}

	return 0;
}

static int parse_options(int *argc, char ***argv)
{
	GError *error;
	GOptionContext *context;

	error = NULL;

	context = g_option_context_new(NULL);
	g_option_context_add_main_entries(context, entries, NULL);

	if (!g_option_context_parse(context, argc, argv, &error)) {
		g_critical("%s.\n", error->message);
		g_error_free(error);
		g_option_context_free(context);
		return FALSE;
	}

	g_option_context_free(context);

	return TRUE;
}

int main(int argc, char **argv)
{
	int ret;
	struct libswo_context *ctx;
	uint8_t buffer[BUFFER_SIZE];
	GIOChannel *input;
	GError *error;
	GIOStatus iostat;
	gsize num;

	opt_decode_dwt = FALSE;

	/* Disable packet filtering for all packet types by default. */
	packet_type_filter = (1 << LIBSWO_PACKET_TYPE_SYNC) | \
		(1 << LIBSWO_PACKET_TYPE_OVERFLOW) | \
		(1 << LIBSWO_PACKET_TYPE_LTS) | \
		(1 << LIBSWO_PACKET_TYPE_GTS1) | \
		(1 << LIBSWO_PACKET_TYPE_GTS2) | \
		(1 << LIBSWO_PACKET_TYPE_EXT) | \
		(1 << LIBSWO_PACKET_TYPE_INST) | \
		(1 << LIBSWO_PACKET_TYPE_HW) | \
		(1 << LIBSWO_PACKET_TYPE_UNKNOWN);

	/* Disable instrumentation source address filtering by default. */
	inst_address_filter = 0xffffffff;

	if (!parse_options(&argc, &argv))
		return EXIT_FAILURE;

	error = NULL;

	if (input_file) {
		input = g_io_channel_new_file(input_file, "r", &error);

		if (!input) {
			g_critical("%s: %s.\n", input_file, error->message);
			g_error_free(error);
			g_free(input_file);
			return EXIT_FAILURE;
		}

		g_free(input_file);
	} else {
		input = g_io_channel_unix_new(STDIN_FILENO);
	}

	/* Set encoding to binary (default is UTF-8). */
	iostat = g_io_channel_set_encoding(input, NULL, &error);

	if (iostat != G_IO_STATUS_NORMAL) {
		g_critical("%s.\n", error->message);
		g_error_free(error);
		g_io_channel_unref(input);
		return EXIT_FAILURE;
	}

	if (!input_file)
		g_io_channel_set_buffered(input, FALSE);

	ret = libswo_init(&ctx, NULL, BUFFER_SIZE * 2);

	if (ret != LIBSWO_OK) {
		g_critical("libswo_init() failed: %s.\n",
			libswo_strerror_name(ret));
		g_io_channel_unref(input);
		return EXIT_FAILURE;
	}

	ret = libswo_set_callback(ctx, &packet_cb, NULL);

	if (ret != LIBSWO_OK) {
		g_critical("libswo_set_callback() failed: %s.\n",
			libswo_strerror_name(ret));
		g_io_channel_unref(input);
		libswo_exit(ctx);
		return EXIT_FAILURE;
	}

	while (TRUE) {
		iostat = g_io_channel_read_chars(input, (gchar *)buffer,
			BUFFER_SIZE, &num, &error);

		if (iostat == G_IO_STATUS_ERROR)
			break;

		ret = libswo_feed(ctx, buffer, num);

		if (ret != LIBSWO_OK) {
			g_critical("libswo_feed() failed: %s.\n",
				libswo_strerror_name(ret));
			g_io_channel_unref(input);
			libswo_exit(ctx);
			return EXIT_FAILURE;
		}

		ret = libswo_decode(ctx, 0, 0);

		if (ret < LIBSWO_OK) {
			g_critical("libswo_decode() failed: %s.\n",
				libswo_strerror_name(ret));
			g_io_channel_unref(input);
			libswo_exit(ctx);
			return EXIT_FAILURE;
		}

		if (iostat == G_IO_STATUS_EOF)
			break;
	}

	if (iostat == G_IO_STATUS_ERROR) {
		g_critical("%s.\n", error->message);
		g_error_free(error);
		g_io_channel_unref(input);
		libswo_exit(ctx);
		return EXIT_FAILURE;
	}

	ret = libswo_decode(ctx, 0, LIBSWO_DF_EOS);

	if (ret < LIBSWO_OK) {
		g_critical("libswo_decode() failed: %s.\n",
			libswo_strerror_name(ret));
		g_io_channel_unref(input);
		libswo_exit(ctx);
		return EXIT_FAILURE;
	}

	g_io_channel_unref(input);
	libswo_exit(ctx);

	return EXIT_SUCCESS;
}