How to Bypass a Standard C++ Function While Maintaining Its Functionality

前端 未结 7 926
迷失自我
迷失自我 2021-01-24 03:03

I am looking for a way to be able to redefine a set of POSIX functions but then end the redefinition with a call to the original function. The idea is that I am trying to create

7条回答
  •  时光取名叫无心
    2021-01-24 03:32

    Typical way of doing is on Unix is via LD_PRELOAD, example (Unix) below proxies a function call, malloc in particular (full example):

    /**
     * malloc() direct call
     */
    inline void * libc_malloc(size_t size)
    {
      typedef void* (*malloc_func_t)(size_t);
      static malloc_func_t malloc_func = (malloc_func_t) dlsym(RTLD_NEXT, "malloc");
    
      return malloc_func(size);
    }
    

提交回复
热议问题