Skip to content

Commit aa6bac0

Browse files
committed
Build correctly on old kernels without precise_ip support
Update Makefile to autodetect kernel features, such as precise_ip. Also, try to autodetect which header file to use for perf_event.
1 parent 14e6a82 commit aa6bac0

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

Makefile

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,28 @@ CC=gcc
22
LD=gcc
33
KERNEL_RELEASE=$(shell uname -r)
44
KERNEL_SRC=/lib/modules/$(KERNEL_RELEASE)/source
5-
CFLAGS=-g -Wall -std=gnu99 -DPERFH=\"$(KERNEL_SRC)/include/linux/perf_event.h\"
5+
6+
# Check if perf_event there is a perf_event.h available in the source
7+
# of the running kernel. In that case, assume that it's newer and use
8+
# that.
9+
ifneq ($(wildcard $(KERNEL_SRC)/include/linux/perf_event.h),)
10+
PERF_EVENT_H=$(KERNEL_SRC)/include/linux/perf_event.h
11+
CONFIG+=-DPERFH=\"$(PERF_EVENT_H)\"
12+
else
13+
PERF_EVENT_H=/usr/include/linux/perf_event.h
14+
endif
15+
16+
ifeq ($(wildcard $(PERF_EVENT_H)),)
17+
$(error "Can't find a usable perf_event header file")
18+
endif
19+
20+
# Check if the header file has support for the precise_ip option. This
21+
# is not the case for old header files.
22+
ifneq ($(shell grep '^[[:space:]]*precise_ip' $(PERF_EVENT_H) 2>/dev/null),)
23+
CONFIG+=-DHAVE_PRECISE_IP
24+
endif
25+
26+
CFLAGS=-g -Wall -std=gnu99 $(CONFIG)
627

728

829
all: rawperf perfrecord perfdump

perf_argp.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ struct perf_event_attr perf_base_attr = {
9999
.enable_on_exec = 0,
100100
.task = 0,
101101
.watermark = 0,
102+
#ifdef HAVE_PRECISE_IP
102103
.precise_ip = 0,
104+
#endif
103105
};
104106

105107
ctr_list_t perf_ctrs = { NULL, NULL };
@@ -109,7 +111,9 @@ enum {
109111
KEY_EXCLUSIVE = -2,
110112
KEY_SAMPLE_PERIOD = -3,
111113
KEY_SAMPLE_FREQ = -4,
114+
#ifdef HAVE_PRECISE_IP
112115
KEY_PRECISE_IP = -5,
116+
#endif
113117
};
114118

115119
static struct argp_option options[] = {
@@ -124,7 +128,9 @@ static struct argp_option options[] = {
124128
"Use raw performance event EVENT (creates a new event)", 0 },
125129
{ "sample-period", KEY_SAMPLE_PERIOD, "N", 0, "Use sample period N", 2 },
126130
{ "sample-freq", KEY_SAMPLE_FREQ, "N", 0, "Use sample frequency N", 2 },
131+
#ifdef HAVE_PRECISE_IP
127132
{ "precise-ip", KEY_PRECISE_IP, "N", 0, "Set the precise_ip field to N", 2 },
133+
#endif
128134
{ 0 }
129135
};
130136

@@ -372,10 +378,12 @@ parse_opt(int key, char *arg, struct argp_state *state)
372378
current_attr->freq = 1;
373379
break;
374380

381+
#ifdef HAVE_PRECISE_IP
375382
case KEY_PRECISE_IP:
376383
current_attr->precise_ip =
377384
perf_argp_parse_long("precise ip", arg, state);
378385
break;
386+
#endif
379387

380388
case ARGP_KEY_END:
381389
if (!perf_ctrs.head) {

0 commit comments

Comments
 (0)