-
Notifications
You must be signed in to change notification settings - Fork 77
/
sptcmd
executable file
·331 lines (303 loc) · 8.84 KB
/
sptcmd
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/bin/bash
# simple pt trace a command
usage() {
cat <<EOU
Simple PT processor branch trace. The complete system is traced
unless a comm filter is set, as long as cmd is executing.
The output files are in ptout.CPU and ptout.sideband
When cmd has options please use -- to separate them from the sptcmd options.
sptcmd [options] cmd
--comm COMM -c COMM Set comm filter for comm (only trace that command)
--no-kernel -K disable kernel trace (default on) (only use with --comm ...)
--no-user -U disable user trace (default on)
--msr-trace -t enable msr trace in side band
--disretc -R disable return compression
--reload -d Always reload driver
--dont-dump -N Don't dump data
--prefix -o PREFIX Dump to PREFIX.cpu and PREFIX.sideband (default ptout)
--enumerate -e enumerate all processes in sideband (default unless -c is set)
--force -f Force initialization even when PT is already active (dangerous)
--keep -k Keep kernel trace points running after end.
--enable -E Only enable tracing, but don't dump. cmd is optional
--dump -D Only dump already running trace (after sptcmd -E)
--disable Only disable already running trace. Can be combined with --dump.
--start-addr addr -s addr
Start trace on CPU when kernel symbol start is hit. Can be address
or sym+offset
--stop-addr addr -y addr
Stop trace on CPU when kernel symbol stop is hit. Can be address
or sym+offset
--print-regs Print PT register setup
Following flags are not supported on Broadwell:
--cyc thresh -C thresh Set cycle packet threshold to 2^(n-1) cycles for fine grained timing
--mtc freq -M freq Set MTC packet frequency to 2^(n-1)
--psb thresh -P thresh Set PSB packet threshold to 2K^n bytes
--filter -F start,end Only trace between virtual address start-end. Can be kernel symbols.
Two ranges allowed (shared with -S)
--stop-range -S start,end Stop trace when reaching virtual address start-end. Can be kernel
symbols. Two ranges allowed (shared with -F)
Feature flags supported only on some CPUs:
--disable-branch Disable branch tracing
--ptwrite Enable output of data from PTWRITE instructions.
--fup-on-ptw Always log instruction pointer of PTWRITE (useful with --disable-branch)
--power Enable power event tracing
EOU
exit 1
}
PATH=$PATH:$(dirname $0)
unset COMM KERNEL TRACE USERMODE DISRETC DRIVER DONTDUMP PREFIX ENUMALL
unset CYC_THRESH MTC_FREQ PSB_FREQ FILTER TSTOP START STOP FORCE KEEP
unset PRINT_REGS ENABLE_ONLY DUMP_ONLY DISABLE DISABLE_BRANCH PTWRITE FUP_ON_PTW
unset PWR_EVT
declare -a RANGE_START RANGE_END RANGE_CFG
NUM_RANGE=0
MAX_RANGE=2
ENUMDFL=1
declare -a ARGS
ARGS=($@)
resolvesym()
{
echo >&2 $1
case "$1" in
[0-9]*) echo -n $1 ;;
*)
# do arithmetic in bc because kernel addresses don't fit into awk double
awk -v sym=$1 '
BEGIN {
offset = 0
if (split(sym,a,"+") == 2) {
offset = strtonum(a[2])
sym = a[1]
}
}
$3 == sym {
printf "ibase=16 ; " toupper($1) " + %X\n", offset
}' /proc/kallsyms | bc
;;
esac
}
B=0
for ((j = 0; j < $#; j++)) ; do
if [ $B -ne 0 ] ; then
ARGS[$j]=${ARGS[$j]}
continue
fi
case "${ARGS[$j]}" in
--comm) ARGS[$j]=-c ;;
--no-kernel) ARGS[$j]=-K ;;
--no-user) ARGS[$j]=-U ;;
--msr-trace) ARGS[$j]=-t ;;
--disretc) ARGS[$j]=-R ;;
--reload) ARGS[$j]=-d ;;
--dont-dump) ARGS[$j]=-N ;;
--force) ARGS[$j]=-f ;;
--prefix) ARGS[$j]=-o ;;
--enum-all) ARGS[$j]=-e ;;
--keep) ARGS[$j]=-k ;;
--cyc) ARGS[$j]=-C ;;
--mtc) ARGS[$j]=-M ;;
--psb) ARGS[$j]=-P ;;
--filter) ARGS[$j]=-F ;;
--stop-range) ARGS[$j]=-S ;;
--start-addr) ARGS[$j]=-s ;;
--stop-addr) ARGS[$j]=-y ;;
--print-regs) ARGS[$j]="-@" ; PRINT_REGS=1 ;;
--enable) ARGS[$j]=-E ;;
--dump) ARGS[$j]=-D ;;
--disable) ARGS[$j]="-@" ; DISABLE=1 ;;
--ptwrite) ARGS[$j]="-@"; PTWRITE=1 ;;
--fup-on-ptw) ARGS[$j]="-@"; FUP_ON_PTW=1 ;;
--disable-branch) ARGS[$j]="-@"; DISABLE_BRANCH=1 ;;
--power) ARGS[$j]="-@"; PWR_EVT=1 ;;
--) ARGS[$j]=${ARGS[$j]}; B=1 ;;
--*) usage ; exit 1 ;;
*) ARGS[$j]="${ARGS[$j]}" ;;
esac
done
while getopts "c:KtURkfdNo:eC:M:P:F:S:s:y:@ED" opt ${ARGS[@]} ; do
case "$opt" in
c) COMM="$OPTARG" ; ENUMDFL=0 ;;
K) KERNEL=0 ;;
t) TRACE=1 ;;
U) USERMODE=0 ;;
R) DISRETC=1 ;;
d) DRIVER=1 ;;
N) DONTDUMP=1 ;;
D) DUMP_ONLY=1 ;;
f) FORCE=1 ;;
o) PREFIX="$OPTARG" ;;
e) ENUMALL=1 ;;
k) KEEP=1 ;;
E) ENABLE_ONLY=1;;
C) CYC_THRESH="$OPTARG"
ptfeature cyc_thresh $CYC_THRESH || usage
;;
M) MTC_FREQ="$OPTARG"
ptfeature mtc_freq $MTC_FREQ || usage
;;
P) PSB_FREQ="$OPTARG"
ptfeature psb_freq $PSB_FREQ || usage
;;
F) IFS=, read st end <<< "$OPTARG"
RANGE_START[$NUM_RANGE]=`resolvesym $st`
RANGE_END[$NUM_RANGE]=`resolvesym $end`
echo ${RANGE_START[$NUM_RANGE]}
echo ${RANGE_END[$NUM_RANGE]}
RANGE_CFG[$NUM_RANGE]=1
(( NUM_RANGE++ ))
;;
S) IFS=, read st end <<< "$OPTARG"
RANGE_START[$NUM_RANGE]=`resolvesym $st`
RANGE_END[$NUM_RANGE]=`resolvesym $end`
RANGE_CFG[$NUM_RANGE]=2
(( NUM_RANGE++ ))
;;
s) START="$OPTARG" ;;
y) STOP="$OPTARG" ;;
@) ;;
\?) usage ; exit 1 ;;
*) break ;;
esac
done
shift $((OPTIND - 1))
if [ -z "$1" ] ; then
ENABLE_ONLY=1
fi
COMM=${COMM:-}
KERNEL=${KERNEL:-1}
TRACE=${TRACE:-0}
USERMODE=${USERMODE:-1}
DISRETC=${DISRETC:-0}
DRIVER=${DRIVER:-}
DONTDUMP=${DONTDUMP:-}
DUMP_ONLY=${DUMP_ONLY:-}
PREFIX=${PREFIX:-ptout}
ENUMALL=${ENUMALL:-$ENUMDFL}
CYC_THRESH=${CYC_THRESH:-0}
MTC_FREQ=${MTC_FREQ:-0}
PSB_FREQ=${PSB_FREQ:-0}
START=${START:-0}
STOP=${STOP:-0}
FORCE=${FORCE:-0}
KEEP=${KEEP:-}
PRINT_REGS=${PRINT_REGS:-0}
DISABLE_BRANCH=${DISABLE_BRANCH:-0}
PTWRITE=${PTWRITE:-0}
FUP_ON_PTW=${FUP_ON_PTW:-0}
PWR_EVT=${PWR_EVT:-0}
if [ "$EUID" -ne 0 ] ; then
echo >&2 sptcmd needs to run as root
exit 1
fi
[ -n "$DRIVER" ] && /sbin/rmmod simple_pt
if ! lsmod | grep -q simple_pt ; then
TL=$(grep -qw tasklist_lock /proc/kallsyms &&
awk '$3 == "tasklist_lock" { print "tasklist_lock_ptr=0x" $1 } ' /proc/kallsyms )
if [ -r simple-pt.ko ] ; then
/sbin/insmod simple-pt.ko force=$FORCE $TL
else
/sbin/modprobe simple-pt force=$FORCE $TL
fi
fi
C=/sys/module/simple_pt/parameters
if [ ! -d $C ] ; then
echo >&2 simple-pt driver did not load. please load manually.
exit 1
fi
if [ -n "$DUMP_ONLY" ] ; then
OLDSTART="`cat $C/start`"
fi
if [ "$KERNEL" != 1 -a "$COMM" = "" ] ; then
echo >&2 WARNING Using -K without a -c filter.
echo >&2 WARNING Decoder will not be able to trace multiple processes.
fi
T=/sys/kernel/debug/tracing
if [ -n "$DISABLE" ] ; then
echo 0 > $C/start
if [ -z "$DUMP_ONLY" ] ; then
exit 0
fi
fi
if [ -z "$DUMP_ONLY" ] ; then
if [ -n "$COMM" ] ; then
echo $COMM > $C/comm_filter
echo 1 > $C/cr3_filter
else
echo > $C/comm_filter
echo 0 > $C/cr3_filter
fi
echo $KERNEL > $C/kernel
echo $USERMODE > $C/user
echo $DISRETC > $C/dis_retc
echo $FORCE > $C/force
echo $TRACE > $T/events/pttp/msr/enable
echo 1 > $T/events/pttp/exec_cr3/enable
echo 1 > $T/events/pttp/mmap_cr3/enable
echo 1 > $T/events/pttp/process_cr3/enable
echo $MTC_FREQ > $C/mtc_freq
echo $CYC_THRESH > $C/cyc_thresh
echo $PSB_FREQ > $C/psb_freq
echo $DISABLE_BRANCH > $C/disable_branch
echo $PTWRITE > $C/ptw
echo $FUP_ON_PTW > $C/fup_on_ptw
echo $PWR_EVT > $C/pwr_evt
for (( r = 0; r < NUM_RANGE; r++ )) ; do
echo ${RANGE_START[$r]} > $C/addr${r}_start
echo ${RANGE_END[$r]} > $C/addr${r}_end
echo ${RANGE_CFG[$r]} > $C/addr${r}_cfg
done
for (( ; r < MAX_RANGE; r++ )) ; do
echo 0 > $C/addr${r}_cfg
done
echo $START > $C/trace_start
echo $STOP > $C/trace_stop
echo > $T/trace
echo $ENUMALL > $C/enumerate_all
if [ "$ENUMALL" != 0 ] ; then
grep . /proc/[0-9]*/maps > ${PREFIX}.maps
echo >&2 "Wrote initial process maps to ${PREFIX}.maps"
else
echo -n > ${PREFIX}.maps
fi
ptfeature > ${PREFIX}.cpuid
echo 1 > $C/start
if [ "$ENABLE_ONLY" != "" ] ; then
echo "PT running"
exit 0
fi
"$@"
fi
if [ "$PRINT_REGS" != 0 ] ; then
ptregs
fi
echo 0 > $C/start
if [ -z "$KEEP" ] ; then
echo 0 > $T/events/pttp/exec_cr3/enable
echo 0 > $T/events/pttp/mmap_cr3/enable
echo 0 > $T/events/pttp/process_cr3/enable
echo 0 > $T/events/pttp/msr/enable
fi
if [ -z "$DONTDUMP" ] ; then
sptdump $PREFIX
ptfeature > ${PREFIX}.cpuid
sptsideband.py $T/trace ${PREFIX}.maps ${PREFIX}.cpuid $MTC_FREQ > ${PREFIX}.sideband
if [ "$TRACE" != 0 ] ; then
spttrace < $T/trace > ${PREFIX}.trace
fi
#if [ -r /boot/vmlinux-$(uname -r) ] ; then
# echo -e "0.0 0 0 0\t/boot/vmlinux-$(uname -r)" >> ${PREFIX}.sideband
#elif [ -r /lib/modules/$(uname -r)/build/vmlinux ] ; then
# echo -e "0.0 0 0 0\t/lib/modules/$(uname -r)/build/vmlinux" >> ${PREFIX}.sideband
#else
# echo "vmlinux not found"
#fi
#shopt -s globstar
#while read name a b c d addr ; do
# name=$(echo $name | sed s/_/?/g)
# echo -e "0.0 0" $addr 0 "\t" /lib/modules/$(uname -r)/**/${name}.ko
#done < /proc/modules >> ${PREFIX}.sideband
echo "Wrote sideband to ${PREFIX}.sideband"
fi
if [ -n "$DUMP_ONLY" ] ; then
echo $OLDSTART > $C/start
fi