From: Arnaldo Carvalho de Melo <[email protected]>
And also don't call the constructor in it, this way it adheres to the
model the other methods follow.
Cc: Frédéric Weisbecker <[email protected]>
Cc: Mike Galbraith <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/builtin-record.c | 16 +++++++-----
tools/perf/builtin-timechart.c | 17 ++++++++++---
tools/perf/util/data_map.c | 18 ++++++++++----
tools/perf/util/header.c | 52 +++++++++++++++++++++++----------------
tools/perf/util/header.h | 7 +++--
5 files changed, 70 insertions(+), 40 deletions(-)
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 82260c5..c97cb2c 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -400,7 +400,7 @@ static int __cmd_record(int argc, const char **argv)
struct stat st;
pid_t pid = 0;
int flags;
- int ret;
+ int err;
unsigned long waking = 0;
page_size = sysconf(_SC_PAGE_SIZE);
@@ -434,16 +434,18 @@ static int __cmd_record(int argc, const char **argv)
exit(-1);
}
- if (!file_new)
- header = perf_header__read(output);
- else
- header = perf_header__new();
-
+ header = perf_header__new();
if (header == NULL) {
pr_err("Not enough memory for reading perf file header\n");
return -1;
}
+ if (!file_new) {
+ err = perf_header__read(header, output);
+ if (err < 0)
+ return err;
+ }
+
if (raw_samples) {
perf_header__set_feat(header, HEADER_TRACE_INFO);
} else {
@@ -527,7 +529,7 @@ static int __cmd_record(int argc, const char **argv)
if (hits == samples) {
if (done)
break;
- ret = poll(event_array, nr_poll, -1);
+ err = poll(event_array, nr_poll, -1);
waking++;
}
diff --git a/tools/perf/builtin-timechart.c b/tools/perf/builtin-timechart.c
index 665877e..dd4d82a 100644
--- a/tools/perf/builtin-timechart.c
+++ b/tools/perf/builtin-timechart.c
@@ -1093,7 +1093,7 @@ static void process_samples(void)
static int __cmd_timechart(void)
{
- int ret, rc = EXIT_FAILURE;
+ int err, rc = EXIT_FAILURE;
unsigned long offset = 0;
unsigned long head, shift;
struct stat statbuf;
@@ -1111,8 +1111,8 @@ static int __cmd_timechart(void)
exit(-1);
}
- ret = fstat(input, &statbuf);
- if (ret < 0) {
+ err = fstat(input, &statbuf);
+ if (err < 0) {
perror("failed to stat file");
exit(-1);
}
@@ -1122,7 +1122,16 @@ static int __cmd_timechart(void)
exit(0);
}
- header = perf_header__read(input);
+ header = perf_header__new();
+ if (header == NULL)
+ return -ENOMEM;
+
+ err = perf_header__read(header, input);
+ if (err < 0) {
+ perf_header__delete(header);
+ return err;
+ }
+
head = header->data_offset;
sample_type = perf_header__sample_type(header);
diff --git a/tools/perf/util/data_map.c b/tools/perf/util/data_map.c
index 14cb846..b8fc0fa 100644
--- a/tools/perf/util/data_map.c
+++ b/tools/perf/util/data_map.c
@@ -106,7 +106,7 @@ int mmap_dispatch_perf_file(struct perf_header **pheader,
int *cwdlen,
char **cwd)
{
- int ret, rc = EXIT_FAILURE;
+ int err, rc = EXIT_FAILURE;
struct perf_header *header;
unsigned long head, shift;
unsigned long offset = 0;
@@ -132,8 +132,8 @@ int mmap_dispatch_perf_file(struct perf_header **pheader,
exit(-1);
}
- ret = fstat(input, &input_stat);
- if (ret < 0) {
+ err = fstat(input, &input_stat);
+ if (err < 0) {
perror("failed to stat file");
exit(-1);
}
@@ -149,8 +149,16 @@ int mmap_dispatch_perf_file(struct perf_header **pheader,
exit(0);
}
- *pheader = perf_header__read(input);
- header = *pheader;
+ header = perf_header__new();
+ if (header == NULL)
+ return -ENOMEM;
+
+ err = perf_header__read(header, input);
+ if (err < 0) {
+ perf_header__delete(header);
+ return err;
+ }
+ *pheader = header;
head = header->data_offset;
sample_type = perf_header__sample_type(header);
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 425a29b..e66c7bd 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -78,16 +78,24 @@ struct perf_header *perf_header__new(void)
return self;
}
+void perf_header__delete(struct perf_header *self)
+{
+ int i;
+
+ for (i = 0; i < self->attrs; ++i)
+ perf_header_attr__delete(self->attr[i]);
+
+ free(self->attr);
+ free(self);
+}
+
int perf_header__add_attr(struct perf_header *self,
struct perf_header_attr *attr)
{
- int pos = self->attrs;
-
if (self->frozen)
return -1;
- self->attrs++;
- if (self->attrs > self->size) {
+ if (self->attrs == self->size) {
int nsize = self->size * 2;
struct perf_header_attr **nattr;
@@ -98,7 +106,8 @@ int perf_header__add_attr(struct perf_header *self,
self->size = nsize;
self->attr = nattr;
}
- self->attr[pos] = attr;
+
+ self->attr[self->attrs++] = attr;
return 0;
}
@@ -441,19 +450,17 @@ static int perf_file_section__process(struct perf_file_section *self,
return 0;
}
-struct perf_header *perf_header__read(int fd)
+int perf_header__read(struct perf_header *self, int fd)
{
- struct perf_header *self = perf_header__new();
struct perf_file_header f_header;
struct perf_file_attr f_attr;
u64 f_id;
int nr_attrs, nr_ids, i, j;
- if (self == NULL)
- die("nomem");
-
- if (perf_file_header__read(&f_header, self, fd) < 0)
- die("incompatible file format");
+ if (perf_file_header__read(&f_header, self, fd) < 0) {
+ pr_debug("incompatible file format\n");
+ return -EINVAL;
+ }
nr_attrs = f_header.attrs.size / sizeof(f_attr);
lseek(fd, f_header.attrs.offset, SEEK_SET);
@@ -467,7 +474,7 @@ struct perf_header *perf_header__read(int fd)
attr = perf_header_attr__new(&f_attr.attr);
if (attr == NULL)
- die("nomem");
+ return -ENOMEM;
nr_ids = f_attr.ids.size / sizeof(u64);
lseek(fd, f_attr.ids.offset, SEEK_SET);
@@ -475,11 +482,15 @@ struct perf_header *perf_header__read(int fd)
for (j = 0; j < nr_ids; j++) {
do_read(fd, &f_id, sizeof(f_id));
- if (perf_header_attr__add_id(attr, f_id) < 0)
- die("nomem");
+ if (perf_header_attr__add_id(attr, f_id) < 0) {
+ perf_header_attr__delete(attr);
+ return -ENOMEM;
+ }
+ }
+ if (perf_header__add_attr(self, attr) < 0) {
+ perf_header_attr__delete(attr);
+ return -ENOMEM;
}
- if (perf_header__add_attr(self, attr) < 0)
- die("nomem");
lseek(fd, tmp, SEEK_SET);
}
@@ -487,8 +498,8 @@ struct perf_header *perf_header__read(int fd)
if (f_header.event_types.size) {
lseek(fd, f_header.event_types.offset, SEEK_SET);
events = malloc(f_header.event_types.size);
- if (!events)
- die("nomem");
+ if (events == NULL)
+ return -ENOMEM;
do_read(fd, events, f_header.event_types.size);
event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
}
@@ -498,8 +509,7 @@ struct perf_header *perf_header__read(int fd)
lseek(fd, self->data_offset, SEEK_SET);
self->frozen = 1;
-
- return self;
+ return 0;
}
u64 perf_header__sample_type(struct perf_header *header)
diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h
index f46a94e..dc8fedb 100644
--- a/tools/perf/util/header.h
+++ b/tools/perf/util/header.h
@@ -55,7 +55,10 @@ struct perf_header {
DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS);
};
-struct perf_header *perf_header__read(int fd);
+struct perf_header *perf_header__new(void);
+void perf_header__delete(struct perf_header *self);
+
+int perf_header__read(struct perf_header *self, int fd);
void perf_header__write(struct perf_header *self, int fd, bool at_exit);
int perf_header__add_attr(struct perf_header *self,
@@ -75,8 +78,6 @@ perf_header__find_attr(u64 id, struct perf_header *header);
void perf_header__set_feat(struct perf_header *self, int feat);
bool perf_header__has_feat(const struct perf_header *self, int feat);
-struct perf_header *perf_header__new(void);
-
int perf_header__process_sections(struct perf_header *self, int fd,
int (*process)(struct perf_file_section *self,
int feat, int fd));
--
1.6.2.5
From: Arnaldo Carvalho de Melo <[email protected]>
This time in perf_header__adds_write, propagating the do_write error
returns.
Cc: Frédéric Weisbecker <[email protected]>
Cc: Mike Galbraith <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/builtin-record.c | 7 +++-
tools/perf/util/header.c | 80 ++++++++++++++++++++++++++++--------------
tools/perf/util/header.h | 2 +-
3 files changed, 59 insertions(+), 30 deletions(-)
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index c97cb2c..87f98fd 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -474,8 +474,11 @@ static int __cmd_record(int argc, const char **argv)
}
}
- if (file_new)
- perf_header__write(header, output, false);
+ if (file_new) {
+ err = perf_header__write(header, output, false);
+ if (err < 0)
+ return err;
+ }
if (!system_wide)
event__synthesize_thread(pid, process_synthesized_event);
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index e66c7bd..d5c81eb 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -176,7 +176,7 @@ static int do_write(int fd, const void *buf, size_t size)
int ret = write(fd, buf, size);
if (ret < 0)
- return -1;
+ return -errno;
size -= ret;
buf += ret;
@@ -190,6 +190,7 @@ static int dsos__write_buildid_table(int fd)
struct dso *pos;
list_for_each_entry(pos, &dsos, node) {
+ int err;
struct build_id_event b;
size_t len;
@@ -200,33 +201,35 @@ static int dsos__write_buildid_table(int fd)
memset(&b, 0, sizeof(b));
memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
b.header.size = sizeof(b) + len;
- if (do_write(fd, &b, sizeof(b)) < 0 ||
- do_write(fd, pos->long_name, len) < 0)
- return -1;
+ err = do_write(fd, &b, sizeof(b));
+ if (err < 0)
+ return err;
+ err = do_write(fd, pos->long_name, len);
+ if (err < 0)
+ return err;
}
return 0;
}
-static void
-perf_header__adds_write(struct perf_header *self, int fd)
+static int perf_header__adds_write(struct perf_header *self, int fd)
{
int nr_sections;
struct perf_file_section *feat_sec;
int sec_size;
u64 sec_start;
- int idx = 0;
+ int idx = 0, err;
if (dsos__read_build_ids())
perf_header__set_feat(self, HEADER_BUILD_ID);
nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
if (!nr_sections)
- return;
+ return 0;
feat_sec = calloc(sizeof(*feat_sec), nr_sections);
- if (!feat_sec)
- die("No memory");
+ if (feat_sec == NULL)
+ return -ENOMEM;
sec_size = sizeof(*feat_sec) * nr_sections;
@@ -258,23 +261,29 @@ perf_header__adds_write(struct perf_header *self, int fd)
/* Write build-ids */
buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
- if (dsos__write_buildid_table(fd) < 0)
- die("failed to write buildid table");
+ err = dsos__write_buildid_table(fd);
+ if (err < 0) {
+ pr_debug("failed to write buildid table\n");
+ goto out_free;
+ }
buildid_sec->size = lseek(fd, 0, SEEK_CUR) - buildid_sec->offset;
}
lseek(fd, sec_start, SEEK_SET);
- if (do_write(fd, feat_sec, sec_size) < 0)
- die("failed to write feature section");
+ err = do_write(fd, feat_sec, sec_size);
+ if (err < 0)
+ pr_debug("failed to write feature section\n");
+out_free:
free(feat_sec);
+ return err;
}
-void perf_header__write(struct perf_header *self, int fd, bool at_exit)
+int perf_header__write(struct perf_header *self, int fd, bool at_exit)
{
struct perf_file_header f_header;
struct perf_file_attr f_attr;
struct perf_header_attr *attr;
- int i;
+ int i, err;
lseek(fd, sizeof(f_header), SEEK_SET);
@@ -283,8 +292,11 @@ void perf_header__write(struct perf_header *self, int fd, bool at_exit)
attr = self->attr[i];
attr->id_offset = lseek(fd, 0, SEEK_CUR);
- if (do_write(fd, attr->id, attr->ids * sizeof(u64)) < 0)
- die("failed to write perf header");
+ err = do_write(fd, attr->id, attr->ids * sizeof(u64));
+ if (err < 0) {
+ pr_debug("failed to write perf header\n");
+ return err;
+ }
}
@@ -300,20 +312,30 @@ void perf_header__write(struct perf_header *self, int fd, bool at_exit)
.size = attr->ids * sizeof(u64),
}
};
- if (do_write(fd, &f_attr, sizeof(f_attr)) < 0)
- die("failed to write perf header attribute");
+ err = do_write(fd, &f_attr, sizeof(f_attr));
+ if (err < 0) {
+ pr_debug("failed to write perf header attribute\n");
+ return err;
+ }
}
self->event_offset = lseek(fd, 0, SEEK_CUR);
self->event_size = event_count * sizeof(struct perf_trace_event_type);
- if (events)
- if (do_write(fd, events, self->event_size) < 0)
- die("failed to write perf header events");
+ if (events) {
+ err = do_write(fd, events, self->event_size);
+ if (err < 0) {
+ pr_debug("failed to write perf header events\n");
+ return err;
+ }
+ }
self->data_offset = lseek(fd, 0, SEEK_CUR);
- if (at_exit)
- perf_header__adds_write(self, fd);
+ if (at_exit) {
+ err = perf_header__adds_write(self, fd);
+ if (err < 0)
+ return err;
+ }
f_header = (struct perf_file_header){
.magic = PERF_MAGIC,
@@ -336,11 +358,15 @@ void perf_header__write(struct perf_header *self, int fd, bool at_exit)
memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features));
lseek(fd, 0, SEEK_SET);
- if (do_write(fd, &f_header, sizeof(f_header)) < 0)
- die("failed to write perf header");
+ err = do_write(fd, &f_header, sizeof(f_header));
+ if (err < 0) {
+ pr_debug("failed to write perf header\n");
+ return err;
+ }
lseek(fd, self->data_offset + self->data_size, SEEK_SET);
self->frozen = 1;
+ return 0;
}
static void do_read(int fd, void *buf, size_t size)
diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h
index dc8fedb..d1dbe2b 100644
--- a/tools/perf/util/header.h
+++ b/tools/perf/util/header.h
@@ -59,7 +59,7 @@ struct perf_header *perf_header__new(void);
void perf_header__delete(struct perf_header *self);
int perf_header__read(struct perf_header *self, int fd);
-void perf_header__write(struct perf_header *self, int fd, bool at_exit);
+int perf_header__write(struct perf_header *self, int fd, bool at_exit);
int perf_header__add_attr(struct perf_header *self,
struct perf_header_attr *attr);
--
1.6.2.5
From: Arnaldo Carvalho de Melo <[email protected]>
Propagate the error, that, interestingly, are already handled by all
callers :-)
Cc: Frédéric Weisbecker <[email protected]>
Cc: Mike Galbraith <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Paul Mackerras <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/data_map.c | 80 ++++++++++++++++++++++++--------------------
1 files changed, 44 insertions(+), 36 deletions(-)
diff --git a/tools/perf/util/data_map.c b/tools/perf/util/data_map.c
index b8fc0fa..5543e7d 100644
--- a/tools/perf/util/data_map.c
+++ b/tools/perf/util/data_map.c
@@ -106,7 +106,7 @@ int mmap_dispatch_perf_file(struct perf_header **pheader,
int *cwdlen,
char **cwd)
{
- int err, rc = EXIT_FAILURE;
+ int err;
struct perf_header *header;
unsigned long head, shift;
unsigned long offset = 0;
@@ -118,64 +118,69 @@ int mmap_dispatch_perf_file(struct perf_header **pheader,
int input;
char *buf;
- if (!curr_handler)
- die("Forgot to register perf file handler");
+ if (curr_handler == NULL) {
+ pr_debug("Forgot to register perf file handler\n");
+ return -EINVAL;
+ }
page_size = getpagesize();
input = open(input_name, O_RDONLY);
if (input < 0) {
- fprintf(stderr, " failed to open file: %s", input_name);
+ pr_err("Failed to open file: %s", input_name);
if (!strcmp(input_name, "perf.data"))
- fprintf(stderr, " (try 'perf record' first)");
- fprintf(stderr, "\n");
- exit(-1);
+ pr_err(" (try 'perf record' first)");
+ pr_err("\n");
+ return -errno;
}
- err = fstat(input, &input_stat);
- if (err < 0) {
- perror("failed to stat file");
- exit(-1);
+ if (fstat(input, &input_stat) < 0) {
+ pr_err("failed to stat file");
+ err = -errno;
+ goto out_close;
}
+ err = -EACCES;
if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
- fprintf(stderr, "file: %s not owned by current user or root\n",
+ pr_err("file: %s not owned by current user or root\n",
input_name);
- exit(-1);
+ goto out_close;
}
- if (!input_stat.st_size) {
- fprintf(stderr, "zero-sized file, nothing to do!\n");
- exit(0);
+ if (input_stat.st_size == 0) {
+ pr_info("zero-sized file, nothing to do!\n");
+ goto done;
}
+ err = -ENOMEM;
header = perf_header__new();
if (header == NULL)
- return -ENOMEM;
+ goto out_close;
err = perf_header__read(header, input);
- if (err < 0) {
- perf_header__delete(header);
- return err;
- }
+ if (err < 0)
+ goto out_delete;
*pheader = header;
head = header->data_offset;
sample_type = perf_header__sample_type(header);
- if (curr_handler->sample_type_check)
- if (curr_handler->sample_type_check(sample_type) < 0)
- exit(-1);
+ err = -EINVAL;
+ if (curr_handler->sample_type_check &&
+ curr_handler->sample_type_check(sample_type) < 0)
+ goto out_delete;
+ err = -ENOMEM;
if (load_kernel(NULL) < 0) {
- perror("failed to load kernel symbols");
- return EXIT_FAILURE;
+ pr_err("failed to load kernel symbols\n");
+ goto out_delete;
}
if (!full_paths) {
if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
- perror("failed to get the current directory");
- return EXIT_FAILURE;
+ pr_err("failed to get the current directory\n");
+ err = -errno;
+ goto out_delete;
}
*cwd = __cwd;
*cwdlen = strlen(*cwd);
@@ -189,11 +194,12 @@ int mmap_dispatch_perf_file(struct perf_header **pheader,
head -= shift;
remap:
- buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
- MAP_SHARED, input, offset);
+ buf = mmap(NULL, page_size * mmap_window, PROT_READ,
+ MAP_SHARED, input, offset);
if (buf == MAP_FAILED) {
- perror("failed to mmap file");
- exit(-1);
+ pr_err("failed to mmap file\n");
+ err = -errno;
+ goto out_delete;
}
more:
@@ -250,10 +256,12 @@ more:
goto more;
done:
- rc = EXIT_SUCCESS;
+ err = 0;
+out_close:
close(input);
- return rc;
+ return err;
+out_delete:
+ perf_header__delete(header);
+ goto out_close;
}
-
-
--
1.6.2.5