gdbserver: turn target op 'supports_range_stepping' into a method
[deliverable/binutils-gdb.git] / gdbserver / target.cc
CommitLineData
ce3a066d 1/* Target operations for the remote server for GDB.
b811d2c2 2 Copyright (C) 2002-2020 Free Software Foundation, Inc.
ce3a066d
DJ
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
ce3a066d
DJ
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
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
ce3a066d
DJ
20
21#include "server.h"
c144c7a0 22#include "tracepoint.h"
d59b55f0 23#include "gdbsupport/byte-vector.h"
ea06bbaa 24#include "hostio.h"
ce3a066d 25
5b6d1e4f 26process_stratum_target *the_target;
ce3a066d 27
f0db101d 28int
f557a88a 29set_desired_thread ()
0d62e5e8 30{
c12a5089
SC
31 client_state &cs = get_client_state ();
32 thread_info *found = find_thread_ptid (cs.general_thread);
0d62e5e8 33
f0db101d
PA
34 current_thread = found;
35 return (current_thread != NULL);
0d62e5e8
DJ
36}
37
a67a9fae
PA
38/* The thread that was current before prepare_to_access_memory was
39 called. done_accessing_memory uses this to restore the previous
40 selected thread. */
41static ptid_t prev_general_thread;
42
43/* See target.h. */
44
45int
46prepare_to_access_memory (void)
47{
c12a5089
SC
48 client_state &cs = get_client_state ();
49
bac608e7
SM
50 /* The first thread found. */
51 struct thread_info *first = NULL;
52 /* The first stopped thread found. */
53 struct thread_info *stopped = NULL;
54 /* The current general thread, if found. */
55 struct thread_info *current = NULL;
a67a9fae 56
bac608e7
SM
57 /* Save the general thread value, since prepare_to_access_memory could change
58 it. */
c12a5089 59 prev_general_thread = cs.general_thread;
a67a9fae 60
79b44087
TBA
61 int res = the_target->pt->prepare_to_access_memory ();
62 if (res != 0)
63 return res;
a67a9fae 64
bac608e7
SM
65 for_each_thread (prev_general_thread.pid (), [&] (thread_info *thread)
66 {
67 if (mythread_alive (thread->id))
68 {
68119632
TBA
69 if (stopped == NULL && the_target->pt->supports_thread_stopped ()
70 && target_thread_stopped (thread))
bac608e7
SM
71 stopped = thread;
72
73 if (first == NULL)
74 first = thread;
75
76 if (current == NULL && prev_general_thread == thread->id)
77 current = thread;
78 }
79 });
80
81 /* The thread we end up choosing. */
82 struct thread_info *thread;
a67a9fae
PA
83
84 /* Prefer a stopped thread. If none is found, try the current
85 thread. Otherwise, take the first thread in the process. If
86 none is found, undo the effects of
87 target->prepare_to_access_memory() and return error. */
bac608e7
SM
88 if (stopped != NULL)
89 thread = stopped;
90 else if (current != NULL)
91 thread = current;
92 else if (first != NULL)
93 thread = first;
a67a9fae
PA
94 else
95 {
96 done_accessing_memory ();
97 return 1;
98 }
99
100 current_thread = thread;
c12a5089 101 cs.general_thread = ptid_of (thread);
a67a9fae
PA
102
103 return 0;
104}
105
106/* See target.h. */
107
108void
109done_accessing_memory (void)
110{
c12a5089
SC
111 client_state &cs = get_client_state ();
112
79b44087 113 the_target->pt->done_accessing_memory ();
a67a9fae
PA
114
115 /* Restore the previous selected thread. */
c12a5089 116 cs.general_thread = prev_general_thread;
5b6d1e4f 117 switch_to_thread (the_target, cs.general_thread);
a67a9fae
PA
118}
119
c3e735a6 120int
f450004a 121read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
611cb4a5 122{
c3e735a6 123 int res;
e2558df3 124 res = the_target->pt->read_memory (memaddr, myaddr, len);
611cb4a5 125 check_mem_read (memaddr, myaddr, len);
c3e735a6 126 return res;
611cb4a5
DJ
127}
128
721ec300
GB
129/* See target/target.h. */
130
131int
132target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
133{
134 return read_inferior_memory (memaddr, myaddr, len);
135}
136
137/* See target/target.h. */
138
139int
140target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
141{
142 return read_inferior_memory (memaddr, (gdb_byte *) result, sizeof (*result));
143}
144
4196ab2a
TT
145/* See target/target.h. */
146
611cb4a5 147int
4196ab2a
TT
148target_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
149 ssize_t len)
0d62e5e8 150{
c6778d00
TT
151 /* Make a copy of the data because check_mem_write may need to
152 update it. */
d59b55f0 153 gdb::byte_vector buffer (myaddr, myaddr + len);
c6778d00 154 check_mem_write (memaddr, buffer.data (), myaddr, len);
e2558df3 155 return the_target->pt->write_memory (memaddr, buffer.data (), len);
0d62e5e8
DJ
156}
157
95954743
PA
158ptid_t
159mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options,
bd99dc85 160 int connected_wait)
611cb4a5 161{
95954743 162 ptid_t ret;
0d62e5e8
DJ
163
164 if (connected_wait)
165 server_waiting = 1;
166
f2b9e3df 167 ret = target_wait (ptid, ourstatus, options);
bd99dc85 168
4210d83e
PA
169 /* We don't expose _LOADED events to gdbserver core. See the
170 `dlls_changed' global. */
171 if (ourstatus->kind == TARGET_WAITKIND_LOADED)
172 ourstatus->kind = TARGET_WAITKIND_STOPPED;
173
1a3d890b
PA
174 /* If GDB is connected through TCP/serial, then GDBserver will most
175 probably be running on its own terminal/console, so it's nice to
176 print there why is GDBserver exiting. If however, GDB is
177 connected through stdio, then there's no need to spam the GDB
178 console with this -- the user will already see the exit through
179 regular GDB output, in that same terminal. */
180 if (!remote_connection_is_stdio ())
181 {
182 if (ourstatus->kind == TARGET_WAITKIND_EXITED)
183 fprintf (stderr,
184 "\nChild exited with status %d\n", ourstatus->value.integer);
185 else if (ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
186 fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
187 gdb_signal_to_host (ourstatus->value.sig),
188 gdb_signal_to_name (ourstatus->value.sig));
189 }
0d62e5e8
DJ
190
191 if (connected_wait)
192 server_waiting = 0;
193
194 return ret;
611cb4a5
DJ
195}
196
f8c1d06b
GB
197/* See target/target.h. */
198
199void
03f4463b 200target_stop_and_wait (ptid_t ptid)
f8c1d06b
GB
201{
202 struct target_waitstatus status;
3e6ec53a 203 bool was_non_stop = non_stop;
17e16485 204 struct thread_resume resume_info;
f8c1d06b 205
17e16485
YQ
206 resume_info.thread = ptid;
207 resume_info.kind = resume_stop;
208 resume_info.sig = GDB_SIGNAL_0;
0e4d7e35 209 the_target->pt->resume (&resume_info, 1);
f8c1d06b 210
3e6ec53a 211 non_stop = true;
f8c1d06b
GB
212 mywait (ptid, &status, 0, 0);
213 non_stop = was_non_stop;
214}
215
216/* See target/target.h. */
217
f2b9e3df
SDJ
218ptid_t
219target_wait (ptid_t ptid, struct target_waitstatus *status, int options)
220{
6532e7e3 221 return the_target->pt->wait (ptid, status, options);
f2b9e3df
SDJ
222}
223
224/* See target/target.h. */
225
bc1e6c81
SDJ
226void
227target_mourn_inferior (ptid_t ptid)
228{
8adb37b9 229 the_target->pt->mourn (find_process_pid (ptid.pid ()));
bc1e6c81
SDJ
230}
231
232/* See target/target.h. */
233
f8c1d06b 234void
03f4463b 235target_continue_no_signal (ptid_t ptid)
f8c1d06b
GB
236{
237 struct thread_resume resume_info;
238
239 resume_info.thread = ptid;
240 resume_info.kind = resume_continue;
241 resume_info.sig = GDB_SIGNAL_0;
0e4d7e35 242 the_target->pt->resume (&resume_info, 1);
f8c1d06b
GB
243}
244
049a8570
SDJ
245/* See target/target.h. */
246
247void
248target_continue (ptid_t ptid, enum gdb_signal signal)
249{
250 struct thread_resume resume_info;
251
252 resume_info.thread = ptid;
253 resume_info.kind = resume_continue;
254 resume_info.sig = gdb_signal_to_host (signal);
0e4d7e35 255 the_target->pt->resume (&resume_info, 1);
049a8570
SDJ
256}
257
1fb77080
SDJ
258/* See target/target.h. */
259
260int
261target_supports_multi_process (void)
262{
652aef77 263 return the_target->pt->supports_multi_process ();
1fb77080
SDJ
264}
265
ce3a066d 266void
5b6d1e4f 267set_target_ops (process_stratum_target *target)
ce3a066d 268{
5b6d1e4f 269 the_target = XNEW (process_stratum_target);
ce3a066d
DJ
270 memcpy (the_target, target, sizeof (*the_target));
271}
95954743
PA
272
273/* Convert pid to printable format. */
274
275const char *
276target_pid_to_str (ptid_t ptid)
277{
278 static char buf[80];
279
d7e15655 280 if (ptid == minus_one_ptid)
6cebaf6e 281 xsnprintf (buf, sizeof (buf), "<all threads>");
d7e15655 282 else if (ptid == null_ptid)
6cebaf6e 283 xsnprintf (buf, sizeof (buf), "<null thread>");
cc6bcb54 284 else if (ptid.tid () != 0)
6cebaf6e 285 xsnprintf (buf, sizeof (buf), "Thread %d.0x%lx",
cc6bcb54 286 ptid.pid (), ptid.tid ());
e38504b3 287 else if (ptid.lwp () != 0)
6cebaf6e 288 xsnprintf (buf, sizeof (buf), "LWP %d.%ld",
e38504b3 289 ptid.pid (), ptid.lwp ());
95954743 290 else
6cebaf6e 291 xsnprintf (buf, sizeof (buf), "Process %d",
e99b03dc 292 ptid.pid ());
95954743
PA
293
294 return buf;
295}
8336d594 296
7255706c 297int
a780ef4f 298kill_inferior (process_info *proc)
7255706c 299{
a780ef4f 300 gdb_agent_about_to_close (proc->pid);
7255706c 301
c6885a57 302 return the_target->pt->kill (proc);
7255706c 303}
70b90b91 304
2e6ee069
AT
305/* Default implementation for breakpoint_kind_for_pc.
306
307 The default behavior for targets that don't implement breakpoint_kind_for_pc
308 is to use the size of a breakpoint as the kind. */
309
310int
311default_breakpoint_kind_from_pc (CORE_ADDR *pcptr)
312{
313 int size = 0;
314
315 gdb_assert (the_target->sw_breakpoint_from_kind != NULL);
316
317 (*the_target->sw_breakpoint_from_kind) (0, &size);
318 return size;
319}
2090129c 320
223ffa71
TT
321/* Define it. */
322
e671cd59
PA
323target_terminal_state target_terminal::m_terminal_state
324 = target_terminal_state::is_ours;
223ffa71 325
2090129c
SDJ
326/* See target/target.h. */
327
328void
223ffa71 329target_terminal::init ()
2090129c
SDJ
330{
331 /* Placeholder needed because of fork_inferior. Not necessary on
332 GDBserver. */
333}
334
335/* See target/target.h. */
336
337void
223ffa71 338target_terminal::inferior ()
2090129c
SDJ
339{
340 /* Placeholder needed because of fork_inferior. Not necessary on
341 GDBserver. */
342}
343
344/* See target/target.h. */
345
346void
223ffa71 347target_terminal::ours ()
2090129c
SDJ
348{
349 /* Placeholder needed because of fork_inferior. Not necessary on
350 GDBserver. */
351}
223ffa71
TT
352
353/* See target/target.h. */
354
355void
356target_terminal::ours_for_output (void)
357{
358 /* Placeholder. */
359}
360
361/* See target/target.h. */
362
363void
364target_terminal::info (const char *arg, int from_tty)
365{
366 /* Placeholder. */
367}
6dee9afb
TBA
368
369/* Default implementations of target ops.
370 See target.h for definitions. */
371
372void
373process_target::post_create_inferior ()
374{
375 /* Nop. */
376}
79b44087
TBA
377
378int
379process_target::prepare_to_access_memory ()
380{
381 return 0;
382}
383
384void
385process_target::done_accessing_memory ()
386{
387 /* Nop. */
388}
2a31c7aa
TBA
389
390void
391process_target::look_up_symbols ()
392{
393 /* Nop. */
394}
eac215cc
TBA
395
396bool
397process_target::supports_read_auxv ()
398{
399 return false;
400}
401
402int
403process_target::read_auxv (CORE_ADDR offset, unsigned char *myaddr,
404 unsigned int len)
405{
406 gdb_assert_not_reached ("target op read_auxv not supported");
407}
a2b2297a
TBA
408
409bool
410process_target::supports_z_point_type (char z_type)
411{
412 return false;
413}
7e0bde70
TBA
414
415int
416process_target::insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
417 int size, raw_breakpoint *bp)
418{
419 return 1;
420}
421
422int
423process_target::remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
424 int size, raw_breakpoint *bp)
425{
426 return 1;
427}
84320c4e
TBA
428
429bool
430process_target::stopped_by_sw_breakpoint ()
431{
432 return false;
433}
434
435bool
436process_target::supports_stopped_by_sw_breakpoint ()
437{
438 return false;
439}
93fe88b2
TBA
440
441bool
442process_target::stopped_by_hw_breakpoint ()
443{
444 return false;
445}
446
447bool
448process_target::supports_stopped_by_hw_breakpoint ()
449{
450 return false;
451}
22aa6223
TBA
452
453bool
454process_target::supports_hardware_single_step ()
455{
456 return false;
457}
6eeb5c55
TBA
458
459bool
460process_target::stopped_by_watchpoint ()
461{
462 return false;
463}
464
465CORE_ADDR
466process_target::stopped_data_address ()
467{
468 return 0;
469}
5203ae1e
TBA
470
471bool
472process_target::supports_read_offsets ()
473{
474 return false;
475}
476
477int
478process_target::read_offsets (CORE_ADDR *text, CORE_ADDR *data)
479{
480 gdb_assert_not_reached ("target op read_offsets not supported");
481}
6e3fd7e9
TBA
482
483bool
484process_target::supports_get_tls_address ()
485{
486 return false;
487}
488
489int
490process_target::get_tls_address (thread_info *thread, CORE_ADDR offset,
491 CORE_ADDR load_module, CORE_ADDR *address)
492{
493 gdb_assert_not_reached ("target op get_tls_address not supported");
494}
ea06bbaa
TBA
495
496void
497process_target::hostio_last_error (char *buf)
498{
499 hostio_last_error_from_errno (buf);
500}
2d0795ee
TBA
501
502bool
503process_target::supports_qxfer_osdata ()
504{
505 return false;
506}
507
508int
509process_target::qxfer_osdata (const char *annex, unsigned char *readbuf,
510 unsigned const char *writebuf,
511 CORE_ADDR offset, int len)
512{
513 gdb_assert_not_reached ("target op qxfer_osdata not supported");
514}
d7abedf7
TBA
515
516bool
517process_target::supports_qxfer_siginfo ()
518{
519 return false;
520}
521
522int
523process_target::qxfer_siginfo (const char *annex, unsigned char *readbuf,
524 unsigned const char *writebuf,
525 CORE_ADDR offset, int len)
526{
527 gdb_assert_not_reached ("target op qxfer_siginfo not supported");
528}
0dc587d4
TBA
529
530bool
531process_target::supports_non_stop ()
532{
533 return false;
534}
535
536bool
537process_target::async (bool enable)
538{
539 return false;
540}
541
542int
543process_target::start_non_stop (bool enable)
544{
545 if (enable)
546 return -1;
547 else
548 return 0;
549}
652aef77
TBA
550
551bool
552process_target::supports_multi_process ()
553{
554 return false;
555}
9690a72a
TBA
556
557bool
558process_target::supports_fork_events ()
559{
560 return false;
561}
562
563bool
564process_target::supports_vfork_events ()
565{
566 return false;
567}
568
569bool
570process_target::supports_exec_events ()
571{
572 return false;
573}
fb00dfce
TBA
574
575void
576process_target::handle_new_gdb_connection ()
577{
578 /* Nop. */
579}
55cf3021
TBA
580
581int
582process_target::handle_monitor_command (char *mon)
583{
584 return 0;
585}
95a45fc1
TBA
586
587int
588process_target::core_of_thread (ptid_t ptid)
589{
590 return -1;
591}
9da41fda
TBA
592
593bool
594process_target::supports_read_loadmap ()
595{
596 return false;
597}
598
599int
600process_target::read_loadmap (const char *annex, CORE_ADDR offset,
601 unsigned char *myaddr, unsigned int len)
602{
603 gdb_assert_not_reached ("target op read_loadmap not supported");
604}
0df28b1b
TBA
605
606void
607process_target::process_qsupported (char **features, int count)
608{
609 /* Nop. */
610}
290732bf
TBA
611
612bool
613process_target::supports_tracepoints ()
614{
615 return false;
616}
770d8f6a
TBA
617
618CORE_ADDR
619process_target::read_pc (regcache *regcache)
620{
621 gdb_assert_not_reached ("process_target::read_pc: Unable to find PC");
622}
623
624void
625process_target::write_pc (regcache *regcache, CORE_ADDR pc)
626{
627 gdb_assert_not_reached ("process_target::write_pc: Unable to update PC");
628}
68119632
TBA
629
630bool
631process_target::supports_thread_stopped ()
632{
633 return false;
634}
635
636bool
637process_target::thread_stopped (thread_info *thread)
638{
639 gdb_assert_not_reached ("target op thread_stopped not supported");
640}
4e2e869c
TBA
641
642bool
643process_target::supports_get_tib_address ()
644{
645 return false;
646}
647
648int
649process_target::get_tib_address (ptid_t ptid, CORE_ADDR *address)
650{
651 gdb_assert_not_reached ("target op get_tib_address not supported");
652}
29e8dc09
TBA
653
654void
655process_target::pause_all (bool freeze)
656{
657 /* Nop. */
658}
659
660void
661process_target::unpause_all (bool unfreeze)
662{
663 /* Nop. */
664}
5c9eb2f2
TBA
665
666void
667process_target::stabilize_threads ()
668{
669 /* Nop. */
670}
c23c9391
TBA
671
672bool
673process_target::supports_fast_tracepoints ()
674{
675 return false;
676}
677
678int
679process_target::install_fast_tracepoint_jump_pad
680 (CORE_ADDR tpoint, CORE_ADDR tpaddr, CORE_ADDR collector,
681 CORE_ADDR lockaddr, ULONGEST orig_size, CORE_ADDR *jump_entry,
682 CORE_ADDR *trampoline, ULONGEST *trampoline_size,
683 unsigned char *jjump_pad_insn, ULONGEST *jjump_pad_insn_size,
684 CORE_ADDR *adjusted_insn_addr, CORE_ADDR *adjusted_insn_addr_end,
685 char *err)
686{
687 gdb_assert_not_reached ("target op install_fast_tracepoint_jump_pad "
688 "not supported");
689}
690
691int
692process_target::get_min_fast_tracepoint_insn_len ()
693{
694 return 0;
695}
345dafad
TBA
696
697struct emit_ops *
698process_target::emit_ops ()
699{
700 return nullptr;
701}
c756403b
TBA
702
703bool
704process_target::supports_disable_randomization ()
705{
706 return false;
707}
974387bb
TBA
708
709bool
710process_target::supports_qxfer_libraries_svr4 ()
711{
712 return false;
713}
714
715int
716process_target::qxfer_libraries_svr4 (const char *annex,
717 unsigned char *readbuf,
718 unsigned const char *writebuf,
719 CORE_ADDR offset, int len)
720{
721 gdb_assert_not_reached ("target op qxfer_libraries_svr4 not supported");
722}
c0245cb9
TBA
723
724bool
725process_target::supports_agent ()
726{
727 return false;
728}
79597bdd
TBA
729
730btrace_target_info *
731process_target::enable_btrace (ptid_t ptid, const btrace_config *conf)
732{
733 error (_("Target does not support branch tracing."));
734}
735
736int
737process_target::disable_btrace (btrace_target_info *tinfo)
738{
739 error (_("Target does not support branch tracing."));
740}
741
742int
743process_target::read_btrace (btrace_target_info *tinfo,
744 buffer *buffer,
745 enum btrace_read_type type)
746{
747 error (_("Target does not support branch tracing."));
748}
749
750int
751process_target::read_btrace_conf (const btrace_target_info *tinfo,
752 buffer *buffer)
753{
754 error (_("Target does not support branch tracing."));
755}
2526e0cd
TBA
756
757bool
758process_target::supports_range_stepping ()
759{
760 return false;
761}
This page took 1.420248 seconds and 4 git commands to generate.