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