diff --git a/src/libperf.c b/src/libperf.c index f9e51b8..bf3e429 100644 --- a/src/libperf.c +++ b/src/libperf.c @@ -156,19 +156,19 @@ libperf_initialize(pid_t pid, int cpu) struct libperf_data *pd = malloc(sizeof(struct libperf_data)); - if (pd == NULL) - { - perror("malloc"); - exit(EXIT_FAILURE); - } + if (pd == NULL) { + goto error; + } - if (pid == -1) + if (pid == -1) { pid = gettid(); + } pd->group = -1; - for (i = 0; i < __LIBPERF_ARRAY_SIZE(pd->fds); i++) + for (i = 0; i < __LIBPERF_ARRAY_SIZE(pd->fds); i++) { pd->fds[i] = -1; + } pd->pid = pid; pd->cpu = cpu; @@ -178,11 +178,9 @@ libperf_initialize(pid_t pid, int cpu) struct perf_event_attr *attrs = malloc(nr_counters * sizeof(struct perf_event_attr)); - if(attrs == NULL) - { - perror("malloc"); - exit(EXIT_FAILURE); - } + if(attrs == NULL) { + goto error; + } memcpy(attrs, default_attrs, sizeof(default_attrs)); pd->attrs = attrs; @@ -192,29 +190,46 @@ libperf_initialize(pid_t pid, int cpu) open(logname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - assert(fd != -1); - pd->log = fdopen(fd, "a"); + if (fd == -1) { + goto error; + } - assert(pd->log != NULL); + pd->log = fdopen(fd, "a"); - for (i = 0; i < nr_counters; i++) - { - attrs[i].size = sizeof(struct perf_event_attr); - attrs[i].inherit = 1; /* default */ - attrs[i].disabled = 1; /* disable them now... */ - attrs[i].enable_on_exec = 0; - pd->fds[i] = sys_perf_event_open(&attrs[i], pid, cpu, -1, 0); - if (pd->fds[i] < 0) - { - fprintf(stderr, "At event %d/%d\n", i, nr_counters); - perror("sys_perf_event_open"); - exit(EXIT_FAILURE); + if (pd->log == NULL) { + goto error; } + for (i = 0; i < nr_counters; i++) { + attrs[i].size = sizeof(struct perf_event_attr); + attrs[i].inherit = 1; /* default */ + attrs[i].disabled = 1; /* disable them now... */ + attrs[i].enable_on_exec = 0; + pd->fds[i] = sys_perf_event_open(&attrs[i], pid, cpu, -1, 0); + if (pd->fds[i] < 0) { + goto close; } + } pd->wall_start = rdclock(); return pd; + +close: + close(fd); + fclose(pd->log); + for (i = 0; i < nr_counters; i++) { + close(pd->fds[i]); + } +error: + if (attrs) { + free(attrs); + attrs = NULL; + } + if (pd) { + free(pd); + pd = NULL; + } + return NULL; } /* thread safe */ @@ -319,6 +334,10 @@ libperf_unit_test(void *n) { struct libperf_data *pd = libperf_initialize(0, -1); + if (!pd) { + return -1; + } + char *x = malloc(1024 * 1024 * 1024L); unsigned long int i; diff --git a/src/libperf_example.c b/src/libperf_example.c index 6615436..416f17e 100644 --- a/src/libperf_example.c +++ b/src/libperf_example.c @@ -32,6 +32,10 @@ int main(int argc, char *argv[]) { struct libperf_data *pd = libperf_initialize(-1, -1); /* init lib */ + if (!pd) { + printf("Error initializing libperf\n"); + return -1; + } libperf_enablecounter(pd, LIBPERF_COUNT_HW_INSTRUCTIONS); /* enable HW counter */