aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: a9bec50bc9a4e0d0e34b4282b8623dc83e6aedbc (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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/*
 * This file is part of the swodec project.
 *
 * Copyright (C) 2014-2016 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/>.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

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

#include <libswo/libswo.h>

#include "version.h"

/*
 * Exception names according to section B1.5 of ARMv7-M Architecture Reference
 * Manual.
 */
static const char *exception_names[] = {
	"Thread",
	"Reset",
	"NMI",
	"HardFault",
	"MemManage",
	"BusFault",
	"UsageFault",
	"Reserved",
	"Reserved",
	"Reserved",
	"Reserved",
	"SVCall",
	"Debug Monitor",
	"Reserved",
	"PendSV",
	"SysTick"
};

/* Number of exception names. */
#define NUM_EXCEPTION_NAMES	16

#define BUFFER_SIZE	1024

static gboolean opt_version;
static enum libswo_log_level opt_log_level;
static gchar *input_file = NULL;
static uint32_t packet_type_filter;
static uint32_t inst_address_filter;
static gboolean opt_dump_inst;

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

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

	if (!strlen(value))
		return TRUE;

	if (value[0] == '~') {
		value++;
		invert = TRUE;
	} else {
		invert = FALSE;
	}

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

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

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

		if (!g_ascii_strcasecmp(tokens[i], "unknown")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_UNKNOWN);
		} else 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], "dwt")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_DWT_EVTCNT);
			tmp |= (1 << LIBSWO_PACKET_TYPE_DWT_EXCTRACE);
			tmp |= (1 << LIBSWO_PACKET_TYPE_DWT_PC_SAMPLE);
			tmp |= (1 << LIBSWO_PACKET_TYPE_DWT_PC_VALUE);
			tmp |= (1 << LIBSWO_PACKET_TYPE_DWT_ADDR_OFFSET);
			tmp |= (1 << LIBSWO_PACKET_TYPE_DWT_DATA_VALUE);
		} else if (!g_ascii_strcasecmp(tokens[i], "evcnt")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_DWT_EVTCNT);
		} else if (!g_ascii_strcasecmp(tokens[i], "exc")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_DWT_EXCTRACE);
		} else if (!g_ascii_strcasecmp(tokens[i], "pc")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_DWT_PC_SAMPLE);
		} else if (!g_ascii_strcasecmp(tokens[i], "dtpc")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_DWT_PC_VALUE);
		} else if (!g_ascii_strcasecmp(tokens[i], "dtaddr")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_DWT_ADDR_OFFSET);
		} else if (!g_ascii_strcasecmp(tokens[i], "dtval")) {
			tmp |= (1 << LIBSWO_PACKET_TYPE_DWT_DATA_VALUE);
		} else {
			g_critical("Invalid packet type: %s.", tokens[i]);
			g_strfreev(tokens);
			return FALSE;
		}

		i++;
	}

	if (invert)
		tmp = ~tmp;

	/*
	 * 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;
	gboolean invert;

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

	if (!strlen(value))
		return TRUE;

	if (value[0] == '~') {
		value++;
		invert = TRUE;
	} else {
		invert = FALSE;
	}

	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++;
	}

	if (invert)
		tmp = ~tmp;

	/*
	 * 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 gboolean parse_log_level_option(const gchar *option_name,
		const gchar *value, gpointer data, GError **error)
{
	uint64_t tmp;
	gchar *endptr;

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

	if (!g_ascii_strcasecmp(value, "none")) {
		opt_log_level = LIBSWO_LOG_LEVEL_NONE;
	} else if (!g_ascii_strcasecmp(value, "error")) {
		opt_log_level = LIBSWO_LOG_LEVEL_ERROR;
	} else if (!g_ascii_strcasecmp(value, "warn")) {
		opt_log_level = LIBSWO_LOG_LEVEL_WARNING;
	} else if (!g_ascii_strcasecmp(value, "info")) {
		opt_log_level = LIBSWO_LOG_LEVEL_INFO;
	} else if (!g_ascii_strcasecmp(value, "debug")) {
		opt_log_level = LIBSWO_LOG_LEVEL_DEBUG;
	} else {
		errno = 0;
		tmp = g_ascii_strtoull(value, &endptr, 10);

		if (*endptr != '\0' || errno != 0 ||
				tmp > LIBSWO_LOG_LEVEL_DEBUG) {
			g_critical("Invalid log level: %s.", value);
			return FALSE;
		}

		opt_log_level = tmp;
	}

	return TRUE;
}

static GOptionEntry entries[] = {
	{"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version,
		"Show version information", NULL},
	{"log-level", 'l', 0, G_OPTION_ARG_CALLBACK, &parse_log_level_option,
		"Log level", NULL},
	{"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &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},
	{"dump-inst", 0, 0, G_OPTION_ARG_NONE, &opt_dump_inst,
		"Dump instrumentation payload", NULL},
	{NULL, 0, 0, 0, NULL, NULL, NULL}
};

static void handle_hw_packet(const union libswo_packet *packet)
{
	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 (!(inst_address_filter & (1 << packet->inst.address)))
		return;

	if (opt_dump_inst) {
		fwrite(packet->inst.payload, packet->inst.size - 1, 1, stdout);
		fflush(stdout);
		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)
{
	(void)packet;
	printf("Overflow\n");
}

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

	switch (packet->ext.source) {
	case LIBSWO_EXT_SRC_ITM:
		src = "ITM";
		break;
	case LIBSWO_EXT_SRC_HW:
		src = "HW";
		break;
	default:
		g_warning("Extension packet with invalid source: %u.",
			packet->ext.source);
		return;
	}

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

static void handle_unknown_packet(const union libswo_packet *packet)
{
	printf("Unknown data (size = %zu bytes)\n", packet->unknown.size);
}

static void handle_sync_packet(const union libswo_packet *packet)
{
	if (packet->sync.size % 8)
		printf("Synchronization (size = %zu bits)\n",
			packet->sync.size);
	else
		printf("Synchronization (size = %zu bytes)\n",
			packet->sync.size / 8);
}

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

	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;
	default:
		g_warning("Local timestamp packet with invalid relation: %u.",
			packet->lts.relation);
		return;
	}

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

static void handle_gts1_packet(const union libswo_packet *packet)
{
	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)
{
	printf("Global timestamp (GTS2) (value = %x)\n", packet->gts2.value);
}

static void handle_dwt_evtcnt_packet(const union libswo_packet *packet)
{
	printf("Event counter (CPI = %u, exc = %u, sleep = %u, LSU = %u, "
		"fold = %u, cyc = %u)\n", packet->evtcnt.cpi,
		packet->evtcnt.exc, packet->evtcnt.sleep, packet->evtcnt.lsu,
		packet->evtcnt.fold, packet->evtcnt.cyc);
}

static void handle_dwt_exctrace_packet(const union libswo_packet *packet)
{
	uint16_t exception;
	const char *func;
	const char *name;
	char buf[23];

	switch (packet->exctrace.function) {
	case LIBSWO_EXCTRACE_FUNC_ENTER:
		func = "enter";
		break;
	case LIBSWO_EXCTRACE_FUNC_EXIT:
		func = "exit";
		break;
	case LIBSWO_EXCTRACE_FUNC_RETURN:
		func = "return";
		break;
	default:
		func = "reserved";
	}

	exception = packet->exctrace.exception;

	if (exception < NUM_EXCEPTION_NAMES) {
		name = exception_names[exception];
	} else {
		snprintf(buf, sizeof(buf), "External interrupt %u",
			exception - NUM_EXCEPTION_NAMES);
		name = buf;
	}

	printf("Exception trace (function = %s, exception = %s)\n", func,
		name);
}

static void handle_dwt_pc_sample_packet(const union libswo_packet *packet)
{
	if (packet->pc_sample.sleep)
		printf("Periodic PC sleep\n");
	else
		printf("Periodic PC sample (value = %x)\n",
			packet->pc_sample.pc);
}

static void handle_dwt_pc_value_packet(const union libswo_packet *packet)
{
	printf("Data trace PC value (comparator = %u, value = %x)\n",
		packet->pc_value.cmpn, packet->pc_value.pc);
}

static void handle_dwt_addr_offset_packet(const union libswo_packet *packet)
{
	printf("Data trace address offset (comparator = %u, value = %x)\n",
		packet->addr_offset.cmpn, packet->addr_offset.offset);
}

static void handle_dwt_data_value_packet(const union libswo_packet *packet)
{
	printf("Data trace data value (comparator = %u, WnR = %u, value = %x, "
		"size = %zu bytes)\n", packet->data_value.cmpn,
		packet->data_value.wnr, packet->data_value.data_value,
		packet->data_value.size - 1);
}

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

	if (!(packet_type_filter & (1 << packet->type)))
		return TRUE;

	switch (packet->type) {
	case LIBSWO_PACKET_TYPE_UNKNOWN:
		handle_unknown_packet(packet);
		break;
	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_DWT_EVTCNT:
		handle_dwt_evtcnt_packet(packet);
		break;
	case LIBSWO_PACKET_TYPE_DWT_EXCTRACE:
		handle_dwt_exctrace_packet(packet);
		break;
	case LIBSWO_PACKET_TYPE_DWT_PC_SAMPLE:
		handle_dwt_pc_sample_packet(packet);
		break;
	case LIBSWO_PACKET_TYPE_DWT_PC_VALUE:
		handle_dwt_pc_value_packet(packet);
		break;
	case LIBSWO_PACKET_TYPE_DWT_ADDR_OFFSET:
		handle_dwt_addr_offset_packet(packet);
		break;
	case LIBSWO_PACKET_TYPE_DWT_DATA_VALUE:
		handle_dwt_data_value_packet(packet);
		break;
	default:
		g_warning("Invalid packet type: %u.", packet->type);
		break;
	}

	return TRUE;
}

static void show_version(void)
{
	printf("%s\n", SWODEC_VERSION_PACKAGE_STRING);
	printf("Using libswo %s\n", libswo_version_package_get_string());
}

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.", error->message);
		g_error_free(error);
		g_option_context_free(context);
		return FALSE;
	}

	g_option_context_free(context);

	return TRUE;
}

static void log_handler(const gchar *domain, GLogLevelFlags level,
		const gchar *message, gpointer user_data)
{
	enum libswo_log_level tmp;

	(void)domain;
	(void)user_data;

	if (level & G_LOG_LEVEL_ERROR)
		tmp = LIBSWO_LOG_LEVEL_ERROR;
	else if (level & G_LOG_LEVEL_CRITICAL)
		tmp = LIBSWO_LOG_LEVEL_ERROR;
	else if (level & G_LOG_LEVEL_WARNING)
		tmp = LIBSWO_LOG_LEVEL_WARNING;
	else if (level & G_LOG_LEVEL_MESSAGE)
		tmp = LIBSWO_LOG_LEVEL_INFO;
	else if (level & G_LOG_LEVEL_INFO)
		tmp = LIBSWO_LOG_LEVEL_INFO;
	else if (level & G_LOG_LEVEL_DEBUG)
		tmp = LIBSWO_LOG_LEVEL_DEBUG;
	else
		tmp = LIBSWO_LOG_LEVEL_WARNING;

	if (tmp > opt_log_level)
		return;

	fprintf(stderr, "%s\n", message);
	fflush(stderr);
}

static int decoder_log_callback(struct libswo_context *ctx,
		enum libswo_log_level level, const char *format, va_list args,
		void *user_data)
{
	(void)ctx;
	(void)user_data;

	if (level > opt_log_level)
		return 0;

	fprintf(stderr, "libswo: ");
	vfprintf(stderr, format, args);
	fprintf(stderr, "\n");

	return 0;
}

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_version = FALSE;
	opt_log_level = LIBSWO_LOG_LEVEL_WARNING;
	opt_dump_inst = FALSE;

	g_log_set_default_handler(&log_handler, NULL);

	/* Disable packet filtering for all packet types by default. */
	packet_type_filter = (1 << LIBSWO_PACKET_TYPE_UNKNOWN) | \
		(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_DWT_EVTCNT) | \
		(1 << LIBSWO_PACKET_TYPE_DWT_EXCTRACE) | \
		(1 << LIBSWO_PACKET_TYPE_DWT_PC_SAMPLE) | \
		(1 << LIBSWO_PACKET_TYPE_DWT_PC_VALUE) | \
		(1 << LIBSWO_PACKET_TYPE_DWT_ADDR_OFFSET) | \
		(1 << LIBSWO_PACKET_TYPE_DWT_DATA_VALUE);

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

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

	if (opt_version) {
		show_version();
		return EXIT_SUCCESS;
	}

	if (opt_dump_inst)
		packet_type_filter = (1 << LIBSWO_PACKET_TYPE_INST);

	error = NULL;

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

		if (!input) {
			g_critical("%s: %s.", 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.", error->message);
		g_error_free(error);
		g_io_channel_unref(input);
		return EXIT_FAILURE;
	}

	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.",
			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.",
			libswo_strerror_name(ret));
		g_io_channel_unref(input);
		libswo_exit(ctx);
		return EXIT_FAILURE;
	}

	ret = libswo_log_set_callback(ctx, &decoder_log_callback, NULL);

	if (ret != LIBSWO_OK) {
		g_critical("libswo_log_set_callback() failed: %s.",
			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.",
				libswo_strerror_name(ret));
			g_io_channel_unref(input);
			libswo_exit(ctx);
			return EXIT_FAILURE;
		}

		ret = libswo_decode(ctx, 0);

		if (ret != LIBSWO_OK) {
			g_critical("libswo_decode() failed: %s.",
				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.", error->message);
		g_error_free(error);
		g_io_channel_unref(input);
		libswo_exit(ctx);
		return EXIT_FAILURE;
	}

	ret = libswo_decode(ctx, LIBSWO_DF_EOS);

	if (ret != LIBSWO_OK) {
		g_critical("libswo_decode() failed: %s.",
			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;
}