简体   繁体   English

G ++和__attribute __((优化))不改变调试器行为

[英]G++ and __attribute__((optimize)) not changing debugger behavior

I am trying to play with __attribute__ to allow a function to be essentially compiled with different flags from the rest of the code. 我正在尝试使用__attribute__来允许函数基本上使用与其余代码不同的标志进行编译。 For example: 例如:

#include <iostream>
#include <vector>

void MyNormalFunction();

void MyDebugabbleFunction() __attribute__((optimize(0)));

void MyNormalFunction()
{
  std::cout << "Test" << std::endl;

  std::vector<int> a;

  for(unsigned int i = 0; i < 10; ++i)
  {
    a.push_back(i);
  }
}

void MyDebugabbleFunction()
{
  std::cout << "Test" << std::endl;

  std::vector<int> a;

  for(unsigned int i = 0; i < 10; ++i)
  {
    a.push_back(i);
  }
}

int main()
{
  MyNormalFunction();
  MyDebugabbleFunction();
  return 0;
}

I am building with -g -O2, but I want to be able to sanely debug MyDebugabbleFunction() — so I used the __attribute__((optimize(0))) on its declaration. 我正在使用-g -O2构建,但我希望能够MyDebugabbleFunction()地调试MyDebugabbleFunction() - 所以我在其声明中使用了__attribute__((optimize(0))) However, I can't really tell any difference when stepping through these two functions with a debugger. 但是,当使用调试器单步执行这两个函数时,我无法区分任何差异。 I would expect the "seemingly erratic" behavior that I usually see when trying to step through optimized code in MyNormalFunction , but the standard "-g"-only debugger behavior in MyDebuggableFunction . 我希望在尝试逐步调试MyNormalFunction优化代码时,通常会看到“看似不稳定”的行为,但MyNormalFunction的标准“-g” MyDebuggableFunction调试器行为。

Is it that I have done something wrong with __attribute__ ? 是不是我对__attribute__做错了什么? Or that I have used bad demo code (ie code that doesn't get "optimized a lot") inside the two functions? 或者我在两个函数中使用了不好的演示代码(即代码没有“优化得很多”)? Or am I misinterpreting what the difference is supposed to be in the debugger? 或者我误解了调试器中应该有什么区别?

I am using gcc 4.6. 我正在使用gcc 4.6。


EDIT based on GManNickG's suggestion 编辑基于GManNickG的建议

I used this code instead, and built with -O2 -g: 我使用了这个代码,并使用-O2 -g构建:

#include <iostream>
#include <vector>

int MyNormalFunction();

int MyDebugabbleFunction() __attribute__((optimize(0)));

int MyNormalFunction()
{
  int val = 0; // breakpoint here - debugger does NOT stop here
  val = 1;
  val = 2;
  return val;
} // debugger stops here instead

int MyDebugabbleFunction()
{
  int val = 0;  // breakpoint here - debugger stops here and steps through the next 3 lines as if it were built with only -g
  val = 1;
  val = 2;
  return val;
}

int main()
{
  int a = MyNormalFunction();
  std::cout << a << std::endl;

  int b = MyDebugabbleFunction();
  std::cout << b << std::endl;

  return 0;
}

Try a test like this instead: 尝试这样的测试:

int MyNormalFunction()
{
    int val = 0;
    val = 1;
    val = 2;

    // should optimize to return 2
    return val;
}

int MyDebuggableFunction() __attribute__((optimize(0)));
{
    int val = 0;
    val = 1;
    val = 2;

    // could optimize to return 2, but attribute blocks that
    return val;
}

int main()
{
    // we need to actually output the return values,
    // or main itself could be optimized to nothing
    std::cout << MyNormalFunction() << std::endl;
    std::cout << MyDebuggableFunction() << std::endl;
}

It'll make it much easier to follow. 它会让它更容易理解。


Note that you should start in main , when stepping through, because it most likely will be reduced to: 请注意,当您单步执行时,您应该从main开始,因为它很可能会缩减为:

int main()
{
    std::cout << 2 << std::endl;
    std::cout << MyDebuggableFunction() << std::endl;
}

If you're up for it, looking at the disassembly makes this task much easier. 如果你准备好了,那么查看反汇编会使这项任务变得更加容易。

After fixing the errors in the code, so it compiles: 修复代码中的错误后,编译:

g++ -S x.c

_Z16MyNormalFunctionv:
.LFB1255:
    .cfi_startproc
    movl    $2, %eax
    ret

_Z20MyDebuggableFunctionv:
.LFB1256:
    .cfi_startproc
    movl    $0, -4(%rsp)
    movl    $1, -4(%rsp)
    movl    $2, -4(%rsp)
    movl    -4(%rsp), %eax
    ret

As you can see the optimization attribute worked fine. 如您所见,优化属性工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM