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/>. */
26 #include "target/target.h"
29 #include "filestuff.h"
33 /* A stdarg wrapper for debug_vprintf. */
35 static void ATTRIBUTE_PRINTF (1, 2)
36 debug_agent_printf (const char *fmt, ...)
43 debug_vprintf (fmt, ap);
47 #define DEBUG_AGENT debug_agent_printf
49 /* Global flag to determine using agent or not. */
52 /* Addresses of in-process agent's symbols both GDB and GDBserver cares
55 struct ipa_sym_addresses
57 CORE_ADDR addr_helper_thread_id;
58 CORE_ADDR addr_cmd_buf;
59 CORE_ADDR addr_capability;
62 /* Cache of the helper thread id. FIXME: this global should be made
64 static uint32_t helper_thread_id = 0;
72 IPA_SYM(helper_thread_id),
77 static struct ipa_sym_addresses ipa_sym_addrs;
79 static int all_agent_symbols_looked_up = 0;
84 return all_agent_symbols_looked_up;
87 /* Look up all symbols needed by agent. Return 0 if all the symbols are
88 found, return non-zero otherwise. */
91 agent_look_up_symbols (void *arg)
95 all_agent_symbols_looked_up = 0;
97 for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
100 (CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);
103 if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0)
105 struct bound_minimal_symbol sym =
106 lookup_minimal_symbol (symbol_list[i].name, NULL,
107 (struct objfile *) arg);
109 if (sym.minsym != NULL)
110 *addrp = BMSYMBOL_VALUE_ADDRESS (sym);
114 DEBUG_AGENT ("symbol `%s' not found\n", symbol_list[i].name);
119 all_agent_symbols_looked_up = 1;
124 agent_get_helper_thread_id (void)
126 if (helper_thread_id == 0)
128 if (target_read_uint32 (ipa_sym_addrs.addr_helper_thread_id,
130 warning (_("Error reading helper thread's id in lib"));
133 return helper_thread_id;
137 #include <sys/socket.h>
139 #define SOCK_DIR P_tmpdir
141 #ifndef UNIX_PATH_MAX
142 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
147 /* Connects to synchronization socket. PID is the pid of inferior, which is
148 used to set up the connection socket. */
151 gdb_connect_sync_socket (int pid)
154 struct sockaddr_un addr;
156 char path[UNIX_PATH_MAX];
158 res = xsnprintf (path, UNIX_PATH_MAX, "%s/gdb_ust%d", P_tmpdir, pid);
159 if (res >= UNIX_PATH_MAX)
162 res = fd = gdb_socket_cloexec (PF_UNIX, SOCK_STREAM, 0);
165 warning (_("error opening sync socket: %s"), strerror (errno));
169 addr.sun_family = AF_UNIX;
171 res = xsnprintf (addr.sun_path, UNIX_PATH_MAX, "%s", path);
172 if (res >= UNIX_PATH_MAX)
174 warning (_("string overflow allocating socket name"));
179 res = connect (fd, (struct sockaddr *) &addr, sizeof (addr));
182 warning (_("error connecting sync socket (%s): %s. "
183 "Make sure the directory exists and that it is writable."),
184 path, strerror (errno));
195 /* Execute an agent command in the inferior. PID is the value of pid of the
196 inferior. CMD is the buffer for command. GDB or GDBserver will store the
197 command into it and fetch the return result from CMD. The interaction
198 between GDB/GDBserver and the agent is synchronized by a synchronization
199 socket. Return zero if success, otherwise return non-zero. */
202 agent_run_command (int pid, const char *cmd, int len)
205 int tid = agent_get_helper_thread_id ();
206 ptid_t ptid = ptid_build (pid, tid, 0);
208 int ret = target_write_memory (ipa_sym_addrs.addr_cmd_buf,
209 (gdb_byte *) cmd, len);
213 warning (_("unable to write"));
217 DEBUG_AGENT ("agent: resumed helper thread\n");
219 /* Resume helper thread. */
220 target_continue_ptid (ptid);
222 fd = gdb_connect_sync_socket (pid);
228 DEBUG_AGENT ("agent: signalling helper thread\n");
232 ret = write (fd, buf, 1);
233 } while (ret == -1 && errno == EINTR);
235 DEBUG_AGENT ("agent: waiting for helper thread's response\n");
239 ret = read (fd, buf, 1);
240 } while (ret == -1 && errno == EINTR);
244 DEBUG_AGENT ("agent: helper thread's response received\n");
249 /* Need to read response with the inferior stopped. */
250 if (!ptid_equal (ptid, null_ptid))
252 /* Stop thread PTID. */
253 DEBUG_AGENT ("agent: stop helper thread\n");
254 target_stop_ptid (ptid);
259 if (target_read_memory (ipa_sym_addrs.addr_cmd_buf, (gdb_byte *) cmd,
262 warning (_("Error reading command response"));
270 /* Each bit of it stands for a capability of agent. */
271 static uint32_t agent_capability = 0;
273 /* Return true if agent has capability AGENT_CAP, otherwise return false. */
276 agent_capability_check (enum agent_capa agent_capa)
278 if (agent_capability == 0)
280 if (target_read_uint32 (ipa_sym_addrs.addr_capability,
282 warning (_("Error reading capability of agent"));
284 return agent_capability & agent_capa;
287 /* Invalidate the cache of agent capability, so we'll read it from inferior
288 again. Call it when launches a new program or reconnect to remote stub. */
291 agent_capability_invalidate (void)
293 agent_capability = 0;