xref: /aosp_15_r20/system/extras/ioshark/collect-straces.sh (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker# Copyright (C) 2016 The Android Open Source Project
2*288bf522SAndroid Build Coastguard Worker#
3*288bf522SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*288bf522SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*288bf522SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*288bf522SAndroid Build Coastguard Worker#
7*288bf522SAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
8*288bf522SAndroid Build Coastguard Worker# # Unless required by applicable law or agreed to in writing, software
9*288bf522SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
10*288bf522SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11*288bf522SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
12*288bf522SAndroid Build Coastguard Worker# limitations under the License.
13*288bf522SAndroid Build Coastguard Worker
14*288bf522SAndroid Build Coastguard Worker#!/bin/sh
15*288bf522SAndroid Build Coastguard Worker
16*288bf522SAndroid Build Coastguard Worker# When signal is received, the stracer will get killed
17*288bf522SAndroid Build Coastguard Worker# Call this (just to make sure anyway)
18*288bf522SAndroid Build Coastguard Workerkill_strace() {
19*288bf522SAndroid Build Coastguard Worker    ps_line=`ps -ef | grep strace | grep adb `
20*288bf522SAndroid Build Coastguard Worker    if [ $? == 0 ]; then
21*288bf522SAndroid Build Coastguard Worker        echo Killing `echo $ps_line | awk '{s = ""; for (i=8; i <= NF ; i++) s = s \
22*288bf522SAndroid Build Coastguard Worker$i " "; print s}' `
23*288bf522SAndroid Build Coastguard Worker        kill `echo $ps_line | awk '{print $2}' `
24*288bf522SAndroid Build Coastguard Worker    fi
25*288bf522SAndroid Build Coastguard Worker}
26*288bf522SAndroid Build Coastguard Worker
27*288bf522SAndroid Build Coastguard Workercatch_sigint()
28*288bf522SAndroid Build Coastguard Worker{
29*288bf522SAndroid Build Coastguard Worker    echo "signal INT received, killing streaming trace capture"
30*288bf522SAndroid Build Coastguard Worker    kill_strace
31*288bf522SAndroid Build Coastguard Worker}
32*288bf522SAndroid Build Coastguard Worker
33*288bf522SAndroid Build Coastguard Workercompile_tracefiles()
34*288bf522SAndroid Build Coastguard Worker{
35*288bf522SAndroid Build Coastguard Worker    for i in trace.*
36*288bf522SAndroid Build Coastguard Worker    do
37*288bf522SAndroid Build Coastguard Worker	if [ $i != trace.begin ] && [ $i != trace.tar ];
38*288bf522SAndroid Build Coastguard Worker	then
39*288bf522SAndroid Build Coastguard Worker	    egrep '\/system\/|\/data\/|\/vendor\/' $i > bar
40*288bf522SAndroid Build Coastguard Worker# parse out /sys/devices/system/...
41*288bf522SAndroid Build Coastguard Worker	    egrep -v '\/sys\/devices\/system\/' bar > bar0
42*288bf522SAndroid Build Coastguard Worker	    mv bar0 bar
43*288bf522SAndroid Build Coastguard Worker	    fgrep -v '= -1'	bar > foo
44*288bf522SAndroid Build Coastguard Worker	    rm bar
45*288bf522SAndroid Build Coastguard Worker	    # begin_time is seconds since epoch
46*288bf522SAndroid Build Coastguard Worker	    begin_time=`cat trace.begin`
47*288bf522SAndroid Build Coastguard Worker	    # replace seconds since epoch with SECONDS SINCE BOOT in the
48*288bf522SAndroid Build Coastguard Worker	    # strace files
49*288bf522SAndroid Build Coastguard Worker	    awk -v begin="$begin_time" '{ printf "%f strace ", $1 - begin; $1=""; print $0}' foo > bar
50*288bf522SAndroid Build Coastguard Worker	    if [ -s bar ]
51*288bf522SAndroid Build Coastguard Worker	    then
52*288bf522SAndroid Build Coastguard Worker		echo parsing $i
53*288bf522SAndroid Build Coastguard Worker		pid=${i##*.}
54*288bf522SAndroid Build Coastguard Worker		compile_ioshark bar $pid.wl
55*288bf522SAndroid Build Coastguard Worker		rm -f bar
56*288bf522SAndroid Build Coastguard Worker	    else
57*288bf522SAndroid Build Coastguard Worker		rm -f $i bar
58*288bf522SAndroid Build Coastguard Worker	    fi
59*288bf522SAndroid Build Coastguard Worker	fi
60*288bf522SAndroid Build Coastguard Worker    done
61*288bf522SAndroid Build Coastguard Worker}
62*288bf522SAndroid Build Coastguard Worker
63*288bf522SAndroid Build Coastguard Worker# main() starts here
64*288bf522SAndroid Build Coastguard Worker
65*288bf522SAndroid Build Coastguard Workeradb root && adb wait-for-device
66*288bf522SAndroid Build Coastguard Worker
67*288bf522SAndroid Build Coastguard Workeradb shell 'ps' | grep zygote > zygote_pids
68*288bf522SAndroid Build Coastguard Worker
69*288bf522SAndroid Build Coastguard Workerfgrep -v grep zygote_pids > bar
70*288bf522SAndroid Build Coastguard Workermv bar zygote_pids
71*288bf522SAndroid Build Coastguard Workerpid1=`grep -w zygote zygote_pids | awk '{print $2}' `
72*288bf522SAndroid Build Coastguard Workerpid2=`grep -w zygote64 zygote_pids | awk '{print $2}' `
73*288bf522SAndroid Build Coastguard Workerrm -f zygote_pids
74*288bf522SAndroid Build Coastguard Worker
75*288bf522SAndroid Build Coastguard Workertrap 'catch_sigint' INT
76*288bf522SAndroid Build Coastguard Worker
77*288bf522SAndroid Build Coastguard Workerecho "^C this script once you finish running your test"
78*288bf522SAndroid Build Coastguard Worker
79*288bf522SAndroid Build Coastguard Workeradb shell "date +%s > /data/local/tmp/trace.begin ; strace -p $pid1,$pid2 -o /data/local/tmp/trace -q -qq -f -ff -y -ttt -e trace=mmap2,read,write,pread64,pwrite64,fsync,fdatasync,openat,close,lseek,_llseek"
80*288bf522SAndroid Build Coastguard Worker
81*288bf522SAndroid Build Coastguard Worker# Remove any remnant tracefiles first
82*288bf522SAndroid Build Coastguard Workerrm -f trace.*
83*288bf522SAndroid Build Coastguard Worker
84*288bf522SAndroid Build Coastguard Worker# Get the tracefiles from the device
85*288bf522SAndroid Build Coastguard Workeradb shell 'cd /data/local/tmp ; tar cvf trace.tar trace.*'
86*288bf522SAndroid Build Coastguard Workeradb pull /data/local/tmp/trace.tar
87*288bf522SAndroid Build Coastguard Worker
88*288bf522SAndroid Build Coastguard Worker# Extract the tracefiles from the device
89*288bf522SAndroid Build Coastguard Workerrm -f *.wl
90*288bf522SAndroid Build Coastguard Workertar xf trace.tar
91*288bf522SAndroid Build Coastguard Worker
92*288bf522SAndroid Build Coastguard Worker# Compile the tracefiles
93*288bf522SAndroid Build Coastguard Workercompile_tracefiles
94*288bf522SAndroid Build Coastguard Worker
95*288bf522SAndroid Build Coastguard Worker# tar up the .wl files just created
96*288bf522SAndroid Build Coastguard Workerrm -f wl.tar
97*288bf522SAndroid Build Coastguard Workertar cf wl.tar ioshark_filenames *.wl
98