一般在平时工作中,基本上很多代码可以在eclipse+ndk进行调试,但如果需要用到具体的硬件设备,如媒体播放设备无法模拟的情况下,只能上硬件(盒子或手机)上进行调试。此时唯一的调试手段就是logcat产生log信息进行分析问题了。
什么时候会有Log文件的产生 ?一般在如下几种情况会产生log文件 。
1、程序异常退出 uncaused exception
2、程序强制关闭 Force Closed (简称FC)
3、程序无响应 Application No Response(简称ANR),一般主线程超过5秒么有处理就会ANR
4、手动生成
进入控制台输入:logcat命令即可进行输出
第一部分
1、分析工具介绍
a、cat /proc/meminfo 显示基本的内存信息
------ MEMORY INFO (/proc/meminfo) ------
MemTotal: 285184 kB
MemFree: 106360 kB
Buffers: 0 kB
Cached: 60036 kB
SwapCached: 0 kB
Active: 98160 kB
Inactive: 49100 kB
Active(anon): 87260 kB
Inactive(anon): 288 kB
Active(file): 10900 kB
Inactive(file): 48812 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 87240 kB
Mapped: 26500 kB
Shmem: 324 kB
Slab: 13340 kB
SReclaimable: 1672 kB
SUnreclaim: 11668 kB
KernelStack: 2160 kB
PageTables: 5600 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 142592 kB
Committed_AS: 1065600 kB
VmallocTotal: 417792 kB
VmallocUsed: 137700 kB
VmallocChunk: 254980 kB
重点关注这下面几个值:
MemTotal: 285184 kB //总计物理内存的大小
MemFree: 106360 kB //可用内存有多少
Buffers: 0 kB //磁盘缓存内存的大小
Cached: 60036 kB
# free
free
total used free shared buffers
Mem: 285184 178884 106300 0 0
Swap: 0 0 0
Total: 285184 178884 106300
在linux中有这么一种思想,内存不用白不用,因此它尽可能的cache和buffer一些数据,以方便下次使用。
但实际上这些内存也是可以立刻拿来使用的。
所以空闲内存=free+buffers+cached=total-used
还有几个命令可使用:
/proc/meminfo 机器的内存使用信息
/proc/pid/maps pid为进程号,显示当前进程所占用的虚拟地址。
/proc/pid/statm 进程所占用的内存
b、查看进程信息
------ CPU INFO (top -n 1 -d 1 -m 30 -t) ------
能够实时显示系统中各个进程的资源占用状况,类似于 Windows 的任务管理器
c、android提供的一些操作工具
------ PROCRANK (procrank) ------
------ PROCMEM (procmem) ------
------ SHOWMAP (showmap) ------
... 就不一一列举了,有兴趣的朋友可以去看看
这此工具的代码位于android的 /system/extras
d、虚拟内存的查看工具
------ VIRTUAL MEMORY STATS (/proc/vmstat) ------
------ VMALLOC INFO (/proc/vmallocinfo) ------
2、时间信息,也是我们主要分析的信息
格式如下:
------ SYSTEM LOG (logcat -b system -v time -d *:v) ------
$:logcat -b system -v time -d *:v
01-02 08:00:02.570 I/SystemServer( 957): Notification Manager
01-02 08:00:02.570 I/SystemServer( 957): Device Storage Monitor
01-02 08:00:02.580 I/SystemServer( 957): Location Manager
01-02 08:00:02.580 I/SystemServer( 957): Search Service
01-02 08:00:02.590 I/SystemServer( 957): DropBox Service
01-02 08:00:02.590 I/SystemServer( 957): Wallpaper Service
3、虚拟机信息,包括进程的,线程的跟踪信息,这是用来跟踪进程和线程具体点的好地方 。
------ VM TRACES JUST NOW (/data/anr/traces.txt.bugreport: 2011-01-15 16:49:02) ------
------ VM TRACES AT LAST ANR (/data/anr/traces.txt: 2011-01-15 16:49:02) ------
格式如下 :
----- pid 1516 at 1970-01-02 08:03:07 -----
Cmd line: com.ipanel.join.appstore
DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0 hwl=0 hwll=0)
"main" prio=5 tid=1 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x4001f188 self=0xd028
| sysTid=1516 nice=0 sched=3/0 cgrp=[fopen-error:2] handle=-1345017744
第二部分
如何分析log信息
1、查找错误信息的关键字眼
"error" "failxx" "E/" 等的错误信息
将这些问题先行解决掉
2、动态库死机
查看类似的“Build fingerprint:”这些关键字
I/DEBUG ( 692): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
I/DEBUG ( 692): Build fingerprint: 'generic/generic/generic:2.3.1/GRH78/eng.userdev-rd6-input.20120221.113348:eng/test-keys'
I/DEBUG ( 692): pid: 694, tid: 694 >>> /system/bin/mediaserver <<<
I/DEBUG ( 692): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000input module init -->
010
对于这此信息,可以查看动态库的分析:
http://blog.csdn.net/andyhuabing/article/details/7074979
3、解决java抛异常的问题解决
E/UsbObserver( 957): java.lang.NullPointerException
E/UsbObserver( 957): at com.android.server.UsbObserver.init(UsbObserver.java:131)
E/UsbObserver( 957): at com.android.server.UsbObserver.<init>(UsbObserver.java:65)
E/UsbObserver( 957): at com.android.server.ServerThread.run(SystemServer.java:419)
I/SystemServer( 957): UI Mode Manager Service
这个直接找到java代码,分析其实现即可解决
4、ANR问题
搜索“ANR”关键词,快速定位到关键事件信息 。
定位到关键的事件信息如下:
I/dalvikvm( 1014): Wrote stack traces to '/data/anr/traces.txt'
I/Process ( 957): Sending signal. PID: 1124 SIG: 9
E/ActivityManager( 957): ANR in com.ipanel.join.appstore
E/ActivityManager( 957): Reason: Broadcast of Intent { act=android.appwidget.action.APPWIDGET_UPDATE cmp=com.ipanel.join.appstore/.widget.SmallWidget1 (has extras) }
E/ActivityManager( 957): Load: 1.57 / 0.38 / 0.13
E/ActivityManager( 957): CPU usage from 6290ms to 0ms ago:
E/ActivityManager( 957): 70% 957/system_server: 1.1% user + 69% kernel / faults: 204 minor
E/ActivityManager( 957): 8.7% 1014/com.ipanel.join.home.jelly: 7.7% user + 0.9% kernel / faults: 899 minor
E/ActivityManager( 957): 0.3% 687/yaffs-bg-1: 0% user + 0.3% kernel
E/ActivityManager( 957): 0.3% 688/yaffs-bg-1: 0% user + 0.3% kernel
E/ActivityManager( 957): 0.3% 935/irkeyservice: 0% user + 0.3% kernel
E/ActivityManager( 957): 0% 890/galcore daemon : 0% user + 0% kernel
E/ActivityManager( 957): 0.1% 1076/com.ipanel.join.browser: 0.1% user + 0% kernel
E/ActivityManager( 957): 0.1% 1124/com.ipanel.join.appstore: 0.1% user + 0% kernel
E/ActivityManager( 957): 75% TOTAL: 4.7% user + 70% kernel
E/ActivityManager( 957): CPU usage from 622ms to 1169ms later:
E/ActivityManager( 957): 71% 957/system_server: 0% user + 71% kernel / faults: 2 minor
E/ActivityManager( 957): 67% 965/SurfaceFlinger: 0% user + 67% kernel
E/ActivityManager( 957): 1.9% 973/ActivityManager: 1.9% user + 0% kernel
E/ActivityManager( 957): 1.9% 991/CursorMove: 0% user + 1.9% kernel
E/ActivityManager( 957): 7.6% 1014/com.ipanel.join.home.jelly: 7.6% user + 0% kernel / faults: 31 minor
E/ActivityManager( 957): 5.7% 1119/Thread-13: 5.7% user + 0% kernel
E/ActivityManager( 957): 1.9% 1123/Thread-13: 1.9% user + 0% kernel
E/ActivityManager( 957): 76% TOTAL: 1.8% user + 72% kernel + 1.8% softirq
指定哪个java包出问题
E/ActivityManager( 957): ANR in com.ipanel.join.appstore
进程号为957发生了如下错误:com.ipanel.join.appstore 包下面 Broadcast问题
ANR原因:
E/ActivityManager( 957): Reason: Broadcast of Intent { act=android.appwidget.action.APPWIDGET_UPDATE cmp=com.ipanel.join.appstore/.widget.SmallWidget1 (has extras) }
这是ANR的堆栈调用文件
I/dalvikvm( 1014): Wrote stack traces to '/data/anr/traces.txt'
通过上面的log信息分析,应该是接收一个广播消息时超时了
我们再分析虚拟机信息 ,打开/data/anr/traces.txt,可有通过adb pull /data/anr/traces.txt .
这里每一段都是一个线程 ,当然我们还是看线程号为1的主线程了。通过分析发现关键问题是这样:
搜索“DALVIK THREADS”关键词,快速定位到本应用程序的虚拟机信息日志
----- pid 1516 at 1970-01-02 08:03:07 -----
Cmd line: com.ipanel.join.appstore
DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0 hwl=0 hwll=0)
"main" prio=5 tid=1 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x4001f188 self=0xd028
| sysTid=1516 nice=0 sched=3/0 cgrp=[fopen-error:2] handle=-1345017744
at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357)
at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)
at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
at java.net.Socket.connect(Socket.java:983)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:74)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:298)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1018)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:726)
at com.ipanel.join.appstore.http.DefaultHttpRequest.doGet(DefaultHttpRequest.java:35)
at com.ipanel.join.appstore.widget.AbsSmallWidget.requestApps(AbsSmallWidget.java:117)
at com.ipanel.join.appstore.widget.AbsSmallWidget.getRemoteViews(AbsSmallWidget.java:56)
其实从这句话:
at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
基本上确认是 socket ->connect 连接超时了,导致主线程5s内没有响应从而产生ANR错误。默认的connect连接timeout时间是75s
其实解决办法就是利用非阻塞方式进行连接即可。
从CPU占用率上也可以看出是在kernel中执行堵塞住了
E/ActivityManager( 957): 75% TOTAL: 4.7% user + 70% kernel
5、执行DexOpt错误
W/dalvikvm( 1803): DexOpt: --- END 'SettingsProvider.apk' --- status=0x000a, process failed
E/dalvikvm( 1803): Unable to extract+optimize DEX from '/system/app/SettingsProvider.apk'
E/SystemServer( 1803): Failure starting Input Manager Service
E/SystemServer( 1803): java.lang.RuntimeException: Unable to get provider com.android.providers.settings.SettingsProvider: java.lang.ClassNotFoundException: com.android.providers.settings.SettingsProvider in loader dalvik.system.PathClassLoader[/system/app/SettingsProvider.apk]
E/SystemServer( 1803): at android.app.ActivityThread.installProvider(ActivityThread.java:3557)
E/SystemServer( 1803): at android.app.ActivityThread.getProvider(ActivityThread.java:3356)
从上面的打印看,是在解压或优化extract+optimize DEX的apk文件时出错了
1、没有出现magic number错误,这个原因与原子操作无关(这是一快速的加锁和解锁的轻量级操作函数)
2、执行dexopt出错
查明是服务器硬盘没空间了,导致引导文件系统的时候没有空间进行解压而失败
6、系统启动后默认其妙或随机死机情况
出现这种错误:
12-01 08:11:56.027: WARN/SharedBufferStack(312): waitForCondition(LockCondition) timed out (identity=19, status=0). CPU may be pegged. trying again.
12-01 08:11:57.315: WARN/SharedBufferStack(312): waitForCondition(LockCondition) timed out (identity=19, status=0). CPU may be pegged. trying again.
12-01 08:11:59.318: WARN/SharedBufferStack(312): waitForCondition(LockCondition) timed out (identity=19, status=0). CPU may be pegged. trying again.
12-01 08:12:03.332: WARN/SharedBufferStack(312): waitForCondition(LockCondition) timed out (identity=19, status=0). CPU may be pegged. trying again.
12-01 08:12:05.329: WARN/SharedBufferStack(312): waitForCondition(LockCondition) timed out (identity=19, status=0). CPU may be pegged. trying again.
12-01 08:12:07.216: WARN/KeyCharacterMap(312): No keyboard for id 0
12-01 08:12:07.216: WARN/KeyCharacterMap(312): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
查看内存: cat /proc/meminfo 发现空闲内存只剩下几M空间了。
请加大android系统的内存即可解决问题