Commit | Line | Data |
---|---|---|
f1aea813 | 1 | /* Base/prototype target for default child (native) targets. |
5bf970f9 | 2 | |
42a4f53d | 3 | Copyright (C) 1988-2019 Free Software Foundation, Inc. |
5bf970f9 AC |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
5bf970f9 AC |
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 | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
5bf970f9 | 19 | |
f1aea813 PA |
20 | /* This file provides a common base class/target that all native |
21 | target implementations extend, by calling inf_child_target to get a | |
22 | new prototype target and then overriding target methods as | |
23 | necessary. */ | |
24 | ||
5bf970f9 AC |
25 | #include "defs.h" |
26 | #include "regcache.h" | |
27 | #include "memattr.h" | |
28 | #include "symtab.h" | |
29 | #include "target.h" | |
30 | #include "inferior.h" | |
53ce3c39 | 31 | #include <sys/stat.h> |
2c0b251b | 32 | #include "inf-child.h" |
268a13a5 TT |
33 | #include "gdbsupport/fileio.h" |
34 | #include "gdbsupport/agent.h" | |
35 | #include "gdbsupport/gdb_wait.h" | |
36 | #include "gdbsupport/filestuff.h" | |
7313baad UW |
37 | |
38 | #include <sys/types.h> | |
7313baad UW |
39 | #include <fcntl.h> |
40 | #include <unistd.h> | |
5bf970f9 | 41 | |
d9f719f1 PA |
42 | static const target_info inf_child_target_info = { |
43 | "native", | |
44 | N_("Native process"), | |
45 | N_("Native process (started by the \"run\" command).") | |
46 | }; | |
47 | ||
48 | const target_info & | |
49 | inf_child_target::info () const | |
50 | { | |
51 | return inf_child_target_info; | |
52 | } | |
53 | ||
dab06dbe PA |
54 | /* Helper function for child_wait and the derivatives of child_wait. |
55 | HOSTSTATUS is the waitstatus from wait() or the equivalent; store our | |
56 | translation of that in OURSTATUS. */ | |
57 | void | |
58 | store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus) | |
59 | { | |
60 | if (WIFEXITED (hoststatus)) | |
61 | { | |
62 | ourstatus->kind = TARGET_WAITKIND_EXITED; | |
63 | ourstatus->value.integer = WEXITSTATUS (hoststatus); | |
64 | } | |
65 | else if (!WIFSTOPPED (hoststatus)) | |
66 | { | |
67 | ourstatus->kind = TARGET_WAITKIND_SIGNALLED; | |
2ea28649 | 68 | ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus)); |
dab06dbe PA |
69 | } |
70 | else | |
71 | { | |
72 | ourstatus->kind = TARGET_WAITKIND_STOPPED; | |
2ea28649 | 73 | ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus)); |
dab06dbe PA |
74 | } |
75 | } | |
76 | ||
f6ac5f3d PA |
77 | inf_child_target::~inf_child_target () |
78 | {} | |
5bf970f9 | 79 | |
f6ac5f3d PA |
80 | void |
81 | inf_child_target::post_attach (int pid) | |
5bf970f9 | 82 | { |
f1aea813 PA |
83 | /* This target doesn't require a meaningful "post attach" operation |
84 | by a debugger. */ | |
5bf970f9 AC |
85 | } |
86 | ||
87 | /* Get ready to modify the registers array. On machines which store | |
88 | individual registers, this doesn't need to do anything. On | |
89 | machines which store all the registers in one fell swoop, this | |
90 | makes sure that registers contains all the registers from the | |
91 | program being debugged. */ | |
92 | ||
f6ac5f3d PA |
93 | void |
94 | inf_child_target::prepare_to_store (struct regcache *regcache) | |
5bf970f9 AC |
95 | { |
96 | } | |
97 | ||
f6ac5f3d PA |
98 | bool |
99 | inf_child_target::supports_terminal_ours () | |
100 | { | |
101 | return true; | |
102 | } | |
103 | ||
104 | void | |
105 | inf_child_target::terminal_init () | |
106 | { | |
107 | child_terminal_init (this); | |
108 | } | |
109 | ||
110 | void | |
111 | inf_child_target::terminal_inferior () | |
112 | { | |
113 | child_terminal_inferior (this); | |
114 | } | |
115 | ||
ae739fe7 SM |
116 | void |
117 | inf_child_target::terminal_save_inferior () | |
118 | { | |
119 | child_terminal_save_inferior (this); | |
120 | } | |
121 | ||
f6ac5f3d PA |
122 | void |
123 | inf_child_target::terminal_ours_for_output () | |
124 | { | |
125 | child_terminal_ours_for_output (this); | |
126 | } | |
127 | ||
128 | void | |
129 | inf_child_target::terminal_ours () | |
130 | { | |
131 | child_terminal_ours (this); | |
132 | } | |
133 | ||
134 | void | |
135 | inf_child_target::interrupt () | |
136 | { | |
137 | child_interrupt (this); | |
138 | } | |
139 | ||
140 | void | |
141 | inf_child_target::pass_ctrlc () | |
142 | { | |
143 | child_pass_ctrlc (this); | |
144 | } | |
145 | ||
146 | void | |
147 | inf_child_target::terminal_info (const char *args, int from_tty) | |
148 | { | |
149 | child_terminal_info (this, args, from_tty); | |
150 | } | |
151 | ||
6a3cb8e8 PA |
152 | /* True if the user did "target native". In that case, we won't |
153 | unpush the child target automatically when the last inferior is | |
154 | gone. */ | |
155 | static int inf_child_explicitly_opened; | |
156 | ||
157 | /* See inf-child.h. */ | |
158 | ||
159 | void | |
d9f719f1 | 160 | inf_child_open_target (const char *arg, int from_tty) |
6a3cb8e8 | 161 | { |
d9f719f1 PA |
162 | target_ops *target = get_native_target (); |
163 | ||
164 | /* There's always only ever one native target, and if we get here, | |
165 | it better be an inf-child target. */ | |
166 | gdb_assert (dynamic_cast<inf_child_target *> (target) != NULL); | |
167 | ||
6a3cb8e8 PA |
168 | target_preopen (from_tty); |
169 | push_target (target); | |
170 | inf_child_explicitly_opened = 1; | |
171 | if (from_tty) | |
172 | printf_filtered ("Done. Use the \"run\" command to start a process.\n"); | |
173 | } | |
174 | ||
6a3cb8e8 PA |
175 | /* Implement the to_disconnect target_ops method. */ |
176 | ||
f6ac5f3d PA |
177 | void |
178 | inf_child_target::disconnect (const char *args, int from_tty) | |
6a3cb8e8 PA |
179 | { |
180 | if (args != NULL) | |
181 | error (_("Argument given to \"disconnect\".")); | |
182 | ||
183 | /* This offers to detach/kill current inferiors, and then pops all | |
184 | targets. */ | |
185 | target_preopen (from_tty); | |
186 | } | |
187 | ||
188 | /* Implement the to_close target_ops method. */ | |
189 | ||
f6ac5f3d PA |
190 | void |
191 | inf_child_target::close () | |
6a3cb8e8 PA |
192 | { |
193 | /* In case we were forcibly closed. */ | |
194 | inf_child_explicitly_opened = 0; | |
195 | } | |
196 | ||
c1ee2fb3 | 197 | void |
f6ac5f3d | 198 | inf_child_target::mourn_inferior () |
c1ee2fb3 PA |
199 | { |
200 | generic_mourn_inferior (); | |
f6ac5f3d | 201 | maybe_unpush_target (); |
c1ee2fb3 PA |
202 | } |
203 | ||
6a3cb8e8 PA |
204 | /* See inf-child.h. */ |
205 | ||
206 | void | |
f6ac5f3d | 207 | inf_child_target::maybe_unpush_target () |
6a3cb8e8 PA |
208 | { |
209 | if (!inf_child_explicitly_opened && !have_inferiors ()) | |
f6ac5f3d | 210 | unpush_target (this); |
5bf970f9 AC |
211 | } |
212 | ||
f6ac5f3d PA |
213 | void |
214 | inf_child_target::post_startup_inferior (ptid_t ptid) | |
5bf970f9 | 215 | { |
f1aea813 PA |
216 | /* This target doesn't require a meaningful "post startup inferior" |
217 | operation by a debugger. */ | |
5bf970f9 AC |
218 | } |
219 | ||
57810aa7 | 220 | bool |
f6ac5f3d | 221 | inf_child_target::can_run () |
5bf970f9 | 222 | { |
57810aa7 | 223 | return true; |
5bf970f9 AC |
224 | } |
225 | ||
f6ac5f3d PA |
226 | bool |
227 | inf_child_target::can_create_inferior () | |
5bf970f9 | 228 | { |
f6ac5f3d PA |
229 | return true; |
230 | } | |
231 | ||
232 | bool | |
233 | inf_child_target::can_attach () | |
234 | { | |
235 | return true; | |
5bf970f9 AC |
236 | } |
237 | ||
f6ac5f3d PA |
238 | char * |
239 | inf_child_target::pid_to_exec_file (int pid) | |
5bf970f9 | 240 | { |
f1aea813 PA |
241 | /* This target doesn't support translation of a process ID to the |
242 | filename of the executable file. */ | |
5bf970f9 AC |
243 | return NULL; |
244 | } | |
245 | ||
12e2a5fd GB |
246 | /* Implementation of to_fileio_open. */ |
247 | ||
f6ac5f3d PA |
248 | int |
249 | inf_child_target::fileio_open (struct inferior *inf, const char *filename, | |
250 | int flags, int mode, int warn_if_slow, | |
251 | int *target_errno) | |
7313baad UW |
252 | { |
253 | int nat_flags; | |
3ac2e371 | 254 | mode_t nat_mode; |
7313baad UW |
255 | int fd; |
256 | ||
3ac2e371 GB |
257 | if (fileio_to_host_openflags (flags, &nat_flags) == -1 |
258 | || fileio_to_host_mode (mode, &nat_mode) == -1) | |
7313baad UW |
259 | { |
260 | *target_errno = FILEIO_EINVAL; | |
261 | return -1; | |
262 | } | |
263 | ||
3ac2e371 | 264 | fd = gdb_open_cloexec (filename, nat_flags, nat_mode); |
7313baad | 265 | if (fd == -1) |
7823a941 | 266 | *target_errno = host_to_fileio_error (errno); |
7313baad UW |
267 | |
268 | return fd; | |
269 | } | |
270 | ||
12e2a5fd GB |
271 | /* Implementation of to_fileio_pwrite. */ |
272 | ||
f6ac5f3d PA |
273 | int |
274 | inf_child_target::fileio_pwrite (int fd, const gdb_byte *write_buf, int len, | |
275 | ULONGEST offset, int *target_errno) | |
7313baad UW |
276 | { |
277 | int ret; | |
278 | ||
279 | #ifdef HAVE_PWRITE | |
280 | ret = pwrite (fd, write_buf, len, (long) offset); | |
281 | #else | |
7c3270ae | 282 | ret = -1; |
7313baad | 283 | #endif |
7c3270ae UW |
284 | /* If we have no pwrite or it failed for this file, use lseek/write. */ |
285 | if (ret == -1) | |
286 | { | |
287 | ret = lseek (fd, (long) offset, SEEK_SET); | |
288 | if (ret != -1) | |
289 | ret = write (fd, write_buf, len); | |
290 | } | |
7313baad UW |
291 | |
292 | if (ret == -1) | |
7823a941 | 293 | *target_errno = host_to_fileio_error (errno); |
7313baad UW |
294 | |
295 | return ret; | |
296 | } | |
297 | ||
12e2a5fd GB |
298 | /* Implementation of to_fileio_pread. */ |
299 | ||
f6ac5f3d PA |
300 | int |
301 | inf_child_target::fileio_pread (int fd, gdb_byte *read_buf, int len, | |
302 | ULONGEST offset, int *target_errno) | |
7313baad UW |
303 | { |
304 | int ret; | |
305 | ||
306 | #ifdef HAVE_PREAD | |
307 | ret = pread (fd, read_buf, len, (long) offset); | |
308 | #else | |
7c3270ae | 309 | ret = -1; |
7313baad | 310 | #endif |
7c3270ae UW |
311 | /* If we have no pread or it failed for this file, use lseek/read. */ |
312 | if (ret == -1) | |
313 | { | |
314 | ret = lseek (fd, (long) offset, SEEK_SET); | |
315 | if (ret != -1) | |
316 | ret = read (fd, read_buf, len); | |
317 | } | |
7313baad UW |
318 | |
319 | if (ret == -1) | |
7823a941 | 320 | *target_errno = host_to_fileio_error (errno); |
7313baad UW |
321 | |
322 | return ret; | |
323 | } | |
324 | ||
9b15c1f0 | 325 | /* Implementation of to_fileio_fstat. */ |
12e2a5fd | 326 | |
f6ac5f3d PA |
327 | int |
328 | inf_child_target::fileio_fstat (int fd, struct stat *sb, int *target_errno) | |
9b15c1f0 GB |
329 | { |
330 | int ret; | |
331 | ||
332 | ret = fstat (fd, sb); | |
333 | if (ret == -1) | |
7823a941 | 334 | *target_errno = host_to_fileio_error (errno); |
9b15c1f0 GB |
335 | |
336 | return ret; | |
337 | } | |
338 | ||
12e2a5fd GB |
339 | /* Implementation of to_fileio_close. */ |
340 | ||
f6ac5f3d PA |
341 | int |
342 | inf_child_target::fileio_close (int fd, int *target_errno) | |
7313baad UW |
343 | { |
344 | int ret; | |
345 | ||
f6ac5f3d | 346 | ret = ::close (fd); |
7313baad | 347 | if (ret == -1) |
7823a941 | 348 | *target_errno = host_to_fileio_error (errno); |
7313baad UW |
349 | |
350 | return ret; | |
351 | } | |
352 | ||
12e2a5fd GB |
353 | /* Implementation of to_fileio_unlink. */ |
354 | ||
f6ac5f3d PA |
355 | int |
356 | inf_child_target::fileio_unlink (struct inferior *inf, const char *filename, | |
357 | int *target_errno) | |
7313baad UW |
358 | { |
359 | int ret; | |
360 | ||
361 | ret = unlink (filename); | |
362 | if (ret == -1) | |
7823a941 | 363 | *target_errno = host_to_fileio_error (errno); |
7313baad UW |
364 | |
365 | return ret; | |
366 | } | |
367 | ||
12e2a5fd GB |
368 | /* Implementation of to_fileio_readlink. */ |
369 | ||
f6ac5f3d PA |
370 | gdb::optional<std::string> |
371 | inf_child_target::fileio_readlink (struct inferior *inf, const char *filename, | |
372 | int *target_errno) | |
b9e7b9c3 UW |
373 | { |
374 | /* We support readlink only on systems that also provide a compile-time | |
d8d2a3ee | 375 | maximum path length (PATH_MAX), at least for now. */ |
bdca27a2 | 376 | #if defined (PATH_MAX) |
d8d2a3ee | 377 | char buf[PATH_MAX]; |
b9e7b9c3 | 378 | int len; |
b9e7b9c3 UW |
379 | |
380 | len = readlink (filename, buf, sizeof buf); | |
381 | if (len < 0) | |
382 | { | |
7823a941 | 383 | *target_errno = host_to_fileio_error (errno); |
e0d3522b | 384 | return {}; |
b9e7b9c3 UW |
385 | } |
386 | ||
e0d3522b | 387 | return std::string (buf, len); |
b9e7b9c3 UW |
388 | #else |
389 | *target_errno = FILEIO_ENOSYS; | |
e0d3522b | 390 | return {}; |
b9e7b9c3 UW |
391 | #endif |
392 | } | |
393 | ||
57810aa7 PA |
394 | bool |
395 | inf_child_target::use_agent (bool use) | |
5808517f YQ |
396 | { |
397 | if (agent_loaded_p ()) | |
398 | { | |
f6ac5f3d | 399 | ::use_agent = use; |
57810aa7 | 400 | return true; |
5808517f YQ |
401 | } |
402 | else | |
57810aa7 | 403 | return false; |
5808517f YQ |
404 | } |
405 | ||
57810aa7 | 406 | bool |
f6ac5f3d | 407 | inf_child_target::can_use_agent () |
5808517f YQ |
408 | { |
409 | return agent_loaded_p (); | |
410 | } | |
7313baad | 411 | |
d9f719f1 PA |
412 | /* See inf-child.h. */ |
413 | ||
414 | void | |
415 | add_inf_child_target (inf_child_target *target) | |
416 | { | |
417 | set_native_target (target); | |
418 | add_target (inf_child_target_info, inf_child_open_target); | |
419 | } |