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