1 /* Gdb/Python header for private use by Python module.
3 Copyright (C) 2008-2013 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #ifndef GDB_PYTHON_INTERNAL_H
21 #define GDB_PYTHON_INTERNAL_H
25 /* Python 2.4 doesn't include stdint.h soon enough to get {u,}intptr_t
26 needed by pyport.h. */
29 /* /usr/include/features.h on linux systems will define _POSIX_C_SOURCE
30 if it sees _GNU_SOURCE (which config.h will define).
31 pyconfig.h defines _POSIX_C_SOURCE to a different value than
32 /usr/include/features.h does causing compilation to fail.
33 To work around this, undef _POSIX_C_SOURCE before we include Python.h.
35 Same problem with _XOPEN_SOURCE. */
36 #undef _POSIX_C_SOURCE
39 /* On sparc-solaris, /usr/include/sys/feature_tests.h defines
40 _FILE_OFFSET_BITS, which pyconfig.h also defines. Same work
41 around technique as above. */
42 #undef _FILE_OFFSET_BITS
44 /* Request clean size types from Python. */
45 #define PY_SSIZE_T_CLEAN
47 /* Include the Python header files using angle brackets rather than
48 double quotes. On case-insensitive filesystems, this prevents us
49 from including our python/python.h header file. */
51 #include <frameobject.h>
53 #if PY_MAJOR_VERSION >= 3
58 #define Py_TPFLAGS_HAVE_ITER 0
59 #define Py_TPFLAGS_CHECKTYPES 0
61 #define PyInt_Check PyLong_Check
62 #define PyInt_FromLong PyLong_FromLong
63 #define PyInt_AsLong PyLong_AsLong
65 #define PyString_FromString PyUnicode_FromString
66 #define PyString_Decode PyUnicode_Decode
67 #define PyString_FromFormat PyUnicode_FromFormat
68 #define PyString_Check PyUnicode_Check
72 /* Py_ssize_t is not defined until 2.5.
73 Logical type for Py_ssize_t is Py_intptr_t, but that fails in 64-bit
74 compilation due to several apparent mistakes in python2.4 API, so we
76 typedef int Py_ssize_t;
79 #ifndef PyVarObject_HEAD_INIT
80 /* Python 2.4 does not define PyVarObject_HEAD_INIT. */
81 #define PyVarObject_HEAD_INIT(type, size) \
82 PyObject_HEAD_INIT(type) size,
87 /* Python 2.4 does not define Py_TYPE. */
88 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
91 /* If Python.h does not define WITH_THREAD, then the various
92 GIL-related functions will not be defined. However,
93 PyGILState_STATE will be. */
95 #define PyGILState_Ensure() ((PyGILState_STATE) 0)
96 #define PyGILState_Release(ARG) ((void)(ARG))
97 #define PyEval_InitThreads()
98 #define PyThreadState_Swap(ARG) ((void)(ARG))
99 #define PyEval_ReleaseLock()
102 /* Python supplies HAVE_LONG_LONG and some `long long' support when it
103 is available. These defines let us handle the differences more
105 #ifdef HAVE_LONG_LONG
107 #define GDB_PY_LL_ARG "L"
108 #define GDB_PY_LLU_ARG "K"
109 typedef PY_LONG_LONG gdb_py_longest;
110 typedef unsigned PY_LONG_LONG gdb_py_ulongest;
111 #define gdb_py_long_from_longest PyLong_FromLongLong
112 #define gdb_py_long_from_ulongest PyLong_FromUnsignedLongLong
113 #define gdb_py_long_as_ulongest PyLong_AsUnsignedLongLong
115 #else /* HAVE_LONG_LONG */
117 #define GDB_PY_LL_ARG "L"
118 #define GDB_PY_LLU_ARG "K"
119 typedef long gdb_py_longest;
120 typedef unsigned long gdb_py_ulongest;
121 #define gdb_py_long_from_longest PyLong_FromLong
122 #define gdb_py_long_from_ulongest PyLong_FromUnsignedLong
123 #define gdb_py_long_as_ulongest PyLong_AsUnsignedLong
125 #endif /* HAVE_LONG_LONG */
128 /* In order to be able to parse symtab_and_line_to_sal_object function
129 a real symtab_and_line structure is needed. */
132 /* Also needed to parse enum var_types. */
134 #include "breakpoint.h"
136 #include "exceptions.h"
138 enum gdbpy_iter_kind { iter_keys, iter_values, iter_items };
142 struct language_defn;
143 struct program_space;
147 extern PyObject *gdb_module;
148 extern PyObject *gdb_python_module;
149 extern PyTypeObject value_object_type;
150 extern PyTypeObject block_object_type;
151 extern PyTypeObject symbol_object_type;
152 extern PyTypeObject event_object_type;
153 extern PyTypeObject events_object_type;
154 extern PyTypeObject stop_event_object_type;
155 extern PyTypeObject breakpoint_object_type;
156 extern PyTypeObject frame_object_type;
158 typedef struct breakpoint_object
162 /* The breakpoint number according to gdb. */
165 /* The gdb breakpoint object, or NULL if the breakpoint has been
167 struct breakpoint *bp;
169 /* 1 is this is a FinishBreakpoint object, 0 otherwise. */
173 /* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
174 exception if it is invalid. */
175 #define BPPY_REQUIRE_VALID(Breakpoint) \
177 if ((Breakpoint)->bp == NULL) \
178 return PyErr_Format (PyExc_RuntimeError, \
179 _("Breakpoint %d is invalid."), \
180 (Breakpoint)->number); \
183 /* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
184 exception if it is invalid. This macro is for use in setter functions. */
185 #define BPPY_SET_REQUIRE_VALID(Breakpoint) \
187 if ((Breakpoint)->bp == NULL) \
189 PyErr_Format (PyExc_RuntimeError, _("Breakpoint %d is invalid."), \
190 (Breakpoint)->number); \
196 /* Variables used to pass information between the Breakpoint
197 constructor and the breakpoint-created hook function. */
198 extern breakpoint_object *bppy_pending_object;
205 /* The thread we represent. */
206 struct thread_info *thread;
208 /* The Inferior object to which this thread belongs. */
212 extern struct cmd_list_element *set_python_list;
213 extern struct cmd_list_element *show_python_list;
215 PyObject *gdbpy_history (PyObject *self, PyObject *args);
216 PyObject *gdbpy_breakpoints (PyObject *, PyObject *);
217 PyObject *gdbpy_frame_stop_reason_string (PyObject *, PyObject *);
218 PyObject *gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw);
219 PyObject *gdbpy_lookup_global_symbol (PyObject *self, PyObject *args,
221 PyObject *gdbpy_newest_frame (PyObject *self, PyObject *args);
222 PyObject *gdbpy_selected_frame (PyObject *self, PyObject *args);
223 PyObject *gdbpy_block_for_pc (PyObject *self, PyObject *args);
224 PyObject *gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw);
225 PyObject *gdbpy_create_lazy_string_object (CORE_ADDR address, long length,
226 const char *encoding,
228 PyObject *gdbpy_inferiors (PyObject *unused, PyObject *unused2);
229 PyObject *gdbpy_selected_thread (PyObject *self, PyObject *args);
230 PyObject *gdbpy_selected_inferior (PyObject *self, PyObject *args);
231 PyObject *gdbpy_string_to_argv (PyObject *self, PyObject *args);
232 PyObject *gdbpy_parameter (PyObject *self, PyObject *args);
233 PyObject *gdbpy_parameter_value (enum var_types type, void *var);
234 char *gdbpy_parse_command_name (const char *name,
235 struct cmd_list_element ***base_list,
236 struct cmd_list_element **start_list);
238 PyObject *symtab_and_line_to_sal_object (struct symtab_and_line sal);
239 PyObject *symtab_to_symtab_object (struct symtab *symtab);
240 PyObject *symbol_to_symbol_object (struct symbol *sym);
241 PyObject *block_to_block_object (const struct block *block,
242 struct objfile *objfile);
243 PyObject *value_to_value_object (struct value *v);
244 PyObject *type_to_type_object (struct type *);
245 PyObject *frame_info_to_frame_object (struct frame_info *frame);
247 PyObject *pspace_to_pspace_object (struct program_space *);
248 PyObject *pspy_get_printers (PyObject *, void *);
250 PyObject *objfile_to_objfile_object (struct objfile *);
251 PyObject *objfpy_get_printers (PyObject *, void *);
253 PyObject *gdbarch_to_arch_object (struct gdbarch *gdbarch);
255 thread_object *create_thread_object (struct thread_info *tp);
256 thread_object *find_thread_object (ptid_t ptid);
257 PyObject *find_inferior_object (int pid);
258 PyObject *inferior_to_inferior_object (struct inferior *inferior);
260 const struct block *block_object_to_block (PyObject *obj);
261 struct symbol *symbol_object_to_symbol (PyObject *obj);
262 struct value *value_object_to_value (PyObject *self);
263 struct value *convert_value_from_python (PyObject *obj);
264 struct type *type_object_to_type (PyObject *obj);
265 struct symtab *symtab_object_to_symtab (PyObject *obj);
266 struct symtab_and_line *sal_object_to_symtab_and_line (PyObject *obj);
267 struct frame_info *frame_object_to_frame_info (PyObject *frame_obj);
268 struct gdbarch *arch_object_to_gdbarch (PyObject *obj);
270 void gdbpy_initialize_gdb_readline (void);
271 void gdbpy_initialize_auto_load (void);
272 void gdbpy_initialize_values (void);
273 void gdbpy_initialize_frames (void);
274 void gdbpy_initialize_symtabs (void);
275 void gdbpy_initialize_commands (void);
276 void gdbpy_initialize_symbols (void);
277 void gdbpy_initialize_symtabs (void);
278 void gdbpy_initialize_blocks (void);
279 void gdbpy_initialize_types (void);
280 void gdbpy_initialize_functions (void);
281 void gdbpy_initialize_pspace (void);
282 void gdbpy_initialize_objfile (void);
283 void gdbpy_initialize_breakpoints (void);
284 void gdbpy_initialize_finishbreakpoints (void);
285 void gdbpy_initialize_lazy_string (void);
286 void gdbpy_initialize_parameters (void);
287 void gdbpy_initialize_thread (void);
288 void gdbpy_initialize_inferior (void);
289 void gdbpy_initialize_eventregistry (void);
290 void gdbpy_initialize_event (void);
291 void gdbpy_initialize_py_events (void);
292 void gdbpy_initialize_stop_event (void);
293 void gdbpy_initialize_signal_event (void);
294 void gdbpy_initialize_breakpoint_event (void);
295 void gdbpy_initialize_continue_event (void);
296 void gdbpy_initialize_exited_event (void);
297 void gdbpy_initialize_thread_event (void);
298 void gdbpy_initialize_new_objfile_event (void);
299 void gdbpy_initialize_arch (void);
301 struct cleanup *make_cleanup_py_decref (PyObject *py);
303 struct cleanup *ensure_python_env (struct gdbarch *gdbarch,
304 const struct language_defn *language);
306 extern struct gdbarch *python_gdbarch;
307 extern const struct language_defn *python_language;
309 /* Use this after a TRY_EXCEPT to throw the appropriate Python
311 #define GDB_PY_HANDLE_EXCEPTION(Exception) \
313 if (Exception.reason < 0) \
314 return gdbpy_convert_exception (Exception); \
317 /* Use this after a TRY_EXCEPT to throw the appropriate Python
318 exception. This macro is for use inside setter functions. */
319 #define GDB_PY_SET_HANDLE_EXCEPTION(Exception) \
321 if (Exception.reason < 0) \
323 gdbpy_convert_exception (Exception); \
328 void gdbpy_print_stack (void);
330 void source_python_script_for_objfile (struct objfile *objfile, FILE *file,
331 const char *filename);
333 PyObject *python_string_to_unicode (PyObject *obj);
334 char *unicode_to_target_string (PyObject *unicode_str);
335 char *python_string_to_target_string (PyObject *obj);
336 PyObject *python_string_to_target_python_string (PyObject *obj);
337 char *python_string_to_host_string (PyObject *obj);
338 PyObject *target_string_to_unicode (const gdb_byte *str, int length);
339 int gdbpy_is_string (PyObject *obj);
340 char *gdbpy_obj_to_string (PyObject *obj);
341 char *gdbpy_exception_to_string (PyObject *ptype, PyObject *pvalue);
343 int gdbpy_is_lazy_string (PyObject *result);
344 void gdbpy_extract_lazy_string (PyObject *string, CORE_ADDR *addr,
345 struct type **str_type,
346 long *length, char **encoding);
348 int gdbpy_is_value_object (PyObject *obj);
350 /* Note that these are declared here, and not in python.h with the
351 other pretty-printer functions, because they refer to PyObject. */
352 PyObject *apply_varobj_pretty_printer (PyObject *print_obj,
353 struct value **replacement,
354 struct ui_file *stream);
355 PyObject *gdbpy_get_varobj_pretty_printer (struct value *value);
356 char *gdbpy_get_display_hint (PyObject *printer);
357 PyObject *gdbpy_default_visualizer (PyObject *self, PyObject *args);
359 void bpfinishpy_pre_stop_hook (struct breakpoint_object *bp_obj);
360 void bpfinishpy_post_stop_hook (struct breakpoint_object *bp_obj);
362 extern PyObject *gdbpy_doc_cst;
363 extern PyObject *gdbpy_children_cst;
364 extern PyObject *gdbpy_to_string_cst;
365 extern PyObject *gdbpy_display_hint_cst;
366 extern PyObject *gdbpy_enabled_cst;
367 extern PyObject *gdbpy_value_cst;
369 /* Exception types. */
370 extern PyObject *gdbpy_gdb_error;
371 extern PyObject *gdbpy_gdb_memory_error;
372 extern PyObject *gdbpy_gdberror_exc;
374 extern PyObject *gdbpy_convert_exception (struct gdb_exception);
376 int get_addr_from_python (PyObject *obj, CORE_ADDR *addr);
378 PyObject *gdb_py_object_from_longest (LONGEST l);
379 PyObject *gdb_py_object_from_ulongest (ULONGEST l);
380 int gdb_py_int_as_long (PyObject *, long *);
382 PyObject *gdb_py_generic_dict (PyObject *self, void *closure);
384 #endif /* GDB_PYTHON_INTERNAL_H */