Add a new class gdb.Architecture which exposes GDB's
[platform/upstream/binutils.git] / gdb / python / py-arch.c
1 /* Python interface to architecture
2
3    Copyright (C) 2013 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 "python-internal.h"
24
25 typedef struct arch_object_type_object {
26   PyObject_HEAD
27   struct gdbarch *gdbarch;
28 } arch_object;
29
30 static struct gdbarch_data *arch_object_data = NULL;
31 static PyTypeObject arch_object_type;
32
33 /* Associates an arch_object with GDBARCH as gdbarch_data via the gdbarch
34    post init registration mechanism (gdbarch_data_register_post_init).  */
35
36 static void *
37 arch_object_data_init (struct gdbarch *gdbarch)
38 {
39   arch_object *arch_obj = PyObject_New (arch_object, &arch_object_type);
40
41   if (arch_obj == NULL)
42     return NULL;
43
44   arch_obj->gdbarch = gdbarch;
45
46   return (void *) arch_obj;
47 }
48
49 /* Returns the struct gdbarch value corresponding to the given Python
50    architecture object OBJ.  */
51
52 struct gdbarch *
53 arch_object_to_gdbarch (PyObject *obj)
54 {
55   arch_object *py_arch = (arch_object *) obj;
56
57   return py_arch->gdbarch;
58 }
59
60 /* Returns the Python architecture object corresponding to GDBARCH.
61    Returns a new reference to the arch_object associated as data with
62    GDBARCH.  */
63
64 PyObject *
65 gdbarch_to_arch_object (struct gdbarch *gdbarch)
66 {
67   PyObject *new_ref = (PyObject *) gdbarch_data (gdbarch, arch_object_data);
68
69   /* new_ref could be NULL if registration of arch_object with GDBARCH failed
70      in arch_object_data_init.  */
71   Py_XINCREF (new_ref);
72
73   return new_ref;
74 }
75
76 /* Implementation of gdb.Architecture.name (self) -> String.
77    Returns the name of the architecture as a string value.  */
78
79 static PyObject *
80 archpy_name (PyObject *self, PyObject *args)
81 {
82   struct gdbarch *gdbarch = arch_object_to_gdbarch (self);
83   const char *name = (gdbarch_bfd_arch_info (gdbarch))->printable_name;
84   PyObject *py_name = PyString_FromString (name);
85
86   return py_name;
87 }
88
89 /* Initializes the Architecture class in the gdb module.  */
90
91 void
92 gdbpy_initialize_arch (void)
93 {
94   arch_object_data = gdbarch_data_register_post_init (arch_object_data_init);
95   arch_object_type.tp_new = PyType_GenericNew;
96   if (PyType_Ready (&arch_object_type) < 0)
97     return;
98
99   Py_INCREF (&arch_object_type);
100   PyModule_AddObject (gdb_module, "Architecture",
101                       (PyObject *) &arch_object_type);
102 }
103
104 static PyMethodDef arch_object_methods [] = {
105   { "name", archpy_name, METH_NOARGS,
106     "name () -> String.\n\
107 Return the name of the architecture as a string value." },
108   {NULL}  /* Sentinel */
109 };
110
111 static PyTypeObject arch_object_type = {
112   PyVarObject_HEAD_INIT (NULL, 0)
113   "gdb.Architecture",                 /* tp_name */
114   sizeof (arch_object),               /* tp_basicsize */
115   0,                                  /* tp_itemsize */
116   0,                                  /* tp_dealloc */
117   0,                                  /* tp_print */
118   0,                                  /* tp_getattr */
119   0,                                  /* tp_setattr */
120   0,                                  /* tp_compare */
121   0,                                  /* tp_repr */
122   0,                                  /* tp_as_number */
123   0,                                  /* tp_as_sequence */
124   0,                                  /* tp_as_mapping */
125   0,                                  /* tp_hash  */
126   0,                                  /* tp_call */
127   0,                                  /* tp_str */
128   0,                                  /* tp_getattro */
129   0,                                  /* tp_setattro */
130   0,                                  /* tp_as_buffer */
131   Py_TPFLAGS_DEFAULT,                 /* tp_flags */
132   "GDB architecture object",          /* tp_doc */
133   0,                                  /* tp_traverse */
134   0,                                  /* tp_clear */
135   0,                                  /* tp_richcompare */
136   0,                                  /* tp_weaklistoffset */
137   0,                                  /* tp_iter */
138   0,                                  /* tp_iternext */
139   arch_object_methods,                /* tp_methods */
140   0,                                  /* tp_members */
141   0,                                  /* tp_getset */
142   0,                                  /* tp_base */
143   0,                                  /* tp_dict */
144   0,                                  /* tp_descr_get */
145   0,                                  /* tp_descr_set */
146   0,                                  /* tp_dictoffset */
147   0,                                  /* tp_init */
148   0,                                  /* tp_alloc */
149 };