Where is LD_PRELOAD under Mac OS X?

It started out when I was trying to do more extensive testing of MySQL 6 alpha version under  Mac OS X. I wanted to do some memory debugging so the obvious choice was Valgrind. However on looking at the official webiste, Valgrind does not exist for the PowerPC version of Mac OS X! On looking at alternatives, the offical Mac development tools include an application can MallocDebug which offers some similar capabilities. However, the application has to be linked with the libmallocdebug library.

How could I do this without recompiling the whole MySQL source tree and relinking? Under Linux and Solaris it is easy using the the LD_PRELOAD environment variable. This allows the dynamic linker to use an interposed library by changing the order that libraries are loaded.  Thus your own library can be matched against symbols that are to be linked rather than the more standard library that would be used later. Well it turns out Mac OS X has a similar feature, at least for version 10.3.9 or later.

The Mac OS X operating system has a similar capability using its dylib (dynamically linked/loaded libraries) format which is handled through Mac OS X's dynamic linker - dyld. Instead of using the LD_PRELOAD environment variable, Mac OS X uses the following in a Terminal window:

set env DYLD_INSERT_LIBRARIES /usr/lib/libMallocDebug.dylib

Now when  you run a command/application in this shell, dyld will look for the library in the specified location. If it does not exist, it will look in specific library paths specified in DYLD_LIBRARY_PATH. If the linking works and the library is able to match the symbols, the program will run, otherwise it will fail.

The interesting feature for Mac OS X here is that if you run the program using gdb when using the libMallocDebug library, if the program detects a memory corruption it will automatically drop you into the debugger.  

All these details are actually independant of the architecture as it works the same way with PowerPC or Intel processor machines, it was just the limitation of Valgrind not being available for PowerPC that made me look further into the workings.

There are other simple programs that may help further in determining problems in a program, but I will mention those in another article in the future.




Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Jonathon Coombes

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today