Optimizations manually disabled, debugger remains optimized

I’m manually setting build flags for compiler, linker, and assembler since I’m using a custom toolchain. Default GCC options are removed and custom ones added.

# Run "pio run -t envdump" to display whoe "env" object with all its contents
# to verify whether data written here is also written in there 

import os
from SCons.Script import Import

platform = env.PioPlatform()

# Filter out excluded build options from flag variable
def FilterFlags(flagVar, filteredOptions):
    filteredFlags = [flag for flag in flagVar if flag not in filteredOptions]
    return filteredFlags

# Define list of flag variables for option exclude

# Define default flags to exclude from all flag variables
filteredOptions = [
    # Add more flags to exclude as needed

# Modify flags globally (as if through "build_flags")
# Whole list [env, projenv, DefaultEnvironment()]
for e in [env, projenv, DefaultEnvironment()]:

    # ARMCC toolchain executables
        AR = "armar",
        AS = "armasm",
        CC = "armcc",
        CXX = "armcc",
        LD = "armlink",
        LINK= "armlink",
        GDB = "arm-none-eabi-gdb",  # to retain compatibility
        OBJCOPY = "fromelf --bin --output $TARGET $SOURCE && echo",
        # For displaying ROM/RAM usage

    # Preprocessor flags
    e.Replace(CPPFLAGS = [])
    e.Append(CPPFLAGS = [
        # Add more flags to exclude as needed

    # Compiler flags
    #e.Replace(CCFLAGS = [])
    e.Append(CCFLAGS = [
        # Add more flags to exclude as needed

    # Assembler flags
    #e.Replace(ASFLAGS = [])
    e.Append(ASFLAGS = [
        # Add more flags to exclude as needed

    # Linker flags
    #e.Replace(LINKFLAGS = [])
    e.Append(LINKFLAGS = [
        #"--list=$BUILD_DIR/$PROGNAME.map"  # causes errors for some reason
        # Add more flags to exclude as needed

    # Replace "-T" with "--scatter" for linker (scatter) scirpt
    old_flags = e["LINKFLAGS"].copy()
        i = old_flags.index("-T")
        old_flags[i] = "--scatter"

    # Remove "-Wl,..." which are GCC specific
    e.Replace(_LIBFLAGS = [])

    # Prevent searching for default linker file
    e.Replace(LIBPATH = [])

    # Filter out excluded flags from all flag variables
    for flag in filteredBuildFlags:
        existingFlags = e.get(flag, [])
        filteredFlags = FilterFlags(existingFlags, filteredOptions)
        e.Replace(**{flag: filteredFlags})

    # Add new toolchain to path
    pkg = platform.get_package("toolchain-armcc")
        os.path.join(pkg.path, "bin")
        if os.path.isdir(os.path.join(pkg.path, "bin"))
        else pkg.path,

# Remove duplicates, because modifying env, envproj, and DefaultEnvironment() objects
# adds redundant (repeated) flags into build options
e['CPPFLAGS'] = list(set(e['CPPFLAGS']))
e['CFLAGS'] = list(set(e['CFLAGS']))
e['CCFLAGS'] = list(set(e['CCFLAGS']))
e['CXXFLAGS'] = list(set(e['CXXFLAGS']))
e['ASFLAGS'] = list(set(e['ASFLAGS']))

Even though I’m modifying build flags globally it seems the -Og flag is still present somewhere in the system. When I run application, it runs without issues. When I run application with debugger, application doesn’t run because some parts of code fail due to optimizations enabled. What I see under “Variables” window is the following:
var_name:<optimized out>
This should be the consequence of the -Og optimization. But as you can see from script, where build flags are set, -Og flag is filtered out (and other optimization levels) and -O0 in added instead.

It seems the build differs when I try and debug application. To check the modified build flags, I’m using the following command:
pio run --target envdump
And after inspecting all flags, there is only -O0 option and no sign of -Og option. Therefore, what I’m trying to find out is the source of -Og option and how to access it and, of course, remove it. Any ideas?