GDB (xrefs)

agent.c
Go to the documentation of this file.
1 /* Shared utility routines for GDB to interact with agent.
2
3 Copyright (C) 2009-2018 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 #include "common-defs.h"
21 #include "target/target.h"
22 #include "common/symbol.h"
23 #include <unistd.h>
24 #include "agent.h"
25 #include "filestuff.h"
26
27 int debug_agent = 0;
28
29 /* A stdarg wrapper for debug_vprintf. */
30
31 static void ATTRIBUTE_PRINTF (1, 2)
32 debug_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
44
45 /* Global flag to determine using agent or not. */
46 int use_agent = 0;
47
48 /* Addresses of in-process agent's symbols both GDB and GDBserver cares
49 about. */
50
51 struct ipa_sym_addresses
52 {
53 CORE_ADDR addr_helper_thread_id;
54 CORE_ADDR addr_cmd_buf;
55 CORE_ADDR addr_capability;
56 };
57
58 /* Cache of the helper thread id. FIXME: this global should be made
59 per-process. */
60 static uint32_t helper_thread_id = 0;
61
62 static struct
63 {
64 const char *name;
65 int offset;
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 static int all_agent_symbols_looked_up = 0;
75
76 int
77 agent_loaded_p (void)
78 {
79 return all_agent_symbols_looked_up;
80 }
81
82 /* Look up all symbols needed by agent. Return 0 if all the symbols are
83 found, return non-zero otherwise. */
84
85 int
86 agent_look_up_symbols (void *arg)
87 {
88 int i;
89
90 all_agent_symbols_looked_up = 0;
91
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);
96 struct objfile *objfile = (struct objfile *) arg;
97
98 if (find_minimal_symbol_address (symbol_list[i].name, addrp,
99 objfile) != 0)
100 {
101 DEBUG_AGENT ("symbol `%s' not found\n", symbol_list[i].name);
102 return -1;
103 }
104 }
105
106 all_agent_symbols_looked_up = 1;
107 return 0;
108 }
109
110 static unsigned int
111 agent_get_helper_thread_id (void)
112 {
113 if (helper_thread_id == 0)
114 {
115 if (target_read_uint32 (ipa_sym_addrs.addr_helper_thread_id,
116 &helper_thread_id))
117 warning (_("Error reading helper thread's id in lib"));
118 }
119
120 return helper_thread_id;
121 }
122
123 #ifdef HAVE_SYS_UN_H
124 #include <sys/socket.h>
125 #include <sys/un.h>
126 #define SOCK_DIR P_tmpdir
127
128 #ifndef UNIX_PATH_MAX
129 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
130 #endif
131
132 #endif
133
134 /* Connects to synchronization socket. PID is the pid of inferior, which is
135 used to set up the connection socket. */
136
137 static int
138 gdb_connect_sync_socket (int pid)
139 {
140 #ifdef HAVE_SYS_UN_H
141 struct sockaddr_un addr;
142 int res, fd;
143 char path[UNIX_PATH_MAX];
144
145 res = xsnprintf (path, UNIX_PATH_MAX, "%s/gdb_ust%d", P_tmpdir, pid);
146 if (res >= UNIX_PATH_MAX)
147 return -1;
148
149 res = fd = gdb_socket_cloexec (PF_UNIX, SOCK_STREAM, 0);
150 if (res == -1)
151 {
152 warning (_("error opening sync socket: %s"), strerror (errno));
153 return -1;
154 }
155
156 addr.sun_family = AF_UNIX;
157
158 res = xsnprintf (addr.sun_path, UNIX_PATH_MAX, "%s", path);
159 if (res >= UNIX_PATH_MAX)
160 {
161 warning (_("string overflow allocating socket name"));
162 close (fd);
163 return -1;
164 }
165
166 res = connect (fd, (struct sockaddr *) &addr, sizeof (addr));
167 if (res == -1)
168 {
169 warning (_("error connecting sync socket (%s): %s. "
170 "Make sure the directory exists and that it is writable."),
171 path, strerror (errno));
172 close (fd);
173 return -1;
174 }
175
176 return fd;
177 #else
178 return -1;
179 #endif
180 }
181
182 /* Execute an agent command in the inferior. PID is the value of pid of the
183 inferior. CMD is the buffer for command. GDB or GDBserver will store the
184 command into it and fetch the return result from CMD. The interaction
185 between GDB/GDBserver and the agent is synchronized by a synchronization
186 socket. Return zero if success, otherwise return non-zero. */
187
188 int
189 agent_run_command (int pid, const char *cmd, int len)
190 {
191 int fd;
192 int tid = agent_get_helper_thread_id ();
193 ptid_t ptid = ptid_build (pid, tid, 0);
194
195 int ret = target_write_memory (ipa_sym_addrs.addr_cmd_buf,
196 (gdb_byte *) cmd, len);
197
198 if (ret != 0)
199 {
200 warning (_("unable to write"));
201 return -1;
202 }
203
204 DEBUG_AGENT ("agent: resumed helper thread\n");
205
206 /* Resume helper thread. */
207 target_continue_no_signal (ptid);
208
209 fd = gdb_connect_sync_socket (pid);
210 if (fd >= 0)
211 {
212 char buf[1] = "";
213 int ret;
214
215 DEBUG_AGENT ("agent: signalling helper thread\n");
216
217 do
218 {
219 ret = write (fd, buf, 1);
220 } while (ret == -1 && errno == EINTR);
221
222 DEBUG_AGENT ("agent: waiting for helper thread's response\n");
223
224 do
225 {
226 ret = read (fd, buf, 1);
227 } while (ret == -1 && errno == EINTR);
228
229 close (fd);
230
231 DEBUG_AGENT ("agent: helper thread's response received\n");
232 }
233 else
234 return -1;
235
236 /* Need to read response with the inferior stopped. */
237 if (!ptid_equal (ptid, null_ptid))
238 {
239 /* Stop thread PTID. */
240 DEBUG_AGENT ("agent: stop helper thread\n");
241 target_stop_and_wait (ptid);
242 }
243
244 if (fd >= 0)
245 {
246 if (target_read_memory (ipa_sym_addrs.addr_cmd_buf, (gdb_byte *) cmd,
247 IPA_CMD_BUF_SIZE))
248 {
249 warning (_("Error reading command response"));
250 return -1;
251 }
252 }
253
254 return 0;
255 }
256
257 /* Each bit of it stands for a capability of agent. */
258 static uint32_t agent_capability = 0;
259
260 /* Return true if agent has capability AGENT_CAP, otherwise return false. */
261
262 int
263 agent_capability_check (enum agent_capa agent_capa)
264 {
265 if (agent_capability == 0)
266 {
267 if (target_read_uint32 (ipa_sym_addrs.addr_capability,
268 &agent_capability))
269 warning (_("Error reading capability of agent"));
270 }
271 return agent_capability & agent_capa;
272 }
273
274 /* Invalidate the cache of agent capability, so we'll read it from inferior
275 again. Call it when launches a new program or reconnect to remote stub. */
276
277 void
278 agent_capability_invalidate (void)
279 {
280 agent_capability = 0;
281 }
ipa_sym_addresses
Definition: agent.c:51
IPA_CMD_BUF_SIZE
#define IPA_CMD_BUF_SIZE
Definition: agent.h:38
read
ssize_t read(int fd, void *buf, size_t count)
Definition: expect-read1.c:26
selftests::ptid::tid
static constexpr ptid_t tid
Definition: ptid-selftests.c:43
agent_get_helper_thread_id
static unsigned int agent_get_helper_thread_id(void)
Definition: agent.c:111
find_minimal_symbol_address
int find_minimal_symbol_address(const char *name, CORE_ADDR *addr, struct objfile *objfile)
Definition: minsyms.c:438
CORE_ADDR
bfd_vma CORE_ADDR
Definition: common-types.h:41
symbol.h
target_write_memory
int target_write_memory(CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
Definition: target.c:1451
pid
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int int rusage_t pid_t pid
Definition: gnu-nat.c:1824
debug_agent
int debug_agent
Definition: agent.c:27
warning
void warning(const char *fmt,...)
Definition: errors.c:26
name
const char * name
Definition: agent.c:64
symbol_list
static struct @23 symbol_list[]
DEBUG_AGENT
#define DEBUG_AGENT
Definition: agent.c:43
_
#define _(String)
Definition: gdb_locale.h:35
agent_capa
agent_capa
Definition: agent.h:51
gdb_socket_cloexec
int gdb_socket_cloexec(int domain, int style, int protocol)
Definition: filestuff.c:359
ptid_build
ptid_t ptid_build(int pid, long lwp, long tid)
Definition: ptid.c:31
target.h
ptid_t
Definition: ptid.h:35
UNIX_PATH_MAX
#define UNIX_PATH_MAX
Definition: agent.c:129
use_agent
int use_agent
Definition: agent.c:46
all_agent_symbols_looked_up
static int all_agent_symbols_looked_up
Definition: agent.c:74
common-defs.h
target_read_uint32
int target_read_uint32(CORE_ADDR memaddr, uint32_t *result)
Definition: target.c:1385
agent_run_command
int agent_run_command(int pid, const char *cmd, int len)
Definition: agent.c:189
target_stop_and_wait
void target_stop_and_wait(ptid_t ptid)
Definition: target.c:3358
agent_capability_invalidate
void agent_capability_invalidate(void)
Definition: agent.c:278
gdb_connect_sync_socket
static int gdb_connect_sync_socket(int pid)
Definition: agent.c:138
debug_vprintf
void void debug_vprintf(const char *format, va_list ap) ATTRIBUTE_PRINTF(1
objfile
Definition: objfiles.h:286
gdb_byte
bfd_byte gdb_byte
Definition: common-types.h:38
ipa_sym_addresses::addr_cmd_buf
CORE_ADDR addr_cmd_buf
Definition: agent.c:54
null_ptid
ptid_t null_ptid
Definition: ptid.c:25
xsnprintf
int xsnprintf(char *str, size_t size, const char *format,...)
Definition: common-utils.c:134
target_read_memory
int target_read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition: target.c:1370
ipa_sym_addrs
static struct ipa_sym_addresses ipa_sym_addrs
Definition: agent.c:72
offset
int offset
Definition: agent.c:65
agent_capability_check
int agent_capability_check(enum agent_capa agent_capa)
Definition: agent.c:263
agent.h
ptid_equal
int ptid_equal(const ptid_t &ptid1, const ptid_t &ptid2)
Definition: ptid.c:71
agent_loaded_p
int agent_loaded_p(void)
Definition: agent.c:77
ipa_sym_addresses::addr_capability
CORE_ADDR addr_capability
Definition: agent.c:55
ATTRIBUTE_PRINTF
static void ATTRIBUTE_PRINTF(1, 2)
Definition: agent.c:31
filestuff.h
agent_capability
static uint32_t agent_capability
Definition: agent.c:258
ipa_sym_addresses::addr_helper_thread_id
CORE_ADDR addr_helper_thread_id
Definition: agent.c:53
target_continue_no_signal
void target_continue_no_signal(ptid_t ptid)
Definition: target.c:3375
IPA_SYM
#define IPA_SYM(SYM)
Definition: agent.h:30
agent_look_up_symbols
int agent_look_up_symbols(void *arg)
Definition: agent.c:86
helper_thread_id
static uint32_t helper_thread_id
Definition: agent.c:60