#!/usr/bin/python3
# Copyright (C) 2019 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.
#
# This is an ADB proxy for Winscope.
#
# Requirements: python3.5 and ADB installed and in system PATH.
#
# Usage:
# run: python3 winscope_proxy.py
#
import json
import logging
import os
import re
import secrets
import signal
import subprocess
import sys
import threading
import time
from abc import abstractmethod
from enum import Enum
from http import HTTPStatus
from http.server import HTTPServer, BaseHTTPRequestHandler
from tempfile import NamedTemporaryFile
import base64
# CONFIG #
LOG_LEVEL = logging.DEBUG
PORT = 5544
# Keep in sync with ProxyClient#VERSION in Winscope
VERSION = '1.0'
WINSCOPE_VERSION_HEADER = "Winscope-Proxy-Version"
WINSCOPE_TOKEN_HEADER = "Winscope-Token"
# Location to save the proxy security token
WINSCOPE_TOKEN_LOCATION = os.path.expanduser('~/.config/winscope/.token')
# Winscope traces extensions
WINSCOPE_EXT = ".winscope"
WINSCOPE_EXT_LEGACY = ".pb"
WINSCOPE_EXTS = [WINSCOPE_EXT, WINSCOPE_EXT_LEGACY]
# Winscope traces directory
WINSCOPE_DIR = "/data/misc/wmtrace/"
# Max interval between the client keep-alive requests in seconds
KEEP_ALIVE_INTERVAL_S = 5
logging.basicConfig(stream=sys.stderr, level=LOG_LEVEL,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
log = logging.getLogger("ADBProxy")
class File:
def __init__(self, file, filetype) -> None:
self.file = file
self.type = filetype
def get_filepaths(self, device_id):
return [self.file]
def get_filetype(self):
return self.type
class FileMatcher:
def __init__(self, path, matcher, filetype) -> None:
self.path = path
self.matcher = matcher
self.type = filetype
def get_filepaths(self, device_id):
matchingFiles = call_adb(
f"shell su root find {self.path} -name {self.matcher}", device_id)
log.debug("Found file %s", matchingFiles.split('\n')[:-1])
return matchingFiles.split('\n')[:-1]
def get_filetype(self):
return self.type
class WinscopeFileMatcher(FileMatcher):
def __init__(self, path, matcher, filetype) -> None:
self.path = path
self.internal_matchers = list(map(lambda ext: FileMatcher(path, f'{matcher}{ext}', filetype),
WINSCOPE_EXTS))
self.type = filetype
def get_filepaths(self, device_id):
for matcher in self.internal_matchers:
files = matcher.get_filepaths(device_id)
if len(files) > 0:
return files
log.debug("No files found")
return []
class TraceTarget:
"""Defines a single parameter to trace.
Attributes:
file_matchers: the matchers used to identify the paths on the device the trace results are saved to.
trace_start: command to start the trace from adb shell, must not block.
trace_stop: command to stop the trace, should block until the trace is stopped.
"""
def __init__(self, files, trace_start: str, trace_stop: str) -> None:
if type(files) is not list:
files = [files]
self.files = files
self.trace_start = trace_start
self.trace_stop = trace_stop
# Order of files matters as they will be expected in that order and decoded in that order
TRACE_TARGETS = {
"window_trace": TraceTarget(
WinscopeFileMatcher(WINSCOPE_DIR, "wm_trace", "window_trace"),
'su root cmd window tracing start\necho "WM trace started."',
'su root cmd window tracing stop >/dev/null 2>&1'
),
"accessibility_trace": TraceTarget(
WinscopeFileMatcher("/data/misc/a11ytrace", "a11y_trace", "accessibility_trace"),
'su root cmd accessibility start-trace\necho "Accessibility trace started."',
'su root cmd accessibility stop-trace >/dev/null 2>&1'
),
"layers_trace": TraceTarget(
WinscopeFileMatcher(WINSCOPE_DIR, "layers_trace", "layers_trace"),
'su root service call SurfaceFlinger 1025 i32 1\necho "SF trace started."',
'su root service call SurfaceFlinger 1025 i32 0 >/dev/null 2>&1'
),
"screen_recording": TraceTarget(
File(f'/data/local/tmp/screen.mp4', "screen_recording"),
f'screenrecord --bit-rate 8M /data/local/tmp/screen.mp4 >/dev/null 2>&1 &\necho "ScreenRecorder started."',
'pkill -l SIGINT screenrecord >/dev/null 2>&1'
),
"transactions": TraceTarget(
WinscopeFileMatcher(WINSCOPE_DIR, "transactions_trace", "transactions"),
'su root service call SurfaceFlinger 1041 i32 1\necho "SF transactions recording started."',
'su root service call SurfaceFlinger 1041 i32 0 >/dev/null 2>&1'
),
"transactions_legacy": TraceTarget(
[
WinscopeFileMatcher(WINSCOPE_DIR, "transaction_trace", "transactions_legacy"),
FileMatcher(WINSCOPE_DIR, f'transaction_merges_*', "transaction_merges"),
],
'su root service call SurfaceFlinger 1020 i32 1\necho "SF transactions recording started."',
'su root service call SurfaceFlinger 1020 i32 0 >/dev/null 2>&1'
),
"proto_log": TraceTarget(
WinscopeFileMatcher(WINSCOPE_DIR, "wm_log", "proto_log"),
'su root cmd window logging start\necho "WM logging started."',
'su root cmd window logging stop >/dev/null 2>&1'
),
"ime_trace_clients": TraceTarget(
WinscopeFileMatcher(WINSCOPE_DIR, "ime_trace_clients", "ime_trace_clients"),
'su root ime tracing start\necho "Clients IME trace started."',
'su root ime tracing stop >/dev/null 2>&1'
),
"ime_trace_service": TraceTarget(
WinscopeFileMatcher(WINSCOPE_DIR, "ime_trace_service", "ime_trace_service"),
'su root ime tracing start\necho "Service IME trace started."',
'su root ime tracing stop >/dev/null 2>&1'
),
"ime_trace_managerservice": TraceTarget(
WinscopeFileMatcher(WINSCOPE_DIR, "ime_trace_managerservice", "ime_trace_managerservice"),
'su root ime tracing start\necho "ManagerService IME trace started."',
'su root ime tracing stop >/dev/null 2>&1'
),
"wayland_trace": TraceTarget(
WinscopeFileMatcher("/data/misc/wltrace", "wl_trace", "wl_trace"),
'su root service call Wayland 26 i32 1 >/dev/null\necho "Wayland trace started."',
'su root service call Wayland 26 i32 0 >/dev/null'
),
"eventlog": TraceTarget(
WinscopeFileMatcher("/data/local/tmp", "eventlog", "eventlog"),
'rm -f /data/local/tmp/eventlog.winscope && EVENT_LOG_TRACING_START_TIME=$EPOCHREALTIME\necho "Event Log trace started."',
'echo "EventLog\\n" > /data/local/tmp/eventlog.winscope && su root logcat -b events -v threadtime -v printable -v uid -v nsec -v epoch -b events -t $EVENT_LOG_TRACING_START_TIME >> /data/local/tmp/eventlog.winscope',
),
"transition_traces": TraceTarget(
[WinscopeFileMatcher(WINSCOPE_DIR, "wm_transition_trace", "wm_transition_trace"),
WinscopeFileMatcher(WINSCOPE_DIR, "shell_transition_trace", "shell_transition_trace")],
'su root cmd window shell tracing start && su root dumpsys activity service SystemUIService WMShell transitions tracing start\necho "Transition traces started."',
'su root cmd window shell tracing stop && su root dumpsys activity service SystemUIService WMShell transitions tracing stop >/dev/null 2>&1'
),
}
class SurfaceFlin
没有合适的资源?快使用搜索试试~ 我知道了~
Android开发中使用的winscope

共2个文件
py:1个
html:1个

2 下载量 33 浏览量
2024-07-16
10:41:02
上传
评论
收藏 1.02MB ZIP 举报
温馨提示
Android开发中使用的winscope
资源推荐
资源详情
资源评论




























收起资源包目录




共 2 条
- 1
资源评论


doublelixin
- 粉丝: 201
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 实训报告-网页制作与网站建设项目实战.doc
- 试论互联网+时代事业单位档案管理创新.docx
- PLC控制中央空调节能改造方案设计书1.doc
- 互联网+会计时代-高职《管理会计》课程改革探究.docx
- 基于SNAP网络的实验室监控系统研究设计.doc
- 嵌入式系统程序可移植性设计方案及性能优化.doc
- 单片机电子台历设计方案.docx
- 2017年广西公需科目-“互联网+”开放合作考试及标准答案2(90分).docx
- 抢答器PLC控制系统设计-河南工业大学.doc
- 培训师大计算机采集处理系统.pptx
- 大数据在健康医疗行业中应用概况.pptx
- 慧锦校园网络布线系统措施设计方案.doc
- 机械产品和零件的计算机辅助设计.docx
- 《数据库课程设计方案》实验任务书学时.doc
- 项目管理中如何建立高绩效的研发项目团队.docx
- 基于51单片机的多路温度采集控制系统方案设计书.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
