gdbsupport: allow passing format string to scoped_debug_start_end
[deliverable/binutils-gdb.git] / gdbserver / target.cc
... / ...
CommitLineData
1/* Target operations for the remote server for GDB.
2 Copyright (C) 2002-2021 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
30process_stratum_target *the_target;
31
32int
33set_desired_thread ()
34{
35 client_state &cs = get_client_state ();
36 thread_info *found = find_thread_ptid (cs.general_thread);
37
38 current_thread = found;
39 return (current_thread != NULL);
40}
41
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. */
45static ptid_t prev_general_thread;
46
47/* See target.h. */
48
49int
50prepare_to_access_memory (void)
51{
52 client_state &cs = get_client_state ();
53
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;
60
61 /* Save the general thread value, since prepare_to_access_memory could change
62 it. */
63 prev_general_thread = cs.general_thread;
64
65 int res = the_target->prepare_to_access_memory ();
66 if (res != 0)
67 return res;
68
69 for_each_thread (prev_general_thread.pid (), [&] (thread_info *thread)
70 {
71 if (mythread_alive (thread->id))
72 {
73 if (stopped == NULL && the_target->supports_thread_stopped ()
74 && target_thread_stopped (thread))
75 stopped = thread;
76
77 if (first == NULL)
78 first = thread;
79
80 if (current == NULL && prev_general_thread == thread->id)
81 current = thread;
82 }
83 });
84
85 /* The thread we end up choosing. */
86 struct thread_info *thread;
87
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
99 {
100 done_accessing_memory ();
101 return 1;
102 }
103
104 current_thread = thread;
105 cs.general_thread = ptid_of (thread);
106
107 return 0;
108}
109
110/* See target.h. */
111
112void
113done_accessing_memory (void)
114{
115 client_state &cs = get_client_state ();
116
117 the_target->done_accessing_memory ();
118
119 /* Restore the previous selected thread. */
120 cs.general_thread = prev_general_thread;
121 switch_to_thread (the_target, cs.general_thread);
122}
123
124int
125read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
126{
127 int res;
128 res = the_target->read_memory (memaddr, myaddr, len);
129 check_mem_read (memaddr, myaddr, len);
130 return res;
131}
132
133/* See target/target.h. */
134
135int
136target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
137{
138 return read_inferior_memory (memaddr, myaddr, len);
139}
140
141/* See target/target.h. */
142
143int
144target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
145{
146 return read_inferior_memory (memaddr, (gdb_byte *) result, sizeof (*result));
147}
148
149/* See target/target.h. */
150
151int
152target_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
153 ssize_t len)
154{
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);
160}
161
162ptid_t
163mywait (ptid_t ptid, struct target_waitstatus *ourstatus,
164 target_wait_flags options, int connected_wait)
165{
166 ptid_t ret;
167
168 if (connected_wait)
169 server_waiting = 1;
170
171 ret = target_wait (ptid, ourstatus, options);
172
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->kind = TARGET_WAITKIND_STOPPED;
177
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 ())
185 {
186 if (ourstatus->kind == TARGET_WAITKIND_EXITED)
187 fprintf (stderr,
188 "\nChild exited with status %d\n", ourstatus->value.integer);
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->value.sig),
192 gdb_signal_to_name (ourstatus->value.sig));
193 }
194
195 if (connected_wait)
196 server_waiting = 0;
197
198 return ret;
199}
200
201/* See target/target.h. */
202
203void
204target_stop_and_wait (ptid_t ptid)
205{
206 struct target_waitstatus status;
207 bool was_non_stop = non_stop;
208 struct thread_resume resume_info;
209
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);
214
215 non_stop = true;
216 mywait (ptid, &status, 0, 0);
217 non_stop = was_non_stop;
218}
219
220/* See target/target.h. */
221
222ptid_t
223target_wait (ptid_t ptid, struct target_waitstatus *status,
224 target_wait_flags options)
225{
226 return the_target->wait (ptid, status, options);
227}
228
229/* See target/target.h. */
230
231void
232target_mourn_inferior (ptid_t ptid)
233{
234 the_target->mourn (find_process_pid (ptid.pid ()));
235}
236
237/* See target/target.h. */
238
239void
240target_continue_no_signal (ptid_t ptid)
241{
242 struct thread_resume resume_info;
243
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);
248}
249
250/* See target/target.h. */
251
252void
253target_continue (ptid_t ptid, enum gdb_signal signal)
254{
255 struct thread_resume resume_info;
256
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);
261}
262
263/* See target/target.h. */
264
265int
266target_supports_multi_process (void)
267{
268 return the_target->supports_multi_process ();
269}
270
271void
272set_target_ops (process_stratum_target *target)
273{
274 the_target = target;
275}
276
277/* Convert pid to printable format. */
278
279const char *
280target_pid_to_str (ptid_t ptid)
281{
282 static char buf[80];
283
284 if (ptid == minus_one_ptid)
285 xsnprintf (buf, sizeof (buf), "<all threads>");
286 else if (ptid == null_ptid)
287 xsnprintf (buf, sizeof (buf), "<null thread>");
288 else if (ptid.tid () != 0)
289 xsnprintf (buf, sizeof (buf), "Thread %d.0x%lx",
290 ptid.pid (), ptid.tid ());
291 else if (ptid.lwp () != 0)
292 xsnprintf (buf, sizeof (buf), "LWP %d.%ld",
293 ptid.pid (), ptid.lwp ());
294 else
295 xsnprintf (buf, sizeof (buf), "Process %d",
296 ptid.pid ());
297
298 return buf;
299}
300
301int
302kill_inferior (process_info *proc)
303{
304 gdb_agent_about_to_close (proc->pid);
305
306 return the_target->kill (proc);
307}
308
309/* Define it. */
310
311target_terminal_state target_terminal::m_terminal_state
312 = target_terminal_state::is_ours;
313
314/* See target/target.h. */
315
316void
317target_terminal::init ()
318{
319 /* Placeholder needed because of fork_inferior. Not necessary on
320 GDBserver. */
321}
322
323/* See target/target.h. */
324
325void
326target_terminal::inferior ()
327{
328 /* Placeholder needed because of fork_inferior. Not necessary on
329 GDBserver. */
330}
331
332/* See target/target.h. */
333
334void
335target_terminal::ours ()
336{
337 /* Placeholder needed because of fork_inferior. Not necessary on
338 GDBserver. */
339}
340
341/* See target/target.h. */
342
343void
344target_terminal::ours_for_output (void)
345{
346 /* Placeholder. */
347}
348
349/* See target/target.h. */
350
351void
352target_terminal::info (const char *arg, int from_tty)
353{
354 /* Placeholder. */
355}
356
357/* Default implementations of target ops.
358 See target.h for definitions. */
359
360void
361process_stratum_target::post_create_inferior ()
362{
363 /* Nop. */
364}
365
366int
367process_stratum_target::prepare_to_access_memory ()
368{
369 return 0;
370}
371
372void
373process_stratum_target::done_accessing_memory ()
374{
375 /* Nop. */
376}
377
378void
379process_stratum_target::look_up_symbols ()
380{
381 /* Nop. */
382}
383
384bool
385process_stratum_target::supports_read_auxv ()
386{
387 return false;
388}
389
390int
391process_stratum_target::read_auxv (CORE_ADDR offset, unsigned char *myaddr,
392 unsigned int len)
393{
394 gdb_assert_not_reached ("target op read_auxv not supported");
395}
396
397bool
398process_stratum_target::supports_z_point_type (char z_type)
399{
400 return false;
401}
402
403int
404process_stratum_target::insert_point (enum raw_bkpt_type type,
405 CORE_ADDR addr,
406 int size, raw_breakpoint *bp)
407{
408 return 1;
409}
410
411int
412process_stratum_target::remove_point (enum raw_bkpt_type type,
413 CORE_ADDR addr,
414 int size, raw_breakpoint *bp)
415{
416 return 1;
417}
418
419bool
420process_stratum_target::stopped_by_sw_breakpoint ()
421{
422 return false;
423}
424
425bool
426process_stratum_target::supports_stopped_by_sw_breakpoint ()
427{
428 return false;
429}
430
431bool
432process_stratum_target::stopped_by_hw_breakpoint ()
433{
434 return false;
435}
436
437bool
438process_stratum_target::supports_stopped_by_hw_breakpoint ()
439{
440 return false;
441}
442
443bool
444process_stratum_target::supports_hardware_single_step ()
445{
446 return false;
447}
448
449bool
450process_stratum_target::stopped_by_watchpoint ()
451{
452 return false;
453}
454
455CORE_ADDR
456process_stratum_target::stopped_data_address ()
457{
458 return 0;
459}
460
461bool
462process_stratum_target::supports_read_offsets ()
463{
464 return false;
465}
466
467bool
468process_stratum_target::supports_memory_tagging ()
469{
470 return false;
471}
472
473bool
474process_stratum_target::fetch_memtags (CORE_ADDR address, size_t len,
475 gdb::byte_vector &tags, int type)
476{
477 gdb_assert_not_reached ("target op fetch_memtags not supported");
478}
479
480bool
481process_stratum_target::store_memtags (CORE_ADDR address, size_t len,
482 const gdb::byte_vector &tags, int type)
483{
484 gdb_assert_not_reached ("target op store_memtags not supported");
485}
486
487int
488process_stratum_target::read_offsets (CORE_ADDR *text, CORE_ADDR *data)
489{
490 gdb_assert_not_reached ("target op read_offsets not supported");
491}
492
493bool
494process_stratum_target::supports_get_tls_address ()
495{
496 return false;
497}
498
499int
500process_stratum_target::get_tls_address (thread_info *thread,
501 CORE_ADDR offset,
502 CORE_ADDR load_module,
503 CORE_ADDR *address)
504{
505 gdb_assert_not_reached ("target op get_tls_address not supported");
506}
507
508bool
509process_stratum_target::supports_qxfer_osdata ()
510{
511 return false;
512}
513
514int
515process_stratum_target::qxfer_osdata (const char *annex,
516 unsigned char *readbuf,
517 unsigned const char *writebuf,
518 CORE_ADDR offset, int len)
519{
520 gdb_assert_not_reached ("target op qxfer_osdata not supported");
521}
522
523bool
524process_stratum_target::supports_qxfer_siginfo ()
525{
526 return false;
527}
528
529int
530process_stratum_target::qxfer_siginfo (const char *annex,
531 unsigned char *readbuf,
532 unsigned const char *writebuf,
533 CORE_ADDR offset, int len)
534{
535 gdb_assert_not_reached ("target op qxfer_siginfo not supported");
536}
537
538bool
539process_stratum_target::supports_non_stop ()
540{
541 return false;
542}
543
544bool
545process_stratum_target::async (bool enable)
546{
547 return false;
548}
549
550int
551process_stratum_target::start_non_stop (bool enable)
552{
553 if (enable)
554 return -1;
555 else
556 return 0;
557}
558
559bool
560process_stratum_target::supports_multi_process ()
561{
562 return false;
563}
564
565bool
566process_stratum_target::supports_fork_events ()
567{
568 return false;
569}
570
571bool
572process_stratum_target::supports_vfork_events ()
573{
574 return false;
575}
576
577bool
578process_stratum_target::supports_exec_events ()
579{
580 return false;
581}
582
583void
584process_stratum_target::handle_new_gdb_connection ()
585{
586 /* Nop. */
587}
588
589int
590process_stratum_target::handle_monitor_command (char *mon)
591{
592 return 0;
593}
594
595int
596process_stratum_target::core_of_thread (ptid_t ptid)
597{
598 return -1;
599}
600
601bool
602process_stratum_target::supports_read_loadmap ()
603{
604 return false;
605}
606
607int
608process_stratum_target::read_loadmap (const char *annex,
609 CORE_ADDR offset,
610 unsigned char *myaddr,
611 unsigned int len)
612{
613 gdb_assert_not_reached ("target op read_loadmap not supported");
614}
615
616void
617process_stratum_target::process_qsupported
618 (gdb::array_view<const char * const> features)
619{
620 /* Nop. */
621}
622
623bool
624process_stratum_target::supports_tracepoints ()
625{
626 return false;
627}
628
629CORE_ADDR
630process_stratum_target::read_pc (regcache *regcache)
631{
632 gdb_assert_not_reached ("process_target::read_pc: Unable to find PC");
633}
634
635void
636process_stratum_target::write_pc (regcache *regcache, CORE_ADDR pc)
637{
638 gdb_assert_not_reached ("process_target::write_pc: Unable to update PC");
639}
640
641bool
642process_stratum_target::supports_thread_stopped ()
643{
644 return false;
645}
646
647bool
648process_stratum_target::thread_stopped (thread_info *thread)
649{
650 gdb_assert_not_reached ("target op thread_stopped not supported");
651}
652
653bool
654process_stratum_target::supports_get_tib_address ()
655{
656 return false;
657}
658
659int
660process_stratum_target::get_tib_address (ptid_t ptid, CORE_ADDR *address)
661{
662 gdb_assert_not_reached ("target op get_tib_address not supported");
663}
664
665void
666process_stratum_target::pause_all (bool freeze)
667{
668 /* Nop. */
669}
670
671void
672process_stratum_target::unpause_all (bool unfreeze)
673{
674 /* Nop. */
675}
676
677void
678process_stratum_target::stabilize_threads ()
679{
680 /* Nop. */
681}
682
683bool
684process_stratum_target::supports_fast_tracepoints ()
685{
686 return false;
687}
688
689int
690process_stratum_target::install_fast_tracepoint_jump_pad
691 (CORE_ADDR tpoint, CORE_ADDR tpaddr, CORE_ADDR collector,
692 CORE_ADDR lockaddr, ULONGEST orig_size, CORE_ADDR *jump_entry,
693 CORE_ADDR *trampoline, ULONGEST *trampoline_size,
694 unsigned char *jjump_pad_insn, ULONGEST *jjump_pad_insn_size,
695 CORE_ADDR *adjusted_insn_addr, CORE_ADDR *adjusted_insn_addr_end,
696 char *err)
697{
698 gdb_assert_not_reached ("target op install_fast_tracepoint_jump_pad "
699 "not supported");
700}
701
702int
703process_stratum_target::get_min_fast_tracepoint_insn_len ()
704{
705 return 0;
706}
707
708struct emit_ops *
709process_stratum_target::emit_ops ()
710{
711 return nullptr;
712}
713
714bool
715process_stratum_target::supports_disable_randomization ()
716{
717 return false;
718}
719
720bool
721process_stratum_target::supports_qxfer_libraries_svr4 ()
722{
723 return false;
724}
725
726int
727process_stratum_target::qxfer_libraries_svr4 (const char *annex,
728 unsigned char *readbuf,
729 unsigned const char *writebuf,
730 CORE_ADDR offset, int len)
731{
732 gdb_assert_not_reached ("target op qxfer_libraries_svr4 not supported");
733}
734
735bool
736process_stratum_target::supports_agent ()
737{
738 return false;
739}
740
741btrace_target_info *
742process_stratum_target::enable_btrace (ptid_t ptid, const btrace_config *conf)
743{
744 error (_("Target does not support branch tracing."));
745}
746
747int
748process_stratum_target::disable_btrace (btrace_target_info *tinfo)
749{
750 error (_("Target does not support branch tracing."));
751}
752
753int
754process_stratum_target::read_btrace (btrace_target_info *tinfo,
755 buffer *buffer,
756 enum btrace_read_type type)
757{
758 error (_("Target does not support branch tracing."));
759}
760
761int
762process_stratum_target::read_btrace_conf (const btrace_target_info *tinfo,
763 buffer *buffer)
764{
765 error (_("Target does not support branch tracing."));
766}
767
768bool
769process_stratum_target::supports_range_stepping ()
770{
771 return false;
772}
773
774bool
775process_stratum_target::supports_pid_to_exec_file ()
776{
777 return false;
778}
779
780const char *
781process_stratum_target::pid_to_exec_file (int pid)
782{
783 gdb_assert_not_reached ("target op pid_to_exec_file not supported");
784}
785
786bool
787process_stratum_target::supports_multifs ()
788{
789 return false;
790}
791
792int
793process_stratum_target::multifs_open (int pid, const char *filename,
794 int flags, mode_t mode)
795{
796 return open (filename, flags, mode);
797}
798
799int
800process_stratum_target::multifs_unlink (int pid, const char *filename)
801{
802 return unlink (filename);
803}
804
805ssize_t
806process_stratum_target::multifs_readlink (int pid, const char *filename,
807 char *buf, size_t bufsiz)
808{
809 return readlink (filename, buf, bufsiz);
810}
811
812int
813process_stratum_target::breakpoint_kind_from_pc (CORE_ADDR *pcptr)
814{
815 /* The default behavior is to use the size of a breakpoint as the
816 kind. */
817 int size = 0;
818 sw_breakpoint_from_kind (0, &size);
819 return size;
820}
821
822int
823process_stratum_target::breakpoint_kind_from_current_state (CORE_ADDR *pcptr)
824{
825 return breakpoint_kind_from_pc (pcptr);
826}
827
828const char *
829process_stratum_target::thread_name (ptid_t thread)
830{
831 return nullptr;
832}
833
834bool
835process_stratum_target::thread_handle (ptid_t ptid, gdb_byte **handle,
836 int *handle_len)
837{
838 return false;
839}
840
841bool
842process_stratum_target::supports_software_single_step ()
843{
844 return false;
845}
846
847bool
848process_stratum_target::supports_catch_syscall ()
849{
850 return false;
851}
852
853int
854process_stratum_target::get_ipa_tdesc_idx ()
855{
856 return 0;
857}
This page took 0.024223 seconds and 4 git commands to generate.