1#!/bin/bash
2
3set -euxo pipefail
4
5OUTPUT_DIR=$(dirname "$0")
6. "$OUTPUT_DIR"/include.sh
7
8TRACEDIR=`mktemp -d`
9
10### Make sure we can connect to the device.
11
12# Get the device's wlan0 address.
13IP_ADDR=$(adb shell ip route get 0.0.0.0 oif wlan0 | sed -En -e 's/.*src (\S+)\s.*/\1/p')
14REMOTE_PORT=5555
15REMOTE=$IP_ADDR:$REMOTE_PORT
16LOCAL_SERIAL=$(adb shell getprop ro.serialno)
17
18# Check that we can connect to it.
19adb disconnect
20
21TRANSPORT_ID=$(adb transport-id)
22adb tcpip $REMOTE_PORT
23adb -t $TRANSPORT_ID wait-for-disconnect
24
25adb connect $REMOTE
26
27REMOTE_FETCHED_SERIAL=$(adb -s $REMOTE shell getprop ro.serialno)
28
29if [[ "$LOCAL_SERIAL" != "$REMOTE_FETCHED_SERIAL" ]]; then
30  echo "Mismatch: local serial = $LOCAL_SERIAL, remote serial = $REMOTE_FETCHED_SERIAL"
31  exit 1
32fi
33
34# Back to USB, and make sure adbd is root.
35adb -s $REMOTE usb
36adb disconnect $REMOTE
37
38adb wait-for-device root
39adb root
40adb wait-for-device
41
42TRANSPORT_ID=$(adb transport-id)
43adb usb
44adb -t $TRANSPORT_ID wait-for-disconnect
45
46adb wait-for-device
47
48### Run the adb unit tests and fetch traces from them.
49mkdir "$TRACEDIR"/test_traces
50adb shell rm -rf /data/local/tmp/adb_coverage
51adb shell mkdir /data/local/tmp/adb_coverage
52
53for TEST in $ADB_TESTS; do
54  adb shell LLVM_PROFILE_FILE=/data/local/tmp/adb_coverage/$TEST.profraw /data/nativetest64/$TEST/$TEST
55  adb pull /data/local/tmp/adb_coverage/$TEST.profraw "$TRACEDIR"/test_traces/
56done
57
58adb pull /data/local/tmp/adb_coverage "$TRACEDIR"/test_traces
59
60# Clear logcat and increase the buffer to something ridiculous so we can fetch the pids of adbd later.
61adb shell logcat -c -G128M
62
63# Turn on extremely verbose logging so as to not count debug logging against us.
64adb shell setprop persist.adb.trace_mask 1
65
66### Run test_device.py over USB.
67TRANSPORT_ID=$(adb transport-id)
68adb shell killall adbd
69adb -t $TRANSPORT_ID wait-for-disconnect
70
71adb wait-for-device shell rm -rf "/data/misc/trace/*" /data/local/tmp/adb_coverage/
72"$OUTPUT_DIR"/../test_device.py
73
74# Do a usb reset to exercise the disconnect code.
75adb_usbreset
76adb wait-for-device
77
78# Dump traces from the currently running adbd.
79adb shell killall -37 adbd
80
81echo Waiting for adbd to finish dumping traces
82sleep 5
83
84# Restart adbd in tcp mode.
85TRANSPORT_ID=$(adb transport-id)
86adb tcpip $REMOTE_PORT
87adb -t $TRANSPORT_ID wait-for-disconnect
88
89adb connect $REMOTE
90adb -s $REMOTE wait-for-device
91
92# Instead of running test_device.py again, which takes forever, do some I/O back and forth instead.
93dd if=/dev/zero bs=1024 count=10240 | adb -s $REMOTE raw sink:10485760
94adb -s $REMOTE raw source:10485760 | dd of=/dev/null bs=1024 count=10240
95
96# Dump traces again.
97adb disconnect $REMOTE
98adb shell killall -37 adbd
99
100echo Waiting for adbd to finish dumping traces
101sleep 5
102
103adb pull /data/misc/trace "$TRACEDIR"/
104echo Pulled traces to $TRACEDIR
105
106# Identify which of the trace files are actually adbd, in case something else exited simultaneously.
107ADBD_PIDS=$(adb shell "logcat -d -s adbd --format=process | grep 'adbd started' | cut -c 3-7 | tr -d ' ' | sort | uniq")
108mkdir "$TRACEDIR"/adbd_traces
109
110adb shell 'setprop persist.adb.trace_mask 0; killall adbd'
111
112IFS=$'\n'
113for PID in $ADBD_PIDS; do
114  cp "$TRACEDIR"/trace/clang-$PID-*.profraw "$TRACEDIR"/adbd_traces 2>/dev/null || true
115done
116unset IFS
117
118### Merge the traces.
119llvm-profdata merge --output="$OUTPUT_DIR"/adbd.profdata "$TRACEDIR"/adbd_traces/* "$TRACEDIR"/test_traces/*
120