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