问题描述
使用-fomit -frame -pointer(用于各种-O设置)时,执行回溯是有问题的.我想知道是否有一种在编译时间确定使用此开关的代码的方法?在这种情况下,我可以放入#ifndef时要在不明智的情况下防止回避.
此-Fomit-Fomit-Fame-Pointer Switch已打开?
谢谢,
setJMP
推荐答案
我刚刚尝试过:
gcc -E -fomit-frame-pointer -Wp,-dM foo.c > fpo.out gcc -E -Wp,-dM foo.c > no-fpo.out diff no-fpo.out fpo.out
foo.c是一个简单的" Hello World"程序,没有结果.这意味着所有预处理器宏都是相同的,无论是否使用-fomit-frame-pointer.所以我认为您的问题的答案是"否".
可能要做的最好的就是修改您的makefile(或任何构建过程使用)以定义您的 houm 宏(例如-DNO_FRAME_POINTERS或其他东西)时,使用-fomit-frame-pointer.-fomit-frame-pointer. p>
其他推荐答案
您不能在编译时执行此操作,但是在运行时您可以检查程序是否优化.
编写一个肯定会通过优化器更改的代码,例如将非挥发变量与setjmp/longjmp混合,并且通过该变量的值,您将知道您的程序是否已优化.
#include <setjmp.h> #include <stdio.h> int is_optimised(void) { int i = 1; jmp_buf jmp_loc; if (setjmp(jmp_loc)) { return i; // optimiser changes it to "return 1" } i = 0; longjmp(jmp_loc, 1); return 0; } int main(int argc, char *argv[]) { printf("%soptimised\n", is_optimised() ? "" : "non-"); return 0; }
如果使用GCC编译,没有-O开关打印" non-optimised",则为-O1 to -O4它打印" optimised".
当然您的里程(与其他编译器)可能会有所不同.
其他推荐答案
至于检查时,在运行时检查EBP寄存器(调整您的体系结构)是否指向堆栈顶部的几个字节,然后遵循存储在[EBP]的指针是有道理的.
问题描述
When -fomit-frame-pointer is used (automatic for various -O settings), performing a backtrace is problematic. I am wondering if there is a way of determining at compile time that the code is compiled with this switch? In that case, I could put in an #ifndef to guard against backtracing when ill-advised.
Is any macro set when this -fomit-frame-pointer switch is on?
Thanks,
SetJmp
推荐答案
I just tried this:
gcc -E -fomit-frame-pointer -Wp,-dM foo.c > fpo.out gcc -E -Wp,-dM foo.c > no-fpo.out diff no-fpo.out fpo.out
where foo.c is a simple "Hello World" program, and got no results. This means all of the preprocessor macros were identical, whether -fomit-frame-pointer was used or not. So I think the answer to your question is "no".
Probably the best you can do is modify your Makefile (or whatever your build process uses) to define your own macro (e.g. -DNO_FRAME_POINTERS, or something) when using -fomit-frame-pointer.
其他推荐答案
You cannot do it at compile time, but at runtime you can check whether your program optimised or not.
Write a code that definitely will be changes by optimiser, like mixing non-volatile variable with setjmp/longjmp, and by the value of that variable you will know whether your program is optimised or not.
#include <setjmp.h> #include <stdio.h> int is_optimised(void) { int i = 1; jmp_buf jmp_loc; if (setjmp(jmp_loc)) { return i; // optimiser changes it to "return 1" } i = 0; longjmp(jmp_loc, 1); return 0; } int main(int argc, char *argv[]) { printf("%soptimised\n", is_optimised() ? "" : "non-"); return 0; }
If compiled with GCC without -O switch it prints "non-optimised", for switches -O1 to -O4 it prints "optimised".
Of course your mileage (with other compilers) may vary.
其他推荐答案
As for the check, at runtime check if ebp register (adjust for your architecture) points to a few bytes below the top of the stack and then if following the pointer stored at [ebp] makes sense.