1 /* Shared utility routines for GDB to interact with agent.
3 Copyright (C) 2009-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 "common-defs.h"
21 #include "target/target.h"
22 #include "gdbsupport/symbol.h"
24 #include "filestuff.h"
26 #define IPA_SYM_STRUCT_NAME ipa_sym_addresses_common
29 bool debug_agent
= false;
31 /* A stdarg wrapper for debug_vprintf. */
33 static void ATTRIBUTE_PRINTF (1, 2)
34 debug_agent_printf (const char *fmt
, ...)
41 debug_vprintf (fmt
, ap
);
45 #define DEBUG_AGENT debug_agent_printf
47 /* Global flag to determine using agent or not. */
48 bool use_agent
= false;
50 /* Addresses of in-process agent's symbols both GDB and GDBserver cares
53 struct ipa_sym_addresses_common
55 CORE_ADDR addr_helper_thread_id
;
56 CORE_ADDR addr_cmd_buf
;
57 CORE_ADDR addr_capability
;
60 /* Cache of the helper thread id. FIXME: this global should be made
62 static uint32_t helper_thread_id
= 0;
69 IPA_SYM(helper_thread_id
),
74 static struct ipa_sym_addresses_common ipa_sym_addrs
;
76 static bool all_agent_symbols_looked_up
= false;
81 return all_agent_symbols_looked_up
;
84 /* Look up all symbols needed by agent. Return 0 if all the symbols are
85 found, return non-zero otherwise. */
88 agent_look_up_symbols (void *arg
)
90 all_agent_symbols_looked_up
= false;
92 for (int i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
95 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
96 struct objfile
*objfile
= (struct objfile
*) arg
;
98 if (find_minimal_symbol_address (symbol_list
[i
].name
, addrp
,
101 DEBUG_AGENT ("symbol `%s' not found\n", symbol_list
[i
].name
);
106 all_agent_symbols_looked_up
= true;
111 agent_get_helper_thread_id (void)
113 if (helper_thread_id
== 0)
115 if (target_read_uint32 (ipa_sym_addrs
.addr_helper_thread_id
,
117 warning (_("Error reading helper thread's id in lib"));
120 return helper_thread_id
;
124 #include <sys/socket.h>
126 #define SOCK_DIR P_tmpdir
128 #ifndef UNIX_PATH_MAX
129 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
134 /* Connects to synchronization socket. PID is the pid of inferior, which is
135 used to set up the connection socket. */
138 gdb_connect_sync_socket (int pid
)
141 struct sockaddr_un addr
= {};
143 char path
[UNIX_PATH_MAX
];
145 res
= xsnprintf (path
, UNIX_PATH_MAX
, "%s/gdb_ust%d", P_tmpdir
, pid
);
146 if (res
>= UNIX_PATH_MAX
)
149 res
= fd
= gdb_socket_cloexec (PF_UNIX
, SOCK_STREAM
, 0);
152 warning (_("error opening sync socket: %s"), safe_strerror (errno
));
156 addr
.sun_family
= AF_UNIX
;
158 res
= xsnprintf (addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
159 if (res
>= UNIX_PATH_MAX
)
161 warning (_("string overflow allocating socket name"));
166 res
= connect (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
169 warning (_("error connecting sync socket (%s): %s. "
170 "Make sure the directory exists and that it is writable."),
171 path
, safe_strerror (errno
));
182 /* Execute an agent command in the inferior. PID is the value of pid
183 of the inferior. CMD is the buffer for command. It is assumed to
184 be at least IPA_CMD_BUF_SIZE bytes long. GDB or GDBserver will
185 store the command into it and fetch the return result from CMD.
186 The interaction between GDB/GDBserver and the agent is synchronized
187 by a synchronization socket. Return zero if success, otherwise
191 agent_run_command (int pid
, char *cmd
, int len
)
194 int tid
= agent_get_helper_thread_id ();
195 ptid_t ptid
= ptid_t (pid
, tid
);
197 int ret
= target_write_memory (ipa_sym_addrs
.addr_cmd_buf
,
198 (gdb_byte
*) cmd
, len
);
202 warning (_("unable to write"));
206 DEBUG_AGENT ("agent: resumed helper thread\n");
208 /* Resume helper thread. */
209 target_continue_no_signal (ptid
);
211 fd
= gdb_connect_sync_socket (pid
);
216 DEBUG_AGENT ("agent: signalling helper thread\n");
220 ret
= write (fd
, buf
, 1);
221 } while (ret
== -1 && errno
== EINTR
);
223 DEBUG_AGENT ("agent: waiting for helper thread's response\n");
227 ret
= read (fd
, buf
, 1);
228 } while (ret
== -1 && errno
== EINTR
);
232 DEBUG_AGENT ("agent: helper thread's response received\n");
237 /* Need to read response with the inferior stopped. */
238 if (ptid
!= null_ptid
)
240 /* Stop thread PTID. */
241 DEBUG_AGENT ("agent: stop helper thread\n");
242 target_stop_and_wait (ptid
);
247 if (target_read_memory (ipa_sym_addrs
.addr_cmd_buf
, (gdb_byte
*) cmd
,
250 warning (_("Error reading command response"));
258 /* Each bit of it stands for a capability of agent. */
259 static uint32_t agent_capability
= 0;
261 /* Return true if agent has capability AGENT_CAP, otherwise return false. */
264 agent_capability_check (enum agent_capa agent_capa
)
266 if (agent_capability
== 0)
268 if (target_read_uint32 (ipa_sym_addrs
.addr_capability
,
270 warning (_("Error reading capability of agent"));
272 return (agent_capability
& agent_capa
) != 0;
275 /* Invalidate the cache of agent capability, so we'll read it from inferior
276 again. Call it when launches a new program or reconnect to remote stub. */
279 agent_capability_invalidate (void)
281 agent_capability
= 0;