• adb shell dumpsys 使用命令和来源


    一、概述

            adb shell dumpsys 在Android开发中经常要用到,平时都是零碎的积累,用到什么的时候就

    记录下来,最近看了一些资料,发现可以汇总所有的命令,当带某个参数的时候,就可以查看具体

    的信息, 本篇文章中还讲解了如何去找命令的源码和源头,供参考!

    二、支持dumpsys命令的服务

    1. 列出系统所有支持的dumpsys命令的服务

    1. adb shell dumpsys | grep -i "DUMP OF SERVICE"

    2. 支持dumpsys命令的服务

     adb shell service list
    1. Found 203 services:
    2. 0 DockObserver: []
    3. 1 SurfaceFlinger: [android.ui.ISurfaceComposer]
    4. 2 VideoCallEngineService: []
    5. 3 accessibility: [android.view.accessibility.IAccessibilityManager]
    6. 4 account: [android.accounts.IAccountManager]
    7. 5 activity: [android.app.IActivityManager]
    8. 6 activity_task: [android.app.IActivityTaskManager]
    9. 7 adb: [android.debug.IAdbManager]
    10. 8 alarm: [android.app.IAlarmManager]
    11. 9 android.hardware.light.ILights/default: []
    12. 10 android.hardware.power.IPower/default: []
    13. 11 android.hardware.rebootescrow.IRebootEscrow/default: []
    14. 12 android.hardware.vibrator.IVibrator/default: []
    15. 13 android.os.UpdateEngineService: []
    16. 14 android.security.identity: [android.security.identity.ICredentialStoreFactory]
    17. 15 android.security.keystore: [android.security.keystore.IKeystoreService]
    18. 16 android.service.gatekeeper.IGateKeeperService: []
    19. 17 app_binding: []
    20. 18 app_integrity: [android.content.integrity.IAppIntegrityManager]
    21. 19 appops: [com.android.internal.app.IAppOpsService]
    22. 20 appwidget: [com.android.internal.appwidget.IAppWidgetService]
    23. 21 audio: [android.media.IAudioService]
    24. 22 auth: [android.hardware.biometrics.IAuthService]
    25. 23 autofill: [android.view.autofill.IAutoFillManager]
    26. 24 backup: [android.app.backup.IBackupManager]
    27. 25 battery: []
    28. 26 batteryproperties: [android.os.IBatteryPropertiesRegistrar]
    29. 27 batterystats: [com.android.internal.app.IBatteryStats]
    30. 28 binder_calls_stats: []
    31. 29 biometric: [android.hardware.biometrics.IBiometricService]
    32. 30 blob_store: [android.app.blob.IBlobStoreManager]
    33. 31 bluetooth_manager: [android.bluetooth.IBluetoothManager]
    34. 32 broadcastradio: [android.hardware.radio.IRadioService]
    35. 33 bugreport: [android.os.IDumpstate]
    36. 34 cacheinfo: []
    37. 35 carrier_config: [com.android.internal.telephony.ICarrierConfigLoader]
    38. 36 clipboard: [android.content.IClipboard]
    39. 37 color_display: [android.hardware.display.IColorDisplayManager]
    40. 38 companiondevice: [android.companion.ICompanionDeviceManager]
    41. 39 connectivity: [android.net.IConnectivityManager]
    42. 40 connmetrics: [android.net.IIpConnectivityMetrics]
    43. 41 consumer_ir: [android.hardware.IConsumerIrService]
    44. 42 content: [android.content.IContentService]
    45. 43 country_detector: [android.location.ICountryDetector]
    46. 44 cpuinfo: []
    47. 45 crossprofileapps: [android.content.pm.ICrossProfileApps]
    48. 46 dataloader_manager: [android.content.pm.IDataLoaderManager]
    49. 47 dbinfo: []
    50. 48 device_config: []
    51. 49 device_identifiers: [android.os.IDeviceIdentifiersPolicyService]
    52. 50 device_policy: [android.app.admin.IDevicePolicyManager]
    53. 51 deviceidle: [android.os.IDeviceIdleController]
    54. 52 devicestoragemonitor: []
    55. 53 diskstats: []
    56. 54 display: [android.hardware.display.IDisplayManager]
    57. 55 dnsresolver: []
    58. 56 dreams: [android.service.dreams.IDreamManager]
    59. 57 drm.drmManager: [drm.IDrmManagerService]
    60. 58 dropbox: [com.android.internal.os.IDropBoxManagerService]
    61. 59 dynamic_system: [android.os.image.IDynamicSystemService]
    62. 60 emergency_affordance: []
    63. 61 ethernet: [android.net.IEthernetManager]
    64. 62 external_vibrator_service: [android.os.IExternalVibratorService]
    65. 63 face: [android.hardware.face.IFaceService]
    66. 64 file_integrity: [android.security.IFileIntegrityService]
    67. 65 fingerprint: [android.hardware.fingerprint.IFingerprintService]
    68. 66 gfxinfo: []
    69. 67 gpu: [android.graphicsenv.IGpuService]
    70. 68 graphicsstats: [android.view.IGraphicsStats]
    71. 69 hardware_properties: [android.os.IHardwarePropertiesManager]
    72. 70 imms: [com.android.internal.telephony.IMms]
    73. 71 ims_doze_manager: [com.android.ims.internal.IImsDozeManager]
    74. 72 ims_ex: [com.android.ims.internal.IImsServiceEx]
    75. 73 ims_ut_ex: [com.android.ims.internal.IImsUtEx]
    76. 74 incident: []
    77. 75 incidentcompanion: [android.os.IIncidentCompanion]
    78. 76 incremental: [android.os.incremental.IIncrementalService]
    79. 77 input: [android.hardware.input.IInputManager]
    80. 78 input_method: [com.android.internal.view.IInputMethodManager]
    81. 79 inputflinger: [android.input.IInputFlinger]
    82. 80 installd: []
    83. 81 ions: [com.android.internal.telephony.IOns]
    84. 82 ions_ex: [com.android.unisoc.telephony.aidl.IOperatorNameHandler]
    85. 83 iphonesubinfo: [com.android.internal.telephony.IPhoneSubInfo]
    86. 84 ipsec: [android.net.IIpSecService]
    87. 85 irit: [com.android.unisoc.telephony.aidl.IRadioInteractor]
    88. 86 isms: [com.android.internal.telephony.ISms]
    89. 87 ismsEx: [com.android.internal.telephony.ISmsEx]
    90. 88 isub: [com.android.internal.telephony.ISub]
    91. 89 jobscheduler: [android.app.job.IJobScheduler]
    92. 90 launcherapps: [android.content.pm.ILauncherApps]
    93. 91 ledsrv: [android.os.ILedService]
    94. 92 lights: [android.hardware.lights.ILightsManager]
    95. 93 linkturbonative: []
    96. 94 location: [android.location.ILocationManager]
    97. 95 lock_settings: [com.android.internal.widget.ILockSettings]
    98. 96 looper_stats: []
    99. 97 manager: [android.os.IServiceManager]
    100. 98 media.audio_flinger: [android.media.IAudioFlinger]
    101. 99 media.audio_policy: [android.media.IAudioPolicyService]
    102. 100 media.camera: [android.hardware.ICameraService]
    103. 101 media.camera.proxy: [android.hardware.ICameraServiceProxy]
    104. 102 media.extractor: [android.IMediaExtractorService]
    105. 103 media.metrics: [android.media.IMediaMetricsService]
    106. 104 media.player: [android.media.IMediaPlayerService]
    107. 105 media.resource_manager: [android.media.IResourceManagerService]
    108. 106 media_projection: [android.media.projection.IMediaProjectionManager]
    109. 107 media_resource_monitor: [android.media.IMediaResourceMonitor]
    110. 108 media_router: [android.media.IMediaRouterService]
    111. 109 media_session: [android.media.session.ISessionManager]
    112. 110 meminfo: []
    113. 111 midi: [android.media.midi.IMidiManager]
    114. 112 mount: [android.os.storage.IStorageManager]
    115. 113 netd: []
    116. 114 netd_listener: [android.net.metrics.INetdEventListener]
    117. 115 netpolicy: [android.net.INetworkPolicyManager]
    118. 116 netstats: [android.net.INetworkStatsService]
    119. 117 network_adapter: [android.os.INetworkAdapterService]
    120. 118 network_management: [android.os.INetworkManagementService]
    121. 119 network_score: [android.net.INetworkScoreService]
    122. 120 network_stack: [android.net.INetworkStackConnector]
    123. 121 network_time_update_service: []
    124. 122 network_watchlist: [com.android.internal.net.INetworkWatchlistManager]
    125. 123 notification: [android.app.INotificationManager]
    126. 124 oem_lock: [android.service.oemlock.IOemLockService]
    127. 125 otadexopt: [android.content.pm.IOtaDexopt]
    128. 126 overlay: [android.content.om.IOverlayManager]
    129. 127 package: [android.content.pm.IPackageManager]
    130. 128 package_native: [android.content.pm.IPackageManagerNative]
    131. 129 performance_fw: [android.app.IPerformanceManagerInternal]
    132. 130 performancemanager: []
    133. 131 permission: [android.os.IPermissionController]
    134. 132 permissionmgr: [android.permission.IPermissionManager]
    135. 133 persistent_data_block: [android.service.persistentdata.IPersistentDataBlockService]
    136. 134 phone: [com.android.internal.telephony.ITelephony]
    137. 135 pinner: []
    138. 136 platform_compat: [com.android.internal.compat.IPlatformCompat]
    139. 137 platform_compat_native: [com.android.internal.compat.IPlatformCompatNative]
    140. 138 power: [android.os.IPowerManager]
    141. 139 power_ex: [android.os.sprdpower.IPowerManagerEx]
    142. 140 powerguru: [android.app.sprdpower.IPowerGuru]
    143. 141 print: [android.print.IPrintManager]
    144. 142 processinfo: [android.os.IProcessInfoService]
    145. 143 procstats: [com.android.internal.app.procstats.IProcessStats]
    146. 144 recovery: [android.os.IRecoverySystem]
    147. 145 restrictions: [android.content.IRestrictionsManager]
    148. 146 role: [android.app.role.IRoleManager]
    149. 147 rollback: [android.content.rollback.IRollbackManager]
    150. 148 runtime: []
    151. 149 scheduling_policy: [android.os.ISchedulingPolicyService]
    152. 150 search: [android.app.ISearchManager]
    153. 151 sec_key_att_app_id_provider: [android.security.keymaster.IKeyAttestationApplicationIdProvider]
    154. 152 sensor_privacy: [android.hardware.ISensorPrivacyManager]
    155. 153 sensorservice: [android.gui.SensorServer]
    156. 154 serial: [android.hardware.ISerialManager]
    157. 155 servicediscovery: [android.net.nsd.INsdManager]
    158. 156 settings: []
    159. 157 shortcut: [android.content.pm.IShortcutService]
    160. 158 simphonebook: [com.android.internal.telephony.IIccPhoneBookEx]
    161. 159 slice: [android.app.slice.ISliceManager]
    162. 160 soundtrigger: [com.android.internal.app.ISoundTriggerService]
    163. 161 soundtrigger_middleware: [android.media.soundtrigger_middleware.ISoundTriggerMiddlewareService]
    164. 162 sprd_power: []
    165. 163 sprdssense: [android.os.sprdpower.ISSense]
    166. 164 stats: [android.os.IStatsd]
    167. 165 statscompanion: [android.os.IStatsCompanionService]
    168. 166 statsmanager: [android.os.IStatsManagerService]
    169. 167 statusbar: [com.android.internal.statusbar.IStatusBarService]
    170. 168 storaged: [android.os.IStoraged]
    171. 169 storaged_pri: [android.os.storaged.IStoragedPrivate]
    172. 170 storagestats: [android.app.usage.IStorageStatsManager]
    173. 171 suspend_control: []
    174. 172 system_config: [android.os.ISystemConfig]
    175. 173 system_update: [android.os.ISystemUpdateManager]
    176. 174 telecom: [com.android.internal.telecom.ITelecomService]
    177. 175 telephony.registry: [com.android.internal.telephony.ITelephonyRegistry]
    178. 176 telephony_ims: [android.telephony.ims.aidl.IImsRcsController]
    179. 177 testharness: []
    180. 178 tethering: [android.net.ITetheringConnector]
    181. 179 textclassification: [android.service.textclassifier.ITextClassifierService]
    182. 180 textservices: [com.android.internal.textservice.ITextServicesManager]
    183. 181 thermalservice: [android.os.IThermalService]
    184. 182 time_detector: [android.app.timedetector.ITimeDetectorService]
    185. 183 time_zone_detector: [android.app.timezonedetector.ITimeZoneDetectorService]
    186. 184 trust: [android.app.trust.ITrustManager]
    187. 185 uimode: [android.app.IUiModeManager]
    188. 186 uni_carrierconfig: [com.android.internal.telephony.IUniCarrierConfigLoader]
    189. 187 uni_telephony: [com.android.internal.telephony.IUniTelephony]
    190. 188 updatelock: [android.os.IUpdateLock]
    191. 189 uri_grants: [android.app.IUriGrantsManager]
    192. 190 usagestats: [android.app.usage.IUsageStatsManager]
    193. 191 usb: [android.hardware.usb.IUsbManager]
    194. 192 user: [android.os.IUserManager]
    195. 193 vibrator: [android.os.IVibratorService]
    196. 194 voiceinteraction: [com.android.internal.app.IVoiceInteractionManagerService]
    197. 195 vold: []
    198. 196 wallpaper: [android.app.IWallpaperManager]
    199. 197 webviewupdate: [android.webkit.IWebViewUpdateService]
    200. 198 wifi: [android.net.wifi.IWifiManager]
    201. 199 wifinl80211: []
    202. 200 wifip2p: [android.net.wifi.p2p.IWifiP2pManager]
    203. 201 wifiscanner: [android.net.wifi.IWifiScanner]
    204. 202 window: [android.view.IWindowManager]

    上面两条命令的结果相差不大,代表系统中可以通过adb shell dumpsys 服务名 去获取相应信息的命令

    三、常用dumpsys命令

    1. 查看AMS中所有信息

    adb shell dumpsys activity

    2. 查看内存信息

    adb shell dumpsys meminfo

    3. 查看电池信息

    adb shell dumpsys battery

    4. 显示alarm信息

    adb shell dumpsys alarm

    5. 显示内存状态

    adb shell dumpsys procstats

    6. 查看Window的相关信息

    adb shell dumpsys window

    7. 查看CPU相关信息

    adb shell dumpsys cpuinfo

    有些服务还可以带参数,如果不是很清楚细节,就加上 -h  参数

    比如:

    1. home@ubuntu:~$ adb shell dumpsys activity -h
    2. Activity manager dump options:
    3. [-a] [-c] [-p PACKAGE] [-h] [WHAT] ...
    4. WHAT may be one of:
    5. a[ctivities]: activity stack state
    6. r[recents]: recent activities state
    7. b[roadcasts] [PACKAGE_NAME] [history [-s]]: broadcast state
    8. broadcast-stats [PACKAGE_NAME]: aggregated broadcast statistics
    9. i[ntents] [PACKAGE_NAME]: pending intent state
    10. p[rocesses] [PACKAGE_NAME]: process state
    11. o[om]: out of memory management
    12. perm[issions]: URI permission grant state
    13. prov[iders] [COMP_SPEC ...]: content provider state
    14. provider [COMP_SPEC]: provider client-side state
    15. s[ervices] [COMP_SPEC ...]: service state
    16. allowed-associations: current package association restrictions
    17. as[sociations]: tracked app associations
    18. exit-info [PACKAGE_NAME]: historical process exit information
    19. lmk: stats on low memory killer
    20. lru: raw LRU process list
    21. binder-proxies: stats on binder objects and IPCs
    22. settings: currently applied config settings
    23. service [COMP_SPEC]: service client-side state
    24. package [PACKAGE_NAME]: all state related to given package
    25. all: dump all activities
    26. top: dump the top activity
    27. WHAT may also be a COMP_SPEC to dump activities.
    28. COMP_SPEC may be a component name (com.foo/.myApp),
    29. a partial substring in a component name, a
    30. hex object identifier.
    31. -a: include all available server state.
    32. -c: include client state.
    33. -p: limit output to given package.
    34. --checkin: output checkin format, resetting data.
    35. --C: output checkin format, not resetting data.
    36. --proto: output dump in protocol buffer format.
    37. --autofill: dump just the autofill-related state of an activity

    然后对照英文注释,带上不同的参数后,可以获取具体的信息:

    1. adb shell dumpsys activity activities----------查看Activity组件信息
    2. adb shell dumpsys activity services-----------查看Service组件信息
    3. adb shell dumpsys activity providers----------产看ContentProvider组件信息
    4. adb shell dumpsys activity broadcasts--------查看BraodcastReceiver信息
    5. adb shell dumpsys activity intents--------------查看Intent信息
    6. adb shell dumpsys activity processes---------查看进程信息
    7. adb shell dumpsys activity intents -------- 查看 PendingIntentRecord 信息

    四、命令来源

    上述的这些命令都涉及到adb shell 命令,我们看看系统源头类

    1. public abstract class BasicShellCommandHandler {
    2. //两个重要的方法:
    3. public abstract int onCommand(String cmd);
    4. public abstract void onHelp();
    5. ​​​​​​​}

    onCommand方法 就是各个系统服务 adb shell 直接操作的方法:

    比如 AMS 中的  启动一个activity

    adb shell am start  -n com.DeviceTest/com.DeviceTest.VersionTestActivity

    AMS 中的  启动一个service

    adb shell am startservice -n com.tentent.mm/.plugin.acountsync.model.AcountAuthenticatorService
    启动微信

    AMS 中的 停止一个服务

    adb shell am stopservice -n  xxxx

    AMS 发送一个广播:

    adb shell am broadcast  -a   android.intent.action.BOOT_COMPLETED  -n org.mazhuang.boottimemeasure/.BootCompletedReceiver
    表示向 org.mazhuang.boottimemeasure/.BootCompletedReceiver 发送一个 BOOT_COMPLETED 

    各系统服务中的 onShellCommand 方法:

    最终都会复写onCommand 方法,比如AMS 中的 onCommand

    1. public int onCommand(String cmd) {
    2. if (cmd == null) {
    3. return handleDefaultCommands(cmd);
    4. }
    5. final PrintWriter pw = getOutPrintWriter();
    6. try {
    7. switch (cmd) {
    8. case "start":
    9. case "start-activity":
    10. return runStartActivity(pw);
    11. case "startservice":
    12. case "start-service":
    13. return runStartService(pw, false);
    14. case "startforegroundservice":
    15. case "startfgservice":
    16. case "start-foreground-service":
    17. case "start-fg-service":
    18. return runStartService(pw, true);
    19. case "stopservice":
    20. case "stop-service":
    21. return runStopService(pw);
    22. case "broadcast":
    23. return runSendBroadcast(pw);
    24. case "instrument":

    到这里,就知道上述 adb shell  am 命令的是从哪里来的

    我们在看看 PMS 相关的 命令:

    adb shell pm uninstall com.example.MyApp    // 卸载一个应用

    adb shell pm path :   查询package的安装位置

    adb shell pm enable :使package或component可用。(如:pm enable "package/class")

    adb shell pm disable :使package或component不可用。(如:pm disable "package/class")


    adb shell pm list packages   :  查看所有应用列表

    adb shell pm list packages -s   : 查看系统应用(system)列表

    adb shell pm list packages -3  :  查看第三方应用


    adb shell pm clear 包名  :   清除应用数据与缓存

    同理,先进去 PMS中 看看 onShellCommand 方法, 然后最后也会复写 onCommand 方法:

    1. public int onCommand(String cmd) {
    2. if (cmd == null) {
    3. return handleDefaultCommands(cmd);
    4. }
    5. final PrintWriter pw = getOutPrintWriter();
    6. try {
    7. switch (cmd) {
    8. case "path":
    9. return runPath();
    10. case "validate":
    11. return runValidate();
    12. case "dump":
    13. return runDump();
    14. case "list":
    15. return runList();
    16. case "resolve-activity":
    17. return runResolveActivity();
    18. case "query-activities":
    19. return runQueryIntentActivities();
    20. case "query-services":
    21. return runQueryIntentServices();
    22. case "query-receivers":
    23. return runQueryIntentReceivers();
    24. case "install":
    25. return runInstall();

    这样子,就找到命令的出处了,如果想知道原理,就继续去看每个方法中的实现逻辑。

    同理,dumpsys 命令相关的源头,同样也可以查看每个服务中的 onHelp() 方法:

    【备注】:只有部分服务有帮助打印信息

    比如 我们想看看 电池服务(BatteryService)相关的信息,通过adb shell service list

    找到电池的服务名:battery

    adb shell dumpsys  battery -h   查看该命令的帮助信息:

    1. Battery service (battery) commands:
    2. help
    3. Print this help text.
    4. set [-f] [ac|usb|wireless|status|level|temp|present|invalid] <value>
    5. Force a battery property value, freezing battery state.
    6. -f: force a battery change broadcast be sent, prints new sequence.
    7. unplug [-f]
    8. Force battery unplugged, freezing battery state.
    9. -f: force a battery change broadcast be sent, prints new sequence.
    10. reset [-f]
    11. Unfreeze battery state, returning to current hardware values.
    12. -f: force a battery change broadcast be sent, prints new sequence.

    然后我们去找 BatteryService.java 中的 onHelp() 方法,可以找到对应的源码,也就知道该命令的源头了,代码如下:

    1. class Shell extends ShellCommand {
    2. @Override
    3. public int onCommand(String cmd) {
    4. return onShellCommand(this, cmd);
    5. }
    6. @Override
    7. public void onHelp() {
    8. PrintWriter pw = getOutPrintWriter();
    9. dumpHelp(pw);
    10. }
    11. }
    12. static void dumpHelp(PrintWriter pw) {
    13. pw.println("Battery service (battery) commands:");
    14. pw.println(" help");
    15. pw.println(" Print this help text.");
    16. pw.println(" set [-f] [ac|usb|wireless|status|level|temp|present|invalid] ");
    17. pw.println(" Force a battery property value, freezing battery state.");
    18. pw.println(" -f: force a battery change broadcast be sent, prints new sequence.");
    19. pw.println(" unplug [-f]");
    20. pw.println(" Force battery unplugged, freezing battery state.");
    21. pw.println(" -f: force a battery change broadcast be sent, prints new sequence.");
    22. pw.println(" reset [-f]");
    23. pw.println(" Unfreeze battery state, returning to current hardware values.");
    24. pw.println(" -f: force a battery change broadcast be sent, prints new sequence.");
    25. }

  • 相关阅读:
    【数据结构】Map和Set
    2023年亚太杯APMCM数学建模大赛ABC题辅导及组队
    golang--channal与select
    器械团体怎么利用自动化流程缩短工程期限
    C#重启 --- 语言基础
    Java 加载、编辑和保存WPS表格文件(.et/.ett)
    深入理解C++智能指针系列(一)
    Spring Cloud Gateway学习(1)—— Gateway 的基本概念 & 引入依赖需要注意的事项 +解决方案 & 全局网关的入门使用案例
    传输机房的基本结构
    ElasticSearch快速入门
  • 原文地址:https://blog.csdn.net/u012514113/article/details/127669871