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/>. */
27 #include "target/target.h"
30 #include "filestuff.h"
34 /* A stdarg wrapper for debug_vprintf. */
36 static void ATTRIBUTE_PRINTF (1, 2)
37 debug_agent_printf (const char *fmt
, ...)
44 debug_vprintf (fmt
, ap
);
48 #define DEBUG_AGENT debug_agent_printf
50 /* Global flag to determine using agent or not. */
53 /* Addresses of in-process agent's symbols both GDB and GDBserver cares
56 struct ipa_sym_addresses
58 CORE_ADDR addr_helper_thread_id
;
59 CORE_ADDR addr_cmd_buf
;
60 CORE_ADDR addr_capability
;
63 /* Cache of the helper thread id. FIXME: this global should be made
65 static uint32_t helper_thread_id
= 0;
73 IPA_SYM(helper_thread_id
),
78 static struct ipa_sym_addresses ipa_sym_addrs
;
80 static int all_agent_symbols_looked_up
= 0;
85 return all_agent_symbols_looked_up
;
88 /* Look up all symbols needed by agent. Return 0 if all the symbols are
89 found, return non-zero otherwise. */
92 agent_look_up_symbols (void *arg
)
96 all_agent_symbols_looked_up
= 0;
98 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
101 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
104 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
106 struct bound_minimal_symbol sym
=
107 lookup_minimal_symbol (symbol_list
[i
].name
, NULL
,
108 (struct objfile
*) arg
);
110 if (sym
.minsym
!= NULL
)
111 *addrp
= BMSYMBOL_VALUE_ADDRESS (sym
);
115 DEBUG_AGENT ("symbol `%s' not found\n", symbol_list
[i
].name
);
120 all_agent_symbols_looked_up
= 1;
125 agent_get_helper_thread_id (void)
127 if (helper_thread_id
== 0)
129 if (target_read_uint32 (ipa_sym_addrs
.addr_helper_thread_id
,
131 warning (_("Error reading helper thread's id in lib"));
134 return helper_thread_id
;
138 #include <sys/socket.h>
140 #define SOCK_DIR P_tmpdir
142 #ifndef UNIX_PATH_MAX
143 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
148 /* Connects to synchronization socket. PID is the pid of inferior, which is
149 used to set up the connection socket. */
152 gdb_connect_sync_socket (int pid
)
155 struct sockaddr_un addr
;
157 char path
[UNIX_PATH_MAX
];
159 res
= xsnprintf (path
, UNIX_PATH_MAX
, "%s/gdb_ust%d", P_tmpdir
, pid
);
160 if (res
>= UNIX_PATH_MAX
)
163 res
= fd
= gdb_socket_cloexec (PF_UNIX
, SOCK_STREAM
, 0);
166 warning (_("error opening sync socket: %s"), strerror (errno
));
170 addr
.sun_family
= AF_UNIX
;
172 res
= xsnprintf (addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
173 if (res
>= UNIX_PATH_MAX
)
175 warning (_("string overflow allocating socket name"));
180 res
= connect (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
183 warning (_("error connecting sync socket (%s): %s. "
184 "Make sure the directory exists and that it is writable."),
185 path
, strerror (errno
));
196 /* Execute an agent command in the inferior. PID is the value of pid of the
197 inferior. CMD is the buffer for command. GDB or GDBserver will store the
198 command into it and fetch the return result from CMD. The interaction
199 between GDB/GDBserver and the agent is synchronized by a synchronization
200 socket. Return zero if success, otherwise return non-zero. */
203 agent_run_command (int pid
, const char *cmd
, int len
)
206 int tid
= agent_get_helper_thread_id ();
207 ptid_t ptid
= ptid_build (pid
, tid
, 0);
209 int ret
= target_write_memory (ipa_sym_addrs
.addr_cmd_buf
,
210 (gdb_byte
*) cmd
, len
);
214 warning (_("unable to write"));
218 DEBUG_AGENT ("agent: resumed helper thread\n");
220 /* Resume helper thread. */
223 struct thread_resume resume_info
;
225 resume_info
.thread
= ptid
;
226 resume_info
.kind
= resume_continue
;
227 resume_info
.sig
= GDB_SIGNAL_0
;
228 (*the_target
->resume
) (&resume_info
, 1);
231 target_resume (ptid
, 0, GDB_SIGNAL_0
);
234 fd
= gdb_connect_sync_socket (pid
);
240 DEBUG_AGENT ("agent: signalling helper thread\n");
244 ret
= write (fd
, buf
, 1);
245 } while (ret
== -1 && errno
== EINTR
);
247 DEBUG_AGENT ("agent: waiting for helper thread's response\n");
251 ret
= read (fd
, buf
, 1);
252 } while (ret
== -1 && errno
== EINTR
);
256 DEBUG_AGENT ("agent: helper thread's response received\n");
261 /* Need to read response with the inferior stopped. */
262 if (!ptid_equal (ptid
, null_ptid
))
264 struct target_waitstatus status
;
265 int was_non_stop
= non_stop
;
266 /* Stop thread PTID. */
267 DEBUG_AGENT ("agent: stop helper thread\n");
270 struct thread_resume resume_info
;
272 resume_info
.thread
= ptid
;
273 resume_info
.kind
= resume_stop
;
274 resume_info
.sig
= GDB_SIGNAL_0
;
275 (*the_target
->resume
) (&resume_info
, 1);
279 mywait (ptid
, &status
, 0, 0);
284 memset (&status
, 0, sizeof (status
));
285 target_wait (ptid
, &status
, 0);
287 non_stop
= was_non_stop
;
292 if (target_read_memory (ipa_sym_addrs
.addr_cmd_buf
, (gdb_byte
*) cmd
,
295 warning (_("Error reading command response"));
303 /* Each bit of it stands for a capability of agent. */
304 static uint32_t agent_capability
= 0;
306 /* Return true if agent has capability AGENT_CAP, otherwise return false. */
309 agent_capability_check (enum agent_capa agent_capa
)
311 if (agent_capability
== 0)
313 if (target_read_uint32 (ipa_sym_addrs
.addr_capability
,
315 warning (_("Error reading capability of agent"));
317 return agent_capability
& agent_capa
;
320 /* Invalidate the cache of agent capability, so we'll read it from inferior
321 again. Call it when launches a new program or reconnect to remote stub. */
324 agent_capability_invalidate (void)
326 agent_capability
= 0;