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: Functions In Python</title> 18 19<meta name="description" content="Debugging with GDB: Functions In Python"> 20<meta name="keywords" content="Debugging with GDB: Functions In Python"> 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="Python-API.html#Python-API" rel="up" title="Python API"> 29<link href="Progspaces-In-Python.html#Progspaces-In-Python" rel="next" title="Progspaces In Python"> 30<link href="Parameters-In-Python.html#Parameters-In-Python" rel="previous" title="Parameters In Python"> 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="Functions-In-Python"></a> 65<div class="header"> 66<p> 67Next: <a href="Progspaces-In-Python.html#Progspaces-In-Python" accesskey="n" rel="next">Progspaces In Python</a>, Previous: <a href="Parameters-In-Python.html#Parameters-In-Python" accesskey="p" rel="previous">Parameters In Python</a>, Up: <a href="Python-API.html#Python-API" accesskey="u" rel="up">Python API</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="Writing-new-convenience-functions"></a> 71<h4 class="subsubsection">23.2.2.22 Writing new convenience functions</h4> 72 73<a name="index-writing-convenience-functions"></a> 74<a name="index-convenience-functions-in-python"></a> 75<a name="index-python-convenience-functions"></a> 76<a name="index-gdb_002eFunction"></a> 77<a name="index-Function"></a> 78<p>You can implement new convenience functions (see <a href="Convenience-Vars.html#Convenience-Vars">Convenience Vars</a>) 79in Python. A convenience function is an instance of a subclass of the 80class <code>gdb.Function</code>. 81</p> 82<dl> 83<dt><a name="index-Function_002e_005f_005finit_005f_005f"></a>Function: <strong>Function.__init__</strong> <em>(name)</em></dt> 84<dd><p>The initializer for <code>Function</code> registers the new function with 85<small>GDB</small>. The argument <var>name</var> is the name of the function, 86a string. The function will be visible to the user as a convenience 87variable of type <code>internal function</code>, whose name is the same as 88the given <var>name</var>. 89</p> 90<p>The documentation for the new function is taken from the documentation 91string for the new class. 92</p></dd></dl> 93 94<dl> 95<dt><a name="index-Function_002einvoke"></a>Function: <strong>Function.invoke</strong> <em>(<var>*args</var>)</em></dt> 96<dd><p>When a convenience function is evaluated, its arguments are converted 97to instances of <code>gdb.Value</code>, and then the function’s 98<code>invoke</code> method is called. Note that <small>GDB</small> does not 99predetermine the arity of convenience functions. Instead, all 100available arguments are passed to <code>invoke</code>, following the 101standard Python calling convention. In particular, a convenience 102function can have default values for parameters without ill effect. 103</p> 104<p>The return value of this method is used as its value in the enclosing 105expression. If an ordinary Python value is returned, it is converted 106to a <code>gdb.Value</code> following the usual rules. 107</p></dd></dl> 108 109<p>The following code snippet shows how a trivial convenience function can 110be implemented in Python: 111</p> 112<div class="smallexample"> 113<pre class="smallexample">class Greet (gdb.Function): 114 """Return string to greet someone. 115Takes a name as argument.""" 116 117 def __init__ (self): 118 super (Greet, self).__init__ ("greet") 119 120 def invoke (self, name): 121 return "Hello, %s!" % name.string () 122 123Greet () 124</pre></div> 125 126<p>The last line instantiates the class, and is necessary to trigger the 127registration of the function with <small>GDB</small>. Depending on how the 128Python code is read into <small>GDB</small>, you may need to import the 129<code>gdb</code> module explicitly. 130</p> 131<p>Now you can use the function in an expression: 132</p> 133<div class="smallexample"> 134<pre class="smallexample">(gdb) print $greet("Bob") 135$1 = "Hello, Bob!" 136</pre></div> 137 138<hr> 139<div class="header"> 140<p> 141Next: <a href="Progspaces-In-Python.html#Progspaces-In-Python" accesskey="n" rel="next">Progspaces In Python</a>, Previous: <a href="Parameters-In-Python.html#Parameters-In-Python" accesskey="p" rel="previous">Parameters In Python</a>, Up: <a href="Python-API.html#Python-API" accesskey="u" rel="up">Python API</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> 142</div> 143 144 145 146</body> 147</html> 148