当前位置:网站首页>How to use ADB shell to query process traffic

How to use ADB shell to query process traffic

2022-06-21 08:36:00 WCanTouch

1、 Get applied Pid.

       Can pass adb shell ps Get all process information , Of course, it contains the application pid And the package name . Then search the returned information ( Find by package name ).

C:\Users>adb shell ps
USER      PID   PPID  VSIZE  RSS   WCHAN              PC  NAME
root      1     0     17336  1668  SyS_epoll_ 0000000000 S /init
root      2     0     0      0       kthreadd 0000000000 S kthreadd
root      3     2     0      0     smpboot_th 0000000000 S ksoftirqd/0
root      5     2     0      0     worker_thr 0000000000 S kworker/0:0H
root      7     2     0      0     rcu_gp_kth 0000000000 S rcu_preempt
root      8     2     0      0     rcu_gp_kth 0000000000 S rcu_sched
root      9     2     0      0     rcu_gp_kth 0000000000 S rcu_bh
root      10    2     0      0     nocb_leade 0000000000 S rcuop/0
root      11    2     0      0     nocb_leade 0000000000 S rcuos/0
root      12    2     0      0     nocb_leade 0000000000 S rcuob/0
root      13    2     0      0     smpboot_th 0000000000 S rcuc/0
root      14    2     0      0     rcu_boost_ 0000000000 S rcub/0
root      15    2     0      0     smpboot_th 0000000000 S migration/0
root      16    2     0      0     smpboot_th 0000000000 S migration/1
root      17    2     0      0     smpboot_th 0000000000 S rcuc/1
root      18    2     0      0     smpboot_th 0000000000 S ksoftirqd/1
root      20    2     0      0     worker_thr 0000000000 S kworker/1:0H
root      21    2     0      0     rcu_nocb_k 0000000000 S rcuop/1
root      22    2     0      0     rcu_nocb_k 0000000000 S rcuos/1

Are the screenshots above still captured ; Find the one you signed up for PID, Use... In the back

2、 Get applied Uid.

      Android System catalog file /proc/pid/status( Be careful : Marked red pid It's No 1 What we got in step pid, Is a greater than 0 The number of ) Contains information about uid Information about . With pid=15904 For example , adopt adb shell cat /proc/15904/status You can get the following information :

C:\Users>adb shell
msm8953_64:/ $ cat /proc/15904/status
cat /proc/15904/status
Name:   com.******
State:  S (sleeping)
Tgid:   15904
Pid:    15904
PPid:   755
TracerPid:      0
Uid:    10092   10092   10092   10092
Gid:    10092   10092   10092   10092
Ngid:   0
FDSize: 256
Groups: 3002 3003 9997 50092
VmPeak:  1666848 kB
VmSize:  1218200 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:    129268 kB
VmRSS:    125288 kB
VmData:   244900 kB
VmStk:      8196 kB
VmExe:        20 kB
VmLib:    144944 kB
VmPTE:       972 kB
VmSwap:      380 kB
Threads:        64
SigQ:   0/10488
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000001204
SigIgn: 0000000000000000
SigCgt: 20000002000084f8
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 0000000000000000
Seccomp:        0
Cpus_allowed:   ff
Cpus_allowed_list:      0-7
voluntary_ctxt_switches:        2751
nonvoluntary_ctxt_switches:     417

      

      By analyzing the above information, you can get Uid.

3、 Read flow value .

      Android System catalog file /proc/uid_stat/uid/ There are usually two files under the tcp_snd and tcp_rcv.( Be careful : Marked red uid Is obtained in step 2 Uid The number ). adopt adb shell cat /proc/uid_stat/10092/tcp_snd and adb shell cat /proc/uid_stat/10092/tcp_rcv You can obtain uplink traffic and downlink traffic respectively .

127|msm8953_64:/ $ ls /proc/uid_stat/10092
ls /proc/uid_stat/10092
tcp_rcv tcp_snd
msm8953_64:/ $ cat /proc/uid_stat/10092/tcp_snd
cat /proc/uid_stat/10092/tcp_snd
119973585
msm8953_64:/ $ cat /proc/uid_stat/10092/tcp_snd
cat /proc/uid_stat/10092/tcp_rcv
123436113

We will provide the following information adb Some commands for :

    adb devices                          List all devices 
    adb -s  Equipment name  shell                 Enter the corresponding equipment 
    cd proc                              Enter the attribute directory of the device 
    cd uid_stat                          Get into  user id  Status directory , When each application is installed, the system will assign a corresponding... To each application  uid
    ls                                   List  uid_stat  Corresponding to all applications in the directory  user id  Catalog 
    cd uid                               Enter the corresponding application  uid  Catalog 
    ls                                   View correspondence  uid  In the catalog  tcp_rcv  and  tcp_snd  Catalog 
    cat tcp_rcv                          View the data information received by the application 
    cat tcp_snd                          View the data information sent by the application 

Reprint it later Analysis of other great gods tcp_send Code :

    private Long getTotalBytesManual(int localUid) {
//        Log.e("BytesManual*****", "localUid:" + localUid);
        File dir = new File("/proc/uid_stat/");
        String[] children = dir.list();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < children.length; i++) {
            stringBuffer.append(children[i]);
            stringBuffer.append("   ");
        }
//        Log.e("children*****", children.length + "");
//        Log.e("children22*****", stringBuffer.toString());
        if (!Arrays.asList(children).contains(String.valueOf(localUid))) {
            return 0L;
        }
        File uidFileDir = new File("/proc/uid_stat/" + String.valueOf(localUid));
        File uidActualFileReceived = new File(uidFileDir, "tcp_rcv");
        File uidActualFileSent = new File(uidFileDir, "tcp_snd");
        String textReceived = "0";
        String textSent = "0";
        try {
            BufferedReader brReceived = new BufferedReader(new FileReader(uidActualFileReceived));
            BufferedReader brSent = new BufferedReader(new FileReader(uidActualFileSent));
            String receivedLine;
            String sentLine;

            if ((receivedLine = brReceived.readLine()) != null) {
                textReceived = receivedLine;
//                Log.e("receivedLine*****", "receivedLine:" + receivedLine);
            }
            if ((sentLine = brSent.readLine()) != null) {
                textSent = sentLine;
//                Log.e("sentLine*****", "sentLine:" + sentLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
//            Log.e("IOException*****", e.toString());
        }
//        Log.e("BytesManualEnd*****", "localUid:" + localUid);
        return Long.valueOf(textReceived).longValue() + Long.valueOf(textSent).longValue();
    }

Please point out what the gods have done wrong , thank you
 

原网站

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