--- title: Bitesize - Value Optimized Out tags: bitesize, gdb, gcc --- # Value Optimized Out :::warning **Question**: The other day, when I was debugging a program with `gdb` debugger. When I tried to print the value of a variable while tracing a function call, `gdb` says `<optimized out>` in the console, not displaying the value. ::: ```shell (gdb) print a $1 = <optimized out> ``` Technically, this is a compiler behavior which wouldn't be documented in the C Standard. Having said that, I decided to quickly search the Standard for the keyword, `optimized out`. Surprisingly, in section 6.7.3, this keywork is mentioned while it explains the `volatile declaration`. > A `volatile` declaration may be used to describe an object corresponding to a memory-mapped input/output port or an object accessed by an asynchronously interrupting fucntion. Actions on objects so declared shall not be **"optimized out"** by an implementation or reordered except as permitted by the rules for evaluating expressions. I furhter looked into the documentation called, *Debugging with GDB*. In Chapter 8.2, it says > The values of arguments that were not saved in their stack frames are shown as `<optimized out>`. ## Digest Modern compilers such as `gcc` are able to perform various optimizations based on syntax and semantic analysis of the code at compile time. The goal of such optimizations is to improve program execution speed and/or reduce binary size, and they typically come with the cost of extra compilation time or reduce debuggability of the program. The `<value optimized out>` message in `gdb` is one symptom of such compiler optimizations. To view the optimized-out value of a variable during debugging, you need to turn off `gcc` compiler optimization, either on a per-variable basis, or program-wide, as described below. ### Per-variable basis If you are interested in a particular variable in `gdb`, you can declare the variable as "volatile" and recompile the code. This will make the compiler turn off compiler optimization for that variable. ```clike volatile int a = 0; ``` If you declare a variable as volatile, it means that the variable can be modified externally (e.g., by the **operating system**, a **signal handler**, or **hardware interrupt**, etc). This essentially tells the compiler that the variable's value in memory can be changed at any time. Thus the compiler must not perform any optimization on the variable, which makes the variable accessible to `gdb` debugger as well. ### Entire Program Another option is to disable the optimization at the compile time by using the compilation flags, `-O0`. :::info :scales: This will reduce compilation time and allows `gdb` to inspect variables properly, but at the cost of possibly reduced run-time program performance. ::: ## Futher Questions :::warning :question: The thing is some time we could not do the aforementioned actions, such as we want to debug Linux kernel. For instance, when you find the refcount in gpiochip is incorrect and hope to debug it through the `gdb` in host. :::