G ++ and __attribute __ ((optimize)) without changing the behavior of the debugger

I am trying to play with __attribute__so that the function is essentially compiled with different flags from the rest of the code. For instance:

#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 creating with -g -O2, but I want to be able to debug MyDebugabbleFunction(), so I used __attribute__((optimize(0)))in his declaration. However, I cannot say any difference when going through these two functions using the debugger. I would expect the "seemingly unstable" behavior that I usually see when I try to execute optimized code in MyNormalFunction, but the standard behavior of "-g" is only a debugger in MyDebuggableFunction.

- __attribute__? - (.. , " " ) ? , ?

gcc 4.6.


EDIT GManNickG

-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;
}
+5
2

:

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;
}

.


, main , , , :

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

, , .

+2

, :

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

, .

0

All Articles