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