Search code examples
cglibc

How does LIBC_PROBE macro actually work in Glibc?


I was trying to understand how mallopt() works in glibc, but unable to understand the use of LIBC_PROBE macro used in the function mallopt(). The definition of LIBC_PROBE is creating another macro LIBC_PROBE_1 and again it is creating another one STAP_PROBE##n. In case of mallopt() it is STAP_PROBE3(a1, a2, a3). After this there no clue how STAP_PROBE3 going to work ?
Source file: https://github.com/lattera/glibc/blob/master/malloc/malloc.c (line:5141).


Solution

  • From include/stap-probe.h:

    Without USE_STAP_PROBE, that does nothing but evaluates all
    its arguments (to prevent bit rot, unlike e.g. assert).
    
    Systemtap's header defines the macros STAP_PROBE (provider, name) and
    STAP_PROBEn (provider, name, arg1, ..., argn).  For "provider" we paste
    in MODULE_NAME (libc, libpthread, etc.) automagically.
    
    The format of the arg parameters is discussed here:
    
    https://sourceware.org/systemtap/wiki/UserSpaceProbeImplementation
    
    The precise details of how register names are specified is
    architecture specific and can be found in the gdb and SystemTap
    source code.  */
    

    So:

    1. Don't worry about it -- it's not important for understanding how mallopt() works.
    2. If you really care, read the wiki article referenced above, and look for how the macro is defined in SystemTap sources (SystemTap is entirely separate project from GLIBC).