forked from Samsung/CAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetrace
executable file
·113 lines (93 loc) · 2.38 KB
/
etrace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
# Run syscalls tracing of a given command
# Usage:
# etrace COMMAND ...
print_usage () {
echo "Usage: etrace [-h] [-l] [-a] [-w WORK_DIR] [--] COMMAND ..."
}
echo_err () {
echo "[!] $1"
}
if [ "$#" -lt 1 ]; then
print_usage
exit 1
fi
# -------- parse args
SAVE_KERNEL_LOG=
WORK_DIR=$(pwd)
INITCWD=$(pwd)
MODULE_PARAMS="ignore_repeated_opens=1"
while getopts "ahlw:" opt; do
case "$opt" in
h)
print_usage
exit 0
;;
l)
SAVE_KERNEL_LOG=y
;;
w)
WORK_DIR="$OPTARG"
mkdir -p "$WORK_DIR"
;;
a)
MODULE_PARAMS=""
;;
esac
done
# Remove script's arguments so that "$@" contains the command to run
# under tracer
shift $((OPTIND-1))
# If arguments were separated from the command by "--", remove it too
[ "${1:-}" = "--" ] && shift
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
NPROC=$(nproc)
NPROC=$(expr $NPROC - 1)
# -------- setting up ftrace
sudo -n setup_etrace.sh -f
if [ "$?" -ne 0 ]; then
echo_err "setup_etrace.sh -f failed"
exit 1
fi
# -------- save our CWD
echo "INITCWD=$INITCWD" > "$WORK_DIR/.nfsdb"
# -------- set up event listener
#${DIR}/etrace_cat "/sys/kernel/debug/tracing/trace_pipe" > "$WORK_DIR/.nfsdb" &
${DIR}/cati "/sys/kernel/debug/tracing/trace_pipe" >> "$WORK_DIR/.nfsdb" &
LISTENER_PID="$!"
sudo -n renice -n -18 -p $LISTENER_PID
# -------- save kernel log
if [ "$SAVE_KERNEL_LOG" = "y" ]; then
dmesg -w > "$WORK_DIR/.nfsdb.klog" &
KLOGGER_PID="$!"
fi
# -------- install execve_trace module
MYPID="$$"
sudo -n setup_etrace.sh -i $MYPID $MODULE_PARAMS
if [ "$?" -ne 0 ]; then
echo_err "setup_etrace.sh -i failed"
exit 1
fi
# -------- run the command
"$@"
RV="$?"
# -------- remove execve_trace module
sudo -n setup_etrace.sh -r
if [ "$?" -ne 0 ]; then
echo_err "setup_etrace.sh -r failed"
exit 1
fi
# Save stat information to see if any events have been missing
rm -f "$WORK_DIR/.nfsdb.stats" && touch "$WORK_DIR/.nfsdb.stats"
for i in `seq 0 $NPROC`; do
echo "##---------- CPU $i ---------- ##" >> "$WORK_DIR/.nfsdb.stats"
cat "/sys/kernel/debug/tracing/per_cpu/cpu$i/stats" >> "$WORK_DIR/.nfsdb.stats"
done
# -------- stop event listener
kill -SIGINT "${LISTENER_PID}"
# -------- stop kernel logger
if [ "$SAVE_KERNEL_LOG" = "y" ]; then
kill -SIGINT "${KLOGGER_PID}"
fi
# We're done here
exit $RV