Home | History | Annotate | Download | only in winscope
      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 
     18 WINSCOPE_URL='http://go/winscope/#sftrace'
     19 
     20 set -e
     21 
     22 outfile=layerstrace.pb
     23 help=
     24 
     25 for 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
     32 done
     33 
     34 if [ "$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
     42 fi
     43 
     44 function log_error() {
     45   echo "FAILED"
     46 }
     47 trap log_error ERR
     48 
     49 outfile_abs="$(cd "$(dirname "$outfile")"; pwd)/$(basename "$outfile")"
     50 
     51 function 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 }
     57 function 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 
     64 which adb >/dev/null 2>/dev/null || { echo "ERROR: ADB not found."; exit 1; }
     65 adb get-state 2>/dev/null | grep -q device || { echo "ERROR: No device connected or device is unauthorized."; exit 1; }
     66 
     67 echo -n "Deactivating SELinux..."
     68 adb shell su root setenforce 0 2>/dev/null >/dev/null
     69 echo "DONE"
     70 
     71 start_tracing
     72 read -p "Press ENTER to stop tracing" -s x
     73 echo
     74 stop_tracing
     75 adb exec-out su root cat /data/misc/trace/layerstrace.pb >"$outfile"
     76 
     77 echo
     78 echo "To view the trace, go to $WINSCOPE_URL, click 'OPEN SF TRACE' and open"
     79 echo "${outfile_abs}"
     80