1<html lang="en"> 2<head> 3<title>Returning - Debugging with GDB</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Debugging with GDB"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Altering.html#Altering" title="Altering"> 9<link rel="prev" href="Signaling.html#Signaling" title="Signaling"> 10<link rel="next" href="Calling.html#Calling" title="Calling"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988-2013 Free Software Foundation, Inc. 14 15Permission is granted to copy, distribute and/or modify this document 16under the terms of the GNU Free Documentation License, Version 1.3 or 17any later version published by the Free Software Foundation; with the 18Invariant Sections being ``Free Software'' and ``Free Software Needs 19Free Documentation'', with the Front-Cover Texts being ``A GNU Manual,'' 20and with the Back-Cover Texts as in (a) below. 21 22(a) The FSF's Back-Cover Text is: ``You are free to copy and modify 23this GNU Manual. Buying copies from GNU Press supports the FSF in 24developing GNU and promoting software freedom.'' 25--> 26<meta http-equiv="Content-Style-Type" content="text/css"> 27<style type="text/css"><!-- 28 pre.display { font-family:inherit } 29 pre.format { font-family:inherit } 30 pre.smalldisplay { font-family:inherit; font-size:smaller } 31 pre.smallformat { font-family:inherit; font-size:smaller } 32 pre.smallexample { font-size:smaller } 33 pre.smalllisp { font-size:smaller } 34 span.sc { font-variant:small-caps } 35 span.roman { font-family:serif; font-weight:normal; } 36 span.sansserif { font-family:sans-serif; font-weight:normal; } 37--></style> 38<link rel="stylesheet" type="text/css" href="../cs.css"> 39</head> 40<body> 41<div class="node"> 42<a name="Returning"></a> 43<p> 44Next: <a rel="next" accesskey="n" href="Calling.html#Calling">Calling</a>, 45Previous: <a rel="previous" accesskey="p" href="Signaling.html#Signaling">Signaling</a>, 46Up: <a rel="up" accesskey="u" href="Altering.html#Altering">Altering</a> 47<hr> 48</div> 49 50<h3 class="section">17.4 Returning from a Function</h3> 51 52 53<a name="index-returning-from-a-function-1039"></a> 54<a name="index-return-1040"></a> 55<dl><dt><code>return</code><dt><code>return </code><var>expression</var><dd>You can cancel execution of a function call with the <code>return</code> 56command. If you give an 57<var>expression</var> argument, its value is used as the function's return 58value. 59</dl> 60 61 <p>When you use <code>return</code>, <span class="sc">gdb</span> discards the selected stack frame 62(and all frames within it). You can think of this as making the 63discarded frame return prematurely. If you wish to specify a value to 64be returned, give that value as the argument to <code>return</code>. 65 66 <p>This pops the selected stack frame (see <a href="Selection.html#Selection">Selecting a Frame</a>), and any other frames inside of it, leaving its caller as the 67innermost remaining frame. That frame becomes selected. The 68specified value is stored in the registers used for returning values 69of functions. 70 71 <p>The <code>return</code> command does not resume execution; it leaves the 72program stopped in the state that would exist if the function had just 73returned. In contrast, the <code>finish</code> command (see <a href="Continuing-and-Stepping.html#Continuing-and-Stepping">Continuing and Stepping</a>) resumes execution until the 74selected stack frame returns naturally. 75 76 <p><span class="sc">gdb</span> needs to know how the <var>expression</var> argument should be set for 77the inferior. The concrete registers assignment depends on the OS ABI and the 78type being returned by the selected stack frame. For example it is common for 79OS ABI to return floating point values in FPU registers while integer values in 80CPU registers. Still some ABIs return even floating point values in CPU 81registers. Larger integer widths (such as <code>long long int</code>) also have 82specific placement rules. <span class="sc">gdb</span> already knows the OS ABI from its 83current target so it needs to find out also the type being returned to make the 84assignment into the right register(s). 85 86 <p>Normally, the selected stack frame has debug info. <span class="sc">gdb</span> will always 87use the debug info instead of the implicit type of <var>expression</var> when the 88debug info is available. For example, if you type <kbd>return -1</kbd>, and the 89function in the current stack frame is declared to return a <code>long long 90int</code>, <span class="sc">gdb</span> transparently converts the implicit <code>int</code> value of -1 91into a <code>long long int</code>: 92 93<pre class="smallexample"> Breakpoint 1, func () at gdb.base/return-nodebug.c:29 94 29 return 31; 95 (gdb) return -1 96 Make func return now? (y or n) y 97 #0 0x004004f6 in main () at gdb.base/return-nodebug.c:43 98 43 printf ("result=%lld\n", func ()); 99 (gdb) 100</pre> 101 <p>However, if the selected stack frame does not have a debug info, e.g., if the 102function was compiled without debug info, <span class="sc">gdb</span> has to find out the type 103to return from user. Specifying a different type by mistake may set the value 104in different inferior registers than the caller code expects. For example, 105typing <kbd>return -1</kbd> with its implicit type <code>int</code> would set only a part 106of a <code>long long int</code> result for a debug info less function (on 32-bit 107architectures). Therefore the user is required to specify the return type by 108an appropriate cast explicitly: 109 110<pre class="smallexample"> Breakpoint 2, 0x0040050b in func () 111 (gdb) return -1 112 Return value type not available for selected stack frame. 113 Please use an explicit cast of the value to return. 114 (gdb) return (long long int) -1 115 Make selected stack frame return now? (y or n) y 116 #0 0x00400526 in main () 117 (gdb) 118</pre> 119 </body></html> 120 121