1 /* Python DAP interpreter
3 Copyright (C) 2022-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "python-internal.h"
26 class dap_interp final
: public interp
30 explicit dap_interp (const char *name
)
32 m_ui_out (new cli_ui_out (gdb_stdout
))
36 ~dap_interp () override
= default;
38 void init (bool top_level
) override
;
40 void suspend () override
44 void resume () override
48 void exec (const char *command
) override
53 void set_logging (ui_file_up logfile
, bool logging_redirect
,
54 bool debug_redirect
) override
59 ui_out
*interp_ui_out () override
61 return m_ui_out
.get ();
64 void pre_command_loop () override
;
68 std::unique_ptr
<ui_out
> m_ui_out
;
72 /* Call function FN_NAME from module gdb.dap. */
75 call_dap_fn (const char *fn_name
)
79 gdbpy_ref
<> dap_module (PyImport_ImportModule ("gdb.dap"));
80 if (dap_module
== nullptr)
81 gdbpy_handle_exception ();
83 gdbpy_ref
<> func (PyObject_GetAttrString (dap_module
.get (), fn_name
));
85 gdbpy_handle_exception ();
87 gdbpy_ref
<> result_obj (PyObject_CallObject (func
.get (), nullptr));
88 if (result_obj
== nullptr)
89 gdbpy_handle_exception ();
93 dap_interp::init (bool top_level
)
98 current_ui
->input_fd
= -1;
99 current_ui
->m_input_interactive_p
= false;
101 error (_("GDB was compiled without threading, which DAP requires"));
106 dap_interp::pre_command_loop ()
108 call_dap_fn ("pre_command_loop");
111 void _initialize_py_interp ();
113 _initialize_py_interp ()
115 /* The dap code uses module typing, available starting python 3.5. */
116 #if PY_VERSION_HEX >= 0x03050000
117 interp_factory_register ("dap", [] (const char *name
) -> interp
*
119 return new dap_interp (name
);