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