Re: xcoff: implement linker relaxation
[binutils-gdb.git] / gdbserver / target.cc
1 /* Target operations for the remote server for GDB.
2 Copyright (C) 2002-2022 Free Software Foundation, Inc.
3
4 Contributed by MontaVista Software.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
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>
29
30 process_stratum_target *the_target;
31
32 int
33 set_desired_thread ()
34 {
35 client_state &cs = get_client_state ();
36 thread_info *found = find_thread_ptid (cs.general_thread);
37
38 switch_to_thread (found);
39 return (current_thread != NULL);
40 }
41
42 int
43 read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
44 {
45 /* At the time of writing, GDB only sends write packets with LEN==0,
46 not read packets (see comment in target_write_memory), but it
47 doesn't hurt to prevent problems if it ever does, or we're
48 connected to some client other than GDB that does. */
49 if (len == 0)
50 return 0;
51
52 int res = the_target->read_memory (memaddr, myaddr, len);
53 check_mem_read (memaddr, myaddr, len);
54 return res;
55 }
56
57 /* See target/target.h. */
58
59 int
60 target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
61 {
62 return read_inferior_memory (memaddr, myaddr, len);
63 }
64
65 /* See target/target.h. */
66
67 int
68 target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
69 {
70 return read_inferior_memory (memaddr, (gdb_byte *) result, sizeof (*result));
71 }
72
73 /* See target/target.h. */
74
75 int
76 target_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
77 ssize_t len)
78 {
79 /* GDB may send X packets with LEN==0, for probing packet support.
80 If we let such a request go through, then buffer.data() below may
81 return NULL, which may confuse target implementations. Handle it
82 here to avoid lower levels having to care about this case. */
83 if (len == 0)
84 return 0;
85
86 /* Make a copy of the data because check_mem_write may need to
87 update it. */
88 gdb::byte_vector buffer (myaddr, myaddr + len);
89 check_mem_write (memaddr, buffer.data (), myaddr, len);
90 return the_target->write_memory (memaddr, buffer.data (), len);
91 }
92
93 ptid_t
94 mywait (ptid_t ptid, struct target_waitstatus *ourstatus,
95 target_wait_flags options, int connected_wait)
96 {
97 ptid_t ret;
98
99 if (connected_wait)
100 server_waiting = 1;
101
102 ret = target_wait (ptid, ourstatus, options);
103
104 /* We don't expose _LOADED events to gdbserver core. See the
105 `dlls_changed' global. */
106 if (ourstatus->kind () == TARGET_WAITKIND_LOADED)
107 ourstatus->set_stopped (GDB_SIGNAL_0);
108
109 /* If GDB is connected through TCP/serial, then GDBserver will most
110 probably be running on its own terminal/console, so it's nice to
111 print there why is GDBserver exiting. If however, GDB is
112 connected through stdio, then there's no need to spam the GDB
113 console with this -- the user will already see the exit through
114 regular GDB output, in that same terminal. */
115 if (!remote_connection_is_stdio ())
116 {
117 if (ourstatus->kind () == TARGET_WAITKIND_EXITED)
118 fprintf (stderr,
119 "\nChild exited with status %d\n", ourstatus->exit_status ());
120 else if (ourstatus->kind () == TARGET_WAITKIND_SIGNALLED)
121 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
122 gdb_signal_to_host (ourstatus->sig ()),
123 gdb_signal_to_name (ourstatus->sig ()));
124 }
125
126 if (connected_wait)
127 server_waiting = 0;
128
129 return ret;
130 }
131
132 /* See target/target.h. */
133
134 void
135 target_stop_and_wait (ptid_t ptid)
136 {
137 struct target_waitstatus status;
138 bool was_non_stop = non_stop;
139 struct thread_resume resume_info;
140
141 resume_info.thread = ptid;
142 resume_info.kind = resume_stop;
143 resume_info.sig = GDB_SIGNAL_0;
144 the_target->resume (&resume_info, 1);
145
146 non_stop = true;
147 mywait (ptid, &status, 0, 0);
148 non_stop = was_non_stop;
149 }
150
151 /* See target/target.h. */
152
153 ptid_t
154 target_wait (ptid_t ptid, struct target_waitstatus *status,
155 target_wait_flags options)
156 {
157 return the_target->wait (ptid, status, options);
158 }
159
160 /* See target/target.h. */
161
162 void
163 target_mourn_inferior (ptid_t ptid)
164 {
165 the_target->mourn (find_process_pid (ptid.pid ()));
166 }
167
168 /* See target/target.h. */
169
170 void
171 target_continue_no_signal (ptid_t ptid)
172 {
173 struct thread_resume resume_info;
174
175 resume_info.thread = ptid;
176 resume_info.kind = resume_continue;
177 resume_info.sig = GDB_SIGNAL_0;
178 the_target->resume (&resume_info, 1);
179 }
180
181 /* See target/target.h. */
182
183 void
184 target_continue (ptid_t ptid, enum gdb_signal signal)
185 {
186 struct thread_resume resume_info;
187
188 resume_info.thread = ptid;
189 resume_info.kind = resume_continue;
190 resume_info.sig = gdb_signal_to_host (signal);
191 the_target->resume (&resume_info, 1);
192 }
193
194 /* See target/target.h. */
195
196 int
197 target_supports_multi_process (void)
198 {
199 return the_target->supports_multi_process ();
200 }
201
202 void
203 set_target_ops (process_stratum_target *target)
204 {
205 the_target = target;
206 }
207
208 /* Convert pid to printable format. */
209
210 std::string
211 target_pid_to_str (ptid_t ptid)
212 {
213 if (ptid == minus_one_ptid)
214 return string_printf("<all threads>");
215 else if (ptid == null_ptid)
216 return string_printf("<null thread>");
217 else if (ptid.tid () != 0)
218 return string_printf("Thread %d.0x%s",
219 ptid.pid (),
220 phex_nz (ptid.tid (), sizeof (ULONGEST)));
221 else if (ptid.lwp () != 0)
222 return string_printf("LWP %d.%ld",
223 ptid.pid (), ptid.lwp ());
224 else
225 return string_printf("Process %d",
226 ptid.pid ());
227 }
228
229 int
230 kill_inferior (process_info *proc)
231 {
232 gdb_agent_about_to_close (proc->pid);
233
234 return the_target->kill (proc);
235 }
236
237 /* Define it. */
238
239 target_terminal_state target_terminal::m_terminal_state
240 = target_terminal_state::is_ours;
241
242 /* See target/target.h. */
243
244 void
245 target_terminal::init ()
246 {
247 /* Placeholder needed because of fork_inferior. Not necessary on
248 GDBserver. */
249 }
250
251 /* See target/target.h. */
252
253 void
254 target_terminal::inferior ()
255 {
256 /* Placeholder needed because of fork_inferior. Not necessary on
257 GDBserver. */
258 }
259
260 /* See target/target.h. */
261
262 void
263 target_terminal::ours ()
264 {
265 /* Placeholder needed because of fork_inferior. Not necessary on
266 GDBserver. */
267 }
268
269 /* See target/target.h. */
270
271 void
272 target_terminal::ours_for_output (void)
273 {
274 /* Placeholder. */
275 }
276
277 /* See target/target.h. */
278
279 void
280 target_terminal::info (const char *arg, int from_tty)
281 {
282 /* Placeholder. */
283 }
284
285 /* Default implementations of target ops.
286 See target.h for definitions. */
287
288 void
289 process_stratum_target::post_create_inferior ()
290 {
291 /* Nop. */
292 }
293
294 void
295 process_stratum_target::look_up_symbols ()
296 {
297 /* Nop. */
298 }
299
300 bool
301 process_stratum_target::supports_read_auxv ()
302 {
303 return false;
304 }
305
306 int
307 process_stratum_target::read_auxv (CORE_ADDR offset, unsigned char *myaddr,
308 unsigned int len)
309 {
310 gdb_assert_not_reached ("target op read_auxv not supported");
311 }
312
313 bool
314 process_stratum_target::supports_z_point_type (char z_type)
315 {
316 return false;
317 }
318
319 int
320 process_stratum_target::insert_point (enum raw_bkpt_type type,
321 CORE_ADDR addr,
322 int size, raw_breakpoint *bp)
323 {
324 return 1;
325 }
326
327 int
328 process_stratum_target::remove_point (enum raw_bkpt_type type,
329 CORE_ADDR addr,
330 int size, raw_breakpoint *bp)
331 {
332 return 1;
333 }
334
335 bool
336 process_stratum_target::stopped_by_sw_breakpoint ()
337 {
338 return false;
339 }
340
341 bool
342 process_stratum_target::supports_stopped_by_sw_breakpoint ()
343 {
344 return false;
345 }
346
347 bool
348 process_stratum_target::stopped_by_hw_breakpoint ()
349 {
350 return false;
351 }
352
353 bool
354 process_stratum_target::supports_stopped_by_hw_breakpoint ()
355 {
356 return false;
357 }
358
359 bool
360 process_stratum_target::supports_hardware_single_step ()
361 {
362 return false;
363 }
364
365 bool
366 process_stratum_target::stopped_by_watchpoint ()
367 {
368 return false;
369 }
370
371 CORE_ADDR
372 process_stratum_target::stopped_data_address ()
373 {
374 return 0;
375 }
376
377 bool
378 process_stratum_target::supports_read_offsets ()
379 {
380 return false;
381 }
382
383 bool
384 process_stratum_target::supports_memory_tagging ()
385 {
386 return false;
387 }
388
389 bool
390 process_stratum_target::fetch_memtags (CORE_ADDR address, size_t len,
391 gdb::byte_vector &tags, int type)
392 {
393 gdb_assert_not_reached ("target op fetch_memtags not supported");
394 }
395
396 bool
397 process_stratum_target::store_memtags (CORE_ADDR address, size_t len,
398 const gdb::byte_vector &tags, int type)
399 {
400 gdb_assert_not_reached ("target op store_memtags not supported");
401 }
402
403 int
404 process_stratum_target::read_offsets (CORE_ADDR *text, CORE_ADDR *data)
405 {
406 gdb_assert_not_reached ("target op read_offsets not supported");
407 }
408
409 bool
410 process_stratum_target::supports_get_tls_address ()
411 {
412 return false;
413 }
414
415 int
416 process_stratum_target::get_tls_address (thread_info *thread,
417 CORE_ADDR offset,
418 CORE_ADDR load_module,
419 CORE_ADDR *address)
420 {
421 gdb_assert_not_reached ("target op get_tls_address not supported");
422 }
423
424 bool
425 process_stratum_target::supports_qxfer_osdata ()
426 {
427 return false;
428 }
429
430 int
431 process_stratum_target::qxfer_osdata (const char *annex,
432 unsigned char *readbuf,
433 unsigned const char *writebuf,
434 CORE_ADDR offset, int len)
435 {
436 gdb_assert_not_reached ("target op qxfer_osdata not supported");
437 }
438
439 bool
440 process_stratum_target::supports_qxfer_siginfo ()
441 {
442 return false;
443 }
444
445 int
446 process_stratum_target::qxfer_siginfo (const char *annex,
447 unsigned char *readbuf,
448 unsigned const char *writebuf,
449 CORE_ADDR offset, int len)
450 {
451 gdb_assert_not_reached ("target op qxfer_siginfo not supported");
452 }
453
454 bool
455 process_stratum_target::supports_non_stop ()
456 {
457 return false;
458 }
459
460 bool
461 process_stratum_target::async (bool enable)
462 {
463 return false;
464 }
465
466 int
467 process_stratum_target::start_non_stop (bool enable)
468 {
469 if (enable)
470 return -1;
471 else
472 return 0;
473 }
474
475 bool
476 process_stratum_target::supports_multi_process ()
477 {
478 return false;
479 }
480
481 bool
482 process_stratum_target::supports_fork_events ()
483 {
484 return false;
485 }
486
487 bool
488 process_stratum_target::supports_vfork_events ()
489 {
490 return false;
491 }
492
493 bool
494 process_stratum_target::supports_exec_events ()
495 {
496 return false;
497 }
498
499 void
500 process_stratum_target::handle_new_gdb_connection ()
501 {
502 /* Nop. */
503 }
504
505 int
506 process_stratum_target::handle_monitor_command (char *mon)
507 {
508 return 0;
509 }
510
511 int
512 process_stratum_target::core_of_thread (ptid_t ptid)
513 {
514 return -1;
515 }
516
517 bool
518 process_stratum_target::supports_read_loadmap ()
519 {
520 return false;
521 }
522
523 int
524 process_stratum_target::read_loadmap (const char *annex,
525 CORE_ADDR offset,
526 unsigned char *myaddr,
527 unsigned int len)
528 {
529 gdb_assert_not_reached ("target op read_loadmap not supported");
530 }
531
532 void
533 process_stratum_target::process_qsupported
534 (gdb::array_view<const char * const> features)
535 {
536 /* Nop. */
537 }
538
539 bool
540 process_stratum_target::supports_tracepoints ()
541 {
542 return false;
543 }
544
545 CORE_ADDR
546 process_stratum_target::read_pc (regcache *regcache)
547 {
548 gdb_assert_not_reached ("process_target::read_pc: Unable to find PC");
549 }
550
551 void
552 process_stratum_target::write_pc (regcache *regcache, CORE_ADDR pc)
553 {
554 gdb_assert_not_reached ("process_target::write_pc: Unable to update PC");
555 }
556
557 bool
558 process_stratum_target::supports_thread_stopped ()
559 {
560 return false;
561 }
562
563 bool
564 process_stratum_target::thread_stopped (thread_info *thread)
565 {
566 gdb_assert_not_reached ("target op thread_stopped not supported");
567 }
568
569 bool
570 process_stratum_target::supports_get_tib_address ()
571 {
572 return false;
573 }
574
575 int
576 process_stratum_target::get_tib_address (ptid_t ptid, CORE_ADDR *address)
577 {
578 gdb_assert_not_reached ("target op get_tib_address not supported");
579 }
580
581 void
582 process_stratum_target::pause_all (bool freeze)
583 {
584 /* Nop. */
585 }
586
587 void
588 process_stratum_target::unpause_all (bool unfreeze)
589 {
590 /* Nop. */
591 }
592
593 void
594 process_stratum_target::stabilize_threads ()
595 {
596 /* Nop. */
597 }
598
599 bool
600 process_stratum_target::supports_fast_tracepoints ()
601 {
602 return false;
603 }
604
605 int
606 process_stratum_target::install_fast_tracepoint_jump_pad
607 (CORE_ADDR tpoint, CORE_ADDR tpaddr, CORE_ADDR collector,
608 CORE_ADDR lockaddr, ULONGEST orig_size, CORE_ADDR *jump_entry,
609 CORE_ADDR *trampoline, ULONGEST *trampoline_size,
610 unsigned char *jjump_pad_insn, ULONGEST *jjump_pad_insn_size,
611 CORE_ADDR *adjusted_insn_addr, CORE_ADDR *adjusted_insn_addr_end,
612 char *err)
613 {
614 gdb_assert_not_reached ("target op install_fast_tracepoint_jump_pad "
615 "not supported");
616 }
617
618 int
619 process_stratum_target::get_min_fast_tracepoint_insn_len ()
620 {
621 return 0;
622 }
623
624 struct emit_ops *
625 process_stratum_target::emit_ops ()
626 {
627 return nullptr;
628 }
629
630 bool
631 process_stratum_target::supports_disable_randomization ()
632 {
633 return false;
634 }
635
636 bool
637 process_stratum_target::supports_qxfer_libraries_svr4 ()
638 {
639 return false;
640 }
641
642 int
643 process_stratum_target::qxfer_libraries_svr4 (const char *annex,
644 unsigned char *readbuf,
645 unsigned const char *writebuf,
646 CORE_ADDR offset, int len)
647 {
648 gdb_assert_not_reached ("target op qxfer_libraries_svr4 not supported");
649 }
650
651 bool
652 process_stratum_target::supports_agent ()
653 {
654 return false;
655 }
656
657 btrace_target_info *
658 process_stratum_target::enable_btrace (thread_info *tp,
659 const btrace_config *conf)
660 {
661 error (_("Target does not support branch tracing."));
662 }
663
664 int
665 process_stratum_target::disable_btrace (btrace_target_info *tinfo)
666 {
667 error (_("Target does not support branch tracing."));
668 }
669
670 int
671 process_stratum_target::read_btrace (btrace_target_info *tinfo,
672 buffer *buffer,
673 enum btrace_read_type type)
674 {
675 error (_("Target does not support branch tracing."));
676 }
677
678 int
679 process_stratum_target::read_btrace_conf (const btrace_target_info *tinfo,
680 buffer *buffer)
681 {
682 error (_("Target does not support branch tracing."));
683 }
684
685 bool
686 process_stratum_target::supports_range_stepping ()
687 {
688 return false;
689 }
690
691 bool
692 process_stratum_target::supports_pid_to_exec_file ()
693 {
694 return false;
695 }
696
697 const char *
698 process_stratum_target::pid_to_exec_file (int pid)
699 {
700 gdb_assert_not_reached ("target op pid_to_exec_file not supported");
701 }
702
703 bool
704 process_stratum_target::supports_multifs ()
705 {
706 return false;
707 }
708
709 int
710 process_stratum_target::multifs_open (int pid, const char *filename,
711 int flags, mode_t mode)
712 {
713 return open (filename, flags, mode);
714 }
715
716 int
717 process_stratum_target::multifs_unlink (int pid, const char *filename)
718 {
719 return unlink (filename);
720 }
721
722 ssize_t
723 process_stratum_target::multifs_readlink (int pid, const char *filename,
724 char *buf, size_t bufsiz)
725 {
726 return readlink (filename, buf, bufsiz);
727 }
728
729 int
730 process_stratum_target::breakpoint_kind_from_pc (CORE_ADDR *pcptr)
731 {
732 /* The default behavior is to use the size of a breakpoint as the
733 kind. */
734 int size = 0;
735 sw_breakpoint_from_kind (0, &size);
736 return size;
737 }
738
739 int
740 process_stratum_target::breakpoint_kind_from_current_state (CORE_ADDR *pcptr)
741 {
742 return breakpoint_kind_from_pc (pcptr);
743 }
744
745 const char *
746 process_stratum_target::thread_name (ptid_t thread)
747 {
748 return nullptr;
749 }
750
751 bool
752 process_stratum_target::thread_handle (ptid_t ptid, gdb_byte **handle,
753 int *handle_len)
754 {
755 return false;
756 }
757
758 thread_info *
759 process_stratum_target::thread_pending_parent (thread_info *thread)
760 {
761 return nullptr;
762 }
763
764 thread_info *
765 process_stratum_target::thread_pending_child (thread_info *thread)
766 {
767 return nullptr;
768 }
769
770 bool
771 process_stratum_target::supports_software_single_step ()
772 {
773 return false;
774 }
775
776 bool
777 process_stratum_target::supports_catch_syscall ()
778 {
779 return false;
780 }
781
782 int
783 process_stratum_target::get_ipa_tdesc_idx ()
784 {
785 return 0;
786 }