summaryrefslogtreecommitdiff
path: root/fakeadbserver/src/main/java/com/android/fakeadbserver/ClientState.kt
blob: 33af25979b39dd05c861546f7cd6d79e147e78f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.fakeadbserver

import java.net.Socket
import java.util.Arrays
import java.util.concurrent.atomic.AtomicInteger

class ClientState internal constructor(
    pid: Int,
    val uid: Int,
    val processName: String,
    val packageName: String, // Whether this client is waiting for a debugger connection or not
    val isWaiting: Boolean
) : ProcessState(pid) {

    val viewsState = ClientViewsState()
    val profilerState = ProfilerState()

    /**
     * Set of DDMS features for this process.
     *
     * See [HandleFEAT source code](https://cs.android.com/android/platform/superproject/+/android13-release:frameworks/base/core/java/android/ddm/DdmHandleHello.java;l=107)
     */
    private val mFeatures: MutableSet<String> = HashSet()
    private var jdwpSocket: Socket? = null
    var isAllocationTrackerEnabled = false
    var allocationTrackerDetails = ""
    private val hgpcRequestsCount = AtomicInteger()
    private val nextDdmsCommandId = AtomicInteger(0x70000000)

    init {
        mFeatures.addAll(Arrays.asList(*mBuiltinVMFeatures))
        mFeatures.addAll(Arrays.asList(*mBuiltinFrameworkFeatures))
    }

    override val debuggable: Boolean
        get() = true
    override val profileable: Boolean
        get() = false

    @Synchronized
    fun startJdwpSession(socket: Socket): Boolean {
        if (jdwpSocket != null) {
            return false
        }
        jdwpSocket = socket
        return true
    }

    @Synchronized
    fun stopJdwpSession() {
        if (jdwpSocket != null) {
            try {
                jdwpSocket!!.shutdownOutput()
                Thread.sleep(10) // So that FIN is received by peer
                jdwpSocket!!.close()
            } catch (e: Exception) {
                throw RuntimeException(e)
            }
        }
        jdwpSocket = null
    }

    fun nextDdmsCommandId(): Int {
        return nextDdmsCommandId.incrementAndGet()
    }

    @Synchronized
    fun clearFeatures() {
        mFeatures.clear()
    }

    @Synchronized
    fun addFeature(value: String) {
        mFeatures.add(value)
    }

    @Synchronized
    fun removeFeature(value: String) {
        mFeatures.remove(value)
    }

    @get:Synchronized
    val features: Set<String>
        get() = HashSet(mFeatures)

    fun requestHgpc() {
        hgpcRequestsCount.incrementAndGet()
    }

    fun getHgpcRequestsCount(): Int {
        return hgpcRequestsCount.get()
    }

    companion object {

        /**
         * See [List of VM features](https://cs.android.com/android/platform/superproject/+/android13-release:art/runtime/native/dalvik_system_VMDebug.cc;l=56)
         */
        private val mBuiltinVMFeatures = arrayOf(
            "method-trace-profiling",
            "method-trace-profiling-streaming",
            "method-sample-profiling",
            "hprof-heap-dump",
            "hprof-heap-dump-streaming"
        )

        /**
         * See [Framework features](https://cs.android.com/android/platform/superproject/+/android13-release:frameworks/base/core/java/android/ddm/DdmHandleHello.java;drc=4794e479f4b485be2680e83993e3cf93f0f42d03;l=44)
         */
        private val mBuiltinFrameworkFeatures = arrayOf(
            "opengl-tracing", "view-hierarchy"
        )
    }
}