1 /* Shared utility routines for GDB to interact with agent.
3 Copyright (C) 2009-2014 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/>. */
25 #include "inferior.h" /* for non_stop */
31 #include "filestuff.h"
36 #define DEBUG_AGENT(fmt, args...) \
38 fprintf (stderr, fmt, ##args);
40 #define DEBUG_AGENT(fmt, args...) \
42 fprintf_unfiltered (gdb_stdlog, fmt, ##args);
45 /* Global flag to determine using agent or not. */
48 /* Addresses of in-process agent's symbols both GDB and GDBserver cares
51 struct ipa_sym_addresses
53 CORE_ADDR addr_helper_thread_id;
54 CORE_ADDR addr_cmd_buf;
55 CORE_ADDR addr_capability;
58 /* Cache of the helper thread id. FIXME: this global should be made
60 static unsigned int helper_thread_id = 0;
68 IPA_SYM(helper_thread_id),
73 static struct ipa_sym_addresses ipa_sym_addrs;
75 static int all_agent_symbols_looked_up = 0;
80 return all_agent_symbols_looked_up;
83 /* Look up all symbols needed by agent. Return 0 if all the symbols are
84 found, return non-zero otherwise. */
87 agent_look_up_symbols (void *arg)
91 all_agent_symbols_looked_up = 0;
93 for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
96 (CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);
99 if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0)
101 struct minimal_symbol *sym =
102 lookup_minimal_symbol (symbol_list[i].name, NULL,
103 (struct objfile *) arg);
106 *addrp = SYMBOL_VALUE_ADDRESS (sym);
110 DEBUG_AGENT ("symbol `%s' not found\n", symbol_list[i].name);
115 all_agent_symbols_looked_up = 1;
120 agent_get_helper_thread_id (void)
122 if (helper_thread_id == 0)
125 if (read_inferior_memory (ipa_sym_addrs.addr_helper_thread_id,
126 (unsigned char *) &helper_thread_id,
127 sizeof helper_thread_id))
129 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
132 if (target_read_memory (ipa_sym_addrs.addr_helper_thread_id,
133 buf, sizeof buf) == 0)
134 helper_thread_id = extract_unsigned_integer (buf, sizeof buf,
139 warning (_("Error reading helper thread's id in lib"));
143 return helper_thread_id;
147 #include <sys/socket.h>
149 #define SOCK_DIR P_tmpdir
151 #ifndef UNIX_PATH_MAX
152 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
157 /* Connects to synchronization socket. PID is the pid of inferior, which is
158 used to set up the connection socket. */
161 gdb_connect_sync_socket (int pid)
164 struct sockaddr_un addr;
166 char path[UNIX_PATH_MAX];
168 res = xsnprintf (path, UNIX_PATH_MAX, "%s/gdb_ust%d", P_tmpdir, pid);
169 if (res >= UNIX_PATH_MAX)
172 res = fd = gdb_socket_cloexec (PF_UNIX, SOCK_STREAM, 0);
175 warning (_("error opening sync socket: %s"), strerror (errno));
179 addr.sun_family = AF_UNIX;
181 res = xsnprintf (addr.sun_path, UNIX_PATH_MAX, "%s", path);
182 if (res >= UNIX_PATH_MAX)
184 warning (_("string overflow allocating socket name"));
189 res = connect (fd, (struct sockaddr *) &addr, sizeof (addr));
192 warning (_("error connecting sync socket (%s): %s. "
193 "Make sure the directory exists and that it is writable."),
194 path, strerror (errno));
205 /* Execute an agent command in the inferior. PID is the value of pid of the
206 inferior. CMD is the buffer for command. GDB or GDBserver will store the
207 command into it and fetch the return result from CMD. The interaction
208 between GDB/GDBserver and the agent is synchronized by a synchronization
209 socket. Return zero if success, otherwise return non-zero. */
212 agent_run_command (int pid, const char *cmd, int len)
215 int tid = agent_get_helper_thread_id ();
216 ptid_t ptid = ptid_build (pid, tid, 0);
219 int ret = write_inferior_memory (ipa_sym_addrs.addr_cmd_buf,
220 (const unsigned char *) cmd, len);
222 int ret = target_write_memory (ipa_sym_addrs.addr_cmd_buf,
223 (gdb_byte *) cmd, len);
228 warning (_("unable to write"));
232 DEBUG_AGENT ("agent: resumed helper thread\n");
234 /* Resume helper thread. */
237 struct thread_resume resume_info;
239 resume_info.thread = ptid;
240 resume_info.kind = resume_continue;
241 resume_info.sig = GDB_SIGNAL_0;
242 (*the_target->resume) (&resume_info, 1);
245 target_resume (ptid, 0, GDB_SIGNAL_0);
248 fd = gdb_connect_sync_socket (pid);
254 DEBUG_AGENT ("agent: signalling helper thread\n");
258 ret = write (fd, buf, 1);
259 } while (ret == -1 && errno == EINTR);
261 DEBUG_AGENT ("agent: waiting for helper thread's response\n");
265 ret = read (fd, buf, 1);
266 } while (ret == -1 && errno == EINTR);
270 DEBUG_AGENT ("agent: helper thread's response received\n");
275 /* Need to read response with the inferior stopped. */
276 if (!ptid_equal (ptid, null_ptid))
278 struct target_waitstatus status;
279 int was_non_stop = non_stop;
280 /* Stop thread PTID. */
281 DEBUG_AGENT ("agent: stop helper thread\n");
284 struct thread_resume resume_info;
286 resume_info.thread = ptid;
287 resume_info.kind = resume_stop;
288 resume_info.sig = GDB_SIGNAL_0;
289 (*the_target->resume) (&resume_info, 1);
293 mywait (ptid, &status, 0, 0);
298 memset (&status, 0, sizeof (status));
299 target_wait (ptid, &status, 0);
301 non_stop = was_non_stop;
307 if (read_inferior_memory (ipa_sym_addrs.addr_cmd_buf,
308 (unsigned char *) cmd, IPA_CMD_BUF_SIZE))
310 if (target_read_memory (ipa_sym_addrs.addr_cmd_buf, (gdb_byte *) cmd,
314 warning (_("Error reading command response"));
322 /* Each bit of it stands for a capability of agent. */
323 static unsigned int agent_capability = 0;
325 /* Return true if agent has capability AGENT_CAP, otherwise return false. */
328 agent_capability_check (enum agent_capa agent_capa)
330 if (agent_capability == 0)
333 if (read_inferior_memory (ipa_sym_addrs.addr_capability,
334 (unsigned char *) &agent_capability,
335 sizeof agent_capability))
337 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
340 if (target_read_memory (ipa_sym_addrs.addr_capability,
341 buf, sizeof buf) == 0)
342 agent_capability = extract_unsigned_integer (buf, sizeof buf,
346 warning (_("Error reading capability of agent"));
348 return agent_capability & agent_capa;
351 /* Invalidate the cache of agent capability, so we'll read it from inferior
352 again. Call it when launches a new program or reconnect to remote stub. */
355 agent_capability_invalidate (void)
357 agent_capability = 0;