7

I'm trying to find out when errno changes.

At first, I tried "watch errno" in gdb, which led to the error

Cannot find thread-local variables on this target

I was able to fix this by compiling with "-pthread". However, it still doesn't work and I now get the error

Cannot find shared library `/usr/lib/debug/lib/x86_64-linux-gnu/libc-2.13.so' in dynamic linker's load module list

when I type "watch errno". What do I need to do such that setting a watchpoint on errno works?

Hermann Speiche
  • 894
  • 1
  • 9
  • 16

1 Answers1

8

errno is not just a static variable anymore. Here's how it appears to userland apps on Linux (from my local /usr/include/x86_64-linux-gnu/bits/errno.h):

#   define errno (*__errno_location ())

This is to get error state per thread.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • 4
    Thanks, I now added "int *errno_p = __errno_location()" as the first statement in main to my program, and can now use "watch *errno_p" to detect when errno changes. Directly using "watch *__errno_location()" did not work for some reason. Are watchpoints on functions not allowed? Probably it is forbidden because it would lead to false behavior when they have sideeffects. – Hermann Speiche May 18 '12 at 21:41
  • 3
    Maybe something changed over the years, but `watch *(int*)__errno_location()` works fine, as long as you set it after glibc initialization (e.g. `b main`) and in the right thread (since the returned address is thread-local) – fzbd Dec 28 '20 at 14:48