xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/agent.cc (revision 889f3bb010ad20d396fb291b89f202288dac2c87)
1 /* Shared utility routines for GDB to interact with agent.
2 
3    Copyright (C) 2009-2024 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
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.
11 
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.
16 
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/>.  */
19 
20 #include "target/target.h"
21 #include "gdbsupport/symbol.h"
22 #include <unistd.h>
23 #include "filestuff.h"
24 
25 #define IPA_SYM_STRUCT_NAME ipa_sym_addresses_common
26 #include "agent.h"
27 
28 bool debug_agent = false;
29 
30 /* A stdarg wrapper for debug_vprintf.  */
31 
32 static void ATTRIBUTE_PRINTF (1, 2)
33 debug_agent_printf (const char *fmt, ...)
34 {
35   va_list ap;
36 
37   if (!debug_agent)
38     return;
39   va_start (ap, fmt);
40   debug_vprintf (fmt, ap);
41   va_end (ap);
42 }
43 
44 #define DEBUG_AGENT debug_agent_printf
45 
46 /* Global flag to determine using agent or not.  */
47 bool use_agent = false;
48 
49 /* Addresses of in-process agent's symbols both GDB and GDBserver cares
50    about.  */
51 
52 struct ipa_sym_addresses_common
53 {
54   CORE_ADDR addr_helper_thread_id;
55   CORE_ADDR addr_cmd_buf;
56   CORE_ADDR addr_capability;
57 };
58 
59 /* Cache of the helper thread id.  FIXME: this global should be made
60    per-process.  */
61 static uint32_t helper_thread_id = 0;
62 
63 static struct
64 {
65   const char *name;
66   int offset;
67 } symbol_list[] = {
68   IPA_SYM(helper_thread_id),
69   IPA_SYM(cmd_buf),
70   IPA_SYM(capability),
71 };
72 
73 static struct ipa_sym_addresses_common ipa_sym_addrs;
74 
75 static bool all_agent_symbols_looked_up = false;
76 
77 bool
78 agent_loaded_p (void)
79 {
80   return all_agent_symbols_looked_up;
81 }
82 
83 /* Look up all symbols needed by agent.  Return 0 if all the symbols are
84    found, return non-zero otherwise.  */
85 
86 int
87 agent_look_up_symbols (void *arg)
88 {
89   all_agent_symbols_looked_up = false;
90 
91   for (int i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
92     {
93       CORE_ADDR *addrp =
94 	(CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);
95       struct objfile *objfile = (struct objfile *) arg;
96 
97       if (find_minimal_symbol_address (symbol_list[i].name, addrp,
98 				       objfile) != 0)
99 	{
100 	  DEBUG_AGENT ("symbol `%s' not found\n", symbol_list[i].name);
101 	  return -1;
102 	}
103     }
104 
105   all_agent_symbols_looked_up = true;
106   return 0;
107 }
108 
109 static unsigned int
110 agent_get_helper_thread_id (void)
111 {
112   if  (helper_thread_id == 0)
113     {
114       if (target_read_uint32 (ipa_sym_addrs.addr_helper_thread_id,
115 			      &helper_thread_id))
116 	warning (_("Error reading helper thread's id in lib"));
117     }
118 
119   return helper_thread_id;
120 }
121 
122 #ifdef HAVE_SYS_UN_H
123 #include <sys/socket.h>
124 #include <sys/un.h>
125 #define SOCK_DIR P_tmpdir
126 
127 #ifndef UNIX_PATH_MAX
128 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
129 #endif
130 
131 #endif
132 
133 /* Connects to synchronization socket.  PID is the pid of inferior, which is
134    used to set up the connection socket.  */
135 
136 static int
137 gdb_connect_sync_socket (int pid)
138 {
139 #ifdef HAVE_SYS_UN_H
140   struct sockaddr_un addr = {};
141   int res, fd;
142   char path[UNIX_PATH_MAX];
143 
144   res = xsnprintf (path, UNIX_PATH_MAX, "%s/gdb_ust%d", P_tmpdir, pid);
145   if (res >= UNIX_PATH_MAX)
146     return -1;
147 
148   res = fd = gdb_socket_cloexec (PF_UNIX, SOCK_STREAM, 0);
149   if (res == -1)
150     {
151       warning (_("error opening sync socket: %s"), safe_strerror (errno));
152       return -1;
153     }
154 
155   addr.sun_family = AF_UNIX;
156 
157   res = xsnprintf (addr.sun_path, UNIX_PATH_MAX, "%s", path);
158   if (res >= UNIX_PATH_MAX)
159     {
160       warning (_("string overflow allocating socket name"));
161       close (fd);
162       return -1;
163     }
164 
165   res = connect (fd, (struct sockaddr *) &addr, sizeof (addr));
166   if (res == -1)
167     {
168       warning (_("error connecting sync socket (%s): %s. "
169 		 "Make sure the directory exists and that it is writable."),
170 		 path, safe_strerror (errno));
171       close (fd);
172       return -1;
173     }
174 
175   return fd;
176 #else
177   return -1;
178 #endif
179 }
180 
181 /* Execute an agent command in the inferior.  PID is the value of pid
182    of the inferior.  CMD is the buffer for command.  It is assumed to
183    be at least IPA_CMD_BUF_SIZE bytes long.  GDB or GDBserver will
184    store the command into it and fetch the return result from CMD.
185    The interaction between GDB/GDBserver and the agent is synchronized
186    by a synchronization socket.  Return zero if success, otherwise
187    return non-zero.  */
188 
189 int
190 agent_run_command (int pid, char *cmd, int len)
191 {
192   int fd;
193   int tid = agent_get_helper_thread_id ();
194   ptid_t ptid = ptid_t (pid, tid);
195 
196   int ret = target_write_memory (ipa_sym_addrs.addr_cmd_buf,
197 				 (gdb_byte *) cmd, len);
198 
199   if (ret != 0)
200     {
201       warning (_("unable to write"));
202       return -1;
203     }
204 
205   DEBUG_AGENT ("agent: resumed helper thread\n");
206 
207   /* Resume helper thread.  */
208   target_continue_no_signal (ptid);
209 
210   fd = gdb_connect_sync_socket (pid);
211   if (fd >= 0)
212     {
213       char buf[1] = "";
214 
215       DEBUG_AGENT ("agent: signalling helper thread\n");
216 
217       do
218 	{
219 	  ret = write (fd, buf, 1);
220 	} while (ret == -1 && errno == EINTR);
221 
222 	DEBUG_AGENT ("agent: waiting for helper thread's response\n");
223 
224       do
225 	{
226 	  ret = read (fd, buf, 1);
227 	} while (ret == -1 && errno == EINTR);
228 
229       close (fd);
230 
231       DEBUG_AGENT ("agent: helper thread's response received\n");
232     }
233   else
234     return -1;
235 
236   /* Need to read response with the inferior stopped.  */
237   if (ptid != null_ptid)
238     {
239       /* Stop thread PTID.  */
240       DEBUG_AGENT ("agent: stop helper thread\n");
241       target_stop_and_wait (ptid);
242     }
243 
244   if (fd >= 0)
245     {
246       if (target_read_memory (ipa_sym_addrs.addr_cmd_buf, (gdb_byte *) cmd,
247 			      IPA_CMD_BUF_SIZE))
248 	{
249 	  warning (_("Error reading command response"));
250 	  return -1;
251 	}
252     }
253 
254   return 0;
255 }
256 
257 /* Each bit of it stands for a capability of agent.  */
258 static uint32_t agent_capability = 0;
259 
260 /* Return true if agent has capability AGENT_CAP, otherwise return false.  */
261 
262 bool
263 agent_capability_check (enum agent_capa agent_capa)
264 {
265   if (agent_capability == 0)
266     {
267       if (target_read_uint32 (ipa_sym_addrs.addr_capability,
268 			      &agent_capability))
269 	warning (_("Error reading capability of agent"));
270     }
271   return (agent_capability & agent_capa) != 0;
272 }
273 
274 /* Invalidate the cache of agent capability, so we'll read it from inferior
275    again.  Call it when launches a new program or reconnect to remote stub.  */
276 
277 void
278 agent_capability_invalidate (void)
279 {
280   agent_capability = 0;
281 }
282