aboutsummaryrefslogtreecommitdiffstats
path: root/trace-cmd.c
diff options
context:
space:
mode:
authorSteven Rostedt <srostedt@redhat.com>2010-01-22 12:48:10 -0500
committerSteven Rostedt <rostedt@goodmis.org>2010-02-01 22:29:28 -0500
commit6f1c80b937eef1608aeed03b2f0df6b13554337e (patch)
tree0f748c8d37cc4d4998d3e72409929620bfe39791 /trace-cmd.c
parentc42213e4b113cdea8062a30945d1101dc02a8d31 (diff)
trace-cmd: Use glob() library call to enable events
Use of the glob() library call makes the enabling of events more robust. Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
Diffstat (limited to 'trace-cmd.c')
-rw-r--r--trace-cmd.c157
1 files changed, 130 insertions, 27 deletions
diff --git a/trace-cmd.c b/trace-cmd.c
index d13cb15..00caf53 100644
--- a/trace-cmd.c
+++ b/trace-cmd.c
@@ -32,6 +32,7 @@
32#include <unistd.h> 32#include <unistd.h>
33#include <ctype.h> 33#include <ctype.h>
34#include <errno.h> 34#include <errno.h>
35#include <glob.h>
35 36
36#include "trace-local.h" 37#include "trace-local.h"
37#include "version.h" 38#include "version.h"
@@ -359,31 +360,102 @@ static void set_option(const char *option)
359 fclose(fp); 360 fclose(fp);
360} 361}
361 362
363static void old_enable_events(const char *name)
364{
365 char *path;
366 FILE *fp;
367 int ret;
368
369 if (strcmp(name, "all") == 0)
370 name = "*:*";
371
372 /* need to use old way */
373 path = get_tracing_file("set_event");
374 fp = fopen(path, "w");
375 if (!fp)
376 die("opening '%s'", path);
377 put_tracing_file(path);
378
379 ret = fwrite(name, 1, strlen(name), fp);
380 if (ret < 0)
381 die("bad event '%s'", name);
382
383 ret = fwrite("\n", 1, 1, fp);
384 if (ret < 0)
385 die("bad event '%s'", name);
386
387 fclose(fp);
388
389 return;
390}
391
392static int enable_glob(const char *name)
393{
394 glob_t globbuf;
395 FILE *fp;
396 char *path;
397 char *str;
398 int len;
399 int ret;
400 int i;
401 int count = 0;
402
403 len = strlen(name) + strlen("events//enable") + 1;
404 str = malloc_or_die(len);
405 snprintf(str, len, "events/%s/enable", name);
406 path = get_tracing_file(str);
407 free(str);
408
409 globbuf.gl_offs = 0;
410 printf("path = %s\n", path);
411 ret = glob(path, GLOB_ONLYDIR, NULL, &globbuf);
412 put_tracing_file(path);
413 if (ret < 0)
414 return 0;
415
416 for (i = 0; i < globbuf.gl_pathc; i++) {
417 path = globbuf.gl_pathv[i];
418
419 fp = fopen(path, "w");
420 if (!fp)
421 die("writing to '%s'", path);
422 ret = fwrite("1", 1, 1, fp);
423 fclose(fp);
424 if (ret < 0)
425 die("writing to '%s'", path);
426
427 count++;
428 }
429 globfree(&globbuf);
430 return count;
431}
432
362static void enable_event(const char *name) 433static void enable_event(const char *name)
363{ 434{
364 struct stat st; 435 struct stat st;
365 FILE *fp; 436 FILE *fp;
366 char *path; 437 char *path;
438 char *str;
439 char *ptr;
440 int len;
367 int ret; 441 int ret;
442 int ret2;
443
444 /* Check if the kernel has the events/enable file */
445 path = get_tracing_file("events/enable");
446 ret = stat(path, &st);
447 if (ret < 0) {
448 put_tracing_file(path);
449 /* old kernel */
450 old_enable_events(name);
451 return;
452 }
368 453
369 fprintf(stderr, "enable %s\n", name); 454 fprintf(stderr, "enable %s\n", name);
370 if (strcmp(name, "all") == 0) {
371 path = get_tracing_file("events/enable");
372 455
373 ret = stat(path, &st); 456 /* We allow the user to use "all" to enable all events */
374 if (ret < 0) {
375 put_tracing_file(path);
376 /* need to use old way */
377 path = get_tracing_file("set_event");
378 fp = fopen(path, "w");
379 if (!fp)
380 die("writing to '%s'", path);
381 put_tracing_file(path);
382 fwrite("*:*\n", 4, 1, fp);
383 fclose(fp);
384 return;
385 }
386 457
458 if (strcmp(name, "all") == 0) {
387 fp = fopen(path, "w"); 459 fp = fopen(path, "w");
388 if (!fp) 460 if (!fp)
389 die("writing to '%s'", path); 461 die("writing to '%s'", path);
@@ -395,18 +467,49 @@ static void enable_event(const char *name)
395 return; 467 return;
396 } 468 }
397 469
398 path = get_tracing_file("set_event"); 470 ptr = strchr(name, ':');
399 fp = fopen(path, "a"); 471
400 if (!fp) 472 if (ptr) {
401 die("writing to '%s'", path); 473 len = ptr - name;
402 put_tracing_file(path); 474 str = strdup(name);
403 ret = fwrite(name, 1, strlen(name), fp); 475 if (!str)
404 if (ret < 0) 476 die("could not allocate memory");
405 die("bad event '%s'", name); 477 str[len] = 0;
406 ret = fwrite("\n", 1, 1, fp); 478 ptr++;
407 if (ret < 0) 479 if (!strlen(ptr) || strcmp(ptr, "*") == 0) {
408 die("bad event '%s'", name); 480 ret = enable_glob(str);
409 fclose(fp); 481 free(str);
482 put_tracing_file(path);
483 if (!ret)
484 goto fail;
485 return;
486 }
487
488 str[len] = '/';
489
490 ret = enable_glob(str);
491 free(str);
492 if (!ret)
493 die("No events enabled with %s", name);
494 return;
495 }
496
497 /* No ':' so enable all matching systems and events */
498 ret = enable_glob(name);
499
500 len = strlen(name) + strlen("*/") + 1;
501 str = malloc_or_die(len);
502 snprintf(str, len, "*/%s", name);
503 ret2 = enable_glob(str);
504 free(str);
505
506 if (!ret && !ret2)
507 goto fail;
508
509 return;
510 fail:
511 die("No events enabled with %s", name);
512
410} 513}
411 514
412static void disable_event(const char *name) 515static void disable_event(const char *name)