是否可以在每个函数的基础上禁用 MSVC _penter 和 _pexit 挂钩?
MSVC 中有编译器选项,可以在进入和退出函数时自动生成检测调用。这些钩子称为 _penter() 和 _pexit()。编译器的选项有:
/Gh 启用_penter钩子函数
/GH启用_pexit挂钩功能
是否有编译指示或某种函数声明可以在每个函数的基础上关闭检测?我知道使用 __declspec(naked) 函数不会被检测,但这并不总是一个非常实用的选择。我在 PC 和非 X86 平台上都使用 MSVC,在非 X86 平台上在汇编器中手动编写 Epilog/Prolog 很痛苦(更不用说它会扰乱调试器堆栈跟踪)。
如果这仅在每个文件(编译器选项)的基础上,我想我将不得不将特殊功能拆分到一个单独的文件中以关闭该选项,但如果我可以在每个文件上控制它会更容易文件依据。
如果无法做到这一点,后备计划是将函数移动到它们自己的 CPP 翻译单元,并在没有选项的情况下单独编译。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看不出有什么办法可以做到这一点。考虑到无论如何您都必须找到并处理每个受影响的函数,也许将它们移动到自己的模块中并不是什么大问题。
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 知道,但值得写出不合格的方法以供将来参考。 /Gh 和 /GH 不检测裸函数。您可以将要选择退出的函数声明为裸函数,并手动提供 标准 prolog/epilog,如下所示,
一个插装函数反汇编示例,显示对 penter 和 pexit 的调用,
等效的未插装函数反汇编(裸体加上标准序言/结语)
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,
An example instrumented function disassembly, showing calls to penter and pexit,
The equivalent uninstrumented function disassembly (naked body plus standard prolog/epilog)