Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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.

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, 2012 at 21:41 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, 2020 at 14:48

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.