1/* Plugin connection declarations
2 Copyright (C) 2014-2023 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#ifndef CC1_PLUGIN_CONNECTION_HH
21#define CC1_PLUGIN_CONNECTION_HH
22
23#include "status.hh"
24#include "callbacks.hh"
25
26namespace cc1_plugin
27{
28 // The connection class represents one side of the connection
29 // between the gdb-side library and the gcc plugin. It handles the
30 // low-level details of reading and writing data.
31 class connection
32 {
33 public:
34
35 connection (int fd)
36 : m_fd (fd),
37 m_aux_fd (-1),
38 m_callbacks ()
39 {
40 }
41
42 connection (int fd, int aux_fd)
43 : m_fd (fd),
44 m_aux_fd (aux_fd),
45 m_callbacks ()
46 {
47 }
48
49 virtual ~connection () = default;
50
51 connection (const connection &) = delete;
52 connection &operator= (const connection &) = delete;
53
54 // Send a single character. This is used to introduce various
55 // higher-level protocol elements.
56 status send (char c);
57
58 // Send data in bulk.
59 status send (const void *buf, int len);
60
61 // Read a single byte from the connection and verify that it
62 // matches the argument C.
63 status require (char c);
64
65 // Read data in bulk.
66 status get (void *buf, int len);
67
68 // This is called after a query (remote function call) has been
69 // sent to the remote. It waits for a response packet. The
70 // response character is read before returning. Any query packets
71 // sent from the remote while waiting for a response are handled
72 // by this function.
73 status wait_for_result ()
74 {
75 return do_wait (true);
76 }
77
78 // Read and respond to query packets sent by the remote. This
79 // function returns when the connection is closed.
80 status wait_for_query ()
81 {
82 return do_wait (false);
83 }
84
85 // Register a callback with this connection. NAME is the name of
86 // the method being registered. FUNC is the function. It must
87 // know how to decode its own arguments. When a query packet is
88 // received by one of the wait_* methods, the corresponding
89 // callback is invoked.
90 void add_callback (const char *name, callback_ftype *func)
91 {
92 m_callbacks.add_callback (name, func);
93 }
94
95 virtual void print (const char *)
96 {
97 }
98
99 private:
100
101 // Helper function for the wait_* methods.
102 status do_wait (bool);
103
104 // The file descriptor.
105 int m_fd;
106
107 // An auxiliary file descriptor, or -1 if none.
108 int m_aux_fd;
109
110 // Callbacks associated with this connection.
111 callbacks m_callbacks;
112 };
113}
114
115#endif // CC1_PLUGIN_CONNECTION_HH
116

source code of libcc1/connection.hh