1#!/bin/bash
2
3# Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
4#
5# Use of this source code is governed by a BSD-style license
6# that can be found in the LICENSE file in the root of the source
7# tree. An additional intellectual property rights grant can be found
8# in the file PATENTS.  All contributing project authors may
9# be found in the AUTHORS file in the root of the source tree.
10
11# Utility functions to be used by perf_setup.sh.
12# Contains helper methods and functions that wraps usage of adb.
13
14function error() {
15  echo "[ERROR] "$@"" >&2
16}
17
18function warning() {
19  echo "[WARNING] "$@"" >&1
20}
21
22function ok() {
23  echo "[OK] "$@"" >&1
24}
25
26function abs_path {
27  (cd $1; pwd)
28}
29
30function is_set() {
31  local var="$1"
32  [[ -n "${var}" ]]
33}
34
35function is_file() {
36  local file="$1"
37  [[ -f "${file}" ]]
38}
39
40function is_not_file() {
41  local file="$1"
42  [[ ! -f "${file}" ]]
43}
44
45function is_dir() {
46  local dir="$1"
47  [[ -d "${dir}" ]]
48}
49
50function is_not_dir() {
51  local dir="$1"
52  [[ ! -d "${dir}" ]]
53}
54
55# Adds (prepends) the PATH environment variable while avoid duplicates.
56function path_add() {
57  case ":${PATH:=$1}:" in
58    *:$1:*)  ;;
59    *) PATH="$1:$PATH"  ;;
60  esac
61}
62
63# Removes a path from the PATH environment variable using search-and-replace
64# parameter expansion.
65function path_remove {
66  local path="$1"
67  # Substitute first occurrence of ":path" in PATH with an empty string.
68  # Deletes instances in the middle or at the end.
69  PATH=${PATH/":$path"/}
70  # Substitute first occurrence of "path:" in PATH with an empty string.
71  # Delete instances at the beginning.
72  PATH=${PATH/"$path:"/}
73}
74
75# Returns the process ID (PID) of the process that corresponds to the
76# application name given as input parameter.
77function find_app_pid() {
78  local app_name="$1"
79  adb shell ps | grep "${app_name}" | awk '{print $2}'
80}
81
82function app_is_installed() {
83  local app_name="$1"
84  local installed_app_name=$(adb shell pm list packages \
85    | grep "${app_name}" | awk -F':' '{print $2}')
86  is_set "${installed_app_name}" \
87    && [[ "${installed_app_name}" = "${app_name}" ]]
88}
89
90function app_is_running() {
91  local app_name="$1"
92  local app_pid=$(find_app_pid "${app_name}")
93  is_set "${app_pid}"
94}
95
96function app_start() {
97  local app_name="$1"
98  adb shell am start \
99    -n "${app_name}/.ConnectActivity" \
100    -a android.intent.action.MAIN
101}
102
103function app_stop() {
104  local app_name="$1"
105  adb shell am force-stop "${app_name}"
106}
107
108function app_uninstall() {
109  local app_name="$1"
110  adb uninstall "${app_name}"
111}
112
113function dev_arch() {
114  adb shell uname -m
115}
116
117function dev_ls() {
118  local dir="$1"
119  adb shell ls "${dir}"
120}
121
122# Returns true if exactly on device is connected.
123function one_device_connected() {
124  [[ $(adb devices | wc -l) = 3 ]]
125}
126
127# Returns true if device is rooted.
128function image_is_root() {
129  [[ $(adb shell getprop ro.build.type) = "userdebug" ]]
130}
131
132# Returns true if device is not rooted.
133function image_is_not_root() {
134  [[ $(adb shell getprop ro.build.type) = "user" ]]
135}
136
137# Returns true if adb is not already running as root.
138# Should only be called on rooted devices.
139function adb_has_no_root_permissions() {
140  [[ $(adb shell getprop service.adb.root) = 0 ]]
141}
142
143# Android devices may disable profiling by default. We must enable it.
144function enable_profiling() {
145  adb shell setprop security.perf_harden 0
146}
147
148# To make the report of symbols on device successful, we need to execute
149# `echo 0 >/proc/sys/kernel/kptr_restrict`.
150# Only needed if we run report commands on the same machine as we run
151# record commands.
152function enable_report_symbols() {
153  adb shell "echo 0 > /proc/sys/kernel/kptr_restrict"
154}
155