GDB (xrefs)
py-arch.c
Go to the documentation of this file.
1 /* Python interface to architecture
2 
3  Copyright (C) 2013-2018 Free Software Foundation, Inc.
4 
5  This file is part of GDB.
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 
20 #include "defs.h"
21 #include "gdbarch.h"
22 #include "arch-utils.h"
23 #include "disasm.h"
24 #include "python-internal.h"
25 #include "py-ref.h"
26 
27 typedef struct arch_object_type_object {
28  PyObject_HEAD
29  struct gdbarch *gdbarch;
30 } arch_object;
31 
32 static struct gdbarch_data *arch_object_data = NULL;
33 
34 /* Require a valid Architecture. */
35 #define ARCHPY_REQUIRE_VALID(arch_obj, arch) \
36  do { \
37  arch = arch_object_to_gdbarch (arch_obj); \
38  if (arch == NULL) \
39  { \
40  PyErr_SetString (PyExc_RuntimeError, \
41  _("Architecture is invalid.")); \
42  return NULL; \
43  } \
44  } while (0)
45 
46 extern PyTypeObject arch_object_type
47  CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("arch_object");
48 
49 /* Associates an arch_object with GDBARCH as gdbarch_data via the gdbarch
50  post init registration mechanism (gdbarch_data_register_post_init). */
51 
52 static void *
54 {
55  arch_object *arch_obj = PyObject_New (arch_object, &arch_object_type);
56 
57  if (arch_obj == NULL)
58  return NULL;
59 
60  arch_obj->gdbarch = gdbarch;
61 
62  return (void *) arch_obj;
63 }
64 
65 /* Returns the struct gdbarch value corresponding to the given Python
66  architecture object OBJ. */
67 
68 struct gdbarch *
69 arch_object_to_gdbarch (PyObject *obj)
70 {
71  arch_object *py_arch = (arch_object *) obj;
72 
73  return py_arch->gdbarch;
74 }
75 
76 /* Returns the Python architecture object corresponding to GDBARCH.
77  Returns a new reference to the arch_object associated as data with
78  GDBARCH. */
79 
80 PyObject *
82 {
83  PyObject *new_ref = (PyObject *) gdbarch_data (gdbarch, arch_object_data);
84 
85  /* new_ref could be NULL if registration of arch_object with GDBARCH failed
86  in arch_object_data_init. */
87  Py_XINCREF (new_ref);
88 
89  return new_ref;
90 }
91 
92 /* Implementation of gdb.Architecture.name (self) -> String.
93  Returns the name of the architecture as a string value. */
94 
95 static PyObject *
96 archpy_name (PyObject *self, PyObject *args)
97 {
98  struct gdbarch *gdbarch = NULL;
99  const char *name;
100  PyObject *py_name;
101 
103 
104  name = (gdbarch_bfd_arch_info (gdbarch))->printable_name;
105  py_name = PyString_FromString (name);
106 
107  return py_name;
108 }
109 
110 /* Implementation of
111  gdb.Architecture.disassemble (self, start_pc [, end_pc [,count]]) -> List.
112  Returns a list of instructions in a memory address range. Each instruction
113  in the list is a Python dict object.
114 */
115 
116 static PyObject *
117 archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
118 {
119  static const char *keywords[] = { "start_pc", "end_pc", "count", NULL };
120  CORE_ADDR start, end = 0;
121  CORE_ADDR pc;
122  gdb_py_ulongest start_temp;
123  long count = 0, i;
124  PyObject *end_obj = NULL, *count_obj = NULL;
125  struct gdbarch *gdbarch = NULL;
126 
128 
129  if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO",
130  keywords, &start_temp, &end_obj,
131  &count_obj))
132  return NULL;
133 
134  start = start_temp;
135  if (end_obj)
136  {
137  /* Make a long logic check first. In Python 3.x, internally,
138  all integers are represented as longs. In Python 2.x, there
139  is still a differentiation internally between a PyInt and a
140  PyLong. Explicitly do this long check conversion first. In
141  GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
142  to be done first to ensure we do not lose information in the
143  conversion process. */
144  if (PyLong_Check (end_obj))
145  end = PyLong_AsUnsignedLongLong (end_obj);
146 #if PY_MAJOR_VERSION == 2
147  else if (PyInt_Check (end_obj))
148  /* If the end_pc value is specified without a trailing 'L', end_obj will
149  be an integer and not a long integer. */
150  end = PyInt_AsLong (end_obj);
151 #endif
152  else
153  {
154  PyErr_SetString (PyExc_TypeError,
155  _("Argument 'end_pc' should be a (long) integer."));
156 
157  return NULL;
158  }
159 
160  if (end < start)
161  {
162  PyErr_SetString (PyExc_ValueError,
163  _("Argument 'end_pc' should be greater than or "
164  "equal to the argument 'start_pc'."));
165 
166  return NULL;
167  }
168  }
169  if (count_obj)
170  {
171  count = PyInt_AsLong (count_obj);
172  if (PyErr_Occurred () || count < 0)
173  {
174  PyErr_SetString (PyExc_TypeError,
175  _("Argument 'count' should be an non-negative "
176  "integer."));
177 
178  return NULL;
179  }
180  }
181 
182  gdbpy_ref<> result_list (PyList_New (0));
183  if (result_list == NULL)
184  return NULL;
185 
186  for (pc = start, i = 0;
187  /* All args are specified. */
188  (end_obj && count_obj && pc <= end && i < count)
189  /* end_pc is specified, but no count. */
190  || (end_obj && count_obj == NULL && pc <= end)
191  /* end_pc is not specified, but a count is. */
192  || (end_obj == NULL && count_obj && i < count)
193  /* Both end_pc and count are not specified. */
194  || (end_obj == NULL && count_obj == NULL && pc == start);)
195  {
196  int insn_len = 0;
197  gdbpy_ref<> insn_dict (PyDict_New ());
198 
199  if (insn_dict == NULL)
200  return NULL;
201  if (PyList_Append (result_list.get (), insn_dict.get ()))
202  return NULL; /* PyList_Append Sets the exception. */
203 
204  string_file stb;
205 
206  TRY
207  {
208  insn_len = gdb_print_insn (gdbarch, pc, &stb, NULL);
209  }
210  CATCH (except, RETURN_MASK_ALL)
211  {
212  gdbpy_convert_exception (except);
213  return NULL;
214  }
215  END_CATCH
216 
217  if (PyDict_SetItemString (insn_dict.get (), "addr",
219  || PyDict_SetItemString (insn_dict.get (), "asm",
220  PyString_FromString (!stb.empty ()
221  ? stb.c_str ()
222  : "<unknown>"))
223  || PyDict_SetItemString (insn_dict.get (), "length",
224  PyInt_FromLong (insn_len)))
225  return NULL;
226 
227  pc += insn_len;
228  i++;
229  }
230 
231  return result_list.release ();
232 }
233 
234 /* Initializes the Architecture class in the gdb module. */
235 
236 int
238 {
240  arch_object_type.tp_new = PyType_GenericNew;
241  if (PyType_Ready (&arch_object_type) < 0)
242  return -1;
243 
244  return gdb_pymodule_addobject (gdb_module, "Architecture",
245  (PyObject *) &arch_object_type);
246 }
247 
248 static PyMethodDef arch_object_methods [] = {
249  { "name", archpy_name, METH_NOARGS,
250  "name () -> String.\n\
251 Return the name of the architecture as a string value." },
252  { "disassemble", (PyCFunction) archpy_disassemble,
253  METH_VARARGS | METH_KEYWORDS,
254  "disassemble (start_pc [, end_pc [, count]]) -> List.\n\
255 Return a list of at most COUNT disassembled instructions from START_PC to\n\
256 END_PC." },
257  {NULL} /* Sentinel */
258 };
259 
260 PyTypeObject arch_object_type = {
261  PyVarObject_HEAD_INIT (NULL, 0)
262  "gdb.Architecture", /* tp_name */
263  sizeof (arch_object), /* tp_basicsize */
264  0, /* tp_itemsize */
265  0, /* tp_dealloc */
266  0, /* tp_print */
267  0, /* tp_getattr */
268  0, /* tp_setattr */
269  0, /* tp_compare */
270  0, /* tp_repr */
271  0, /* tp_as_number */
272  0, /* tp_as_sequence */
273  0, /* tp_as_mapping */
274  0, /* tp_hash */
275  0, /* tp_call */
276  0, /* tp_str */
277  0, /* tp_getattro */
278  0, /* tp_setattro */
279  0, /* tp_as_buffer */
280  Py_TPFLAGS_DEFAULT, /* tp_flags */
281  "GDB architecture object", /* tp_doc */
282  0, /* tp_traverse */
283  0, /* tp_clear */
284  0, /* tp_richcompare */
285  0, /* tp_weaklistoffset */
286  0, /* tp_iter */
287  0, /* tp_iternext */
288  arch_object_methods, /* tp_methods */
289  0, /* tp_members */
290  0, /* tp_getset */
291  0, /* tp_base */
292  0, /* tp_dict */
293  0, /* tp_descr_get */
294  0, /* tp_descr_set */
295  0, /* tp_dictoffset */
296  0, /* tp_init */
297  0, /* tp_alloc */
298 };
int gdb_pymodule_addobject(PyObject *module, const char *name, PyObject *object)
Definition: py-utils.c:376
PyObject_HEAD struct gdbarch * gdbarch
Definition: py-arch.c:29
bfd_vma CORE_ADDR
Definition: common-types.h:41
unsigned long gdb_py_ulongest
bool empty() const
Definition: ui-file.h:139
static PyObject * archpy_disassemble(PyObject *self, PyObject *args, PyObject *kw)
Definition: py-arch.c:117
int gdbpy_initialize_arch(void)
Definition: py-arch.c:237
#define gdb_py_long_from_ulongest
static void * arch_object_data_init(struct gdbarch *gdbarch)
Definition: py-arch.c:53
struct gdbarch * arch_object_to_gdbarch(PyObject *obj)
Definition: py-arch.c:69
#define _(String)
Definition: gdb_locale.h:35
static int gdb_PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, const char **keywords,...)
PyTypeObject arch_object_type
Definition: py-arch.c:260
#define PyVarObject_HEAD_INIT(type, size)
#define END_CATCH
PyTypeObject arch_object_type CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF("arch_object")
PyObject * gdbarch_to_arch_object(struct gdbarch *gdbarch)
Definition: py-arch.c:81
#define TRY
const char *const name
Definition: aarch64-tdep.c:76
#define CATCH(EXCEPTION, MASK)
int gdb_print_insn(struct gdbarch *gdbarch, CORE_ADDR memaddr, struct ui_file *stream, int *branch_delay_insns)
Definition: disasm.c:813
#define GDB_PY_LLU_ARG
#define ARCHPY_REQUIRE_VALID(arch_obj, arch)
Definition: py-arch.c:35
struct arch_object_type_object arch_object
static PyObject * archpy_name(PyObject *self, PyObject *args)
Definition: py-arch.c:96
void gdbpy_convert_exception(struct gdb_exception exception)
Definition: py-utils.c:235
const char * c_str() const
Definition: ui-file.h:137
static struct gdbarch_data * arch_object_data
Definition: py-arch.c:32
T * get() const
Definition: gdb_ref_ptr.h:130
const struct bfd_arch_info * gdbarch_bfd_arch_info(struct gdbarch *gdbarch)
Definition: gdbarch.c:1500
PyObject * gdb_module
Definition: python.c:116
static PyMethodDef arch_object_methods[]
Definition: py-arch.c:248
struct gdbarch_data * gdbarch_data_register_post_init(gdbarch_data_post_init_ftype *post_init)
Definition: gdbarch.c:5136