禁用“if(0)”消除在gcc [英] Disable "if(0)" elimination in gcc

查看:183
本文介绍了禁用“if(0)”消除在gcc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何防止GCC删除 if(0)阻止中的代码?



当我使用Visual Studio时,调试技巧是在我的程序中放置这样的代码:

  if(0)
do_some_debug_printing_and_checking

然后,当点击断点时,我点击 do_some_debug_printing_and_checking > line,选择set next statement并强制它执行。



当我使用gcc / gdb作为后端时,set next语句工作,因为GCC只是从 if(0)语句内删除代码。



我当然使用 O0 标志以停用优化。我也试过 -fno-dce -fno-tree-dce 标志来显式禁用死代码消除,但它没有效果: if(0)只是不存在于二进制文件,我不能使用设置下一个语句跳转到它。



有没有什么好办法告诉gcc要禁用消除 if(0)内容?



编辑:



对于附加变量解决方法,但有两件事情我不喜欢它:


  1. 它还是一行额外的代码

  2. 当我构建发布版本并且希望这些调试事件消失时,它不会被自动优化。当然,我可以使用#ifdef-s,但这是更多的额外行。

真的,绝对没有选择让GCC保持死代码?

解决方案

最简单的做法是使检查依赖于外部链接的变量。 / p>

例如

  extern bool debug; 
if(debug)
do_some_debug_printing_and_checking();

命名空间范围的某处:

  bool debug = false; 


How can I prevent GCC from eliminating the code inside if(0) block?

When I use Visual Studio, one of my debugging techniques is to put code like this in my program:

if (0)
    do_some_debug_printing_and_checking();

Then, when a breakpoint is hit, I click at the do_some_debug_printing_and_checking() line, select "set next statement" and force it to execute.

When I use gcc/gdb as a back-end, the "set next statement" does not work anymore, as GCC simply removes the code from inside the if(0) statement.

I am of course using the -O0 flag to disable optimization. I have also tried the -fno-dce -fno-tree-dce flags to disable dead code elimination explicitly, but it has no effect: the contents of if(0) is just not present in the binary file and I cannot use set next statement to jump into it.

Is there any good way to tell gcc to disable elimination of if(0) contents?

Edit:

Thanks for the "additional variable" workaround, however there are 2 things I don't like about it:

  1. It's still an extra line of code
  2. It won't be automatically optimized out when I build the release version and do want those debug things to disappear. Surely I can use #ifdef-s, but that's even more extra lines.

Really, there's absolutely no option to make GCC keep that dead code?

解决方案

The simplest thing to do is to make the check depend on (say) a variable with external linkage.

E.g.

extern bool debug;
if (debug)
    do_some_debug_printing_and_checking();

The somewhere at namespace scope:

bool debug = false;

这篇关于禁用“if(0)”消除在gcc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆