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