Add translations for various sub-directories
[binutils-gdb.git] / gdb / inf-child.c
blobb6b3750fbb85d8508b4edb10b481a73e7fcc69c0
1 /* Base/prototype target for default child (native) targets.
3 Copyright (C) 1988-2024 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/>. */
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. */
25 #include "regcache.h"
26 #include "memattr.h"
27 #include "symtab.h"
28 #include "target.h"
29 #include "inferior.h"
30 #include <sys/stat.h>
31 #include "inf-child.h"
32 #include "gdbsupport/fileio.h"
33 #include "gdbsupport/agent.h"
34 #include "gdbsupport/gdb_wait.h"
35 #include "gdbsupport/filestuff.h"
36 #include "cli/cli-style.h"
38 #include <sys/types.h>
39 #include <fcntl.h>
40 #include <unistd.h>
42 static const target_info inf_child_target_info = {
43 "native",
44 N_("Native process"),
45 N_("Native process (started by the \"run\" command).")
48 const target_info &
49 inf_child_target::info () const
51 return inf_child_target_info;
54 /* See inf-child.h. */
56 target_waitstatus
57 host_status_to_waitstatus (int hoststatus)
59 if (WIFEXITED (hoststatus))
60 return target_waitstatus ().set_exited (WEXITSTATUS (hoststatus));
61 else if (!WIFSTOPPED (hoststatus))
62 return target_waitstatus ().set_signalled
63 (gdb_signal_from_host (WTERMSIG (hoststatus)));
64 else
65 return target_waitstatus ().set_stopped
66 (gdb_signal_from_host (WSTOPSIG (hoststatus)));
69 inf_child_target::~inf_child_target ()
72 void
73 inf_child_target::post_attach (int pid)
75 /* This target doesn't require a meaningful "post attach" operation
76 by a debugger. */
79 /* Get ready to modify the registers array. On machines which store
80 individual registers, this doesn't need to do anything. On
81 machines which store all the registers in one fell swoop, this
82 makes sure that registers contains all the registers from the
83 program being debugged. */
85 void
86 inf_child_target::prepare_to_store (struct regcache *regcache)
90 bool
91 inf_child_target::supports_terminal_ours ()
93 return true;
96 void
97 inf_child_target::terminal_init ()
99 child_terminal_init (this);
102 void
103 inf_child_target::terminal_inferior ()
105 child_terminal_inferior (this);
108 void
109 inf_child_target::terminal_save_inferior ()
111 child_terminal_save_inferior (this);
114 void
115 inf_child_target::terminal_ours_for_output ()
117 child_terminal_ours_for_output (this);
120 void
121 inf_child_target::terminal_ours ()
123 child_terminal_ours (this);
126 void
127 inf_child_target::interrupt ()
129 child_interrupt (this);
132 void
133 inf_child_target::pass_ctrlc ()
135 child_pass_ctrlc (this);
138 void
139 inf_child_target::terminal_info (const char *args, int from_tty)
141 child_terminal_info (this, args, from_tty);
144 /* True if the user did "target native". In that case, we won't
145 unpush the child target automatically when the last inferior is
146 gone. */
147 static int inf_child_explicitly_opened;
149 /* See inf-child.h. */
151 void
152 inf_child_open_target (const char *arg, int from_tty)
154 target_ops *target = get_native_target ();
156 /* There's always only ever one native target, and if we get here,
157 it better be an inf-child target. */
158 gdb_assert (dynamic_cast<inf_child_target *> (target) != NULL);
160 target_preopen (from_tty);
161 current_inferior ()->push_target (target);
162 inf_child_explicitly_opened = 1;
163 if (from_tty)
164 gdb_printf ("Done. Use the \"%ps\" command to start a process.\n",
165 styled_string (command_style.style (), "run"));
168 /* Implement the to_disconnect target_ops method. */
170 void
171 inf_child_target::disconnect (const char *args, int from_tty)
173 if (args != NULL)
174 error (_("Argument given to \"disconnect\"."));
176 /* This offers to detach/kill current inferiors, and then pops all
177 targets. */
178 target_preopen (from_tty);
181 /* Implement the to_close target_ops method. */
183 void
184 inf_child_target::close ()
186 /* In case we were forcibly closed. */
187 inf_child_explicitly_opened = 0;
190 void
191 inf_child_target::mourn_inferior ()
193 generic_mourn_inferior ();
194 maybe_unpush_target ();
197 /* See inf-child.h. */
199 void
200 inf_child_target::maybe_unpush_target ()
202 if (!inf_child_explicitly_opened)
203 current_inferior ()->unpush_target (this);
206 bool
207 inf_child_target::can_run ()
209 return true;
212 bool
213 inf_child_target::can_create_inferior ()
215 return true;
218 bool
219 inf_child_target::can_attach ()
221 return true;
224 const char *
225 inf_child_target::pid_to_exec_file (int pid)
227 /* This target doesn't support translation of a process ID to the
228 filename of the executable file. */
229 return NULL;
232 /* Implementation of to_fileio_open. */
235 inf_child_target::fileio_open (struct inferior *inf, const char *filename,
236 int flags, int mode, int warn_if_slow,
237 fileio_error *target_errno)
239 int nat_flags;
240 mode_t nat_mode;
241 int fd;
243 if (fileio_to_host_openflags (flags, &nat_flags) == -1
244 || fileio_to_host_mode (mode, &nat_mode) == -1)
246 *target_errno = FILEIO_EINVAL;
247 return -1;
250 fd = gdb_open_cloexec (filename, nat_flags, nat_mode).release ();
251 if (fd == -1)
252 *target_errno = host_to_fileio_error (errno);
254 return fd;
257 /* Implementation of to_fileio_pwrite. */
260 inf_child_target::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
261 ULONGEST offset, fileio_error *target_errno)
263 int ret;
265 #ifdef HAVE_PWRITE
266 ret = pwrite (fd, write_buf, len, (long) offset);
267 #else
268 ret = -1;
269 #endif
270 /* If we have no pwrite or it failed for this file, use lseek/write. */
271 if (ret == -1)
273 ret = lseek (fd, (long) offset, SEEK_SET);
274 if (ret != -1)
275 ret = write (fd, write_buf, len);
278 if (ret == -1)
279 *target_errno = host_to_fileio_error (errno);
281 return ret;
284 /* Implementation of to_fileio_pread. */
287 inf_child_target::fileio_pread (int fd, gdb_byte *read_buf, int len,
288 ULONGEST offset, fileio_error *target_errno)
290 int ret;
292 #ifdef HAVE_PREAD
293 ret = pread (fd, read_buf, len, (long) offset);
294 #else
295 ret = -1;
296 #endif
297 /* If we have no pread or it failed for this file, use lseek/read. */
298 if (ret == -1)
300 ret = lseek (fd, (long) offset, SEEK_SET);
301 if (ret != -1)
302 ret = read (fd, read_buf, len);
305 if (ret == -1)
306 *target_errno = host_to_fileio_error (errno);
308 return ret;
311 /* Implementation of to_fileio_fstat. */
314 inf_child_target::fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno)
316 int ret;
318 ret = fstat (fd, sb);
319 if (ret == -1)
320 *target_errno = host_to_fileio_error (errno);
322 return ret;
325 /* Implementation of to_fileio_stat. */
328 inf_child_target::fileio_stat (struct inferior *inf, const char *filename,
329 struct stat *sb, fileio_error *target_errno)
331 int ret;
333 ret = lstat (filename, sb);
334 if (ret == -1)
335 *target_errno = host_to_fileio_error (errno);
337 return ret;
340 /* Implementation of to_fileio_close. */
343 inf_child_target::fileio_close (int fd, fileio_error *target_errno)
345 int ret;
347 ret = ::close (fd);
348 if (ret == -1)
349 *target_errno = host_to_fileio_error (errno);
351 return ret;
354 /* Implementation of to_fileio_unlink. */
357 inf_child_target::fileio_unlink (struct inferior *inf, const char *filename,
358 fileio_error *target_errno)
360 int ret;
362 ret = unlink (filename);
363 if (ret == -1)
364 *target_errno = host_to_fileio_error (errno);
366 return ret;
369 /* Implementation of to_fileio_readlink. */
371 std::optional<std::string>
372 inf_child_target::fileio_readlink (struct inferior *inf, const char *filename,
373 fileio_error *target_errno)
375 /* We support readlink only on systems that also provide a compile-time
376 maximum path length (PATH_MAX), at least for now. */
377 #if defined (PATH_MAX)
378 char buf[PATH_MAX];
379 int len;
381 len = readlink (filename, buf, sizeof buf);
382 if (len < 0)
384 *target_errno = host_to_fileio_error (errno);
385 return {};
388 return std::string (buf, len);
389 #else
390 *target_errno = FILEIO_ENOSYS;
391 return {};
392 #endif
395 bool
396 inf_child_target::use_agent (bool use)
398 if (agent_loaded_p ())
400 ::use_agent = use;
401 return true;
403 else
404 return false;
407 bool
408 inf_child_target::can_use_agent ()
410 return agent_loaded_p ();
413 void
414 inf_child_target::follow_exec (inferior *follow_inf, ptid_t ptid,
415 const char *execd_pathname)
417 inferior *orig_inf = current_inferior ();
419 process_stratum_target::follow_exec (follow_inf, ptid, execd_pathname);
421 if (orig_inf != follow_inf)
423 /* If the target was implicitly push in the original inferior, unpush
424 it. */
425 scoped_restore_current_thread restore_thread;
426 switch_to_inferior_no_thread (orig_inf);
427 maybe_unpush_target ();
431 /* See inf-child.h. */
433 void
434 add_inf_child_target (inf_child_target *target)
436 set_native_target (target);
437 add_target (inf_child_target_info, inf_child_open_target);