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