30#include "gdbsupport/event-loop.h"
31#include "readline/tilde.h"
38#include "gdbsupport/selftest.h"
71#include "gdbsupport/version.h"
83extern PyMethodDef python_GdbMethods[];
110static void gdbpy_eval_from_control_command
117 gdb::unique_xmalloc_ptr<char> *);
124static gdb::optional<std::string> gdbpy_colorize
125 (
const std::string &filename,
const std::string &contents);
126static gdb::optional<std::string> gdbpy_colorize_disasm
134 gdbpy_source_objfile_script,
135 gdbpy_execute_objfile_script,
146 gdbpy_eval_from_control_command,
148 gdbpy_start_type_printers,
149 gdbpy_apply_type_printers,
150 gdbpy_free_type_printers,
162 gdbpy_check_quit_flag,
164 gdbpy_before_prompt_hook,
170 gdbpy_colorize_disasm,
191 &python_extension_script_ops,
192 &python_extension_ops
207: m_gdbarch (python_gdbarch),
212 error (_(
"Python not initialized"));
229 if (PyErr_Occurred ())
233 warning (_(
"internal error: Unhandled Python exception"));
277 DISABLE_COPY_AND_ASSIGN (gdbpy_gil);
281 PyGILState_STATE m_state;
289 PyErr_SetInterrupt ();
301 return PyOS_InterruptOccurred ();
309eval_python_command (
const char *command)
313 m = PyImport_AddModule (
"__main__");
317 d = PyModule_GetDict (m);
320 gdbpy_ref<> v (PyRun_StringFlags (command, Py_single_input, d, d, NULL));
335 scoped_restore save_async = make_scoped_restore (&
current_ui->
async, 0);
337 arg = skip_spaces (arg);
343 std::string script = std::string (arg) +
"\n";
344 err = eval_python_command (script.c_str ());
355 error (_(
"Error while executing Python code."));
375python_run_simple_file (FILE *file,
const char *filename)
379 PyRun_SimpleFile (file, filename);
385 gdb::unique_xmalloc_ptr<char> full_path (tilde_expand (filename));
389 error (_(
"Installation error: gdb._execute_file function is missing"));
394 if (return_value ==
nullptr)
413 for (iter = l; iter; iter = iter->
next)
415 script += iter->
line;
431 error (_(
"Invalid \"python\" block structure."));
435 std::string script = compute_python_string (cmd->
body_list_0.get ());
436 ret = PyRun_SimpleString (script.c_str ());
438 error (_(
"Error while executing Python code."));
448 scoped_restore save_async = make_scoped_restore (&
current_ui->
async, 0);
450 arg = skip_spaces (arg);
453 if (PyRun_SimpleString (arg))
454 error (_(
"Error while executing Python code."));
482 str = var.
get<
const char *> ();
484 str = var.
get<std::string> ().c_str ();
491 if (var.
get<
bool> ())
515 ?
static_cast<LONGEST
> (var.
get<
unsigned int> ())
516 : static_cast<LONGEST> (var.
get<int> ()));
520 l->literal !=
nullptr;
524 if (strcmp (l->literal,
"unlimited") == 0)
528 && l->val.has_value ()
534 else if (l->val.has_value ())
543 (
static_cast<unsigned int> (
value)).release ();
547 (
static_cast<int> (
value)).release ();
551 return PyErr_Format (PyExc_RuntimeError,
552 _(
"Programmer error: unhandled type."));
565 if (! PyArg_ParseTuple (args,
"s", &arg))
568 std::string newarg = std::string (
"show ") + arg;
574 catch (
const gdb_exception &ex)
580 return PyErr_Format (PyExc_RuntimeError,
581 _(
"Could not find parameter `%s'."), arg);
583 if (!cmd->
var.has_value ())
584 return PyErr_Format (PyExc_RuntimeError,
585 _(
"`%s' is not a parameter."), arg);
597 return PyUnicode_Decode (cset, strlen (cset),
host_charset (), NULL);
607 return PyUnicode_Decode (cset, strlen (cset),
host_charset (), NULL);
617 return PyUnicode_Decode (cset, strlen (cset),
host_charset (), NULL);
628 static const char *keywords[] = {
"command",
"from_tty",
"to_string",
632 &PyBool_Type, &from_tty_obj,
633 &PyBool_Type, &to_string_obj))
636 bool from_tty =
false;
637 if (from_tty_obj !=
nullptr)
639 int cmp = PyObject_IsTrue (from_tty_obj);
642 from_tty = (cmp != 0);
646 if (to_string_obj !=
nullptr)
648 int cmp = PyObject_IsTrue (to_string_obj);
654 std::string to_string_res;
664 std::string arg_copy = arg;
666 char *save_ptr =
nullptr;
668 = [&] (std::string &buffer)
670 const char *result = strtok_r (first ? &arg_copy[0] :
nullptr,
682 scoped_restore save_uiout = make_scoped_restore (&
current_uiout);
699 catch (
const gdb_exception &except)
711 return PyUnicode_FromString (to_string_res.c_str ());
732 std::vector<symbol_search> symbols;
733 unsigned long count = 0;
737 unsigned int throttle = 0;
738 static const char *keywords[] = {
"regex",
"minsyms",
"throttle",
742 ®ex, &PyBool_Type,
743 &minsyms_p_obj, &throttle,
748 if (minsyms_p_obj != NULL)
750 int cmp = PyObject_IsTrue (minsyms_p_obj);
758 for (
const char *elem : spec.filenames)
759 xfree ((void *) elem);
771 if (symtab_list != NULL)
784 if (PyErr_Occurred ())
789 gdbpy_ref<> obj_name (PyObject_GetAttrString (next.get (),
792 if (obj_name == NULL)
796 if (obj_name == Py_None)
799 gdb::unique_xmalloc_ptr<char> filename =
802 if (filename == NULL)
807 spec.filenames.push_back (
nullptr);
808 spec.filenames.back () = filename.release ();
813 symbols = spec.search ();
822 if (p.msymbol.minsym != NULL)
826 if (p.symbol != NULL)
831 if (throttle != 0 && count > throttle)
833 PyErr_SetString (PyExc_RuntimeError,
834 _(
"Number of breakpoints exceeds throttled maximum."));
840 if (return_list == NULL)
848 std::string symbol_name;
852 if (p.msymbol.minsym != NULL)
855 if (p.msymbol.minsym == NULL)
862 symbol_name += p.symbol->linkage_name ();
865 symbol_name = p.msymbol.minsym->linkage_name ();
867 gdbpy_ref<> argList (Py_BuildValue(
"(s)", symbol_name.c_str ()));
877 if (PyList_Append (
return_list.get (), obj.get ()) == -1)
889 const char *arg = NULL;
894 if (! PyArg_ParseTuple (args,
"|s", &arg))
901 arg = skip_spaces (arg);
910 std::vector<symtab_and_line> decoded_sals;
912 gdb::array_view<symtab_and_line> sals;
917 decoded_sals =
decode_line_1 (locspec.get (), 0, NULL, NULL, 0);
927 catch (
const gdb_exception &ex)
936 result.reset (PyTuple_New (sals.size ()));
939 for (
size_t i = 0; i < sals.size (); ++i)
945 PyTuple_SetItem (result.get (), i, obj);
952 if (return_result == NULL)
955 if (arg != NULL && strlen (arg) > 0)
957 unparsed.reset (PyUnicode_FromString (arg));
958 if (unparsed == NULL)
964 PyTuple_SetItem (return_result.get (), 0, unparsed.release ());
965 PyTuple_SetItem (return_result.get (), 1, result.release ());
967 return return_result.release ();
974 static const char *keywords[] = {
"expression",
"global_context",
nullptr };
976 const char *expr_str;
977 PyObject *global_context_obj =
nullptr;
981 &PyBool_Type, &global_context_obj))
984 parser_flags
flags = 0;
985 if (global_context_obj != NULL)
987 int cmp = PyObject_IsTrue (global_context_obj);
1010 catch (
const gdb_exception &except)
1035 FILE *file,
const char *filename)
1038 python_run_simple_file (file, filename);
1049 : m_func (
func.release ())
1053 gdbpy_event (gdbpy_event &&other) noexcept
1054 : m_func (other.m_func)
1056 other.m_func =
nullptr;
1059 gdbpy_event (
const gdbpy_event &other)
1060 : m_func (other.m_func)
1063 Py_XINCREF (m_func);
1069 Py_XDECREF (m_func);
1072 gdbpy_event &operator= (
const gdbpy_event &other) =
delete;
1078 gdbpy_ref<> call_result (PyObject_CallObject (m_func, NULL));
1079 if (call_result == NULL)
1097 if (!PyArg_ParseTuple (args,
"O", &
func))
1100 if (!PyCallable_Check (
func))
1102 PyErr_SetString (PyExc_RuntimeError,
1103 _(
"Posted event is not callable"));
1108 gdbpy_event event (std::move (func_ref));
1120 const char *current_gdb_prompt)
1142 if (PyCallable_Check (hook.get ()))
1144 gdbpy_ref<> current_prompt (PyUnicode_FromString (current_gdb_prompt));
1145 if (current_prompt == NULL)
1152 (PyObject_CallFunctionObjArgs (hook.get (), current_prompt.get (),
1163 if (result != Py_None && !PyUnicode_Check (result.get ()))
1165 PyErr_Format (PyExc_RuntimeError,
1166 _(
"Return from prompt_hook must " \
1167 "be either a Python string, or None"));
1172 if (result != Py_None)
1174 gdb::unique_xmalloc_ptr<char>
1194static gdb::optional<std::string>
1195gdbpy_colorize (
const std::string &filename,
const std::string &contents)
1202 gdbpy_ref<> module (PyImport_ImportModule (
"gdb.styling"));
1203 if (module ==
nullptr)
1209 if (!PyObject_HasAttrString (module.get (),
"colorize"))
1212 gdbpy_ref<> hook (PyObject_GetAttrString (module.get (),
"colorize"));
1213 if (hook ==
nullptr)
1219 if (!PyCallable_Check (hook.get ()))
1222 gdbpy_ref<> fname_arg (PyUnicode_FromString (filename.c_str ()));
1223 if (fname_arg ==
nullptr)
1234 gdbpy_ref<> contents_arg (PyBytes_FromStringAndSize (contents.c_str (),
1236 if (contents_arg ==
nullptr)
1246 gdbpy_ref<> result (PyObject_CallFunctionObjArgs (hook.get (),
1248 contents_arg.get (),
1250 if (result ==
nullptr)
1256 if (result == Py_None)
1258 else if (!PyBytes_Check (result.get ()))
1260 PyErr_SetString (PyExc_TypeError,
1261 _(
"Return value from gdb.colorize should be a bytes object or None."));
1266 return std::string (PyBytes_AsString (result.get ()));
1271static gdb::optional<std::string>
1272gdbpy_colorize_disasm (
const std::string &content,
gdbarch *
gdbarch)
1279 gdbpy_ref<> module (PyImport_ImportModule (
"gdb.styling"));
1280 if (module ==
nullptr)
1286 if (!PyObject_HasAttrString (module.get (),
"colorize_disasm"))
1289 gdbpy_ref<> hook (PyObject_GetAttrString (module.get (),
1290 "colorize_disasm"));
1291 if (hook ==
nullptr)
1297 if (!PyCallable_Check (hook.get ()))
1300 gdbpy_ref<> content_arg (PyBytes_FromString (content.c_str ()));
1301 if (content_arg ==
nullptr)
1308 if (gdbarch_arg ==
nullptr)
1314 gdbpy_ref<> result (PyObject_CallFunctionObjArgs (hook.get (),
1318 if (result ==
nullptr)
1324 if (result == Py_None)
1327 if (!PyBytes_Check (result.get ()))
1329 PyErr_SetString (PyExc_TypeError,
1330 _(
"Return value from gdb.colorize_disasm should be a bytes object or None."));
1335 return std::string (PyBytes_AsString (result.get ()));
1347 static const char *keywords[] =
1349 "address",
"progspace",
"architecture",
nullptr
1351 PyObject *addr_obj =
nullptr, *pspace_obj =
nullptr, *arch_obj =
nullptr;
1357 &addr_obj, &pspace_obj, &arch_obj))
1368 if (pspace_obj == Py_None)
1369 pspace_obj =
nullptr;
1370 if (arch_obj == Py_None)
1373 if (pspace_obj ==
nullptr && arch_obj ==
nullptr)
1380 else if (arch_obj ==
nullptr || pspace_obj ==
nullptr)
1387 PyErr_SetString (PyExc_ValueError,
1388 _(
"The architecture and progspace arguments must both be supplied"));
1397 PyErr_SetString (PyExc_TypeError,
1398 _(
"The progspace argument is not a gdb.Progspace object"));
1403 if (pspace ==
nullptr)
1405 PyErr_SetString (PyExc_ValueError,
1406 _(
"The progspace argument is not valid"));
1412 PyErr_SetString (PyExc_TypeError,
1413 _(
"The architecture argument is not a gdb.Architecture object"));
1420 gdb_assert (
gdbarch !=
nullptr);
1425 gdb_assert (pspace !=
nullptr);
1426 gdb_assert (
gdbarch !=
nullptr);
1436 return PyUnicode_FromString (buf.
c_str ());
1451 static const char *keywords[] = {
"text",
"stream", NULL };
1452 int stream_type = 0;
1460 switch (stream_type)
1476 catch (
const gdb_exception &except)
1491 static const char *keywords[] = {
"stream", NULL };
1492 int stream_type = 0;
1498 switch (stream_type)
1549 catch (
const gdb_exception &except)
1558 gdb::unique_xmalloc_ptr<char> msg = fetched_error.
to_string ();
1559 gdb::unique_xmalloc_ptr<char>
type;
1567 if (msg == NULL ||
type == NULL)
1572 _(
"Error occurred computing Python error" \
1578 type.get (), msg.get ());
1580 catch (
const gdb_exception &except)
1592 if (PyErr_ExceptionMatches (PyExc_KeyboardInterrupt))
1595 throw_quit (
"Quit");
1615 if (item == NULL || PyList_Append (list.get (), item.get ()) == -1)
1619 return list.release ();
1644 const char *filename)
1650 scoped_restore restire_current_objfile
1653 python_run_simple_file (file, filename);
1671 scoped_restore restire_current_objfile
1674 PyRun_SimpleString (script);
1704 gdbpy_ref<> type_module (PyImport_ImportModule (
"gdb.types"));
1705 if (type_module == NULL)
1712 "get_type_recognizers"));
1719 printers_obj = PyObject_CallFunctionObjArgs (
func.get (), (
char *) NULL);
1720 if (printers_obj == NULL)
1737 gdb::unique_xmalloc_ptr<char> *prettied_type)
1740 gdb::unique_xmalloc_ptr<char> result;
1742 if (printers_obj == NULL)
1751 if (type_obj == NULL)
1757 gdbpy_ref<> type_module (PyImport_ImportModule (
"gdb.types"));
1758 if (type_module == NULL)
1765 "apply_type_recognizers"));
1776 if (result_obj == NULL)
1782 if (result_obj == Py_None)
1792 *prettied_type = std::move (result);
1805 if (printers == NULL)
1812 Py_DECREF (printers);
1823 arg = skip_spaces (arg);
1825 error (_(
"Python scripting is not supported in this copy of GDB."));
1853 gdb_printf (file, _(
"Python's ignore-environment setting is %s.\n"),
1871#if PY_VERSION_HEX < 0x030a0000
1889 const char *auto_string
1891 || getenv (
"PYTHONDONTWRITEBYTECODE") ==
nullptr) ?
"off" :
"on";
1894 _(
"Python's dont-write-bytecode setting is %s (currently %s).\n"),
1895 value, auto_string);
1898 gdb_printf (file, _(
"Python's dont-write-bytecode setting is %s.\n"),
1908python_write_bytecode ()
1918 const char *pdwbc = getenv (
"PYTHONDONTWRITEBYTECODE");
1919 wbc = (pdwbc ==
nullptr || pdwbc[0] ==
'\0') ? 1 : 0;
1943#if PY_VERSION_HEX < 0x030a0000
1944 Py_DontWriteBytecodeFlag = !python_write_bytecode ();
1964finalize_python (
void *ignore)
1990static struct PyModuleDef python_GdbModuleDef =
1992 PyModuleDef_HEAD_INIT,
2006PyMODINIT_FUNC init__gdb_module (
void);
2008init__gdb_module (
void)
2010 return PyModule_Create (&python_GdbModuleDef);
2017emit_exiting_event (
int exit_code)
2023 if (event_obj ==
nullptr)
2037gdbpy_gdb_exiting (
int exit_code)
2044 if (emit_exiting_event (exit_code) < 0)
2049do_start_initialization ()
2053 struct _inittab mods[] =
2055 {
"_gdb", init__gdb_module },
2057 {
nullptr,
nullptr }
2060 if (PyImport_ExtendInittab (mods) < 0)
2063#ifdef WITH_PYTHON_PATH
2070 gdb::unique_xmalloc_ptr<char> progname
2072 SLASH_STRING,
"python", (
char *) NULL));
2080 static wchar_t *progname_copy;
2082 std::string oldloc = setlocale (LC_ALL, NULL);
2083 setlocale (LC_ALL,
"");
2084 size_t progsize = strlen (progname.get ());
2085 progname_copy = XNEWVEC (
wchar_t, progsize + 1);
2086 size_t count = mbstowcs (progname_copy, progname.get (), progsize + 1);
2087 if (count == (
size_t) -1)
2089 fprintf (stderr,
"Could not convert python path to string\n");
2092 setlocale (LC_ALL, oldloc.c_str ());
2096#if PY_VERSION_HEX < 0x030a0000
2100 Py_SetProgramName (progname_copy);
2105 PyConfig_InitPythonConfig (&config);
2106 PyStatus
status = PyConfig_SetString (&config, &config.program_name,
2108 if (PyStatus_Exception (
status))
2111 config.write_bytecode = python_write_bytecode ();
2114 status = PyConfig_Read (&config);
2115 if (PyStatus_Exception (
status))
2118 status = Py_InitializeFromConfig (&config);
2121 PyConfig_Clear (&config);
2122 if (PyStatus_Exception (
status))
2129#if PY_VERSION_HEX < 0x03090000
2142 || PyModule_AddStringConstant (
gdb_module,
"TARGET_CONFIG",
2147 if (PyModule_AddIntConstant (
gdb_module,
"STDOUT", 0) < 0
2148 || PyModule_AddIntConstant (
gdb_module,
"STDERR", 1) < 0
2149 || PyModule_AddIntConstant (
gdb_module,
"STDLOG", 2) < 0)
2174#define GDB_PY_DEFINE_EVENT_TYPE(name, py_name, doc, base) \
2175 if (gdbpy_initialize_event_generic (&name##_event_object_type, py_name) < 0) \
2177#include "py-event-types.def"
2178#undef GDB_PY_DEFINE_EVENT_TYPE
2202 PyEval_SaveThread ();
2204 make_final_cleanup (finalize_python, NULL);
2219#define CMD(S) execute_command_to_string (S, "python print(5)", 0, true)
2224 SELF_CHECK (output ==
"5\n");
2227 bool saw_exception =
false;
2229 scoped_restore reset_gdb_python_initialized
2235 catch (
const gdb_exception &e)
2237 saw_exception =
true;
2238 SELF_CHECK (e.reason == RETURN_ERROR);
2239 SELF_CHECK (e.error == GENERIC_ERROR);
2240 SELF_CHECK (*e.message ==
"Python not initialized");
2242 SELF_CHECK (saw_exception);
2243 SELF_CHECK (output.empty ());
2246 saw_exception =
false;
2248 scoped_restore save_hook
2249 = make_scoped_restore (&hook_set_active_ext_lang,
2250 []() { raise (SIGINT); });
2255 catch (
const gdb_exception &e)
2257 saw_exception =
true;
2258 SELF_CHECK (e.reason == RETURN_ERROR);
2259 SELF_CHECK (e.error == GENERIC_ERROR);
2260 SELF_CHECK (*e.message ==
"Error while executing Python code.");
2262 SELF_CHECK (saw_exception);
2263 std::string ref_output_0 (
"Traceback (most recent call last):\n"
2264 " File \"<string>\", line 0, in <module>\n"
2265 "KeyboardInterrupt\n");
2266 std::string ref_output_1 (
"Traceback (most recent call last):\n"
2267 " File \"<string>\", line 1, in <module>\n"
2268 "KeyboardInterrupt\n");
2269 SELF_CHECK (output == ref_output_0 || output == ref_output_1);
2294Start an interactive Python prompt.\n\
2296To return to GDB, type the EOF character (e.g., Ctrl-D on an empty\n\
2299Alternatively, a single-line Python command can be given as an\n\
2300argument, and if the command is an expression, the result will be\n\
2301printed. For example:\n\
2303 (gdb) python-interactive 2 + 3\n\
2307Start a Python interactive prompt.\n\
2309Python scripting is not supported in this copy of GDB.\n\
2310This command is only a placeholder.")
2318Evaluate a Python command.\n\
2320The command can be given as an argument, for instance:\n\
2322 python print (23)\n\
2324If no argument is given, the following lines are read and used\n\
2325as the Python commands. Type a line containing \"end\" to indicate\n\
2326the end of the command.")
2329Evaluate a Python command.\n\
2331Python scripting is not supported in this copy of GDB.\n\
2332This command is only a placeholder.")
2339 _(
"Prefix command for python preference settings."),
2340 _(
"Prefix command for python preference settings."),
2346Set mode for Python stack dump on error."), _(
"\
2347Show the mode of Python stack printing on error."), _(
"\
2348none == no stack or message will be printed.\n\
2349full == a message and a stack will be printed.\n\
2350message == an error message without a stack will be printed."),
2357Set whether the Python interpreter should ignore environment variables."), _(
" \
2358Show whether the Python interpreter showlist ignore environment variables."), _(
" \
2359When enabled GDB's Python interpreter will ignore any Python related\n \
2360flags in the environment. This is equivalent to passing `-E' to a\n \
2361python executable."),
2369Set whether the Python interpreter should avoid byte-compiling python modules."), _(
"\
2370Show whether the Python interpreter should avoid byte-compiling python modules."), _(
"\
2371When enabled, GDB's embedded Python interpreter won't byte-compile python\n\
2372modules. In order to take effect, this setting must be enabled in an early\n\
2373initialization file, i.e. those run via the --early-init-eval-command or\n\
2374-eix command line options. A 'set python dont-write-bytecode on' command\n\
2375can also be issued directly from the GDB command line via the\n\
2376--early-init-eval-command or -eiex command line options.\n\
2378This setting defaults to 'auto'. In this mode, provided the 'python\n\
2379ignore-environment' setting is 'off', the environment variable\n\
2380PYTHONDONTWRITEBYTECODE is examined to determine whether or not to\n\
2381byte-compile python modules. PYTHONDONTWRITEBYTECODE is considered to be\n\
2382off/disabled either when set to the empty string or when the\n\
2383environment variable doesn't exist. All other settings, including those\n\
2384which don't seem to make sense, indicate that it's on/enabled."),
2392 selftests::register_test (
"python", selftests::test_python);
2411 std::string gdb_pythondir = (std::string (
gdb_datadir) + SLASH_STRING
2425#if PY_VERSION_HEX < 0x030a0000
2427 if (!(sys_path && PyList_Check (sys_path)))
2433 if (sys_path && PyList_Check (sys_path))
2435 gdbpy_ref<> pythondir (PyUnicode_FromString (gdb_pythondir.c_str ()));
2436 if (pythondir == NULL || PyList_Insert (sys_path, 0, pythondir.get ()))
2444 m = PyImport_AddModule (
"__main__");
2457 "Could not load the Python gdb module from `%s'.\n"
2458 "Limited Python support is available from the _gdb module.\n"
2459 "Suggest passing --data-directory=/path/to/gdb/data-directory."),
2460 gdb_pythondir.c_str ());
2476 if (!do_start_initialization () && PyErr_Occurred ())
2481 if (!do_initialize (extlang))
2484 warning (_(
"internal error: Unhandled Python exception"));
2497PyMethodDef python_GdbMethods[] =
2500 "Get a value from history" },
2502 "Add a value to the value history list" },
2504 "Return an integer, the number of values in GDB's value history" },
2505 {
"execute", (PyCFunction) execute_gdb_command, METH_VARARGS | METH_KEYWORDS,
2506 "execute (command [, from_tty] [, to_string]) -> [String]\n\
2507Evaluate command, a string, as a gdb CLI command. Optionally returns\n\
2508a Python String containing the output of the command if to_string is\n\
2511 METH_VARARGS | METH_KEYWORDS,
2512 "execute_mi (command, arg...) -> dictionary\n\
2513Evaluate command, a string, as a gdb MI command.\n\
2514Arguments (also strings) are passed to the command." },
2515 {
"parameter", gdbpy_parameter, METH_VARARGS,
2516 "Return a gdb parameter's value" },
2519 "Return a tuple of all breakpoint objects" },
2522 "Find the default visualizer for a Value." },
2524 {
"progspaces", gdbpy_progspaces, METH_NOARGS,
2525 "Return a sequence of all progspaces." },
2527 {
"current_objfile", gdbpy_get_current_objfile, METH_NOARGS,
2528 "Return the current Objfile being loaded, or None." },
2531 "newest_frame () -> gdb.Frame.\n\
2532Return the newest frame object." },
2534 "selected_frame () -> gdb.Frame.\n\
2535Return the selected frame object." },
2537 "stop_reason_string (Integer) -> String.\n\
2538Return a string explaining unwind stop reason." },
2541 "start_recording ([method] [, format]) -> gdb.Record.\n\
2542Start recording with the given method. If no method is given, will fall back\n\
2543to the system default method. If no format is given, will fall back to the\n\
2544default format for the given method."},
2546 "current_recording () -> gdb.Record.\n\
2547Return current recording object." },
2549 "stop_recording () -> None.\n\
2550Stop current recording." },
2553 METH_VARARGS | METH_KEYWORDS,
2554 "lookup_type (name [, block]) -> type\n\
2555Return a Type corresponding to the given name." },
2557 METH_VARARGS | METH_KEYWORDS,
2558 "lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)\n\
2559Return a tuple with the symbol corresponding to the given name (or None) and\n\
2560a boolean indicating if name is a field of the current implied argument\n\
2561`this' (when the current language is object-oriented)." },
2563 METH_VARARGS | METH_KEYWORDS,
2564 "lookup_global_symbol (name [, domain]) -> symbol\n\
2565Return the symbol corresponding to the given name (or None)." },
2567 METH_VARARGS | METH_KEYWORDS,
2568 "lookup_static_symbol (name [, domain]) -> symbol\n\
2569Return the static-linkage symbol corresponding to the given name (or None)." },
2571 METH_VARARGS | METH_KEYWORDS,
2572 "lookup_static_symbols (name [, domain]) -> symbol\n\
2573Return a list of all static-linkage symbols corresponding to the given name." },
2576 METH_VARARGS | METH_KEYWORDS,
2577 "lookup_objfile (name, [by_build_id]) -> objfile\n\
2578Look up the specified objfile.\n\
2579If by_build_id is True, the objfile is looked up by using name\n\
2582 {
"decode_line", gdbpy_decode_line, METH_VARARGS,
2583 "decode_line (String) -> Tuple. Decode a string argument the way\n\
2584that 'break' or 'edit' does. Return a tuple containing two elements.\n\
2585The first element contains any unparsed portion of the String parameter\n\
2586(or None if the string was fully parsed). The second element contains\n\
2587a tuple that contains all the locations that match, represented as\n\
2588gdb.Symtab_and_line objects (or None)."},
2589 {
"parse_and_eval", (PyCFunction) gdbpy_parse_and_eval,
2590 METH_VARARGS | METH_KEYWORDS,
2591 "parse_and_eval (String, [Boolean]) -> Value.\n\
2592Parse String as an expression, evaluate it, and return the result as a Value."
2595 {
"post_event", gdbpy_post_event, METH_VARARGS,
2596 "Post an event into gdb's event loop." },
2598 {
"target_charset", gdbpy_target_charset, METH_NOARGS,
2599 "target_charset () -> string.\n\
2600Return the name of the current target charset." },
2601 {
"target_wide_charset", gdbpy_target_wide_charset, METH_NOARGS,
2602 "target_wide_charset () -> string.\n\
2603Return the name of the current target wide charset." },
2604 {
"host_charset", gdbpy_host_charset, METH_NOARGS,
2605 "host_charset () -> string.\n\
2606Return the name of the current host charset." },
2607 {
"rbreak", (PyCFunction) gdbpy_rbreak, METH_VARARGS | METH_KEYWORDS,
2608 "rbreak (Regex) -> List.\n\
2609Return a Tuple containing gdb.Breakpoint objects that match the given Regex." },
2611 "string_to_argv (String) -> Array.\n\
2612Parse String and return an argv-like array.\n\
2613Arguments are separate by spaces and may be quoted."
2615 {
"write", (PyCFunction)gdbpy_write, METH_VARARGS | METH_KEYWORDS,
2616 "Write a string using gdb's filtered stream." },
2617 {
"flush", (PyCFunction)gdbpy_flush, METH_VARARGS | METH_KEYWORDS,
2618 "Flush gdb's filtered stdout stream." },
2620 "selected_thread () -> gdb.InferiorThread.\n\
2621Return the selected thread object." },
2623 "selected_inferior () -> gdb.Inferior.\n\
2624Return the selected inferior object." },
2626 "inferiors () -> (gdb.Inferior, ...).\n\
2627Return a tuple containing all inferiors." },
2629 {
"invalidate_cached_frames", gdbpy_invalidate_cached_frames, METH_NOARGS,
2630 "invalidate_cached_frames () -> None.\n\
2631Invalidate any cached frame objects in gdb.\n\
2632Intended for internal use only." },
2635 "convenience_variable (NAME) -> value.\n\
2636Return the value of the convenience variable $NAME,\n\
2637or None if not set." },
2639 "convenience_variable (NAME, VALUE) -> None.\n\
2640Set the value of the convenience variable $NAME." },
2644 METH_VARARGS | METH_KEYWORDS,
2645 "register_window_type (NAME, CONSTRUCTOR) -> None\n\
2646Register a TUI window constructor." },
2650 "architecture_names () -> List.\n\
2651Return a list of all the architecture names GDB understands." },
2654 "connections () -> List.\n\
2655Return a list of gdb.TargetConnection objects." },
2657 {
"format_address", (PyCFunction) gdbpy_format_address,
2658 METH_VARARGS | METH_KEYWORDS,
2659 "format_address (ADDRESS, PROG_SPACE, ARCH) -> String.\n\
2660Format ADDRESS, an address within PROG_SPACE, a gdb.Progspace, using\n\
2661ARCH, a gdb.Architecture to determine the address size. The format of\n\
2662the returned string is 'ADDRESS <SYMBOL+OFFSET>' without the quotes." },
2664 {
"current_language", gdbpy_current_language, METH_NOARGS,
2665 "current_language () -> string\n\
2666Return the name of the currently selected language." },
2669 "print_options () -> dict\n\
2670Return the current print options." },
2672 {NULL, NULL, 0, NULL}
2676#define GDB_PY_DEFINE_EVENT_TYPE(name, py_name, doc, base) \
2677 PyTypeObject name##_event_object_type \
2678 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("event_object") \
2680 PyVarObject_HEAD_INIT (NULL, 0) \
2682 sizeof (event_object), \
2699 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, \
2718#include "py-event-types.def"
2719#undef GDB_PY_DEFINE_EVENT_TYPE
constexpr string_view get()
struct gdbarch * get_current_arch(void)
struct gdbarch * target_gdbarch(void)
void bpstat_do_actions(void)
const char * target_wide_charset(struct gdbarch *gdbarch)
const char * host_charset(void)
const char * target_charset(struct gdbarch *gdbarch)
static struct gdbarch * python_gdbarch
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)
struct gdbarch * m_gdbarch
struct active_ext_lang_state * m_previous_active
gdb::unique_xmalloc_ptr< char > type_to_string() const
gdb::unique_xmalloc_ptr< char > to_string() const
static void finalize_all()
static bool initialize_all()
struct program_space * pspace
virtual ui_out * interp_ui_out()=0
const char * c_str() const
struct cmd_list_element * showlist
struct cmd_list_element * setlist
cmd_list_element * add_com_alias(const char *name, cmd_list_element *target, command_class theclass, int abbrev_flag)
struct cmd_list_element * add_com(const char *name, enum command_class theclass, cmd_simple_func_ftype *fun, const char *doc)
set_show_commands add_setshow_enum_cmd(const char *name, enum command_class theclass, const char *const *enumlist, const char **var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
set_show_commands add_setshow_prefix_cmd(const char *name, command_class theclass, const char *set_doc, const char *show_doc, cmd_list_element **set_subcommands_list, cmd_list_element **show_subcommands_list, cmd_list_element **set_list, cmd_list_element **show_list)
set_show_commands add_setshow_boolean_cmd(const char *name, enum command_class theclass, bool *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
set_show_commands add_setshow_auto_boolean_cmd(const char *name, enum command_class theclass, enum auto_boolean *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
int lookup_cmd_composition(const char *text, struct cmd_list_element **alias, struct cmd_list_element **prefix_cmd, struct cmd_list_element **cmd)
counted_command_line read_command_lines_1(read_next_line_ftype read_next_line_func, int parse_commands, gdb::function_view< void(const char *)> validator)
void execute_control_commands(struct command_line *cmdlines, int from_tty)
counted_command_line get_command_line(enum command_control_type type, const char *arg)
enum command_control_type execute_control_command_untraced(struct command_line *cmd)
std::string execute_control_commands_to_string(struct command_line *commands, int from_tty)
std::shared_ptr< command_line > counted_command_line
scoped_restore_tmpl< int > prevent_dont_repeat(void)
std::string to_string(cooked_index_flag flags)
std::string python_libdir
void print_address(struct gdbarch *, CORE_ADDR, struct ui_file *)
struct value * parse_and_eval(const char *exp, parser_flags flags)
void async_enable_stdin(void)
@ PARSER_LEAVE_BLOCK_ALONE
struct active_ext_lang_state * set_active_ext_lang(const struct extension_language_defn *)
void restore_active_ext_lang(struct active_ext_lang_state *previous)
void script_sourcer_func(const struct extension_language_defn *, FILE *stream, const char *filename)
void objfile_script_sourcer_func(const struct extension_language_defn *, struct objfile *, FILE *stream, const char *filename)
void objfile_script_executor_func(const struct extension_language_defn *, struct objfile *, const char *name, const char *script)
void reinit_frame_cache(void)
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t err
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
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int status
struct inferior * current_inferior(void)
struct interp * interp_lookup(struct ui *ui, const char *name)
static void set_language(const char *language)
const struct language_defn * current_language
std::vector< symtab_and_line > decode_line_1(const location_spec *locspec, int flags, struct program_space *search_pspace, struct symtab *default_symtab, int default_line)
location_spec_up string_to_location_spec_basic(const char **stringp, const struct language_defn *language, symbol_name_match_type match_type)
std::unique_ptr< location_spec > location_spec_up
observable< int > gdb_exiting
#define prefix(a, b, R, do)
void set_current_program_space(struct program_space *pspace)
std::vector< struct program_space * > program_spaces
PyObject * gdbpy_all_architecture_names(PyObject *self, PyObject *args)
bool gdbpy_is_architecture(PyObject *obj)
struct gdbarch * arch_object_to_gdbarch(PyObject *obj)
PyObject * gdbarch_to_arch_object(struct gdbarch *gdbarch)
bool gdbpy_auto_load_enabled(const struct extension_language_defn *extlang)
PyObject * gdbpy_breakpoints(PyObject *self, PyObject *args)
PyTypeObject breakpoint_object_type
int gdbpy_breakpoint_has_cond(const struct extension_language_defn *extlang, struct breakpoint *b)
enum ext_lang_bp_stop gdbpy_breakpoint_cond_says_stop(const struct extension_language_defn *extlang, struct breakpoint *b)
PyObject * gdbpy_string_to_argv(PyObject *self, PyObject *args)
PyObject * gdbpy_connections(PyObject *self, PyObject *args)
gdb::optional< int > gdbpy_print_insn(struct gdbarch *gdbarch, CORE_ADDR memaddr, disassemble_info *info)
gdbpy_ref create_event_object(PyTypeObject *py_type)
int evpy_add_attribute(PyObject *event, const char *name, PyObject *attr)
int evpy_emit_event(PyObject *event, eventregistry_object *registry)
events_object gdb_py_events
bool evregpy_no_listeners_p(eventregistry_object *registry)
PyMODINIT_FUNC gdbpy_events_mod_func()
PyObject * gdbpy_frame_stop_reason_string(PyObject *self, PyObject *args)
PyObject * gdbpy_selected_frame(PyObject *self, PyObject *args)
PyObject * gdbpy_newest_frame(PyObject *self, PyObject *args)
enum ext_lang_bt_status gdbpy_apply_frame_filter(const struct extension_language_defn *extlang, 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)
PyObject * gdbpy_selected_inferior(PyObject *self, PyObject *args)
PyObject * gdbpy_inferiors(PyObject *unused, PyObject *unused2)
PyObject * gdbpy_selected_thread(PyObject *self, PyObject *args)
PyObject * gdbpy_execute_mi_command(PyObject *self, PyObject *args, PyObject *kw)
gdbpy_ref objfile_to_objfile_object(struct objfile *objfile)
PyObject * gdbpy_lookup_objfile(PyObject *self, PyObject *args, PyObject *kw)
PyObject * gdbpy_default_visualizer(PyObject *self, PyObject *args)
PyObject * gdbpy_print_options(PyObject *unused1, PyObject *unused2)
enum ext_lang_rc gdbpy_apply_val_pretty_printer(const struct extension_language_defn *extlang, struct value *value, struct ui_file *stream, int recurse, const struct value_print_options *options, const struct language_defn *language)
bool gdbpy_is_progspace(PyObject *obj)
gdbpy_ref pspace_to_pspace_object(struct program_space *pspace)
struct program_space * progspace_object_to_program_space(PyObject *obj)
PyObject * gdbpy_stop_recording(PyObject *self, PyObject *args)
PyObject * gdbpy_start_recording(PyObject *self, PyObject *args)
PyObject * gdbpy_current_recording(PyObject *self, PyObject *args)
gdb::ref_ptr< T, gdbpy_ref_policy< T > > gdbpy_ref
PyObject * gdbpy_lookup_static_symbol(PyObject *self, PyObject *args, PyObject *kw)
PyObject * gdbpy_lookup_global_symbol(PyObject *self, PyObject *args, PyObject *kw)
PyObject * gdbpy_lookup_symbol(PyObject *self, PyObject *args, PyObject *kw)
PyObject * gdbpy_lookup_static_symbols(PyObject *self, PyObject *args, PyObject *kw)
PyObject * symtab_and_line_to_sal_object(struct symtab_and_line sal)
PyObject * type_to_type_object(struct type *type)
PyObject * gdbpy_lookup_type(PyObject *self, PyObject *args, PyObject *kw)
gdbpy_ref host_string_to_python_string(const char *str)
gdb::unique_xmalloc_ptr< char > python_string_to_target_string(PyObject *obj)
gdbpy_ref gdb_py_object_from_longest(LONGEST l)
int get_addr_from_python(PyObject *obj, CORE_ADDR *addr)
void gdbpy_convert_exception(const struct gdb_exception &exception)
int gdb_pymodule_addobject(PyObject *module, const char *name, PyObject *object)
gdb::unique_xmalloc_ptr< char > python_string_to_host_string(PyObject *obj)
gdbpy_ref gdb_py_object_from_ulongest(ULONGEST l)
PyObject * value_to_value_object(struct value *val)
PyObject * gdbpy_add_history(PyObject *self, PyObject *args)
PyObject * gdbpy_history_count(PyObject *self, PyObject *args)
PyObject * gdbpy_history(PyObject *self, PyObject *args)
PyObject * gdbpy_set_convenience_variable(PyObject *self, PyObject *args)
void gdbpy_preserve_values(const struct extension_language_defn *extlang, struct objfile *objfile, htab_t copied_types)
PyObject * gdbpy_convenience_variable(PyObject *self, PyObject *args)
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)
void gdbpy_print_stack(void)
#define PyObject_CallMethod
int gdbpy_print_python_errors_p(void)
int gdb_python_initialized
PyObject * gdbpy_register_tui_window(PyObject *self, PyObject *args, PyObject *kw)
PyObject * gdbpy_children_cst
void gdbpy_print_stack_or_quit()
#define PyEval_InitThreads()
PyObject * gdbpy_gdb_memory_error
PyObject * gdbpy_display_hint_cst
PyObject * gdbpy_parameter_value(const setting &var)
#define PyGILState_Release(ARG)
#define PyGILState_Ensure()
PyObject * gdbpy_gdb_error
PyObject * gdbpy_enabled_cst
PyObject * gdbpy_to_string_cst
PyObject * gdb_python_module
PyObject * gdbpy_value_cst
#define PyErr_NewException
PyObject * gdbpy_gdberror_exc
#define GDB_PY_HANDLE_EXCEPTION(Exception)
static int gdb_PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, const char **keywords,...)
static enum auto_boolean python_dont_write_bytecode
static struct cmd_list_element * user_show_python_list
static const char *const python_excp_enums[]
static void set_python_ignore_environment(const char *args, int from_tty, struct cmd_list_element *c)
static const char python_excp_none[]
static void python_command(const char *arg, int from_tty)
const struct extension_language_defn extension_language_python
static const char python_excp_message[]
static struct cmd_list_element * user_set_python_list
cmd_list_element * python_cmd_element
void _initialize_python()
static void python_interactive_command(const char *arg, int from_tty)
static const char * gdbpy_should_print_stack
static void show_python_ignore_environment(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
static const char python_excp_full[]
static bool python_ignore_environment
static void show_python_dont_write_bytecode(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
static void set_python_dont_write_bytecode(const char *args, int from_tty, struct cmd_list_element *c)
struct objfile * gdbpy_current_objfile
void(* func)(remote_target *remote, char *)
void run_on_main_thread(std::function< void()> &&func)
const char * symtab_to_fullname(struct symtab *s)
struct symtab_and_line get_current_source_symtab_and_line(void)
void set_default_source_symtab_and_line(void)
gdb::optional< setting > var
counted_command_line body_list_0
counted_command_line body_list_1
struct command_line * next
enum language la_language
virtual const char * name() const =0
struct gdbarch * arch() const
const literal_def * extra_literals() const
setting_func_types< T >::type get() const
void set_prompt(const char *s)
std::string ldirname(const char *filename)
void gdb_printf(struct ui_file *stream, const char *format,...)
void gdb_flush(struct ui_file *stream)