Calling.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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: Calling</title>
  16. <meta name="description" content="Debugging with GDB: Calling">
  17. <meta name="keywords" content="Debugging with GDB: Calling">
  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="Altering.html#Altering" rel="up" title="Altering">
  26. <link href="Patching.html#Patching" rel="next" title="Patching">
  27. <link href="Returning.html#Returning" rel="previous" title="Returning">
  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="Calling"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Patching.html#Patching" accesskey="n" rel="next">Patching</a>, Previous: <a href="Returning.html#Returning" accesskey="p" rel="previous">Returning</a>, Up: <a href="Altering.html#Altering" accesskey="u" rel="up">Altering</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="Calling-Program-Functions"></a>
  65. <h3 class="section">17.5 Calling Program Functions</h3>
  66. <dl compact="compact">
  67. <dd><a name="index-calling-functions"></a>
  68. <a name="index-inferior-functions_002c-calling"></a>
  69. </dd>
  70. <dt><code>print <var>expr</var></code></dt>
  71. <dd><p>Evaluate the expression <var>expr</var> and display the resulting value.
  72. The expression may include calls to functions in the program being
  73. debugged.
  74. </p>
  75. <a name="index-call"></a>
  76. </dd>
  77. <dt><code>call <var>expr</var></code></dt>
  78. <dd><p>Evaluate the expression <var>expr</var> without displaying <code>void</code>
  79. returned values.
  80. </p>
  81. <p>You can use this variant of the <code>print</code> command if you want to
  82. execute a function from your program that does not return anything
  83. (a.k.a. <em>a void function</em>), but without cluttering the output
  84. with <code>void</code> returned values that <small>GDB</small> will otherwise
  85. print. If the result is not void, it is printed and saved in the
  86. value history.
  87. </p></dd>
  88. </dl>
  89. <p>It is possible for the function you call via the <code>print</code> or
  90. <code>call</code> command to generate a signal (e.g., if there&rsquo;s a bug in
  91. the function, or if you passed it incorrect arguments). What happens
  92. in that case is controlled by the <code>set unwindonsignal</code> command.
  93. </p>
  94. <p>Similarly, with a C<tt>++</tt> program it is possible for the function you
  95. call via the <code>print</code> or <code>call</code> command to generate an
  96. exception that is not handled due to the constraints of the dummy
  97. frame. In this case, any exception that is raised in the frame, but has
  98. an out-of-frame exception handler will not be found. GDB builds a
  99. dummy-frame for the inferior function call, and the unwinder cannot
  100. seek for exception handlers outside of this dummy-frame. What happens
  101. in that case is controlled by the
  102. <code>set unwind-on-terminating-exception</code> command.
  103. </p>
  104. <dl compact="compact">
  105. <dt><code>set unwindonsignal</code></dt>
  106. <dd><a name="index-set-unwindonsignal"></a>
  107. <a name="index-unwind-stack-in-called-functions"></a>
  108. <a name="index-call-dummy-stack-unwinding"></a>
  109. <p>Set unwinding of the stack if a signal is received while in a function
  110. that <small>GDB</small> called in the program being debugged. If set to on,
  111. <small>GDB</small> unwinds the stack it created for the call and restores
  112. the context to what it was before the call. If set to off (the
  113. default), <small>GDB</small> stops in the frame where the signal was
  114. received.
  115. </p>
  116. </dd>
  117. <dt><code>show unwindonsignal</code></dt>
  118. <dd><a name="index-show-unwindonsignal"></a>
  119. <p>Show the current setting of stack unwinding in the functions called by
  120. <small>GDB</small>.
  121. </p>
  122. </dd>
  123. <dt><code>set unwind-on-terminating-exception</code></dt>
  124. <dd><a name="index-set-unwind_002don_002dterminating_002dexception"></a>
  125. <a name="index-unwind-stack-in-called-functions-with-unhandled-exceptions"></a>
  126. <a name="index-call-dummy-stack-unwinding-on-unhandled-exception_002e"></a>
  127. <p>Set unwinding of the stack if a C<tt>++</tt> exception is raised, but left
  128. unhandled while in a function that <small>GDB</small> called in the program being
  129. debugged. If set to on (the default), <small>GDB</small> unwinds the stack
  130. it created for the call and restores the context to what it was before
  131. the call. If set to off, <small>GDB</small> the exception is delivered to
  132. the default C<tt>++</tt> exception handler and the inferior terminated.
  133. </p>
  134. </dd>
  135. <dt><code>show unwind-on-terminating-exception</code></dt>
  136. <dd><a name="index-show-unwind_002don_002dterminating_002dexception"></a>
  137. <p>Show the current setting of stack unwinding in the functions called by
  138. <small>GDB</small>.
  139. </p>
  140. </dd>
  141. <dt><code>set may-call-functions</code></dt>
  142. <dd><a name="index-set-may_002dcall_002dfunctions"></a>
  143. <a name="index-disabling-calling-functions-in-the-program"></a>
  144. <a name="index-calling-functions-in-the-program_002c-disabling"></a>
  145. <p>Set permission to call functions in the program.
  146. This controls whether <small>GDB</small> will attempt to call functions in
  147. the program, such as with expressions in the <code>print</code> command. It
  148. defaults to <code>on</code>.
  149. </p>
  150. <p>To call a function in the program, <small>GDB</small> has to temporarily
  151. modify the state of the inferior. This has potentially undesired side
  152. effects. Also, having <small>GDB</small> call nested functions is likely to
  153. be erroneous and may even crash the program being debugged. You can
  154. avoid such hazards by forbidding <small>GDB</small> from calling functions
  155. in the program being debugged. If calling functions in the program
  156. is forbidden, GDB will throw an error when a command (such as printing
  157. an expression) starts a function call in the program.
  158. </p>
  159. </dd>
  160. <dt><code>show may-call-functions</code></dt>
  161. <dd><a name="index-show-may_002dcall_002dfunctions"></a>
  162. <p>Show permission to call functions in the program.
  163. </p>
  164. </dd>
  165. </dl>
  166. <a name="Calling-functions-with-no-debug-info"></a>
  167. <h4 class="subsection">17.5.1 Calling functions with no debug info</h4>
  168. <a name="index-no-debug-info-functions"></a>
  169. <p>Sometimes, a function you wish to call is missing debug information.
  170. In such case, <small>GDB</small> does not know the type of the function,
  171. including the types of the function&rsquo;s parameters. To avoid calling
  172. the inferior function incorrectly, which could result in the called
  173. function functioning erroneously and even crash, <small>GDB</small> refuses
  174. to call the function unless you tell it the type of the function.
  175. </p>
  176. <p>For prototyped (i.e. ANSI/ISO style) functions, there are two ways
  177. to do that. The simplest is to cast the call to the function&rsquo;s
  178. declared return type. For example:
  179. </p>
  180. <div class="smallexample">
  181. <pre class="smallexample">(gdb) p getenv (&quot;PATH&quot;)
  182. 'getenv' has unknown return type; cast the call to its declared return type
  183. (gdb) p (char *) getenv (&quot;PATH&quot;)
  184. $1 = 0x7fffffffe7ba &quot;/usr/local/bin:/&quot;...
  185. </pre></div>
  186. <p>Casting the return type of a no-debug function is equivalent to
  187. casting the function to a pointer to a prototyped function that has a
  188. prototype that matches the types of the passed-in arguments, and
  189. calling that. I.e., the call above is equivalent to:
  190. </p>
  191. <div class="smallexample">
  192. <pre class="smallexample">(gdb) p ((char * (*) (const char *)) getenv) (&quot;PATH&quot;)
  193. </pre></div>
  194. <p>and given this prototyped C or C++ function with float parameters:
  195. </p>
  196. <div class="smallexample">
  197. <pre class="smallexample">float multiply (float v1, float v2) { return v1 * v2; }
  198. </pre></div>
  199. <p>these calls are equivalent:
  200. </p>
  201. <div class="smallexample">
  202. <pre class="smallexample">(gdb) p (float) multiply (2.0f, 3.0f)
  203. (gdb) p ((float (*) (float, float)) multiply) (2.0f, 3.0f)
  204. </pre></div>
  205. <p>If the function you wish to call is declared as unprototyped (i.e.
  206. old K&amp;R style), you must use the cast-to-function-pointer syntax, so
  207. that <small>GDB</small> knows that it needs to apply default argument
  208. promotions (promote float arguments to double). See <a href="ABI.html#ABI">float
  209. promotion</a>. For example, given this unprototyped C function with
  210. float parameters, and no debug info:
  211. </p>
  212. <div class="smallexample">
  213. <pre class="smallexample">float
  214. multiply_noproto (v1, v2)
  215. float v1, v2;
  216. {
  217. return v1 * v2;
  218. }
  219. </pre></div>
  220. <p>you call it like this:
  221. </p>
  222. <div class="smallexample">
  223. <pre class="smallexample"> (gdb) p ((float (*) ()) multiply_noproto) (2.0f, 3.0f)
  224. </pre></div>
  225. <hr>
  226. <div class="header">
  227. <p>
  228. Next: <a href="Patching.html#Patching" accesskey="n" rel="next">Patching</a>, Previous: <a href="Returning.html#Returning" accesskey="p" rel="previous">Returning</a>, Up: <a href="Altering.html#Altering" accesskey="u" rel="up">Altering</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>
  229. </div>
  230. </body>
  231. </html>