Add end_psymtab_common, have all debug info readers call it.
[deliverable/binutils-gdb.git] / gdb / common / agent.c
CommitLineData
2fa291ac
YQ
1/* Shared utility routines for GDB to interact with agent.
2
32d0add0 3 Copyright (C) 2009-2015 Free Software Foundation, Inc.
2fa291ac
YQ
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
727605ca 20#include "common-defs.h"
721ec300 21#include "target/target.h"
bd9269f7 22#include "common/symbol.h"
2fa291ac
YQ
23#include <unistd.h>
24#include "agent.h"
614c279d 25#include "filestuff.h"
2fa291ac
YQ
26
27int debug_agent = 0;
28
34abf635
GB
29/* A stdarg wrapper for debug_vprintf. */
30
31static void ATTRIBUTE_PRINTF (1, 2)
32debug_agent_printf (const char *fmt, ...)
33{
34 va_list ap;
35
36 if (!debug_agent)
37 return;
38 va_start (ap, fmt);
39 debug_vprintf (fmt, ap);
40 va_end (ap);
41}
42
43#define DEBUG_AGENT debug_agent_printf
2fa291ac 44
d1feda86
YQ
45/* Global flag to determine using agent or not. */
46int use_agent = 0;
47
2fa291ac
YQ
48/* Addresses of in-process agent's symbols both GDB and GDBserver cares
49 about. */
50
51struct ipa_sym_addresses
52{
53 CORE_ADDR addr_helper_thread_id;
54 CORE_ADDR addr_cmd_buf;
8ffcbaaf 55 CORE_ADDR addr_capability;
2fa291ac
YQ
56};
57
58/* Cache of the helper thread id. FIXME: this global should be made
59 per-process. */
721ec300 60static uint32_t helper_thread_id = 0;
2fa291ac
YQ
61
62static struct
63{
64 const char *name;
65 int offset;
2fa291ac
YQ
66} symbol_list[] = {
67 IPA_SYM(helper_thread_id),
68 IPA_SYM(cmd_buf),
8ffcbaaf 69 IPA_SYM(capability),
2fa291ac
YQ
70};
71
72static struct ipa_sym_addresses ipa_sym_addrs;
73
58b4daa5
YQ
74static int all_agent_symbols_looked_up = 0;
75
76int
77agent_loaded_p (void)
78{
79 return all_agent_symbols_looked_up;
80}
81
2fa291ac
YQ
82/* Look up all symbols needed by agent. Return 0 if all the symbols are
83 found, return non-zero otherwise. */
84
85int
5808517f 86agent_look_up_symbols (void *arg)
2fa291ac
YQ
87{
88 int i;
89
58b4daa5
YQ
90 all_agent_symbols_looked_up = 0;
91
2fa291ac
YQ
92 for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
93 {
94 CORE_ADDR *addrp =
95 (CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);
2fa291ac 96
bd9269f7
GB
97 if (find_minimal_symbol_address (symbol_list[i].name, addrp,
98 arg) != 0)
2fa291ac
YQ
99 {
100 DEBUG_AGENT ("symbol `%s' not found\n", symbol_list[i].name);
101 return -1;
102 }
103 }
104
58b4daa5 105 all_agent_symbols_looked_up = 1;
2fa291ac
YQ
106 return 0;
107}
108
109static unsigned int
110agent_get_helper_thread_id (void)
111{
112 if (helper_thread_id == 0)
113 {
721ec300
GB
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"));
2fa291ac
YQ
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
136static int
137gdb_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
614c279d 148 res = fd = gdb_socket_cloexec (PF_UNIX, SOCK_STREAM, 0);
2fa291ac
YQ
149 if (res == -1)
150 {
87399aa1 151 warning (_("error opening sync socket: %s"), strerror (errno));
2fa291ac
YQ
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 {
87399aa1 160 warning (_("string overflow allocating socket name"));
2fa291ac
YQ
161 close (fd);
162 return -1;
163 }
164
165 res = connect (fd, (struct sockaddr *) &addr, sizeof (addr));
166 if (res == -1)
167 {
87399aa1
YQ
168 warning (_("error connecting sync socket (%s): %s. "
169 "Make sure the directory exists and that it is writable."),
170 path, strerror (errno));
2fa291ac
YQ
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 of the
182 inferior. CMD is the buffer for command. GDB or GDBserver will store the
183 command into it and fetch the return result from CMD. The interaction
184 between GDB/GDBserver and the agent is synchronized by a synchronization
185 socket. Return zero if success, otherwise return non-zero. */
186
187int
42476b70 188agent_run_command (int pid, const char *cmd, int len)
2fa291ac
YQ
189{
190 int fd;
191 int tid = agent_get_helper_thread_id ();
192 ptid_t ptid = ptid_build (pid, tid, 0);
2fa291ac 193
fda0389f
PA
194 int ret = target_write_memory (ipa_sym_addrs.addr_cmd_buf,
195 (gdb_byte *) cmd, len);
2fa291ac
YQ
196
197 if (ret != 0)
198 {
87399aa1 199 warning (_("unable to write"));
2fa291ac
YQ
200 return -1;
201 }
202
203 DEBUG_AGENT ("agent: resumed helper thread\n");
204
205 /* Resume helper thread. */
03f4463b 206 target_continue_no_signal (ptid);
2fa291ac
YQ
207
208 fd = gdb_connect_sync_socket (pid);
209 if (fd >= 0)
210 {
211 char buf[1] = "";
212 int ret;
213
214 DEBUG_AGENT ("agent: signalling helper thread\n");
215
216 do
217 {
218 ret = write (fd, buf, 1);
219 } while (ret == -1 && errno == EINTR);
220
221 DEBUG_AGENT ("agent: waiting for helper thread's response\n");
222
223 do
224 {
225 ret = read (fd, buf, 1);
226 } while (ret == -1 && errno == EINTR);
227
228 close (fd);
229
230 DEBUG_AGENT ("agent: helper thread's response received\n");
231 }
232 else
233 return -1;
234
235 /* Need to read response with the inferior stopped. */
236 if (!ptid_equal (ptid, null_ptid))
237 {
2fa291ac
YQ
238 /* Stop thread PTID. */
239 DEBUG_AGENT ("agent: stop helper thread\n");
03f4463b 240 target_stop_and_wait (ptid);
2fa291ac
YQ
241 }
242
243 if (fd >= 0)
244 {
2fa291ac
YQ
245 if (target_read_memory (ipa_sym_addrs.addr_cmd_buf, (gdb_byte *) cmd,
246 IPA_CMD_BUF_SIZE))
2fa291ac 247 {
87399aa1 248 warning (_("Error reading command response"));
2fa291ac
YQ
249 return -1;
250 }
251 }
252
253 return 0;
254}
8ffcbaaf
YQ
255
256/* Each bit of it stands for a capability of agent. */
721ec300 257static uint32_t agent_capability = 0;
8ffcbaaf
YQ
258
259/* Return true if agent has capability AGENT_CAP, otherwise return false. */
260
261int
262agent_capability_check (enum agent_capa agent_capa)
263{
264 if (agent_capability == 0)
265 {
721ec300
GB
266 if (target_read_uint32 (ipa_sym_addrs.addr_capability,
267 &agent_capability))
87399aa1 268 warning (_("Error reading capability of agent"));
8ffcbaaf
YQ
269 }
270 return agent_capability & agent_capa;
271}
272
273/* Invalidate the cache of agent capability, so we'll read it from inferior
274 again. Call it when launches a new program or reconnect to remote stub. */
275
276void
277agent_capability_invalidate (void)
278{
279 agent_capability = 0;
280}
This page took 0.281379 seconds and 4 git commands to generate.