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/>. */
20 #include "python-internal.h"
25 class dap_interp final
: public interp
29 explicit dap_interp (const char *name
)
31 m_ui_out (new cli_ui_out (gdb_stdout
))
35 ~dap_interp () override
= default;
37 void init (bool top_level
) override
;
39 void suspend () override
43 void resume () override
47 void exec (const char *command
) override
52 void set_logging (ui_file_up logfile
, bool logging_redirect
,
53 bool debug_redirect
) override
58 ui_out
*interp_ui_out () override
60 return m_ui_out
.get ();
63 void pre_command_loop () override
;
65 bool supports_new_ui () const override
70 std::unique_ptr
<ui_out
> m_ui_out
;
74 /* Call function FN_NAME from module gdb.dap. */
77 call_dap_fn (const char *fn_name
)
81 gdbpy_ref
<> dap_module (PyImport_ImportModule ("gdb.dap"));
82 if (dap_module
== nullptr)
83 gdbpy_handle_exception ();
85 gdbpy_ref
<> func (PyObject_GetAttrString (dap_module
.get (), fn_name
));
87 gdbpy_handle_exception ();
89 gdbpy_ref
<> result_obj (PyObject_CallObject (func
.get (), nullptr));
90 if (result_obj
== nullptr)
91 gdbpy_handle_exception ();
95 dap_interp::init (bool top_level
)
100 current_ui
->input_fd
= -1;
101 current_ui
->m_input_interactive_p
= false;
103 error (_("GDB was compiled without threading, which DAP requires"));
108 dap_interp::pre_command_loop ()
110 call_dap_fn ("pre_command_loop");
113 void _initialize_py_interp ();
115 _initialize_py_interp ()
117 /* The dap code uses module typing, available starting python 3.5. */
118 #if PY_VERSION_HEX >= 0x03050000
119 interp_factory_register ("dap", [] (const char *name
) -> interp
*
121 return new dap_interp (name
);