1#!/bin/bash 2# 3# Copyright (C) 2017 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18WINSCOPE_URL='http://go/winscope/#sftrace' 19 20set -e 21 22outfile=layerstrace.pb 23help= 24 25for arg in "$@"; do 26 case $arg in 27 -h|--help) help=1;; 28 --);; 29 -*) echo "Unknown option: $arg"; help=1;; 30 *) outfile="$arg";; 31 esac 32done 33 34if [ "$help" != "" ]; then 35 echo "usage: $0 [-h | --help] [OUTFILE]" 36 echo 37 echo "Traces SurfaceFlinger and writes the output to OUTFILE (default ./layerstrace.pb)." 38 echo "To view the traces, use $WINSCOPE_URL." 39 echo 40 echo "WARNING: This calls adb root and deactivates SELinux." 41 exit 1 42fi 43 44function log_error() { 45 echo "FAILED" 46} 47trap log_error ERR 48 49outfile_abs="$(cd "$(dirname "$outfile")"; pwd)/$(basename "$outfile")" 50 51function start_tracing() { 52 echo -n "Starting SurfaceFlinger trace..." 53 adb shell su root service call SurfaceFlinger 1025 i32 1 >/dev/null 54 echo "DONE" 55 trap stop_tracing EXIT 56} 57function stop_tracing() { 58 echo -n "Stopping SurfaceFlinger trace..." 59 adb shell su root service call SurfaceFlinger 1025 i32 0 >/dev/null 60 echo "DONE" 61 trap - EXIT 62} 63 64which adb >/dev/null 2>/dev/null || { echo "ERROR: ADB not found."; exit 1; } 65adb get-state 2>/dev/null | grep -q device || { echo "ERROR: No device connected or device is unauthorized."; exit 1; } 66 67echo -n "Deactivating SELinux..." 68adb shell su root setenforce 0 2>/dev/null >/dev/null 69echo "DONE" 70 71start_tracing 72read -p "Press ENTER to stop tracing" -s x 73echo 74stop_tracing 75adb exec-out su root cat /data/misc/wmtrace/layers_trace.pb >"$outfile" 76 77echo 78echo "To view the trace, go to $WINSCOPE_URL, click 'OPEN SF TRACE' and open" 79echo "${outfile_abs}" 80