GDB (xrefs)
Loading...
Searching...
No Matches
python-internal.h
Go to the documentation of this file.
1/* Gdb/Python header for private use by Python module.
2
3 Copyright (C) 2008-2023 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 PYTHON_PYTHON_INTERNAL_H
21#define PYTHON_PYTHON_INTERNAL_H
22
23#include "extension.h"
24#include "extension-priv.h"
25
26/* These WITH_* macros are defined by the CPython API checker that
27 comes with the Python plugin for GCC. See:
28 https://gcc-python-plugin.readthedocs.org/en/latest/cpychecker.html
29 The checker defines a WITH_ macro for each attribute it
30 exposes. Note that we intentionally do not use
31 'cpychecker_returns_borrowed_ref' -- that idiom is forbidden in
32 gdb. */
33
34#ifdef WITH_CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF_ATTRIBUTE
35#define CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF(ARG) \
36 __attribute__ ((cpychecker_type_object_for_typedef (ARG)))
37#else
38#define CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF(ARG)
39#endif
40
41#ifdef WITH_CPYCHECKER_SETS_EXCEPTION_ATTRIBUTE
42#define CPYCHECKER_SETS_EXCEPTION __attribute__ ((cpychecker_sets_exception))
43#else
44#define CPYCHECKER_SETS_EXCEPTION
45#endif
46
47#ifdef WITH_CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION_ATTRIBUTE
48#define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION \
49 __attribute__ ((cpychecker_negative_result_sets_exception))
50#else
51#define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
52#endif
53
54/* /usr/include/features.h on linux systems will define _POSIX_C_SOURCE
55 if it sees _GNU_SOURCE (which config.h will define).
56 pyconfig.h defines _POSIX_C_SOURCE to a different value than
57 /usr/include/features.h does causing compilation to fail.
58 To work around this, undef _POSIX_C_SOURCE before we include Python.h.
59
60 Same problem with _XOPEN_SOURCE. */
61#undef _POSIX_C_SOURCE
62#undef _XOPEN_SOURCE
63
64/* On sparc-solaris, /usr/include/sys/feature_tests.h defines
65 _FILE_OFFSET_BITS, which pyconfig.h also defines. Same work
66 around technique as above. */
67#undef _FILE_OFFSET_BITS
68
69/* A kludge to avoid redefinition of snprintf on Windows by pyerrors.h. */
70#if defined(_WIN32) && defined(HAVE_DECL_SNPRINTF)
71#define HAVE_SNPRINTF 1
72#endif
73
74/* Another kludge to avoid compilation errors because MinGW defines
75 'hypot' to '_hypot', but the C++ headers says "using ::hypot". */
76#ifdef __MINGW32__
77# define _hypot hypot
78#endif
79
80/* Request clean size types from Python. */
81#define PY_SSIZE_T_CLEAN
82
83/* Include the Python header files using angle brackets rather than
84 double quotes. On case-insensitive filesystems, this prevents us
85 from including our python/python.h header file. */
86#include <Python.h>
87#include <frameobject.h>
88#include "py-ref.h"
89
90#define Py_TPFLAGS_CHECKTYPES 0
91
92/* If Python.h does not define WITH_THREAD, then the various
93 GIL-related functions will not be defined. However,
94 PyGILState_STATE will be. */
95#ifndef WITH_THREAD
96#define PyGILState_Ensure() ((PyGILState_STATE) 0)
97#define PyGILState_Release(ARG) ((void)(ARG))
98#define PyEval_InitThreads()
99#define PyThreadState_Swap(ARG) ((void)(ARG))
100#define PyEval_ReleaseLock()
101#endif
102
103/* Python supplies HAVE_LONG_LONG and some `long long' support when it
104 is available. These defines let us handle the differences more
105 cleanly. */
106#ifdef HAVE_LONG_LONG
107
108#define GDB_PY_LL_ARG "L"
109#define GDB_PY_LLU_ARG "K"
110typedef PY_LONG_LONG gdb_py_longest;
111typedef unsigned PY_LONG_LONG gdb_py_ulongest;
112#define gdb_py_long_as_ulongest PyLong_AsUnsignedLongLong
113#define gdb_py_long_as_long_and_overflow PyLong_AsLongLongAndOverflow
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_as_ulongest PyLong_AsUnsignedLong
122#define gdb_py_long_as_long_and_overflow PyLong_AsLongAndOverflow
123
124#endif /* HAVE_LONG_LONG */
125
126#if PY_VERSION_HEX < 0x03020000
127typedef long Py_hash_t;
128#endif
129
130/* PyMem_RawMalloc appeared in Python 3.4. For earlier versions, we can just
131 fall back to PyMem_Malloc. */
132
133#if PY_VERSION_HEX < 0x03040000
134#define PyMem_RawMalloc PyMem_Malloc
135#endif
136
137/* PyObject_CallMethod's 'method' and 'format' parameters were missing
138 the 'const' qualifier before Python 3.4. Hence, we wrap the
139 function in our own version to avoid errors with string literals.
140 Note, this is a variadic template because PyObject_CallMethod is a
141 varargs function and Python doesn't have a "PyObject_VaCallMethod"
142 variant taking a va_list that we could defer to instead. */
143
144template<typename... Args>
145static inline PyObject *
146gdb_PyObject_CallMethod (PyObject *o, const char *method, const char *format,
147 Args... args) /* ARI: editCase function */
148{
149 return PyObject_CallMethod (o,
150 const_cast<char *> (method),
151 const_cast<char *> (format),
152 args...);
153}
154
155#undef PyObject_CallMethod
156#define PyObject_CallMethod gdb_PyObject_CallMethod
157
158/* The 'name' parameter of PyErr_NewException was missing the 'const'
159 qualifier in Python <= 3.4. Hence, we wrap it in a function to
160 avoid errors when compiled with -Werror. */
161
162static inline PyObject*
163gdb_PyErr_NewException (const char *name, PyObject *base, PyObject *dict)
164{
165 return PyErr_NewException (const_cast<char *> (name), base, dict);
166}
167
168#define PyErr_NewException gdb_PyErr_NewException
169
170/* PySys_GetObject's 'name' parameter was missing the 'const'
171 qualifier before Python 3.4. Hence, we wrap it in a function to
172 avoid errors when compiled with -Werror. */
173
174static inline PyObject *
176{
177 return PySys_GetObject (const_cast<char *> (name));
178}
179
180#define PySys_GetObject gdb_PySys_GetObject
181
182/* PySys_SetPath was deprecated in Python 3.11. Disable the deprecated
183 code for Python 3.10 and newer. */
184#if PY_VERSION_HEX < 0x030a0000
185
186/* PySys_SetPath's 'path' parameter was missing the 'const' qualifier
187 before Python 3.6. Hence, we wrap it in a function to avoid errors
188 when compiled with -Werror. */
189
190# define GDB_PYSYS_SETPATH_CHAR wchar_t
191
192static inline void
194{
195 PySys_SetPath (const_cast<GDB_PYSYS_SETPATH_CHAR *> (path));
196}
197
198#define PySys_SetPath gdb_PySys_SetPath
199#endif
200
201/* Wrap PyGetSetDef to allow convenient construction with string
202 literals. Unfortunately, PyGetSetDef's 'name' and 'doc' members
203 are 'char *' instead of 'const char *', meaning that in order to
204 list-initialize PyGetSetDef arrays with string literals (and
205 without the wrapping below) would require writing explicit 'char *'
206 casts. Instead, we extend PyGetSetDef and add constexpr
207 constructors that accept const 'name' and 'doc', hiding the ugly
208 casts here in a single place. */
209
210struct gdb_PyGetSetDef : PyGetSetDef
211{
212 constexpr gdb_PyGetSetDef (const char *name_, getter get_, setter set_,
213 const char *doc_, void *closure_)
214 : PyGetSetDef {const_cast<char *> (name_), get_, set_,
215 const_cast<char *> (doc_), closure_}
216 {}
217
218 /* Alternative constructor that allows omitting the closure in list
219 initialization. */
220 constexpr gdb_PyGetSetDef (const char *name_, getter get_, setter set_,
221 const char *doc_)
222 : gdb_PyGetSetDef {name_, get_, set_, doc_, NULL}
223 {}
224
225 /* Constructor for the sentinel entries. */
226 constexpr gdb_PyGetSetDef (std::nullptr_t)
227 : gdb_PyGetSetDef {NULL, NULL, NULL, NULL, NULL}
228 {}
229};
230
231/* The 'keywords' parameter of PyArg_ParseTupleAndKeywords has type
232 'char **'. However, string literals are const in C++, and so to
233 avoid casting at every keyword array definition, we'll need to make
234 the keywords array an array of 'const char *'. To avoid having all
235 callers add a 'const_cast<char **>' themselves when passing such an
236 array through 'char **', we define our own version of
237 PyArg_ParseTupleAndKeywords here with a corresponding 'keywords'
238 parameter type that does the cast in a single place. (This is not
239 an overload of PyArg_ParseTupleAndKeywords in order to make it
240 clearer that we're calling our own function instead of a function
241 that exists in some newer Python version.) */
242
243static inline int
245 const char *format, const char **keywords, ...)
246{
247 va_list ap;
248 int res;
249
250 va_start (ap, keywords);
251 res = PyArg_VaParseTupleAndKeywords (args, kw, format,
252 const_cast<char **> (keywords),
253 ap);
254 va_end (ap);
255
256 return res;
257}
258
259/* In order to be able to parse symtab_and_line_to_sal_object function
260 a real symtab_and_line structure is needed. */
261#include "symtab.h"
262
263/* Also needed to parse enum var_types. */
264#include "command.h"
265#include "breakpoint.h"
266
268
269struct block;
270struct value;
271struct language_defn;
272struct program_space;
273struct bpstat;
274struct inferior;
275
276extern int gdb_python_initialized;
277
278extern PyObject *gdb_module;
280extern PyTypeObject value_object_type
282extern PyTypeObject block_object_type
284extern PyTypeObject symbol_object_type
286extern PyTypeObject event_object_type
288extern PyTypeObject breakpoint_object_type
290extern PyTypeObject frame_object_type
292extern PyTypeObject thread_object_type
294
295/* Ensure that breakpoint_object_type is initialized and return true. If
296 breakpoint_object_type can't be initialized then set a suitable Python
297 error and return false.
298
299 This function needs to be called from any gdbpy_initialize_* function
300 that wants to reference breakpoint_object_type. After all the
301 gdbpy_initialize_* functions have been called then breakpoint_object_type
302 is guaranteed to have been initialized, and this function does not need
303 calling before referencing breakpoint_object_type. */
304
306
308{
309 PyObject_HEAD
310
311 /* The breakpoint number according to gdb. */
313
314 /* The gdb breakpoint object, or NULL if the breakpoint has been
315 deleted. */
316 struct breakpoint *bp;
317
318 /* 1 is this is a FinishBreakpoint object, 0 otherwise. */
320};
321
322/* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
323 exception if it is invalid. */
324#define BPPY_REQUIRE_VALID(Breakpoint) \
325 do { \
326 if ((Breakpoint)->bp == NULL) \
327 return PyErr_Format (PyExc_RuntimeError, \
328 _("Breakpoint %d is invalid."), \
329 (Breakpoint)->number); \
330 } while (0)
331
332/* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
333 exception if it is invalid. This macro is for use in setter functions. */
334#define BPPY_SET_REQUIRE_VALID(Breakpoint) \
335 do { \
336 if ((Breakpoint)->bp == NULL) \
337 { \
338 PyErr_Format (PyExc_RuntimeError, _("Breakpoint %d is invalid."), \
339 (Breakpoint)->number); \
340 return -1; \
341 } \
342 } while (0)
343
344
345/* Variables used to pass information between the Breakpoint
346 constructor and the breakpoint-created hook function. */
348
349
351{
352 PyObject_HEAD
353
354 /* The thread we represent. */
356
357 /* The Inferior object to which this thread belongs. */
359};
360
361struct inferior_object;
362
363extern struct cmd_list_element *set_python_list;
365
366/* extension_language_script_ops "methods". */
367
368/* Return true if auto-loading Python scripts is enabled.
369 This is the extension_language_script_ops.auto_load_enabled "method". */
370
371extern bool gdbpy_auto_load_enabled (const struct extension_language_defn *);
372
373/* extension_language_ops "methods". */
374
376 (const struct extension_language_defn *,
377 struct value *value,
378 struct ui_file *stream, int recurse,
379 const struct value_print_options *options,
380 const struct language_defn *language);
382 (const struct extension_language_defn *,
383 frame_info_ptr frame, frame_filter_flags flags,
384 enum ext_lang_frame_args args_type,
385 struct ui_out *out, int frame_low, int frame_high);
386extern void gdbpy_preserve_values (const struct extension_language_defn *,
387 struct objfile *objfile,
388 htab_t copied_types);
390 (const struct extension_language_defn *, struct breakpoint *);
391extern int gdbpy_breakpoint_has_cond (const struct extension_language_defn *,
392 struct breakpoint *b);
393
395 (const struct extension_language_defn *extlang,
396 struct type *obj_type, const char *method_name,
397 std::vector<xmethod_worker_up> *dm_vec);
398
399
402extern PyObject *gdbpy_history_count (PyObject *self, PyObject *args);
409 PyObject *kw);
411 PyObject *kw);
413 PyObject *kw);
420int gdbpy_is_field (PyObject *obj);
421PyObject *gdbpy_create_lazy_string_object (CORE_ADDR address, long length,
422 const char *encoding,
423 struct type *type);
424PyObject *gdbpy_inferiors (PyObject *unused, PyObject *unused2);
430gdb::unique_xmalloc_ptr<char> gdbpy_parse_command_name
431 (const char *name, struct cmd_list_element ***base_list,
432 struct cmd_list_element **start_list);
434 PyObject *kw);
435
440 struct objfile *objfile);
450
457
460
462 const char *group_name);
464
468
469PyObject *gdbpy_buffer_to_membuf (gdb::unique_xmalloc_ptr<gdb_byte> buffer,
470 CORE_ADDR address, ULONGEST length);
471
475
476const struct block *block_object_to_block (PyObject *obj);
478struct value *value_object_to_value (PyObject *self);
480struct type *type_object_to_type (PyObject *obj);
485
487 PyObject *kw);
488
489/* Convert Python object OBJ to a program_space pointer. OBJ must be a
490 gdb.Progspace reference. Return nullptr if the gdb.Progspace is not
491 valid (see gdb.Progspace.is_valid), otherwise return the program_space
492 pointer. */
493
495
496/* A class for managing the initialization, and finalization functions
497 from all Python files (e.g. gdb/python/py-*.c).
498
499 Within any Python file, create an instance of this class, passing in
500 the initialization function, and, optionally, the finalization
501 function.
502
503 These functions are added to a single global list of functions, which
504 can then be called from do_start_initialization and finalize_python
505 (see python.c) to initialize all the Python files within GDB. */
506
508{
509 /* The type of a function that can be called just after GDB has setup the
510 Python interpreter. This function will setup any additional Python
511 state required by a particular subsystem. Return 0 if the setup was
512 successful, or return -1 if setup failed, in which case a Python
513 exception should have been raised. */
514
515 using gdbpy_initialize_file_ftype = int (*) (void);
516
517 /* The type of a function that can be called just before GDB shuts down
518 the Python interpreter. This function can cleanup an Python state
519 that is cached within GDB, for example, if GDB is holding any
520 references to Python objects, these should be released before the
521 Python interpreter is shut down.
522
523 There is no error return in this case. This function is only called
524 when GDB is already shutting down. The function should make a best
525 effort to clean up, and then return. */
526
527 using gdbpy_finalize_file_ftype = void (*) (void);
528
529 /* The type for an initialization and finalization function pair. */
530
533
534 /* Return the vector of callbacks. The vector is defined as a static
535 variable within this function so that it will be initialized the first
536 time this function is called. This is important, as this function is
537 called as part of the global object initialization process; if the
538 vector was a static variable within this class then we could not
539 guarantee that it had been initialized before it was used. */
540
541 static std::vector<callback_pair_t> &
543 {
544 static std::vector<callback_pair_t> list;
545 return list;
546 }
547
548public:
549
550 /* Register the initialization (INIT) and finalization (FINI) functions
551 for a Python file. See the comments on the function types above for
552 when these functions will be called.
553
554 Either of these functions can be nullptr, in which case no function
555 will be called.
556
557 The FINI argument is optional, and defaults to nullptr (no function to
558 call). */
559
561 gdbpy_finalize_file_ftype fini = nullptr)
562 {
563 callbacks ().emplace_back (init, fini);
564 }
565
566 /* Run all the Python file initialize functions and return true. If any
567 of the initialize functions fails then this function returns false.
568 In the case of failure it is undefined how many of the initialize
569 functions will have been called. */
570
571 static bool
573 {
574 /* The initialize_all function should only be called once. The
575 following check reverses the global list, which will effect this
576 initialize_all call, as well as the later finalize_all call.
577
578 The environment variable checked here is the same as the one checked
579 in the generated init.c file. */
580 if (getenv ("GDB_REVERSE_INIT_FUNCTIONS") != nullptr)
581 std::reverse (callbacks ().begin (), callbacks ().end ());
582
583 for (const auto &p : gdbpy_initialize_file::callbacks ())
584 {
585 if (p.first != nullptr && p.first () < 0)
586 return false;
587 }
588 return true;
589 }
590
591 /* Run all the Python file finalize functions. */
592
593 static void
595 {
596 for (const auto &p : gdbpy_initialize_file::callbacks ())
597 {
598 if (p.second != nullptr)
599 p.second ();
600 }
601 }
602};
603
604/* Macro to simplify registering the initialization and finalization
605 functions for a Python file. */
606
607#define GDBPY_INITIALIZE_FILE(INIT, ...) \
608 static gdbpy_initialize_file \
609 CONCAT(gdbpy_initialize_file_obj_, __LINE__) (INIT, ##__VA_ARGS__)
610
611PyMODINIT_FUNC gdbpy_events_mod_func ();
612
613/* A wrapper for PyErr_Fetch that handles reference counting for the
614 caller. */
616{
617public:
618
620 {
621 PyObject *error_type, *error_value, *error_traceback;
622
623 PyErr_Fetch (&error_type, &error_value, &error_traceback);
624 m_error_type.reset (error_type);
625 m_error_value.reset (error_value);
626 m_error_traceback.reset (error_traceback);
627 }
628
629 /* Call PyErr_Restore using the values stashed in this object.
630 After this call, this object is invalid and neither the to_string
631 nor restore methods may be used again. */
632
633 void restore ()
634 {
635 PyErr_Restore (m_error_type.release (),
636 m_error_value.release (),
637 m_error_traceback.release ());
638 }
639
640 /* Return the string representation of the exception represented by
641 this object. If the result is NULL a python error occurred, the
642 caller must clear it. */
643
644 gdb::unique_xmalloc_ptr<char> to_string () const;
645
646 /* Return the string representation of the type of the exception
647 represented by this object. If the result is NULL a python error
648 occurred, the caller must clear it. */
649
650 gdb::unique_xmalloc_ptr<char> type_to_string () const;
651
652 /* Return true if the stored type matches TYPE, false otherwise. */
653
655 {
656 return PyErr_GivenExceptionMatches (m_error_type.get (), type);
657 }
658
659 /* Return a new reference to the exception value object. */
660
662 {
663 return m_error_value;
664 }
665
666private:
667
669};
670
671/* Called before entering the Python interpreter to install the
672 current language and architecture to be used for Python values.
673 Also set the active extension language for GDB so that SIGINT's
674 are directed our way, and if necessary install the right SIGINT
675 handler. */
677{
678 public:
679
680 /* Set the ambient Python architecture to GDBARCH and the language
681 to LANGUAGE. If GDBARCH is nullptr, then the architecture will
682 be computed, when needed, using get_current_arch; see the
683 get_gdbarch method. If LANGUAGE is not nullptr, then the current
684 language at time of construction will be saved (to be restored on
685 destruction), and the current language will be set to
686 LANGUAGE. */
687 explicit gdbpy_enter (struct gdbarch *gdbarch = nullptr,
688 const struct language_defn *language = nullptr);
689
691
693
694 /* Return the current gdbarch, as known to the Python layer. This
695 is either python_gdbarch (which comes from the most recent call
696 to the gdbpy_enter constructor), or, if that is nullptr, the
697 result of get_current_arch. */
698 static struct gdbarch *get_gdbarch ();
699
700 /* Called only during gdb shutdown. This sets python_gdbarch to an
701 acceptable value. */
702 static void finalize ();
703
704 private:
705
706 /* The current gdbarch, according to Python. This can be
707 nullptr. */
708 static struct gdbarch *python_gdbarch;
709
711 PyGILState_STATE m_state;
714
715 /* An optional is used here because we don't want to call
716 PyErr_Fetch too early. */
717 gdb::optional<gdbpy_err_fetch> m_error;
718};
719
720/* Like gdbpy_enter, but takes a varobj. This is a subclass just to
721 make constructor delegation a little nicer. */
723{
724 public:
725
726 /* This is defined in varobj.c, where it can access varobj
727 internals. */
728 gdbpy_enter_varobj (const struct varobj *var);
729
730};
731
732/* The opposite of gdb_enter: this releases the GIL around a region,
733 allowing other Python threads to run. No Python APIs may be used
734 while this is active. */
736{
737public:
738
740 : m_save (PyEval_SaveThread ())
741 {
742 gdb_assert (m_save != nullptr);
743 }
744
746 {
747 PyEval_RestoreThread (m_save);
748 }
749
751
752private:
753
754 PyThreadState *m_save;
755};
756
757/* Use this after a TRY_EXCEPT to throw the appropriate Python
758 exception. */
759#define GDB_PY_HANDLE_EXCEPTION(Exception) \
760 do { \
761 if (Exception.reason < 0) \
762 { \
763 gdbpy_convert_exception (Exception); \
764 return NULL; \
765 } \
766 } while (0)
767
768/* Use this after a TRY_EXCEPT to throw the appropriate Python
769 exception. This macro is for use inside setter functions. */
770#define GDB_PY_SET_HANDLE_EXCEPTION(Exception) \
771 do { \
772 if (Exception.reason < 0) \
773 { \
774 gdbpy_convert_exception (Exception); \
775 return -1; \
776 } \
777 } while (0)
778
782void gdbpy_handle_exception () ATTRIBUTE_NORETURN;
783
784/* A wrapper around calling 'error'. Prefixes the error message with an
785 'Error occurred in Python' string. Use this in C++ code if we spot
786 something wrong with an object returned from Python code. The prefix
787 string gives the user a hint that the mistake is within Python code,
788 rather than some other part of GDB.
789
790 This always calls error, and never returns. */
791
792void gdbpy_error (const char *fmt, ...)
793 ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2);
794
796gdb::unique_xmalloc_ptr<char> unicode_to_target_string (PyObject *unicode_str);
797gdb::unique_xmalloc_ptr<char> python_string_to_target_string (PyObject *obj);
799gdb::unique_xmalloc_ptr<char> python_string_to_host_string (PyObject *obj);
800gdbpy_ref<> host_string_to_python_string (const char *str);
801int gdbpy_is_string (PyObject *obj);
802gdb::unique_xmalloc_ptr<char> gdbpy_obj_to_string (PyObject *obj);
803
804int gdbpy_is_lazy_string (PyObject *result);
805void gdbpy_extract_lazy_string (PyObject *string, CORE_ADDR *addr,
806 struct type **str_type,
807 long *length,
808 gdb::unique_xmalloc_ptr<char> *encoding);
809
811
812/* Note that these are declared here, and not in python.h with the
813 other pretty-printer functions, because they refer to PyObject. */
815 struct value **replacement,
816 struct ui_file *stream,
817 const value_print_options *opts);
819gdb::unique_xmalloc_ptr<char> gdbpy_get_display_hint (PyObject *printer);
821
825
829
830extern PyObject *gdbpy_doc_cst;
836
837/* Exception types. */
841
842extern void gdbpy_convert_exception (const struct gdb_exception &)
844
845int get_addr_from_python (PyObject *obj, CORE_ADDR *addr)
847
850int gdb_py_int_as_long (PyObject *, long *);
851
852PyObject *gdb_py_generic_dict (PyObject *self, void *closure);
853
854int gdb_pymodule_addobject (PyObject *module, const char *name,
855 PyObject *object)
857
858struct varobj_iter;
859struct varobj;
861 (struct varobj *var,
862 PyObject *printer,
863 const value_print_options *opts);
864
865/* Deleter for Py_buffer unique_ptr specialization. */
866
868{
869 void operator() (Py_buffer *b) const
870 {
871 PyBuffer_Release (b);
872 }
873};
874
875/* A unique_ptr specialization for Py_buffer. */
876typedef std::unique_ptr<Py_buffer, Py_buffer_deleter> Py_buffer_up;
877
878/* Parse a register number from PYO_REG_ID and place the register number
879 into *REG_NUM. The register is a register for GDBARCH.
880
881 If a register is parsed successfully then *REG_NUM will have been
882 updated, and true is returned. Otherwise the contents of *REG_NUM are
883 undefined, and false is returned. When false is returned, the
884 Python error is set.
885
886 The PYO_REG_ID object can be a string, the name of the register. This
887 is the slowest approach as GDB has to map the name to a number for each
888 call. Alternatively PYO_REG_ID can be an internal GDB register
889 number. This is quick but should not be encouraged as this means
890 Python scripts are now dependent on GDB's internal register numbering.
891 Final PYO_REG_ID can be a gdb.RegisterDescriptor object, these objects
892 can be looked up by name once, and then cache the register number so
893 should be as quick as using a register number. */
894
895extern bool gdbpy_parse_register_id (struct gdbarch *gdbarch,
896 PyObject *pyo_reg_id, int *reg_num);
897
898/* Return true if OBJ is a gdb.Architecture object, otherwise, return
899 false. */
900
901extern bool gdbpy_is_architecture (PyObject *obj);
902
903/* Return true if OBJ is a gdb.Progspace object, otherwise, return false. */
904
905extern bool gdbpy_is_progspace (PyObject *obj);
906
907/* Take DOC, the documentation string for a GDB command defined in Python,
908 and return an (possibly) modified version of that same string.
909
910 When a command is defined in Python, the documentation string will
911 usually be indented based on the indentation of the surrounding Python
912 code. However, the documentation string is a literal string, all the
913 white-space added for indentation is included within the documentation
914 string.
915
916 This indentation is then included in the help text that GDB displays,
917 which looks odd out of the context of the original Python source code.
918
919 This function analyses DOC and tries to figure out what white-space
920 within DOC was added as part of the indentation, and then removes that
921 white-space from the copy that is returned.
922
923 If the analysis of DOC fails then DOC will be returned unmodified. */
924
925extern gdb::unique_xmalloc_ptr<char> gdbpy_fix_doc_string_indentation
926 (gdb::unique_xmalloc_ptr<char> doc);
927
928/* Implement the 'print_insn' hook for Python. Disassemble an instruction
929 whose address is ADDRESS for architecture GDBARCH. The bytes of the
930 instruction should be read with INFO->read_memory_func as the
931 instruction being disassembled might actually be in a buffer.
932
933 Used INFO->fprintf_func to print the results of the disassembly, and
934 return the length of the instruction in octets.
935
936 If no instruction can be disassembled then return an empty value. */
937
938extern gdb::optional<int> gdbpy_print_insn (struct gdbarch *gdbarch,
939 CORE_ADDR address,
940 disassemble_info *info);
941
942#endif /* PYTHON_PYTHON_INTERNAL_H */
const char *const name
DISABLE_COPY_AND_ASSIGN(gdbpy_allow_threads)
PyThreadState * m_save
gdbpy_enter_varobj(const struct varobj *var)
static struct gdbarch * python_gdbarch
PyGILState_STATE m_state
const struct language_defn * m_language
gdb::optional< gdbpy_err_fetch > m_error
static struct gdbarch * get_gdbarch()
gdbpy_enter(struct gdbarch *gdbarch=nullptr, const struct language_defn *language=nullptr)
static void finalize()
struct gdbarch * m_gdbarch
struct active_ext_lang_state * m_previous_active
DISABLE_COPY_AND_ASSIGN(gdbpy_enter)
bool type_matches(PyObject *type) const
gdbpy_ref m_error_traceback
gdb::unique_xmalloc_ptr< char > type_to_string() const
Definition py-utils.c:207
gdb::unique_xmalloc_ptr< char > to_string() const
Definition py-utils.c:186
gdbpy_ref m_error_value
gdbpy_initialize_file(gdbpy_initialize_file_ftype init, gdbpy_finalize_file_ftype fini=nullptr)
std::pair< gdbpy_initialize_file_ftype, gdbpy_finalize_file_ftype > callback_pair_t
void(*)(void) gdbpy_finalize_file_ftype
static std::vector< callback_pair_t > & callbacks()
int(*)(void) gdbpy_initialize_file_ftype
static bool initialize_all()
language
Definition defs.h:211
ext_lang_rc
Definition extension.h:165
ext_lang_bt_status
Definition extension.h:71
ext_lang_frame_args
Definition extension.h:114
ext_lang_bp_stop
Definition extension.h:138
static void ATTRIBUTE_PRINTF(1, 0)
Definition gdb_bfd.c:1154
mach_port_t kern_return_t mach_port_t mach_msg_type_name_t msgportsPoly mach_port_t kern_return_t pid_t pid mach_port_t kern_return_t mach_port_t task mach_port_t kern_return_t int flags
Definition gnu-nat.c:1861
Definition aarch64.h:67
PyTypeObject block_object_type
Definition py-block.c:510
PyTypeObject breakpoint_object_type
PyTypeObject event_object_type
Definition py-event.c:126
PyTypeObject frame_object_type
Definition py-frame.c:832
PyTypeObject thread_object_type
gdb::ref_ptr< T, gdbpy_ref_policy< T > > gdbpy_ref
Definition py-ref.h:42
PyTypeObject symbol_object_type
Definition py-symbol.c:750
PyTypeObject value_object_type
Definition py-value.c:2244
PyObject * gdbpy_buffer_to_membuf(gdb::unique_xmalloc_ptr< gdb_byte > buffer, CORE_ADDR address, ULONGEST length)
Definition py-membuf.c:44
enum ext_lang_bp_stop gdbpy_breakpoint_cond_says_stop(const struct extension_language_defn *, struct breakpoint *)
void gdbpy_print_stack(void)
bool gdbpy_is_progspace(PyObject *obj)
void gdbpy_ref python_string_to_unicode(PyObject *obj)
Definition py-utils.c:38
PyObject * gdbpy_new_register_descriptor_iterator(struct gdbarch *gdbarch, const char *group_name)
PyMODINIT_FUNC gdbpy_events_mod_func()
Definition py-evts.c:56
struct value * convert_value_from_python(PyObject *obj)
Definition py-value.c:1892
PyObject * gdbpy_all_architecture_names(PyObject *self, PyObject *args)
Definition py-arch.c:341
struct symbol * symbol_object_to_symbol(PyObject *obj)
Definition py-symbol.c:357
bool gdbpy_breakpoint_init_breakpoint_type()
gdbpy_ref host_string_to_python_string(const char *str)
Definition py-utils.c:154
PyObject * symtab_to_linetable_object(PyObject *symtab)
PyObject * block_to_block_object(const struct block *block, struct objfile *objfile)
Definition py-block.c:323
#define GDB_PYSYS_SETPATH_CHAR
PyObject * objfpy_get_xmethods(PyObject *, void *)
Definition py-objfile.c:383
PyObject * pspy_get_printers(PyObject *, void *)
void gdbpy_convert_exception(const struct gdb_exception &) CPYCHECKER_SETS_EXCEPTION
Definition py-utils.c:217
gdb::unique_xmalloc_ptr< char > gdbpy_fix_doc_string_indentation(gdb::unique_xmalloc_ptr< char > doc)
Definition py-utils.c:410
#define PyObject_CallMethod
int gdbpy_breakpoint_has_cond(const struct extension_language_defn *, struct breakpoint *b)
struct symtab * symtab_object_to_symtab(PyObject *obj)
Definition py-symtab.c:505
gdbpy_ref< inferior_object > inferior_to_inferior_object(inferior *inf)
void gdbpy_handle_exception() ATTRIBUTE_NORETURN
Definition py-utils.c:369
int gdbpy_print_python_errors_p(void)
gdb::unique_xmalloc_ptr< char > python_string_to_target_string(PyObject *obj)
Definition py-utils.c:114
gdbpy_ref apply_varobj_pretty_printer(PyObject *print_obj, struct value **replacement, struct ui_file *stream, const value_print_options *opts)
PyObject * symbol_to_symbol_object(struct symbol *sym)
Definition py-symbol.c:344
enum ext_lang_rc gdbpy_get_matching_xmethod_workers(const struct extension_language_defn *extlang, struct type *obj_type, const char *method_name, std::vector< xmethod_worker_up > *dm_vec)
std::unique_ptr< Py_buffer, Py_buffer_deleter > Py_buffer_up
int gdbpy_is_value_object(PyObject *obj)
Definition py-value.c:2122
PyObject * gdbpy_lookup_static_symbol(PyObject *self, PyObject *args, PyObject *kw)
Definition py-symbol.c:510
PyObject * gdb_module
struct symtab_and_line * sal_object_to_symtab_and_line(PyObject *obj)
Definition py-symtab.c:496
int gdb_python_initialized
PyObject * gdbpy_stop_recording(PyObject *self, PyObject *args)
Definition py-record.c:640
PyObject * gdbpy_lookup_global_symbol(PyObject *self, PyObject *args, PyObject *kw)
Definition py-symbol.c:470
gdbpy_ref gdb_py_object_from_longest(LONGEST l)
Definition py-utils.c:282
PyObject * gdbpy_register_tui_window(PyObject *self, PyObject *args, PyObject *kw)
frame_info_ptr frame_object_to_frame_info(PyObject *frame_obj)
Definition py-frame.c:62
std::unique_ptr< varobj_iter > py_varobj_get_iterator(struct varobj *var, PyObject *printer, const value_print_options *opts)
Definition py-varobj.c:147
PyObject * gdbpy_lookup_objfile(PyObject *self, PyObject *args, PyObject *kw)
Definition py-objfile.c:591
PyObject * gdbpy_execute_mi_command(PyObject *self, PyObject *args, PyObject *kw)
Definition py-mi.c:258
PyObject * pspy_get_xmethods(PyObject *, void *)
gdbpy_ref objfile_to_objfile_object(struct objfile *)
Definition py-objfile.c:686
void bpfinishpy_post_stop_hook(struct gdbpy_breakpoint_object *bp_obj)
PyObject * symtab_to_symtab_object(struct symtab *symtab)
Definition py-symtab.c:467
gdb::unique_xmalloc_ptr< char > gdbpy_parse_command_name(const char *name, struct cmd_list_element ***base_list, struct cmd_list_element **start_list)
Definition py-cmd.c:347
PyObject * symtab_and_line_to_sal_object(struct symtab_and_line sal)
Definition py-symtab.c:481
long gdb_py_longest
struct cmd_list_element * show_python_list
#define CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF(ARG)
struct value * value_object_to_value(PyObject *self)
Definition py-value.c:1877
#define PySys_SetPath
PyObject * gdbpy_children_cst
PyObject * frame_info_to_frame_object(frame_info_ptr frame)
Definition py-frame.c:346
void gdbpy_extract_lazy_string(PyObject *string, CORE_ADDR *addr, struct type **str_type, long *length, gdb::unique_xmalloc_ptr< char > *encoding)
gdbpy_iter_kind
@ iter_values
@ iter_items
@ iter_keys
void gdbpy_print_stack_or_quit()
int gdbpy_is_lazy_string(PyObject *result)
PyObject * gdbpy_add_history(PyObject *self, PyObject *args)
Definition py-value.c:2004
#define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
PyObject * gdbpy_create_lazy_string_object(CORE_ADDR address, long length, const char *encoding, struct type *type)
gdb::unique_xmalloc_ptr< char > gdbpy_get_display_hint(PyObject *printer)
PyObject * gdbpy_string_to_argv(PyObject *self, PyObject *args)
Definition py-cmd.c:667
PyObject * gdbpy_selected_inferior(PyObject *self, PyObject *args)
#define CPYCHECKER_SETS_EXCEPTION
void bpfinishpy_pre_delete_hook(struct gdbpy_breakpoint_object *bp_obj)
PyObject * gdbpy_default_visualizer(PyObject *self, PyObject *args)
bool gdbpy_is_architecture(PyObject *obj)
Definition py-arch.c:80
PyObject * gdbpy_selected_frame(PyObject *self, PyObject *args)
Definition py-frame.c:673
gdb::unique_xmalloc_ptr< char > gdbpy_obj_to_string(PyObject *obj)
Definition py-utils.c:173
PyObject * gdbpy_history_count(PyObject *self, PyObject *args)
Definition py-value.c:2031
PyObject * gdbpy_gdb_memory_error
PyObject * gdbpy_display_hint_cst
unsigned long gdb_py_ulongest
long Py_hash_t
PyObject * gdbpy_parameter_value(const setting &var)
PyObject * gdbpy_create_ptid_object(ptid_t ptid)
PyObject * gdbpy_start_recording(PyObject *self, PyObject *args)
Definition py-record.c:598
PyObject * gdbpy_selected_thread(PyObject *self, PyObject *args)
PyObject * gdbpy_new_reggroup_iterator(struct gdbarch *gdbarch)
PyObject * objfpy_get_frame_filters(PyObject *, void *)
Definition py-objfile.c:293
struct gdbarch * arch_object_to_gdbarch(PyObject *obj)
Definition py-arch.c:69
PyObject * gdbpy_lookup_symbol(PyObject *self, PyObject *args, PyObject *kw)
Definition py-symbol.c:401
gdb::unique_xmalloc_ptr< char > python_string_to_host_string(PyObject *obj)
Definition py-utils.c:142
int gdb_pymodule_addobject(PyObject *module, const char *name, PyObject *object) CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
Definition py-utils.c:334
void gdbpy_error(const char *fmt,...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF(1
gdbpy_ref target_to_connection_object(process_stratum_target *target)
PyObject * gdbpy_newest_frame(PyObject *self, PyObject *args)
Definition py-frame.c:653
#define PySys_GetObject
PyObject * gdbpy_history(PyObject *self, PyObject *args)
Definition py-value.c:1979
PyObject * objfpy_get_frame_unwinders(PyObject *, void *)
Definition py-objfile.c:332
PyObject * gdbpy_set_convenience_variable(PyObject *self, PyObject *args)
Definition py-value.c:2078
PyObject * gdbpy_gdb_error
PyObject * gdbpy_current_recording(PyObject *self, PyObject *args)
Definition py-record.c:623
gdbpy_breakpoint_object * bppy_pending_object
int gdb_py_int_as_long(PyObject *, long *)
Definition py-utils.c:304
void gdbpy_preserve_values(const struct extension_language_defn *, struct objfile *objfile, htab_t copied_types)
Definition py-value.c:224
enum ext_lang_rc gdbpy_apply_val_pretty_printer(const struct extension_language_defn *, struct value *value, struct ui_file *stream, int recurse, const struct value_print_options *options, const struct language_defn *language)
PyObject * value_to_value_object(struct value *v)
Definition py-value.c:1854
gdbpy_ref pspace_to_pspace_object(struct program_space *)
PyObject * gdbpy_enabled_cst
PyObject * pspy_get_frame_unwinders(PyObject *, void *)
PyObject * gdbpy_lookup_static_symbols(PyObject *self, PyObject *args, PyObject *kw)
Definition py-symbol.c:579
PyObject * gdbpy_doc_cst
bool gdbpy_parse_register_id(struct gdbarch *gdbarch, PyObject *pyo_reg_id, int *reg_num)
PyObject * gdbpy_frame_stop_reason_string(PyObject *, PyObject *)
Definition py-frame.c:693
enum ext_lang_bt_status gdbpy_apply_frame_filter(const struct extension_language_defn *, frame_info_ptr frame, frame_filter_flags flags, enum ext_lang_frame_args args_type, struct ui_out *out, int frame_low, int frame_high)
gdbpy_ref thread_to_thread_object(thread_info *thr)
const struct block * block_object_to_block(PyObject *obj)
Definition py-block.c:336
PyObject * gdbpy_print_options(PyObject *self, PyObject *args)
gdbpy_ref gdbpy_get_varobj_pretty_printer(struct value *value)
PyObject * pspy_get_frame_filters(PyObject *, void *)
PyObject * gdbpy_to_string_cst
int gdbpy_is_field(PyObject *obj)
Definition py-type.c:126
PyObject * objfpy_get_printers(PyObject *, void *)
Definition py-objfile.c:255
const struct value_print_options * gdbpy_current_print_options
gdbpy_ref python_string_to_target_python_string(PyObject *obj)
Definition py-utils.c:129
PyObject * gdb_python_module
PyObject * gdbpy_inferiors(PyObject *unused, PyObject *unused2)
struct program_space * progspace_object_to_program_space(PyObject *obj)
PyObject * gdb_py_generic_dict(PyObject *self, void *closure)
Definition py-utils.c:317
int get_addr_from_python(PyObject *obj, CORE_ADDR *addr) CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
Definition py-utils.c:239
PyObject * gdbarch_to_arch_object(struct gdbarch *gdbarch)
Definition py-arch.c:90
PyObject * gdbpy_value_cst
gdb::optional< int > gdbpy_print_insn(struct gdbarch *gdbarch, CORE_ADDR address, disassemble_info *info)
Definition py-disasm.c:1219
int gdbpy_is_string(PyObject *obj)
Definition py-utils.c:164
PyObject * gdbpy_convenience_variable(PyObject *self, PyObject *args)
Definition py-value.c:2038
static PyObject * gdb_PySys_GetObject(const char *name)
#define PyErr_NewException
PyObject * type_to_type_object(struct type *)
Definition py-type.c:1460
gdbpy_ref< thread_object > create_thread_object(struct thread_info *tp)
struct type * type_object_to_type(PyObject *obj)
Definition py-type.c:1483
bool gdbpy_auto_load_enabled(const struct extension_language_defn *)
void bpfinishpy_pre_stop_hook(struct gdbpy_breakpoint_object *bp_obj)
static PyObject * gdb_PyObject_CallMethod(PyObject *o, const char *method, const char *format, Args... args)
PyObject * gdbpy_lookup_type(PyObject *self, PyObject *args, PyObject *kw)
Definition py-type.c:1494
PyObject * gdbpy_gdberror_exc
static void gdb_PySys_SetPath(const GDB_PYSYS_SETPATH_CHAR *path)
gdbpy_ref gdb_py_object_from_ulongest(ULONGEST l)
Definition py-utils.c:293
static PyObject * gdb_PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
struct cmd_list_element * set_python_list
static int gdb_PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, const char **keywords,...)
gdb::unique_xmalloc_ptr< char > unicode_to_target_string(PyObject *unicode_str)
Definition py-utils.c:91
PyObject * gdbpy_connections(PyObject *self, PyObject *args)
PyObject * gdbpy_breakpoints(PyObject *, PyObject *)
void gdbpy_get_print_options(value_print_options *opts)
static struct type * error_type(const char **, struct objfile *)
Definition stabsread.c:1445
Definition block.h:109
gdb::optional< setting > var
Definition cli-decode.h:236
constexpr gdb_PyGetSetDef(std::nullptr_t)
constexpr gdb_PyGetSetDef(const char *name_, getter get_, setter set_, const char *doc_, void *closure_)
constexpr gdb_PyGetSetDef(const char *name_, getter get_, setter set_, const char *doc_)
struct breakpoint * bp
PyObject_HEAD int number
Definition gnu-nat.c:153
PyObject_HEAD struct thread_info * thread
PyObject * inf_obj
Definition value.h:130
int PyObject
Definition varobj.c:41