Where is LD_PRELOAD under Mac OS X?DYLD_INSERT_LIBRARIES

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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值