aboutsummaryrefslogtreecommitdiff
path: root/tracecmd/trace-list.c
blob: fbf2882e1a83d0aa1cecd03e76a4922680937c8c (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
 *
 */

#include <stdlib.h>
#include <sys/stat.h>

#include "tracefs.h"
#include "trace-local.h"


static void dump_file_content(const char *path)
{
	char buf[BUFSIZ];
	ssize_t n;
	FILE *fp;

	fp = fopen(path, "r");
	if (!fp)
		die("reading %s", path);

	do {
		n = fread(buf, 1, BUFSIZ, fp);
		if (n > 0)
			fwrite(buf, 1, n, stdout);
	} while (n > 0);
	fclose(fp);
}



void show_instance_file(struct buffer_instance *instance, const char *name)
{
	char *path;

	path = tracefs_instance_get_file(instance->tracefs, name);
	dump_file_content(path);
	tracefs_put_tracing_file(path);
}

enum {
	SHOW_EVENT_FORMAT		= 1 << 0,
	SHOW_EVENT_FILTER		= 1 << 1,
	SHOW_EVENT_TRIGGER		= 1 << 2,
	SHOW_EVENT_FULL			= 1 << 3,
};


void show_file(const char *name)
{
	char *path;

	path = tracefs_get_tracing_file(name);
	dump_file_content(path);
	tracefs_put_tracing_file(path);
}

typedef int (*process_file_func)(char *buf, int len, int flags);

static void process_file_re(process_file_func func,
			    const char *name, const char *re, int flags)
{
	regex_t reg;
	char *path;
	char *buf = NULL;
	char *str;
	FILE *fp;
	ssize_t n;
	size_t l = strlen(re);

	/* Just in case :-p */
	if (!re || l == 0) {
		show_file(name);
		return;
	}

	/* Handle the newline at end of names for the user */
	str = malloc(l + 3);
	if (!str)
		die("Failed to allocate reg ex %s", re);
	strcpy(str, re);
	if (re[l-1] == '$')
		strcpy(&str[l-1], "\n*$");

	if (regcomp(&reg, str, REG_ICASE|REG_NOSUB))
		die("invalid function regex '%s'", re);

	free(str);

	path = tracefs_get_tracing_file(name);
	fp = fopen(path, "r");
	if (!fp)
		die("reading %s", path);
	tracefs_put_tracing_file(path);

	do {
		n = getline(&buf, &l, fp);
		if (n > 0 && regexec(&reg, buf, 0, NULL, 0) == 0)
			func(buf, n, flags);
	} while (n > 0);
	free(buf);
	fclose(fp);

	regfree(&reg);
}

static void show_event(process_file_func func, const char *system,
		       const char *event, int flags)
{
	char *buf;
	int ret;

	ret = asprintf(&buf, "%s:%s", system, event);
	if (ret < 0)
		die("Can not allocate event");
	func(buf, strlen(buf), flags);
	free(buf);
}

static void show_system(process_file_func func, const char *system, int flags)
{
	char **events;
	int e;

	events = tracefs_system_events(NULL, system);
	if (!events) /* die? */
		return;

	for (e = 0; events[e]; e++)
		show_event(func, system, events[e], flags);
}

static void show_event_systems(process_file_func func, char **systems, int flags)
{
	int s;

	for (s = 0; systems[s]; s++)
		show_system(func, systems[s], flags);
}

static void match_system_events(process_file_func func, const char *system,
				regex_t *reg, int flags)
{
	char **events;
	int e;

	events = tracefs_system_events(NULL, system);
	if (!events) /* die? */
		return;
	for (e = 0; events[e]; e++) {
		if (regexec(reg, events[e], 0, NULL, 0) == 0)
			show_event(func, system, events[e], flags);
	}
	tracefs_list_free(events);
}

static void process_events(process_file_func func, const char *re, int flags)
{
	const char *ftrace = "ftrace";
	regex_t system_reg;
	regex_t event_reg;
	char *str;
	size_t l = strlen(re);
	bool just_systems = true;
	char **systems;
	char *system;
	char *event;
	int s;

	systems = tracefs_event_systems(NULL);
	if (!systems)
		return process_file_re(func, "available_events", re, flags);

	if (!re || l == 0) {
		show_event_systems(func, systems, flags);
		return;
	}

	str = strdup(re);
	if (!str)
		die("Can not allocate momory for regex");

	system = strtok(str, ":");
	event = strtok(NULL, "");

	if (regcomp(&system_reg, system, REG_ICASE|REG_NOSUB))
		die("invalid regex '%s'", system);

	if (event) {
		if (regcomp(&event_reg, event, REG_ICASE|REG_NOSUB))
			die("invalid regex '%s'", event);
	} else {
		/*
		 * If the regex ends with ":", then event would be null,
		 * but we do not want to match events.
		 */
		if (re[l-1] != ':')
			just_systems = false;
	}
	free(str);

	/*
	 * See if this matches the special ftrace system, as ftrace is not included
	 * in the systems list, but can get events from tracefs_system_events().
	 */
	if (regexec(&system_reg, ftrace, 0, NULL, 0) == 0) {
		if (!event)
			show_system(func, ftrace, flags);
		else
			match_system_events(func, ftrace, &event_reg, flags);
	} else if (!just_systems) {
		match_system_events(func, ftrace, &system_reg, flags);
	}

	for (s = 0; systems[s]; s++) {

		if (regexec(&system_reg, systems[s], 0, NULL, 0) == 0) {
			if (!event) {
				show_system(func, systems[s], flags);
				continue;
			}
			match_system_events(func, systems[s], &event_reg, flags);
			continue;
		}
		if (just_systems)
			continue;

		match_system_events(func, systems[s], &system_reg, flags);
	}
	tracefs_list_free(systems);

	regfree(&system_reg);
	if (event)
		regfree(&event_reg);
}

static int show_file_write(char *buf, int len, int flags)
{
	return fwrite(buf, 1, len, stdout);
}

static void show_file_re(const char *name, const char *re)
{
	process_file_re(show_file_write, name, re, 0);
}

static char *get_event_file(const char *type, char *buf, int len)
{
	char *system;
	char *event;
	char *path;
	char *file;
	int ret;

	if (buf[len-1] == '\n')
		buf[len-1] = '\0';

	system = strtok(buf, ":");
	if (!system)
		die("no system found in %s", buf);

	event = strtok(NULL, ":");
	if (!event)
		die("no event found in %s\n", buf);

	path = tracefs_get_tracing_file("events");
	ret = asprintf(&file, "%s/%s/%s/%s", path, system, event, type);
	if (ret < 0)
		die("Failed to allocate event file %s %s", system, event);

	tracefs_put_tracing_file(path);

	return file;
}

static int event_filter_write(char *buf, int len, int flags)
{
	char *file;

	if (buf[len-1] == '\n')
		buf[len-1] = '\0';

	printf("%s\n", buf);

	file = get_event_file("filter", buf, len);
	dump_file_content(file);
	free(file);
	printf("\n");

	return 0;
}

static int event_trigger_write(char *buf, int len, int flags)
{
	char *file;

	if (buf[len-1] == '\n')
		buf[len-1] = '\0';

	printf("%s\n", buf);

	file = get_event_file("trigger", buf, len);
	dump_file_content(file);
	free(file);
	printf("\n");

	return 0;
}

static int event_format_write(char *fbuf, int len, int flags)
{
	char *file = get_event_file("format", fbuf, len);
	char *buf = NULL;
	size_t l;
	FILE *fp;
	bool full;
	int n;

	full = flags & SHOW_EVENT_FULL;

	/* The get_event_file() crops system in fbuf */
	printf("system: %s\n", fbuf);

	/* Don't print the print fmt, it's ugly */

	fp = fopen(file, "r");
	if (!fp)
		die("reading %s", file);

	do {
		n = getline(&buf, &l, fp);
		if (n > 0) {
			if (!full && strncmp(buf, "print fmt", 9) == 0)
				break;
			fwrite(buf, 1, n, stdout);
		}
	} while (n > 0);
	fclose(fp);
	free(buf);
	free(file);

	return 0;
}

static int event_name(char *buf, int len, int flags)
{
	printf("%s\n", buf);

	return 0;
}

static void show_event_filter_re(const char *re)
{
	process_events(event_filter_write, re, 0);
}


static void show_event_trigger_re(const char *re)
{
	process_events(event_trigger_write, re, 0);
}


static void show_event_format_re(const char *re, int flags)
{
	process_events(event_format_write, re, flags);
}

static void show_event_names_re(const char *re)
{
	process_events(event_name, re, 0);
}

static void show_events(const char *eventre, int flags)
{
	if (flags && !eventre)
		die("When specifying event files, an event must be named");

	if (eventre) {
		if (flags & SHOW_EVENT_FORMAT)
			show_event_format_re(eventre, flags);

		else if (flags & SHOW_EVENT_FILTER)
			show_event_filter_re(eventre);

		else if (flags & SHOW_EVENT_TRIGGER)
			show_event_trigger_re(eventre);
		else
			show_event_names_re(eventre);
	} else
		show_file("available_events");
}


static void show_tracers(void)
{
	show_file("available_tracers");
}

void show_options(const char *prefix, struct buffer_instance *buffer)
{
	struct tracefs_instance *instance = buffer ? buffer->tracefs : NULL;
	struct dirent *dent;
	struct stat st;
	char *path;
	DIR *dir;

	if (!prefix)
		prefix = "";

	path = tracefs_instance_get_file(instance, "options");
	if (!path)
		goto show_file;
	if (stat(path, &st) < 0)
		goto show_file;

	if ((st.st_mode & S_IFMT) != S_IFDIR)
		goto show_file;

	dir = opendir(path);
	if (!dir)
		die("Can not read instance directory");

	while ((dent = readdir(dir))) {
		const char *name = dent->d_name;
		long long val;
		char *file;
		int ret;

		if (strcmp(name, ".") == 0 ||
		    strcmp(name, "..") == 0)
			continue;

		ret = asprintf(&file, "options/%s", name);
		if (ret < 0)
			die("Failed to allocate file name");
		ret = tracefs_instance_file_read_number(instance, file, &val);
		if (!ret) {
			if (val)
				printf("%s%s\n", prefix, name);
			else
				printf("%sno%s\n", prefix, name);
		}
		free(file);
	}
	closedir(dir);
	tracefs_put_tracing_file(path);
	return;

 show_file:
	tracefs_put_tracing_file(path);
	show_file("trace_options");
}

static void show_clocks(void)
{
	char *clocks;
	int size;

	clocks = tracefs_instance_file_read(NULL, "trace_clock", &size);
	if (!clocks)
		die("getting clocks");
	if (clocks[size - 1] == '\n')
		clocks[size - 1] = 0;

	if (trace_tsc2nsec_is_supported())
		printf("%s %s\n", clocks, TSCNSEC_CLOCK);
	else
		printf("%s\n", clocks);

	free(clocks);
}


static void show_functions(const char *funcre)
{
	if (funcre)
		show_file_re("available_filter_functions", funcre);
	else
		show_file("available_filter_functions");
}


static void show_buffers(void)
{
	struct dirent *dent;
	DIR *dir;
	char *path;
	int printed = 0;

	path = tracefs_get_tracing_file("instances");
	dir = opendir(path);
	tracefs_put_tracing_file(path);
	if (!dir)
		die("Can not read instance directory");

	while ((dent = readdir(dir))) {
		const char *name = dent->d_name;

		if (strcmp(name, ".") == 0 ||
		    strcmp(name, "..") == 0)
			continue;

		printf("%s\n", name);
		printed = 1;
	}
	closedir(dir);

	if (!printed)
		printf("No buffer instances defined\n");
}


static void show_systems(void)
{
	struct dirent *dent;
	char *path;
	DIR *dir;

	path = tracefs_get_tracing_file("events");
	dir = opendir(path);

	if (!dir)
		die("Can not read events directory");

	while ((dent = readdir(dir))) {
		const char *name = dent->d_name;
		struct stat st;
		char *spath;
		int ret;

		if (strcmp(name, ".") == 0 ||
		    strcmp(name, "..") == 0)
			continue;

		if (asprintf(&spath, "%s/%s", path, name) < 0)
			continue;
		ret = stat(spath, &st);
		if (!ret && S_ISDIR(st.st_mode))
			printf("%s\n", name);

		free(spath);
	}

	printf("\n");
	closedir(dir);
	tracefs_put_tracing_file(path);
}

static void show_plugin_options(void)
{
	struct tep_handle *pevent;
	struct tep_plugin_list *list;
	struct trace_seq s;

	tracecmd_ftrace_load_options();

	pevent = tep_alloc();
	if (!pevent)
		die("Can not allocate pevent\n");

	trace_seq_init(&s);

	list = trace_load_plugins(pevent, 0);
	tep_plugin_print_options(&s);
	trace_seq_do_printf(&s);
	tep_unload_plugins(list, pevent);
	tep_free(pevent);
}


void trace_option(int argc, char **argv)
{
	show_plugin_options();
}


static void show_plugins(void)
{
	struct tep_handle *pevent;
	struct tep_plugin_list *list;
	struct trace_seq s;

	pevent = tep_alloc();
	if (!pevent)
		die("Can not allocate pevent\n");

	trace_seq_init(&s);

	list = trace_load_plugins(pevent, 0);
	tep_print_plugins(&s, "  ", "\n", list);

	trace_seq_do_printf(&s);
	tep_unload_plugins(list, pevent);
	tep_free(pevent);
}

static void show_compression(void)
{
	char **versions, **names;
	int c, i;

	c = tracecmd_compress_protos_get(&names, &versions);
	if (c <= 0) {
		printf("No compression algorithms are supported\n");
		return;
	}
	printf("Supported compression algorithms:\n");
	for (i = 0; i < c; i++)
		printf("\t%s, %s\n", names[i], versions[i]);

	free(names);
	free(versions);
}

void trace_list(int argc, char **argv)
{
	int events = 0;
	int tracer = 0;
	int options = 0;
	int funcs = 0;
	int buffers = 0;
	int clocks = 0;
	int plug = 0;
	int plug_op = 0;
	int flags = 0;
	int systems = 0;
	int show_all = 1;
	int compression = 0;
	int i;
	const char *arg;
	const char *funcre = NULL;
	const char *eventre = NULL;

	for (i = 2; i < argc; i++) {
		arg = NULL;
		if (argv[i][0] == '-') {
			if (i < argc - 1) {
				if (argv[i+1][0] != '-')
					arg = argv[i+1];
			}
			switch (argv[i][1]) {
			case 'h':
				usage(argv);
				break;
			case 'e':
				events = 1;
				eventre = arg;
				show_all = 0;
				break;
			case 'B':
				buffers = 1;
				show_all = 0;
				break;
			case 'C':
				clocks = 1;
				show_all = 0;
				break;
			case 'F':
				flags |= SHOW_EVENT_FORMAT;
				break;
			case 'R':
				flags |= SHOW_EVENT_TRIGGER;
				break;
			case 'l':
				flags |= SHOW_EVENT_FILTER;
				break;
			case 'p':
			case 't':
				tracer = 1;
				show_all = 0;
				break;
			case 'P':
				plug = 1;
				show_all = 0;
				break;
			case 'O':
				plug_op = 1;
				show_all = 0;
				break;
			case 'o':
				options = 1;
				show_all = 0;
				break;
			case 'f':
				funcs = 1;
				funcre = arg;
				show_all = 0;
				break;
			case 's':
				systems = 1;
				show_all = 0;
				break;
			case 'c':
				compression = 1;
				show_all = 0;
				break;
			case '-':
				if (strcmp(argv[i], "--debug") == 0) {
					tracecmd_set_debug(true);
					break;
				}
				if (strcmp(argv[i], "--full") == 0) {
					flags |= SHOW_EVENT_FULL;
					break;
				}
				fprintf(stderr, "list: invalid option -- '%s'\n",
					argv[i]);
			default:
				fprintf(stderr, "list: invalid option -- '%c'\n",
					argv[i][1]);
				usage(argv);
			}
		}
	}

	if (events)
		show_events(eventre, flags);

	if (tracer)
		show_tracers();

	if (options)
		show_options(NULL, NULL);

	if (plug)
		show_plugins();

	if (plug_op)
		show_plugin_options();

	if (funcs)
		show_functions(funcre);

	if (buffers)
		show_buffers();

	if (clocks)
		show_clocks();
	if (systems)
		show_systems();
	if (compression)
		show_compression();
	if (show_all) {
		printf("event systems:\n");
		show_systems();
		printf("events:\n");
		show_events(NULL, 0);
		printf("\ntracers:\n");
		show_tracers();
		printf("\noptions:\n");
		show_options(NULL, NULL);
		show_compression();
	}

	return;

}