1 /* Shared utility routines for GDB to interact with agent.
3 Copyright (C) 2009-2012 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 */
35 #define DEBUG_AGENT(fmt, args...) \
37 fprintf (stderr, fmt, ##args);
39 #define DEBUG_AGENT(fmt, args...) \
41 fprintf_unfiltered (gdb_stdlog, fmt, ##args);
44 /* Global flag to determine using agent or not. */
47 /* Addresses of in-process agent's symbols both GDB and GDBserver cares
50 struct ipa_sym_addresses
52 CORE_ADDR addr_helper_thread_id
;
53 CORE_ADDR addr_cmd_buf
;
54 CORE_ADDR addr_capability
;
57 /* Cache of the helper thread id. FIXME: this global should be made
59 static unsigned int helper_thread_id
= 0;
67 IPA_SYM(helper_thread_id
),
72 static struct ipa_sym_addresses ipa_sym_addrs
;
74 static int all_agent_symbols_looked_up
= 0;
79 return all_agent_symbols_looked_up
;
82 /* Look up all symbols needed by agent. Return 0 if all the symbols are
83 found, return non-zero otherwise. */
86 agent_look_up_symbols (void *arg
)
90 all_agent_symbols_looked_up
= 0;
92 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
95 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
98 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
100 struct minimal_symbol
*sym
=
101 lookup_minimal_symbol (symbol_list
[i
].name
, NULL
,
102 (struct objfile
*) arg
);
105 *addrp
= SYMBOL_VALUE_ADDRESS (sym
);
109 DEBUG_AGENT ("symbol `%s' not found\n", symbol_list
[i
].name
);
114 all_agent_symbols_looked_up
= 1;
119 agent_get_helper_thread_id (void)
121 if (helper_thread_id
== 0)
124 if (read_inferior_memory (ipa_sym_addrs
.addr_helper_thread_id
,
125 (unsigned char *) &helper_thread_id
,
126 sizeof helper_thread_id
))
128 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
131 if (target_read_memory (ipa_sym_addrs
.addr_helper_thread_id
,
132 buf
, sizeof buf
) == 0)
133 helper_thread_id
= extract_unsigned_integer (buf
, sizeof buf
,
138 warning (_("Error reading helper thread's id in lib"));
142 return helper_thread_id
;
146 #include <sys/socket.h>
148 #define SOCK_DIR P_tmpdir
150 #ifndef UNIX_PATH_MAX
151 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
156 /* Connects to synchronization socket. PID is the pid of inferior, which is
157 used to set up the connection socket. */
160 gdb_connect_sync_socket (int pid
)
163 struct sockaddr_un addr
;
165 char path
[UNIX_PATH_MAX
];
167 res
= xsnprintf (path
, UNIX_PATH_MAX
, "%s/gdb_ust%d", P_tmpdir
, pid
);
168 if (res
>= UNIX_PATH_MAX
)
171 res
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
174 warning (_("error opening sync socket: %s"), strerror (errno
));
178 addr
.sun_family
= AF_UNIX
;
180 res
= xsnprintf (addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
181 if (res
>= UNIX_PATH_MAX
)
183 warning (_("string overflow allocating socket name"));
188 res
= connect (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
191 warning (_("error connecting sync socket (%s): %s. "
192 "Make sure the directory exists and that it is writable."),
193 path
, strerror (errno
));
204 /* Execute an agent command in the inferior. PID is the value of pid of the
205 inferior. CMD is the buffer for command. GDB or GDBserver will store the
206 command into it and fetch the return result from CMD. The interaction
207 between GDB/GDBserver and the agent is synchronized by a synchronization
208 socket. Return zero if success, otherwise return non-zero. */
211 agent_run_command (int pid
, const char *cmd
, int len
)
214 int tid
= agent_get_helper_thread_id ();
215 ptid_t ptid
= ptid_build (pid
, tid
, 0);
218 int ret
= write_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
219 (const unsigned char *) cmd
, len
);
221 int ret
= target_write_memory (ipa_sym_addrs
.addr_cmd_buf
, cmd
, len
);
226 warning (_("unable to write"));
230 DEBUG_AGENT ("agent: resumed helper thread\n");
232 /* Resume helper thread. */
235 struct thread_resume resume_info
;
237 resume_info
.thread
= ptid
;
238 resume_info
.kind
= resume_continue
;
239 resume_info
.sig
= TARGET_SIGNAL_0
;
240 (*the_target
->resume
) (&resume_info
, 1);
243 target_resume (ptid
, 0, TARGET_SIGNAL_0
);
246 fd
= gdb_connect_sync_socket (pid
);
252 DEBUG_AGENT ("agent: signalling helper thread\n");
256 ret
= write (fd
, buf
, 1);
257 } while (ret
== -1 && errno
== EINTR
);
259 DEBUG_AGENT ("agent: waiting for helper thread's response\n");
263 ret
= read (fd
, buf
, 1);
264 } while (ret
== -1 && errno
== EINTR
);
268 DEBUG_AGENT ("agent: helper thread's response received\n");
273 /* Need to read response with the inferior stopped. */
274 if (!ptid_equal (ptid
, null_ptid
))
276 struct target_waitstatus status
;
277 int was_non_stop
= non_stop
;
278 /* Stop thread PTID. */
279 DEBUG_AGENT ("agent: stop helper thread\n");
282 struct thread_resume resume_info
;
284 resume_info
.thread
= ptid
;
285 resume_info
.kind
= resume_stop
;
286 resume_info
.sig
= TARGET_SIGNAL_0
;
287 (*the_target
->resume
) (&resume_info
, 1);
291 mywait (ptid
, &status
, 0, 0);
296 memset (&status
, 0, sizeof (status
));
297 target_wait (ptid
, &status
, 0);
299 non_stop
= was_non_stop
;
305 if (read_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
306 (unsigned char *) cmd
, IPA_CMD_BUF_SIZE
))
308 if (target_read_memory (ipa_sym_addrs
.addr_cmd_buf
, (gdb_byte
*) cmd
,
312 warning (_("Error reading command response"));
320 /* Each bit of it stands for a capability of agent. */
321 static unsigned int agent_capability
= 0;
323 /* Return true if agent has capability AGENT_CAP, otherwise return false. */
326 agent_capability_check (enum agent_capa agent_capa
)
328 if (agent_capability
== 0)
331 if (read_inferior_memory (ipa_sym_addrs
.addr_capability
,
332 (unsigned char *) &agent_capability
,
333 sizeof agent_capability
))
335 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
338 if (target_read_memory (ipa_sym_addrs
.addr_capability
,
339 buf
, sizeof buf
) == 0)
340 agent_capability
= extract_unsigned_integer (buf
, sizeof buf
,
344 warning (_("Error reading capability of agent"));
346 return agent_capability
& agent_capa
;
349 /* Invalidate the cache of agent capability, so we'll read it from inferior
350 again. Call it when launches a new program or reconnect to remote stub. */
353 agent_capability_invalidate (void)
355 agent_capability
= 0;