Updated French translation for the ld/ and gold/ sub-directories
[binutils-gdb.git] / gdbserver / target.cc
blob5009146d66373665ea74c92ffc42a5cd0a377e9e
1 /* Target operations for the remote server for GDB.
2 Copyright (C) 2002-2022 Free Software Foundation, Inc.
4 Contributed by MontaVista Software.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "server.h"
22 #include "tracepoint.h"
23 #include "gdbsupport/byte-vector.h"
24 #include "hostio.h"
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
30 process_stratum_target *the_target;
32 int
33 set_desired_thread ()
35 client_state &cs = get_client_state ();
36 thread_info *found = find_thread_ptid (cs.general_thread);
38 switch_to_thread (found);
39 return (current_thread != NULL);
42 /* The thread that was current before prepare_to_access_memory was
43 called. done_accessing_memory uses this to restore the previous
44 selected thread. */
45 static ptid_t prev_general_thread;
47 /* See target.h. */
49 int
50 prepare_to_access_memory (void)
52 client_state &cs = get_client_state ();
54 /* The first thread found. */
55 struct thread_info *first = NULL;
56 /* The first stopped thread found. */
57 struct thread_info *stopped = NULL;
58 /* The current general thread, if found. */
59 struct thread_info *current = NULL;
61 /* Save the general thread value, since prepare_to_access_memory could change
62 it. */
63 prev_general_thread = cs.general_thread;
65 int res = the_target->prepare_to_access_memory ();
66 if (res != 0)
67 return res;
69 for_each_thread (prev_general_thread.pid (), [&] (thread_info *thread)
71 if (mythread_alive (thread->id))
73 if (stopped == NULL && the_target->supports_thread_stopped ()
74 && target_thread_stopped (thread))
75 stopped = thread;
77 if (first == NULL)
78 first = thread;
80 if (current == NULL && prev_general_thread == thread->id)
81 current = thread;
83 });
85 /* The thread we end up choosing. */
86 struct thread_info *thread;
88 /* Prefer a stopped thread. If none is found, try the current
89 thread. Otherwise, take the first thread in the process. If
90 none is found, undo the effects of
91 target->prepare_to_access_memory() and return error. */
92 if (stopped != NULL)
93 thread = stopped;
94 else if (current != NULL)
95 thread = current;
96 else if (first != NULL)
97 thread = first;
98 else
100 done_accessing_memory ();
101 return 1;
104 switch_to_thread (thread);
105 cs.general_thread = ptid_of (thread);
107 return 0;
110 /* See target.h. */
112 void
113 done_accessing_memory (void)
115 client_state &cs = get_client_state ();
117 the_target->done_accessing_memory ();
119 /* Restore the previous selected thread. */
120 cs.general_thread = prev_general_thread;
121 switch_to_thread (the_target, cs.general_thread);
125 read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
127 int res;
128 res = the_target->read_memory (memaddr, myaddr, len);
129 check_mem_read (memaddr, myaddr, len);
130 return res;
133 /* See target/target.h. */
136 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
138 return read_inferior_memory (memaddr, myaddr, len);
141 /* See target/target.h. */
144 target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
146 return read_inferior_memory (memaddr, (gdb_byte *) result, sizeof (*result));
149 /* See target/target.h. */
152 target_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
153 ssize_t len)
155 /* Make a copy of the data because check_mem_write may need to
156 update it. */
157 gdb::byte_vector buffer (myaddr, myaddr + len);
158 check_mem_write (memaddr, buffer.data (), myaddr, len);
159 return the_target->write_memory (memaddr, buffer.data (), len);
162 ptid_t
163 mywait (ptid_t ptid, struct target_waitstatus *ourstatus,
164 target_wait_flags options, int connected_wait)
166 ptid_t ret;
168 if (connected_wait)
169 server_waiting = 1;
171 ret = target_wait (ptid, ourstatus, options);
173 /* We don't expose _LOADED events to gdbserver core. See the
174 `dlls_changed' global. */
175 if (ourstatus->kind () == TARGET_WAITKIND_LOADED)
176 ourstatus->set_stopped (GDB_SIGNAL_0);
178 /* If GDB is connected through TCP/serial, then GDBserver will most
179 probably be running on its own terminal/console, so it's nice to
180 print there why is GDBserver exiting. If however, GDB is
181 connected through stdio, then there's no need to spam the GDB
182 console with this -- the user will already see the exit through
183 regular GDB output, in that same terminal. */
184 if (!remote_connection_is_stdio ())
186 if (ourstatus->kind () == TARGET_WAITKIND_EXITED)
187 fprintf (stderr,
188 "\nChild exited with status %d\n", ourstatus->exit_status ());
189 else if (ourstatus->kind () == TARGET_WAITKIND_SIGNALLED)
190 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
191 gdb_signal_to_host (ourstatus->sig ()),
192 gdb_signal_to_name (ourstatus->sig ()));
195 if (connected_wait)
196 server_waiting = 0;
198 return ret;
201 /* See target/target.h. */
203 void
204 target_stop_and_wait (ptid_t ptid)
206 struct target_waitstatus status;
207 bool was_non_stop = non_stop;
208 struct thread_resume resume_info;
210 resume_info.thread = ptid;
211 resume_info.kind = resume_stop;
212 resume_info.sig = GDB_SIGNAL_0;
213 the_target->resume (&resume_info, 1);
215 non_stop = true;
216 mywait (ptid, &status, 0, 0);
217 non_stop = was_non_stop;
220 /* See target/target.h. */
222 ptid_t
223 target_wait (ptid_t ptid, struct target_waitstatus *status,
224 target_wait_flags options)
226 return the_target->wait (ptid, status, options);
229 /* See target/target.h. */
231 void
232 target_mourn_inferior (ptid_t ptid)
234 the_target->mourn (find_process_pid (ptid.pid ()));
237 /* See target/target.h. */
239 void
240 target_continue_no_signal (ptid_t ptid)
242 struct thread_resume resume_info;
244 resume_info.thread = ptid;
245 resume_info.kind = resume_continue;
246 resume_info.sig = GDB_SIGNAL_0;
247 the_target->resume (&resume_info, 1);
250 /* See target/target.h. */
252 void
253 target_continue (ptid_t ptid, enum gdb_signal signal)
255 struct thread_resume resume_info;
257 resume_info.thread = ptid;
258 resume_info.kind = resume_continue;
259 resume_info.sig = gdb_signal_to_host (signal);
260 the_target->resume (&resume_info, 1);
263 /* See target/target.h. */
266 target_supports_multi_process (void)
268 return the_target->supports_multi_process ();
271 void
272 set_target_ops (process_stratum_target *target)
274 the_target = target;
277 /* Convert pid to printable format. */
279 std::string
280 target_pid_to_str (ptid_t ptid)
282 if (ptid == minus_one_ptid)
283 return string_printf("<all threads>");
284 else if (ptid == null_ptid)
285 return string_printf("<null thread>");
286 else if (ptid.tid () != 0)
287 return string_printf("Thread %d.0x%s",
288 ptid.pid (),
289 phex_nz (ptid.tid (), sizeof (ULONGEST)));
290 else if (ptid.lwp () != 0)
291 return string_printf("LWP %d.%ld",
292 ptid.pid (), ptid.lwp ());
293 else
294 return string_printf("Process %d",
295 ptid.pid ());
299 kill_inferior (process_info *proc)
301 gdb_agent_about_to_close (proc->pid);
303 return the_target->kill (proc);
306 /* Define it. */
308 target_terminal_state target_terminal::m_terminal_state
309 = target_terminal_state::is_ours;
311 /* See target/target.h. */
313 void
314 target_terminal::init ()
316 /* Placeholder needed because of fork_inferior. Not necessary on
317 GDBserver. */
320 /* See target/target.h. */
322 void
323 target_terminal::inferior ()
325 /* Placeholder needed because of fork_inferior. Not necessary on
326 GDBserver. */
329 /* See target/target.h. */
331 void
332 target_terminal::ours ()
334 /* Placeholder needed because of fork_inferior. Not necessary on
335 GDBserver. */
338 /* See target/target.h. */
340 void
341 target_terminal::ours_for_output (void)
343 /* Placeholder. */
346 /* See target/target.h. */
348 void
349 target_terminal::info (const char *arg, int from_tty)
351 /* Placeholder. */
354 /* Default implementations of target ops.
355 See target.h for definitions. */
357 void
358 process_stratum_target::post_create_inferior ()
360 /* Nop. */
364 process_stratum_target::prepare_to_access_memory ()
366 return 0;
369 void
370 process_stratum_target::done_accessing_memory ()
372 /* Nop. */
375 void
376 process_stratum_target::look_up_symbols ()
378 /* Nop. */
381 bool
382 process_stratum_target::supports_read_auxv ()
384 return false;
388 process_stratum_target::read_auxv (CORE_ADDR offset, unsigned char *myaddr,
389 unsigned int len)
391 gdb_assert_not_reached ("target op read_auxv not supported");
394 bool
395 process_stratum_target::supports_z_point_type (char z_type)
397 return false;
401 process_stratum_target::insert_point (enum raw_bkpt_type type,
402 CORE_ADDR addr,
403 int size, raw_breakpoint *bp)
405 return 1;
409 process_stratum_target::remove_point (enum raw_bkpt_type type,
410 CORE_ADDR addr,
411 int size, raw_breakpoint *bp)
413 return 1;
416 bool
417 process_stratum_target::stopped_by_sw_breakpoint ()
419 return false;
422 bool
423 process_stratum_target::supports_stopped_by_sw_breakpoint ()
425 return false;
428 bool
429 process_stratum_target::stopped_by_hw_breakpoint ()
431 return false;
434 bool
435 process_stratum_target::supports_stopped_by_hw_breakpoint ()
437 return false;
440 bool
441 process_stratum_target::supports_hardware_single_step ()
443 return false;
446 bool
447 process_stratum_target::stopped_by_watchpoint ()
449 return false;
452 CORE_ADDR
453 process_stratum_target::stopped_data_address ()
455 return 0;
458 bool
459 process_stratum_target::supports_read_offsets ()
461 return false;
464 bool
465 process_stratum_target::supports_memory_tagging ()
467 return false;
470 bool
471 process_stratum_target::fetch_memtags (CORE_ADDR address, size_t len,
472 gdb::byte_vector &tags, int type)
474 gdb_assert_not_reached ("target op fetch_memtags not supported");
477 bool
478 process_stratum_target::store_memtags (CORE_ADDR address, size_t len,
479 const gdb::byte_vector &tags, int type)
481 gdb_assert_not_reached ("target op store_memtags not supported");
485 process_stratum_target::read_offsets (CORE_ADDR *text, CORE_ADDR *data)
487 gdb_assert_not_reached ("target op read_offsets not supported");
490 bool
491 process_stratum_target::supports_get_tls_address ()
493 return false;
497 process_stratum_target::get_tls_address (thread_info *thread,
498 CORE_ADDR offset,
499 CORE_ADDR load_module,
500 CORE_ADDR *address)
502 gdb_assert_not_reached ("target op get_tls_address not supported");
505 bool
506 process_stratum_target::supports_qxfer_osdata ()
508 return false;
512 process_stratum_target::qxfer_osdata (const char *annex,
513 unsigned char *readbuf,
514 unsigned const char *writebuf,
515 CORE_ADDR offset, int len)
517 gdb_assert_not_reached ("target op qxfer_osdata not supported");
520 bool
521 process_stratum_target::supports_qxfer_siginfo ()
523 return false;
527 process_stratum_target::qxfer_siginfo (const char *annex,
528 unsigned char *readbuf,
529 unsigned const char *writebuf,
530 CORE_ADDR offset, int len)
532 gdb_assert_not_reached ("target op qxfer_siginfo not supported");
535 bool
536 process_stratum_target::supports_non_stop ()
538 return false;
541 bool
542 process_stratum_target::async (bool enable)
544 return false;
548 process_stratum_target::start_non_stop (bool enable)
550 if (enable)
551 return -1;
552 else
553 return 0;
556 bool
557 process_stratum_target::supports_multi_process ()
559 return false;
562 bool
563 process_stratum_target::supports_fork_events ()
565 return false;
568 bool
569 process_stratum_target::supports_vfork_events ()
571 return false;
574 bool
575 process_stratum_target::supports_exec_events ()
577 return false;
580 void
581 process_stratum_target::handle_new_gdb_connection ()
583 /* Nop. */
587 process_stratum_target::handle_monitor_command (char *mon)
589 return 0;
593 process_stratum_target::core_of_thread (ptid_t ptid)
595 return -1;
598 bool
599 process_stratum_target::supports_read_loadmap ()
601 return false;
605 process_stratum_target::read_loadmap (const char *annex,
606 CORE_ADDR offset,
607 unsigned char *myaddr,
608 unsigned int len)
610 gdb_assert_not_reached ("target op read_loadmap not supported");
613 void
614 process_stratum_target::process_qsupported
615 (gdb::array_view<const char * const> features)
617 /* Nop. */
620 bool
621 process_stratum_target::supports_tracepoints ()
623 return false;
626 CORE_ADDR
627 process_stratum_target::read_pc (regcache *regcache)
629 gdb_assert_not_reached ("process_target::read_pc: Unable to find PC");
632 void
633 process_stratum_target::write_pc (regcache *regcache, CORE_ADDR pc)
635 gdb_assert_not_reached ("process_target::write_pc: Unable to update PC");
638 bool
639 process_stratum_target::supports_thread_stopped ()
641 return false;
644 bool
645 process_stratum_target::thread_stopped (thread_info *thread)
647 gdb_assert_not_reached ("target op thread_stopped not supported");
650 bool
651 process_stratum_target::supports_get_tib_address ()
653 return false;
657 process_stratum_target::get_tib_address (ptid_t ptid, CORE_ADDR *address)
659 gdb_assert_not_reached ("target op get_tib_address not supported");
662 void
663 process_stratum_target::pause_all (bool freeze)
665 /* Nop. */
668 void
669 process_stratum_target::unpause_all (bool unfreeze)
671 /* Nop. */
674 void
675 process_stratum_target::stabilize_threads ()
677 /* Nop. */
680 bool
681 process_stratum_target::supports_fast_tracepoints ()
683 return false;
687 process_stratum_target::install_fast_tracepoint_jump_pad
688 (CORE_ADDR tpoint, CORE_ADDR tpaddr, CORE_ADDR collector,
689 CORE_ADDR lockaddr, ULONGEST orig_size, CORE_ADDR *jump_entry,
690 CORE_ADDR *trampoline, ULONGEST *trampoline_size,
691 unsigned char *jjump_pad_insn, ULONGEST *jjump_pad_insn_size,
692 CORE_ADDR *adjusted_insn_addr, CORE_ADDR *adjusted_insn_addr_end,
693 char *err)
695 gdb_assert_not_reached ("target op install_fast_tracepoint_jump_pad "
696 "not supported");
700 process_stratum_target::get_min_fast_tracepoint_insn_len ()
702 return 0;
705 struct emit_ops *
706 process_stratum_target::emit_ops ()
708 return nullptr;
711 bool
712 process_stratum_target::supports_disable_randomization ()
714 return false;
717 bool
718 process_stratum_target::supports_qxfer_libraries_svr4 ()
720 return false;
724 process_stratum_target::qxfer_libraries_svr4 (const char *annex,
725 unsigned char *readbuf,
726 unsigned const char *writebuf,
727 CORE_ADDR offset, int len)
729 gdb_assert_not_reached ("target op qxfer_libraries_svr4 not supported");
732 bool
733 process_stratum_target::supports_agent ()
735 return false;
738 btrace_target_info *
739 process_stratum_target::enable_btrace (thread_info *tp,
740 const btrace_config *conf)
742 error (_("Target does not support branch tracing."));
746 process_stratum_target::disable_btrace (btrace_target_info *tinfo)
748 error (_("Target does not support branch tracing."));
752 process_stratum_target::read_btrace (btrace_target_info *tinfo,
753 buffer *buffer,
754 enum btrace_read_type type)
756 error (_("Target does not support branch tracing."));
760 process_stratum_target::read_btrace_conf (const btrace_target_info *tinfo,
761 buffer *buffer)
763 error (_("Target does not support branch tracing."));
766 bool
767 process_stratum_target::supports_range_stepping ()
769 return false;
772 bool
773 process_stratum_target::supports_pid_to_exec_file ()
775 return false;
778 const char *
779 process_stratum_target::pid_to_exec_file (int pid)
781 gdb_assert_not_reached ("target op pid_to_exec_file not supported");
784 bool
785 process_stratum_target::supports_multifs ()
787 return false;
791 process_stratum_target::multifs_open (int pid, const char *filename,
792 int flags, mode_t mode)
794 return open (filename, flags, mode);
798 process_stratum_target::multifs_unlink (int pid, const char *filename)
800 return unlink (filename);
803 ssize_t
804 process_stratum_target::multifs_readlink (int pid, const char *filename,
805 char *buf, size_t bufsiz)
807 return readlink (filename, buf, bufsiz);
811 process_stratum_target::breakpoint_kind_from_pc (CORE_ADDR *pcptr)
813 /* The default behavior is to use the size of a breakpoint as the
814 kind. */
815 int size = 0;
816 sw_breakpoint_from_kind (0, &size);
817 return size;
821 process_stratum_target::breakpoint_kind_from_current_state (CORE_ADDR *pcptr)
823 return breakpoint_kind_from_pc (pcptr);
826 const char *
827 process_stratum_target::thread_name (ptid_t thread)
829 return nullptr;
832 bool
833 process_stratum_target::thread_handle (ptid_t ptid, gdb_byte **handle,
834 int *handle_len)
836 return false;
839 thread_info *
840 process_stratum_target::thread_pending_parent (thread_info *thread)
842 return nullptr;
845 thread_info *
846 process_stratum_target::thread_pending_child (thread_info *thread)
848 return nullptr;
851 bool
852 process_stratum_target::supports_software_single_step ()
854 return false;
857 bool
858 process_stratum_target::supports_catch_syscall ()
860 return false;
864 process_stratum_target::get_ipa_tdesc_idx ()
866 return 0;