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: Xmethods In Python</title>
18
19<meta name="description" content="Debugging with GDB: Xmethods In Python">
20<meta name="keywords" content="Debugging with GDB: Xmethods 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="Xmethod-API.html#Xmethod-API" rel="next" title="Xmethod API">
30<link href="Unwinding-Frames-in-Python.html#Unwinding-Frames-in-Python" rel="previous" title="Unwinding Frames 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="Xmethods-In-Python"></a>
65<div class="header">
66<p>
67Next: <a href="Xmethod-API.html#Xmethod-API" accesskey="n" rel="next">Xmethod API</a>, Previous: <a href="Unwinding-Frames-in-Python.html#Unwinding-Frames-in-Python" accesskey="p" rel="previous">Unwinding Frames in Python</a>, Up: <a href="Python-API.html#Python-API" accesskey="u" rel="up">Python API</a> &nbsp; [<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="Xmethods-In-Python-1"></a>
71<h4 class="subsubsection">23.2.2.13 Xmethods In Python</h4>
72<a name="index-xmethods-in-Python"></a>
73
74<p><em>Xmethods</em> are additional methods or replacements for existing
75methods of a C<tt>++</tt> class.  This feature is useful for those cases
76where a method defined in C<tt>++</tt> source code could be inlined or
77optimized out by the compiler, making it unavailable to <small>GDB</small>.
78For such cases, one can define an xmethod to serve as a replacement
79for the method defined in the C<tt>++</tt> source code.  <small>GDB</small> will
80then invoke the xmethod, instead of the C<tt>++</tt> method, to
81evaluate expressions.  One can also use xmethods when debugging
82with core files.  Moreover, when debugging live programs, invoking an
83xmethod need not involve running the inferior (which can potentially
84perturb its state).  Hence, even if the C<tt>++</tt> method is available, it
85is better to use its replacement xmethod if one is defined.
86</p>
87<p>The xmethods feature in Python is available via the concepts of an
88<em>xmethod matcher</em> and an <em>xmethod worker</em>.  To
89implement an xmethod, one has to implement a matcher and a
90corresponding worker for it (more than one worker can be
91implemented, each catering to a different overloaded instance of the
92method).  Internally, <small>GDB</small> invokes the <code>match</code> method of a
93matcher to match the class type and method name.  On a match, the
94<code>match</code> method returns a list of matching <em>worker</em> objects.
95Each worker object typically corresponds to an overloaded instance of
96the xmethod.  They implement a <code>get_arg_types</code> method which
97returns a sequence of types corresponding to the arguments the xmethod
98requires.  <small>GDB</small> uses this sequence of types to perform
99overload resolution and picks a winning xmethod worker.  A winner
100is also selected from among the methods <small>GDB</small> finds in the
101C<tt>++</tt> source code.  Next, the winning xmethod worker and the
102winning C<tt>++</tt> method are compared to select an overall winner.  In
103case of a tie between a xmethod worker and a C<tt>++</tt> method, the
104xmethod worker is selected as the winner.  That is, if a winning
105xmethod worker is found to be equivalent to the winning C<tt>++</tt>
106method, then the xmethod worker is treated as a replacement for
107the C<tt>++</tt> method.  <small>GDB</small> uses the overall winner to invoke the
108method.  If the winning xmethod worker is the overall winner, then
109the corresponding xmethod is invoked via the <code>__call__</code> method
110of the worker object.
111</p>
112<p>If one wants to implement an xmethod as a replacement for an
113existing C<tt>++</tt> method, then they have to implement an equivalent
114xmethod which has exactly the same name and takes arguments of
115exactly the same type as the C<tt>++</tt> method.  If the user wants to
116invoke the C<tt>++</tt> method even though a replacement xmethod is
117available for that method, then they can disable the xmethod.
118</p>
119<p>See <a href="Xmethod-API.html#Xmethod-API">Xmethod API</a>, for API to implement xmethods in Python.
120See <a href="Writing-an-Xmethod.html#Writing-an-Xmethod">Writing an Xmethod</a>, for implementing xmethods in Python.
121</p>
122<hr>
123<div class="header">
124<p>
125Next: <a href="Xmethod-API.html#Xmethod-API" accesskey="n" rel="next">Xmethod API</a>, Previous: <a href="Unwinding-Frames-in-Python.html#Unwinding-Frames-in-Python" accesskey="p" rel="previous">Unwinding Frames in Python</a>, Up: <a href="Python-API.html#Python-API" accesskey="u" rel="up">Python API</a> &nbsp; [<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>
126</div>
127
128
129
130</body>
131</html>
132