1Symbols on macOS 2================ 3 4On macOS, debug symbols are often in stand alone bundles called **dSYM** files. 5These are bundles that contain DWARF debug information and other resources 6related to builds and debug info. 7 8The DebugSymbols.framework framework helps locate dSYM files when given a UUID. 9It can locate the symbols using a variety of methods: 10 11- Spotlight 12- Explicit search paths 13- Implicit search paths 14- File mapped UUID paths 15- Running one or more shell scripts 16 17DebugSymbols.framework also has global defaults that can be modified to allow 18all of the debug tools (lldb, gdb, sample, CoreSymbolication.framework) to 19easily find important debug symbols. The domain for the DebugSymbols.framework 20defaults is **com.apple.DebugSymbols**, and the defaults can be read, written 21or modified using the **defaults** shell command: 22 23:: 24 25 % defaults read com.apple.DebugSymbols 26 % defaults write com.apple.DebugSymbols KEY ... 27 % defaults delete com.apple.DebugSymbols KEY 28 29The following is a list of the defaults key value setting pairs that can 30be used to enhance symbol location: 31 32**DBGFileMappedPaths** 33 34This default can be specified as a single string, or an array of 35strings. Each string represents a directory that contains file mapped 36UUID values that point to dSYM files. See the "File Mapped UUID 37Directories" section below for more details. Whenever 38DebugSymbols.framework is asked to lookup a dSYM file, it will first 39look in any file mapped UUID directories for a quick match. 40 41:: 42 43 % defaults write com.apple.DebugSymbols DBGFileMappedPaths -string /path/to/uuidmap1 44 % defaults write com.apple.DebugSymbols DBGFileMappedPaths -array /path/to/uuidmap1 45 /path/to/uuidmap2 46 47**DBGShellCommands** 48 49This default can be specified as a single string, or an array of 50strings. Specifies a shell script that will get run in order to find the 51dSYM. The shell script will be run given a single UUID value as the 52shell command arguments and the shell command is expected to return a 53property list. See the property list format defined below. 54 55:: 56 57 % defaults write com.apple.DebugSymbols DBGShellCommands -string /path/to/script1 58 % defaults write com.apple.DebugSymbols DBGShellCommands -array /path/to/script1 59 /path/to/script2 60 61**DBGSpotlightPaths** 62 63Specifies the directories to limit spotlight searches to as a string or 64array of strings. When any other defaults are supplied to 65**com.apple.DebugSymbols**, spotlight searches will be disabled unless 66this default is set to an empty array: 67 68:: 69 70 # Specify an empty array to keep Spotlight searches enabled in all locations 71 % defaults write com.apple.DebugSymbols DBGSpotlightPaths -array 72 73 # Specify an array of paths to limit spotlight searches to certain directories 74 % defaults write com.apple.DebugSymbols DBGSpotlightPaths -array /path/dir1 /path/dir2 75 76Shell Script Property List Format 77--------------------------------- 78 79Shell scripts that are specified with the **DBGShellCommands** defaults key 80will be run in the order in which they are specified until a match is found. 81The shell script will be invoked with a single UUID string value like 82"23516BE4-29BE-350C-91C9-F36E7999F0F1". The shell script must respond with a 83property list being written to STDOUT. The property list returned must contain 84UUID string values as the root key values, with a dictionary for each UUID. The 85dictionaries can contain one or more of the following keys: 86 87+-----------------------------------+-----------------------------------+ 88| Key | Description | 89+-----------------------------------+-----------------------------------+ 90| **DBGArchitecture** | A textual architecture or target | 91| | triple like "x86_64", "i386", or | 92| | "x86_64-apple-macosx". | 93+-----------------------------------+-----------------------------------+ 94| **DBGBuildSourcePath** | A path prefix that was used when | 95| | building the dSYM file. The debug | 96| | information will contain paths | 97| | with this prefix. | 98+-----------------------------------+-----------------------------------+ 99| **DBGSourcePath** | A path prefix for where the | 100| | sources exist after the build has | 101| | completed. Often when building | 102| | projects, build machines will | 103| | host the sources in a temporary | 104| | directory while building, then | 105| | move the sources to another | 106| | location for archiving. If the | 107| | paths in the debug info don't | 108| | match where the sources are | 109| | currently hosted, then specifying | 110| | this path along with the | 111| | **DBGBuildSourcePath** will help | 112| | the developer tools always show | 113| | you sources when debugging or | 114| | symbolicating. | 115+-----------------------------------+-----------------------------------+ 116| **DBGDSYMPath** | A path to the dSYM mach-o file | 117| | inside the dSYM bundle. | 118+-----------------------------------+-----------------------------------+ 119| **DBGSymbolRichExecutable** | A path to the symbol rich | 120| | executable. Binaries are often | 121| | stripped after being built and | 122| | packaged into a release. If your | 123| | build systems saves an unstripped | 124| | executable a path to this | 125| | executable can be provided. | 126+-----------------------------------+-----------------------------------+ 127| **DBGError** | If a binary can not be located | 128| | for the supplied UUID, a user | 129| | readable error can be returned. | 130+-----------------------------------+-----------------------------------+ 131 132Below is a sample shell script output for a binary that contains two 133architectures: 134 135:: 136 137 <?xml version="1.0" encoding="UTF-8"?> 138 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 139 <plist version="1.0"> 140 <dict> 141 <key>23516BE4-29BE-350C-91C9-F36E7999F0F1</key> 142 <dict> 143 <key>DBGArchitecture</key> 144 <string>i386</string> 145 <key>DBGBuildSourcePath</key> 146 <string>/path/to/build/sources</string> 147 <key>DBGSourcePath</key> 148 <string>/path/to/actual/sources</string> 149 <key>DBGDSYMPath</key> 150 <string>/path/to/foo.dSYM/Contents/Resources/DWARF/foo</string> 151 <key>DBGSymbolRichExecutable</key> 152 <string>/path/to/unstripped/executable</string> 153 </dict> 154 <key>A40597AA-5529-3337-8C09-D8A014EB1578</key> 155 <dict> 156 <key>DBGArchitecture</key> 157 <string>x86_64</string> 158 <key>DBGBuildSourcePath</key> 159 <string>/path/to/build/sources</string> 160 <key>DBGSourcePath</key> 161 <string>/path/to/actual/sources</string> 162 <key>DBGDSYMPath</key> 163 <string>/path/to/foo.dSYM/Contents/Resources/DWARF/foo</string> 164 <key>DBGSymbolRichExecutable</key> 165 <string>/path/to/unstripped/executable</string> 166 </dict> 167 </dict> 168 </plist> 169 170There is no timeout imposed on a shell script when is it asked to locate a dSYM 171file, so be careful to not make a shell script that has high latency or takes a 172long time to download unless this is really what you want. This can slow down 173debug sessions in LLDB and GDB, symbolication with CoreSymbolication or Report 174Crash, with no visible feedback to the user. You can quickly return a plist 175with a single **DBGError** key that indicates a timeout has been reached. You 176might also want to exec new processes to do the downloads so that if you return 177an error that indicates a timeout, your download can still proceed after your 178shell script has exited so subsequent debug sessions can use the cached files. 179It is also important to track when a current download is in progress in case 180you get multiple requests for the same UUID so that you don't end up 181downloading the same file simultaneously. Also you will want to verify the 182download was successful and then and only then place the file into the cache 183for tools that will cache files locally. 184 185Embedding UUID property lists inside the dSYM bundles 186----------------------------------------------------- 187 188Since dSYM files are bundles, you can also place UUID info plists files inside 189your dSYM bundles in the **Contents/Resources** directory. One of the main 190reasons to create the UUID plists inside the dSYM bundles is that it will help 191LLDB and other developer tools show you source. LLDB currently knows how to 192check for these plist files so it can automatically remap the source location 193information in the debug info. 194 195If we take the two UUID values from the returns plist above, we can split them 196out and save then in the dSYM bundle: 197 198:: 199 200 % ls /path/to/foo.dSYM/Contents/Resources 201 23516BE4-29BE-350C-91C9-F36E7999F0F1.plist 202 A40597AA-5529-3337-8C09-D8A014EB1578.plist 203 204 % cat /path/to/foo.dSYM/Contents/Resources/23516BE4-29BE-350C-91C9-F36E7999F0F1.plist 205 <?xml version="1.0" encoding="UTF-8"?> 206 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 207 <plist version="1.0"> 208 <dict> 209 <key>DBGArchitecture</key> 210 <string>i386</string> 211 <key>DBGBuildSourcePath</key> 212 <string>/path/to/build/sources</string> 213 <key>DBGSourcePath</key> 214 <string>/path/to/actual/sources</string> 215 <key>DBGDSYMPath</key> 216 <string>/path/to/foo.dSYM/Contents/Resources/DWARF/foo</string> 217 <key>DBGSymbolRichExecutable</key> 218 <string>/path/to/unstripped/executable</string> 219 <key>DBGVersion</key> 220 <string>3</string> 221 <key>DBGSourcePathRemapping</key> 222 <dict> 223 <key>/path/to/build/time/src/location1</key> 224 <string>/path/to/debug/time/src/location</string> 225 <key>/path/to/build/time/src/location2</key> 226 <string>/path/to/debug/time/src/location</string> 227 </dict> 228 <key>DBGSymbolRichExecutable</key> 229 <string>/path/to/unstripped/executable</string> 230 </dict> 231 </plist> 232 233Note that the output is very close to what is needed by shell script output, so 234making the results of your shell script will be very easy to create by 235combining two plists into a single one where you take the UUID and use it a 236string key, and the value is the contents of the plist. 237 238LLDB will read the following entries from the per-UUID plist file in the dSYM 239bundle: **DBGSymbolRichExecutable**, **DBGBuildSourcePath** and 240**DBGSourcePath**, and **DBGSourcePathRemapping** if **DBGVersion** is 3 or 241higher. **DBGBuildSourcePath** and **DBGSourcePath** are for remapping a single 242file path. For instance, the files may be in /BuildDir/SheetApp/SheetApp-37 243when built, but they are in /SourceDir/SheetApp/SheetApp-37 at debug time, 244those two paths could be listed in those keys. If there are multiple source 245path remappings, the **DBGSourcePathRemapping** dictionary can be used, where 246an arbitrary number of entries may be present. **DBGVersion** should be 3 or 247**DBGSourcePathRemapping** will not be read. If both **DBGSourcePathRemapping** 248AND **DBGBuildSourcePath**/**DBGSourcePath** are present in the plist, the 249**DBGSourcePathRemapping** entries will be used for path remapping first. This 250may allow for more specific remappings in the **DBGSourcePathRemapping** 251dictionary and a less specific remapping in the 252**DBGBuildSourcePath**/**DBGSourcePath** pair as a last resort. 253 254File Mapped UUID Directories 255---------------------------- 256 257File Mapped directories can be used for efficient dSYM file lookups for local 258or remote dSYM files. The UUID is broken up by splitting the first 20 hex 259digits into 4 character chunks, and a directory is created for each chunk, and 260each subsequent directory is created inside the previous one. A symlink is then 261created whose name is the last 12 hex digits in the deepest directory. The 262symlinks value is a full path to the mach-o files inside the dSYM bundle which 263contains the DWARF. Whenever DebugSymbols.framework is asked to lookup a dSYM 264file, it will first look in any file mapped UUID directories for a quick match 265if the defaults are appropriately set. 266 267For example, if we take the sample UUID plist information from above, we can 268create a File Mapped UUID directory cache in 269**~/Library/SymbolCache/dsyms/uuids**. We can easily see how things are laid 270out: 271 272:: 273 274 % find ~/Library/SymbolCache/dsyms/uuids -type l 275 ~/Library/SymbolCache/dsyms/uuids/2351/6BE4/29BE/350C/91C9/F36E7999F0F1 276 ~/Library/SymbolCache/dsyms/uuids/A405/97AA/5529/3337/8C09/D8A014EB1578 277 278The last entries in these file mapped directories are symlinks to the actual 279dsym mach file in the dsym bundle: 280 281:: 282 283 % ls -lAF ~/Library/SymbolCache/dsyms/uuids/2351/6BE4/29BE/350C/91C9/F36E7999F0F1 284 ~/Library/SymbolCache/dsyms/uuids/2351/6BE4/29BE/350C/91C9/F36E7999F0F1@ -> ../../../../../../dsyms/foo.dSYM/Contents/Resources/DWARF/foo 285 286Then you can also tell DebugSymbols to check this UUID file map cache using: 287 288:: 289 290 % defaults write com.apple.DebugSymbols DBGFileMappedPaths ~/Library/SymbolCache/dsyms/uuids 291 292dSYM Locating Shell Script Tips 293------------------------------- 294 295One possible implementation of a dSYM finding shell script is to have the 296script download and cache files locally in a known location. Then create a UUID 297map for each UUID value that was found in a local UUID File Map cache so the 298next query for the dSYM file will be able to use the cached version. So the 299shell script is used to initially download and cache the file, and subsequent 300accesses will use the cache and avoid calling the shell script. 301 302Then the defaults for DebugSymbols.framework will entail enabling your shell 303script, enabling the file mapped path setting so that already downloaded dSYMS 304fill quickly be found without needing to run the shell script every time, and 305also leaving spotlight enabled so that other normal dSYM files are still found: 306 307:: 308 309 % defaults write com.apple.DebugSymbols DBGShellCommands /path/to/shellscript 310 % defaults write com.apple.DebugSymbols DBGFileMappedPaths ~/Library/SymbolCache/dsyms/uuids 311 % defaults write com.apple.DebugSymbols DBGSpotlightPaths -array 312 313Hopefully this helps explain how DebugSymbols.framework can help any company 314implement a smart symbol finding and caching with minimal overhead. 315