py-arch.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* Python interface to architecture
  2. Copyright (C) 2013-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "gdbarch.h"
  16. #include "arch-utils.h"
  17. #include "disasm.h"
  18. #include "python-internal.h"
  19. struct arch_object {
  20. PyObject_HEAD
  21. struct gdbarch *gdbarch;
  22. };
  23. static struct gdbarch_data *arch_object_data = NULL;
  24. /* Require a valid Architecture. */
  25. #define ARCHPY_REQUIRE_VALID(arch_obj, arch) \
  26. do { \
  27. arch = arch_object_to_gdbarch (arch_obj); \
  28. if (arch == NULL) \
  29. { \
  30. PyErr_SetString (PyExc_RuntimeError, \
  31. _("Architecture is invalid.")); \
  32. return NULL; \
  33. } \
  34. } while (0)
  35. extern PyTypeObject arch_object_type
  36. CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("arch_object");
  37. /* Associates an arch_object with GDBARCH as gdbarch_data via the gdbarch
  38. post init registration mechanism (gdbarch_data_register_post_init). */
  39. static void *
  40. arch_object_data_init (struct gdbarch *gdbarch)
  41. {
  42. arch_object *arch_obj = PyObject_New (arch_object, &arch_object_type);
  43. if (arch_obj == NULL)
  44. return NULL;
  45. arch_obj->gdbarch = gdbarch;
  46. return (void *) arch_obj;
  47. }
  48. /* Returns the struct gdbarch value corresponding to the given Python
  49. architecture object OBJ, which must be a gdb.Architecture object. */
  50. struct gdbarch *
  51. arch_object_to_gdbarch (PyObject *obj)
  52. {
  53. gdb_assert (gdbpy_is_architecture (obj));
  54. arch_object *py_arch = (arch_object *) obj;
  55. return py_arch->gdbarch;
  56. }
  57. /* See python-internal.h. */
  58. bool
  59. gdbpy_is_architecture (PyObject *obj)
  60. {
  61. return PyObject_TypeCheck (obj, &arch_object_type);
  62. }
  63. /* Returns the Python architecture object corresponding to GDBARCH.
  64. Returns a new reference to the arch_object associated as data with
  65. GDBARCH. */
  66. PyObject *
  67. gdbarch_to_arch_object (struct gdbarch *gdbarch)
  68. {
  69. PyObject *new_ref = (PyObject *) gdbarch_data (gdbarch, arch_object_data);
  70. /* new_ref could be NULL if registration of arch_object with GDBARCH failed
  71. in arch_object_data_init. */
  72. Py_XINCREF (new_ref);
  73. return new_ref;
  74. }
  75. /* Implementation of gdb.Architecture.name (self) -> String.
  76. Returns the name of the architecture as a string value. */
  77. static PyObject *
  78. archpy_name (PyObject *self, PyObject *args)
  79. {
  80. struct gdbarch *gdbarch = NULL;
  81. const char *name;
  82. ARCHPY_REQUIRE_VALID (self, gdbarch);
  83. name = (gdbarch_bfd_arch_info (gdbarch))->printable_name;
  84. return PyUnicode_FromString (name);
  85. }
  86. /* Implementation of
  87. gdb.Architecture.disassemble (self, start_pc [, end_pc [,count]]) -> List.
  88. Returns a list of instructions in a memory address range. Each instruction
  89. in the list is a Python dict object.
  90. */
  91. static PyObject *
  92. archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
  93. {
  94. static const char *keywords[] = { "start_pc", "end_pc", "count", NULL };
  95. CORE_ADDR start, end = 0;
  96. CORE_ADDR pc;
  97. gdb_py_ulongest start_temp;
  98. long count = 0, i;
  99. PyObject *end_obj = NULL, *count_obj = NULL;
  100. struct gdbarch *gdbarch = NULL;
  101. ARCHPY_REQUIRE_VALID (self, gdbarch);
  102. if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO",
  103. keywords, &start_temp, &end_obj,
  104. &count_obj))
  105. return NULL;
  106. start = start_temp;
  107. if (end_obj)
  108. {
  109. /* Make a long logic check first. In Python 3.x, internally,
  110. all integers are represented as longs. In Python 2.x, there
  111. is still a differentiation internally between a PyInt and a
  112. PyLong. Explicitly do this long check conversion first. In
  113. GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
  114. to be done first to ensure we do not lose information in the
  115. conversion process. */
  116. if (PyLong_Check (end_obj))
  117. end = PyLong_AsUnsignedLongLong (end_obj);
  118. else
  119. {
  120. PyErr_SetString (PyExc_TypeError,
  121. _("Argument 'end_pc' should be a (long) integer."));
  122. return NULL;
  123. }
  124. if (end < start)
  125. {
  126. PyErr_SetString (PyExc_ValueError,
  127. _("Argument 'end_pc' should be greater than or "
  128. "equal to the argument 'start_pc'."));
  129. return NULL;
  130. }
  131. }
  132. if (count_obj)
  133. {
  134. count = PyLong_AsLong (count_obj);
  135. if (PyErr_Occurred () || count < 0)
  136. {
  137. PyErr_SetString (PyExc_TypeError,
  138. _("Argument 'count' should be an non-negative "
  139. "integer."));
  140. return NULL;
  141. }
  142. }
  143. gdbpy_ref<> result_list (PyList_New (0));
  144. if (result_list == NULL)
  145. return NULL;
  146. for (pc = start, i = 0;
  147. /* All args are specified. */
  148. (end_obj && count_obj && pc <= end && i < count)
  149. /* end_pc is specified, but no count. */
  150. || (end_obj && count_obj == NULL && pc <= end)
  151. /* end_pc is not specified, but a count is. */
  152. || (end_obj == NULL && count_obj && i < count)
  153. /* Both end_pc and count are not specified. */
  154. || (end_obj == NULL && count_obj == NULL && pc == start);)
  155. {
  156. int insn_len = 0;
  157. gdbpy_ref<> insn_dict (PyDict_New ());
  158. if (insn_dict == NULL)
  159. return NULL;
  160. if (PyList_Append (result_list.get (), insn_dict.get ()))
  161. return NULL; /* PyList_Append Sets the exception. */
  162. string_file stb;
  163. try
  164. {
  165. insn_len = gdb_print_insn (gdbarch, pc, &stb, NULL);
  166. }
  167. catch (const gdb_exception &except)
  168. {
  169. gdbpy_convert_exception (except);
  170. return NULL;
  171. }
  172. gdbpy_ref<> pc_obj = gdb_py_object_from_ulongest (pc);
  173. if (pc_obj == nullptr)
  174. return nullptr;
  175. gdbpy_ref<> asm_obj
  176. (PyUnicode_FromString (!stb.empty () ? stb.c_str () : "<unknown>"));
  177. if (asm_obj == nullptr)
  178. return nullptr;
  179. gdbpy_ref<> len_obj = gdb_py_object_from_longest (insn_len);
  180. if (len_obj == nullptr)
  181. return nullptr;
  182. if (PyDict_SetItemString (insn_dict.get (), "addr", pc_obj.get ())
  183. || PyDict_SetItemString (insn_dict.get (), "asm", asm_obj.get ())
  184. || PyDict_SetItemString (insn_dict.get (), "length", len_obj.get ()))
  185. return NULL;
  186. pc += insn_len;
  187. i++;
  188. }
  189. return result_list.release ();
  190. }
  191. /* Implementation of gdb.Architecture.registers (self, reggroup) -> Iterator.
  192. Returns an iterator over register descriptors for registers in GROUP
  193. within the architecture SELF. */
  194. static PyObject *
  195. archpy_registers (PyObject *self, PyObject *args, PyObject *kw)
  196. {
  197. static const char *keywords[] = { "reggroup", NULL };
  198. struct gdbarch *gdbarch = NULL;
  199. const char *group_name = NULL;
  200. /* Parse method arguments. */
  201. if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "|s", keywords,
  202. &group_name))
  203. return NULL;
  204. /* Extract the gdbarch from the self object. */
  205. ARCHPY_REQUIRE_VALID (self, gdbarch);
  206. return gdbpy_new_register_descriptor_iterator (gdbarch, group_name);
  207. }
  208. /* Implementation of gdb.Architecture.register_groups (self) -> Iterator.
  209. Returns an iterator that will give up all valid register groups in the
  210. architecture SELF. */
  211. static PyObject *
  212. archpy_register_groups (PyObject *self, PyObject *args)
  213. {
  214. struct gdbarch *gdbarch = NULL;
  215. /* Extract the gdbarch from the self object. */
  216. ARCHPY_REQUIRE_VALID (self, gdbarch);
  217. return gdbpy_new_reggroup_iterator (gdbarch);
  218. }
  219. /* Implementation of gdb.integer_type. */
  220. static PyObject *
  221. archpy_integer_type (PyObject *self, PyObject *args, PyObject *kw)
  222. {
  223. static const char *keywords[] = { "size", "signed", NULL };
  224. int size;
  225. PyObject *is_signed_obj = nullptr;
  226. if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "i|O", keywords,
  227. &size, &is_signed_obj))
  228. return nullptr;
  229. /* Assume signed by default. */
  230. bool is_signed = (is_signed_obj == nullptr
  231. || PyObject_IsTrue (is_signed_obj));
  232. struct gdbarch *gdbarch;
  233. ARCHPY_REQUIRE_VALID (self, gdbarch);
  234. const struct builtin_type *builtins = builtin_type (gdbarch);
  235. struct type *type = nullptr;
  236. switch (size)
  237. {
  238. case 0:
  239. type = builtins->builtin_int0;
  240. break;
  241. case 8:
  242. type = is_signed ? builtins->builtin_int8 : builtins->builtin_uint8;
  243. break;
  244. case 16:
  245. type = is_signed ? builtins->builtin_int16 : builtins->builtin_uint16;
  246. break;
  247. case 24:
  248. type = is_signed ? builtins->builtin_int24 : builtins->builtin_uint24;
  249. break;
  250. case 32:
  251. type = is_signed ? builtins->builtin_int32 : builtins->builtin_uint32;
  252. break;
  253. case 64:
  254. type = is_signed ? builtins->builtin_int64 : builtins->builtin_uint64;
  255. break;
  256. case 128:
  257. type = is_signed ? builtins->builtin_int128 : builtins->builtin_uint128;
  258. break;
  259. default:
  260. PyErr_SetString (PyExc_ValueError,
  261. _("no integer type of that size is available"));
  262. return nullptr;
  263. }
  264. return type_to_type_object (type);
  265. }
  266. /* Implementation of gdb.architecture_names(). Return a list of all the
  267. BFD architecture names that GDB understands. */
  268. PyObject *
  269. gdbpy_all_architecture_names (PyObject *self, PyObject *args)
  270. {
  271. gdbpy_ref<> list (PyList_New (0));
  272. if (list == nullptr)
  273. return nullptr;
  274. std::vector<const char *> name_list = gdbarch_printable_names ();
  275. for (const char *name : name_list)
  276. {
  277. gdbpy_ref <> py_name (PyUnicode_FromString (name));
  278. if (py_name == nullptr)
  279. return nullptr;
  280. if (PyList_Append (list.get (), py_name.get ()) < 0)
  281. return nullptr;
  282. }
  283. return list.release ();
  284. }
  285. void _initialize_py_arch ();
  286. void
  287. _initialize_py_arch ()
  288. {
  289. arch_object_data = gdbarch_data_register_post_init (arch_object_data_init);
  290. }
  291. /* Initializes the Architecture class in the gdb module. */
  292. int
  293. gdbpy_initialize_arch (void)
  294. {
  295. arch_object_type.tp_new = PyType_GenericNew;
  296. if (PyType_Ready (&arch_object_type) < 0)
  297. return -1;
  298. return gdb_pymodule_addobject (gdb_module, "Architecture",
  299. (PyObject *) &arch_object_type);
  300. }
  301. static PyMethodDef arch_object_methods [] = {
  302. { "name", archpy_name, METH_NOARGS,
  303. "name () -> String.\n\
  304. Return the name of the architecture as a string value." },
  305. { "disassemble", (PyCFunction) archpy_disassemble,
  306. METH_VARARGS | METH_KEYWORDS,
  307. "disassemble (start_pc [, end_pc [, count]]) -> List.\n\
  308. Return a list of at most COUNT disassembled instructions from START_PC to\n\
  309. END_PC." },
  310. { "integer_type", (PyCFunction) archpy_integer_type,
  311. METH_VARARGS | METH_KEYWORDS,
  312. "integer_type (size [, signed]) -> type\n\
  313. Return an integer Type corresponding to the given bitsize and signed-ness.\n\
  314. If not specified, the type defaults to signed." },
  315. { "registers", (PyCFunction) archpy_registers,
  316. METH_VARARGS | METH_KEYWORDS,
  317. "registers ([ group-name ]) -> Iterator.\n\
  318. Return an iterator of register descriptors for the registers in register\n\
  319. group GROUP-NAME." },
  320. { "register_groups", archpy_register_groups,
  321. METH_NOARGS,
  322. "register_groups () -> Iterator.\n\
  323. Return an iterator over all of the register groups in this architecture." },
  324. {NULL} /* Sentinel */
  325. };
  326. PyTypeObject arch_object_type = {
  327. PyVarObject_HEAD_INIT (NULL, 0)
  328. "gdb.Architecture", /* tp_name */
  329. sizeof (arch_object), /* tp_basicsize */
  330. 0, /* tp_itemsize */
  331. 0, /* tp_dealloc */
  332. 0, /* tp_print */
  333. 0, /* tp_getattr */
  334. 0, /* tp_setattr */
  335. 0, /* tp_compare */
  336. 0, /* tp_repr */
  337. 0, /* tp_as_number */
  338. 0, /* tp_as_sequence */
  339. 0, /* tp_as_mapping */
  340. 0, /* tp_hash */
  341. 0, /* tp_call */
  342. 0, /* tp_str */
  343. 0, /* tp_getattro */
  344. 0, /* tp_setattro */
  345. 0, /* tp_as_buffer */
  346. Py_TPFLAGS_DEFAULT, /* tp_flags */
  347. "GDB architecture object", /* tp_doc */
  348. 0, /* tp_traverse */
  349. 0, /* tp_clear */
  350. 0, /* tp_richcompare */
  351. 0, /* tp_weaklistoffset */
  352. 0, /* tp_iter */
  353. 0, /* tp_iternext */
  354. arch_object_methods, /* tp_methods */
  355. 0, /* tp_members */
  356. 0, /* tp_getset */
  357. 0, /* tp_base */
  358. 0, /* tp_dict */
  359. 0, /* tp_descr_get */
  360. 0, /* tp_descr_set */
  361. 0, /* tp_dictoffset */
  362. 0, /* tp_init */
  363. 0, /* tp_alloc */
  364. };