基於Android 7.0的原始碼分析
一.概述
前面介紹過AMS相關的一些資料結構,每個資料結構有大量的成員變數,為了查詢當前手機執行時狀態的 變化值,可以通過dumpsys activity命令來完成,該方法最終是呼叫AMS.dump()方法。
dumpsys命令用法簡要介紹過dumpsys命令 的基本用法,以及系統服務列表資訊,那麼本文重點介紹AMS。 根據dumpsys activity傳遞不同的引數, 對於AMS.dump便會輸出相應的物件資訊。 具體可以跟哪些引數.
1.1 命令格式
dumpsys activity [options] [WHAT]
複製程式碼
其中options為可選項,以-
開頭, 主要有以下幾類:
options | 含義 |
---|---|
-a | 包括所有可用Server狀態 |
-c | 包括Client狀態,即App端情況 |
-p PACKAGE | 限定輸出指定包名 |
1.2 WHAT引數
列舉常見的WHAT引數:
序號 | WHAT | 解釋 | 對應原始碼 |
---|---|---|---|
1 | a[ctivities] | activity狀態 | dumpActivitiesLocked() |
2 | b[roadcasts] [PACKAGE_NAME] | broadcast狀態 | dumpBroadcastsLocked() |
3 | s[ervices] [COMP_SPEC …] | service狀態 | newServiceDumperLocked().dumpLocked |
4 | prov[iders] [COMP_SPEC …] | content provider狀態 | dumpProvidersLocked() |
5 | p[rocesses] [PACKAGE_NAME] | 程式狀態 | dumpProcessesLocked() |
6 | o[om] | 記憶體管理 | dumpOomLocked() |
7 | i[ntents] [PACKAGE_NAME] | pending intent狀態 | dumpPendingIntentsLocked() |
8 | r[ecents] | 最近activity | dumpRecentsLocked() |
9 | perm[issions] | URI授權情況 | dumpPermissionsLocked() |
10 | all | 所有activities資訊 | dumpActivity() |
11 | top | 頂部activity資訊 | dumpActivity() |
12 | package | package相關資訊 | dump() |
其中PACKAGE_NAME是指可跟包名,COMP_SPEC是指可跟具體元件資訊,中括號是指縮寫字母;
二. dumpsys activity
前面介紹dumpsys activity根據後面跟著的不同引數則輸出相應的內容,當不跟任何引數, dumpsys activity
等價於依次輸出下面8條命令:
dumpsys activity intents
dumpsys activity broadcasts //廣播
dumpsys activity providers //provider
dumpsys activity permissions
dumpsys activity services //服務
dumpsys activity recents
dumpsys activity activities //activity
dumpsys activity processes
複製程式碼
依次簡要說明這8條命令:
2.1 intents
//標誌性開頭,dumpPendingIntentsLocked
ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)
複製程式碼
輸出物件:
- PendingIntentRecord
2.2 broadcasts
//標誌性開頭,dumpBroadcastsLocked
ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)
Registered Receivers:
Receiver Resolver Table:
Historical broadcasts [foreground]:
Historical broadcasts summary [foreground]:
Historical broadcasts [background]:
Historical broadcasts summary [background]:
Sticky broadcasts
mHandler
複製程式碼
主要輸出的物件:
- ReceiverList, BroadcastFilter,
- IntentResolver,
- BroadcastQueue, BroadcastRecord
- Handler, Looper
2.3 provider
//標誌性開頭,dumpProvidersLocked
ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)
Published single-user content providers (by class):
Published user [n] content providers (by class):
Single-user authority to provider mappings:
User [n] authority to provider mappings:
複製程式碼
主要輸出的物件:
- ProviderMap
- ContentProviderRecord, ContentProviderConnection
2.4 permissions
//標誌性開頭,dumpPermissionsLocked
ACTIVITY MANAGER URI PERMISSIONS (dumpsys activity permissions)
複製程式碼
主要輸出的物件:
- UriPermission
2.5 Service
//標誌性開頭,newServiceDumperLocked().dumpLocked
ACTIVITY MANAGER SERVICES (dumpsys activity services)
複製程式碼
主要輸出的物件:
- ActiveServices,
- ServiceRecord, ConnectionRecord,ProcessRecord
2.6 recents
//標誌性開頭,dumpRecentsLocked
ACTIVITY MANAGER RECENT TASKS (dumpsys activity recents)
複製程式碼
主要輸出的物件:
- TaskRecord
2.7 activities
//標誌性開頭,dumpActivitiesLocked
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Display #0 (activities from top to bottom):
Stack #0:
Task id #[n]
* Hist #[m]:
Stack #1:
複製程式碼
主要輸出的物件:
- ActivityStackSupervisor, ActivityStack,
- TaskRecord, ActivityRecord
- ActivityThread, Activity
- ViewRootImpl
- Looper, MessageQueue
輸出格式樣例:
//{Hashcode #TaskId Affinity UserId 該task的Activity個數};
TaskRecord{e6d7a8e #156 A=com.gityuan.demo U=0 sz=1}
userId=0 effectiveUid=1000 mCallingUid=1000 mCallingPackage=android
realActivity=com.gityuan.demo/.Blog
//ProcessRecord{Hashcode pid:程式名/uid}
ProcessRecord{7c8a2af 12265:com.gityuan.demo/1000}
複製程式碼
2.8 processes
//標誌性開頭,dumpProcessesLocked
ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)
All known processes:
Isolated process list (sorted by uid):
UID states:
UID validation:
Process LRU list (sorted by oom_adj, 60 total, non-act at 2, non-svc at 2):
PID mappings:
Foreground Processes:
複製程式碼
主要輸出的物件:
- AMS各種程式物件
- ProcessRecord, UidRecord
轉載:http://gityuan.com/2017/07/04/ams_dumpsys/