{"id":5479,"date":"2014-03-30T22:34:01","date_gmt":"2014-03-30T22:34:01","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/why-cant-errnos-value-be-printed-collection-of-common-programming-errors\/"},"modified":"2014-03-30T22:34:01","modified_gmt":"2014-03-30T22:34:01","slug":"why-cant-errnos-value-be-printed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/why-cant-errnos-value-be-printed-collection-of-common-programming-errors\/","title":{"rendered":"Why can&#39;t errno&#39;s value be printed?-Collection of common programming errors"},"content":{"rendered":"<p><code>errno<\/code> is actually required by the C standard to be a macro that expands to a modifiable lvalue. In the simplest case, it can expand to the name of a declared variable, but for implementations that need distinct <code>errno<\/code> objects for different threads, it&#8217;s typically defined something like this:<\/p>\n<pre><code>#define errno (*__errno_location ())\n<\/code><\/pre>\n<p><code>gdb<\/code> is usually able to evaluate function calls; for example, on my system:<\/p>\n<pre><code>(gdb) p __errno_location()\n$1 = -134383968\n(gdb) p errno\nCannot find thread-local variables on this target\n<\/code><\/pre>\n<p>The first printed value happens to be the low-order 32 bits of the pointer value returned by <code>__errno_location()<\/code>. I don&#8217;t know gdb well enough to explain that behavior, but it does demonstrate that it can execute function calls.<\/p>\n<p>As a workaround, you can modify the source code so that it saves either the address of <code>errno<\/code>, or its value, in a variable that gdb can display:<\/p>\n<pre><code>(gdb) l\n1       #include \n2       #include \n3       int main(void) {\n4           errno = 42; \/* arbitrary value *\/\n5           const int *errno_ptr = &amp;errno;\n6           int errno_value = errno;\n7           printf(\"%d %d %d\\n\", errno, errno_value, *errno_ptr);\n8       }\n(gdb) b 8\nBreakpoint 1 at 0x4005b6: file c.c, line 8.\n(gdb) r\nStarting program: \/home\/kst\/c \n42 42 42\n\nBreakpoint 1, main () at c.c:8\n8       }\n(gdb) p errno\nCannot find thread-local variables on this target\n(gdb) p errno_value\n$1 = 42\n(gdb) p *errno_ptr\n$2 = 42\n<\/code><\/pre>\n<p>The <code>*errno_ptr<\/code> approach has the advantage that you only have to assign it once &#8212; unless you&#8217;re debugging a multi-threaded program. In that case, the value of <code>&amp;errno<\/code> can vary depending on the thread in which you evaluate it.<\/p>\n<p>This is probably a bug, or at least a missing feature, in <code>gdb<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>errno is actually required by the C standard to be a macro that expands to a modifiable lvalue. In the simplest case, it can expand to the name of a declared variable, but for implementations that need distinct errno objects for different threads, it&#8217;s typically defined something like this: #define errno (*__errno_location ()) gdb is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5479","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5479"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5479\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}