{"id":18079,"date":"2024-12-16T17:18:00","date_gmt":"2024-12-16T17:18:00","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=18079"},"modified":"2024-12-16T17:19:37","modified_gmt":"2024-12-16T17:19:37","slug":"stack-unwinding","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=18079","title":{"rendered":"Stack unwinding"},"content":{"rendered":"<p><strong>Mechanism<\/strong><\/p>\n<p>1. <u>Frame pointers<\/u> &#8230; the old default way<\/p>\n<ul>\n<li>Frame pointers are enabled by default.<\/li>\n<li>Strong optimization (&#8220;-O2&#8221;, &#8220;-O3&#8221;) will implicitly disable frame pointers.<\/li>\n<\/ul>\n<p>2. <u>Unwind tables<\/u> &#8230; a separate binary section<\/p>\n<ul>\n<li>Unwind tables are enabled by default.<\/li>\n<li>Disabling exceptions (&#8220;-fno-exceptions&#8221;) may implicitly disable unwind tables.<\/li>\n<\/ul>\n<hr>\n<p><strong>Showcase<\/strong><\/p>\n<pre class=\"brush: cpp; gutter: false; title: test.cpp; notranslate\" title=\"test.cpp\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid print()\r\n{\r\n    printf(&quot;Test&quot;);\r\n    *((char*)NULL) = 0;\r\n}\r\n\r\nint main()\r\n{\r\n    print();\r\n    return 0;\r\n}\r\n<\/pre>\n<p><u>With backtrace<\/u><\/p>\n<pre>\r\n% clang++ test.cpp -w -O1 -fno-inline\r\n% lldb a.out                         \r\n(lldb) r\r\nTarget 0: (a.out) stopped.\r\n(lldb) bt\r\n* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x100003f84)\r\n  * frame #0: 0x0000000100003f84 a.out`print() + 20\r\n    frame #1: 0x0000000100003f94 a.out`main + 12\r\n    frame #2: 0x000000018249f154 dyld`start + 2476\r\n<\/pre>\n<p><u>Without backtrace<\/u><\/p>\n<pre>\r\n% clang++ test.cpp -w -O1 -fno-inline -fomit-frame-pointer -fno-unwind-tables\r\n% lldb a.out\r\n(lldb) r\r\nTarget 0: (a.out) stopped.\r\n(lldb) bt\r\n* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x100003f8c)\r\n  * frame #0: 0x0000000100003f8c a.out`print() + 16\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Mechanism 1. Frame pointers &#8230; the old default way Frame pointers are enabled by default. Strong optimization (&#8220;-O2&#8221;, &#8220;-O3&#8221;) will<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[28],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18079"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18079"}],"version-history":[{"count":26,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18079\/revisions"}],"predecessor-version":[{"id":18119,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18079\/revisions\/18119"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18079"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}