1. 程式人生 > >Xcode - 使用 dyld 環境變數進行除錯

Xcode - 使用 dyld 環境變數進行除錯


一、使用 DYLD_PRINT_STATISTICS 獲取啟動時間

arguments裡面加入環境變數 DYLD_PRINT_STATISTICS = 1.

具體設定方法可見文章:ios app獲取main函式之前的啟動時間
https://www.jianshu.com/p/0e91993b2f4a

這裡寫圖片描述

這裡寫圖片描述

那麼是否還有其他方法可以使用呢?
下面列表展示了 dyld 相關變數


二、DYLD_PRINT 變數列表

可以通過 manual page 來訪問
或者在終端中輸入 man dyld

DYLD_FRAMEWORK_PATH
       DYLD_FALLBACK_FRAMEWORK_PATH
       DYLD_VERSIONED_FRAMEWORK_PATH
       DYLD_LIBRARY_PATH
       DYLD_FALLBACK_LIBRARY_PATH
       DYLD_VERSIONED_LIBRARY_PATH
       DYLD_PRINT_TO_FILE
       DYLD_SHARED_REGION
       DYLD_INSERT_LIBRARIES
       DYLD_FORCE_FLAT_NAMESPACE
       DYLD_IMAGE_SUFFIX
       DYLD_PRINT_OPTS
       DYLD_PRINT_ENV
       DYLD_PRINT_LIBRARIES
       DYLD_BIND_AT_LAUNCH
       DYLD_DISABLE_DOFS
       DYLD_PRINT_APIS
       DYLD_PRINT_BINDINGS
       DYLD_PRINT_INITIALIZERS
       DYLD_PRINT_REBASINGS
       DYLD_PRINT_SEGMENTS
       DYLD_PRINT_STATISTICS
       DYLD_PRINT_DOFS
       DYLD_PRINT_RPATHS
       DYLD_SHARED_CACHE_DIR
       DYLD_SHARED_CACHE_DONT_VALIDATE

這裡列舉一下常用的以及描述

Environment variable Description
DYLD_PRINT_LIBRARIES Logs when images are loaded. 映象檔案載入後列印
DYLD_PRINT_LIBRARIES_POST_LAUNCH Logs when images are loaded as a result of a dlopen call. Includes a dynamic libraries’ dependent libraries.
DYLD_PRINT_STATISTICS Logs statistical information on an application’s launch process, such as how many images were loaded, when the application finishes launching.
DYLD_PRINT_INITIALIZERS Logs when the dynamic loader calls initializer and finalizer functions.
DYLD_PRINT_SEGMENTS Logs when the dynamic loader maps a segment of a dynamic library to the current process’s address space.
DYLD_PRINT_BINDINGS Logs when the dynamic loader binds an undefined external symbol with its definition. 動態庫繫結未定義的外部符號檔案。

三、除錯示例


1、DYLD_PRINT_LIBRARIES

列印內容如下(不全),這裡只擷取前面幾個檔案。

dyld: loaded: /Users/administrator/Library/Developer/CoreSimulator/Devices/7E3D7605-368C-4DA1-BE76-1100849590C3/data/Containers/Bundle/Application/C503DC1E-4CE2-4A7E-A283-6406F6A43595/SocketDemo.app/SocketDemo
dyld: loaded: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libBacktraceRecording.dylib
dyld: loaded: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libMainThreadChecker.dylib
dyld: loaded: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib
dyld: loaded: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/Foundation.framework/Foundation
dyld: loaded: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libobjc.A.dylib
dyld: loaded: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libSystem.dylib
dyld: loaded: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
dyld: loaded: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/UIKit.framework/UIKit

可以發現,大都是 dylib 或者可執行檔案。


2、DYLD_PRINT_LIBRARIES_POST_LAUNCH

dyld: loaded: /Users/administrator/Library/Developer/CoreSimulator/Devices/7E3D7605-368C-4DA1-BE76-1100849590C3/data/Containers/Bundle/Application/6C837C0F-CEDE-4215-B2B4-E67BADAB30BE/SocketDemo.app/SocketDemo
dyld: loaded: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libBacktraceRecording.dylib
dyld: loaded: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libMainThreadChecker.dylib
dyld: loaded: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib
dyld: loaded: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/Foundation.framework/Foundation
dyld: loaded: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libobjc.A.dylib
dyld: loaded: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libSystem.dylib
dyld: loaded: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
dyld: loaded: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/UIKit.framework/UIKit

可以發現,幾乎和 DYLD_PRINT_LIBRARIES 一致。


3、DYLD_PRINT_STATISTICS

列印內容如下

Total pre-main time: 1.5 seconds (100.0%)
         dylib loading time: 531.66 milliseconds (33.2%)
        rebase/binding time: 876.81 milliseconds (54.8%)
            ObjC setup time:  60.85 milliseconds (3.8%)
           initializer time: 130.36 milliseconds (8.1%)
           slowest intializers :
               libSystem.dylib :  68.74 milliseconds (4.2%)
    libMainThreadChecker.dylib :  43.35 milliseconds (2.7%)

4、DYLD_PRINT_INITIALIZERS

dyld: calling initializer function 0x1014f69ee in /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libSystem.dylib
dyld: calling -init function 0x10041632a in /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libBacktraceRecording.dylib
dyld: calling initializer function 0x102a172ba in /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libc++.1.dylib
dyld: calling -init function 0x1014fe1f0 in /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
dyld: calling initializer function 0x10165e8f0 in /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
dyld: calling initializer function 0x1058804e0 in /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libnetwork.dylib

5、DYLD_PRINT_SEGMENTS

dyld: Main executable mapped /Users/administrator/Library/Developer/CoreSimulator/Devices/7E3D7605-368C-4DA1-BE76-1100849590C3/data/Containers/Bundle/Application/557FC56B-A0E1-4F55-A06D-AF74862C032F/SocketDemo.app/SocketDemo
        __PAGEZERO at 0x00000000->0x100000000
            __TEXT at 0x1046E9000->0x1046ED000
            __DATA at 0x1046ED000->0x1046EF000
        __LINKEDIT at 0x1046EF000->0x1046F7000
dyld: Mapping /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libBacktraceRecording.dylib
            __TEXT at 0x10477A000->0x104781FFF with permissions r.x
            __DATA at 0x104782000->0x104782FFF with permissions rw.
        __LINKEDIT at 0x104783000->0x104788AFF with permissions r..

6、DYLD_PRINT_BINDINGS

一直列印,很瘋狂
這就是所謂的動態繫結?


dyld: bind: GeoServices:0x11660C620 = libobjc.A.dylib:__objc_empty_cache, *0x11660C620 = 0x10373A610
dyld: bind: GeoServices:0x11660C648 = libobjc.A.dylib:__objc_empty_cache, *0x11660C648 = 0x10373A610
dyld: bind: GeoServices:0x11660C670 = libobjc.A.dylib:__objc_empty_cache, *0x11660C670 = 0x10373A610
dyld: bind: GeoServices:0x11660C698 = libobjc.A.dylib:__objc_empty_cache, *0x11660C698 = 0x10373A610
dyld: bind: GeoServices:0x11660C6C0 = libobjc.A.dylib:__objc_empty_cache, *0x11660C6C0 = 0x10373A610
dyld: bind: GeoServices:0x11660C6E8 = libobjc.A.dylib:__objc_empty_cache, *0x11660C6E8 = 0x10373A610

dyld: bind: Contacts:0x1142B71C0 = libobjc.A.dylib:_OBJC_METACLASS_$_NSObject, *0x1142B71C0 = 0x103DCAE58
dyld: bind: Contacts:0x1142B71C8 = libobjc.A.dylib:_OBJC_METACLASS_$_NSObject, *0x1142B71C8 = 0x103DCAE58
dyld: bind: Contacts:0x1142B7210 = libobjc.A.dylib:_OBJC_METACLASS_$_NSObject, *0x1142B7210 = 0x103DCAE58


dyld: bind: vCard:0x1173B4DC0 = libobjc.A.dylib:_OBJC_METACLASS_$_NSObject, *0x1173B4DC0 = 0x103DCAE58
dyld: bind: vCard:0x1173B4E08 = libobjc.A.dylib:_OBJC_METACLASS_$_NSObject, *0x1173B4E08 = 0x103DCAE58
dyld: bind: vCard:0x1173B4E10 = libobjc.A.dylib:_OBJC_METACLASS_$_NSObject, *0x1173B4E10 = 0x103DCAE58
dyld: bind: vCard:0x1173A2068 = libobjc.A.dylib:___objc_personality_v0, *0x1173A2068 = 0x10371EF69
dyld: bind: vCard:0x1173B3838 = libobjc.A.dylib:__objc_empty_cache, *0x1173B3838 = 0x10373A610
dyld: bind: vCard:0x1173B3860 = libobjc.A.dylib:__objc_empty_cache, *0x1173B3860 = 0x10373A610
dyld: bind: vCard:0x1173B3888 = libobjc.A.dylib:__objc_empty_cache, *0x1173B3888 = 0x10373A610