7d52c068607cdc7ff6ad8a1261bf97a55c090a43
[platform/upstream/binutils.git] / gdb / python / python-internal.h
1 /* Gdb/Python header for private use by Python module.
2
3    Copyright (C) 2008-2013 Free Software Foundation, Inc.
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
23 #include <stdio.h>
24
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.
33    To work around this, undef _POSIX_C_SOURCE before we include Python.h.
34
35    Same problem with _XOPEN_SOURCE.  */
36 #undef _POSIX_C_SOURCE
37 #undef _XOPEN_SOURCE
38
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
43
44 /* Request clean size types from Python.  */
45 #define PY_SSIZE_T_CLEAN
46
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>
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
71 #if HAVE_LIBPYTHON2_4
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;
77 #endif
78
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
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)
96 #define PyGILState_Release(ARG) ((void)(ARG))
97 #define PyEval_InitThreads()
98 #define PyThreadState_Swap(ARG) ((void)(ARG))
99 #define PyEval_ReleaseLock()
100 #endif
101
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
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
132 /* Also needed to parse enum var_types. */
133 #include "command.h"
134 #include "breakpoint.h"
135
136 #include "exceptions.h"
137
138 enum gdbpy_iter_kind { iter_keys, iter_values, iter_items };
139
140 struct block;
141 struct value;
142 struct language_defn;
143 struct program_space;
144 struct bpstats;
145 struct inferior;
146
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;
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;
199
200
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
212 extern struct cmd_list_element *set_python_list;
213 extern struct cmd_list_element *show_python_list;
214
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,
220                                       PyObject *kw);
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,
227                                            struct type *type);
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);
237
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);
246
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 *);
251 PyObject *objfpy_get_printers (PyObject *, void *);
252
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);
256 PyObject *inferior_to_inferior_object (struct inferior *inferior);
257
258 const struct block *block_object_to_block (PyObject *obj);
259 struct symbol *symbol_object_to_symbol (PyObject *obj);
260 struct value *value_object_to_value (PyObject *self);
261 struct value *convert_value_from_python (PyObject *obj);
262 struct type *type_object_to_type (PyObject *obj);
263 struct symtab *symtab_object_to_symtab (PyObject *obj);
264 struct symtab_and_line *sal_object_to_symtab_and_line (PyObject *obj);
265 struct frame_info *frame_object_to_frame_info (PyObject *frame_obj);
266
267 void gdbpy_initialize_gdb_readline (void);
268 void gdbpy_initialize_auto_load (void);
269 void gdbpy_initialize_values (void);
270 void gdbpy_initialize_frames (void);
271 void gdbpy_initialize_symtabs (void);
272 void gdbpy_initialize_commands (void);
273 void gdbpy_initialize_symbols (void);
274 void gdbpy_initialize_symtabs (void);
275 void gdbpy_initialize_blocks (void);
276 void gdbpy_initialize_types (void);
277 void gdbpy_initialize_functions (void);
278 void gdbpy_initialize_pspace (void);
279 void gdbpy_initialize_objfile (void);
280 void gdbpy_initialize_breakpoints (void);
281 void gdbpy_initialize_finishbreakpoints (void);
282 void gdbpy_initialize_lazy_string (void);
283 void gdbpy_initialize_parameters (void);
284 void gdbpy_initialize_thread (void);
285 void gdbpy_initialize_inferior (void);
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);
295 void gdbpy_initialize_new_objfile_event (void);
296
297 struct cleanup *make_cleanup_py_decref (PyObject *py);
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;
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)                                         \
310         return gdbpy_convert_exception (Exception);                     \
311     } while (0)
312
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         {                                                               \
319           gdbpy_convert_exception (Exception);                          \
320           return -1;                                                    \
321         }                                                               \
322     } while (0)
323
324 void gdbpy_print_stack (void);
325
326 void source_python_script_for_objfile (struct objfile *objfile, FILE *file,
327                                        const char *filename);
328
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);
332 PyObject *python_string_to_target_python_string (PyObject *obj);
333 char *python_string_to_host_string (PyObject *obj);
334 PyObject *target_string_to_unicode (const gdb_byte *str, int length);
335 int gdbpy_is_string (PyObject *obj);
336 char *gdbpy_obj_to_string (PyObject *obj);
337 char *gdbpy_exception_to_string (PyObject *ptype, PyObject *pvalue);
338
339 int gdbpy_is_lazy_string (PyObject *result);
340 void gdbpy_extract_lazy_string (PyObject *string, CORE_ADDR *addr,
341                                 struct type **str_type, 
342                                 long *length, char **encoding);
343
344 int gdbpy_is_value_object (PyObject *obj);
345
346 /* Note that these are declared here, and not in python.h with the
347    other pretty-printer functions, because they refer to PyObject.  */
348 PyObject *apply_varobj_pretty_printer (PyObject *print_obj,
349                                        struct value **replacement,
350                                        struct ui_file *stream);
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
355 void bpfinishpy_pre_stop_hook (struct breakpoint_object *bp_obj);
356 void bpfinishpy_post_stop_hook (struct breakpoint_object *bp_obj);
357
358 extern PyObject *gdbpy_doc_cst;
359 extern PyObject *gdbpy_children_cst;
360 extern PyObject *gdbpy_to_string_cst;
361 extern PyObject *gdbpy_display_hint_cst;
362 extern PyObject *gdbpy_enabled_cst;
363 extern PyObject *gdbpy_value_cst;
364
365 /* Exception types.  */
366 extern PyObject *gdbpy_gdb_error;
367 extern PyObject *gdbpy_gdb_memory_error;
368 extern PyObject *gdbpy_gdberror_exc;
369
370 extern PyObject *gdbpy_convert_exception (struct gdb_exception);
371
372 int get_addr_from_python (PyObject *obj, CORE_ADDR *addr);
373
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
378 PyObject *gdb_py_generic_dict (PyObject *self, void *closure);
379
380 #endif /* GDB_PYTHON_INTERNAL_H */