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/'
19
20set -e
21
22help=
23
24for arg in "$@"; do
25  case $arg in
26    -h|--help) help=1;;
27    --);;
28    -*) echo "Unknown option: $arg"; help=1;;
29    *) outfile="$arg";;
30  esac
31done
32
33outfileTrans=${outfile}_transactiontrace.pb
34outfileSurf=${outfile}_layerstrace.pb
35
36outfileTrans_abs="$(cd "$(dirname "$outfileTrans")"; pwd)/$(basename "$outfileTrans")"
37outfileSurf_abs="$(cd "$(dirname "$outfileSurf")"; pwd)/$(basename "$outfileSurf")"
38
39if [ "$help" != "" ]; then
40  echo "usage: $0 [-h | --help] [OUTFILE]"
41  echo
42  echo "Records Transaction traces (default transactiontrace.pb)."
43  echo "Records Surface traces (default layerstrace.pb)."
44  echo "To view the traces, use $WINSCOPE_URL."
45  exit 1
46fi
47
48function log_error() {
49  echo "FAILED"
50}
51trap log_error ERR
52
53function start_tracing() {
54  echo -n "Starting transaction and surface recording..."
55  echo
56  adb shell su root service call SurfaceFlinger 1020 i32 1 >/dev/null
57  adb shell su root service call SurfaceFlinger 1025 i32 1 >/dev/null
58  echo "DONE"
59  trap stop_tracing EXIT
60}
61
62function stop_tracing() {
63  echo -n "Stopping transaction and surface recording..."
64  echo
65  adb shell su root service call SurfaceFlinger 1020 i32 0 >/dev/null
66  adb shell su root service call SurfaceFlinger 1025 i32 0 >/dev/null
67  echo "DONE"
68  trap - EXIT
69}
70
71which adb >/dev/null 2>/dev/null || { echo "ERROR: ADB not found."; exit 1; }
72adb get-state 2>/dev/null | grep -q device || { echo "ERROR: No device connected or device is unauthorized."; exit 1; }
73
74start_tracing
75read -p "Press ENTER to stop recording" -s x
76echo
77stop_tracing
78adb exec-out su root cat /data/misc/wmtrace/transaction_trace.pb >"$outfileTrans"
79adb exec-out su root cat /data/misc/wmtrace/layers_trace.pb >"$outfileSurf"
80
81echo
82echo "To view the trace, go to $WINSCOPE_URL, and open"
83echo "${outfileTrans_abs}"
84echo "${outfileSurf_abs}"
85