GDB (xrefs)
py-arch.c
Go to the documentation of this file.
1 /* Python interface to architecture
2 
3  Copyright (C) 2013-2015 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 
26 typedef struct arch_object_type_object {
27  PyObject_HEAD
28  struct gdbarch *gdbarch;
29 } arch_object;
30 
31 static struct gdbarch_data *arch_object_data = NULL;
32 
33 /* Require a valid Architecture. */
34 #define ARCHPY_REQUIRE_VALID(arch_obj, arch) \
35  do { \
36  arch = arch_object_to_gdbarch (arch_obj); \
37  if (arch == NULL) \
38  { \
39  PyErr_SetString (PyExc_RuntimeError, \
40  _("Architecture is invalid.")); \
41  return NULL; \
42  } \
43  } while (0)
44 
45 extern PyTypeObject arch_object_type
46  CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("arch_object");
47 
48 /* Associates an arch_object with GDBARCH as gdbarch_data via the gdbarch
49  post init registration mechanism (gdbarch_data_register_post_init). */
50 
51 static void *
53 {
54  arch_object *arch_obj = PyObject_New (arch_object, &arch_object_type);
55 
56  if (arch_obj == NULL)
57  return NULL;
58 
59  arch_obj->gdbarch = gdbarch;
60 
61  return (void *) arch_obj;
62 }
63 
64 /* Returns the struct gdbarch value corresponding to the given Python
65  architecture object OBJ. */
66 
67 struct gdbarch *
68 arch_object_to_gdbarch (PyObject *obj)
69 {
70  arch_object *py_arch = (arch_object *) obj;
71 
72  return py_arch->gdbarch;
73 }
74 
75 /* Returns the Python architecture object corresponding to GDBARCH.
76  Returns a new reference to the arch_object associated as data with
77  GDBARCH. */
78 
79 PyObject *
81 {
82  PyObject *new_ref = (PyObject *) gdbarch_data (gdbarch, arch_object_data);
83 
84  /* new_ref could be NULL if registration of arch_object with GDBARCH failed
85  in arch_object_data_init. */
86  Py_XINCREF (new_ref);
87 
88  return new_ref;
89 }
90 
91 /* Implementation of gdb.Architecture.name (self) -> String.
92  Returns the name of the architecture as a string value. */
93 
94 static PyObject *
95 archpy_name (PyObject *self, PyObject *args)
96 {
97  struct gdbarch *gdbarch = NULL;
98  const char *name;
99  PyObject *py_name;
100 
101  ARCHPY_REQUIRE_VALID (self, gdbarch);
102 
103  name = (gdbarch_bfd_arch_info (gdbarch))->printable_name;
104  py_name = PyString_FromString (name);
105 
106  return py_name;
107 }
108 
109 /* Implementation of
110  gdb.Architecture.disassemble (self, start_pc [, end_pc [,count]]) -> List.
111  Returns a list of instructions in a memory address range. Each instruction
112  in the list is a Python dict object.
113 */
114 
115 static PyObject *
116 archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
117 {
118  static char *keywords[] = { "start_pc", "end_pc", "count", NULL };
119  CORE_ADDR start, end = 0;
120  CORE_ADDR pc;
121  gdb_py_ulongest start_temp;
122  long count = 0, i;
123  PyObject *result_list, *end_obj = NULL, *count_obj = NULL;
124  struct gdbarch *gdbarch = NULL;
125 
126  ARCHPY_REQUIRE_VALID (self, gdbarch);
127 
128  if (!PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO", keywords,
129  &start_temp, &end_obj, &count_obj))
130  return NULL;
131 
132  start = start_temp;
133  if (end_obj)
134  {
135  /* Make a long logic check first. In Python 3.x, internally,
136  all integers are represented as longs. In Python 2.x, there
137  is still a differentiation internally between a PyInt and a
138  PyLong. Explicitly do this long check conversion first. In
139  GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
140  to be done first to ensure we do not lose information in the
141  conversion process. */
142  if (PyLong_Check (end_obj))
143  end = PyLong_AsUnsignedLongLong (end_obj);
144  else if (PyInt_Check (end_obj))
145  /* If the end_pc value is specified without a trailing 'L', end_obj will
146  be an integer and not a long integer. */
147  end = PyInt_AsLong (end_obj);
148  else
149  {
150  Py_DECREF (end_obj);
151  Py_XDECREF (count_obj);
152  PyErr_SetString (PyExc_TypeError,
153  _("Argument 'end_pc' should be a (long) integer."));
154 
155  return NULL;
156  }
157 
158  if (end < start)
159  {
160  Py_DECREF (end_obj);
161  Py_XDECREF (count_obj);
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  Py_DECREF (count_obj);
175  Py_XDECREF (end_obj);
176  PyErr_SetString (PyExc_TypeError,
177  _("Argument 'count' should be an non-negative "
178  "integer."));
179 
180  return NULL;
181  }
182  }
183 
184  result_list = PyList_New (0);
185  if (result_list == NULL)
186  return NULL;
187 
188  for (pc = start, i = 0;
189  /* All args are specified. */
190  (end_obj && count_obj && pc <= end && i < count)
191  /* end_pc is specified, but no count. */
192  || (end_obj && count_obj == NULL && pc <= end)
193  /* end_pc is not specified, but a count is. */
194  || (end_obj == NULL && count_obj && i < count)
195  /* Both end_pc and count are not specified. */
196  || (end_obj == NULL && count_obj == NULL && pc == start);)
197  {
198  int insn_len = 0;
199  char *as = NULL;
200  struct ui_file *memfile = mem_fileopen ();
201  PyObject *insn_dict = PyDict_New ();
202 
203  if (insn_dict == NULL)
204  {
205  Py_DECREF (result_list);
206  ui_file_delete (memfile);
207 
208  return NULL;
209  }
210  if (PyList_Append (result_list, insn_dict))
211  {
212  Py_DECREF (result_list);
213  Py_DECREF (insn_dict);
214  ui_file_delete (memfile);
215 
216  return NULL; /* PyList_Append Sets the exception. */
217  }
218 
219  TRY
220  {
221  insn_len = gdb_print_insn (gdbarch, pc, memfile, NULL);
222  }
223  CATCH (except, RETURN_MASK_ALL)
224  {
225  Py_DECREF (result_list);
226  ui_file_delete (memfile);
227 
228  gdbpy_convert_exception (except);
229  return NULL;
230  }
231  END_CATCH
232 
233  as = ui_file_xstrdup (memfile, NULL);
234  if (PyDict_SetItemString (insn_dict, "addr",
236  || PyDict_SetItemString (insn_dict, "asm",
237  PyString_FromString (*as ? as : "<unknown>"))
238  || PyDict_SetItemString (insn_dict, "length",
239  PyInt_FromLong (insn_len)))
240  {
241  Py_DECREF (result_list);
242 
243  ui_file_delete (memfile);
244  xfree (as);
245 
246  return NULL;
247  }
248 
249  pc += insn_len;
250  i++;
251  ui_file_delete (memfile);
252  xfree (as);
253  }
254 
255  return result_list;
256 }
257 
258 /* Initializes the Architecture class in the gdb module. */
259 
260 int
262 {
264  arch_object_type.tp_new = PyType_GenericNew;
265  if (PyType_Ready (&arch_object_type) < 0)
266  return -1;
267 
268  return gdb_pymodule_addobject (gdb_module, "Architecture",
269  (PyObject *) &arch_object_type);
270 }
271 
272 static PyMethodDef arch_object_methods [] = {
273  { "name", archpy_name, METH_NOARGS,
274  "name () -> String.\n\
275 Return the name of the architecture as a string value." },
276  { "disassemble", (PyCFunction) archpy_disassemble,
277  METH_VARARGS | METH_KEYWORDS,
278  "disassemble (start_pc [, end_pc [, count]]) -> List.\n\
279 Return a list of at most COUNT disassembled instructions from START_PC to\n\
280 END_PC." },
281  {NULL} /* Sentinel */
282 };
283 
284 PyTypeObject arch_object_type = {
285  PyVarObject_HEAD_INIT (NULL, 0)
286  "gdb.Architecture", /* tp_name */
287  sizeof (arch_object), /* tp_basicsize */
288  0, /* tp_itemsize */
289  0, /* tp_dealloc */
290  0, /* tp_print */
291  0, /* tp_getattr */
292  0, /* tp_setattr */
293  0, /* tp_compare */
294  0, /* tp_repr */
295  0, /* tp_as_number */
296  0, /* tp_as_sequence */
297  0, /* tp_as_mapping */
298  0, /* tp_hash */
299  0, /* tp_call */
300  0, /* tp_str */
301  0, /* tp_getattro */
302  0, /* tp_setattro */
303  0, /* tp_as_buffer */
304  Py_TPFLAGS_DEFAULT, /* tp_flags */
305  "GDB architecture object", /* tp_doc */
306  0, /* tp_traverse */
307  0, /* tp_clear */
308  0, /* tp_richcompare */
309  0, /* tp_weaklistoffset */
310  0, /* tp_iter */
311  0, /* tp_iternext */
312  arch_object_methods, /* tp_methods */
313  0, /* tp_members */
314  0, /* tp_getset */
315  0, /* tp_base */
316  0, /* tp_dict */
317  0, /* tp_descr_get */
318  0, /* tp_descr_set */
319  0, /* tp_dictoffset */
320  0, /* tp_init */
321  0, /* tp_alloc */
322 };
int gdb_pymodule_addobject(PyObject *module, const char *name, PyObject *object)
Definition: py-utils.c:437
#define Py_DECREF(op)
PyObject_HEAD struct gdbarch * gdbarch
Definition: py-arch.c:28
bfd_vma CORE_ADDR
Definition: common-types.h:41
void xfree(void *)
Definition: common-utils.c:97
char * ui_file_xstrdup(struct ui_file *file, long *length)
Definition: ui-file.c:345
unsigned long gdb_py_ulongest
void ui_file_delete(struct ui_file *file)
Definition: ui-file.c:76
static PyObject * archpy_disassemble(PyObject *self, PyObject *args, PyObject *kw)
Definition: py-arch.c:116
int gdbpy_initialize_arch(void)
Definition: py-arch.c:261
#define gdb_py_long_from_ulongest
static void * arch_object_data_init(struct gdbarch *gdbarch)
Definition: py-arch.c:52
struct gdbarch * arch_object_to_gdbarch(PyObject *obj)
Definition: py-arch.c:68
#define _(String)
Definition: gdb_locale.h:40
#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:80
#define TRY
const char *const name
Definition: aarch64-tdep.c:68
#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:444
#define GDB_PY_LLU_ARG
#define ARCHPY_REQUIRE_VALID(arch_obj, arch)
Definition: py-arch.c:34
struct arch_object_type_object arch_object
static PyObject * archpy_name(PyObject *self, PyObject *args)
Definition: py-arch.c:95
struct ui_file * mem_fileopen(void)
Definition: ui-file.c:427
void gdbpy_convert_exception(struct gdb_exception exception)
Definition: py-utils.c:295
const struct bfd_arch_info * gdbarch_bfd_arch_info(struct gdbarch *gdbarch)
Definition: gdbarch.c:1411
PyObject * gdb_module
Definition: python.c:112
static PyMethodDef arch_object_methods[]
Definition: py-arch.c:272
struct gdbarch_data * gdbarch_data_register_post_init(gdbarch_data_post_init_ftype *post_init)
Definition: gdbarch.c:4812
#define PyVarObject_HEAD_INIT(type, size)