当前位置:网站首页>The skill of using ADB and the principle of USB communication

The skill of using ADB and the principle of USB communication

2022-06-21 08:36:00 WCanTouch

About adb The tips and new findings on will be updated later , This article will be updated for a long time

One 、adb The technique used

1. Query the currently displayed Activity

adb shell dumpsys activity top | head -n 10

 

dumpactivity

 

2. Back up and restore all installed apk

  • Find the third party installed on the device apk Package name : adb shell pm list packages -3

  • basis apk Package name finding apk: adb shell pm path $pkg

  • export apk file : adb shell pull $path

  • Export the apk Install the file into the new device : adb install -r $file

Script :

 

    
#!/bin/sh
echo -n "" > apks
adb shell pm list packages -3 | sed -E "s/\r$//" \
| while read line
do
    pkg=${line#*:}
    line=$(adb shell pm path "$pkg"&)
    path=${line#*:}
    echo $pkg:$path >> apks
done
cat apks
cat apks | sed -E "s/\r$//" | while read line
do
    pkg=${line%:*}
    path=${line#*:}
    adb pull "$path" "$pkg.apk"
    echo $path
done
rm apks
#echo $apks;

 

doubt , The first 7 Rows query by package name apk When installing the path , Add a... At the end of the command & character , You can't find all without adding apk route , I don't know why .

3. adb debugging wifi Patterns and usb Mode switch

Mobile terminal (root)

  • install terminal

  • cut wifi: setprop service.adb.tcp.port 5555

  • cut wifi The restart still works :setprop persist.adb.tcp.port 5555

  • cut usb: setprop service.adb.tcp.port -1

  • stop adbd

  • start adbd

Switch on the computer wifi

  • cut wifi: adb tcpip 5555

  • cut usb: adb usb

Computer end connection equipment

  • wifi Pattern : adb connect mobile phone ip:5555

  • usb Pattern : adopt usb Connect the phone

Two 、adbs End sum adbd End ,usb Exploration of communication principle

1. distinguish usb equipment , find USB Equipment information

lsusb

usb_device_info
Record ID, Visit the website to view usb Device type ( What is the device )

http://www.linux-usb.org
It is possible to confirm the Vendors And equipment type ( The printer /Mass Storage etc. )

2. Linux The kernel recognizes the device

udevadm monitor --kernel

usb_kernel_monitor
Then go and check device Information about
udevadm info -q all -p

 

usb_kernel_info

3. adbs Access to the device file

start-up adbs, Check the process access device file
adb start-server ps -ef | grep adb lsof -p $pid

 

adb_access_device_file

4. adbs Source code analysis

The call stack

 

main(adb.c)
main_adb(adb.c)
[
    usb_vendors_init(usb_vendors.c)
    usb_init(usb_linux.c)
]
-----new thread----
device_poll_thread(usb_linux.c)
find_usb_device(usb_linux.c)
kick_disconnected_devices(usb_linux.c)

 

from usb_vendors.c In file , You can know vendor Information is " curing "adbs Inside .( Can that explain why linux Unwanted adb What about the driver )

 

 

adbs_init_vendors
usb_linux.c Function list of the file

 

 

adbs_usb_linux_functions
Read the function inside , The main packaging linux usb Universal access device The way .

5. adbd Access to the device file

adb shell su (root jurisdiction ) lsof > /sdcard/lsof.data adb pull /sdcard/lsof.data less lsof.data

 

adbd_access_file

6. adbd Source code analysis

The call stack

 

main(adb.c)
main_adb(adb.c)
usb_init(usb_linux_client.c)
usb_adb_init(usb_linux_client.c)

usb_adb_init The content of , Can be determined adbd Indeed visited /dev/android_adb file ( Equipment node )

 

usb_linux_client.c File definition read function , From the implementation point of view, it is mainly through /dev/android_adb Document communication with the outside world .

 

 

 

adbd_usb_linux_client_function

7. Series connection adbs and adbd(android kernel)

 

 

init(android.c)
usb_composite_register[&android_usb_driver](android.c)
android_bind(android.c)
usb_add_config[cdev, &android_config_driver](android.c)
android_bind_config(android.c)
adb_function_add(f_adb.c)
misc_deregister[&adb_device](f_adb.c)
static struct miscdevice adb_device = {
      .minor = MISC_DYNAMIC_MINOR,
      .name = shortname,
      .fops = &adb_fops,
};
static const char shortname[] = "android_adb";

 

android.c and f_adb.c The code path is /drivers/usb/gadget/ Be careful : The code here is kernel Source code , No Android Source code (aosp). If you also downloaded linux kernel, You will find that there are no these two files .

 

1, Memory

Access to the device shell Environmental Science :

adb -s [ Equipment serial number ] shell

Get the process number of the current target package :

ps | grep com.xxx.xxx

View the memory information of the current process :

dumpsys meminfo  Process number / package 

View the maximum memory limit for a single application :

getprop | grep heapgrowthlimit

2,  CPU

Displays the of the current process CPU Information :

top -n 10 -d 5 | grep [PID]  [ notes ]:-n Indicates the number of refreshes ,-d Indicates the refresh interval 

Displays the total of the current process CPU Occupancy rate :

dumpsys cpuinfo  Process number / package 

Start a activity, Show start time :

am start -W [ package /Activity]  [ notes ]:e.g. com.xxx.xxx/com.xxx.xxx.xxx.activity

    WaitTime:  Including the previous application Activity pause Time and new Activity Start time 

    TotalTime: The time it takes to start a new application , Including process startup and Activity Start of 

    ThisTime: It means a series of starts Activity The last Activity The start-up time of 

3,   Traffic

Get the process number of the current target package :

ps | grep com.xxx.xxx

according to PID obtain UID【1】

cat /proc/PID/status

Get download traffic

cat /proc/uid_stat /UID/tcp_rcv

Get upload traffic

cat /proc/uid_stat/UID/tcp_snd

Android Each application is assigned a different UID, here UID and Linux Of UID Somewhat different .

 

 

 

 

 

 

 

 

 

 

 

 

原网站

版权声明
本文为[WCanTouch]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206210832426430.html