开发者

Can MSVC _penter and _pexit hooks be disabled on a per function basis?

There are compiler options in MSVC to enable the automatic generation of instrumentation calls on entering and exiting functions. These hooks are called _penter() and _pexit(). The options to the compiler are:

/Gh Enable _penter Hook Function

/GH Enable _pexit Hook Function

Is there a pragma or some sort of function declaration that will turn off the instrumentation on a per function basis? I know that using __declspec(naked) functions will not be instrumented but this isn't always a very practical option. I'm using MSVC both on PC and on a non-X86 platform and the non-X86 platform is a pain to manually write epilog/prolog in assembler (not to mention it messes up the debugger stack tracing).

If this in only on开发者_如何学Go a per file (compiler option) basis, I think I will have to split out the special functions into a separate file to turn the option off but it'd be much easier if I could just control it on a per file basis.

The fallback plan if this can't be done is to just move the functions to their own CPP translation unit and compile separately without the options.


I don't see any way to do this. Given that you would have to locate and handle every affected function anyway, perhaps moving them into their own module(s) is not such a big deal.


Asker is aware, but worth writing out the disqualified approach for future reference. /Gh and /GH do not instrument naked functions. You can declare the function you want to opt-out for as naked and manually supply the standard prolog/epilog, as shown below,

void instrumented_fn(void *p)
{
    /* Function body */
}

__declspec(naked) void uninstrumented_fn(void *p)
{
    __asm
    {      
        /* prolog */
        push    ebp
        mov     ebp, esp
        sub     esp, __LOCAL_SIZE
    }

    /* Function body */

    __asm
    {   
        /* epilog */
        mov     esp, ebp
        pop     ebp
        ret
    }
}

An example instrumented function disassembly, showing calls to penter and pexit,

   537b0:   e8 7c d9 ff ff          call   0x51131
   537b5:   55                      push   %ebp
   537b6:   8b ec                   mov    %esp,%ebp
   537b8:   83 ec 40                sub    $0x40,%esp
   537bb:   53                      push   %ebx
   537bc:   56                      push   %esi
   537bd:   57                      push   %edi
   537be:   90                      nop
   537bf:   90                      nop
   537c0:   90                      nop
   537c1:   5f                      pop    %edi
   537c2:   5e                      pop    %esi
   537c3:   5b                      pop    %ebx
   537c4:   8b e5                   mov    %ebp,%esp
   537c6:   5d                      pop    %ebp
   537c7:   e8 01 d9 ff ff          call   0x510cd
   537cc:   c3                      ret    

The equivalent uninstrumented function disassembly (naked body plus standard prolog/epilog)

   51730:   55                      push   %ebp
   51731:   8b ec                   mov    %esp,%ebp
   51733:   83 ec 40                sub    $0x40,%esp
   51736:   90                      nop
   51737:   90                      nop
   51738:   90                      nop
   51739:   8b e5                   mov    %ebp,%esp
   5173b:   5d                      pop    %ebp
   5173c:   c3                      ret   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜