1<html lang="en"> 2<head> 3<title>Values From Inferior - 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="Python-API.html#Python-API" title="Python API"> 9<link rel="prev" href="Exception-Handling.html#Exception-Handling" title="Exception Handling"> 10<link rel="next" href="Types-In-Python.html#Types-In-Python" title="Types In Python"> 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="Values-From-Inferior"></a> 44<p> 45Next: <a rel="next" accesskey="n" href="Types-In-Python.html#Types-In-Python">Types In Python</a>, 46Previous: <a rel="previous" accesskey="p" href="Exception-Handling.html#Exception-Handling">Exception Handling</a>, 47Up: <a rel="up" accesskey="u" href="Python-API.html#Python-API">Python API</a> 48<hr> 49</div> 50 51<h5 class="subsubsection">23.2.2.3 Values From Inferior</h5> 52 53<p><a name="index-values-from-inferior_002c-with-Python-1609"></a><a name="index-python_002c-working-with-values-from-inferior-1610"></a> 54<a name="index-g_t_0040code_007bgdb_002eValue_007d-1611"></a><span class="sc">gdb</span> provides values it obtains from the inferior program in 55an object of type <code>gdb.Value</code>. <span class="sc">gdb</span> uses this object 56for its internal bookkeeping of the inferior's values, and for 57fetching values when necessary. 58 59 <p>Inferior values that are simple scalars can be used directly in 60Python expressions that are valid for the value's data type. Here's 61an example for an integer or floating-point value <code>some_val</code>: 62 63<pre class="smallexample"> bar = some_val + 2 64</pre> 65 <p class="noindent">As result of this, <code>bar</code> will also be a <code>gdb.Value</code> object 66whose values are of the same type as those of <code>some_val</code>. 67 68 <p>Inferior values that are structures or instances of some class can 69be accessed using the Python <dfn>dictionary syntax</dfn>. For example, if 70<code>some_val</code> is a <code>gdb.Value</code> instance holding a structure, you 71can access its <code>foo</code> element with: 72 73<pre class="smallexample"> bar = some_val['foo'] 74</pre> 75 <p>Again, <code>bar</code> will also be a <code>gdb.Value</code> object. 76 77 <p>A <code>gdb.Value</code> that represents a function can be executed via 78inferior function call. Any arguments provided to the call must match 79the function's prototype, and must be provided in the order specified 80by that prototype. 81 82 <p>For example, <code>some_val</code> is a <code>gdb.Value</code> instance 83representing a function that takes two integers as arguments. To 84execute this function, call it like so: 85 86<pre class="smallexample"> result = some_val (10,20) 87</pre> 88 <p>Any values returned from a function call will be stored as a 89<code>gdb.Value</code>. 90 91 <p>The following attributes are provided: 92 93 <dl> 94 95 <div class="defun"> 96— Instance Variable of Value: <b>address</b><var><a name="index-address-1612"></a></var><br> 97<blockquote> <p>If this object is addressable, this read-only attribute holds a 98<code>gdb.Value</code> object representing the address. Otherwise, 99this attribute holds <code>None</code>. 100</p></blockquote></div> 101 102 <p><a name="index-optimized-out-value-in-Python-1613"></a> 103 104 <div class="defun"> 105— Instance Variable of Value: <b>is_optimized_out</b><var><a name="index-is_005foptimized_005fout-1614"></a></var><br> 106<blockquote> <p>This read-only boolean attribute is true if the compiler optimized out 107this value, thus it is not available for fetching from the inferior. 108</p></blockquote></div> 109 110 <div class="defun"> 111— Instance Variable of Value: <b>type</b><var><a name="index-type-1615"></a></var><br> 112<blockquote> <p>The type of this <code>gdb.Value</code>. The value of this attribute is a 113<code>gdb.Type</code> object. 114</p></blockquote></div> 115 116 <div class="defun"> 117— Instance Variable of Value: <b>dynamic_type</b><var><a name="index-dynamic_005ftype-1616"></a></var><br> 118<blockquote> <p>The dynamic type of this <code>gdb.Value</code>. This uses C<tt>++</tt> run-time 119type information to determine the dynamic type of the value. If this 120value is of class type, it will return the class in which the value is 121embedded, if any. If this value is of pointer or reference to a class 122type, it will compute the dynamic type of the referenced object, and 123return a pointer or reference to that type, respectively. In all 124other cases, it will return the value's static type. 125</p></blockquote></div> 126 </dl> 127 128 <p>The following methods are provided: 129 130 <dl> 131 132 <div class="defun"> 133— Method on Value: <b>cast</b><var> type<a name="index-cast-on-Value-1617"></a></var><br> 134<blockquote> <p>Return a new instance of <code>gdb.Value</code> that is the result of 135casting this instance to the type described by <var>type</var>, which must 136be a <code>gdb.Type</code> object. If the cast cannot be performed for some 137reason, this method throws an exception. 138</p></blockquote></div> 139 140 <div class="defun"> 141— Method on Value: <b>dereference</b><var><a name="index-dereference-on-Value-1618"></a></var><br> 142<blockquote> <p>For pointer data types, this method returns a new <code>gdb.Value</code> object 143whose contents is the object pointed to by the pointer. For example, if 144<code>foo</code> is a C pointer to an <code>int</code>, declared in your C program as 145 146 <pre class="smallexample"> int *foo; 147</pre> 148 <p class="noindent">then you can use the corresponding <code>gdb.Value</code> to access what 149<code>foo</code> points to like this: 150 151 <pre class="smallexample"> bar = foo.dereference () 152</pre> 153 <p>The result <code>bar</code> will be a <code>gdb.Value</code> object holding the 154value pointed to by <code>foo</code>. 155</p></blockquote></div> 156 157 <div class="defun"> 158— Method on Value: <b>dynamic_cast</b><var> type<a name="index-dynamic_005fcast-on-Value-1619"></a></var><br> 159<blockquote> <p>Like <code>Value.cast</code>, but works as if the C<tt>++</tt> <code>dynamic_cast</code> 160operator were used. Consult a C<tt>++</tt> reference for details. 161</p></blockquote></div> 162 163 <div class="defun"> 164— Method on Value: <b>reinterpret_cast</b><var> type<a name="index-reinterpret_005fcast-on-Value-1620"></a></var><br> 165<blockquote> <p>Like <code>Value.cast</code>, but works as if the C<tt>++</tt> <code>reinterpret_cast</code> 166operator were used. Consult a C<tt>++</tt> reference for details. 167</p></blockquote></div> 168 169 <div class="defun"> 170— Method on Value: <b>string</b> <span class="roman">[</span><var>encoding</var><span class="roman">]</span> <span class="roman">[</span><var>errors</var><span class="roman">]</span> <span class="roman">[</span><var>length</var><span class="roman">]</span><var><a name="index-string-on-Value-1621"></a></var><br> 171<blockquote> <p>If this <code>gdb.Value</code> represents a string, then this method 172converts the contents to a Python string. Otherwise, this method will 173throw an exception. 174 175 <p>Strings are recognized in a language-specific way; whether a given 176<code>gdb.Value</code> represents a string is determined by the current 177language. 178 179 <p>For C-like languages, a value is a string if it is a pointer to or an 180array of characters or ints. The string is assumed to be terminated 181by a zero of the appropriate width. However if the optional length 182argument is given, the string will be converted to that given length, 183ignoring any embedded zeros that the string may contain. 184 185 <p>If the optional <var>encoding</var> argument is given, it must be a string 186naming the encoding of the string in the <code>gdb.Value</code>, such as 187<code>"ascii"</code>, <code>"iso-8859-6"</code> or <code>"utf-8"</code>. It accepts 188the same encodings as the corresponding argument to Python's 189<code>string.decode</code> method, and the Python codec machinery will be used 190to convert the string. If <var>encoding</var> is not given, or if 191<var>encoding</var> is the empty string, then either the <code>target-charset</code> 192(see <a href="Character-Sets.html#Character-Sets">Character Sets</a>) will be used, or a language-specific encoding 193will be used, if the current language is able to supply one. 194 195 <p>The optional <var>errors</var> argument is the same as the corresponding 196argument to Python's <code>string.decode</code> method. 197 198 <p>If the optional <var>length</var> argument is given, the string will be 199fetched and converted to the given length. 200</p></blockquote></div> 201 202 <div class="defun"> 203— Method on Value: <b>lazy_string</b> <span class="roman">[</span><var>encoding</var><span class="roman">]</span> <span class="roman">[</span><var>length</var><span class="roman">]</span><var><a name="index-lazy_005fstring-on-Value-1622"></a></var><br> 204<blockquote> <p>If this <code>gdb.Value</code> represents a string, then this method 205converts the contents to a <code>gdb.LazyString</code> (see <a href="Lazy-Strings-In-Python.html#Lazy-Strings-In-Python">Lazy Strings In Python</a>). Otherwise, this method will throw an exception. 206 207 <p>If the optional <var>encoding</var> argument is given, it must be a string 208naming the encoding of the <code>gdb.LazyString</code>. Some examples are: 209‘<samp><span class="samp">ascii</span></samp>’, ‘<samp><span class="samp">iso-8859-6</span></samp>’ or ‘<samp><span class="samp">utf-8</span></samp>’. If the 210<var>encoding</var> argument is an encoding that <span class="sc">gdb</span> does 211recognize, <span class="sc">gdb</span> will raise an error. 212 213 <p>When a lazy string is printed, the <span class="sc">gdb</span> encoding machinery is 214used to convert the string during printing. If the optional 215<var>encoding</var> argument is not provided, or is an empty string, 216<span class="sc">gdb</span> will automatically select the encoding most suitable for 217the string type. For further information on encoding in <span class="sc">gdb</span> 218please see <a href="Character-Sets.html#Character-Sets">Character Sets</a>. 219 220 <p>If the optional <var>length</var> argument is given, the string will be 221fetched and encoded to the length of characters specified. If 222the <var>length</var> argument is not provided, the string will be fetched 223and encoded until a null of appropriate width is found. 224</p></blockquote></div> 225 </dl> 226 227 </body></html> 228 229