Tail-Call-Frames.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Free Software" and "Free Software Needs
  8. Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
  9. and with the Back-Cover Texts as in (a) below.
  10. (a) The FSF's Back-Cover Text is: "You are free to copy and modify
  11. this GNU Manual. Buying copies from GNU Press supports the FSF in
  12. developing GNU and promoting software freedom." -->
  13. <!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <title>Debugging with GDB: Tail Call Frames</title>
  16. <meta name="description" content="Debugging with GDB: Tail Call Frames">
  17. <meta name="keywords" content="Debugging with GDB: Tail Call Frames">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
  24. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  25. <link href="Optimized-Code.html#Optimized-Code" rel="up" title="Optimized Code">
  26. <link href="Macros.html#Macros" rel="next" title="Macros">
  27. <link href="Inline-Functions.html#Inline-Functions" rel="previous" title="Inline Functions">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.indentedblock {margin-left: 3.2em}
  35. div.lisp {margin-left: 3.2em}
  36. div.smalldisplay {margin-left: 3.2em}
  37. div.smallexample {margin-left: 3.2em}
  38. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  39. div.smalllisp {margin-left: 3.2em}
  40. kbd {font-style:oblique}
  41. pre.display {font-family: inherit}
  42. pre.format {font-family: inherit}
  43. pre.menu-comment {font-family: serif}
  44. pre.menu-preformatted {font-family: serif}
  45. pre.smalldisplay {font-family: inherit; font-size: smaller}
  46. pre.smallexample {font-size: smaller}
  47. pre.smallformat {font-family: inherit; font-size: smaller}
  48. pre.smalllisp {font-size: smaller}
  49. span.nocodebreak {white-space:nowrap}
  50. span.nolinebreak {white-space:nowrap}
  51. span.roman {font-family:serif; font-weight:normal}
  52. span.sansserif {font-family:sans-serif; font-weight:normal}
  53. ul.no-bullet {list-style: none}
  54. -->
  55. </style>
  56. </head>
  57. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  58. <a name="Tail-Call-Frames"></a>
  59. <div class="header">
  60. <p>
  61. Previous: <a href="Inline-Functions.html#Inline-Functions" accesskey="p" rel="previous">Inline Functions</a>, Up: <a href="Optimized-Code.html#Optimized-Code" accesskey="u" rel="up">Optimized Code</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  62. </div>
  63. <hr>
  64. <a name="Tail-Call-Frames-1"></a>
  65. <h3 class="section">11.2 Tail Call Frames</h3>
  66. <a name="index-tail-call-frames_002c-debugging"></a>
  67. <p>Function <code>B</code> can call function <code>C</code> in its very last statement. In
  68. unoptimized compilation the call of <code>C</code> is immediately followed by return
  69. instruction at the end of <code>B</code> code. Optimizing compiler may replace the
  70. call and return in function <code>B</code> into one jump to function <code>C</code>
  71. instead. Such use of a jump instruction is called <em>tail call</em>.
  72. </p>
  73. <p>During execution of function <code>C</code>, there will be no indication in the
  74. function call stack frames that it was tail-called from <code>B</code>. If function
  75. <code>A</code> regularly calls function <code>B</code> which tail-calls function <code>C</code>,
  76. then <small>GDB</small> will see <code>A</code> as the caller of <code>C</code>. However, in
  77. some cases <small>GDB</small> can determine that <code>C</code> was tail-called from
  78. <code>B</code>, and it will then create fictitious call frame for that, with the
  79. return address set up as if <code>B</code> called <code>C</code> normally.
  80. </p>
  81. <p>This functionality is currently supported only by DWARF 2 debugging format and
  82. the compiler has to produce &lsquo;<samp>DW_TAG_call_site</samp>&rsquo; tags. With
  83. <small>GCC</small>, you need to specify <samp>-O -g</samp> during compilation, to get
  84. this information.
  85. </p>
  86. <p><kbd>info frame</kbd> command (see <a href="Frame-Info.html#Frame-Info">Frame Info</a>) will indicate the tail call frame
  87. kind by text <code>tail call frame</code> such as in this sample <small>GDB</small> output:
  88. </p>
  89. <div class="smallexample">
  90. <pre class="smallexample">(gdb) x/i $pc - 2
  91. 0x40066b &lt;b(int, double)+11&gt;: jmp 0x400640 &lt;c(int, double)&gt;
  92. (gdb) info frame
  93. Stack level 1, frame at 0x7fffffffda30:
  94. rip = 0x40066d in b (amd64-entry-value.cc:59); saved rip 0x4004c5
  95. tail call frame, caller of frame at 0x7fffffffda30
  96. source language c++.
  97. Arglist at unknown address.
  98. Locals at unknown address, Previous frame's sp is 0x7fffffffda30
  99. </pre></div>
  100. <p>The detection of all the possible code path executions can find them ambiguous.
  101. There is no execution history stored (possible <a href="Reverse-Execution.html#Reverse-Execution">Reverse Execution</a> is never
  102. used for this purpose) and the last known caller could have reached the known
  103. callee by multiple different jump sequences. In such case <small>GDB</small> still
  104. tries to show at least all the unambiguous top tail callers and all the
  105. unambiguous bottom tail calees, if any.
  106. </p>
  107. <dl compact="compact">
  108. <dd><a name="set-debug-entry_002dvalues"></a></dd>
  109. <dt><code>set debug entry-values</code></dt>
  110. <dd><a name="index-set-debug-entry_002dvalues"></a>
  111. <p>When set to on, enables printing of analysis messages for both frame argument
  112. values at function entry and tail calls. It will show all the possible valid
  113. tail calls code paths it has considered. It will also print the intersection
  114. of them with the final unambiguous (possibly partial or even empty) code path
  115. result.
  116. </p>
  117. </dd>
  118. <dt><code>show debug entry-values</code></dt>
  119. <dd><a name="index-show-debug-entry_002dvalues"></a>
  120. <p>Show the current state of analysis messages printing for both frame argument
  121. values at function entry and tail calls.
  122. </p></dd>
  123. </dl>
  124. <p>The analysis messages for tail calls can for example show why the virtual tail
  125. call frame for function <code>c</code> has not been recognized (due to the indirect
  126. reference by variable <code>x</code>):
  127. </p>
  128. <div class="smallexample">
  129. <pre class="smallexample">static void __attribute__((noinline, noclone)) c (void);
  130. void (*x) (void) = c;
  131. static void __attribute__((noinline, noclone)) a (void) { x++; }
  132. static void __attribute__((noinline, noclone)) c (void) { a (); }
  133. int main (void) { x (); return 0; }
  134. Breakpoint 1, DW_OP_entry_value resolving cannot find
  135. DW_TAG_call_site 0x40039a in main
  136. a () at t.c:3
  137. 3 static void __attribute__((noinline, noclone)) a (void) { x++; }
  138. (gdb) bt
  139. #0 a () at t.c:3
  140. #1 0x000000000040039a in main () at t.c:5
  141. </pre></div>
  142. <p>Another possibility is an ambiguous virtual tail call frames resolution:
  143. </p>
  144. <div class="smallexample">
  145. <pre class="smallexample">int i;
  146. static void __attribute__((noinline, noclone)) f (void) { i++; }
  147. static void __attribute__((noinline, noclone)) e (void) { f (); }
  148. static void __attribute__((noinline, noclone)) d (void) { f (); }
  149. static void __attribute__((noinline, noclone)) c (void) { d (); }
  150. static void __attribute__((noinline, noclone)) b (void)
  151. { if (i) c (); else e (); }
  152. static void __attribute__((noinline, noclone)) a (void) { b (); }
  153. int main (void) { a (); return 0; }
  154. tailcall: initial: 0x4004d2(a) 0x4004ce(b) 0x4004b2(c) 0x4004a2(d)
  155. tailcall: compare: 0x4004d2(a) 0x4004cc(b) 0x400492(e)
  156. tailcall: reduced: 0x4004d2(a) |
  157. (gdb) bt
  158. #0 f () at t.c:2
  159. #1 0x00000000004004d2 in a () at t.c:8
  160. #2 0x0000000000400395 in main () at t.c:9
  161. </pre></div>
  162. <p>Frames #0 and #2 are real, #1 is a virtual tail call frame.
  163. The code can have possible execution paths <code>main&rarr;a&rarr;b&rarr;c&rarr;d&rarr;f</code> or
  164. <code>main&rarr;a&rarr;b&rarr;e&rarr;f</code>, <small>GDB</small> cannot find which one from the inferior state.
  165. </p>
  166. <p><code>initial:</code> state shows some random possible calling sequence <small>GDB</small>
  167. has found. It then finds another possible calling sequence - that one is
  168. prefixed by <code>compare:</code>. The non-ambiguous intersection of these two is
  169. printed as the <code>reduced:</code> calling sequence. That one could have many
  170. further <code>compare:</code> and <code>reduced:</code> statements as long as there remain
  171. any non-ambiguous sequence entries.
  172. </p>
  173. <p>For the frame of function <code>b</code> in both cases there are different possible
  174. <code>$pc</code> values (<code>0x4004cc</code> or <code>0x4004ce</code>), therefore this frame is
  175. also ambiguous. The only non-ambiguous frame is the one for function <code>a</code>,
  176. therefore this one is displayed to the user while the ambiguous frames are
  177. omitted.
  178. </p>
  179. <p>There can be also reasons why printing of frame argument values at function
  180. entry may fail:
  181. </p>
  182. <div class="smallexample">
  183. <pre class="smallexample">int v;
  184. static void __attribute__((noinline, noclone)) c (int i) { v++; }
  185. static void __attribute__((noinline, noclone)) a (int i);
  186. static void __attribute__((noinline, noclone)) b (int i) { a (i); }
  187. static void __attribute__((noinline, noclone)) a (int i)
  188. { if (i) b (i - 1); else c (0); }
  189. int main (void) { a (5); return 0; }
  190. (gdb) bt
  191. #0 c (i=i@entry=0) at t.c:2
  192. #1 0x0000000000400428 in a (DW_OP_entry_value resolving has found
  193. function &quot;a&quot; at 0x400420 can call itself via tail calls
  194. i=&lt;optimized out&gt;) at t.c:6
  195. #2 0x000000000040036e in main () at t.c:7
  196. </pre></div>
  197. <p><small>GDB</small> cannot find out from the inferior state if and how many times did
  198. function <code>a</code> call itself (via function <code>b</code>) as these calls would be
  199. tail calls. Such tail calls would modify the <code>i</code> variable, therefore
  200. <small>GDB</small> cannot be sure the value it knows would be right - <small>GDB</small>
  201. prints <code>&lt;optimized out&gt;</code> instead.
  202. </p>
  203. <hr>
  204. <div class="header">
  205. <p>
  206. Previous: <a href="Inline-Functions.html#Inline-Functions" accesskey="p" rel="previous">Inline Functions</a>, Up: <a href="Optimized-Code.html#Optimized-Code" accesskey="u" rel="up">Optimized Code</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  207. </div>
  208. </body>
  209. </html>