-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrace-cmd.c
445 lines (381 loc) · 8.51 KB
/
trace-cmd.c
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
/*
* Copyright (C) 2008, 2009, 2010 Red Hat Inc, Steven Rostedt <[email protected]>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* 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; version 2 of the License (not later!)
*
* 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 <string.h>
#include <unistd.h>
#include <errno.h>
#include "trace-local.h"
int silence_warnings;
int show_status;
void warning(const char *fmt, ...)
{
va_list ap;
if (silence_warnings)
return;
if (errno)
perror("trace-cmd");
errno = 0;
va_start(ap, fmt);
fprintf(stderr, " ");
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
}
void pr_stat(const char *fmt, ...)
{
va_list ap;
if (!show_status)
return;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
}
void *malloc_or_die(unsigned int size)
{
void *data;
data = malloc(size);
if (!data)
die("malloc");
return data;
}
void show_file(const char *name)
{
char buf[BUFSIZ];
char *path;
FILE *fp;
ssize_t n;
path = tracecmd_get_tracing_file(name);
fp = fopen(path, "r");
if (!fp)
die("reading %s", path);
tracecmd_put_tracing_file(path);
do {
n = fread(buf, 1, BUFSIZ, fp);
if (n > 0)
fwrite(buf, 1, n, stdout);
} while (n > 0);
fclose(fp);
}
static void show_file_re(const char *name, const char *re)
{
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_or_die(l + 3);
strcpy(str, re);
if (re[l-1] == '$')
strcpy(&str[l-1], "\n*$");
if (regcomp(®, str, REG_ICASE|REG_NOSUB))
die("invalid function regex '%s'", re);
free(str);
path = tracecmd_get_tracing_file(name);
fp = fopen(path, "r");
if (!fp)
die("reading %s", path);
tracecmd_put_tracing_file(path);
do {
n = getline(&buf, &l, fp);
if (n > 0 && regexec(®, buf, 0, NULL, 0) == 0)
fwrite(buf, 1, n, stdout);
} while (n > 0);
free(buf);
fclose(fp);
regfree(®);
}
static void show_events(void)
{
show_file("available_events");
}
static void show_tracers(void)
{
show_file("available_tracers");
}
static void show_options(void)
{
show_file("trace_options");
}
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_plugins(void)
{
struct pevent *pevent;
struct plugin_list *list;
struct trace_seq s;
pevent = pevent_alloc();
if (!pevent)
die("Can not allocate pevent\n");
trace_seq_init(&s);
list = tracecmd_load_plugins(pevent);
trace_util_print_plugins(&s, " ", "\n", list);
trace_seq_do_printf(&s);
tracecmd_unload_plugins(list);
pevent_free(pevent);
}
static void show_plugin_options(void)
{
struct pevent *pevent;
struct plugin_list *list;
struct trace_seq s;
tracecmd_ftrace_load_options();
pevent = pevent_alloc();
if (!pevent)
die("Can not allocate pevent\n");
trace_seq_init(&s);
list = tracecmd_load_plugins(pevent);
trace_util_print_plugin_options(&s);
trace_seq_do_printf(&s);
tracecmd_unload_plugins(list);
pevent_free(pevent);
}
int main (int argc, char **argv)
{
int c;
errno = 0;
if (argc < 2)
usage(argv);
if (strcmp(argv[1], "report") == 0) {
trace_report(argc, argv);
exit(0);
} else if (strcmp(argv[1], "snapshot") == 0) {
trace_snapshot(argc, argv);
exit(0);
} else if (strcmp(argv[1], "hist") == 0) {
trace_hist(argc, argv);
exit(0);
} else if (strcmp(argv[1], "mem") == 0) {
trace_mem(argc, argv);
exit(0);
} else if (strcmp(argv[1], "listen") == 0) {
trace_listen(argc, argv);
exit(0);
} else if (strcmp(argv[1], "split") == 0) {
trace_split(argc, argv);
exit(0);
} else if (strcmp(argv[1], "restore") == 0) {
trace_restore(argc, argv);
exit(0);
} else if (strcmp(argv[1], "stack") == 0) {
trace_stack(argc, argv);
exit(0);
} else if (strcmp(argv[1], "check-events") == 0) {
char *tracing;
int ret;
struct pevent *pevent = NULL;
struct plugin_list *list = NULL;
while ((c = getopt(argc-1, argv+1, "+hN")) >= 0) {
switch (c) {
case 'h':
default:
usage(argv);
break;
case 'N':
tracecmd_disable_plugins = 1;
break;
}
}
tracing = tracecmd_find_tracing_dir();
if (!tracing) {
printf("Can not find or mount tracing directory!\n"
"Either tracing is not configured for this "
"kernel\n"
"or you do not have the proper permissions to "
"mount the directory");
exit(EINVAL);
}
pevent = pevent_alloc();
if (!pevent)
exit(EINVAL);
list = tracecmd_load_plugins(pevent);
ret = tracecmd_fill_local_events(tracing, pevent);
if (ret || pevent->parsing_failures)
ret = EINVAL;
tracecmd_unload_plugins(list);
pevent_free(pevent);
exit(ret);
} else if (strcmp(argv[1], "record") == 0 ||
strcmp(argv[1], "start") == 0 ||
strcmp(argv[1], "extract") == 0 ||
strcmp(argv[1], "stop") == 0 ||
strcmp(argv[1], "reset") == 0) {
trace_record(argc, argv);
exit(0);
} else if (strcmp(argv[1], "options") == 0) {
show_plugin_options();
exit(0);
} else if (strcmp(argv[1], "show") == 0) {
const char *buffer = NULL;
const char *file = "trace";
const char *cpu = NULL;
char cpu_path[128];
char *path;
int snap = 0;
int pipe = 0;
int show_name = 0;
while ((c = getopt(argc-1, argv+1, "B:c:fsp")) >= 0) {
switch (c) {
case 'h':
usage(argv);
break;
case 'B':
if (buffer)
die("Can only show one buffer at a time");
buffer = optarg;
break;
case 'c':
if (cpu)
die("Can only show one CPU at a time");
cpu = optarg;
break;
case 'f':
show_name = 1;
break;
case 's':
snap = 1;
if (pipe)
die("Can not have -s and -p together");
break;
case 'p':
pipe = 1;
if (snap)
die("Can not have -s and -p together");
break;
default:
usage(argv);
}
}
if (pipe)
file = "trace_pipe";
else if (snap)
file = "snapshot";
if (cpu) {
snprintf(cpu_path, 128, "per_cpu/cpu%d/%s", atoi(cpu), file);
file = cpu_path;
}
if (buffer) {
path = malloc_or_die(strlen(buffer) + strlen("instances//") +
strlen(file) + 1);
sprintf(path, "instances/%s/%s", buffer, file);
file = path;
}
if (show_name) {
char *name;
name = tracecmd_get_tracing_file(file);
printf("%s\n", name);
tracecmd_put_tracing_file(name);
}
show_file(file);
if (buffer)
free(path);
exit(0);
} else if (strcmp(argv[1], "list") == 0) {
int events = 0;
int tracer = 0;
int options = 0;
int funcs = 0;
int plug = 0;
int plug_op = 0;
const char *funcre = NULL;
while ((c = getopt(argc-1, argv+1, ":heptPOof:")) >= 0) {
next:
switch (c) {
case 'h':
usage(argv);
break;
case 'e':
events = 1;
break;
case 'p':
case 't':
tracer = 1;
break;
case 'P':
plug = 1;
break;
case 'O':
plug_op = 1;
break;
case 'o':
options = 1;
break;
case 'f':
funcs = 1;
if (optarg[0] == '-') {
c = optarg[1];
goto next;
}
funcre = optarg;
break;
default:
/* -f can have an optional parameter */
if (strcmp(argv[optind], "-f") == 0) {
funcs = 1;
break;
}
fprintf(stderr, "list: invalid option -- '%c'\n",
argv[optind][1]);
usage(argv);
}
}
if (events)
show_events();
if (tracer)
show_tracers();
if (options)
show_options();
if (plug)
show_plugins();
if (plug_op)
show_plugin_options();
if (funcs)
show_functions(funcre);
if (!events && !tracer && !options && !plug && !plug_op && !funcs) {
printf("events:\n");
show_events();
printf("\ntracers:\n");
show_tracers();
printf("\noptions:\n");
show_options();
}
exit(0);
} else if (strcmp(argv[1], "-h") == 0 ||
strcmp(argv[1], "help") == 0) {
usage(argv);
} else {
fprintf(stderr, "unknown command: %s\n", argv[1]);
usage(argv);
}
return 0;
}