2 # SPDX-License-Identifier: GPL-2.0-only
3 # perf-with-kcore: use perf with a copy of kcore
4 # Copyright (c) 2014, Intel Corporation.
11 echo "Usage: perf-with-kcore <perf sub-command> <perf.data directory> [<sub-command options> [ -- <workload>]]" >&2
12 echo " <perf sub-command> can be record, script, report or inject" >&2
13 echo " or: perf-with-kcore fix_buildid_cache_permissions" >&2
19 if [ -n "$PERF" ] ; then
22 PERF
=`which perf || true`
23 if [ -z "$PERF" ] ; then
24 echo "Failed to find perf" >&2
27 if [ ! -x "$PERF" ] ; then
28 echo "Failed to find perf" >&2
39 if [ $EUID -eq 0 ] ; then
46 ("$PERF" record
-o perf.data.junk
"${PERF_OPTIONS[@]}" -- sleep 60) >/dev
/null
2>/dev
/null
&
49 # Need to make sure that perf has started
52 KCORE
=$
(($SUDO "$PERF" buildid-cache
-v -f -k /proc
/kcore
>/dev
/null
) 2>&1)
54 "kcore added to build-id cache directory "*)
55 KCORE_DIR
=${KCORE#"kcore added to build-id cache directory "}
59 wait >/dev
/null
2>/dev
/null || true
62 echo "Failed to find kcore" >&2
68 wait >/dev
/null
2>/dev
/null || true
71 $SUDO cp -a "$KCORE_DIR" "$(pwd)/$PERF_DATA_DIR"
72 $SUDO rm -f "$KCORE_DIR/kcore"
73 $SUDO rm -f "$KCORE_DIR/kallsyms"
74 $SUDO rm -f "$KCORE_DIR/modules"
75 $SUDO rmdir "$KCORE_DIR"
77 KCORE_DIR_BASENAME
=$
(basename "$KCORE_DIR")
78 KCORE_DIR
="$(pwd)/$PERF_DATA_DIR/$KCORE_DIR_BASENAME"
80 $SUDO chown
$UID "$KCORE_DIR"
81 $SUDO chown
$UID "$KCORE_DIR/kcore"
82 $SUDO chown
$UID "$KCORE_DIR/kallsyms"
83 $SUDO chown
$UID "$KCORE_DIR/modules"
85 $SUDO chgrp
$GROUPS "$KCORE_DIR"
86 $SUDO chgrp
$GROUPS "$KCORE_DIR/kcore"
87 $SUDO chgrp
$GROUPS "$KCORE_DIR/kallsyms"
88 $SUDO chgrp
$GROUPS "$KCORE_DIR/modules"
90 ln -s "$KCORE_DIR_BASENAME" "$PERF_DATA_DIR/kcore_dir"
93 fix_buildid_cache_permissions
()
95 if [ $EUID -ne 0 ] ; then
96 echo "This script must be run as root via sudo " >&2
100 if [ -z "$SUDO_USER" ] ; then
101 echo "This script must be run via sudo" >&2
105 USER_HOME
=$
(bash
<<< "echo ~$SUDO_USER")
107 echo "Fixing buildid cache permissions"
109 find "$USER_HOME/.debug" -xdev -type d
! -user "$SUDO_USER" -ls -exec chown
"$SUDO_USER" \
{\
} \
;
110 find "$USER_HOME/.debug" -xdev -type f
-links 1 ! -user "$SUDO_USER" -ls -exec chown
"$SUDO_USER" \
{\
} \
;
111 find "$USER_HOME/.debug" -xdev -type l
! -user "$SUDO_USER" -ls -exec chown
-h "$SUDO_USER" \
{\
} \
;
113 if [ -n "$SUDO_GID" ] ; then
114 find "$USER_HOME/.debug" -xdev -type d
! -group "$SUDO_GID" -ls -exec chgrp
"$SUDO_GID" \
{\
} \
;
115 find "$USER_HOME/.debug" -xdev -type f
-links 1 ! -group "$SUDO_GID" -ls -exec chgrp
"$SUDO_GID" \
{\
} \
;
116 find "$USER_HOME/.debug" -xdev -type l
! -group "$SUDO_GID" -ls -exec chgrp
-h "$SUDO_GID" \
{\
} \
;
122 check_buildid_cache_permissions
()
124 if [ $EUID -eq 0 ] ; then
128 PERMISSIONS_OK
+=$
(find "$HOME/.debug" -xdev -type d
! -user "$USER" -print -quit)
129 PERMISSIONS_OK
+=$
(find "$HOME/.debug" -xdev -type f
-links 1 ! -user "$USER" -print -quit)
130 PERMISSIONS_OK
+=$
(find "$HOME/.debug" -xdev -type l
! -user "$USER" -print -quit)
132 PERMISSIONS_OK
+=$
(find "$HOME/.debug" -xdev -type d
! -group "$GROUPS" -print -quit)
133 PERMISSIONS_OK
+=$
(find "$HOME/.debug" -xdev -type f
-links 1 ! -group "$GROUPS" -print -quit)
134 PERMISSIONS_OK
+=$
(find "$HOME/.debug" -xdev -type l
! -group "$GROUPS" -print -quit)
136 if [ -n "$PERMISSIONS_OK" ] ; then
137 echo "*** WARNING *** buildid cache permissions may need fixing" >&2
145 if [ $EUID -ne 0 ] ; then
147 if [ "$(cat /proc/sys/kernel/kptr_restrict)" -ne 0 ] ; then
148 echo "*** WARNING *** /proc/sys/kernel/kptr_restrict prevents access to kernel addresses" >&2
151 if echo "${PERF_OPTIONS[@]}" |
grep -q ' -a \|^-a \| -a$\|^-a$\| --all-cpus \|^--all-cpus \| --all-cpus$\|^--all-cpus$' ; then
152 echo "*** WARNING *** system-wide tracing without root access will not be able to read all necessary information from /proc" >&2
155 if echo "${PERF_OPTIONS[@]}" |
grep -q 'intel_pt\|intel_bts\| -I\|^-I' ; then
156 if [ "$(cat /proc/sys/kernel/perf_event_paranoid)" -gt -1 ] ; then
157 echo "*** WARNING *** /proc/sys/kernel/perf_event_paranoid restricts buffer size and tracepoint (sched_switch) use" >&2
160 if echo "${PERF_OPTIONS[@]}" |
grep -q ' --per-thread \|^--per-thread \| --per-thread$\|^--per-thread$' ; then
162 elif echo "${PERF_OPTIONS[@]}" |
grep -q ' -t \|^-t \| -t$\|^-t$' ; then
164 elif [ ! -r /sys
/kernel
/debug
-o ! -x /sys
/kernel
/debug
] ; then
165 echo "*** WARNING *** /sys/kernel/debug permissions prevent tracepoint (sched_switch) use" >&2
170 if [ -z "$1" ] ; then
171 echo "Workload is required for recording" >&2
175 if [ -e "$PERF_DATA_DIR" ] ; then
176 echo "'$PERF_DATA_DIR' exists" >&2
182 mkdir
"$PERF_DATA_DIR"
184 echo "$PERF record -o $PERF_DATA_DIR/perf.data ${PERF_OPTIONS[@]} -- $@"
185 "$PERF" record
-o "$PERF_DATA_DIR/perf.data" "${PERF_OPTIONS[@]}" -- "$@" || true
187 if rmdir "$PERF_DATA_DIR" > /dev
/null
2>/dev
/null
; then
199 check_buildid_cache_permissions
200 echo "$PERF $PERF_SUB_COMMAND -i $PERF_DATA_DIR/perf.data --kallsyms=$PERF_DATA_DIR/kcore_dir/kallsyms $@"
201 "$PERF" $PERF_SUB_COMMAND -i "$PERF_DATA_DIR/perf.data" "--kallsyms=$PERF_DATA_DIR/kcore_dir/kallsyms" "$@"
204 if [ "$1" = "fix_buildid_cache_permissions" ] ; then
205 fix_buildid_cache_permissions
214 if [ -z "$PERF_SUB_COMMAND" ] ; then
218 if [ -z "$PERF_DATA_DIR" ] ; then
222 case "$PERF_SUB_COMMAND" in
224 while [ "$1" != "--" ] ; do
228 if [ "$1" != "--" ] ; then
229 echo "Options and workload are required for recording" >&2