# Optimized code debugging

**URL:** <https://ziggit.dev/t/optimized-code-debugging/6210>\
**Category:** Help\
**Tags:** debugger\
**Created:** [October 1, 2024, 12:57pm UTC](https://ziggit.dev/t/optimized-code-debugging/6210 "2024-10-01T12:57:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssilnicki-dev](https://ziggit.dev/user_avatar/ziggit.dev/ssilnicki-dev/32/2151_2.png) [@ssilnicki-dev](https://ziggit.dev/u/ssilnicki-dev)\
**Post date:** [October 1, 2024, 12:57pm UTC](https://ziggit.dev/t/optimized-code-debugging/6210/1 "2024-10-01T12:57:49Z")

</div>

Hi All!

what is best (convenient) option to suppress optimizing out some variable in order to break and observe its value in debug session?

So far I have used to use this controversial practice of assembly injection:

```zig
    var _some_var: *@TypeOf(some_var) = &some_var;
    asm volatile ("push {r0-r7}");
    asm volatile ("mov r0, %[addr]"
        :
        : [addr] "r" (&_some_var),
    );
    asm volatile ("pop {r0-r7}");
    asm volatile ("nop");

```

I’m sure, there have to be a better way…

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [October 1, 2024, 1:16pm UTC](https://ziggit.dev/t/optimized-code-debugging/6210/2 "2024-10-01T13:16:12Z")

</div>

The mechanisms are:

1. [Atomics](https://ziglang.org/documentation/master/#Atomics)

```zig
const _var = @atomicLoad(@TypeOf(some_var), &some_var, .seq_cst);

```

2. [doNotOptimizeAway](https://ziglang.org/documentation/master/std/#std.mem.doNotOptimizeAway) See: [How to use std.mem.doNotOptimizeAway?](https://ziggit.dev/t/how-to-use-std-mem-donotoptimizeaway/4677)
3. [volatile](https://ziglang.org/documentation/master/#volatile) Note: [Benchmarking isDigit - #17 by andrewrk](https://ziggit.dev/t/benchmarking-isdigit/4604/17)

---

<div class="post-metadata">

**Author:** ![ssilnicki-dev](https://ziggit.dev/user_avatar/ziggit.dev/ssilnicki-dev/32/2151_2.png) [@ssilnicki-dev](https://ziggit.dev/u/ssilnicki-dev)\
**Post date:** [October 1, 2024, 3:07pm UTC](https://ziggit.dev/t/optimized-code-debugging/6210/3 "2024-10-01T15:07:16Z")

</div>

thank you, @dimdin !

I tried to utilize atomic approach, but it seems I did it wrong, as soon as \_atomic\_report looks to be optimized out anyway (first screenshot). It only shows up if the source data (report) is preserved as a symbol in the binary with my recent approach (second screnshot).

 ![Screenshot from 2024-10-01 18-00-10](https://ziggit.dev/uploads/default/original/2X/b/b28ab07f19dd59b9c334256d9d1f64ae621e6be0.png)  
 ![Screenshot from 2024-10-01 17-56-11](https://ziggit.dev/uploads/default/original/2X/f/f868a1250f762fd4e13a92cdb9e82b10aa3fcdad.png)

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [October 1, 2024, 10:23pm UTC](https://ziggit.dev/t/optimized-code-debugging/6210/4 "2024-10-01T22:23:42Z")

</div>

Do you mean that the value is not visible in the debugger as local variable?

What editor and what debugger are you using?

---

<div class="post-metadata">

**Author:** ![ssilnicki-dev](https://ziggit.dev/user_avatar/ziggit.dev/ssilnicki-dev/32/2151_2.png) [@ssilnicki-dev](https://ziggit.dev/u/ssilnicki-dev)\
**Post date:** [October 1, 2024, 10:37pm UTC](https://ziggit.dev/t/optimized-code-debugging/6210/5 "2024-10-01T22:37:13Z")

</div>

yes, I’d like to be able to see some locals in optimized binary. I use emacs + lldb + openocd. It works great with debug binary, when all symbols in place, but llvm optimizer does its job carefully, so it is tricky to make it keep the symbol with ‘fast’ or ‘small’ target.

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [October 1, 2024, 10:47pm UTC](https://ziggit.dev/t/optimized-code-debugging/6210/6 "2024-10-01T22:47:33Z")

</div>

Try building with the option `-fno-strip` (Keep debug symbols).  
In build.zig executable options is:

```zig
const exe = b.addExecutable(.{
    .strip = false,
    ...
});

```

---

<div class="post-metadata">

**Author:** ![ssilnicki-dev](https://ziggit.dev/user_avatar/ziggit.dev/ssilnicki-dev/32/2151_2.png) [@ssilnicki-dev](https://ziggit.dev/u/ssilnicki-dev)\
**Post date:** [October 1, 2024, 10:59pm UTC](https://ziggit.dev/t/optimized-code-debugging/6210/7 "2024-10-01T22:59:38Z")

</div>

of course, I use it. The problem is that llvm’s optimizer may not keep local variables, using general registers for vars, so no memory to refer to and no debug info is required/present in the optimized binary. I struggle to keep selected variables to be kept as memory references. If they are references, debug info (symbol name, mapping, etc.) is kept in binary and available during debug session.

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [October 1, 2024, 11:10pm UTC](https://ziggit.dev/t/optimized-code-debugging/6210/8 "2024-10-01T23:10:10Z")

</div>

`std.mem.doNotOptimizeAway(&report);` makes the compiler believe the address of `report` is used and allocates the variable in memory (because a register does not have an address).

---

<div class="post-metadata">

**Author:** ![ssilnicki-dev](https://ziggit.dev/user_avatar/ziggit.dev/ssilnicki-dev/32/2151_2.png) [@ssilnicki-dev](https://ziggit.dev/u/ssilnicki-dev)\
**Post date:** [October 1, 2024, 11:23pm UTC](https://ziggit.dev/t/optimized-code-debugging/6210/9 "2024-10-01T23:23:53Z")

</div>

it has worked! Thank you, @dimdin !

 ![Screenshot from 2024-10-02 02-22-53](https://ziggit.dev/uploads/default/original/2X/b/b580689e6a15190c9881cd7e3d824173fa6f0280.png)
