您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页iOS奔溃日志分析

iOS奔溃日志分析

来源:二三娱乐

iOS奔溃日志分析

前言(扯淡)

iOS奔溃日志能够比较有效的分析奔溃的原因,方便我们debug我们的项目。当然现在有很多线上的这种bug收集和集成的第三方例如:Bugly这种。这次我来说说,怎么样在拿到奔溃日志后发现我们代码中的bug。

需要的装备

Symbolicatecarash

这个是Xcode自带的一个Carsh分析的工具。我们如果想要分析奔溃日志必须要用到它。那么如何找到他呢???

1. 找到Symbolicatecarash文件`
find /Applications/Xcode.app -name symbolicatecrash -type f
你会找到:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/PrivateFrameworks/DTDeviceKitBase.framework/Versions/A/Resources/./symbolicatecrash

2.拷贝到指定目录
cp /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/PrivateFrameworks/DTDeviceKitBase.framework/Versions/A/Resources/symbolicatecrash /Users/angel/Desktop/crash

工程项目编程好的包文件

把项目编译好的Product里面的对于文件拉到刚刚的文件夹下。

dSYM文件

这个是符号化相关的文件 把对应工程包的dSYM文件也拉到刚刚的文件夹下。

Crash文件

真机上的Crash文件导出,可以通过itunes。只要在手机上奔溃过,利用itunes点击同步便可把奔溃日志同步到本地电脑上。在如下路径找到对应时间点的Crash文件,拉到刚刚的目录下。文件路劲~/Library/Logs/CrashReporter/MobileDevice/

自己写的Python批处理文件

对应代码如下

#!/usr/bin/python
#-*- coding: utf-8 -*-
#encoding=utf-8
__author__ = 'zengmingjian'

import os
import sys
import commands

def crashFileValid(excuteFile,dsymFile):
    if len(excuteFile) < 4:
        return
    excuteCmd =  "dwarfdump --uuid " + excuteFile + "/" + excuteFile[0:len(excuteFile) - 4]
    print excuteCmd
    dsymCmd = "dwarfdump --uuid " + dsymFile
    lines1 = os.popen(excuteCmd).readlines()
    lines2 = os.popen(dsymCmd).readlines()
    for line in lines1:
        uuidString1 = line.split(' ')[1]
    for line in lines2:
        uuidString2 = line.split(' ')[1]
    if (uuidString1 == uuidString2):
        print ("uuidString = " + uuidString1)
        return True
    else:
        print ("uuidString1 = " + uuidString1)
        print ("uuidString2 = " + uuidString2)
        print("!!!!!dsym 文件 和 可执行app文件不匹配!!!!!")
        return False

#解析设置环境
def setEnvironment():
    os.environ["DEVELOPER_DIR"]="/Applications/Xcode.app/Contents/Developer/"  #DISPLAY是环境变量名,:0.0是要设置的值
    os.system('$DEVELOPER_DIR')
    
#检测是否存在symbolicatecrash工具,如果不存在,则在xcode拷贝到当前目录
def getSymbolicatecrashTool():
    currentDir = os.getcwd()
    symbolDir = "/Applications/Xcode.app/Contents/SharedFrameworks/DTDeviceKitBase.framework/Versions/A/Resources/symbolicatecrash"
    for root,dirs,files in os.walk(currentDir):
        if os.path.exists(root + "/" + "symbolicatecrash"):
            print("=================")
            setEnvironment()
            return
        else:
            os.system("cp " + symbolDir + " " + currentDir)
            setEnvironment()
            break

#遍历当前目录,解析文件
def parseCrashLog():
    dirList = os.listdir(os.getcwd())
    print dirList
    crashList = []
    dsymName = ""
    appName = ""
    for d in dirList:
        if d.endswith(".crash") or d.endswith(".ips"):
            crashList.append(d)
        if d.endswith(".dSYM"):
            dsymName = d
        if d.endswith(".app"):
            appName = d
    #不存在dsym文件
    if dsymName == "":
        print "error:请将dsym文件考到当前目录!!!!!"
        return
    if appName == "":
        print "error:请将app文件考到当前目录!!!!"
        return

    #检测dsym文件 和app文件是否匹配
    if (crashFileValid(appName, dsymName) == False):
        return

    for crashFile in crashList:
        if crashFile.endswith(".crash"):
            print crashFile
            print dsymName
            os.system("./symbolicatecrash "+ crashFile + " " + dsymName + " >> " + crashFile.replace(".crash",".log"))
        elif crashFile.endswith(".ips"):
            os.system("./symbolicatecrash "+ crashFile + " " + dsymName +" >> " + crashFile.replace(".ips",".log"))

def startPaser():
    getSymbolicatecrashTool()
    parseCrashLog()


if __name__ == '__main__':
    startPaser()

使用方法

把Crash文件对应的包对应的项目文件,dSYM文件和Crash文件放在同一个目录下。然后调用上面的python文件就可以得到一个.log的解析文件。根据解析文件 就可以知道是哪一行代码导致的奔溃。然后就可以修改奔溃。

参考和致谢

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务