Add support for Python 3.
[deliverable/binutils-gdb.git] / gdb / python / python-internal.h
CommitLineData
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. */
76typedef 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"
109typedef PY_LONG_LONG gdb_py_longest;
110typedef 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"
119typedef long gdb_py_longest;
120typedef 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
138enum gdbpy_iter_kind { iter_keys, iter_values, iter_items };
139
f3e9a817 140struct block;
a08702d6 141struct value;
d452c4bc 142struct language_defn;
fa33c3cd 143struct program_space;
505500db 144struct bpstats;
619cebe8 145struct inferior;
d57a3c85
TJB
146
147extern PyObject *gdb_module;
b9516fa1 148extern PyObject *gdb_python_module;
a08702d6 149extern PyTypeObject value_object_type;
f3e9a817
PM
150extern PyTypeObject block_object_type;
151extern PyTypeObject symbol_object_type;
505500db
SW
152extern PyTypeObject event_object_type;
153extern PyTypeObject events_object_type;
154extern PyTypeObject stop_event_object_type;
cc72b2a2 155extern PyTypeObject breakpoint_object_type;
f0823d2c 156extern PyTypeObject frame_object_type;
cc72b2a2
KP
157
158typedef 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. */
198extern breakpoint_object *bppy_pending_object;
505500db 199
a08702d6 200
595939de
PM
201typedef 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
212extern struct cmd_list_element *set_python_list;
213extern struct cmd_list_element *show_python_list;
214
08c637de 215PyObject *gdbpy_history (PyObject *self, PyObject *args);
adc36818 216PyObject *gdbpy_breakpoints (PyObject *, PyObject *);
f8f6f20b 217PyObject *gdbpy_frame_stop_reason_string (PyObject *, PyObject *);
f3e9a817 218PyObject *gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw);
6e6fbe60
DE
219PyObject *gdbpy_lookup_global_symbol (PyObject *self, PyObject *args,
220 PyObject *kw);
d8e22779 221PyObject *gdbpy_newest_frame (PyObject *self, PyObject *args);
f8f6f20b 222PyObject *gdbpy_selected_frame (PyObject *self, PyObject *args);
f3e9a817 223PyObject *gdbpy_block_for_pc (PyObject *self, PyObject *args);
2c74e833 224PyObject *gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw);
be759fcf 225PyObject *gdbpy_create_lazy_string_object (CORE_ADDR address, long length,
9a2b4c1b
MS
226 const char *encoding,
227 struct type *type);
595939de
PM
228PyObject *gdbpy_inferiors (PyObject *unused, PyObject *unused2);
229PyObject *gdbpy_selected_thread (PyObject *self, PyObject *args);
2aa48337 230PyObject *gdbpy_selected_inferior (PyObject *self, PyObject *args);
07ca107c 231PyObject *gdbpy_string_to_argv (PyObject *self, PyObject *args);
d7b32ed3
PM
232PyObject *gdbpy_parameter (PyObject *self, PyObject *args);
233PyObject *gdbpy_parameter_value (enum var_types type, void *var);
63d97a20 234char *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
238PyObject *symtab_and_line_to_sal_object (struct symtab_and_line sal);
239PyObject *symtab_to_symtab_object (struct symtab *symtab);
240PyObject *symbol_to_symbol_object (struct symbol *sym);
9df2fbc4
PM
241PyObject *block_to_block_object (const struct block *block,
242 struct objfile *objfile);
a08702d6 243PyObject *value_to_value_object (struct value *v);
2c74e833 244PyObject *type_to_type_object (struct type *);
595939de 245PyObject *frame_info_to_frame_object (struct frame_info *frame);
89c73ade 246
fa33c3cd
DE
247PyObject *pspace_to_pspace_object (struct program_space *);
248PyObject *pspy_get_printers (PyObject *, void *);
249
250PyObject *objfile_to_objfile_object (struct objfile *);
89c73ade 251PyObject *objfpy_get_printers (PyObject *, void *);
a08702d6 252
595939de
PM
253thread_object *create_thread_object (struct thread_info *tp);
254thread_object *find_thread_object (ptid_t ptid);
255PyObject *find_inferior_object (int pid);
505500db 256PyObject *inferior_to_inferior_object (struct inferior *inferior);
595939de 257
9df2fbc4 258const struct block *block_object_to_block (PyObject *obj);
f3e9a817 259struct symbol *symbol_object_to_symbol (PyObject *obj);
a6bac58e 260struct value *value_object_to_value (PyObject *self);
a08702d6 261struct value *convert_value_from_python (PyObject *obj);
2c74e833 262struct type *type_object_to_type (PyObject *obj);
f3e9a817
PM
263struct symtab *symtab_object_to_symtab (PyObject *obj);
264struct symtab_and_line *sal_object_to_symtab_and_line (PyObject *obj);
cc72b2a2 265struct frame_info *frame_object_to_frame_info (PyObject *frame_obj);
a08702d6 266
037bbc8e 267void gdbpy_initialize_gdb_readline (void);
8a1ea21f 268void gdbpy_initialize_auto_load (void);
a08702d6 269void gdbpy_initialize_values (void);
f8f6f20b 270void gdbpy_initialize_frames (void);
f3e9a817 271void gdbpy_initialize_symtabs (void);
d8906c6f 272void gdbpy_initialize_commands (void);
f3e9a817
PM
273void gdbpy_initialize_symbols (void);
274void gdbpy_initialize_symtabs (void);
275void gdbpy_initialize_blocks (void);
2c74e833 276void gdbpy_initialize_types (void);
bc3b79fd 277void gdbpy_initialize_functions (void);
fa33c3cd 278void gdbpy_initialize_pspace (void);
89c73ade 279void gdbpy_initialize_objfile (void);
adc36818 280void gdbpy_initialize_breakpoints (void);
cc72b2a2 281void gdbpy_initialize_finishbreakpoints (void);
be759fcf 282void gdbpy_initialize_lazy_string (void);
d7b32ed3 283void gdbpy_initialize_parameters (void);
595939de
PM
284void gdbpy_initialize_thread (void);
285void gdbpy_initialize_inferior (void);
505500db
SW
286void gdbpy_initialize_eventregistry (void);
287void gdbpy_initialize_event (void);
288void gdbpy_initialize_py_events (void);
289void gdbpy_initialize_stop_event (void);
290void gdbpy_initialize_signal_event (void);
291void gdbpy_initialize_breakpoint_event (void);
292void gdbpy_initialize_continue_event (void);
293void gdbpy_initialize_exited_event (void);
294void gdbpy_initialize_thread_event (void);
20c168b5 295void gdbpy_initialize_new_objfile_event (void);
d57a3c85
TJB
296
297struct cleanup *make_cleanup_py_decref (PyObject *py);
d452c4bc
UW
298
299struct cleanup *ensure_python_env (struct gdbarch *gdbarch,
300 const struct language_defn *language);
301
302extern struct gdbarch *python_gdbarch;
303extern 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
324void gdbpy_print_stack (void);
325
4c63965b
JK
326void source_python_script_for_objfile (struct objfile *objfile, FILE *file,
327 const char *filename);
8a1ea21f 328
d57a3c85
TJB
329PyObject *python_string_to_unicode (PyObject *obj);
330char *unicode_to_target_string (PyObject *unicode_str);
331char *python_string_to_target_string (PyObject *obj);
fbb8f299 332PyObject *python_string_to_target_python_string (PyObject *obj);
08c637de 333char *python_string_to_host_string (PyObject *obj);
b6cb8e7d 334PyObject *target_string_to_unicode (const gdb_byte *str, int length);
08c637de 335int gdbpy_is_string (PyObject *obj);
07ca107c
DE
336char *gdbpy_obj_to_string (PyObject *obj);
337char *gdbpy_exception_to_string (PyObject *ptype, PyObject *pvalue);
338
be759fcf 339int gdbpy_is_lazy_string (PyObject *result);
09ca9e2e
TT
340void gdbpy_extract_lazy_string (PyObject *string, CORE_ADDR *addr,
341 struct type **str_type,
342 long *length, char **encoding);
d57a3c85 343
595939de
PM
344int 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 348PyObject *apply_varobj_pretty_printer (PyObject *print_obj,
621c8364
TT
349 struct value **replacement,
350 struct ui_file *stream);
b6313243
TT
351PyObject *gdbpy_get_varobj_pretty_printer (struct value *value);
352char *gdbpy_get_display_hint (PyObject *printer);
353PyObject *gdbpy_default_visualizer (PyObject *self, PyObject *args);
354
cc72b2a2
KP
355void bpfinishpy_pre_stop_hook (struct breakpoint_object *bp_obj);
356void bpfinishpy_post_stop_hook (struct breakpoint_object *bp_obj);
357
d8906c6f 358extern PyObject *gdbpy_doc_cst;
a6bac58e
TT
359extern PyObject *gdbpy_children_cst;
360extern PyObject *gdbpy_to_string_cst;
361extern PyObject *gdbpy_display_hint_cst;
967cf477 362extern PyObject *gdbpy_enabled_cst;
fb6a3ed3 363extern PyObject *gdbpy_value_cst;
d8906c6f 364
621c8364
TT
365/* Exception types. */
366extern PyObject *gdbpy_gdb_error;
367extern PyObject *gdbpy_gdb_memory_error;
07ca107c
DE
368extern PyObject *gdbpy_gdberror_exc;
369
621c8364
TT
370extern PyObject *gdbpy_convert_exception (struct gdb_exception);
371
595939de
PM
372int get_addr_from_python (PyObject *obj, CORE_ADDR *addr);
373
74aedc46
TT
374PyObject *gdb_py_object_from_longest (LONGEST l);
375PyObject *gdb_py_object_from_ulongest (ULONGEST l);
376int gdb_py_int_as_long (PyObject *, long *);
377
2e8265fd
TT
378PyObject *gdb_py_generic_dict (PyObject *self, void *closure);
379
d57a3c85 380#endif /* GDB_PYTHON_INTERNAL_H */
This page took 0.610305 seconds and 4 git commands to generate.