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