2014年5月19日 星期一

iOS -- 使用 Crash Report 進行除錯

當 APP 發佈之後,若使用者發現此APP會當掉,該如何回報給開發商,而開發 APP的人又該如何處理這些問題呢?以下針對上述問題的解決方式作一整理。

1. APP Crash Report
當 APP 發生問題導致Crash,iOS 會將各個 APP 的 Crash Report 統一儲存,路徑為 /private/var/mobile/Library/Logs/CrashReporter/



2. 使用者主動回報 APP 問題的方法
a. 回報問題給 APP Store,開發者可以使用 iTunes Connect 檢視問題。
iOS設備進行以下設定,便會自動回報 APP Crash Report
設定 -> 一般 -> 關於本機 -> 診斷與用量 -> 自動傳送
詳細設定方式可參考這裡,MAC設定Windows設定
b. 回報問題給APP開發商,參考 chromium retrieving-crash-reports-on-ios
  1. Start by opening up the Settings app.
  2. Navigate to General -> About -> Diagnostics & Usage -> Diagnostic & Usage Data.
  3. Select a APPNAME crash from the list. This will start with “APPNAME_” and contain the timestamp of the crash.
  4. Tap on the crash and you will see a text field with a crash log. Long press to Select All and then Copy the crash text.
  5. Paste it into something you can get off of your device (for example, an email to yourself).

3. APP自行記錄所發現的Crash Report,並送出Crash Report給開發商
使用3rd party提供的函式庫。例如:plcrashreporter

4. 使用 xcode 載入 crash report, 確認問題
If you get crash logs off a device through Xcode's Organizer window, then they will be symbolicated for you automatically after a few seconds. Otherwise you will need to symbolicate the .crash file yourself by importing it to the Xcode Organizer. Open the Xcode Organizer, select the “Devices” tab, select “Device Logs” under “LIBRARY” on the top of the sidebar, click the "Import" button and select the .crash file.
 
5. 若 Crash Report 內的函數名稱無法正確顯示,則需要作 Symbolicating
Symbolication定義 
- resolving stack trace addresses to source code methods and lines  
- requires the application binary that was uploaded to the App Store and the .dSYM file that was generated when that binary was built. This must be an exact match - otherwise, the report cannot be fully symbolicated. It is essential that you keep each build distributed to users (regardless of the details of that distribution) with its .dSYM file. 
You must keep both the application binary and the .dSYM file in order to be able to fully symbolicate crash reports.  
Given a crash report, the matching binary, and its .dSYM file, all you need to do for symbolication is to add the crash report to the Xcode Organizer.

建立 Symbolication,摘錄 stackoverflow symbolicating-iphone-app-crash-reports,步驟如下
Step 1: 
Create a folder in desktop, I give name it to "CrashReport" and put three files ("MYApp.app", "MyApp.app.dSYM", "MYApp_2013-07-18.crash") in it. 
Step 2: 
Open Finder and go to Applications, where you will find the Xcode application, right click on this and Click "Show Package Contents" , after this follow this simple path 
"Contents->Developer->Platforms->iPhoneOS.platform->Developer->Library->PrivateFrameworks->DTDeviceKit.framework->Versions->A->Resources" 
OR 
"Contents->Developer->Platforms->iPhoneOS.platform->Developer->Library->PrivateFrameworks->DTDeviceKitBase.framework->Versions->A->Resources" 
Where you find "symbolicatecrash" file , copy this and paste it to "CrashReport" folder. 
Step 3: launch the terminal, run these 3 Command
cd /Users/mac38/Desktop/CrashLog and press Enter button
export DEVELOPER_DIR="/Applications/XCode.app/Contents/Developer" and press Enter
./symbolicatecrash -A -v MYApp_2013-07-18.crash MyApp.app.dSYM and press Enter Now its Done.   

以下是一個Crash Report的簡單範例
完整的 Crash Report 可以參考這裡 

名詞解釋:
build UUID
1. 每次 build image 都會產生一個對應的 build UUID 
2. 可以使用 dwarfdump 取得 build UUID
   xcrun dwarfdump --uuid  
3. 會針對不同 CPU Type 產生不同的 build UUID,如下:
   UUID: 270A9B9D-7A33-3A4A-9F1A-AF8186F81394 (armv7) Example.app/Example
   UUID: 7711EC60-C0B2-3608-A539-182C77AE01ED (armv7s) Example.app/Example   
       
參考資料
  1. Debugging Deployed iOS Apps   
  2. Understanding and Analyzing iOS Application Crash Reports
  3. How to reproduce bugs reported against App Store submissions
  4. How to Match a Crash Report to a Build
  5. Understanding Crash Reports on iPhone OS WWDC 2010 Session
  6. Advanced Memory Analysis with Instruments   
  7. 如何取得 iPhone 某個 process crash log 並且寄給某個 email
  8. Symbolicating iPhone App Crash Reports