gdb/gdbserver/target.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Target operations for the remote server for GDB.
  2.    Copyright (C) 2002-2015 Free Software Foundation, Inc.

  3.    Contributed by MontaVista Software.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "server.h"
  16. #include "tracepoint.h"

  17. struct target_ops *the_target;

  18. void
  19. set_desired_thread (int use_general)
  20. {
  21.   struct thread_info *found;

  22.   if (use_general == 1)
  23.     found = find_thread_ptid (general_thread);
  24.   else
  25.     found = find_thread_ptid (cont_thread);

  26.   if (found == NULL)
  27.     current_thread = get_first_thread ();
  28.   else
  29.     current_thread = found;
  30. }

  31. int
  32. read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
  33. {
  34.   int res;
  35.   res = (*the_target->read_memory) (memaddr, myaddr, len);
  36.   check_mem_read (memaddr, myaddr, len);
  37.   return res;
  38. }

  39. /* See target/target.h.  */

  40. int
  41. target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
  42. {
  43.   return read_inferior_memory (memaddr, myaddr, len);
  44. }

  45. /* See target/target.h.  */

  46. int
  47. target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
  48. {
  49.   return read_inferior_memory (memaddr, (gdb_byte *) result, sizeof (*result));
  50. }

  51. int
  52. write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr,
  53.                        int len)
  54. {
  55.   /* Lacking cleanups, there is some potential for a memory leak if the
  56.      write fails and we go through error().  Make sure that no more than
  57.      one buffer is ever pending by making BUFFER static.  */
  58.   static unsigned char *buffer = 0;
  59.   int res;

  60.   if (buffer != NULL)
  61.     free (buffer);

  62.   buffer = xmalloc (len);
  63.   memcpy (buffer, myaddr, len);
  64.   check_mem_write (memaddr, buffer, myaddr, len);
  65.   res = (*the_target->write_memory) (memaddr, buffer, len);
  66.   free (buffer);
  67.   buffer = NULL;

  68.   return res;
  69. }

  70. /* See target/target.h.  */

  71. int
  72. target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
  73. {
  74.   return write_inferior_memory (memaddr, myaddr, len);
  75. }

  76. ptid_t
  77. mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options,
  78.         int connected_wait)
  79. {
  80.   ptid_t ret;

  81.   if (connected_wait)
  82.     server_waiting = 1;

  83.   ret = (*the_target->wait) (ptid, ourstatus, options);

  84.   /* We don't expose _LOADED events to gdbserver core.  See the
  85.      `dlls_changed' global.  */
  86.   if (ourstatus->kind == TARGET_WAITKIND_LOADED)
  87.     ourstatus->kind = TARGET_WAITKIND_STOPPED;

  88.   /* If GDB is connected through TCP/serial, then GDBserver will most
  89.      probably be running on its own terminal/console, so it's nice to
  90.      print there why is GDBserver exiting.  If however, GDB is
  91.      connected through stdio, then there's no need to spam the GDB
  92.      console with this -- the user will already see the exit through
  93.      regular GDB output, in that same terminal.  */
  94.   if (!remote_connection_is_stdio ())
  95.     {
  96.       if (ourstatus->kind == TARGET_WAITKIND_EXITED)
  97.         fprintf (stderr,
  98.                  "\nChild exited with status %d\n", ourstatus->value.integer);
  99.       else if (ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
  100.         fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n",
  101.                  gdb_signal_to_host (ourstatus->value.sig),
  102.                  gdb_signal_to_name (ourstatus->value.sig));
  103.     }

  104.   if (connected_wait)
  105.     server_waiting = 0;

  106.   return ret;
  107. }

  108. /* See target/target.h.  */

  109. void
  110. target_stop_and_wait (ptid_t ptid)
  111. {
  112.   struct target_waitstatus status;
  113.   int was_non_stop = non_stop;
  114.   struct thread_resume resume_info;

  115.   resume_info.thread = ptid;
  116.   resume_info.kind = resume_stop;
  117.   resume_info.sig = GDB_SIGNAL_0;
  118.   (*the_target->resume) (&resume_info, 1);

  119.   non_stop = 1;
  120.   mywait (ptid, &status, 0, 0);
  121.   non_stop = was_non_stop;
  122. }

  123. /* See target/target.h.  */

  124. void
  125. target_continue_no_signal (ptid_t ptid)
  126. {
  127.   struct thread_resume resume_info;

  128.   resume_info.thread = ptid;
  129.   resume_info.kind = resume_continue;
  130.   resume_info.sig = GDB_SIGNAL_0;
  131.   (*the_target->resume) (&resume_info, 1);
  132. }

  133. int
  134. start_non_stop (int nonstop)
  135. {
  136.   if (the_target->start_non_stop == NULL)
  137.     {
  138.       if (nonstop)
  139.         return -1;
  140.       else
  141.         return 0;
  142.     }

  143.   return (*the_target->start_non_stop) (nonstop);
  144. }

  145. void
  146. set_target_ops (struct target_ops *target)
  147. {
  148.   the_target = (struct target_ops *) xmalloc (sizeof (*the_target));
  149.   memcpy (the_target, target, sizeof (*the_target));
  150. }

  151. /* Convert pid to printable format.  */

  152. const char *
  153. target_pid_to_str (ptid_t ptid)
  154. {
  155.   static char buf[80];

  156.   if (ptid_equal (ptid, minus_one_ptid))
  157.     xsnprintf (buf, sizeof (buf), "<all threads>");
  158.   else if (ptid_equal (ptid, null_ptid))
  159.     xsnprintf (buf, sizeof (buf), "<null thread>");
  160.   else if (ptid_get_tid (ptid) != 0)
  161.     xsnprintf (buf, sizeof (buf), "Thread %d.0x%lx",
  162.                ptid_get_pid (ptid), ptid_get_tid (ptid));
  163.   else if (ptid_get_lwp (ptid) != 0)
  164.     xsnprintf (buf, sizeof (buf), "LWP %d.%ld",
  165.                ptid_get_pid (ptid), ptid_get_lwp (ptid));
  166.   else
  167.     xsnprintf (buf, sizeof (buf), "Process %d",
  168.                ptid_get_pid (ptid));

  169.   return buf;
  170. }

  171. int
  172. kill_inferior (int pid)
  173. {
  174.   gdb_agent_about_to_close (pid);

  175.   return (*the_target->kill) (pid);
  176. }