On Windows there a few libraries that allow you to intercept calls to DLLs:
http://www.codeproject.com/kb/system/hooksys.aspx
Is it possible to do this on Mac OS? If so, how is it done?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The answer depends on whether you want to do this in your own application or systemwide. In your own application, it’s pretty easy; the dynamic linker provides features such as
DYLD_INSERT_LIBRARIES. If you’re doing this for debugging/instrumentation purposes, also check out DTrace.You can replace Objective-C method implementations with method swizzling, e.g. JRSwizzle or Apple’s
method_exchangeImplementations(10.5+).If you want to modify library behavior systemwide, you’re going to need to load into other processes’ address spaces.
mach_inject/mach_overrideare an open-source set of libraries for loading code and replacing function implementations, respectively; however, you’re responsible for writing your own application which uses the libraries. (Also, take a look at this answer; you need special permissions to inject code into other processes.)Please keep in mind that application patching/code injection for non-debugging purposes is strongly discouraged by Apple and some Mac users (and developers) are extremely critical of the practice. Much of this criticism is poorly informed, but there have been a number of legitimately poorly written “plug-ins” (particularly those which patch Safari) that have been implicated in application crashes and problems. Code defensively.
(Disclaimer: I am the author of a (free) APE module and an application which uses
mach_inject.)