session.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <linux/kernel.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include "session.h"
  5. #include "util.h"
  6. static int perf_session__open(struct perf_session *self, bool force)
  7. {
  8. struct stat input_stat;
  9. self->fd = open(self->filename, O_RDONLY);
  10. if (self->fd < 0) {
  11. pr_err("failed to open file: %s", self->filename);
  12. if (!strcmp(self->filename, "perf.data"))
  13. pr_err(" (try 'perf record' first)");
  14. pr_err("\n");
  15. return -errno;
  16. }
  17. if (fstat(self->fd, &input_stat) < 0)
  18. goto out_close;
  19. if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
  20. pr_err("file %s not owned by current user or root\n",
  21. self->filename);
  22. goto out_close;
  23. }
  24. if (!input_stat.st_size) {
  25. pr_info("zero-sized file (%s), nothing to do!\n",
  26. self->filename);
  27. goto out_close;
  28. }
  29. if (perf_header__read(&self->header, self->fd) < 0) {
  30. pr_err("incompatible file format");
  31. goto out_close;
  32. }
  33. self->size = input_stat.st_size;
  34. return 0;
  35. out_close:
  36. close(self->fd);
  37. self->fd = -1;
  38. return -1;
  39. }
  40. struct perf_session *perf_session__new(const char *filename, int mode,
  41. bool force, struct symbol_conf *conf)
  42. {
  43. size_t len = filename ? strlen(filename) + 1 : 0;
  44. struct perf_session *self = zalloc(sizeof(*self) + len);
  45. if (self == NULL)
  46. goto out;
  47. if (perf_header__init(&self->header) < 0)
  48. goto out_free;
  49. memcpy(self->filename, filename, len);
  50. self->threads = RB_ROOT;
  51. self->last_match = NULL;
  52. self->mmap_window = 32;
  53. self->cwd = NULL;
  54. self->cwdlen = 0;
  55. map_groups__init(&self->kmaps);
  56. if (perf_session__create_kernel_maps(self, conf) < 0)
  57. goto out_delete;
  58. if (mode == O_RDONLY && perf_session__open(self, force) < 0)
  59. goto out_delete;
  60. out:
  61. return self;
  62. out_free:
  63. free(self);
  64. return NULL;
  65. out_delete:
  66. perf_session__delete(self);
  67. return NULL;
  68. }
  69. void perf_session__delete(struct perf_session *self)
  70. {
  71. perf_header__exit(&self->header);
  72. close(self->fd);
  73. free(self->cwd);
  74. free(self);
  75. }