xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/python/py-disasm.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
1*6881a400Schristos /* Python interface to instruction disassembly.
2*6881a400Schristos 
3*6881a400Schristos    Copyright (C) 2021-2023 Free Software Foundation, Inc.
4*6881a400Schristos 
5*6881a400Schristos    This file is part of GDB.
6*6881a400Schristos 
7*6881a400Schristos    This program is free software; you can redistribute it and/or modify
8*6881a400Schristos    it under the terms of the GNU General Public License as published by
9*6881a400Schristos    the Free Software Foundation; either version 3 of the License, or
10*6881a400Schristos    (at your option) any later version.
11*6881a400Schristos 
12*6881a400Schristos    This program is distributed in the hope that it will be useful,
13*6881a400Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*6881a400Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*6881a400Schristos    GNU General Public License for more details.
16*6881a400Schristos 
17*6881a400Schristos    You should have received a copy of the GNU General Public License
18*6881a400Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19*6881a400Schristos 
20*6881a400Schristos #include "defs.h"
21*6881a400Schristos #include "python-internal.h"
22*6881a400Schristos #include "dis-asm.h"
23*6881a400Schristos #include "arch-utils.h"
24*6881a400Schristos #include "charset.h"
25*6881a400Schristos #include "disasm.h"
26*6881a400Schristos #include "progspace.h"
27*6881a400Schristos 
28*6881a400Schristos /* Implement gdb.disassembler.DisassembleInfo type.  An object of this type
29*6881a400Schristos    represents a single disassembler request from GDB.  */
30*6881a400Schristos 
31*6881a400Schristos struct disasm_info_object
32*6881a400Schristos {
33*6881a400Schristos   PyObject_HEAD
34*6881a400Schristos 
35*6881a400Schristos   /* The architecture in which we are disassembling.  */
36*6881a400Schristos   struct gdbarch *gdbarch;
37*6881a400Schristos 
38*6881a400Schristos   /* The program_space in which we are disassembling.  */
39*6881a400Schristos   struct program_space *program_space;
40*6881a400Schristos 
41*6881a400Schristos   /* Address of the instruction to disassemble.  */
42*6881a400Schristos   bfd_vma address;
43*6881a400Schristos 
44*6881a400Schristos   /* The disassemble_info passed from core GDB, this contains the
45*6881a400Schristos      callbacks necessary to read the instruction from core GDB, and to
46*6881a400Schristos      print the disassembled instruction.  */
47*6881a400Schristos   disassemble_info *gdb_info;
48*6881a400Schristos 
49*6881a400Schristos   /* If copies of this object are created then they are chained together
50*6881a400Schristos      via this NEXT pointer, this allows all the copies to be invalidated at
51*6881a400Schristos      the same time as the parent object.  */
52*6881a400Schristos   struct disasm_info_object *next;
53*6881a400Schristos };
54*6881a400Schristos 
55*6881a400Schristos extern PyTypeObject disasm_info_object_type
56*6881a400Schristos     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("disasm_info_object");
57*6881a400Schristos 
58*6881a400Schristos /* Implement gdb.disassembler.DisassemblerResult type, an object that holds
59*6881a400Schristos    the result of calling the disassembler.  This is mostly the length of
60*6881a400Schristos    the disassembled instruction (in bytes), and the string representing the
61*6881a400Schristos    disassembled instruction.  */
62*6881a400Schristos 
63*6881a400Schristos struct disasm_result_object
64*6881a400Schristos {
65*6881a400Schristos   PyObject_HEAD
66*6881a400Schristos 
67*6881a400Schristos   /* The length of the disassembled instruction in bytes.  */
68*6881a400Schristos   int length;
69*6881a400Schristos 
70*6881a400Schristos   /* A buffer which, when allocated, holds the disassembled content of an
71*6881a400Schristos      instruction.  */
72*6881a400Schristos   string_file *content;
73*6881a400Schristos };
74*6881a400Schristos 
75*6881a400Schristos extern PyTypeObject disasm_result_object_type
76*6881a400Schristos     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("disasm_result_object");
77*6881a400Schristos 
78*6881a400Schristos /* When this is false we fast path out of gdbpy_print_insn, which should
79*6881a400Schristos    keep the performance impact of the Python disassembler down.  This is
80*6881a400Schristos    set to true from Python by calling gdb.disassembler._set_enabled() when
81*6881a400Schristos    the user registers a disassembler.  */
82*6881a400Schristos 
83*6881a400Schristos static bool python_print_insn_enabled = false;
84*6881a400Schristos 
85*6881a400Schristos /* A sub-class of gdb_disassembler that holds a pointer to a Python
86*6881a400Schristos    DisassembleInfo object.  A pointer to an instance of this class is
87*6881a400Schristos    placed in the application_data field of the disassemble_info that is
88*6881a400Schristos    used when we call gdbarch_print_insn.  */
89*6881a400Schristos 
90*6881a400Schristos struct gdbpy_disassembler : public gdb_printing_disassembler
91*6881a400Schristos {
92*6881a400Schristos   /* Constructor.  */
93*6881a400Schristos   gdbpy_disassembler (disasm_info_object *obj, PyObject *memory_source);
94*6881a400Schristos 
95*6881a400Schristos   /* Get the DisassembleInfo object pointer.  */
96*6881a400Schristos   disasm_info_object *
97*6881a400Schristos   py_disasm_info () const
98*6881a400Schristos   {
99*6881a400Schristos     return m_disasm_info_object;
100*6881a400Schristos   }
101*6881a400Schristos 
102*6881a400Schristos   /* Callbacks used by disassemble_info.  */
103*6881a400Schristos   static void memory_error_func (int status, bfd_vma memaddr,
104*6881a400Schristos 				 struct disassemble_info *info) noexcept;
105*6881a400Schristos   static void print_address_func (bfd_vma addr,
106*6881a400Schristos 				  struct disassemble_info *info) noexcept;
107*6881a400Schristos   static int read_memory_func (bfd_vma memaddr, gdb_byte *buff,
108*6881a400Schristos 			       unsigned int len,
109*6881a400Schristos 			       struct disassemble_info *info) noexcept;
110*6881a400Schristos 
111*6881a400Schristos   /* Return a reference to an optional that contains the address at which a
112*6881a400Schristos      memory error occurred.  The optional will only have a value if a
113*6881a400Schristos      memory error actually occurred.  */
114*6881a400Schristos   const gdb::optional<CORE_ADDR> &memory_error_address () const
115*6881a400Schristos   { return m_memory_error_address; }
116*6881a400Schristos 
117*6881a400Schristos   /* Return the content of the disassembler as a string.  The contents are
118*6881a400Schristos      moved out of the disassembler, so after this call the disassembler
119*6881a400Schristos      contents have been reset back to empty.  */
120*6881a400Schristos   std::string release ()
121*6881a400Schristos   {
122*6881a400Schristos     return m_string_file.release ();
123*6881a400Schristos   }
124*6881a400Schristos 
125*6881a400Schristos   /* If there is a Python exception stored in this disassembler then
126*6881a400Schristos      restore it (i.e. set the PyErr_* state), clear the exception within
127*6881a400Schristos      this disassembler, and return true.  There must be no current
128*6881a400Schristos      exception set (i.e. !PyErr_Occurred()) when this function is called,
129*6881a400Schristos      as any such exception might get lost.
130*6881a400Schristos 
131*6881a400Schristos      Otherwise, there is no exception stored in this disassembler, return
132*6881a400Schristos      false.  */
133*6881a400Schristos   bool restore_exception ()
134*6881a400Schristos   {
135*6881a400Schristos     gdb_assert (!PyErr_Occurred ());
136*6881a400Schristos     if (m_stored_exception.has_value ())
137*6881a400Schristos       {
138*6881a400Schristos 	gdbpy_err_fetch ex = std::move (*m_stored_exception);
139*6881a400Schristos 	m_stored_exception.reset ();
140*6881a400Schristos 	ex.restore ();
141*6881a400Schristos 	return true;
142*6881a400Schristos       }
143*6881a400Schristos 
144*6881a400Schristos     return false;
145*6881a400Schristos   }
146*6881a400Schristos 
147*6881a400Schristos private:
148*6881a400Schristos 
149*6881a400Schristos   /* Where the disassembler result is written.  */
150*6881a400Schristos   string_file m_string_file;
151*6881a400Schristos 
152*6881a400Schristos   /* The DisassembleInfo object we are disassembling for.  */
153*6881a400Schristos   disasm_info_object *m_disasm_info_object;
154*6881a400Schristos 
155*6881a400Schristos   /* When the user indicates that a memory error has occurred then the
156*6881a400Schristos      address of the memory error is stored in here.  */
157*6881a400Schristos   gdb::optional<CORE_ADDR> m_memory_error_address;
158*6881a400Schristos 
159*6881a400Schristos   /* When the user calls the builtin_disassemble function, if they pass a
160*6881a400Schristos      memory source object then a pointer to the object is placed in here,
161*6881a400Schristos      otherwise, this field is nullptr.  */
162*6881a400Schristos   PyObject *m_memory_source;
163*6881a400Schristos 
164*6881a400Schristos   /* Move the exception EX into this disassembler object.  */
165*6881a400Schristos   void store_exception (gdbpy_err_fetch &&ex)
166*6881a400Schristos   {
167*6881a400Schristos     /* The only calls to store_exception are from read_memory_func, which
168*6881a400Schristos        will return early if there's already an exception stored.  */
169*6881a400Schristos     gdb_assert (!m_stored_exception.has_value ());
170*6881a400Schristos     m_stored_exception.emplace (std::move (ex));
171*6881a400Schristos   }
172*6881a400Schristos 
173*6881a400Schristos   /* Return true if there is an exception stored in this disassembler.  */
174*6881a400Schristos   bool has_stored_exception () const
175*6881a400Schristos   {
176*6881a400Schristos     return m_stored_exception.has_value ();
177*6881a400Schristos   }
178*6881a400Schristos 
179*6881a400Schristos   /* Store a single exception.  This is used to pass Python exceptions back
180*6881a400Schristos      from ::memory_read to disasmpy_builtin_disassemble.  */
181*6881a400Schristos   gdb::optional<gdbpy_err_fetch> m_stored_exception;
182*6881a400Schristos };
183*6881a400Schristos 
184*6881a400Schristos /* Return true if OBJ is still valid, otherwise, return false.  A valid OBJ
185*6881a400Schristos    will have a non-nullptr gdb_info field.  */
186*6881a400Schristos 
187*6881a400Schristos static bool
188*6881a400Schristos disasm_info_object_is_valid (disasm_info_object *obj)
189*6881a400Schristos {
190*6881a400Schristos   return obj->gdb_info != nullptr;
191*6881a400Schristos }
192*6881a400Schristos 
193*6881a400Schristos /* Fill in OBJ with all the other arguments.  */
194*6881a400Schristos 
195*6881a400Schristos static void
196*6881a400Schristos disasm_info_fill (disasm_info_object *obj, struct gdbarch *gdbarch,
197*6881a400Schristos 		  program_space *progspace, bfd_vma address,
198*6881a400Schristos 		  disassemble_info *di, disasm_info_object *next)
199*6881a400Schristos {
200*6881a400Schristos   obj->gdbarch = gdbarch;
201*6881a400Schristos   obj->program_space = progspace;
202*6881a400Schristos   obj->address = address;
203*6881a400Schristos   obj->gdb_info = di;
204*6881a400Schristos   obj->next = next;
205*6881a400Schristos }
206*6881a400Schristos 
207*6881a400Schristos /* Implement DisassembleInfo.__init__.  Takes a single argument that must
208*6881a400Schristos    be another DisassembleInfo object and copies the contents from the
209*6881a400Schristos    argument into this new object.  */
210*6881a400Schristos 
211*6881a400Schristos static int
212*6881a400Schristos disasm_info_init (PyObject *self, PyObject *args, PyObject *kwargs)
213*6881a400Schristos {
214*6881a400Schristos   static const char *keywords[] = { "info", NULL };
215*6881a400Schristos   PyObject *info_obj;
216*6881a400Schristos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "O!", keywords,
217*6881a400Schristos 					&disasm_info_object_type,
218*6881a400Schristos 					&info_obj))
219*6881a400Schristos     return -1;
220*6881a400Schristos 
221*6881a400Schristos   disasm_info_object *other = (disasm_info_object *) info_obj;
222*6881a400Schristos   disasm_info_object *info = (disasm_info_object *) self;
223*6881a400Schristos   disasm_info_fill (info, other->gdbarch, other->program_space,
224*6881a400Schristos 		    other->address, other->gdb_info, other->next);
225*6881a400Schristos   other->next = info;
226*6881a400Schristos 
227*6881a400Schristos   /* As the OTHER object now holds a pointer to INFO we inc the ref count
228*6881a400Schristos      on INFO.  This stops INFO being deleted until OTHER has gone away.  */
229*6881a400Schristos   Py_INCREF ((PyObject *) info);
230*6881a400Schristos   return 0;
231*6881a400Schristos }
232*6881a400Schristos 
233*6881a400Schristos /* The tp_dealloc callback for the DisassembleInfo type.  */
234*6881a400Schristos 
235*6881a400Schristos static void
236*6881a400Schristos disasm_info_dealloc (PyObject *self)
237*6881a400Schristos {
238*6881a400Schristos   disasm_info_object *obj = (disasm_info_object *) self;
239*6881a400Schristos 
240*6881a400Schristos   /* We no longer care about the object our NEXT pointer points at, so we
241*6881a400Schristos      can decrement its reference count.  This macro handles the case when
242*6881a400Schristos      NEXT is nullptr.  */
243*6881a400Schristos   Py_XDECREF ((PyObject *) obj->next);
244*6881a400Schristos 
245*6881a400Schristos   /* Now core deallocation behaviour.  */
246*6881a400Schristos   Py_TYPE (self)->tp_free (self);
247*6881a400Schristos }
248*6881a400Schristos 
249*6881a400Schristos /* Implement DisassembleInfo.is_valid(), really just a wrapper around the
250*6881a400Schristos    disasm_info_object_is_valid function above.  */
251*6881a400Schristos 
252*6881a400Schristos static PyObject *
253*6881a400Schristos disasmpy_info_is_valid (PyObject *self, PyObject *args)
254*6881a400Schristos {
255*6881a400Schristos   disasm_info_object *disasm_obj = (disasm_info_object *) self;
256*6881a400Schristos 
257*6881a400Schristos   if (disasm_info_object_is_valid (disasm_obj))
258*6881a400Schristos     Py_RETURN_TRUE;
259*6881a400Schristos 
260*6881a400Schristos   Py_RETURN_FALSE;
261*6881a400Schristos }
262*6881a400Schristos 
263*6881a400Schristos /* Set the Python exception to be a gdb.MemoryError object, with ADDRESS
264*6881a400Schristos    as its payload.  */
265*6881a400Schristos 
266*6881a400Schristos static void
267*6881a400Schristos disasmpy_set_memory_error_for_address (CORE_ADDR address)
268*6881a400Schristos {
269*6881a400Schristos   PyObject *address_obj = gdb_py_object_from_longest (address).release ();
270*6881a400Schristos   PyErr_SetObject (gdbpy_gdb_memory_error, address_obj);
271*6881a400Schristos }
272*6881a400Schristos 
273*6881a400Schristos /* Ensure that a gdb.disassembler.DisassembleInfo is valid.  */
274*6881a400Schristos 
275*6881a400Schristos #define DISASMPY_DISASM_INFO_REQUIRE_VALID(Info)			\
276*6881a400Schristos   do {									\
277*6881a400Schristos     if (!disasm_info_object_is_valid (Info))				\
278*6881a400Schristos       {									\
279*6881a400Schristos 	PyErr_SetString (PyExc_RuntimeError,				\
280*6881a400Schristos 			 _("DisassembleInfo is no longer valid."));	\
281*6881a400Schristos 	return nullptr;							\
282*6881a400Schristos       }									\
283*6881a400Schristos   } while (0)
284*6881a400Schristos 
285*6881a400Schristos /* Initialise OBJ, a DisassemblerResult object with LENGTH and CONTENT.
286*6881a400Schristos    OBJ might already have been initialised, in which case any existing
287*6881a400Schristos    content should be discarded before the new CONTENT is moved in.  */
288*6881a400Schristos 
289*6881a400Schristos static void
290*6881a400Schristos disasmpy_init_disassembler_result (disasm_result_object *obj, int length,
291*6881a400Schristos 				   std::string content)
292*6881a400Schristos {
293*6881a400Schristos   if (obj->content == nullptr)
294*6881a400Schristos     obj->content = new string_file;
295*6881a400Schristos   else
296*6881a400Schristos     obj->content->clear ();
297*6881a400Schristos 
298*6881a400Schristos   obj->length = length;
299*6881a400Schristos   *(obj->content) = std::move (content);
300*6881a400Schristos }
301*6881a400Schristos 
302*6881a400Schristos /* Implement gdb.disassembler.builtin_disassemble().  Calls back into GDB's
303*6881a400Schristos    builtin disassembler.  The first argument is a DisassembleInfo object
304*6881a400Schristos    describing what to disassemble.  The second argument is optional and
305*6881a400Schristos    provides a mechanism to modify the memory contents that the builtin
306*6881a400Schristos    disassembler will actually disassemble.
307*6881a400Schristos 
308*6881a400Schristos    Returns an instance of gdb.disassembler.DisassemblerResult, an object
309*6881a400Schristos    that wraps a disassembled instruction, or it raises a
310*6881a400Schristos    gdb.MemoryError.  */
311*6881a400Schristos 
312*6881a400Schristos static PyObject *
313*6881a400Schristos disasmpy_builtin_disassemble (PyObject *self, PyObject *args, PyObject *kw)
314*6881a400Schristos {
315*6881a400Schristos   PyObject *info_obj, *memory_source_obj = nullptr;
316*6881a400Schristos   static const char *keywords[] = { "info", "memory_source", nullptr };
317*6881a400Schristos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O!|O", keywords,
318*6881a400Schristos 					&disasm_info_object_type, &info_obj,
319*6881a400Schristos 					&memory_source_obj))
320*6881a400Schristos     return nullptr;
321*6881a400Schristos 
322*6881a400Schristos   disasm_info_object *disasm_info = (disasm_info_object *) info_obj;
323*6881a400Schristos   DISASMPY_DISASM_INFO_REQUIRE_VALID (disasm_info);
324*6881a400Schristos 
325*6881a400Schristos   /* Where the result will be written.  */
326*6881a400Schristos   gdbpy_disassembler disassembler (disasm_info, memory_source_obj);
327*6881a400Schristos 
328*6881a400Schristos   /* Now actually perform the disassembly.  LENGTH is set to the length of
329*6881a400Schristos      the disassembled instruction, or -1 if there was a memory-error
330*6881a400Schristos      encountered while disassembling.  See below more more details on
331*6881a400Schristos      handling of -1 return value.  */
332*6881a400Schristos   int length = gdbarch_print_insn (disasm_info->gdbarch, disasm_info->address,
333*6881a400Schristos 				   disassembler.disasm_info ());
334*6881a400Schristos 
335*6881a400Schristos   /* It is possible that, while calling a user overridden memory read
336*6881a400Schristos      function, a Python exception was raised that couldn't be
337*6881a400Schristos      translated into a standard memory-error.  In this case the first such
338*6881a400Schristos      exception is stored in the disassembler and restored here.  */
339*6881a400Schristos   if (disassembler.restore_exception ())
340*6881a400Schristos     return nullptr;
341*6881a400Schristos 
342*6881a400Schristos   if (length == -1)
343*6881a400Schristos     {
344*6881a400Schristos 
345*6881a400Schristos       /* In an ideal world, every disassembler should always call the
346*6881a400Schristos 	 memory error function before returning a status of -1 as the only
347*6881a400Schristos 	 error a disassembler should encounter is a failure to read
348*6881a400Schristos 	 memory.  Unfortunately, there are some disassemblers who don't
349*6881a400Schristos 	 follow this rule, and will return -1 without calling the memory
350*6881a400Schristos 	 error function.
351*6881a400Schristos 
352*6881a400Schristos 	 To make the Python API simpler, we just classify everything as a
353*6881a400Schristos 	 memory error, but the message has to be modified for the case
354*6881a400Schristos 	 where the disassembler didn't call the memory error function.  */
355*6881a400Schristos       if (disassembler.memory_error_address ().has_value ())
356*6881a400Schristos 	{
357*6881a400Schristos 	  CORE_ADDR addr = *disassembler.memory_error_address ();
358*6881a400Schristos 	  disasmpy_set_memory_error_for_address (addr);
359*6881a400Schristos 	}
360*6881a400Schristos       else
361*6881a400Schristos 	{
362*6881a400Schristos 	  std::string content = disassembler.release ();
363*6881a400Schristos 	  if (!content.empty ())
364*6881a400Schristos 	    PyErr_SetString (gdbpy_gdberror_exc, content.c_str ());
365*6881a400Schristos 	  else
366*6881a400Schristos 	    PyErr_SetString (gdbpy_gdberror_exc,
367*6881a400Schristos 			     _("Unknown disassembly error."));
368*6881a400Schristos 	}
369*6881a400Schristos       return nullptr;
370*6881a400Schristos     }
371*6881a400Schristos 
372*6881a400Schristos   /* Instructions are either non-zero in length, or we got an error,
373*6881a400Schristos      indicated by a length of -1, which we handled above.  */
374*6881a400Schristos   gdb_assert (length > 0);
375*6881a400Schristos 
376*6881a400Schristos   /* We should not have seen a memory error in this case.  */
377*6881a400Schristos   gdb_assert (!disassembler.memory_error_address ().has_value ());
378*6881a400Schristos 
379*6881a400Schristos   /* Create a DisassemblerResult containing the results.  */
380*6881a400Schristos   std::string content = disassembler.release ();
381*6881a400Schristos   PyTypeObject *type = &disasm_result_object_type;
382*6881a400Schristos   gdbpy_ref<disasm_result_object> res
383*6881a400Schristos     ((disasm_result_object *) type->tp_alloc (type, 0));
384*6881a400Schristos   disasmpy_init_disassembler_result (res.get (), length, std::move (content));
385*6881a400Schristos   return reinterpret_cast<PyObject *> (res.release ());
386*6881a400Schristos }
387*6881a400Schristos 
388*6881a400Schristos /* Implement gdb._set_enabled function.  Takes a boolean parameter, and
389*6881a400Schristos    sets whether GDB should enter the Python disassembler code or not.
390*6881a400Schristos 
391*6881a400Schristos    This is called from within the Python code when a new disassembler is
392*6881a400Schristos    registered.  When no disassemblers are registered the global C++ flag
393*6881a400Schristos    is set to false, and GDB never even enters the Python environment to
394*6881a400Schristos    check for a disassembler.
395*6881a400Schristos 
396*6881a400Schristos    When the user registers a new Python disassembler, the global C++ flag
397*6881a400Schristos    is set to true, and now GDB will enter the Python environment to check
398*6881a400Schristos    if there's a disassembler registered for the current architecture.  */
399*6881a400Schristos 
400*6881a400Schristos static PyObject *
401*6881a400Schristos disasmpy_set_enabled (PyObject *self, PyObject *args, PyObject *kw)
402*6881a400Schristos {
403*6881a400Schristos   PyObject *newstate;
404*6881a400Schristos   static const char *keywords[] = { "state", nullptr };
405*6881a400Schristos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O", keywords,
406*6881a400Schristos 					&newstate))
407*6881a400Schristos     return nullptr;
408*6881a400Schristos 
409*6881a400Schristos   if (!PyBool_Check (newstate))
410*6881a400Schristos     {
411*6881a400Schristos       PyErr_SetString (PyExc_TypeError,
412*6881a400Schristos 		       _("The value passed to `_set_enabled' must be a boolean."));
413*6881a400Schristos       return nullptr;
414*6881a400Schristos     }
415*6881a400Schristos 
416*6881a400Schristos   python_print_insn_enabled = PyObject_IsTrue (newstate);
417*6881a400Schristos   Py_RETURN_NONE;
418*6881a400Schristos }
419*6881a400Schristos 
420*6881a400Schristos /* Implement DisassembleInfo.read_memory(LENGTH, OFFSET).  Read LENGTH
421*6881a400Schristos    bytes at OFFSET from the start of the instruction currently being
422*6881a400Schristos    disassembled, and return a memory buffer containing the bytes.
423*6881a400Schristos 
424*6881a400Schristos    OFFSET defaults to zero if it is not provided.  LENGTH is required.  If
425*6881a400Schristos    the read fails then this will raise a gdb.MemoryError exception.  */
426*6881a400Schristos 
427*6881a400Schristos static PyObject *
428*6881a400Schristos disasmpy_info_read_memory (PyObject *self, PyObject *args, PyObject *kw)
429*6881a400Schristos {
430*6881a400Schristos   disasm_info_object *obj = (disasm_info_object *) self;
431*6881a400Schristos   DISASMPY_DISASM_INFO_REQUIRE_VALID (obj);
432*6881a400Schristos 
433*6881a400Schristos   LONGEST length, offset = 0;
434*6881a400Schristos   gdb::unique_xmalloc_ptr<gdb_byte> buffer;
435*6881a400Schristos   static const char *keywords[] = { "length", "offset", nullptr };
436*6881a400Schristos 
437*6881a400Schristos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "L|L", keywords,
438*6881a400Schristos 					&length, &offset))
439*6881a400Schristos     return nullptr;
440*6881a400Schristos 
441*6881a400Schristos   /* The apparent address from which we are reading memory.  Note that in
442*6881a400Schristos      some cases GDB actually disassembles instructions from a buffer, so
443*6881a400Schristos      we might not actually be reading this information directly from the
444*6881a400Schristos      inferior memory.  This is all hidden behind the read_memory_func API
445*6881a400Schristos      within the disassemble_info structure.  */
446*6881a400Schristos   CORE_ADDR address = obj->address + offset;
447*6881a400Schristos 
448*6881a400Schristos   /* Setup a buffer to hold the result.  */
449*6881a400Schristos   buffer.reset ((gdb_byte *) xmalloc (length));
450*6881a400Schristos 
451*6881a400Schristos   /* Read content into BUFFER.  If the read fails then raise a memory
452*6881a400Schristos      error, otherwise, convert BUFFER to a Python memory buffer, and return
453*6881a400Schristos      it to the user.  */
454*6881a400Schristos   disassemble_info *info = obj->gdb_info;
455*6881a400Schristos   if (info->read_memory_func ((bfd_vma) address, buffer.get (),
456*6881a400Schristos 			      (unsigned int) length, info) != 0)
457*6881a400Schristos     {
458*6881a400Schristos       disasmpy_set_memory_error_for_address (address);
459*6881a400Schristos       return nullptr;
460*6881a400Schristos     }
461*6881a400Schristos   return gdbpy_buffer_to_membuf (std::move (buffer), address, length);
462*6881a400Schristos }
463*6881a400Schristos 
464*6881a400Schristos /* Implement DisassembleInfo.address attribute, return the address at which
465*6881a400Schristos    GDB would like an instruction disassembled.  */
466*6881a400Schristos 
467*6881a400Schristos static PyObject *
468*6881a400Schristos disasmpy_info_address (PyObject *self, void *closure)
469*6881a400Schristos {
470*6881a400Schristos   disasm_info_object *obj = (disasm_info_object *) self;
471*6881a400Schristos   DISASMPY_DISASM_INFO_REQUIRE_VALID (obj);
472*6881a400Schristos   return gdb_py_object_from_longest (obj->address).release ();
473*6881a400Schristos }
474*6881a400Schristos 
475*6881a400Schristos /* Implement DisassembleInfo.architecture attribute.  Return the
476*6881a400Schristos    gdb.Architecture in which we are disassembling.  */
477*6881a400Schristos 
478*6881a400Schristos static PyObject *
479*6881a400Schristos disasmpy_info_architecture (PyObject *self, void *closure)
480*6881a400Schristos {
481*6881a400Schristos   disasm_info_object *obj = (disasm_info_object *) self;
482*6881a400Schristos   DISASMPY_DISASM_INFO_REQUIRE_VALID (obj);
483*6881a400Schristos   return gdbarch_to_arch_object (obj->gdbarch);
484*6881a400Schristos }
485*6881a400Schristos 
486*6881a400Schristos /* Implement DisassembleInfo.progspace attribute.  Return the
487*6881a400Schristos    gdb.Progspace in which we are disassembling.  */
488*6881a400Schristos 
489*6881a400Schristos static PyObject *
490*6881a400Schristos disasmpy_info_progspace (PyObject *self, void *closure)
491*6881a400Schristos {
492*6881a400Schristos   disasm_info_object *obj = (disasm_info_object *) self;
493*6881a400Schristos   DISASMPY_DISASM_INFO_REQUIRE_VALID (obj);
494*6881a400Schristos   return pspace_to_pspace_object (obj->program_space).release ();
495*6881a400Schristos }
496*6881a400Schristos 
497*6881a400Schristos /* This implements the disassemble_info read_memory_func callback and is
498*6881a400Schristos    called from the libopcodes disassembler when the disassembler wants to
499*6881a400Schristos    read memory.
500*6881a400Schristos 
501*6881a400Schristos    From the INFO argument we can find the gdbpy_disassembler object for
502*6881a400Schristos    which we are disassembling, and from that object we can find the
503*6881a400Schristos    DisassembleInfo for the current disassembly call.
504*6881a400Schristos 
505*6881a400Schristos    This function reads the instruction bytes by calling the read_memory
506*6881a400Schristos    method on the DisassembleInfo object.  This method might have been
507*6881a400Schristos    overridden by user code.
508*6881a400Schristos 
509*6881a400Schristos    Read LEN bytes from MEMADDR and place them into BUFF.  Return 0 on
510*6881a400Schristos    success (in which case BUFF has been filled), or -1 on error, in which
511*6881a400Schristos    case the contents of BUFF are undefined.  */
512*6881a400Schristos 
513*6881a400Schristos int
514*6881a400Schristos gdbpy_disassembler::read_memory_func (bfd_vma memaddr, gdb_byte *buff,
515*6881a400Schristos 				      unsigned int len,
516*6881a400Schristos 				      struct disassemble_info *info) noexcept
517*6881a400Schristos {
518*6881a400Schristos   gdbpy_disassembler *dis
519*6881a400Schristos     = static_cast<gdbpy_disassembler *> (info->application_data);
520*6881a400Schristos   disasm_info_object *obj = dis->py_disasm_info ();
521*6881a400Schristos 
522*6881a400Schristos   /* If a previous read attempt resulted in an exception, then we don't
523*6881a400Schristos      allow any further reads to succeed.  We only do this check for the
524*6881a400Schristos      read_memory_func as this is the only one the user can hook into,
525*6881a400Schristos      thus, this check prevents us calling back into user code if a
526*6881a400Schristos      previous call has already thrown an error.  */
527*6881a400Schristos   if (dis->has_stored_exception ())
528*6881a400Schristos     return -1;
529*6881a400Schristos 
530*6881a400Schristos   /* The DisassembleInfo.read_memory method expects an offset from the
531*6881a400Schristos      address stored within the DisassembleInfo object; calculate that
532*6881a400Schristos      offset here.  */
533*6881a400Schristos   LONGEST offset = (LONGEST) memaddr - (LONGEST) obj->address;
534*6881a400Schristos 
535*6881a400Schristos   /* Now call the DisassembleInfo.read_memory method.  This might have been
536*6881a400Schristos      overridden by the user.  */
537*6881a400Schristos   gdbpy_ref<> result_obj (PyObject_CallMethod ((PyObject *) obj,
538*6881a400Schristos 					       "read_memory",
539*6881a400Schristos 					       "KL", len, offset));
540*6881a400Schristos 
541*6881a400Schristos   /* Handle any exceptions.  */
542*6881a400Schristos   if (result_obj == nullptr)
543*6881a400Schristos     {
544*6881a400Schristos       /* If we got a gdb.MemoryError then we ignore this and just report
545*6881a400Schristos 	 that the read failed to the caller.  The caller is then
546*6881a400Schristos 	 responsible for calling the memory_error_func if it wants to.
547*6881a400Schristos 	 Remember, the disassembler might just be probing to see if these
548*6881a400Schristos 	 bytes can be read, if we automatically call the memory error
549*6881a400Schristos 	 function, we can end up registering an error prematurely.  */
550*6881a400Schristos       if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
551*6881a400Schristos 	{
552*6881a400Schristos 	  PyErr_Clear ();
553*6881a400Schristos 	  return -1;
554*6881a400Schristos 	}
555*6881a400Schristos 
556*6881a400Schristos       /* For any other exception type we capture the value of the Python
557*6881a400Schristos 	 exception and throw it, this will then be caught in
558*6881a400Schristos 	 disasmpy_builtin_disassemble, at which point the exception will be
559*6881a400Schristos 	 restored.  */
560*6881a400Schristos       dis->store_exception (gdbpy_err_fetch ());
561*6881a400Schristos       return -1;
562*6881a400Schristos     }
563*6881a400Schristos 
564*6881a400Schristos   /* Convert the result to a buffer.  */
565*6881a400Schristos   Py_buffer py_buff;
566*6881a400Schristos   if (!PyObject_CheckBuffer (result_obj.get ())
567*6881a400Schristos       || PyObject_GetBuffer (result_obj.get(), &py_buff, PyBUF_CONTIG_RO) < 0)
568*6881a400Schristos     {
569*6881a400Schristos       PyErr_Format (PyExc_TypeError,
570*6881a400Schristos 		    _("Result from read_memory is not a buffer"));
571*6881a400Schristos       dis->store_exception (gdbpy_err_fetch ());
572*6881a400Schristos       return -1;
573*6881a400Schristos     }
574*6881a400Schristos 
575*6881a400Schristos   /* Wrap PY_BUFF so that it is cleaned up correctly at the end of this
576*6881a400Schristos      scope.  */
577*6881a400Schristos   Py_buffer_up buffer_up (&py_buff);
578*6881a400Schristos 
579*6881a400Schristos   /* Validate that the buffer is the correct length.  */
580*6881a400Schristos   if (py_buff.len != len)
581*6881a400Schristos     {
582*6881a400Schristos       PyErr_Format (PyExc_ValueError,
583*6881a400Schristos 		    _("Buffer returned from read_memory is sized %d instead of the expected %d"),
584*6881a400Schristos 		    py_buff.len, len);
585*6881a400Schristos       dis->store_exception (gdbpy_err_fetch ());
586*6881a400Schristos       return -1;
587*6881a400Schristos     }
588*6881a400Schristos 
589*6881a400Schristos   /* Copy the data out of the Python buffer and return success.  */
590*6881a400Schristos   const gdb_byte *buffer = (const gdb_byte *) py_buff.buf;
591*6881a400Schristos   memcpy (buff, buffer, len);
592*6881a400Schristos   return 0;
593*6881a400Schristos }
594*6881a400Schristos 
595*6881a400Schristos /* Implement DisassemblerResult.length attribute, return the length of the
596*6881a400Schristos    disassembled instruction.  */
597*6881a400Schristos 
598*6881a400Schristos static PyObject *
599*6881a400Schristos disasmpy_result_length (PyObject *self, void *closure)
600*6881a400Schristos {
601*6881a400Schristos   disasm_result_object *obj = (disasm_result_object *) self;
602*6881a400Schristos   return gdb_py_object_from_longest (obj->length).release ();
603*6881a400Schristos }
604*6881a400Schristos 
605*6881a400Schristos /* Implement DisassemblerResult.string attribute, return the content string
606*6881a400Schristos    of the disassembled instruction.  */
607*6881a400Schristos 
608*6881a400Schristos static PyObject *
609*6881a400Schristos disasmpy_result_string (PyObject *self, void *closure)
610*6881a400Schristos {
611*6881a400Schristos   disasm_result_object *obj = (disasm_result_object *) self;
612*6881a400Schristos 
613*6881a400Schristos   gdb_assert (obj->content != nullptr);
614*6881a400Schristos   gdb_assert (strlen (obj->content->c_str ()) > 0);
615*6881a400Schristos   gdb_assert (obj->length > 0);
616*6881a400Schristos   return PyUnicode_Decode (obj->content->c_str (),
617*6881a400Schristos 			   obj->content->size (),
618*6881a400Schristos 			   host_charset (), nullptr);
619*6881a400Schristos }
620*6881a400Schristos 
621*6881a400Schristos /* Implement DisassemblerResult.__init__.  Takes two arguments, an
622*6881a400Schristos    integer, the length in bytes of the disassembled instruction, and a
623*6881a400Schristos    string, the disassembled content of the instruction.  */
624*6881a400Schristos 
625*6881a400Schristos static int
626*6881a400Schristos disasmpy_result_init (PyObject *self, PyObject *args, PyObject *kwargs)
627*6881a400Schristos {
628*6881a400Schristos   static const char *keywords[] = { "length", "string", NULL };
629*6881a400Schristos   int length;
630*6881a400Schristos   const char *string;
631*6881a400Schristos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "is", keywords,
632*6881a400Schristos 					&length, &string))
633*6881a400Schristos     return -1;
634*6881a400Schristos 
635*6881a400Schristos   if (length <= 0)
636*6881a400Schristos     {
637*6881a400Schristos       PyErr_SetString (PyExc_ValueError,
638*6881a400Schristos 		       _("Length must be greater than 0."));
639*6881a400Schristos       return -1;
640*6881a400Schristos     }
641*6881a400Schristos 
642*6881a400Schristos   if (strlen (string) == 0)
643*6881a400Schristos     {
644*6881a400Schristos       PyErr_SetString (PyExc_ValueError,
645*6881a400Schristos 		       _("String must not be empty."));
646*6881a400Schristos       return -1;
647*6881a400Schristos     }
648*6881a400Schristos 
649*6881a400Schristos   disasm_result_object *obj = (disasm_result_object *) self;
650*6881a400Schristos   disasmpy_init_disassembler_result (obj, length, std::string (string));
651*6881a400Schristos 
652*6881a400Schristos   return 0;
653*6881a400Schristos }
654*6881a400Schristos 
655*6881a400Schristos /* Implement memory_error_func callback for disassemble_info.  Extract the
656*6881a400Schristos    underlying DisassembleInfo Python object, and set a memory error on
657*6881a400Schristos    it.  */
658*6881a400Schristos 
659*6881a400Schristos void
660*6881a400Schristos gdbpy_disassembler::memory_error_func (int status, bfd_vma memaddr,
661*6881a400Schristos 				       struct disassemble_info *info) noexcept
662*6881a400Schristos {
663*6881a400Schristos   gdbpy_disassembler *dis
664*6881a400Schristos     = static_cast<gdbpy_disassembler *> (info->application_data);
665*6881a400Schristos   dis->m_memory_error_address.emplace (memaddr);
666*6881a400Schristos }
667*6881a400Schristos 
668*6881a400Schristos /* Wrapper of print_address.  */
669*6881a400Schristos 
670*6881a400Schristos void
671*6881a400Schristos gdbpy_disassembler::print_address_func (bfd_vma addr,
672*6881a400Schristos 					struct disassemble_info *info) noexcept
673*6881a400Schristos {
674*6881a400Schristos   gdbpy_disassembler *dis
675*6881a400Schristos     = static_cast<gdbpy_disassembler *> (info->application_data);
676*6881a400Schristos   print_address (dis->arch (), addr, dis->stream ());
677*6881a400Schristos }
678*6881a400Schristos 
679*6881a400Schristos /* constructor.  */
680*6881a400Schristos 
681*6881a400Schristos gdbpy_disassembler::gdbpy_disassembler (disasm_info_object *obj,
682*6881a400Schristos 					PyObject *memory_source)
683*6881a400Schristos   : gdb_printing_disassembler (obj->gdbarch, &m_string_file,
684*6881a400Schristos 			       read_memory_func, memory_error_func,
685*6881a400Schristos 			       print_address_func),
686*6881a400Schristos     m_disasm_info_object (obj),
687*6881a400Schristos     m_memory_source (memory_source)
688*6881a400Schristos { /* Nothing.  */ }
689*6881a400Schristos 
690*6881a400Schristos /* A wrapper around a reference to a Python DisassembleInfo object, which
691*6881a400Schristos    ensures that the object is marked as invalid when we leave the enclosing
692*6881a400Schristos    scope.
693*6881a400Schristos 
694*6881a400Schristos    Each DisassembleInfo is created in gdbpy_print_insn, and is done with by
695*6881a400Schristos    the time that function returns.  However, there's nothing to stop a user
696*6881a400Schristos    caching a reference to the DisassembleInfo, and thus keeping the object
697*6881a400Schristos    around.
698*6881a400Schristos 
699*6881a400Schristos    We therefore have the notion of a DisassembleInfo becoming invalid, this
700*6881a400Schristos    happens when gdbpy_print_insn returns.  This class is responsible for
701*6881a400Schristos    marking the DisassembleInfo as invalid in its destructor.  */
702*6881a400Schristos 
703*6881a400Schristos struct scoped_disasm_info_object
704*6881a400Schristos {
705*6881a400Schristos   /* Constructor.  */
706*6881a400Schristos   scoped_disasm_info_object (struct gdbarch *gdbarch, CORE_ADDR memaddr,
707*6881a400Schristos 			     disassemble_info *info)
708*6881a400Schristos     : m_disasm_info (allocate_disasm_info_object ())
709*6881a400Schristos   {
710*6881a400Schristos     disasm_info_fill (m_disasm_info.get (), gdbarch, current_program_space,
711*6881a400Schristos 		      memaddr, info, nullptr);
712*6881a400Schristos   }
713*6881a400Schristos 
714*6881a400Schristos   /* Upon destruction mark m_diasm_info as invalid.  */
715*6881a400Schristos   ~scoped_disasm_info_object ()
716*6881a400Schristos   {
717*6881a400Schristos     /* Invalidate the original DisassembleInfo object as well as any copies
718*6881a400Schristos        that the user might have made.  */
719*6881a400Schristos     for (disasm_info_object *obj = m_disasm_info.get ();
720*6881a400Schristos 	 obj != nullptr;
721*6881a400Schristos 	 obj = obj->next)
722*6881a400Schristos       obj->gdb_info = nullptr;
723*6881a400Schristos   }
724*6881a400Schristos 
725*6881a400Schristos   /* Return a pointer to the underlying disasm_info_object instance.  */
726*6881a400Schristos   disasm_info_object *
727*6881a400Schristos   get () const
728*6881a400Schristos   {
729*6881a400Schristos     return m_disasm_info.get ();
730*6881a400Schristos   }
731*6881a400Schristos 
732*6881a400Schristos private:
733*6881a400Schristos 
734*6881a400Schristos   /* Wrapper around the call to PyObject_New, this wrapper function can be
735*6881a400Schristos      called from the constructor initialization list, while PyObject_New, a
736*6881a400Schristos      macro, can't.  */
737*6881a400Schristos   static disasm_info_object *
738*6881a400Schristos   allocate_disasm_info_object ()
739*6881a400Schristos   {
740*6881a400Schristos     return (disasm_info_object *) PyObject_New (disasm_info_object,
741*6881a400Schristos 						&disasm_info_object_type);
742*6881a400Schristos   }
743*6881a400Schristos 
744*6881a400Schristos   /* A reference to a gdb.disassembler.DisassembleInfo object.  When this
745*6881a400Schristos      containing instance goes out of scope this reference is released,
746*6881a400Schristos      however, the user might be holding other references to the
747*6881a400Schristos      DisassembleInfo object in Python code, so the underlying object might
748*6881a400Schristos      not be deleted.  */
749*6881a400Schristos   gdbpy_ref<disasm_info_object> m_disasm_info;
750*6881a400Schristos };
751*6881a400Schristos 
752*6881a400Schristos /* See python-internal.h.  */
753*6881a400Schristos 
754*6881a400Schristos gdb::optional<int>
755*6881a400Schristos gdbpy_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
756*6881a400Schristos 		  disassemble_info *info)
757*6881a400Schristos {
758*6881a400Schristos   /* Early exit case.  This must be done as early as possible, and
759*6881a400Schristos      definitely before we enter Python environment.  The
760*6881a400Schristos      python_print_insn_enabled flag is set (from Python) only when the user
761*6881a400Schristos      has installed one (or more) Python disassemblers.  So in the common
762*6881a400Schristos      case (no custom disassembler installed) this flag will be false,
763*6881a400Schristos      allowing for a quick return.  */
764*6881a400Schristos   if (!gdb_python_initialized || !python_print_insn_enabled)
765*6881a400Schristos     return {};
766*6881a400Schristos 
767*6881a400Schristos   gdbpy_enter enter_py (get_current_arch (), current_language);
768*6881a400Schristos 
769*6881a400Schristos   /* Import the gdb.disassembler module.  */
770*6881a400Schristos   gdbpy_ref<> gdb_python_disassembler_module
771*6881a400Schristos     (PyImport_ImportModule ("gdb.disassembler"));
772*6881a400Schristos   if (gdb_python_disassembler_module == nullptr)
773*6881a400Schristos     {
774*6881a400Schristos       gdbpy_print_stack ();
775*6881a400Schristos       return {};
776*6881a400Schristos     }
777*6881a400Schristos 
778*6881a400Schristos   /* Get the _print_insn attribute from the module, this should be the
779*6881a400Schristos      function we are going to call to actually perform the disassembly.  */
780*6881a400Schristos   gdbpy_ref<> hook
781*6881a400Schristos     (PyObject_GetAttrString (gdb_python_disassembler_module.get (),
782*6881a400Schristos 			     "_print_insn"));
783*6881a400Schristos   if (hook == nullptr)
784*6881a400Schristos     {
785*6881a400Schristos       gdbpy_print_stack ();
786*6881a400Schristos       return {};
787*6881a400Schristos     }
788*6881a400Schristos 
789*6881a400Schristos   /* Create the new DisassembleInfo object we will pass into Python.  This
790*6881a400Schristos      object will be marked as invalid when we leave this scope.  */
791*6881a400Schristos   scoped_disasm_info_object scoped_disasm_info (gdbarch, memaddr, info);
792*6881a400Schristos   disasm_info_object *disasm_info = scoped_disasm_info.get ();
793*6881a400Schristos 
794*6881a400Schristos   /* Call into the registered disassembler to (possibly) perform the
795*6881a400Schristos      disassembly.  */
796*6881a400Schristos   PyObject *insn_disas_obj = (PyObject *) disasm_info;
797*6881a400Schristos   gdbpy_ref<> result (PyObject_CallFunctionObjArgs (hook.get (),
798*6881a400Schristos 						    insn_disas_obj,
799*6881a400Schristos 						    nullptr));
800*6881a400Schristos 
801*6881a400Schristos   if (result == nullptr)
802*6881a400Schristos     {
803*6881a400Schristos       /* The call into Python code resulted in an exception.  If this was a
804*6881a400Schristos 	 gdb.MemoryError, then we can figure out an address and call the
805*6881a400Schristos 	 disassemble_info::memory_error_func to report the error back to
806*6881a400Schristos 	 core GDB.  Any other exception type we report back to core GDB as
807*6881a400Schristos 	 an unknown error (return -1 without first calling the
808*6881a400Schristos 	 memory_error_func callback).  */
809*6881a400Schristos 
810*6881a400Schristos       if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
811*6881a400Schristos 	{
812*6881a400Schristos 	  /* A gdb.MemoryError might have an address attribute which
813*6881a400Schristos 	     contains the address at which the memory error occurred.  If
814*6881a400Schristos 	     this is the case then use this address, otherwise, fallback to
815*6881a400Schristos 	     just using the address of the instruction we were asked to
816*6881a400Schristos 	     disassemble.  */
817*6881a400Schristos 	  gdbpy_err_fetch err;
818*6881a400Schristos 	  PyErr_Clear ();
819*6881a400Schristos 
820*6881a400Schristos 	  CORE_ADDR addr;
821*6881a400Schristos 	  if (err.value () != nullptr
822*6881a400Schristos 	      && PyObject_HasAttrString (err.value ().get (), "address"))
823*6881a400Schristos 	    {
824*6881a400Schristos 	      PyObject *addr_obj
825*6881a400Schristos 		= PyObject_GetAttrString (err.value ().get (), "address");
826*6881a400Schristos 	      if (get_addr_from_python (addr_obj, &addr) < 0)
827*6881a400Schristos 		addr = disasm_info->address;
828*6881a400Schristos 	    }
829*6881a400Schristos 	  else
830*6881a400Schristos 	    addr = disasm_info->address;
831*6881a400Schristos 
832*6881a400Schristos 	  info->memory_error_func (-1, addr, info);
833*6881a400Schristos 	  return gdb::optional<int> (-1);
834*6881a400Schristos 	}
835*6881a400Schristos       else if (PyErr_ExceptionMatches (gdbpy_gdberror_exc))
836*6881a400Schristos 	{
837*6881a400Schristos 	  gdbpy_err_fetch err;
838*6881a400Schristos 	  gdb::unique_xmalloc_ptr<char> msg = err.to_string ();
839*6881a400Schristos 
840*6881a400Schristos 	  info->fprintf_func (info->stream, "%s", msg.get ());
841*6881a400Schristos 	  return gdb::optional<int> (-1);
842*6881a400Schristos 	}
843*6881a400Schristos       else
844*6881a400Schristos 	{
845*6881a400Schristos 	  gdbpy_print_stack ();
846*6881a400Schristos 	  return gdb::optional<int> (-1);
847*6881a400Schristos 	}
848*6881a400Schristos 
849*6881a400Schristos     }
850*6881a400Schristos   else if (result == Py_None)
851*6881a400Schristos     {
852*6881a400Schristos       /* A return value of None indicates that the Python code could not,
853*6881a400Schristos 	 or doesn't want to, disassemble this instruction.  Just return an
854*6881a400Schristos 	 empty result and core GDB will try to disassemble this for us.  */
855*6881a400Schristos       return {};
856*6881a400Schristos     }
857*6881a400Schristos 
858*6881a400Schristos   /* Check the result is a DisassemblerResult (or a sub-class).  */
859*6881a400Schristos   if (!PyObject_IsInstance (result.get (),
860*6881a400Schristos 			    (PyObject *) &disasm_result_object_type))
861*6881a400Schristos     {
862*6881a400Schristos       PyErr_SetString (PyExc_TypeError,
863*6881a400Schristos 		       _("Result is not a DisassemblerResult."));
864*6881a400Schristos       gdbpy_print_stack ();
865*6881a400Schristos       return gdb::optional<int> (-1);
866*6881a400Schristos     }
867*6881a400Schristos 
868*6881a400Schristos   /* The call into Python neither raised an exception, or returned None.
869*6881a400Schristos      Check to see if the result looks valid.  */
870*6881a400Schristos   gdbpy_ref<> length_obj (PyObject_GetAttrString (result.get (), "length"));
871*6881a400Schristos   if (length_obj == nullptr)
872*6881a400Schristos     {
873*6881a400Schristos       gdbpy_print_stack ();
874*6881a400Schristos       return gdb::optional<int> (-1);
875*6881a400Schristos     }
876*6881a400Schristos 
877*6881a400Schristos   gdbpy_ref<> string_obj (PyObject_GetAttrString (result.get (), "string"));
878*6881a400Schristos   if (string_obj == nullptr)
879*6881a400Schristos     {
880*6881a400Schristos       gdbpy_print_stack ();
881*6881a400Schristos       return gdb::optional<int> (-1);
882*6881a400Schristos     }
883*6881a400Schristos   if (!gdbpy_is_string (string_obj.get ()))
884*6881a400Schristos     {
885*6881a400Schristos       PyErr_SetString (PyExc_TypeError, _("String attribute is not a string."));
886*6881a400Schristos       gdbpy_print_stack ();
887*6881a400Schristos       return gdb::optional<int> (-1);
888*6881a400Schristos     }
889*6881a400Schristos 
890*6881a400Schristos   gdb::unique_xmalloc_ptr<char> string
891*6881a400Schristos     = gdbpy_obj_to_string (string_obj.get ());
892*6881a400Schristos   if (string == nullptr)
893*6881a400Schristos     {
894*6881a400Schristos       gdbpy_print_stack ();
895*6881a400Schristos       return gdb::optional<int> (-1);
896*6881a400Schristos     }
897*6881a400Schristos 
898*6881a400Schristos   long length;
899*6881a400Schristos   if (!gdb_py_int_as_long (length_obj.get (), &length))
900*6881a400Schristos     {
901*6881a400Schristos       gdbpy_print_stack ();
902*6881a400Schristos       return gdb::optional<int> (-1);
903*6881a400Schristos     }
904*6881a400Schristos 
905*6881a400Schristos   long max_insn_length = (gdbarch_max_insn_length_p (gdbarch) ?
906*6881a400Schristos 			  gdbarch_max_insn_length (gdbarch) : INT_MAX);
907*6881a400Schristos   if (length <= 0)
908*6881a400Schristos     {
909*6881a400Schristos       PyErr_SetString
910*6881a400Schristos 	(PyExc_ValueError,
911*6881a400Schristos 	 _("Invalid length attribute: length must be greater than 0."));
912*6881a400Schristos       gdbpy_print_stack ();
913*6881a400Schristos       return gdb::optional<int> (-1);
914*6881a400Schristos     }
915*6881a400Schristos   if (length > max_insn_length)
916*6881a400Schristos     {
917*6881a400Schristos       PyErr_Format
918*6881a400Schristos 	(PyExc_ValueError,
919*6881a400Schristos 	 _("Invalid length attribute: length %d greater than architecture maximum of %d"),
920*6881a400Schristos 	 length, max_insn_length);
921*6881a400Schristos       gdbpy_print_stack ();
922*6881a400Schristos       return gdb::optional<int> (-1);
923*6881a400Schristos     }
924*6881a400Schristos 
925*6881a400Schristos   if (strlen (string.get ()) == 0)
926*6881a400Schristos     {
927*6881a400Schristos       PyErr_SetString (PyExc_ValueError,
928*6881a400Schristos 		       _("String attribute must not be empty."));
929*6881a400Schristos       gdbpy_print_stack ();
930*6881a400Schristos       return gdb::optional<int> (-1);
931*6881a400Schristos     }
932*6881a400Schristos 
933*6881a400Schristos   /* Print the disassembled instruction back to core GDB, and return the
934*6881a400Schristos      length of the disassembled instruction.  */
935*6881a400Schristos   info->fprintf_func (info->stream, "%s", string.get ());
936*6881a400Schristos   return gdb::optional<int> (length);
937*6881a400Schristos }
938*6881a400Schristos 
939*6881a400Schristos /* The tp_dealloc callback for the DisassemblerResult type.  Takes care of
940*6881a400Schristos    deallocating the content buffer.  */
941*6881a400Schristos 
942*6881a400Schristos static void
943*6881a400Schristos disasmpy_dealloc_result (PyObject *self)
944*6881a400Schristos {
945*6881a400Schristos   disasm_result_object *obj = (disasm_result_object *) self;
946*6881a400Schristos   delete obj->content;
947*6881a400Schristos   Py_TYPE (self)->tp_free (self);
948*6881a400Schristos }
949*6881a400Schristos 
950*6881a400Schristos /* The get/set attributes of the gdb.disassembler.DisassembleInfo type.  */
951*6881a400Schristos 
952*6881a400Schristos static gdb_PyGetSetDef disasm_info_object_getset[] = {
953*6881a400Schristos   { "address", disasmpy_info_address, nullptr,
954*6881a400Schristos     "Start address of the instruction to disassemble.", nullptr },
955*6881a400Schristos   { "architecture", disasmpy_info_architecture, nullptr,
956*6881a400Schristos     "Architecture to disassemble in", nullptr },
957*6881a400Schristos   { "progspace", disasmpy_info_progspace, nullptr,
958*6881a400Schristos     "Program space to disassemble in", nullptr },
959*6881a400Schristos   { nullptr }   /* Sentinel */
960*6881a400Schristos };
961*6881a400Schristos 
962*6881a400Schristos /* The methods of the gdb.disassembler.DisassembleInfo type.  */
963*6881a400Schristos 
964*6881a400Schristos static PyMethodDef disasm_info_object_methods[] = {
965*6881a400Schristos   { "read_memory", (PyCFunction) disasmpy_info_read_memory,
966*6881a400Schristos     METH_VARARGS | METH_KEYWORDS,
967*6881a400Schristos     "read_memory (LEN, OFFSET = 0) -> Octets[]\n\
968*6881a400Schristos Read LEN octets for the instruction to disassemble." },
969*6881a400Schristos   { "is_valid", disasmpy_info_is_valid, METH_NOARGS,
970*6881a400Schristos     "is_valid () -> Boolean.\n\
971*6881a400Schristos Return true if this DisassembleInfo is valid, false if not." },
972*6881a400Schristos   {nullptr}  /* Sentinel */
973*6881a400Schristos };
974*6881a400Schristos 
975*6881a400Schristos /* The get/set attributes of the gdb.disassembler.DisassemblerResult type.  */
976*6881a400Schristos 
977*6881a400Schristos static gdb_PyGetSetDef disasm_result_object_getset[] = {
978*6881a400Schristos   { "length", disasmpy_result_length, nullptr,
979*6881a400Schristos     "Length of the disassembled instruction.", nullptr },
980*6881a400Schristos   { "string", disasmpy_result_string, nullptr,
981*6881a400Schristos     "String representing the disassembled instruction.", nullptr },
982*6881a400Schristos   { nullptr }   /* Sentinel */
983*6881a400Schristos };
984*6881a400Schristos 
985*6881a400Schristos /* These are the methods we add into the _gdb.disassembler module, which
986*6881a400Schristos    are then imported into the gdb.disassembler module.  These are global
987*6881a400Schristos    functions that support performing disassembly.  */
988*6881a400Schristos 
989*6881a400Schristos PyMethodDef python_disassembler_methods[] =
990*6881a400Schristos {
991*6881a400Schristos   { "builtin_disassemble", (PyCFunction) disasmpy_builtin_disassemble,
992*6881a400Schristos     METH_VARARGS | METH_KEYWORDS,
993*6881a400Schristos     "builtin_disassemble (INFO, MEMORY_SOURCE = None) -> None\n\
994*6881a400Schristos Disassemble using GDB's builtin disassembler.  INFO is an instance of\n\
995*6881a400Schristos gdb.disassembler.DisassembleInfo.  The MEMORY_SOURCE, if not None, should\n\
996*6881a400Schristos be an object with the read_memory method." },
997*6881a400Schristos   { "_set_enabled", (PyCFunction) disasmpy_set_enabled,
998*6881a400Schristos     METH_VARARGS | METH_KEYWORDS,
999*6881a400Schristos     "_set_enabled (STATE) -> None\n\
1000*6881a400Schristos Set whether GDB should call into the Python _print_insn code or not." },
1001*6881a400Schristos   {nullptr, nullptr, 0, nullptr}
1002*6881a400Schristos };
1003*6881a400Schristos 
1004*6881a400Schristos /* Structure to define the _gdb.disassembler module.  */
1005*6881a400Schristos 
1006*6881a400Schristos static struct PyModuleDef python_disassembler_module_def =
1007*6881a400Schristos {
1008*6881a400Schristos   PyModuleDef_HEAD_INIT,
1009*6881a400Schristos   "_gdb.disassembler",
1010*6881a400Schristos   nullptr,
1011*6881a400Schristos   -1,
1012*6881a400Schristos   python_disassembler_methods,
1013*6881a400Schristos   nullptr,
1014*6881a400Schristos   nullptr,
1015*6881a400Schristos   nullptr,
1016*6881a400Schristos   nullptr
1017*6881a400Schristos };
1018*6881a400Schristos 
1019*6881a400Schristos /* Called to initialize the Python structures in this file.  */
1020*6881a400Schristos 
1021*6881a400Schristos int
1022*6881a400Schristos gdbpy_initialize_disasm ()
1023*6881a400Schristos {
1024*6881a400Schristos   /* Create the _gdb.disassembler module, and add it to the _gdb module.  */
1025*6881a400Schristos 
1026*6881a400Schristos   PyObject *gdb_disassembler_module;
1027*6881a400Schristos   gdb_disassembler_module = PyModule_Create (&python_disassembler_module_def);
1028*6881a400Schristos   if (gdb_disassembler_module == nullptr)
1029*6881a400Schristos     return -1;
1030*6881a400Schristos   PyModule_AddObject(gdb_module, "disassembler", gdb_disassembler_module);
1031*6881a400Schristos 
1032*6881a400Schristos   /* This is needed so that 'import _gdb.disassembler' will work.  */
1033*6881a400Schristos   PyObject *dict = PyImport_GetModuleDict ();
1034*6881a400Schristos   PyDict_SetItemString (dict, "_gdb.disassembler", gdb_disassembler_module);
1035*6881a400Schristos 
1036*6881a400Schristos   disasm_info_object_type.tp_new = PyType_GenericNew;
1037*6881a400Schristos   if (PyType_Ready (&disasm_info_object_type) < 0)
1038*6881a400Schristos     return -1;
1039*6881a400Schristos 
1040*6881a400Schristos   if (gdb_pymodule_addobject (gdb_disassembler_module, "DisassembleInfo",
1041*6881a400Schristos 			      (PyObject *) &disasm_info_object_type) < 0)
1042*6881a400Schristos     return -1;
1043*6881a400Schristos 
1044*6881a400Schristos   disasm_result_object_type.tp_new = PyType_GenericNew;
1045*6881a400Schristos   if (PyType_Ready (&disasm_result_object_type) < 0)
1046*6881a400Schristos     return -1;
1047*6881a400Schristos 
1048*6881a400Schristos   if (gdb_pymodule_addobject (gdb_disassembler_module, "DisassemblerResult",
1049*6881a400Schristos 			      (PyObject *) &disasm_result_object_type) < 0)
1050*6881a400Schristos     return -1;
1051*6881a400Schristos 
1052*6881a400Schristos   return 0;
1053*6881a400Schristos }
1054*6881a400Schristos 
1055*6881a400Schristos /* Describe the gdb.disassembler.DisassembleInfo type.  */
1056*6881a400Schristos 
1057*6881a400Schristos PyTypeObject disasm_info_object_type = {
1058*6881a400Schristos   PyVarObject_HEAD_INIT (nullptr, 0)
1059*6881a400Schristos   "gdb.disassembler.DisassembleInfo",		/*tp_name*/
1060*6881a400Schristos   sizeof (disasm_info_object),			/*tp_basicsize*/
1061*6881a400Schristos   0,						/*tp_itemsize*/
1062*6881a400Schristos   disasm_info_dealloc,				/*tp_dealloc*/
1063*6881a400Schristos   0,						/*tp_print*/
1064*6881a400Schristos   0,						/*tp_getattr*/
1065*6881a400Schristos   0,						/*tp_setattr*/
1066*6881a400Schristos   0,						/*tp_compare*/
1067*6881a400Schristos   0,						/*tp_repr*/
1068*6881a400Schristos   0,						/*tp_as_number*/
1069*6881a400Schristos   0,						/*tp_as_sequence*/
1070*6881a400Schristos   0,						/*tp_as_mapping*/
1071*6881a400Schristos   0,						/*tp_hash */
1072*6881a400Schristos   0,						/*tp_call*/
1073*6881a400Schristos   0,						/*tp_str*/
1074*6881a400Schristos   0,						/*tp_getattro*/
1075*6881a400Schristos   0,						/*tp_setattro*/
1076*6881a400Schristos   0,						/*tp_as_buffer*/
1077*6881a400Schristos   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/*tp_flags*/
1078*6881a400Schristos   "GDB instruction disassembler object",	/* tp_doc */
1079*6881a400Schristos   0,						/* tp_traverse */
1080*6881a400Schristos   0,						/* tp_clear */
1081*6881a400Schristos   0,						/* tp_richcompare */
1082*6881a400Schristos   0,						/* tp_weaklistoffset */
1083*6881a400Schristos   0,						/* tp_iter */
1084*6881a400Schristos   0,						/* tp_iternext */
1085*6881a400Schristos   disasm_info_object_methods,			/* tp_methods */
1086*6881a400Schristos   0,						/* tp_members */
1087*6881a400Schristos   disasm_info_object_getset,			/* tp_getset */
1088*6881a400Schristos   0,						/* tp_base */
1089*6881a400Schristos   0,						/* tp_dict */
1090*6881a400Schristos   0,						/* tp_descr_get */
1091*6881a400Schristos   0,						/* tp_descr_set */
1092*6881a400Schristos   0,						/* tp_dictoffset */
1093*6881a400Schristos   disasm_info_init,				/* tp_init */
1094*6881a400Schristos   0,						/* tp_alloc */
1095*6881a400Schristos };
1096*6881a400Schristos 
1097*6881a400Schristos /* Describe the gdb.disassembler.DisassemblerResult type.  */
1098*6881a400Schristos 
1099*6881a400Schristos PyTypeObject disasm_result_object_type = {
1100*6881a400Schristos   PyVarObject_HEAD_INIT (nullptr, 0)
1101*6881a400Schristos   "gdb.disassembler.DisassemblerResult",	/*tp_name*/
1102*6881a400Schristos   sizeof (disasm_result_object),		/*tp_basicsize*/
1103*6881a400Schristos   0,						/*tp_itemsize*/
1104*6881a400Schristos   disasmpy_dealloc_result,			/*tp_dealloc*/
1105*6881a400Schristos   0,						/*tp_print*/
1106*6881a400Schristos   0,						/*tp_getattr*/
1107*6881a400Schristos   0,						/*tp_setattr*/
1108*6881a400Schristos   0,						/*tp_compare*/
1109*6881a400Schristos   0,						/*tp_repr*/
1110*6881a400Schristos   0,						/*tp_as_number*/
1111*6881a400Schristos   0,						/*tp_as_sequence*/
1112*6881a400Schristos   0,						/*tp_as_mapping*/
1113*6881a400Schristos   0,						/*tp_hash */
1114*6881a400Schristos   0,						/*tp_call*/
1115*6881a400Schristos   0,						/*tp_str*/
1116*6881a400Schristos   0,						/*tp_getattro*/
1117*6881a400Schristos   0,						/*tp_setattro*/
1118*6881a400Schristos   0,						/*tp_as_buffer*/
1119*6881a400Schristos   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/*tp_flags*/
1120*6881a400Schristos   "GDB object, representing a disassembler result",	/* tp_doc */
1121*6881a400Schristos   0,						/* tp_traverse */
1122*6881a400Schristos   0,						/* tp_clear */
1123*6881a400Schristos   0,						/* tp_richcompare */
1124*6881a400Schristos   0,						/* tp_weaklistoffset */
1125*6881a400Schristos   0,						/* tp_iter */
1126*6881a400Schristos   0,						/* tp_iternext */
1127*6881a400Schristos   0,						/* tp_methods */
1128*6881a400Schristos   0,						/* tp_members */
1129*6881a400Schristos   disasm_result_object_getset,			/* tp_getset */
1130*6881a400Schristos   0,						/* tp_base */
1131*6881a400Schristos   0,						/* tp_dict */
1132*6881a400Schristos   0,						/* tp_descr_get */
1133*6881a400Schristos   0,						/* tp_descr_set */
1134*6881a400Schristos   0,						/* tp_dictoffset */
1135*6881a400Schristos   disasmpy_result_init,				/* tp_init */
1136*6881a400Schristos   0,						/* tp_alloc */
1137*6881a400Schristos };
1138