util.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "util.h"
  2. #include "../debug.h"
  3. /*
  4. * Default error logging functions
  5. */
  6. static int perf_stdio__error(const char *format, va_list args)
  7. {
  8. fprintf(stderr, "Error:\n");
  9. vfprintf(stderr, format, args);
  10. return 0;
  11. }
  12. static int perf_stdio__warning(const char *format, va_list args)
  13. {
  14. fprintf(stderr, "Warning:\n");
  15. vfprintf(stderr, format, args);
  16. return 0;
  17. }
  18. static struct perf_error_ops default_eops =
  19. {
  20. .error = perf_stdio__error,
  21. .warning = perf_stdio__warning,
  22. };
  23. static struct perf_error_ops *perf_eops = &default_eops;
  24. int ui__error(const char *format, ...)
  25. {
  26. int ret;
  27. va_list args;
  28. va_start(args, format);
  29. ret = perf_eops->error(format, args);
  30. va_end(args);
  31. return ret;
  32. }
  33. int ui__warning(const char *format, ...)
  34. {
  35. int ret;
  36. va_list args;
  37. va_start(args, format);
  38. ret = perf_eops->warning(format, args);
  39. va_end(args);
  40. return ret;
  41. }
  42. int ui__error_paranoid(void)
  43. {
  44. return ui__error("Permission error - are you root?\n"
  45. "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
  46. " -1 - Not paranoid at all\n"
  47. " 0 - Disallow raw tracepoint access for unpriv\n"
  48. " 1 - Disallow cpu events for unpriv\n"
  49. " 2 - Disallow kernel profiling for unpriv\n");
  50. }
  51. /**
  52. * perf_error__register - Register error logging functions
  53. * @eops: The pointer to error logging function struct
  54. *
  55. * Register UI-specific error logging functions. Before calling this,
  56. * other logging functions should be unregistered, if any.
  57. */
  58. int perf_error__register(struct perf_error_ops *eops)
  59. {
  60. if (perf_eops != &default_eops)
  61. return -1;
  62. perf_eops = eops;
  63. return 0;
  64. }
  65. /**
  66. * perf_error__unregister - Unregister error logging functions
  67. * @eops: The pointer to error logging function struct
  68. *
  69. * Unregister already registered error logging functions.
  70. */
  71. int perf_error__unregister(struct perf_error_ops *eops)
  72. {
  73. if (perf_eops != eops)
  74. return -1;
  75. perf_eops = &default_eops;
  76. return 0;
  77. }