1#!/bin/bash
2
3# Copyright (C) 2018 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
17set -e
18. "${0%/*}"/bash_util.bash
19
20function usage() {
21    cat 1>&2 <<"EOF"
22
23  $script_name: Get heapdump from a process and open in ahat.
24
25  Usage: $script_name PID_OR_PROCESS_NAME
26
27EOF
28    exit 1
29}
30
31pid="$(resolve_pid "$1" || usage)"
32
33dev_file=/data/local/tmp/heapdump.prof
34local_file="$(make_temp_file .prof)"
35
36
37info "Getting heap dump from $pid ..."
38do_adb shell am dumpheap -g "$pid" "$dev_file"
39
40
41info "Pulling the dump file to $local_file ..."
42do_adb pull "$dev_file" "$local_file"
43
44
45info "Looking for available port ..."
46port=$(find_open_port 11000)
47info "Using port $port"
48
49(
50    sleep 2
51    info "Starting the browser ..."
52    setsid "${AHAT_BROWSER:-google-chrome}" http://localhost:$port
53) &
54
55
56info "Opening $local_file with ahat ..."
57ahat -p $port "$local_file"