]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/python/lib/gdb/dap/__init__.py
689c2049eec18d44a82937aab1119031c73969b1
[thirdparty/binutils-gdb.git] / gdb / python / lib / gdb / dap / __init__.py
1 # Copyright 2022-2023 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 import os
17 import gdb
18
19 # This must come before other DAP imports.
20 from . import startup
21
22 # Load modules that define commands.
23 from . import breakpoint
24 from . import bt
25 from . import disassemble
26 from . import evaluate
27 from . import launch
28 from . import locations
29 from . import memory
30 from . import modules
31 from . import next
32 from . import pause
33 from . import scopes
34 from . import sources
35 from . import threads
36
37 from .server import Server
38
39
40 def run():
41 """Main entry point for the DAP server.
42 This is called by the GDB DAP interpreter."""
43 startup.exec_and_log("set python print-stack full")
44 startup.exec_and_log("set pagination off")
45
46 # We want to control gdb stdin and stdout entirely, so we dup
47 # them to new file descriptors.
48 saved_out = os.dup(1)
49 saved_in = os.dup(0)
50 # Make sure these are not inheritable. This is already the case
51 # for Unix, but not for Windows.
52 os.set_inheritable(saved_out, False)
53 os.set_inheritable(saved_in, False)
54
55 # The new gdb (and inferior) stdin will just be /dev/null. For
56 # gdb, the "dap" interpreter also rewires the UI so that gdb
57 # doesn't try to read this (and thus see EOF and exit).
58 new_in = os.open(os.devnull, os.O_RDONLY)
59 os.dup2(new_in, 0, True)
60 os.close(new_in)
61
62 # Make the new stdout be a pipe. This way the DAP code can easily
63 # read from the inferior and send OutputEvent to the client.
64 (rfd, wfd) = os.pipe()
65 os.set_inheritable(rfd, False)
66 os.dup2(wfd, 1, True)
67 # Also send stderr this way.
68 os.dup2(wfd, 2, True)
69 os.close(wfd)
70
71 # Note the inferior output is opened in text mode.
72 server = Server(open(saved_in, "rb"), open(saved_out, "wb"), open(rfd, "r"))
73 startup.start_dap(server.main_loop)