簡體   English   中英

使用注釋在編譯時自動包裝C / C ++函數

[英]Automatically wrap C/C++ function at compile-time with annotation

在我的C / C ++代碼中,我想要注釋不同的函數和方法,以便在編譯時(或鏈接時)添加額外的代碼。 添加的包裝代碼應該能夠檢查上下文(被調用的函數,線程信息等),讀/寫輸入變量以及修改返回值和輸出變量。

我怎么能用GCC和/或Clang做到這一點?

看看GCC中的儀器功能。 來自man gcc

   -finstrument-functions
       Generate instrumentation calls for entry and exit to functions.  Just after function entry and just before function exit, the following profiling functions will be called with the address of the current function and its call site.  (On some platforms,
       "__builtin_return_address" does not work beyond the current function, so the call site information may not be available to the profiling functions otherwise.)

               void __cyg_profile_func_enter (void *this_fn,
                                              void *call_site);
               void __cyg_profile_func_exit  (void *this_fn,
                                              void *call_site);

       The first argument is the address of the start of the current function, which may be looked up exactly in the symbol table.

       This instrumentation is also done for functions expanded inline in other functions.  The profiling calls will indicate where, conceptually, the inline function is entered and exited.  This means that addressable versions of such functions must be available.  If
       all your uses of a function are expanded inline, this may mean an additional expansion of code size.  If you use extern inline in your C code, an addressable version of such functions must be provided.  (This is normally the case anyways, but if you get lucky
       and the optimizer always expands the functions inline, you might have gotten away without providing static copies.)

       A function may be given the attribute "no_instrument_function", in which case this instrumentation will not be done.  This can be used, for example, for the profiling functions listed above, high-priority interrupt routines, and any functions from which the
       profiling functions cannot safely be called (perhaps signal handlers, if the profiling routines generate output or allocate memory).

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM