• Antelope Release 5.10 Linux CentOS release 7.6.1810 (Core) 3.10.0 2020-05-12

 

NAME

notes_mac_development - advice for people writing programs on Mac OS X

DESCRIPTION

Xcode

Most developer tools for Mac OS X come bundled together in a package named Xcode. This package has been available from Apple separately and usually on the system install DVD; but it has to be installed separately from the main Mac OS X install. Now Xcode is available at a nominal price from Apple's App store. If you want to write code in c or fortran, the first step is installing Xcode, from the App Store.

The GNU compiler collection has long been the underlying compiler, linker and debugger in Xcode, but in recent years, Apple has been moving toward a newer set of tools, the LLVM compiler Infrastructure: http://llvm.org/ The clang compiler (http://clang.llvm.org/) provided at least some part of the performance increase between the Leopard and Snow Leopard releases of Mac OS X.

Recently, lldb (http://lldb.llvm.org/ ) has become available as an alternative to the gdb debugger, but gdb is probably still the first choice.

Xcode includes a very sophisticated IDE for creating Macintosh applications. Its primary focus, however, is creating single large complicated applications (like GarageBand or iPhoto) rather than many simpler tools. Getting started with Xcode from a regular Unix Make environment is relatively difficult.

Xcode command-line tools and header files

To install Xcode command-line tools and perhaps links to header files (such as stdio.h) in standard locations, in OSX 10.9 and above you may need to execute

% xcode-select --install

clang options

clang has a variety of options which can be seen with the command:

/Developer/usr/bin/../libexec/clang-cc --help

One interesting option is -dump-tokens, which can be used like either of these commands:
% /Developer/usr/bin/../libexec/clang-cc -I$antelope/include -dump-tokens dbcp.c
% clang -Xclang -dump-tokens -I$ANTELOPE/include -c dbcp.c

Other interesting options are -ast-dump. The -v option can be passed directly to the apple clang script, to show what it invokes and also be passed on to the real clang compiler.

malloc debugging

Turn on malloc debugging with


setenv DYLD_INSERT_LIBRARIES /usr/lib/libgmalloc.dylib

See man libgmalloc for more info. Also look at CrashReporter Some other environment variables you might try setting:

Inside gdb, use


set environment DYLD_INSERT_LIBRARIES /usr/lib/libgmalloc.dylib

otool

Darwin doesn't have ldd, but otools provides much of the same functionality. See the -L option. The otx program is an extended otool with a GUI and better disassembly skills.

otool can also help in other situations. Here's a report from a bus error on a Mac:


aspenrt:log superuser$ more /Library/Logs/DiagnosticReports/orbserver_2010-03-12-223713_localhost.crash
Process:         orbserver [2019]
Path:            /opt/antelope/4.11/bin/orbserver
Identifier:      orbserver
Version:         ??? (???)
Code Type:       X86 (Native)
Parent Process:  perl [199]

Date/Time:       2010-03-12 22:37:12.991 +0500
OS Version:      Mac OS X 10.6.2 (10C2234)
Report Version:  6

Exception Type:  EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000000000000
Crashed Thread:  0  Dispatch queue: com.apple.main-thread

Thread 0 Crashed:  Dispatch queue: com.apple.main-thread
0   orbserver                           0x00005673 setup_Status + 162
1   orbserver                           0x00005184 main + 980
2   orbserver                           0x00001816 start + 54

Thread 0 crashed with X86 Thread State (32-bit):
  eax: 0x00000000  ebx: 0x000055e1  ecx: 0x00200690  edx: 0x0000001c
  edi: 0x00000005  esi: 0x00000000  ebp: 0xbffffb88  esp: 0xbffff630
   ss: 0x0000001f  efl: 0x00010286  eip: 0x00005673   cs: 0x00000017
   ds: 0x0000001f   es: 0x0000001f   fs: 0x00000000   gs: 0x00000037
  cr2: 0x00000000

Binary Images:
    0x1000 -    0x10fff +orbserver ??? (???) <9E583B6F-533E-E324-1D56-33999757DAC1> /opt/antelope/4.11/bin/orbserver
   0x17000 -    0x26fff +liborb.dylib ??? (???) <D5814D70-72C8-2FE2-9C8F-0A3C9F7E1C36> /opt/antelope/4.11/lib/liborb.dylib
   0x2e000 -    0x34ff3 +libcoords.dylib ??? (???) <A9849BD1-ECBD-ED5A-6A29-B8D732312EF6> /opt/antelope/4.11/lib/libcoords.dylib
   0x3a000 -    0xabfeb +libstock.dylib ??? (???) <6959D38D-BD2C-4A40-70DD-FF594D5B9CD7> /opt/antelope/4.11/lib/libstock.dylib
   0xd5000 -    0xe7ff7 +libdeviants.dylib ??? (???) <83CFFCC8-6F4B-06A5-1834-5F7CEAFBAA68> /opt/antelope/4.11/lib/libdeviants.dylib
   0xf6000 -    0xf6ffc +libbrttpool.dylib ??? (???) <7BB15689-C70F-2DF5-67D8-52D9E27DD85B> /opt/antelope/4.11/lib/libbrttpool.dylib
0x8fe00000 - 0x8fe41627  dyld 132.1 (???) <7E8A62A5-BB2D-C9C5-940D-B493DC9CD231> /usr/lib/dyld
0x918cf000 - 0x918d2fe7  libmathCommon.A.dylib ??? (???) <1622A54F-1A98-2CBE-B6A4-2122981A500E> /usr/lib/system/libmathCommon.A.dylib
0x979b7000 - 0x97b5bfeb  libSystem.B.dylib ??? (???) <D84E6C3F-163B-315B-AA8B-D6D0B383F207> /usr/lib/libSystem.B.dylib
0xffff0000 - 0xffff1fff  libSystem.B.dylib ??? (???) <D84E6C3F-163B-315B-AA8B-D6D0B383F207> /usr/lib/libSystem.B.dylib

You can get a lot of potentially useful information from otool in this situation, with
% otool -t -v -V setup_Status.o
This might show the actual location in the code where the bus error occurred.

Other tools

hfsdebug

prints out info about HFS filesystems

BootstrapDump

shows what bootstrap port a process is using, showing whether it has access to services like gethostbyname and getpwuid.

latency(1)

sc_usage(1)

fs_usage(1)

Some other tools which you have to download and install yourself are:

Static Analyzer with clang

http://clang-analyzer.llvm.org has a code analyzer which is potentially useful.

xdb

xdbis a script which creates a project for an executable on the fly and runs the debugger on the executable: e.g.:

xdb some-program program-arguments

For the Antelope 4.7 release, I moved all the dynamic libg2c*.dylib to a hidden directory, so that fortran used the static libraries. I couldn't figure out how to get the loader to use the dynamic libraries if I copied them to $antelope/lib, before or after linking.

** Beware, however, MacPorts sneakily modifies your path by adding something to .tcshrc:


# MacPorts Installer addition on 2009-11-11_at_08:47:32: adding an appropriate PATH variable for use with MacPorts.
setenv PATH /opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.

This will break compilation of programs against Antelope, so we strongly advise you to get rid of it!

Crash Reports

Look for core files in /cores and crash reports in ~/Library/Logs/DiagnosticReports. Also see the otool example above.

Man pages

Postscript tools such as dpost are not always available for things like formatting man-pages into postscript. To do this on Mac OSX 10.10, there is an undocumented feature of preview:

% man -t <whatever> | open -f -a /Applications/Preview.app 

SEE ALSO

notes_mac_bookshelf(5)
notes_mac_extras(5)
notes_mac_setup(5)
notes_mac_unix_anomalies(5)
notes_mac_mavericks(5)

Look at this Apple document for some debugging hints:

http://developer.apple.com/technotes/tn2004/tn2124.html

Search the Mac OS X Developer Library for specific information about debugging:


http://developer.apple.com/library/mac/navigation/

You might search for "Mac OS X Debugging Magic".
otool(1)
http://ctags.sourceforge.net

AUTHOR

Daniel Quinlan
Printer icon