開發(fā)調(diào)試常用命令列表
記錄并收集一些開發(fā)調(diào)試過程中有效的命令峻凫,主要是Android相關补胚。
1 通用命令
1.1 iptables
- 限制上行和下行速率
可用于模擬弱網(wǎng)模式
iptables -I OUTPUT 1 -m hashlimit --hashlimit-name testup --hashlimit-above 5/s -j DROP
iptables -I INPUT 1 -m hashlimit --hashlimit-name testdown --hashlimit-above 5/s -j DROP
- 設置丟包率
iptables -I INPUT 1 -m statistic --mode random --probability 0.8 -j DROP
- 查看所有規(guī)則
iptables --list
- 清空所有規(guī)則
iptables -F
1.2 find
- 查找大文件
busybox find [PATH] -size +10000k
- 按條件查找并執(zhí)行命令
find /root -size -5557c -size +5555c -exec ls -ld {} \;
- 結(jié)合xargs
find . -name "*.java" | xargs grep -HRn 'AudioFormat.ENCODING_PCM_16BIT'
- 查看指定目錄層級
busybox find /sdcard/test/ -maxdepth 4
- notepad lazzy pattern
用lazy pattern模式正則表達式搜索,"text":".?"*
1.3 mount
- 掛載虛擬機虛擬文件系統(tǒng)
sudo mount -t vboxsf share /mnt
- 掛載ci文件系統(tǒng)(for ubuntu)
mount.cifs -o username=username /////////////////path to ci package
1.4 計算器
echo | awk '{print (126+39+35+18+12+12+11+11+0.1)/4}'
1.5 sed
分析日志
// 查找識別結(jié)果
sed -n 's/.*,\("text":"[^"]*"\),.*/\1/p' log.txt
sed -n 's/.*SrSolution: ISS_SR_MSG_RESULT.*,\("text":"[^"]*"\),.*/\1/p' log.txt
2.adb命令
2.1 包管理相關
- 查看應用包名信息
dumpsys package com.test.test
- 根據(jù)組件名稱打開對應的Activity
// 打開Android原生設置頁面
am start -n com.android.settings/.Settings
- 打開指定uri對應的Activity
am start "intent:#Intent;action=android.media.action.MEDIA_PLAY_FROM_SEARCH;\
S.android.intent.extra.focus=vnd.android.cursor.item%2Fartist;\
S.android.intent.extra.artist=Adele;end"
am start "intent:#Intent;action=android.intent.action.VIEW;\
category=android.intent.category.BROWSABLE;launchFlags=0x10200000;\
component=com.retton.h5/.WebActivity;\
S.title=%E6%97%A5%E6%9C%9F%2F%E6%97%B6%E9%97%B4;\
S.url=http%3A%2F%2Fwww.pingwest.com;end"
- startService
am startservice -a com.test.VoiceKey -e StartFrom HomeVoiceButton
- 外部調(diào)用
adb shell "am startservice -a com.test.dictionary -e param test -e data '"'[{"group":"","location":{"city":"合肥","country":"中國","province":"安徽"},"name":"張小三","phoneNumber":"13019001001","teleOper":"聯(lián)通","type":"工作"}]'"'"
adb shell "am startservice ... '"'JSON_TEXT'"'"
2.2 logcat
- 過濾指定文本
查看識別結(jié)果
adb logcat | sed -n 's/.*intent.*,"text":"\([^"]*\)",.*/\1/p'
- 常用
logcat | busybox grep `ps | busybox grep "com.package.name$" | busybox awk -F " " '{print $2}'`
logcat | busybox grep `ps | busybox grep "com.package.name$" | busybox awk 'NR%2==1{T=$2;next}{print T"|"$2}'
logcat | grep -E `ps |grep account| busybox awk 'NR%2==1{T=$2;next}{printf T"|"$2}'`
adb logcat -vtime -d > ~/Desktop/dump.log
2.3 網(wǎng)絡抓包
tcpdump -i usb0 -s0 -w /data/local/tmp/`date "+%Y%m%d%H%M%S"`.pcap
tcpdump -i usb0 -s0 -vv tcp port 80 and host hostname
2.4 monkey命令
monkey -p com.package.name --pct-syskeys 0 100000
2.5 系統(tǒng)命令
- wifi控制
svc wifi [enable|disable]
- 數(shù)據(jù)流量控制
svc data [enable|disable]
- 按鍵注入
input keyevent [--longpress] <key code number or name>
input keyevent HOME
input keyevent BACK
2.6 系統(tǒng)分析
- 抓取CPU占用信息
echo 'H' | busybox top -d 1 | busybox tee /sdcard/top_`date "+%Y%m%d%H%M%S"`.log
- 生成native trace
執(zhí)行之后在/data/tombstones中生成native棧信息
debuggerd -b [pid]
- 生成java虛擬機棧信息
在/data/anr/traces.txt中生成虛擬機各線程調(diào)用棧信息
kill -3 [pid]
- 獲取視圖樹
uiautomator dump /sdcard/dump.xml
- 獲取CPU主頻信息
cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq
- 查看虛擬機OOM ADJ
cat /proc/PID/oom_adj
3.簽名工具
3.1 列出密鑰庫中的條目
keytool -list -v -keystore debug.keystore
3.2 打印APK解壓出來的證書內(nèi)容
通過此方法能查看APK的簽名信息篡腌,包括SHA1等疟呐。
keytool -printcert -file META-INF/CERT.RSA
3.3 使用keystore給unsigned apk簽名
jarsigner -verbose -keystore [keystorePath] -signedjar [apkOut] [apkIn] [alias]
3.4 驗證APK的簽名狀態(tài)
jarsigner -verbose -verify -certs [apk]
3.5 查看APK信息
aapt dump badging {PATH}.apk
3.6 系統(tǒng)簽名導入keystore
將一般ROM的系統(tǒng)簽名(pk8私鑰和pem公鑰)導入到keystore中方便在ide或ci中自動化構(gòu)建译柏,步驟如下:
- pk8私鑰轉(zhuǎn)成pem格式
openssl pkcs8 -inform DER -nocrypt -in platform.pk8 -out key.pem
- 導出p12證書
openssl pkcs12 -export -in platform.x509.pem -inkey key.pem -out platform.p12 -password pass:test -name test
- 導入keystore
keytool -importkeystore -deststorepass test -destkeystore test.jks -srckeystore platform.p12 -srcstoretype PKCS12 -srcstorepass test
- 檢證keystore簽名列表
keytool -list -v -keystore test.jks
4.git
4.1 gitbash
Windows系統(tǒng)通過gitbash默認push文件到android系統(tǒng)時會出現(xiàn)文件系統(tǒng)解析錯誤灸撰,導致找不到目標路徑挪捕,需在目標系統(tǒng)路徑前增加“/”:
adb push myfile //{path}/myfile
4.2 git
#刪除對文件的跟蹤粗梭,會修改commit id,只在本地使用
git filter-branch --tree-filter 'rm -f testme.txt' HEAD
忽略merge日志
git log --no-merges
查看從v2.6.12開始include/scsi和drivers/scsi目錄中的改動的日志
git log v2.6.12.. include/scsi drivers/scsi
導出兩周前開始的修改記錄到文件gitk级零,“--”是為了避免和名為gitk的分支沖突
git log --since="2 weeks ago" -- gitk
查看test分支有但release分支沒有的提交記錄断医,并顯示提交記錄修改的路徑信息
git log --name-status release..test
查看指定文件的修改記錄,包含該文件被重命名等操作前的修改記錄
git log --follow builtin/rev-list.c
本地有而遠端沒有的提交記錄
git log --branches --not --remotes=origin
本地master分支有而遠端master分支沒有的提交記錄
git log master --not --remotes=*/master
git log -p -m --first-parent
Shows the history including change diffs, but only from the “main branch” perspective, skipping commits that come from merged branches, and showing full diffs of changes introduced by the merges. This makes sense only when following a strict policy of merging all topic branches when staying on a single integration branch.
git log -L '/int main/',/^}/:main.c
Shows how the function main() in the file main.c evolved over time.