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 /* 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 49 /* Request clean size types from Python. */ 50 #define PY_SSIZE_T_CLEAN 51 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> 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 76 #if HAVE_LIBPYTHON2_4 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; 82 #endif 83 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 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) 101 #define PyGILState_Release(ARG) ((void)(ARG)) 102 #define PyEval_InitThreads() 103 #define PyThreadState_Swap(ARG) ((void)(ARG)) 104 #define PyEval_ReleaseLock() 105 #endif 106 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 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 137 /* Also needed to parse enum var_types. */ 138 #include "command.h" 139 #include "breakpoint.h" 140 141 #include "exceptions.h" 142 143 enum gdbpy_iter_kind { iter_keys, iter_values, iter_items }; 144 145 struct block; 146 struct value; 147 struct language_defn; 148 struct program_space; 149 struct bpstats; 150 struct inferior; 151 152 extern PyObject *gdb_module; 153 extern PyObject *gdb_python_module; 154 extern PyTypeObject value_object_type; 155 extern PyTypeObject block_object_type; 156 extern PyTypeObject symbol_object_type; 157 extern PyTypeObject event_object_type; 158 extern PyTypeObject events_object_type; 159 extern PyTypeObject stop_event_object_type; 160 extern PyTypeObject breakpoint_object_type; 161 extern PyTypeObject frame_object_type; 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; 204 205 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 217 extern struct cmd_list_element *set_python_list; 218 extern struct cmd_list_element *show_python_list; 219 220 PyObject *gdbpy_history (PyObject *self, PyObject *args); 221 PyObject *gdbpy_breakpoints (PyObject *, PyObject *); 222 PyObject *gdbpy_frame_stop_reason_string (PyObject *, PyObject *); 223 PyObject *gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw); 224 PyObject *gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, 225 PyObject *kw); 226 PyObject *gdbpy_newest_frame (PyObject *self, PyObject *args); 227 PyObject *gdbpy_selected_frame (PyObject *self, PyObject *args); 228 PyObject *gdbpy_block_for_pc (PyObject *self, PyObject *args); 229 PyObject *gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw); 230 PyObject *gdbpy_create_lazy_string_object (CORE_ADDR address, long length, 231 const char *encoding, 232 struct type *type); 233 PyObject *gdbpy_inferiors (PyObject *unused, PyObject *unused2); 234 PyObject *gdbpy_selected_thread (PyObject *self, PyObject *args); 235 PyObject *gdbpy_selected_inferior (PyObject *self, PyObject *args); 236 PyObject *gdbpy_string_to_argv (PyObject *self, PyObject *args); 237 PyObject *gdbpy_parameter (PyObject *self, PyObject *args); 238 PyObject *gdbpy_parameter_value (enum var_types type, void *var); 239 char *gdbpy_parse_command_name (const char *name, 240 struct cmd_list_element ***base_list, 241 struct cmd_list_element **start_list); 242 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); 246 PyObject *block_to_block_object (const struct block *block, 247 struct objfile *objfile); 248 PyObject *value_to_value_object (struct value *v); 249 PyObject *type_to_type_object (struct type *); 250 PyObject *frame_info_to_frame_object (struct frame_info *frame); 251 252 PyObject *pspace_to_pspace_object (struct program_space *); 253 PyObject *pspy_get_printers (PyObject *, void *); 254 255 PyObject *objfile_to_objfile_object (struct objfile *); 256 PyObject *objfpy_get_printers (PyObject *, void *); 257 258 PyObject *gdbarch_to_arch_object (struct gdbarch *gdbarch); 259 260 thread_object *create_thread_object (struct thread_info *tp); 261 thread_object *find_thread_object (ptid_t ptid); 262 PyObject *find_inferior_object (int pid); 263 PyObject *inferior_to_inferior_object (struct inferior *inferior); 264 265 const struct block *block_object_to_block (PyObject *obj); 266 struct symbol *symbol_object_to_symbol (PyObject *obj); 267 struct value *value_object_to_value (PyObject *self); 268 struct value *convert_value_from_python (PyObject *obj); 269 struct type *type_object_to_type (PyObject *obj); 270 struct symtab *symtab_object_to_symtab (PyObject *obj); 271 struct symtab_and_line *sal_object_to_symtab_and_line (PyObject *obj); 272 struct frame_info *frame_object_to_frame_info (PyObject *frame_obj); 273 struct gdbarch *arch_object_to_gdbarch (PyObject *obj); 274 275 void gdbpy_initialize_gdb_readline (void); 276 void gdbpy_initialize_auto_load (void); 277 void gdbpy_initialize_values (void); 278 void gdbpy_initialize_frames (void); 279 void gdbpy_initialize_symtabs (void); 280 void gdbpy_initialize_commands (void); 281 void gdbpy_initialize_symbols (void); 282 void gdbpy_initialize_symtabs (void); 283 void gdbpy_initialize_blocks (void); 284 void gdbpy_initialize_types (void); 285 void gdbpy_initialize_functions (void); 286 void gdbpy_initialize_pspace (void); 287 void gdbpy_initialize_objfile (void); 288 void gdbpy_initialize_breakpoints (void); 289 void gdbpy_initialize_finishbreakpoints (void); 290 void gdbpy_initialize_lazy_string (void); 291 void gdbpy_initialize_parameters (void); 292 void gdbpy_initialize_thread (void); 293 void gdbpy_initialize_inferior (void); 294 void gdbpy_initialize_eventregistry (void); 295 void gdbpy_initialize_event (void); 296 void gdbpy_initialize_py_events (void); 297 void gdbpy_initialize_stop_event (void); 298 void gdbpy_initialize_signal_event (void); 299 void gdbpy_initialize_breakpoint_event (void); 300 void gdbpy_initialize_continue_event (void); 301 void gdbpy_initialize_exited_event (void); 302 void gdbpy_initialize_thread_event (void); 303 void gdbpy_initialize_new_objfile_event (void); 304 void gdbpy_initialize_arch (void); 305 306 struct cleanup *make_cleanup_py_decref (PyObject *py); 307 308 struct cleanup *ensure_python_env (struct gdbarch *gdbarch, 309 const struct language_defn *language); 310 311 extern struct gdbarch *python_gdbarch; 312 extern const struct language_defn *python_language; 313 314 /* Use this after a TRY_EXCEPT to throw the appropriate Python 315 exception. */ 316 #define GDB_PY_HANDLE_EXCEPTION(Exception) \ 317 do { \ 318 if (Exception.reason < 0) \ 319 return gdbpy_convert_exception (Exception); \ 320 } while (0) 321 322 /* Use this after a TRY_EXCEPT to throw the appropriate Python 323 exception. This macro is for use inside setter functions. */ 324 #define GDB_PY_SET_HANDLE_EXCEPTION(Exception) \ 325 do { \ 326 if (Exception.reason < 0) \ 327 { \ 328 gdbpy_convert_exception (Exception); \ 329 return -1; \ 330 } \ 331 } while (0) 332 333 void gdbpy_print_stack (void); 334 335 void source_python_script_for_objfile (struct objfile *objfile, FILE *file, 336 const char *filename); 337 338 PyObject *python_string_to_unicode (PyObject *obj); 339 char *unicode_to_target_string (PyObject *unicode_str); 340 char *python_string_to_target_string (PyObject *obj); 341 PyObject *python_string_to_target_python_string (PyObject *obj); 342 char *python_string_to_host_string (PyObject *obj); 343 int gdbpy_is_string (PyObject *obj); 344 char *gdbpy_obj_to_string (PyObject *obj); 345 char *gdbpy_exception_to_string (PyObject *ptype, PyObject *pvalue); 346 347 int gdbpy_is_lazy_string (PyObject *result); 348 void gdbpy_extract_lazy_string (PyObject *string, CORE_ADDR *addr, 349 struct type **str_type, 350 long *length, char **encoding); 351 352 int gdbpy_is_value_object (PyObject *obj); 353 354 /* Note that these are declared here, and not in python.h with the 355 other pretty-printer functions, because they refer to PyObject. */ 356 PyObject *apply_varobj_pretty_printer (PyObject *print_obj, 357 struct value **replacement, 358 struct ui_file *stream); 359 PyObject *gdbpy_get_varobj_pretty_printer (struct value *value); 360 char *gdbpy_get_display_hint (PyObject *printer); 361 PyObject *gdbpy_default_visualizer (PyObject *self, PyObject *args); 362 363 void bpfinishpy_pre_stop_hook (struct breakpoint_object *bp_obj); 364 void bpfinishpy_post_stop_hook (struct breakpoint_object *bp_obj); 365 366 extern PyObject *gdbpy_doc_cst; 367 extern PyObject *gdbpy_children_cst; 368 extern PyObject *gdbpy_to_string_cst; 369 extern PyObject *gdbpy_display_hint_cst; 370 extern PyObject *gdbpy_enabled_cst; 371 extern PyObject *gdbpy_value_cst; 372 373 /* Exception types. */ 374 extern PyObject *gdbpy_gdb_error; 375 extern PyObject *gdbpy_gdb_memory_error; 376 extern PyObject *gdbpy_gdberror_exc; 377 378 extern PyObject *gdbpy_convert_exception (struct gdb_exception); 379 380 int get_addr_from_python (PyObject *obj, CORE_ADDR *addr); 381 382 PyObject *gdb_py_object_from_longest (LONGEST l); 383 PyObject *gdb_py_object_from_ulongest (ULONGEST l); 384 int gdb_py_int_as_long (PyObject *, long *); 385 386 PyObject *gdb_py_generic_dict (PyObject *self, void *closure); 387 388 #endif /* GDB_PYTHON_INTERNAL_H */ 389