Skip to content

Instantly share code, notes, and snippets.

@b1tninja
Last active April 23, 2019 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b1tninja/445b480b6b4e0c910b3a9180e01ac8fc to your computer and use it in GitHub Desktop.
Save b1tninja/445b480b6b4e0c910b3a9180e01ac8fc to your computer and use it in GitHub Desktop.
powertop segfault
commit 318dcc2daf1bf6f297eed022699c39354eab4699
gpg: Signature made Tue 23 Apr 2019 06:09:08 AM PDT
gpg: using RSA key F6CC1F4DF325EACBBE2532481F299543498470BA
gpg: issuer "justincapella@gmail.com"
gpg: Good signature from "Justin Capella <justincapella@gmail.com>" [ultimate]
Author: b1tninja <devnull@localhost>
Date: Tue Apr 23 06:09:08 2019 -0700
Check that perf event is properly allocated, combine constructor
diff --git a/src/perf/perf.cpp b/src/perf/perf.cpp
index 9ed0ba8..c9af036 100644
--- a/src/perf/perf.cpp
+++ b/src/perf/perf.cpp
@@ -138,7 +138,8 @@ void perf_event::create_perf_event(char *eventname, int _cpu)
ret = ioctl(perf_fd, PERF_EVENT_IOC_ENABLE, 0);
if (ret < 0) {
- fprintf(stderr, "failed to enable perf \n");
+ fprintf(stderr, "failed to enable perf\n");
+ return;
}
pc = (perf_event_mmap_page *)perf_mmap;
@@ -192,13 +193,9 @@ static void allocate_pevent(void)
perf_event::perf_event(const char *event_name, int _cpu, int buffer_size)
{
- allocate_pevent();
- name = NULL;
- perf_fd = -1;
+ perf_event();
bufsize = buffer_size;
cpu = _cpu;
- perf_mmap = NULL;
- trace_type = 0;
set_event_name(event_name);
}
@@ -208,7 +205,9 @@ perf_event::perf_event(void)
name = NULL;
perf_fd = -1;
bufsize = 128;
- perf_mmap = NULL;
+ perf_mmap = MAP_FAILED;
+ data_mmap = MAP_FAILED;
+ pc = NULL;
cpu = 0;
trace_type = 0;
}
@@ -230,7 +229,7 @@ void perf_event::process(void *cookie)
{
struct perf_event_header *header;
- if (perf_fd < 0)
+ if (perf_fd < 0 || MAP_FAILED == perf_mmap || NULL == pc)
return;
while (pc->data_tail != pc->data_head ) {
@@ -255,10 +254,11 @@ void perf_event::process(void *cookie)
void perf_event::clear(void)
{
- if (perf_mmap) {
+ pc = NULL;
+ if (MAP_FAILED != perf_mmap) {
// memset(perf_mmap, 0, (bufsize)*getpagesize());
munmap(perf_mmap, (bufsize+1)*getpagesize());
- perf_mmap = NULL;
+ perf_mmap = MAP_FAILED;
}
if (perf_fd != -1)
close(perf_fd);
@b1tninja
Copy link
Author

For debugging reasons, figured it best to just check the other variables are not set-- instead of relying on the fd being negative by clearing many... may help with debugging, may reveal other locations making false assumptions based around perf_fd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment