Commit | Line | Data |
---|---|---|
d57a3c85 TJB |
1 | /* Gdb/Python header for private use by Python module. |
2 | ||
28e7fd62 | 3 | Copyright (C) 2008-2013 Free Software Foundation, Inc. |
d57a3c85 TJB |
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 | #ifndef GDB_PYTHON_INTERNAL_H | |
21 | #define GDB_PYTHON_INTERNAL_H | |
22 | ||
8a1ea21f DE |
23 | #include <stdio.h> |
24 | ||
d57a3c85 TJB |
25 | /* Python 2.4 doesn't include stdint.h soon enough to get {u,}intptr_t |
26 | needed by pyport.h. */ | |
27 | #include <stdint.h> | |
28 | ||
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. | |
aac63f0f JB |
33 | To work around this, undef _POSIX_C_SOURCE before we include Python.h. |
34 | ||
35 | Same problem with _XOPEN_SOURCE. */ | |
d57a3c85 | 36 | #undef _POSIX_C_SOURCE |
aac63f0f | 37 | #undef _XOPEN_SOURCE |
d57a3c85 | 38 | |
aed1781d JB |
39 | /* On sparc-solaris, /usr/include/sys/feature_tests.h defines |
40 | _FILE_OFFSET_BITS, which pyconfig.h also defines. Same work | |
284a3db3 | 41 | around technique as above. */ |
aed1781d JB |
42 | #undef _FILE_OFFSET_BITS |
43 | ||
1cdd3232 EZ |
44 | /* A kludge to avoid redefinition of snprintf on Windows by pyerrors.h. */ |
45 | #if defined(_WIN32) && defined(HAVE_DECL_SNPRINTF) | |
46 | #define HAVE_SNPRINTF 1 | |
47 | #endif | |
48 | ||
1c033f8c TT |
49 | /* Request clean size types from Python. */ |
50 | #define PY_SSIZE_T_CLEAN | |
51 | ||
ac534cba JB |
52 | /* Include the Python header files using angle brackets rather than |
53 | double quotes. On case-insensitive filesystems, this prevents us | |
54 | from including our python/python.h header file. */ | |
55 | #include <Python.h> | |
56 | #include <frameobject.h> | |
9a27f2c6 PK |
57 | |
58 | #if PY_MAJOR_VERSION >= 3 | |
59 | #define IS_PY3K 1 | |
60 | #endif | |
61 | ||
62 | #ifdef IS_PY3K | |
63 | #define Py_TPFLAGS_HAVE_ITER 0 | |
64 | #define Py_TPFLAGS_CHECKTYPES 0 | |
65 | ||
66 | #define PyInt_Check PyLong_Check | |
67 | #define PyInt_FromLong PyLong_FromLong | |
68 | #define PyInt_AsLong PyLong_AsLong | |
69 | ||
70 | #define PyString_FromString PyUnicode_FromString | |
71 | #define PyString_Decode PyUnicode_Decode | |
72 | #define PyString_FromFormat PyUnicode_FromFormat | |
73 | #define PyString_Check PyUnicode_Check | |
74 | #endif | |
75 | ||
d57a3c85 | 76 | #if HAVE_LIBPYTHON2_4 |
5171e6b3 TT |
77 | /* Py_ssize_t is not defined until 2.5. |
78 | Logical type for Py_ssize_t is Py_intptr_t, but that fails in 64-bit | |
79 | compilation due to several apparent mistakes in python2.4 API, so we | |
80 | use 'int' instead. */ | |
81 | typedef int Py_ssize_t; | |
d57a3c85 TJB |
82 | #endif |
83 | ||
9a27f2c6 PK |
84 | #ifndef PyVarObject_HEAD_INIT |
85 | /* Python 2.4 does not define PyVarObject_HEAD_INIT. */ | |
86 | #define PyVarObject_HEAD_INIT(type, size) \ | |
87 | PyObject_HEAD_INIT(type) size, | |
88 | ||
89 | #endif | |
90 | ||
91 | #ifndef Py_TYPE | |
92 | /* Python 2.4 does not define Py_TYPE. */ | |
93 | #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) | |
94 | #endif | |
95 | ||
ca30a762 TT |
96 | /* If Python.h does not define WITH_THREAD, then the various |
97 | GIL-related functions will not be defined. However, | |
98 | PyGILState_STATE will be. */ | |
99 | #ifndef WITH_THREAD | |
100 | #define PyGILState_Ensure() ((PyGILState_STATE) 0) | |
548a926a | 101 | #define PyGILState_Release(ARG) ((void)(ARG)) |
aed1781d | 102 | #define PyEval_InitThreads() |
548a926a | 103 | #define PyThreadState_Swap(ARG) ((void)(ARG)) |
aed1781d | 104 | #define PyEval_ReleaseLock() |
ca30a762 TT |
105 | #endif |
106 | ||
74aedc46 TT |
107 | /* Python supplies HAVE_LONG_LONG and some `long long' support when it |
108 | is available. These defines let us handle the differences more | |
109 | cleanly. */ | |
110 | #ifdef HAVE_LONG_LONG | |
111 | ||
112 | #define GDB_PY_LL_ARG "L" | |
113 | #define GDB_PY_LLU_ARG "K" | |
114 | typedef PY_LONG_LONG gdb_py_longest; | |
115 | typedef unsigned PY_LONG_LONG gdb_py_ulongest; | |
116 | #define gdb_py_long_from_longest PyLong_FromLongLong | |
117 | #define gdb_py_long_from_ulongest PyLong_FromUnsignedLongLong | |
118 | #define gdb_py_long_as_ulongest PyLong_AsUnsignedLongLong | |
119 | ||
120 | #else /* HAVE_LONG_LONG */ | |
121 | ||
122 | #define GDB_PY_LL_ARG "L" | |
123 | #define GDB_PY_LLU_ARG "K" | |
124 | typedef long gdb_py_longest; | |
125 | typedef unsigned long gdb_py_ulongest; | |
126 | #define gdb_py_long_from_longest PyLong_FromLong | |
127 | #define gdb_py_long_from_ulongest PyLong_FromUnsignedLong | |
128 | #define gdb_py_long_as_ulongest PyLong_AsUnsignedLong | |
129 | ||
130 | #endif /* HAVE_LONG_LONG */ | |
131 | ||
132 | ||
9cb74f47 PM |
133 | /* In order to be able to parse symtab_and_line_to_sal_object function |
134 | a real symtab_and_line structure is needed. */ | |
135 | #include "symtab.h" | |
136 | ||
d7b32ed3 PM |
137 | /* Also needed to parse enum var_types. */ |
138 | #include "command.h" | |
505500db | 139 | #include "breakpoint.h" |
d7b32ed3 | 140 | |
621c8364 TT |
141 | #include "exceptions.h" |
142 | ||
a73bb892 PK |
143 | enum gdbpy_iter_kind { iter_keys, iter_values, iter_items }; |
144 | ||
f3e9a817 | 145 | struct block; |
a08702d6 | 146 | struct value; |
d452c4bc | 147 | struct language_defn; |
fa33c3cd | 148 | struct program_space; |
505500db | 149 | struct bpstats; |
619cebe8 | 150 | struct inferior; |
d57a3c85 TJB |
151 | |
152 | extern PyObject *gdb_module; | |
b9516fa1 | 153 | extern PyObject *gdb_python_module; |
a08702d6 | 154 | extern PyTypeObject value_object_type; |
f3e9a817 PM |
155 | extern PyTypeObject block_object_type; |
156 | extern PyTypeObject symbol_object_type; | |
505500db SW |
157 | extern PyTypeObject event_object_type; |
158 | extern PyTypeObject events_object_type; | |
159 | extern PyTypeObject stop_event_object_type; | |
cc72b2a2 | 160 | extern PyTypeObject breakpoint_object_type; |
f0823d2c | 161 | extern PyTypeObject frame_object_type; |
cc72b2a2 KP |
162 | |
163 | typedef struct breakpoint_object | |
164 | { | |
165 | PyObject_HEAD | |
166 | ||
167 | /* The breakpoint number according to gdb. */ | |
168 | int number; | |
169 | ||
170 | /* The gdb breakpoint object, or NULL if the breakpoint has been | |
171 | deleted. */ | |
172 | struct breakpoint *bp; | |
173 | ||
174 | /* 1 is this is a FinishBreakpoint object, 0 otherwise. */ | |
175 | int is_finish_bp; | |
176 | } breakpoint_object; | |
177 | ||
178 | /* Require that BREAKPOINT be a valid breakpoint ID; throw a Python | |
179 | exception if it is invalid. */ | |
180 | #define BPPY_REQUIRE_VALID(Breakpoint) \ | |
181 | do { \ | |
182 | if ((Breakpoint)->bp == NULL) \ | |
183 | return PyErr_Format (PyExc_RuntimeError, \ | |
184 | _("Breakpoint %d is invalid."), \ | |
185 | (Breakpoint)->number); \ | |
186 | } while (0) | |
187 | ||
188 | /* Require that BREAKPOINT be a valid breakpoint ID; throw a Python | |
189 | exception if it is invalid. This macro is for use in setter functions. */ | |
190 | #define BPPY_SET_REQUIRE_VALID(Breakpoint) \ | |
191 | do { \ | |
192 | if ((Breakpoint)->bp == NULL) \ | |
193 | { \ | |
194 | PyErr_Format (PyExc_RuntimeError, _("Breakpoint %d is invalid."), \ | |
195 | (Breakpoint)->number); \ | |
196 | return -1; \ | |
197 | } \ | |
198 | } while (0) | |
199 | ||
200 | ||
201 | /* Variables used to pass information between the Breakpoint | |
202 | constructor and the breakpoint-created hook function. */ | |
203 | extern breakpoint_object *bppy_pending_object; | |
505500db | 204 | |
a08702d6 | 205 | |
595939de PM |
206 | typedef struct |
207 | { | |
208 | PyObject_HEAD | |
209 | ||
210 | /* The thread we represent. */ | |
211 | struct thread_info *thread; | |
212 | ||
213 | /* The Inferior object to which this thread belongs. */ | |
214 | PyObject *inf_obj; | |
215 | } thread_object; | |
216 | ||
8a1ea21f DE |
217 | extern struct cmd_list_element *set_python_list; |
218 | extern struct cmd_list_element *show_python_list; | |
219 | ||
08c637de | 220 | PyObject *gdbpy_history (PyObject *self, PyObject *args); |
adc36818 | 221 | PyObject *gdbpy_breakpoints (PyObject *, PyObject *); |
f8f6f20b | 222 | PyObject *gdbpy_frame_stop_reason_string (PyObject *, PyObject *); |
f3e9a817 | 223 | PyObject *gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw); |
6e6fbe60 DE |
224 | PyObject *gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, |
225 | PyObject *kw); | |
d8e22779 | 226 | PyObject *gdbpy_newest_frame (PyObject *self, PyObject *args); |
f8f6f20b | 227 | PyObject *gdbpy_selected_frame (PyObject *self, PyObject *args); |
f3e9a817 | 228 | PyObject *gdbpy_block_for_pc (PyObject *self, PyObject *args); |
2c74e833 | 229 | PyObject *gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw); |
be759fcf | 230 | PyObject *gdbpy_create_lazy_string_object (CORE_ADDR address, long length, |
9a2b4c1b MS |
231 | const char *encoding, |
232 | struct type *type); | |
595939de PM |
233 | PyObject *gdbpy_inferiors (PyObject *unused, PyObject *unused2); |
234 | PyObject *gdbpy_selected_thread (PyObject *self, PyObject *args); | |
2aa48337 | 235 | PyObject *gdbpy_selected_inferior (PyObject *self, PyObject *args); |
07ca107c | 236 | PyObject *gdbpy_string_to_argv (PyObject *self, PyObject *args); |
d7b32ed3 PM |
237 | PyObject *gdbpy_parameter (PyObject *self, PyObject *args); |
238 | PyObject *gdbpy_parameter_value (enum var_types type, void *var); | |
63d97a20 | 239 | char *gdbpy_parse_command_name (const char *name, |
d7b32ed3 PM |
240 | struct cmd_list_element ***base_list, |
241 | struct cmd_list_element **start_list); | |
a08702d6 | 242 | |
f3e9a817 PM |
243 | PyObject *symtab_and_line_to_sal_object (struct symtab_and_line sal); |
244 | PyObject *symtab_to_symtab_object (struct symtab *symtab); | |
245 | PyObject *symbol_to_symbol_object (struct symbol *sym); | |
9df2fbc4 PM |
246 | PyObject *block_to_block_object (const struct block *block, |
247 | struct objfile *objfile); | |
a08702d6 | 248 | PyObject *value_to_value_object (struct value *v); |
2c74e833 | 249 | PyObject *type_to_type_object (struct type *); |
595939de | 250 | PyObject *frame_info_to_frame_object (struct frame_info *frame); |
89c73ade | 251 | |
fa33c3cd DE |
252 | PyObject *pspace_to_pspace_object (struct program_space *); |
253 | PyObject *pspy_get_printers (PyObject *, void *); | |
1e611234 | 254 | PyObject *pspy_get_frame_filters (PyObject *, void *); |
fa33c3cd DE |
255 | |
256 | PyObject *objfile_to_objfile_object (struct objfile *); | |
89c73ade | 257 | PyObject *objfpy_get_printers (PyObject *, void *); |
1e611234 | 258 | PyObject *objfpy_get_frame_filters (PyObject *, void *); |
a08702d6 | 259 | |
bea883fd SCR |
260 | PyObject *gdbarch_to_arch_object (struct gdbarch *gdbarch); |
261 | ||
595939de PM |
262 | thread_object *create_thread_object (struct thread_info *tp); |
263 | thread_object *find_thread_object (ptid_t ptid); | |
264 | PyObject *find_inferior_object (int pid); | |
505500db | 265 | PyObject *inferior_to_inferior_object (struct inferior *inferior); |
595939de | 266 | |
9df2fbc4 | 267 | const struct block *block_object_to_block (PyObject *obj); |
f3e9a817 | 268 | struct symbol *symbol_object_to_symbol (PyObject *obj); |
a6bac58e | 269 | struct value *value_object_to_value (PyObject *self); |
a08702d6 | 270 | struct value *convert_value_from_python (PyObject *obj); |
2c74e833 | 271 | struct type *type_object_to_type (PyObject *obj); |
f3e9a817 PM |
272 | struct symtab *symtab_object_to_symtab (PyObject *obj); |
273 | struct symtab_and_line *sal_object_to_symtab_and_line (PyObject *obj); | |
cc72b2a2 | 274 | struct frame_info *frame_object_to_frame_info (PyObject *frame_obj); |
bea883fd | 275 | struct gdbarch *arch_object_to_gdbarch (PyObject *obj); |
a08702d6 | 276 | |
037bbc8e | 277 | void gdbpy_initialize_gdb_readline (void); |
8a1ea21f | 278 | void gdbpy_initialize_auto_load (void); |
a08702d6 | 279 | void gdbpy_initialize_values (void); |
f8f6f20b | 280 | void gdbpy_initialize_frames (void); |
f3e9a817 | 281 | void gdbpy_initialize_symtabs (void); |
d8906c6f | 282 | void gdbpy_initialize_commands (void); |
f3e9a817 PM |
283 | void gdbpy_initialize_symbols (void); |
284 | void gdbpy_initialize_symtabs (void); | |
285 | void gdbpy_initialize_blocks (void); | |
2c74e833 | 286 | void gdbpy_initialize_types (void); |
bc3b79fd | 287 | void gdbpy_initialize_functions (void); |
fa33c3cd | 288 | void gdbpy_initialize_pspace (void); |
89c73ade | 289 | void gdbpy_initialize_objfile (void); |
adc36818 | 290 | void gdbpy_initialize_breakpoints (void); |
cc72b2a2 | 291 | void gdbpy_initialize_finishbreakpoints (void); |
be759fcf | 292 | void gdbpy_initialize_lazy_string (void); |
d7b32ed3 | 293 | void gdbpy_initialize_parameters (void); |
595939de PM |
294 | void gdbpy_initialize_thread (void); |
295 | void gdbpy_initialize_inferior (void); | |
505500db SW |
296 | void gdbpy_initialize_eventregistry (void); |
297 | void gdbpy_initialize_event (void); | |
298 | void gdbpy_initialize_py_events (void); | |
299 | void gdbpy_initialize_stop_event (void); | |
300 | void gdbpy_initialize_signal_event (void); | |
301 | void gdbpy_initialize_breakpoint_event (void); | |
302 | void gdbpy_initialize_continue_event (void); | |
303 | void gdbpy_initialize_exited_event (void); | |
304 | void gdbpy_initialize_thread_event (void); | |
20c168b5 | 305 | void gdbpy_initialize_new_objfile_event (void); |
bea883fd | 306 | void gdbpy_initialize_arch (void); |
d57a3c85 TJB |
307 | |
308 | struct cleanup *make_cleanup_py_decref (PyObject *py); | |
1e611234 | 309 | struct cleanup *make_cleanup_py_xdecref (PyObject *py); |
d452c4bc UW |
310 | |
311 | struct cleanup *ensure_python_env (struct gdbarch *gdbarch, | |
312 | const struct language_defn *language); | |
313 | ||
314 | extern struct gdbarch *python_gdbarch; | |
315 | extern const struct language_defn *python_language; | |
d57a3c85 TJB |
316 | |
317 | /* Use this after a TRY_EXCEPT to throw the appropriate Python | |
318 | exception. */ | |
319 | #define GDB_PY_HANDLE_EXCEPTION(Exception) \ | |
320 | do { \ | |
321 | if (Exception.reason < 0) \ | |
621c8364 | 322 | return gdbpy_convert_exception (Exception); \ |
d57a3c85 TJB |
323 | } while (0) |
324 | ||
adc36818 PM |
325 | /* Use this after a TRY_EXCEPT to throw the appropriate Python |
326 | exception. This macro is for use inside setter functions. */ | |
327 | #define GDB_PY_SET_HANDLE_EXCEPTION(Exception) \ | |
328 | do { \ | |
329 | if (Exception.reason < 0) \ | |
330 | { \ | |
621c8364 | 331 | gdbpy_convert_exception (Exception); \ |
adc36818 PM |
332 | return -1; \ |
333 | } \ | |
334 | } while (0) | |
d57a3c85 TJB |
335 | |
336 | void gdbpy_print_stack (void); | |
337 | ||
4c63965b JK |
338 | void source_python_script_for_objfile (struct objfile *objfile, FILE *file, |
339 | const char *filename); | |
8a1ea21f | 340 | |
d57a3c85 TJB |
341 | PyObject *python_string_to_unicode (PyObject *obj); |
342 | char *unicode_to_target_string (PyObject *unicode_str); | |
343 | char *python_string_to_target_string (PyObject *obj); | |
fbb8f299 | 344 | PyObject *python_string_to_target_python_string (PyObject *obj); |
08c637de TJB |
345 | char *python_string_to_host_string (PyObject *obj); |
346 | int gdbpy_is_string (PyObject *obj); | |
07ca107c DE |
347 | char *gdbpy_obj_to_string (PyObject *obj); |
348 | char *gdbpy_exception_to_string (PyObject *ptype, PyObject *pvalue); | |
349 | ||
be759fcf | 350 | int gdbpy_is_lazy_string (PyObject *result); |
09ca9e2e TT |
351 | void gdbpy_extract_lazy_string (PyObject *string, CORE_ADDR *addr, |
352 | struct type **str_type, | |
353 | long *length, char **encoding); | |
d57a3c85 | 354 | |
595939de PM |
355 | int gdbpy_is_value_object (PyObject *obj); |
356 | ||
b6313243 TT |
357 | /* Note that these are declared here, and not in python.h with the |
358 | other pretty-printer functions, because they refer to PyObject. */ | |
fbb8f299 | 359 | PyObject *apply_varobj_pretty_printer (PyObject *print_obj, |
621c8364 TT |
360 | struct value **replacement, |
361 | struct ui_file *stream); | |
b6313243 TT |
362 | PyObject *gdbpy_get_varobj_pretty_printer (struct value *value); |
363 | char *gdbpy_get_display_hint (PyObject *printer); | |
364 | PyObject *gdbpy_default_visualizer (PyObject *self, PyObject *args); | |
365 | ||
cc72b2a2 KP |
366 | void bpfinishpy_pre_stop_hook (struct breakpoint_object *bp_obj); |
367 | void bpfinishpy_post_stop_hook (struct breakpoint_object *bp_obj); | |
368 | ||
d8906c6f | 369 | extern PyObject *gdbpy_doc_cst; |
a6bac58e TT |
370 | extern PyObject *gdbpy_children_cst; |
371 | extern PyObject *gdbpy_to_string_cst; | |
372 | extern PyObject *gdbpy_display_hint_cst; | |
967cf477 | 373 | extern PyObject *gdbpy_enabled_cst; |
fb6a3ed3 | 374 | extern PyObject *gdbpy_value_cst; |
d8906c6f | 375 | |
621c8364 TT |
376 | /* Exception types. */ |
377 | extern PyObject *gdbpy_gdb_error; | |
378 | extern PyObject *gdbpy_gdb_memory_error; | |
07ca107c DE |
379 | extern PyObject *gdbpy_gdberror_exc; |
380 | ||
621c8364 TT |
381 | extern PyObject *gdbpy_convert_exception (struct gdb_exception); |
382 | ||
595939de PM |
383 | int get_addr_from_python (PyObject *obj, CORE_ADDR *addr); |
384 | ||
74aedc46 TT |
385 | PyObject *gdb_py_object_from_longest (LONGEST l); |
386 | PyObject *gdb_py_object_from_ulongest (ULONGEST l); | |
387 | int gdb_py_int_as_long (PyObject *, long *); | |
388 | ||
2e8265fd TT |
389 | PyObject *gdb_py_generic_dict (PyObject *self, void *closure); |
390 | ||
d57a3c85 | 391 | #endif /* GDB_PYTHON_INTERNAL_H */ |