Author Topic: Variables <optimized out>  (Read 2225 times)

Offline jury

  • Multiple posting newcomer
  • *
  • Posts: 18
Variables <optimized out>
« on: December 10, 2022, 10:18:21 pm »
I need to debug one project so I switched off O2 compiler option from "Project -> Build options -> Compiler settings" (I do not have this option set in global settings) Nothing more is set there besides "-g" option.
Then the project was fully rebuild, but when debugging most of the variables are still <optimized out>
What do I have to do to unoptimize out my variables?

Code::Blocks 20.03 PCLinuxOS

Offline jury

  • Multiple posting newcomer
  • *
  • Posts: 18
Re: Variables <optimized out>
« Reply #1 on: December 11, 2022, 11:05:51 am »
Even thou I have unchecked the O2 from "Project -> Build Options -> Compiler settings" it still found it in .cbp file. So I manually removed it from there and rebuild the project.
But no changes, most variables <optimized out>
So I added -O0 to the other compiler options and rebuild the project again. In build log I see that -O0 was added to the compile command fine, but in debugger I still see <optimized out>
Any clues are welcomed.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Variables <optimized out>
« Reply #2 on: December 12, 2022, 09:19:06 pm »
it is possible that the compiler "optimizes" variables to registers and so they are invisible for the debugger and it is possible that the compiler does this even with -g..

I suspect it is a quite easy code? Can you show a code example?
Can you post the build log from the build log tab?
Can you enable full debug logging in settings->debugger->common->full debug log and post the output from the debug log tab?

Quote
Even thou I have unchecked the O2 from "Project -> Build Options -> Compiler settings" it still found it in .cbp file. So I manually removed it from there and rebuild the project.
On the left you can select the build target (debug or release) you have to remove it from the debug target, and before compiling activate the debug target from the drop down menu near the compile button in the toolbar. It can stay in the release target.

Offline jury

  • Multiple posting newcomer
  • *
  • Posts: 18
Re: Variables <optimized out>
« Reply #3 on: December 12, 2022, 10:35:28 pm »
I suspect it is a quite easy code? Can you show a code example?

Well, at least for me its definitely not simple code. Its an open source remake of Abuse game. Code sample at the end of the post (its the function I wanted to debug)

Can you post the build log from the build log tab?
Can you enable full debug logging in settings->debugger->common->full debug log and post the output from the debug log tab?

Its not possible now. Yesterday I gave up with code::blocks and fired up gdb (with the binary built up by code::blocks) And all the variables were visible fine.
And today I needed to debug another piece of code and having code::blocks still open I forgot about its debugging strangeness so I fired debugger again. This time I can see the variables.

But if the strangeness comes back, I will post the full logs.
Thanks



Code
nfs_file::nfs_file(char const *filename, char const *mode)
{
  local=NULL;
  nfs_fd=-1;

  int local_only=0;
  char const *s=mode;
  for (; *s; s++)    // check to see if writeable file, if so don't go through nfs
    if (*s=='w' || *s=='W' || *s=='a' || *s=='A')
      local_only=1;

  char name[256], *c;
  char const *f = filename;
  c = name;
  while (*f) { *c=*(f++); *c=toupper(*c); c++; } *c=0;
  if (strstr(name,"REGISTER"))
    local_only=1;

  if (net_crcs && !local_only)
  {
    int fail1,fail2,fail3=0;
    char const *local_filename = filename;
    if (filename[0]=='/' && filename[1]=='/')
    { local_filename+=2;
      while (*local_filename && *local_filename!='/') local_filename++;
      local_filename++;
    }

    int remote_file_num=net_crcs->get_filenumber(local_filename);
    uint32_t remote_crc=net_crcs->get_crc(remote_file_num,fail2);
    if (!fail2)
    {
      int local_file_num=crc_manager.get_filenumber(local_filename);
      uint32_t local_crc=crc_manager.get_crc(local_file_num,fail1);
      if (fail1)
      {
    bFILE *fp=new jFILE(local_filename,"rb");
    if (!fp->open_failure())
    {
      local_crc=crc_file(fp);
      crc_manager.set_crc(local_file_num,local_crc);
    } else fail3=1;
    delete fp;
      }

      if (!fail3)
      {
    if (local_crc==remote_crc)
          local_only=1;
      }
    }
  }

  local_only = 1;

  if (local_only)
  {
    local=new jFILE(filename,mode);
    if (local->open_failure()) { delete local; local=NULL; }
  }
  else
  {


    char nm[256];
    strcpy(nm,filename);
    nfs_fd=NF_open_file(nm,mode);
    if (nfs_fd==-2)
    {
      local=new jFILE(nm,mode);
      if (local->open_failure()) { delete local; local=NULL; }
      nfs_fd=-1;
    }
  }
}




Edit:

Its not possible now. Yesterday I gave up with code::blocks and fired up gdb (with the binary built up by code::blocks) And all the variables were visible fine.
And today I needed to debug another piece of code and having code::blocks still open I forgot about its debugging strangeness so I fired debugger again. This time I can see the variables.

Well, its not that it magically started debugging in code::blocks. I tried to debug exactly the same function I did the first time and I still see <optimized out> for most variables, but the same binary debugs fine in gdb. I give up on this and will use gdb for those functions that do not show variables in code::blocks.
« Last Edit: December 18, 2022, 02:04:37 pm by jury »

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Variables <optimized out>
« Reply #4 on: December 20, 2022, 12:11:49 am »
It would still be nice if you could post a full rebuild log and the debugger log so we can try to fix it....