1<html lang="en"> 2<head> 3<title>Functions In Python - 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="Parameters-In-Python.html#Parameters-In-Python" title="Parameters In Python"> 10<link rel="next" href="Progspaces-In-Python.html#Progspaces-In-Python" title="Progspaces In Python"> 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="Functions-In-Python"></a> 43<p> 44Next: <a rel="next" accesskey="n" href="Progspaces-In-Python.html#Progspaces-In-Python">Progspaces In Python</a>, 45Previous: <a rel="previous" accesskey="p" href="Parameters-In-Python.html#Parameters-In-Python">Parameters In Python</a>, 46Up: <a rel="up" accesskey="u" href="Python-API.html#Python-API">Python API</a> 47<hr> 48</div> 49 50<h5 class="subsubsection">23.2.2.17 Writing new convenience functions</h5> 51 52<p><a name="index-writing-convenience-functions-2008"></a><a name="index-convenience-functions-in-python-2009"></a><a name="index-python-convenience-functions-2010"></a><a name="index-gdb_002eFunction-2011"></a><a name="index-Function-2012"></a>You can implement new convenience functions (see <a href="Convenience-Vars.html#Convenience-Vars">Convenience Vars</a>) 53in Python. A convenience function is an instance of a subclass of the 54class <code>gdb.Function</code>. 55 56<div class="defun"> 57— Function: <b>Function.__init__</b> (<var>name</var>)<var><a name="index-Function_002e_005f_005finit_005f_005f-2013"></a></var><br> 58<blockquote><p>The initializer for <code>Function</code> registers the new function with 59<span class="sc">gdb</span>. The argument <var>name</var> is the name of the function, 60a string. The function will be visible to the user as a convenience 61variable of type <code>internal function</code>, whose name is the same as 62the given <var>name</var>. 63 64 <p>The documentation for the new function is taken from the documentation 65string for the new class. 66</p></blockquote></div> 67 68<div class="defun"> 69— Function: <b>Function.invoke</b> (<var>*args</var>)<var><a name="index-Function_002einvoke-2014"></a></var><br> 70<blockquote><p>When a convenience function is evaluated, its arguments are converted 71to instances of <code>gdb.Value</code>, and then the function's 72<code>invoke</code> method is called. Note that <span class="sc">gdb</span> does not 73predetermine the arity of convenience functions. Instead, all 74available arguments are passed to <code>invoke</code>, following the 75standard Python calling convention. In particular, a convenience 76function can have default values for parameters without ill effect. 77 78 <p>The return value of this method is used as its value in the enclosing 79expression. If an ordinary Python value is returned, it is converted 80to a <code>gdb.Value</code> following the usual rules. 81</p></blockquote></div> 82 83 <p>The following code snippet shows how a trivial convenience function can 84be implemented in Python: 85 86<pre class="smallexample"> class Greet (gdb.Function): 87 """Return string to greet someone. 88 Takes a name as argument.""" 89 90 def __init__ (self): 91 super (Greet, self).__init__ ("greet") 92 93 def invoke (self, name): 94 return "Hello, %s!" % name.string () 95 96 Greet () 97</pre> 98 <p>The last line instantiates the class, and is necessary to trigger the 99registration of the function with <span class="sc">gdb</span>. Depending on how the 100Python code is read into <span class="sc">gdb</span>, you may need to import the 101<code>gdb</code> module explicitly. 102 103 <p>Now you can use the function in an expression: 104 105<pre class="smallexample"> (gdb) print $greet("Bob") 106 $1 = "Hello, Bob!" 107</pre> 108 </body></html> 109 110