본문 바로가기

Android

[android] adb shell 명령어 정리


- App 내부저장소 접근 / 확인

접근 : adb shell cd /data/data/[packageNm]/

확인 : ls -ls


- 현재 Activity PID찾기 

adb shell "dumpsys activity | grep top-activity"



시스템 기본정보: 하드웨어, 커널
cat /proc/version : 커널 버전
cat /proc/cpuinfo : 프로세서 정보, CPU타입, 모델 제조사
cat /porc/meminfo : 메모리 정보, 실제 메모리 가상 메모리
cat /proc/devices : 현재 커널에 설정되어 있는 장치 목록
mount : 마운트된 모든 장치 정보
df : 하드디스크 사용량
cat /porc/filesystems : 커널에 설정되어 있는 파일시스템 목록
cat /proc/swaps : 스왑 파티션의 크기와 사용량
cat /proc/interrupts : 장치가 사용중인 인터럽트(IRQ)목록 표시
cat /proc/ioports : 현재 사용중인 input/output 포트
cat /proc/partitions : 파티션 정보
cat /proc/uptime : 시스템이 얼마나 살아있었는지
cat /proc/stat : 시스템 상태에 관한 다양한 정보, CPU 사용 통계, 부팅이후 page fault 발생 횟수
cat /proc/zoneinfo : ZONEINFO
dmesg : 시스템 부팅시 나왔던 메시지
ps : 실행중인 프로세스 정보
ps -p - t : 프로세스와 쓰레드 목록
set or printenv : 환경설정값 출력


시스템 리소스 사용 현황
vmstat : 시스템 리소스 상황 모니터, CPU, I/O, Memory
cat /proc/diskstats : 디스크 utilization throughput. 디스크 I/O현황
top : 시스템 프로세스 상황 모니터링/ 프로세스별 CPU사용량, 메모리와 스왑 사용량
procrank : 프로세스별 메모리
dumpsys meminfo [PID] : 해당 프로세스 메모리 상세 정보
cat /proc/[PID]/stat : 해당 프로세스에 대한 정보, 시작시간, 상태, CPU 사용량
cat /proc/[PID]/maps : 해당 프로세스의 메모리 정보
cat /proc/vmstat : 버추얼 메모리 통계?
librank : 라이브러리별 메모리 사용량?


네트워크 관련
cat /proc/net/netlink : 네트워크 정보
netcfg : 네트워크 인터페이스와 IP주소 목록
netstat : 네트워크 연결상태 확인
nc : 네트워크용 cat 명령어(netcat)
ifconfig : 네트워크 인터페이스 설정 정보. 장치명을 파라미터로 받음. ip 주소. 서브넷마스크
tcpdump : 실시간 패킷 모니터링
iftop : 네트워크를 위한 top
route : 해당 호스트까지 연결하는 중간 경로 정보인 라우팅 테이블 표시
ping : 원격 호스트와의 연결 테스트
cat /proc/net/route : Route


안드로이드 제공
logcat : 로그캣 보기
pm : package manager 약자. 패키지/permission/instrumentation/feature 목록, 패키지 설치/제거
am : activity manager 약자, 액티비티 시작, Intent 브로드캐스팅, Instrumentation 시작, profiling 시작 / 중지
service : 안드로이드 서비스 목록 표시, 서비스에 명령 전달
monkey : 애플리케이션에 랜덤 이벤트 발생시킴. 사용자 이벤트, 시스템 이벤트의 무작위 발행
cat /data/anr/traces.txt : VM TRACES (쓰레드 덤프)
cat /proc/binder/proc/[PID] : 바인더 프로세스 상태
cat /proc/binder/xxx : 바인더 관련 정보(xxx transaction, transaction_log, failed_transaction_log, stats )
cat /data/system/packages.xml : 설치된 패키지 세팅 정보
setprop : system property 셋팅
getprop : 셋팅된 system property 목록 출력


종합 리포트
dumpsys [service] : app/service 상태정보 덤프, 서비스별로 추가 파라미터 받을 있음
dumpstate : device 상태정보 덤프. 상태정보를 추출하는 여러 명령어들의 조합으로 구성
dumpcrash : 애플리케이션이 crach 때의 상태정보 덤프
bugreport : logcat + dumpsys + dumpstat

밖에...
밖의 안드로이드 shell 명령어는 /system/bin /system/xbin 뒤져보면 많이 나온다. 이제 남은 일은 찾아낸 명령어의 사용법, 출력결과를 어떻게 해석할지, 어떤 상황에서 이들을 활용할지 사례조사, 그리고 직접 활용해보는 것이다.

-----------------------------------------------------------------

ADB (Android Debug Bridge)




ADB 구성


client : development machine에서 동작. shell에서 client invocation해서 command 실행 가능. ADT, DDMS client


server : development machine background process이며, client daemon 사이에서의 communication 관리


daemon : 각각의 device에서 동작하는 background process


client 시작하면, ADB server 실행중인지 확인해서 실행중이지 않으면 server 시작시킨다. server 시작되면 TCP 5037 port binding해서 adb client로부터의 command 받아들이는데, 모든 client server communication 하기 위해 5037 port 사용.


실행파일 : ${android_sdk_path}/tools/adb




Command 입력 형식


adb [-d | -e | -s <serialNumber>] <command>


adb devices : adb server 연결된 device 보여준다.

형식 : [serialNumber] [state]


serialNumber = type-consolePort (ex : emulator-5554)

state = offline (device adb 아직 연결되지 않았거나 응답이 없는 상태)

            device (adb server 연결된 상태)


-s option : 여러개의 device 연결되어 있는 상태에서 특정 device command 입력하고 싶을 사용

형식

adb -s <serialNumber> <command>


ex : adb -s emulator-5554 install sample.apk




Port forwarding


device port forwarding 하고 싶을 사용하는 명령

ex : port 6100에서 7100으로 forwarding 하고 싶은 경우

adb forward tcp:6100 tcp:7100




Copying files


pull

device에서 file copy 사용 (recursively)

adb pull <remote> <local>


push

pull 반대로 file device 복사해 넣고 싶을 사용 (recursively)

adb push <local> <remote>

ex : adb push foo.txt /sdcard/foo.txt) (/sdcard/foo.txt 에서 /sdcard device 존재하는 path




Listing of adb Commands


Options


-d

연결된 USB device에만 direct command 전송

USB device 하나 이상이면 error return

-e

실행중인 device에만 command 전송

하나 이상의 device 실행중이면 error return


-s <serialNumber>

특정 device에만 command 전송 (serialNumber 형식은 내용 참조)



General


devices

연결된 모든 device list 출력


help

제공되는 adb command list 출력


version

adb version 출력



Debug


logcat [<option>] [<filter-specs>]

log data 출력


bugreport

bugreport 위한 dumpsys(system data dump), dumpstate(state dump), logcat data 출력


jdwp

특정 device JDWP process들의 list(pid) 출력



Data

install <path-to-apk>

application 설치


pull <remote> <local>

push <local> <remote>

내용 참조



Ports and Networking

forward <local> <remote>

특정 local port remote port fowarding

Scheme

tcp:<portnum>

local:<UNIX domain socket name>

dev:<character device name>

jdwp:<pid>


ppp <tty> [parm]...

USB PPP 실행

<tty> : PPP stream 위한 tty

[parm] : PPP option



Scripting


get-serialno

adb serial number 출력


get-state

adb state 출력 (device / offline)


wait-for-device

device online 까지 command 실행 정지


ex : adb wait-for-device shell getprop (state device 되면 shell getprop command 실행


Note : 완전히 boot 이후에 사용 가능한 install 등의 command 함께 사용할 경우 wait-for-device device state 확인하므로 fully boot 되지 않았을 경우 error 발생 가능



Server

start-server

adb server 실행중인지 확인해서 running 상태가 아니면 실행


kill-server

adb server process 종료



Shell


shell

target device 안에서 remote shell 시작


shell [<shellCommand>]

target device 안에서 shell command 실행하고 remote shell 빠져나간다




Shell Command 실행


ADB ash shell 제공하는데, ash shell 실행 바이너리는 device 내부의 /system/bin 경로에 존재


adb [-d | -e | -s <serialNumber>] shell remote shell 실행한 후에 shell 종료하고 싶으면 Ctrl + D or exit 입력



UI/Application Exerciser Monkey


User event random stream 생성해 device에서 실행시키는 tool (stress test 용도)

ex : adb shell monkey -v -p packageName 500 (500가지의 random stream)




Other Shell Commands

device /system/bin 경로의 file들을 살펴보거나, adb -help 확인


dumpsys

system data dump 표시


dumpstate

state dump file 저장


logcat [<option>]...[<filter-spec>]

logging 가능하게 하거나 화면에 표시


dmesg

kernel debugging message 화면에 출력


start

device 시작(재시작)


stop

device 종료




Using logcat Commands

adb logcat

단순히 전체 log 보고싶은 경우 사용 또는 remote shell에서 logcat 실행


Android에서 모든 log message tag priority 가지고 있음

tag : system component 짧은 문자열로 표현 (ex : view system 경우 "View")

priority (ordered from lowest to highest)

V (Verbose)

D (Debug)

I (Info)

W (Warning)

E (Error)

F (Fatal)

S (Silent)


logcat 실행 후에 나타나는 log message에서 tag priority column priority/tag 형태로 표시

ex : I/ActivityManager( 585) : Starting activity : Intent { action = android.intent.action... }


logcat filter tag:priority 형태로 표현하는데 tag 표시하려는 tag 이름을 입력하면 되고 입력한 priority 상위 priority log들을 표시


ex : adb logcat ActivityManager:I MyApp:D *:S

ActivityManager I(Info) level 이상, MyApp D(Debug) level 이상의 log들만 표시



default filter expression

환경변수 ANDROID_LOG_TAGS default 사용할 filter expression setting 후에 export 해서 사용


ex : export ANDROID_LOG_TAGS="ActivityManager:I MyApp:D *:S"


Note : remote shell 사용해서 logcat 실행하고 있을 경우 export 되지 않음 (?)



Controlling Log Output Format


-v option 사용해서 출력되는 log f