When you add the -g command line option to clang, DWARF debug information is put in the .o
file. When you link your object files (.o
, ranlib archives aka static libraries aka .a
files) into an executable/dylib/framework/bundle, "debug notes" are put in the executable to say (1) the location of the .o
etc files with the debug information, and (2) the final addresses of the functions/variables in the executable binary. Optimization flags (-O0
, -O2
etc) do not have an impact on debug information generation - although debugging code compiled with optimization is much more difficult than debugging code built at -O0
.
If you run the debugger on that executable binary -- without any other modification -- the debugger will read the debug information from the .o
etc files as long as they're still on the filesystem at the same file path when you built the executable. This makes iterative development quick - no tool needs to read, update, and output the (large) debug information. You can see these "debug notes" in the executable by running nm -pa exename
and looking for OSO
entries (among others). These are stabs nlist entries and running strip(1)
on your executable will remove them.
If you want to collect all of the debug information (in the .o
files) into a standalone bundle, then you run dsymutil
on the executable. This uses the debug notes (assumptions: (1) the .o
files are still in their orig location, and (2) the executable has not been stripped) to create a "dSYM
bundle". If the binary is exename, the dSYM bundle is exename.dSYM. When the debugger is run on exename, it will look next to that binary for the dSYM bundle. If not found there, it will do a Spotlight search to see if the dSYM is in a spotlight-indexed location on your computer.
You can run dwarfdump
on .o
files, or on the dSYM bundle -- they both have debug information in them. dwarfdump
won't find any debug information in your output executable.
So, the normal workflow: Compile with -g. Link executable image. If iterative development, run debugger. If shipping/archiving the binary, create dSYM, strip executable.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…