]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/common/agent.c
start change to progspace independence
[thirdparty/binutils-gdb.git] / gdb / common / agent.c
1 /* Shared utility routines for GDB to interact with agent.
2
3 Copyright (C) 2009-2014 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 #ifdef GDBSERVER
21 #include "server.h"
22 #else
23 #include "defs.h"
24 #include "target.h"
25 #include "inferior.h" /* for non_stop */
26 #include "objfiles.h"
27 #endif
28
29 #include <string.h>
30 #include <unistd.h>
31 #include "agent.h"
32 #include "filestuff.h"
33
34 int debug_agent = 0;
35
36 #ifdef GDBSERVER
37 #define DEBUG_AGENT(fmt, args...) \
38 if (debug_agent) \
39 fprintf (stderr, fmt, ##args);
40 #else
41 #define DEBUG_AGENT(fmt, args...) \
42 if (debug_agent) \
43 fprintf_unfiltered (gdb_stdlog, fmt, ##args);
44 #endif
45
46 /* Global flag to determine using agent or not. */
47 int use_agent = 0;
48
49 /* Addresses of in-process agent's symbols both GDB and GDBserver cares
50 about. */
51
52 struct ipa_sym_addresses
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 unsigned int helper_thread_id = 0;
62
63 static struct
64 {
65 const char *name;
66 int offset;
67 int required;
68 } symbol_list[] = {
69 IPA_SYM(helper_thread_id),
70 IPA_SYM(cmd_buf),
71 IPA_SYM(capability),
72 };
73
74 static struct ipa_sym_addresses ipa_sym_addrs;
75
76 static int all_agent_symbols_looked_up = 0;
77
78 int
79 agent_loaded_p (void)
80 {
81 return all_agent_symbols_looked_up;
82 }
83
84 /* Look up all symbols needed by agent. Return 0 if all the symbols are
85 found, return non-zero otherwise. */
86
87 int
88 agent_look_up_symbols (void *arg)
89 {
90 int i;
91
92 all_agent_symbols_looked_up = 0;
93
94 for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
95 {
96 CORE_ADDR *addrp =
97 (CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);
98 #ifdef GDBSERVER
99
100 if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0)
101 #else
102 struct bound_minimal_symbol sym =
103 lookup_minimal_symbol (symbol_list[i].name, NULL,
104 (struct objfile *) arg);
105
106 if (sym.minsym != NULL)
107 *addrp = BMSYMBOL_VALUE_ADDRESS (sym);
108 else
109 #endif
110 {
111 DEBUG_AGENT ("symbol `%s' not found\n", symbol_list[i].name);
112 return -1;
113 }
114 }
115
116 all_agent_symbols_looked_up = 1;
117 return 0;
118 }
119
120 static unsigned int
121 agent_get_helper_thread_id (void)
122 {
123 if (helper_thread_id == 0)
124 {
125 #ifdef GDBSERVER
126 if (read_inferior_memory (ipa_sym_addrs.addr_helper_thread_id,
127 (unsigned char *) &helper_thread_id,
128 sizeof helper_thread_id))
129 #else
130 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
131 gdb_byte buf[4];
132
133 if (target_read_memory (ipa_sym_addrs.addr_helper_thread_id,
134 buf, sizeof buf) == 0)
135 helper_thread_id = extract_unsigned_integer (buf, sizeof buf,
136 byte_order);
137 else
138 #endif
139 {
140 warning (_("Error reading helper thread's id in lib"));
141 }
142 }
143
144 return helper_thread_id;
145 }
146
147 #ifdef HAVE_SYS_UN_H
148 #include <sys/socket.h>
149 #include <sys/un.h>
150 #define SOCK_DIR P_tmpdir
151
152 #ifndef UNIX_PATH_MAX
153 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
154 #endif
155
156 #endif
157
158 /* Connects to synchronization socket. PID is the pid of inferior, which is
159 used to set up the connection socket. */
160
161 static int
162 gdb_connect_sync_socket (int pid)
163 {
164 #ifdef HAVE_SYS_UN_H
165 struct sockaddr_un addr;
166 int res, fd;
167 char path[UNIX_PATH_MAX];
168
169 res = xsnprintf (path, UNIX_PATH_MAX, "%s/gdb_ust%d", P_tmpdir, pid);
170 if (res >= UNIX_PATH_MAX)
171 return -1;
172
173 res = fd = gdb_socket_cloexec (PF_UNIX, SOCK_STREAM, 0);
174 if (res == -1)
175 {
176 warning (_("error opening sync socket: %s"), strerror (errno));
177 return -1;
178 }
179
180 addr.sun_family = AF_UNIX;
181
182 res = xsnprintf (addr.sun_path, UNIX_PATH_MAX, "%s", path);
183 if (res >= UNIX_PATH_MAX)
184 {
185 warning (_("string overflow allocating socket name"));
186 close (fd);
187 return -1;
188 }
189
190 res = connect (fd, (struct sockaddr *) &addr, sizeof (addr));
191 if (res == -1)
192 {
193 warning (_("error connecting sync socket (%s): %s. "
194 "Make sure the directory exists and that it is writable."),
195 path, strerror (errno));
196 close (fd);
197 return -1;
198 }
199
200 return fd;
201 #else
202 return -1;
203 #endif
204 }
205
206 /* Execute an agent command in the inferior. PID is the value of pid of the
207 inferior. CMD is the buffer for command. GDB or GDBserver will store the
208 command into it and fetch the return result from CMD. The interaction
209 between GDB/GDBserver and the agent is synchronized by a synchronization
210 socket. Return zero if success, otherwise return non-zero. */
211
212 int
213 agent_run_command (int pid, const char *cmd, int len)
214 {
215 int fd;
216 int tid = agent_get_helper_thread_id ();
217 ptid_t ptid = ptid_build (pid, tid, 0);
218
219 #ifdef GDBSERVER
220 int ret = write_inferior_memory (ipa_sym_addrs.addr_cmd_buf,
221 (const unsigned char *) cmd, len);
222 #else
223 int ret = target_write_memory (ipa_sym_addrs.addr_cmd_buf,
224 (gdb_byte *) cmd, len);
225 #endif
226
227 if (ret != 0)
228 {
229 warning (_("unable to write"));
230 return -1;
231 }
232
233 DEBUG_AGENT ("agent: resumed helper thread\n");
234
235 /* Resume helper thread. */
236 #ifdef GDBSERVER
237 {
238 struct thread_resume resume_info;
239
240 resume_info.thread = ptid;
241 resume_info.kind = resume_continue;
242 resume_info.sig = GDB_SIGNAL_0;
243 (*the_target->resume) (&resume_info, 1);
244 }
245 #else
246 target_resume (ptid, 0, GDB_SIGNAL_0);
247 #endif
248
249 fd = gdb_connect_sync_socket (pid);
250 if (fd >= 0)
251 {
252 char buf[1] = "";
253 int ret;
254
255 DEBUG_AGENT ("agent: signalling helper thread\n");
256
257 do
258 {
259 ret = write (fd, buf, 1);
260 } while (ret == -1 && errno == EINTR);
261
262 DEBUG_AGENT ("agent: waiting for helper thread's response\n");
263
264 do
265 {
266 ret = read (fd, buf, 1);
267 } while (ret == -1 && errno == EINTR);
268
269 close (fd);
270
271 DEBUG_AGENT ("agent: helper thread's response received\n");
272 }
273 else
274 return -1;
275
276 /* Need to read response with the inferior stopped. */
277 if (!ptid_equal (ptid, null_ptid))
278 {
279 struct target_waitstatus status;
280 int was_non_stop = non_stop;
281 /* Stop thread PTID. */
282 DEBUG_AGENT ("agent: stop helper thread\n");
283 #ifdef GDBSERVER
284 {
285 struct thread_resume resume_info;
286
287 resume_info.thread = ptid;
288 resume_info.kind = resume_stop;
289 resume_info.sig = GDB_SIGNAL_0;
290 (*the_target->resume) (&resume_info, 1);
291 }
292
293 non_stop = 1;
294 mywait (ptid, &status, 0, 0);
295 #else
296 non_stop = 1;
297 target_stop (ptid);
298
299 memset (&status, 0, sizeof (status));
300 target_wait (ptid, &status, 0);
301 #endif
302 non_stop = was_non_stop;
303 }
304
305 if (fd >= 0)
306 {
307 #ifdef GDBSERVER
308 if (read_inferior_memory (ipa_sym_addrs.addr_cmd_buf,
309 (unsigned char *) cmd, IPA_CMD_BUF_SIZE))
310 #else
311 if (target_read_memory (ipa_sym_addrs.addr_cmd_buf, (gdb_byte *) cmd,
312 IPA_CMD_BUF_SIZE))
313 #endif
314 {
315 warning (_("Error reading command response"));
316 return -1;
317 }
318 }
319
320 return 0;
321 }
322
323 /* Each bit of it stands for a capability of agent. */
324 static unsigned int agent_capability = 0;
325
326 /* Return true if agent has capability AGENT_CAP, otherwise return false. */
327
328 int
329 agent_capability_check (enum agent_capa agent_capa)
330 {
331 if (agent_capability == 0)
332 {
333 #ifdef GDBSERVER
334 if (read_inferior_memory (ipa_sym_addrs.addr_capability,
335 (unsigned char *) &agent_capability,
336 sizeof agent_capability))
337 #else
338 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
339 gdb_byte buf[4];
340
341 if (target_read_memory (ipa_sym_addrs.addr_capability,
342 buf, sizeof buf) == 0)
343 agent_capability = extract_unsigned_integer (buf, sizeof buf,
344 byte_order);
345 else
346 #endif
347 warning (_("Error reading capability of agent"));
348 }
349 return agent_capability & agent_capa;
350 }
351
352 /* Invalidate the cache of agent capability, so we'll read it from inferior
353 again. Call it when launches a new program or reconnect to remote stub. */
354
355 void
356 agent_capability_invalidate (void)
357 {
358 agent_capability = 0;
359 }