python-internal.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /* Gdb/Python header for private use by Python module.
  2. Copyright (C) 2008-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. #ifndef PYTHON_PYTHON_INTERNAL_H
  15. #define PYTHON_PYTHON_INTERNAL_H
  16. #include "extension.h"
  17. #include "extension-priv.h"
  18. /* These WITH_* macros are defined by the CPython API checker that
  19. comes with the Python plugin for GCC. See:
  20. https://gcc-python-plugin.readthedocs.org/en/latest/cpychecker.html
  21. The checker defines a WITH_ macro for each attribute it
  22. exposes. Note that we intentionally do not use
  23. 'cpychecker_returns_borrowed_ref' -- that idiom is forbidden in
  24. gdb. */
  25. #ifdef WITH_CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF_ATTRIBUTE
  26. #define CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF(ARG) \
  27. __attribute__ ((cpychecker_type_object_for_typedef (ARG)))
  28. #else
  29. #define CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF(ARG)
  30. #endif
  31. #ifdef WITH_CPYCHECKER_SETS_EXCEPTION_ATTRIBUTE
  32. #define CPYCHECKER_SETS_EXCEPTION __attribute__ ((cpychecker_sets_exception))
  33. #else
  34. #define CPYCHECKER_SETS_EXCEPTION
  35. #endif
  36. #ifdef WITH_CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION_ATTRIBUTE
  37. #define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION \
  38. __attribute__ ((cpychecker_negative_result_sets_exception))
  39. #else
  40. #define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
  41. #endif
  42. /* /usr/include/features.h on linux systems will define _POSIX_C_SOURCE
  43. if it sees _GNU_SOURCE (which config.h will define).
  44. pyconfig.h defines _POSIX_C_SOURCE to a different value than
  45. /usr/include/features.h does causing compilation to fail.
  46. To work around this, undef _POSIX_C_SOURCE before we include Python.h.
  47. Same problem with _XOPEN_SOURCE. */
  48. #undef _POSIX_C_SOURCE
  49. #undef _XOPEN_SOURCE
  50. /* On sparc-solaris, /usr/include/sys/feature_tests.h defines
  51. _FILE_OFFSET_BITS, which pyconfig.h also defines. Same work
  52. around technique as above. */
  53. #undef _FILE_OFFSET_BITS
  54. /* A kludge to avoid redefinition of snprintf on Windows by pyerrors.h. */
  55. #if defined(_WIN32) && defined(HAVE_DECL_SNPRINTF)
  56. #define HAVE_SNPRINTF 1
  57. #endif
  58. /* Another kludge to avoid compilation errors because MinGW defines
  59. 'hypot' to '_hypot', but the C++ headers says "using ::hypot". */
  60. #ifdef __MINGW32__
  61. # define _hypot hypot
  62. #endif
  63. /* Request clean size types from Python. */
  64. #define PY_SSIZE_T_CLEAN
  65. /* Include the Python header files using angle brackets rather than
  66. double quotes. On case-insensitive filesystems, this prevents us
  67. from including our python/python.h header file. */
  68. #include <Python.h>
  69. #include <frameobject.h>
  70. #include "py-ref.h"
  71. #define Py_TPFLAGS_CHECKTYPES 0
  72. /* If Python.h does not define WITH_THREAD, then the various
  73. GIL-related functions will not be defined. However,
  74. PyGILState_STATE will be. */
  75. #ifndef WITH_THREAD
  76. #define PyGILState_Ensure() ((PyGILState_STATE) 0)
  77. #define PyGILState_Release(ARG) ((void)(ARG))
  78. #define PyEval_InitThreads()
  79. #define PyThreadState_Swap(ARG) ((void)(ARG))
  80. #define PyEval_ReleaseLock()
  81. #endif
  82. /* Python supplies HAVE_LONG_LONG and some `long long' support when it
  83. is available. These defines let us handle the differences more
  84. cleanly. */
  85. #ifdef HAVE_LONG_LONG
  86. #define GDB_PY_LL_ARG "L"
  87. #define GDB_PY_LLU_ARG "K"
  88. typedef PY_LONG_LONG gdb_py_longest;
  89. typedef unsigned PY_LONG_LONG gdb_py_ulongest;
  90. #define gdb_py_long_as_ulongest PyLong_AsUnsignedLongLong
  91. #else /* HAVE_LONG_LONG */
  92. #define GDB_PY_LL_ARG "L"
  93. #define GDB_PY_LLU_ARG "K"
  94. typedef long gdb_py_longest;
  95. typedef unsigned long gdb_py_ulongest;
  96. #define gdb_py_long_as_ulongest PyLong_AsUnsignedLong
  97. #endif /* HAVE_LONG_LONG */
  98. #if PY_VERSION_HEX < 0x03020000
  99. typedef long Py_hash_t;
  100. #endif
  101. /* PyMem_RawMalloc appeared in Python 3.4. For earlier versions, we can just
  102. fall back to PyMem_Malloc. */
  103. #if PY_VERSION_HEX < 0x03040000
  104. #define PyMem_RawMalloc PyMem_Malloc
  105. #endif
  106. /* PyObject_CallMethod's 'method' and 'format' parameters were missing
  107. the 'const' qualifier before Python 3.4. Hence, we wrap the
  108. function in our own version to avoid errors with string literals.
  109. Note, this is a variadic template because PyObject_CallMethod is a
  110. varargs function and Python doesn't have a "PyObject_VaCallMethod"
  111. variant taking a va_list that we could defer to instead. */
  112. template<typename... Args>
  113. static inline PyObject *
  114. gdb_PyObject_CallMethod (PyObject *o, const char *method, const char *format,
  115. Args... args) /* ARI: editCase function */
  116. {
  117. return PyObject_CallMethod (o,
  118. const_cast<char *> (method),
  119. const_cast<char *> (format),
  120. args...);
  121. }
  122. #undef PyObject_CallMethod
  123. #define PyObject_CallMethod gdb_PyObject_CallMethod
  124. /* The 'name' parameter of PyErr_NewException was missing the 'const'
  125. qualifier in Python <= 3.4. Hence, we wrap it in a function to
  126. avoid errors when compiled with -Werror. */
  127. static inline PyObject*
  128. gdb_PyErr_NewException (const char *name, PyObject *base, PyObject *dict)
  129. {
  130. return PyErr_NewException (const_cast<char *> (name), base, dict);
  131. }
  132. #define PyErr_NewException gdb_PyErr_NewException
  133. /* PySys_GetObject's 'name' parameter was missing the 'const'
  134. qualifier before Python 3.4. Hence, we wrap it in a function to
  135. avoid errors when compiled with -Werror. */
  136. static inline PyObject *
  137. gdb_PySys_GetObject (const char *name)
  138. {
  139. return PySys_GetObject (const_cast<char *> (name));
  140. }
  141. #define PySys_GetObject gdb_PySys_GetObject
  142. /* PySys_SetPath's 'path' parameter was missing the 'const' qualifier
  143. before Python 3.6. Hence, we wrap it in a function to avoid errors
  144. when compiled with -Werror. */
  145. # define GDB_PYSYS_SETPATH_CHAR wchar_t
  146. static inline void
  147. gdb_PySys_SetPath (const GDB_PYSYS_SETPATH_CHAR *path)
  148. {
  149. PySys_SetPath (const_cast<GDB_PYSYS_SETPATH_CHAR *> (path));
  150. }
  151. #define PySys_SetPath gdb_PySys_SetPath
  152. /* Wrap PyGetSetDef to allow convenient construction with string
  153. literals. Unfortunately, PyGetSetDef's 'name' and 'doc' members
  154. are 'char *' instead of 'const char *', meaning that in order to
  155. list-initialize PyGetSetDef arrays with string literals (and
  156. without the wrapping below) would require writing explicit 'char *'
  157. casts. Instead, we extend PyGetSetDef and add constexpr
  158. constructors that accept const 'name' and 'doc', hiding the ugly
  159. casts here in a single place. */
  160. struct gdb_PyGetSetDef : PyGetSetDef
  161. {
  162. constexpr gdb_PyGetSetDef (const char *name_, getter get_, setter set_,
  163. const char *doc_, void *closure_)
  164. : PyGetSetDef {const_cast<char *> (name_), get_, set_,
  165. const_cast<char *> (doc_), closure_}
  166. {}
  167. /* Alternative constructor that allows omitting the closure in list
  168. initialization. */
  169. constexpr gdb_PyGetSetDef (const char *name_, getter get_, setter set_,
  170. const char *doc_)
  171. : gdb_PyGetSetDef {name_, get_, set_, doc_, NULL}
  172. {}
  173. /* Constructor for the sentinel entries. */
  174. constexpr gdb_PyGetSetDef (std::nullptr_t)
  175. : gdb_PyGetSetDef {NULL, NULL, NULL, NULL, NULL}
  176. {}
  177. };
  178. /* The 'keywords' parameter of PyArg_ParseTupleAndKeywords has type
  179. 'char **'. However, string literals are const in C++, and so to
  180. avoid casting at every keyword array definition, we'll need to make
  181. the keywords array an array of 'const char *'. To avoid having all
  182. callers add a 'const_cast<char **>' themselves when passing such an
  183. array through 'char **', we define our own version of
  184. PyArg_ParseTupleAndKeywords here with a corresponding 'keywords'
  185. parameter type that does the cast in a single place. (This is not
  186. an overload of PyArg_ParseTupleAndKeywords in order to make it
  187. clearer that we're calling our own function instead of a function
  188. that exists in some newer Python version.) */
  189. static inline int
  190. gdb_PyArg_ParseTupleAndKeywords (PyObject *args, PyObject *kw,
  191. const char *format, const char **keywords, ...)
  192. {
  193. va_list ap;
  194. int res;
  195. va_start (ap, keywords);
  196. res = PyArg_VaParseTupleAndKeywords (args, kw, format,
  197. const_cast<char **> (keywords),
  198. ap);
  199. va_end (ap);
  200. return res;
  201. }
  202. /* In order to be able to parse symtab_and_line_to_sal_object function
  203. a real symtab_and_line structure is needed. */
  204. #include "symtab.h"
  205. /* Also needed to parse enum var_types. */
  206. #include "command.h"
  207. #include "breakpoint.h"
  208. enum gdbpy_iter_kind { iter_keys, iter_values, iter_items };
  209. struct block;
  210. struct value;
  211. struct language_defn;
  212. struct program_space;
  213. struct bpstat;
  214. struct inferior;
  215. extern int gdb_python_initialized;
  216. extern PyObject *gdb_module;
  217. extern PyObject *gdb_python_module;
  218. extern PyTypeObject value_object_type
  219. CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("value_object");
  220. extern PyTypeObject block_object_type
  221. CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF("block_object");
  222. extern PyTypeObject symbol_object_type
  223. CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("symbol_object");
  224. extern PyTypeObject event_object_type
  225. CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("event_object");
  226. extern PyTypeObject breakpoint_object_type
  227. CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("breakpoint_object");
  228. extern PyTypeObject frame_object_type
  229. CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("frame_object");
  230. extern PyTypeObject thread_object_type
  231. CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("thread_object");
  232. struct gdbpy_breakpoint_object
  233. {
  234. PyObject_HEAD
  235. /* The breakpoint number according to gdb. */
  236. int number;
  237. /* The gdb breakpoint object, or NULL if the breakpoint has been
  238. deleted. */
  239. struct breakpoint *bp;
  240. /* 1 is this is a FinishBreakpoint object, 0 otherwise. */
  241. int is_finish_bp;
  242. };
  243. /* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
  244. exception if it is invalid. */
  245. #define BPPY_REQUIRE_VALID(Breakpoint) \
  246. do { \
  247. if ((Breakpoint)->bp == NULL) \
  248. return PyErr_Format (PyExc_RuntimeError, \
  249. _("Breakpoint %d is invalid."), \
  250. (Breakpoint)->number); \
  251. } while (0)
  252. /* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
  253. exception if it is invalid. This macro is for use in setter functions. */
  254. #define BPPY_SET_REQUIRE_VALID(Breakpoint) \
  255. do { \
  256. if ((Breakpoint)->bp == NULL) \
  257. { \
  258. PyErr_Format (PyExc_RuntimeError, _("Breakpoint %d is invalid."), \
  259. (Breakpoint)->number); \
  260. return -1; \
  261. } \
  262. } while (0)
  263. /* Variables used to pass information between the Breakpoint
  264. constructor and the breakpoint-created hook function. */
  265. extern gdbpy_breakpoint_object *bppy_pending_object;
  266. struct thread_object
  267. {
  268. PyObject_HEAD
  269. /* The thread we represent. */
  270. struct thread_info *thread;
  271. /* The Inferior object to which this thread belongs. */
  272. PyObject *inf_obj;
  273. };
  274. struct inferior_object;
  275. extern struct cmd_list_element *set_python_list;
  276. extern struct cmd_list_element *show_python_list;
  277. /* extension_language_script_ops "methods". */
  278. /* Return true if auto-loading Python scripts is enabled.
  279. This is the extension_language_script_ops.auto_load_enabled "method". */
  280. extern bool gdbpy_auto_load_enabled (const struct extension_language_defn *);
  281. /* extension_language_ops "methods". */
  282. extern enum ext_lang_rc gdbpy_apply_val_pretty_printer
  283. (const struct extension_language_defn *,
  284. struct value *value,
  285. struct ui_file *stream, int recurse,
  286. const struct value_print_options *options,
  287. const struct language_defn *language);
  288. extern enum ext_lang_bt_status gdbpy_apply_frame_filter
  289. (const struct extension_language_defn *,
  290. struct frame_info *frame, frame_filter_flags flags,
  291. enum ext_lang_frame_args args_type,
  292. struct ui_out *out, int frame_low, int frame_high);
  293. extern void gdbpy_preserve_values (const struct extension_language_defn *,
  294. struct objfile *objfile,
  295. htab_t copied_types);
  296. extern enum ext_lang_bp_stop gdbpy_breakpoint_cond_says_stop
  297. (const struct extension_language_defn *, struct breakpoint *);
  298. extern int gdbpy_breakpoint_has_cond (const struct extension_language_defn *,
  299. struct breakpoint *b);
  300. extern enum ext_lang_rc gdbpy_get_matching_xmethod_workers
  301. (const struct extension_language_defn *extlang,
  302. struct type *obj_type, const char *method_name,
  303. std::vector<xmethod_worker_up> *dm_vec);
  304. PyObject *gdbpy_history (PyObject *self, PyObject *args);
  305. PyObject *gdbpy_add_history (PyObject *self, PyObject *args);
  306. extern PyObject *gdbpy_history_count (PyObject *self, PyObject *args);
  307. PyObject *gdbpy_convenience_variable (PyObject *self, PyObject *args);
  308. PyObject *gdbpy_set_convenience_variable (PyObject *self, PyObject *args);
  309. PyObject *gdbpy_breakpoints (PyObject *, PyObject *);
  310. PyObject *gdbpy_frame_stop_reason_string (PyObject *, PyObject *);
  311. PyObject *gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw);
  312. PyObject *gdbpy_lookup_global_symbol (PyObject *self, PyObject *args,
  313. PyObject *kw);
  314. PyObject *gdbpy_lookup_static_symbol (PyObject *self, PyObject *args,
  315. PyObject *kw);
  316. PyObject *gdbpy_lookup_static_symbols (PyObject *self, PyObject *args,
  317. PyObject *kw);
  318. PyObject *gdbpy_start_recording (PyObject *self, PyObject *args);
  319. PyObject *gdbpy_current_recording (PyObject *self, PyObject *args);
  320. PyObject *gdbpy_stop_recording (PyObject *self, PyObject *args);
  321. PyObject *gdbpy_newest_frame (PyObject *self, PyObject *args);
  322. PyObject *gdbpy_selected_frame (PyObject *self, PyObject *args);
  323. PyObject *gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw);
  324. int gdbpy_is_field (PyObject *obj);
  325. PyObject *gdbpy_create_lazy_string_object (CORE_ADDR address, long length,
  326. const char *encoding,
  327. struct type *type);
  328. PyObject *gdbpy_inferiors (PyObject *unused, PyObject *unused2);
  329. PyObject *gdbpy_create_ptid_object (ptid_t ptid);
  330. PyObject *gdbpy_selected_thread (PyObject *self, PyObject *args);
  331. PyObject *gdbpy_selected_inferior (PyObject *self, PyObject *args);
  332. PyObject *gdbpy_string_to_argv (PyObject *self, PyObject *args);
  333. PyObject *gdbpy_parameter_value (const setting &var);
  334. gdb::unique_xmalloc_ptr<char> gdbpy_parse_command_name
  335. (const char *name, struct cmd_list_element ***base_list,
  336. struct cmd_list_element **start_list);
  337. PyObject *gdbpy_register_tui_window (PyObject *self, PyObject *args,
  338. PyObject *kw);
  339. PyObject *symtab_and_line_to_sal_object (struct symtab_and_line sal);
  340. PyObject *symtab_to_symtab_object (struct symtab *symtab);
  341. PyObject *symbol_to_symbol_object (struct symbol *sym);
  342. PyObject *block_to_block_object (const struct block *block,
  343. struct objfile *objfile);
  344. PyObject *value_to_value_object (struct value *v);
  345. PyObject *value_to_value_object_no_release (struct value *v);
  346. PyObject *type_to_type_object (struct type *);
  347. PyObject *frame_info_to_frame_object (struct frame_info *frame);
  348. PyObject *symtab_to_linetable_object (PyObject *symtab);
  349. gdbpy_ref<> pspace_to_pspace_object (struct program_space *);
  350. PyObject *pspy_get_printers (PyObject *, void *);
  351. PyObject *pspy_get_frame_filters (PyObject *, void *);
  352. PyObject *pspy_get_frame_unwinders (PyObject *, void *);
  353. PyObject *pspy_get_xmethods (PyObject *, void *);
  354. gdbpy_ref<> objfile_to_objfile_object (struct objfile *);
  355. PyObject *objfpy_get_printers (PyObject *, void *);
  356. PyObject *objfpy_get_frame_filters (PyObject *, void *);
  357. PyObject *objfpy_get_frame_unwinders (PyObject *, void *);
  358. PyObject *objfpy_get_xmethods (PyObject *, void *);
  359. PyObject *gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw);
  360. PyObject *gdbarch_to_arch_object (struct gdbarch *gdbarch);
  361. PyObject *gdbpy_all_architecture_names (PyObject *self, PyObject *args);
  362. PyObject *gdbpy_new_register_descriptor_iterator (struct gdbarch *gdbarch,
  363. const char *group_name);
  364. PyObject *gdbpy_new_reggroup_iterator (struct gdbarch *gdbarch);
  365. gdbpy_ref<thread_object> create_thread_object (struct thread_info *tp);
  366. gdbpy_ref<> thread_to_thread_object (thread_info *thr);;
  367. gdbpy_ref<inferior_object> inferior_to_inferior_object (inferior *inf);
  368. PyObject *gdbpy_buffer_to_membuf (gdb::unique_xmalloc_ptr<gdb_byte> buffer,
  369. CORE_ADDR address, ULONGEST length);
  370. struct process_stratum_target;
  371. gdbpy_ref<> target_to_connection_object (process_stratum_target *target);
  372. PyObject *gdbpy_connections (PyObject *self, PyObject *args);
  373. const struct block *block_object_to_block (PyObject *obj);
  374. struct symbol *symbol_object_to_symbol (PyObject *obj);
  375. struct value *value_object_to_value (PyObject *self);
  376. struct value *convert_value_from_python (PyObject *obj);
  377. struct type *type_object_to_type (PyObject *obj);
  378. struct symtab *symtab_object_to_symtab (PyObject *obj);
  379. struct symtab_and_line *sal_object_to_symtab_and_line (PyObject *obj);
  380. struct frame_info *frame_object_to_frame_info (PyObject *frame_obj);
  381. struct gdbarch *arch_object_to_gdbarch (PyObject *obj);
  382. /* Convert Python object OBJ to a program_space pointer. OBJ must be a
  383. gdb.Progspace reference. Return nullptr if the gdb.Progspace is not
  384. valid (see gdb.Progspace.is_valid), otherwise return the program_space
  385. pointer. */
  386. extern struct program_space *progspace_object_to_program_space (PyObject *obj);
  387. void gdbpy_initialize_gdb_readline (void);
  388. int gdbpy_initialize_auto_load (void)
  389. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  390. int gdbpy_initialize_values (void)
  391. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  392. int gdbpy_initialize_frames (void)
  393. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  394. int gdbpy_initialize_instruction (void)
  395. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  396. int gdbpy_initialize_btrace (void)
  397. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  398. int gdbpy_initialize_record (void)
  399. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  400. int gdbpy_initialize_symtabs (void)
  401. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  402. int gdbpy_initialize_commands (void)
  403. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  404. int gdbpy_initialize_symbols (void)
  405. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  406. int gdbpy_initialize_symtabs (void)
  407. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  408. int gdbpy_initialize_blocks (void)
  409. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  410. int gdbpy_initialize_types (void)
  411. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  412. int gdbpy_initialize_functions (void)
  413. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  414. int gdbpy_initialize_pspace (void)
  415. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  416. int gdbpy_initialize_objfile (void)
  417. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  418. int gdbpy_initialize_breakpoints (void)
  419. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  420. int gdbpy_initialize_finishbreakpoints (void)
  421. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  422. int gdbpy_initialize_lazy_string (void)
  423. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  424. int gdbpy_initialize_linetable (void)
  425. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  426. int gdbpy_initialize_parameters (void)
  427. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  428. int gdbpy_initialize_thread (void)
  429. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  430. int gdbpy_initialize_inferior (void)
  431. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  432. int gdbpy_initialize_eventregistry (void)
  433. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  434. int gdbpy_initialize_event (void)
  435. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  436. int gdbpy_initialize_py_events (void)
  437. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  438. int gdbpy_initialize_arch (void)
  439. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  440. int gdbpy_initialize_registers ()
  441. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  442. int gdbpy_initialize_xmethods (void)
  443. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  444. int gdbpy_initialize_unwind (void)
  445. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  446. int gdbpy_initialize_tui ()
  447. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  448. int gdbpy_initialize_membuf ()
  449. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  450. int gdbpy_initialize_connection ()
  451. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  452. int gdbpy_initialize_micommands (void)
  453. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  454. void gdbpy_finalize_micommands ();
  455. /* A wrapper for PyErr_Fetch that handles reference counting for the
  456. caller. */
  457. class gdbpy_err_fetch
  458. {
  459. public:
  460. gdbpy_err_fetch ()
  461. {
  462. PyErr_Fetch (&m_error_type, &m_error_value, &m_error_traceback);
  463. }
  464. ~gdbpy_err_fetch ()
  465. {
  466. Py_XDECREF (m_error_type);
  467. Py_XDECREF (m_error_value);
  468. Py_XDECREF (m_error_traceback);
  469. }
  470. /* Call PyErr_Restore using the values stashed in this object.
  471. After this call, this object is invalid and neither the to_string
  472. nor restore methods may be used again. */
  473. void restore ()
  474. {
  475. PyErr_Restore (m_error_type, m_error_value, m_error_traceback);
  476. m_error_type = nullptr;
  477. m_error_value = nullptr;
  478. m_error_traceback = nullptr;
  479. }
  480. /* Return the string representation of the exception represented by
  481. this object. If the result is NULL a python error occurred, the
  482. caller must clear it. */
  483. gdb::unique_xmalloc_ptr<char> to_string () const;
  484. /* Return the string representation of the type of the exception
  485. represented by this object. If the result is NULL a python error
  486. occurred, the caller must clear it. */
  487. gdb::unique_xmalloc_ptr<char> type_to_string () const;
  488. /* Return true if the stored type matches TYPE, false otherwise. */
  489. bool type_matches (PyObject *type) const
  490. {
  491. return PyErr_GivenExceptionMatches (m_error_type, type);
  492. }
  493. private:
  494. PyObject *m_error_type, *m_error_value, *m_error_traceback;
  495. };
  496. /* Called before entering the Python interpreter to install the
  497. current language and architecture to be used for Python values.
  498. Also set the active extension language for GDB so that SIGINT's
  499. are directed our way, and if necessary install the right SIGINT
  500. handler. */
  501. class gdbpy_enter
  502. {
  503. public:
  504. /* Set the ambient Python architecture to GDBARCH and the language
  505. to LANGUAGE. If GDBARCH is nullptr, then the architecture will
  506. be computed, when needed, using get_current_arch; see the
  507. get_gdbarch method. If LANGUAGE is not nullptr, then the current
  508. language at time of construction will be saved (to be restored on
  509. destruction), and the current language will be set to
  510. LANGUAGE. */
  511. explicit gdbpy_enter (struct gdbarch *gdbarch = nullptr,
  512. const struct language_defn *language = nullptr);
  513. ~gdbpy_enter ();
  514. DISABLE_COPY_AND_ASSIGN (gdbpy_enter);
  515. /* Return the current gdbarch, as known to the Python layer. This
  516. is either python_gdbarch (which comes from the most recent call
  517. to the gdbpy_enter constructor), or, if that is nullptr, the
  518. result of get_current_arch. */
  519. static struct gdbarch *get_gdbarch ();
  520. /* Called only during gdb shutdown. This sets python_gdbarch to an
  521. acceptable value. */
  522. static void finalize ();
  523. private:
  524. /* The current gdbarch, according to Python. This can be
  525. nullptr. */
  526. static struct gdbarch *python_gdbarch;
  527. struct active_ext_lang_state *m_previous_active;
  528. PyGILState_STATE m_state;
  529. struct gdbarch *m_gdbarch;
  530. const struct language_defn *m_language;
  531. /* An optional is used here because we don't want to call
  532. PyErr_Fetch too early. */
  533. gdb::optional<gdbpy_err_fetch> m_error;
  534. };
  535. /* Like gdbpy_enter, but takes a varobj. This is a subclass just to
  536. make constructor delegation a little nicer. */
  537. class gdbpy_enter_varobj : public gdbpy_enter
  538. {
  539. public:
  540. /* This is defined in varobj.c, where it can access varobj
  541. internals. */
  542. gdbpy_enter_varobj (const struct varobj *var);
  543. };
  544. /* The opposite of gdb_enter: this releases the GIL around a region,
  545. allowing other Python threads to run. No Python APIs may be used
  546. while this is active. */
  547. class gdbpy_allow_threads
  548. {
  549. public:
  550. gdbpy_allow_threads ()
  551. : m_save (PyEval_SaveThread ())
  552. {
  553. gdb_assert (m_save != nullptr);
  554. }
  555. ~gdbpy_allow_threads ()
  556. {
  557. PyEval_RestoreThread (m_save);
  558. }
  559. DISABLE_COPY_AND_ASSIGN (gdbpy_allow_threads);
  560. private:
  561. PyThreadState *m_save;
  562. };
  563. /* Use this after a TRY_EXCEPT to throw the appropriate Python
  564. exception. */
  565. #define GDB_PY_HANDLE_EXCEPTION(Exception) \
  566. do { \
  567. if (Exception.reason < 0) \
  568. { \
  569. gdbpy_convert_exception (Exception); \
  570. return NULL; \
  571. } \
  572. } while (0)
  573. /* Use this after a TRY_EXCEPT to throw the appropriate Python
  574. exception. This macro is for use inside setter functions. */
  575. #define GDB_PY_SET_HANDLE_EXCEPTION(Exception) \
  576. do { \
  577. if (Exception.reason < 0) \
  578. { \
  579. gdbpy_convert_exception (Exception); \
  580. return -1; \
  581. } \
  582. } while (0)
  583. int gdbpy_print_python_errors_p (void);
  584. void gdbpy_print_stack (void);
  585. void gdbpy_print_stack_or_quit ();
  586. void gdbpy_handle_exception () ATTRIBUTE_NORETURN;
  587. /* A wrapper around calling 'error'. Prefixes the error message with an
  588. 'Error occurred in Python' string. Use this in C++ code if we spot
  589. something wrong with an object returned from Python code. The prefix
  590. string gives the user a hint that the mistake is within Python code,
  591. rather than some other part of GDB.
  592. This always calls error, and never returns. */
  593. void gdbpy_error (const char *fmt, ...)
  594. ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2);
  595. gdbpy_ref<> python_string_to_unicode (PyObject *obj);
  596. gdb::unique_xmalloc_ptr<char> unicode_to_target_string (PyObject *unicode_str);
  597. gdb::unique_xmalloc_ptr<char> python_string_to_target_string (PyObject *obj);
  598. gdbpy_ref<> python_string_to_target_python_string (PyObject *obj);
  599. gdb::unique_xmalloc_ptr<char> python_string_to_host_string (PyObject *obj);
  600. gdbpy_ref<> host_string_to_python_string (const char *str);
  601. int gdbpy_is_string (PyObject *obj);
  602. gdb::unique_xmalloc_ptr<char> gdbpy_obj_to_string (PyObject *obj);
  603. int gdbpy_is_lazy_string (PyObject *result);
  604. void gdbpy_extract_lazy_string (PyObject *string, CORE_ADDR *addr,
  605. struct type **str_type,
  606. long *length,
  607. gdb::unique_xmalloc_ptr<char> *encoding);
  608. int gdbpy_is_value_object (PyObject *obj);
  609. /* Note that these are declared here, and not in python.h with the
  610. other pretty-printer functions, because they refer to PyObject. */
  611. gdbpy_ref<> apply_varobj_pretty_printer (PyObject *print_obj,
  612. struct value **replacement,
  613. struct ui_file *stream);
  614. gdbpy_ref<> gdbpy_get_varobj_pretty_printer (struct value *value);
  615. gdb::unique_xmalloc_ptr<char> gdbpy_get_display_hint (PyObject *printer);
  616. PyObject *gdbpy_default_visualizer (PyObject *self, PyObject *args);
  617. void bpfinishpy_pre_stop_hook (struct gdbpy_breakpoint_object *bp_obj);
  618. void bpfinishpy_post_stop_hook (struct gdbpy_breakpoint_object *bp_obj);
  619. extern PyObject *gdbpy_doc_cst;
  620. extern PyObject *gdbpy_children_cst;
  621. extern PyObject *gdbpy_to_string_cst;
  622. extern PyObject *gdbpy_display_hint_cst;
  623. extern PyObject *gdbpy_enabled_cst;
  624. extern PyObject *gdbpy_value_cst;
  625. /* Exception types. */
  626. extern PyObject *gdbpy_gdb_error;
  627. extern PyObject *gdbpy_gdb_memory_error;
  628. extern PyObject *gdbpy_gdberror_exc;
  629. extern void gdbpy_convert_exception (const struct gdb_exception &)
  630. CPYCHECKER_SETS_EXCEPTION;
  631. int get_addr_from_python (PyObject *obj, CORE_ADDR *addr)
  632. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  633. gdbpy_ref<> gdb_py_object_from_longest (LONGEST l);
  634. gdbpy_ref<> gdb_py_object_from_ulongest (ULONGEST l);
  635. int gdb_py_int_as_long (PyObject *, long *);
  636. PyObject *gdb_py_generic_dict (PyObject *self, void *closure);
  637. int gdb_pymodule_addobject (PyObject *module, const char *name,
  638. PyObject *object)
  639. CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
  640. struct varobj_iter;
  641. struct varobj;
  642. std::unique_ptr<varobj_iter> py_varobj_get_iterator (struct varobj *var,
  643. PyObject *printer);
  644. /* Deleter for Py_buffer unique_ptr specialization. */
  645. struct Py_buffer_deleter
  646. {
  647. void operator() (Py_buffer *b) const
  648. {
  649. PyBuffer_Release (b);
  650. }
  651. };
  652. /* A unique_ptr specialization for Py_buffer. */
  653. typedef std::unique_ptr<Py_buffer, Py_buffer_deleter> Py_buffer_up;
  654. /* Parse a register number from PYO_REG_ID and place the register number
  655. into *REG_NUM. The register is a register for GDBARCH.
  656. If a register is parsed successfully then *REG_NUM will have been
  657. updated, and true is returned. Otherwise the contents of *REG_NUM are
  658. undefined, and false is returned.
  659. The PYO_REG_ID object can be a string, the name of the register. This
  660. is the slowest approach as GDB has to map the name to a number for each
  661. call. Alternatively PYO_REG_ID can be an internal GDB register
  662. number. This is quick but should not be encouraged as this means
  663. Python scripts are now dependent on GDB's internal register numbering.
  664. Final PYO_REG_ID can be a gdb.RegisterDescriptor object, these objects
  665. can be looked up by name once, and then cache the register number so
  666. should be as quick as using a register number. */
  667. extern bool gdbpy_parse_register_id (struct gdbarch *gdbarch,
  668. PyObject *pyo_reg_id, int *reg_num);
  669. /* Return true if OBJ is a gdb.Architecture object, otherwise, return
  670. false. */
  671. extern bool gdbpy_is_architecture (PyObject *obj);
  672. /* Return true if OBJ is a gdb.Progspace object, otherwise, return false. */
  673. extern bool gdbpy_is_progspace (PyObject *obj);
  674. #endif /* PYTHON_PYTHON_INTERNAL_H */