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-2021 Free Software Foundation, Inc. 4 5Permission is granted to copy, distribute and/or modify this document 6under the terms of the GNU Free Documentation License, Version 1.3 or 7any later version published by the Free Software Foundation; with the 8Invariant Sections being "Free Software" and "Free Software Needs 9Free Documentation", with the Front-Cover Texts being "A GNU Manual," 10and with the Back-Cover Texts as in (a) below. 11 12(a) The FSF's Back-Cover Text is: "You are free to copy and modify 13this GNU Manual. Buying copies from GNU Press supports the FSF in 14developing GNU and promoting software freedom." --> 15<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ --> 16<head> 17<title>Debugging with GDB: Calling</title> 18 19<meta name="description" content="Debugging with GDB: Calling"> 20<meta name="keywords" content="Debugging with GDB: Calling"> 21<meta name="resource-type" content="document"> 22<meta name="distribution" content="global"> 23<meta name="Generator" content="makeinfo"> 24<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 25<link href="index.html#Top" rel="start" title="Top"> 26<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> 27<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> 28<link href="Altering.html#Altering" rel="up" title="Altering"> 29<link href="Patching.html#Patching" rel="next" title="Patching"> 30<link href="Returning.html#Returning" rel="previous" title="Returning"> 31<style type="text/css"> 32<!-- 33a.summary-letter {text-decoration: none} 34blockquote.smallquotation {font-size: smaller} 35div.display {margin-left: 3.2em} 36div.example {margin-left: 3.2em} 37div.indentedblock {margin-left: 3.2em} 38div.lisp {margin-left: 3.2em} 39div.smalldisplay {margin-left: 3.2em} 40div.smallexample {margin-left: 3.2em} 41div.smallindentedblock {margin-left: 3.2em; font-size: smaller} 42div.smalllisp {margin-left: 3.2em} 43kbd {font-style:oblique} 44pre.display {font-family: inherit} 45pre.format {font-family: inherit} 46pre.menu-comment {font-family: serif} 47pre.menu-preformatted {font-family: serif} 48pre.smalldisplay {font-family: inherit; font-size: smaller} 49pre.smallexample {font-size: smaller} 50pre.smallformat {font-family: inherit; font-size: smaller} 51pre.smalllisp {font-size: smaller} 52span.nocodebreak {white-space:nowrap} 53span.nolinebreak {white-space:nowrap} 54span.roman {font-family:serif; font-weight:normal} 55span.sansserif {font-family:sans-serif; font-weight:normal} 56ul.no-bullet {list-style: none} 57--> 58</style> 59 60 61</head> 62 63<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000"> 64<a name="Calling"></a> 65<div class="header"> 66<p> 67Next: <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> [<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> 68</div> 69<hr> 70<a name="Calling-Program-Functions"></a> 71<h3 class="section">17.5 Calling Program Functions</h3> 72 73<dl compact="compact"> 74<dd><a name="index-calling-functions"></a> 75<a name="index-inferior-functions_002c-calling"></a> 76</dd> 77<dt><code>print <var>expr</var></code></dt> 78<dd><p>Evaluate the expression <var>expr</var> and display the resulting value. 79The expression may include calls to functions in the program being 80debugged. 81</p> 82<a name="index-call"></a> 83</dd> 84<dt><code>call <var>expr</var></code></dt> 85<dd><p>Evaluate the expression <var>expr</var> without displaying <code>void</code> 86returned values. 87</p> 88<p>You can use this variant of the <code>print</code> command if you want to 89execute a function from your program that does not return anything 90(a.k.a. <em>a void function</em>), but without cluttering the output 91with <code>void</code> returned values that <small>GDB</small> will otherwise 92print. If the result is not void, it is printed and saved in the 93value history. 94</p></dd> 95</dl> 96 97<p>It is possible for the function you call via the <code>print</code> or 98<code>call</code> command to generate a signal (e.g., if there’s a bug in 99the function, or if you passed it incorrect arguments). What happens 100in that case is controlled by the <code>set unwindonsignal</code> command. 101</p> 102<p>Similarly, with a C<tt>++</tt> program it is possible for the function you 103call via the <code>print</code> or <code>call</code> command to generate an 104exception that is not handled due to the constraints of the dummy 105frame. In this case, any exception that is raised in the frame, but has 106an out-of-frame exception handler will not be found. GDB builds a 107dummy-frame for the inferior function call, and the unwinder cannot 108seek for exception handlers outside of this dummy-frame. What happens 109in that case is controlled by the 110<code>set unwind-on-terminating-exception</code> command. 111</p> 112<dl compact="compact"> 113<dt><code>set unwindonsignal</code></dt> 114<dd><a name="index-set-unwindonsignal"></a> 115<a name="index-unwind-stack-in-called-functions"></a> 116<a name="index-call-dummy-stack-unwinding"></a> 117<p>Set unwinding of the stack if a signal is received while in a function 118that <small>GDB</small> called in the program being debugged. If set to on, 119<small>GDB</small> unwinds the stack it created for the call and restores 120the context to what it was before the call. If set to off (the 121default), <small>GDB</small> stops in the frame where the signal was 122received. 123</p> 124</dd> 125<dt><code>show unwindonsignal</code></dt> 126<dd><a name="index-show-unwindonsignal"></a> 127<p>Show the current setting of stack unwinding in the functions called by 128<small>GDB</small>. 129</p> 130</dd> 131<dt><code>set unwind-on-terminating-exception</code></dt> 132<dd><a name="index-set-unwind_002don_002dterminating_002dexception"></a> 133<a name="index-unwind-stack-in-called-functions-with-unhandled-exceptions"></a> 134<a name="index-call-dummy-stack-unwinding-on-unhandled-exception_002e"></a> 135<p>Set unwinding of the stack if a C<tt>++</tt> exception is raised, but left 136unhandled while in a function that <small>GDB</small> called in the program being 137debugged. If set to on (the default), <small>GDB</small> unwinds the stack 138it created for the call and restores the context to what it was before 139the call. If set to off, <small>GDB</small> the exception is delivered to 140the default C<tt>++</tt> exception handler and the inferior terminated. 141</p> 142</dd> 143<dt><code>show unwind-on-terminating-exception</code></dt> 144<dd><a name="index-show-unwind_002don_002dterminating_002dexception"></a> 145<p>Show the current setting of stack unwinding in the functions called by 146<small>GDB</small>. 147</p> 148</dd> 149<dt><code>set may-call-functions</code></dt> 150<dd><a name="index-set-may_002dcall_002dfunctions"></a> 151<a name="index-disabling-calling-functions-in-the-program"></a> 152<a name="index-calling-functions-in-the-program_002c-disabling"></a> 153<p>Set permission to call functions in the program. 154This controls whether <small>GDB</small> will attempt to call functions in 155the program, such as with expressions in the <code>print</code> command. It 156defaults to <code>on</code>. 157</p> 158<p>To call a function in the program, <small>GDB</small> has to temporarily 159modify the state of the inferior. This has potentially undesired side 160effects. Also, having <small>GDB</small> call nested functions is likely to 161be erroneous and may even crash the program being debugged. You can 162avoid such hazards by forbidding <small>GDB</small> from calling functions 163in the program being debugged. If calling functions in the program 164is forbidden, GDB will throw an error when a command (such as printing 165an expression) starts a function call in the program. 166</p> 167</dd> 168<dt><code>show may-call-functions</code></dt> 169<dd><a name="index-show-may_002dcall_002dfunctions"></a> 170<p>Show permission to call functions in the program. 171</p> 172</dd> 173</dl> 174 175<a name="Calling-Functions-with-No-Debug-Info"></a> 176<h4 class="subsection">17.5.1 Calling Functions with No Debug Info</h4> 177 178<a name="index-no-debug-info-functions"></a> 179<p>Sometimes, a function you wish to call is missing debug information. 180In such case, <small>GDB</small> does not know the type of the function, 181including the types of the function’s parameters. To avoid calling 182the inferior function incorrectly, which could result in the called 183function functioning erroneously and even crash, <small>GDB</small> refuses 184to call the function unless you tell it the type of the function. 185</p> 186<p>For prototyped (i.e. ANSI/ISO style) functions, there are two ways 187to do that. The simplest is to cast the call to the function’s 188declared return type. For example: 189</p> 190<div class="smallexample"> 191<pre class="smallexample">(gdb) p getenv ("PATH") 192'getenv' has unknown return type; cast the call to its declared return type 193(gdb) p (char *) getenv ("PATH") 194$1 = 0x7fffffffe7ba "/usr/local/bin:/"... 195</pre></div> 196 197<p>Casting the return type of a no-debug function is equivalent to 198casting the function to a pointer to a prototyped function that has a 199prototype that matches the types of the passed-in arguments, and 200calling that. I.e., the call above is equivalent to: 201</p> 202<div class="smallexample"> 203<pre class="smallexample">(gdb) p ((char * (*) (const char *)) getenv) ("PATH") 204</pre></div> 205 206<p>and given this prototyped C or C++ function with float parameters: 207</p> 208<div class="smallexample"> 209<pre class="smallexample">float multiply (float v1, float v2) { return v1 * v2; } 210</pre></div> 211 212<p>these calls are equivalent: 213</p> 214<div class="smallexample"> 215<pre class="smallexample">(gdb) p (float) multiply (2.0f, 3.0f) 216(gdb) p ((float (*) (float, float)) multiply) (2.0f, 3.0f) 217</pre></div> 218 219<p>If the function you wish to call is declared as unprototyped (i.e. 220old K&R style), you must use the cast-to-function-pointer syntax, so 221that <small>GDB</small> knows that it needs to apply default argument 222promotions (promote float arguments to double). See <a href="ABI.html#ABI">float 223promotion</a>. For example, given this unprototyped C function with 224float parameters, and no debug info: 225</p> 226<div class="smallexample"> 227<pre class="smallexample">float 228multiply_noproto (v1, v2) 229 float v1, v2; 230{ 231 return v1 * v2; 232} 233</pre></div> 234 235<p>you call it like this: 236</p> 237<div class="smallexample"> 238<pre class="smallexample"> (gdb) p ((float (*) ()) multiply_noproto) (2.0f, 3.0f) 239</pre></div> 240 241<hr> 242<div class="header"> 243<p> 244Next: <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> [<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> 245</div> 246 247 248 249</body> 250</html> 251